From 765914d8632e09be962968726645c29f5f34e65d Mon Sep 17 00:00:00 2001 From: Martin Ambrus Date: Tue, 26 Dec 2017 19:58:34 +0100 Subject: [PATCH] fix: villages with artifacts must not be destroyed #425 --- GameEngine/Automation.php | 33 +++++++++++++++++++++++++++++---- GameEngine/Database.php | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index 56bae211..00fb49ba 100755 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -2246,9 +2246,14 @@ class Automation { //catapults look :D $info_cat = $info_chief = $info_ram = $info_hero = ","; //check to see if can destroy village - if(count($varray)!='1' AND $to['capital']!='1'){ + $hasArtefact = false; + if (count($varray) != 1 && $to['capital'] != 1 && !$database->villageHasArtefact($DefenderWref)) { $can_destroy=1; - }else{ + } else { + if ($database->villageHasArtefact($DefenderWref)) { + $hasArtefact = true; + } + $can_destroy=0; } if ($isoasis == 1) $can_destroy=0; @@ -2304,6 +2309,20 @@ class Automation { /** * FIRST CATAPULTS ROW */ + + // first of all, update data if we're targetting a Treasury in village with an artefact, + // since such village (and its Treasury) cannot be destroyed until that artefact is + // removed... don't worry about Treasury levels here either, that can be taken care of + // once the artefact is gone + if ($hasArtefact && $data['ctar1'] == 27) { + $data['ctar1'] = 0; + } + + if (isset($data['ctar2']) && $hasArtefact && $data['ctar2'] == 27) { + $data['ctar2'] = 0; + } + + // on to targetting $basearray = $data['to']; $bdo = $database->getResourceLevel($basearray, false); $catapultTarget = $data['ctar1']; @@ -2359,7 +2378,10 @@ class Automation { if ($i==41) $i=99; if ($bdo['f'.$i] > 0 && $catapultTarget != 31 && $catapultTarget != 32 && $catapultTarget != 33) { - $list[]=$i; + // don't allow to randomly select Treasury if there's an artefact in it + if (!$hasArtefact || ($hasArtefact && $bdo['f'.$i.'t'] != 27)) { + $list[] = $i; + } } } $catapultTarget = $list[ rand(0, count($list) - 1) ]; @@ -2421,7 +2443,10 @@ class Automation { if ($i==41) $i=99; if ($bdo['f'.$i] > 0) { - $list[]=$i; + // don't allow to randomly select Treasury if there's an artefact in it + if (!$hasArtefact || ($hasArtefact && $bdo['f'.$i.'t'] != 27)) { + $list[] = $i; + } } } $catapultTarget2 = $list[ rand(0, count($list) - 1) ]; diff --git a/GameEngine/Database.php b/GameEngine/Database.php index a7abcd1c..8c9583b7 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -7130,6 +7130,24 @@ References: User ID/Message ID, Mode } } + function villageHasArtefact($vref) { + // this is a somewhat non-ideal, externally non-changeable way of caching + // but since we're only ever going to be calling this from a single point of Automation, + // this will more than suffice + static $cachedData = []; + $vref = (int) $vref; + + if (isset($cachedData[$vref])) { + return $cachedData[$vref]; + } + + $q = "SELECT Count(*) as Total FROM " . TB_PREFIX . "artefacts WHERE vref = $vref"; + $result = mysqli_fetch_array(mysqli_query($this->dblink, $q), MYSQLI_ASSOC); + $cachedData[$vref] = $result['Total']; + + return $cachedData[$vref]; + } + function claimArtefact($vref, $ovref, $id) { list($vref, $ovref, $id) = $this->escape_input((int) $vref, (int) $ovref, (int) $id);