game-village-politics-backend/app/Game/Military/Conquest.php

67 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Game\Military;
use App\Game\WorldConfig;
/** Loyalty, its drop under noblemen and the coin slots that limit them (combat.conquest). */
final class Conquest
{
public function __construct(private readonly WorldConfig $config) {}
public function maxLoyalty(): float
{
return (float) $this->config->get('combat.conquest.loyalty.max', 100);
}
public function afterConquest(): float
{
return (float) $this->config->get('combat.conquest.loyalty.after_conquest', 25);
}
/** Loyalty $hours after it was $stored, grown back at the world's speed, up to the maximum. */
public function loyalty(float $stored, float $hours): float
{
$rate = (float) $this->config->get('combat.conquest.loyalty.regen_per_hour', 1) * $this->config->speed();
return min($this->maxLoyalty(), $stored + $rate * max(0, $hours));
}
/** One nobleman's drop for a draw in [0, 1). */
public function drop(float $roll): float
{
[$min, $max] = $this->config->get('combat.conquest.loyalty.drop', [20, 35]);
return round($min + ($max - $min) * $roll);
}
/** Noble slots that $coins pay for: the n-th slot needs n more coins. */
public static function slots(int $coins): int
{
return (int) floor((sqrt(8 * max(0, $coins) + 1) - 1) / 2);
}
/** Coins in total for $slots slots. */
public static function coinsFor(int $slots): int
{
return intdiv($slots * ($slots + 1), 2);
}
/** @return array{wood: int, clay: int, iron: int} */
public function coinCost(int $count = 1): array
{
return array_map(fn ($n) => (int) $n * $count, $this->config->get('combat.conquest.coin_cost'));
}
public function maxDistance(): float
{
return (float) $this->config->get('combat.conquest.max_distance', 0);
}
/** @return list<string> */
public function nobleUnits(): array
{
return array_keys(array_filter($this->config->get('units', []), fn (array $u) => $u['noble'] ?? false));
}
}