From e3e8f4cf548722a3376fe290f5193ddbde2b4396 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Sat, 31 Oct 2026 10:50:31 +0100 Subject: [PATCH] feat: coins pay for villages, not noblemen: training a nobleman needs no coins, and a conquest without a free slot leaves loyalty at 0 and sends the noblemen home Co-Authored-By: Claude Opus 5.5 (1M context) --- app/Actions/MintCoins.php | 2 +- app/Engine/Military.php | 12 +---------- app/Engine/Nobility.php | 31 +++++++++++------------------ app/Engine/Rally.php | 10 ++++++---- app/Game/Military/Conquest.php | 10 ++++++++-- app/Presenters/VillagePresenter.php | 7 ++++--- tests/Unit/Game/ConquestTest.php | 12 ++++++++++- 7 files changed, 43 insertions(+), 41 deletions(-) diff --git a/app/Actions/MintCoins.php b/app/Actions/MintCoins.php index 81a7b24..2345ebd 100644 --- a/app/Actions/MintCoins.php +++ b/app/Actions/MintCoins.php @@ -9,7 +9,7 @@ use App\Models\User; use App\Models\Village; use Carbon\CarbonImmutable; -/** Mint coins at the village's academy; they belong to the player and pay for noble slots. */ +/** Mint coins at the village's academy; they belong to the player and pay for the villages beyond the first. */ final class MintCoins extends VillageAction { public function __invoke(User $user, int $villageId, int $count): Village diff --git a/app/Engine/Military.php b/app/Engine/Military.php index 8fd6bbb..4a2efe3 100644 --- a/app/Engine/Military.php +++ b/app/Engine/Military.php @@ -85,14 +85,7 @@ final class Military )); $byVillagers = (int) floor($this->sim->workforce()->undrafted() / $this->units->upkeep($unit)); - $bySlots = $this->isNoble($unit) && $this->village->player ? Nobility::of($this->village)->freeSlots($this->village->player) : PHP_INT_MAX; - - return max(0, min($byResources, $byVillagers, $bySlots)); - } - - private function isNoble(string $unit): bool - { - return (bool) $this->sim->config()->get("units.$unit.noble", false); + return max(0, min($byResources, $byVillagers)); } /** Seconds per unit at this building instance, from its level and staffing. */ @@ -115,9 +108,6 @@ final class Military if ($this->missingRequirements($unit)) { throw new GameRuleViolation('requires', 'Requirements not met.'); } - if ($this->isNoble($unit) && $count > Nobility::of($this->village)->freeSlots($this->village->player)) { - throw new GameRuleViolation('coins', 'Mint more coins for another nobleman.'); - } if ($count * $this->units->upkeep($unit) > $this->sim->workforce()->undrafted()) { throw new GameRuleViolation('villagers', 'Not enough villagers to train that many.'); } diff --git a/app/Engine/Nobility.php b/app/Engine/Nobility.php index a26d091..576ddae 100644 --- a/app/Engine/Nobility.php +++ b/app/Engine/Nobility.php @@ -6,12 +6,11 @@ use App\Game\Military\Conquest; use App\Models\GameEvent; use App\Models\Movement; use App\Models\Player; -use App\Models\RecruitOrder; use App\Models\UnitStack; use App\Models\Village; use Carbon\CarbonImmutable; -/** Coins, noble slots, loyalty and handing a conquered village over (combat.conquest). */ +/** Coins and the villages they pay for, loyalty and handing a conquered village over (combat.conquest). */ final class Nobility { private function __construct(private readonly Conquest $rules) {} @@ -37,32 +36,26 @@ final class Nobility return $this->rules->loyalty((float) $village->loyalty, $hours); } - /** - * Noble slots in use: noblemen standing anywhere, under way and in training, plus - * every village beyond the first. - */ + /** Slots in use: every village beyond the first. Noblemen take none. */ public function slotsUsed(Player $player): int { - $nobles = $this->rules->nobleUnits(); - if (! $nobles) { - return 0; - } - $villages = Village::where('player_id', $player->id)->pluck('id'); - $standing = (int) UnitStack::whereIn('home_village_id', $villages)->whereIn('unit', $nobles)->sum('count'); - $moving = Movement::where('player_id', $player->id)->pluck('units') - ->sum(fn (array $units) => array_sum(array_intersect_key($units, array_flip($nobles)))); - $training = RecruitOrder::whereIn('village_id', $villages)->whereIn('unit', $nobles)->get() - ->sum(fn (RecruitOrder $o) => $o->count - $o->done); - - return $standing + $moving + $training + max(0, $villages->count() - 1); + return max(0, Village::where('player_id', $player->id)->count() - 1); } - /** Noble slots the player's coins pay for and doesn't use yet. */ + /** Slots the player's coins pay for and no village uses yet. */ public function freeSlots(Player $player): int { return max(0, Conquest::slots((int) $player->coins) - $this->slotsUsed($player)); } + /** Whether the player's coins pay for one more village. */ + public function canTake(?int $playerId): bool + { + $player = $playerId ? Player::find($playerId) : null; + + return $player !== null && Conquest::canTake((int) $player->coins, Village::where('player_id', $player->id)->count()); + } + /** * The village goes to the attacker. Its queues end without refunds, the old owner's * groups from or to it are lost, and so is its support standing elsewhere. diff --git a/app/Engine/Rally.php b/app/Engine/Rally.php index 128b062..85e809e 100644 --- a/app/Engine/Rally.php +++ b/app/Engine/Rally.php @@ -370,7 +370,8 @@ final class Rally } } - // Surviving noblemen of a won attack lower loyalty; at 0 the village changes hands. + // Surviving noblemen of a won attack lower loyalty; at 0 the village changes hands + // if the attacker's coins pay for one more. If not, it holds out at 0. $loyalty = null; $nobility = Nobility::of($this->village); $nobles = array_sum(array_intersect_key($survivors, array_flip($nobility->rules()->nobleUnits()))); @@ -380,10 +381,11 @@ final class Rally for ($i = 0; $i < $nobles; $i++) { $after -= $nobility->rules()->drop(mt_rand() / (mt_getrandmax() + 1)); } - $conquered = $after <= 0; - $this->village->loyalty = $conquered ? $nobility->rules()->afterConquest() : $after; + $fallen = $after <= 0; + $conquered = $fallen && $nobility->canTake($movement->player_id); + $this->village->loyalty = $conquered ? $nobility->rules()->afterConquest() : max(0, $after); $this->village->loyalty_at = $at; - $loyalty = ['before' => (int) floor($before), 'after' => $conquered ? 0 : (int) floor($after), 'conquered' => $conquered]; + $loyalty = ['before' => (int) floor($before), 'after' => $fallen ? 0 : (int) floor($after), 'conquered' => $conquered, 'no_coins' => $fallen && ! $conquered]; } $conquered = $loyalty['conquered'] ?? false; // Part of the own fallen come back wounded, unless the village falls with them. diff --git a/app/Game/Military/Conquest.php b/app/Game/Military/Conquest.php index d94afb8..aeb4a3a 100644 --- a/app/Game/Military/Conquest.php +++ b/app/Game/Military/Conquest.php @@ -4,7 +4,7 @@ namespace App\Game\Military; use App\Game\WorldConfig; -/** Loyalty, its drop under noblemen and the coin slots that limit them (combat.conquest). */ +/** Loyalty, its drop under noblemen and the coins that pay for the villages taken (combat.conquest). */ final class Conquest { public function __construct(private readonly WorldConfig $config) {} @@ -35,12 +35,18 @@ final class Conquest return round($min + ($max - $min) * $roll); } - /** Noble slots that $coins pay for: the n-th slot needs n more coins. */ + /** Slots that $coins pay for, one per village beyond the first: the n-th needs n more coins. */ public static function slots(int $coins): int { return (int) floor((sqrt(8 * max(0, $coins) + 1) - 1) / 2); } + /** Whether a player with $coins coins and $villages villages may take one more. */ + public static function canTake(int $coins, int $villages): bool + { + return $villages < 1 + self::slots($coins); + } + /** Coins in total for $slots slots. */ public static function coinsFor(int $slots): int { diff --git a/app/Presenters/VillagePresenter.php b/app/Presenters/VillagePresenter.php index 1b4c7ca..19155e4 100644 --- a/app/Presenters/VillagePresenter.php +++ b/app/Presenters/VillagePresenter.php @@ -220,7 +220,7 @@ final class VillagePresenter ]; } - /** Coins and noble slots of the player, and what a coin costs. */ + /** The player's coins, the villages they pay for, and what a coin costs. */ private static function conquest(Village $village): array { $nobility = Nobility::of($village); @@ -229,10 +229,11 @@ final class VillagePresenter return [ 'coins' => $coins, + // Villages beyond the first: the ones the coins pay for, and the ones held. 'slots' => $slots, 'used' => $village->player ? $nobility->slotsUsed($village->player) : 0, - // Coins still missing for the next slot. - 'next_slot_coins' => Conquest::coinsFor($slots + 1) - $coins, + // Coins in total for one more village. + 'next_slot_coins' => Conquest::coinsFor($slots + 1), 'coin_cost' => $nobility->rules()->coinCost(), 'max_distance' => $nobility->rules()->maxDistance(), ]; diff --git a/tests/Unit/Game/ConquestTest.php b/tests/Unit/Game/ConquestTest.php index b8e7837..9cba95a 100644 --- a/tests/Unit/Game/ConquestTest.php +++ b/tests/Unit/Game/ConquestTest.php @@ -16,11 +16,21 @@ it('drops loyalty by 20 to 35 per nobleman', function () { ->and($conquest->drop(0.9999))->toBe(35.0); }); -it('gives the n-th noble slot for n more coins', function () { +it('gives the n-th slot for n more coins', function () { expect(array_map(Conquest::slots(...), [0, 1, 2, 3, 5, 6, 10]))->toBe([0, 1, 1, 2, 2, 3, 4]) ->and(Conquest::coinsFor(4))->toBe(10); }); +it('lets coins pay for villages: 1 for the 2nd, 3 in total for the 3rd, 6 for the 4th', function () { + expect(Conquest::canTake(0, 0))->toBeTrue() + ->and(Conquest::canTake(0, 1))->toBeFalse() + ->and(Conquest::canTake(1, 1))->toBeTrue() + ->and(Conquest::canTake(2, 2))->toBeFalse() + ->and(Conquest::canTake(3, 2))->toBeTrue() + ->and(Conquest::canTake(5, 3))->toBeFalse() + ->and(Conquest::canTake(6, 3))->toBeTrue(); +}); + it('knows the noblemen and what a coin costs', function () { $conquest = new Conquest(gameConfig());