sim->config()); } /** Beds of all hospitals here, each as far as it is staffed. */ public function beds(): int { $rules = $this->sim->rules(); $workforce = $this->sim->workforce(); return (int) floor($this->village->buildings->sum(function (Building $b) use ($rules, $workforce) { $beds = $rules->beds($b->type, $b->level); return $beds > 0 ? $beds * $this->sim->staffingFactor($workforce->ratio($b->id)) : 0; })); } /** Beds the wounded lie in now. */ public function occupied(): int { return $this->village->wounded->sum(fn (Wounded $w) => array_sum($w->units)); } /** * Take in the wounded among the own units that fell defending here at $at. Barbarian * villages keep no hospital. * * @param array $dead * @return Wounded|null the wounded of this battle, if any */ public function admit(array $dead, CarbonImmutable $at): ?Wounded { if ($this->village->player_id === null) { return null; } $wounded = $this->rules()->admit($dead, max(0, $this->beds() - $this->occupied())); if (! $wounded) { return null; } $batch = $this->village->wounded()->create(['units' => $wounded, 'heals_at' => $at->addSeconds($this->rules()->healSeconds())]); $this->village->wounded->push($batch); GameEvent::create([ 'world_id' => $this->village->world_id, 'village_id' => $this->village->id, 'type' => GameEvent::WOUNDED_HEALED, 'subject_id' => $batch->id, 'fire_at' => $batch->heals_at, ]); return $batch; } /** The wounded healed by $at stand at home again. They ate all along, so upkeep stays. */ public function heal(CarbonImmutable $at): void { $military = Military::of($this->village, $this->sim); $healed = $this->village->wounded->filter(fn (Wounded $w) => ! $w->heals_at->greaterThan($at)); foreach ($healed as $batch) { foreach ($batch->units as $unit => $count) { $military->addTroops($unit, $count); } $batch->delete(); } $this->village->setRelation('wounded', $this->village->wounded->diff($healed)->values()); } /** The wounded don't outlive the owner's rule: they are gone when the village changes hands. */ public static function clear(Village $village): void { foreach ($village->wounded as $batch) { GameEvent::where('type', GameEvent::WOUNDED_HEALED)->where('subject_id', $batch->id)->whereNull('processed_at')->delete(); $batch->delete(); } $village->setRelation('wounded', collect()); } }