feat: copies of a building type work at a falling share (economy.instance_share 0.5)
Instances of a type rank by level, then id; rank n works at 0.5^n: production, the farm's nominal food, storage, hides, hospital beds and training speed. Workers stay full. The payload carries instance_share on every building and build option, and effects/next_effects at the building's share. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
12a994e91c
commit
5b5a412d71
|
|
@ -28,7 +28,7 @@ final class Hospital
|
||||||
return new Wounds($this->sim->config());
|
return new Wounds($this->sim->config());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Beds of all hospitals here, each as far as it is staffed. */
|
/** Beds of all hospitals here, each as far as it is staffed, a copy at its instance share. */
|
||||||
public function beds(): int
|
public function beds(): int
|
||||||
{
|
{
|
||||||
$rules = $this->sim->rules();
|
$rules = $this->sim->rules();
|
||||||
|
|
@ -37,7 +37,7 @@ final class Hospital
|
||||||
return (int) floor($this->village->buildings->sum(function (Building $b) use ($rules, $workforce) {
|
return (int) floor($this->village->buildings->sum(function (Building $b) use ($rules, $workforce) {
|
||||||
$beds = $rules->beds($b->type, $b->level);
|
$beds = $rules->beds($b->type, $b->level);
|
||||||
|
|
||||||
return $beds > 0 ? $beds * $this->sim->staffingFactor($workforce->ratio($b->id)) : 0;
|
return $beds > 0 ? $beds * $this->sim->staffingFactor($workforce->ratio($b->id)) * $this->sim->share($b) : 0;
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -88,12 +88,12 @@ final class Military
|
||||||
return max(0, min($byResources, $byVillagers));
|
return max(0, min($byResources, $byVillagers));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Seconds per unit at this building instance, from its level and staffing. */
|
/** Seconds per unit at this building instance, from its level, staffing and instance share. */
|
||||||
public function secondsPerUnit(Building $building, string $unit): int
|
public function secondsPerUnit(Building $building, string $unit): int
|
||||||
{
|
{
|
||||||
$staffing = $this->sim->staffingFactor($this->sim->workforce()->ratio($building->id));
|
$staffing = $this->sim->staffingFactor($this->sim->workforce()->ratio($building->id));
|
||||||
|
|
||||||
return $this->units->seconds($unit, $building->level, $staffing, $this->sim->specialisation()->recruitBonus($this->sim->role()));
|
return $this->units->seconds($unit, $building->level, $staffing, $this->sim->specialisation()->recruitBonus($this->sim->role()), $this->sim->share($building));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Queue units at a military building. Pays up front; the villagers stop working at once. */
|
/** Queue units at a military building. Pays up front; the villagers stop working at once. */
|
||||||
|
|
|
||||||
|
|
@ -57,12 +57,12 @@ final class Rally
|
||||||
+ $this->village->buildings->sum(fn (Building $b) => $rules->detection($b->type, $b->level));
|
+ $this->village->buildings->sum(fn (Building $b) => $rules->detection($b->type, $b->level));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Wood, clay and iron each that raiders can't take. */
|
/** Wood, clay and iron each that raiders can't take; a copy hides at its instance share. */
|
||||||
public function hidden(): float
|
public function hidden(): float
|
||||||
{
|
{
|
||||||
$rules = $this->sim->rules();
|
$rules = $this->sim->rules();
|
||||||
|
|
||||||
return $this->village->buildings->sum(fn (Building $b) => $rules->hides($b->type, $b->level));
|
return $this->village->buildings->sum(fn (Building $b) => $rules->hides($b->type, $b->level) * $this->sim->share($b));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** When beginner protection ends for this player. */
|
/** When beginner protection ends for this player. */
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,9 @@ final class Simulation
|
||||||
/** @var array<int, int>|null building id => its index among its type */
|
/** @var array<int, int>|null building id => its index among its type */
|
||||||
private ?array $indexes = null;
|
private ?array $indexes = null;
|
||||||
|
|
||||||
|
/** @var array<int, float>|null building id => the share its instance works at */
|
||||||
|
private ?array $shares = null;
|
||||||
|
|
||||||
private ?string $workforceFor = null;
|
private ?string $workforceFor = null;
|
||||||
|
|
||||||
private function __construct(private readonly Village $village)
|
private function __construct(private readonly Village $village)
|
||||||
|
|
@ -148,7 +151,27 @@ final class Simulation
|
||||||
/** @return array{resource: string, per_hour: float}|null */
|
/** @return array{resource: string, per_hour: float}|null */
|
||||||
public function output(Building $building, Workforce $workforce): ?array
|
public function output(Building $building, Workforce $workforce): ?array
|
||||||
{
|
{
|
||||||
return $this->production->output($building->state(), $workforce, $this->specialisation()->productionBonus($this->role(), $building->type));
|
return $this->production->output($building->state(), $workforce, $this->specialisation()->productionBonus($this->role(), $building->type), $this->share($building));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The share each standing instance works at: copies of a type give less, ranked by
|
||||||
|
* level (BuildingRules::instanceShares).
|
||||||
|
*
|
||||||
|
* @return array<int, float> building id => share
|
||||||
|
*/
|
||||||
|
public function shares(): array
|
||||||
|
{
|
||||||
|
if ($this->frozen && $this->shares !== null) {
|
||||||
|
return $this->shares;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->shares = $this->rules->instanceShares($this->states());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function share(Building $building): float
|
||||||
|
{
|
||||||
|
return $this->shares()[$building->id] ?? 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function specialisation(): Specialisation
|
public function specialisation(): Specialisation
|
||||||
|
|
@ -169,7 +192,9 @@ final class Simulation
|
||||||
|
|
||||||
public function storageCap(): float
|
public function storageCap(): float
|
||||||
{
|
{
|
||||||
return $this->village->buildings->sum(fn (Building $b) => $this->rules->capacity($b->type, $b->level));
|
$shares = $this->shares();
|
||||||
|
|
||||||
|
return $this->village->buildings->sum(fn (Building $b) => $this->rules->capacity($b->type, $b->level) * ($shares[$b->id] ?? 1.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Write stored + rate × elapsed up to $at into the village. Call before any change. */
|
/** Write stored + rate × elapsed up to $at into the village. Call before any change. */
|
||||||
|
|
@ -377,6 +402,8 @@ final class Simulation
|
||||||
'space' => $this->rules->space($type),
|
'space' => $this->rules->space($type),
|
||||||
'target_level' => 1,
|
'target_level' => 1,
|
||||||
'cost' => $this->rules->cost($type, 1, $this->instanceIndex($type)),
|
'cost' => $this->rules->cost($type, 1, $this->instanceIndex($type)),
|
||||||
|
// A new instance ranks after all of its type, standing or queued.
|
||||||
|
'instance_share' => $this->rules->instanceShare($this->instances($type)),
|
||||||
'seconds' => $this->rules->buildSeconds($type, 1, $this->hqLevel(), $this->hqStaffing()),
|
'seconds' => $this->rules->buildSeconds($type, 1, $this->hqLevel(), $this->hqStaffing()),
|
||||||
'blocked' => $blocked,
|
'blocked' => $blocked,
|
||||||
// What is still missing, for the "not yet available" list.
|
// What is still missing, for the "not yet available" list.
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,33 @@ final class BuildingRules
|
||||||
return max(1, (int) ceil($seconds));
|
return max(1, (int) ceil($seconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @param list<BuildingState> $buildings
|
||||||
|
* @return array<int, float> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The share the instance at this rank among its type works at (0 = the first). */
|
||||||
|
public function instanceShare(int $rank): float
|
||||||
|
{
|
||||||
|
return (float) $this->config->get('economy.instance_share', 1.0) ** $rank;
|
||||||
|
}
|
||||||
|
|
||||||
/** Seconds to take $level down by one (level 1 down means removing the instance). */
|
/** Seconds to take $level down by one (level 1 down means removing the instance). */
|
||||||
public function demolishSeconds(string $type, int $level, int $hqLevel, float $hqStaffing): int
|
public function demolishSeconds(string $type, int $level, int $hqLevel, float $hqStaffing): int
|
||||||
{
|
{
|
||||||
|
|
@ -127,19 +154,21 @@ final class BuildingRules
|
||||||
* What one instance does at a level, for the building's own screen: production at
|
* What one instance does at a level, for the building's own screen: production at
|
||||||
* full staffing, storage, hiding, beds, detection, the wall's defence, the headquarters'
|
* full staffing, storage, hiding, beds, detection, the wall's defence, the headquarters'
|
||||||
* build speed, builders and space, a military building's training speed. Percentages
|
* build speed, builders and space, a military building's training speed. Percentages
|
||||||
* are the bonus over level 1 (or over no wall).
|
* are the bonus over level 1 (or over no wall). A copy shows its output, storage,
|
||||||
|
* hiding, beds and training speed at its $share (instanceShares), so its training
|
||||||
|
* speed can fall below level 1's.
|
||||||
*
|
*
|
||||||
* @return list<array{key: string, value: float|int, resource?: string}>
|
* @return list<array{key: string, value: float|int, resource?: string}>
|
||||||
*/
|
*/
|
||||||
public function effects(string $type, int $level): array
|
public function effects(string $type, int $level, float $share = 1.0): array
|
||||||
{
|
{
|
||||||
$effects = [];
|
$effects = [];
|
||||||
if ($output = $this->output($type, $level)) {
|
if ($output = $this->output($type, $level)) {
|
||||||
$effects[] = ['key' => 'production', 'value' => round($output['per_hour'], 1), 'resource' => $output['resource']];
|
$effects[] = ['key' => 'production', 'value' => round($output['per_hour'] * $share, 1), 'resource' => $output['resource']];
|
||||||
}
|
}
|
||||||
foreach (['capacity', 'hides', 'beds'] as $key) {
|
foreach (['capacity', 'hides', 'beds'] as $key) {
|
||||||
if ($value = $this->{$key}($type, $level)) {
|
if ($value = $this->{$key}($type, $level)) {
|
||||||
$effects[] = ['key' => $key, 'value' => (int) round($value)];
|
$effects[] = ['key' => $key, 'value' => (int) round($value * $share)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($detection = $this->detection($type, $level)) {
|
if ($detection = $this->detection($type, $level)) {
|
||||||
|
|
@ -157,7 +186,8 @@ final class BuildingRules
|
||||||
}
|
}
|
||||||
$trains = array_filter($this->config->get('units', []), fn (array $u) => ($u['building'] ?? null) === $type);
|
$trains = array_filter($this->config->get('units', []), fn (array $u) => ($u['building'] ?? null) === $type);
|
||||||
if ($trains) {
|
if ($trains) {
|
||||||
$effects[] = ['key' => 'recruit_speed', 'value' => (int) round($this->config->get('economy.recruit_speed_per_level', 0) * self::above($level) * 100)];
|
$speed = (1 + $this->config->get('economy.recruit_speed_per_level', 0) * self::above($level)) * $share;
|
||||||
|
$effects[] = ['key' => 'recruit_speed', 'value' => (int) round(($speed - 1) * 100)];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $effects;
|
return $effects;
|
||||||
|
|
|
||||||
|
|
@ -22,14 +22,19 @@ final class Population
|
||||||
$this->rules = new BuildingRules($config);
|
$this->rules = new BuildingRules($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param list<BuildingState> $buildings */
|
/**
|
||||||
|
* Nominal farm output, a copy at its instance share.
|
||||||
|
*
|
||||||
|
* @param list<BuildingState> $buildings
|
||||||
|
*/
|
||||||
public function foodBudget(array $buildings): float
|
public function foodBudget(array $buildings): float
|
||||||
{
|
{
|
||||||
$budget = 0.0;
|
$budget = 0.0;
|
||||||
|
$shares = $this->rules->instanceShares($buildings);
|
||||||
foreach ($buildings as $b) {
|
foreach ($buildings as $b) {
|
||||||
$output = $this->rules->output($b->type, $b->level);
|
$output = $this->rules->output($b->type, $b->level);
|
||||||
if ($output && $output['resource'] === 'food') {
|
if ($output && $output['resource'] === 'food') {
|
||||||
$budget += $output['per_hour'];
|
$budget += $output['per_hour'] * $shares[$b->id];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,22 +30,23 @@ final class Production
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param float $bonus extra share from the village's role (0.2 = +20%)
|
* @param float $bonus extra share from the village's role (0.2 = +20%)
|
||||||
|
* @param float $share the instance's share among its type (BuildingRules::instanceShares)
|
||||||
* @return array{resource: string, per_hour: float}|null
|
* @return array{resource: string, per_hour: float}|null
|
||||||
*/
|
*/
|
||||||
public function output(BuildingState $building, Workforce $workforce, float $bonus = 0.0): ?array
|
public function output(BuildingState $building, Workforce $workforce, float $bonus = 0.0, float $share = 1.0): ?array
|
||||||
{
|
{
|
||||||
$output = $this->rules->output($building->type, $building->level);
|
$output = $this->rules->output($building->type, $building->level);
|
||||||
if (! $output) {
|
if (! $output) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$output['per_hour'] *= $this->staffingFactor($workforce->ratio($building->id)) * (1 + $bonus);
|
$output['per_hour'] *= $this->staffingFactor($workforce->ratio($building->id)) * (1 + $bonus) * $share;
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Net change per hour, with the village role's bonus on its buildings. Food is farm
|
* Net change per hour, with the village role's bonus on its buildings and copies at
|
||||||
* output minus upkeep, so it can go negative.
|
* their instance share. Food is farm output minus upkeep, so it can go negative.
|
||||||
*
|
*
|
||||||
* @param list<BuildingState> $buildings
|
* @param list<BuildingState> $buildings
|
||||||
* @return array{wood: float, clay: float, iron: float, food: float}
|
* @return array{wood: float, clay: float, iron: float, food: float}
|
||||||
|
|
@ -57,8 +58,9 @@ final class Production
|
||||||
$rates[$resource] += $perHour * $this->config->speed();
|
$rates[$resource] += $perHour * $this->config->speed();
|
||||||
}
|
}
|
||||||
$role = $this->specialisation->role(array_map(fn (BuildingState $b) => $b->type, $buildings));
|
$role = $this->specialisation->role(array_map(fn (BuildingState $b) => $b->type, $buildings));
|
||||||
|
$shares = $this->rules->instanceShares($buildings);
|
||||||
foreach ($buildings as $b) {
|
foreach ($buildings as $b) {
|
||||||
if ($output = $this->output($b, $workforce, $this->specialisation->productionBonus($role, $b->type))) {
|
if ($output = $this->output($b, $workforce, $this->specialisation->productionBonus($role, $b->type), $shares[$b->id])) {
|
||||||
$rates[$output['resource']] += $output['per_hour'];
|
$rates[$output['resource']] += $output['per_hour'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,15 +46,17 @@ final class UnitRules
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Seconds to train one unit, at world speed, sped up by the training building's
|
* Seconds to train one unit, at world speed, sped up by the training building's
|
||||||
* level and a military village's bonus, and slowed by its staffing (output factor 0..1).
|
* level and a military village's bonus, and slowed by its staffing (output factor 0..1)
|
||||||
|
* and, for a copy, by its instance share.
|
||||||
*/
|
*/
|
||||||
public function seconds(string $unit, int $buildingLevel, float $staffing, float $bonus = 0.0): int
|
public function seconds(string $unit, int $buildingLevel, float $staffing, float $bonus = 0.0, float $share = 1.0): int
|
||||||
{
|
{
|
||||||
$seconds = $this->unit($unit)['build_time']
|
$seconds = $this->unit($unit)['build_time']
|
||||||
/ $this->config->speed()
|
/ $this->config->speed()
|
||||||
/ (1 + $this->config->get('economy.recruit_speed_per_level', 0) * BuildingRules::above($buildingLevel))
|
/ (1 + $this->config->get('economy.recruit_speed_per_level', 0) * BuildingRules::above($buildingLevel))
|
||||||
/ (1 + $bonus)
|
/ (1 + $bonus)
|
||||||
/ max($staffing, 0.01);
|
/ max($staffing, 0.01)
|
||||||
|
/ $share;
|
||||||
|
|
||||||
return max(1, (int) ceil($seconds));
|
return max(1, (int) ceil($seconds));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,7 @@ final class VillagePresenter
|
||||||
'builders' => $sim->builders(),
|
'builders' => $sim->builders(),
|
||||||
'buildings' => $village->buildings->map(function (Building $b) use ($sim, $workforce, $rules) {
|
'buildings' => $village->buildings->map(function (Building $b) use ($sim, $workforce, $rules) {
|
||||||
$output = $sim->output($b, $workforce);
|
$output = $sim->output($b, $workforce);
|
||||||
|
$share = $sim->share($b);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => $b->id,
|
'id' => $b->id,
|
||||||
|
|
@ -100,9 +101,11 @@ final class VillagePresenter
|
||||||
'workers' => ['assigned' => $workforce->assigned[$b->id] ?? 0, 'required' => $workforce->required[$b->id] ?? 0],
|
'workers' => ['assigned' => $workforce->assigned[$b->id] ?? 0, 'required' => $workforce->required[$b->id] ?? 0],
|
||||||
'staffing' => round($workforce->ratio($b->id), 4),
|
'staffing' => round($workforce->ratio($b->id), 4),
|
||||||
'production' => $output ? ['resource' => $output['resource'], 'per_hour' => round($output['per_hour'], 2)] : null,
|
'production' => $output ? ['resource' => $output['resource'], 'per_hour' => round($output['per_hour'], 2)] : null,
|
||||||
// What it does at this level and would do one level up, for its own screen.
|
// 1 for the first or only one of its type; a copy works at less.
|
||||||
'effects' => $rules->effects($b->type, $b->level),
|
'instance_share' => $share,
|
||||||
'next_effects' => $b->level < $rules->maxLevel($b->type) ? $rules->effects($b->type, $b->level + 1) : null,
|
// What it does at this level and would do one level up, at its share, for its own screen.
|
||||||
|
'effects' => $rules->effects($b->type, $b->level, $share),
|
||||||
|
'next_effects' => $b->level < $rules->maxLevel($b->type) ? $rules->effects($b->type, $b->level + 1, $share) : null,
|
||||||
'upgrade' => $sim->upgradeOffer($b),
|
'upgrade' => $sim->upgradeOffer($b),
|
||||||
'demolish' => $sim->demolishOffer($b),
|
'demolish' => $sim->demolishOffer($b),
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,9 @@
|
||||||
// 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).
|
||||||
// The n-th instance of a type (0-based) costs instance_cost_factor^n times as much.
|
// 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
|
||||||
|
// 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)).
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,11 @@ return [
|
||||||
// Output at staffing ratio r is e^(k·(r−1)): 100% full, about 10% empty.
|
// Output at staffing ratio r is e^(k·(r−1)): 100% full, about 10% empty.
|
||||||
'staffing' => ['k' => 2.3],
|
'staffing' => ['k' => 2.3],
|
||||||
|
|
||||||
|
// Copies of a building type work at a falling share. The instances of a type rank by
|
||||||
|
// level (highest first, then oldest); the one at rank n (0-based) gives instance_share^n
|
||||||
|
// of its output, storage, hides, beds and training speed, but needs all its workers.
|
||||||
|
'instance_share' => 0.5,
|
||||||
|
|
||||||
// Share of a level's cost refunded when it is demolished, and its build time spent doing so.
|
// Share of a level's cost refunded when it is demolished, and its build time spent doing so.
|
||||||
'demolish_refund' => 0.5,
|
'demolish_refund' => 0.5,
|
||||||
'demolish_time_factor' => 0.5,
|
'demolish_time_factor' => 0.5,
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,8 @@ it('makes a later instance of a type cost more', function () {
|
||||||
$village = startVillage();
|
$village = startVillage();
|
||||||
|
|
||||||
$farms = collect(shown($village)['buildable'])->firstWhere('type', 'farm');
|
$farms = collect(shown($village)['buildable'])->firstWhere('type', 'farm');
|
||||||
expect($farms['cost'])->toBe(['wood' => 90, 'clay' => 80, 'iron' => 60]);
|
expect($farms['cost'])->toBe(['wood' => 90, 'clay' => 80, 'iron' => 60])
|
||||||
|
->and($farms['instance_share'])->toBe(0.5);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('keeps a third order waiting and starts it when a builder frees up', function () {
|
it('keeps a third order waiting and starts it when a builder frees up', function () {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,95 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Actions\QueueBuild;
|
||||||
|
use App\Actions\QueueUpgrade;
|
||||||
|
use App\Actions\Recruit;
|
||||||
|
use App\Engine\Rally;
|
||||||
|
use App\Models\Village;
|
||||||
|
use Carbon\CarbonImmutable;
|
||||||
|
|
||||||
|
beforeEach(fn () => $this->travelTo(CarbonImmutable::parse('2026-10-30 12:00:00')));
|
||||||
|
|
||||||
|
/** A start village with a farm big enough to staff everything, plus these level-1 instances. */
|
||||||
|
function villageWith(array $types): Village
|
||||||
|
{
|
||||||
|
$village = startVillage();
|
||||||
|
$village->buildings()->where('type', 'farm')->update(['level' => 10]);
|
||||||
|
foreach ($types as $type) {
|
||||||
|
$village->buildings()->create(['type' => $type, 'level' => 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $village->fresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
it('lets a second timber camp cost double and produce half', function () {
|
||||||
|
$village = villageWith(['timber_camp']);
|
||||||
|
$option = collect(shown($village)['buildable'])->firstWhere('type', 'timber_camp');
|
||||||
|
expect($option)->toMatchArray(['cost' => ['wood' => 100, 'clay' => 120, 'iron' => 80], 'instance_share' => 0.5]);
|
||||||
|
|
||||||
|
app(QueueBuild::class)(owner($village), $village->id, 'timber_camp');
|
||||||
|
$state = shown($village);
|
||||||
|
expect($state['resources'])->toMatchArray(['wood' => 400.0, 'clay' => 380.0, 'iron' => 420.0])
|
||||||
|
// A third one would rank after the queued copy.
|
||||||
|
->and(collect($state['buildable'])->firstWhere('type', 'timber_camp'))
|
||||||
|
->toMatchArray(['cost' => ['wood' => 200, 'clay' => 240, 'iron' => 160], 'instance_share' => 0.25]);
|
||||||
|
|
||||||
|
$this->travel(90)->seconds();
|
||||||
|
$state = shown($village);
|
||||||
|
$camps = collect($state['buildings'])->where('type', 'timber_camp')->values();
|
||||||
|
expect($camps)->toHaveCount(2)
|
||||||
|
->and($camps[0])->toMatchArray(['instance_share' => 1.0, 'production' => ['resource' => 'wood', 'per_hour' => 30.0]])
|
||||||
|
->and($camps[1])->toMatchArray(['instance_share' => 0.5, 'production' => ['resource' => 'wood', 'per_hour' => 15.0]])
|
||||||
|
// A copy needs all its workers all the same.
|
||||||
|
->and($camps[1]['workers'])->toBe(['assigned' => 2, 'required' => 2])
|
||||||
|
->and($state['rates']['wood'])->toBe(5 + 30.0 + 15.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows a copy\'s effects at its share, now and one level up', function () {
|
||||||
|
$state = shown(villageWith(['timber_camp', 'timber_camp']));
|
||||||
|
[$first, $copy] = collect($state['buildings'])->where('type', 'timber_camp')->values()->all();
|
||||||
|
|
||||||
|
expect($first['effects'])->toBe([['key' => 'production', 'value' => 30.0, 'resource' => 'wood']])
|
||||||
|
->and($copy['effects'])->toBe([['key' => 'production', 'value' => 15.0, 'resource' => 'wood']])
|
||||||
|
->and($copy['next_effects'])->toBe([['key' => 'production', 'value' => round(30 * 1.163 * 0.5, 1), 'resource' => 'wood']])
|
||||||
|
// Upgrading the copy costs double too.
|
||||||
|
->and($copy['upgrade']['cost'])->toBe(['wood' => 125, 'clay' => 150, 'iron' => 100])
|
||||||
|
->and(collect($state['buildings'])->firstWhere('type', 'headquarters')['instance_share'])->toBe(1.0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('adds half of a second storage to the cap, and swaps when the copy is upgraded past the first', function () {
|
||||||
|
$village = villageWith(['storage']);
|
||||||
|
expect(shown($village)['storage_cap'])->toEqual(1500);
|
||||||
|
|
||||||
|
$village->update(['wood' => 1000, 'clay' => 1000, 'iron' => 1000]);
|
||||||
|
$copy = $village->buildings()->where('type', 'storage')->get()->last();
|
||||||
|
app(QueueUpgrade::class)(owner($village), $village->id, $copy->id);
|
||||||
|
$this->travel(1)->hours();
|
||||||
|
|
||||||
|
$state = shown($village);
|
||||||
|
$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);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('trains at half the speed in a second barracks', function () {
|
||||||
|
$village = villageWith(['barracks', 'barracks']);
|
||||||
|
$village->update(['wood' => 1000, 'clay' => 1000, 'iron' => 1000]);
|
||||||
|
[$first, $copy] = $village->buildings->where('type', 'barracks')->values()->all();
|
||||||
|
|
||||||
|
$military = collect(shown($village)['military']);
|
||||||
|
$seconds = fn (int $id) => collect($military->firstWhere('building_id', $id)['units'])->firstWhere('unit', 'spear')['seconds'];
|
||||||
|
expect($seconds($first->id))->toBe(1020)
|
||||||
|
->and($seconds($copy->id))->toBe(2040);
|
||||||
|
|
||||||
|
app(Recruit::class)(owner($village), $village->id, $copy->id, 'spear', 2);
|
||||||
|
$queue = collect(shown($village)['military'])->firstWhere('building_id', $copy->id)['queue'];
|
||||||
|
expect($queue[0])->toMatchArray(['seconds_per_unit' => 2040, 'finishes_at' => '2026-10-30T13:08:00.000Z']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gives a second hospital half its beds and a second hiding place half its hides', function () {
|
||||||
|
$village = villageWith(['hospital', 'hospital', 'hiding_place', 'hiding_place']);
|
||||||
|
|
||||||
|
expect(shown($village)['hospital']['beds'])->toBe(30)
|
||||||
|
->and(Rally::of($village)->hidden())->toBe(150 + 75.0);
|
||||||
|
});
|
||||||
|
|
@ -23,6 +23,34 @@ it('makes later instances of a type more expensive', function () {
|
||||||
->and($rules->cost('headquarters', 1, 3))->toBe($rules->cost('headquarters', 1));
|
->and($rules->cost('headquarters', 1, 3))->toBe($rules->cost('headquarters', 1));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('lets copies of a type work at a falling share, ranked by level then id', function () {
|
||||||
|
$rules = new BuildingRules(gameConfig());
|
||||||
|
|
||||||
|
expect($rules->instanceShares([
|
||||||
|
building(4, 'timber_camp', 3),
|
||||||
|
building(2, 'timber_camp', 5),
|
||||||
|
building(1, 'farm', 2),
|
||||||
|
building(7, 'timber_camp', 3),
|
||||||
|
building(9, 'storage'),
|
||||||
|
]))->toEqual([2 => 1.0, 4 => 0.5, 7 => 0.25, 1 => 1.0, 9 => 1.0])
|
||||||
|
->and($rules->instanceShare(0))->toBe(1.0)
|
||||||
|
->and($rules->instanceShare(3))->toBe(0.125)
|
||||||
|
->and($rules->instanceShares([]))->toBe([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('swaps the shares when a copy is upgraded past the first', function () {
|
||||||
|
$rules = new BuildingRules(gameConfig());
|
||||||
|
|
||||||
|
expect($rules->instanceShares([building(1, 'storage', 4), building(2, 'storage', 4)]))->toEqual([1 => 1.0, 2 => 0.5])
|
||||||
|
->and($rules->instanceShares([building(1, 'storage', 4), building(2, 'storage', 5)]))->toEqual([2 => 1.0, 1 => 0.5]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('gives every instance its full share when the world turns copies off', function () {
|
||||||
|
$rules = new BuildingRules(gameConfig(['economy' => ['instance_share' => 1.0]]));
|
||||||
|
|
||||||
|
expect($rules->instanceShares([building(1, 'iron_mine'), building(2, 'iron_mine')]))->toEqual([1 => 1.0, 2 => 1.0]);
|
||||||
|
});
|
||||||
|
|
||||||
it('divides build time by world speed, headquarters level and staffing', function () {
|
it('divides build time by world speed, headquarters level and staffing', function () {
|
||||||
$rules = new BuildingRules(gameConfig());
|
$rules = new BuildingRules(gameConfig());
|
||||||
$fast = new BuildingRules(gameConfig(['world' => ['speed' => 100]]));
|
$fast = new BuildingRules(gameConfig(['world' => ['speed' => 100]]));
|
||||||
|
|
@ -106,3 +134,15 @@ it('lists what an instance does at a level', function () {
|
||||||
->and($rules->effects('barracks', 6))->toBe([['key' => 'recruit_speed', 'value' => 25]])
|
->and($rules->effects('barracks', 6))->toBe([['key' => 'recruit_speed', 'value' => 25]])
|
||||||
->and($rules->effects('rally_point', 1))->toBe([]);
|
->and($rules->effects('rally_point', 1))->toBe([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('shows what a copy does at its share', function () {
|
||||||
|
$rules = new BuildingRules(gameConfig());
|
||||||
|
|
||||||
|
// A copy trains at half the speed of level 1 (−50%), at level 6 at 62.5% (−38%).
|
||||||
|
expect($rules->effects('timber_camp', 1, 0.5))->toBe([['key' => 'production', 'value' => 15.0, 'resource' => 'wood']])
|
||||||
|
->and($rules->effects('storage', 2, 0.5))->toBe([['key' => 'capacity', 'value' => 615]])
|
||||||
|
->and($rules->effects('hiding_place', 1, 0.25))->toBe([['key' => 'hides', 'value' => 38]])
|
||||||
|
->and($rules->effects('hospital', 2, 0.5))->toBe([['key' => 'beds', 'value' => 12]])
|
||||||
|
->and($rules->effects('barracks', 1, 0.5))->toBe([['key' => 'recruit_speed', 'value' => -50]])
|
||||||
|
->and($rules->effects('barracks', 6, 0.5))->toBe([['key' => 'recruit_speed', 'value' => -38]]);
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,21 @@ it('feeds villagers from the share of farm output, leaving a surplus', function
|
||||||
$population = new Population(gameConfig());
|
$population = new Population(gameConfig());
|
||||||
$workforce = $population->workforce([building(1, 'farm'), building(2, 'farm', 2)]);
|
$workforce = $population->workforce([building(1, 'farm'), building(2, 'farm', 2)]);
|
||||||
|
|
||||||
// Budget 30 + 37.5 = 67.5 food/h; villagers fill 80% of it.
|
// Budget 37.5 from the level 2 farm and half of the copy's 30 = 52.5 food/h;
|
||||||
expect($workforce->foodBudget)->toBe(30 + 30 * 1.25)
|
// villagers fill 80% of it.
|
||||||
->and($workforce->villagers)->toBe(54)
|
expect($workforce->foodBudget)->toBe(30 * 1.25 + 30 * 0.5)
|
||||||
->and($workforce->upkeep)->toEqual(54);
|
->and($workforce->villagers)->toBe(42)
|
||||||
|
->and($workforce->upkeep)->toEqual(42);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('counts a copy farm at its share in the budget, but staffs it in full', function () {
|
||||||
|
$population = new Population(gameConfig());
|
||||||
|
$one = $population->workforce([building(1, 'farm', 10)]);
|
||||||
|
$two = $population->workforce([building(1, 'farm', 10), building(2, 'farm', 10)]);
|
||||||
|
|
||||||
|
expect($two->foodBudget)->toBe($one->foodBudget * 1.5)
|
||||||
|
->and($two->required)->toBe([1 => 5, 2 => 5])
|
||||||
|
->and($two->assigned)->toBe([1 => 5, 2 => 5]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('leaves soldiers upkeep out of the villagers share', function () {
|
it('leaves soldiers upkeep out of the villagers share', function () {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,18 @@ it('adds buildings to the base trickle and nets food against upkeep', function (
|
||||||
->and($rates['food'])->toBe(30.0 - 24);
|
->and($rates['food'])->toBe(30.0 - 24);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('makes half in a copy and a quarter in the copy after it', function () {
|
||||||
|
$config = gameConfig();
|
||||||
|
$buildings = [building(1, 'farm', 10), building(2, 'timber_camp', 2), building(3, 'timber_camp', 2), building(4, 'timber_camp', 3)];
|
||||||
|
$workforce = (new Population($config))->workforce($buildings);
|
||||||
|
$rates = (new Production($config))->rates($buildings, $workforce);
|
||||||
|
|
||||||
|
// The level 3 camp ranks first, then the older level 2 one.
|
||||||
|
expect($workforce->workers())->toBe(array_sum($workforce->required))
|
||||||
|
->and($rates['wood'])->toEqualWithDelta(5 + 30 * 1.163 ** 2 + 30 * 1.163 * 0.5 + 30 * 1.163 * 0.25, 1e-9)
|
||||||
|
->and((new Production($config))->output($buildings[2], $workforce, share: 0.25)['per_hour'])->toEqualWithDelta(30 * 1.163 * 0.25, 1e-9);
|
||||||
|
});
|
||||||
|
|
||||||
it('makes less in an understaffed building', function () {
|
it('makes less in an understaffed building', function () {
|
||||||
$config = gameConfig();
|
$config = gameConfig();
|
||||||
// 24 villagers (80% of 30 food) for a level 30 timber camp needing round(2 × 1.12^29) = 53.
|
// 24 villagers (80% of 30 food) for a level 30 timber camp needing round(2 × 1.12^29) = 53.
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,13 @@ it('trains faster at a higher level and slower when understaffed', function () {
|
||||||
->and($fast->seconds('spear', 1, 1.0))->toBe(11);
|
->and($fast->seconds('spear', 1, 1.0))->toBe(11);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('trains at a copy\'s share of the speed', function () {
|
||||||
|
$units = new UnitRules(gameConfig());
|
||||||
|
|
||||||
|
expect($units->seconds('spear', 1, 1.0, share: 0.5))->toBe(2040)
|
||||||
|
->and($units->seconds('spear', 11, 1.0, share: 0.25))->toBe(680 * 4);
|
||||||
|
});
|
||||||
|
|
||||||
it('lints units that point at unknown buildings or eat nothing', function () {
|
it('lints units that point at unknown buildings or eat nothing', function () {
|
||||||
$config = gameConfig(['units' => ['spear' => ['building' => 'tavern', 'upkeep' => 0]]]);
|
$config = gameConfig(['units' => ['spear' => ['building' => 'tavern', 'upkeep' => 0]]]);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue