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
fix(gameengine): light the keys that really press the bar
Olcay Seygan676bb2245969
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4f3a6e8ff..8a4d2ae62 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -646,9 +646,9 @@ found and fixed — EA's own, not port damage.**The whole HUD is on the hardware. Four colours carry it, and they mean the same thing everywhere: green is something you have, red is something wrong, white is something ready to fire, and your own player colour is everything else. How bright a key is says how much.-- The command bar is on the letter keys. Every letter the bar has bound lights while that button can be pressed and goes dark the moment it cannot, and a button with a queue dims in step with the clock drawn on it, so the key tells you how far along the tank is.+- The command bar is on the keys that actually press it, whichever input scheme you play on and however you have rebound it. Under Modern that is the grid, Q Z W X and on; under Legacy it is the letter written in each button's name. A key lights while its button can be pressed and goes dark the moment it cannot, and a button with a queue dims in step with the clock drawn on it, so the key tells you how far along the tank is. A builder's two key chord is followed: press Q and the lights move onto the structures Q opened, the same moment the labels on screen do.- The number row is your power. Green keys while there is headroom, fewer as the base fills up, yellow for the last of it, and the whole row blinking red once consumption passes production. That is the brownout that stops your radar and your defences, and you see it before you have gone looking for the meter. A faction with no power plant leaves the row dark rather than pretending to be full.-- The function row is your generals powers, key for key with the strip on screen. Each one brightens as it charges, blinks white for three seconds when it comes ready and then holds steady. A power you have not bought yet stays dark. F7 lit means F7 is worth pressing.+- The function row is your generals powers, the way the keys really reach them: the first press names a row of the strip and the second a power in it. So with nothing pressed, the key of every row holding a power you can use blinks white. Press it and the blinking moves onto the keys of the usable powers in that row. A power still charging shows how far it has got, and a key that reaches nothing stays quiet.- The numpad fills from the bottom as your nearest superweapon charges, then the whole block blinks. The particle cannon arrives on your hand, not in a corner of the screen.- A superweapon going off takes the keyboard, the mouse and the mousepad with it, and each of the three has its own. The nuke sends a thin white shock across the board and then the blast behind it, red at the front, a long stretch of white, orange at the back, all of it breathing like embers. The particle cannon is the spot the beam lands on: a white point that charges in the corner and wanders the keys, throwing blue rings off itself and leaving a blue fire burning down along where it has been. The scud storm is nine landings, which is what the building fires, one after another: a flash, a green ring running out of it, and a pool of something left on the ground. Anybody's launch does it, yours or the one aimed at you, at the moment EVA tells you about it.- The bottom row is your factories, one key each, brightening with whatever they are building. A dark key is a factory standing idle, which is the thing nobody notices until the fight is lost.diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ControlBar.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ControlBar.hindex 73b31760f..67efedbcd 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ControlBar.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ControlBar.h@@ -734,6 +734,30 @@ public:TRUE when there was one to forget. */Bool clearSpecialPowerShortcutRow( void );+ /** What a press would do this instant, for whoever shows the bar somewhere other than the+ screen - the hardware lighting is the one that does. These run the same resolution the two+ press calls above run, chords and all, and stop short of pressing: a second copy of that+ logic kept elsewhere is a copy that goes stale, and this file has been bitten by one already.+ button comes back as the window the press would reach. For a press that only arms a chord+ it is the first window in the armed group that would take the second key, or NULL when the+ group holds nothing to press, so a caller can tell a chord worth starting from an empty one. */+ enum PressOutcome+ {+ PRESS_DOES_NOTHING, ///< no button behind this key right now+ PRESS_ARMS_CHORD, ///< first half of a two key chord+ PRESS_IS_REFUSED, ///< a button is there and it is disabled+ PRESS_FIRES ///< the button would take it+ };+ PressOutcome peekCommandButtonPress( Int index, GameWindow **button );+ PressOutcome peekSpecialPowerShortcutPress( Int index, GameWindow **button );++ /** Where a press lands: a slot index, or one of these two. The two functions below are the+ whole decision and touch no window, so the tests can hold every row of it; the press calls+ and the peek calls gather what is on the bar and ask them. */+ enum { SLOT_NOTHING = -1, SLOT_ARMS_CHORD = -2 };+ static Int resolveGridPress( Int index, Int chordGroup, Bool hasStructures, Bool indexIsStructure );+ static Int resolveTrayPress( Int index, Int armedRow, Int visibleSlots );+/** The promotion screen answers the group keys while it is open: 1 to 5 name its five columns.A promotion point is spent for good, so it takes two presses like the general's powers do -the first marks the next science the column will sell you, the same key again buys it. A@@ -1087,7 +1111,12 @@ protected:void updateSpecialPowerShortcut( void );void arrangeSpecialPowerShortcutGrid( void ); ///< re-lay the layout's single column as rows of SPECIAL_POWER_SHORTCUT_COLSInt countVisibleSpecialPowerShortcuts( void ); ///< how many slots carry a power right now, which is not the command set's size-++ /** Where a press lands with the chords as they stand. The press calls and the peek calls both+ go through these, which is the point of them. */+ Int resolveCommandSlot( Int index, Bool *hasStructures ) const;+ Int resolveSpecialPowerShortcutSlot( Int index );+static const Image* calculateVeterancyOverlayForThing( const ThingTemplate *thingTemplate );static const Image* calculateVeterancyOverlayForObject( const Object *obj );diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/ChromaKeyboard.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/ChromaKeyboard.cppindex 221e427fa..5db51fec9 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/ChromaKeyboard.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/ChromaKeyboard.cpp@@ -31,6 +31,8 @@#include "GameClient/GameWindow.h"#include "GameClient/HotKey.h"#include "GameClient/InGameUI.h"+#include "GameClient/Keyboard.h"+#include "GameClient/MetaEvent.h"#include "Common/Energy.h"#include "Common/GameCommon.h"#include "Common/GlobalData.h"@@ -322,9 +324,8 @@ static Int chromaReadyColor( UnsignedInt frame, UnsignedInt readySince, Int coloreturn chromaBlinkIsOn( frame, BLINK_PERIOD_FRAMES ) ? color : COLOR_OFF;}-/// When each power last came ready, so the key can announce itself and then stop-/// shouting. Zero means it is not ready at all.-static UnsignedInt s_powerReadySince[ SPECIALPOWER_COUNT ];+/// When the first superweapon came ready, so the numpad can announce it and then+/// stop shouting. Zero means none is.static UnsignedInt s_superweaponReadySince = 0;//-----------------------------------------------------------------------------@@ -1029,8 +1030,6 @@ struct ChromaProducerstruct ChromaSnapshot{- Real powerCharge[ SPECIALPOWER_COUNT ]; ///< -1 where the player has no such power- Bool powerReady[ SPECIALPOWER_COUNT ];Real bestSuperweaponCharge;Bool anySuperweaponReady;ChromaProducer producers[ PRODUCTION_LAMPS ];@@ -1073,29 +1072,18 @@ static void chromaVisitObject( Object *obj, void *userData )|| obj->isEffectivelyDead() )return;- const Bool isSuperweapon = obj->isKindOf( KINDOF_FS_SUPERWEAPON );- for( BehaviorModule **module = obj->getBehaviorModules(); module && *module; ++module )+ // Only the superweapons are read here. The generals powers come off the tray+ // itself, which already knows which of them are showing and which key reaches+ // each, neither of which this walk could work out.+ if( obj->isKindOf( KINDOF_FS_SUPERWEAPON ) ){- SpecialPowerModuleInterface *power = (*module)->getSpecialPower();- if( power == NULL || power->isScriptOnly() )- continue;-- const SpecialPowerTemplate *powerTemplate = power->getSpecialPowerTemplate();- if( powerTemplate == NULL )- continue;-- const Int type = (Int)powerTemplate->getSpecialPowerType();- if( type < 0 || type >= SPECIALPOWER_COUNT )- continue;-- const Real charge = power->getPercentReady();- if( charge > snapshot->powerCharge[ type ] )- snapshot->powerCharge[ type ] = charge;- if( power->isReady() )- snapshot->powerReady[ type ] = TRUE;-- if( isSuperweapon )+ for( BehaviorModule **module = obj->getBehaviorModules(); module && *module; ++module ){+ SpecialPowerModuleInterface *power = (*module)->getSpecialPower();+ if( power == NULL || power->isScriptOnly() )+ continue;++ const Real charge = power->getPercentReady();if( charge > snapshot->bestSuperweaponCharge )snapshot->bestSuperweaponCharge = charge;if( power->isReady() )@@ -1127,11 +1115,6 @@ static const ChromaSnapshot &chromaWalkPlayer( Player *localPlayer, UnsignedInteverWalked = TRUE;lastWalkFrame = frame;- for( Int type = 0; type < SPECIALPOWER_COUNT; ++type )- {- snapshot.powerCharge[ type ] = -1.0f;- snapshot.powerReady[ type ] = FALSE;- }snapshot.bestSuperweaponCharge = 0.0f;snapshot.anySuperweaponReady = FALSE;snapshot.producerCount = 0;@@ -1139,13 +1122,6 @@ static const ChromaSnapshot &chromaWalkPlayer( Player *localPlayer, UnsignedIntif( localPlayer != NULL )localPlayer->iterateObjects( chromaVisitObject, &snapshot );- for( Int type = 0; type < SPECIALPOWER_COUNT; ++type )- {- if( !snapshot.powerReady[ type ] )- s_powerReadySince[ type ] = 0;- else if( s_powerReadySince[ type ] == 0 )- s_powerReadySince[ type ] = frame;- }if( !snapshot.anySuperweaponReady )s_superweaponReadySince = 0;else if( s_superweaponReadySince == 0 )@@ -1265,10 +1241,99 @@ static void chromaFillPowerRow( Int *cells, const Energy *energy, UnsignedInt fr}//------------------------------------------------------------------------------/** The command bar, key for key, with the build clock the button is drawing on- * screen carried as brightness. The clock is read straight off the gadget the- * control bar already wrote it to, so the keys cannot drift from the buttons. */-static void chromaFillCommandBar( Int *cells, Int pressable, Int unavailable )+/** How far round its clock a button is, zero to one, or one for a button that is+ * drawing no clock. Read straight off the gadget the control bar already wrote+ * it to, so a key cannot drift from the button it stands for. */+static Real chromaButtonClock( GameWindow *button )+{+ if( button == NULL || !BitTest( button->winGetStyle(), GWS_PUSH_BUTTON ) )+ return 1.0f;++ const PushButtonData *buttonData = (const PushButtonData *)button->winGetUserData();+ if( buttonData == NULL || buttonData->drawClock == NO_CLOCK )+ return 1.0f;+ return buttonData->percentClock / 100.0f;+}++//-----------------------------------------------------------------------------+/** A key that can be pressed, dimmed by its build clock but never to nothing: a+ * factory that has just started a tank still takes a second order, and a dark+ * key says it would not. */+static Int chromaPressableWithClock( Int pressable, GameWindow *button )+{+ static const Real CLOCK_FLOOR = 0.35f;+ return chromaScale( pressable, CLOCK_FLOOR + (1.0f - CLOCK_FLOOR) * chromaButtonClock( button ) );+}++//-----------------------------------------------------------------------------+/** The lamp a bound key lights, or -1 for a key the grid has no lamp for or the+ * layout has no letter for. */+static Int chromaCellForBoundKey( MappableKeyType key )+{+ if( key >= MK_F1 && key <= MK_F10 )+ return chromaKeyboardCell( FKEY_ROW, FKEY_FIRST_COLUMN + (key - MK_F1) );+ if( key == MK_F11 )+ return chromaKeyboardCell( FKEY_ROW, FKEY_FIRST_COLUMN + 10 );+ if( key == MK_F12 )+ return chromaKeyboardCell( FKEY_ROW, FKEY_FIRST_COLUMN + 11 );+ if( key == MK_NONE || TheKeyboard == NULL )+ return -1;++ WideChar printable = TheKeyboard->getPrintableKey( (UnsignedByte)key, 0 );+ if( printable >= L'A' && printable <= L'Z' )+ printable += L'a' - L'A';+ return printable > 0 && printable < 128 ? chromaCellForKey( (char)printable ) : -1;+}++//-----------------------------------------------------------------------------+// Which lamp each command bar slot and each tray slot answers to under the+// bindings in force, looked up twice a second rather than per lamp per frame.+// The player can rebind a slot in Options and change scheme mid-match, so it+// cannot be looked up once. Under Legacy nothing is bound to either and every+// entry is -1, which is right: there the letters in the labels do the work.+//-----------------------------------------------------------------------------+static Int s_commandSlotCell[ MAX_COMMANDS_PER_SET ];+static Int s_shortcutSlotCell[ MAX_SPECIAL_POWER_SHORTCUTS ];++//-----------------------------------------------------------------------------+static void chromaRefreshBoundCells( UnsignedInt frame )+{+ static UnsignedInt lastRefreshFrame = 0;+ static Bool everRefreshed = FALSE;+ if( everRefreshed && frame >= lastRefreshFrame+ && frame - lastRefreshFrame < (UnsignedInt)WALK_INTERVAL_FRAMES )+ return;++ everRefreshed = TRUE;+ lastRefreshFrame = frame;+ for( Int slot = 0; slot < MAX_COMMANDS_PER_SET; ++slot )+ s_commandSlotCell[ slot ] = -1;+ for( Int slot = 0; slot < MAX_SPECIAL_POWER_SHORTCUTS; ++slot )+ s_shortcutSlotCell[ slot ] = -1;+ if( TheMetaMap == NULL )+ return;++ for( const MetaMapRec *rec = TheMetaMap->getFirstMetaMapRec(); rec; rec = rec->m_next )+ {+ // only the bare key: the shifted copy of a slot is the same lamp+ if( rec->m_modState != 0 )+ continue;++ const Int commandSlot = (Int)rec->m_meta - (Int)GameMessage::MSG_META_COMMAND_SLOT01;+ if( commandSlot >= 0 && commandSlot < MAX_COMMANDS_PER_SET && s_commandSlotCell[ commandSlot ] < 0 )+ s_commandSlotCell[ commandSlot ] = chromaCellForBoundKey( rec->m_key );++ const Int shortcutSlot = (Int)rec->m_meta - (Int)GameMessage::MSG_META_SHORTCUT_SLOT01;+ if( shortcutSlot >= 0 && shortcutSlot < MAX_SPECIAL_POWER_SHORTCUTS && s_shortcutSlotCell[ shortcutSlot ] < 0 )+ s_shortcutSlotCell[ shortcutSlot ] = chromaCellForBoundKey( rec->m_key );+ }+}++//-----------------------------------------------------------------------------+/** The command bar under Legacy input: the letter marked in each button's label,+ * which is what HotKeyManager presses it with. Under Modern that manager holds+ * no command bar letters at all and this lights nothing. */+static void chromaFillLabelHotKeys( Int *cells, Int pressable, Int unavailable ){for( Int row = CHROMA_FIRST_HOTKEY_ROW; row < CHROMA_KEY_ROW_COUNT; ++row ){@@ -1281,66 +1346,77 @@ static void chromaFillCommandBar( Int *cells, Int pressable, Int unavailable )if( win == NULL )continue;- Int color = isPressable ? pressable : unavailable;- if( isPressable && BitTest( win->winGetStyle(), GWS_PUSH_BUTTON ) )- {- const PushButtonData *buttonData = (const PushButtonData *)win->winGetUserData();- if( buttonData != NULL && buttonData->drawClock != NO_CLOCK )- color = chromaScale( color, buttonData->percentClock / 100.0f );- }- cells[ chromaCellForKey( keys[ column ] ) ] = color;+ cells[ chromaCellForKey( keys[ column ] ) ] =+ isPressable ? chromaPressableWithClock( pressable, win ) : unavailable;}}}//------------------------------------------------------------------------------/** The function row is the generals powers tray, slot for slot, taken from the- * same command set the on-screen tray is built from. */-static void chromaFillPowerTray( Int *cells, Player *localPlayer, const ChromaSnapshot &snapshot,- Int factionColor, UnsignedInt frame )+/** The command bar under Modern input: the grid keys, with the builder's two key+ * chord followed. What a key would do comes from the control bar's own+ * resolution of the press, so when Q has armed a group the lamps move onto that+ * group's cells the moment the labels on screen do. */+static void chromaFillCommandGrid( Int *cells, Int pressable, Int unavailable ){- const PlayerTemplate *playerTemplate = localPlayer->getPlayerTemplate();- if( playerTemplate == NULL || TheControlBar == NULL )- return;-- const AsciiString setName = playerTemplate->getSpecialPowerShortcutCommandSet();- if( setName.isEmpty() )- return;-- const CommandSet *commandSet = TheControlBar->findCommandSet( setName );- if( commandSet == NULL )- return;-- Int slots = playerTemplate->getSpecialPowerShortcutButtonCount();- if( slots > FKEY_COUNT )- slots = FKEY_COUNT;-- for( Int slot = 0; slot < slots; ++slot )+ for( Int slot = 0; slot < MAX_COMMANDS_PER_SET; ++slot ){- const CommandButton *button = commandSet->getCommandButton( slot );- if( button == NULL )+ const Int cell = s_commandSlotCell[ slot ];+ if( cell < 0 )continue;- const SpecialPowerTemplate *powerTemplate = button->getSpecialPowerTemplate();- if( powerTemplate == NULL )- continue;+ GameWindow *button = NULL;+ switch( TheControlBar->peekCommandButtonPress( slot, &button ) )+ {+ case ControlBar::PRESS_FIRES:+ cells[ cell ] = chromaPressableWithClock( pressable, button );+ break;+ case ControlBar::PRESS_ARMS_CHORD:+ // worth starting only if the group holds something the second key can build+ cells[ cell ] = button != NULL ? pressable : unavailable;+ break;+ case ControlBar::PRESS_IS_REFUSED:+ cells[ cell ] = unavailable;+ break;+ default:+ break; // no button behind this key, so it keeps the bed's colour+ }+ }+}- // A power whose promotion has not been bought is not on the tray on screen- // and has no business on the key either, which is what made the first two- // keys of the row light for powers nobody could use.- const ScienceType required = powerTemplate->getRequiredScience();- if( required != SCIENCE_INVALID && !localPlayer->hasScience( required ) )- continue;+//-----------------------------------------------------------------------------+/** The generals powers, on the keys that reach them. It takes two presses: the+ * first names a row of the tray and the second a power in that row. So with+ * nothing armed, the key of every row that holds a usable power blinks; once a+ * row is armed the blinking moves onto the keys of the usable powers inside it.+ * A power still charging shows as far as its clock has got, and a key that+ * reaches nothing stays the colour of the bed. */+static void chromaFillPowerTray( Int *cells, Int factionColor, UnsignedInt frame )+{+ const Int usable = chromaBlinkIsOn( frame, BLINK_PERIOD_FRAMES ) ? COLOR_WARM_WHITE : COLOR_OFF;+ const Int nothingUsable = chromaScale( factionColor, AMBIENT_SCALE );- const Int type = (Int)powerTemplate->getSpecialPowerType();- if( type < 0 || type >= SPECIALPOWER_COUNT || snapshot.powerCharge[ type ] < 0.0f )+ for( Int slot = 0; slot < MAX_SPECIAL_POWER_SHORTCUTS; ++slot )+ {+ const Int cell = s_shortcutSlotCell[ slot ];+ if( cell < 0 )continue;- const Int cell = chromaKeyboardCell( FKEY_ROW, FKEY_FIRST_COLUMN + slot );- if( snapshot.powerReady[ type ] )- cells[ cell ] = chromaReadyColor( frame, s_powerReadySince[ type ], COLOR_WARM_WHITE );- else- cells[ cell ] = chromaScale( factionColor, snapshot.powerCharge[ type ] );+ GameWindow *button = NULL;+ switch( TheControlBar->peekSpecialPowerShortcutPress( slot, &button ) )+ {+ case ControlBar::PRESS_FIRES:+ cells[ cell ] = usable;+ break;+ case ControlBar::PRESS_ARMS_CHORD:+ cells[ cell ] = button != NULL ? usable : nothingUsable;+ break;+ case ControlBar::PRESS_IS_REFUSED:+ cells[ cell ] = chromaScale( factionColor, chromaButtonClock( button ) );+ break;+ default:+ break;+ }}}@@ -1551,20 +1627,27 @@ static void chromaFillCells( Int *cells )chromaResetAlerts();s_effect = EFFECT_NONE;s_superweaponReadySince = 0;- for( Int type = 0; type < SPECIALPOWER_COUNT; ++type )- s_powerReadySince[ type ] = 0;return;}chromaUpdateAlerts( localPlayer, frame );+ chromaRefreshBoundCells( frame );const ChromaSnapshot &snapshot = chromaWalkPlayer( localPlayer, frame );const Int pressable = chromaAlarmed( factionColor, alarmData );const Int unavailable = chromaAlarmed( COLOR_OFF, alarmData );- chromaFillCommandBar( cells, pressable, unavailable );+ // Two input schemes reach the same buttons by different keys, and only one of+ // them is live at a time: Legacy registers the letters in the labels and binds+ // no grid, Modern binds the grid and registers no letters. Filling from both+ // lights whichever is real and nothing from the other.+ chromaFillLabelHotKeys( cells, pressable, unavailable );+ if( TheControlBar != NULL )+ {+ chromaFillCommandGrid( cells, pressable, unavailable );+ chromaFillPowerTray( cells, factionColor, frame );+ }chromaFillPowerRow( cells, localPlayer->getEnergy(), frame, alarmData );- chromaFillPowerTray( cells, localPlayer, snapshot, factionColor, frame );chromaFillNumpad( cells, snapshot, factionColor, frame );chromaFillProduction( cells, snapshot, factionColor );chromaFillSelection( cells );diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cppindex 803084308..a78d6baaa 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/ControlBar/ControlBar.cpp@@ -171,14 +171,14 @@ static MappableKeyType getGridHotKey( Int slot )} // end getGridHotKey//--------------------------------------------------------------------------------------------------/** Press a command bar button by slot index. Mirrors HotKeyManager::executeHotKey: a hidden- slot does nothing at all, an enabled one gets the same GBM_SELECTED the mouse would send,- and a disabled one just makes the rejection noise. */+/** Which slot a press on the grid key for 'index' lands on with the structure chord as it+ stands, or SLOT_ARMS_CHORD for a press that would only start one, or SLOT_NOTHING. Changes+ nothing: pressCommandButton acts on the answer and peekCommandButtonPress reports it. *///--------------------------------------------------------------------------------------------------void ControlBar::pressCommandButton( Int index )+Int ControlBar::resolveGridPress( Int index, Int chordGroup, Bool hasStructures, Bool indexIsStructure ){if( index < 0 || index >= MAX_COMMANDS_PER_SET )- return;+ return SLOT_NOTHING;//// a builder's structures are reached by a two-key chord so the whole set can stay on@@ -188,7 +188,35 @@ void ControlBar::pressCommandButton( Int index )// way in: its own letter on its own is the second half of a chord nobody started. Q and W// are whatever the grid binds to slots 0 and 2, so with W A S D on the camera they are Q and E.//- Bool hasStructures = FALSE;+ if( !hasStructures )+ return index;++ if( chordGroup < 0 )+ {+ if( index == CHORD_SLOT_Q || index == CHORD_SLOT_W )+ return SLOT_ARMS_CHORD;++ //+ // Every structure is two keys, and only two. The key painted on a cell is the *second*+ // of its pair, so on its own it used to fall through to whatever sits in the slot that+ // key names - which for a builder is another structure. So the same building could be+ // put up either by the chord written on it or by one bare letter nobody wrote anywhere,+ // and a key pressed after a Q that had already been dropped built something.+ //+ return indexIsStructure ? SLOT_NOTHING : index;+ }++ if( index >= CHORD_GROUP_SIZE )+ return SLOT_NOTHING; // the second key must be one of the first group's cells (Q W E R A S D F)+ index += chordGroup * CHORD_GROUP_SIZE;+ return index < MAX_COMMANDS_PER_SET ? index : SLOT_NOTHING;++} // end resolveGridPress++//-------------------------------------------------------------------------------------------------+Int ControlBar::resolveCommandSlot( Int index, Bool *hasStructures ) const+{+ *hasStructures = FALSE;Bool indexIsStructure = FALSE;for( Int i = 0; i < MAX_COMMANDS_PER_SET; i++ ){@@ -198,47 +226,82 @@ void ControlBar::pressCommandButton( Int index )const CommandButton *c = (const CommandButton *)GadgetButtonGetData( w );if( c == NULL || c->getCommandType() != GUI_COMMAND_DOZER_CONSTRUCT )continue;- hasStructures = TRUE;+ *hasStructures = TRUE;if( i == index )indexIsStructure = TRUE;}- if( hasStructures )+ return resolveGridPress( index, m_chordGroup, *hasStructures, indexIsStructure );++} // end resolveCommandSlot++//-------------------------------------------------------------------------------------------------+ControlBar::PressOutcome ControlBar::peekCommandButtonPress( Int index, GameWindow **button )+{+ *button = NULL;++ Bool hasStructures = FALSE;+ const Int slot = resolveCommandSlot( index, &hasStructures );+ if( slot == SLOT_NOTHING )+ return PRESS_DOES_NOTHING;++ if( slot == SLOT_ARMS_CHORD ){- if( m_chordGroup < 0 )+ // the first cell of the group this key would arm that the second key could press+ const Int first = ( index == CHORD_SLOT_Q ? 0 : 1 ) * CHORD_GROUP_SIZE;+ for( Int i = first; i < first + CHORD_GROUP_SIZE && i < MAX_COMMANDS_PER_SET; i++ ){- if( index == CHORD_SLOT_Q || index == CHORD_SLOT_W )+ GameWindow *w = m_commandWindows[ i ];+ if( w && !BitTest( w->winGetStatus(), WIN_STATUS_HIDDEN )+ && BitTest( w->winGetStatus(), WIN_STATUS_ENABLED ) ){- m_chordGroup = ( index == CHORD_SLOT_Q ) ? 0 : 1;- m_chordStartMs = timeGetTime();- m_chordDrawableID = m_currentSelectedDrawable ? m_currentSelectedDrawable->getID()- : INVALID_DRAWABLE_ID;- markUIDirty(); // the group that is armed greys the other one out- return;+ *button = w;+ break;}-- //- // Every structure is two keys, and only two. The key painted on a cell is the *second*- // of its pair, so on its own it used to fall through to whatever sits in the slot that- // key names - which for a builder is another structure. So the same building could be- // put up either by the chord written on it or by one bare letter nobody wrote anywhere,- // and a key pressed after a Q that had already been dropped built something.- //- if( indexIsStructure )- return;- }- else- {- Int group = m_chordGroup;- dropChord();- if( index >= CHORD_GROUP_SIZE )- return; // the second key must be one of the first group's cells (Q W E R A S D F)- index += group * CHORD_GROUP_SIZE;- if( index >= MAX_COMMANDS_PER_SET )- return;}+ return PRESS_ARMS_CHORD;}- GameWindow *win = m_commandWindows[ index ];+ GameWindow *win = m_commandWindows[ slot ];+ if( win == NULL || BitTest( win->winGetStatus(), WIN_STATUS_HIDDEN ) )+ return PRESS_DOES_NOTHING;++ *button = win;+ return BitTest( win->winGetStatus(), WIN_STATUS_ENABLED ) ? PRESS_FIRES : PRESS_IS_REFUSED;++} // end peekCommandButtonPress++//-------------------------------------------------------------------------------------------------+/** Press a command bar button by slot index. Mirrors HotKeyManager::executeHotKey: a hidden+ slot does nothing at all, an enabled one gets the same GBM_SELECTED the mouse would send,+ and a disabled one just makes the rejection noise. */+//-------------------------------------------------------------------------------------------------+void ControlBar::pressCommandButton( Int index )+{+ // a press that names no slot at all leaves an armed chord alone+ if( index < 0 || index >= MAX_COMMANDS_PER_SET )+ return;++ Bool hasStructures = FALSE;+ const Int slot = resolveCommandSlot( index, &hasStructures );++ if( slot == SLOT_ARMS_CHORD )+ {+ m_chordGroup = ( index == CHORD_SLOT_Q ) ? 0 : 1;+ m_chordStartMs = timeGetTime();+ m_chordDrawableID = m_currentSelectedDrawable ? m_currentSelectedDrawable->getID()+ : INVALID_DRAWABLE_ID;+ markUIDirty(); // the group that is armed greys the other one out+ return;+ }++ // an armed chord is spent by its second key whether or not that key found a cell+ if( hasStructures && m_chordGroup >= 0 )+ dropChord();++ if( slot == SLOT_NOTHING )+ return;++ GameWindow *win = m_commandWindows[ slot ];if( win == NULL || BitTest( win->winGetStatus(), WIN_STATUS_HIDDEN ) )return;@@ -4292,47 +4355,110 @@ Int ControlBar::countVisibleSpecialPowerShortcuts( void )} // end countVisibleSpecialPowerShortcuts//--------------------------------------------------------------------------------------------------/** One key cannot reach eleven powers laid out three to a row, so it takes two: the first press- * picks the row - F1 the row in the corner, F2 the one above it - and the second picks the power- * in it, F1 being the rightmost. Until a row is picked only the head of each row is labelled,- * with the key that picks that row; once one is, the labels move onto its three powers. */+/** Which tray slot a press on shortcut key 'index' lands on with the row chord as it stands, or+ SLOT_ARMS_CHORD for a press that names a row, or SLOT_NOTHING. Changes nothing. *///--------------------------------------------------------------------------------------------------void ControlBar::pressSpecialPowerShortcut( Int index )+Int ControlBar::resolveTrayPress( Int index, Int armedRow, Int visibleSlots ){- if( index < 0 || m_specialPowerShortcutParent == NULL || m_specialPowerShortcutParent->winIsHidden() )- return;+ if( index < 0 )+ return SLOT_NOTHING;//// what the keys can reach is what is on screen, not the size of the command set the general// could eventually fill: a USA player with three powers has one row, and arming the second// one would swallow the key after it with nothing to spend it on//- const Int visible = countVisibleSpecialPowerShortcuts();- const Int rows = ( visible + SPECIAL_POWER_SHORTCUT_COLS - 1 ) / SPECIAL_POWER_SHORTCUT_COLS;+ const Int rows = ( visibleSlots + SPECIAL_POWER_SHORTCUT_COLS - 1 ) / SPECIAL_POWER_SHORTCUT_COLS;// nothing pending: this press names a row and stops there- if( m_specialPowerShortcutRow < 0 )+ if( armedRow < 0 )+ return index < rows ? SLOT_ARMS_CHORD : SLOT_NOTHING;++ // a row is pending: this press names the power in it+ const Int slot = armedRow * SPECIAL_POWER_SHORTCUT_COLS + index;+ if( index >= SPECIAL_POWER_SHORTCUT_COLS || slot >= visibleSlots )+ return SLOT_NOTHING;++ return slot;++} // end resolveTrayPress++//-------------------------------------------------------------------------------------------------+Int ControlBar::resolveSpecialPowerShortcutSlot( Int index )+{+ if( m_specialPowerShortcutParent == NULL || m_specialPowerShortcutParent->winIsHidden() )+ return SLOT_NOTHING;++ const Int slot = resolveTrayPress( index, m_specialPowerShortcutRow, countVisibleSpecialPowerShortcuts() );+ if( slot < 0 )+ return slot;++ GameWindow *win = m_specialPowerShortcutButtons[ slot ];+ return ( win == NULL || win->winIsHidden() ) ? SLOT_NOTHING : slot;++} // end resolveSpecialPowerShortcutSlot++//-------------------------------------------------------------------------------------------------+ControlBar::PressOutcome ControlBar::peekSpecialPowerShortcutPress( Int index, GameWindow **button )+{+ *button = NULL;++ const Int slot = resolveSpecialPowerShortcutSlot( index );+ if( slot == SLOT_NOTHING )+ return PRESS_DOES_NOTHING;++ if( slot == SLOT_ARMS_CHORD ){- if( index < rows )+ // the first power in the row this key would name that the second key could fire+ const Int visible = countVisibleSpecialPowerShortcuts();+ const Int first = index * SPECIAL_POWER_SHORTCUT_COLS;+ for( Int i = first; i < first + SPECIAL_POWER_SHORTCUT_COLS && i < visible; i++ ){- m_specialPowerShortcutRow = index;- m_specialPowerShortcutRowMs = timeGetTime();+ GameWindow *w = m_specialPowerShortcutButtons[ i ];+ if( w && !w->winIsHidden() && BitTest( w->winGetStatus(), WIN_STATUS_ENABLED ) )+ {+ *button = w;+ break;+ }}+ return PRESS_ARMS_CHORD;+ }++ *button = m_specialPowerShortcutButtons[ slot ];+ return BitTest( (*button)->winGetStatus(), WIN_STATUS_ENABLED ) ? PRESS_FIRES : PRESS_IS_REFUSED;++} // end peekSpecialPowerShortcutPress++//-------------------------------------------------------------------------------------------------+/** One key cannot reach eleven powers laid out three to a row, so it takes two: the first press+ * picks the row - F1 the row in the corner, F2 the one above it - and the second picks the power+ * in it, F1 being the rightmost. Until a row is picked only the head of each row is labelled,+ * with the key that picks that row; once one is, the labels move onto its three powers. */+//-------------------------------------------------------------------------------------------------+void ControlBar::pressSpecialPowerShortcut( Int index )+{+ // a press that cannot reach the tray at all leaves a pending row alone+ if( index < 0 || m_specialPowerShortcutParent == NULL || m_specialPowerShortcutParent->winIsHidden() )+ return;++ const Int slot = resolveSpecialPowerShortcutSlot( index );++ if( slot == SLOT_ARMS_CHORD )+ {+ m_specialPowerShortcutRow = index;+ m_specialPowerShortcutRowMs = timeGetTime();return;}//- // a row is pending: this press names the power in it, and either way the row is spent - a key- // that names no power in that row simply cancels+ // a row that was pending is spent by this press either way - a key that names no power in+ // that row simply cancels//- const Int slot = m_specialPowerShortcutRow * SPECIAL_POWER_SHORTCUT_COLS + index;clearSpecialPowerShortcutRow();- if( index >= SPECIAL_POWER_SHORTCUT_COLS || slot >= visible )+ if( slot == SLOT_NOTHING )return;GameWindow *win = m_specialPowerShortcutButtons[ slot ];- if( win == NULL || win->winIsHidden() || m_specialPowerShortcutParent->winIsHidden() )- return;if( BitTest( win->winGetStatus(), WIN_STATUS_ENABLED ) ){diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex ea2d02767..7143dc4b1 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -13094,6 +13094,72 @@ TEST(chroma_power_meter_empties_as_the_draw_catches_the_supply)CHECK_EQ(chromaPowerSegments(10, 11), CHROMA_POWER_BROWNOUT);}+// Where a grid key lands. This is the decision pressCommandButton used to make inline, pulled+// out so the keyboard lighting could ask the same question without pressing anything; every+// row below is what the inline version did, read off it before it was moved.+TEST(grid_press_follows_the_builders_two_key_chord)+{+ const Int NOTHING = ControlBar::SLOT_NOTHING;+ const Int ARMS = ControlBar::SLOT_ARMS_CHORD;+ const Int Q = ControlBar::CHORD_SLOT_Q;+ const Int W = ControlBar::CHORD_SLOT_W;+ const Int GROUP = ControlBar::CHORD_GROUP_SIZE;++ // no structures on the bar: a slot is a slot, chord or no chord+ CHECK_EQ(ControlBar::resolveGridPress(0, -1, FALSE, FALSE), 0);+ CHECK_EQ(ControlBar::resolveGridPress(5, -1, FALSE, FALSE), 5);+ CHECK_EQ(ControlBar::resolveGridPress(5, 0, FALSE, FALSE), 5);++ // out of range names nothing+ CHECK_EQ(ControlBar::resolveGridPress(-1, -1, FALSE, FALSE), NOTHING);+ CHECK_EQ(ControlBar::resolveGridPress(MAX_COMMANDS_PER_SET, -1, TRUE, FALSE), NOTHING);++ // a builder, nothing armed: Q and W arm, a structure's own key does nothing on its own,+ // and a cell that is not a structure is still one press+ CHECK_EQ(ControlBar::resolveGridPress(Q, -1, TRUE, TRUE), ARMS);+ CHECK_EQ(ControlBar::resolveGridPress(W, -1, TRUE, FALSE), ARMS);+ CHECK_EQ(ControlBar::resolveGridPress(4, -1, TRUE, TRUE), NOTHING);+ CHECK_EQ(ControlBar::resolveGridPress(4, -1, TRUE, FALSE), 4);++ // armed: the second key is a cell of the first group, shifted into the armed one+ CHECK_EQ(ControlBar::resolveGridPress(0, 0, TRUE, TRUE), 0);+ CHECK_EQ(ControlBar::resolveGridPress(3, 0, TRUE, FALSE), 3);+ CHECK_EQ(ControlBar::resolveGridPress(0, 1, TRUE, TRUE), GROUP);+ CHECK_EQ(ControlBar::resolveGridPress(3, 1, TRUE, FALSE), GROUP + 3);+ CHECK_EQ(ControlBar::resolveGridPress(GROUP - 1, 1, TRUE, FALSE), GROUP + GROUP - 1);+ // a second key from outside the first group's cells is not a cell of any group+ CHECK_EQ(ControlBar::resolveGridPress(GROUP, 0, TRUE, FALSE), NOTHING);+ CHECK_EQ(ControlBar::resolveGridPress(GROUP + 2, 1, TRUE, FALSE), NOTHING);+}++// The generals powers tray: the first key names a row, the second a power in it, and what the+// keys can reach is what is showing rather than the size of the general's command set.+TEST(tray_press_names_a_row_and_then_a_power_in_it)+{+ const Int NOTHING = ControlBar::SLOT_NOTHING;+ const Int ARMS = ControlBar::SLOT_ARMS_CHORD;+ const Int COLS = SPECIAL_POWER_SHORTCUT_COLS;++ // three powers showing is one row: F1 names it, F2 names nothing+ CHECK_EQ(ControlBar::resolveTrayPress(0, -1, 3), ARMS);+ CHECK_EQ(ControlBar::resolveTrayPress(1, -1, 3), NOTHING);+ // four is two rows+ CHECK_EQ(ControlBar::resolveTrayPress(1, -1, 4), ARMS);+ CHECK_EQ(ControlBar::resolveTrayPress(2, -1, 4), NOTHING);+ // none showing, nothing to name+ CHECK_EQ(ControlBar::resolveTrayPress(0, -1, 0), NOTHING);+ CHECK_EQ(ControlBar::resolveTrayPress(-1, -1, 3), NOTHING);++ // row armed: the key is a column in it+ CHECK_EQ(ControlBar::resolveTrayPress(0, 0, 3), 0);+ CHECK_EQ(ControlBar::resolveTrayPress(2, 0, 3), 2);+ CHECK_EQ(ControlBar::resolveTrayPress(0, 1, 4), COLS);+ // the second row of four holds one power, so its second column is nothing+ CHECK_EQ(ControlBar::resolveTrayPress(1, 1, 4), NOTHING);+ // and a key past the row's width is nothing however many are showing+ CHECK_EQ(ControlBar::resolveTrayPress(COLS, 0, 11), NOTHING);+}+// A bar that has started has to show it. Rounding a tenth of a charge down to// nothing is how a gauge comes to read empty while the thing behind it is// running, which is worse than no gauge.