HB/game/Class/class.flotte.php

592 lines
20 KiB
PHP

<?php
include_once(_FCORE."../game/Class/class.rapport.php");
/***************************************************************************
* class.flotte.php
* ------------------
* begin : Samedi 20 septembre 2008
* update : Samedi 20 septembre 2008
* email : nemunaire@gmail.com
*
*
***************************************************************************/
class Flotte
{
var $id_flotte = 0,
$nom,
$start_planete,
$start_time,
$end_planete,
$end_time,
$ret_planete,
$ret_time,
$nb_vais,
$vaisseaux = array(),
$tactique = 0,
$mission,
$vitesse,
$statut = 0,
$last,
$contenu = array(0,0,0),
$contenuMax = 0,
$modifFlotte = array();
/**
* Constructeur
* @param int $id id de la flotte à importer
*
* @return void
* @access public
*/
function Flotte($id = 0)
{
if (!empty($id))
{
global $var___db, $config, $table_flottes;
global $nomvaisnVAR, $ressoVAR;
$bdd = new bdd();
$bdd->connexion();
$bdd->escape($id);
$flotte = $bdd->unique_query("SELECT * FROM $table_flottes WHERE id = $id;");
$bdd->query("UPDATE $table_flottes SET last = ".time()." WHERE id = $id;"); //Obtention d'un vérrou de 10 seconde sur la flotte
$bdd->deconnexion();
if (!empty($flotte))
{
$this->id_flotte = $flotte["id"];
$this->nom = $flotte["nom"];
$this->start_planete = $flotte["start_planete"];
$this->start_time = $flotte["start_time"];
$this->end_planete = $flotte["end_planete"];
$this->end_time = $flotte["end_time"];
$this->ret_planete = $flotte["ret_planete"];
$this->ret_time = $flotte["ret_time"];
$this->tactique = $flotte["tactique"];
$this->mission = $flotte["mission"];
$this->vitesse = $flotte["vitesse"];
$this->statut = $flotte["statut"];
$this->last = $flotte["last"];
$this->nb_vais = $flotte["nb_vais"];
foreach($nomvaisnVAR as $vais)
$this->vaisseaux[] = $flotte[$vais];
$this->contenu = array($flotte["contenu_metal"], $flotte["contenu_cristal"], $flotte["contenu_hydrogene"]);
$this->calculer();
}
}
}
function calculer()
{
global $nomvais_rs;
$this->nb_vais = 0;
//Calcul de la capacité maximale d'embarquement de la flotte
foreach($this->vaisseaux as $key => $vais)
{
$this->nb_vais += $vais;
$this->contenuMax += $nomvais_rs[$key] * $vais;
}
}
function load_planete()
{
if (is_numeric($this->start_planete) && !empty($this->start_planete))
{
global $planete;
//Si la planète est la même que celle du joueur actuel, on l'utilise, sinon, on la crée
if ($planete->id == $this->start_planete)
$this->start_planete = $planete;
else
$this->start_planete = new Planete($this->start_planete);
}
if (is_numeric($this->end_planete) && !empty($this->end_planete))
{
global $planete;
//Si la planète est la même que celle du joueur actuel, on l'utilise, sinon, on la crée
if ($planete->id == $this->end_planete)
$this->end_planete = $planete;
else
$this->end_planete = new Planete($this->end_planete);
}
if (is_numeric($this->ret_planete) && !empty($this->ret_planete))
{
global $planete;
//Si la planète est la même que celle du joueur actuel, on l'utilise, sinon, on la crée
if ($planete->id == $this->ret_planete)
$this->ret_planete = $planete;
else
$this->ret_planete = new Planete($this->ret_planete);
}
}
function calc_deplacement($start_galaxie, $start_systeme, $start_position, $end_galaxie, $end_systeme, $end_position, $vitesse, $returnArray = false, $returnConso = false)
{
//Si la planète de départ n'est pas chargée, on charge les planètes
if (is_numeric($this->start_planete))
$this->load_planete();
global $config, $nomvais_vitesseP, $nomvais_vitesseS, $nomvais_vitesseG, $nomvais_rs;
$this->vitesse = $vitesse;
//Calcul de la longueur du déplacement
$diff_galaxie = abs($start_galaxie - $end_galaxie);
$diff_systeme = abs($start_systeme - $end_systeme);
$diff_position = abs($start_position - $end_position);
$diff_centre_position_start = abs(ceil($config['nb_amas']/2) - $start_position);
$diff_centre_systeme_start = abs(ceil($config['nb_systeme']/2) - $start_systeme);
$diff_centre_position_end = abs(ceil($config['nb_amas']/2) - $end_position);
$diff_centre_systeme_end = abs(ceil($config['nb_systeme']/2) - $end_systeme);
//Calcul du temps de déplacement pour chaque vaisseau
$temps = array(); $conso = array(0, 0, 0);
foreach($this->vaisseaux as $key => $vais)
{
//S'il n'y a pas de vaisseaux de ce type, on ne calcul pas leur vitesse
if ($vais == 0)
continue;
//Calcul du temps de déplacement entre planètes
if ($start_systeme == $end_systeme && $start_galaxie == $end_galaxie)
{
$temps[0][$key] = (10/$nomvais_vitesseP[$key]) * (1 + 0.1 * $diff_position);
$temps[1][$key] = $temps[2][$key] = 0;
}
//Calcul du temps de déplacement entre système
elseif ($start_galaxie == $end_galaxie)
{
$temps[0][$key] = (10/$nomvais_vitesseP[$key]) * (1 + 0.1 * ($diff_centre_position_start + $diff_centre_position_end));
$temps[1][$key] = (20/$nomvais_vitesseS[$key]) * (2 + 1 * $diff_systeme);
$temps[2][$key] = 0;
}
//Calcul du temps de déplacement entre galaxies
else
{
$temps[0][$key] = (10/$nomvais_vitesseP[$key]) * (1 + 0.1 * ($diff_centre_position_start + $diff_centre_position_end));
$temps[1][$key] = (20/$nomvais_vitesseS[$key]) * (2 + 1 * ($diff_centre_systeme_start + $diff_centre_systeme_end));
$temps[2][$key] = (50/$nomvais_vitesseG[$key]) * (2 + 1.5 * $diff_galaxie);
}
//Calcul du bonus pour le réacteur à combustion
$techR = $this->start_planete->technologies[1];
if ($techR & 56)
$bonus = 0.7;
elseif ($techR & 24)
$bonus = 0.8;
elseif ($techR & 8)
$bonus = 0.9;
else
$bonus = 1;
$temps[0][$key] *= $bonus * 1/$vitesse;
$conso[0] += $vais * $temps[0][$key] * $bonus / exp($vitesse/5);
//Calcul du bonus pour le réacteur à fusion
$techR = $this->start_planete->technologies[1];
if ($techR &448)
$bonus = 0.7;
elseif ($techR &192)
$bonus = 0.8;
elseif ($techR &64)
$bonus = 0.9;
else
$bonus = 1;
$temps[1][$key] *= $bonus * 1/$vitesse;
$conso[1] += $vais * $temps[1][$key] * $bonus / exp($vitesse/7.5);
//Calcul du bonus pour le réacteur à fusion de type II
$techR = $this->start_planete->technologies[1];
if ($techR &3584)
$bonus = 0.7;
elseif ($techR &1536)
$bonus = 0.8;
elseif ($techR &512)
$bonus = 0.9;
else
$bonus = 1;
$temps[2][$key] *= $bonus * 1/$vitesse;
$conso[2] += $vais * $temps[2][$key] * $bonus / exp($vitesse/10);
}
if (!isset($this->vaisseaux[4]))
$this->vaisseaux[4] = 0;
if (!isset($this->vaisseaux[5]))
$this->vaisseaux[5] = 0;
//Si les chasseurs peuvent rentrer dans les cales des vaisseaux, on les enlèves
if ($this->contenuMax - ($this->contenu[0] + $this->contenu[1] + $this->contenu[2]) - ($this->vaisseaux[4] * $nomvais_rs[4] + $this->vaisseaux[5] * $nomvais_rs[5]) >= ($this->vaisseaux[4] + $this->vaisseaux[5]) * 200)
$temps[2][4] = $temps[2][5] = $temps[1][4] = $temps[1][5] = $temps[0][4] = $temps[0][5] = 0;
//On calcul le temps de déplacement maximal
if ($returnArray)
return $temps;
elseif ($returnConso)
return array(max($temps[0]) + max($temps[1]) + max($temps[2]), ceil($conso[0]+$conso[1]+$conso[2]));
else
return (max($temps[0]) + max($temps[1]) + max($temps[2]));
}
function check_mission()
{
//On vérifie qu'un calcul ne soit pas déjà en cours
if ($this->last >= time() - 10)
return false;
elseif ($this->start_time + $this->end_time > time())
return false;
if ($this->statut == 0)
{
switch($this->mission)
{
case 0:
return $this->stationner();
break;
case 1:
return $this->transporter();
break;
case 2:
return $this->coloniser();
break;
case 3:
return $this->attaquer();
break;
case 4:
return $this->recycler();
break;
case 5:
return $this->espionner();
break;
}
}
if ($this->statut == 1 && ($this->ret_time > time() || $this->start_time + $this->end_time * 2 <= time()))
return $this->retourner();
}
function stationner()
{
//On décharge les ressources éventuellement contenue
$this->decharger();
//On fait atterir les vaisseaux
foreach ($this->vaisseaux as $key => $vais)
$this->end_planete->vaisseaux[$key] += $vais;
if (!in_array("vaisseaux", $this->end_planete->modif))
$this->end_planete->modif[] = "vaisseaux";
$this->modifFlotte = "DELETE";
}
function transporter()
{
$max = $this->decharger();
//Envoie du MP de confirmation au joueur
$send = new Rapport(2, $this->start_planete->id_user, $this->end_planete->id_user, $this->start_time + $this->end_time);
$send->addInfo(array($this->end_planete->nom_planete, $this->end_planete->galaxie, $this->end_planete->ss, $this->end_planete->position), 0);
$send->addInfo($max, 1);
$this->statut = 1;
if (!in_array('statut', $this->modifFlotte))
$this->modifFlotte[] = 'statut';
}
function coloniser()
{
//On vérifie que les coordonnées de la planètes sont bien enregistrée
if (empty($this->end_planete) || is_object($this->end_planete) || !preg_match('#^\[?([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})\]?$#', $this->end_planete))
{
$this->load_planete();
file_log("Erreur de colonisation de la planète : ".$this->end_planete." pour le joueur : ".$this->start_planete->id_user, 2);
send_mp($this->start_planete->id_user, "Erreur de colonisation [F#01]", "Une erreur s'est produite lors de la tentative de colonisation de votre flotte, elle a fait demi-tour.");
$this->rappeler();
}
//On vérifie que la planète ne soit pas déjà colonisée
global $var___db, $config, $table_planete;
$bdd = new bdd();
preg_match('#^\[?([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})\]?$#', $this->end_planete, $position);
$bdd->connexion();
$p = $bdd->query("SELECT * FROM $table_planete WHERE galaxie = ".$position[1]." AND ss = ".$position[2]." AND position = ".$position[3].";");
$bdd->deconnexion();
if ($p)
{
$this->load_planete();
$rapport = new Rapport(2, $this->start_planete->id_user, 0, $this->start_time + $this->end_time);
$rapport->addInfo(array($position[1], $position[2], $position[3]), 0);
$rapport->addInfo(false, 1);
$rapport->send();
}
else
{
$this->load_planete();
//On crée la planète
$this->end_planete = new Planete();
$this->end_planete->galaxie = $position[1];
$this->end_planete->ss = $position[2];
$this->end_planete->position = $position[3];
$this->end_planete->creer($this->start_planete->id_user);
//Rembousement du carburant non utilisé (la colonisation prévois au départ un allé/retour)
$conso = $this->calc_deplacement($this->start_planete->galaxie, $this->start_planete->ss, $this->start_planete->position, $position[1], $position[2], $position[3], $this->vitesse, false, true);
$this->end_planete->hydrogene += $conso[1];
//On définit la limite de ressources pour permettre le déchargement de celles contenues dans les vaisseaux
$this->end_planete->cap = 100000;
//On enlève un vaisseau de colonisation de la liste
$this->vaisseaux[2]--;
//On fait atterir les vaisseaux et décharger les ressources
$this->decharger();
$this->atterir();
//On envoie un rapport
$rapport = new Rapport(2, $this->start_planete->id_user, 0, $this->start_time + $this->end_time);
$rapport->addInfo(array($position[1], $position[2], $position[3]), 0);
$rapport->addInfo(true, 1);
$rapport->send();
}
}
function recycler()
{
//Si la planète d'arrivé n'est pas chargée, on charge les planètes
if (is_numeric($this->end_planete))
$this->load_planete();
$coeff = ($this->contenuMax - $this->contenu[0] - $this->contenu[1] - $this->contenu[2])/($this->end_planete->debris_met + $this->end_planete->debris_cri);
if ($coeff > 1)
$coeff = 1;
$a = floor($this->end_planete->debris_met * $coeff);
$b = floor($this->end_planete->debris_cri * $coeff);
$this->contenu[0] += $a;
$this->contenu[1] += $b;
if (!in_array('contenu', $this->modifFlotte))
$this->modifFlotte[] = 'contenu';
$this->end_planete->debris_met -= $a;
$this->end_planete->debris_cri -= $b;
if (!in_array('debris_met', $this->end_planete->modif))
$this->end_planete->modif[] = 'debris_met';
if (!in_array('debris_cri', $this->end_planete->modif))
$this->end_planete->modif[] = 'debris_cri';
//Send link
$rapport = new Rapport(4, $this->start_planete->id_user, 0, $this->start_time + $this->end_time);
$rapport->addInfo($this->end_planete, 0);
$rapport->addInfo(array($a, $b), 1);
$rapport->send();
$this->statut = 1;
if (!in_array('statut', $this->modifFlotte))
$this->modifFlotte[] = 'statut';
}
function espionnage()
{
//Si la planète d'arrivé n'est pas chargée, on charge les planètes
if (is_numeric($this->end_planete))
$this->load_planete();
$this->statut = 1;
if (!in_array('statut', $this->modifFlotte))
$this->modifFlotte[] = 'statut';
}
function decharger()
{
//Si la planète d'arrivé n'est pas chargée, on charge les planètes
if (is_numeric($this->end_planete))
$this->load_planete();
$max = array(0, 0, 0);
//Si on dépasse les capacités, on laisse les ressources en trop dans le cargo
if ($this->end_planete->metal + $this->contenu[0] > $this->end_planete->cap)
{
$max[0] = $this->end_planete->cap - $this->end_planete->metal;
if ($max[0] < 0) $max[0] = 0;
}
else
$max[0] = $this->contenu[0];
$this->end_planete->metal += $max[0];
$this->contenu[0] -= $max[0];
if ($this->end_planete->cristal + $this->contenu[1] > $this->end_planete->cap)
{
$max[1] = $this->end_planete->cap - $this->end_planete->cristal;
if ($max[1] < 0) $max[1] = 0;
}
else
$max[1] = $this->contenu[1];
$this->end_planete->cristal += $max[1];
$this->contenu[1] -= $max[1];
if ($this->end_planete->hydrogene + $this->contenu[2] > $this->end_planete->cap)
{
$max[2] = $this->end_planete->cap - $this->end_planete->hydrogene;
if ($max[2] < 0) $max[2] = 0;
}
else
$max[2] = $this->contenu[2];
$this->end_planete->hydrogene += $max[2];
$this->contenu[2] -= $max[2];
if (!in_array('contenu', $this->modifFlotte))
$this->modifFlotte[] = 'contenu';
return $max;
}
function atterir($planete = "end_planete")
{
//Si la planète d'arrivé n'est pas chargée, on charge les planètes
if (is_numeric($this->$planete))
$this->load_planete();
if (isset($this->$planete->vaisseaux[0]))
{
foreach ($this->vaisseaux as $key => $vais)
$this->$planete->vaisseaux[$key] += $vais;
}
else
$this->$planete->vaisseaux = $this->vaisseaux;
if (!in_array("vaisseaux", $this->$planete->modif))
$this->$planete->modif[] = "vaisseaux";
$this->modifFlotte = "DELETE";
}
function rappeler()
{
if ($this->start_time + $this->end_time >= time())
return false;
else
{
$this->end_time = time() - $this->start_time + 10;
$this->mission = 5;
if (!in_array('mission', $this->modifFlotte))
$this->modifFlotte[] = 'mission';
if (!in_array('end_time', $this->modifFlotte))
$this->modifFlotte[] = 'end_time';
return true;
}
}
function retourner()
{
//Si la planète de départ n'est pas chargée, on charge les planètes
if (is_numeric($this->start_planete))
$this->load_planete();
//Si on a demandé une planète particulière au retour
if (!empty($this->ret_time) && !empty($this->ret_planete) && !is_numeric($this->ret_planete))
{
foreach($this->vaisseaux as $key => $vais)
$this->ret_planete->vaisseaux[$key] += $vais;
}
//Si le retour se fait sur la planète source
else
{
foreach($this->vaisseaux as $key => $vais)
$this->start_planete->vaisseaux[$key] += $vais;
}
if (!in_array("vaisseaux", $this->start_planete->modif))
$this->start_planete->modif[] = "vaisseaux";
$this->modifFlotte = "DELETE";
}
/**
* Destructeur
*
* @return void
* @access public
*/
function __destruct()
{
global $var___db, $config, $table_flottes;
$nb = count($this->modifFlotte);
$out = array();
$bdd = new bdd();
$bdd->connexion();
if ($this->modifFlotte === "DELETE")
$bdd->query("DELETE FROM $table_flottes WHERE id = ".$this->id_flotte.";");
else
{
if (empty($this->id_flotte))
{
if ($this->modifFlotte == "INSERT")
{
$out1 = ''; $out2 = '';
global $nomvaisnVAR;
foreach ($this->vaisseaux as $key => $vais)
{
$out1 .= ', '.$nomvaisnVAR[$key];
$out2 .= ', '.$vais;
}
$sql = "INSERT INTO $table_flottes (id_user, mission, start_time, start_planete, end_time, end_planete, vitesse, contenu_metal, contenu_cristal, contenu_hydrogene, tactique, nom, nb_vais$out1) VALUES ('".$this->start_planete->id_user."', '".$this->mission."', '".$this->start_time."', '".$this->start_planete->id."', '".$this->end_time."', '".$this->end_planete."', '".$this->vitesse."', '".$this->contenu[0]."', '".$this->contenu[1]."', '".$this->contenu[2]."', '".$this->tactique."', '".$this->nom."', ".$this->nb_vais."$out2);";
//var_dump($sql);
$bdd->query($sql);
}
}
elseif(isset($this->modifFlotte[0]))
{
for($i = 0; $i < $nb; $i++)
{
if (!is_array($this->{$this->modifFlotte[$i]}))
{
$bdd->escape($this->{$this->modifFlotte[$i]});
if (is_int($this->{$this->modifFlotte[$i]}) || is_float($this->{$this->modifFlotte[$i]}))
$out[] .= $this->modifFlotte[$i]." = ".$this->{$this->modifFlotte[$i]};
else
$out[] .= $this->modifFlotte[$i]." = '".$this->{$this->modifFlotte[$i]}."'";
}
else {
if ($this->modifFlotte[$i] == "contenu")
$calc = "resso";
elseif ($this->modifFlotte[$i] == "vaisseaux")
$calc = "nomvaisn";
if (!isset(${$calc.'VAR'}))
global ${$calc.'VAR'};
$nombr = count(${$calc.'VAR'});
for($j = 0; $j < $nombr; $j++)
{
$bdd->escape($this->{$this->modifFlotte[$i]}[$j]);
$out[] = ${$calc.'VAR'}[$j]." = ".$this->{$this->modifFlotte[$i]}[$j];
}
}
}
if (!empty($out))
$bdd->query("UPDATE $table_flottes SET ".implode(', ', $out).", last = 0 WHERE id = ".$this->id_flotte.";");
else
$bdd->query("UPDATE $table_flottes SET last = 0 WHERE id = ".$this->id_flotte.";");
}
}
$bdd->deconnexion();
}
}
?>