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): health bar fill, batch build, queue timings, smooth zoom
Olcay Seygan992ea2fcbab4
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cppindex e1a8ce076..b42e3bd28 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp@@ -3947,6 +3947,11 @@ void Drawable::drawHealthBar(const IRegion2D* healthBarRegion)TheDisplay->drawOpenRect( healthBarRegion->lo.x, healthBarRegion->lo.y, healthBoxWidth, healthBoxHeight,healthBoxOutlineSize, outlineColor );+ // draw a filled bar for the health+ TheDisplay->drawFillRect( healthBarRegion->lo.x + 1, healthBarRegion->lo.y + 1,+ (healthBoxWidth - 2) * healthRatio, healthBoxHeight - 2,+ color );+// selected drawables get a white overline (1px gap above the bar; below collides with the container pips) so they stand out now that all bars are always on// (+1: drawOpenRect covers width+1 columns, drawFillRect only width)if( isSelected() )@@ -3959,7 +3964,12 @@ void Drawable::drawHealthBar(const IRegion2D* healthBarRegion)if( pe ){Int prodY = healthBarRegion->lo.y - 3 - healthBoxHeight;+ // the entry keeps counting past 100% while the finished unit waits for a free exit door, so clampReal pct = pe->getPercentComplete();+ if( pct > 100.0f )+ pct = 100.0f;+ else if( pct < 0.0f )+ pct = 0.0f;TheDisplay->drawOpenRect( healthBarRegion->lo.x, prodY, healthBoxWidth, healthBoxHeight,healthBoxOutlineSize, GameMakeColor( 128, 128, 0, 255 ) );TheDisplay->drawFillRect( healthBarRegion->lo.x + 1, prodY + 1,@@ -3970,10 +3980,15 @@ void Drawable::drawHealthBar(const IRegion2D* healthBarRegion)Int totalFrames = pe->getProductionType() == PRODUCTION_UNIT? pe->getProductionObject()->calcTimeToBuild( player ): pe->getProductionUpgrade()->calcTimeToBuild( player );- // clamp: the entry lingers at >100% while the unit exits the factoryInt secondsLeft = REAL_TO_INT_CEIL( totalFrames * (1.0f - pct * 0.01f) / LOGICFRAMES_PER_SECOND );- if( secondsLeft < 0 )- secondsLeft = 0;++ // everything behind the head of the queue has not started yet, so each costs its full+ // build time (an entry builds its whole quantity at once, so quantity does not scale it)+ Int queuedFrames = 0;+ for( const ProductionEntry *q = pu->nextProduction( pe ); q != NULL; q = pu->nextProduction( q ) )+ queuedFrames += q->getProductionType() == PRODUCTION_UNIT+ ? q->getProductionObject()->calcTimeToBuild( player )+ : q->getProductionUpgrade()->calcTimeToBuild( player );// one shared string: draw() renders immediately, and the manager lives for the whole appstatic DisplayString *prodTimeString = NULL;@@ -3984,8 +3999,13 @@ void Drawable::drawHealthBar(const IRegion2D* healthBarRegion)TheGlobalLanguageData->adjustFontSize( TheInGameUI->getDrawableCaptionPointSize() - 2 ),FALSE ) );}+ // "52s (70s)": this one, then the whole queue. Nothing behind it, no parentheses.UnicodeString text;- text.format( L"%ds", secondsLeft );+ if( queuedFrames > 0 )+ text.format( L"%ds (%ds)", secondsLeft,+ secondsLeft + REAL_TO_INT_CEIL( INT_TO_REAL( queuedFrames ) / LOGICFRAMES_PER_SECOND ) );+ else+ text.format( L"%ds", secondsLeft );if( prodTimeString->getText().compare( text ) != 0 )prodTimeString->setText( text );Int textW, textH;diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cppindex a75a2fa93..6b1d2242b 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp@@ -50,12 +50,16 @@#include "GameClient/GameWindow.h"#include "GameClient/GameWindowManager.h"#include "GameClient/InGameUI.h"+#include "GameClient/Keyboard.h"#include "GameClient/AnimateWindowManager.h"#include "GameLogic/GameLogic.h"#include "GameLogic/Object.h"#include "GameLogic/Module/ProductionUpdate.h"+// how many units a shift-click on a build button queues at once+static const Int SHIFT_BUILD_QUEUE_COUNT = 5;+#ifdef _INTERNAL// for occasional debugging...//#pragma optimize("", off)@@ -396,13 +400,26 @@ CBCommandStatus ControlBar::processCommandUI( GameWindow *control,//const ThingTemplate *whatToBuild = commandButton->getThingTemplate();- // get the "factory" object that is going to make the thing. With several factories- // selected (multi-select context) use the one with the fewest queued items that can- // actually make it, so repeated clicks spread the work over all selected factories- Object *factory = obj;- if( factory == NULL && m_currContext == CB_CONTEXT_MULTI_SELECT )+ // sanity, we must have something to build+ DEBUG_ASSERTCRASH( whatToBuild, ("Undefined BUILD command for object '%s'\n",+ commandButton->getThingTemplate()->getName().str()) );++ // holding shift queues a whole batch with one click+ Int wanted = TheKeyboard->isShift() ? SHIFT_BUILD_QUEUE_COUNT : 1;++ // gather the "factory" objects that could make the thing. With several factories+ // selected (multi-select context) every one that can make it is a candidate, and each+ // unit of the batch goes to the least loaded one, so the work spreads over all of them+ std::vector<Object *> factories;+ std::vector<UnsignedInt> queued; // real production count plus what this click already gave it+ if( obj )+ {+ ProductionUpdateInterface *opu = obj->getProductionUpdateInterface();+ factories.push_back( obj );+ queued.push_back( opu ? opu->getProductionCount() : 0 );+ }+ else if( m_currContext == CB_CONTEXT_MULTI_SELECT ){- UnsignedInt bestCount = 0;const DrawableList *selected = TheInGameUI->getAllSelectedDrawables();for( DrawableListCIt it = selected->begin(); it != selected->end(); ++it ){@@ -410,77 +427,90 @@ CBCommandStatus ControlBar::processCommandUI( GameWindow *control,ProductionUpdateInterface *cpu = candidate ? candidate->getProductionUpdateInterface() : NULL;if( cpu == NULL || TheBuildAssistant->canMakeUnit( candidate, whatToBuild ) != CANMAKE_OK )continue;- if( factory == NULL || cpu->getProductionCount() < bestCount )- {- factory = candidate;- bestCount = cpu->getProductionCount();- }+ factories.push_back( candidate );+ queued.push_back( cpu->getProductionCount() );}// nothing can take it: fall back to the first factory so the usual messages fire- if( factory == NULL && !selected->empty() )- factory = selected->front()->getObject();+ if( factories.empty() && !selected->empty() && selected->front()->getObject() )+ {+ factories.push_back( selected->front()->getObject() );+ queued.push_back( 0 );+ }}- if( factory == NULL )+ if( factories.empty() )break;- // sanity, we must have something to build- DEBUG_ASSERTCRASH( whatToBuild, ("Undefined BUILD command for object '%s'\n",- commandButton->getThingTemplate()->getName().str()) );-- CanMakeType cmt = TheBuildAssistant->canMakeUnit(factory, whatToBuild);-- if (cmt == CANMAKE_NO_MONEY)- {- TheEva->setShouldPlay(EVA_InsufficientFunds);- TheInGameUI->message( "GUI:NotEnoughMoneyToBuild" );- break;- }- else if (cmt == CANMAKE_QUEUE_FULL)- {- TheInGameUI->message( "GUI:ProductionQueueFull" );- break;- }- else if (cmt == CANMAKE_PARKING_PLACES_FULL)- {- TheInGameUI->message( "GUI:ParkingPlacesFull" );- break;- }- else if( cmt == CANMAKE_MAXED_OUT_FOR_PLAYER )+ for( Int built = 0; built < wanted; built++ ){- TheInGameUI->message( "GUI:UnitMaxedOut" );- break;- }- else if (cmt != CANMAKE_OK)- {- DEBUG_ASSERTCRASH( 0, ("Cannot create '%s' because the factory object '%s' returns false for canMakeUnit\n",- whatToBuild->getName().str(),- factory->getTemplate()->getName().str()) );- break;- }+ // least loaded factory takes this one+ Int best = 0;+ for( Int i = 1; i < (Int)factories.size(); i++ )+ if( queued[ i ] < queued[ best ] )+ best = i;+ Object *factory = factories[ best ];- // get the production interface from the factory object- ProductionUpdateInterface *pu = factory->getProductionUpdateInterface();+ CanMakeType cmt = TheBuildAssistant->canMakeUnit(factory, whatToBuild);- // sanity, we can't build things if we can't produce units- if( pu == NULL )- {+ if (cmt == CANMAKE_NO_MONEY)+ {+ TheEva->setShouldPlay(EVA_InsufficientFunds);+ TheInGameUI->message( "GUI:NotEnoughMoneyToBuild" );+ break;+ }+ else if (cmt == CANMAKE_QUEUE_FULL)+ {+ TheInGameUI->message( "GUI:ProductionQueueFull" );+ break;+ }+ else if (cmt == CANMAKE_PARKING_PLACES_FULL)+ {+ TheInGameUI->message( "GUI:ParkingPlacesFull" );+ break;+ }+ else if( cmt == CANMAKE_MAXED_OUT_FOR_PLAYER )+ {+ TheInGameUI->message( "GUI:UnitMaxedOut" );+ break;+ }+ else if (cmt != CANMAKE_OK)+ {+ DEBUG_ASSERTCRASH( 0, ("Cannot create '%s' because the factory object '%s' returns false for canMakeUnit\n",+ whatToBuild->getName().str(),+ factory->getTemplate()->getName().str()) );+ break;+ }- DEBUG_ASSERTCRASH( 0, ("Cannot create '%s' because the factory object '%s' is not capable of producting units\n",- whatToBuild->getName().str(),- factory->getTemplate()->getName().str()) );- break;+ // get the production interface from the factory object+ ProductionUpdateInterface *pu = factory->getProductionUpdateInterface();- } // end if-- // get a new production id to assign to this- ProductionID productionID = pu->requestUniqueUnitID();+ // sanity, we can't build things if we can't produce units+ if( pu == NULL )+ {- // create a message to build this thing+ DEBUG_ASSERTCRASH( 0, ("Cannot create '%s' because the factory object '%s' is not capable of producting units\n",+ whatToBuild->getName().str(),+ factory->getTemplate()->getName().str()) );+ break;++ } // end if++ // get a new production id to assign to this+ ProductionID productionID = pu->requestUniqueUnitID();++ // create a message to build this thing++ GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_QUEUE_UNIT_CREATE );+ msg->appendIntegerArgument( whatToBuild->getTemplateID() );+ msg->appendIntegerArgument( productionID );+ msg->appendObjectIDArgument( factory->getID() ); // which of the selected factories builds it++ //+ // the real production count only catches up once the logic runs these messages, so+ // count it here to keep spreading the rest of the batch+ //+ queued[ best ]++;- GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_QUEUE_UNIT_CREATE );- msg->appendIntegerArgument( whatToBuild->getTemplateID() );- msg->appendIntegerArgument( productionID );- msg->appendObjectIDArgument( factory->getID() ); // which of the selected factories builds it+ } // end for, one message per unit of the batchbreak;diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cppindex 0c89fa10b..73dd5f6be 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp@@ -261,6 +261,10 @@ void W3DView::setOrigin( Int x, Int y)/** @todo This is inefficient. We should construct the matrix directly using vectors. *///-------------------------------------------------------------------------------------------------#define MIN_CAPPED_ZOOM (0.5f) //WST 10.19.2002. JSC integrated 5/20/03.+// how far past the map's default max camera height the player may zoom out by hand.+// only the manual zoom limit is stretched - the default/scripted views still use+// m_maxHeightAboveGround, so a map still opens framed the way its author meant it to.+#define ZOOM_OUT_LIMIT_FACTOR (1.6f)void W3DView::buildCameraTransform( Matrix3D *transform ){Vector3 sourcePos, targetPos;@@ -1361,7 +1365,19 @@ void W3DView::update(void)*/m_terrainHeightUnderCamera = getHeightAroundPos(m_pos.x, m_pos.y);m_currentHeightAboveGround = m_cameraOffset.z * m_zoom - m_terrainHeightUnderCamera;- if (stepTime && TheTerrainLogic && TheGlobalData && TheInGameUI && m_okToAdjustHeight && !TheGameLogic->isGamePaused())+ //+ // The settle below is an exponential approach tuned for one 30Hz step. Gating it on+ // stepTime made the zoom crawl in visible 30Hz jumps on a faster display; run it every+ // render frame instead and convert m_cameraAdjustSpeed into the equivalent rate for the+ // time this frame actually took, so the zoom is smooth and its speed stays the same.+ //+ static DWORD prevZoomStepTime = 0;+ Real zoomSteps = (Real)(nowCameraStepTime - prevZoomStepTime) / (Real)TheW3DFrameLengthInMsec;+ prevZoomStepTime = nowCameraStepTime;+ if (zoomSteps <= 0.0f || zoomSteps > 10.0f)+ zoomSteps = 1.0f; // first frame ever, or a hitch: take one plain step+ Real cameraAdjustSpeed = 1.0f - (Real)pow(1.0f - TheGlobalData->m_cameraAdjustSpeed, zoomSteps);+ if (TheTerrainLogic && TheGlobalData && TheInGameUI && m_okToAdjustHeight && !TheGameLogic->isGamePaused()){Real desiredHeight = (m_terrainHeightUnderCamera + m_heightAboveGround);Real desiredZoom = desiredHeight / m_cameraOffset.z;@@ -1375,9 +1391,9 @@ void W3DView::update(void)if (TheInGameUI->isScrolling()){// if scrolling, only adjust if we're too close or too far- if (m_scrollAmount.length() < m_scrollAmountCutoff || (m_currentHeightAboveGround < m_minHeightAboveGround) || (TheGlobalData->m_enforceMaxCameraHeight && m_currentHeightAboveGround > m_maxHeightAboveGround))+ if (m_scrollAmount.length() < m_scrollAmountCutoff || (m_currentHeightAboveGround < m_minHeightAboveGround) || (TheGlobalData->m_enforceMaxCameraHeight && m_currentHeightAboveGround > m_maxHeightAboveGround * ZOOM_OUT_LIMIT_FACTOR)){- Real zoomAdj = (desiredZoom - m_zoom)*TheGlobalData->m_cameraAdjustSpeed;+ Real zoomAdj = (desiredZoom - m_zoom)*cameraAdjustSpeed;if (fabs(zoomAdj) >= 0.0001) // only do positive{m_zoom += zoomAdj;@@ -1388,7 +1404,7 @@ void W3DView::update(void)else{// we're not scrolling; settle toward desired height above ground- Real zoomAdj = (m_zoom - desiredZoom)*TheGlobalData->m_cameraAdjustSpeed;+ Real zoomAdj = (m_zoom - desiredZoom)*cameraAdjustSpeed;Real zoomAdjAbs = fabs(zoomAdj);if (zoomAdjAbs >= 0.0001 && !didScriptedMovement){@@ -1975,8 +1991,8 @@ void W3DView::setHeightAboveGround(Real z)if (m_heightAboveGround < m_minHeightAboveGround)m_heightAboveGround = m_minHeightAboveGround;- if (m_heightAboveGround > m_maxHeightAboveGround)- m_heightAboveGround = m_maxHeightAboveGround;+ if (m_heightAboveGround > m_maxHeightAboveGround * ZOOM_OUT_LIMIT_FACTOR)+ m_heightAboveGround = m_maxHeightAboveGround * ZOOM_OUT_LIMIT_FACTOR;} // end if