3 #################################################################################
4 ## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
5 ## --------------------------------------------------------------------------- ##
6 ## Filename Buildng.php ##
7 ## Developed by: Dzoki & Dixie ##
8 ## License: TravianX Project ##
9 ## Copyright: TravianX (c) 2010-2011. All rights reserved. ##
11 #################################################################################
16 public $NewBuilding = false;
17 private $maxConcurrent;
19 private $basic,$inner,$plus = 0;
20 public $buildArray = array();
22 public function Building() {
24 $this->maxConcurrent
= BASIC_MAX
;
25 if(ALLOW_ALL_TRIBE ||
$session->tribe
== 1) {
26 $this->maxConcurrent +
= INNER_MAX
;
29 $this->maxConcurrent +
= PLUS_MAX
;
31 $this->LoadBuilding();
35 public function procBuild($get) { global $session;
36 if(isset($get['a']) && $get['c'] == $session->checker
&& !isset($get['id'])) {
38 $this->removeBuilding($get['d']);
41 $session->changeChecker();
42 $this->upgradeBuilding($get['a']);
45 if(isset($get['a']) && $get['c'] == $session->checker
&& isset($get['id'])) {
46 $session->changeChecker();
47 $this->constructBuilding($get['id'],$get['a']);
49 if(isset($get['buildingFinish'])) {
50 if($session->gold
>= 2) {
56 public function canBuild($id,$tid) {
57 global $village,$session;
58 if($this->isMax($tid,$id)) {
62 if($this->isCurrent($id)) {
63 if($this->isLoop($id)) {
71 if($this->allocated
<= $this->maxConcurrent
) {
72 if($village->getProd("crop") == 2) {
76 switch($this->checkResource($tid,$id)) {
88 if($session->tribe
== 1 || ALLOW_ALL_TRIBE
) {
89 if($this->inner
== 0) {
94 if($this->plus
== 0) {
107 if($this->basic
== 0) {
112 if($this->plus
== 0) {
126 if($this->basic
== 1) {
127 if($session->plus
&& $this->plus
== 0) {
149 public function walling() {
151 $wall = array(31,32,33);
152 foreach($this->buildArray
as $job) {
153 if(in_array($job['type'],$wall)) {
154 return "3".$session->tribe
;
160 public function rallying() {
161 foreach($this->buildArray
as $job) {
162 if($job['type'] == 16) {
169 public function procResType($ref) {
172 case 1: $build = "Woodcutter"; break;
173 case 2: $build = "Clay Pit"; break;
174 case 3: $build = "Iron Mine"; break;
175 case 4: $build = "Cropland"; break;
176 case 5: $build = "Sawmill"; break;
177 case 6: $build = "Brickyard"; break;
178 case 7: $build = "Iron Foundry"; break;
179 case 8: $build = "Grain Mill"; break;
180 case 9: $build = "Bakery"; break;
181 case 10: $build = "Warehouse"; break;
182 case 11: $build = "Granary"; break;
183 case 12: $build = "Blacksmith"; break;
184 case 13: $build = "Armoury"; break;
185 case 14: $build = "Tournament Square"; break;
186 case 15: $build = "Main Building"; break;
187 case 16: $build = "Rally Point"; break;
188 case 17: $build = "Marketplace"; break;
189 case 18: $build = "Embassy"; break;
190 case 19: $build = "Barracks"; break;
191 case 20: $build = "Stable"; break;
192 case 21: $build = "Workshop"; break;
193 case 22: $build = "Academy"; break;
194 case 23: $build = "Cranny"; break;
195 case 24: $build = "Town Hall"; break;
196 case 25: $build = "Residence"; break;
197 case 26: $build = "Palace"; break;
198 case 27: $build = "Treasury"; break;
199 case 28: $build = "Trade Office"; break;
200 case 29: $build = "Great Barracks"; break;
201 case 30: $build = "Great Stable"; break;
202 case 31: $build = "City Wall"; break;
203 case 32: $build = "Earth Wall"; break;
204 case 33: $build = "Palisade"; break;
205 case 34: $build = "Stonemason's Lodge"; break;
206 case 35: $build = "Brewery"; break;
207 case 36: $build = "Trapper"; break;
208 case 37: $build = "Hero's Mansion"; break;
209 case 38: $build = "Great Warehouse"; break;
210 case 39: $build = "Great Granary"; break;
211 case 40: $build = "Wonder of the World"; break;
212 case 41: $build = "Horse Drinking Trough"; break;
213 default: $build = "Error"; break;
218 private function loadBuilding() {
219 global $database,$village,$session;
220 $this->buildArray
= $database->getJobs($village->wid
);
221 $this->allocated
= count($this->buildArray
);
222 if($this->allocated
> 0) {
223 foreach($this->buildArray
as $build) {
224 if($build['loopcon'] == 1) {
228 if($build['field'] <= 18) {
232 if($session->tribe
== 1 || ALLOW_ALL_TRIBE
) {
241 $this->NewBuilding
= true;
245 private function removeBuilding($d) {
246 global $database,$village;
247 foreach($this->buildArray
as $jobs) {
248 if($jobs['id'] == $d) {
249 $uprequire = $this->resourceRequired($jobs['field'],$jobs['type']);
250 if($database->removeBuilding($d)) {
251 $database->modifyResource($village->wid
,$uprequire['wood'],$uprequire['clay'],$uprequire['iron'],$uprequire['crop'],1);
252 if($jobs['field'] >= 19) {
253 header("Location: dorf2.php");
256 header("Location: dorf1.php");
263 private function upgradeBuilding($id) {
264 global $database,$village,$session,$logging,$ranking;
265 if($this->allocated
< $this->maxConcurrent
) {
266 $uprequire = $this->resourceRequired($id,$village->resarray
['f'.$id.'t']);
267 $time = time() +
$uprequire['time'];
268 $bindicate = $this->canBuild($id,$village->resarray
['f'.$id.'t']);
269 $loop = ($bindicate == 9 ?
1 : 0);
272 foreach($this->buildArray
as $build) {
273 if($build['field']==$id) {
275 $uprequire = $this->resourceRequired($id,$village->resarray
['f'.$id.'t'],($loopsame>0?
2:1));
278 if($session->tribe
== 1 || ALLOW_ALL_TRIBE
) {
280 foreach($this->buildArray
as $build) {
281 if($build['field'] >= 19) {
282 $time = $build['timestamp'] +
$uprequire['time'];
287 foreach($this->buildArray
as $build) {
288 if($build['field'] <= 18) {
289 $time = $build['timestamp'] +
$uprequire['time'];
295 $time = $this->buildArray
[0]['timestamp'] +
$uprequire['time'];
298 if($database->addBuilding($village->wid
,$id,$village->resarray
['f'.$id.'t'],$loop,$time)) {
299 $database->modifyResource($village->wid
,$uprequire['wood'],$uprequire['clay'],$uprequire['iron'],$uprequire['crop'],0);
300 //$ranking->procRankReq($session->uid);
301 $playerarray = $database->getUserArray($session->uid
,1);
302 $oldrank = $ranking->searchRank($session->username
,"username");
303 $database->updateoldrank($session->uid
,$oldrank);
304 $database->addclimberpop($session->uid
,$uprequire['pop']);
306 $logging->addBuildLog($village->wid
,$this->procResType($village->resarray
['f'.$id.'t']),($village->resarray
['f'.$id]+
($loopsame>0?
2:1)),0);
308 header("Location: dorf2.php");
311 header("Location: dorf1.php");
317 private function downgradeBuilding($id) {
318 global $database,$village,$session,$logging;
319 if($this->allocated
< $this->maxConcurrent
) {
320 $name = "bid".$village->resarray
['f'.$id.'t'];
323 $time = time() +
round($dataarray[$village->resarray
['f'.$id]-1]['time'] / 4);
325 if($this->inner
== 1 ||
$this->basic
== 1) {
326 if($session->plus
&& $this->plus
== 0) {
331 if($session->tribe
== 1 || ALLOW_ALL_TRIBE
) {
333 foreach($this->buildArray
as $build) {
334 if($build['field'] >= 19) {
335 $time = $build['timestamp'] +
round($dataarray[$village->resarray
['f'.$id]-1]['time'] / 4);
341 $time = $this->buildArray
[0]['timestamp'] +
round($dataarray[$village->resarray
['f'.$id]-1]['time'] / 4);
344 if($database->addBuilding($village->wid
,$id,$village->resarray
['f'.$id.'t'],$loop,$time,0)) {
345 $logging->addBuildLog($village->wid
,$this->procResType($village->resarray
['f'.$id.'t']),($village->resarray
['f'.$id]-1),2);
346 header("Location: dorf2.php");
353 private function constructBuilding($id,$tid) {
354 global $database,$village,$session,$logging;
355 if($this->allocated
< $this->maxConcurrent
) {
359 else if($tid == 31 ||
$tid == 32 ||
$tid == 33) {
362 $uprequire = $this->resourceRequired($id,$tid);
363 $time = time() +
$uprequire['time'];
364 $bindicate = $this->canBuild($id,$village->resarray
['f'.$id.'t']);
365 $loop = ($bindicate == 9 ?
1 : 0);
367 foreach($this->buildArray
as $build) {
368 if($build['field'] >= 19) {
369 $time = $build['timestamp'] +
$uprequire['time'];
373 if($this->meetRequirement($tid)) {
374 if($database->addBuilding($village->wid
,$id,$tid,$loop,$time)) {
375 $logging->addBuildLog($village->wid
,$this->procResType($tid),($village->resarray
['f'.$id]+
1),1);
376 $database->modifyResource($village->wid
,$uprequire['wood'],$uprequire['clay'],$uprequire['iron'],$uprequire['crop'],0);
377 $database->addclimberpop($session->uid
,$uprequire['pop']);
379 header("Location: dorf2.php");
385 private function meetRequirement($id) {
403 return ($this->getTypeLevel(15) >= 1)?
true : false;
406 if($this->getTypeLevel(1) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
409 if($this->getTypeLevel(2) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
412 if($this->getTypeLevel(3) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
415 if($this->getTypeLevel(4) >= 5) { return true; } else { return false; }
418 if($this->getTypeLevel(15) >= 5 && $this->getTypeLevel(4) >= 10 && $this->getTypeLevel(8) >= 5) { return true; } else { return false; }
421 if($this->getTypeLevel(22) >= 3 && $this->getTypeLevel(15) >= 3) { return true; } else { return false; }
424 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(22) >= 1) { return true; } else { return false; }
427 if($this->getTypeLevel(16) >= 15) { return true; } else { return false; }
430 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(10) >= 1 && $this->getTypeLevel(11) >= 1) { return true; } else { return false; }
433 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1) { return true; } else { return false; }
436 if($this->getTypeLevel(12) >= 3 && $this->getTypeLevel(22) >= 5) { return true; } else { return false; }
439 if($this->getTypeLevel(22) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
442 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1) { return true; } else { return false; }
445 if($this->getTypeLevel(22) >= 10 && $this->getTypeLevel(15) >= 10) { return true; } else { return false; }
448 if($this->getTypeLevel(15) >= 5) { return true; } else { return false; }
451 if($this->getTypeLevel(18) >= 1 && $this->getTypeLevel(15) >= 5 && $this->getTypeLevel(25) == 0) { return true; } else { return false; }
454 if($this->getTypeLevel(15) >= 10) { return true; } else { return false; }
457 if($this->getTypeLevel(17) == 20 && $this->getTypeLevel(20) >= 10) { return true; } else { return false; }
460 if($this->getTypeLevel(16) == 20) { return true; } else { return false; }
463 if($this->getTypeLevel(20) == 20) { return true; } else { return false; }
466 if($this->getTypeLevel(26) >= 3 && $this->getTypeLevel(15) >= 5 && $this->getTypeLevel(25) == 0) { return true; } else { return false; }
469 if($this->getTypeLevel(16) >= 10 && $this->getTypeLevel(11) == 20) { return true; } else { return false; }
472 if($this->getTypeLevel(16) >= 1) { return true; } else { return false; }
475 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1) { return true; } else { return false; }
479 if($this->getTypeLevel(15) >= 10) { return true; } else { return false; }
482 return false; //not implemented
485 if($this->getTypeLevel(16) >= 10 && $this->getTypeLevel(20) == 20) { return true; } else { return false; }
490 private function checkResource($tid,$id) {
492 global $village,$
$name;
494 $wood = $dataarray[$village->resarray
['f'.$id]+
1]['wood'];
495 $clay = $dataarray[$village->resarray
['f'.$id]+
1]['clay'];
496 $iron = $dataarray[$village->resarray
['f'.$id]+
1]['iron'];
497 $crop = $dataarray[$village->resarray
['f'.$id]+
1]['crop'];
498 if($wood > $village->maxstore ||
$clay > $village->maxstore ||
$iron > $village->maxstore
) {
502 if($crop > $village->maxcrop
) {
506 if($wood > $village->awood ||
$clay > $village->aclay ||
$iron > $village->airon ||
$crop > $village->acrop
) {
510 if($village->awood
-$wood > 0 && $village->aclay
-$clay > 0 && $village->airon
-$iron > 0 && $village->acrop
-$crop >0){
521 public function isMax($id,$field) {
523 global $
$name,$village;
526 if($village->capital
== 1) {
527 return ($village->resarray
['f'.$field] == (count($dataarray) - 1));
530 return ($village->resarray
['f'.$field] == (count($dataarray) - 11));
534 return ($village->resarray
['f'.$field] == count($dataarray));
538 public function getTypeLevel($tid) {
540 $keyholder = array();
541 foreach(array_keys($village->resarray
,$tid) as $key) {
542 if(strpos($key,'t')) {
543 $key = preg_replace("/[^0-9]/", '', $key);
544 array_push($keyholder, $key);
547 $element = count($keyholder);
550 $temparray = array();
551 for($i=0;$i<=$element-1;$i++
) {
552 array_push($temparray,$village->resarray
['f'.$keyholder[$i]]);
554 foreach ($temparray as $key => $val) {
555 if ($val == max($temparray))
560 for($i=0;$i<=$element-1;$i++
) {
561 if($village->resarray
['f'.$keyholder[$i]] != $keyholder[0]) {
567 else if($element == 1) {
573 if($keyholder[$target] != "") {
574 return $village->resarray
['f'.$keyholder[$target]];
582 private function isCurrent($id) {
583 foreach($this->buildArray
as $build) {
584 if($build['field'] < $id && $build['field'] > $id) {
590 private function isLoop($id) {
591 foreach($this->buildArray
as $build) {
592 if($build['field'] == $id && $build['loopcon'] == 1) {
599 private function finishAll() {
600 global $database,$session,$logging,$village,$bid18,$bid10,$bid11,$technology;
601 foreach($this->buildArray
as $jobs) {
602 $level = $database->getFieldLevel($jobs['wid'],$jobs['field']);
603 $level = ($level == -1) ?
0 : $level;
604 if($jobs['type'] != 25 AND $jobs['type'] != 26 AND $jobs['type'] != 40) {
605 $resource = $this->resourceRequired($jobs['field'],$jobs['type']);
606 $q = "UPDATE ".TB_PREFIX
."fdata set f".$jobs['field']." = f".$jobs['field']." + 1, f".$jobs['field']."t = ".$jobs['type']." where vref = ".$jobs['wid'];
607 if($database->query($q)) {
609 $database->modifyPop($jobs['wid'],$resource['pop'],0);
610 $database->addCP($jobs['wid'],$resource['cp']);
612 $q = "DELETE FROM ".TB_PREFIX
."bdata where id = ".$jobs['id'];
613 $database->query($q);
614 if($jobs['type'] == 18) {
615 $owner = $database->getVillageField($jobs['wid'],"owner");
616 $max = $bid18[$level]['attri'];
617 $q = "UPDATE ".TB_PREFIX
."alidata set max = $max where leader = $owner";
618 $database->query($q);
620 if($jobs['type'] == 10) {
621 $max=$database->getVillageField($jobs['wid'],"maxstore");
622 if($level=='0'){ $max-=800; }
623 $max-=$bid10[$level]['attri'];
624 $max+
=$bid10[$level+
1]['attri'];
625 $database->setVillageField($jobs['wid'],"maxstore",$max);
628 if($jobs['type'] == 11) {
629 $max=$database->getVillageField($jobs['wid'],"maxcrop");
630 if($level=='0'){ $max-=800; }
631 $max-=$bid11[$level]['attri'];
632 $max+
=$bid11[$level+
1]['attri'];
633 $database->setVillageField($jobs['wid'],"maxcrop",$max);
639 $technology->finishTech();
640 $logging->goldFinLog($village->wid
);
641 $database->modifyGold($session->uid
,0,0);
642 header("Location: ".$session->referrer
);
645 public function resourceRequired($id,$tid,$plus=1) {
647 global $
$name,$village,$bid15;
649 $wood = $dataarray[$village->resarray
['f'.$id]+
$plus]['wood'];
650 $clay = $dataarray[$village->resarray
['f'.$id]+
$plus]['clay'];
651 $iron = $dataarray[$village->resarray
['f'.$id]+
$plus]['iron'];
652 $crop = $dataarray[$village->resarray
['f'.$id]+
$plus]['crop'];
653 $pop = $dataarray[$village->resarray
['f'.$id]+
$plus]['pop'];
655 if($this->getTypeLevel(15) == 0) {
656 $time = round($dataarray[$village->resarray
['f'.$id]+
$plus]['time']/ SPEED
*5);
659 $time = round($dataarray[$village->resarray
['f'.$id]+
$plus]['time'] / SPEED
);
663 if($this->getTypeLevel(15) != 0) {
664 $time = round($dataarray[$village->resarray
['f'.$id]+
$plus]['time'] * ($bid15[$this->getTypeLevel(15)]['attri']/100) / SPEED
);
667 $time = round($dataarray[$village->resarray
['f'.$id]+
$plus]['time']*5 / SPEED
);
670 $cp = $dataarray[$village->resarray
['f'.$id]+
$plus]['cp'];
671 return array("wood"=>$wood,"clay"=>$clay,"iron"=>$iron,"crop"=>$crop,"pop"=>$pop,"time"=>$time,"cp"=>$cp);
674 public function getTypeField($type) {
676 for($i=19;$i<=40;$i++
) {
677 if($village->resarray
['f'.$i.'t'] == $type) {
683 public function calculateAvaliable($id,$tid) {
684 global $village,$generator;
685 $uprequire = $this->resourceRequired($id,$tid);
686 $rwood = $uprequire['wood']-$village->awood
;
687 $rclay = $uprequire['clay']-$village->aclay
;
688 $rcrop = $uprequire['crop']-$village->acrop
;
689 $riron = $uprequire['iron']-$village->airon
;
690 $rwtime = $rwood / $village->getProd("wood") * 3600;
691 $rcltime = $rclay / $village->getProd("clay")* 3600;
692 $rctime = $rcrop / $village->getProd("crop")* 3600;
693 $ritime = $riron / $village->getProd("iron")* 3600;
694 $reqtime = max($rwtime,$rctime,$rcltime,$ritime);
696 return $generator->procMtime($reqtime);