2026-10-29 15:07:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Engine;
|
|
|
|
|
|
2026-10-31 10:05:05 +00:00
|
|
|
use App\Game\Combat\CombatResolver;
|
2026-10-29 15:07:54 +00:00
|
|
|
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,
|
2026-10-31 10:05:05 +00:00
|
|
|
* so the village sees it coming hours ahead, unless the band would be beaten hollow;
|
2026-10-31 10:06:24 +00:00
|
|
|
* the next raid is planned at once. Runs with the village locked; the camp is only read.
|
2026-10-29 15:07:54 +00:00
|
|
|
*/
|
|
|
|
|
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;
|
2026-10-29 15:31:34 +00:00
|
|
|
if ($this->village->player_id === null || ! $this->raids->enabled() || $this->village->world->ended_at) {
|
2026-10-29 15:07:54 +00:00
|
|
|
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()));
|
2026-10-31 10:05:05 +00:00
|
|
|
// The camp sizes the village up first; against a defence far too strong it stays home.
|
|
|
|
|
if (! $this->dares($units, $wall, $arrives)) {
|
|
|
|
|
$this->schedule($at->addSeconds($interval));
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-10-29 15:07:54 +00:00
|
|
|
$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());
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-31 10:05:05 +00:00
|
|
|
/**
|
|
|
|
|
* The battle on paper, as the full band would fight it on arrival: no luck, full
|
|
|
|
|
* morale, against everyone in the village now, its wall, a military village's bonus
|
|
|
|
|
* and the night at arrival.
|
|
|
|
|
*
|
|
|
|
|
* @param array<string, int> $units
|
|
|
|
|
*/
|
|
|
|
|
private function dares(array $units, int $wall, CarbonImmutable $arrives): bool
|
|
|
|
|
{
|
|
|
|
|
$combat = new CombatResolver($this->sim->config());
|
|
|
|
|
$fort = 1 + $this->sim->specialisation()->defenseBonus($this->sim->role());
|
|
|
|
|
$battle = $combat->fight($units, Rally::of($this->village, $this->sim)->defense(), $wall, 1.0, 0.0, $combat->night($arrives) * $fort);
|
|
|
|
|
|
|
|
|
|
return $this->raids->dares($battle);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-29 15:31:34 +00:00
|
|
|
/** The nearest barbarian village in range; crown forts never raid. */
|
2026-10-29 15:07:54 +00:00
|
|
|
private function camp(): ?Village
|
|
|
|
|
{
|
|
|
|
|
$range = $this->raids->range();
|
2026-10-29 15:31:34 +00:00
|
|
|
$camps = Village::where('world_id', $this->village->world_id)->whereNull('player_id')->where('crown', false)
|
2026-10-29 15:07:54 +00:00
|
|
|
->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,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|