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): let go of the modifier first and the key still comes up

Olcay Seygan68e8b718d70e
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/KeyDownInfo.h b/GeneralsMD/Code/GameEngine/Include/GameClient/KeyDownInfo.h
new file mode 100644
index 000000000..57229bb40
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/KeyDownInfo.h
@@ -0,0 +1,109 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** This program is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation, either version 3 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// KeyDownInfo.h //////////////////////////////////////////////////////////////////////////////////
+// Which modifier combinations a key is currently held down with.
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#ifndef _KEY_DOWN_INFO_H_
+#define _KEY_DOWN_INFO_H_
+
+#include "Lib/BaseType.h"
+#include "GameClient/KeyDefs.h"
+
+/**
+ * A command bound to Ctrl+X with an UP transition only fired when X came up while Ctrl was still
+ * held. Let go of Ctrl first and the key's own release arrived with no Ctrl in the modifier
+ * state, the record no longer matched, and whatever the DOWN had switched on stayed on.
+ *
+ * This remembers, for one key, every modifier combination it was pressed with, so the release
+ * can be recognised whichever of the two the player lets go of first. Ctrl, Alt and Shift make
+ * seven combinations with at least one of them set, and one bit each is all this needs.
+ *
+ * The modifier values are the KEY_STATE_L* bits, which are not consecutive, so slots are
+ * numbered by a packed Ctrl/Alt/Shift triple rather than by the mask itself.
+ */
+class KeyDownInfo
+{
+public:
+
+ enum { MOD_STATE_COUNT = 7 };
+
+ KeyDownInfo() : m_modStateBits( 0 ) {}
+
+ /// Slot number for a modifier mask, or -1 when the mask holds none of the three.
+ static Int indexOfModState( Int modState )
+ {
+ const Int packed = ( ( modState & KEY_STATE_LCONTROL ) ? 1 : 0 )
+ | ( ( modState & KEY_STATE_LSHIFT ) ? 2 : 0 )
+ | ( ( modState & KEY_STATE_LALT ) ? 4 : 0 );
+ return packed ? packed - 1 : -1;
+ }
+
+ /// The modifier mask a slot stands for, or zero when the slot number is out of range.
+ static Int modStateAtIndex( Int index )
+ {
+ if( index < 0 || index >= MOD_STATE_COUNT )
+ return 0;
+
+ const Int packed = index + 1;
+ return ( ( packed & 1 ) ? KEY_STATE_LCONTROL : 0 )
+ | ( ( packed & 2 ) ? KEY_STATE_LSHIFT : 0 )
+ | ( ( packed & 4 ) ? KEY_STATE_LALT : 0 );
+ }
+
+ Bool isKeyDown() const { return m_modStateBits != 0; }
+
+ Bool hasModStateAtIndex( Int index ) const
+ {
+ return index >= 0 && index < MOD_STATE_COUNT && ( m_modStateBits & ( 1 << index ) ) != 0;
+ }
+
+ Bool hasModState( Int modState ) const
+ {
+ return hasModStateAtIndex( indexOfModState( modState ) );
+ }
+
+ void setModState( Int modState )
+ {
+ const Int index = indexOfModState( modState );
+ if( index >= 0 )
+ m_modStateBits |= (UnsignedByte)( 1 << index );
+ }
+
+ void clearModStateAtIndex( Int index )
+ {
+ if( index >= 0 && index < MOD_STATE_COUNT )
+ m_modStateBits &= (UnsignedByte)~( 1 << index );
+ }
+
+ void clearModState( Int modState )
+ {
+ clearModStateAtIndex( indexOfModState( modState ) );
+ }
+
+ void clear() { m_modStateBits = 0; }
+
+private:
+
+ UnsignedByte m_modStateBits; ///< one bit per Ctrl/Alt/Shift combination the key is held with
+};
+
+#endif // _KEY_DOWN_INFO_H_
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/MetaEvent.h b/GeneralsMD/Code/GameEngine/Include/GameClient/MetaEvent.h
index 2bab4ac00..e8cca93de 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/MetaEvent.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/MetaEvent.h
@@ -32,6 +32,7 @@
#include "Common/SubsystemInterface.h"
#include "GameClient/InGameUI.h"
+#include "GameClient/KeyDownInfo.h"
enum MappableKeyCategories
@@ -343,6 +344,11 @@ private:
Int m_lastKeyDown; // really a MappableKeyType
Int m_lastModState; // really a MappableKeyModState
+ // Which modifier combinations each key is currently held down with, so that a release can be
+ // matched whichever of the key and the modifier the player lets go of first.
+ enum { NUM_MAPPABLE_KEYS = 256 };
+ KeyDownInfo m_keyDownInfos[NUM_MAPPABLE_KEYS];
+
enum { NUM_MOUSE_BUTTONS = 3 };
ICoord2D m_mouseDownPosition[NUM_MOUSE_BUTTONS];
Bool m_nextUpShouldCreateDoubleClick[NUM_MOUSE_BUTTONS];
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp
index b4852cef7..6b5b64a28 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/MessageStream/MetaEvent.cpp
@@ -528,9 +528,54 @@ GameMessageDisposition MetaEventTranslator::translateGameMessage(const GameMessa
return DESTROY_MESSAGE;
}
+ //
+ // A command bound to a key plus a modifier, with an UP transition, used to fire only when
+ // the key came up while the modifier was still held. Letting go of Ctrl first meant the
+ // key's own release carried no Ctrl, the record no longer matched, and whatever the DOWN
+ // had switched on stayed on. Every key remembers the combinations it was pressed with, so
+ // a modifier release can finish them off in whatever order the player let go.
+ //
+ const Bool isModifierKey = ( key == KEY_LCTRL || key == KEY_RCTRL ||
+ key == KEY_LSHIFT || key == KEY_RSHIFT ||
+ key == KEY_LALT || key == KEY_RALT );
+
+ if( isModifierKey && ( keyState & KEY_STATE_UP ) )
+ {
+ for( Int keyIndex = 0; keyIndex < NUM_MAPPABLE_KEYS; ++keyIndex )
+ {
+ KeyDownInfo &keyDownInfo = m_keyDownInfos[ keyIndex ];
+ if( !keyDownInfo.isKeyDown() )
+ continue;
+
+ for( Int modIndex = 0; modIndex < KeyDownInfo::MOD_STATE_COUNT; ++modIndex )
+ {
+ if( !keyDownInfo.hasModStateAtIndex( modIndex ) )
+ continue;
+
+ const Int heldModState = KeyDownInfo::modStateAtIndex( modIndex );
+ if( ( newModState & heldModState ) == heldModState )
+ continue; // every modifier of that combination is still down
+
+ keyDownInfo.clearModStateAtIndex( modIndex );
+
+ for( const MetaMapRec *map = TheMetaMap->getFirstMetaMapRec(); map; map = map->m_next )
+ {
+ if( map->m_key != keyIndex || map->m_transition != UP )
+ continue;
+
+ const Int wantedModState = metaIgnoresShift( map ) ? ( heldModState & ~SHIFT ) : heldModState;
+ if( map->m_modState != wantedModState )
+ continue;
+
+ TheMessageStream->appendMessage( map->m_meta );
+ }
+ }
+ }
+ }
+
for (const MetaMapRec *map = TheMetaMap->getFirstMetaMapRec(); map; map = map->m_next)
{
- DEBUG_ASSERTCRASH(map->m_meta > GameMessage::MSG_BEGIN_META_MESSAGES &&
+ DEBUG_ASSERTCRASH(map->m_meta > GameMessage::MSG_BEGIN_META_MESSAGES &&
map->m_meta < GameMessage::MSG_END_META_MESSAGES, ("hmm, expected only meta-msgs here"));
//
@@ -627,6 +672,15 @@ GameMessageDisposition MetaEventTranslator::translateGameMessage(const GameMessa
+ // remember, or forget, that this key is held with these modifiers
+ if( !isModifierKey && newModState != NONE && key >= 0 && key < NUM_MAPPABLE_KEYS )
+ {
+ if( keyState & KEY_STATE_DOWN )
+ m_keyDownInfos[ key ].setModState( newModState );
+ else
+ m_keyDownInfos[ key ].clearModState( newModState );
+ }
+
if (t == GameMessage::MSG_RAW_KEY_DOWN)
{
m_lastKeyDown = key;
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 92c0b2f67..1303450a4 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -43,6 +43,7 @@
#include "Common/Energy.h"
#include "Common/RandomValue.h"
#include "GameClient/ClickTolerance.h"
+#include "GameClient/KeyDownInfo.h"
#include "GameLogic/ScenarioDrill.h"
#include "Common/ControlServer.h"
#include "GameLogic/LogicRandomValue.h"
@@ -10400,3 +10401,58 @@ TEST(click_tolerance_is_a_radius_in_time_screen_and_world)
CHECK( ClickTolerance_isClick( 0.0f, 0.0f, 20.0f, 0, screenTolerance, cameraTolerance, msTolerance ) );
CHECK( !ClickTolerance_isClick( 0.0f, 0.0f, 20.5f, 0, screenTolerance, cameraTolerance, msTolerance ) );
}
+
+/* Which modifiers a key is held with.
+ *
+ * The modifier masks are the KEY_STATE_L* bits, which are 0x04, 0x10 and 0x40 - not consecutive,
+ * so the seven combinations cannot index anything directly. Getting that mapping wrong is silent:
+ * a released Ctrl would clear the wrong slot and the key's UP command would fire for a combination
+ * that is still held, or never fire at all.
+ */
+TEST(key_down_info_remembers_each_modifier_combination)
+{
+ const Int CTRL_MASK = KEY_STATE_LCONTROL;
+ const Int SHIFT_MASK = KEY_STATE_LSHIFT;
+ const Int ALT_MASK = KEY_STATE_LALT;
+
+ // the seven combinations round-trip through their slot numbers, and nothing shares a slot
+ Int seen = 0;
+ const Int combos[ KeyDownInfo::MOD_STATE_COUNT ] = {
+ CTRL_MASK, SHIFT_MASK, ALT_MASK,
+ CTRL_MASK | SHIFT_MASK, CTRL_MASK | ALT_MASK, SHIFT_MASK | ALT_MASK,
+ CTRL_MASK | SHIFT_MASK | ALT_MASK
+ };
+ for( Int i = 0; i < KeyDownInfo::MOD_STATE_COUNT; ++i )
+ {
+ const Int index = KeyDownInfo::indexOfModState( combos[ i ] );
+ CHECK( index >= 0 && index < KeyDownInfo::MOD_STATE_COUNT );
+ CHECK_EQ( KeyDownInfo::modStateAtIndex( index ), combos[ i ] );
+ CHECK( ( seen & ( 1 << index ) ) == 0 );
+ seen |= 1 << index;
+ }
+ CHECK_EQ( seen, ( 1 << KeyDownInfo::MOD_STATE_COUNT ) - 1 );
+
+ // no modifier at all has no slot, and neither does a stray bit such as caps lock
+ CHECK_EQ( KeyDownInfo::indexOfModState( 0 ), -1 );
+ CHECK_EQ( KeyDownInfo::indexOfModState( KEY_STATE_CAPSLOCK ), -1 );
+
+ // a key pressed under two different combinations remembers both, and releasing one leaves
+ // the other alone - this is the case the old single "last modifier state" could not hold
+ KeyDownInfo info;
+ CHECK( !info.isKeyDown() );
+
+ info.setModState( CTRL_MASK );
+ info.setModState( CTRL_MASK | SHIFT_MASK );
+ CHECK( info.isKeyDown() );
+ CHECK( info.hasModState( CTRL_MASK ) );
+ CHECK( info.hasModState( CTRL_MASK | SHIFT_MASK ) );
+ CHECK( !info.hasModState( ALT_MASK ) );
+
+ info.clearModState( CTRL_MASK );
+ CHECK( !info.hasModState( CTRL_MASK ) );
+ CHECK( info.hasModState( CTRL_MASK | SHIFT_MASK ) );
+ CHECK( info.isKeyDown() );
+
+ info.clearModState( CTRL_MASK | SHIFT_MASK );
+ CHECK( !info.isKeyDown() );
+}