type = $unitType; $unit->isTraveling = false; $unit->homeVillageId = $villageId; $unit->amount = 0; return $unit->cast(); } public function getBuildTime(int $amount): int { $building = $this->getBuilding(); if (! $building) { return -1; } return intval((($_ENV['BASE_UNIT_BUILD_TIME_FACTOR'] * $this->buildTimeFactor) / ($building->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(); $resourceRequirements = []; foreach (ResourceType::asProperties() as $resourceType) { $base = $unit->resourceRequirementsBase[$resourceType]; $factor = $unit->resourceRequirementsFactor[$resourceType]; $r = $base; for ($i = 0; $i <= $amount; $i++) { $r += ceil( ( pow( $_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_BASE'], $currentAmount + 1 ) * $factor * $_ENV['BASE_UNIT_RESOURCE_REQUIREMENT_FACTOR'] ) / ($building->level ?? 1) ); } $resourceRequirements[$resourceType] = $r; } return $resourceRequirements; } public function getPopulationDemand(): int { return $this->getPopulationDemandForAmount($this->amount); } public function getPopulationDemandForAmount(int $amount): int { return $amount * $this->populationDemandFactor; } /* 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); } public function cast(): Unit { $class = Unit::resolveType($this->type); return Model::castToType($this, Unit::resolveType($this->type)); } public static function resolveType(string $type): string { return __NAMESPACE__ . '\\Unit\\' . $type; } /* Static */ public static function getAmountResiding(string $unitType, int $villageId): int { $statement = DB::query( 'select SUM(amount) from village_units where type=:type and residence_village_id=:id and is_traveling=false', ['type' => $unitType, 'id' => $villageId] ); $result = $statement->fetch()['sum']; return intval($result); } }