/php/base/village.inc.php
PHP | 853 lines | 721 code | 69 blank | 63 comment | 42 complexity | 93a4b3661570353997229503801a3914 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
- <?php
- /* village.inc.php - Class representing a village
- *
- * Copyright (C) 2006-2010 Kevin Read, Simone Schaefer
- * Copyright (C) 2010 Markus Bender
- *
- * This file is part of Selador, a browser-based fantasy strategy game
- *
- * This program is distributed under the terms of the GNU Affero General Public License.
- *
- *
- * Selador is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * any later version.
- *
- * Selador is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with Selador. If not, see <http://www.gnu.org/licenses/>.
- **/
- class Village
- {
- public $name;
- public $villageid;
- public $uid;
- public $x;
- public $y;
- public $ress_prod = array();
- public $ress_max;
- public $ress = array();
- public $ress_spent = array (0, 0, 0, 0, 0, 0);
- public $goldcost;
- public $population;
- public $secs_yielded = 0;
- public $secs_passed;
- public $secs_recruited;
- public $score_to_add=0;
- public $outposts = array();
- public $outpost_of = 0;
- public $speed_build;
- public $speed_research;
- public $speed_recruit = array ();
- public $recruit_rest;
- public $recruiting = array ();
- public $owner; // the user object of the village owner - used for logging (shudder)
- // These show that the village is under siege:
- public $besieged_by;
- public $besieged_from;
- public $exhaustion_in = 0;
- // These show that the village will be under siege soon:
- /*
- public $soon_siege_in; // The timestamp, when siege will start
- public $soon_siege_by; // The UID of the besieger
- public $soon_siege_from; // The village id from where the siege comes
- */
- public $merchants; // Number of merchants that live here
- public $expansion_slots; // Number of times we can add ress fields before upgrading bergfried
- public $outpost_expansion; // How many new outposts can be founded or overtaken
- public $updated; // If the stamp updater has to save this information at the end of computation
- public $dirty = true; // If update_to_latest has to re-read village info
- public function __construct ($villageInfo, $user)
- {
- $this->set_info ($villageInfo, $user);
- }
- function set_info ($villageInfo, $user)
- {
- $this->villageid = $villageInfo['villageid'];
- $this->owner = $user;
- $this->uid = $villageInfo['uid'];
- $this->name = stripslashes ($villageInfo['name']);
- $this->x = $villageInfo['x'];
- $this->y = $villageInfo['y'];
- $this->outpost_of = $villageInfo['outpost_of'];
- $this->ress_max = $villageInfo['resmax'];
- $this->ress_prod[1] = $villageInfo['resprod1'];
- $this->ress_prod[2] = $villageInfo['resprod2'];
- $this->ress_prod[3] = $villageInfo['resprod3'];
- $this->ress_prod[4] = $villageInfo['resprod4'];
- $this->ress_prod[5] = $villageInfo['resprod5'];
- if (isset ($villageInfo['goldcost']))
- $this->goldcost = $villageInfo['goldcost'];
- $this->speed_build = $villageInfo['buildspeed'];
- $this->speed_research = $villageInfo['resspeed'];
- $this->speed_recruit[1] = $villageInfo['recrspeed1']; // foot
- $this->speed_recruit[2] = $villageInfo['recrspeed2']; // horse
- $this->speed_recruit[3] = $villageInfo['recrspeed3']; // bow
- $this->speed_recruit[4] = $villageInfo['recrspeed4']; // siege
- $this->speed_recruit[5] = $villageInfo['recrspeed5']; // castle
- $this->ress[1] = $villageInfo['ress1'];
- $this->ress[2] = $villageInfo['ress2'];
- $this->ress[3] = $villageInfo['ress3'];
- $this->ress[4] = $villageInfo['ress4'];
- // $this->population = $villageInfo['population'];
- $this->expansion_slots = $villageInfo['expansion'];
- $this->outpost_expansion = $villageInfo['out_expansion'];
- $this->recruit_rest = $villageInfo['recruit_rest'];
- $this->secs_yielded = $this->secs_recruited = $this->secs_passed = $villageInfo['secs_passed'];
- $this->merchants = $villageInfo['merchants'];
- $this->score = $villageInfo['score'];
- if (FINITE_RESSOURCES)
- $this->exhaustion_in = -$villageInfo['exhaustion_in'];
- /*
- if (!$villageInfo['besieged_in'])
- {
- $this->besieged_by = $villageInfo['besieged_by'];
- $this->besieged_from = $villageInfo['besieged_from'];
- }
- else
- {
- $this->soon_siege_in = $villageInfo['besieged_in'];
- $this->soon_siege_by = $villageInfo['besieged_by'];
- $this->soon_siege_from = $villageInfo['besieged_from'];
- } */
- $this->updated = false;
- $this->dirty = false;
- $this->ress_spent = array (0, 0, 0, 0, 0, 0);
- }
- public function update_to_latest ($debug=false, $time=false)
- {
- global $user_row;
- if (!$time)
- $time = $user_row['time'];
- // Fetch village ressources
- if (FINITE_RESSOURCES)
- $query = "select *, abs(time_to_sec(timediff(stamp,\"" . $time . "\"))) as secs_passed, time_to_sec(timediff(next_exhaust,now())) as exhaustion_in from village where villageid=".$this->villageid;
- else
- $query = "select *, abs(time_to_sec(timediff(stamp,\"" . $time . "\"))) as secs_passed from village where villageid=".$this->villageid;
- $res = mysql_query ($query);
- $stamprow = mysql_fetch_array($res);
- $this->secs_yielded = $this->secs_recruited = $this->secs_passed = $stamprow['secs_passed'];
- $this->recruit_rest = $stamprow['recruit_rest'];
- $this->population = $stamprow['population'];
- $this->ress[1] = $stamprow['ress1'];
- $this->ress[2] = $stamprow['ress2'];
- $this->ress[3] = $stamprow['ress3'];
- $this->ress[4] = $stamprow['ress4'];
- $this->ress_spent = array (0, 0, 0, 0, 0, 0);
- if (isset ($stamprow['goldcost']))
- $this->goldcost = $stamprow['goldcost'];
- $this->expansion_slots = $stamprow['expansion'];
- $this->outpost_expansion = $stamprow['out_expansion'];
- $this->besieged_by = $stamprow['besieged_by'];
- $this->besieged_from = $stamprow['besieged_from'];
- if (FINITE_RESSOURCES)
- $this->exhaustion_in = -$stamprow['exhaustion_in'];
- $this->updated = false;
- $this->dirty = false;
- mysql_free_result ($res);
- if ($debug)
- $this->owner->log_debug ("Own village ".$this->villageid." (".$this->name.") updated to latest. Slept ".$this->secs_passed." seconds, old ress ".$this->ress[1].", ".$this->ress[2].", ".$this->ress[3].", ".$this->ress[4]." - exhaustion in ".$this->exhaustion_in);
- }
- function update_production ()
- {
- // Fetch village ressources
- $query = "select villageid,resprod1,resprod2,resprod3,resprod4,resprod5,besieged_by,besieged_from, time_to_sec(timediff(next_exhaust,now())) as exhaustion_in from village where villageid in (".$this->villageid;
- if (count ($this->outposts))
- {
- foreach ($this->outposts as $id=>$this_outpost)
- $query .= ",".$id;
- }
- $query .= ")";
- $this->ress_prod[1] = $this->ress_prod[2] = $this->ress_prod[3] = $this->ress_prod[4] = $this->ress_prod[5] = 0;
- $res = mysql_query ($query);
- while ($villageInfo = mysql_fetch_array($res))
- {
- $factor = 1;
- if ($villageInfo['besieged_by'] == 0)
- {
- $this->ress_prod[1] += (int)($villageInfo['resprod1'] * $factor);
- $this->ress_prod[2] += (int)($villageInfo['resprod2'] * $factor);
- $this->ress_prod[3] += (int)($villageInfo['resprod3'] * $factor);
- $this->ress_prod[4] += (int)($villageInfo['resprod4'] * $factor);
- $this->ress_prod[5] += (int)($villageInfo['resprod5'] * $factor);
- $this->exhaustion_in = -$villageInfo['exhaustion_in'];
- }
- }
- mysql_free_result ($res);
- }
- function give_z ()
- {
- return (($this->y * WORLD_SIZEX) + $this->x);
- }
- /* Calculate the amount of ressource income and update local copy of ressource vars */
- function do_income ($debug=false)
- {
- $this->do_income_partial (0, $debug);
- }
- /* Calculate the amount of ressource income and update local copy of ressource vars */
- function do_income_partial ($newtime, $debug=false)
- {
- $newtime = abs ($newtime);
- if (FINITE_RESSOURCES)
- {
- if ($newtime < $this->exhaustion_in)
- {
- $this->do_income_partial ($this->exhaustion_in);
- $this->secs_yielded = abs ($this->exhaustion_in);
- $this->owner->CalculateVillageProd ($this->villageid);
- // $owner->CalculateVillageProd ($this->villageid);
- }
- }
- $time_passed = $this->secs_yielded - $newtime;
- if ($time_passed > 0)
- {
- $gone = (float)($time_passed)/(float)3600;
- // Calculate ressource income. Note that secspassed might have been changed by event handler
- $this->ress_spent[1] -= ($this->ress_prod[1] * $gone);
- $this->ress_spent[2] -= ($this->ress_prod[2] * $gone);
- $this->ress_spent[3] -= ($this->ress_prod[3] * $gone);
- $this->ress_spent[4] -= ($this->ress_prod[4] * $gone);
- // Cap the ressources at the limit
- foreach ($this->ress as $currency=>$amount)
- {
- if ($amount - $this->ress_spent[$currency] > $this->ress_max)
- {
- $this->ress_spent[$currency] += $amount - $this->ress_spent[$currency] - $this->ress_max;
- $this->owner->log_debug ("C".$currency.": RS ".$this->ress_spent[$currency]." ".$amount." - ".$this->ress_spent[$currency]." - ".$this->ress_max);
- }
- }
- if ($debug)
- $this->owner->log_debug ("Own village ".$this->villageid." (".$this->name.") partial income after ".($this->secs_yielded-$newtime)." seconds, yield seconds left ".abs($newtime)." to ".$this->ress[1].", ".$this->ress[2].", ".$this->ress[3].", ".$this->ress[4]);
- $this->secs_yielded = abs ($newtime);
- $this->updated = true;
- }
- }
- public function spend ($ress1, $ress2, $ress3, $ress4=0)
- {
- $this->ress_spent[1] += $ress1;
- $this->ress_spent[2] += $ress2;
- $this->ress_spent[3] += $ress3;
- $this->ress_spent[4] += $ress4;
- $this->updated = true;
- }
- public function spend_array ($ress)
- {
- $this->ress_spent[1] += $ress[1];
- $this->ress_spent[2] += $ress[2];
- $this->ress_spent[3] += $ress[3];
- $this->ress_spent[4] += $ress[4];
- $this->updated = true;
- }
- public function spend_type ($type, $amount)
- {
- $this->ress_spent[$type] += $amount;
- $this->updated = true;
- }
- public function can_spend ($ress1, $ress2, $ress3, $ress4=0)
- {
- if (($this->ress[1] - $this->ress_spent[1] >= $ress1) && ($this->ress[2] - $this->ress_spent[2] >= $ress2) && ($this->ress[3] - $this->ress_spent[3] >= $ress3) && ($this->ress[4] - $this->ress_spent[4] >= $ress4))
- return (true);
- else
- return (false);
- }
- public function can_spend_array ($ress)
- {
- if (($this->ress[1] - $this->ress_spent[1] >= $ress[1]) && ($this->ress[2] - $this->ress_spent[2] >= $ress[2]) && ($this->ress[3] - $this->ress_spent[3] >= $ress[3]) && ($this->ress[4] - $this->ress_spent[4] >= $ress[4]))
- return (true);
- else
- return (false);
- }
- public function can_spend_single ($ress1, $ress2, $ress3, $ress4=0)
- {
- $result = array (1=>false, 2=>false, 3=>false, 4=>false);
- if ($this->ress[1] - $this->ress_spent[1] >= $ress1)
- $result[1] = true;
- if ($this->ress[2] - $this->ress_spent[2] >= $ress2)
- $result[2] = true;
- if ($this->ress[3] - $this->ress_spent[3] >= $ress3)
- $result[3] = true;
- if ($this->ress[4] - $this->ress_spent[4] >= $ress4)
- $result[4] = true;
- return ($result);
- }
- public function ress_val ($type)
- {
- return ($this->ress[$type] - $this->ress_spent[$type]);
- }
- public function can_spend_type ($type, $amount)
- {
- if ($this->ress[$type] - $this->ress_spent[$type] >= $amount)
- return true;
- else
- return false;
- }
- /**************************************************************************
- Update the timestamp, but note that this was a user action
- *************************************************************************/
- // Need to think about how to save timestamps. But maybe we don't need to think about that,
- // as long as multi-row village updates don't touch indexed columns.
- public function update_stamp ($debug=false, $time=false)
- {
- global $db_locked;
- if ($this->updated)
- {
- $this->update_stamp_nounlock ($debug, "lock", $time);
- }
- mysql_query ("commit");
- $db_locked = false;
- }
- public function update_stamp_nounlock ($debug=false, $logname="nounlock", $time=false)
- {
- global $user_row;
- global $gamedb;
- if (!$time) {
- $time = $user_row['time'];
- }
- if ($this->updated)
- {
- // Cap the ressources at the limit
- foreach ($this->ress as $currency=>$amount)
- {
- if ($amount - $this->ress_spent[$currency] > $this->ress_max)
- {
- $this->ress_spent[$currency] += $amount - $this->ress_spent[$currency] - $this->ress_max;
- $this->owner->log_debug ("C".$currency.": RS ".$this->ress_spent[$currency]." ".$amount." - ".$this->ress_spent[$currency]." - ".$this->ress_max);
- }
- }
- if ($this->score_to_add > 0)
- {
- $query = "update village set ress1=ress1-".$this->ress_spent[1].", ress2=ress2-".$this->ress_spent[2].", ress3=ress3-".$this->ress_spent[3].", ress4=ress4-".$this->ress_spent[4].", stamp=\"" . $time . "\", score=score+".$this->score_to_add." where villageid=".$this->villageid;
- $this->score_to_add = 0;
- }
- else
- $query = "update village set ress1=ress1-".$this->ress_spent[1].", ress2=ress2-".$this->ress_spent[2].", ress3=ress3-".$this->ress_spent[3].", ress4=ress4-".$this->ress_spent[4].", stamp=\"" . $time . "\" where villageid=".$this->villageid;
- if (!$gamedb->query ($query))
- $this->owner->log_err ("Couldn't update stamp in ".$logname."!! Query: ".$query.": ".mysql_error());
- // if ($debug)
- $this->owner->log_debug ("Own village ".$this->villageid." (".$this->name.") ".$logname." update query ".$query);
- $this->updated = false;
- $this->dirty = true;
- }
- }
- function building_enoughcash ($type, $tolevel, $costfac=1)
- {
- if (($spend = building_costs ($type, $tolevel)))
- {
- if ($this->can_spend ($spend[1], $spend[2], $spend[3], $spend[4]))
- return (true);
- }
- return false;
- }
- function building_build ($type, $tolevel, $costfac=1)
- {
- global $me; // hmm not very bright var name eh :)
- $no_go = false;
- if ($spend = building_costs ($type, $tolevel, $costfac))
- {
- if ($this->building_enoughcash ($type, $tolevel, $costfac))
- {
- foreach ($this->ress as $ressidx => $thisress)
- {
- $this->ress[$ressidx] -= $spend[$ressidx];
- // $me->score += $spend[$ressidx];
- }
- return true;
- }
- else
- return false;
- }
- }
- function now_ress ($type)
- {
- return (min ((int)$this->ress_max, (int)($this->ress[$type] - $this->ress_spent[$type])));
- }
- /***********************************************************************
- Display monetary amounts in a nice way
- **********************************************************************/
- function display_money ($costs, $dont_display_timer = false)
- {
- global $tmp_notenough;
- $width = 100;
- $time_to_wait = 0;
- $prod_is_zero = false;
- if (isset ($costs[1]))
- {
- if ($this->now_ress(1) < $costs[1])
- {
- $retstr = "<span class=\"notenough\"><img src=\"styles/".$_SESSION['theme']."/holz.gif\" width=\"14\"> ".prettynumber ($costs[1])."</span> ";
- $tmp_notenough = true;
- if ($this->ress_prod[1] < 1)
- {
- $prod_is_zero = true;
- }
- else
- {
- //calculate how long it will last until we have enough ressources
- $time_to_wait = ($costs[1] - ($this->now_ress(1))) / ($this->ress_prod[1] );
- //$retstr .= $time_to_wait;
- }
- }
- else
- $retstr = "<img src=\"styles/".$_SESSION['theme']."/holz.gif\" width=\"14\"> ".prettynumber ($costs[1])." ";
- }
- if (isset ($costs[2]))
- {
- if ($this->now_ress(2) < $costs[2])
- {
- $retstr .= "<img src=\"styles/".$_SESSION['theme']."/stein.gif\" width=\"14\"><span class=\"notenough\"> ".prettynumber ($costs[2])."</span> ";
- $tmp_notenough = true;
- if ($this->ress_prod[2] < 1)
- {
- $prod_is_zero = true;
- }
- else
- {
- if (($costs[2] - $this->now_ress(2)) / $this->ress_prod[2] > $time_to_wait)
- $time_to_wait = ($costs[2] - ($this->now_ress(2))) / ($this->ress_prod[2] );
- }
- }
- else
- $retstr .= "<img src=\"styles/".$_SESSION['theme']."/stein.gif\" width=\"14\"> ".prettynumber ($costs[2])." ";
- }
- if (isset ($costs[3]))
- {
- if ($this->now_ress(3) < $costs[3])
- {
- if ($this->ress_prod[3] < 1)
- {
- $prod_is_zero = true;
- }
- else
- {
- if (($costs[3] - $this->now_ress(3)) / $this->ress_prod[3] > $time_to_wait)
- $time_to_wait = ($costs[3] - ($this->now_ress(3))) / ($this->ress_prod[3] );
- }
- $retstr .= "<img src=\"styles/".$_SESSION['theme']."/eisen.gif\" width=\"14\"> <span class=\"notenough\">".prettynumber ($costs[3])."</span> ";
- $tmp_notenough = true;
- }
- else
- $retstr .= "<img src=\"styles/".$_SESSION['theme']."/eisen.gif\" width=\"14\"> ".prettynumber ($costs[3])." ";
- }
- if (isset ($costs[4]))
- {
- if ($this->now_ress(4) < $costs[4])
- {
- if ($this->ress_prod[4] < 1)
- {
- $prod_is_zero = true;
- }
- else
- {
- if (($costs[4] - ($this->now_ress(4))) / $this->ress_prod[4] > $time_to_wait)
- $time_to_wait = ($costs[4] - ($this->now_ress(4))) / ($this->ress_prod[4] );
- }
- $retstr .= "<img src=\"styles/".$_SESSION['theme']."/gold.gif\" width=\"14\"> <span class=\"notenough\">".prettynumber ($costs[4])."</span> ";
- $tmp_notenough = true;
- }
- else
- $retstr .= "<img src=\"styles/".$_SESSION['theme']."/gold.gif\" width=\"14\"> ".prettynumber ($costs[4])." ";
- }
- if ($tmp_notenough && !$dont_display_timer)
- {
- $hours = floor ($time_to_wait);
- $rest = $time_to_wait - $hours;
- $minutes = floor ($rest * 60);
- $rest2 = ($rest*60) - $minutes;
- $seconds = floor ($rest2 * 60);
- $minutes += floor ($seconds/60);
- $seconds = ($seconds % 60);
- if ($hours == 1)
- $hour_string = "Stunde";
- else
- $hour_string = "Stunden";
- if ($hours < 10)
- $hours = "0".$hours;
- if ($minutes < 10)
- $minutes = "0".$minutes;
- if ($prod_is_zero)
- $retstr .= "<span style=\"color:grey\"> (Genug Rohstoffe: <i>nie</i>)</span>";
- else
- $retstr .= "<span style=\"color:grey\"> (Genug Rohstoffe in ".$hours.":".$minutes." Stunden)</span>";
- }
- return $retstr;
- }
- /***********************************************************************
- Display monetary amounts in a nice way, each ressource in its own
- table cell
- **********************************************************************/
- function display_money_table ($costs)
- {
- global $tmp_notenough;
- $width = 100;
- if ($this->ress[1] - $this->ress_spent[1] < $costs[1])
- {
- $retstr = "<td><span class=\"notenough\"><img src=\"styles/".$_SESSION['theme']."/holz.gif\" width=\"18\">".prettynumber ($costs[1])."</span></td>";
- $tmp_notenough = true;
- }
- else
- $retstr = "<td class=\"none\"><img src=\"styles/".$_SESSION['theme']."/holz.gif\" width=\"18\">".prettynumber ($costs[1])."</td>";
- if ($this->ress[2] - $this->ress_spent[2] < $costs[2])
- {
- $retstr .= "<td class=\"none\"><img src=\"styles/".$_SESSION['theme']."/stein.gif\" width=\"18\"><span class=\"notenough\">".prettynumber ($costs[2])."</span></td>";
- $tmp_notenough = true;
- }
- else
- $retstr .= "<td class=\"none\"><img src=\"styles/".$_SESSION['theme']."/stein.gif\" width=\"18\">".prettynumber ($costs[2])."</td>";
- if ($this->ress[3] - $this->ress_spent[3] < $costs[3])
- {
- $retstr .= "<td class=\"none\"><img src=\"styles/".$_SESSION['theme']."/eisen.gif\" width=\"18\"><span class=\"notenough\">".prettynumber ($costs[3])."</span></td>";
- $tmp_notenough = true;
- }
- else
- $retstr .= "<td class=\"none\"><img src=\"styles/".$_SESSION['theme']."/eisen.gif\" width=\"18\">".prettynumber ($costs[3])."</td>";
- if ($this->ress[4] - $this->ress_spent[4] < $costs[4])
- {
- $retstr .= "<td><img src=\"styles/".$_SESSION['theme']."/gold.gif\" width=\"18\"><span class=\"notenough\">".prettynumber ($costs[4])."</span></td>";
- $tmp_notenough = true;
- }
- else
- $retstr .= "<td ><img src=\"styles/".$_SESSION['theme']."/gold.gif\" width=\"18\">".prettynumber ($costs[4])."</td>";
- return $retstr;
- }
- // Retrieve all armies und troops in a village
- function fetch_troops ()
- {
- $troops = array ();
- $query = "select A.troopid, A.uid, A.flag, A.location, B.unitid, B.amount from armies A join troops B on (A.troopid=B.troopid) where location=".$this->villageid." and flag=0";
- if (!($res = mysql_query ($query)))
- {
- $this->owner->log_err ("Cannot read armies and troops for this village. Query: ".$query.", ".mysql_error());
- }
- else
- {
- while ($row = mysql_fetch_array ($res))
- {
- $troops[$row['uid']][$row['unitid']] = $row['amount'];
- }
- }
- return ($troops);
- }
- // TODO: Zero gui unit arrays, if timelimit != 0
- public function recruit_handler (&$gui, &$deleids, $time_remains=0)
- {
- global $me; // ouch
- // global $troops_done;
- $score_to_add = 0;
- $troops_done = false;
- // Iterate over all remaining recruit jobs, because we'll stop after we have hit the time limit
- foreach ($this->recruiting as $recrindex=>$evrow)
- {
- // Stop here if we have already "recruited away" all available time
- if (!$troops_done)
- {
- // timelimit specifies if we should only recruit to a certain point in time
- if ($time_remains > 0)
- $real_time = -$evrow['dif'] - $time_remains; // FIXME: Can this be true?
- else
- $real_time = -$evrow['dif'];
- // So maybe there is no time left for this recruit job
- if ($real_time > 0)
- {
- // Yes, there is. The upped bound is still the amount of time available for this recruit job
- $timediff = min ($evrow['village2'], $real_time);
- $this->recruit_rest = (-$evrow['dif'] - $this->secs_recruited) - ((-$evrow['dif'] - $this->secs_recruited) % $evrow['param2']) ;
- $this->owner->log_debug ("D".(-$evrow['dif'])." - SR ".$this->secs_recruited."=".(-$evrow['dif'] - $this->secs_recruited)." - MOD (".((-$evrow['dif'] - $this->secs_recruited) % $evrow['param2'])." = ".$this->recruit_rest);
- // Subtract the recruit rest from the time that we have now for recruiting
- $newtrooptime = $timediff - $this->recruit_rest;
- // This directly translates into the amount of units we can recruit
- $newtroops = max (0, floor ($newtrooptime / $evrow['param2']));
- $this->owner->log_debug ("Recruiting done: ".$newtrooptime." Limit ".$time_remains." (".$timediff." - ".$this->recruit_rest.") final ".$evrow['village2']);
- $this->owner->log_debug ("Newtroops: ".$newtroops." (".$newtrooptime." / ".$evrow['param2']." )");
- $this->secs_recruited -= $newtrooptime;
- if ($real_time > $evrow['village2'])
- {
- $deleids[] = $evrow['eid'];
- $this->owner->log_debug ("Killing event ".$evrow['eid']);
- unset ($this->recruiting[$recrindex]);
- $this->recruit_rest = 0;
- }
- else
- {
- $troops_done = true;
- $this->recruit_rest = $timediff - ($timediff % $evrow['param2']);
- $timeremaining = $evrow['village2'] + $evrow['dif'];
- // Only update unit time if units later in the queue didn't already set a higher wait time
- if ($gui)
- {
- if (isset ($gui->unit_time))
- {
- if ($gui->unit_time < $timeremaining)
- $gui->unit_time = $timeremaining;
- }
- $gui->unit_first = ceil ($timeremaining / $evrow['param2'])." <img src=\"gfx/u/".$evrow['param'].".gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"".$evrow['param']."\"> in <span title=\"Fertig am ".date ("d.m. H:i:s", time () + $timeremaining)."\" id=\"timer";
- $gui->unit_queue_suffix = external_calcdur ($timeremaining)."</span>";
- $gui->unit_number += ceil ($timeremaining / $evrow['param2']);
- }
- }
- if ($newtroops > 0)
- {
- $query = "update troops A, armies B set amount=amount+".$newtroops." where A.troopid=B.troopid and B.villageid=".$evrow['village1']." and B.location=".$evrow['village1']." and B.flag=0 and A.unitid=".$evrow['param'];
- $this->owner->log_debug ("Newtroopquery: ".$query);
- if (!mysql_query ($query))
- {
- $this->owner->log_err ("Error while finishing recruitment: ".$query.", ".mysql_error ());
- }
- $num_aff = mysql_affected_rows ();
- if ($num_aff != 1)
- {
- $this->owner->log_err ("Strange effect while updating troops. Affected rows ".$num_aff.", query: ".$query);
- if ($num_aff == 0)
- {
- $query = "select troopid from armies where villageid=".$evrow['village1']." and location=".$evrow['village1']." and flag=0";
- if (!($res = mysql_query ($query)))
- $this->owner->log_err ("Cannot fetch army to recruit into when trying recruiting fault recovery. Query: ".$query.", ".mysql_error ());
- else
- {
- if (($row = mysql_fetch_array ($res)))
- $troopid = $row['troopid'];
- else
- {
- $query = "insert into armies values (NULL, ".$evrow['village1'].", ".$evrow['uid1'].", 0, ".$evrow['village1'].")";
- if (!mysql_query ($query))
- $this->owner->log_err ("Cannot insert army to recruit into when trying recruiting fault recovery. Query: ".$query.", ".mysql_error ());
- else
- {
- $troopid = mysql_insert_id ();
- $this->owner->log_err ("No army here, dude. We create one: ".$query.", new id is ".$troopid);
- }
- }
- if (isset ($troopid))
- {
- $query = "insert into troops values (".$troopid.", ".$evrow['param'].", ".$newtroops.")";
- $this->owner->log_err ("Also adding this troop entry: ".$query);
- if (!mysql_query ($query))
- $this->owner->log_err ("Cannot insert troops to recruit into when trying recruiting fault recovery. Query: ".$query.", ".mysql_error ());
- }
- }
- }
- }
- $this->updated = true;
- $query = "insert into eventlog values (".$evrow['eid'].", ".$evrow['uid1'].", ".$evrow['village1'].", ".$evrow['uid2'].", ".$newtroops.", now(), '".$evrow['timestamp']."', ".$evrow['type'].", ".$evrow['param'].", ".$evrow['param2'].", ".$evrow['score'].", 1)";
- if (!mysql_query ($query))
- $this->owner->log_err ("Cannot update eventlog. Query: ".$query.", ".mysql_error());
- $score_to_add += ($evrow['score']*$newtroops);
- }
- }
- }
- }
- return ($score_to_add);
- }
- function sum_population ($Gui)
- {
- $query = "select sum(B.amount) from armies A, troops B where A.villageid=".$this->villageid." and A.troopid=B.troopid and B.unitid<1000";
- if (!($res = mysql_query ($query)))
- $this->owner->log_err ("Cannot sum population in villages.inc.php. Query: ".$query.", ".mysql_error ());
- else
- {
- if (($row = mysql_fetch_row ($res)))
- return ($row[0] + $Gui->unit_number);
- else
- $this->owner->log_err ("No rows returned in population sum. Query: ".$query.", ".mysql_error ());
- }
- return ($Gui->unit_number);
- }
- function update_recrspeed ($which, $evrow)
- {
- $factor = ConstructionTime ($evrow['param']) * 6;
- $this->speed_recruit[$which] = $factor;
- $query = "update village set recrspeed".$which."=\"".$factor."\" where villageid=".$evrow['village1'];
- // echo $query;
- if (!mysql_query ($query))
- $this->log_err ("Event recrspeed".$which." update failed: ".$query.": ".mysql_error());
- }
- function recalculate_upgrade ($outpostid, $num_vill)
- {
- global $world, $debug;
- if (isset ($this->outposts[$outpostid]))
- {
- // First check how many villages this player already has
- $query = "select level from tileres where villageid=".$outpostid;
- if (!($res = mysql_query ($query)))
- $this->owner->log_err ("WTF, cannot read out tile levels for upgrade outpost->city: ".$query.", ".mysql_error ());
- else
- {
- //$this->owner->log_err ("tileres query ".$query);
- // We iterate through all ressource fields and check if they are equal to
- // or greater than the necessary level to upgrade this outpost to a village
- $enough = true;
- $fieldcount = 0;
- while (($row = mysql_fetch_row ($res)) && $enough)
- {
- // echo '<br><br>LEvel: '.$row[0].', num villages: '.$num_vill;
- if ($row[0])
- {
- if ($row[0] < $num_vill * OP_LEVEL_FAC)
- {
- $enough = false;
- break;
- }
- $fieldcount++;
- }
- }
- $this->owner->log_debug ("O->C: Fieldcount ".$fieldcount.", enough is ".$enough.", num_vill is ".$num_vill);
- // If this outpost can be upgraded, we note this change and also flush the outposts info cache
- if ($enough && ($fieldcount >= 4))
- {
- $this->owner->log_debug ("Outpost ".$outpostid." can be upgraded to village!!");
- $query = "update village set expansion=1 where villageid=".$outpostid;
- if (!mysql_query ($query))
- $this->owner->log_err ("Cannot set outpost to upgradeable. Query: ".$query.", ".mysql_error ());
- // To flush this info cache, we need the coordinates
- $query = "select x, y from village where villageid=".$outpostid;
- if (!($res = mysql_query ($query)))
- {
- $this->owner->log_err ("Cannot fetch coordinates for info flushing when outpost can be upgraded. Query: ".$query.", ".mysql_error ());
- }
- else
- {
- if (($row = mysql_fetch_array ($res)))
- {
- $delz = $row['y'] * $world->sizex + $row['x'];
- $path = TILEINFO_PATH.$delz.".php";
- @safe_delete ($path);
- // $this->log_err ("Cannot delete outpost info cache file after upgrade is possible: ".$path);
- }
- }
- }
- }
- }
- }
- }
- ?>