game-village-politics-backend/app/Game/Combat/CombatResolver.php

134 lines
5.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Game\Combat;
use App\Game\WorldConfig;
use Carbon\CarbonImmutable;
/**
* The Die Stämme battle (architecture.md, "Combat"). Attack strength is split by class
* (infantry, cavalry, archer); every defender counts with its defence against each class,
* weighted by the attacker's mix. The wall adds basic defence and multiplies it, and rams
* lower the wall first. The winner loses (weaker / stronger)^1.5 of every unit, the
* loser everything. Luck, morale and the night bonus come in as numbers.
*/
final class CombatResolver
{
public function __construct(private readonly WorldConfig $config) {}
/**
* @param array<string, int> $attack units sent
* @param array<string, int> $defense units in the village; `militia` for levied villagers
* @param float $luck between −luck and +luck, see luck()
* @param float $night defence multiplier, see night()
*/
public function fight(array $attack, array $defense, int $wall, float $morale, float $luck, float $night = 1.0): Battle
{
$wallAfter = $this->siege('ram', $wall, $attack['ram'] ?? 0);
$byClass = $this->attackByClass($attack);
$total = array_sum($byClass);
if ($total <= 0) {
return new Battle(false, 1.0, 0.0, $wall, $wall, 0.0, 0.0);
}
$attackPower = $total * $morale * (1 + $luck);
$defensePower = $this->defense($defense, $byClass, $total, $wallAfter) * $night;
$exponent = (float) $this->config->get('combat.loss_exponent', 1.5);
if ($attackPower > $defensePower) {
return new Battle(true, ($defensePower / $attackPower) ** $exponent, 1.0, $wall, $wallAfter, $attackPower, $defensePower);
}
return new Battle(false, 1.0, ($attackPower / $defensePower) ** $exponent, $wall, $wallAfter, $attackPower, $defensePower);
}
/**
* The level a building is left at after $count rams or catapults hit it. Each level
* L down needs base × factor^(L − 1) of them; nothing drops below level 1.
*/
public function siege(string $kind, int $level, int $count): int
{
$siege = $this->config->get("combat.siege.$kind");
$left = (float) $count;
while ($level > 1) {
$needed = $siege['base'] * $siege['factor'] ** ($level - 1);
if ($left < $needed) {
break;
}
$left -= $needed;
$level--;
}
return $level;
}
/** Attack multiplier from points: 1 against barbarians (null), less against smaller players. */
public function morale(?int $defenderPoints, int $attackerPoints): float
{
$morale = $this->config->get('combat.morale');
if (! ($morale['enabled'] ?? false) || $defenderPoints === null || $attackerPoints <= 0) {
return 1.0;
}
return min(1.0, $morale['floor'] + $morale['points_factor'] * $defenderPoints / $attackerPoints);
}
/** Luck from a roll in [0, 1): spread evenly over [−luck, +luck]. */
public function luck(float $roll): float
{
return (2 * $roll - 1) * (float) $this->config->get('combat.luck', 0);
}
/** Defence multiplier at $at: the night factor while it is night on the world's clock. */
public function night(CarbonImmutable $at): float
{
$night = $this->config->get('combat.night');
if (! ($night['enabled'] ?? false)) {
return 1.0;
}
$hour = $at->setTimezone($night['timezone'] ?? 'UTC')->hour;
$inside = $night['start'] <= $night['end']
? $hour >= $night['start'] && $hour < $night['end']
: $hour >= $night['start'] || $hour < $night['end'];
return $inside ? (float) $night['factor'] : 1.0;
}
/**
* @param array<string, int> $units
* @return array<string, float> class => attack strength
*/
private function attackByClass(array $units): array
{
$byClass = ['infantry' => 0.0, 'cavalry' => 0.0, 'archer' => 0.0];
foreach ($units as $unit => $count) {
$values = $this->config->get("units.$unit");
$byClass[$values['class']] += $count * $values['attack'];
}
return $byClass;
}
/**
* Defence against this attack mix, with the wall's basic defence and bonus.
*
* @param array<string, int> $units
* @param array<string, float> $byClass
*/
private function defense(array $units, array $byClass, float $total, int $wall): float
{
$sum = 0.0;
foreach ($units as $unit => $count) {
$values = $unit === 'militia'
? $this->config->get('combat.militia.defense')
: $this->config->get("units.$unit.defense");
foreach ($byClass as $class => $strength) {
$sum += $count * $values[$class] * $strength / $total;
}
}
$walls = $this->config->get('combat.wall');
return ($sum + $walls['basic_defense'] * $wall) * $walls['factor'] ** $wall;
}
}