← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/bug-1388028 into lp:widelands

 

GunChleoc has proposed merging lp:~widelands-dev/widelands/bug-1388028 into lp:widelands.

Requested reviews:
  TiborB (tiborb95)
Related bugs:
  Bug #1388028 in widelands: "Unable to load saved game"
  https://bugs.launchpad.net/widelands/+bug/1388028

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/bug-1388028/+merge/240357

Fixed division by zero in AI that can come up when loading a savegame.

@Tibor: could you please make sure that this is still intended behaviour for the AI and remove the NOCOM comments when you're done?
-- 
https://code.launchpad.net/~widelands-dev/widelands/bug-1388028/+merge/240357
Your team Widelands Developers is subscribed to branch lp:~widelands-dev/widelands/bug-1388028.
=== modified file 'src/ai/defaultai.cc'
--- src/ai/defaultai.cc	2014-10-31 20:49:02 +0000
+++ src/ai/defaultai.cc	2014-11-01 14:08:59 +0000
@@ -698,7 +698,7 @@
 		}
 	}
 
-	// folowing is done allways (regardless of military or not)
+	// the following is done always (regardless of military or not)
 
 	// we get immovables with higher radius
 	immovables.clear();
@@ -710,6 +710,7 @@
 	field.military_presence_ = 0;
 
 	for (uint32_t i = 0; i < immovables.size(); ++i) {
+
 		const BaseImmovable& base_immovable = *immovables.at(i).object;
 
 		// testing if it is enemy-owned field
@@ -3089,20 +3090,27 @@
 	const Game::GeneralStatsVector& genstats = game().get_general_statistics();
 	for (uint8_t j = 1; j <= plr_in_game; ++j) {
 		if (pn == j) {
-			player_attackable[j - 1] = false;
+			player_attackable.at(j - 1) = false;
 			continue;
 		}
 
-		if (genstats[j - 1].miltary_strength.back() == 0) {
-			// to avoid improbable zero division
-			player_attackable[j - 1] = true;
-			any_attackable = true;
-		} else if ((genstats[pn - 1].miltary_strength.back() * 100 /
-		            genstats[j - 1].miltary_strength.back()) > treshold_ratio) {
-			player_attackable[j - 1] = true;
-			any_attackable = true;
-		} else {
-			player_attackable[j - 1] = false;
+		try {
+			// Avoid division by zero
+			if (genstats.at(j - 1).miltary_strength.empty() ||
+				 genstats.at(j - 1).miltary_strength.back() == 0) {
+				player_attackable.at(j - 1) = true;
+				any_attackable = true;
+			// Check threshold
+			} else if ((genstats.at(pn - 1).miltary_strength.back() * 100 /
+							genstats.at(j - 1).miltary_strength.back()) > treshold_ratio) {
+				player_attackable.at(j - 1) = true;
+				any_attackable = true;
+			} else {
+				player_attackable.at(j - 1) = false;
+			}
+		} catch (const std::out_of_range&) {
+			player_attackable.at(j - 1) = true;
+			any_attackable = true;
 		}
 	}
 


Follow ups