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
fix(gameengine): keep both ends of a shift-dragged building row
Olcay Seygan439128633cad
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4cf5e0bd0..d2fdf07f7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -651,7 +651,7 @@ A third pass went through the generals' powers, promotions and unit abilities.- 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 runs exactly the way you drag it, at 20 degrees as happily as along the map's edge, 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, and a factory keeps its door lane clear. A row of Power Plants turned square to the map packs 44 apart; it used to go down on whole grid squares, 50 apart, with a strip of dirt at every joint. 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.-- While that row is on the cursor, the wheel opens and closes the space between the buildings, one grid square a notch. It will not pack them any tighter than they already sit against each other. Letting go places the row and takes the building off the cursor, so the next click is not another one. Holding shift and clicking them out one at a time still keeps the ghost up.+- While that row is on the cursor, the wheel opens and closes the space between the buildings, one grid square a notch. The one where you pressed and the one under the cursor stay put; what changes is how many stand between them and how far apart those are. It will not pack them any tighter than they already sit against each other, and it will not take either end away to make the gap. Letting go places the row and takes the building off the cursor, so the next click is not another one. Holding shift and clicking them out one at a time still keeps the ghost up.- 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 95daacc5d..9abecea8c 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h@@ -696,33 +696,48 @@ public: // ********************************************************************* rounded to the build grid: a Power Plant is 44 across and the grid is 10, and rounding it* up left 6 of dirt at every joint. 'extraGap' is world units the wheel adds on top of that* touch while the line is being drawn. Zero packs them flush, and a negative value is- * ignored, so the row never overlaps. Never fewer than one, never more than 'most'. Inline- * and static so a test can reach it without linking the whole in-game UI. */+ * ignored, so the row never overlaps. The first piece stands on the anchor and the last on+ * the cursor. As many as that spacing allows go between them, and any slack is spread along+ * the line so the last one still lands on the cursor. Opening the gap takes pieces out of+ * the middle. A drag keeps both ends even when the asked gap is longer than the line, unless+ * 'most' allows only one. 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 headingCos, Real headingSin, Real halfFacing,Real halfSide, Int most, Coord2D *step, Real extraGap = 0.0f ){step->x = 0.0f;step->y = 0.0f;const Real length = (Real)sqrt( dx * dx + dy * dy );++ Int count = 1;if( length > 0.0f ){Real span = placementTouchDistance( dx / length, dy / length, headingCos, headingSin,2.0f * halfFacing, 2.0f * halfSide );if( extraGap > 0.0f )span += extraGap;- step->x = dx / length * span;- step->y = dy / length * span;+ if( span < 0.01f )+ span = 0.01f;++ // one gap per step, and a drag always has the piece under the cursor as well as the anchor.+ // the 0.001 covers a quotient that lands a hair under a whole number: the drag and the+ // touch are the same geometry, and a table sine leaves it at 1.999+ Int gaps = REAL_TO_INT_FLOOR( length / span + 0.001f );+ if( gaps < 1 )+ gaps = 1;+ count = gaps + 1;}- Int count = 1;- const Real stepSqr = step->x * step->x + step->y * step->y;- if( stepSqr > 0.0f )- count += REAL_TO_INT_FLOOR( ( dx * step->x + dy * step->y ) / stepSqr );-if( count > most )count = most;if( count < 1 )count = 1;++ if( count >= 2 )+ {+ step->x = dx / (Real)( count - 1 );+ step->y = dy / (Real)( count - 1 );+ }return count;}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 0c59d6540..595d39ea0 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -2258,74 +2258,91 @@ 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_packs_the_footprint_along_the_drag)+TEST(placement_row_pins_both_ends_and_opens_the_middle){Coord2D step;const Real half = 0.70710678f; /* cos and sin of an eighth of a turn */- /* facing along x, a 40 by 30 footprint: one piece per 40 dragged and the hair the logic's- * clearance test needs between them, the first free */- CHECK_EQ(InGameUI::placementRow(119.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 3);+ /* facing along x, a 40 by 30 footprint. three exact gaps of 40.5: four pieces, the last+ * on the cursor, the step the footprint and the hair the clearance test needs */+ CHECK_EQ(InGameUI::placementRow(121.5f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 4);CHECK_NEAR(step.x, 40.5f, 0.0001f);CHECK_NEAR(step.y, 0.0f, 0.0001f);- CHECK_EQ(InGameUI::placementRow(122.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 4);+ CHECK_NEAR(step.x * 3.0f, 121.5f, 0.0001f);- /* backwards runs backwards */- CHECK_EQ(InGameUI::placementRow(-100.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 3);+ /* a length that is not a multiple still ends on the cursor: the spare is spread, the+ * step grows past 40.5, and both ends stay */+ CHECK_EQ(InGameUI::placementRow(130.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 4);+ CHECK_NEAR(step.x, 130.0f / 3.0f, 0.0001f);+ CHECK(step.x > 40.5f);++ /* backwards runs backwards, and the last piece is the far end */+ CHECK_EQ(InGameUI::placementRow(-81.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 3);CHECK_NEAR(step.x, -40.5f, 0.0001f);CHECK_NEAR(step.y, 0.0f, 0.0001f);- /* a line dragged 20 degrees off the axis runs at 20 degrees, not flat: the pieces meet through- * the facing face, 40 / cos 20 apart, and the hair */+ /* a line dragged 20 degrees off the axis runs at 20 degrees, not flat, and the pieces+ * meet through the facing face */const Real twenty = 20.0f * PI / 180.0f;- InGameUI::placementRow(100.0f * Cos(twenty), 100.0f * Sin(twenty), 1.0f, 0.0f, 20.0f, 15.0f, 50, &step);+ const Real touch20 = 40.0f / Cos(twenty) + 0.5f;+ const Real drag20 = touch20 * 2.0f;+ CHECK_EQ(InGameUI::placementRow(drag20 * Cos(twenty), drag20 * Sin(twenty), 1.0f, 0.0f,+ 20.0f, 15.0f, 50, &step), 3);CHECK_NEAR(step.y / step.x, Sin(twenty) / Cos(twenty), 0.0001f);- CHECK_NEAR(sqrt(step.x * step.x + step.y * step.y), 40.0f / Cos(twenty) + 0.5f, 0.001f);+ CHECK_NEAR(sqrt(step.x * step.x + step.y * step.y), touch20, 0.001f);- /* along y the step is the footprint's other side */- InGameUI::placementRow(0.0f, -90.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step);+ /* along y the step is the footprint's other side, two exact gaps */+ CHECK_EQ(InGameUI::placementRow(0.0f, -61.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 3);CHECK_NEAR(step.x, 0.0f, 0.0001f);CHECK_NEAR(step.y, -30.5f, 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 and the hair, where 40 by 30 left a triangle of ground */- CHECK_EQ(InGameUI::placementRow(80.0f, 80.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 3);- CHECK_NEAR(step.x, 30.0f + 0.5f * half, 0.001f);- CHECK_NEAR(step.y, 30.0f + 0.5f * half, 0.001f);+ const Real diag = 30.0f + 0.5f * half;+ CHECK_EQ(InGameUI::placementRow(diag * 2.0f, diag * 2.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 3);+ CHECK_NEAR(step.x, diag, 0.001f);+ CHECK_NEAR(step.y, diag, 0.001f);/* turned a quarter, the footprint's sides swap axes, Cos's float dust and all */- InGameUI::placementRow(100.0f, 0.0f, -0.00000004f, 1.0f, 20.0f, 15.0f, 50, &step);+ CHECK_EQ(InGameUI::placementRow(61.0f, 0.0f, -0.00000004f, 1.0f, 20.0f, 15.0f, 50, &step), 3);CHECK_NEAR(step.x, 30.5f, 0.0001f);- /* turned an eighth, a row along the structure's own line stands face to face with the- * next: a step 40 long, and the hair */- InGameUI::placementRow(100.0f, 100.0f, half, half, 20.0f, 15.0f, 50, &step);+ /* turned an eighth, a row along the structure's own line stands face to face: a step 40.5+ * long. across that line it meets on the short side */+ CHECK_EQ(InGameUI::placementRow(81.0f * half, 81.0f * half, half, half, 20.0f, 15.0f, 50, &step), 3);CHECK_NEAR(step.x * step.x + step.y * step.y, 40.5f * 40.5f, 0.01f);- InGameUI::placementRow(-100.0f, 100.0f, half, half, 20.0f, 15.0f, 50, &step);+ CHECK_EQ(InGameUI::placementRow(-61.0f * half, 61.0f * half, half, half, 20.0f, 15.0f, 50, &step), 3);CHECK_NEAR(step.x * step.x + step.y * step.y, 30.5f * 30.5f, 0.01f);/* 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, 50, &step);- CHECK_NEAR(step.x, 30.0f / half + 0.5f, 0.001f);+ const Real corner = 30.0f / half + 0.5f;+ CHECK_EQ(InGameUI::placementRow(corner * 2.0f, 0.0f, half, half, 20.0f, 15.0f, 50, &step), 3);+ CHECK_NEAR(step.x, corner, 0.001f);CHECK_NEAR(step.y, 0.0f, 0.0001f);/* the step is the footprint, not the build grid's next whole cell */- InGameUI::placementRow(100.0f, 0.0f, 1.0f, 0.0f, 22.0f, 30.0f, 50, &step);+ CHECK_EQ(InGameUI::placementRow(89.0f, 0.0f, 1.0f, 0.0f, 22.0f, 30.0f, 50, &step), 3);CHECK_NEAR(step.x, 44.5f, 0.0001f);- /* never more than the cap, never fewer than one, and no drag is one piece */+ /* never more than the cap, and what is affordable still reaches both ends */CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 5, &step), 5);+ CHECK_NEAR(step.x * 4.0f, 1000.0f, 0.001f);CHECK_EQ(InGameUI::placementRow(1000.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 0, &step), 1);CHECK_EQ(InGameUI::placementRow(0.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step), 1);- /* ten units of extra gap on the 40.5 touch: the same drag that held four now holds three */- CHECK_EQ(InGameUI::placementRow(122.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step, 10.0f), 3);- CHECK_NEAR(step.x, 50.5f, 0.0001f);+ /* ten units of extra gap: a line that held four flush now holds three, and the two ends+ * are still the anchor and the cursor */+ CHECK_EQ(InGameUI::placementRow(121.5f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step, 10.0f), 3);+ CHECK_NEAR(step.x, 60.75f, 0.0001f);CHECK_NEAR(step.y, 0.0f, 0.0001f);+ /* a gap longer than the line leaves the two ends and nothing between them */+ CHECK_EQ(InGameUI::placementRow(121.5f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step, 500.0f), 2);+ CHECK_NEAR(step.x, 121.5f, 0.0001f);+/* a negative gap is ignored: the row never packs tighter than the footprints allow */- InGameUI::placementRow(100.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step, -20.0f);+ InGameUI::placementRow(81.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f, 50, &step, -20.0f);CHECK_NEAR(step.x, 40.5f, 0.0001f);}