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
refactor(gamelogic): pick the hunt target without sorting the map
Olcay Seygan276862c9d61f
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cppindex fdd38f577..37d2060c2 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp@@ -830,6 +830,28 @@ Int AI_threatScore( Int templateThreatValue, Int buildCost, Bool threatensMe,return score;}+//-----------------------------------------------------------------------------+/**+ * Whether a candidate of the scripted-priority search below takes the lead, Object-free so+ * test_gameengine can link to it.+ *+ * That search used to sort every enemy on the map near to far, a stable merge sort over the+ * gathered list, and let a candidate lead only on a higher priority after distance or an equal+ * one with a higher priority before it. So the pick was the best by those two, then the smaller+ * distance, then whichever the gathered list held first. One pass over the unsorted list that+ * compares on all of it and keeps the incumbent on a full tie picks the same Object. A hunting+ * unit asks for this over the whole map once a second, and the sort was 7% of its time.+ */+Bool AI_attackPriorityTakesLead( Int modPriority, Int curPriority, Real distSqr,+ Int bestModPriority, Int bestCurPriority, Real bestDistSqr )+{+ if (modPriority != bestModPriority)+ return modPriority > bestModPriority;+ if (curPriority != bestCurPriority)+ return curPriority > bestCurPriority;+ return distSqr < bestDistSqr;+}+//-----------------------------------------------------------------------------/*** Return the closest enemy, according to the qualifiers.@@ -1009,9 +1031,13 @@ Object *AI::findClosestEnemy( const Object *me, Real range, UnsignedInt qualifieObject *bestEnemy = NULL;Int effectivePriority=0;Int actualPriority=0;- ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange(me, range, FROM_BOUNDINGSPHERE_2D, gatherFilters, ITER_SORTED_NEAR_TO_FAR, enemyPlayers);+ Real bestDistSqr=0.0f;+ // unsorted, see AI_attackPriorityTakesLead; each entry carries the distance the gather measured,+ // which is what getDistanceSquared would measure again+ SimpleObjectIterator *iter = ThePartitionManager->iterateObjectsInRange(me, range, FROM_BOUNDINGSPHERE_2D, gatherFilters, ITER_FASTEST, enemyPlayers);MemoryPoolObjectHolder holder(iter);- for (Object *theEnemy = iter->first(); theEnemy; theEnemy = iter->next())+ Real distSqr;+ for (Object *theEnemy = iter->firstWithNumeric(&distSqr); theEnemy; theEnemy = iter->nextWithNumeric(&distSqr)){Int curPriority = info->getPriority(theEnemy->getTemplate());if (curPriority == 0)@@ -1029,18 +1055,17 @@ Object *AI::findClosestEnemy( const Object *me, Real range, UnsignedInt qualifie}}- Real distSqr = ThePartitionManager->getDistanceSquared(me, theEnemy, FROM_BOUNDINGSPHERE_2D);Real dist = sqrt(distSqr);Int modifier = floatToIntAsMsvc(dist/TheAI->getAiData()->m_attackPriorityDistanceModifier);Int modPriority = curPriority-modifier;if (modPriority < 1)modPriority = 1;- const Bool takesLead = modPriority > effectivePriority ||- (modPriority == effectivePriority && curPriority > actualPriority);- if (takesLead && AI_filtersAllow(filters + numGatherFilters, theEnemy))+ if (AI_attackPriorityTakesLead(modPriority, curPriority, distSqr, effectivePriority, actualPriority, bestDistSqr)+ && AI_filtersAllow(filters + numGatherFilters, theEnemy)){effectivePriority = modPriority;actualPriority = curPriority;+ bestDistSqr = distSqr;bestEnemy = theEnemy;}}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex ed895fef7..2d3156df7 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -3205,6 +3205,74 @@ TEST(attack_move_shoots_the_biggest_threat_first)CHECK_EQ( AI_threatScore( 0, 1000, false, -5.0f, MOD ), 1000 );}+/* AI.cpp: the scripted-priority search a hunter runs over the whole map picks in one pass over+ the unsorted list what the near-to-far sort and the lead loop over it picked, ties included,+ or every replay moves. */+extern Bool AI_attackPriorityTakesLead( Int modPriority, Int curPriority, Real distSqr,+ Int bestModPriority, Int bestCurPriority, Real bestDistSqr );++TEST(attack_priority_pick_needs_no_sort)+{+ enum { N = 24 };+ /* few distances, so equal distances and equal priorities after distance come up often */+ const Real DIST[] = { 0.0f, 50.0f, 100.0f, 110.0f, 200.0f, 500.0f };+ UnsignedInt seed = 12345;+ for (Int round = 0; round < 2000; ++round)+ {+ Int modP[N], curP[N];+ Real distSqr[N];+ Bool allowed[N];+ for (Int i = 0; i < N; ++i)+ {+ seed = seed * 1664525u + 1013904223u;+ const Real dist = DIST[(seed >> 16) % 6];+ curP[i] = (Int)((seed >> 8) % 5) + 1;+ distSqr[i] = dist * dist;+ allowed[i] = ((seed >> 24) & 3) != 0;+ modP[i] = curP[i] - (Int)(dist / 100.0f);+ if (modP[i] < 1)+ modP[i] = 1;+ }++ /* the old way: a stable sort near to far, then the lead on a higher (modP, curP) */+ Int order[N];+ for (Int i = 0; i < N; ++i)+ {+ Int j = i;+ for ( ; j > 0 && distSqr[order[j - 1]] > distSqr[i]; --j)+ order[j] = order[j - 1];+ order[j] = i;+ }+ Int sortedPick = -1, e = 0, a = 0;+ for (Int k = 0; k < N; ++k)+ {+ const Int i = order[k];+ if ((modP[i] > e || (modP[i] == e && curP[i] > a)) && allowed[i])+ {+ e = modP[i];+ a = curP[i];+ sortedPick = i;+ }+ }++ Int onePassPick = -1, bestMod = 0, bestCur = 0;+ Real bestDistSqr = 0.0f;+ for (Int i = 0; i < N; ++i)+ {+ if (AI_attackPriorityTakesLead(modP[i], curP[i], distSqr[i], bestMod, bestCur, bestDistSqr) && allowed[i])+ {+ bestMod = modP[i];+ bestCur = curP[i];+ bestDistSqr = distSqr[i];+ onePassPick = i;+ }+ }+ CHECK_EQ( onePassPick, sortedPick );+ if (onePassPick != sortedPick)+ break;+ }+}+//////////////////////////////////////////////////////////////////////////////// Particle ground collision//