From f5e54f4aa171be216e71b3f5c120f3167d5cc610 Mon Sep 17 00:00:00 2001 From: Dan Hemberger <846186+hemberger@users.noreply.github.com> Date: Wed, 16 Oct 2019 14:36:10 -0700 Subject: [PATCH] Improve Smr{Port,Planet}::checkForDowngrade (#779) * Determine the number of downgrade chances up front to simplify the looping condition. * For SmrPlanet, return an array instead of an HTML string so that we can report total structure losses. (E.g., instead of repeating "This team destroys 1 generator" 3 times, it will simply say "This team destroys 3 generators" once.) --- lib/Default/AbstractSmrPort.class.php | 16 ++++++++++------ lib/Default/SmrPlanet.class.php | 16 ++++++++++++---- .../includes/PlanetTraderTeamCombatResults.inc | 6 +++--- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/Default/AbstractSmrPort.class.php b/lib/Default/AbstractSmrPort.class.php index 7a73c5d29..5febde7f6 100644 --- a/lib/Default/AbstractSmrPort.class.php +++ b/lib/Default/AbstractSmrPort.class.php @@ -568,16 +568,20 @@ public function removePortGood($goodID) { $this->db->query('DELETE FROM port_has_goods WHERE ' . $this->SQL . ' AND good_id=' . $this->db->escapeNumber($goodID) . ';'); $this->db->query('DELETE FROM route_cache WHERE game_id=' . $this->db->escapeNumber($this->getGameID())); } - - public function checkForDowngrade($damageDone) { - $downgrades = 0; - for (;$damageDone > self::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE; $damageDone -= self::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE) { + + /** + * Returns the number of port level downgrades due to damage taken. + */ + public function checkForDowngrade($damage) : int { + $numDowngrades = 0; + $numChances = floor($damage / self::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE); + for ($i = 0; $i < $numChances; $i++) { if (mt_rand(1, 100) <= self::CHANCE_TO_DOWNGRADE && $this->level > 1) { - ++$downgrades; + ++$numDowngrades; $this->doDowngrade(); } } - return $downgrades; + return $numDowngrades; } protected function selectAndRemoveGood($goodClass) { diff --git a/lib/Default/SmrPlanet.class.php b/lib/Default/SmrPlanet.class.php index 9f8d0b889..a66578b69 100644 --- a/lib/Default/SmrPlanet.class.php +++ b/lib/Default/SmrPlanet.class.php @@ -1170,12 +1170,16 @@ public function &shootPlayers(array $targetPlayers) { return $results; } - public function &checkForDowngrade($damage) { - $results = ''; + /** + * Returns an array of structure losses due to damage taken. + */ + public function checkForDowngrade($damage) : array { + $results = []; // For every 70 damage there is a 15% chance of destroying a structure. // Which structure is destroyed depends on the ratio of buildings and // the time it takes to build them. - for ($i = 0; $damage > self::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE; $damage -= self::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE) { + $numChances = floor($damage / self::DAMAGE_NEEDED_FOR_DOWNGRADE_CHANCE); + for ($i = 0; $i < $numChances; $i++) { // Stop if the planet has no more buildlings if ($this->getLevel() == 0) { break; @@ -1189,7 +1193,11 @@ public function &checkForDowngrade($damage) { $destroyID = getWeightedRandom($chanceFactors); $this->destroyBuilding($destroyID, 1); $this->checkForExcessDefense(); - $results .= 'This team destroys 1 ' . $this->getStructureTypes($destroyID)->name() . '.
'; + if (isset($results[$destroyID])) { + $results[$destroyID] += 1; + } else { + $results[$destroyID] = 1; + } } } return $results; diff --git a/templates/Default/engine/Default/includes/PlanetTraderTeamCombatResults.inc b/templates/Default/engine/Default/includes/PlanetTraderTeamCombatResults.inc index 2ae5fe13b..3eb103bfc 100644 --- a/templates/Default/engine/Default/includes/PlanetTraderTeamCombatResults.inc +++ b/templates/Default/engine/Default/includes/PlanetTraderTeamCombatResults.inc @@ -131,6 +131,6 @@ foreach($TraderTeamCombatResults['Traders'] as $AccountID => $TraderResults) { } $TotalDamage = $TraderTeamCombatResults['TotalDamage']; ?> This fleet 0){ ?>hits for a total of damage in this round of combatdoes no damage this round. You call that a fleet? They need a better recruiter.
\ No newline at end of file +foreach ($TraderTeamCombatResults['Downgrades'] as $structureID => $numDestroyed) { ?> + This team destroys getStructureTypes($structureID)->name(), $numDestroyed); ?>.