124 lines
4.5 KiB
PHP
124 lines
4.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Game\Map;
|
||
|
|
|
||
|
|
use App\Game\WorldConfig;
|
||
|
|
use Closure;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Where the world grows, the way Die Stämme does it: villages settle on a ring around
|
||
|
|
* 0|0 whose radius follows the number of all villages (players and barbarians), so the
|
||
|
|
* settled disc keeps an even density and spreads outward as players join. A new player
|
||
|
|
* lands at a random angle on that ring, inside the quarter they chose.
|
||
|
|
*/
|
||
|
|
final class Frontier
|
||
|
|
{
|
||
|
|
/** Start directions a joining player can pick. */
|
||
|
|
public const DIRECTIONS = ['random', 'nw', 'ne', 'sw', 'se'];
|
||
|
|
|
||
|
|
/** Degrees per direction, with 0° pointing east and y growing south. */
|
||
|
|
private const SECTORS = ['random' => [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<string, bool> $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<string, bool> $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<string, bool> $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;
|
||
|
|
}
|
||
|
|
}
|