sim->settle($at); $starving = $this->starving(); $this->schedule(GameEvent::FOOD_RUNS_OUT, $starving ? null : $this->emptyAt()); if (! $starving) { $this->village->starving_since = null; $this->schedule(GameEvent::DESERTION, null); return; } $this->village->starving_since ??= $at; if (! $this->pending(GameEvent::DESERTION)) { $first = $this->village->starving_since->addSeconds($this->seconds('grace')); $this->schedule(GameEvent::DESERTION, $first->max($at)); } } /** When the stock runs out at the current rates, or null if it doesn't. */ public function emptyAt(): ?CarbonImmutable { $rate = $this->sim->rates()['food']; if ($rate >= 0 || $this->village->food <= self::EMPTY) { return null; } return $this->village->resources_at->addMilliseconds((int) ceil($this->village->food / -$rate * 3_600_000)); } public function nextDesertion(): ?CarbonImmutable { return $this->pending(GameEvent::DESERTION)?->fire_at; } /** * One desertion wave at $at: support stationed here is sent home first, group by * group; then own soldiers at home leave, highest upkeep first, as few as it takes * for income to cover upkeep; if that isn't enough, the levy ends. When the village * still goes hungry, the next wave follows after `wave_every`. * * @return array what left, by unit; `support` for units sent home, `militia` for an ended levy */ public function wave(CarbonImmutable $at): array { $this->sim->settle($at); if (! $this->starving()) { return []; } $units = new UnitRules($this->sim->config()); $gone = []; // Support goes home first, the hungriest group first. $support = $this->village->troops ->filter(fn (UnitStack $s) => $s->home_village_id !== $this->village->id) ->groupBy('home_village_id') ->sortByDesc(fn ($group) => $group->sum(fn (UnitStack $s) => $units->upkeep($s->unit) * $s->count)); foreach ($support as $homeId => $group) { if ($this->fed()) { break; } Rally::of($this->village, $this->sim)->sendHome((int) $homeId, $at); $gone['support'] = ($gone['support'] ?? 0) + $group->sum('count'); } $stacks = $this->village->troops ->where('home_village_id', $this->village->id) ->sortByDesc(fn (UnitStack $s) => $units->upkeep($s->unit)); foreach ($stacks as $stack) { if ($this->fed()) { break; } $gone[$stack->unit] = $this->fewestToFeed($stack); } foreach ($stacks as $stack) { $stack->count > 0 ? $stack->save() : $stack->delete(); } $this->village->setRelation('troops', $this->village->troops->filter(fn (UnitStack $s) => $s->count > 0)->values()); if (! $this->fed() && $this->village->militia > 0) { $gone['militia'] = (int) $this->village->militia; Military::of($this->village, $this->sim)->endLevy($at); } if (! $this->fed()) { $this->schedule(GameEvent::DESERTION, $at->addSeconds($this->seconds('wave_every'))); } return array_filter($gone); } /** Take units out of the stack until the village is fed or the stack is empty; returns how many left. */ private function fewestToFeed(UnitStack $stack): int { $count = $stack->count; $stack->count = 0; if (! $this->fed()) { return $count; } // Feeding never gets worse with fewer soldiers, so search the smallest number. [$low, $high] = [1, $count]; while ($low < $high) { $mid = intdiv($low + $high, 2); $stack->count = $count - $mid; $this->fed() ? $high = $mid : $low = $mid + 1; } $stack->count = $count - $low; return $low; } private function fed(): bool { return $this->sim->rates()['food'] >= 0; } private function starving(): bool { return $this->village->food <= self::EMPTY && ! $this->fed(); } private function seconds(string $key): int { return max(1, (int) ceil($this->sim->config()->get("economy.starvation.$key", 3600) / $this->sim->config()->speed())); } private function pending(string $type): ?GameEvent { return GameEvent::where('type', $type)->where('village_id', $this->village->id)->whereNull('processed_at')->first(); } /** Keep exactly one pending event of this type at $at, or none for null. */ private function schedule(string $type, ?CarbonImmutable $at): void { $pending = $this->pending($type); if (! $at) { $pending?->delete(); return; } if ($pending) { if ($pending->fire_at->getTimestampMs() !== $at->getTimestampMs()) { $pending->update(['fire_at' => $at]); } return; } GameEvent::create([ 'world_id' => $this->village->world_id, 'village_id' => $this->village->id, 'type' => $type, 'subject_id' => $this->village->id, 'fire_at' => $at, ]); } }