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): pack a shift-dragged row by the turned footprint
Olcay Seygan6663ca4973cf
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6699adb62..bb9f2e5a2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -555,7 +555,7 @@ found and fixed — EA's own, not port damage.**- Unbuildable ground is a soft red wash, crossed out square by square.- Running out of money no longer strands the ghost on the map.- Holding shift and clicking out a row of buildings no longer stops halfway and selects one.-- Hold shift and drag, and the row is one stroke. Press where the first building goes, drag the way the row runs, and a ghost appears for every building that fits end to end along the line, all facing the way the first one was turned. The row goes straight or on the diagonal, flush on the build grid, and stops at the last building your bank can pay for. A ghost that lands on a rock or someone's tank turns red and is left out when you let go; the rest go up. With two or three workers selected the buildings are shared out to the nearest free one, and the ones nobody is free for wait as plans until a worker comes off its job. A plain drag still turns the building, and walls still tile the way they always did.+- Hold shift and drag, and the row is one stroke. Press where the first building goes, drag the way the row runs, and a ghost appears for every building that fits end to end along the line, all facing the way the first one was turned. The row goes straight or on the diagonal and packs the buildings as close as their shape allows: one turned onto the line of the row stands wall to wall with the next, a factory keeps its door lane clear, and a building standing square to the build grid stays on it. It stops at the last building your bank can pay for. A ghost that lands on a rock or someone's tank turns red and is left out when you let go; the rest go up. With two or three workers selected the buildings are shared out to the nearest free one, and the ones nobody is free for wait as plans until a worker comes off its job. A plain drag still turns the building, and walls still tile the way they always did.- Buildings need a worker selected. With nothing selected the command bar used to borrow one of your dozers and offer its buildings anyway. It stays empty now until you pick a worker, and a replay recorded with a building placed that way plays back without it.- Turning a building no longer turns everything you build afterwards. A wheeled heading still carries from one wall to the next, but the next supply centre comes out facing the way it was designed to.- A building you point at blocked ground slides to the nearest spot it fits, and lands there.diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.hindex 1fdfc1421..185548c83 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h@@ -644,26 +644,48 @@ public: // ********************************************************************/** A shift-dragged row: the step from one structure to the next, and how many fit between the* anchor and a cursor 'dx'/'dy' away. The row runs along the nearest eighth of a turn - a- * component counts once it is more than tan 22.5 degrees of the other - and a step is the- * footprint's whole size, 'sizeX'/'sizeY' with the bib, along each axis it moves on, so- * neighbours stand flush. On the build grid that size goes up to whole cells, which keeps every- * piece on the grid the first one was snapped to. Never fewer than one, never more than+ * component counts once it is more than tan 22.5 degrees of the other - and packs as tight as+ * the footprint allows along it: the footprint is the box 'halfFacing' by 'halfSide' turned to+ * the heading, and the step is where the row leaves the box two of them would share. So a+ * structure turned onto the row's own line stands face to face with the next, and one turned+ * across it corner to corner, which is the closest a straight row of those can get. With+ * the heading on the grid's axes and the build grid on, the step goes up to whole cells and+ * every piece stays on the grid the first one was snapped to; anywhere else nothing lines two+ * edges up exactly, so a hair is left between them. Never fewer than one, never more than* 'most'. Inline and static so a test can reach it without linking the whole in-game UI. */- static Int placementRow( Real dx, Real dy, Real sizeX, Real sizeY, Bool grid, Int most,- Coord2D *step )+ static Int placementRow( Real dx, Real dy, Real headingCos, Real headingSin, Real halfFacing,+ Real halfSide, Bool grid, Int most, Coord2D *step ){const Real slope = 0.41421356f; // tan 22.5 degrees+ const Real diagonal = 0.70710678f; // each component of a unit step on a diagonalconst Real cell = (Real)PLACEMENT_CELL;const Real slack = 0.01f; // Cos of a quarter turn is a hair off zero, not a cell's worth-- if( grid )- {- sizeX = (Real)REAL_TO_INT_CEIL( sizeX / cell - slack ) * cell;- sizeY = (Real)REAL_TO_INT_CEIL( sizeY / cell - slack ) * cell;- }-- step->x = fabs( dx ) > fabs( dy ) * slope ? ( dx < 0.0f ? -sizeX : sizeX ) : 0.0f;- step->y = fabs( dy ) > fabs( dx ) * slope ? ( dy < 0.0f ? -sizeY : sizeY ) : 0.0f;+ const Real parallel = 0.0001f; // a projection this small is a face the row runs along, not into+ const Real clearance = 0.5f; // world units left between two pieces off the grid++ const Real signX = fabs( dx ) > fabs( dy ) * slope ? ( dx < 0.0f ? -1.0f : 1.0f ) : 0.0f;+ const Real signY = fabs( dy ) > fabs( dx ) * slope ? ( dy < 0.0f ? -1.0f : 1.0f ) : 0.0f;+ const Real along = ( signX != 0.0f && signY != 0.0f ) ? diagonal : 1.0f;+ const Real ux = signX * along;+ const Real uy = signY * along;++ const Real intoFacing = (Real)fabs( ux * headingCos + uy * headingSin );+ const Real intoSide = (Real)fabs( uy * headingCos - ux * headingSin );+ Real reach = 0.0f;+ if( intoFacing > parallel )+ reach = 2.0f * halfFacing / intoFacing;+ if( intoSide > parallel && ( reach == 0.0f || 2.0f * halfSide / intoSide < reach ) )+ reach = 2.0f * halfSide / intoSide;++ Real component = reach * along;+ const Bool onGridAxes = fabs( headingSin * headingCos ) < parallel;+ if( grid && onGridAxes )+ component = (Real)REAL_TO_INT_CEIL( component / cell - slack ) * cell;+ else+ component += clearance;++ step->x = signX * component;+ step->y = signY * component;Int count = 1;const Real stepSqr = step->x * step->x + step->y * step->y;diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cppindex c151a8cee..c8f010773 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp@@ -6893,21 +6893,27 @@ Real InGameUI::computePlacementAngle( const ICoord2D *start, const ICoord2D *end* 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. *///--------------------------------------------------------------------------------------------------static void placementHalfExtents( const ThingTemplate *what, Real angle, Real *halfX, Real *halfY )+static void placementHalfSizes( const ThingTemplate *what, Real *major, Real *minor ){const GeometryInfo &geom = what->getTemplateGeometryInfo();- Real major = geom.getMajorRadius();- Real minor = geom.getMinorRadius();+ *major = geom.getMajorRadius();+ *minor = geom.getMinorRadius();if( geom.getGeomType() != GEOMETRY_BOX )- major = minor = geom.getBoundingCircleRadius();+ *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();+ *major += what->getFactoryExtraBibWidth();+ *minor += what->getFactoryExtraBibWidth();+}++static void placementHalfExtents( const ThingTemplate *what, Real angle, Real *halfX, Real *halfY )+{+ Real major, minor;+ placementHalfSizes( what, &major, &minor );const Real c = (Real)fabs( Cos( angle ) );const Real sn = (Real)fabs( Sin( angle ) );@@ -6947,8 +6953,14 @@ Bool InGameUI::placesRow( void )void InGameUI::computePlacementRow( const ThingTemplate *what, Real angle, const Coord3D *start,const Coord3D *end, std::vector<Coord3D> *positions ) const{- Real halfX, halfY;- placementHalfExtents( what, angle, &halfX, &halfY );+ //+ // A factory's door needs its lane clear of the next structure (isLocationClearOfObjects' exit+ // check), so a row running out of the door or into it leaves the lane between each pair.+ // Half the lane on each half-length: the pair's shared box grows by the whole of it.+ //+ Real major, minor;+ placementHalfSizes( what, &major, &minor );+ const Real halfFacing = major + what->getFactoryExitWidth() * 0.5f;Int most = TheGlobalData->m_maxLineBuildObjects;Player *player = ThePlayerList->getLocalPlayer();@@ -6961,7 +6973,8 @@ void InGameUI::computePlacementRow( const ThingTemplate *what, Real angle, const}Coord2D step;- const Int count = placementRow( end->x - start->x, end->y - start->y, 2.0f * halfX, 2.0f * halfY,+ const Int count = placementRow( end->x - start->x, end->y - start->y, (Real)Cos( angle ),+ (Real)Sin( angle ), halfFacing, minor,TheGlobalData->m_gridBuildPlacement, most, &step );positions->clear();diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 474311461..7a3768336 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -2265,43 +2265,63 @@ TEST(placement_grid_snap_puts_footprint_edges_on_cell_lines)CHECK_NEAR(InGameUI::snapPlacementAxis(13.0f, 0.0f), 14.5f, 0.0001f);}-TEST(placement_row_steps_flush_along_the_nearest_eighth)+TEST(placement_row_packs_the_footprint_along_the_nearest_eighth){Coord2D step;+ const Real half = 0.70710678f; /* cos and sin of an eighth of a turn */- /* straight along x, a 40 by 30 footprint: one piece per 40 dragged, the first free */- CHECK_EQ(InGameUI::placementRow(119.0f, 0.0f, 40.0f, 30.0f, TRUE, 50, &step), 3);+ /* facing along x, a 40 by 30 footprint: one piece per 40 dragged, the first free */+ CHECK_EQ(InGameUI::placementRow(119.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 3);CHECK_NEAR(step.x, 40.0f, 0.0001f);CHECK_NEAR(step.y, 0.0f, 0.0001f);- CHECK_EQ(InGameUI::placementRow(120.0f, 0.0f, 40.0f, 30.0f, TRUE, 50, &step), 4);+ CHECK_EQ(InGameUI::placementRow(120.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 4);/* a hand-drawn line 20 degrees off still runs straight, and backwards runs backwards */- CHECK_EQ(InGameUI::placementRow(-100.0f, 36.0f, 40.0f, 30.0f, TRUE, 50, &step), 3);+ CHECK_EQ(InGameUI::placementRow(-100.0f, 36.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 3);CHECK_NEAR(step.x, -40.0f, 0.0001f);CHECK_NEAR(step.y, 0.0f, 0.0001f);/* along y the step is the footprint's other side */- InGameUI::placementRow(0.0f, -90.0f, 40.0f, 30.0f, TRUE, 50, &step);+ InGameUI::placementRow(0.0f, -90.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step);CHECK_NEAR(step.x, 0.0f, 0.0001f);CHECK_NEAR(step.y, -30.0f, 0.0001f);- /* a diagonal steps corner to corner */- CHECK_EQ(InGameUI::placementRow(80.0f, 60.0f, 40.0f, 30.0f, TRUE, 50, &step), 3);- CHECK_NEAR(step.x, 40.0f, 0.0001f);+ /* a diagonal slides each piece along the last one's long side instead of meeting it corner+ * to corner: 30 on each axis, where 40 by 30 left a triangle of ground between them */+ CHECK_EQ(InGameUI::placementRow(80.0f, 60.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 3);+ CHECK_NEAR(step.x, 30.0f, 0.0001f);CHECK_NEAR(step.y, 30.0f, 0.0001f);- /* on the grid a size goes up to whole cells, a turned footprint's float dust does not */- InGameUI::placementRow(100.0f, 0.0f, 42.0f, 30.0f, TRUE, 50, &step);+ /* turned a quarter, the footprint's sides swap axes; Cos's float dust stays off the grid */+ InGameUI::placementRow(100.0f, 0.0f, -0.00000004f, 1.0f, 20.0f, 15.0f, TRUE, 50, &step);+ CHECK_NEAR(step.x, 30.0f, 0.0001f);++ /* turned an eighth, a row along the structure's own line stands face to face with the+ * next: a step 40 long, less the hair left off the grid */+ InGameUI::placementRow(100.0f, 100.0f, half, half, 20.0f, 15.0f, TRUE, 50, &step);+ CHECK_NEAR(step.x * step.x + step.y * step.y, 40.0f * 40.0f, 60.0f);+ CHECK(step.x * step.x + step.y * step.y > 40.0f * 40.0f);+ InGameUI::placementRow(-100.0f, 100.0f, half, half, 20.0f, 15.0f, TRUE, 50, &step);+ CHECK_NEAR(step.x * step.x + step.y * step.y, 30.0f * 30.0f, 60.0f);++ /* and across the structure's line the row can do no better than corner to corner: the+ * shorter half-side's diagonal, 30 / cos 45 */+ InGameUI::placementRow(100.0f, 0.0f, half, half, 20.0f, 15.0f, TRUE, 50, &step);+ CHECK_NEAR(step.x, 30.0f / half + 0.5f, 0.001f);+ CHECK_NEAR(step.y, 0.0f, 0.0001f);++ /* on the grid a size goes up to whole cells; off it the size stands, plus the hair */+ InGameUI::placementRow(100.0f, 0.0f, 1.0f, 0.0f, 21.0f, 15.0f, TRUE, 50, &step);CHECK_NEAR(step.x, 50.0f, 0.0001f);- InGameUI::placementRow(100.0f, 0.0f, 40.00002f, 30.0f, TRUE, 50, &step);+ InGameUI::placementRow(100.0f, 0.0f, 1.0f, 0.0f, 20.00001f, 15.0f, TRUE, 50, &step);CHECK_NEAR(step.x, 40.0f, 0.0001f);- InGameUI::placementRow(100.0f, 0.0f, 42.0f, 30.0f, FALSE, 50, &step);- CHECK_NEAR(step.x, 42.0f, 0.0001f);+ InGameUI::placementRow(100.0f, 0.0f, 1.0f, 0.0f, 21.0f, 15.0f, FALSE, 50, &step);+ CHECK_NEAR(step.x, 42.5f, 0.0001f);/* never more than the cap, never fewer than one, and no drag is one piece */- CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 40.0f, 30.0f, TRUE, 5, &step), 5);- CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 40.0f, 30.0f, TRUE, 0, &step), 1);- CHECK_EQ(InGameUI::placementRow(0.0f, 0.0f, 40.0f, 30.0f, TRUE, 50, &step), 1);+ CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 5, &step), 5);+ CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 0, &step), 1);+ CHECK_EQ(InGameUI::placementRow(0.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, TRUE, 50, &step), 1);}