Skip to content

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.

fix(gameengine): ai knows buildings from logic memory, not the draw loop

Olcay Seygan9d79ffa43362
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.h
index ea3883931..03257e51d 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/PartitionManager.h
@@ -356,7 +356,7 @@ public:
void removeCashValue( Int playerIndex, UnsignedInt cashValue );
void invalidateShroudedStatusForAllCois(Int playerIndex);
- void updateSeenStructures(Int playerIndex); ///< this cell just changed for that player: who kept or lost sight of which structure
+ void updateSeenStructures(Int playerIndex, CellShroudStatus oldShroud, CellShroudStatus newShroud); ///< this cell just changed for that player: who kept or lost sight of which structure
#ifdef PM_CACHE_TERRAIN_HEIGHT
inline Real getLoTerrain() const { return m_loTerrainZ; }
@@ -533,6 +533,7 @@ public:
void invalidateShroudedStatusForAllPlayers();
Bool isInSightOf(Int playerIndex); ///< is any cell it touches clear for that player, read straight from the cells
+ Bool isFullyShroudedFor(Int playerIndex); ///< is every cell it touches shrouded for that player
ObjectShroudStatus getShroudedStatus(Int playerIndex);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 28c746222..61e731a4a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -86,12 +86,12 @@
/** Does 'observerNdx' know this thing is there?
*
- * The partition manager already draws exactly the line wanted here. A player's shroud status for
- * an object is SHROUDED until that player has seen it; once seen, an *immobile* object stays
- * FOGGED when the vision leaves it (PartitionData::friend_calcActualShroudedStatus) while anything
- * that can move goes back to SHROUDED. So "not SHROUDED" already means "I can see it now, or it
- * is a building I have seen and buildings do not walk away" - which is the whole information model
- * the AI needs, with no memory of its own to keep, save or desync.
+ * "I can see it now, or it is a building I have seen and buildings do not walk away" - which is
+ * the whole information model the AI needs. Object::isUnknownTo draws that line from the cells
+ * and each structure's memory of who last saw it, both kept on the logic's own frames. It used to
+ * be getShroudedStatus != SHROUDED, which draws the same line but decides "has seen it" from
+ * whichever code happens to ask while the building is in view: the drawing loop, run for every
+ * player only on an observer's machine, so two machines could think different things.
*
* observerNdx < 0 is the old omniscient answer, for callers that are not one player's thinking. */
static Bool observerKnowsAbout( const Object *obj, Int observerNdx )
@@ -100,7 +100,7 @@ static Bool observerKnowsAbout( const Object *obj, Int observerNdx )
return FALSE;
if( observerNdx < 0 )
return TRUE;
- return obj->getShroudedStatus( observerNdx ) != OBJECTSHROUD_SHROUDED;
+ return !obj->isUnknownTo( observerNdx );
}
/** What a unit is worth in a fight, for every decision here that has to weigh one force against
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
index 7c62cccaa..3cb5c0856 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp
@@ -1960,7 +1960,16 @@ Bool Object::isUnknownTo( Int playerIndex ) const
if( m_partitionData == NULL )
return FALSE;
- return !m_partitionData->isInSightOf( playerIndex ) && getSeenStateFor( playerIndex ) == NULL;
+ if( m_partitionData->isInSightOf( playerIndex ) || getSeenStateFor( playerIndex ) != NULL )
+ return FALSE;
+
+ // A neutral building under fog rather than shroud is drawn for the player without ever having
+ // been in sight: a skirmish map opens fogged for everyone, so every supply dock on it is there
+ // to see from the first frame. getShroudedStatus draws the same one. Without this line the
+ // computer never knew the dock beside its own base, and built no supply centre all match.
+ const Player *player = ThePlayerList->getNthPlayer( playerIndex );
+ return !( isKindOf( KINDOF_IMMOBILE ) && player->getRelationship( getTeam() ) == NEUTRAL &&
+ !m_partitionData->isFullyShroudedFor( playerIndex ) );
}
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cpp
index 75bfbb95d..409c7b4a0 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/Object/PartitionManager.cpp
@@ -1287,9 +1287,12 @@ void PartitionCell::invalidateShroudedStatusForAllCois(Int playerIndex)
//-----------------------------------------------------------------------------
/** Called only from the logic's own look and shroud edges, never from the local player's display
- * refresh, so every machine keeps the same memories on the same frame. */
+ * refresh, so every machine keeps the same memories on the same frame. Only a cell leaving sight
+ * can take the last of a structure out of view, so that is the one edge that remembers; shroud
+ * laid over a cell nobody was looking at never makes a player remember a building they did not
+ * see. Shroud laid over all of it wipes the memory, as it wipes the ghost. */
//-----------------------------------------------------------------------------
-void PartitionCell::updateSeenStructures(Int playerIndex)
+void PartitionCell::updateSeenStructures(Int playerIndex, CellShroudStatus oldShroud, CellShroudStatus newShroud)
{
for (CellAndObjectIntersection* coi = m_firstCoiInCell; coi; coi = coi->getNextCoi())
{
@@ -1300,9 +1303,9 @@ void PartitionCell::updateSeenStructures(Int playerIndex)
if (object == NULL || !object->isKindOf(KINDOF_STRUCTURE) || object->isKindOf(KINDOF_ALWAYS_VISIBLE))
continue;
- if (module->isInSightOf(playerIndex))
+ if (newShroud == CELLSHROUD_CLEAR || module->isFullyShroudedFor(playerIndex))
object->forgetAsSeenBy(playerIndex);
- else
+ else if (oldShroud == CELLSHROUD_CLEAR && !module->isInSightOf(playerIndex))
object->rememberAsSeenBy(playerIndex);
}
}
@@ -1328,7 +1331,7 @@ void PartitionCell::addLooker(Int playerIndex)
{
// On an edge trigger, tell all objects to think about their shroudedness
invalidateShroudedStatusForAllCois( playerIndex );
- updateSeenStructures( playerIndex );
+ updateSeenStructures( playerIndex, oldShroud, newShroud );
if( playerIndex == ThePlayerList->getLocalPlayer()->getPlayerIndex() )
{
@@ -1365,7 +1368,7 @@ void PartitionCell::removeLooker(Int playerIndex)
{
// On an edge trigger, tell all objects to think about their shroudedness
invalidateShroudedStatusForAllCois( playerIndex );
- updateSeenStructures( playerIndex );
+ updateSeenStructures( playerIndex, oldShroud, newShroud );
if( playerIndex == ThePlayerList->getLocalPlayer()->getPlayerIndex() )
{
@@ -1393,7 +1396,7 @@ void PartitionCell::addShrouder( Int playerIndex )
{
// On an edge trigger, tell all objects to think about their shroudedness
invalidateShroudedStatusForAllCois( playerIndex );
- updateSeenStructures( playerIndex );
+ updateSeenStructures( playerIndex, oldShroud, newShroud );
// and update the client if we are on the local player
if( playerIndex == ThePlayerList->getLocalPlayer()->getPlayerIndex() )
@@ -1648,6 +1651,18 @@ Bool PartitionData::isInSightOf(Int playerIndex)
return FALSE;
}
+//-----------------------------------------------------------------------------
+Bool PartitionData::isFullyShroudedFor(Int playerIndex)
+{
+ CellAndObjectIntersection* coi = m_coiArray;
+ for (Int i = m_coiInUseCount; i; --i, ++coi)
+ {
+ if (coi->getCell()->getShroudStatusForPlayer(playerIndex) != CELLSHROUD_SHROUDED)
+ return FALSE;
+ }
+ return TRUE;
+}
+
//-----------------------------------------------------------------------------
// only used to restore state after map border resizing and/or xfer!
void PartitionData::friend_setShroudednessPrevious(Int playerIndex, ObjectShroudStatus status)