From 94a3dd52da3ae180af37c6fd0e8c24b3562da388 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 4 Oct 2023 11:32:04 +0200 Subject: initial commit 2 --- src/Model/Unit.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/Model/Unit.php') 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 + */ + 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); -- cgit v1.2.3