forked from halo-battle/game
658 lines
No EOL
19 KiB
PHP
658 lines
No EOL
19 KiB
PHP
<?php
|
|
/***************************************************************************
|
|
* class.file.php
|
|
* ----------------
|
|
* begin : Samedi 11 octobre 2008
|
|
* update : Samedi 11 octobre 2008
|
|
* email : nemunaire@gmail.com
|
|
*
|
|
*
|
|
***************************************************************************/
|
|
class File
|
|
{
|
|
var $file;
|
|
|
|
function File($file)
|
|
{
|
|
$this->file = unserialize($file);
|
|
}
|
|
|
|
function reajusteVacances($timeVac)
|
|
{
|
|
if (isset($this->file[0]))
|
|
$this->file[0] += time() - $timeVac;
|
|
|
|
return serialize($this->file);
|
|
}
|
|
|
|
/**
|
|
* Ajoute $nombre objets $objet dans la file $file
|
|
* @param int $file Nom de la file d'attente
|
|
* @param int $objet Id de l'objet à ajouter
|
|
* @param int $nombre = 1 Nombre d'objet $objet à ajouter à la file
|
|
*
|
|
* @return int Numéro de l'erreur
|
|
* @access public
|
|
*/
|
|
function file_addObjet($file, $objet, $nombre = 1)
|
|
{
|
|
$planete = $this;
|
|
|
|
switch($file)
|
|
{
|
|
case "alli_batiments":
|
|
$court = "abat";
|
|
$calc = "alli_batiment";
|
|
global ${$calc.'LIMIT'};
|
|
$exist = $this->file_exist($objet, "file_".$court);
|
|
break;
|
|
|
|
case "batiments":
|
|
$court = "bat";
|
|
$calc = "batiment";
|
|
$exist = $this->file_exist($objet, "file_".$court);
|
|
break;
|
|
|
|
case "technologies":
|
|
1/0;
|
|
die('Erreur dans la file d\'attente ! #43, veuillez contacter un administrateur.');
|
|
break;
|
|
|
|
case "casernes":
|
|
$court = "cas";
|
|
$calc = "casernen";
|
|
$exist = false;
|
|
break;
|
|
|
|
case "terrestres":
|
|
$court = "ter";
|
|
$calc = "nomterrn";
|
|
$exist = false;
|
|
break;
|
|
|
|
case "vaisseaux":
|
|
$court = "vais";
|
|
$calc = "nomvaisn";
|
|
$exist = false;
|
|
break;
|
|
|
|
default:
|
|
return 1;
|
|
}
|
|
|
|
//On calcul la taille maximale de la file d'attente
|
|
if (!empty($this->technologies['techno_inge']))
|
|
{
|
|
if (($this->technologies['techno_inge'] &131072) == 131072) $max = 5;
|
|
elseif (($this->technologies['techno_inge'] &65536) == 65536) $max = 4;
|
|
elseif (($this->technologies['techno_inge'] &32768) == 32768) $max = 3;
|
|
else $max = 2;
|
|
}
|
|
else $max = 3; //Au cas où il n'y ait pas de technologie sur le lieu, on fixe la taille de la file d'attente
|
|
|
|
//Load the global variables for the line
|
|
global ${$calc}, ${$calc.'CALC'}, ${$calc.'TECH'};
|
|
|
|
//Check the conditions of building
|
|
if (empty(${$calc}[$objet]) || !requestDeblok(${$calc.'TECH'}[$objet], $this))
|
|
return 1;
|
|
|
|
//Check that we have not overtake the maximum level
|
|
if (!empty(${$calc.'LIMIT'}[$objet]) && ${$file}[$objet] >= ${$calc.'LIMIT'}[$objet])
|
|
return 1;
|
|
|
|
//Calculate next object's level
|
|
$n = $this->{$file}[$objet] + 1;
|
|
|
|
//Check that there isn't another object's entreaties yet building
|
|
if ($exist)
|
|
return 2;
|
|
|
|
//Check the sended number, it mustn't be negativ
|
|
if ($nombre < 0 && ($file != "batiments" || $nombre < -1))
|
|
return 5;
|
|
|
|
//Refresh time if no object in the line
|
|
if (count($this->{"file_".$court}) < 2)
|
|
$this->{"file_".$court}[0] = time();
|
|
|
|
|
|
if ($nombre > 0)
|
|
{
|
|
if ($file == "alli_batiments" || $file == "batiments")
|
|
{
|
|
eval(${$calc.'CALC'}[$objet][0]);
|
|
eval(${$calc.'CALC'}[$objet][1]);
|
|
eval(${$calc.'CALC'}[$objet][2]);
|
|
$d = 0;
|
|
}
|
|
elseif ($file == "technologies")
|
|
{
|
|
$a = $b = $c = 0;
|
|
$d = $technologiesCALC[$branch][$objet][2];
|
|
}
|
|
else
|
|
{
|
|
$a = ${$calc.'CALC'}[$objet][0];
|
|
$b = ${$calc.'CALC'}[$objet][1];
|
|
$c = ${$calc.'CALC'}[$objet][2];
|
|
$d = 0;
|
|
}
|
|
|
|
//Apply policy bonus for time and cost
|
|
if (isset($this->politique) && $this->politique == 1 && ($file == "casernes" || $file == "terrestres" || $file == "vaisseaux"))
|
|
{
|
|
$a *= 0.9;
|
|
$b *= 0.9;
|
|
$c *= 0.9;
|
|
}
|
|
//Apply bonus from technology for the mines
|
|
if ($file == "batiments" && $objet < 4 && isset($this->technologies))
|
|
{
|
|
if ($this->technologies[0] &262144)
|
|
{
|
|
$a *= 0.85;
|
|
$b *= 0.85;
|
|
$c *= 0.85;
|
|
}
|
|
elseif ($this->technologies[0] &131072)
|
|
{
|
|
$a *= 0.9;
|
|
$b *= 0.9;
|
|
$c *= 0.9;
|
|
}
|
|
elseif ($this->technologies[0] &65536)
|
|
{
|
|
$a *= 0.95;
|
|
$b *= 0.95;
|
|
$c *= 0.95;
|
|
}
|
|
}
|
|
|
|
//Check maximum number unity building on this planet with means
|
|
if ($nombre > 1)
|
|
{
|
|
if ($a) $nombre = min(floor($this->metal/$a), $nombre);
|
|
if ($b) $nombre = min(floor($this->cristal/$b), $nombre);
|
|
if ($c) $nombre = min(floor($this->hydrogene/$c), $nombre);
|
|
if ($d) $nombre = min(floor($this->credits/$d), $nombre);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
$a = 0;
|
|
$b = 0;
|
|
$c = 0;
|
|
$d = 0;
|
|
}
|
|
|
|
//Check means on the planet
|
|
if ($this->metal < $a * $nombre || $this->cristal < $b * $nombre || $this->hydrogene < $c * $nombre || $this->credits < $d * $nombre)
|
|
return 3;
|
|
else
|
|
{
|
|
//Line generate
|
|
$nb = count($this->{"file_".$court});
|
|
|
|
//If the last object is similar as this one
|
|
if ($file == "technologies" && $max <= count($this->{"file_".$court}))
|
|
return 4; //Check height line
|
|
elseif ($file == "technologies")
|
|
$this->{"file_".$court}[] = array($objet, $branch);
|
|
elseif ($nombre > 99999 || ($nb > 1 && $objet == $this->{"file_".$court}[$nb-1][0] && $this->{"file_".$court}[$nb-1][1] + $nombre > 99999))
|
|
erreur('Dépacement de capacité !<br />Vous ne pouvez pas mettre autant d\'unités en file d\'attente en une seule fois.');
|
|
elseif ($nb > 1 && $objet == $this->{"file_".$court}[$nb-1][0])
|
|
$this->{"file_".$court}[$nb-1][1] += $nombre;
|
|
elseif ($max <= count($this->{"file_".$court}))
|
|
return 4; //Check height line
|
|
else
|
|
$this->{"file_".$court}[] = array($objet, $nombre);
|
|
|
|
//Update means on the planet after building
|
|
$this->metal -= $a * $nombre;
|
|
$this->cristal -= $b * $nombre;
|
|
$this->hydrogene -= $c * $nombre;
|
|
$this->credits -= $d * $nombre;
|
|
|
|
if (!in_array("file_".$court, $this->modif))
|
|
$this->modif[] = "file_".$court;
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ajoute à la file d'attente technologique la technologie $technology dans la branche $branch
|
|
* @param int $branch Branche technologique dans laquel se trouve la technologie
|
|
* @param int $technology ID de la technologie à ajouter
|
|
*
|
|
* @return int Numéro de l'erreur
|
|
* @access public
|
|
*/
|
|
function file_addTechno($branch, $technology)
|
|
{
|
|
$planete = $this;
|
|
|
|
//Calculate maximum height line
|
|
if (!empty($this->technologies['techno_inge']))
|
|
{
|
|
if (($this->technologies['techno_inge'] &131072) == 131072) $max = 5;
|
|
elseif (($this->technologies['techno_inge'] &65536) == 65536) $max = 4;
|
|
elseif (($this->technologies['techno_inge'] &32768) == 32768) $max = 3;
|
|
else $max = 2;
|
|
}
|
|
else $max = 3;
|
|
|
|
//Load globals variables
|
|
global $technologiesCALC;
|
|
|
|
//Check branch and technology
|
|
if (empty($technologiesCALC[$branch][$technology]))
|
|
return 1;
|
|
|
|
//Check search conditions
|
|
if (!(($this->technologies[$branch] &$technologiesCALC[$branch][$technology][1]) == $technologiesCALC[$branch][$technology][1] && !($this->technologies[$branch] &$technologiesCALC[$branch][$technology][0] && $this->technologies[$branch] != 0)))
|
|
return 1;
|
|
|
|
//Check that there isn't another object's entreaties yet building
|
|
//TODO
|
|
if ($this->file_existTech($branch, $technology))
|
|
return 2;
|
|
|
|
//Refresh time if that is the first search
|
|
if (count($this->file_tech) < 2)
|
|
$this->file_tech[0] = time();
|
|
|
|
//Calculate cost and time
|
|
$a = $b = $c = 0;
|
|
$d = $technologiesCALC[$branch][$technology][2];
|
|
|
|
//Check means on planet
|
|
if ($this->metal < $a || $this->cristal < $b || $this->hydrogene < $c || $this->credits < $d) return 3;
|
|
else
|
|
{
|
|
if ($max <= count($this->file_tech))
|
|
return 4; //Vérification de la taille de la file d'attente
|
|
else
|
|
$this->file_tech[] = array($technology, $branch);
|
|
|
|
//Update means on the planet after start searching
|
|
$this->metal -= $a;
|
|
$this->cristal -= $b;
|
|
$this->hydrogene -= $c;
|
|
$this->credits -= $d;
|
|
|
|
if (!in_array("file_tech", $this->modif))
|
|
$this->modif[] = "file_tech";
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
function file_delObjet($file, $objet, $nombre = 1, $w = 99)
|
|
{
|
|
$planete = $this;
|
|
|
|
switch($file)
|
|
{
|
|
case "alli_batiments":
|
|
$court = "abat";
|
|
$calc = "alli_batiment";
|
|
$exist = $this->file_exist($objet, "file_".$court);
|
|
break;
|
|
|
|
case "batiments":
|
|
$court = "bat";
|
|
$calc = "batiment";
|
|
$exist = $this->file_exist($objet, "file_".$court);
|
|
break;
|
|
|
|
case "technologies":
|
|
1/0;
|
|
die('Erreur dans la file d\'attent : #273. Contactez un administrateur.');
|
|
break;
|
|
|
|
case "casernes":
|
|
$court = "cas";
|
|
$calc = "casernen";
|
|
$exist = $this->file_exist($objet, "file_".$court);
|
|
break;
|
|
|
|
case "terrestres":
|
|
$court = "ter";
|
|
$calc = "nomterrn";
|
|
$exist = $this->file_exist($objet, "file_".$court);
|
|
break;
|
|
|
|
case "vaisseaux":
|
|
$court = "vais";
|
|
$calc = "nomvaisn";
|
|
$exist = $this->file_exist($objet, "file_".$court);
|
|
break;
|
|
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
//Si l'objet n'est pas dans la file d'attente, on annule la suite
|
|
if (!$exist)
|
|
return 0;
|
|
|
|
if ($w == 99)
|
|
$w = count($this->{"file_".$court})-1;
|
|
|
|
global ${$calc}, ${$calc.'CALC'};
|
|
|
|
for($i = $w; $i > 0; $i--)
|
|
{
|
|
if($this->{"file_".$court}[$i][0] == $objet)
|
|
{
|
|
$nombre = min(abs($nombre), $this->{"file_".$court}[$i][1]);
|
|
|
|
if($this->{"file_".$court}[$i][1] <= $nombre)
|
|
{
|
|
unset($this->{"file_".$court}[$i]);
|
|
$this->{"file_".$court} = array_merge($this->{"file_".$court});
|
|
if ($i == 1)
|
|
$this->{"file_".$court}[0] = time();
|
|
}
|
|
else
|
|
$this->{"file_".$court}[$i][1] -= $nombre;
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
//Calcul du prochain niveau de l'objet
|
|
$n = $this->{$file}[$objet] + 1;
|
|
|
|
|
|
if (!in_array("file_".$court, $this->modif))
|
|
$this->modif[] = "file_".$court;
|
|
|
|
if ($nombre > 0)
|
|
{
|
|
if ($file == "batiments")
|
|
{
|
|
eval(${$calc.'CALC'}[$objet][0]);
|
|
eval(${$calc.'CALC'}[$objet][1]);
|
|
eval(${$calc.'CALC'}[$objet][2]);
|
|
$d = 0;
|
|
}
|
|
else
|
|
{
|
|
$a = ${$calc.'CALC'}[$objet][0];
|
|
$b = ${$calc.'CALC'}[$objet][1];
|
|
$c = ${$calc.'CALC'}[$objet][2];
|
|
$d = 0;
|
|
}
|
|
|
|
//On applique les bonus politiques aux temps et coûts
|
|
if (isset($this->politique) && $this->politique == 1 && ($file == "casernes" || $file == "terrestres" || $file == "vaisseaux"))
|
|
{
|
|
$a *= 0.9;
|
|
$b *= 0.9;
|
|
$c *= 0.9;
|
|
}
|
|
|
|
//Mise à jour des ressources de la planète en conséquence à la construction
|
|
$this->metal += $a * $nombre;
|
|
$this->cristal += $b * $nombre;
|
|
$this->hydrogene += $c * $nombre;
|
|
$this->credits += $d * $nombre;
|
|
}
|
|
|
|
return $nombre;
|
|
}
|
|
|
|
function file_delTechno($id)
|
|
{
|
|
$planete = $this;
|
|
|
|
//Si l'objet n'est pas dans la file d'attente, on annule la suite
|
|
//TODO
|
|
if (!isset($this->file_tech[$id+1]))
|
|
return 0;
|
|
else
|
|
{
|
|
global $technologiesCALC;
|
|
//On réinitialise le temps si c'est la première série de la liste
|
|
if ($id == 0)
|
|
$this->file_tech[0] = time();
|
|
|
|
$d = $technologiesCALC[$this->file_tech[$id+1][1]][$this->file_tech[$id+1][0]][2];
|
|
|
|
unset($this->file_tech[$id+1]);
|
|
$this->file_tech = array_merge($this->file_tech);
|
|
}
|
|
|
|
|
|
if (!in_array("file_tech", $this->modif))
|
|
$this->modif[] = "file_tech";
|
|
|
|
$a = $b = $c = 0;
|
|
|
|
//Mise à jour des ressources de la planète en conséquence à la construction
|
|
$this->metal += $a;
|
|
$this->cristal += $b;
|
|
$this->hydrogene += $c;
|
|
$this->credits += $d;
|
|
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Vérifie l'existance dans la file $file d'attente de $objet
|
|
* @param int $objet ID à vérifier
|
|
* @param string $file Nom de la file d'attente
|
|
*
|
|
* @return boolean
|
|
* @access public
|
|
*/
|
|
function file_exist($objet, $file)
|
|
{
|
|
//On traite le cas d'une file type arbre
|
|
if (is_numeric($file)) {
|
|
1/0;
|
|
die('A corriger, File d\'attente : #422. Contacter un administrateur');
|
|
}
|
|
else {
|
|
if (count($this->$file) <= 1) return false;
|
|
foreach($this->$file as $key => $bout){
|
|
if ($key == 0) continue;
|
|
if($objet == $bout[0]) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function file_existTech($branch, $technology)
|
|
{
|
|
foreach($this->file_tech as $key => $part){
|
|
if ($key == 0) continue;
|
|
|
|
if($technology == $part[0] && $branch == $part[1]) return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Actualise la file $file en terminant les constructions/entraînements.
|
|
* @param string $file Nom de la file d'attente
|
|
*
|
|
* @return boolean
|
|
* @access public
|
|
*/
|
|
function file_ready($file)
|
|
{
|
|
$planete = $this;
|
|
|
|
switch($file)
|
|
{
|
|
case "alli_batiments":
|
|
$court = "abat";
|
|
$calc = "alli_batiment";
|
|
$exist = $this->file_exist($objet, "file_".$court);
|
|
break;
|
|
|
|
case "batiments":
|
|
$court = "bat";
|
|
$calc = "batiment";
|
|
break;
|
|
|
|
case "technologies":
|
|
die('Erreur dans la file d\'attente : #449. Veuillez contacter un administrateur.');
|
|
break;
|
|
|
|
case "casernes":
|
|
$court = "cas";
|
|
$calc = "casernen";
|
|
break;
|
|
|
|
case "terrestres":
|
|
$court = "ter";
|
|
$calc = "nomterrn";
|
|
break;
|
|
|
|
case "vaisseaux":
|
|
$court = "vais";
|
|
$calc = "nomvaisn";
|
|
break;
|
|
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
if (empty($this->{"file_".$court}[0]))
|
|
return false;
|
|
|
|
//Calcul du temps écoulé depuis le dernier entrainement validé
|
|
$tps = time() - $this->{"file_".$court}[0];
|
|
|
|
global ${$calc}, ${$calc.'CALC'};
|
|
|
|
foreach($this->{"file_".$court} as $key => $bout)
|
|
{
|
|
if ($key == 0) continue;
|
|
|
|
if ($bout[1] < 0) $n = $this->{$file}[$bout[0]]; //Extraction du niveau en cours
|
|
else $n = $this->{$file}[$bout[0]] + 1; //Extraction du niveau en cours
|
|
eval(${$calc.'CALC'}[$bout[0]][3]); //Récupération du temps de construction
|
|
|
|
//On applique le bonus de temps des ingénieurs et des centrales informatiques
|
|
if ($file == "batiments" && $bout[0] != 11) $sec /= pow(1.23,$this->batiments[11]) + 0.0025*$this->casernes[7];
|
|
|
|
|
|
//Accélération du temps de construction
|
|
$sec /= VITESSE;
|
|
|
|
if ($bout[1] < 0)
|
|
{
|
|
$sec *= 0.6;
|
|
|
|
if ($sec < $tps)
|
|
{
|
|
eval(${$calc.'CALC'}[$bout[0]][0]);
|
|
eval(${$calc.'CALC'}[$bout[0]][1]);
|
|
eval(${$calc.'CALC'}[$bout[0]][2]);
|
|
|
|
//On reçoit 70% des ressources en bonus
|
|
$this->metal += $a * 0.7;
|
|
$this->cristal += $b * 0.7;
|
|
$this->hydrogene += $c * 0.7;
|
|
|
|
$this->{$file}[$bout[0]]--;
|
|
$this->{"file_".$court}[0] += $bout[1] * $sec;
|
|
$tps -= $sec;
|
|
unset($this->{"file_".$court}[$key]);
|
|
|
|
if (!in_array($file, $this->modif)) $this->modif[] = $file;
|
|
if (!in_array("file_".$court, $this->modif)) $this->modif[] = "file_".$court;
|
|
}
|
|
}
|
|
elseif ($sec * $bout[1] < $tps && $bout[1] > 0)
|
|
{
|
|
//S'il s'agit d'un silo, on sauvegarde le temps pour utilisation par le script de production
|
|
if ($file == "batiments" && $bout[0] == 10) $timestamp_lastSilo = $this->{"file_".$court}[0];
|
|
if ($file == "batiments" && $bout[0] == 0) $timestamp_mineM = $this->{"file_".$court}[0];
|
|
if ($file == "batiments" && $bout[0] == 1) $timestamp_mineC = $this->{"file_".$court}[0];
|
|
if ($file == "batiments" && $bout[0] == 2) $timestamp_mineH = $this->{"file_".$court}[0];
|
|
|
|
$this->{$file}[$bout[0]] += $bout[1];
|
|
$this->{"file_".$court}[0] += $bout[1] * $sec;
|
|
$tps -= $bout[1] * $sec;
|
|
unset($this->{"file_".$court}[$key]);
|
|
|
|
if (!in_array($file, $this->modif)) $this->modif[] = $file;
|
|
if (!in_array("file_".$court, $this->modif)) $this->modif[] = "file_".$court;
|
|
}
|
|
elseif ($sec < time() - $this->{"file_".$court}[0] && $bout[1] > 0)
|
|
{
|
|
$j = floor($tps/$sec);
|
|
|
|
$this->{"file_".$court}[$key][1] -= $j;
|
|
$this->{$file}[$bout[0]] += $j;
|
|
$this->{"file_".$court}[0] += $j * $sec;
|
|
$tps -= $j * $sec;
|
|
|
|
if (!in_array($file, $this->modif)) $this->modif[] = $file;
|
|
if (!in_array("file_".$court, $this->modif)) $this->modif[] = "file_".$court;
|
|
|
|
//Comme la première attente n'est pas terminée, on stoppe le tout
|
|
break;
|
|
}
|
|
else break;
|
|
}
|
|
//On actualise seulement s'il y a eu une modification de faite
|
|
if (in_array($file, $this->modif)) $this->actualiser(false);
|
|
$this->{"file_".$court} = array_merge($this->{"file_".$court});
|
|
}
|
|
|
|
function file_readyTechno($file)
|
|
{
|
|
$planete = $this;
|
|
|
|
$court = "tech"; $calc = "technolo";
|
|
|
|
if (empty($this->file_tech[0])) return false;
|
|
|
|
//Calcul du temps écoulé depuis le dernier entrainement validé
|
|
$tps = time() - $this->file_tech[0];
|
|
|
|
global $technologiesCALC;
|
|
|
|
foreach($this->file_tech as $key => $bout)
|
|
{
|
|
if ($key == 0) continue;
|
|
|
|
//Récupération du temps de recherche
|
|
$sec = $technologiesCALC[$bout[1]][$bout[0]][3] / (1 + $this->batiments[6] * 0.01);
|
|
|
|
//Accélération du temps de construction en fonction de la vitesse du serveur
|
|
$sec /= VITESSE;
|
|
|
|
if ($sec < $tps)
|
|
{
|
|
$this->technologies[$bout[1]] |= $technologiesCALC[$bout[1]][$bout[0]][0];
|
|
$this->file_tech[0] += $sec;
|
|
$tps -= $sec;
|
|
unset($this->file_tech[$key]);
|
|
$this->file_tech = array_merge($this->file_tech);
|
|
|
|
//La déouverte de technologie est bénéfique pour le moral de la population
|
|
if ($this->moral) {
|
|
if ($this->politique == 3) $this->setMoral(0.1); //Démocratie : Découvrir une nouvelle technologie a plus d'effet bénéfique sur le moral
|
|
else $this->setMoral(0.05);
|
|
}
|
|
|
|
if (!in_array("technologies", $this->modifUser)) $this->modifUser[] = "technologies";
|
|
if (!in_array("file_tech", $this->modif)) $this->modif[] = "file_tech";
|
|
}
|
|
else break;
|
|
}
|
|
if (in_array("file_tech", $this->modif)) $this->actualiser(false);
|
|
}
|
|
}
|
|
?>
|