diff --git a/app/Engine/Simulation.php b/app/Engine/Simulation.php index 4b04c5e..8d0f1bd 100644 --- a/app/Engine/Simulation.php +++ b/app/Engine/Simulation.php @@ -199,13 +199,24 @@ final class Simulation public function meetsRequirements(string $type): bool { + return $this->missingRequirements($type) === []; + } + + /** + * Requirements of a type that no building in the village meets yet. + * + * @return list + */ + public function missingRequirements(string $type): array + { + $missing = []; foreach ($this->rules->requires($type) as $needed => $level) { if (! $this->village->buildings->where('type', $needed)->where('level', '>=', $level)->count()) { - return false; + $missing[] = ['type' => $needed, 'level' => $level]; } } - return true; + return $missing; } /** @return array{target_level: int, cost: array, seconds: int, blocked: null}|null */ @@ -247,7 +258,7 @@ final class Simulation $max = $this->rules->maxPerVillage($type); $blocked = match (true) { $max !== null && $this->instances($type) >= $max => 'max_per_village', - ! $this->meetsRequirements($type) => 'requires', + $this->missingRequirements($type) !== [] => 'requires', ! $this->spaceFits($type) => 'no_space', default => null, }; @@ -259,6 +270,8 @@ final class Simulation 'cost' => $this->rules->cost($type, 0, $this->instanceIndex($type)), 'seconds' => $this->rules->buildSeconds($type, 0, $this->hqLevel(), $this->hqStaffing()), 'blocked' => $blocked, + // What is still missing, for the "not yet available" list. + 'requires' => $this->missingRequirements($type), ]; }, $this->config->buildingTypes()); } diff --git a/config/game/buildings.php b/config/game/buildings.php index 7f7d493..40ad8a8 100644 --- a/config/game/buildings.php +++ b/config/game/buildings.php @@ -104,7 +104,7 @@ return [ 'production' => null, 'instance_cost_factor' => 1.0, 'points' => ['base' => 8, 'factor' => 1.2], - 'requires' => ['headquarters' => 1], + 'requires' => [], ], 'rally_point' => [ 'space' => 1, diff --git a/docs/architecture.md b/docs/architecture.md index db669a9..5025294 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -76,6 +76,7 @@ When upkeep exceeds income, food stock drains. The engine computes the exact mom - 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 0. Level 0 is a real, primitive state: the HQ is a tent, storage is open ground, and the farm is gatherers. A new instance is built at level 0. - Multiple instances per type are allowed. `max_per_village` limits types that must stay unique (HQ, rally point, wall). `instance_cost_factor` makes the n-th instance of a type more expensive, so many cheap copies don't beat upgrading. +- 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). Below level 0 the instance is removed and its space is freed. - The wall takes 0 space: it surrounds the village. - Each building level is worth `points` (config); a village's points are their sum, kept on the row when an order finishes and shown on the map. @@ -115,7 +116,7 @@ A building entry looks like this (numbers start TW-like): 'build_time' => ['base' => 900, 'factor' => 1.2], // seconds at world speed 1 'workers' => ['base' => 2, 'factor' => 1.12], 'production' => ['resource' => 'wood', 'base' => 30, 'factor' => 1.163], - 'requires' => ['headquarters' => 1], + 'requires' => [], ], ``` diff --git a/docs/roadmap.md b/docs/roadmap.md index d0a2de2..57b0563 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -31,7 +31,7 @@ Each milestone ends with tests green, `ConfigLint` green, and something playable ## M3: Units, upkeep, starvation - [ ] `config/game/units.php`: spear, sword, archer, light cav, heavy cav, ram, catapult, nobleman, with food upkeep -- [ ] Barracks, stable, workshop, academy as building instances (several barracks run parallel recruit queues) +- [ ] Barracks, stable, workshop, academy as building instances (several barracks run parallel recruit queues); only these advanced buildings get requirements, e.g. academy needs headquarters + barracks - [ ] Recruiting turns villagers into soldiers; lazy per-unit completion - [ ] Levy: draft workers as militia, automatic return - [ ] Starvation: zero-crossing event, grace timer, desertion order diff --git a/tests/Feature/BuildQueueTest.php b/tests/Feature/BuildQueueTest.php index b45f4c4..354766c 100644 --- a/tests/Feature/BuildQueueTest.php +++ b/tests/Feature/BuildQueueTest.php @@ -64,15 +64,26 @@ it('refuses a building that does not fit the free building space', function () { ->and(collect($state['buildable'])->firstWhere('type', 'hiding_place')['blocked'])->toBe('no_space'); }); -it('refuses a second headquarters, a wall without HQ 1 and what it cannot pay', function () { +it('refuses a second headquarters and what it cannot pay, but lets the wall be built right away', function () { $village = startVillage(); - $village->update(['wood' => 10]); + expect(collect(shown($village)['buildable'])->firstWhere('type', 'wall'))->toMatchArray(['blocked' => null, 'requires' => []]); + + $village->update(['wood' => 10]); expect(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'headquarters')))->toBe('max_per_village') - ->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'wall')))->toBe('requires') ->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'storage')))->toBe('resources'); }); +it('lists the requirements an advanced building still misses', function () { + // No M1 building has requirements; advanced ones (barracks, academy) will. + config(['game.buildings.rally_point.requires' => ['headquarters' => 3, 'storage' => 0]]); + $village = startVillage(); + + expect(collect(shown($village)['buildable'])->firstWhere('type', 'rally_point')) + ->toMatchArray(['blocked' => 'requires', 'requires' => [['type' => 'headquarters', 'level' => 3]]]) + ->and(rule(fn () => app(QueueBuild::class)(owner($village), $village->id, 'rally_point')))->toBe('requires'); +}); + it('makes a later instance of a type cost more', function () { $village = startVillage(); diff --git a/tests/Feature/JoinWorldTest.php b/tests/Feature/JoinWorldTest.php index 05855d7..2196a68 100644 --- a/tests/Feature/JoinWorldTest.php +++ b/tests/Feature/JoinWorldTest.php @@ -52,6 +52,6 @@ it('shows a start village to its owner only', function () { ->and($state['population'])->toEqual(['food_budget' => 30, 'upkeep' => 30, 'villagers' => 30, 'workers' => 5, 'soldiers' => 0]) ->and($state['builders'])->toBe(2) ->and(collect($state['buildable'])->firstWhere('type', 'headquarters')['blocked'])->toBe('max_per_village') - ->and(collect($state['buildable'])->firstWhere('type', 'wall')['blocked'])->toBe('requires') + ->and(collect($state['buildable'])->firstWhere('type', 'wall')['blocked'])->toBeNull() ->and(rule(fn () => app(ShowVillage::class)(User::factory()->create(), $village->id)))->toBe('not_found'); });