Browse Source

Update BattleEvaluator.cpp

Make sure trace-message doesn't crash from accessing invalid element.
Xilmi 1 year ago
parent
commit
d4fd4ed670
1 changed files with 25 additions and 9 deletions
  1. 25 9
      AI/BattleAI/BattleEvaluator.cpp

+ 25 - 9
AI/BattleAI/BattleEvaluator.cpp

@@ -768,15 +768,31 @@ bool BattleEvaluator::attemptCastingSpell(const CStack * activeStack)
 							ps.value -= 4 * dpsReduce * scoreEvaluator.getNegativeEffectMultiplier();
 
 #if BATTLE_TRACE_LEVEL >= 1
-						logAi->trace(
-							"Spell %s to %d affects %s (%d), dps: %2f oldHealth: %d newHealth: %d", 
-							ps.spell->getNameTranslated(),
-							ps.dest.at(0).hexValue.hex,
-							unit->creatureId().toCreature()->getNameSingularTranslated(),
-							unit->getCount(),
-							dpsReduce,
-							oldHealth,
-							newHealth);
+						// Ensure ps.dest is not empty before accessing the first element
+						if (!ps.dest.empty()) 
+						{
+							logAi->trace(
+								"Spell %s to %d affects %s (%d), dps: %2f oldHealth: %d newHealth: %d",
+								ps.spell->getNameTranslated(),
+								ps.dest.at(0).hexValue.hex,  // Safe to access .at(0) now
+								unit->creatureId().toCreature()->getNameSingularTranslated(),
+								unit->getCount(),
+								dpsReduce,
+								oldHealth,
+								newHealth);
+						}
+						else 
+						{
+							// Handle the case where ps.dest is empty
+							logAi->trace(
+								"Spell %s has no destination, affects %s (%d), dps: %2f oldHealth: %d newHealth: %d",
+								ps.spell->getNameTranslated(),
+								unit->creatureId().toCreature()->getNameSingularTranslated(),
+								unit->getCount(),
+								dpsReduce,
+								oldHealth,
+								newHealth);
+						}
 #endif
 					}
 				}