feat: barbarian villages hold garrisons that regroup after a hit
Full strength follows the village's points in one of four profiles, capped by its food; a barbarian.regroups event brings it back a quarter at a time. Existing barbarians get theirs from the seeder on deploy. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
b94788aa92
commit
7e520512cf
|
|
@ -2,12 +2,14 @@
|
|||
|
||||
namespace App\Actions;
|
||||
|
||||
use App\Engine\Barbarians;
|
||||
use App\Engine\VillageFactory;
|
||||
use App\Game\Map\BarbarianGrowth;
|
||||
use App\Game\Map\Frontier;
|
||||
use App\Models\GameEvent;
|
||||
use App\Models\Village;
|
||||
use App\Models\World;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Barbarian villages come with the players, as in Die Stämme: each joining player brings
|
||||
|
|
@ -44,6 +46,7 @@ final class SpawnBarbarians
|
|||
$levels[$type] = mt_rand(1, $maxLevel);
|
||||
}
|
||||
$barbarian = $this->villages->create($world, null, 'Barbarian village', $spot[0], $spot[1], $levels);
|
||||
Barbarians::of($barbarian)->fill();
|
||||
$this->scheduleFirstGrowth($world, $barbarian);
|
||||
$created[] = $barbarian;
|
||||
}
|
||||
|
|
@ -68,6 +71,43 @@ final class SpawnBarbarians
|
|||
return $villages->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Give every barbarian village its garrison: full strength at once for one that never
|
||||
* had any (from before garrisons), a regroup step for one that is short without one
|
||||
* waiting. Safe to run on every deploy.
|
||||
*
|
||||
* @return int villages that got troops or a regroup step
|
||||
*/
|
||||
public function scheduleGarrisons(World $world): int
|
||||
{
|
||||
$now = now()->toImmutable();
|
||||
$changed = 0;
|
||||
foreach ($world->villages()->whereNull('player_id')->pluck('id') as $id) {
|
||||
$changed += DB::transaction(function () use ($id, $now) {
|
||||
$village = Village::whereKey($id)->lockForUpdate()->first();
|
||||
if (! $village || $village->player_id !== null) {
|
||||
return 0;
|
||||
}
|
||||
$barbarians = Barbarians::of($village);
|
||||
$waiting = GameEvent::where('type', GameEvent::BARBARIAN_REGROUPS)->where('village_id', $id)->whereNull('processed_at')->exists();
|
||||
if ($barbarians->current() === [] && ! $waiting) {
|
||||
$barbarians->fill();
|
||||
|
||||
return $barbarians->current() === [] ? 0 : 1;
|
||||
}
|
||||
if (! $waiting && ! $barbarians->rules()->isFull($barbarians->current(), $barbarians->full())) {
|
||||
$barbarians->scheduleRegroup($now);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
return $changed;
|
||||
}
|
||||
|
||||
/** The first growth step lands 0.5–1.5 intervals out, so villages don't grow in step. */
|
||||
private function scheduleFirstGrowth(World $world, Village $village): void
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
|
||||
namespace App\Engine;
|
||||
|
||||
use App\Game\Map\BarbarianGarrison;
|
||||
use App\Models\GameEvent;
|
||||
use App\Models\UnitStack;
|
||||
use App\Models\Village;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
/**
|
||||
* The garrison of one barbarian village (architecture.md, "Barbarian garrisons"): filled
|
||||
* when it appears, regrouping step by step after a hit or when the village grows.
|
||||
* Callers hold the village lock.
|
||||
*/
|
||||
final class Barbarians
|
||||
{
|
||||
private BarbarianGarrison $garrison;
|
||||
|
||||
private function __construct(private readonly Village $village, private readonly Simulation $sim)
|
||||
{
|
||||
$this->garrison = new BarbarianGarrison($sim->config());
|
||||
}
|
||||
|
||||
public static function of(Village $village, ?Simulation $sim = null): self
|
||||
{
|
||||
return new self($village, $sim ?? Simulation::of($village));
|
||||
}
|
||||
|
||||
public function rules(): BarbarianGarrison
|
||||
{
|
||||
return $this->garrison;
|
||||
}
|
||||
|
||||
public function profile(): ?string
|
||||
{
|
||||
return $this->garrison->profile($this->village->id);
|
||||
}
|
||||
|
||||
/** @return array<string, int> full strength now, from the village's points and food */
|
||||
public function full(): array
|
||||
{
|
||||
return $this->garrison->full($this->profile(), (int) $this->village->points, $this->sim->workforce()->foodBudget);
|
||||
}
|
||||
|
||||
/** @return array<string, int> */
|
||||
public function current(): array
|
||||
{
|
||||
return Military::of($this->village, $this->sim)->troopsAtHome();
|
||||
}
|
||||
|
||||
/** How much of full strength stands, 0..1. */
|
||||
public function strength(): float
|
||||
{
|
||||
return $this->garrison->strength($this->current(), $this->full());
|
||||
}
|
||||
|
||||
/** Bring the garrison to full strength at once (a new village, or one from before garrisons). */
|
||||
public function fill(): void
|
||||
{
|
||||
$this->set($this->full());
|
||||
}
|
||||
|
||||
/** One regroup step; the next is scheduled while the garrison is still short. */
|
||||
public function regroup(CarbonImmutable $at): void
|
||||
{
|
||||
if ($this->village->player_id !== null) {
|
||||
return;
|
||||
}
|
||||
$this->sim->settle($at);
|
||||
$this->set($this->garrison->regroup($this->current(), $this->full()));
|
||||
$this->scheduleRegroup($at);
|
||||
}
|
||||
|
||||
/** Schedule the next regroup step if the garrison is short and none is waiting. */
|
||||
public function scheduleRegroup(CarbonImmutable $at): void
|
||||
{
|
||||
if ($this->village->player_id !== null || $this->garrison->isFull($this->current(), $this->full())) {
|
||||
return;
|
||||
}
|
||||
$waiting = GameEvent::where('type', GameEvent::BARBARIAN_REGROUPS)->where('village_id', $this->village->id)->whereNull('processed_at')->exists();
|
||||
if (! $waiting) {
|
||||
GameEvent::create([
|
||||
'world_id' => $this->village->world_id,
|
||||
'village_id' => $this->village->id,
|
||||
'type' => GameEvent::BARBARIAN_REGROUPS,
|
||||
'fire_at' => $at->addSeconds($this->garrison->interval()),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/** Raiders coming home stay only as far as the garrison has room for them. */
|
||||
public function trim(): void
|
||||
{
|
||||
$full = $this->full();
|
||||
$kept = [];
|
||||
foreach ($this->current() as $unit => $count) {
|
||||
$kept[$unit] = min($count, $full[$unit] ?? 0);
|
||||
}
|
||||
$this->set($kept);
|
||||
}
|
||||
|
||||
/** @param array<string, int> $units the garrison's own troops from now on */
|
||||
private function set(array $units): void
|
||||
{
|
||||
$own = $this->village->troops->filter(fn (UnitStack $s) => $s->home_village_id === $this->village->id);
|
||||
foreach ($own as $stack) {
|
||||
$count = $units[$stack->unit] ?? 0;
|
||||
if ($count > 0) {
|
||||
$stack->update(['count' => $count]);
|
||||
} else {
|
||||
$stack->delete();
|
||||
}
|
||||
unset($units[$stack->unit]);
|
||||
}
|
||||
$kept = $this->village->troops->filter(fn (UnitStack $s) => $s->exists);
|
||||
foreach (array_filter($units) as $unit => $count) {
|
||||
$kept->push($this->village->troops()->create(['home_village_id' => $this->village->id, 'unit' => $unit, 'count' => $count]));
|
||||
}
|
||||
$this->village->setRelation('troops', $kept->values());
|
||||
}
|
||||
}
|
||||
|
|
@ -113,6 +113,7 @@ final class Engine
|
|||
match ($event->type) {
|
||||
GameEvent::BUILD_ORDER_FINISHED => $this->finishBuildOrder($village, $event),
|
||||
GameEvent::BARBARIAN_GROWS => $this->growBarbarian($village, $event),
|
||||
GameEvent::BARBARIAN_REGROUPS => Barbarians::of($village)->regroup($event->fire_at),
|
||||
GameEvent::RECRUIT_FINISHED => Military::of($village)->settle($event->fire_at),
|
||||
GameEvent::LEVY_ENDS => Military::of($village)->endLevy($event->fire_at),
|
||||
// The stock just hit 0; the sync() after every event starts the grace timer.
|
||||
|
|
@ -142,6 +143,8 @@ final class Engine
|
|||
$sim->settle($event->fire_at);
|
||||
$village->buildings->firstWhere('id', $state->id)->increment('level');
|
||||
$village->points = $sim->points();
|
||||
// A bigger village fields a bigger garrison.
|
||||
Barbarians::of($village, $sim)->scheduleRegroup($event->fire_at);
|
||||
GameEvent::create([
|
||||
'world_id' => $village->world_id,
|
||||
'village_id' => $village->id,
|
||||
|
|
|
|||
|
|
@ -265,7 +265,8 @@ final class Rally
|
|||
$defenderPoints = $this->village->player_id ? (int) Village::where('player_id', $this->village->player_id)->sum('points') : null;
|
||||
mt_srand($movement->rng_seed);
|
||||
$luck = $combat->luck(mt_rand() / (mt_getrandmax() + 1));
|
||||
$morale = $combat->morale($defenderPoints, (int) Village::where('player_id', $movement->player_id)->sum('points'));
|
||||
// Raiders (no player) attack at full strength.
|
||||
$morale = $combat->morale($defenderPoints, $movement->player_id ? (int) Village::where('player_id', $movement->player_id)->sum('points') : 0);
|
||||
$night = $combat->night($at);
|
||||
|
||||
// Scouts meet the defending scouts first and never fight; the rest of the army does.
|
||||
|
|
@ -394,6 +395,10 @@ final class Rally
|
|||
Push::queue($home->id);
|
||||
}
|
||||
Push::queue($movement->origin_id);
|
||||
// A barbarian garrison that lost men starts to regroup.
|
||||
if (! $conquered) {
|
||||
Barbarians::of($this->village, $this->sim)->scheduleRegroup($at);
|
||||
}
|
||||
|
||||
if ($conquered) {
|
||||
$this->conquer($movement, $survivors, $at);
|
||||
|
|
@ -446,6 +451,10 @@ final class Rally
|
|||
if ($movement->cargo) {
|
||||
$this->sim->refund($movement->cargo);
|
||||
}
|
||||
// Raiders back at their camp stay only as far as its garrison has room.
|
||||
if ($this->village->player_id === null) {
|
||||
Barbarians::of($this->village, $this->sim)->trim();
|
||||
}
|
||||
$movement->delete();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ final class ConfigLint
|
|||
...self::unitsTrainable($config, $rules),
|
||||
...self::timersRun($config),
|
||||
...self::questsReachable($config, $rules),
|
||||
...self::garrisonsRegroup($config),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -74,6 +75,35 @@ final class ConfigLint
|
|||
return $problems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Barbarian garrisons field real units, regroup in finite steps, and eat less than
|
||||
* their village's villagers leave over, so they never starve it.
|
||||
*/
|
||||
private static function garrisonsRegroup(WorldConfig $config): array
|
||||
{
|
||||
$garrison = $config->get('world.map.barbarians.garrison');
|
||||
if (! $garrison) {
|
||||
return [];
|
||||
}
|
||||
$units = (new UnitRules($config))->units();
|
||||
$problems = [];
|
||||
foreach ($garrison['profiles'] ?? [] as $profile => $shares) {
|
||||
foreach ($shares as $unit => $share) {
|
||||
if (! in_array($unit, $units, true) || $share <= 0) {
|
||||
$problems[] = "Garrison profile [$profile] fields [$unit], which is not a unit with a share above 0.";
|
||||
}
|
||||
}
|
||||
}
|
||||
if (($garrison['regroup']['every_hours'] ?? 0) <= 0 || ($garrison['regroup']['share'] ?? 0) <= 0) {
|
||||
$problems[] = 'Barbarian garrisons must regroup: [regroup.every_hours] and [regroup.share] above 0.';
|
||||
}
|
||||
if (($garrison['max_food_share'] ?? 0) >= $config->get('economy.villager_share', 0.8)) {
|
||||
$problems[] = '[garrison.max_food_share] must stay below [economy.villager_share], or a garrison starves its village.';
|
||||
}
|
||||
|
||||
return $problems;
|
||||
}
|
||||
|
||||
/** A levy has to end and desertion waves have to come apart in time, or the village stays stuck. */
|
||||
private static function timersRun(WorldConfig $config): array
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,147 @@
|
|||
<?php
|
||||
|
||||
namespace App\Game\Map;
|
||||
|
||||
use App\Game\Military\UnitRules;
|
||||
use App\Game\WorldConfig;
|
||||
|
||||
/**
|
||||
* The troops a barbarian village holds (world.map.barbarians.garrison). Full strength
|
||||
* follows its points, in one of a few profiles fixed by the village id, so scouting a
|
||||
* village tells an attacker what to send. After a hit it regroups step by step.
|
||||
*/
|
||||
final class BarbarianGarrison
|
||||
{
|
||||
private UnitRules $units;
|
||||
|
||||
public function __construct(private readonly WorldConfig $config)
|
||||
{
|
||||
$this->units = new UnitRules($config);
|
||||
}
|
||||
|
||||
/** @return list<string> */
|
||||
public function profiles(): array
|
||||
{
|
||||
return array_keys($this->config->get('world.map.barbarians.garrison.profiles', []));
|
||||
}
|
||||
|
||||
/** The profile a barbarian village fields, or null when the world has none. */
|
||||
public function profile(int $villageId): ?string
|
||||
{
|
||||
$profiles = $this->profiles();
|
||||
|
||||
return $profiles ? $profiles[$villageId % count($profiles)] : null;
|
||||
}
|
||||
|
||||
/** @return array<string, float> unit => share of heads */
|
||||
public function shares(string $profile): array
|
||||
{
|
||||
return $this->config->get("world.map.barbarians.garrison.profiles.$profile", []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Full strength: per_point × (points − free_points) heads split by the profile's
|
||||
* shares, cut down so its upkeep stays within max_food_share of the food budget.
|
||||
*
|
||||
* @return array<string, int>
|
||||
*/
|
||||
public function full(?string $profile, int $points, float $foodBudget): array
|
||||
{
|
||||
if ($profile === null) {
|
||||
return [];
|
||||
}
|
||||
$garrison = $this->config->get('world.map.barbarians.garrison');
|
||||
$shares = $this->shares($profile);
|
||||
$heads = (int) floor($garrison['per_point'] * max(0, $points - $garrison['free_points']));
|
||||
$perHead = array_sum(array_map(fn (string $u, float $s) => $s * $this->units->upkeep($u), array_keys($shares), $shares));
|
||||
if ($perHead > 0) {
|
||||
$heads = min($heads, (int) floor($garrison['max_food_share'] * $foodBudget / $perHead));
|
||||
}
|
||||
|
||||
return self::split(max(0, $heads), $shares);
|
||||
}
|
||||
|
||||
/**
|
||||
* One regroup step: every unit gets regroup.share of its full count back (at least
|
||||
* one), never above full.
|
||||
*
|
||||
* @param array<string, int> $current
|
||||
* @param array<string, int> $full
|
||||
* @return array<string, int>
|
||||
*/
|
||||
public function regroup(array $current, array $full): array
|
||||
{
|
||||
$share = (float) $this->config->get('world.map.barbarians.garrison.regroup.share', 0.25);
|
||||
$next = $current;
|
||||
foreach ($full as $unit => $count) {
|
||||
$have = $current[$unit] ?? 0;
|
||||
if ($have < $count) {
|
||||
$next[$unit] = min($count, $have + max(1, (int) ceil($share * $count)));
|
||||
}
|
||||
}
|
||||
|
||||
return $next;
|
||||
}
|
||||
|
||||
/**
|
||||
* How much of full strength stands, by food upkeep (a rider weighs three spears), 0..1.
|
||||
* A village without a garrison to field counts as full.
|
||||
*
|
||||
* @param array<string, int> $current
|
||||
* @param array<string, int> $full
|
||||
*/
|
||||
public function strength(array $current, array $full): float
|
||||
{
|
||||
$weigh = fn (array $units) => array_sum(array_map(fn (string $u, int $n) => $n * $this->units->upkeep($u), array_keys($units), $units));
|
||||
$whole = $weigh($full);
|
||||
if ($whole <= 0) {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
return min(1.0, $weigh(array_intersect_key($current, $full)) / $whole);
|
||||
}
|
||||
|
||||
public function isFull(array $current, array $full): bool
|
||||
{
|
||||
foreach ($full as $unit => $count) {
|
||||
if (($current[$unit] ?? 0) < $count) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Seconds between two regroup steps, at world speed. */
|
||||
public function interval(): int
|
||||
{
|
||||
$hours = $this->config->get('world.map.barbarians.garrison.regroup.every_hours', 2);
|
||||
|
||||
return max(1, (int) round($hours * 3600 / $this->config->speed()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Whole heads by share, largest remainders first, so they add up to $heads.
|
||||
*
|
||||
* @param array<string, float> $shares
|
||||
* @return array<string, int>
|
||||
*/
|
||||
public static function split(int $heads, array $shares): array
|
||||
{
|
||||
$total = array_sum($shares);
|
||||
if ($heads <= 0 || $total <= 0) {
|
||||
return [];
|
||||
}
|
||||
$exact = array_map(fn (float $s) => $heads * $s / $total, $shares);
|
||||
$counts = array_map(fn (float $n) => (int) floor($n), $exact);
|
||||
$rest = $heads - array_sum($counts);
|
||||
$remainders = array_map(fn (float $n, int $c) => $n - $c, $exact, $counts);
|
||||
arsort($remainders);
|
||||
$keys = array_keys($shares);
|
||||
foreach (array_slice(array_keys($remainders), 0, $rest) as $i) {
|
||||
$counts[$keys[$i]]++;
|
||||
}
|
||||
|
||||
return array_filter($counts);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,9 @@ class GameEvent extends Model
|
|||
|
||||
public const BARBARIAN_GROWS = 'barbarian.grows';
|
||||
|
||||
// A barbarian garrison gets part of its full strength back.
|
||||
public const BARBARIAN_REGROUPS = 'barbarian.regroups';
|
||||
|
||||
public const RECRUIT_FINISHED = 'recruit.finished';
|
||||
|
||||
public const LEVY_ENDS = 'levy.ends';
|
||||
|
|
|
|||
|
|
@ -41,6 +41,23 @@ return [
|
|||
'near' => 4,
|
||||
'max_level' => 3,
|
||||
'growth' => ['every_hours' => 8, 'max_level' => 10],
|
||||
// Troops a barbarian village holds at full strength: per_point × (points −
|
||||
// free_points) heads, split by its profile's shares (the profile follows from
|
||||
// the village id), and never eating more than max_food_share of its farms'
|
||||
// food budget. After a hit, or when the village grows, it regroups: every
|
||||
// regroup.every_hours (at world speed) it gets regroup.share of full strength back.
|
||||
'garrison' => [
|
||||
'per_point' => 0.1,
|
||||
'free_points' => 30,
|
||||
'max_food_share' => 0.5,
|
||||
'profiles' => [
|
||||
'spearmen' => ['spear' => 0.7, 'sword' => 0.3],
|
||||
'swordsmen' => ['sword' => 0.7, 'spear' => 0.3],
|
||||
'archers' => ['archer' => 0.6, 'spear' => 0.4],
|
||||
'riders' => ['light_cavalry' => 0.5, 'spear' => 0.5],
|
||||
],
|
||||
'regroup' => ['every_hours' => 2, 'share' => 0.25],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
|
|
|||
|
|
@ -22,9 +22,10 @@ class WorldSeeder extends Seeder
|
|||
'started_at' => now(),
|
||||
]),
|
||||
];
|
||||
// Barbarian villages that lost their growth event get it back.
|
||||
// Barbarian villages that lost their growth event get it back, and their garrison.
|
||||
foreach ($worlds as $world) {
|
||||
app(SpawnBarbarians::class)->scheduleGrowth($world);
|
||||
app(SpawnBarbarians::class)->scheduleGarrisons($world);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -182,6 +182,7 @@ The world grows the way Die Stämme's does (measured on de261, see the research
|
|||
- **Frontier** (`Frontier`): a new player's village lands at a random angle on a ring of radius `start_radius + sqrt(villages / (π · density))`, ± `band` fields, where `villages` counts every village of the world, barbarians included. `density` is DS's 0.29 villages per field (about one in 3.4 fields). The first tries keep clear of the 8 fields around other players; barbarians may stand right next to a new village. If the ring has no room, the nearest free field around 0|0 is taken.
|
||||
- **Direction**: when joining (`world.join {world_id, direction}`) a player picks random, nw, ne, sw or se, and the angle stays inside that quarter.
|
||||
- **Barbarians come with players** (`SpawnBarbarians`): each join brings `barbarians.per_join` of them on average (1.3: one, and a second with 30% chance, as in DS), placed within `barbarians.near` fields of the new village so a small world has something to loot from the start (DS scatters them on the frontier instead; with thousands of players that ends up the same). Their buildings start at random levels up to `max_level`, and each has a `barbarian.grows` event: every `growth.every_hours` (at world speed) one building goes up a level, up to `growth.max_level`. Bonus villages (DS adds 0.4 per join) come later.
|
||||
- **Garrisons** (`BarbarianGarrison`, `Barbarians`): a barbarian village holds troops, so raiding it takes scouting and an army sized for it. Full strength is `garrison.per_point × (points − free_points)` heads, split by the village's profile (spearmen, swordsmen, archers or riders, fixed by its id, so a scout report tells what to send), and its upkeep stays under `max_food_share` (50%) of the village's food budget, so it never starves. A new barbarian village starts at full strength. After a battle there, or when it grows, a `barbarian.regroups` event gives back `regroup.share` (25%, at least one per unit) every `regroup.every_hours` (2) until it is full again. Their stock grows from their own buildings, settled like any village, up to their storage. Barbarian villages from before garrisons get theirs on deploy (`WorldSeeder`).
|
||||
- A village whose player has been inactive for too long turns barbarian (M6).
|
||||
- `php artisan game:reset-world {slug}` wipes a world back to empty (villages, players, movements, reports, events) and keeps user accounts and the world's config.
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
use App\Actions\SendTroops;
|
||||
use App\Engine\Barbarians;
|
||||
use App\Engine\VillageFactory;
|
||||
use App\Models\GameEvent;
|
||||
use App\Models\Village;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Database\Seeders\WorldSeeder;
|
||||
|
||||
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-29 12:00:00')));
|
||||
|
||||
/** A barbarian village with every start building at this level, garrison not filled. */
|
||||
function camp(Village $near, int $level, int $dx = 3, int $dy = 0): Village
|
||||
{
|
||||
$types = ['headquarters', 'storage', 'farm', 'timber_camp', 'clay_pit', 'iron_mine'];
|
||||
|
||||
return app(VillageFactory::class)->create($near->world, null, 'Barbarian village', $near->x + $dx, $near->y + $dy, array_fill_keys($types, $level));
|
||||
}
|
||||
|
||||
/** @return array<string, int> the camp's own troops */
|
||||
function garrisonOf(Village $camp): array
|
||||
{
|
||||
return $camp->fresh()->troops->where('home_village_id', $camp->id)->mapWithKeys(fn ($s) => [$s->unit => $s->count])->all();
|
||||
}
|
||||
|
||||
function heads(Village $camp): int
|
||||
{
|
||||
return array_sum(garrisonOf($camp));
|
||||
}
|
||||
|
||||
it('brings a garrison with every new barbarian village', function () {
|
||||
config(['game.world.map.barbarians.per_join' => 1, 'game.world.map.barbarians.garrison.free_points' => 0]);
|
||||
$village = startVillage();
|
||||
$barbarian = Village::whereNull('player_id')->sole();
|
||||
|
||||
// 0.1 a point, in the village's profile.
|
||||
expect(heads($barbarian))->toBe((int) floor(0.1 * $barbarian->points))
|
||||
->and(garrisonOf($barbarian))->toEqual(Barbarians::of($barbarian)->full())
|
||||
->and(array_keys(garrisonOf($barbarian)))->each->toBeIn(['spear', 'sword', 'archer', 'light_cavalry']);
|
||||
});
|
||||
|
||||
it('regroups a quarter of full strength every two hours after being wiped out', function () {
|
||||
$village = startVillage();
|
||||
$village->buildings()->create(['type' => 'rally_point', 'level' => 1]);
|
||||
$village->troops()->create(['home_village_id' => $village->id, 'unit' => 'light_cavalry', 'count' => 100]);
|
||||
$camp = camp($village, 8);
|
||||
Barbarians::of($camp)->fill();
|
||||
$full = garrisonOf($camp);
|
||||
expect(array_sum($full))->toBeGreaterThan(4);
|
||||
|
||||
app(SendTroops::class)(owner($village), $village->id, 'attack', $camp->x, $camp->y, ['light_cavalry' => 100], null, null);
|
||||
$this->travel(30)->minutes();
|
||||
$this->artisan('game:events --once')->assertSuccessful();
|
||||
expect(garrisonOf($camp))->toBe([])
|
||||
->and(GameEvent::where('type', GameEvent::BARBARIAN_REGROUPS)->whereNull('processed_at')->value('fire_at')->toDateTimeString())->toBe('2026-10-29 14:30:00');
|
||||
|
||||
$this->travel(2)->hours();
|
||||
$this->artisan('game:events --once')->assertSuccessful();
|
||||
$step = array_map(fn (int $n) => max(1, (int) ceil(0.25 * $n)), $full);
|
||||
expect(garrisonOf($camp))->toEqual($step);
|
||||
|
||||
$this->travel(6)->hours();
|
||||
$this->artisan('game:events --once')->assertSuccessful();
|
||||
expect(garrisonOf($camp))->toEqual($full)
|
||||
->and(GameEvent::where('type', GameEvent::BARBARIAN_REGROUPS)->whereNull('processed_at')->exists())->toBeFalse();
|
||||
});
|
||||
|
||||
it('fields more men as the village grows', function () {
|
||||
config(['game.world.map.barbarians.garrison.per_point' => 1, 'game.world.map.barbarians.garrison.max_food_share' => 10]);
|
||||
$village = startVillage();
|
||||
$camp = camp($village, 3);
|
||||
Barbarians::of($camp)->fill();
|
||||
$before = heads($camp);
|
||||
GameEvent::create(['world_id' => $camp->world_id, 'village_id' => $camp->id, 'type' => GameEvent::BARBARIAN_GROWS, 'fire_at' => now()]);
|
||||
|
||||
$this->artisan('game:events --once')->assertSuccessful();
|
||||
$grown = $camp->fresh()->points;
|
||||
expect($grown)->toBeGreaterThan($camp->points);
|
||||
|
||||
$this->travel(2)->hours();
|
||||
$this->artisan('game:events --once')->assertSuccessful();
|
||||
expect(heads($camp))->toBe($grown - 30)->and(heads($camp))->toBeGreaterThan($before);
|
||||
});
|
||||
|
||||
it('gives barbarian villages from before garrisons theirs on deploy, once', function () {
|
||||
$village = startVillage();
|
||||
$camp = camp($village, 8);
|
||||
expect(garrisonOf($camp))->toBe([]);
|
||||
|
||||
(new WorldSeeder)->run();
|
||||
$full = garrisonOf($camp);
|
||||
expect($full)->toEqual(Barbarians::of($camp->fresh())->full())->and($full)->not->toBe([]);
|
||||
|
||||
(new WorldSeeder)->run();
|
||||
expect(garrisonOf($camp))->toEqual($full);
|
||||
});
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
use App\Game\Map\BarbarianGarrison;
|
||||
|
||||
it('picks a profile by village id', function () {
|
||||
$garrison = new BarbarianGarrison(gameConfig());
|
||||
|
||||
expect($garrison->profile(0))->toBe('spearmen')
|
||||
->and($garrison->profile(3))->toBe('riders')
|
||||
->and($garrison->profile(5))->toBe('swordsmen');
|
||||
});
|
||||
|
||||
it('grows full strength with the points above the free ones', function () {
|
||||
$garrison = new BarbarianGarrison(gameConfig());
|
||||
|
||||
// 0.1 × (40 − 30) = 1 head; 0.1 × (230 − 30) = 20 heads, 70/30 spears and swords.
|
||||
expect($garrison->full('spearmen', 30, 100))->toBe([])
|
||||
->and($garrison->full('spearmen', 40, 100))->toBe(['spear' => 1])
|
||||
->and($garrison->full('spearmen', 230, 100))->toBe(['spear' => 14, 'sword' => 6])
|
||||
->and($garrison->full(null, 230, 100))->toBe([]);
|
||||
});
|
||||
|
||||
it('never eats more than its share of the food budget', function () {
|
||||
$garrison = new BarbarianGarrison(gameConfig());
|
||||
|
||||
// Riders eat (0.5 × 3 + 0.5 × 1) = 2 a head; half of 30 food/h feeds 7 heads.
|
||||
expect($garrison->full('riders', 230, 30))->toBe(['light_cavalry' => 4, 'spear' => 3]);
|
||||
});
|
||||
|
||||
it('splits heads by largest remainder so they add up', function () {
|
||||
expect(BarbarianGarrison::split(5, ['spear' => 0.7, 'sword' => 0.3]))->toBe(['spear' => 4, 'sword' => 1])
|
||||
->and(BarbarianGarrison::split(1, ['archer' => 0.6, 'spear' => 0.4]))->toBe(['archer' => 1])
|
||||
->and(BarbarianGarrison::split(0, ['spear' => 1.0]))->toBe([]);
|
||||
});
|
||||
|
||||
it('regroups a quarter of full strength per step, at least one, never above full', function () {
|
||||
$garrison = new BarbarianGarrison(gameConfig());
|
||||
$full = ['spear' => 14, 'sword' => 6];
|
||||
|
||||
expect($garrison->regroup([], $full))->toBe(['spear' => 4, 'sword' => 2])
|
||||
->and($garrison->regroup(['spear' => 12, 'sword' => 6], $full))->toBe(['spear' => 14, 'sword' => 6])
|
||||
->and($garrison->regroup([], ['spear' => 1]))->toBe(['spear' => 1])
|
||||
->and($garrison->isFull(['spear' => 14, 'sword' => 6], $full))->toBeTrue()
|
||||
->and($garrison->isFull(['spear' => 14], $full))->toBeFalse();
|
||||
});
|
||||
|
||||
it('weighs strength by upkeep', function () {
|
||||
$garrison = new BarbarianGarrison(gameConfig());
|
||||
$full = ['light_cavalry' => 2, 'spear' => 2];
|
||||
|
||||
// 2 riders eat 6 of the 8 food.
|
||||
expect($garrison->strength(['light_cavalry' => 2], $full))->toBe(0.75)
|
||||
->and($garrison->strength([], $full))->toBe(0.0)
|
||||
->and($garrison->strength(['spear' => 9, 'light_cavalry' => 2], $full))->toBe(1.0)
|
||||
->and($garrison->strength([], []))->toBe(1.0);
|
||||
});
|
||||
|
||||
it('regroups every few hours at world speed', function () {
|
||||
expect((new BarbarianGarrison(gameConfig()))->interval())->toBe(7200)
|
||||
->and((new BarbarianGarrison(gameConfig(['world' => ['speed' => 100]])))->interval())->toBe(72);
|
||||
});
|
||||
|
|
@ -61,3 +61,16 @@ it('reports a levy or desertion timer that never runs out', function () {
|
|||
'[economy.starvation.grace] cannot be negative.',
|
||||
]);
|
||||
});
|
||||
|
||||
it('reports garrisons that would starve their village or never regroup', function () {
|
||||
$config = gameConfig(['world' => ['map' => ['barbarians' => ['garrison' => [
|
||||
'max_food_share' => 0.9,
|
||||
'regroup' => ['every_hours' => 0],
|
||||
'profiles' => ['ghosts' => ['wraith' => 1.0]],
|
||||
]]]]]);
|
||||
|
||||
expect(ConfigLint::check($config))
|
||||
->toContain('Garrison profile [ghosts] fields [wraith], which is not a unit with a share above 0.')
|
||||
->toContain('Barbarian garrisons must regroup: [regroup.every_hours] and [regroup.share] above 0.')
|
||||
->toContain('[garrison.max_food_share] must stay below [economy.villager_share], or a garrison starves its village.');
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue