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(gameclient): let a health bar answer a click for its unit
Olcay Seygan7f89d133a97e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex cd3a0873d..3a1655061 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -336,6 +336,7 @@ found and fixed — EA's own, not port damage.**- A vehicle's load stays private.- Lamps, barrels, rocks and bushes no longer wear health bars.- Bridges no longer float a health bar over the middle of the river. There was never anything under it: the span is part of the terrain, and the bar belonged to an invisible marker standing in the water.+- The bar is part of the unit now: click it and you select whoever it belongs to. Zoomed out, an infantryman is a few pixels of helmet under a bar that is far easier to hit, and a unit half behind a building still has its bar in the clear. It only answers clicks that would have missed everything anyway, so it never takes a click away from the unit you were actually pointing at, and right-clicking bare ground under a bar still orders a move.- The city itself stops wearing them too. A building nobody owns shows a bar only if there is something to do about it - troops can go inside, or it can be taken. A row of houses does not, and neither does the concrete apron each of them stands on, which is a separate two-thousand-point object that was drawing a second bar down at street level.## Twenty-two-year-old bugs, found by testingdiff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.h b/GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.hindex 19af0c659..5df012fb3 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/GameClient.h@@ -64,6 +64,18 @@ typedef void (*GameClientFuncPtr)( Drawable *draw, void *userData );typedef std::vector<Drawable*> DrawablePtrVector;+//-----------------------------------------------------------------------------+/** Does this screen pixel land on a health bar's click target? `pad` widens the drawn rectangle+ * on all four sides - a bar is three pixels tall, which is a bar to read, not a bar to hit. */+//-----------------------------------------------------------------------------+extern Bool GameClient_healthBarPickHit( const IRegion2D& region, const ICoord2D& screen, Int pad );++//-----------------------------------------------------------------------------+/** Squared pixel distance from the click to the centre of a bar. When bars overlap - a column of+ * tanks seen side on - the one whose middle the cursor is nearest wins. */+//-----------------------------------------------------------------------------+extern Int GameClient_healthBarPickScore( const IRegion2D& region, const ICoord2D& screen );+//-----------------------------------------------------------------------------/** The Client message dispatcher, this is the last "translator" on the message* stream before the messages go to the network for processing. It gives@@ -114,6 +126,11 @@ public:CommandTranslator::CommandEvaluateType cmdType );void addTextBearingDrawable( Drawable *tbd );void flushTextBearingDrawables( void);++ void addHealthBarPickRegion( Drawable *draw, const IRegion2D& region ); ///< a drawable that just drew a health bar, and where+ void clearHealthBarPickRegions( void ); ///< start a fresh frame's worth+ Drawable *pickDrawableByHealthBar( const ICoord2D *screen ); ///< the drawable whose health bar is under this pixel, if any+void updateFakeDrawables(void);virtual void removeFromRayEffects( Drawable *draw ); ///< remove the drawable from the ray effect system if present@@ -211,6 +228,22 @@ private:typedef std::list< Drawable* > TextBearingDrawableList;typedef TextBearingDrawableList::iterator TextBearingDrawableListIterator;TextBearingDrawableList m_textBearingDrawableList; ///< the drawables that have registered here during drawablepostdraw++ //+ // Where each health bar was drawn this frame. The bars are a 2D overlay - they are not in the+ // scene the pick ray is cast against - so a click that lands on one hits nothing. Recording the+ // rectangles as they are drawn costs nothing (they are already computed) and inherits every rule+ // the drawing has: shrouded, hidden and scenery objects never register, so they can never be+ // picked this way.+ //+ struct HealthBarPickEntry+ {+ IRegion2D region;+ DrawableID id;+ };+ typedef std::vector< HealthBarPickEntry > HealthBarPickList;+ typedef HealthBarPickList::iterator HealthBarPickListIterator;+ HealthBarPickList m_healthBarPickList;};//Kris: Try not to use this if possible. In every case I found in the code base, the status was always Drawable::SELECTED.diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cppindex 4d4fd08fa..d2b5b59ea 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp@@ -4246,6 +4246,14 @@ void Drawable::drawHealthBar(const IRegion2D* healthBarRegion)Real healthBoxHeight = max(3, healthBarRegion->hi.y - healthBarRegion->lo.y);Real healthBoxOutlineSize = 1.0f;+ //+ // past every bail-out above, so this drawable really is wearing a bar this frame: hand the+ // rectangle to the client so a click that lands on it can find its way back here. The bar is+ // 2D overlay and the pick ray only knows about the 3D scene, so without this a click on a bar+ // hits nothing at all.+ //+ TheGameClient->addHealthBarPickRegion( this, *healthBarRegion );+// draw the health box outlineTheDisplay->drawOpenRect( healthBarRegion->lo.x, healthBarRegion->lo.y, healthBoxWidth, healthBoxHeight,healthBoxOutlineSize, outlineColor );diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cppindex 9c3ca43f6..4a4c11bec 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp@@ -496,6 +496,9 @@ void GameClient::reset( void )m_drawableVector.clear();m_drawableVector.resize(DRAWABLE_HASH_SIZE, NULL);+ // last frame's health bars belong to the map we are leaving+ m_healthBarPickList.clear();+// need to reset the in game UI to clear drawables before they are destroyedTheInGameUI->reset();@@ -1089,6 +1092,79 @@ void GameClient::flushTextBearingDrawables( void )m_textBearingDrawableList.clear();}+// ------------------------------------------------------------------------------------------------+Bool GameClient_healthBarPickHit( const IRegion2D& region, const ICoord2D& screen, Int pad )+{+ return screen.x >= region.lo.x - pad && screen.x <= region.hi.x + pad &&+ screen.y >= region.lo.y - pad && screen.y <= region.hi.y + pad;+}++// ------------------------------------------------------------------------------------------------+Int GameClient_healthBarPickScore( const IRegion2D& region, const ICoord2D& screen )+{+ const Int dx = screen.x - (region.lo.x + region.hi.x) / 2;+ const Int dy = screen.y - (region.lo.y + region.hi.y) / 2;++ return dx * dx + dy * dy;+}++// ------------------------------------------------------------------------------------------------+void GameClient::addHealthBarPickRegion( Drawable *draw, const IRegion2D& region )+{+ HealthBarPickEntry entry;++ entry.region = region;+ entry.id = draw->getID();++ m_healthBarPickList.push_back( entry );+}++// ------------------------------------------------------------------------------------------------+void GameClient::clearHealthBarPickRegions( void )+{+ m_healthBarPickList.clear();+}++// ------------------------------------------------------------------------------------------------+/** Pick by health bar. This is a *fallback*: the callers reach it only when the pick ray found+ * nothing at all, so it can never take a click away from the unit whose model was actually under+ * the cursor - a bar floats above its own unit and so sits over whatever is standing behind it,+ * and "I clicked the tank at the back and got the one in front" would be worse than the miss it+ * is fixing. It is also selection-only: the command translators keep the ray, or right-clicking+ * bare ground under a bar would order an attack instead of a move. */+// ------------------------------------------------------------------------------------------------+Drawable *GameClient::pickDrawableByHealthBar( const ICoord2D *screen )+{+ // the bar is three pixels of art; the click target is the bar plus a finger's width of slack+ const Int pad = REAL_TO_INT_CEIL( 3.0f * TheUIScale() );++ Drawable *best = NULL;+ Int bestScore = 0;++ for( HealthBarPickListIterator it = m_healthBarPickList.begin(); it != m_healthBarPickList.end(); ++it )+ {+ if( !GameClient_healthBarPickHit( it->region, *screen, pad ) )+ continue;++ const Int score = GameClient_healthBarPickScore( it->region, *screen );+ if( best != NULL && score >= bestScore )+ continue;++ //+ // the list is last frame's drawing and this is this frame's click, so a drawable that died in+ // between is genuinely gone+ //+ Drawable *draw = findDrawableByID( it->id );+ if( draw == NULL )+ continue;++ best = draw;+ bestScore = score;+ }++ return best;+}+// ------------------------------------------------------------------------------------------------GameMessage::Type GameClient::evaluateContextCommand( Drawable *draw,const Coord3D *pos,diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cppindex 66a303a43..ff6d6b29d 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/SelectionXlat.cpp@@ -453,6 +453,15 @@ GameMessageDisposition SelectionTranslator::translateGameMessage(const GameMessaUnsignedInt pickType = getPickTypesForContext( true /*TheInGameUI->isInForceAttackMode()*/ );Drawable *underCursor = TheTacticalView->pickDrawable( &pixel, TheInGameUI->isInForceAttackMode(), (PickType) pickType );++ //+ // a click on a health bar selects its owner, so the cursor has to say so before the+ // click - the same fallback, in the same order, as the point pick in+ // W3DView::iterateDrawablesInRegion+ //+ if( underCursor == NULL )+ underCursor = TheGameClient->pickDrawableByHealthBar( &pixel );+Object *objUnderCursor = underCursor ? underCursor->getObject() : NULL;if( objUnderCursor && (!objUnderCursor->isEffectivelyDead() || objUnderCursor->isKindOf( KINDOF_ALWAYS_SELECTABLE )) )diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cppindex 2b22a29a4..76c26b203 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp@@ -1901,6 +1901,8 @@ void W3DView::draw( void )/// @todo we might want to consider wiping this iterate out if there is nothing to post draw//TheGameClient->resetRenderedObjectCount();+ // the post draw is where health bars are drawn, and each one records where it landed+ TheGameClient->clearHealthBarPickRegions();TheGameClient->iterateDrawablesInRegion( &axisAlignedRegion, drawablePostDraw, this );TheGameClient->flushTextBearingDrawables();@@ -2299,6 +2301,18 @@ Int W3DView::iterateDrawablesInRegion( IRegion2D *screenRegion,{// Allow all drawables to be picked.onlyDrawableToTest = pickDrawable(&screenRegion->lo, TRUE, (PickType) getPickTypesForContext(TheInGameUI->isInForceAttackMode()));++ //+ // Nothing under the cursor in the scene, so a health bar is allowed to answer for its owner.+ // Every bar is on in this fork, which makes a unit's bar the one part of it that is never+ // hidden behind a building or lost in a crowd - and infantry at full zoom out is a handful of+ // pixels of model under a bar that is easier to hit than the man. Fallback order matters: a+ // bar hangs in the air above its own unit and so overlaps whatever stands behind it, so it may+ // only answer a click that would otherwise have missed everything.+ //+ if (onlyDrawableToTest == NULL)+ onlyDrawableToTest = TheGameClient->pickDrawableByHealthBar(&screenRegion->lo);+if (onlyDrawableToTest == NULL) {return 0;}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex e76b8bcb3..dea8869f6 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -5599,6 +5599,68 @@ TEST(an_owned_structure_always_wears_a_health_bar)CHECK( Drawable_structureShowsHealthBar( FALSE, FALSE, TRUE, FALSE ) == TRUE );}+/** The bar is a 2D overlay drawn after the scene, so the pick ray knows nothing about it and a+ click that lands on one used to hit bare ground. Every bar is on in this fork, which makes it+ the one part of a unit that is never behind a building - and at full zoom out a rifleman is a+ few pixels of model under a bar that is easier to hit than the man. The bar is three pixels+ tall, so the click target is the drawn rectangle plus slack on every side. */+TEST(a_click_lands_on_a_health_bar_plus_its_slack)+{+ IRegion2D bar;+ bar.lo.x = 100;+ bar.lo.y = 50;+ bar.hi.x = 140;+ bar.hi.y = 53;++ ICoord2D onTheBar = { 120, 51 };+ CHECK( GameClient_healthBarPickHit( bar, onTheBar, 0 ) == TRUE );++ // the drawn edges are in+ ICoord2D corner = { 100, 50 };+ CHECK( GameClient_healthBarPickHit( bar, corner, 0 ) == TRUE );++ // four pixels above the bar is a miss on the art and a hit on the target you can actually click+ ICoord2D justAbove = { 120, 46 };+ CHECK( GameClient_healthBarPickHit( bar, justAbove, 0 ) == FALSE );+ CHECK( GameClient_healthBarPickHit( bar, justAbove, 4 ) == TRUE );++ // and the slack is slack, not a free pass: the rest of the screen is on no bar at all+ ICoord2D elsewhere = { 400, 300 };+ CHECK( GameClient_healthBarPickHit( bar, elsewhere, 4 ) == FALSE );+}++/** A column of tanks seen side on stacks its bars on top of each other, so one pixel can be on two+ of them. Whichever bar the cursor is nearest the middle of wins - the alternative is list+ order, which is drawing order, which is nothing the player can see. */+TEST(overlapping_health_bars_go_to_the_one_the_cursor_is_nearest)+{+ IRegion2D nearBar;+ nearBar.lo.x = 100;+ nearBar.lo.y = 52;+ nearBar.hi.x = 140;+ nearBar.hi.y = 55;++ IRegion2D farBar;+ farBar.lo.x = 110;+ farBar.lo.y = 50;+ farBar.hi.x = 150;+ farBar.hi.y = 53;++ // dead centre of the near bar, and inside the far one too+ ICoord2D pixel = { 120, 53 };+ CHECK( GameClient_healthBarPickHit( nearBar, pixel, 0 ) == TRUE );+ CHECK( GameClient_healthBarPickHit( farBar, pixel, 0 ) == TRUE );++ CHECK( GameClient_healthBarPickScore( nearBar, pixel ) < GameClient_healthBarPickScore( farBar, pixel ) );++ // the score is the squared pixel distance to the centre, so it is zero on the middle of a bar+ CHECK_EQ( GameClient_healthBarPickScore( nearBar, pixel ), 0 );++ // three pixels to the right of centre is nine, not three - ordering only, never a distance+ ICoord2D offCentre = { 123, 53 };+ CHECK_EQ( GameClient_healthBarPickScore( nearBar, offCentre ), 9 );+}+/** Ground you have already scouted stays buildable after your units leave it, so a base can beplanned out into the fog and the builder sent to walk there. Shroud - terrain nobody of yourshas ever laid eyes on - is still off limits, which is what stops the map being read through a