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): the AI reads the influence map, through the fog
Olcay Seygan63887712fb8c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 70da39d6d..2b2d88721 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -29,6 +29,12 @@ found and fixed — EA's own, not port damage.**- It scouts. There was no such thing as scouting in the computer opponent - it never needed to look, because it could already see everything. One cheap unit now tours the enemy start positions and keeps going round for the rest of the match, replaced out of spare change when it dies. Every difficulty scouts; an opponent that never looks reads as broken, not as easy.- Nor does it read your base off the map. Where your base is, what it is worth, which supply dock to expand to and where to aim a superweapon all came off a walk of your object list, in the shroud, from the first second of the match. The computer now only counts what it can see, plus the buildings it has already found - buildings do not walk away. Before it has scouted you, all it knows is where you started, which is on the map preview anyway.+## The computer comes in where you are thin++- The game has kept a value-and-danger map of every square of the battlefield, per player, since 2003. It has a query interface and even a debug view. Nothing in the computer opponent had ever read it - its only users were two map-script actions almost nobody used.+- At the top level the opponent now aims its attacks at where your money is rather than at the middle of your buildings, which is also the middle of your defences.+- It reads that map through the fog like everything else: only ground it has actually seen counts. An opponent that has not scouted you still attacks the old way.+## The computer expands, and defends what it takes- It never decided to expand. The machinery was all there - place a supply centre beside a pile, pick a pile worth taking, send a team to sit on it - and every piece of it only ran when a map script said so. An opponent that ran its starting piles dry simply stopped earning.diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AISkirmishPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AISkirmishPlayer.hindex 3d7277f8e..f6acee398 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AISkirmishPlayer.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AISkirmishPlayer.h@@ -75,6 +75,11 @@ public: // AIPlayer interface methods.virtual Player *getAiEnemy(void); ///< Solo AI attacks based on scripting. Only skirmish auto-acquires an enemy at this point. jba.+ /** B4: where to send an attack, off the engine's own influence map instead of at the geometric+ * centre of the enemy's buildings - which is the middle of his base and so the middle of his+ * guns. FALSE when this AI has not seen anything worth attacking yet. */+ Bool influenceMapAttackGoal(Coord3D *goal);+protected:// snapshot methodsdiff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.hindex f865d4d54..0298c3c55 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.h@@ -1535,6 +1535,15 @@ public:Real getGroundOrStructureHeight(Real posx, Real posy);void getMostValuableLocation( Int playerIndex, UnsignedInt whichPlayerTypes, ValueOrThreat valType, Coord3D *outLocation );++ /** The same question, asked only of ground this player has actually seen.+ *+ * The influence map is kept per player and does no fog checking of its own, so the plain+ * getMostValuableLocation above answers out of the whole map - which is fine for a script action+ * the map author wrote deliberately, and is omniscience for an AI that is supposed to have to+ * scout (AI-ROADMAP.md A2, B4). Returns FALSE when nothing it can see has any value, which is+ * the honest answer for an AI that has not looked yet. */+ Bool getMostValuableVisibleLocation( Int playerIndex, UnsignedInt whichPlayerTypes, ValueOrThreat valType, Coord3D *outLocation );void getNearestGroupWithValue( Int playerIndex, UnsignedInt whichPlayerTypes, ValueOrThreat valType, const Coord3D *sourceLocation,Int valueRequired, Bool greaterThan, Coord3D *outLocation );diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cppindex f7edc1309..2a63224fb 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp@@ -476,6 +476,39 @@ Int AISkirmishPlayer::getMyEnemyPlayerIndex(void) {/**Get the AI's enemy. Recalc if it has been a while (5 seconds.)*/+//----------------------------------------------------------------------------------------------------------+/** Where to send an attack, off the engine's own influence map.+ *+ * This is B4: the map is already there, per player, with cash and threat per cell, with a query+ * API and even an in-game debug view - and until now its only users were two script actions. The+ * C++ AI never touched it, and aimed every attack at the geometric centre of the enemy's+ * buildings, which is the middle of his base and therefore the middle of his defences.+ *+ * The enemy's most valuable cell is where his economy is, which is usually not where his guns+ * are. Fog-masked, so it is somewhere this AI has actually seen; FALSE means it has not looked+ * yet and the caller keeps the old aim point.+ *+ * Off below the top rung: this is what Merciless adds.+ */+//----------------------------------------------------------------------------------------------------------+Bool AISkirmishPlayer::influenceMapAttackGoal( Coord3D *goal )+{+ if( !getSkillProfile()->m_useInfluenceMapForAttackLane )+ return FALSE;+ if( ThePartitionManager == NULL || goal == NULL )+ return FALSE;++ Coord3D found;+ if( !ThePartitionManager->getMostValuableVisibleLocation( m_player->getPlayerIndex(), ALLOW_ENEMIES,+ VOT_CashValue, &found ) )+ return FALSE;++ goal->x = found.x;+ goal->y = found.y;+ goal->z = TheTerrainLogic->getGroundHeight( goal->x, goal->y );+ return TRUE;+}+void AISkirmishPlayer::acquireEnemy(void){Player *bestEnemy = NULL;@@ -632,10 +665,12 @@ void AISkirmishPlayer::buildAIBaseDefenseStructure(const AsciiString &thingName,goalPos = *way->getLocation();} else {if (flank) return;- Region2D bounds;- getPlayerStructureBounds(&bounds, getMyEnemyPlayerIndex(), FALSE, m_player->getPlayerIndex());- goalPos.x = bounds.lo.x + bounds.width()/2;- goalPos.y = bounds.lo.y + bounds.height()/2;+ if (!influenceMapAttackGoal(&goalPos)) {+ Region2D bounds;+ getPlayerStructureBounds(&bounds, getMyEnemyPlayerIndex(), FALSE, m_player->getPlayerIndex());+ goalPos.x = bounds.lo.x + bounds.width()/2;+ goalPos.y = bounds.lo.y + bounds.height()/2;+ }}Coord2D offset;offset.x = goalPos.x-m_baseCenter.x;@@ -1184,10 +1219,12 @@ Bool AISkirmishPlayer::computeSuperweaponTarget(const SpecialPowerTemplate *poweif (way) {goalPos = *way->getLocation();} else {- Region2D bounds;- getPlayerStructureBounds(&bounds, getMyEnemyPlayerIndex(), FALSE, m_player->getPlayerIndex());- goalPos.x = bounds.lo.x + bounds.width()/2;- goalPos.y = bounds.lo.y + bounds.height()/2;+ if (!influenceMapAttackGoal(&goalPos)) {+ Region2D bounds;+ getPlayerStructureBounds(&bounds, getMyEnemyPlayerIndex(), FALSE, m_player->getPlayerIndex());+ goalPos.x = bounds.lo.x + bounds.width()/2;+ goalPos.y = bounds.lo.y + bounds.height()/2;+ }}Coord2D offset;offset.x = goalPos.x-m_baseCenter.x;diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cppindex 6c1574d56..dcfa5ad0d 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cpp@@ -4831,6 +4831,65 @@ void PartitionManager::getMostValuableLocation( Int playerIndex, UnsignedInt whi);}+//-------------------------------------------------------------------------------------------------+Bool PartitionManager::getMostValuableVisibleLocation( Int playerIndex, UnsignedInt whichPlayerTypes,+ ValueOrThreat valType, Coord3D *outLocation )+{+ if (!outLocation)+ return FALSE;++ PlayerMaskType playerMask = ThePlayerList->getPlayersWithRelationship(playerIndex, whichPlayerTypes);+ if (playerMask == 0)+ return FALSE;++ PlayerMaskType allPlayerMasks[MAX_PLAYER_COUNT] = { 0 };+ Int totalPlayerCount = ThePlayerList->getPlayerCount();+ Int i;+ for (i = 0; i < totalPlayerCount; ++i) {+ Player *player = ThePlayerList->getNthPlayer(i);+ if (player)+ allPlayerMasks[i] = player->getPlayerMask();+ }++ const Int cellCount = m_cellCountX * m_cellCountY;+ Int greatestValueCell = -1;+ Int maxCellValue = 0; // zero, not -1: a cell worth nothing is not an answer++ for (i = 0; i < cellCount; ++i) {+ Int cellValue = 0;+ for (Int player = 0; player < MAX_PLAYER_COUNT; ++player) {+ if (BitTest(allPlayerMasks[player], playerMask)) {+ if (valType == VOT_CashValue) {+ cellValue += m_cells[i].getCashValue(player);+ } else {+ cellValue += m_cells[i].getThreatValue(player);+ }+ }+ }++ if (cellValue <= maxCellValue)+ continue;++ // ... and only if this player has seen the ground it is standing on+ Coord3D where;+ where.set(m_cells[i].getCellX() * TheGlobalData->m_partitionCellSize,+ m_cells[i].getCellY() * TheGlobalData->m_partitionCellSize, 0);+ if (getPropShroudStatusForPlayer(playerIndex, &where) == OBJECTSHROUD_SHROUDED)+ continue;++ maxCellValue = cellValue;+ greatestValueCell = i;+ }++ if (greatestValueCell == -1)+ return FALSE;++ outLocation->set(m_cells[greatestValueCell].getCellX() * TheGlobalData->m_partitionCellSize,+ m_cells[greatestValueCell].getCellY() * TheGlobalData->m_partitionCellSize,+ 0);+ return TRUE;+}+//-------------------------------------------------------------------------------------------------void PartitionManager::getNearestGroupWithValue( Int playerIndex, UnsignedInt whichPlayerTypes, ValueOrThreat valType,const Coord3D *sourceLocation, Int valueRequired, Bool greaterThan, Coord3D *outLocation )