2026-10-28 18:45:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Engine;
|
|
|
|
|
|
|
|
|
|
use App\Game\GameRuleViolation;
|
2026-10-29 00:51:37 +00:00
|
|
|
use App\Game\Map\Frontier;
|
|
|
|
|
use App\Game\Map\MapBounds;
|
2026-10-28 18:45:02 +00:00
|
|
|
use App\Models\Player;
|
|
|
|
|
use App\Models\Village;
|
|
|
|
|
use App\Models\World;
|
|
|
|
|
|
|
|
|
|
/** Creates villages for players and barbarians, and finds free map coordinates for them. */
|
|
|
|
|
final class VillageFactory
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* A village with the given building levels (type => level, one instance each),
|
|
|
|
|
* start resources and the matching points.
|
|
|
|
|
*
|
|
|
|
|
* @param array<string, int> $levels
|
|
|
|
|
*/
|
|
|
|
|
public function create(World $world, ?Player $player, string $name, int $x, int $y, array $levels): Village
|
|
|
|
|
{
|
|
|
|
|
$config = $world->gameConfig();
|
|
|
|
|
$village = Village::create([
|
|
|
|
|
'world_id' => $world->id,
|
|
|
|
|
'player_id' => $player?->id,
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'x' => $x,
|
|
|
|
|
'y' => $y,
|
|
|
|
|
'space' => $config->get('world.village.space'),
|
|
|
|
|
...$config->get('world.village.start_resources'),
|
|
|
|
|
'resources_at' => now()->toImmutable(),
|
|
|
|
|
]);
|
|
|
|
|
foreach ($levels as $type => $level) {
|
|
|
|
|
$village->buildings()->create(['type' => $type, 'level' => $level]);
|
|
|
|
|
}
|
|
|
|
|
$village->update(['points' => Simulation::of($village)->points()]);
|
|
|
|
|
|
|
|
|
|
return $village;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 20:32:09 +00:00
|
|
|
/**
|
2026-10-29 00:51:37 +00:00
|
|
|
* Where a new player starts: on the frontier, which moves outward as the world fills,
|
|
|
|
|
* in the direction they picked. Falls back to the nearest free field if it has no room.
|
2026-10-28 20:32:09 +00:00
|
|
|
*
|
2026-10-29 00:51:37 +00:00
|
|
|
* @param array<string, bool> $taken "x|y" => whether a player owns it, updated with the result
|
2026-10-28 20:32:09 +00:00
|
|
|
* @return array{0: int, 1: int}
|
|
|
|
|
*/
|
2026-10-29 00:51:37 +00:00
|
|
|
public static function spawnSpot(World $world, array &$taken, string $direction = 'random'): array
|
2026-10-28 20:32:09 +00:00
|
|
|
{
|
2026-10-29 00:51:37 +00:00
|
|
|
$frontier = new Frontier($world->gameConfig());
|
|
|
|
|
$spot = $frontier->pick(count($taken), $taken, $direction, self::random(...)) ?? self::freeSpot($world, $taken);
|
2026-10-28 20:32:09 +00:00
|
|
|
$taken["{$spot[0]}|{$spot[1]}"] = true;
|
|
|
|
|
|
|
|
|
|
return $spot;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-28 18:45:02 +00:00
|
|
|
/**
|
2026-10-29 00:51:37 +00:00
|
|
|
* First free field on square rings around 0|0.
|
2026-10-28 18:45:02 +00:00
|
|
|
*
|
2026-10-29 00:51:37 +00:00
|
|
|
* @param array<string, bool> $taken
|
2026-10-28 18:45:02 +00:00
|
|
|
* @return array{0: int, 1: int}
|
|
|
|
|
*/
|
2026-10-29 00:51:37 +00:00
|
|
|
public static function freeSpot(World $world, array $taken): array
|
2026-10-28 18:45:02 +00:00
|
|
|
{
|
2026-10-29 00:51:37 +00:00
|
|
|
$bounds = MapBounds::of($world->gameConfig());
|
|
|
|
|
for ($r = 0; $r <= $bounds->max(); $r++) {
|
2026-10-28 18:45:02 +00:00
|
|
|
for ($dy = -$r; $dy <= $r; $dy++) {
|
|
|
|
|
for ($dx = -$r; $dx <= $r; $dx++) {
|
2026-10-29 00:51:37 +00:00
|
|
|
if (max(abs($dx), abs($dy)) === $r && ! isset($taken["$dx|$dy"]) && $bounds->contains($dx, $dy)) {
|
|
|
|
|
return [$dx, $dy];
|
2026-10-28 18:45:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new GameRuleViolation('world_full', 'This world is full.');
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-29 00:51:37 +00:00
|
|
|
/** @return array<string, bool> "x|y" => whether a player owns the village there */
|
2026-10-28 18:45:02 +00:00
|
|
|
public static function takenSpots(World $world): array
|
|
|
|
|
{
|
2026-10-29 00:51:37 +00:00
|
|
|
return $world->villages()->get(['x', 'y', 'player_id'])
|
|
|
|
|
->mapWithKeys(fn (Village $v) => ["{$v->x}|{$v->y}" => $v->player_id !== null])
|
|
|
|
|
->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Uniform in [0, 1). */
|
|
|
|
|
public static function random(): float
|
|
|
|
|
{
|
|
|
|
|
return mt_rand() / (mt_getrandmax() + 1);
|
2026-10-28 18:45:02 +00:00
|
|
|
}
|
|
|
|
|
}
|