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) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-31 10:58:56 +01:00
parent 8579e5ee75
commit 2c20fd24ea
5 changed files with 38 additions and 29 deletions

View File

@ -267,27 +267,23 @@ final class Simulation
+ $this->village->orders->where('action', BuildOrder::BUILD)->where('type', $type)->count(); + $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 public function instanceIndex(string $type, ?Building $building = null): int
{ {
if (! $building) { if (! $building) {
return $this->instances($type); return $this->instances($type);
} }
if ($this->frozen) { if (! $this->frozen || $this->indexes === null) {
if ($this->indexes === null) { $this->indexes = $this->rules->instanceRanks($this->states());
$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; return $this->indexes[$building->id] ?? 0;
} }
return $this->village->buildings->where('type', $type)->where('id', '<', $building->id)->count();
}
/** Level after everything already queued for this instance; 0 if it is being removed. */ /** Level after everything already queued for this instance; 0 if it is being removed. */
public function queuedLevel(Building $building): int public function queuedLevel(Building $building): int
{ {

View File

@ -51,24 +51,34 @@ final class BuildingRules
} }
/** /**
* The share each instance works at. Instances of one type rank by level (highest * The rank of each instance among its type: by level (highest first, then lowest id),
* first, then lowest id); rank n (0-based) works at economy.instance_share^n, so the * 0 for the first or only one. Copies work and pay by this rank.
* first or only one works at 1 and copies give less. *
* @param list<BuildingState> $buildings
* @return array<int, int> 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<BuildingState> $buildings * @param list<BuildingState> $buildings
* @return array<int, float> building id => share * @return array<int, float> building id => share
*/ */
public function instanceShares(array $buildings): array public function instanceShares(array $buildings): array
{ {
usort($buildings, fn (BuildingState $a, BuildingState $b) => [$b->level, $a->id] <=> [$a->level, $b->id]); return array_map(fn (int $rank) => $this->instanceShare($rank), $this->instanceRanks($buildings));
$shares = [];
$rank = [];
foreach ($buildings as $b) {
$rank[$b->type] = ($rank[$b->type] ?? -1) + 1;
$shares[$b->id] = $this->instanceShare($rank[$b->type]);
}
return $shares;
} }
/** The share the instance at this rank among its type works at (0 = the first). */ /** The share the instance at this rank among its type works at (0 = the first). */

View File

@ -5,8 +5,8 @@
// Level n: cost = cost × factor^(n−1), build time = base × factor^(n−1) seconds at world // 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, // speed 1, workers = round(base × factor^(n−1)), and production, capacity, hides,
// detection and beds = base × factor^(n−1). // 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 // One instance of a type is enough; copies are the exception. The instance at rank n
// (0-based, in build order) costs instance_cost_factor^n times as much at every level, and // (0-based, by level, highest first) costs instance_cost_factor^n times as much, and
// copies work at a falling share (economy.instance_share). // copies work at a falling share (economy.instance_share).
// Map points of an instance: round(points.base × points.factor^(level−1)). // Map points of an instance: round(points.base × points.factor^(level−1)).

View File

@ -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). - 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. - 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). - 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 ### 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. - 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. - 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. - 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. - 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. - 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. - The wall takes 0 space: it surrounds the village.

View File

@ -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(); $storages = collect($state['buildings'])->where('type', 'storage')->values();
expect($storages->pluck('level')->all())->toBe([1, 2]) expect($storages->pluck('level')->all())->toBe([1, 2])
->and($storages->pluck('instance_share')->all())->toBe([0.5, 1.0]) ->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 () { it('trains at half the speed in a second barracks', function () {