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) {
55 public function isCurrent($id) {
56 foreach($this->buildArray
as $build) {
57 if($build['field'] == $id && $build['loopcon'] <> 1) {
63 public function canBuild($id,$tid) {
64 global $village,$session;
65 if($this->isMax($tid,$id)) {
67 } else if($this->isMax($tid,$id,1) && ($this->isLoop($id) ||
$this->isCurrent($id))) {
69 } else if($this->isMax($tid,$id,2) && $this->isLoop($id) && $this->isCurrent($id)) {
73 if($this->allocated
<= $this->maxConcurrent
) {
74 if($village->getProd("crop") == 2) {
78 switch($this->checkResource($tid,$id)) {
90 if($session->tribe
== 1 || ALLOW_ALL_TRIBE
) {
91 if($this->inner
== 0) {
96 if($this->plus
== 0) {
109 if($this->basic
== 0) {
114 if($this->plus
== 0) {
128 if($this->basic
== 1) {
129 if($session->plus
&& $this->plus
== 0) {
150 public function walling() {
152 $wall = array(31,32,33);
153 foreach($this->buildArray
as $job) {
154 if(in_array($job['type'],$wall)) {
155 return "3".$session->tribe
;
161 public function rallying() {
162 foreach($this->buildArray
as $job) {
163 if($job['type'] == 16) {
170 public function procResType($ref) {
173 case 1: $build = "Woodcutter"; break;
174 case 2: $build = "Clay Pit"; break;
175 case 3: $build = "Iron Mine"; break;
176 case 4: $build = "Cropland"; break;
177 case 5: $build = "Sawmill"; break;
178 case 6: $build = "Brickyard"; break;
179 case 7: $build = "Iron Foundry"; break;
180 case 8: $build = "Grain Mill"; break;
181 case 9: $build = "Bakery"; break;
182 case 10: $build = "Warehouse"; break;
183 case 11: $build = "Granary"; break;
184 case 12: $build = "Blacksmith"; break;
185 case 13: $build = "Armoury"; break;
186 case 14: $build = "Tournament Square"; break;
187 case 15: $build = "Main Building"; break;
188 case 16: $build = "Rally Point"; break;
189 case 17: $build = "Marketplace"; break;
190 case 18: $build = "Embassy"; break;
191 case 19: $build = "Barracks"; break;
192 case 20: $build = "Stable"; break;
193 case 21: $build = "Workshop"; break;
194 case 22: $build = "Academy"; break;
195 case 23: $build = "Cranny"; break;
196 case 24: $build = "Town Hall"; break;
197 case 25: $build = "Residence"; break;
198 case 26: $build = "Palace"; break;
199 case 27: $build = "Treasury"; break;
200 case 28: $build = "Trade Office"; break;
201 case 29: $build = "Great Barracks"; break;
202 case 30: $build = "Great Stable"; break;
203 case 31: $build = "City Wall"; break;
204 case 32: $build = "Earth Wall"; break;
205 case 33: $build = "Palisade"; break;
206 case 34: $build = "Stonemason's Lodge"; break;
207 case 35: $build = "Brewery"; break;
208 case 36: $build = "Trapper"; break;
209 case 37: $build = "Hero's Mansion"; break;
210 case 38: $build = "Great Warehouse"; break;
211 case 39: $build = "Great Granary"; break;
212 case 40: $build = "Wonder of the World"; break;
213 case 41: $build = "Horse Drinking Trough"; break;
214 case 42: $build = "Great Workshop"; break;
215 default: $build = "Error"; break;
220 private function loadBuilding() {
221 global $database,$village,$session;
222 $this->buildArray
= $database->getJobs($village->wid
);
223 $this->allocated
= count($this->buildArray
);
224 if($this->allocated
> 0) {
225 foreach($this->buildArray
as $build) {
226 if($build['loopcon'] == 1) {
230 if($build['field'] <= 18) {
234 if($session->tribe
== 1 || ALLOW_ALL_TRIBE
) {
243 $this->NewBuilding
= true;
247 private function removeBuilding($d) {
248 global $database,$village;
249 foreach($this->buildArray
as $jobs) {
250 if($jobs['id'] == $d) {
251 $uprequire = $this->resourceRequired($jobs['field'],$jobs['type']);
252 if($database->removeBuilding($d)) {
253 $database->modifyResource($village->wid
,$uprequire['wood'],$uprequire['clay'],$uprequire['iron'],$uprequire['crop'],1);
254 if($jobs['field'] >= 19) {
255 header("Location: dorf2.php");
258 header("Location: dorf1.php");
265 private function upgradeBuilding($id) {
266 global $database,$village,$session,$logging;
267 if($this->allocated
< $this->maxConcurrent
) {
268 $uprequire = $this->resourceRequired($id,$village->resarray
['f'.$id.'t']);
269 $time = time() +
$uprequire['time'];
270 $bindicate = $this->canBuild($id,$village->resarray
['f'.$id.'t']);
271 $loop = ($bindicate == 9 ?
1 : 0);
274 foreach($this->buildArray
as $build) {
275 if($build['field']==$id) {
277 $uprequire = $this->resourceRequired($id,$village->resarray
['f'.$id.'t'],($loopsame>0?
2:1));
280 if($session->tribe
== 1 || ALLOW_ALL_TRIBE
) {
282 foreach($this->buildArray
as $build) {
283 if($build['field'] >= 19) {
284 $time = $build['timestamp'] +
$uprequire['time'];
289 foreach($this->buildArray
as $build) {
290 if($build['field'] <= 18) {
291 $time = $build['timestamp'] +
$uprequire['time'];
297 $time = $this->buildArray
[0]['timestamp'] +
$uprequire['time'];
300 if($database->addBuilding($village->wid
,$id,$village->resarray
['f'.$id.'t'],$loop,$time+
($loop==1?
60:0))) {
301 $database->modifyResource($village->wid
,$uprequire['wood'],$uprequire['clay'],$uprequire['iron'],$uprequire['crop'],0);
302 $logging->addBuildLog($village->wid
,$this->procResType($village->resarray
['f'.$id.'t']),($village->resarray
['f'.$id]+
($loopsame>0?
2:1)),0);
304 header("Location: dorf2.php");
307 header("Location: dorf1.php");
313 private function downgradeBuilding($id) {
314 global $database,$village,$session,$logging;
315 if($this->allocated
< $this->maxConcurrent
) {
316 $name = "bid".$village->resarray
['f'.$id.'t'];
319 $time = time() +
round($dataarray[$village->resarray
['f'.$id]-1]['time'] / 4);
321 if($this->inner
== 1 ||
$this->basic
== 1) {
322 if($session->plus
&& $this->plus
== 0) {
327 if($session->tribe
== 1 || ALLOW_ALL_TRIBE
) {
329 foreach($this->buildArray
as $build) {
330 if($build['field'] >= 19) {
331 $time = $build['timestamp'] +
round($dataarray[$village->resarray
['f'.$id]-1]['time'] / 4);
337 $time = $this->buildArray
[0]['timestamp'] +
round($dataarray[$village->resarray
['f'.$id]-1]['time'] / 4);
340 if($database->addBuilding($village->wid
,$id,$village->resarray
['f'.$id.'t'],$loop,$time,0)) {
341 $logging->addBuildLog($village->wid
,$this->procResType($village->resarray
['f'.$id.'t']),($village->resarray
['f'.$id]-1),2);
342 header("Location: dorf2.php");
349 private function constructBuilding($id,$tid) {
350 global $database,$village,$session,$logging;
351 if($this->allocated
< $this->maxConcurrent
) {
355 else if($tid == 31 ||
$tid == 32 ||
$tid == 33) {
358 $uprequire = $this->resourceRequired($id,$tid);
359 $time = time() +
$uprequire['time'];
360 $bindicate = $this->canBuild($id,$village->resarray
['f'.$id.'t']);
361 $loop = ($bindicate == 9 ?
1 : 0);
363 foreach($this->buildArray
as $build) {
364 if($build['field'] >= 19) {
365 $time = $build['timestamp'] +
$uprequire['time'];
369 if($this->meetRequirement($tid)) {
370 if($database->addBuilding($village->wid
,$id,$tid,$loop,$time)) {
371 $logging->addBuildLog($village->wid
,$this->procResType($tid),($village->resarray
['f'.$id]+
1),1);
372 $database->modifyResource($village->wid
,$uprequire['wood'],$uprequire['clay'],$uprequire['iron'],$uprequire['crop'],0);
373 $database->addclimberpop($session->uid
,$uprequire['pop']);
375 header("Location: dorf2.php");
381 private function meetRequirement($id) {
399 return ($this->getTypeLevel(15) >= 1)?
true : false;
402 if($this->getTypeLevel(1) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
405 if($this->getTypeLevel(2) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
408 if($this->getTypeLevel(3) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
411 if($this->getTypeLevel(4) >= 5) { return true; } else { return false; }
414 if($this->getTypeLevel(15) >= 5 && $this->getTypeLevel(4) >= 10 && $this->getTypeLevel(8) >= 5) { return true; } else { return false; }
417 if($this->getTypeLevel(22) >= 3 && $this->getTypeLevel(15) >= 3) { return true; } else { return false; }
420 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(22) >= 1) { return true; } else { return false; }
423 if($this->getTypeLevel(16) >= 15) { return true; } else { return false; }
426 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(10) >= 1 && $this->getTypeLevel(11) >= 1) { return true; } else { return false; }
429 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1) { return true; } else { return false; }
432 if($this->getTypeLevel(12) >= 3 && $this->getTypeLevel(22) >= 5) { return true; } else { return false; }
435 if($this->getTypeLevel(22) >= 10 && $this->getTypeLevel(15) >= 5) { return true; } else { return false; }
438 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1) { return true; } else { return false; }
441 if($this->getTypeLevel(22) >= 10 && $this->getTypeLevel(15) >= 10) { return true; } else { return false; }
444 if($this->getTypeLevel(15) >= 5) { return true; } else { return false; }
447 if($this->getTypeLevel(18) >= 1 && $this->getTypeLevel(15) >= 5 && $this->getTypeLevel(25) == 0) { return true; } else { return false; }
450 if($this->getTypeLevel(15) >= 10) { return true; } else { return false; }
453 if($this->getTypeLevel(17) == 20 && $this->getTypeLevel(20) >= 10) { return true; } else { return false; }
456 if($this->getTypeLevel(19) == 20) { return true; } else { return false; }
459 if($this->getTypeLevel(20) == 20) { return true; } else { return false; }
462 if($this->getTypeLevel(26) >= 3 && $this->getTypeLevel(15) >= 5 && $this->getTypeLevel(25) == 0) { return true; } else { return false; }
465 if($this->getTypeLevel(16) >= 10 && $this->getTypeLevel(11) == 20) { return true; } else { return false; }
468 if($this->getTypeLevel(16) >= 1) { return true; } else { return false; }
471 if($this->getTypeLevel(15) >= 3 && $this->getTypeLevel(16) >= 1) { return true; } else { return false; }
475 if($this->getTypeLevel(15) >= 10) { return true; } else { return false; }
478 return false; //not implemented
481 if($this->getTypeLevel(16) >= 10 && $this->getTypeLevel(20) == 20) { return true; } else { return false; }
484 if($this->getTypeLevel(21) == 20 && $village->capital
== 0 && GREAT_WKS
) { return true; } else { return false; }
489 private function checkResource($tid,$id) {
491 global $village,$
$name;
493 $wood = $dataarray[$village->resarray
['f'.$id]+
1]['wood'];
494 $clay = $dataarray[$village->resarray
['f'.$id]+
1]['clay'];
495 $iron = $dataarray[$village->resarray
['f'.$id]+
1]['iron'];
496 $crop = $dataarray[$village->resarray
['f'.$id]+
1]['crop'];
497 if($wood > $village->maxstore ||
$clay > $village->maxstore ||
$iron > $village->maxstore
) {
501 if($crop > $village->maxcrop
) {
505 if($wood > $village->awood ||
$clay > $village->aclay ||
$iron > $village->airon ||
$crop > $village->acrop
) {
509 if($village->awood
-$wood > 0 && $village->aclay
-$clay > 0 && $village->airon
-$iron > 0 && $village->acrop
-$crop >0){
520 public function isMax($id,$field,$loop=0) {
522 global $
$name,$village;
525 if($village->capital
== 1) {
526 return ($village->resarray
['f'.$field] == (count($dataarray) - 1 - $loop));
529 return ($village->resarray
['f'.$field] == (count($dataarray) - 11 - $loop));
533 return ($village->resarray
['f'.$field] == count($dataarray) - $loop);
537 public function getTypeLevel($tid) {
539 $keyholder = array();
540 foreach(array_keys($village->resarray
,$tid) as $key) {
541 if(strpos($key,'t')) {
542 $key = preg_replace("/[^0-9]/", '', $key);
543 array_push($keyholder, $key);
546 $element = count($keyholder);
549 $temparray = array();
550 for($i=0;$i<=$element-1;$i++
) {
551 array_push($temparray,$village->resarray
['f'.$keyholder[$i]]);
553 foreach ($temparray as $key => $val) {
554 if ($val == max($temparray))
559 for($i=0;$i<=$element-1;$i++
) {
560 if($village->resarray
['f'.$keyholder[$i]] != $keyholder[0]) {
566 else if($element == 1) {
572 if($keyholder[$target] != "") {
573 return $village->resarray
['f'.$keyholder[$target]];
581 public function isLoop($id=0) {
582 foreach($this->buildArray
as $build) {
583 if(($build['field'] == $id && $build['loopcon']) ||
($build['loopcon'] == 1 && $id == 0)) {
592 private function finishAll() {
593 global $database,$session,$logging,$village,$bid18,$bid10,$bid11,$technology;
594 foreach($this->buildArray
as $jobs) {
595 $level = $database->getFieldLevel($jobs['wid'],$jobs['field']);
596 $level = ($level == -1) ?
0 : $level;
597 if($jobs['type'] != 25 AND $jobs['type'] != 26 AND $jobs['type'] != 40) {
598 $resource = $this->resourceRequired($jobs['field'],$jobs['type']);
599 $q = "UPDATE ".TB_PREFIX
."fdata set f".$jobs['field']." = f".$jobs['field']." + 1, f".$jobs['field']."t = ".$jobs['type']." where vref = ".$jobs['wid'];
600 if($database->query($q)) {
602 $database->modifyPop($jobs['wid'],$resource['pop'],0);
603 $database->addCP($jobs['wid'],$resource['cp']);
605 $q = "DELETE FROM ".TB_PREFIX
."bdata where id = ".$jobs['id'];
606 $database->query($q);
607 if($jobs['type'] == 18) {
608 $owner = $database->getVillageField($jobs['wid'],"owner");
609 $max = $bid18[$level]['attri'];
610 $q = "UPDATE ".TB_PREFIX
."alidata set max = $max where leader = $owner";
611 $database->query($q);
613 if($jobs['type'] == 10) {
614 $max=$database->getVillageField($jobs['wid'],"maxstore");
615 if($level=='0'){ $max-=800; }
616 $max-=$bid10[$level]['attri'];
617 $max+
=$bid10[$level+
1]['attri'];
618 $database->setVillageField($jobs['wid'],"maxstore",$max);
621 if($jobs['type'] == 11) {
622 $max=$database->getVillageField($jobs['wid'],"maxcrop");
623 if($level=='0'){ $max-=800; }
624 $max-=$bid11[$level]['attri'];
625 $max+
=$bid11[$level+
1]['attri'];
626 $database->setVillageField($jobs['wid'],"maxcrop",$max);
632 $technology->finishTech();
633 $logging->goldFinLog($village->wid
);
634 $database->modifyGold($session->uid
,0,0);
635 header("Location: ".$session->referrer
);
638 public function resourceRequired($id,$tid,$plus=1) {
640 global $
$name,$village,$bid15;
642 $wood = $dataarray[$village->resarray
['f'.$id]+
$plus]['wood'];
643 $clay = $dataarray[$village->resarray
['f'.$id]+
$plus]['clay'];
644 $iron = $dataarray[$village->resarray
['f'.$id]+
$plus]['iron'];
645 $crop = $dataarray[$village->resarray
['f'.$id]+
$plus]['crop'];
646 $pop = $dataarray[$village->resarray
['f'.$id]+
$plus]['pop'];
648 if($this->getTypeLevel(15) == 0) {
649 $time = round($dataarray[$village->resarray
['f'.$id]+
$plus]['time']/ SPEED
*5);
652 $time = round($dataarray[$village->resarray
['f'.$id]+
$plus]['time'] / SPEED
);
656 if($this->getTypeLevel(15) != 0) {
657 $time = round($dataarray[$village->resarray
['f'.$id]+
$plus]['time'] * ($bid15[$this->getTypeLevel(15)]['attri']/100) / SPEED
);
660 $time = round($dataarray[$village->resarray
['f'.$id]+
$plus]['time']*5 / SPEED
);
663 $cp = $dataarray[$village->resarray
['f'.$id]+
$plus]['cp'];
664 return array("wood"=>$wood,"clay"=>$clay,"iron"=>$iron,"crop"=>$crop,"pop"=>$pop,"time"=>$time,"cp"=>$cp);
667 public function getTypeField($type) {
669 for($i=19;$i<=40;$i++
) {
670 if($village->resarray
['f'.$i.'t'] == $type) {
676 public function calculateAvaliable($id,$tid,$plus=1) {
677 global $village,$generator;
678 $uprequire = $this->resourceRequired($id,$tid,$plus);
679 $rwood = $uprequire['wood']-$village->awood
;
680 $rclay = $uprequire['clay']-$village->aclay
;
681 $rcrop = $uprequire['crop']-$village->acrop
;
682 $riron = $uprequire['iron']-$village->airon
;
683 $rwtime = $rwood / $village->getProd("wood") * 3600;
684 $rcltime = $rclay / $village->getProd("clay")* 3600;
685 $rctime = $rcrop / $village->getProd("crop")* 3600;
686 $ritime = $riron / $village->getProd("iron")* 3600;
687 $reqtime = max($rwtime,$rctime,$rcltime,$ritime);
689 return $generator->procMtime($reqtime);