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(gameclient): a building dead in the fog still blocks placement

Olcay Seygan5c94c34c87e4
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 76bdf9c47..d0dcb50d4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -687,6 +687,7 @@ found and fixed — EA's own, not port damage.**
- Cancelling a plan is silent — nothing was built, so nothing explodes.
- Ground your units have walked stays buildable after they leave it.
- Placing a building no longer shows you the enemy's base. The red cells under the building on your cursor marked every structure on the map, the ones standing in the shroud and the ones going up in the fog since you last looked included, so sweeping the cursor over the dark drew your opponent's foundations for you. The red tint and the outline under the building did the same in the fog, and the building slid flush against walls you had never seen. All of it now goes by what you have seen: a building you scouted still shows where your eyes left it, one you have not shows nothing until you find it. Put a building down on top of one you could not see and the order is turned down, as it always was.
+- The other way round too: an enemy building destroyed while you were not looking still stands in your fog, and placing over it now treats it as standing. The grid used to show bare ground under it and the building on your cursor went green there, which told you it was gone before you had seen it go. Its ground stays red, the building on your cursor will not go down on it and slides flush against it as against any other, until you look there again.
- A plan opens no fog of its own, and your opponent never sees it. Not with a unit parked right next
to the site, and not as a leftover outline in the fog after that unit walks away. Both used to show
an AI's whole base layout before its first worker arrived.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/BuildAssistant.h b/GeneralsMD/Code/GameEngine/Include/Common/BuildAssistant.h
index 4a8288c52..b099ac9ee 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/BuildAssistant.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/BuildAssistant.h
@@ -45,6 +45,7 @@
class ThingTemplate;
class Player;
class Object;
+class GhostObject;
// ------------------------------------------------------------------------------------------------
// this enum is used for the construction percent of objects
@@ -114,6 +115,22 @@ extern Bool BuildAssistant_shroudBlocksBuilding( CellShroudStatus status );
//-------------------------------------------------------------------------------------------------
extern Bool BuildAssistant_isHiddenFromLocalPlayer( const Object *obj );
+//-------------------------------------------------------------------------------------------------
+/** The other half of that picture: a building that died where the local player could not see it
+ * is still drawn for him from its ghost, and its footprint stands until he looks there again.
+ * Fills ghosts with up to maxGhosts such ghosts touching the partition cells within radius of
+ * center and returns how many. Client only, like the test above. */
+//-------------------------------------------------------------------------------------------------
+extern Int BuildAssistant_findDeadGhostsInFog( const Coord3D *center, Real radius,
+ const GhostObject **ghosts, Int maxGhosts );
+
+/// does the footprint a dead building's ghost remembers overlap this shape on the ground?
+extern Bool BuildAssistant_ghostOverlaps( const GhostObject *ghost, const Coord3D *pos,
+ const GeometryInfo &geom, Real angle );
+
+/// does the placement grid wash this pathfinder cell red for that ghost?
+extern Bool BuildAssistant_ghostCoversCell( const GhostObject *ghost, Int cellX, Int cellY );
+
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
class BuildAssistant : public SubsystemInterface
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
index d4a7956f3..93f5747bf 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/BuildAssistant.cpp
@@ -48,6 +48,7 @@
#include "GameClient/TerrainVisual.h"
#include "GameLogic/AI.h"
#include "GameLogic/GameLogic.h"
+#include "GameLogic/GhostObject.h"
#include "GameLogic/PartitionManager.h"
#include "GameLogic/TerrainLogic.h"
#include "GameLogic/AIPathfind.h"
@@ -964,6 +965,23 @@ LegalBuildCode BuildAssistant::isLocationClearOfObjects( const Coord3D *worldPos
}
} // end for, them
+
+ // The other half of IGNORE_UNSEEN: a building that fell in his fog still stands for him, drawn
+ // from its ghost until he looks there again, and the ghost going green over it told him it was
+ // gone. The logic let go of the spot when it died and would take the order, so this is the only
+ // place it is turned down. No bib: a ghost has no drawable to put one under.
+ if( BitTest( options, IGNORE_UNSEEN ) )
+ {
+ enum { MAX_GHOSTS = 16 };
+ const GhostObject *ghosts[ MAX_GHOSTS ];
+ const Int ghostCount = BuildAssistant_findDeadGhostsInFog( &groundPos, range, ghosts, MAX_GHOSTS );
+ for( Int i = 0; i < ghostCount; ++i )
+ {
+ if( BuildAssistant_ghostOverlaps( ghosts[ i ], &groundPos, myBounds, angle ) )
+ return LBC_OBJECTS_IN_THE_WAY;
+ }
+ }
+
return LBC_OK;
}
@@ -997,6 +1015,92 @@ Bool BuildAssistant_isHiddenFromLocalPlayer( const Object *obj )
return status != OBJECTSHROUD_CLEAR && status != OBJECTSHROUD_PARTIAL_CLEAR && status != OBJECTSHROUD_FOGGED;
}
+//-------------------------------------------------------------------------------------------------
+/** PartitionManager::unRegisterObject keeps the partition data of a building that dies while some
+ * player remembers it in the fog: no Object any more, only the ghost, still in the cells its
+ * footprint covered. The ghost holds a snapshot for this player exactly while his fog draws it,
+ * and loses it the moment he sees the spot again. */
+//-------------------------------------------------------------------------------------------------
+Int BuildAssistant_findDeadGhostsInFog( const Coord3D *center, Real radius,
+ const GhostObject **ghosts, Int maxGhosts )
+{
+ const Int me = ThePlayerList->getLocalPlayer()->getPlayerIndex();
+
+ Int loX, loY, hiX, hiY;
+ ThePartitionManager->worldToCell( center->x - radius, center->y - radius, &loX, &loY );
+ ThePartitionManager->worldToCell( center->x + radius, center->y + radius, &hiX, &hiY );
+
+ Int count = 0;
+ for( Int y = loY; y <= hiY; ++y )
+ {
+ for( Int x = loX; x <= hiX; ++x )
+ {
+ PartitionCell *cell = ThePartitionManager->getCellAt( x, y );
+ if( cell == NULL ) // past the map's edge
+ continue;
+
+ for( CellAndObjectIntersection *coi = cell->getFirstCoiInCell(); coi; coi = coi->getNextCoi() )
+ {
+ const PartitionData *pd = coi->getModule();
+ const GhostObject *ghost = pd->getGhostObject();
+ if( pd->getObject() != NULL || ghost == NULL || !ghost->hasSnapShot( me ) )
+ continue;
+
+ // a footprint spans several cells; a handful of ghosts at most, so a linear search
+ Int i = 0;
+ while( i < count && ghosts[ i ] != ghost )
+ ++i;
+ if( i == count && count < maxGhosts )
+ ghosts[ count++ ] = ghost;
+ }
+ }
+ }
+ return count;
+}
+
+//-------------------------------------------------------------------------------------------------
+Bool BuildAssistant_ghostOverlaps( const GhostObject *ghost, const Coord3D *pos,
+ const GeometryInfo &geom, Real angle )
+{
+ const GeometryInfo remembered( ghost->getGeometryType(), ghost->getGeometrySmall(), 1.0f,
+ ghost->getGeometryMajorRadius(), ghost->getGeometryMinorRadius() );
+
+ // on the ground, both of them: the test is three dimensional and only the footprint is asked
+ Coord3D at = *ghost->getParentPosition();
+ Coord3D other = *pos;
+ at.z = other.z = 0.0f;
+ return ThePartitionManager->geomCollidesWithGeom( &at, remembered, ghost->getParentAngle(),
+ &other, geom, angle );
+}
+
+//-------------------------------------------------------------------------------------------------
+/** The cells Pathfinder::internal_classifyObjectFootprint gave the building while it stood, so the
+ * patch looks the same before and after it died. A round one is the pathfinder's own test, a cell
+ * centre within the radius and four tenths of a cell. A box is sampled there from its near edge
+ * and stops short of the far one; a pathfinder cell x spans [10x - 0.5, 10x + 9.5), and counting
+ * the ones the box overlaps by more than a sliver on both axes is near enough that sampling. */
+//-------------------------------------------------------------------------------------------------
+Bool BuildAssistant_ghostCoversCell( const GhostObject *ghost, Int cellX, Int cellY )
+{
+ // the collision test squares a circle off (xy_collideTest_Rect_Circle), the pathfinder does not
+ if( ghost->getGeometryType() != GEOMETRY_BOX )
+ {
+ const Real dx = ( (Real)cellX + 0.5f ) * PATHFIND_CELL_SIZE_F - ghost->getParentPosition()->x;
+ const Real dy = ( (Real)cellY + 0.5f ) * PATHFIND_CELL_SIZE_F - ghost->getParentPosition()->y;
+ const Real reach = ghost->getGeometryMajorRadius() + 0.4f * PATHFIND_CELL_SIZE_F;
+ return dx * dx + dy * dy <= reach * reach;
+ }
+
+ const Real CELL_HALF = 0.44f * PATHFIND_CELL_SIZE_F;
+ const GeometryInfo cell( GEOMETRY_BOX, FALSE, 1.0f, CELL_HALF, CELL_HALF );
+
+ Coord3D centre;
+ centre.x = ( (Real)cellX + 0.45f ) * PATHFIND_CELL_SIZE_F;
+ centre.y = ( (Real)cellY + 0.45f ) * PATHFIND_CELL_SIZE_F;
+ centre.z = 0.0f;
+ return BuildAssistant_ghostOverlaps( ghost, &centre, cell, 0.0f );
+}
+
//-------------------------------------------------------------------------------------------------
/** Rule 16 of Pro Rules for one spot: are the owner's defences round the derrick cluster this spot
* belongs to already at the cap? The largest cluster measured on the game's maps is four derricks,
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 0b34144cc..3594395f5 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -121,6 +121,7 @@
#include "GameLogic/Object.h"
#include "GameLogic/RankInfo.h"
#include "GameLogic/GameLogic.h"
+#include "GameLogic/GhostObject.h"
#include "GameLogic/PartitionManager.h"
#include "GameLogic/PolygonTrigger.h"
#include "GameLogic/TerrainLogic.h"
@@ -5878,6 +5879,24 @@ void InGameUI::snapPlacementToNeighbour( Coord3D *world, const ThingTemplate *wh
neighbours.push_back( standing );
}
+ // and one that died in his fog, which he still sees standing in its ghost. The ghost keeps no
+ // template, so its box is the bare geometry, without a factory's bib.
+ enum { MAX_GHOSTS = 16 };
+ const GhostObject *ghosts[ MAX_GHOSTS ];
+ const Int ghostCount = BuildAssistant_findDeadGhostsInFog( world, searchRadius, ghosts, MAX_GHOSTS );
+ for( Int g = 0; g < ghostCount; g++ )
+ {
+ PlacementBox remembered;
+ remembered.x = ghosts[ g ]->getParentPosition()->x;
+ remembered.y = ghosts[ g ]->getParentPosition()->y;
+ remembered.c = (Real)Cos( ghosts[ g ]->getParentAngle() );
+ remembered.s = (Real)Sin( ghosts[ g ]->getParentAngle() );
+ remembered.halfMajor = ghosts[ g ]->getGeometryMajorRadius();
+ remembered.halfMinor = ghosts[ g ]->getGeometryType() == GEOMETRY_BOX ?
+ ghosts[ g ]->getGeometryMinorRadius() : remembered.halfMajor;
+ neighbours.push_back( remembered );
+ }
+
for( size_t i = 0; i < neighbours.size(); i++ )
{
PlacementBox moved = mine;
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp
index a9ce1f467..aff8bd458 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp
@@ -44,6 +44,7 @@
#include "GameLogic/AIPathfind.h"
#include "GameLogic/TerrainLogic.h"
#include "GameLogic/GameLogic.h"
+#include "GameLogic/GhostObject.h"
#include "GameLogic/Object.h"
#include "GameLogic/PartitionManager.h"
#include "GameLogic/Module/AIUpdate.h"
@@ -1018,7 +1019,8 @@ void W3DInGameUI::drawBuildGrid( void )
* the sync point. The pathfinder marks every structure the moment it is placed, the enemy's in the
* shroud included, so its cells alone drew enemy foundations into the dark: a cell held by
* something the local player has no picture of stays clear here, and so does rubble under the
- * shroud. Water, cliffs and plain impassable ground are the map's own and always show. */
+ * shroud, while a building he still sees standing in the fog after it died is washed from its
+ * ghost. Water, cliffs and plain impassable ground are the map's own and always show. */
//-------------------------------------------------------------------------------------------------
void W3DInGameUI::takeBuildGrid( void )
{
@@ -1056,6 +1058,33 @@ void W3DInGameUI::takeBuildGrid( void )
}
}
+ // A building that died in his fog still stands there for him, drawn from its ghost, but the
+ // pathfinder let go of its cells the moment it died: wash the footprint the ghost remembers, or
+ // bare ground under the ghost tells him it is gone.
+ enum { MAX_GHOSTS = 16 };
+ const GhostObject *ghosts[ MAX_GHOSTS ];
+ const Int ghostCount = BuildAssistant_findDeadGhostsInFog( center, BUILD_GRID_RADIUS * PATHFIND_CELL_SIZE_F,
+ ghosts, MAX_GHOSTS );
+ for( Int g = 0; g < ghostCount; ++g )
+ {
+ const Coord3D *at = ghosts[ g ]->getParentPosition();
+ const Real reach = ghosts[ g ]->getGeometryMajorRadius() + ghosts[ g ]->getGeometryMinorRadius() +
+ PATHFIND_CELL_SIZE_F; // a round one reaches four tenths of a cell past its radius
+ const Int loX = max( 0, REAL_TO_INT_FLOOR( (at->x - reach) / PATHFIND_CELL_SIZE_F ) - m_buildGridCellX );
+ const Int loY = max( 0, REAL_TO_INT_FLOOR( (at->y - reach) / PATHFIND_CELL_SIZE_F ) - m_buildGridCellY );
+ const Int hiX = min( BUILD_GRID_CELLS - 1, REAL_TO_INT_FLOOR( (at->x + reach) / PATHFIND_CELL_SIZE_F ) - m_buildGridCellX );
+ const Int hiY = min( BUILD_GRID_CELLS - 1, REAL_TO_INT_FLOOR( (at->y + reach) / PATHFIND_CELL_SIZE_F ) - m_buildGridCellY );
+
+ for( Int iy = loY; iy <= hiY; ++iy )
+ {
+ for( Int ix = loX; ix <= hiX; ++ix )
+ {
+ if( BuildAssistant_ghostCoversCell( ghosts[ g ], m_buildGridCellX + ix, m_buildGridCellY + iy ) )
+ m_buildGridBlocked[ iy ][ ix ] = TRUE;
+ }
+ }
+ }
+
} // end takeBuildGrid
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 14290a7a7..5c16b7883 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -188,6 +188,7 @@ static int GetSystemMetrics( int which )
#include "GameClient/CommandXlat.h"
#include "Common/ActionManager.h"
#include "GameLogic/PartitionManager.h"
+#include "GameLogic/GhostObject.h"
#include <stdio.h>
#include <string.h>
@@ -8590,6 +8591,63 @@ TEST(a_base_can_be_planned_into_fog_but_not_into_shroud)
CHECK( BuildAssistant_shroudBlocksBuilding( CELLSHROUD_SHROUDED ) == TRUE );
}
+/** A ghost with nothing behind it but its remembered footprint, the shape a building that died in
+ the fog leaves in the partition manager. */
+class FootprintOnlyGhost : public GhostObject
+{
+public:
+ FootprintOnlyGhost( GeometryType type, Real major, Real minor, Real x, Real y, Real angle )
+ {
+ m_parentGeometryType = type;
+ m_parentGeometryIsSmall = FALSE;
+ m_parentGeometryMajorRadius = major;
+ m_parentGeometryminorRadius = minor;
+ m_parentAngle = angle;
+ m_parentPosition.x = x;
+ m_parentPosition.y = y;
+ }
+ virtual void snapShot( int ) {}
+ virtual void updateParentObject( Object *, PartitionData * ) {}
+ virtual void freeSnapShot( int ) {}
+ virtual Bool hasSnapShot( int ) const { return TRUE; }
+};
+
+/** A building that died in the player's fog still stands there for him, drawn from its ghost, and
+ the placement grid washes the cells under that ghost red as it did while the building stood:
+ bare ground there told him it was gone. A pathfinder cell spans [10x - 0.5, 10x + 9.5), and the
+ pathfinder's own footprint takes a cell the near edge reaches and leaves one the far edge only
+ grazes, so a 60 by 40 box from x 70 to 130 covers cells 7 to 12 and not 13. */
+TEST(a_building_dead_in_the_fog_keeps_its_cells_on_the_placement_grid)
+{
+ ThePartitionManager = NEW PartitionManager; // only for its geometry test
+
+ const FootprintOnlyGhost box( GEOMETRY_BOX, 30.0f, 20.0f, 100.0f, 100.0f, 0.0f ); // x 70..130, y 80..120
+ CHECK( BuildAssistant_ghostCoversCell( &box, 7, 8 ) == TRUE );
+ CHECK( BuildAssistant_ghostCoversCell( &box, 12, 11 ) == TRUE );
+ CHECK( BuildAssistant_ghostCoversCell( &box, 6, 10 ) == FALSE );
+ CHECK( BuildAssistant_ghostCoversCell( &box, 13, 10 ) == FALSE );
+ CHECK( BuildAssistant_ghostCoversCell( &box, 10, 7 ) == FALSE );
+ CHECK( BuildAssistant_ghostCoversCell( &box, 10, 12 ) == FALSE );
+
+ // the same box turned a quarter: x 80..120, y 70..130
+ const FootprintOnlyGhost turned( GEOMETRY_BOX, 30.0f, 20.0f, 100.0f, 100.0f, PI / 2.0f );
+ CHECK( BuildAssistant_ghostCoversCell( &turned, 7, 10 ) == FALSE );
+ CHECK( BuildAssistant_ghostCoversCell( &turned, 10, 7 ) == TRUE );
+ CHECK( BuildAssistant_ghostCoversCell( &turned, 10, 12 ) == TRUE );
+
+ // a round one of radius 25 round 200, 200 is the pathfinder's circle, a cell centre within 29
+ // of it, not the square the collision test makes of a circle: the corner cell 17,17 is out
+ const FootprintOnlyGhost round( GEOMETRY_CYLINDER, 25.0f, 25.0f, 200.0f, 200.0f, 0.0f );
+ CHECK( BuildAssistant_ghostCoversCell( &round, 19, 19 ) == TRUE );
+ CHECK( BuildAssistant_ghostCoversCell( &round, 22, 19 ) == TRUE );
+ CHECK( BuildAssistant_ghostCoversCell( &round, 17, 19 ) == TRUE );
+ CHECK( BuildAssistant_ghostCoversCell( &round, 23, 19 ) == FALSE );
+ CHECK( BuildAssistant_ghostCoversCell( &round, 17, 17 ) == FALSE );
+
+ delete ThePartitionManager; // its shutdown asks the global
+ ThePartitionManager = NULL;
+}
+
/** A plan is not a scout. A structure that has been placed but not started opens no shroud at all,
so drawing a base out into the fog cannot be used to see what is standing there. Once the
builder arrives the structure sees as far as its template says, finished or not. */