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): remember an in-flight order by its turned footprint
Olcay Seyganb33c2b3465b8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bb9f2e5a2..f459c707a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -565,7 +565,7 @@ found and fixed — EA's own, not port damage.**- Inside that red area, the ground the defence cannot see is shaded. A Patriot battery, a Gattling Cannon or a Fire Base does not shoot through a building - the game refuses the shot when a solid structure stands between the gun and its target - so from where your cursor is, each building in range throws a shadow that starts at its near wall and runs out to the edge of the reach, a wedge that swings as you move the defence round the corner of your War Factory. Hills count as well. The game will not pick a target when the ground rises between the top of the gun and the top of a tank, so the far side of a ridge goes dark too, and a valley beyond it lights up again where the gun can see down into it. The shade lies on the ground as one piece and follows its slopes. A corner one of your turrets, or an ally's, already sees into is not a hole, so it stays bright: the shade is only what nothing of yours can shoot. A Stinger Site or a bunker gets no shade, because a building in the way does not stop them. Click one of your defences, or an ally's, that is already standing and it shows the same shade from where it stands, its own and nobody else's, so the corner behind your War Factory is there to see after the turret is built too.- Clicking a turret in a base full of them no longer drags the game down. Every circle's outline was worked out again against every other circle on every frame, so with 120 Patriot batteries on the map one selected battery took the game from 94 frames a second to 53. The outlines are worked out once and kept until a building goes up, comes down or comes out of the fog: the same base now holds 83 with the battery selected against 84 with nothing selected.- Selecting a GLA supply stash no longer puts reach circles round every defence on the map. The stash spawns workers, a worker carries a weapon for clearing mines, and that was enough to count the stash as armed.-- Two buildings can no longer be put down on the same spot online. A build order travels to the other players before anything appears on the ground, and every click made while it was in flight was answered by a map that still showed the square as empty - so a shift-held row on a bad connection came out as buildings standing inside each other. The ghost now knows about the orders you have already spent, and turns red over them or slides to the square beside; and the order itself is checked again where it lands, on every machine, after the first building is standing. A second one aimed at the same ground is refused there and costs you nothing.+- Two buildings can no longer be put down on the same spot online. A build order travels to the other players before anything appears on the ground, and every click made while it was in flight was answered by a map that still showed the square as empty - so a shift-held row on a bad connection came out as buildings standing inside each other. The ghost now knows about the orders you have already spent, and turns red over them or slides to the square beside; and the order itself is checked again where it lands, on every machine, after the first building is standing. A second one aimed at the same ground is refused there and costs you nothing. What the ghost remembered of each order was the square around the building rather than the building, and a building standing at an angle, which is how most of them come out, fills barely half its square: a shift-clicked row of them found its neighbour in the way, slid off, and left a gap. It remembers the building's own outline now, and five Power Plants clicked side by side stand wall to wall.- The right button drops the building on your cursor whether the click travelled or not. Sending a dozer off somewhere in the middle of a shift-held row moved it and left the ghost riding the cursor, because the cancel only counted a press that had not moved a pixel and a hand on the way to a destination always moves a few.- A click clears a half-typed building shortcut.- The two-key building shortcut survives the command bar's redraw now.diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.hindex 185548c83..0ebcf44c0 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h@@ -730,11 +730,48 @@ public: // ********************************************************************enum { PENDING_PLACEMENT_FRAMES = 60 }; ///< logic frames one is remembered for - two seconds,/// comfortably longer than any network delay+ /// the ground a structure stands on: its box with the bib, turned to its heading+ struct PlacementBox+ {+ Real x, y; ///< centre+ Real c, s; ///< cos and sin of the heading+ Real halfMajor; ///< half-length along the heading, bib included+ Real halfMinor; ///< half-width across it, bib included+ };+/// the ground a structure of this template put down here would stand onstatic void placementFootprint( const ThingTemplate *what, const Coord3D *world, Real angle,- Region2D *footprint );- /// do two of those share any ground? edge to edge is not sharing - structures are built flush- static Bool footprintsOverlap( const Region2D *a, const Region2D *b );+ PlacementBox *footprint );++ /** Do two of those share any ground? The same turned boxes the logic's own clearance check+ * compares, so a structure the logic would take next to one still on its way is not refused+ * here: a box around a turned box is far bigger than the box, and two diagonal buildings+ * clicked side by side used to find their neighbour in the way and slide off it. Edge to edge+ * is not sharing - structures are built flush. Separating axes, the four sides of the two. */+ static Bool footprintsOverlap( const PlacementBox *a, const PlacementBox *b )+ {+ const Real slack = 0.01f; // edges this close are touching, not crossing+ const Real dx = b->x - a->x;+ const Real dy = b->y - a->y;+ const PlacementBox *boxes[ 2 ] = { a, b };++ for( Int k = 0; k < 2; k++ )+ {+ const Real axes[ 2 ][ 2 ] = { { boxes[ k ]->c, boxes[ k ]->s }, { -boxes[ k ]->s, boxes[ k ]->c } };+ for( Int i = 0; i < 2; i++ )+ {+ const Real ax = axes[ i ][ 0 ];+ const Real ay = axes[ i ][ 1 ];+ const Real reachA = a->halfMajor * (Real)fabs( ax * a->c + ay * a->s ) ++ a->halfMinor * (Real)fabs( ay * a->c - ax * a->s );+ const Real reachB = b->halfMajor * (Real)fabs( ax * b->c + ay * b->s ) ++ b->halfMinor * (Real)fabs( ay * b->c - ax * b->s );+ if( (Real)fabs( ax * dx + ay * dy ) >= reachA + reachB - slack )+ return FALSE;+ }+ }+ return TRUE;+ }/// remember a structure just ordered here, so the next click can see itvoid recordPendingPlacement( const ThingTemplate *what, const Coord3D *world, Real angle );@@ -1223,8 +1260,8 @@ protected:/// a structure ordered here, and the logic frame it was ordered on - see recordPendingPlacementstruct PendingPlacement{- Region2D footprint;- UnsignedInt frame; ///< 0 for a slot nothing has been written to yet+ PlacementBox footprint;+ UnsignedInt frame; ///< 0 for a slot nothing has been written to yet};PendingPlacement m_pendingPlacement[ PENDING_PLACEMENTS ];Int m_pendingPlacementAt; ///< where the next order is written, round the ringdiff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cppindex c8f010773..7ebbf2d39 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp@@ -7007,28 +7007,19 @@ UnsignedInt InGameUI::placementCheckOptions( void )} // end placementCheckOptions//--------------------------------------------------------------------------------------------------/** The ground a structure of this template would stand on, put down here at this heading. The- * geometry's own bounds, which for a box at any angle is the rectangle around the turned box:- * two of those sharing ground is close enough to "on top of each other" for the client to refuse- * a second click, and the logic side asks the real question afterwards anyway. */+/** The ground a structure of this template would stand on, put down here at this heading: the box+ * isLocationClearOfObjects compares, bib and all. *///-------------------------------------------------------------------------------------------------void InGameUI::placementFootprint( const ThingTemplate *what, const Coord3D *world, Real angle,- Region2D *footprint )+ PlacementBox *footprint ){- what->getTemplateGeometryInfo().get2DBounds( *world, angle, *footprint );+ placementHalfSizes( what, &footprint->halfMajor, &footprint->halfMinor );+ footprint->x = world->x;+ footprint->y = world->y;+ footprint->c = (Real)Cos( angle );+ footprint->s = (Real)Sin( angle );}-//--------------------------------------------------------------------------------------------------/** Do two footprints share any ground? Touching edge to edge does not count: structures are built- * flush against each other on the build grid all game, and a row of them is not an overlap. */-//--------------------------------------------------------------------------------------------------Bool InGameUI::footprintsOverlap( const Region2D *a, const Region2D *b )-{- return a->lo.x < b->hi.x && b->lo.x < a->hi.x &&- a->lo.y < b->hi.y && b->lo.y < a->hi.y;--} // end footprintsOverlap-//-------------------------------------------------------------------------------------------------/** Remember a structure just ordered, so the click after it knows the ground is spoken for. Round* a ring: the oldest goes, which is the one most likely to be standing by now. */@@ -7066,7 +7057,7 @@ Bool InGameUI::overlapsPendingPlacement( const Coord3D *world, const ThingTemplaif( what == NULL || world == NULL || TheGameLogic == NULL )return FALSE;- Region2D mine;+ PlacementBox mine;placementFootprint( what, world, angle, &mine );const UnsignedInt now = TheGameLogic->getFrame();diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 7a3768336..31d7b3d97 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -11122,36 +11122,64 @@ TEST(the_health_bar_grows_with_the_unit_under_it_and_not_with_the_screens_width)spent, so a shift-held run of clicks does not put two structures on the same square. What itremembers is the footprint, and two footprints that merely touch are two structures built flushagainst each other, which is most of a base wall. */-TEST(two_structures_ordered_onto_the_same_ground_are_one_too_many)+static InGameUI::PlacementBox placementBox( Real x, Real y, Real c, Real s, Real halfMajor, Real halfMinor ){- Region2D a, b;+ InGameUI::PlacementBox box = { x, y, c, s, halfMajor, halfMinor };+ return box;+}- a.lo.x = 0.0f; a.lo.y = 0.0f; a.hi.x = 40.0f; a.hi.y = 40.0f;+TEST(two_structures_ordered_onto_the_same_ground_are_one_too_many)+{+ const Real half = 0.70710678f; /* cos and sin of an eighth of a turn */+ InGameUI::PlacementBox a = placementBox( 20.0f, 20.0f, 1.0f, 0.0f, 20.0f, 20.0f );+ InGameUI::PlacementBox b;// itself, obviouslyCHECK( InGameUI::footprintsOverlap( &a, &a ) );// a corner inside it counts, from either side- b.lo.x = 39.0f; b.lo.y = 39.0f; b.hi.x = 79.0f; b.hi.y = 79.0f;+ b = placementBox( 59.0f, 59.0f, 1.0f, 0.0f, 20.0f, 20.0f );CHECK( InGameUI::footprintsOverlap( &a, &b ) );CHECK( InGameUI::footprintsOverlap( &b, &a ) );// flush against it does not - a row of buildings on the build grid is not an overlap- b.lo.x = 40.0f; b.lo.y = 0.0f; b.hi.x = 80.0f; b.hi.y = 40.0f;+ b = placementBox( 60.0f, 20.0f, 1.0f, 0.0f, 20.0f, 20.0f );CHECK( !InGameUI::footprintsOverlap( &a, &b ) );CHECK( !InGameUI::footprintsOverlap( &b, &a ) );// nor does clear of it, on either axis alone- b.lo.x = 10.0f; b.lo.y = 41.0f; b.hi.x = 30.0f; b.hi.y = 60.0f;+ b = placementBox( 20.0f, 50.5f, 1.0f, 0.0f, 10.0f, 9.5f );CHECK( !InGameUI::footprintsOverlap( &a, &b ) );- b.lo.x = 41.0f; b.lo.y = 10.0f; b.hi.x = 60.0f; b.hi.y = 30.0f;+ b = placementBox( 50.5f, 20.0f, 1.0f, 0.0f, 9.5f, 10.0f );CHECK( !InGameUI::footprintsOverlap( &a, &b ) );// one wholly inside another - a small structure ordered into a big one's middle- b.lo.x = 10.0f; b.lo.y = 10.0f; b.hi.x = 20.0f; b.hi.y = 20.0f;+ b = placementBox( 15.0f, 15.0f, 1.0f, 0.0f, 5.0f, 5.0f );CHECK( InGameUI::footprintsOverlap( &a, &b ) );CHECK( InGameUI::footprintsOverlap( &b, &a ) );+ //+ // Two structures turned an eighth, side by side along their own faces. The squares around+ // them overlap by half their width; the structures themselves only touch, and the logic builds+ // both - so the second click is not refused and the ghost does not slide off to leave a gap.+ //+ a = placementBox( 0.0f, 0.0f, half, half, 20.0f, 15.0f );+ b = placementBox( 40.0f * half, 40.0f * half, half, half, 20.0f, 15.0f );+ CHECK( !InGameUI::footprintsOverlap( &a, &b ) );+ CHECK( !InGameUI::footprintsOverlap( &b, &a ) );+ b = placementBox( -30.0f * half, 30.0f * half, half, half, 20.0f, 15.0f );+ CHECK( !InGameUI::footprintsOverlap( &a, &b ) );++ // a step shorter and they do share ground+ b = placementBox( 38.0f * half, 38.0f * half, half, half, 20.0f, 15.0f );+ CHECK( InGameUI::footprintsOverlap( &a, &b ) );++ // and one turned against the other is judged on the other's sides too+ b = placementBox( 34.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f );+ CHECK( InGameUI::footprintsOverlap( &a, &b ) );+ b = placementBox( 45.0f, 0.0f, 1.0f, 0.0f, 20.0f, 15.0f );+ CHECK( !InGameUI::footprintsOverlap( &a, &b ) );+//// and the window it is remembered for has to outlast a bad link: a network game runs the order// a handful of frames after the click, and two seconds of logic frames is a long link