diff --git a/includes/application/code/community/Legacies/Empire/Model/Planet.php b/includes/application/code/community/Legacies/Empire/Model/Planet.php index b598a31..bb59b8a 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Planet.php +++ b/includes/application/code/community/Legacies/Empire/Model/Planet.php @@ -61,6 +61,16 @@ class Legacies_Empire_Model_Planet return $this->_now; } + public function getLastUpdate() + { + return $this->getData('last_update'); + } + + public function setLastUpdate($time) + { + return $this->setData('last_update', $time); + } + public function updateStorages($time = null) { Math::setPrecision(10); @@ -81,9 +91,11 @@ class Legacies_Empire_Model_Planet $this->setData($resourceData['storage_field'], $value); } Math::setPrecision(); + + return $this; } - public function updateResources($time = null) + public function updateResourceProduction($time = null) { $types = Legacies_Empire_Model_Game_Types::getSingleton(); $resources = Legacies_Empire_Model_Game_Resources::getSingleton(); @@ -93,11 +105,19 @@ class Legacies_Empire_Model_Planet $time = $this->_now(); } - if ($this->getData('planet_type') != 1) { + if (!$this->isPlanet()) { return $this; } - $resourcesProductions = array(); + /* + * Compute resources consumers and resources producers + */ + $resourcesTotals = array(); + $resourcesProductionTotals = array(); + $resourcesConsumptionTotals = array(); + $resourcesProducers = array(); + $resourcesConsumers = array(); + $productionRatios = array(); foreach ($resources->getAllDatas() as $resource => $resourceData) { foreach ($resourceData['production'] as $productionUnit => $ratioField) { if (!in_array($productionUnit, $types['prod'])) { @@ -108,30 +128,93 @@ class Legacies_Empire_Model_Planet $ratio = $this->getData($ratioField); $element = self::getProducitonElementInstance($productionUnit); - foreach ($element->getProductionRatios($level, $ratio, $this, $this->getUser()) as $resourceId => $resourceProduction) { - if (!isset($resourcesProductions[$resourceId])) { - $resourcesProductions[$resourceId] = $resourceProduction; - } else { - $resourcesProductions[$resourceId] = Math::add($resourcesProductions[$resourceId], $resourceProduction); + $productionRatios[$productionUnit] = $element->getProductionRatios($level, $ratio, $this, $this->getUser()); + foreach ($productionRatios[$productionUnit] as $resourceId => $resourceProduction) { + + $comp = Math::comp($resourceProduction, 0); + if ($comp < 0) { + if (!isset($resourcesConsumers[$resourceId])) { + $resourcesConsumers[$resourceId] = array(); + } + $resourcesConsumers[$resourceId][] = $productionUnit; + + if (!isset($resourcesConsumptionTotals[$resourceId])) { + $resourcesConsumptionTotals[$resourceId] = 0; + } + $resourcesConsumptionTotals[$resourceId] = Math::add($resourcesConsumptionTotals[$resourceId], $resourceProduction); + + if (!isset($resourcesTotals[$resourceId])) { + $resourcesTotals[$resourceId] = 0; + } + $resourcesTotals[$resourceId] = Math::add($resourcesTotals[$resourceId], $resourceProduction); + } else if ($comp > 0) { + if (!isset($resourcesProducers[$resourceId])) { + $resourcesProducers[$resourceId] = array(); + } + $resourcesProducers[$resourceId][] = $productionUnit; + + if (!isset($resourcesProductionTotals[$resourceId])) { + $resourcesProductionTotals[$resourceId] = 0; + } + $resourcesProductionTotals[$resourceId] = Math::add($resourcesProductionTotals[$resourceId], $resourceProduction); + + if (!isset($resourcesTotals[$resourceId])) { + $resourcesTotals[$resourceId] = 0; + } + $resourcesTotals[$resourceId] = Math::add($resourcesTotals[$resourceId], $resourceProduction); } } } } - var_dump($resourcesProductions); - $timeDiff = ($time - $this->getData('last_update')) / 3600; - foreach ($resourcesProductions as $resource => $productionPerHour) { - if (!isset($resources[$resource])) { + /* + * Compute resource consumption + */ + $actualProduction = $resourcesTotals; + foreach ($resourcesTotals as $resource => $total) { + if (Math::isPositiveOrZero($total, 0)) { continue; } - $this->setData($resources[$resource]['production_field'], $productionPerHour); - $production = Math::add($this->getData($resources[$resource]['field']), Math::mul($timeDiff, $productionPerHour)); - - if (Math::comp($production, $this->getData($resources[$resource]['storage_field'])) > 0) { - $production = $this->getData($resources[$resource]['storage_field']); + $productionRatio = 0; + if (isset($resourcesProductionTotals[$resource]) && Math::isPositive($resourcesProductionTotals[$resource])) { + $consumptions = Math::mul(-1, $resourcesConsumptionTotals[$resource]); + Math::setPrecision(50); + $productionRatio = Math::div($resourcesProductionTotals[$resource], $consumptions); + Math::setPrecision(); } - $this->setData($resources[$resource]['field'], $production); + + foreach ($resourcesConsumers[$resource] as $consumer) { + foreach ($productionRatios[$consumer] as $resourceId => $ratio) { + Math::setPrecision(50); + $productionOverhead = Math::mul(Math::sub(1, $productionRatio), $ratio); + Math::setPrecision(); + + $actualProduction[$resourceId] = Math::sub($actualProduction[$resourceId], $productionOverhead); + } + } + } + + foreach ($actualProduction as $resource => $production) { + $this->setData($resources[$resource]['production_field'], $production); + } + + return $this; + } + + public function updateResources($time = null) + { + $resources = Legacies_Empire_Model_Game_Resources::getSingleton(); + + if ($time === null) { + $time = $this->_now(); + } + + $timeDiff = ($time - $this->getLastUpdate()) / 3600; + var_dump($time, $this->getLastUpdate(), $timeDiff); + foreach ($resources->getAllDatas() as $resourceId => $resourceConfig) { + $production = Math::mul($this->getData($resourceConfig['production_field']), $timeDiff); + $this->setData($resourceConfig['field'], Math::min($production, $this->getData($resourceConfig['storage_field']))); } return $this; @@ -486,7 +569,7 @@ class Legacies_Empire_Model_Planet } else { $planet->updateResources($time); } - $planet->setData('last_update', $time); + $planet->setLastUpdate($time); } } } \ No newline at end of file diff --git a/includes/application/code/community/Legacies/Empire/Model/Planet/Building/CristalMine.php b/includes/application/code/community/Legacies/Empire/Model/Planet/Building/CristalMine.php index 384abe6..06cb180 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Planet/Building/CristalMine.php +++ b/includes/application/code/community/Legacies/Empire/Model/Planet/Building/CristalMine.php @@ -6,7 +6,7 @@ class Legacies_Empire_Model_Planet_Building_CristalMine public function getProductionRatios($level, $produtionRatio, $planet, $user) { return array( - Legacies_Empire::RESOURCE_CRISTAL => 20 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio), + Legacies_Empire::RESOURCE_CRISTAL => 10 + 20 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio), Legacies_Empire::RESOURCE_ENERGY => -10 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio) ); } diff --git a/includes/application/code/community/Legacies/Empire/Model/Planet/Building/MetalMine.php b/includes/application/code/community/Legacies/Empire/Model/Planet/Building/MetalMine.php index 91177a6..6bff798 100644 --- a/includes/application/code/community/Legacies/Empire/Model/Planet/Building/MetalMine.php +++ b/includes/application/code/community/Legacies/Empire/Model/Planet/Building/MetalMine.php @@ -6,7 +6,7 @@ class Legacies_Empire_Model_Planet_Building_MetalMine public function getProductionRatios($level, $produtionRatio, $planet, $user) { return array( - Legacies_Empire::RESOURCE_METAL => 30 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio), + Legacies_Empire::RESOURCE_METAL => 20 + 30 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio), Legacies_Empire::RESOURCE_ENERGY => -10 * floatval($level) * pow(1.1, floatval($level)) * (0.1 * $produtionRatio) ); } diff --git a/includes/application/code/community/Math.php b/includes/application/code/community/Math.php index 72f438a..d099d9a 100644 --- a/includes/application/code/community/Math.php +++ b/includes/application/code/community/Math.php @@ -81,4 +81,50 @@ class Math { return self::getSingleton()->render($a); } + + public static function isNegative($a) + { + return (bool) (self::getSingleton()->comp($a, 0) < 0); + } + + public static function isPositive($a) + { + return (bool) (self::getSingleton()->comp($a, 0) > 0); + } + + public static function isNegativeOrZero($a) + { + return (bool) (self::getSingleton()->comp($a, 0) <= 0); + } + + public static function isPositiveOrZero($a) + { + return (bool) (self::getSingleton()->comp($a, 0) >= 0); + } + + public static function isZero($a) + { + return (bool) (self::getSingleton()->comp($a, 0) == 0); + } + + public static function isNotZero($a) + { + return (bool) (self::getSingleton()->comp($a, 0) != 0); + } + + public static function min($a, $b) + { + if (self::getSingleton()->comp($a, $b) > 0) { + return $b; + } + return $a; + } + + public static function max($a, $b) + { + if (self::getSingleton()->comp($a, $b) < 0) { + return $b; + } + return $a; + } } \ No newline at end of file diff --git a/includes/application/test/UpdatePlanet.php b/includes/application/test/UpdatePlanet.php index 3b22473..bc772d5 100644 --- a/includes/application/test/UpdatePlanet.php +++ b/includes/application/test/UpdatePlanet.php @@ -9,6 +9,7 @@ if (!extension_loaded('xdebug')) { $planet = new Legacies_Empire_Model_Planet(array( 'last_update' => 0, 'planet_type' => 1, + 'b_building' => (3600 * 10000), 'metal' => 0, 'metal_perhour' => 20, @@ -26,10 +27,10 @@ $planet = new Legacies_Empire_Model_Planet(array( 'solar_satelit_porcent' => 10, 'deuterium_sintetizer_porcent' => 10, - 'metal_mine' => 10, + 'metal_mine' => 20, 'crystal_mine' => 0, 'deuterium_sintetizer' => 0, - 'solar_plant' => 0, + 'solar_plant' => 20, 'fusion_plant' => 0, 'robot_factory' => 0, 'nano_factory' => 0, @@ -47,9 +48,21 @@ $planet->updateStorages(3600); var_dump($planet->getAllDatas()); +$planet->updateResourceProduction(3600); + +var_dump($planet->getAllDatas()); + Legacies::dispatchEvent('planet.update', array( 'planet' => $planet, 'time' => 3600 )); +var_dump($planet->getAllDatas()); + +var_dump((3600 + (3600 * 200))); +Legacies::dispatchEvent('planet.update', array( + 'planet' => $planet, + 'time' => (3600 + (3600 * 200)) + )); + var_dump($planet->getAllDatas()); \ No newline at end of file diff --git a/includes/application/test/bootstrap.php b/includes/application/test/bootstrap.php new file mode 100644 index 0000000..9e013d7 --- /dev/null +++ b/includes/application/test/bootstrap.php @@ -0,0 +1,43 @@ + $listenerList) { + foreach ($listenerList as $listener) { + Legacies::registerListener($event, $listener); + } +} + +include ROOT_PATH . 'includes/constants.php'; \ No newline at end of file