158 lines
6.0 KiB
PHP
158 lines
6.0 KiB
PHP
<?php
|
||
|
||
namespace App\Actions;
|
||
|
||
use App\Engine\Barbarians;
|
||
use App\Engine\Raiders;
|
||
use App\Engine\VillageFactory;
|
||
use App\Game\Map\BarbarianGrowth;
|
||
use App\Game\Map\Frontier;
|
||
use App\Models\GameEvent;
|
||
use App\Models\Village;
|
||
use App\Models\World;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* Barbarian villages come with the players, as in Die Stämme: each joining player brings
|
||
* `barbarians.per_join` of them on average, here placed within a few fields of the new
|
||
* village so even a small world has something to loot from the start. They then grow
|
||
* by themselves (BarbarianGrowth). Waves (Engine\Waves) bring more near the active
|
||
* players every few world days.
|
||
*/
|
||
final class SpawnBarbarians
|
||
{
|
||
private const BUILDINGS = ['headquarters', 'storage', 'farm', 'timber_camp', 'clay_pit', 'iron_mine'];
|
||
|
||
public function __construct(private readonly VillageFactory $villages) {}
|
||
|
||
/**
|
||
* Barbarian villages around a new player's village, each with its first growth step.
|
||
*
|
||
* @param array<string, bool> $taken updated with the new villages
|
||
* @return list<Village>
|
||
*/
|
||
public function around(World $world, Village $village, array &$taken): array
|
||
{
|
||
$created = [];
|
||
for ($n = (new Frontier($world->gameConfig()))->barbariansPerJoin(VillageFactory::random(...)); $n > 0; $n--) {
|
||
$barbarian = $this->near($world, $village, $taken);
|
||
if (! $barbarian) {
|
||
break;
|
||
}
|
||
$created[] = $barbarian;
|
||
}
|
||
|
||
return $created;
|
||
}
|
||
|
||
/**
|
||
* One barbarian village on a free field near a player's village, within $reach fields
|
||
* (`barbarians.near` unless given): buildings at random levels up to `max_level`, a
|
||
* full garrison and its first growth step. Null when no field near enough is free.
|
||
*
|
||
* @param array<string, bool> $taken updated with the new village
|
||
*/
|
||
public function near(World $world, Village $village, array &$taken, ?float $reach = null): ?Village
|
||
{
|
||
$config = $world->gameConfig();
|
||
$spot = (new Frontier($config))->near($village->x, $village->y, $taken, VillageFactory::random(...), $reach);
|
||
if (! $spot) {
|
||
return null;
|
||
}
|
||
$taken["{$spot[0]}|{$spot[1]}"] = false;
|
||
$maxLevel = $config->get('world.map.barbarians.max_level', 3);
|
||
$levels = [];
|
||
foreach (self::BUILDINGS as $type) {
|
||
$levels[$type] = mt_rand(1, $maxLevel);
|
||
}
|
||
$barbarian = $this->villages->create($world, null, 'Barbarian village', $spot[0], $spot[1], $levels);
|
||
Barbarians::of($barbarian)->fill();
|
||
$this->scheduleFirstGrowth($world, $barbarian);
|
||
|
||
return $barbarian;
|
||
}
|
||
|
||
/**
|
||
* Give every barbarian village without one a pending growth event, spread over one
|
||
* interval so they don't all grow at once. Safe to run on every deploy.
|
||
*
|
||
* @return int events scheduled
|
||
*/
|
||
public function scheduleGrowth(World $world): int
|
||
{
|
||
$waiting = GameEvent::where('type', GameEvent::BARBARIAN_GROWS)->whereNull('processed_at')->select('village_id');
|
||
$villages = $world->villages()->whereNull('player_id')->where('crown', false)->whereNotIn('id', $waiting)->get();
|
||
foreach ($villages as $village) {
|
||
$this->scheduleFirstGrowth($world, $village);
|
||
}
|
||
|
||
return $villages->count();
|
||
}
|
||
|
||
/**
|
||
* Give every barbarian village its garrison: full strength at once for one that never
|
||
* had any (from before garrisons), a regroup step for one that is short without one
|
||
* waiting. Safe to run on every deploy.
|
||
*
|
||
* @return int villages that got troops or a regroup step
|
||
*/
|
||
public function scheduleGarrisons(World $world): int
|
||
{
|
||
$now = now()->toImmutable();
|
||
$changed = 0;
|
||
foreach ($world->villages()->whereNull('player_id')->pluck('id') as $id) {
|
||
$changed += DB::transaction(function () use ($id, $now) {
|
||
$village = Village::whereKey($id)->lockForUpdate()->first();
|
||
if (! $village || $village->player_id !== null) {
|
||
return 0;
|
||
}
|
||
$barbarians = Barbarians::of($village);
|
||
$waiting = GameEvent::where('type', GameEvent::BARBARIAN_REGROUPS)->where('village_id', $id)->whereNull('processed_at')->exists();
|
||
if ($barbarians->current() === [] && ! $waiting) {
|
||
$barbarians->fill();
|
||
|
||
return $barbarians->current() === [] ? 0 : 1;
|
||
}
|
||
if (! $waiting && ! $barbarians->rules()->isFull($barbarians->current(), $barbarians->full())) {
|
||
$barbarians->scheduleRegroup($now);
|
||
|
||
return 1;
|
||
}
|
||
|
||
return 0;
|
||
});
|
||
}
|
||
|
||
return $changed;
|
||
}
|
||
|
||
/**
|
||
* Plan a raid for every player village without one, from now or the end of its
|
||
* owner's beginner protection. Safe to run on every deploy.
|
||
*/
|
||
public function scheduleRaids(World $world): void
|
||
{
|
||
$now = now()->toImmutable();
|
||
foreach ($world->villages()->whereNotNull('player_id')->pluck('id') as $id) {
|
||
DB::transaction(function () use ($id, $now) {
|
||
$village = Village::whereKey($id)->lockForUpdate()->first();
|
||
if ($village?->player_id !== null) {
|
||
Raiders::of($village)->ensurePlanned($now);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
/** The first growth step lands 0.5–1.5 intervals out, so villages don't grow in step. */
|
||
private function scheduleFirstGrowth(World $world, Village $village): void
|
||
{
|
||
$interval = (new BarbarianGrowth($world->gameConfig()))->interval();
|
||
GameEvent::create([
|
||
'world_id' => $world->id,
|
||
'village_id' => $village->id,
|
||
'type' => GameEvent::BARBARIAN_GROWS,
|
||
'fire_at' => now()->toImmutable()->addSeconds((int) round($interval * (0.5 + VillageFactory::random()))),
|
||
]);
|
||
}
|
||
}
|