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(gameclient): repair input, radar and command bar defects

Olcay Seyganb7632fb26bef
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/Radar.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/Radar.cpp
index c940c9e15..75deb9c87 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/Radar.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/Radar.cpp
@@ -720,6 +720,11 @@ Bool Radar::localPixelToRadar( const ICoord2D *pixel, ICoord2D *radar )
if( pixel == NULL || radar == NULL )
return FALSE;
+ // if we have no radar window can't do the conversion (m_radarWindow is NULL between maps and
+ // in the shell; the sibling screenPixelToWorld below has always checked this, this one did not)
+ if( m_radarWindow == NULL )
+ return FALSE;
+
// get window size of the radar
ICoord2D size;
m_radarWindow->winGetSize( &size.x, &size.y );
@@ -736,9 +741,11 @@ Bool Radar::localPixelToRadar( const ICoord2D *pixel, ICoord2D *radar )
Int scaledWidth = lr.x - ul.x;
Int scaledHeight = lr.y - ul.y;
- // if the pixel is outsize of the adjusted radar area there are no logical coords
- if( pixel->x < ul.x || pixel->x > lr.x ||
- pixel->y < ul.y || pixel->y > lr.y )
+ // if the pixel is outsize of the adjusted radar area there are no logical coords.
+ // the upper bound is exclusive: at pixel->x == lr.x the conversion below yields exactly
+ // RADAR_CELL_WIDTH, one cell past the grid, and the caller maps it outside the map.
+ if( pixel->x < ul.x || pixel->x >= lr.x ||
+ pixel->y < ul.y || pixel->y >= lr.y )
return FALSE;
if( scaledWidth >= scaledHeight )
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp
index 045a93bf4..2eec6e091 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp
@@ -545,7 +545,9 @@ void ControlBar::populatePurchaseScience( Player* player )
}
*/
- win = TheWindowManager->winGetWindowFromId( m_contextParent[ CP_PURCHASE_SCIENCE ], TheNameKeyGenerator->nameToKey( "GeneralsExpPoints.wnd:ProgressBarExperience" ) );
+ // hash the name once, not on every render frame this panel is open
+ static const NameKeyType key_progressBarExperience = TheNameKeyGenerator->nameToKey( "GeneralsExpPoints.wnd:ProgressBarExperience" );
+ win = TheWindowManager->winGetWindowFromId( m_contextParent[ CP_PURCHASE_SCIENCE ], key_progressBarExperience );
if(win)
{
Int progress;
@@ -604,7 +606,9 @@ void ControlBar::updateContextPurchaseScience( void )
{
GameWindow *win =NULL;
Player *player = ThePlayerList->getLocalPlayer();
- win = TheWindowManager->winGetWindowFromId( m_contextParent[ CP_PURCHASE_SCIENCE ], TheNameKeyGenerator->nameToKey( "GeneralsExpPoints.wnd:ProgressBarExperience" ) );
+ // hash the name once, not on every render frame this panel is open
+ static const NameKeyType key_progressBarExperience = TheNameKeyGenerator->nameToKey( "GeneralsExpPoints.wnd:ProgressBarExperience" );
+ win = TheWindowManager->winGetWindowFromId( m_contextParent[ CP_PURCHASE_SCIENCE ], key_progressBarExperience );
if(win)
{
Int progress;
@@ -1588,7 +1592,8 @@ void ControlBar::update( void )
- if( !m_buildToolTipLayout->isHidden())
+ // every other use of m_buildToolTipLayout is null-guarded; this one was not
+ if( m_buildToolTipLayout && !m_buildToolTipLayout->isHidden())
{
m_buildToolTipLayout->runUpdate();
m_showBuildToolTipLayout = FALSE;
@@ -1603,8 +1608,14 @@ void ControlBar::update( void )
// if we're an observer, don't do the complete update
if( m_isObserverCommandBar)
{
- if((TheGameLogic->getFrame() % (LOGICFRAMES_PER_SECOND/2)) == 0)
+ // same per-render-frame retrigger as the flash above: once per logic frame is enough
+ static UnsignedInt s_lastObserverFrame = 0xffffffff;
+ const UnsignedInt observerNow = TheGameLogic->getFrame();
+ if( (observerNow % (LOGICFRAMES_PER_SECOND/2)) == 0 && observerNow != s_lastObserverFrame )
+ {
+ s_lastObserverFrame = observerNow;
populateObserverInfoWindow();
+ }
Drawable *drawToEvaluateFor = NULL;
Bool multiSelect = FALSE;
@@ -1628,6 +1639,17 @@ void ControlBar::update( void )
}
+ //
+ // ControlBar::update runs once per RENDER frame, so a bare "getFrame() % 10 == 0" fires on
+ // every render frame that lands inside that one logic frame - the flash count then burned
+ // down several times per tick and the button stopped flashing early. Latch it to the frame.
+ //
+ static UnsignedInt s_lastFlashFrame = 0xffffffff;
+ const UnsignedInt flashNow = TheGameClient->getFrame();
+ const Bool flashTick = (flashNow % 10 == 0) && (flashNow != s_lastFlashFrame);
+ if( flashTick )
+ s_lastFlashFrame = flashNow;
+
// check flashing
if( m_flash )
{
@@ -1640,7 +1662,7 @@ void ControlBar::update( void )
const CommandButton *commandButton = (const CommandButton *)GadgetButtonGetData(button);
if( commandButton != NULL )
{
- if( commandButton->getFlashCount() > 0 && TheGameClient->getFrame() % 10 == 0 )
+ if( commandButton->getFlashCount() > 0 && flashTick )
{
if( commandButton->getFlashCount() % 2 == 0 )
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp
index e714f8ad1..739d29ccf 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommand.cpp
@@ -1142,7 +1142,10 @@ CommandAvailability ControlBar::getCommandAvailability( const CommandButton *com
return COMMAND_RESTRICTED;
}
- Bool queueMaxed = pu ? ( pu->getProductionCount() == MAX_BUILD_QUEUE_BUTTONS ) : FALSE;
+ // >=, not ==: the production queue is far deeper than the number of buttons and a shift-click
+ // queues several at once, so an exact-equality test greyed the button out at exactly
+ // MAX_BUILD_QUEUE_BUTTONS and then stopped firing once the count stepped past it.
+ Bool queueMaxed = pu ? ( pu->getProductionCount() >= MAX_BUILD_QUEUE_BUTTONS ) : FALSE;
switch( command->getCommandType() )
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp
index ba4d1a0ac..5408c3d45 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarCommandProcessing.cpp
@@ -754,15 +754,20 @@ CBCommandStatus ControlBar::processCommandUI( GameWindow *control,
case GUI_COMMAND_EXIT_CONTAINER:
{
Int i;
- ObjectID objID;
+ ObjectID objID = INVALID_ID;
//
// find the object ID that wants to exit by scanning through the transport data and looking
// for the matching control button
//
+ // (this used to run to the end of the array without breaking and without initializing
+ // objID, so a miss fed garbage to findObjectByID and then wrote one past m_containData)
for( i = 0; i < MAX_COMMANDS_PER_SET; i++ )
if( m_containData[ i ].control == control )
+ {
objID = m_containData[ i ].objectID;
+ break;
+ }
// get the actual object
Object *objWantingExit = TheGameLogic->findObjectByID( objID );
@@ -776,8 +781,11 @@ CBCommandStatus ControlBar::processCommandUI( GameWindow *control,
// cycle of the UI will repopulate any buttons as the contents of objects
// change so this is only an edge case that will be visually corrected next frame
//
- m_containData[ i ].control = NULL;
- m_containData[ i ].objectID = INVALID_ID;
+ if( i < MAX_COMMANDS_PER_SET )
+ {
+ m_containData[ i ].control = NULL;
+ m_containData[ i ].objectID = INVALID_ID;
+ }
break; // exit case
} // end if
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarScheme.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarScheme.cpp
index 2d8ea7745..8b0bfac00 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarScheme.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarScheme.cpp
@@ -1004,7 +1004,9 @@ void ControlBarSchemeManager::preloadAssets( TimeOfDay timeOfDay )
if (CBScheme->m_rightHUDImage)
{
- TheDisplay->preloadTextureAssets(CBScheme->m_buttonQueueImage->getFilename());
+ // was preloading m_buttonQueueImage here (copy-paste), which also meant a NULL deref
+ // for a scheme that sets RightHUD but no button queue image.
+ TheDisplay->preloadTextureAssets(CBScheme->m_rightHUDImage->getFilename());
}
for (Int layer = 0; layer < MAX_CONTROL_BAR_SCHEME_IMAGE_LAYERS; ++layer)
@@ -1063,8 +1065,10 @@ void ControlBarSchemeManager::setControlBarScheme(AsciiString schemeName)
if(tempScheme)
{
// setup the multiplyer value
- m_multiplyer.x = TheDisplay->getWidth() / tempScheme->m_ScreenCreationRes.x;
- m_multiplyer.y = TheDisplay->getHeight() / tempScheme->m_ScreenCreationRes.y;
+ // (cast like the sibling below: this was an Int/Int divide, so any resolution that is not
+ // an exact multiple of the scheme's authoring resolution scaled by 1.0 or 0.)
+ m_multiplyer.x = TheDisplay->getWidth() / (Real)tempScheme->m_ScreenCreationRes.x;
+ m_multiplyer.y = TheDisplay->getHeight() / (Real)tempScheme->m_ScreenCreationRes.y;
m_currentScheme = tempScheme;
}
else
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarStructureInventory.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarStructureInventory.cpp
index 9a4d08c39..79828754e 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarStructureInventory.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBarStructureInventory.cpp
@@ -74,6 +74,12 @@ void ControlBar::populateButtonProc( Object *obj, void *userData )
("Too many objects inside '%s' for the inventory buttons to hold",
info->source->getTemplate()->getName().str()) );
+ // ...and actually stop. In release the assert is gone, so a container holding more than
+ // MAX_STRUCTURE_INVENTORY_BUTTONS occupants wrote past m_containData and repainted the
+ // STOP/EVACUATE buttons that sit right after the inventory slots.
+ if( info->buttonIndex >= MAX_STRUCTURE_INVENTORY_BUTTONS )
+ return;
+
// put object in inventory data
info->self->m_containData[ info->buttonIndex ].control = info->inventoryButtons[ info->buttonIndex ];
info->self->m_containData[ info->buttonIndex ].objectID = obj->getID();
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
index 305ea9f55..48b977334 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/InGameUI.cpp
@@ -1654,11 +1654,18 @@ void InGameUI::update( void )
// frame
//
UnsignedInt currLogicFrame = TheGameLogic->getFrame();
- const int messageTimeout = m_messageDelayMS / LOGICFRAMES_PER_SECOND / 1000;
+ // ms -> logic frames. This used to divide by LOGICFRAMES_PER_SECOND instead of multiplying and
+ // then divide by 1000 again, so with the shipped MessageDelayMS (~5000) it was 5000/30 = 166,
+ // 166/1000 = 0 - every UI message started fading on the frame it was posted.
+ const int messageTimeout = m_messageDelayMS * LOGICFRAMES_PER_SECOND / 1000;
UnsignedByte r, g, b, a;
Int amount;
for( i = MAX_UI_MESSAGES - 1; i >= 0; i-- )
{
+ // removeMessageAtIndex NULLs displayString; without this empty slots counted as expired
+ // every frame and were re-cleared forever.
+ if( m_uiMessages[ i ].displayString == NULL )
+ continue;
if( currLogicFrame - m_uiMessages[ i ].timestamp > messageTimeout )
{
@@ -1692,6 +1699,12 @@ void InGameUI::update( void )
// if the timeis frozen by a script, then we still want the text to display
if(TheScriptEngine->isTimeFrozenScript())
{
+ // NOTE: these are LOGIC-frame counters decremented to fake time passing while the
+ // logic clock is frozen, but InGameUI::update runs once per RENDER frame - so with
+ // rendering uncapped they run down (fps/30)x too fast and cutscene subtitles expire
+ // early. Not gated on the logic frame here because the logic frame is exactly what is
+ // NOT advancing during a script freeze; the fix needs a wall-clock 30Hz source and an
+ // in-game cutscene to verify. See FINDINGS.md 7.2.
m_militarySubtitle->lifetime--;
m_militarySubtitle->blockBeginFrame--;
m_militarySubtitle->incrementOnFrame--;
@@ -1750,6 +1763,13 @@ void InGameUI::update( void )
else
{
// if we've exceeded the allocated number of display strings, this will force us to essentially truncate the remaining text
+ //
+ // roll the index back to the last real line. It used to be left at
+ // MAX_SUBTITLE_LINES, and displayStrings only has that many entries, so the
+ // draw loops (which run i <= currentDisplayString) read displayStrings[4]
+ // and removeMilitarySubtitle then freed that out-of-bounds pointer.
+ //
+ m_militarySubtitle->currentDisplayString = MAX_SUBTITLE_LINES - 1;
m_militarySubtitle->index = m_militarySubtitle->subtitle.getLength();
DEBUG_CRASH(("You're Only Allowed to use %d lines of subtitle text\n",MAX_SUBTITLE_LINES));
}
@@ -2026,8 +2046,11 @@ void InGameUI::message( AsciiString stringManagerLabel, ... )
va_list args;
va_start( args, stringManagerLabel );
WideChar buf[ UnicodeString::MAX_FORMAT_BUF_LEN ];
+ // truncate rather than throw: an uncaught engine exception aborts with 0xC0000409 and no log
+ // at all, so an over-long chat or script message used to be a silent hard crash.
if( _vsnwprintf(buf, sizeof( buf )/sizeof( WideChar ) - 1, stringManagerString.str(), args ) < 0 )
- throw ERROR_OUT_OF_MEMORY;
+ DEBUG_LOG(("InGameUI::message - text truncated to %d characters\n", (Int)(sizeof( buf )/sizeof( WideChar ) - 1)));
+ buf[ sizeof( buf )/sizeof( WideChar ) - 1 ] = 0;
formattedMessage.set( buf );
va_end(args);
@@ -2048,8 +2071,11 @@ void InGameUI::message( UnicodeString format, ... )
va_list args;
va_start( args, format );
WideChar buf[ UnicodeString::MAX_FORMAT_BUF_LEN ];
+ // truncate rather than throw: an uncaught engine exception aborts with 0xC0000409 and no log
+ // at all, so an over-long chat or script message used to be a silent hard crash.
if( _vsnwprintf(buf, sizeof( buf )/sizeof( WideChar ) - 1, format.str(), args ) < 0 )
- throw ERROR_OUT_OF_MEMORY;
+ DEBUG_LOG(("InGameUI::message - text truncated to %d characters\n", (Int)(sizeof( buf )/sizeof( WideChar ) - 1)));
+ buf[ sizeof( buf )/sizeof( WideChar ) - 1 ] = 0;
formattedMessage.set( buf );
va_end(args);
@@ -2070,8 +2096,11 @@ void InGameUI::messageColor( const RGBColor *rgbColor, UnicodeString format, ...
va_list args;
va_start( args, format );
WideChar buf[ UnicodeString::MAX_FORMAT_BUF_LEN ];
+ // truncate rather than throw: an uncaught engine exception aborts with 0xC0000409 and no log
+ // at all, so an over-long chat or script message used to be a silent hard crash.
if( _vsnwprintf(buf, sizeof( buf )/sizeof( WideChar ) - 1, format.str(), args ) < 0 )
- throw ERROR_OUT_OF_MEMORY;
+ DEBUG_LOG(("InGameUI::message - text truncated to %d characters\n", (Int)(sizeof( buf )/sizeof( WideChar ) - 1)));
+ buf[ sizeof( buf )/sizeof( WideChar ) - 1 ] = 0;
formattedMessage.set( buf );
va_end(args);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/Input/Keyboard.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/Input/Keyboard.cpp
index 5b49309c3..d5a87b02a 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/Input/Keyboard.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/Input/Keyboard.cpp
@@ -126,6 +126,17 @@ void Keyboard::updateKeys( void )
}
while( m_keys[ index ].key == KEY_LOST );
+
+ //
+ // keep the terminator slot free. This loop had no bound at all against NUM_KEYS, so a
+ // burst of more than 256 events in one frame walked the write cursor off m_keys and
+ // straight into m_keyStatus, the very next member - silent key-state corruption.
+ //
+ if( index >= NUM_KEYS - 1 )
+ {
+ m_keys[ NUM_KEYS - 1 ].key = KEY_NONE;
+ break;
+ }
}
while( m_keys[ index++ ].key != KEY_NONE );
@@ -227,11 +238,14 @@ Bool Keyboard::checkKeyRepeat( void )
m_keys[ index ].status = KeyboardIO::STATUS_UNUSED;
// Set End Flag
- m_keys[ ++index ].key = KEY_NONE;
+ if( index + 1 < NUM_KEYS )
+ m_keys[ ++index ].key = KEY_NONE;
- // Set all keys as new to prevent multiple keys repeating
- for( index = 0; index< NUM_KEYS; index++ )
- m_keyStatus[ index ].sequence = m_inputFrame;
+ // Set all keys as new to prevent multiple keys repeating.
+ // (own counter: this loop used to reuse `index`, the live write cursor into
+ // m_keys, and only got away with it because of the break below.)
+ for( Int resetIndex = 0; resetIndex < NUM_KEYS; resetIndex++ )
+ m_keyStatus[ resetIndex ].sequence = m_inputFrame;
// Set repeated key so it will repeat again in two frames
m_keyStatus[ key ].sequence = m_inputFrame - (Keyboard::KEY_REPEAT_DELAY + 2);
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
index 4be502295..9d7c44282 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/HotKey.cpp
@@ -100,9 +100,13 @@ GameMessageDisposition HotKeyTranslator::translateGameMessage(const GameMessage
// the label hotkey is meant to be that click's equal. Ctrl and alt still block the hotkey.
if(newModState & ~SHIFT)
return disp;
- WideChar key = TheKeyboard->getPrintableKey(msg->getArgument(0)->integer, 0);
+ // NUL-terminate it: UnicodeString::set() runs wcslen over what it is given, and this used
+ // to hand it a single un-terminated WideChar on the stack.
+ WideChar key[2];
+ key[0] = TheKeyboard->getPrintableKey(msg->getArgument(0)->integer, 0);
+ key[1] = 0;
UnicodeString uKey;
- uKey.set(&key);
+ uKey.set(key);
AsciiString aKey;
aKey.translate(uKey);
if(TheHotKeyManager && TheHotKeyManager->executeHotKey(aKey))
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp
index ee18ed184..36a62f2e5 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/LookAtXlat.cpp
@@ -370,8 +370,12 @@ GameMessageDisposition LookAtTranslator::translateGameMessage(const GameMessage
for ( ;spin < 0; spin++ )
TheTacticalView->zoomOut();
}
+ break; // without this the case fell into MSG_META_OPTIONS below and every wheel
+ // notch called stopScrolling(), killing zoom-while-panning and leaving
+ // m_isScrolling/m_scrollType torn.
}
-
+
+
//-----------------------------------------------------------------------------
case GameMessage::MSG_META_OPTIONS:
{
diff --git a/GeneralsMD/Code/Main/WinMain.cpp b/GeneralsMD/Code/Main/WinMain.cpp
index 0f65a3d19..875e45fb6 100644
--- a/GeneralsMD/Code/Main/WinMain.cpp
+++ b/GeneralsMD/Code/Main/WinMain.cpp
@@ -369,10 +369,14 @@ LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
// Prevent moving/sizing and power loss in fullscreen mode
switch( wParam )
{
+ // a bare left Alt opens the system menu and its modal loop stalls the game, so
+ // swallow it in windowed mode too (Alt is also the waypoint modifier).
+ case SC_KEYMENU:
+ return 1;
+
case SC_MOVE:
case SC_SIZE:
case SC_MAXIMIZE:
- case SC_KEYMENU:
case SC_MONITORPOWER:
if( FALSE == ApplicationIsWindowed )
return 1;