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.

fix(gameengine): refuse a second structure on ground already ordered

Olcay Seygande051efd8ed9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fd26e910c..d6107d34e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -316,6 +316,7 @@ found and fixed — EA's own, not port damage.**
- A building you point at blocked ground slides to the nearest spot it fits, and lands there.
- The pointer keeps its build cursor while you place, even passing over your own buildings.
- GLA defences get a range ring while you site them.
+- 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.
- A click clears a half-typed building shortcut.
- The two-key building shortcut survives the command bar's redraw now.
- Escape cancels what you were doing before it opens the menu.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
index b809caf54..8f631e501 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/InGameUI.h
@@ -494,6 +494,34 @@ public: // ********************************************************************
/** What the ghost asks of a spot before it goes green. One place, because the nudge search has
* to ask exactly the same question or it would offer a spot the ghost then paints red. */
static UnsignedInt placementCheckOptions( void );
+
+ //
+ // Structures ordered but not standing yet. A build order is a message: it crosses the network
+ // and only becomes an object on the ground several frames later, and until it does the spot it
+ // was ordered on is empty ground to every check that decides whether the next click is legal.
+ // Holding shift on a laggy link put two structures on the same square that way, and the ghost
+ // stayed green over a building that was already paid for. So the client remembers its own
+ // orders for a couple of seconds and treats them as standing.
+ //
+ // It is a client-side courtesy, not the rule: the rule is re-asked on the logic side, where the
+ // first structure really does exist by the time the second order arrives.
+ //
+ enum { PENDING_PLACEMENTS = 8 }; ///< orders in flight at once; the oldest is overwritten
+ enum { PENDING_PLACEMENT_FRAMES = 60 }; ///< logic frames one is remembered for - two seconds,
+ /// comfortably longer than any network delay
+
+ /// the ground a structure of this template put down here would stand on
+ static 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 );
+
+ /// remember a structure just ordered here, so the next click can see it
+ void recordPendingPlacement( const ThingTemplate *what, const Coord3D *world, Real angle );
+ /// drop the lot - a new game is not the old one's orders
+ void forgetPendingPlacements( void );
+ /// would a structure here land on one of those?
+ Bool overlapsPendingPlacement( const Coord3D *world, const ThingTemplate *what, Real angle ) const;
enum { PLACEMENT_NUDGE_PATHFINDS = 12 }; ///< unreachable candidates tolerated before the search gives up
/** The offsets that search tries: every cell of a square this many rings across, ordered by
@@ -943,6 +971,16 @@ protected:
const ThingTemplate * m_placeAngleType; ///< the structure that heading was chosen for; another type starts square again
Bool m_placementLegal; ///< last legality verdict for the spot under the structure being placed
Coord3D m_placementNudge; ///< how far the last legality check had to slide the structure to make it fit
+
+ /// a structure ordered here, and the logic frame it was ordered on - see recordPendingPlacement
+ struct PendingPlacement
+ {
+ Region2D 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 ring
+
Int m_selectCount; ///< Number of objects currently "selected"
Int m_maxSelectCount; ///< Max number of objects to select
UnsignedInt m_frameSelectionChanged; ///< Frame when the selection last changed.
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 7893ec1ba..a54c5830f 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1119,6 +1119,7 @@ InGameUI::InGameUI()
m_placementRangeRingUp = FALSE;
m_placementRingRadius = 0.0f;
+ forgetPendingPlacements();
m_hudDisplayString = NULL;
m_incomeDisplayString = NULL;
m_lastIncomeDisplayed = -1;
@@ -1761,6 +1762,14 @@ void InGameUI::handleBuildPlacements( void )
lbc = TheBuildAssistant->isLocationLegalToBuild( &spot, m_pendingPlaceType, angle,
checkOptions, builderObject, NULL );
+ //
+ // and the ground an order of your own has already been placed on but has not landed on
+ // yet - see recordPendingPlacement. Without this the ghost is green over a structure
+ // that is already paid for, which is what a shift-held run of clicks sees on a laggy link.
+ //
+ if( lbc == LBC_OK && overlapsPendingPlacement( &spot, m_pendingPlaceType, angle ) )
+ lbc = LBC_OBJECTS_IN_THE_WAY;
+
//
// Blocked: look for the nearest spot that is not, and move the ghost there - the click
// does the same search (see PlaceEventTranslator), so what you see is where it lands.
@@ -2336,6 +2345,7 @@ void InGameUI::reset( void )
placeBuildAvailable( NULL, NULL );
m_placeAngleOffset = 0.0f;
m_placeAngleType = NULL;
+ forgetPendingPlacements();
// free any message resources allocated
freeMessageResources();
@@ -3736,6 +3746,97 @@ 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. */
+//-------------------------------------------------------------------------------------------------
+void InGameUI::placementFootprint( const ThingTemplate *what, const Coord3D *world, Real angle,
+ Region2D *footprint )
+{
+ what->getTemplateGeometryInfo().get2DBounds( *world, angle, *footprint );
+}
+
+//-------------------------------------------------------------------------------------------------
+/** 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 of the eight goes, which is the one most likely to be standing by now. */
+//-------------------------------------------------------------------------------------------------
+void InGameUI::recordPendingPlacement( const ThingTemplate *what, const Coord3D *world, Real angle )
+{
+ if( what == NULL || world == NULL || TheGameLogic == NULL )
+ return;
+
+ PendingPlacement *pending = &m_pendingPlacement[ m_pendingPlacementAt ];
+ m_pendingPlacementAt = ( m_pendingPlacementAt + 1 ) % PENDING_PLACEMENTS;
+
+ placementFootprint( what, world, angle, &pending->footprint );
+ pending->frame = TheGameLogic->getFrame();
+
+ //
+ // frame zero is a real frame and zero is how an empty slot is spelt, so an order placed on it
+ // is remembered from frame one. It costs one frame of one order in the first thirtieth of a
+ // second of a match, when nothing is built yet.
+ //
+ if( pending->frame == 0 )
+ pending->frame = 1;
+
+} // end recordPendingPlacement
+
+//-------------------------------------------------------------------------------------------------
+/** Would a structure put down here land on one already ordered? Orders older than
+ * PENDING_PLACEMENT_FRAMES are ignored rather than cleared: by then the structure is either
+ * standing, where the ordinary check sees it, or the logic refused the order and the ground is
+ * free again. */
+//-------------------------------------------------------------------------------------------------
+Bool InGameUI::overlapsPendingPlacement( const Coord3D *world, const ThingTemplate *what,
+ Real angle ) const
+{
+ if( what == NULL || world == NULL || TheGameLogic == NULL )
+ return FALSE;
+
+ Region2D mine;
+ placementFootprint( what, world, angle, &mine );
+
+ const UnsignedInt now = TheGameLogic->getFrame();
+
+ for( Int i = 0; i < PENDING_PLACEMENTS; i++ )
+ {
+ const PendingPlacement *pending = &m_pendingPlacement[ i ];
+
+ if( pending->frame == 0 || now < pending->frame ||
+ now - pending->frame > PENDING_PLACEMENT_FRAMES )
+ continue;
+
+ if( footprintsOverlap( &mine, &pending->footprint ) )
+ return TRUE;
+ }
+
+ return FALSE;
+
+} // end overlapsPendingPlacement
+
+//-------------------------------------------------------------------------------------------------
+void InGameUI::forgetPendingPlacements( void )
+{
+ for( Int i = 0; i < PENDING_PLACEMENTS; i++ )
+ m_pendingPlacement[ i ].frame = 0;
+
+ m_pendingPlacementAt = 0;
+
+} // end forgetPendingPlacements
+
//-------------------------------------------------------------------------------------------------
/** NudgeBuildPlacement (Options.ini): the spot under the cursor is blocked - a rock, a neighbour's
* bib, ground too steep by a hair - so find the nearest one that is not and put the structure
@@ -3785,6 +3886,10 @@ Bool InGameUI::nudgePlacementToLegal( Coord3D *world, const ThingTemplate *what,
builderObject, NULL ) != LBC_OK )
continue;
+ // room a structure you have already ordered is on its way to is not room
+ if( overlapsPendingPlacement( &test, what, angle ) )
+ continue;
+
// there is - can the builder actually walk to it?
if( TheBuildAssistant->isLocationLegalToBuild( &test, what, angle, fullOptions,
builderObject, NULL ) == LBC_OK )
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp
index fe0153a3d..60fb66061 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/PlaceEventTranslator.cpp
@@ -343,6 +343,10 @@ GameMessageDisposition PlaceEventTranslator::translateGameMessage(const GameMess
lbc = TheBuildAssistant->isLocationLegalToBuild( &world, build, angle, placeOptions,
builderObj, NULL );
+ // plus the ground your own last click already spent - see recordPendingPlacement
+ if( lbc == LBC_OK && TheInGameUI->overlapsPendingPlacement( &world, build, angle ) )
+ lbc = LBC_OBJECTS_IN_THE_WAY;
+
//
// NudgeBuildPlacement: blocked where they clicked, so build at the nearest spot that
// is not - the same search the ghost was already showing them (InGameUI::update ->
@@ -408,6 +412,14 @@ GameMessageDisposition PlaceEventTranslator::translateGameMessage(const GameMess
} // end if
+ //
+ // The order is on its way; the structure will not be on the ground for another
+ // few frames, and on a network game that is however long the link takes. Hold on
+ // to the spot so the next click in a shift-held run does not put a second
+ // structure on top of this one.
+ //
+ TheInGameUI->recordPendingPlacement( build, &world, angle );
+
pickAndPlayUnitVoiceResponse( TheInGameUI->getAllSelectedDrawables(), placeMsg->getType() );
//
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
index 7dd54eed7..22c91abec 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogicDispatch.cpp
@@ -1637,6 +1637,24 @@ void GameLogic::logicMessageDispatcher( GameMessage *msg, void *userData )
if( TheBuildAssistant->canMakeUnit( constructorObject, place ) != CANMAKE_OK )
break;
+ //
+ // And re-check the ground, which nobody was checking here at all. The client asks
+ // before it sends, and the answer it gets is several frames old by the time the order
+ // arrives - on a network game, as old as the link is slow. So two structures ordered
+ // onto the same spot in that window both found it empty and both went up, one inside
+ // the other; holding shift on a bad connection did it every time. Asked here, the
+ // first one is already standing when the second order lands.
+ //
+ // Only the ground: no pathfind and no shroud, because those two answer differently a
+ // third of a second apart and refusing an order for that would be a new bug. The line
+ // build checks every tile after the first for itself (buildTiledLocations).
+ //
+ if( TheBuildAssistant->isLocationLegalToBuild( &loc, place, angle,
+ BuildAssistant::TERRAIN_RESTRICTIONS |
+ BuildAssistant::NO_OBJECT_OVERLAP,
+ constructorObject, NULL ) != LBC_OK )
+ break;
+
if( msg->getType() == GameMessage::MSG_DOZER_CONSTRUCT )
{
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 1784b8fba..20eb8de78 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -7715,3 +7715,46 @@ TEST(borderless_asks_for_a_windowed_device_the_size_of_the_desktop)
TheWritableGlobalData = saved;
delete scratch;
}
+
+/* A build order is a message, and the structure it orders does not exist until the logic runs it -
+ on a network game, however long the link takes. Until then the client remembers the ground it
+ spent, so a shift-held run of clicks does not put two structures on the same square. What it
+ remembers is the footprint, and two footprints that merely touch are two structures built flush
+ against each other, which is most of a base wall. */
+TEST(two_structures_ordered_onto_the_same_ground_are_one_too_many)
+{
+ Region2D a, b;
+
+ a.lo.x = 0.0f; a.lo.y = 0.0f; a.hi.x = 40.0f; a.hi.y = 40.0f;
+
+ // itself, obviously
+ CHECK( 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ CHECK( InGameUI::footprintsOverlap( &a, &b ) );
+ CHECK( InGameUI::footprintsOverlap( &b, &a ) );
+
+ //
+ // 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
+ //
+ CHECK( (Int)InGameUI::PENDING_PLACEMENT_FRAMES >= 30 );
+ CHECK( (Int)InGameUI::PENDING_PLACEMENTS >= 2 );
+}