[0, 360], 'se' => [0, 90], 'sw' => [90, 180], 'nw' => [180, 270], 'ne' => [270, 360]]; /** Tries that keep clear of other players before any free field will do. */ private const CLEAR_TRIES = 200; private const TRIES = 400; public function __construct(private readonly WorldConfig $config) {} /** Radius in fields of the settled disc when the world has this many villages. */ public function radius(int $villages): float { $spawn = $this->config->get('world.map.spawn'); return $spawn['start_radius'] + sqrt($villages / (M_PI * $spawn['density'])); } /** * A free field near the ring for a new player, in the given direction. The first tries * want no player village on the 8 fields around it; barbarians may stand right next * to it, as in Die Stämme. Each failed try widens the band around the ring a little. * * @param array $taken "x|y" => whether a player owns the village there * @param Closure(): float $random uniform in [0, 1) * @return array{0: int, 1: int}|null */ public function pick(int $villages, array $taken, string $direction, Closure $random): ?array { $bounds = MapBounds::of($this->config); [$from, $to] = self::SECTORS[$direction] ?? self::SECTORS['random']; $radius = $this->radius($villages); $band = $this->config->get('world.map.spawn.band', 3); for ($try = 0; $try < self::TRIES; $try++) { $angle = deg2rad($from + $random() * ($to - $from)); $distance = max(0, $radius + ($random() * 2 - 1) * ($band + $try / 30)); $x = (int) round(cos($angle) * $distance); $y = (int) round(sin($angle) * $distance); if (! $bounds->contains($x, $y) || isset($taken["$x|$y"])) { continue; } if ($try < self::CLEAR_TRIES && self::nextToPlayer($x, $y, $taken)) { continue; } return [$x, $y]; } return null; } /** * A free field for a barbarian village close to a new player's village: between 1.5 * fields and `barbarians.near` fields away, at a random angle. * * @param array $taken * @param Closure(): float $random * @return array{0: int, 1: int}|null */ public function near(int $x, int $y, array $taken, Closure $random): ?array { $bounds = MapBounds::of($this->config); $reach = (float) $this->config->get('world.map.barbarians.near', 4); for ($try = 0; $try < self::TRIES; $try++) { $angle = $random() * 2 * M_PI; $distance = 1.5 + $random() * max(0, $reach - 1.5) + $try / 100; $bx = $x + (int) round(cos($angle) * $distance); $by = $y + (int) round(sin($angle) * $distance); if ($bounds->contains($bx, $by) && ! isset($taken["$bx|$by"])) { return [$bx, $by]; } } return null; } /** * How many barbarian villages come with a joining player: `per_join` on average, * e.g. 1.3 is one, plus a second with a 30% chance. * * @param Closure(): float $random */ public function barbariansPerJoin(Closure $random): int { $average = (float) $this->config->get('world.map.barbarians.per_join', 0); return (int) floor($average) + ($random() < $average - floor($average) ? 1 : 0); } /** @param array $taken */ private static function nextToPlayer(int $x, int $y, array $taken): bool { for ($dy = -1; $dy <= 1; $dy++) { for ($dx = -1; $dx <= 1; $dx++) { if (($taken[($x + $dx).'|'.($y + $dy)] ?? false) === true) { return true; } } } return false; } }