summaryrefslogtreecommitdiff
path: root/src/Model/Unit.php
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-10-04 11:32:04 +0200
committerDaniel Weipert <code@drogueronin.de>2023-10-04 11:32:04 +0200
commit94a3dd52da3ae180af37c6fd0e8c24b3562da388 (patch)
treeacced055660bfd65ca3e955ea26d412457ba4507 /src/Model/Unit.php
parentfa00b957378a393f8edbfc98ef111d35d18ecb09 (diff)
initial commit 2
Diffstat (limited to 'src/Model/Unit.php')
-rw-r--r--src/Model/Unit.php52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/Model/Unit.php b/src/Model/Unit.php
index a0d1a35..621651c 100644
--- a/src/Model/Unit.php
+++ b/src/Model/Unit.php
@@ -30,6 +30,48 @@ class Unit
return intval(($_ENV['BASE_UNIT_BUILD_TIME_FACTOR'] / ($this->getBuilding()->level ?: 1)) * $amount);
}
+ public static function getTravelTime(Unit $unit, int $distance): int
+ {
+ return self::getTravelTimePerCell($unit) * $distance;
+ }
+
+ public static function getTravelTimePerCell(Unit $unit): int
+ {
+ return intval(ceil($unit->travelTime * $_ENV['BASE_UNIT_TRAVEL_TIME_FACTOR']));
+ }
+
+
+ /**
+ * @return array<string, int>
+ */
+ public static function getResourceRequirements(Unit $unit, int $amount): array
+ {
+ /**@var Building $building*/
+ $building = DB::fetch(
+ Building::resolveType($unit->buildingType),
+ 'select level from village_buildings where type=:type and village_id=:id',
+ ['type' => $unit->buildingType, 'id' => $unit->homeVillageId]
+ )[0] ?? null;
+
+ $currentAmount = DB::query(
+ 'select sum(amount) from village_units where type=:type and home_village_id=:id',
+ ['type' => $unit->type, 'id' => $unit->homeVillageId]
+ )->fetchColumn();
+
+
+ return array_map(
+ function ($resourceRequirement) use ($amount, $currentAmount, $building) {
+ $r = 0;
+ for ($i = 0; $i <= $amount; $i++) {
+ $r += ceil((pow($_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_BASE'], $currentAmount + 1) * $resourceRequirement * $_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_FACTOR']) / ($building->level ?? 1));
+ }
+
+ return $r;
+ },
+ $unit->resourceRequirements
+ );
+ }
+
public function getPopulationDemand(): int
{
return $this->getPopulationDemandForAmount($this->amount);
@@ -43,6 +85,16 @@ class Unit
/* Relations */
+ public function getHomeVillage(): Village
+ {
+ return DB::fetch(Village::class, 'select * from villages where id=:id', ['id' => $this->homeVillageId])[0];
+ }
+
+ public function getResidenceVillage(): Village
+ {
+ return DB::fetch(Village::class, 'select * from villages where id=:id', ['id' => $this->residenceVillageId])[0];
+ }
+
public function getBuilding(): ?Building
{
return Village::getBuilding($this->homeVillageId, $this->buildingType);