HB/game/Class/class.planete.php

382 lines
13 KiB
PHP

<?php
/***************************************************************************
* class.planete.php
* -------------------
* begin : Jeudi 21 août 2008
* update : Dimanche 7 septembre 2008
* email : nemunaire@gmail.com
*
*
***************************************************************************/
class Planete extends User{
var $id,
$galaxie,
$ss,
$position,
$isolement,
$nom_planete,
$image,
$cases,
$casesRest,
$debris_met,
$debris_cri,
$metal,
$cristal,
$hydrogene,
$population,
$alert_ressources = array(false, false, false),
$timestamp,
$timestamp_lastSilo,
$timestamp_lastMineM,
$timestamp_lastMineC,
$timestamp_lastMineH,
$energie,
$energieConso,
$file_tech,
$file_bat,
$file_cas,
$file_ter,
$file_vais,
$coeff_bat = array(),
$batiments = array(),
$casernes = array(),
$terrestres = array(),
$vaisseaux = array(),
$modif = array('metal', 'cristal', 'hydrogene', 'timestamp');
/**
* Constructeur
* @param int $id id de la planète à importer
*
* @return void
* @access public
*/
function Planete($id = 0){
if (!empty($id)) {
global $var___db, $config, $table_planete;
global $batimentVAR, $casernenVAR, $nomterrnVAR, $nomvaisnVAR;
$bdd = new bdd();
$bdd->connexion();
$bdd->escape($id);
$plan = $bdd->unique_query("SELECT * FROM $table_planete WHERE id = $id;");
$bdd->deconnexion();
if (!empty($plan)) {
$this->id = $plan["id"];
parent::User($plan["id_user"]);
$this->galaxie = $plan["galaxie"];
$this->ss = $plan["ss"];
$this->position = $plan["position"];
if (!empty($plan["isolement"])) $this->isolement = unserialize($plan["isolement"]);
else $this->isolement = array();
$this->nom_planete = $plan["nom_planete"];
$this->image = $plan["image"];
$this->cases = $plan["cases"];
$this->debris_met = $plan["debris_met"];
$this->debris_cri = $plan["debris_cri"];
$this->metal = $plan["metal"];
$this->cristal = $plan["cristal"];
$this->hydrogene = $plan["hydrogene"];
$this->population = $plan["population"];
$this->timestamp = $plan["timestamp"];
foreach($batimentVAR as $bat){
$this->batiments[] = $plan[$bat];
}
if (!empty($plan["file_bat"])) $this->file_bat = unserialize($plan["file_bat"]);
else $this->file_bat = array();
$this->coeff_bat = array($plan["coeff_mine_m"], $plan["coeff_mine_c"], $plan["coeff_mine_h"], $plan["coeff_centrale_s"], $plan["coeff_centrale_f"]);
for($i = 0; $i < 5; $i++){
if ($this->coeff_bat[$i] > 1) $this->coeff_bat[$i] = 1;
elseif ($this->coeff_bat[$i] < 0) $this->coeff_bat[$i] = 0;
}
if (!empty($plan["file_tech"])) $this->file_tech = unserialize($plan["file_tech"]);
else $this->file_tech = array();
foreach($casernenVAR as $cas){
$this->casernes[] = $plan[$cas];
}
if (!empty($plan["file_cas"])) $this->file_cas = unserialize($plan["file_cas"]);
else $this->file_cas = array();
foreach($nomterrnVAR as $ter){
$this->terrestres[] = $plan[$ter];
}
if (!empty($plan["file_ter"])) $this->file_ter = unserialize($plan["file_ter"]);
else $this->file_ter = array();
foreach($nomvaisnVAR as $vais){
$this->vaisseaux[] = $plan[$vais];
}
if (!empty($plan["file_vais"])) $this->file_vais = unserialize($plan["file_vais"]);
else $this->file_vais = array();
$this->actualiser();
}
}
}
/**
* 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){
//Actualisation des files d'attentes
if ($actuFile) {
$this->file_pret("batiments");
$this->file_pret("technologies");
$this->file_pret("casernes");
$this->file_pret("terrestres");
$this->file_pret("vaisseaux");
}
//Calcul de la capacité de stockage maximale
if (!empty($timestamp_lastSilo)) {
$cap = pow(2, $this->batiments[10]-1) * 100000;
$capnouv = pow(2, $this->batiments[10]) * 100000;
}
else $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] < $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 = $cap;
}
}
if ($this->cristal + $ressources[1] < $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 = $cap;
}
}
if ($this->hydrogene + $ressources[2] < $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 = $cap;
}
}
if (date('zya') != date('zya', $this->timestamp)) {
//Si la population est à 0, on ajoute des habitants
if ($this->population <= 0) $this->population = 1000;
$this->population = $this->population * 1.0153^max(1, floor((time()-$this->timestamp)/86400));
$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;
}
}
/**
* Vérifie si la planète est isolée ou non
*
* @return boolean
* @access public
*/
function isolement(){
$return = false;
if (time() > $isolement[0]) {
if (!isset($isolement[1]) || (time() > $isolement[1] && date('dmY') != date('dmY', $isolement[0]))) {
$nbPlan = count($queryPlanetes);
$numP = 0;
for ($i=0 ; $i<$nbPlan ; $i++) {
if ($queryPlanetes[$i]['id'] == $queryPlanete['id']) $numP = $i;
}
if ($numP > 10) {
switch($numP){
case 11: $tps = 2; break;
case 12: $tps = 4; break;
case 13: $tps = 6; break;
case 14: $tps = 8; break;
case 15: $tps = 12; break;
case 16: $tps = 16; break;
case 17: $tps = 20; break;
default: $tps = 24;
}
$debut = mktime(rand(0, 24-$tps), 0, 0, date('n'), date('j'), date('Y'));
$fin = $debut + $tps * 3600;
$bdd->connexion();
$bdd->query("UPDATE $table_planete SET isolement = '$debut $fin' WHERE id = '$idPlan';");
$bdd->deconnexion();
$isolement[0] = $debut;
if (time() > $isolement[0]) $isolement[1] = $fin;
}
}
$p = gpc('p');
if (isset($isolement[1]) && time() < $isolement[1]) {
$return = true;
}
}
//On applique les bonus politiques aux productions
if (isset($this->politique) && $this->politique == 1) {
$sec *= 0.9;
}
if (!in_array('isolement', $this->modif)) $this->modif[] = 'isolement';
return $return;
}
/**
* 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 - $conso_h;
//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 de la politique
if ($this->politique == 2) {
$prod_met *= 1.10;
$prod_cri *= 1.10;
$prod_hy *= 1.10;
}
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);
}
/**
* Destructeur
*
* @return void
* @access public
*/
function __destruct(){
global $var___db, $config, $table_planete;
$nb = count($this->modif);
$out = array();
$bdd = new bdd();
$bdd->connexion();
for($i = 0; $i < $nb; $i++){
if ($this->modif[$i] == 'technologies') $this->modifUser[] = $this->modif[$i];
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)) $plan = $bdd->query("UPDATE $table_planete SET ".implode(', ', $out)." WHERE id = ".$this->id.";");
$bdd->deconnexion();
parent::__destruct();
}
}
?>