game-village-politics-backend/app/Engine/Raiders.php

142 lines
5.2 KiB
PHP

<?php
namespace App\Engine;
use App\Game\Combat\Raids;
use App\Game\Travel\TravelTime;
use App\Models\GameEvent;
use App\Models\Movement;
use App\Models\Village;
use Carbon\CarbonImmutable;
/**
* Raids on one player village (architecture.md, "Raids"). A `raid.planned` event on the
* village picks the nearest barbarian camp and sends a band that gathers there first,
* so the village sees it coming hours ahead; the next raid is planned at once. Runs
* with the village locked; the camp is only read.
*/
final class Raiders
{
private Raids $raids;
private function __construct(private readonly Village $village, private readonly Simulation $sim)
{
$this->raids = new Raids($sim->config());
}
public static function of(Village $village, ?Simulation $sim = null): self
{
return new self($village, $sim ?? Simulation::of($village));
}
public function rules(): Raids
{
return $this->raids;
}
/** Make sure a raid is planned, no earlier than $earliest or the end of beginner protection. */
public function ensurePlanned(CarbonImmutable $earliest): void
{
if ($this->village->player_id === null || ! $this->raids->enabled()) {
return;
}
$waiting = GameEvent::where('type', GameEvent::RAID_PLANNED)->where('village_id', $this->village->id)->whereNull('processed_at')->exists();
if (! $waiting) {
$protected = Rally::of($this->village, $this->sim)->protectedUntil($this->village->player);
$this->schedule($protected->greaterThan($earliest) ? $protected : $earliest);
}
}
/** The `raid.planned` handler: send a band from the nearest camp and plan the next raid. */
public function plan(GameEvent $event): void
{
$at = $event->fire_at;
if ($this->village->player_id === null || ! $this->raids->enabled()) {
return;
}
$protected = Rally::of($this->village, $this->sim)->protectedUntil($this->village->player);
if ($protected->greaterThan($at)) {
$this->schedule($protected);
return;
}
// Seeded per event, so a replayed event draws the same interval.
mt_srand($event->id);
$interval = $this->raids->interval(mt_rand() / (mt_getrandmax() + 1));
$camp = $this->camp();
$wall = $this->village->buildings->firstWhere('type', 'wall')?->level ?? 0;
$units = $camp ? $this->raids->band(Barbarians::of($camp)->profile(), (int) $this->village->points, $wall) : [];
if (! $units) {
$this->schedule($at->addSeconds($interval));
return;
}
$travel = new TravelTime($this->sim->config());
$marches = $at->addSeconds($this->raids->gatherSeconds());
$arrives = $marches->addSeconds($travel->seconds($units, TravelTime::distance($camp->x, $camp->y, $this->village->x, $this->village->y), $this->raids->mode()));
$movement = Movement::create([
'world_id' => $this->village->world_id,
'player_id' => null,
'type' => Movement::ATTACK,
'mode' => $this->raids->mode(),
'origin_id' => $camp->id,
'target_id' => $this->village->id,
'units' => $units,
'rng_seed' => mt_rand(1, 2_000_000_000),
// It gathers until it marches, and is seen from the start.
'sent_at' => $marches,
'arrives_at' => $arrives,
'visible_at' => $at,
]);
GameEvent::create([
'world_id' => $this->village->world_id,
'village_id' => $this->village->id,
'type' => GameEvent::MOVEMENT_ARRIVES,
'subject_id' => $movement->id,
'fire_at' => $arrives,
]);
Push::queue($this->village->id);
$this->schedule($arrives->addSeconds($interval));
}
/**
* The band as it reaches the village: as strong as its camp is now. Empty when it
* scatters (the camp was beaten down or changed hands).
*
* @return array<string, int>
*/
public function muster(Movement $movement): array
{
$camp = Village::find($movement->origin_id);
if (! $camp || $camp->player_id !== null) {
return [];
}
return $this->raids->muster($movement->units, Barbarians::of($camp)->strength());
}
/** The nearest barbarian village in range. */
private function camp(): ?Village
{
$range = $this->raids->range();
$camps = Village::where('world_id', $this->village->world_id)->whereNull('player_id')
->whereBetween('x', [$this->village->x - $range, $this->village->x + $range])
->whereBetween('y', [$this->village->y - $range, $this->village->y + $range])
->get(['id', 'x', 'y']);
$id = $this->raids->nearest($camps->map(fn (Village $v) => ['id' => $v->id, 'x' => $v->x, 'y' => $v->y]), $this->village->x, $this->village->y);
return $id ? Village::find($id) : null;
}
private function schedule(CarbonImmutable $at): void
{
GameEvent::create([
'world_id' => $this->village->world_id,
'village_id' => $this->village->id,
'type' => GameEvent::RAID_PLANNED,
'fire_at' => $at,
]);
}
}