35 lines
1.1 KiB
PHP
35 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Game\Map;
|
||
|
|
|
||
|
|
use App\Game\Buildings\BuildingState;
|
||
|
|
use App\Game\WorldConfig;
|
||
|
|
|
||
|
|
/** Barbarian villages slowly grow: now and then one building goes up a level, up to a cap. */
|
||
|
|
final class BarbarianGrowth
|
||
|
|
{
|
||
|
|
public function __construct(private readonly WorldConfig $config) {}
|
||
|
|
|
||
|
|
/** Seconds between two growth steps, at world speed. */
|
||
|
|
public function interval(): int
|
||
|
|
{
|
||
|
|
$hours = $this->config->get('world.map.barbarians.growth.every_hours', 8);
|
||
|
|
|
||
|
|
return max(1, (int) round($hours * 3600 / $this->config->speed()));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Which building grows next, or null when all of them reached the cap.
|
||
|
|
*
|
||
|
|
* @param list<BuildingState> $buildings
|
||
|
|
* @param float $random uniform in [0, 1)
|
||
|
|
*/
|
||
|
|
public function pick(array $buildings, float $random): ?BuildingState
|
||
|
|
{
|
||
|
|
$cap = $this->config->get('world.map.barbarians.growth.max_level', 10);
|
||
|
|
$open = array_values(array_filter($buildings, fn (BuildingState $b) => $b->level < $cap));
|
||
|
|
|
||
|
|
return $open ? $open[(int) floor($random * count($open))] : null;
|
||
|
|
}
|
||
|
|
}
|