170 lines
5.6 KiB
PHP
170 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Engine;
|
|
|
|
use App\Game\Military\UnitRules;
|
|
use App\Models\GameEvent;
|
|
use App\Models\UnitStack;
|
|
use App\Models\Village;
|
|
use Carbon\CarbonImmutable;
|
|
|
|
/**
|
|
* Food running out and what follows (architecture.md, "Starvation"). sync() runs after
|
|
* every mutation and event: it keeps a food.runs_out event at the moment the stock
|
|
* hits 0, starts the grace timer once the village starves, and stops it as soon as
|
|
* income covers upkeep again. wave() is one desertion wave.
|
|
*/
|
|
final class Starvation
|
|
{
|
|
/** Food at or below this counts as none, so float rest can't hold off starvation. */
|
|
private const EMPTY = 0.001;
|
|
|
|
private function __construct(private readonly Village $village, private readonly Simulation $sim) {}
|
|
|
|
public static function of(Village $village, ?Simulation $sim = null): self
|
|
{
|
|
return new self($village, $sim ?? Simulation::of($village));
|
|
}
|
|
|
|
public function sync(CarbonImmutable $at): void
|
|
{
|
|
$this->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: 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<string, int> what left, by unit; `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());
|
|
$stacks = $this->village->troops
|
|
->where('home_village_id', $this->village->id)
|
|
->sortByDesc(fn (UnitStack $s) => $units->upkeep($s->unit));
|
|
$gone = [];
|
|
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,
|
|
]);
|
|
}
|
|
}
|