garrison = new BarbarianGarrison($sim->config()); } public static function of(Village $village, ?Simulation $sim = null): self { return new self($village, $sim ?? Simulation::of($village)); } public function rules(): BarbarianGarrison { return $this->garrison; } public function profile(): ?string { return $this->garrison->profile($this->village->id); } /** @return array full strength now, from the village's points and food */ public function full(): array { // A crown fort holds its fixed garrison. if ($this->village->crown) { return array_map('intval', $this->sim->config()->get('world.season.garrison', [])); } return $this->garrison->full($this->profile(), (int) $this->village->points, $this->sim->workforce()->foodBudget); } /** @return array */ public function current(): array { return Military::of($this->village, $this->sim)->troopsAtHome(); } /** How much of full strength stands, 0..1. */ public function strength(): float { return $this->garrison->strength($this->current(), $this->full()); } /** Bring the garrison to full strength at once (a new village, or one from before garrisons). */ public function fill(): void { $this->set($this->full()); } /** One regroup step; the next is scheduled while the garrison is still short. */ public function regroup(CarbonImmutable $at): void { if ($this->village->player_id !== null) { return; } $this->sim->settle($at); $this->set($this->garrison->regroup($this->current(), $this->full())); $this->scheduleRegroup($at); } /** Schedule the next regroup step if the garrison is short and none is waiting. */ public function scheduleRegroup(CarbonImmutable $at): void { if ($this->village->player_id !== null || $this->garrison->isFull($this->current(), $this->full())) { return; } $waiting = GameEvent::where('type', GameEvent::BARBARIAN_REGROUPS)->where('village_id', $this->village->id)->whereNull('processed_at')->exists(); if (! $waiting) { GameEvent::create([ 'world_id' => $this->village->world_id, 'village_id' => $this->village->id, 'type' => GameEvent::BARBARIAN_REGROUPS, 'fire_at' => $at->addSeconds($this->garrison->interval()), ]); } } /** Raiders coming home stay only as far as the garrison has room for them. */ public function trim(): void { $full = $this->full(); $kept = []; foreach ($this->current() as $unit => $count) { $kept[$unit] = min($count, $full[$unit] ?? 0); } $this->set($kept); } /** @param array $units the garrison's own troops from now on */ private function set(array $units): void { $own = $this->village->troops->filter(fn (UnitStack $s) => $s->home_village_id === $this->village->id); foreach ($own as $stack) { $count = $units[$stack->unit] ?? 0; if ($count > 0) { $stack->update(['count' => $count]); } else { $stack->delete(); } unset($units[$stack->unit]); } $kept = $this->village->troops->filter(fn (UnitStack $s) => $s->exists); foreach (array_filter($units) as $unit => $count) { $kept->push($this->village->troops()->create(['home_village_id' => $this->village->id, 'unit' => $unit, 'count' => $count])); } $this->village->setRelation('troops', $kept->values()); } }