46 lines
1.5 KiB
PHP
46 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Game\Map;
|
|
|
|
use App\Game\WorldConfig;
|
|
|
|
/**
|
|
* Barbarian waves (world.map.barbarians.waves): every few world days new barbarian
|
|
* villages settle near the active players, so a world with few players keeps getting
|
|
* new targets. How long until the next wave, and how many come per player.
|
|
*/
|
|
final class BarbarianWaves
|
|
{
|
|
public function __construct(private readonly WorldConfig $config) {}
|
|
|
|
public function enabled(): bool
|
|
{
|
|
return (bool) $this->config->get('world.map.barbarians.waves.enabled', false);
|
|
}
|
|
|
|
/** Seconds until the next wave, from a roll in [0, 1): every_days drawn evenly, at world speed. */
|
|
public function interval(float $roll): int
|
|
{
|
|
[$min, $max] = $this->config->get('world.map.barbarians.waves.every_days', [7, 7]) + [7, 7];
|
|
|
|
return max(1, (int) round(($min + ($max - $min) * $roll) * 86400 / $this->config->speed()));
|
|
}
|
|
|
|
/**
|
|
* New villages for one player in a wave, from a roll in [0, 1): the whole part of
|
|
* per_player always, one more with the fractional part as its chance.
|
|
*/
|
|
public function perPlayer(float $roll): int
|
|
{
|
|
$average = max(0.0, (float) $this->config->get('world.map.barbarians.waves.per_player', 0));
|
|
|
|
return (int) floor($average) + ($roll < $average - floor($average) ? 1 : 0);
|
|
}
|
|
|
|
/** At most this many fields from the player's village they settle. */
|
|
public function near(): float
|
|
{
|
|
return (float) $this->config->get('world.map.barbarians.waves.near', 8);
|
|
}
|
|
}
|