game-village-politics-backend/tests/Unit/Game/FrontierTest.php

52 lines
2.2 KiB
PHP
Raw Normal View History

<?php
use App\Game\Map\Frontier;
/** A random source that repeats the given values. */
function sequence(float ...$values): Closure
{
$i = 0;
return function () use ($values, &$i) {
return $values[$i++ % count($values)];
};
}
it('grows the settled disc with all villages, at Die Stämme density', function () {
$frontier = new Frontier(gameConfig());
expect($frontier->radius(0))->toBe(0.0)
->and($frontier->radius(37346))->toEqualWithDelta(202.5, 0.1)
->and($frontier->radius(100))->toEqualWithDelta(sqrt(100 / (M_PI * 0.29)), 1e-9);
});
it('starts the world at 0|0 and keeps new players inside the quarter they picked', function () {
$frontier = new Frontier(gameConfig());
// No offset from the ring: the first village sits on 0|0; with 100 villages the ring is ~10.5 out.
expect($frontier->pick(0, [], 'random', sequence(0.0, 0.5)))->toBe([0, 0])
->and($frontier->pick(100, [], 'random', sequence(0.0, 0.5)))->toBe([10, 0])
->and($frontier->pick(100, [], 'ne', sequence(0.0, 0.5)))->toBe([0, -10])
->and($frontier->pick(100, [], 'sw', sequence(0.5, 0.5)))->toBe([-7, 7]);
});
it('keeps clear of other players for as long as it can, but not of barbarians', function () {
$frontier = new Frontier(gameConfig());
expect($frontier->pick(100, ['11|1' => false], 'random', sequence(0.0, 0.5)))->toBe([10, 0])
// Next to a player every try lands in the same spot, so the clear tries run out first.
->and($frontier->pick(100, ['11|1' => true], 'random', sequence(0.0, 0.5)))->toBe([10, 0])
->and($frontier->pick(100, ['10|0' => false], 'random', sequence(0.0, 0.5)))->toBeNull();
});
it('places barbarians a few fields from the new village and brings 1.3 per join on average', function () {
$frontier = new Frontier(gameConfig());
// Angle 0, distance 1.5 + 0.5 × 2.5 = 2.75: three fields east.
expect($frontier->near(5, 5, [], sequence(0.0, 0.5)))->toBe([8, 5])
// A wave reaches further: 1.5 + 0.5 × 6.5 = 4.75, five fields east.
->and($frontier->near(5, 5, [], sequence(0.0, 0.5), 8.0))->toBe([10, 5])
->and($frontier->barbariansPerJoin(sequence(0.29)))->toBe(2)
->and($frontier->barbariansPerJoin(sequence(0.31)))->toBe(1);
});