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.

feat(gameclient): draw the pathfinder's build grid under a pending structure

Olcay Seygan1fc41920e5ff
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b897abe5b..6fd79bffe 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -165,7 +165,9 @@ what the game looks like.
Alongside it: zoom further out (with the whole map drawn, so the corners are not black), zoom toward
the cursor, 45-degree snapping for camera rotation and building placement, edge scrolling in
-windowed mode, and scrolling speed that no longer depends on your frame rate.
+windowed mode, and scrolling speed that no longer depends on your frame rate. The extra zoom-out was
+pulled back in after playing with it — a quarter again on top of what the original allowed, rather
+than half, which had the camera so high the units stopped reading as units.
**Zoom toward the cursor works now.** It was in the options and it did nothing at all. The camera
does not jump to a new zoom, it glides there over the next fraction of a second - and the game was
@@ -194,6 +196,19 @@ Placement now lands on the same ten-foot squares the pathfinder reasons about, s
an edge, a row of them comes out straight, and the gaps you leave are gaps you meant to leave. Put
`GridBuildPlacement = No` in `Options.ini` for the old to-the-pixel placement.
+**And now you can see those squares.** Pick a structure and the ground under it comes up ruled into
+the grid it is about to land on — a wide patch, enough to plan a whole row against, fading out
+towards its edge rather than stopping on a hard line. Every square you cannot build on — water, a
+cliff, rubble, something already standing there — is crossed out in red as you pass over it, so you
+find the edge of the buildable ground by looking at it instead of by being told *no* after the click.
+
+Two things were quietly out by a hair and are now exact. The squares the game reasons about are not
+where the round numbers are — they sit half a foot off — so every building was going down half a
+foot inside its neighbour's square, which is the sliver the snapping was meant to remove in the
+first place. And a factory's concrete apron is wider than the building on top of it; that apron is
+what you see, and what the game measures when it decides whether two buildings fit, so it is now
+what gets lined up. Buildings sit *on* their squares now rather than near them.
+
**A building now faces the way you drag it.** Press, pull out in the direction you want it to look,
let go — that has always been how you aim a structure, but the game was reading your aim two frames
late, so anything faster than a slow deliberate drag went down pointing somewhere you never chose.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index b420d4b00..f98fc81af 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -457,19 +457,28 @@ public: // ********************************************************************
* the pathfinder actually reasons about. */
virtual void snapPlacementToGrid( Coord3D *world, const ThingTemplate *what, Real angle ) const;
+ /** Where the pathfinder's cell boundaries actually are. It files a world position under
+ * floor( (v + 0.5) / PATHFIND_CELL_SIZE ) (see Pathfinder::internal_classifyObjectFootprint),
+ * so cell k spans [10k - 0.5, 10k + 9.5) - the lines are at k*10 - 0.5, half a unit off the
+ * round numbers. Snapping to the round numbers left every footprint edge half a unit inside
+ * the neighbouring cell, so two buildings that looked flush were both claiming it. */
+ enum { PLACEMENT_CELL = 10 }; ///< PATHFIND_CELL_SIZE, without the pathfinder header
+ static Real placementGridLine( Int k ) { return k * (Real)PLACEMENT_CELL - 0.5f; }
+
/** One axis of that snap. 'extent' is the structure's half-size along this axis, so a footprint
* that is an odd number of cells wide is centred on a cell and an even one on the line between
* two - either way its edges land on grid lines and two neighbours can share one. Inline and
* static so a test can reach it without linking the whole in-game UI. */
static Real snapPlacementAxis( Real v, Real extent )
{
- const Real cell = 10.0f; // PATHFIND_CELL_SIZE_F: one pathfinder cell, one grid square
+ const Real cell = (Real)PLACEMENT_CELL; // one pathfinder cell, one grid square
+ const Real line = placementGridLine( 0 ); // where cell 0 starts
Int cells = REAL_TO_INT_FLOOR( 2.0f * extent / cell + 0.5f );
if( cells < 1 )
cells = 1;
- const Real offset = (cells & 1) ? cell * 0.5f : 0.0f;
+ const Real offset = ((cells & 1) ? cell * 0.5f : 0.0f) + line;
return ((Real)REAL_TO_INT_FLOOR( (v - offset) / cell + 0.5f )) * cell + offset;
}
@@ -749,6 +758,8 @@ protected:
void drawFloatingText( void ); ///< Draw all our floating text
void clearFloatingText( void ); ///< clear the floating text list
+ virtual void drawBuildGrid( void ) { } ///< the pathfinder's build grid under a pending structure
+
void clearWorldAnimations( void ); ///< delete all world animations
void updateAndDrawWorldAnimations( void ); ///< update and draw visible world animations
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 10ec4dcd1..539ee40d6 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1715,6 +1715,9 @@ void InGameUI::preDraw( void )
// handle radius-cursors, if any
handleRadiusCursor();
+ // show the build grid under a structure waiting to be placed
+ drawBuildGrid();
+
// draw the floating text first;
drawFloatingText();
@@ -3401,6 +3404,14 @@ void InGameUI::snapPlacementToGrid( Coord3D *world, const ThingTemplate *what, R
if( geom.getGeomType() != GEOMETRY_BOX )
major = minor = geom.getBoundingCircleRadius();
+ // The ground a structure really takes is not its collision box: BuildAssistant's own clearance
+ // check grows both radii by the factory bib (isLocationClearOfObjects' myBounds), and the bib is
+ // the concrete apron you can see under it. Snapping the bare box left that apron hanging off
+ // the grid by the bib's width, which is what makes a placed building look like it did not
+ // quite sit down on its squares.
+ major += what->getFactoryExtraBibWidth();
+ minor += what->getFactoryExtraBibWidth();
+
const Real c = (Real)fabs( Cos( angle ) );
const Real sn = (Real)fabs( Sin( angle ) );
@@ -3409,6 +3420,7 @@ void InGameUI::snapPlacementToGrid( Coord3D *world, const ThingTemplate *what, R
} // end snapPlacementToGrid
+
//-------------------------------------------------------------------------------------------------
/** Aim the structure sitting on the cursor at 'angle', and keep that heading for the placements
* that follow - the same offset the wheel writes (see rotatePendingPlacement), so a wall aimed by
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DInGameUI.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DInGameUI.h
index be6c695b1..cf44dcb9c 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DInGameUI.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DInGameUI.h
@@ -78,12 +78,19 @@ protected:
virtual void drawMoveHints( View *view ); ///< draw move hint visual feedback
virtual void drawAttackHints( View *view ); ///< draw attack hint visual feedback
virtual void drawPlaceAngle( View *view ); ///< draw place building angle if needed
+ virtual void drawBuildGrid( void ); ///< the pathfinder's build grid under a pending structure
RenderObjClass *m_moveHintRenderObj[ MAX_MOVE_HINTS ];
HAnimClass *m_moveHintAnim[ MAX_MOVE_HINTS ];
RenderObjClass *m_buildingPlacementAnchor;
RenderObjClass *m_buildingPlacementArrow;
+ /** The build grid is a few thousand short lines, and TheDisplay->drawLine is a Reset() and a
+ * Render() each - one draw call per segment (EA's own @todo on it). One renderer of our own,
+ * filled in a single pass and drawn once, is what lets the patch be wide enough to lay a base
+ * out on. */
+ Render2DClass *m_buildGridRender;
+
}; // end class W3DInGameUI
#endif // end __W3DINGAMEUI_H_
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp
index c9efa2adc..f5d7ed16d 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DInGameUI.cpp
@@ -32,6 +32,8 @@
#include "Common/GlobalData.h"
#include "Common/ThingTemplate.h"
#include "Common/ThingFactory.h"
+#include "GameLogic/AI.h"
+#include "GameLogic/AIPathfind.h"
#include "GameLogic/TerrainLogic.h"
#include "GameLogic/GameLogic.h"
#include "GameLogic/Object.h"
@@ -282,6 +284,7 @@ W3DInGameUI::W3DInGameUI()
m_buildingPlacementAnchor = NULL;
m_buildingPlacementArrow = NULL;
+ m_buildGridRender = NULL;
} // end W3DInGameUI
@@ -303,6 +306,13 @@ W3DInGameUI::~W3DInGameUI()
REF_PTR_RELEASE( m_buildingPlacementAnchor );
REF_PTR_RELEASE( m_buildingPlacementArrow );
+ if( m_buildGridRender )
+ {
+ m_buildGridRender->Reset();
+ delete m_buildGridRender;
+ m_buildGridRender = NULL;
+ }
+
} // end ~W3DInGameUI
// loadText ===================================================================
@@ -444,6 +454,131 @@ void W3DInGameUI::draw( void )
} // end draw
+//-------------------------------------------------------------------------------------------------
+/** Draw the pathfinder's own cell grid under the structure sitting on the cursor, and cross out
+ * the cells it cannot go on. GridBuildPlacement snaps a footprint's edges to these very lines
+ * (see snapPlacementToGrid), so being able to see them is the difference between guessing at a
+ * flush row of buildings and laying one out.
+ *
+ * Only the cells around the cursor are drawn. The whole map's worth would be a wall of lines,
+ * and the ones being aimed at are the ones worth seeing - so the lines also fade out towards the
+ * edge of the patch instead of ending on a hard square.
+ *
+ * "Cannot build" here is the pathfinder cell's own type: water, a cliff, rubble, an existing
+ * structure, plain impassable. It deliberately does not run the full isLocationLegalToBuild for
+ * every cell - that is a per-structure query (build radius, shroud, supply proximity) and a
+ * couple of hundred of them a frame is not worth it. The ghost's own red tint already answers
+ * that question for the one spot the cursor is actually on. */
+//-------------------------------------------------------------------------------------------------
+void W3DInGameUI::drawBuildGrid( void )
+{
+ // the grid you see is the grid you snap to: with the snap off it would mean nothing
+ if( m_pendingPlaceType == NULL || TheGlobalData->m_gridBuildPlacement == FALSE )
+ return;
+ if( m_placeIcon == NULL || m_placeIcon[ 0 ] == NULL )
+ return;
+ if( TheTacticalView == NULL || TheDisplay == NULL || TheTerrainLogic == NULL || TheAI == NULL )
+ return;
+
+ Pathfinder *pathfinder = TheAI->pathfinder();
+ if( pathfinder == NULL )
+ return;
+
+ const Coord3D *center = m_placeIcon[ 0 ]->getPosition();
+ if( center == NULL )
+ return;
+
+ // how many cells each way around the cursor we light up
+ enum { GRID_RADIUS = 14 };
+ enum { GRID_CELLS = GRID_RADIUS * 2 + 1, GRID_POINTS = GRID_CELLS + 1 };
+
+ // the pathfinder's own cell indexing, so the lines drawn are the lines it reasons about
+ const Int cellX = REAL_TO_INT_FLOOR( (center->x + 0.5f) / PATHFIND_CELL_SIZE_F ) - GRID_RADIUS;
+ const Int cellY = REAL_TO_INT_FLOOR( (center->y + 0.5f) / PATHFIND_CELL_SIZE_F ) - GRID_RADIUS;
+
+ // project every corner of the patch once, rather than four times per cell
+ ICoord2D screen[ GRID_POINTS ][ GRID_POINTS ];
+ Bool onScreen[ GRID_POINTS ][ GRID_POINTS ];
+ Int ix, iy;
+ for( iy = 0; iy < GRID_POINTS; ++iy )
+ {
+ for( ix = 0; ix < GRID_POINTS; ++ix )
+ {
+ Coord3D world;
+ world.x = placementGridLine( cellX + ix );
+ world.y = placementGridLine( cellY + iy );
+ world.z = TheTerrainLogic->getGroundHeight( world.x, world.y );
+ onScreen[ iy ][ ix ] = TheTacticalView->worldToScreen( &world, &screen[ iy ][ ix ] );
+ }
+ }
+
+ if( m_buildGridRender == NULL )
+ m_buildGridRender = NEW Render2DClass;
+ if( m_buildGridRender == NULL )
+ return;
+
+ m_buildGridRender->Reset();
+ m_buildGridRender->Enable_Texturing( FALSE );
+ m_buildGridRender->Set_Coordinate_Range( RectClass( 0, 0, (Real)TheDisplay->getWidth(),
+ (Real)TheDisplay->getHeight() ) );
+
+ // the lines fade to nothing at the edge of the patch, measured from the cursor's own cell
+ const Real fadeFrom = (Real)GRID_RADIUS;
+
+ // the grid itself
+ for( iy = 0; iy < GRID_POINTS; ++iy )
+ {
+ for( ix = 0; ix < GRID_POINTS; ++ix )
+ {
+ // distance in cells from the middle, on the axis the segment runs along
+ const Real dx = (Real)fabs( ix - GRID_RADIUS - 0.5f );
+ const Real dy = (Real)fabs( iy - GRID_RADIUS - 0.5f );
+
+ const Real d = ( dx > dy ) ? dx : dy;
+ const Int alpha = REAL_TO_INT( 0x50 * ( 1.0f - d / fadeFrom ) );
+ if( alpha <= 0 )
+ continue;
+
+ const UnsignedInt color = (alpha << 24) | 0x00FFFFFF;
+
+ if( ix + 1 < GRID_POINTS && onScreen[ iy ][ ix ] && onScreen[ iy ][ ix + 1 ] )
+ m_buildGridRender->Add_Line( Vector2( (Real)screen[ iy ][ ix ].x, (Real)screen[ iy ][ ix ].y ),
+ Vector2( (Real)screen[ iy ][ ix + 1 ].x, (Real)screen[ iy ][ ix + 1 ].y ),
+ 1.0f, color );
+
+ if( iy + 1 < GRID_POINTS && onScreen[ iy ][ ix ] && onScreen[ iy + 1 ][ ix ] )
+ m_buildGridRender->Add_Line( Vector2( (Real)screen[ iy ][ ix ].x, (Real)screen[ iy ][ ix ].y ),
+ Vector2( (Real)screen[ iy + 1 ][ ix ].x, (Real)screen[ iy + 1 ][ ix ].y ),
+ 1.0f, color );
+ }
+ }
+
+ // and an X through every cell a structure cannot stand on
+ for( iy = 0; iy < GRID_CELLS; ++iy )
+ {
+ for( ix = 0; ix < GRID_CELLS; ++ix )
+ {
+ PathfindCell *cell = pathfinder->getCell( LAYER_GROUND, cellX + ix, cellY + iy );
+ if( cell == NULL || cell->getType() == PathfindCell::CELL_CLEAR )
+ continue;
+
+ if( !onScreen[ iy ][ ix ] || !onScreen[ iy + 1 ][ ix + 1 ] ||
+ !onScreen[ iy ][ ix + 1 ] || !onScreen[ iy + 1 ][ ix ] )
+ continue;
+
+ m_buildGridRender->Add_Line( Vector2( (Real)screen[ iy ][ ix ].x, (Real)screen[ iy ][ ix ].y ),
+ Vector2( (Real)screen[ iy + 1 ][ ix + 1 ].x, (Real)screen[ iy + 1 ][ ix + 1 ].y ),
+ 1.0f, 0xAAFF3030 );
+ m_buildGridRender->Add_Line( Vector2( (Real)screen[ iy ][ ix + 1 ].x, (Real)screen[ iy ][ ix + 1 ].y ),
+ Vector2( (Real)screen[ iy + 1 ][ ix ].x, (Real)screen[ iy + 1 ][ ix ].y ),
+ 1.0f, 0xAAFF3030 );
+ }
+ }
+
+ m_buildGridRender->Render();
+
+} // end drawBuildGrid
+
//-------------------------------------------------------------------------------------------------
/** draw 2d selection region on screen */
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 71b4fb2c6..8a0293717 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -988,16 +988,21 @@ TEST(placement_grid_snap_puts_footprint_edges_on_cell_lines)
{
const Real cell = 10.0f;
+ /* The pathfinder files a position under floor((v + 0.5) / 10), so its cell lines sit at
+ * k*10 - 0.5, not at k*10. Everything below is measured against those. */
+ CHECK_NEAR(InGameUI::placementGridLine(0), -0.5f, 0.0001f);
+ CHECK_NEAR(InGameUI::placementGridLine(3), 29.5f, 0.0001f);
+
/* 3 cells wide (extent 15) - centre on a cell centre, whatever it started as */
- CHECK_NEAR(InGameUI::snapPlacementAxis(0.0f, 15.0f), 5.0f, 0.0001f);
- CHECK_NEAR(InGameUI::snapPlacementAxis(4.0f, 15.0f), 5.0f, 0.0001f);
- CHECK_NEAR(InGameUI::snapPlacementAxis(9.0f, 15.0f), 5.0f, 0.0001f);
- CHECK_NEAR(InGameUI::snapPlacementAxis(11.0f, 15.0f), 15.0f, 0.0001f);
+ CHECK_NEAR(InGameUI::snapPlacementAxis(0.0f, 15.0f), 4.5f, 0.0001f);
+ CHECK_NEAR(InGameUI::snapPlacementAxis(4.0f, 15.0f), 4.5f, 0.0001f);
+ CHECK_NEAR(InGameUI::snapPlacementAxis(9.0f, 15.0f), 4.5f, 0.0001f);
+ CHECK_NEAR(InGameUI::snapPlacementAxis(11.0f, 15.0f), 14.5f, 0.0001f);
/* 4 cells wide (extent 20) - centre on a cell line */
- CHECK_NEAR(InGameUI::snapPlacementAxis(4.0f, 20.0f), 0.0f, 0.0001f);
- CHECK_NEAR(InGameUI::snapPlacementAxis(6.0f, 20.0f), 10.0f, 0.0001f);
- CHECK_NEAR(InGameUI::snapPlacementAxis(123.0f, 20.0f), 120.0f, 0.0001f);
+ CHECK_NEAR(InGameUI::snapPlacementAxis(4.0f, 20.0f), -0.5f, 0.0001f);
+ CHECK_NEAR(InGameUI::snapPlacementAxis(6.0f, 20.0f), 9.5f, 0.0001f);
+ CHECK_NEAR(InGameUI::snapPlacementAxis(123.0f, 20.0f), 119.5f, 0.0001f);
/* a snap never moves anything more than half a cell */
for (Int i = 0; i < 400; i++)
@@ -1008,18 +1013,26 @@ TEST(placement_grid_snap_puts_footprint_edges_on_cell_lines)
CHECK(fabsf(InGameUI::snapPlacementAxis(v, 20.0f) - v) <= cell / 2.0f + 0.0001f);
}
- /* and both edges of the footprint end up on cell lines, odd width and even alike */
+ /* and both edges of the footprint end up on the pathfinder's own cell lines, odd width and
+ * even alike - which is what makes the cells a structure blocks exactly the cells it covers */
for (Int cells = 1; cells <= 8; cells++)
{
Real extent = cells * cell * 0.5f;
Real centre = InGameUI::snapPlacementAxis(37.3f, extent);
- Real lo = (centre - extent) / cell;
- Real hi = (centre + extent) / cell;
+ Real lo = (centre - extent - InGameUI::placementGridLine(0)) / cell;
+ Real hi = (centre + extent - InGameUI::placementGridLine(0)) / cell;
CHECK_NEAR(lo, (Real)REAL_TO_INT_FLOOR(lo + 0.5f), 0.0001f);
CHECK_NEAR(hi, (Real)REAL_TO_INT_FLOOR(hi + 0.5f), 0.0001f);
+
+ /* the cell the pathfinder puts each edge in is the first and last cell covered: no
+ * half-unit sliver of a neighbouring cell left over for the building next door */
+ CHECK_EQ(REAL_TO_INT_FLOOR((centre - extent + 0.5f) / cell),
+ REAL_TO_INT_FLOOR((centre - extent + 0.5f + 0.1f) / cell));
+ CHECK_EQ(REAL_TO_INT_FLOOR((centre + extent + 0.5f) / cell) - 1,
+ REAL_TO_INT_FLOOR((centre + extent + 0.5f - 0.1f) / cell));
}
/* a template with no footprint worth the name still lands on a whole cell, not on nothing */
- CHECK_NEAR(InGameUI::snapPlacementAxis(13.0f, 0.0f), 15.0f, 0.0001f);
+ CHECK_NEAR(InGameUI::snapPlacementAxis(13.0f, 0.0f), 14.5f, 0.0001f);
}