2026-10-28 18:17:34 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Engine;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Game\Buildings\BuildingRules;
|
|
|
|
|
|
use App\Game\Buildings\BuildingState;
|
|
|
|
|
|
use App\Game\Buildings\BuildQueue;
|
2026-10-29 15:20:52 +00:00
|
|
|
|
use App\Game\Buildings\Specialisation;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
use App\Game\Economy\Population;
|
|
|
|
|
|
use App\Game\Economy\Production;
|
|
|
|
|
|
use App\Game\Economy\Settle;
|
|
|
|
|
|
use App\Game\Economy\Workforce;
|
|
|
|
|
|
use App\Game\GameRuleViolation;
|
2026-10-28 20:39:48 +00:00
|
|
|
|
use App\Game\Military\UnitRules;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
use App\Game\WorldConfig;
|
|
|
|
|
|
use App\Models\Building;
|
|
|
|
|
|
use App\Models\BuildOrder;
|
|
|
|
|
|
use App\Models\GameEvent;
|
2026-10-28 20:39:48 +00:00
|
|
|
|
use App\Models\RecruitOrder;
|
|
|
|
|
|
use App\Models\UnitStack;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
use App\Models\Village;
|
2026-10-30 12:45:02 +00:00
|
|
|
|
use App\Models\Wounded;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Applies the pure rules in app/Game to one village and its rows. Callers hold the
|
|
|
|
|
|
* village lock; this class changes the loaded models and their relations in step.
|
|
|
|
|
|
*/
|
|
|
|
|
|
final class Simulation
|
|
|
|
|
|
{
|
|
|
|
|
|
private WorldConfig $config;
|
|
|
|
|
|
|
|
|
|
|
|
private BuildingRules $rules;
|
|
|
|
|
|
|
|
|
|
|
|
private Population $population;
|
|
|
|
|
|
|
|
|
|
|
|
private Production $production;
|
|
|
|
|
|
|
2026-10-30 12:07:09 +00:00
|
|
|
|
private ?Workforce $workforce = null;
|
|
|
|
|
|
|
|
|
|
|
|
/** Set while a payload is built: nothing changes, so derived values are computed once. */
|
|
|
|
|
|
private bool $frozen = false;
|
|
|
|
|
|
|
|
|
|
|
|
private ?float $hqStaffing = null;
|
|
|
|
|
|
|
|
|
|
|
|
/** @var array<int, int>|null building id => its index among its type */
|
|
|
|
|
|
private ?array $indexes = null;
|
|
|
|
|
|
|
2026-10-31 09:56:59 +00:00
|
|
|
|
/** @var array<int, float>|null building id => the share its instance works at */
|
|
|
|
|
|
private ?array $shares = null;
|
|
|
|
|
|
|
2026-10-30 12:07:09 +00:00
|
|
|
|
private ?string $workforceFor = null;
|
|
|
|
|
|
|
2026-10-28 18:17:34 +00:00
|
|
|
|
private function __construct(private readonly Village $village)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->config = $village->world->gameConfig();
|
|
|
|
|
|
$this->rules = new BuildingRules($this->config);
|
|
|
|
|
|
$this->population = new Population($this->config);
|
|
|
|
|
|
$this->production = new Production($this->config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 12:07:09 +00:00
|
|
|
|
/** For reading only (the village payload): workforce, staffing and indexes are computed once. */
|
|
|
|
|
|
public function freeze(): self
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->frozen = true;
|
|
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 18:17:34 +00:00
|
|
|
|
public static function of(Village $village): self
|
|
|
|
|
|
{
|
2026-10-30 12:45:02 +00:00
|
|
|
|
return new self($village->loadMissing(['world', 'buildings', 'orders', 'troops', 'recruits', 'wounded']));
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function config(): WorldConfig
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->config;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function rules(): BuildingRules
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->rules;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Economy
|
|
|
|
|
|
|
|
|
|
|
|
/** @return list<BuildingState> */
|
|
|
|
|
|
public function states(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->village->buildings->map(fn (Building $b) => $b->state())->values()->all();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function workforce(): Workforce
|
|
|
|
|
|
{
|
2026-10-30 12:07:09 +00:00
|
|
|
|
// One village state, one workforce: a payload asks for it dozens of times.
|
|
|
|
|
|
if ($this->frozen && $this->workforce) {
|
|
|
|
|
|
return $this->workforce;
|
|
|
|
|
|
}
|
|
|
|
|
|
$key = $this->workforceKey();
|
|
|
|
|
|
if ($key !== $this->workforceFor) {
|
|
|
|
|
|
$this->workforce = $this->population->workforce($this->states(), $this->soldierUpkeep(), (int) $this->village->militia);
|
|
|
|
|
|
$this->workforceFor = $key;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->workforce;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** What the workforce depends on: buildings with their levels and priorities, troops, training, militia. */
|
|
|
|
|
|
private function workforceKey(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
$parts = [(string) (int) $this->village->militia];
|
|
|
|
|
|
foreach ($this->village->buildings as $b) {
|
|
|
|
|
|
$parts[] = "$b->id:$b->level:$b->priority";
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach ($this->village->troops as $s) {
|
|
|
|
|
|
$parts[] = "t$s->id:$s->unit:$s->count";
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach ($this->village->recruits as $o) {
|
|
|
|
|
|
$parts[] = "r$o->id:".($o->count - $o->done);
|
|
|
|
|
|
}
|
2026-10-30 12:45:02 +00:00
|
|
|
|
foreach ($this->village->wounded as $w) {
|
|
|
|
|
|
$parts[] = "w$w->id";
|
|
|
|
|
|
}
|
2026-10-30 12:07:09 +00:00
|
|
|
|
|
|
|
|
|
|
return implode('|', $parts);
|
2026-10-28 20:39:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-30 12:45:02 +00:00
|
|
|
|
* Food per hour eaten by troops standing here, by units still in training and by the
|
|
|
|
|
|
* wounded: a queued unit takes its villager's place at once, a wounded one keeps it.
|
2026-10-28 20:39:48 +00:00
|
|
|
|
*/
|
|
|
|
|
|
public function soldierUpkeep(): float
|
|
|
|
|
|
{
|
|
|
|
|
|
$units = new UnitRules($this->config);
|
|
|
|
|
|
$standing = $this->village->troops->mapWithKeys(fn (UnitStack $s) => [$s->id => $units->upkeep($s->unit) * $s->count])->sum();
|
|
|
|
|
|
$training = $this->village->recruits->sum(fn (RecruitOrder $o) => $units->upkeep($o->unit) * ($o->count - $o->done));
|
2026-10-30 12:45:02 +00:00
|
|
|
|
$wounded = $this->village->wounded->sum(fn (Wounded $w) => array_sum(array_map(
|
|
|
|
|
|
fn (string $unit, int $count) => $units->exists($unit) ? $units->upkeep($unit) * $count : 0,
|
|
|
|
|
|
array_keys($w->units), $w->units,
|
|
|
|
|
|
)));
|
2026-10-28 20:39:48 +00:00
|
|
|
|
|
2026-10-30 12:45:02 +00:00
|
|
|
|
return $standing + $training + $wounded;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return array{wood: float, clay: float, iron: float, food: float} */
|
|
|
|
|
|
public function rates(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->production->rates($this->states(), $this->workforce());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return array{resource: string, per_hour: float}|null */
|
|
|
|
|
|
public function output(Building $building, Workforce $workforce): ?array
|
|
|
|
|
|
{
|
2026-10-31 09:56:59 +00:00
|
|
|
|
return $this->production->output($building->state(), $workforce, $this->specialisation()->productionBonus($this->role(), $building->type), $this->share($building));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The share each standing instance works at: copies of a type give less, ranked by
|
|
|
|
|
|
* level (BuildingRules::instanceShares).
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array<int, float> building id => share
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function shares(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($this->frozen && $this->shares !== null) {
|
|
|
|
|
|
return $this->shares;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->shares = $this->rules->instanceShares($this->states());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function share(Building $building): float
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->shares()[$building->id] ?? 1.0;
|
2026-10-29 15:20:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function specialisation(): Specialisation
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Specialisation($this->config);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** The village's role from the buildings standing, or null. */
|
|
|
|
|
|
public function role(): ?string
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->specialisation()->role($this->village->buildings->pluck('type'));
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function staffingFactor(float $ratio): float
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->production->staffingFactor($ratio);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function storageCap(): float
|
|
|
|
|
|
{
|
2026-10-31 09:56:59 +00:00
|
|
|
|
$shares = $this->shares();
|
|
|
|
|
|
|
|
|
|
|
|
return $this->village->buildings->sum(fn (Building $b) => $this->rules->capacity($b->type, $b->level) * ($shares[$b->id] ?? 1.0));
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Write stored + rate × elapsed up to $at into the village. Call before any change. */
|
|
|
|
|
|
public function settle(CarbonImmutable $at): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$from = $this->village->resources_at;
|
|
|
|
|
|
if ($at->lessThanOrEqualTo($from)) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
$hours = ($at->getTimestampMs() - $from->getTimestampMs()) / 3_600_000;
|
|
|
|
|
|
$this->village->fill(Settle::advance($this->village->resources(), $this->rates(), $this->storageCap(), $hours));
|
|
|
|
|
|
$this->village->resources_at = $at;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @param array{wood: int, clay: int, iron: int} $cost */
|
|
|
|
|
|
public function canPay(array $cost): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->village->wood >= $cost['wood']
|
|
|
|
|
|
&& $this->village->clay >= $cost['clay']
|
|
|
|
|
|
&& $this->village->iron >= $cost['iron'];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @param array{wood: int, clay: int, iron: int} $cost */
|
|
|
|
|
|
public function pay(array $cost): void
|
|
|
|
|
|
{
|
|
|
|
|
|
if (! $this->canPay($cost)) {
|
|
|
|
|
|
throw new GameRuleViolation('resources', 'Not enough resources.');
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach ($cost as $resource => $amount) {
|
|
|
|
|
|
$this->village->{$resource} -= $amount;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @param array<string, int> $amount */
|
|
|
|
|
|
public function refund(array $amount): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$cap = $this->storageCap();
|
|
|
|
|
|
foreach ($amount as $resource => $n) {
|
|
|
|
|
|
$this->village->{$resource} = min($cap, $this->village->{$resource} + $n);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Buildings
|
|
|
|
|
|
|
|
|
|
|
|
public function hqLevel(): int
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->village->buildings->firstWhere('type', 'headquarters')?->level ?? 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Output factor of the headquarters' staffing; it scales build speed. */
|
|
|
|
|
|
public function hqStaffing(): float
|
|
|
|
|
|
{
|
2026-10-30 12:07:09 +00:00
|
|
|
|
if ($this->frozen && $this->hqStaffing !== null) {
|
|
|
|
|
|
return $this->hqStaffing;
|
|
|
|
|
|
}
|
2026-10-28 18:17:34 +00:00
|
|
|
|
$hq = $this->village->buildings->firstWhere('type', 'headquarters');
|
|
|
|
|
|
|
2026-10-30 12:07:09 +00:00
|
|
|
|
return $this->hqStaffing = $hq ? $this->production->staffingFactor($this->workforce()->ratio($hq->id)) : $this->production->staffingFactor(0);
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function builders(): int
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->rules->builders($this->hqLevel());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Instances of a type, counting new ones still in the queue. */
|
|
|
|
|
|
public function instances(string $type): int
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->village->buildings->where('type', $type)->count()
|
|
|
|
|
|
+ $this->village->orders->where('action', BuildOrder::BUILD)->where('type', $type)->count();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 09:58:56 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* 0-based rank among same-type instances, by level like the share (BuildingRules::
|
|
|
|
|
|
* instanceRanks), so the copy that works at less is also the one that pays more;
|
|
|
|
|
|
* a new instance comes after all of them.
|
|
|
|
|
|
*/
|
2026-10-28 18:17:34 +00:00
|
|
|
|
public function instanceIndex(string $type, ?Building $building = null): int
|
|
|
|
|
|
{
|
|
|
|
|
|
if (! $building) {
|
|
|
|
|
|
return $this->instances($type);
|
|
|
|
|
|
}
|
2026-10-31 09:58:56 +00:00
|
|
|
|
if (! $this->frozen || $this->indexes === null) {
|
|
|
|
|
|
$this->indexes = $this->rules->instanceRanks($this->states());
|
2026-10-30 12:07:09 +00:00
|
|
|
|
}
|
2026-10-28 18:17:34 +00:00
|
|
|
|
|
2026-10-31 09:58:56 +00:00
|
|
|
|
return $this->indexes[$building->id] ?? 0;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 21:26:25 +00:00
|
|
|
|
/** Level after everything already queued for this instance; 0 if it is being removed. */
|
2026-10-28 18:17:34 +00:00
|
|
|
|
public function queuedLevel(Building $building): int
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->village->orders->where('building_id', $building->id)->last()?->target_level ?? $building->level;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 18:45:02 +00:00
|
|
|
|
/** Building space taken by buildings and by new buildings in the queue. */
|
|
|
|
|
|
public function spaceUsed(): int
|
2026-10-28 18:17:34 +00:00
|
|
|
|
{
|
2026-10-28 18:45:02 +00:00
|
|
|
|
return $this->rules->spaceUsed([
|
|
|
|
|
|
...$this->village->buildings->pluck('type'),
|
|
|
|
|
|
...$this->village->orders->where('action', BuildOrder::BUILD)->pluck('type'),
|
|
|
|
|
|
]);
|
|
|
|
|
|
}
|
2026-10-28 18:17:34 +00:00
|
|
|
|
|
2026-10-28 21:16:45 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* The village's building space, from its headquarters level. A queued demolition of
|
|
|
|
|
|
* the headquarters counts already, so nothing can be queued into space that goes away.
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function spaceMax(): int
|
|
|
|
|
|
{
|
|
|
|
|
|
$hq = $this->village->buildings->firstWhere('type', 'headquarters');
|
|
|
|
|
|
$level = $hq ? min($hq->level, $this->queuedLevel($hq)) : 0;
|
|
|
|
|
|
|
|
|
|
|
|
return $this->rules->spaceMax($this->village->space, $level);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 18:45:02 +00:00
|
|
|
|
public function spaceFits(string $type): bool
|
|
|
|
|
|
{
|
2026-10-28 21:16:45 +00:00
|
|
|
|
return $this->spaceUsed() + $this->rules->space($type) <= $this->spaceMax();
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 21:26:25 +00:00
|
|
|
|
/** The headquarters, and the last storage or farm, never go below level 1. */
|
2026-10-28 18:17:34 +00:00
|
|
|
|
public function isCore(Building $building): bool
|
|
|
|
|
|
{
|
|
|
|
|
|
if ($building->type === 'headquarters') {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return in_array($building->type, $this->config->get('world.village.start_buildings', []), true)
|
|
|
|
|
|
&& $this->village->buildings->where('type', $building->type)->count() === 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function meetsRequirements(string $type): bool
|
|
|
|
|
|
{
|
2026-10-28 19:58:35 +00:00
|
|
|
|
return $this->missingRequirements($type) === [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Requirements of a type that no building in the village meets yet.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return list<array{type: string, level: int}>
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function missingRequirements(string $type): array
|
|
|
|
|
|
{
|
|
|
|
|
|
$missing = [];
|
2026-10-28 18:17:34 +00:00
|
|
|
|
foreach ($this->rules->requires($type) as $needed => $level) {
|
|
|
|
|
|
if (! $this->village->buildings->where('type', $needed)->where('level', '>=', $level)->count()) {
|
2026-10-28 19:58:35 +00:00
|
|
|
|
$missing[] = ['type' => $needed, 'level' => $level];
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-28 19:58:35 +00:00
|
|
|
|
return $missing;
|
2026-10-28 18:17:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return array{target_level: int, cost: array<string, int>, seconds: int, blocked: null}|null */
|
|
|
|
|
|
public function upgradeOffer(Building $building): ?array
|
|
|
|
|
|
{
|
|
|
|
|
|
$current = $this->queuedLevel($building);
|
2026-10-28 21:26:25 +00:00
|
|
|
|
if ($current < 1 || $current >= $this->rules->maxLevel($building->type)) {
|
2026-10-28 18:17:34 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
$target = $current + 1;
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
'target_level' => $target,
|
|
|
|
|
|
'cost' => $this->rules->cost($building->type, $target, $this->instanceIndex($building->type, $building)),
|
|
|
|
|
|
'seconds' => $this->rules->buildSeconds($building->type, $target, $this->hqLevel(), $this->hqStaffing()),
|
|
|
|
|
|
'blocked' => null,
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @return array{target_level: int, refund: array<string, int>, seconds: int}|null */
|
|
|
|
|
|
public function demolishOffer(Building $building): ?array
|
|
|
|
|
|
{
|
|
|
|
|
|
$current = $this->queuedLevel($building);
|
2026-10-28 21:26:25 +00:00
|
|
|
|
if ($current < 1 || ($current === 1 && $this->isCore($building))) {
|
2026-10-28 18:17:34 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
'target_level' => $current - 1,
|
|
|
|
|
|
'refund' => $this->rules->demolishRefund($building->type, $current, $this->instanceIndex($building->type, $building)),
|
|
|
|
|
|
'seconds' => $this->rules->demolishSeconds($building->type, $current, $this->hqLevel(), $this->hqStaffing()),
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** New instances the player could queue, with why not where they can't. */
|
|
|
|
|
|
public function buildOptions(): array
|
|
|
|
|
|
{
|
2026-10-28 18:45:02 +00:00
|
|
|
|
return array_map(function (string $type) {
|
2026-10-28 18:17:34 +00:00
|
|
|
|
$max = $this->rules->maxPerVillage($type);
|
|
|
|
|
|
$blocked = match (true) {
|
|
|
|
|
|
$max !== null && $this->instances($type) >= $max => 'max_per_village',
|
2026-10-28 19:58:35 +00:00
|
|
|
|
$this->missingRequirements($type) !== [] => 'requires',
|
2026-10-28 18:45:02 +00:00
|
|
|
|
! $this->spaceFits($type) => 'no_space',
|
2026-10-28 18:17:34 +00:00
|
|
|
|
default => null,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
'type' => $type,
|
2026-10-28 18:45:02 +00:00
|
|
|
|
'space' => $this->rules->space($type),
|
2026-10-28 21:26:25 +00:00
|
|
|
|
'target_level' => 1,
|
|
|
|
|
|
'cost' => $this->rules->cost($type, 1, $this->instanceIndex($type)),
|
2026-10-31 09:56:59 +00:00
|
|
|
|
// A new instance ranks after all of its type, standing or queued.
|
|
|
|
|
|
'instance_share' => $this->rules->instanceShare($this->instances($type)),
|
2026-10-28 21:26:25 +00:00
|
|
|
|
'seconds' => $this->rules->buildSeconds($type, 1, $this->hqLevel(), $this->hqStaffing()),
|
2026-10-28 18:17:34 +00:00
|
|
|
|
'blocked' => $blocked,
|
2026-10-28 19:58:35 +00:00
|
|
|
|
// What is still missing, for the "not yet available" list.
|
|
|
|
|
|
'requires' => $this->missingRequirements($type),
|
2026-10-28 18:17:34 +00:00
|
|
|
|
];
|
|
|
|
|
|
}, $this->config->buildingTypes());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Build queue
|
|
|
|
|
|
|
|
|
|
|
|
/** @param array<string, mixed> $attributes */
|
|
|
|
|
|
public function enqueue(array $attributes, CarbonImmutable $now): BuildOrder
|
|
|
|
|
|
{
|
|
|
|
|
|
$order = $this->village->orders()->create($attributes);
|
|
|
|
|
|
$this->village->orders->push($order);
|
|
|
|
|
|
$this->startWaitingOrders($now);
|
|
|
|
|
|
|
|
|
|
|
|
return $order;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Hand free builders their next orders, timed from $at, and schedule when they finish. */
|
|
|
|
|
|
public function startWaitingOrders(CarbonImmutable $at): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$queue = $this->village->orders
|
|
|
|
|
|
->map(fn (BuildOrder $o) => ['id' => $o->id, 'building_id' => $o->building_id, 'builder' => $o->builder])
|
|
|
|
|
|
->values()->all();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (BuildQueue::startable($queue, $this->builders()) as $orderId => $slot) {
|
|
|
|
|
|
$order = $this->village->orders->firstWhere('id', $orderId);
|
|
|
|
|
|
$finishes = $at->addSeconds($this->orderSeconds($order));
|
|
|
|
|
|
$order->update(['builder' => $slot, 'starts_at' => $at, 'finishes_at' => $finishes]);
|
|
|
|
|
|
GameEvent::create([
|
|
|
|
|
|
'world_id' => $this->village->world_id,
|
|
|
|
|
|
'village_id' => $this->village->id,
|
|
|
|
|
|
'type' => GameEvent::BUILD_ORDER_FINISHED,
|
|
|
|
|
|
'subject_id' => $order->id,
|
|
|
|
|
|
'fire_at' => $finishes,
|
|
|
|
|
|
]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function orderSeconds(BuildOrder $order): int
|
|
|
|
|
|
{
|
|
|
|
|
|
return $order->action === BuildOrder::DEMOLISH
|
|
|
|
|
|
? $this->rules->demolishSeconds($order->type, $order->target_level + 1, $this->hqLevel(), $this->hqStaffing())
|
|
|
|
|
|
: $this->rules->buildSeconds($order->type, $order->target_level, $this->hqLevel(), $this->hqStaffing());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-29 09:05:55 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Apply a finished order, drop it from the queue and recount the village's points. A
|
|
|
|
|
|
* level reached for the first time leaves a level reward for the player.
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function finish(BuildOrder $order, CarbonImmutable $at): void
|
2026-10-28 18:45:02 +00:00
|
|
|
|
{
|
|
|
|
|
|
$this->applyFinished($order);
|
|
|
|
|
|
$this->village->points = $this->points();
|
2026-10-29 09:05:55 +00:00
|
|
|
|
if ($order->action !== BuildOrder::DEMOLISH) {
|
|
|
|
|
|
Quests::reached($this->village, $order->type, $order->target_level, $order->cost, $at);
|
|
|
|
|
|
}
|
2026-10-28 18:45:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Points shown on the map: the sum over buildings of their level's points. */
|
|
|
|
|
|
public function points(): int
|
|
|
|
|
|
{
|
|
|
|
|
|
return $this->village->buildings->sum(fn (Building $b) => $this->rules->points($b->type, $b->level));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function applyFinished(BuildOrder $order): void
|
2026-10-28 18:17:34 +00:00
|
|
|
|
{
|
|
|
|
|
|
$this->removeOrder($order);
|
|
|
|
|
|
|
|
|
|
|
|
if ($order->action === BuildOrder::BUILD) {
|
2026-10-28 21:26:25 +00:00
|
|
|
|
$building = $this->village->buildings()->create(['type' => $order->type, 'level' => 1]);
|
2026-10-28 18:17:34 +00:00
|
|
|
|
$this->village->buildings->push($building->refresh());
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$building = $this->village->buildings->firstWhere('id', $order->building_id);
|
|
|
|
|
|
if (! $building) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($order->action === BuildOrder::DEMOLISH) {
|
|
|
|
|
|
$this->refund($this->rules->demolishRefund($building->type, $building->level, $this->instanceIndex($building->type, $building)));
|
|
|
|
|
|
}
|
|
|
|
|
|
$building->level = $order->target_level;
|
2026-10-28 21:26:25 +00:00
|
|
|
|
if ($building->level < 1) {
|
2026-10-28 20:39:48 +00:00
|
|
|
|
// Units still in training there go back to being villagers, with a refund.
|
|
|
|
|
|
foreach ($this->village->recruits->where('building_id', $building->id) as $order) {
|
|
|
|
|
|
$this->refund(array_map(fn (int $n) => $n * ($order->count - $order->done), $order->cost));
|
|
|
|
|
|
GameEvent::where('type', GameEvent::RECRUIT_FINISHED)->where('subject_id', $order->id)->whereNull('processed_at')->delete();
|
|
|
|
|
|
}
|
|
|
|
|
|
$this->village->setRelation('recruits', $this->village->recruits->reject(fn (RecruitOrder $o) => $o->building_id === $building->id)->values());
|
2026-10-28 18:17:34 +00:00
|
|
|
|
$building->delete();
|
|
|
|
|
|
$this->village->setRelation('buildings', $this->village->buildings->reject(fn (Building $b) => $b->is($building))->values());
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$building->save();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Cancel an order with a full refund, plus later orders on the same instance that build on it. */
|
|
|
|
|
|
public function cancel(BuildOrder $order): void
|
|
|
|
|
|
{
|
|
|
|
|
|
$cancelled = $this->village->orders->filter(fn (BuildOrder $o) => $o->is($order)
|
|
|
|
|
|
|| ($order->building_id !== null && $o->building_id === $order->building_id && $o->id > $order->id));
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($cancelled as $o) {
|
|
|
|
|
|
$this->refund($o->cost);
|
|
|
|
|
|
$this->removeOrder($o);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Drops the order and its pending finish event (the engine marks a running one processed first). */
|
|
|
|
|
|
private function removeOrder(BuildOrder $order): void
|
|
|
|
|
|
{
|
|
|
|
|
|
GameEvent::where('type', GameEvent::BUILD_ORDER_FINISHED)
|
|
|
|
|
|
->where('subject_id', $order->id)
|
|
|
|
|
|
->whereNull('processed_at')
|
|
|
|
|
|
->delete();
|
|
|
|
|
|
$order->delete();
|
|
|
|
|
|
$this->village->setRelation('orders', $this->village->orders->reject(fn (BuildOrder $o) => $o->is($order))->values());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|