From f032523e1c389c9ac07feaa9a8f76277afa5e7f0 Mon Sep 17 00:00:00 2001 From: iopietro Date: Sat, 28 Jul 2018 15:07:52 +0200 Subject: [PATCH] Generale fixes +Fixed a bug that didn't return reinforcements in counquered oasis, if it was released by the owner, or from the admin panel +Changed the catapults and rams formulas (still not completed at 100%), thanks to Kirilloid! +Changed the Natars' capital default coordinates --- Admin/Templates/village.tpl | 2 +- GameEngine/Admin/function.php | 9 ++- GameEngine/Artifacts.php | 25 +++---- GameEngine/Automation.php | 50 +++++++------ GameEngine/Battle.php | 137 ++++++++++++++++++---------------- GameEngine/Database.php | 4 +- GameEngine/Units.php | 1 + Templates/Build/37_land.tpl | 3 +- warsim.php | 2 +- 9 files changed, 123 insertions(+), 110 deletions(-) diff --git a/Admin/Templates/village.tpl b/Admin/Templates/village.tpl index 49368d54..91659d19 100644 --- a/Admin/Templates/village.tpl +++ b/Admin/Templates/village.tpl @@ -253,7 +253,7 @@ if(isset($id)){ foreach($newResult as $row){ echo " - + ".$row['name']." getMapCheck($row['wref'])."\" target=\"blank\">(".$row['x']."|".$row['y'].") ".round($row['loyalty'])."% diff --git a/GameEngine/Admin/function.php b/GameEngine/Admin/function.php index b77d1b35..385629d1 100755 --- a/GameEngine/Admin/function.php +++ b/GameEngine/Admin/function.php @@ -18,6 +18,9 @@ // # ## // ################################################################################ include_once("../GameEngine/Artifacts.php"); +include_once("../GameEngine/Units.php"); +include_once("../GameEngine/Generator.php"); + class funct { @@ -28,7 +31,7 @@ class funct function Act($get) { - global $admin, $database; + global $admin, $database, $units, $generator; $artifact = new Artifacts(); @@ -71,8 +74,8 @@ class funct // add ban break; case "delOas": - $database->query('UPDATE ' . TB_PREFIX . 'odata SET conqured = 0, owner = 2, name = "Unoccupied Oasis" WHERE wref = ' . $get['oid']); - // oaza + $units->returnTroops($get['did'], 1); + $database->removeOases($get['oid']); break; case "logout": $this->LogOut(); diff --git a/GameEngine/Artifacts.php b/GameEngine/Artifacts.php index 4c49989e..035119ab 100644 --- a/GameEngine/Artifacts.php +++ b/GameEngine/Artifacts.php @@ -35,23 +35,18 @@ class Artifacts */ NATARS_CAPITAL_COORDINATES = [[WORLD_MAX, WORLD_MAX], + [WORLD_MAX, 0], [WORLD_MAX, -WORLD_MAX], + [0, -WORLD_MAX], [-WORLD_MAX, -WORLD_MAX], - [WORLD_MAX - 1, WORLD_MAX], - [WORLD_MAX, WORLD_MAX - 1], - [-WORLD_MAX, WORLD_MAX - 1], - [WORLD_MAX - 1, -WORLD_MAX], - [WORLD_MAX - 1, WORLD_MAX - 1], - [WORLD_MAX, -WORLD_MAX + 1], - [WORLD_MAX - 1, -WORLD_MAX + 1], - [-WORLD_MAX + 1, -WORLD_MAX + 1], - [WORLD_MAX - 2, WORLD_MAX], - [WORLD_MAX - 2, -WORLD_MAX], - [WORLD_MAX - 2, WORLD_MAX - 1], - [WORLD_MAX - 1, WORLD_MAX - 2], - [-WORLD_MAX + 2, WORLD_MAX], - [-WORLD_MAX + 2, WORLD_MAX - 1], - [-WORLD_MAX + 2, -WORLD_MAX + 2]], + [-WORLD_MAX, 0], + [-WORLD_MAX, WORLD_MAX], + [0, WORLD_MAX], + [WORLD_MAX / 10, WORLD_MAX / 20], + [WORLD_MAX / 10, -WORLD_MAX / 10], + [-WORLD_MAX / 20, -WORLD_MAX / 10], + [-WORLD_MAX / 10, 0], + [-WORLD_MAX / 20, WORLD_MAX / 10]], /** * @var array Normal Natars' artifacts diff --git a/GameEngine/Automation.php b/GameEngine/Automation.php index 90804055..bc0fb714 100644 --- a/GameEngine/Automation.php +++ b/GameEngine/Automation.php @@ -711,27 +711,33 @@ class Automation { if(isset($catapultTarget)) { - // currently targeted building/field level + //Currently targeted building/field level $tblevel = (int) $bdo['f'.$catapultTarget]; - // currently targetet building/field GID (ID of the building/field type - woodcutter, cropland, embassy...) + //Currently targetet building/field GID (ID of the building/field type - woodcutter, cropland, embassy...) $tbgid = (int) $bdo['f'.$catapultTarget.'t']; - // currently targeted building/field ID in the database (fdata, the fID field, e.g. f1, f2, f3...) + //Currently targeted building/field ID in the database (fdata, the fID field, e.g. f1, f2, f3...) $tbid = (int) $catapultTarget; - $newLevel = $battle->CalculateNewBuildingLevel($battlepart['catapults']['moral'], - $battlepart['catapults']['upgrades'], - $battlepart['catapults']['downgrades'], - $tblevel, - $battlepart['catapults']['realAttackers'] / ($twoRowsCatapultSetup ? 2 : 1), - $data['t8'] / ($twoRowsCatapultSetup ? 2 : 1), - ($catapultTarget == 40 ? 1 : $battlepart['catapults']['strongerBuildings'])); + //If we're targeting the WW + if($catapultTarget == 40){ + $battlepart['catapults']['strongerBuildings'] = 1; + $battlepart['catapults']['moraleBonus'] = 1; + } + + $catapultsDamage = $battle->calculateCatapultsDamage($data['t8'], + $battlepart['catapults']['upgrades'], + $battlepart['catapults']['durability'], + $battlepart['catapults']['attackDefenseRatio'], + $battlepart['catapults']['strongerBuildings'], + $battlepart['catapults']['moraleBonus']); + + $newLevel = $battle->calculateNewBuildingLevel($tblevel, $catapultsDamage / ($twoRowsCatapultSetup ? 2 : 1)); //If that building was present in the building queue, we have to modify his level or remove it $database->modifyBData($data['to'], $tbid, [$newLevel, $tblevel], $tribe); // building/field destroyed - if ($newLevel == 0) - { + if ($newLevel == 0){ // prepare data to be updated $fieldsToSet = ["f".$tbid]; $fieldValuesToSet = [0]; @@ -1266,11 +1272,8 @@ class Automation { } } } - - //to battle.php + //fix by ronix - //MUST TO BE FIX : You need to filter these values - //filter_input_array($battlepart = $battle->calculateBattle($Attacker,$Defender,$def_wall,$att_tribe,$def_tribe,$residence,$attpop,$defpop,$type,$def_ab,$att_ab1,$att_ab2,$att_ab3,$att_ab4,$att_ab5,$att_ab6,$att_ab7,$att_ab8,$tblevel,$stonemason,$walllevel,0,0,0,$AttackerID,$DefenderID,$AttackerWref,$DefenderWref,$conqureby)); if (!isset($walllevel)) $walllevel = 0; $battlepart = $battle->calculateBattle($Attacker, $Defender, $def_wall, $att_tribe, $def_tribe, $residence, $attpop, $defpop, $type, $def_ab, $att_ab1, $att_ab2, $att_ab3, $att_ab4, $att_ab5, $att_ab6, $att_ab7, $att_ab8, $tblevel, $stonemason, $walllevel, 0, 0, 0, $AttackerID, $DefenderID, $AttackerWref, $DefenderWref, $conqureby, $enforcementarray); @@ -1286,17 +1289,18 @@ class Automation { else $can_destroy = 0; //Catapults and rams management + //TODO: Move this in Battle.php if($isoasis == 0){ if ($type == 3){ if (($data['t7'] - $traped7) > 0){ if($walllevel > 0){ - $newLevel = $battle->CalculateNewBuildingLevel($battlepart['rams']['moral'], - $battlepart['rams']['upgrades'], - $battlepart['rams']['downgrades'], - $walllevel, - $battlepart['rams']['realAttackers'], - $data['t7'], - $battlepart['rams']['strongerBuildings']); + $ramsDamage = $battle->calculateCatapultsDamage($data['t7'], + $battlepart['rams']['upgrades'], + $battlepart['rams']['durability'], + $battlepart['rams']['attackDefenseRatio'], + $battlepart['rams']['strongerBuildings'], + $battlepart['rams']['moraleBonus']); + $newLevel = $battle->calculateNewBuildingLevel($walllevel, $ramsDamage); if ($newLevel == 0){ $info_ram = "".$ram_pic.",Wall destroyed."; diff --git a/GameEngine/Battle.php b/GameEngine/Battle.php index 44df9cd4..cc189a92 100755 --- a/GameEngine/Battle.php +++ b/GameEngine/Battle.php @@ -8,7 +8,7 @@ ## Filename Battle.php ## ## Developed by: Dzoki & Dixie ## ## Fixed by: Shadow ## -## Thanks to: Akakori & Elmar ## +## Thanks to: Akakori, Elmar & Kirilloid ## ## Reworked and Fix by: ronix ## ## Fixed by: InCube - double troops ## ## License: TravianZ Project ## @@ -21,6 +21,21 @@ class Battle { + /** + * + * @author Kirilloid --> https://github.com/kirilloid/travian/blob/master/src/model/base/combat.ts + * @var double The number of attacking catapults: 1 = 100%, 0 = 0% + * + */ + + private $sigma; + + public function __construct(){ + + $this->sigma = function($x) { return ($x > 1 ? 2 - $x ** -1.5 : $x ** 1.5) / 2; }; + + } + public function procSim($post) { global $form; @@ -374,10 +389,10 @@ class Battle { $abcount += 1; // Points catapult the attacker - if(in_array($i,$catapult)) $catp += (int) $Attacker['u'.$i]; + if(in_array($i, $catapult)) $catp += (int) $Attacker['u'.$i]; // Points of the Rams attacker - if(in_array($i,$rams)) $ram += (int) $Attacker['u'.$i]; + if(in_array($i, $rams)) $ram += (int) $Attacker['u'.$i]; $involve += (int) $Attacker['u'.$i]; $units['Att_unit'][$i] = (int) $Attacker['u'.$i]; @@ -459,7 +474,7 @@ class Battle { $result['Defend_points'] = $rdp; $winner = ($rap > $rdp); - // Formula for calculating the Moral + // Formula for calculating the Morale bonus // WW villages aren't affected by this bonus if($attpop > $defpop && !$isWWVillage) { $moralbonus = 1 / round(max(0.667, pow($defpop / $attpop, 0.2 * min(1, $rap / ($rdp > 0 ? $rdp : 1)))), 3); @@ -533,57 +548,54 @@ class Battle { } $result['hero_fealthy'] = $fealthy; } + $ram -= ($winner) ? round($ram * $result[1] / 100) : round($ram * $result[2] / 100); $catp -= ($winner) ? round($catp * $result[1] / 100) : round($catp * $result[2] / 100); } - // Formula for the calculation of catapults needed + if($catp > 0 && $tblevel != 0) { - //catapults upgrades - $upgrades = round(200 * pow(1.0205, $att_ab8) ) / 200; + //Catapults blacksmith upgrades + $upgrades = round(200 * pow(1.0205, $att_ab8)) / 200; - $wctp = pow(($rap / $rdp), 1.5); - $wctp = ($wctp >= 1) ? 1 - 0.5 / $wctp : 0.5 * $wctp; - $wctp *= ($catp + $upgrades); - - //catapults downgrades - $downgrades = ($stonemason > 0 ? $bid34[$stonemason]['attri'] / 100 : 1); - - //catapults moral - $catpMoral = pow($attpop / ($defpop > 0 ? $defpop : 1) , 0.3); - - // New level of the building (only for warsim.php) - $result[3] = $this->calculateNewBuildingLevel($catpMoral, $upgrades, $downgrades, $tblevel, $wctp, $catp, $strongerbuildings); + //Buildings durability + $durability = ($stonemason > 0 ? $bid34[$stonemason]['attri'] / 100 : 1); + + //Calculates the catapults morale bonus + $catpMoraleBonus = min(max(($attpop / ($defpop > 0 ? $defpop : 1)) ** 0.3, 1), 3); + + //New level of the building (only for warsim.php) + $catapultsDamage = $this->calculateCatapultsDamage($catp, $upgrades, $durability, $rap / $rdp, $strongerbuildings, $catpMoraleBonus); + $result[3] = $this->calculateNewBuildingLevel($tblevel, $catapultsDamage); $result[4] = $tblevel; - // Results for Automation.php - $result['catapults']['moral'] = $catpMoral; + //Results for Automation.php $result['catapults']['upgrades'] = $upgrades; - $result['catapults']['downgrades'] = $downgrades; - $result['catapults']['realAttackers'] = $wctp; + $result['catapults']['durability'] = $durability; + $result['catapults']['attackDefenseRatio'] = $rap / $rdp; $result['catapults']['strongerBuildings'] = $strongerbuildings; + $result['catapults']['moraleBonus'] = $catpMoraleBonus; } if($ram > 0 && $walllevel != 0) { + //Rams blacksmith upgrades $upgrades = round(200 * pow(1.0205, $att_ab7)) / 200; - - $wctp = pow(($rap / $rdp), 1.5); - $wctp = ($wctp >= 1) ? 1 - 0.5 / $wctp : 0.5 * $wctp; - $wctp *= ($ram) + $upgrades; - $downgrades = ($stonemason > 0 ? $bid34[$stonemason]['attri'] / 100 : 1); + //Building durability + $durability = ($stonemason > 0 ? $bid34[$stonemason]['attri'] / 100 : 1); // New level of the building (only for warsim.php) - $result[7] = $this->calculateNewBuildingLevel(1, $upgrades, $downgrades, $walllevel, $wctp, $ram, $strongerbuildings * 5); + $ramsDamage = $this->calculateCatapultsDamage($ram, $upgrades, $durability, $rap / $rdp, $strongerbuildings, 1); + $result[7] = $this->calculateNewBuildingLevel($walllevel, $ramsDamage); $result[8] = $walllevel; // Results for Automation.php - $result['rams']['moral'] = 1; $result['rams']['upgrades'] = $upgrades; - $result['rams']['downgrades'] = $downgrades; - $result['rams']['realAttackers'] = $wctp; - $result['rams']['strongerBuildings'] = $strongerbuildings * 5; + $result['rams']['durability'] = $durability; + $result['rams']['attackDefenseRatio'] = $rap / $rdp; + $result['rams']['strongerBuildings'] = $strongerbuildings; + $result['rams']['moraleBonus'] = 1; } $result[6] = pow($rap / ($rdp * $moralbonus > 0 ? $rdp * $moralbonus : 1), $Mfactor); @@ -608,7 +620,7 @@ class Battle { if ($hero_health <= $damage_health || $damage_health > 90){ //hero die - $result['casualties_attacker']['11'] = 1; + $result['casualties_attacker'][11] = 1; mysqli_query($database->dblink,"update " . TB_PREFIX . "hero set `dead` = 1, `health` = 0 where `heroid`=".(int) $hero_id); }else{ mysqli_query($database->dblink,"update " . TB_PREFIX . "hero set `health`=`health`-".(int) $damage_health." where `heroid`=".(int) $hero_id); @@ -735,46 +747,41 @@ class Battle { } /** - * Calculates the new building level, after catapults/rams have damaged it + * @author Kirilloid --> https://github.com/kirilloid/travian/blob/master/src/model/base/combat.ts * - * @param float $moral The catapults/rams battle moral, 1 < moral < 3 => (Total attacker points / Total defender points)^3 - * @param int $upgrades Upgrades of catapults/rams made in the blacksmith - * @param int $downgrades Defender bonuses, such as the stonemason's lodge - * @param int $actualLevel The level of the building before the battle - * @param int $realAttackers Effective catapults/rams involved in the building damage - * @param int $totalAttackers Total catapults/rams sent in the attack - * @param int $strongerBuildings Indicates the defender artifacts bonus against catapults - * @return int Returns the new level of the damaged building + * Calculates the new building level, after damaging it + * + * @param int $oldLevel The old building level + * @param float $damage The damage done by catapults + * @return int Returns the new building level */ - public function CalculateNewBuildingLevel($moral, $upgrades, $downgrades, $actualLevel, $realAttackers, $totalAttackers, $strongerBuildings) - { - if($moral < 1) $moral = 1; - elseif($moral > 3) $moral = 3; - - $needMax = round($this->CalculateNeededCatapults($moral, $upgrades / ($downgrades * $strongerBuildings), $actualLevel)); - if($needMax <= $realAttackers) return 0; - for($i = $actualLevel - 1; $i >= 1; $i--) - { - $need = $this->CalculateNeededCatapults($moral, $upgrades / ($downgrades * $strongerBuildings), $i); - if(min($realAttackers, $totalAttackers) / ($needMax - $need) <= 1) return ++$i; - } - - return $actualLevel; + public function calculateNewBuildingLevel($oldLevel, $damage){ + $damage -= 0.5; + if ($damage < 0) return $oldLevel; + + while ($damage >= $oldLevel && $oldLevel) $damage -= $oldLevel--; + + return $oldLevel; } /** - * Calculate the needed catapults/rams to completely destroy a building/wall + * @author Kirilloid --> https://github.com/kirilloid/travian/blob/master/src/model/base/combat.ts * - * @param float $moral The catapults/rams battle moral - * @param int $upDown (Upgrades / Downgrades / Stronger buildings) ratio of catapults/rams - * @param int $actualLevel The level of the building before the battle - * @return float Returns the needed catapults/rams to destroy a building/wall + * Calculates the damage done by catapults + * + * @param int $catapultsQuantity The quantity of catapults which take part in the attack + * @param double $catapultsUpgrade The catapults upgrade multiplier, affected by the cataputls level in the blacksmith + * @param double $durability The building durability, affected by the stonemason's lodge + * @param double $ADRatio The attack points / defensive points ratio + * @param double $strongerBuildings The artifacts multiplier, which strengthens the building, affected by durability artifacts + * @param double $moraleBonus The defender morale bonus + * @return double Returns the damage done by catapults */ - public function CalculateNeededCatapults($moral, $upDown, $actualLevel) - { - return ($moral * (pow($actualLevel, 2) + $actualLevel + 1) / ($upDown == 0 ? 1 : 8 * $upDown)) + 0.5; + public function calculateCatapultsDamage($catapultsQuantity, $catapultsUpgrade, $durability, $ADRatio, $strongerBuildings, $moraleBonus){ + $catapultsEfficiency = floor($catapultsQuantity / ($durability * $strongerBuildings)); + return 4 * ($this->sigma)($ADRatio) * $catapultsEfficiency * $catapultsUpgrade / $moraleBonus; } }; diff --git a/GameEngine/Database.php b/GameEngine/Database.php index 8ecbeb56..447c5335 100755 --- a/GameEngine/Database.php +++ b/GameEngine/Database.php @@ -855,7 +855,9 @@ class MYSQLi_DB implements IDbConnection { if(!is_array($coordinatesArray[0])) $coordinatesArray = [$coordinatesArray]; $conditions = []; - foreach($coordinatesArray as $coordinate) $conditions[] = "(x = ".$coordinate[0]." AND y = ".$coordinate[1].")"; + foreach($coordinatesArray as $coordinate){ + $conditions[] = "(x = ".round($coordinate[0])." AND y = ".round($coordinate[1]).")"; + } $q = "SELECT id FROM " . TB_PREFIX . "wdata WHERE ".implode(" OR ", $conditions); $result = mysqli_query($this->dblink, $q); diff --git a/GameEngine/Units.php b/GameEngine/Units.php index 80f1e13e..b80ccaac 100755 --- a/GameEngine/Units.php +++ b/GameEngine/Units.php @@ -224,6 +224,7 @@ class Units { $to = $database->getVillage($enforce['from']); $tribe = $database->getUserField($to['owner'], 'tribe', 0); + $start = ($tribe - 1) * 10 + 1; $troopsTime = $this->getWalkingTroopsTime($enforce['from'], $enforce['vref'], $to['owner'], $tribe, $enforce, 1); $time = $database->getArtifactsValueInfluence($from['owner'], $enforce['from'], 2, $troopsTime); diff --git a/Templates/Build/37_land.tpl b/Templates/Build/37_land.tpl index f86daf43..90c4b208 100644 --- a/Templates/Build/37_land.tpl +++ b/Templates/Build/37_land.tpl @@ -4,7 +4,8 @@ $oasisarray = $database->getOasis($village->wid); if(isset($_GET['gid']) && $_GET['gid'] == 37 && isset($_GET['del']) && $database->getOasisField($_GET['del'], 'owner') == $session->uid){ - $database->removeOases($_GET['del']); + $units->returnTroops($village->wid, 1); + $database->removeOases($_GET['del']); header("Location: build.php?id=".$id."&land"); exit; } diff --git a/warsim.php b/warsim.php index 4e3282a9..d8b069c8 100644 --- a/warsim.php +++ b/warsim.php @@ -72,7 +72,7 @@ if(isset($_POST['result'])) { include("Templates/Simulator/res_d".$tar.".tpl"); } echo "

Type of attack: "; - echo $form->getValue('ktyp') == 0? "Normal" : "Raid"; + echo $form->getValue('ktyp') == 0 ? "Normal" : "Raid"; echo "

"; echo "

"; if (isset($_POST['result'][7]) && isset($_POST['result'][8])){