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

148 lines
4.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Game\Combat;
use App\Game\Map\BarbarianGarrison;
use App\Game\Travel\TravelTime;
use App\Game\WorldConfig;
/**
* Raiders from barbarian camps (config/game/raids.php, architecture.md "Raids"): how
* big a band is, how often it comes, how its camp's strength shrinks it on arrival,
* and what a beaten band leaves behind.
*/
final class Raids
{
public const RESOURCES = ['wood', 'clay', 'iron'];
private BarbarianGarrison $garrison;
public function __construct(private readonly WorldConfig $config)
{
$this->garrison = new BarbarianGarrison($config);
}
public function enabled(): bool
{
return (bool) $this->config->get('raids.enabled', false);
}
/** Attack strength of a band against a village with these points. */
public function attack(int $points): float
{
$attack = $this->config->get('raids.attack');
return $attack['base'] + $attack['per_point'] * max(0, $points);
}
/**
* The band a camp sends: heads in the camp's profile until they reach the attack
* strength, plus rams against the wall once the village is big enough.
*
* @return array<string, int>
*/
public function band(?string $profile, int $points, int $wall): array
{
if ($profile === null) {
return [];
}
$shares = $this->garrison->shares($profile);
$total = array_sum($shares);
$perHead = $total > 0 ? array_sum(array_map(fn (string $u, float $s) => $s / $total * $this->unitAttack($u), array_keys($shares), $shares)) : 0;
if ($perHead <= 0) {
return [];
}
$units = BarbarianGarrison::split((int) ceil($this->attack($points) / $perHead), $shares);
$rams = $this->config->get('raids.rams');
if ($points >= $rams['from_points'] && $wall > 0 && $this->config->get('units.ram')) {
$count = (int) round($wall * $rams['per_wall_level']);
if ($count > 0) {
$units['ram'] = $count;
}
}
return $units;
}
/**
* The band as it arrives, as strong as its camp is now (share of full strength).
* Below scatter_below nothing is left of it.
*
* @param array<string, int> $units
* @return array<string, int>
*/
public function muster(array $units, float $strength): array
{
if ($strength < (float) $this->config->get('raids.scatter_below', 0)) {
return [];
}
return array_filter(array_map(fn (int $n) => (int) round($n * min(1.0, $strength)), $units));
}
/**
* What a beaten band leaves: bounty × its attack strength, a third each of wood,
* clay and iron.
*
* @param array<string, int> $units
* @return array{wood: int, clay: int, iron: int}
*/
public function bounty(array $units): array
{
$strength = array_sum(array_map(fn (string $u, int $n) => $n * $this->unitAttack($u), array_keys($units), $units));
$each = (int) floor($this->config->get('raids.bounty', 0) * $strength / count(self::RESOURCES));
return array_fill_keys(self::RESOURCES, $each);
}
/** Seconds until the next raid, at world speed, from a roll in [0, 1). */
public function interval(float $roll): int
{
[$min, $max] = $this->config->get('raids.every_hours');
return max(1, (int) round(($min + ($max - $min) * $roll) * 3600 / $this->config->speed()));
}
/** How long a band gathers at its camp before it marches, at world speed. */
public function gatherSeconds(): int
{
return (int) round($this->config->get('raids.gather_hours', 0) * 3600 / $this->config->speed());
}
public function mode(): string
{
return (string) $this->config->get('raids.mode', 'normal');
}
/**
* The nearest camp within range of x|y, the lower id on a tie.
*
* @param iterable<array{id: int, x: int, y: int}> $camps
*/
public function nearest(iterable $camps, int $x, int $y): ?int
{
$range = (float) $this->config->get('raids.range', 0);
$best = null;
$bestDistance = INF;
foreach ($camps as $camp) {
$distance = TravelTime::distance($x, $y, $camp['x'], $camp['y']);
if ($distance <= $range && ($distance < $bestDistance || ($distance === $bestDistance && $camp['id'] < $best))) {
$best = $camp['id'];
$bestDistance = $distance;
}
}
return $best;
}
public function range(): int
{
return (int) ceil($this->config->get('raids.range', 0));
}
private function unitAttack(string $unit): float
{
return (float) $this->config->get("units.$unit.attack", 0);
}
}