$attack units sent * @param array $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; if ($attackPower > $defensePower) { return new Battle(true, $this->winnerLoss($defensePower / $attackPower), 1.0, $wall, $wallAfter, $attackPower, $defensePower); } return new Battle(false, 1.0, $this->winnerLoss($attackPower / $defensePower), $wall, $wallAfter, $attackPower, $defensePower); } /** * The share of every unit the winner loses at r = loser strength / winner strength: * ((r − t) / (1 − t))^loss_exponent with t = loss_threshold, and nothing at r ≤ t. * A side 1 / t times stronger loses nobody; at even strength both sides fall. */ public function winnerLoss(float $ratio): float { $threshold = (float) $this->config->get('combat.loss_threshold', 0); if ($ratio <= $threshold) { return 0.0; } return min(1.0, (($ratio - $threshold) / (1 - $threshold)) ** (float) $this->config->get('combat.loss_exponent', 1.5)); } /** * Attacking scouts against the scouts in the village. They get through when they * outnumber the defence (scouting.defense × defenders) and lose (defence / attack)^1.5 * of themselves; otherwise all of them die. Defending scouts never die here. * * @return array{0: bool, 1: int} whether they got through, and how many died */ public function scout(int $attackers, int $defenders): array { if ($attackers <= 0) { return [false, 0]; } $defense = $defenders * (float) $this->config->get('combat.scouting.defense', 1.0); if ($attackers <= $defense) { return [false, $attackers]; } $loss = ($defense / $attackers) ** (float) $this->config->get('combat.loss_exponent', 1.5); return [true, min($attackers, (int) round($attackers * $loss))]; } /** * 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 $units * @return array 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 $units * @param array $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; } }