From 2c20fd24eac7811f1d12a4d049dc8ed43a3f3e65 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Sat, 31 Oct 2026 10:58:56 +0100 Subject: [PATCH] fix: copies pay by the same rank they work at: by level, so the copy that gives less pays double Co-Authored-By: Claude Opus 5.5 (1M context) --- app/Engine/Simulation.php | 20 +++++++--------- app/Game/Buildings/BuildingRules.php | 34 ++++++++++++++++++---------- config/game/buildings.php | 4 ++-- docs/architecture.md | 4 ++-- tests/Feature/InstanceShareTest.php | 5 +++- 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/app/Engine/Simulation.php b/app/Engine/Simulation.php index 4395b92..80c941b 100644 --- a/app/Engine/Simulation.php +++ b/app/Engine/Simulation.php @@ -267,25 +267,21 @@ final class Simulation + $this->village->orders->where('action', BuildOrder::BUILD)->where('type', $type)->count(); } - /** 0-based index among same-type instances; a new instance comes after all of them. */ + /** + * 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. + */ public function instanceIndex(string $type, ?Building $building = null): int { if (! $building) { return $this->instances($type); } - if ($this->frozen) { - if ($this->indexes === null) { - $this->indexes = []; - $seen = []; - foreach ($this->village->buildings->sortBy('id') as $b) { - $this->indexes[$b->id] = $seen[$b->type] = ($seen[$b->type] ?? -1) + 1; - } - } - - return $this->indexes[$building->id] ?? 0; + if (! $this->frozen || $this->indexes === null) { + $this->indexes = $this->rules->instanceRanks($this->states()); } - return $this->village->buildings->where('type', $type)->where('id', '<', $building->id)->count(); + return $this->indexes[$building->id] ?? 0; } /** Level after everything already queued for this instance; 0 if it is being removed. */ diff --git a/app/Game/Buildings/BuildingRules.php b/app/Game/Buildings/BuildingRules.php index a95029e..b4ff2d5 100644 --- a/app/Game/Buildings/BuildingRules.php +++ b/app/Game/Buildings/BuildingRules.php @@ -51,24 +51,34 @@ final class BuildingRules } /** - * The share each instance works at. Instances of one type rank by level (highest - * first, then lowest id); rank n (0-based) works at economy.instance_share^n, so the - * first or only one works at 1 and copies give less. + * The rank of each instance among its type: by level (highest first, then lowest id), + * 0 for the first or only one. Copies work and pay by this rank. + * + * @param list $buildings + * @return array building id => rank + */ + public function instanceRanks(array $buildings): array + { + usort($buildings, fn (BuildingState $a, BuildingState $b) => [$b->level, $a->id] <=> [$a->level, $b->id]); + $ranks = []; + $seen = []; + foreach ($buildings as $b) { + $ranks[$b->id] = $seen[$b->type] = ($seen[$b->type] ?? -1) + 1; + } + + return $ranks; + } + + /** + * The share each instance works at: rank n (instanceRanks) works at + * economy.instance_share^n, so the first or only one works at 1 and copies give less. * * @param list $buildings * @return array building id => share */ public function instanceShares(array $buildings): array { - usort($buildings, fn (BuildingState $a, BuildingState $b) => [$b->level, $a->id] <=> [$a->level, $b->id]); - $shares = []; - $rank = []; - foreach ($buildings as $b) { - $rank[$b->type] = ($rank[$b->type] ?? -1) + 1; - $shares[$b->id] = $this->instanceShare($rank[$b->type]); - } - - return $shares; + return array_map(fn (int $rank) => $this->instanceShare($rank), $this->instanceRanks($buildings)); } /** The share the instance at this rank among its type works at (0 = the first). */ diff --git a/config/game/buildings.php b/config/game/buildings.php index 691469a..537d7f3 100644 --- a/config/game/buildings.php +++ b/config/game/buildings.php @@ -5,8 +5,8 @@ // Level n: cost = cost × factor^(n−1), build time = base × factor^(n−1) seconds at world // speed 1, workers = round(base × factor^(n−1)), and production, capacity, hides, // detection and beds = base × factor^(n−1). -// One instance of a type is enough; copies are the exception. The n-th instance of a type -// (0-based, in build order) costs instance_cost_factor^n times as much at every level, and +// One instance of a type is enough; copies are the exception. The instance at rank n +// (0-based, by level, highest first) costs instance_cost_factor^n times as much, and // copies work at a falling share (economy.instance_share). // Map points of an instance: round(points.base × points.factor^(level−1)). diff --git a/docs/architecture.md b/docs/architecture.md index 58f8c44..1b1f852 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -70,7 +70,7 @@ One instance of each type is enough, as in Die Stämme. A copy works at a fallin - The share scales production, a farm's nominal output in the food budget, storage capacity, hides, hospital beds and training speed (seconds per unit divide by it). - Workers needed stay full, so a copy eats like a full building and gives half. -- The rank follows the standing buildings on every read (`BuildingRules::instanceShares`). Upgrading a copy past the first swaps their shares. +- The rank follows the standing buildings on every read (`BuildingRules::instanceRanks`). Upgrading a copy past the first swaps their shares and their costs: the copy that works at less is the one that pays more. A new instance ranks after all standing and queued ones. - The village payload carries `instance_share` on every building, and on every build option the share a new instance would work at: `instance_share`^(standing + queued instances of that type). ### Builders @@ -89,7 +89,7 @@ A wave takes the fewest units that make income cover upkeep again: support stati - Each building type takes a fixed `space` from config, for example HQ 9, barracks 6, resource buildings 4, and hiding place 1. The level changes only the look, never the space. A building fits when the space of all buildings plus new buildings still in the queue, plus its own, stays within the budget. The space is reserved when the order is queued. - There are no tiles and no stored positions. The client draws the village as a low-poly isometric diorama and lays the buildings out itself (HQ in the centre, the rest around it), so where a building stands is presentation, not game state. - Levels start at 1, as in Die Stämme, and a new instance is built at level 1. Every per-level formula uses `level − 1` as the exponent of its factor, so level 1 has the base values. -- One of each type is enough: one storage, one farm, one barracks. Copies are the exception and pay twice. `max_per_village` keeps some types unique (HQ, rally point, wall, watchtower, academy). The n-th instance of a type (0-based, in build order) costs `instance_cost_factor`^n as much at every level (2: a second camp costs double, a third four times), and it works at a falling share (see "Copies"). Upgrading beats copying. +- One of each type is enough: one storage, one farm, one barracks. Copies are the exception and pay twice. `max_per_village` keeps some types unique (HQ, rally point, wall, watchtower, academy). The instance at rank n (the same rank as its share) costs `instance_cost_factor`^n as much at every level (2: a second camp costs double, a third four times), and it works at a falling share (see "Copies"). Upgrading beats copying. - Basic buildings have no requirements: everything in the core set, the wall included, can be built from the start. Only advanced buildings get `requires` (type => minimum level of any instance), for example the academy (Adelshof) needs the headquarters and a barracks (Kaserne). A build option lists what is still missing. - Demolishing goes through the build queue. It lowers an instance by one level and refunds `demolish_refund` × that level's cost (default 0.5). Taking level 1 down removes the instance (an order with target level 0) and frees its space. - The wall takes 0 space: it surrounds the village. diff --git a/tests/Feature/InstanceShareTest.php b/tests/Feature/InstanceShareTest.php index e1cdf3c..85db4ec 100644 --- a/tests/Feature/InstanceShareTest.php +++ b/tests/Feature/InstanceShareTest.php @@ -69,7 +69,10 @@ it('adds half of a second storage to the cap, and swaps when the copy is upgrade $storages = collect($state['buildings'])->where('type', 'storage')->values(); expect($storages->pluck('level')->all())->toBe([1, 2]) ->and($storages->pluck('instance_share')->all())->toBe([0.5, 1.0]) - ->and($state['storage_cap'])->toEqual(1230 + 500); + ->and($state['storage_cap'])->toEqual(1230 + 500) + // The cost follows the rank too: the old first now pays double, the new first single. + ->and($storages[0]['upgrade']['cost'])->toBe(['wood' => 152, 'clay' => 127, 'iron' => 102]) + ->and($storages[1]['upgrade']['cost'])->toBe(['wood' => 97, 'clay' => 81, 'iron' => 65]); }); it('trains at half the speed in a second barracks', function () {