HB/onyx2/include/game/Class/class.asteroide.php

637 lines
20 KiB
PHP
Raw Normal View History

2009-11-01 11:00:00 +00:00
<?php
include_once("game/Class/class.surface.php");
/***************************************************************************
* class.asteroide.php
* ---------------------
* begin : Jeudi 25 décembre 2008
* update : Dimanche 4 janvier 2008
* email : nemunaire@gmail.com
*
*
***************************************************************************/
class Asteroide extends Surface
{
var $fondateur,
$sante,
$nom_alliance,
$tag,
$nom_asteroide,
$image_asteroide,
$credits_alliance;
/**
* Constructeur
* @param int $id id de la planète à importer
*
* @return void
* @access public
*/
function Asteroide($id = 0)
{
if (!empty($id)) {
global $table_alliances, $SESS;
global $alli_batimentVAR, $nomvaisnVAR;
$bdd = new bdd();
//On traite le cas où l'on envoie les coordonnées
if (is_numeric($id))
{
$plan = $bdd->unique_query("SELECT * FROM $table_alliances WHERE id = $id;");
$bdd->deconnexion();
}
elseif (preg_match('#^\[?([0-9]{1,2}):([0-9]{1,2}):?[Aa]?\]?$#', $id, $position))
{
$plan = $bdd->unique_query("SELECT * FROM $table_alliances WHERE galaxie = ".$position[1]." AND ss = ".$position[2].";");
$bdd->deconnexion();
}
else
die('Erreur #04 : Format de recherche d\'asteroide incorrect !');
if (!empty($plan))
{
$this->id = $plan["id"];
parent::User($SESS->values['id']); //On utilise le numéro d'utilisateur enregistré en session
$this->galaxie = $plan["galaxie"];
$this->ss = $plan["ss"];
$this->image = $this->image_asteroide = $plan["image_asteroide"];
$this->debris_met = $plan["debris_met"];
$this->debris_cri = $plan["debris_cri"];
$this->metal = $plan["metal"];
$this->cristal = $plan["cristal"];
$this->hydrogene = $plan["hydrogene"];
foreach($alli_batimentVAR as $bat)
$this->batiments[] = $plan[$bat];
if (!empty($plan["file_bat"]))
$this->file_bat = unserialize($plan["file_bat"]);
else
$this->file_bat = new File('alli_batiments');
foreach($nomvaisnVAR as $vais)
$this->vaisseaux[] = $plan[$vais];
if (!empty($plan["file_vais"]))
$this->file_vais = unserialize($plan["file_vais"]);
else
$this->file_vais = new File('vaisseaux');
}
}
}
/**
* Actualise les ressources de la planète en fonction de la production et termine les files d'attentes.
*
* @return void
* @access public
*/
function actualiser($actuFile = true, $first = false)
{
//Actualisation des files d'attentes
if ($actuFile)
{
$this->file_ready("batiments");
$this->file_readyTechno("technologies");
$this->file_ready("casernes");
$this->file_ready("terrestres");
$this->file_ready("vaisseaux");
}
//Calcul de la capacité de stockage maximale
if (!empty($timestamp_lastSilo))
{
$this->cap = pow(2, $this->batiments[10]-1) * 100000;
$capnouv = pow(2, $this->batiments[10]) * 100000;
}
else
$this->cap = pow(2, $this->batiments[10]) * 100000;
//Calcul du temps écoulé depuis la dernière mise à jour de la planète
$temps_ecoule = time() - $this->timestamp;
$ressources = $this->production($temps_ecoule);
if ($this->metal + $ressources[0] < $this->cap)
$this->metal += $ressources[0];
else
{
//Si les capacité de stockage ont changé depuis la dernière actualisation
if (isset($capnouv))
{
$ressources = $this->production(time() - $this->timestamp_lastSilo);
if ($this->metal + $ressources[0] < $capnouv)
$this->metal += $ressources[0];
}
else
{
$this->alert_ressources[0] = true;
$this->metal = $this->cap;
}
}
if ($this->cristal + $ressources[1] < $this->cap)
$this->cristal += $ressources[1];
else
{
//Si les capacité de stockage ont changé depuis la dernière actualisation
if (isset($capnouv))
{
$ressources = $this->production(time() - $this->timestamp_lastSilo);
if ($this->cristal + $ressources[1] < $capnouv)
$this->cristal += $ressources[1];
}
else
{
$this->alert_ressources[1] = true;
$this->cristal = $this->cap;
}
}
if ($this->hydrogene + $ressources[2] < $this->cap)
$this->hydrogene += $ressources[2];
else
{
//Si les capacité de stockage ont changé depuis la dernière actualisation
if (isset($capnouv))
{
$ressources = $this->production(time() - $this->timestamp_lastSilo);
if ($this->hydrogene + $ressources[2] < $capnouv)
$this->hydrogene += $ressources[2];
}
else
{
$this->alert_ressources[2] = true;
$this->hydrogene = $this->cap;
}
}
//Vérification de la date pour faire les actions journalières
if (date('zya') != date('zya', $this->timestamp))
{
//On évalue le moral
$this->evalMoral($first);
//Si la population est à 0, on ajoute des habitants
if ($this->population <= 0)
$this->population = 1000;
$popPlus = $this->population * 0.0153^max(1, floor((time()-$this->timestamp)/86400));
if ($this->politique == 2)
$popPlus *= 1.1; //Communisme : 10 % de population qui arrive en plus.
elseif ($this->politique == 3)
$popPlus *= 1.05; //Démocratie : 5 % de population qui arrive en plus.
if ($this->technologies[2] & 4)
$popPlus *= 1.15;
elseif ($this->technologies[2] & 2)
$popPlus *= 1.10;
elseif ($this->technologies[2] & 1)
$popPlus *= 1.05;
$this->population += $popPlus;
$this->credits += $this->population/100*exp(0.01)*25;
$this->modif[] = 'population';
}
$this->timestamp = time();
//Calcul du nombre de cases restantes
$this->casesRest = $this->cases;
foreach($this->batiments as $bat)
$this->casesRest -= $bat;
}
function setMoral($difference)
{
$this->moral += $difference;
//Ajustement du moral
if ($this->moral > 1)
$this->moral = 1;
elseif ($this->moral < 0)
$this->moral = 0;
if (!in_array("moral", $this->modif))
$this->modif[] = "moral";
}
function evalMoral($first = false)
{
//Cas de sous-production
if (($this->coeff_bat[0] + $this->coeff_bat[1] + $this->coeff_bat[2])/3 < 0.9)
{
if ($this->politique == 2)
$this->moral -= 0.10; //Communisme : démoralise 2x plus
else
$this->moral -= 0.05;
if (!in_array('moral', $this->modif))
$this->modif[] = 'moral';
}
//Surpopulation
//Surlogement
//Ajustement du moral en fonction de la politique
if ($this->politique == 1 && $this->moral > 0.7)
$this->moral = 0.7;
//On vérifie qu'on ne dépasse pas le maximum
if ($this->moral > 1)
$this->moral = 1;
if ($this->moral < 0)
$this->moral = 0;
//Isolement si besoin
if ($this->moral < 0.1)
{
//On vérifie qu'il ne s'agit pas de la planète mère
global $bdd, $table_planete;
$bdd->connexion();
$res = $bdd->unique_query("SELECT id FROM $table_planete WHERE id_user = ".$this->id_user." ORDER BY id LIMIT 1;");
$bdd->deconnexion();
if ($res['id'] != $this->id)
{
if ($this->moral <= 0.01 || $this->moral <= 0.04)
{
if ($this->moral <= 0.01)
$rand = rand(0,4);
else
$rand = rand(0,20);
//Perte de la planète
if ($rand == 1)
{
$bdd->connexion();
$bdd->query("DELETE FROM $table_planete WHERE id = ".$this->id.";");
$bdd->deconnexion();
send_mp($this->id_user, 'Perte de contrôle de '.$this->nom_planete, "Suite à une démoralisation percistante de la population sur la planète ".$this->nom_planete." [".$this->galaxie.":".$this->ss.":".$this->position."], la population a renversé votre gouvernement en tuant tous vos gouverneurs. Vous perdez donc définitivement le contrôle de cette planète.");
if (!$first)
{
$SESS->values['idPlan'] = $res['id'];
erreur('La population de cette planète est tellement démoralisée qu\'elle s\'est révolté contre vous. Vous ne contrôlez plus cette planète.');
}
}
}
elseif ($this->moral <= 0.06 || $this->moral <= 0.1)
{
if ($this->moral <= 0.06)
$rand = rand(0,2);
else
$rand = rand(0,10);
//Perte de contrôle temporaire
if ($rand == 1)
{
$debut = time() - rand(0, 3600)*4;
$fin = $debut + 86400;
$this->isolement = array($debut, $fin);
if (!in_array('isolement', $this->modif)) $this->modif[] = 'isolement';
send_mp($this->id_user, 'Perte de contrôle temporaire de '.$this->nom_planete, "Suite à une démoralisation percistante de la population sur la planète ".$this->nom_planete." [".$this->galaxie.":".$this->ss.":".$this->position."], la population a pris le contrôle de votre planète. Vous perdez le contrôle de cette planète le temps que vos gouverneurs reprennent le pouvoir.");
if (!$first)
{
$SESS->values['idPlan'] = $res['id'];
erreur('La population de cette planète est tellement démoralisée qu\'elle s\'est révoltée contre vous. Vous perdez temporairement le contrôle de cette planète.');
}
}
}
}
}
}
/**
* Vérifie si la planète est isolée ou non
*
* @return boolean
* @access public
*/
function isolement()
{
return false;
}
/**
* Calcul les ressources produites en fonction de $temps_ecoule
* @param int $temps_ecoule Temps écoulé depuis la dernière actualisation
*
* @return array
* @access public
*/
function production($temps_ecoule, $retarray = false)
{
//Accélération de la production
$temps_ecoule *= VITESSE;
//Calcul de la consomation d'énergie
if ($this->batiments[0] > 0)
$energie_m = ceil(exp(0.28*$this->batiments[0])*10);
else
$energie_m = 0;
if ($this->batiments[1] > 0)
$energie_c = ceil(exp(0.28*$this->batiments[1])*10);
else
$energie_c = 0;
if ($this->batiments[2] > 0)
$energie_h = ceil(exp(0.2849*$this->batiments[2])*13);
else
$energie_h = 0;
if ($this->batiments[3] > 0)
$energie_s = ceil(exp(0.28*$this->batiments[3])*22);
else
$energie_s = 0;
if ($this->batiments[4] > 0)
$energie_f = ceil(exp(0.297*$this->batiments[4])*25);
else
$energie_f = 0;
//Calcul de la consomation d'énergie
$this->energieConso = $energie_m * $this->coeff_bat[0] + $energie_c * $this->coeff_bat[1] + $energie_h * $this->coeff_bat[2];
$nrjmx = $energie_m + $energie_c + $energie_h;
//Calcul de la production d'énergie
$this->energie = $energie_s * $this->coeff_bat[3] + $energie_f * $this->coeff_bat[4];
if ($this->energieConso == 0)
$coeff = 0;
else
$coeff = $this->energie / $this->energieConso;
if ($coeff < 0)
$coeff = 0;
elseif ($coeff > 1)
$coeff = 1;
$Ncoeff = array();
for($i = 0; $i < 3; $i++)
{
$Ncoeff[$i] = $coeff * $this->coeff_bat[$i];
if ($Ncoeff[$i] > 1)
$Ncoeff[$i] = 1;
if ($Ncoeff[$i] < $this->coeff_bat[$i] && $this->batiments[$i] != 0)
{
$this->coeff_bat[$i] = $Ncoeff[$i];
if (!in_array('coeff_bat', $this->modif))
$this->modif[] = 'coeff_bat';
}
}
//Calcul de la consomation d'hydrogène
if ($this->batiments[4] > 0)
$conso_h = ((ceil(pow(1.34,($this->batiments[4]-1))*9)/3600)*$temps_ecoule) * $this->coeff_bat[4];
else
$conso_h = 0;
//Calcul des production de ressources
if ($this->batiments[0] <= 0 || $this->batiments[3] <= 0)
$prod_met = 0.011 * $temps_ecoule;
else
$prod_met = ((ceil(pow(1.1, $this->batiments[0]) * 35 * $this->batiments[0]) / 3600) * $temps_ecoule) * $this->coeff_bat[0] * 1.5;
if ($this->batiments[1] <= 0 || $this->batiments[3] <= 0)
$prod_cri = 0.0055 * $temps_ecoule;
else
$prod_cri = ((ceil(pow(1.1, $this->batiments[1]) * 23 * $this->batiments[1]) / 3600) * $temps_ecoule) * $this->coeff_bat[1] * 1.5;
if ($this->batiments[2] <= 0)
$prod_hy = 0;
else
$prod_hy = ((ceil(pow(1.1, $this->batiments[2]) * 14 * ($this->batiments[2] + 0.7)) / 3600) * $temps_ecoule) * $this->coeff_bat[2] * 1.5;
//Augmentation de la production en fonction des technologies
if ($this->technologies[0] &4)
{
$prod_met *= 1.15;
$prod_cri *= 1.15;
$prod_hy *= 1.15;
}
elseif ($this->technologies[0] &2)
{
$prod_met *= 1.10;
$prod_cri *= 1.10;
$prod_hy *= 1.10;
}
elseif ($this->technologies[0] &1)
{
$prod_met *= 1.05;
$prod_cri *= 1.05;
$prod_hy *= 1.05;
}
//Augmentation de la production en fonction du moral
if ($this->moral > 0.9)
{
$prod_met *= 1.05;
$prod_cri *= 1.05;
$prod_hy *= 1.05;
}
elseif ($this->moral > 0.75)
{
$prod_met *= 1.02;
$prod_cri *= 1.02;
$prod_hy *= 1.02;
}
elseif ($this->moral < 0.45)
{
$prod_met *= 0.97;
$prod_cri *= 0.97;
$prod_hy *= 0.97;
}
elseif ($this->moral < 0.25)
{
$prod_met *= 0.94;
$prod_cri *= 0.94;
$prod_hy *= 0.94;
}
//Augmentation de la production en fonction de la politique
if ($this->politique == 2)
{
$prod_met *= 1.10;
$prod_cri *= 1.10;
$prod_hy *= 1.10;
}
//On enlève la consomation d'hydrogène
$prod_hy -= $conso_h;
if ($retarray)
return array(array(ceil($this->coeff_bat[0]*100), ceil($this->coeff_bat[1]*100), ceil($this->coeff_bat[2]*100), ceil($this->coeff_bat[3]*100), ceil($this->coeff_bat[4]*100)), array($prod_met, $prod_cri, $prod_hy + $conso_h, $energie_s*$this->coeff_bat[3], $energie_f*$this->coeff_bat[4]), array($energie_m*$this->coeff_bat[0], $energie_c*$this->coeff_bat[1], $energie_h*$this->coeff_bat[2], $conso_h, ($energie_s*$this->coeff_bat[3] + $energie_f*$this->coeff_bat[4])-($energie_m*$this->coeff_bat[0] + $energie_c*$this->coeff_bat[1] + $energie_h*$this->coeff_bat[2])));
else
return array($prod_met, $prod_cri, $prod_hy);
}
function creer($id_user)
{
//Définition des paramètres de l'utilisateur pour la planète
$this->id_user = $id_user;
//Génération du nombre de case et de l'image en fonction de la position dans le système
if ($this->position > MAX_PLANETE*0.75)
{
$this->cases = mt_rand(200,255);
$this->image = mt_rand(1,19);
}
elseif ($this->position > MAX_PLANETE/2)
{
$this->cases = mt_rand(250,300);
$this->image = mt_rand(1,19);
}
elseif ($this->position > MAX_PLANETE/4)
{
$this->cases = mt_rand(175,260);
$this->image = mt_rand(1,19);
}
else
{
$this->cases = mt_rand(150,220);
$this->image = mt_rand(1,19);
}
//Définition des principaux paramètres de la planète
$this->nom_planete = 'Planète colonisée';
$this->timestamp = time();
$this->metal = 1000;
$this->cristal = 700;
$this->hydrogene = 0;
$this->modif = array("id_user", "nom_planete", "galaxie", "ss", "position", "image", "cases", "timestamp", "metal", "cristal", "hydrogene");
}
/**
* Destructeur
*
* @return void
* @access public
*/
function __destruct()
{
if (empty($this->ss) || empty($this->id_user))
return;
if ($this->id_user == 1)
var_dump($this);
global $var___db, $config, $table_planete;
if (empty($this->id))
{
$out1 = array(); $out2 = array();
$bdd = new bdd();
foreach($this->modif as $modif)
{
if ($modif == "force")
continue;
elseif (!is_array($this->{$modif}))
{
$bdd->escape($this->{$modif});
$out1[] = $modif;
if (is_int($this->{$modif}) || is_float($this->{$modif}))
$out2[] = $this->{$modif};
else
$out2[] = "'".$this->{$modif}."'";
}
else
{
if (is_array($this->{$modif}) && $modif != "coeff_bat" && $modif != "vaisseaux" && $modif != "terrestres" && $modif != "casernes" && $modif != "technologies" && $modif != "batiments")
{
$prep = serialize($this->{$modif});
$bdd->escape($prep);
$out1[] = $modif;
$out2[] = "'$prep'";
}
else
{
if ($modif == "batiments")
$calc = "batiment";
elseif ($modif == "technologies")
$calc = "technolo";
elseif ($modif == "casernes")
$calc = "casernen";
elseif ($modif == "terrestres")
$calc = "nomterrn";
elseif ($modif == "vaisseaux")
$calc = "nomvaisn";
elseif ($modif == "coeff_bat")
$calc = "coeff";
if (!isset(${$calc.'VAR'}))
global ${$calc.'VAR'};
foreach($this->{$modif} as $j => $value)
{
$out1[] = ${$calc.'VAR'}[$j];
$out2[] = $value;
}
}
}
}
$bdd->query("INSERT INTO $table_planete (".implode(', ', $out1).", hash_planete) VALUES (".implode(', ', $out2).", SHA1(CONCAT('g',planete.galaxie,'s',planete.ss,'p',planete.position)))");
$bdd->deconnexion();
}
else
{
$nb = count($this->modif);
$out = array();
$bdd = new bdd();
for($i = 0; $i < $nb; $i++)
{
if ($this->modif[$i] == "force")
$out[] = "timestamp = timestamp";
elseif ($this->modif[$i] == 'technologies')
$this->modifUser[] = $this->modif[$i];
elseif (is_object($this->{$this->modif[$i]}))
{
$export = serialize($this->{$this->modif[$i]});
$bdd->escape($export);
$out[] = $this->modif[$i]." = '".$export."'";
}
elseif (!is_array($this->{$this->modif[$i]}))
{
$bdd->escape($this->{$this->modif[$i]});
if (is_int($this->{$this->modif[$i]}) || is_float($this->{$this->modif[$i]}))
$out[] = $this->modif[$i]." = ".$this->{$this->modif[$i]};
else
$out[] = $this->modif[$i]." = '".$this->{$this->modif[$i]}."'";
}
else
{
if (is_array($this->{$this->modif[$i]}) && $this->modif[$i] != "coeff_bat" && $this->modif[$i] != "vaisseaux" && $this->modif[$i] != "terrestres" && $this->modif[$i] != "casernes" && $this->modif[$i] != "technologies" && $this->modif[$i] != "batiments")
{
$prep = serialize($this->{$this->modif[$i]});
$bdd->escape($prep);
$out[] = $this->modif[$i]." = '$prep'";
}
else
{
if ($this->modif[$i] == "batiments")
$calc = "batiment";
elseif ($this->modif[$i] == "technologies")
$calc = "technolo";
elseif ($this->modif[$i] == "casernes")
$calc = "casernen";
elseif ($this->modif[$i] == "terrestres")
$calc = "nomterrn";
elseif ($this->modif[$i] == "vaisseaux")
$calc = "nomvaisn";
elseif ($this->modif[$i] == "coeff_bat")
$calc = "coeff";
if (!isset(${$calc.'VAR'}))
global ${$calc.'VAR'};
$nombr = count(${$calc.'VAR'});
for($j = 0; $j < $nombr; $j++)
{
$bdd->escape($this->{$this->modif[$i]}[$j]);
$out[] = ${$calc.'VAR'}[$j]." = ".$this->{$this->modif[$i]}[$j]."";
}
}
}
}
if (!empty($out))
{
$sql = "UPDATE $table_planete SET timestamp = ".time().", metal = ".$this->metal.", cristal = ".$this->cristal.", hydrogene = ".$this->hydrogene.", ".implode(', ', $out)." WHERE id = ".$this->id.";";
if (DEBUG) echo '<br /><br />'.$sql;
$bdd->query($sql);
}
$bdd->deconnexion();
parent::__destruct();
}
}
}
?>