<?php
/**
* classe che gestisce le civiltà
* @property int $status
* @property DBRow data
* @property int $cid
* @property DBRow $resource
* @property DBRow $village
* @property array $village_list
* @property Array $ages
* @property buildings $building
* @property Array $queue
* @property Array $ages
* @property Array $nameResource
* @property Array $troopers_now
* @property Array $my_troopers
* @property Option $option
*/
class civilta {
public $status = null;
//utente in attesa o meno senza civiltà
public $data = null;
public $cid = null;
public $resource = null;
private $currentVillage = null;
public $village = null;
public $village_list = null;
public $building = null;
private $queue = null;
private $ages = array('Preistorica', 'Antica', 'Medioevale', 'Industriale', 'Moderna', 'Futura');
public $nameResource = array(array('pietra', 'cibo', 'legno'), array('sale', 'bronzo', 'legno'), array('oro', 'ferro', 'pietra'), array('dollari', 'carbone', 'cemento'), array('crediti', 'petrolio', 'acciaio'), array('nanomacchine', '(2)', '(3)'));
public $troopers_now = null;
public $poptroop = 0;
public $my_troopers = null;
public $other_troopers = null;
public $outAttack = null;
public $inAttack = null;
public $outReinf = null;
public $inReinf = null;
public $training = null;
private $debug = false;
public $sharer = null;
public $option=null;
/**
* @param int $id user id
* cerca nel DB l'id dell'utente passato come parametro, e definisce i parametri della classe.
*/
function __construct($id) {
global $fw;
$result = $fw->db->sql_query("SELECT `civ_id`,`status`,`current_village` FROM `" . RELATION_USER_CIV_TABLE . "` WHERE `user_id`='" . $id . "' AND `server`='" . SERVER . "'");
$cid = $fw->db->sql_fetchrow($result);
$this->status = (int) $cid['status'];
if ($this->status)
$this->cid = (int) $cid['civ_id'];
if ($this->status > 1) {
$this->cid = (int) $cid['civ_id'];
$this->data = new DBRow(CIV_TABLE, $cid['civ_id'], "civ_id");
$this->option=new Option($this->cid, "civilta");
//lista villaggi
if (is_numeric($_GET['vid'])) {
$cid['current_village'] = $_GET['vid'];
$this->changeCurrentVillage($_GET['vid']);
}
$this->village_list = $fw->db->Sql_totable("SELECT `id`,`name`,`x`,`y` FROM `" . VILLAGE_TABLE . "` WHERE `civ_id`='" . $this->cid . "'");
for ($i = 0, $bool = false; ($this->village_list[$i]) && (!$bool); $i++)
if ($cid['current_village'] == $this->village_list[$i]['id'])
$bool = true;
// controllo se il current_village è nella lista
if ($bool)
$this->currentVillage = (int) $cid['current_village'];
else {// se non c'e imposto il current_village con il primo pianeta della lista
$this->currentVillage = (int) $this->village_list[0]['id'];
$this->changeCurrentVillage($this->currentVillage);
}
$this->village = new DBRow(VILLAGE_TABLE, $this->currentVillage, "id", "`name`,`capital`,`type`,`civ_id`,`x`,`y`");
$this->resource = new DBRow(VILLAGE_TABLE, $this->currentVillage, "id", "`resource_1`,`resource_2`,`resource_3`,`production_1`,`production_2`,`production_3`,`agg`,`pop`,`busy_pop`,`aggPop`");
$this->building = new buildings($this->cid, $this->currentVillage);
$id = $this->getCurrentVillage();
//code edifici
$this->queue = $fw->db->sql_totable("SELECT * FROM `" . EVENTS_TABLE . "` WHERE `type`='1' AND `params`LIKE'%\"village_id\";i:" . $id . "%' ORDER BY `id` ASC");
//gestione truppe
//truppe nel villaggio
$this->troopers_now = $fw->db->sql_totable("SELECT * FROM `" . TROOPERS . "` WHERE `civ_id`='" . $this->cid . "' AND `village_now`='" . $this->currentVillage . "' AND `village_prev`='" . $this->currentVillage . "' ORDER BY `trooper_id`");
//truppe in altri villi
$this->my_troopers = $fw->db->sql_totable("SELECT `" . TROOPERS . "`.*,`" . VILLAGE_TABLE . "`.`name`,`" . VILLAGE_TABLE . "`.`x`,`" . VILLAGE_TABLE . "`.`y` FROM `" . TROOPERS . "`,`" . VILLAGE_TABLE . "` WHERE `village_prev`='" . $this->currentVillage . "' AND `village_now`!='" . $this->currentVillage . "' AND `village_now`=`" . VILLAGE_TABLE . "`.`id` ORDER BY `village_now`,`trooper_id`");
//truppe straniere nel proprio villo
$this->other_troopers = $fw->db->sql_totable("SELECT `" . TROOPERS . "`.*,`" . VILLAGE_TABLE . "`.`name`,`" . VILLAGE_TABLE . "`.`x`,`" . VILLAGE_TABLE . "`.`y` FROM `" . TROOPERS . "`,`" . VILLAGE_TABLE . "` WHERE `village_prev`!='" . $this->currentVillage . "' AND `village_now`='" . $this->currentVillage . "' AND `village_prev`=`" . VILLAGE_TABLE . "`.`id` ORDER BY `village_prev`,`trooper_id`");
//movimenti truppa
debug("other_troopers", $this->other_troopers, "civilta", 80, $this->debug);
debug("my_troopers", $this->my_troopers, "civilta", 80, $this->debug);
$movements = $fw->db->sql_totable("SELECT * FROM `" . EVENTS_TABLE . "` WHERE `type`='" . MOVEMENTS_EVENT . "' AND (`params`LIKE'%\"village_A\";i:" . $this->currentVillage . "%' OR `params`LIKE'%\"village_B\";i:" . $this->currentVillage . "%') ORDER BY `time`");
debug("movements", $movements, "civilta", 83, $this->debug);
for ($i = 0; $movements[$i]; $i++) {
$param = unserialize($movements[$i]['params']);
debug("type", $movements[$i]['params'], "civilta", 86, $this->debug);
switch ($param['type']) {
case ATTACK:
case RAID:
if ($param['village_A'] == $this->currentVillage)
$this->outAttack[] = $movements[$i];
else
$this->inAttack[] = $movements[$i];
break;
case RETURN_T:
case REINFORCEMENT:
if ($param['village_A'] == $this->currentVillage) {
if ($param['civ_id'] == $this->cid)
$this->outReinf[] = $movements[$i];
}
else
$this->inReinf[] = $movements[$i];
break;
default :
}
}
debug("inreinf", $this->inReinf, "civilta", 97, $this->debug);
debug("inAttack", $this->inAttack, "civilta", 97, $this->debug);
debug("outAttack", $this->outAttack, "civilta", 97, $this->debug);
debug("outReinf", $this->outReinf, "civilta", 97, $this->debug);
$this->training = $fw->db->sql_totable("SELECT * FROM `" . EVENTS_TABLE . "` WHERE `type`='" . TRAINING_EVENT . "' AND `params`LIKE'%\"village_id\";i:" . $this->currentVillage . "%' ORDER BY `time`");
$this->sharer = $fw->db->sql_totable("SELECT `username`,`status`,`user_id`, IF( EXISTS ( SELECT `last_activity` FROM `" . SESSIONS_TABLE . "` WHERE `" . SESSIONS_TABLE . "`.`user_id` = `" . USERS_TABLE . "`.`ID` ) >0, (SELECT `last_activity` FROM `" . SESSIONS_TABLE . "` WHERE `" . SESSIONS_TABLE . "`.`user_id` = `" . USERS_TABLE . "`.`ID` ORDER BY `last_activity` DESC LIMIT 1), '0' ) AS `last_activity`
FROM `" . RELATION_USER_CIV_TABLE . "` , `" . CIV_TABLE . "` , `" . USERS_TABLE . "`
WHERE `" . CIV_TABLE . "`.`civ_id` = '" . $this->cid . "'
AND `" . RELATION_USER_CIV_TABLE . "`.`civ_id`=`" . CIV_TABLE . "`.`civ_id`
AND `" . RELATION_USER_CIV_TABLE . "`.`user_id`=`" . USERS_TABLE . "`.`ID`");
}
}
/**
* @param String $permix
* @param Bool $dp
* @return bool
*/
function can($permix, $dp = false) {
global $fw;
$q = $fw->db->sql_query("SELECT `value` FROM `" . ALLYPERM_TABLE . "` WHERE `civ_id='" . $fw->civ->cid . "' AND `name`='" . $fw->db->t($permix) . "' LIMIT 1;");
if (mysql_num_rows($q) == 0) {
return $dp;
}
else {
if ($fw->db->sql_result($q, 0, 0) == 1) {
return true;
}
else {
return false;
}
}
}
/**
* form per la nuova civiltà
*/
function registerf() {
echo '<script type="text/javascript" src="scripts/civ.js"></script>
<h2>crea una nuova civiltà</h2><span id="load"></span>
<p id="mess"><form name="civ"><table>
<tr>
<td>Nome:</td>
<td><input id="name" size="20" maxlength="30" /></td>
</tr>
<tr>
<td>Aggettivo</td>
<td><input id="agg" size="20" maxlength="30" /></td>
</tr>
<tr>
<td colspan="2"><input type="radio" name="sector" value="0" checked="true" /> settore casuale</td>
</tr>
<tr>
<td><input type="radio" name="sector" value="1" /> nord/est (+/+)</td>
<td><input type="radio" name="sector" value="2" /> nord/ovest (-/+)</td>
</tr><tr>
<td><input type="radio" name="sector" value="3" /> sud/ovest (-/-)</td>
<td><input type="radio" name="sector" value="4" /> sud/est (+/-)</td>
</tr>
<tr>
<td colspan="2"><input type="button" value="crea" onclick="createciv()" /></td>
</tr>
</table>
</form></p>';
}
/**
* @param Array $vect , indice per il campo e valore come valore
* @return bool
* registra un utente, ritorna true se la registrazione è andata bene.
*/
function register($vect) {
global $fw;
if ($vect) {
$value = array();
foreach ($vect as $campo => $valore) {
$value[] = "`" . $fw->db->t($campo) . "`='" . $fw->db->t($valore) . "'";
}
$fw->db->sql_query("INSERT INTO " . CIV_TABLE . " SET " . implode(",", $value));
return true;
}
else {
return false;
}
}
/**
* @param Array $vect, indice per il campo e valore come valore
* @param int $id user id
* @return bool
* modifica i valori della civiltà
*/
function update($vect, $id = false) {
global $fw;
print_r($vect);
if ($vect) {
$value = array();
foreach ($vect as $campo => $valore) {
$value[] = "`" . $fw->db->t($campo) . "`='" . $fw->db->t($valore) . "'";
$this->data->$campo = $valore;
}
$fw->db->sql_query("UPDATE " . CIV_TABLE . " SET " . implode(",", $value) . " WHERE `civ_id`='" . ($id ? $id : $this->data->ID) . "'");
}
return true;
}
/**
* @param int $age (parte da 1)
* @return String
* ritorna il nome di un'et� dall'eta corrente
*/
function getCivAge($age = 0) {
return $this->ages[($age ? $age - 1 : $this->data->civ_age)];
}
/**
* aggiunge il legame user civiltà
* @param int $civ
* @param int $id
* @param bool $owner
* @return bool
*/
function subscrive($civ, $id = false, $owner = true) {
global $fw;
if (!$id) {
$id = $fw->user->data->ID;
}
if ($owner)
$status = 3;
else
$status = 1;
// @todo da aggiungere controllo iscrizioni
$fw->db->sql_query("INSERT INTO " . RELATION_USER_CIV_TABLE . " SET `user_id`='" . $id . "' , `server`='" . SERVER . "' , `civ_id`='" . $civ . "' , `status`='" . $status . "'");
return true;
}
/**
* restituisce le risorse
* @return Array
*/
function getResource() {
$this->aggResource();
return array($this->resource->resource_1, $this->resource->resource_2, $this->resource->resource_3);
}
/**
* aggiorna le risorse in caso di transazioni di risorse
* @param Array $trans
* @param bool $add
*/
function aggResource($trans=array(0, 0, 0, 0), $add=false) {
global $fw;
global $Building_Array;
global $Troops_Array;
if ($add) {
for ($i = 0; $i < 4; $i++) {
$trans[$i] = -$trans[$i];
}
}
//popolazione in truppe
debug("troop now", $this->troopers_now, "agg", 337, $this->debug);
for ($i = 0; ($this->troopers_now[$i]) || ($this->my_troopers[$i]); $i++) {
if ($this->troopers_now[$i])
$this->poptroop+=$this->troopers_now[$i]['numbers'] * $Troops_Array[$this->troopers_now[$i]['trooper_id']]::$pop;
if ($this->my_troopers[$i])
$this->poptroop+=$this->my_troopers[$i]['numbers'] * $Troops_Array[$this->my_troopers[$i]['trooper_id']]::$pop;
}
debug("poptroop", $this->poptroop, "agg", 337, $this->debug);
//controllo pop max e min
for ($i = 0; $i < TOTBUILDING; $i++) {
for ($s = 0, $j = 0; $j < $this->building->getLiv($i); $s+=++$j
);
if ($this->building->data[$i] != null) {
$maxpop = $s * $Building_Array[$this->building->getType($i) - 1]::$maxPop[$this->getAge()];
if ($this->building->data[$i]['pop'] > $maxpop) {
$this->building->data[$i]['pop'] = $maxpop;
$fw->db->sql_query("UPDATE `" . BUILDING_TABLE . "` SET `pop`='" . $maxpop . "' WHERE `village_id`='" . $this->getCurrentVillage() . "' AND `pos`='" . $i . "'");
}
//controllo pop occupata minima
if ($this->building->data[$i]['pop'] < $s) {
$this->building->data[$i]['pop'] = $s;
$fw->db->sql_query("UPDATE `" . BUILDING_TABLE . "` SET `pop`='" . $s . "' WHERE `village_id`='" . $this->getCurrentVillage() . "' AND `pos`='" . $i . "'");
}
}
}
//pop nelle strutture in coda
$queue = $this->getQueue();
$popc = 0;
for ($i = 0; $queue[$i]; $i++) {
$param = unserialize($queue[$i]['params']);
$popc+=$this->building->getLiv($param['pos']) + 1;
}
// popolazione nelle strutture
$this->resource->busy_pop = $fw->db->sql_tovariable("SELECT SUM( `pop` ) FROM `s1_building` WHERE `village_id` = '" . $this->currentVillage . "'") + $popc;
$maxP = $this->building->getCapTot(HOUSE);
$popT = $this->poptroop;
$busypop = $this->resource->busy_pop;
$negativ = $popT + $busypop - $maxP;
if ($negativ < 0)
$negativ = 0;
for ($i = 0; $this->other_troopers[$i]; $i++) {
$negativ+=$this->other_troopers[$i]['numbers'] * $Troops_Array[$this->other_troopers[$i]['trooper_id']]::$pop;
}
$this->village->negativ = $negativ;
debug("negativ", $negativ, "agg", 308, $this->debug);
$this->aggProd();
$now = mktime();
$agg = $this->resource->agg;
// ore di differenza dall'ultimo aggiornamento
$dif = ($now - $agg) / 3600;
//aggiornamento risorse
$this->resource->resource_1+=$dif * ($this->resource->production_1 - $negativ);
$this->resource->resource_2+=$dif * $this->resource->production_2;
$this->resource->resource_3+=$dif * $this->resource->production_3;
// controllo saturamento magazzino
$sto1 = $this->building->getCapTot();
$sto2 = $this->building->getCapTot(STORAGE2);
if ($this->resource->resource_1 > $sto1)
$this->resource->resource_1 = $sto1;
if ($this->resource->resource_2 > $sto2)
$this->resource->resource_2 = $sto2;
if ($this->resource->resource_3 > $sto2)
$this->resource->resource_3 = $sto2;
//applicazione transazioni
debug("resource", $this->resource, "civ", 255, $this->debug);
$this->resource->resource_1-=$trans[0];
$this->resource->resource_2-=$trans[1];
$this->resource->resource_3-=$trans[2];
debug("resource", $this->resource, "civ", 259, $this->debug);
//controllo morte truppe
$prod = ($this->resource->production_1 - $negativ);
debug("prod", $prod, "agg", 351, $this->debug);
debug("resource", $this->resource->resource_1, "agg", 351, $this->debug);
debug("other_troopers", $this->other_troopers, "agg", 351, $this->debug);
while (($this->resource->resource_1 < 0) && ($prod < 0)) {
if ($this->other_troopers) {//se ci sono rinforzi iniziamo con loro
$prod=$this->killtroop($this->other_troopers, $prod);
}
elseif ($this->troopers_now) {
$prod=$this->killtroop($this->troopers_now, $prod);
}
else {
$prod=$this->killtroop($this->my_troopers, $prod);
}
}
if ($this->resource->resource_1 < 0)
$this->resource->resource_1 = 0;
/**
* aggiornamento popolazione.
*/
$dif = ($now - $this->resource->aggPop) / 3600;
debug("dif", $dif, "agg", 475, $this->debug);
debug("now", $now, "agg");
if ($dif > 1) {// se da +di un ora che non aggiorno
$pop = $this->resource->pop;
debug("maxp", $maxP, "agg", 475, $this->debug);
debug("pop", $pop, "agg", 475, $this->debug);
debug("popT", $popT, "agg", 475, $this->debug);
if (($pop + $popT) <= $maxP) {
$d = (int) $dif; // ore intere
$nat = 1; //@todo modificare a seconda dell'età
for ($i = 1; ($i <= $d) && ($pop < $maxP); $i++) {//aggiorno ora per ora
$pr = $pop * $nat / 2;
$pop = $pr + $pop;
}
$this->resource->aggPop+=$d * 3600; //aggiorno il contatore
debug("pop", $pop, "agg", 481, $this->debug);
if ($pop > ($maxP-$popT))
$pop = ($maxP-$popT);
}
else { //senza alloggi le persone muoiono
$d = (int) $dif; // ore intere
debug("d", $d, "agg", 395, $this->debug);
for ($i = 1; ($i <= $d) && ($pop > $busypop)&&(($pop + $popT) > $maxP); $i++) {//aggiorno ora per ora
$pop--;
}
$this->resource->aggPop+=$d * 3600;
}
$this->resource->pop = $pop;
}
$this->resource->agg = mktime();
$this->resource->write($this->resource);
}
/**
* villaggio selezionato
* @return int
*/
function getCurrentVillage() {
return $this->currentVillage;
}
/**
*
* @param int $set
*/
function setCurrentVillage($set) {
$this->currentVillage = $set;
}
/**
* ritorna l'età della civiltà
* @return int
*/
function getAge() {
return $this->data->civ_age;
}
/**
* aggiorna la produzione.
*/
function aggProd() {
if ($this->resource->production_1 != prod1::$prod[$this->building->getLiv(1)]) {
$this->resource->production_1 = prod1::$prod[$this->building->getLiv(1)];
}
if ($this->resource->production_2 != prod2::$prod[$this->building->getLiv(2)]) {
$this->resource->production_2 = prod2::$prod[$this->building->getLiv(2)];
}
if ($this->resource->production_3 != prod3::$prod[$this->building->getLiv(3)]) {
$this->resource->production_3 = prod3::$prod[$this->building->getLiv(3)];
}
}
/**
* crea un villaggio alle coordinate date
*
* @param int $x
* @param int $y
* @param int $cap
* @param int $civ_id
* @param int $type
* @param String $name
*/
function addVillage($x, $y, $cap = 0, $civ_id = null, $type = 0, $name = 'Nuovo Villaggio') {
global $fw;
if ($civ_id == null)
$civ_id = $fw->civ->cid;
$fw->db->sql_query("INSERT INTO `" . VILLAGE_TABLE . "` (`id`, `civ_id`, `name`, `capital`, `type`, `pop`, `busy_pop`, `resource_1`, `resource_2`, `resource_3`, `production_1`, `production_2`, `production_3`, `agg`, `x`, `y`) VALUES (NULL, '" . $civ_id . "', '" . $name . "', '" . $cap . "', '" . $type . "', '20', '0', '" . START_RES . "', '" . START_RES . "', '" . START_RES . "', '" . prod1::$prod[0] . "', '" . prod2::$prod[0] . "', '" . prod3::$prod[0] . "', '" . mktime() . "', '" . $x . "', '" . $y . "');");
$vid = $fw->db->insert_id();
$fw->db->sql_query("INSERT INTO `" . BUILDING_TABLE . "` (`village_id`,`type`,`liv`,`pos`,`pop`) value ('" . $vid . "','1','0','0','1')"); //main liv 1
$fw->db->sql_query("INSERT INTO `" . BUILDING_TABLE . "` (`village_id`,`type`,`liv`,`pos`) value ('" . $vid . "','4','0','1')");
$fw->db->sql_query("INSERT INTO `" . BUILDING_TABLE . "` (`village_id`,`type`,`liv`,`pos`) value ('" . $vid . "','5','0','2')");
$fw->db->sql_query("INSERT INTO `" . BUILDING_TABLE . "` (`village_id`,`type`,`liv`,`pos`) value ('" . $vid . "','6','0','3')");
}
/**
* crea coordinate casuali polari che poi saranno convertite in cartesiane
* come raggio viene preso il raggio massimo del settore +o- $intervallo
* $alfa è random
* $sectorx 0 settore casuale, 1 negativo 2 positivo
* $sectory 0 settore casuale, 1 negativo 2 positivo
* @param int $intervallo
* @param int $sectorx
* @param int $sectorY
* @author Pagliaccio
* @return array
* @todo implementare controllo coordinate massime! cosa fare se le coordinate sforano?
*/
function randomcoord($sectorx=0, $sectory=0, $intervallo = 10) {
global $fw;
// discriminazione settore
//discriminazione x
if ($sectorx)
$tipex = $sectorx - 1; else
$tipex=rand(1, 2) - 1;
//discriminazione y
if ($sectory)
$tipey = $sectory - 1; else
$tipey=rand(1, 2) - 1;
// massimo e minimo di alfa
$max[0][0] = 270; //--
$max[0][1] = 180; //-+
$max[1][0] = 360; //+-
$max[1][1] = 90; //++
$min[0][0] = 180; //--
$min[0][1] = 90; //-+
$min[1][0] = 270; //+-
$min[1][1] = 0; //++
//inizializzazione
$fine = true;
$c = 0;
//richiamo le variabili del server
$minrad = $fw->params->get("minrad", 1);
$dif = mktime() - $fw->params->get("time", mktime());
$dif = (int) $dif / 86400;
$maxrad = $minrad + $intervallo + $dif;
do { // inizio 1° ciclo per generare le variabili
$quadmin = $minrad * $minrad;
$quadmax = $maxrad * $maxrad;
// numero di tentativi per trovare delle coordinate valide
// oltre si aumenta il raggio
$quad = ($quadmax - $quadmin) / 10;
// controllo di fare almeno 1 tentativo ;)
if ($quad < 1)
$quad = 1;
for ($count = $fw->params->get("count", 0); ($fine) && ($count < $quad); $count++) {
//genero le coordinate
$alfa = rand($min[$tipex][$tipey], $max[$tipex][$tipey]);
$rad = rand($minrad, $maxrad);
//controllo raggio sia almeno 1
if ($rad < 1)
$rad = 1;
//trasformazione in cartesiane
$x = $rad * cos(deg2rad($alfa));
$y = $rad * sin(deg2rad($alfa));
//rendo intere le coordinate
$xint = (int) $x;
$yint = (int) $y;
if ($tipex == 1) { //se x è positivo arrotondo ad un numero + grande se x non è intero
if ($xint < $x)
$x = (int) $x + 1;
}
else { // idem
if ($xint > $x)
$x = (int) $x - 1;
}
if ($tipey == 1) {
if ($yint < $y)
$y = (int) $y + 1;
}
else {
if ($yint > $y)
$y = (int) $y - 1;
}//rendo intere le variabili x e y per sicurezza
$x = (int) $x;
$y = (int) $y;
if (!$fw->db->sql_count("SELECT `x`,`y` FROM `" . VILLAGE_TABLE . "` WHERE `x`='" . $x . "' AND `y`='" . $y . "'"))
$fine = false;
}
if ($count >= $quad)
$fw->params->set("count", 0);
else
$fw->params->set("count", $count);
$maxrad+=$intervallo;
$minrad+=$intervallo;
if ($fine) {
$fw->params->set("minrad", $minrad);
$fw->params->set("time", mktime());
}
$c++;
}
while (($fine) && ($c < 100));
if ($fine) {
echo "errore!!!";
exit;
}
return array('x' => $x, 'y' => $y);
}
function changeCurrentVillage($id) {
global $fw;
$fw->db->sql_query("UPDATE `" . RELATION_USER_CIV_TABLE . "` SET `current_village`='" . $id . "' WHERE `user_id`='" . $fw->user->data->ID . "' AND `SERVER`='" . SERVER . "'");
}
/**
* modifica il nome dei villaggi.
* @param <Array> $vect [id]=>nome
* @return <bool>
*/
function modNameVillage($vect) {
global $fw;
foreach ($vect as $key => $value) {
$bool = $fw->db->sql_query("UPDATE `" . VILLAGE_TABLE . "` SET `name`='" . $value . "' WHERE `id`='" . $key . "'");
}
return true;
}
/**
* ritorna un array ordinato con le informazioni delle code di costruzioni.
* @param int $id
* @return Array
*/
function getQueue() {
return $this->queue;
}
/**
* aggiunge al DB un invio di risorse. dest e mitt sono id villaggi,
* $res array risorse, n numero di mercanti occupati, time secondi necessari
* @param int $dest
* @param int $mitt
* @param Array $res
* @param int $n
* @param int $time
*/
function sendMercants($dest, $mitt, $res, $n, $time, $rap=1) {
global $fw;
$fw->db->sql_query("INSERT INTO `" . EVENTS_TABLE . "` SET `type`='" . MARKET_EVENT . "' , `time`='" . ($time + mktime()) . "' , `params`='" . serialize(array('mittente' => $mitt, 'destinatario' => $dest, 'res' => $res, 'n' => $n, 'time' => $time, 'rap' => $rap)) . "'");
}
/**
* ritorna un array con tutti gli invii di mercanti in arrivo e in uscita dal villaggio
* @return Array
*/
function getMercantsTravel() {
global $fw;
$row = null; // "mittente";i:1207;s:12:"destinatario";i:1;
$row['out'] = $fw->db->sql_totable("SELECT * FROM `" . EVENTS_TABLE . "` WHERE (`params`LIKE '%\"mittente\";i:" . $this->currentVillage . "%' OR `params`LIKE'%\"destinatario\";i:" . $this->currentVillage . "%') AND `type`='" . MARKET_EVENT . "'");
$row['in'] = $fw->db->sql_totable("SELECT * FROM `" . EVENTS_TABLE . "` WHERE `params`LIKE '%\"mittente\";i:" . $this->currentVillage . "%' AND `type`='" . RETURN_MERCANT_EVENT . "'");
return $row;
}
/**
* da il nome del villaggio
* @param int $id
* @return String
*/
function getVillageName($id) {
global $fw;
if ($id)
return $fw->db->sql_tovariable("SELECT `name` FROM `" . VILLAGE_TABLE . "` WHERE `id`='" . $id . "'");
else return __("valle inabitata");
}
/**
* mercanti occupati
* @return int
*/
function getMercantBusy() {
global $fw;
$col = $fw->db->sql_tocolarray("SELECT `params` FROM `" . EVENTS_TABLE . "` WHERE (`type`='" . MARKET_EVENT . "' OR `type`='" . RETURN_MERCANT_EVENT . "' ) AND `params`LIKE'%\"mittente\";i:" . $this->currentVillage . "%'");
for ($i = 0, $n = 0; $col[$i]; $i++) {
$num = unserialize($col[$i]);
$n+=$num['n'];
}
return $n;
}
/**
* ritorna il nome delle risorse
* @param int $res
* @param int $age
* @return String
*/
function getNameResource($res, $age) {
return $this->nameResource[$age][$res];
}
/**
* aggiorna le risorse nel villaggio
* @param int $id
* @return array risorse
*/
function aggResourceById($id) {
global $fw;
$res = $fw->db->sql_toarray("SELECT * FROM `" . VILLAGE_TABLE . "` WHERE `id`='" . $id . "'");
$now = mktime();
$agg = $res['agg'];
// ore di differenza dall'ultimo aggiornamento
$dif = ($now - $agg) / 3600;
//aggiornamento risorse
$res['resource_1']+=$dif * $res['production_1'];
$res['resource_2']+=$dif * $res['production_2'];
$res['resource_3']+=$dif * $res['production_3'];
// controllo saturamento magazzino
$sto1 = $this->getStorageById($id);
$sto2 = $this->getStorageById($id, STORAGE2);
debug("sto", $sto1, "civ", 576, $this->debug);
debug("sto", $sto2, "civ", 577, $this->debug);
if ($res['resource_1'] > $sto1)
$res['resource_1'] = $sto1;
if ($res['resource_2'] > $sto2)
$res['resource_2'] = $sto2;
if ($res['resource_3'] > $sto2)
$res['resource_3'] = $sto2;
$fw->db->sql_query("UPDATE `" . VILLAGE_TABLE . "` SET `resource_1`='" . $par['resource_1'] . "' , `resource_2`='" . $res['resource_2'] . "' , `resource_3`='" . $par['resource_3'] . "' , `agg`='" . mktime() . "' WHERE `id`='" . $p['village_B'] . "'");
debug("resource", array($res['resource_1'], $res['resource_2'], $res['resource_3']), "civ", 581, $this->debug);
return array($res['resource_1'], $res['resource_2'], $res['resource_3']);
}
/**
* ricava la capacità di un villaggio
* @param int $id
* @param int $storagetype
* @return int
*/
function getStorageById($id, $storagetype = STORAGE1) {
global $fw, $Building_Array;
$build = $fw->db->sql_totable("SELECT * FROM `" . BUILDING_TABLE . "` WHERE `village_id`='" . $id . "' AND `type`='" . $storagetype . "'");
$storage = 0;
for ($i = 0; $build[$i]; $i++) {
$storage += $Building_Array[$storagetype - 1]::$capacity[$build[$i]['liv']];
}
if ($storage == 0) {
if ($storagetype == STORAGE1) {
$storage = storage1::$capacity[0];
}
elseif ($storagetype == STORAGE2) {
$storage = storage2::$capacity[0];
}
else {
$storage = house::$capacity[0];
}
}
return $storage;
}
function killtroop($troop,$prod) {
$max = 0; //cerco le truppe che consumano di più
for ($i = 0; $troop[$i]; $i++) {
$cost = $Troops_Array[$troop[$i]['trooper_id']]::$cost[0]
+ $Troops_Array[$troop[$i]['trooper_id']]::$cost[1]
+ $Troops_Array[$troop[$i]['trooper_id']]::$cost[2];
$costTot = $troop[$i]['numbers'] * $cost;
$cons = $Troops_Array[$troop[$i]['trooper_id']]::$pop * $troop[$i]['numbers'];
if ($cons > $max) {
$max = $cons; //ne memorizzo il consumo,
$costm = $cost; // il costo x truppa
$costTotm = $costTot; //e costo totale
$j = $i; // indice
}
}
if ($max < (-$prod)) { //il consumo non è sufficente per portare la prod in positivo
$this->resource->resource_1+=$costTot;
$prod+=$max;
$troop[$j]['numbers'] = 0;
$fw->db->sql_query("DELETE FROM `" . TROOPERS . "` WHERE `trooper_id`='" . $troop[$j]['trooper_id'] . "' AND `village_now`='" . $troop[$j]['village_now'] . "' AND `village_prev`='" . $troop[$j]['village_prev'] . "'");
}
else {//diminuisco il numero
$n = -($prod / $Troops_Array[$troop[$j]['trooper_id']]::$pop );
if ($n > intval($n))
$n = intval($n) + 1;
$troop[$j]['numbers']-=$n;
$prod+=$max;
$this->resource->resource_1 = 0;
$fw->db->sql_query("UPDATE `" . TROOPERS . "` SET `numbers`='" . $troop[$j]['numbers'] . "' WHERE `trooper_id`='" . $troop[$j]['trooper_id'] . "' AND `village_now`='" . $troop[$j]['village_now'] . "' AND `village_prev`='" . $troop[$j]['village_prev'] . "'");
}
return $prod;
}
}
?>