Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
feat(gameengine): rank attack-move targets by threat, not distance
Olcay Seygan93bf49e0bd99
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cf0ac9cf5..5db5c17f1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -35,6 +35,7 @@ found and fixed — EA's own, not port damage.**- Aircraft make their pass, fly home, rearm and resume your order.- Nothing walks off the map chasing a target that keeps retreating.- A unit turns onto whatever is actually shooting it, then resumes the advance.+- Otherwise it picks the worst thing in range: anything armed before anything that is not, then the more dangerous of the two, and worth fades with distance so it is never the far one it walks the field for. A group ordered through a base no longer stops for the first dozer it passes while the artillery beside it keeps firing.- Ctrl on the attack-move click no longer shells the ground instead.- Troop Crawlers wait for their squad to climb back in, up to ten seconds.- One man who cannot get back aboard no longer parks the Crawler for the rest of the attack move.diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.hindex ce02906b7..36a538ab8 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h@@ -264,7 +264,8 @@ public:IGNORE_INSIGNIFICANT_BUILDINGS = 1 << 2,ATTACK_BUILDINGS = 1 << 3,WITHIN_ATTACK_RANGE = 1 << 4,- UNFOGGED = 1 << 5+ UNFOGGED = 1 << 5,+ PREFER_HIGH_THREAT = 1 << 6 ///< pick the biggest threat in range, not the nearest thing};Object *findClosestEnemy( const Object *me, Real range, UnsignedInt qualifiers,const AttackPriorityInfo *info=NULL, PartitionFilter *optionalFilter=NULL);diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cppindex 4fcf36feb..1dc59960e 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp@@ -602,6 +602,57 @@ static void priorityFunc(Object *obj, void *userData)}+//-----------------------------------------------------------------------------+/**+ * How much an enemy is worth shooting before the others, with no Object involved so+ * test_gameengine can link straight to it - the same trick as AIAttackMove_shouldRetaliate+ * in AIStates.cpp.+ *+ * Attack move used to hand out whatever the scan found first, which is the nearest thing:+ * a group ordered through an enemy base stopped for the first dozer it passed while the+ * artillery beside it kept firing. Ranking by threat fixes the order of business.+ *+ * 'ThreatValue' is the INI field the threat map already feeds on, so a mod that sets it+ * decides this outright; the shipped Zero Hour INI never sets it (it is absent from every+ * file in INIZH.big), so build cost stands in - EA's own price is the only per-unit measure+ * of dangerousness the shipped data has, and it tracks it well enough (an Overlord costs+ * five times a Humvee).+ *+ * Anything that can shoot back at *us* outranks anything that cannot, whatever it cost: a+ * Command Center is worth more than a Gattling tank and is still not what is killing us.+ * That is what AI_THREAT_ARMED_BONUS buys - it is larger than any build cost in the game, so+ * the two groups never interleave, and distance never moves a target out of its group either.+ *+ * Within a group distance does decide, because a threat we have to cross the field to reach is+ * not the one hurting us now: the worth of a target falls off with range, halving every+ * 'distanceModifier' world units away. That is the same knob the scripted attack priorities use+ * (AI INI's AttackPriorityDistanceModifier, 100.0 in the shipped data), so both notions of "worth+ * shooting" fade over the same distance. It is 0 until that INI is read - and the ctor default -+ * so a zero (or a negative, or a distance behind us) means no falloff at all rather than a+ * divide by zero.+ */+enum { AI_THREAT_ARMED_BONUS = 1000000 }; ///< larger than any build cost in the game++Int AI_threatScore( Int templateThreatValue, Int buildCost, Bool threatensMe,+ Real dist, Real distanceModifier )+{+ Int score = (templateThreatValue > 0) ? templateThreatValue : buildCost;+ if (score < 1)+ score = 1; // a free civilian car is still worth more than nothing at all++ if (distanceModifier > 0.0f && dist > 0.0f)+ {+ score = REAL_TO_INT( score * distanceModifier / (distanceModifier + dist) );+ if (score < 1)+ score = 1; // however far off it is, it still beats finding nothing+ }++ if (threatensMe)+ score += AI_THREAT_ARMED_BONUS;++ return score;+}+//-----------------------------------------------------------------------------/*** Return the closest enemy, according to the qualifiers.@@ -696,6 +747,39 @@ Object *AI::findClosestEnemy( const Object *me, Real range, UnsignedInt qualifieif (info == NULL || info == TheScriptEngine->getDefaultAttackInfo()){+ if (qualifiers & PREFER_HIGH_THREAT)+ {+ // Biggest threat in range wins; the iteration is sorted near to far, so a strict+ // improvement keeps the nearest of equal threats - which is the whole priority+ // order the caller wants: what shoots us (its own check, before we are called),+ // then what is worth shooting, then what is closest.+ Real distanceModifier = TheAI->getAiData()->m_attackPriorityDistanceModifier;+ Object *bestThreat = NULL;+ Int bestScore = 0;+ ObjectIterator *threatIter = ThePartitionManager->iterateObjectsInRange(me, range, FROM_BOUNDINGSPHERE_2D, filters, ITER_SORTED_NEAR_TO_FAR);+ MemoryPoolObjectHolder threatHolder(threatIter);+ for (Object *theEnemy = threatIter->first(); theEnemy; theEnemy = threatIter->next())+ {+ Bool threatensMe = theEnemy->isAbleToAttack(); // cheap bit test, and required before the call below+ if (threatensMe)+ {+ CanAttackResult r = theEnemy->getAbleToAttackSpecificObject( ATTACK_NEW_TARGET, me, CMD_FROM_AI );+ threatensMe = (r == ATTACKRESULT_POSSIBLE || r == ATTACKRESULT_POSSIBLE_AFTER_MOVING);+ }++ Real dist = sqrt( ThePartitionManager->getDistanceSquared(me, theEnemy, FROM_BOUNDINGSPHERE_2D) );+ Int score = AI_threatScore( theEnemy->getTemplate()->getThreatValue(),+ theEnemy->getTemplate()->calcCostToBuild( theEnemy->getControllingPlayer() ),+ threatensMe, dist, distanceModifier );+ if (score > bestScore)+ {+ bestScore = score;+ bestThreat = theEnemy;+ }+ }+ return bestThreat;+ }+// No additional attack info, so just return the closest one.Object* o = ThePartitionManager->getClosestObject( me, range, FROM_BOUNDINGSPHERE_2D, filters );return o;diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cppindex 1eb3c3696..38284d7d1 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp@@ -4715,6 +4715,17 @@ Object* AIUpdateInterface::getNextMoodTarget( Bool calledByAI, Bool calledDuringflags |= AI::UNFOGGED;}+ //+ // A caller that closes with what it finds picks the biggest threat in range instead of the+ // nearest thing (see AI_threatScore). Walking up to a dozer while the artillery next to it+ // keeps firing is only sensible for a unit that cannot move to a better target - and this+ // caller can.+ //+ if (allowOutOfWeaponRangeTargets)+ {+ flags |= AI::PREFER_HIGH_THREAT;+ }+Object *newVictim = TheAI->findClosestEnemy(obj, rangeToFindWithin, flags, getAttackInfo());/*diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex c66cef1d3..5453cac37 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -980,6 +980,55 @@ TEST(attack_move_turns_on_whoever_is_shooting_it)CHECK( !AIAttackMove_shouldRetaliate( 0xffffffff, 0xfffffff0, WINDOW, false, true ) );}+/* AI.cpp: on attack move the scan ranks what it finds by threat instead of handing+ back the nearest thing. */+extern Int AI_threatScore( Int templateThreatValue, Int buildCost, Bool threatensMe,+ Real dist, Real distanceModifier );++TEST(attack_move_shoots_the_biggest_threat_first)+{+ const Real MOD = 100.0f; /* AttackPriorityDistanceModifier, shipped value */+ const Real HERE = 0.0f;++ /* a mod that sets ThreatValue decides outright; build cost only stands in for it. */+ CHECK( AI_threatScore( 50, 4000, false, HERE, MOD ) > AI_threatScore( 40, 5000, false, HERE, MOD ) );++ /* the shipped INI sets ThreatValue nowhere, so cost has to do the ranking:+ an Overlord-priced tank over a Humvee-priced one. */+ CHECK( AI_threatScore( 0, 1200, true, HERE, MOD ) > AI_threatScore( 0, 700, true, HERE, MOD ) );++ /* anything that can shoot back outranks anything that cannot, whatever it cost -+ a Command Center is worth more than a Gattling tank and is still not what is+ killing us - and no distance may move a target out of its group, or a unit would+ walk past the artillery firing at it to reach a far off barracks. */+ CHECK( AI_threatScore( 0, 1, true, HERE, MOD ) > AI_threatScore( 0, 5000, false, HERE, MOD ) );+ CHECK( AI_threatScore( 1, 0, true, HERE, MOD ) > AI_threatScore( 9999, 0, false, HERE, MOD ) );+ CHECK( AI_threatScore( 0, 1, true, 9999.0f, MOD ) > AI_threatScore( 0, 5000, false, HERE, MOD ) );++ /* free civilian junk still scores, otherwise a scan that found only that would+ report "nothing here" and the unit would walk past what it was told to clear. */+ CHECK( AI_threatScore( 0, 0, false, HERE, MOD ) > 0 );+ CHECK( AI_threatScore( 0, 0, false, 9999.0f, MOD ) > 0 );++ /* worth halves every modifier of distance. */+ CHECK_EQ( AI_threatScore( 0, 1000, false, MOD, MOD ), 500 );+ CHECK_EQ( AI_threatScore( 0, 1000, false, 3.0f*MOD, MOD ), 250 );++ /* so the same unit closer up wins, and a big enough threat still outranks a cheap+ one underfoot: an Overlord a modifier away beats a Ranger at our feet. */+ CHECK( AI_threatScore( 0, 1200, true, 10.0f, MOD ) > AI_threatScore( 0, 1200, true, 200.0f, MOD ) );+ CHECK( AI_threatScore( 0, 2000, true, MOD, MOD ) > AI_threatScore( 0, 225, true, HERE, MOD ) );++ /* equal threats at equal range must tie exactly: the scan is sorted near to far and+ keeps the first of a tie, which is how "then the closest one" gets honoured. */+ CHECK_EQ( AI_threatScore( 0, 900, true, 50.0f, MOD ), AI_threatScore( 0, 900, true, 50.0f, MOD ) );++ /* the modifier is 0 until the AI INI is read, and a target can measure as behind us+ from a bounding sphere: neither may divide by zero or invert the ranking. */+ CHECK_EQ( AI_threatScore( 0, 1000, false, 500.0f, 0.0f ), 1000 );+ CHECK_EQ( AI_threatScore( 0, 1000, false, -5.0f, MOD ), 1000 );+}+//////////////////////////////////////////////////////////////////////////////// Particle ground collision//