Planet resource produciton calculations and planet resources updates implemented and tested

Signed-off-by: Gregory PLANCHAT <g.planchat@gmail.com>
This commit is contained in:
Gregory PLANCHAT 2011-06-19 15:53:50 +02:00
commit 282eb43a31
6 changed files with 207 additions and 22 deletions

View file

@ -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);
}
}
}

View file

@ -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)
);
}

View file

@ -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)
);
}

View file

@ -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;
}
}

View file

@ -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());

View file

@ -0,0 +1,43 @@
<?php
if (!defined('DEBUG') && in_array(strtolower(getenv('DEBUG')), array('1', 'on', 'true'))) {
define('DEBUG', true);
}
if (!defined('DEBUG')) {
@ini_set('display_errors', false);
} else {
@ini_set('display_errors', true);
@error_reporting(E_ALL | E_STRICT);
}
defined('ROOT_PATH') || define('ROOT_PATH', dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR);
defined('APPLICATION_PATH') || define('APPLICATION_PATH', ROOT_PATH . 'includes' . DIRECTORY_SEPARATOR . 'application' . DIRECTORY_SEPARATOR);
defined('PHPEXT') || define('PHPEXT', require ROOT_PATH . 'extension.inc');
defined('VERSION') || define('VERSION', '2009.5');
set_include_path(implode(PATH_SEPARATOR, array(
APPLICATION_PATH . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR . 'core',
APPLICATION_PATH . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR . 'community',
APPLICATION_PATH . DIRECTORY_SEPARATOR . 'code' . DIRECTORY_SEPARATOR . 'local',
get_include_path()
)));
function __autoload($class) {
include_once str_replace('_', '/', $class) . '.php';
}
if (0 === filesize(ROOT_PATH . 'config.php')) {
header('Location: install/');
die();
}
foreach (include ROOT_PATH . 'includes/data/events.php' as $event => $listenerList) {
foreach ($listenerList as $listener) {
Legacies::registerListener($event, $listener);
}
}
include ROOT_PATH . 'includes/constants.php';