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() || $this->village->world->ended_at) { 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())); // 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; } $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 */ 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 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 $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); } /** The nearest barbarian village in range; crown forts never raid. */ private function camp(): ?Village { $range = $this->raids->range(); $camps = Village::where('world_id', $this->village->world_id)->whereNull('player_id')->where('crown', false) ->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, ]); } }