Browse Source

Update BattleEvaluator.cpp

Restored spell-damage-calculations for units that would die from spells.
Xilmi 1 year ago
parent
commit
c838f5d0c2
1 changed files with 49 additions and 1 deletions
  1. 49 1
      AI/BattleAI/BattleEvaluator.cpp

+ 49 - 1
AI/BattleAI/BattleEvaluator.cpp

@@ -731,7 +731,55 @@ bool BattleEvaluator::attemptCastingSpell(const CStack * activeStack)
 
 					ps.value = scoreEvaluator.evaluateExchange(updatedAttack, cachedAttack.turn, *targets, innerCache, state);
 				}
-
+				//! Some units may be dead alltogether. So if they existed before but not now, we know they were killed by the spell
+				for (const auto& unit : all)
+				{
+					if (!unit->isValidTarget())
+						continue;
+					bool isDead = true;
+					for (const auto& remainingUnit : allUnits)
+					{
+						if (remainingUnit->unitId() == unit->unitId())
+							isDead = false;
+					}
+					if (isDead)
+					{
+						auto newHealth = 0;
+						auto oldHealth = vstd::find_or(healthOfStack, unit->unitId(), 0);
+						if (oldHealth != newHealth)
+						{
+							auto damage = std::abs(oldHealth - newHealth);
+							auto originalDefender = cb->getBattle(battleID)->battleGetUnitByID(unit->unitId());
+							auto dpsReduce = AttackPossibility::calculateDamageReduce(
+								nullptr,
+								originalDefender && originalDefender->alive() ? originalDefender : unit,
+								damage,
+								innerCache,
+								state);
+							auto ourUnit = unit->unitSide() == side ? 1 : -1;
+							auto goodEffect = newHealth > oldHealth ? 1 : -1;
+							if (ourUnit * goodEffect == 1)
+							{
+								if (ourUnit && goodEffect && (unit->isClone() || unit->isGhost()))
+									continue;
+								ps.value += dpsReduce * scoreEvaluator.getPositiveEffectMultiplier();
+							}
+							else
+								ps.value -= 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);
+#endif
+						}
+					}
+				}
 				for(const auto & unit : allUnits)
 				{
 					if(!unit->isValidTarget(true))