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): snap building placement to the pathfinder's grid

Olcay Seyganaa528bfbae6e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4ba076ce0..49486f95d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -178,6 +178,13 @@ that corner, not in the middle of the screen.
notch, and the next building you place keeps the same facing, so a wall or a line of bunkers goes
down straight without aiming each piece by hand.
+**Buildings snap to the grid the game itself thinks in** (experimental, on by default). Put two
+structures down next to each other by eye and they used to end up a few feet out of line, leaving a
+stripe of ground between them too narrow to walk a soldier through but too wide to look deliberate.
+Placement now lands on the same ten-foot squares the pathfinder reasons about, so neighbours share
+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.
+
**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.
@@ -207,6 +214,23 @@ slopes like theirs do.
If you would rather have the old flat blobs back — they are cheaper to draw — put
`UseShadowVolumesForSkins = No` in the game's `GameData.ini`.
+Two more things had no shadow at all. A Scud, a Tomahawk, a rocket, a bomb falling out of a
+bomber — every one of them crossed the map with nothing underneath it, and there was no way to tell
+from the screen whether a missile was about to clear a ridge or plough into it. They all have a
+shadow now, sized from the missile itself, and it runs along the ground beneath the thing as it
+flies: you can watch a Scud's shadow climb a hill ahead of the Scud.
+
+And the smoke. A burning building, a dust plume, the black column off a wrecked tank — the smoke
+hung over the terrain in full daylight with clean bright ground beneath it. Now a big soft cloud
+darkens the ground under itself, and the patch drifts and fades exactly as the smoke does, so a fire
+that dies out takes its shade with it. Only real clouds get one: bullet trails, muzzle flashes and
+sparks are too small and too short-lived to earn it, and fire and glows are still light rather than
+shade. It is one soft patch per plume rather than a shadow for every puff, so a battlefield full of
+smoke costs no more than a battlefield with one fire.
+
+Both are switchable the same way: `ShadowsForProjectiles = No` and `ShadowsForParticles = No` in
+`GameData.ini`.
+
## It does not crash
- **It used to crash at the main menu.** Two blocks of hand-written assembly, untouched since 2003,
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
index 2fed7dfee..0bc91378e 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
@@ -127,6 +127,8 @@ public:
Bool m_useShadowVolumes;
Bool m_useShadowVolumesForSkins; // "UseShadowVolumesForSkins": also cast volume shadows off skinned meshes
Bool m_useShadowDecals;
+ Bool m_shadowsForProjectiles; // "ShadowsForProjectiles": missiles and bombs that have no shadow of their own get a decal one
+ Bool m_shadowsForParticles; // "ShadowsForParticles": big alpha-blended particle clouds drop a soft blob on the ground
Bool m_particleGroundBounce; // "-particlebounce": terrain collision on for every particle system
Int m_textureReductionFactor; //how much to cut texture resolution: 2 is half, 3 is quarter, etc.
Bool m_enableBehindBuildingMarkers;
@@ -329,6 +331,7 @@ public:
Bool m_edgeScrollInWindowedMode; ///< allow screen-edge scrolling while running in a window
Bool m_snapBuildPlacementTo45; ///< quantize the drag-to-rotate build placement angle to 45 degrees
Bool m_snapCameraRotateTo45; ///< quantize the camera heading to 45 degrees when a middle-drag rotate ends
+ Bool m_gridBuildPlacement; ///< quantize structure placement to the pathfinder's build grid
Bool m_middleMousePans; ///< middle-button drag pans the camera instead of rotating it
Bool m_zoomToCursor; ///< the mouse wheel zooms toward whatever the cursor is over
Bool m_showHudOverlay; ///< draw the fps / elapsed time / income line in the corner
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/UserPreferences.h b/GeneralsMD/Code/GameEngine/Include/Common/UserPreferences.h
index 4aa8cec17..e93520b41 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/UserPreferences.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/UserPreferences.h
@@ -112,6 +112,7 @@ public:
Bool getEdgeScrollInWindowedMode(void);
Bool getSnapBuildPlacementTo45(void);
Bool getSnapCameraRotateTo45(void);
+ Bool getGridBuildPlacement(void);
Bool getMiddleMousePans(void);
Bool getZoomToCursor(void);
Bool getShowHudOverlay(void);
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index 1b51dec55..b420d4b00 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -452,6 +452,27 @@ public: // ********************************************************************
return ((Real)REAL_TO_INT_FLOOR( angle / step + 0.5f )) * step;
}
+ /** Experimental GridBuildPlacement: quantize a structure's position to the pathfinder's own
+ * build grid, so that structures put down by eye line up with each other and with the cells
+ * the pathfinder actually reasons about. */
+ virtual void snapPlacementToGrid( Coord3D *world, const ThingTemplate *what, Real angle ) const;
+
+ /** 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
+
+ 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;
+
+ return ((Real)REAL_TO_INT_FLOOR( (v - offset) / cell + 0.5f )) * cell + offset;
+ }
virtual void setPlacementAngle( Real angle ); ///< aim the structure on the cursor, and keep that heading for the next one
virtual Bool rotatePendingPlacement( Int steps ); ///< turn the structure on the cursor by 45 degrees a step
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameLOD.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameLOD.cpp
index 668dc9888..8754646c5 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameLOD.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameLOD.cpp
@@ -368,6 +368,7 @@ void GameLODManager::init(void)
TheWritableGlobalData->m_edgeScrollInWindowedMode = optionPref.getEdgeScrollInWindowedMode();
TheWritableGlobalData->m_snapBuildPlacementTo45 = optionPref.getSnapBuildPlacementTo45();
TheWritableGlobalData->m_snapCameraRotateTo45 = optionPref.getSnapCameraRotateTo45();
+ TheWritableGlobalData->m_gridBuildPlacement = optionPref.getGridBuildPlacement();
TheWritableGlobalData->m_middleMousePans = optionPref.getMiddleMousePans();
TheWritableGlobalData->m_zoomToCursor = optionPref.getZoomToCursor();
TheWritableGlobalData->m_showHudOverlay = optionPref.getShowHudOverlay();
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
index b78da0d85..b67419d76 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
@@ -99,6 +99,8 @@ GlobalData* GlobalData::m_theOriginal = NULL;
{ "UseShadowVolumes", INI::parseBool, NULL, offsetof( GlobalData, m_useShadowVolumes ) },
{ "UseShadowVolumesForSkins", INI::parseBool, NULL, offsetof( GlobalData, m_useShadowVolumesForSkins ) },
{ "UseShadowDecals", INI::parseBool, NULL, offsetof( GlobalData, m_useShadowDecals ) },
+ { "ShadowsForProjectiles", INI::parseBool, NULL, offsetof( GlobalData, m_shadowsForProjectiles ) },
+ { "ShadowsForParticles", INI::parseBool, NULL, offsetof( GlobalData, m_shadowsForParticles ) },
{ "TextureReductionFactor", INI::parseInt, NULL, offsetof( GlobalData, m_textureReductionFactor ) },
{ "UseBehindBuildingMarker", INI::parseBool, NULL, offsetof( GlobalData, m_enableBehindBuildingMarkers ) },
{ "WaterPositionX", INI::parseReal, NULL, offsetof( GlobalData, m_waterPositionX ) },
@@ -185,6 +187,7 @@ GlobalData* GlobalData::m_theOriginal = NULL;
{ "EdgeScrollInWindowedMode", INI::parseBool, NULL, offsetof( GlobalData, m_edgeScrollInWindowedMode ) },
{ "SnapBuildPlacementTo45", INI::parseBool, NULL, offsetof( GlobalData, m_snapBuildPlacementTo45 ) },
{ "SnapCameraRotateTo45", INI::parseBool, NULL, offsetof( GlobalData, m_snapCameraRotateTo45 ) },
+ { "GridBuildPlacement", INI::parseBool, NULL, offsetof( GlobalData, m_gridBuildPlacement ) },
{ "MiddleMousePans", INI::parseBool, NULL, offsetof( GlobalData, m_middleMousePans ) },
{ "ZoomToCursor", INI::parseBool, NULL, offsetof( GlobalData, m_zoomToCursor ) },
{ "ShowHudOverlay", INI::parseBool, NULL, offsetof( GlobalData, m_showHudOverlay ) },
@@ -642,6 +645,8 @@ GlobalData::GlobalData()
m_useShadowVolumes = FALSE;
m_useShadowVolumesForSkins = TRUE; //on by default: the shipped INI has no entry for it
m_useShadowDecals = FALSE;
+ m_shadowsForProjectiles = TRUE; //on by default: the shipped INI has no entry for it
+ m_shadowsForParticles = TRUE; //on by default: the shipped INI has no entry for it
m_textureReductionFactor = -1;
m_enableBehindBuildingMarkers = TRUE;
m_scriptDebug = FALSE;
@@ -1004,6 +1009,7 @@ GlobalData::GlobalData()
m_edgeScrollInWindowedMode = TRUE;
m_snapBuildPlacementTo45 = TRUE;
m_snapCameraRotateTo45 = TRUE;
+ m_gridBuildPlacement = TRUE;
// middle-drag stays a rotate by default (it snaps to 45 degrees on release); pan is opt-in
m_middleMousePans = FALSE;
m_zoomToCursor = TRUE;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp
index 69c0761fd..7f2bde769 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/OptionsMenu.cpp
@@ -769,6 +769,19 @@ Bool OptionPreferences::getSnapCameraRotateTo45(void)
return stricmp(it->second.str(), "yes") == 0;
}
+Bool OptionPreferences::getGridBuildPlacement(void)
+{
+ // Experimental Options.ini key. Retail places a structure wherever the cursor happens to be,
+ // to the pixel; with this on the footprint lands on the pathfinder's own 10-unit build grid, so
+ // two buildings put down by eye end up sharing an edge instead of leaving a stripe of ground
+ // too narrow to walk through.
+ OptionPreferences::const_iterator it = find("GridBuildPlacement");
+ if (it == end())
+ return TheGlobalData->m_gridBuildPlacement;
+
+ return stricmp(it->second.str(), "yes") == 0;
+}
+
Bool OptionPreferences::getMiddleMousePans(void)
{
OptionPreferences::const_iterator it = find("MiddleMousePans");
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index bd54035ea..10ec4dcd1 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1562,6 +1562,7 @@ void InGameUI::handleBuildPlacements( void )
/**@todo this whole orientation vector thing is LAME! Must replace, all I want to
to do is set a simple angle and have it automatically change, ug! */
TheTacticalView->screenToTerrain( &loc, &world );
+ snapPlacementToGrid( &world, m_pendingPlaceType, angle );
m_placeIcon[ 0 ]->setPosition( &world );
m_placeIcon[ 0 ]->setOrientation( angle );
@@ -1632,6 +1633,10 @@ void InGameUI::handleBuildPlacements( void )
TheTacticalView->screenToTerrain( &screenStart, &worldStart );
TheTacticalView->screenToTerrain( &screenEnd, &worldEnd );
+ // both ends, so a wall lands on the grid and tiles from a grid square
+ snapPlacementToGrid( &worldStart, m_pendingPlaceType, angle );
+ snapPlacementToGrid( &worldEnd, m_pendingPlaceType, angle );
+
// how big are each of our objects
Real objectSize = m_pendingPlaceType->getTemplateGeometryInfo().getMajorRadius() * 2.0f;
@@ -3373,6 +3378,37 @@ Real InGameUI::computePlacementAngle( const ICoord2D *start, const ICoord2D *end
} // end computePlacementAngle
+//-------------------------------------------------------------------------------------------------
+/** Experimental GridBuildPlacement (Options.ini): put the structure on the pathfinder's build grid
+ * instead of wherever the cursor happened to be to the pixel. Everything the pathfinder does is
+ * in 10-unit cells (PATHFIND_CELL_SIZE), and a structure dropped a couple of units off that grid
+ * blocks a strip of a cell it does not fill, which is what leaves the gaps you cannot walk a
+ * soldier through between two buildings that look flush. Snapping the footprint's edges to the
+ * grid lines makes neighbours share an edge exactly, and a row of them come out straight.
+ *
+ * The footprint is the template's own geometry, turned by the heading it is being placed at: a
+ * box is major along its facing and minor across it, and anything round is its bounding circle.
+ * At 45 degrees the axis-aligned extents grow, which is right - that is the ground it covers. */
+//-------------------------------------------------------------------------------------------------
+void InGameUI::snapPlacementToGrid( Coord3D *world, const ThingTemplate *what, Real angle ) const
+{
+ if( world == NULL || what == NULL || TheGlobalData->m_gridBuildPlacement == FALSE )
+ return;
+
+ const GeometryInfo &geom = what->getTemplateGeometryInfo();
+ Real major = geom.getMajorRadius();
+ Real minor = geom.getMinorRadius();
+ if( geom.getGeomType() != GEOMETRY_BOX )
+ major = minor = geom.getBoundingCircleRadius();
+
+ const Real c = (Real)fabs( Cos( angle ) );
+ const Real sn = (Real)fabs( Sin( angle ) );
+
+ world->x = snapPlacementAxis( world->x, major * c + minor * sn );
+ world->y = snapPlacementAxis( world->y, major * sn + minor * c );
+
+} // 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/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp
index e25caf2cf..9e6b04401 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp
@@ -252,6 +252,9 @@ GameMessageDisposition PlaceEventTranslator::translateGameMessage(const GameMess
// translate the screen position of start to world target location
TheTacticalView->screenToTerrain( &anchorStart, &world );
+ // and put it on the build grid, exactly as the ghost the player was looking at
+ TheInGameUI->snapPlacementToGrid( &world, build, angle );
+
// get the source object ID of the thing that is "building" the object
ObjectID builderID = INVALID_ID;
Object *builderObj = TheGameLogic->findObjectByID( TheInGameUI->getPendingPlaceSourceObjectID() );
@@ -351,6 +354,7 @@ GameMessageDisposition PlaceEventTranslator::translateGameMessage(const GameMess
Coord3D worldEnd;
TheTacticalView->screenToTerrain( &anchorEnd, &worldEnd );
+ TheInGameUI->snapPlacementToGrid( &worldEnd, build, angle );
placeMsg->appendLocationArgument( worldEnd );
} // end if
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index b5ba02fa2..8384489c7 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -718,6 +718,128 @@ TEST(particle_already_leaving_the_surface_is_only_lifted_clear)
CHECK_NEAR(vel.x, 1.0f, 1e-5f);
}
+//////////////////////////////////////////////////////////////////////////////
+// The ground blob under a particle system
+//
+// particleShadowBlob* is the whole of the decision: which systems earn a soft
+// shadow on the terrain, where it goes, how big it is and how dark. Pure
+// arithmetic over the live particles, so it can be driven directly.
+//////////////////////////////////////////////////////////////////////////////
+
+static void blobFeed(ParticleShadowBlob *blob, Int count, Real size, Real alpha)
+{
+ for (Int i = 0; i < count; i++)
+ particleShadowBlobAdd(blob, (Real)i, 0.0f, size, alpha);
+}
+
+TEST(blob_reset_leaves_nothing_to_resolve)
+{
+ ParticleShadowBlob blob;
+ Real x, y, sx, sy;
+ Int op;
+
+ particleShadowBlobReset(&blob);
+ CHECK_EQ(blob.m_count, 0);
+ CHECK(!particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));
+}
+
+TEST(blob_rejects_a_bullet_trail_because_its_particles_are_tiny)
+{
+ // plenty of particles, all of them a couple of units across - this is the case that
+ // keeps trails, sparks and muzzle flashes from staining the ground
+ ParticleShadowBlob blob;
+ Real x, y, sx, sy;
+ Int op;
+
+ particleShadowBlobReset(&blob);
+ blobFeed(&blob, 40, 3.0f, 1.0f);
+ CHECK(!particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));
+}
+
+TEST(blob_rejects_a_puff_of_one_or_two_particles)
+{
+ ParticleShadowBlob blob;
+ Real x, y, sx, sy;
+ Int op;
+
+ particleShadowBlobReset(&blob);
+ blobFeed(&blob, 2, 50.0f, 1.0f);
+ CHECK(!particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));
+}
+
+TEST(blob_rejects_a_cloud_that_has_faded_to_nothing)
+{
+ // big particles, enough of them, but no alpha left: a decal here would be a black
+ // smear under smoke that is no longer drawn
+ ParticleShadowBlob blob;
+ Real x, y, sx, sy;
+ Int op;
+
+ particleShadowBlobReset(&blob);
+ blobFeed(&blob, 10, 60.0f, 0.0f);
+ CHECK(!particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));
+}
+
+TEST(blob_centres_on_the_particles_and_covers_their_spread_plus_one_particle)
+{
+ ParticleShadowBlob blob;
+ Real x, y, sx, sy;
+ Int op;
+
+ particleShadowBlobReset(&blob);
+ particleShadowBlobAdd(&blob, 100.0f, 200.0f, 20.0f, 0.5f);
+ particleShadowBlobAdd(&blob, 140.0f, 200.0f, 30.0f, 0.5f);
+ particleShadowBlobAdd(&blob, 120.0f, 260.0f, 10.0f, 0.5f);
+
+ CHECK(particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));
+ CHECK_NEAR(x, 120.0f, 1e-4f); // midpoint of 100..140
+ CHECK_NEAR(y, 230.0f, 1e-4f); // midpoint of 200..260
+ CHECK_NEAR(sx, 40.0f + 30.0f, 1e-4f); // spread plus the biggest particle's width
+ CHECK_NEAR(sy, 60.0f + 30.0f, 1e-4f);
+ CHECK(op > 0);
+}
+
+TEST(blob_darkens_with_more_smoke_then_saturates_below_opaque)
+{
+ ParticleShadowBlob thin, thick, absurd;
+ Real x, y, sx, sy;
+ Int thinOp, thickOp, absurdOp;
+
+ particleShadowBlobReset(&thin);
+ blobFeed(&thin, 4, 40.0f, 0.25f);
+ CHECK(particleShadowBlobResolve(&thin, &x, &y, &sx, &sy, &thinOp));
+
+ particleShadowBlobReset(&thick);
+ blobFeed(&thick, 8, 40.0f, 1.0f); // past the saturation point
+ CHECK(particleShadowBlobResolve(&thick, &x, &y, &sx, &sy, &thickOp));
+
+ particleShadowBlobReset(&absurd);
+ blobFeed(&absurd, 200, 40.0f, 1.0f);
+ CHECK(particleShadowBlobResolve(&absurd, &x, &y, &sx, &sy, &absurdOp));
+
+ CHECK(thinOp < thickOp);
+ CHECK(thickOp <= absurdOp);
+ CHECK(absurdOp < 255); // smoke shades the ground, it never blacks it out
+ CHECK_EQ(thickOp, absurdOp); // and past saturation more smoke changes nothing
+}
+
+TEST(blob_never_smears_wider_than_the_cap)
+{
+ // a wind-blown system whose particles have drifted right across the map
+ ParticleShadowBlob blob;
+ Real x, y, sx, sy;
+ Int op;
+
+ particleShadowBlobReset(&blob);
+ particleShadowBlobAdd(&blob, 0.0f, 0.0f, 40.0f, 1.0f);
+ particleShadowBlobAdd(&blob, 5000.0f, 4000.0f, 40.0f, 1.0f);
+ particleShadowBlobAdd(&blob, 2500.0f, 2000.0f, 40.0f, 1.0f);
+
+ CHECK(particleShadowBlobResolve(&blob, &x, &y, &sx, &sy, &op));
+ CHECK(sx <= 300.0f);
+ CHECK(sy <= 300.0f);
+}
+
// ---------------------------------------------------------------------------------------------
// The HUD income estimate (InGameUI.cpp). It samples the score keeper's cumulative earnings
// into a ring of buckets and averages over every bucket it holds.
@@ -970,3 +1092,50 @@ TEST(placement_snap_takes_the_nearest_45_on_both_halves_of_the_circle)
CHECK_NEAR(snapped / step, (Real)REAL_TO_INT_FLOOR(snapped / step + 0.5f), 0.0001f);
}
}
+
+/*
+ * GridBuildPlacement. The grid is the pathfinder's, 10 world units a cell, and what has to
+ * land on it is the footprint's *edges*, not its centre: an odd number of cells wide means
+ * the centre sits in the middle of a cell, an even number means it sits on the line between
+ * two. Get that backwards and every second structure straddles a cell it only half fills,
+ * which is the gap-you-cannot-walk-through this snap exists to remove.
+ */
+TEST(placement_grid_snap_puts_footprint_edges_on_cell_lines)
+{
+ const Real cell = 10.0f;
+
+ /* 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);
+
+ /* 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);
+
+ /* a snap never moves anything more than half a cell */
+ for (Int i = 0; i < 400; i++)
+ {
+ Real v = i * 0.7f;
+
+ CHECK(fabsf(InGameUI::snapPlacementAxis(v, 15.0f) - v) <= cell / 2.0f + 0.0001f);
+ 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 */
+ 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;
+
+ 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);
+ }
+
+ /* 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);
+}