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(gameengine): put power and superweapons on the Razer hardware
Olcay Seygan1e9af4597e96
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7d0846cbb..27dabb397 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -642,12 +642,14 @@ found and fixed — EA's own, not port damage.**- Your allies see the smoke and their radar blinks at the spot, and the message area says who sent it. Three seconds later the smoke has cleared, so the call you made a minute ago is never still standing next to the one you are making now. Enemies see none of it.- One signal a second per player. Hammering the key gets you the first puff and nothing else, and a teammate who tries it cannot bury your screen in smoke either, because every machine in the game holds everyone to the same limit.-## A Razer keyboard plays along--- The keyboard sits in your own player colour for the whole match, dim enough to be a bed rather than a light show.-- The command bar is on the keys. Every letter and digit the bar has bound to a button lights up while that button can be pressed, and goes dark the moment it cannot - the moment you cannot afford it, or the thing that builds it is busy. What you can buy is readable without looking away from the fight.-- The board pulses red while something is shooting at your base, and settles again four seconds after the last hit.-- Nothing to install and nothing to turn on. If Synapse is running the lighting is there; if it is not, the game never mentions it.+## Your Razer hardware plays along++- The keyboard, the mouse and the mousepad all sit in your own player colour for the whole match, dim enough to be a bed rather than a light show.+- The command bar is on the letter keys. Every letter the bar has bound to a button lights up while that button can be pressed, and goes dark the moment it cannot - the moment you cannot afford it, or the thing that builds it is busy. What you can buy is readable without looking away from the fight.+- The number row is your power. Ten green keys while nothing is drawing on it, fewer as the base fills up, and the whole row blinking red the moment consumption passes production - the brownout that stops your radar and your defences, announced before you have gone looking for the meter. A faction with no power at all leaves the row dark rather than pretending to be full.+- The numpad blinks white when a superweapon of yours is ready to fire, and the mousepad fills up as the nearest one charges, so you can see the particle cannon coming without opening anything.+- Everything pulses red while something is shooting at your base, and settles again four seconds after the last hit. The mouse under your hand pulses with it, which is the one thing you cannot miss.+- Nothing to install and nothing to turn on. If Synapse is running the lighting is there; if it is not, the game never mentions it. A device you do not own is simply skipped.## The options screen has pagesdiff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/ChromaKeyboard.h b/GeneralsMD/Code/GameEngine/Include/GameClient/ChromaKeyboard.hindex cd85dfa48..cf3addc12 100644--- a/GeneralsMD/Code/GameEngine/Include/GameClient/ChromaKeyboard.h+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/ChromaKeyboard.h@@ -44,4 +44,13 @@ extern void shutdownChromaKeyboard( void );* an answer, which is all the command bar ever binds. */extern Int chromaCellForKey( char key );+/// How many keys the power meter spans, and what it answers once consumption has+/// passed production and the whole row goes to blinking red instead.+enum { CHROMA_POWER_SEGMENTS = 10, CHROMA_POWER_BROWNOUT = -1 };++/** How many of those keys light for a production and consumption pair: all of+ * them while nothing is drawing, none at all for a player who has not built a+ * power plant, and CHROMA_POWER_BROWNOUT once the draw has passed the supply. */+extern Int chromaPowerSegments( Int production, Int consumption );+#endif // __CHROMA_KEYBOARD_H_diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/ChromaKeyboard.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/ChromaKeyboard.cppindex 0c5c1e7fa..3f5f0a4fd 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/ChromaKeyboard.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/ChromaKeyboard.cpp@@ -26,27 +26,59 @@#include "GameClient/ChromaKeyboard.h"#include "GameClient/Color.h"#include "GameClient/HotKey.h"+#include "Common/Energy.h"#include "Common/GameCommon.h"#include "Common/Player.h"#include "Common/PlayerList.h"#include "GameLogic/GameLogic.h"+#include "GameLogic/Object.h"+#include "GameLogic/Module/BehaviorModule.h"+#include "GameLogic/Module/SpecialPowerModule.h"//------------------------------------------------------------------------------// The Chroma grid is fixed at six rows of twenty-two, whatever keyboard is-// underneath; a device with fewer keys drops what it has no lamp for.+// The devices, and where each one's cells sit in the single flat array the main+// thread fills and the worker sends. The keyboard grid is fixed at six rows of+// twenty-two whatever board is underneath, the mouse at nine of seven, and the+// mousepad is one strip of fifteen.//------------------------------------------------------------------------------static const Int CHROMA_ROWS = 6;-static const Int CHROMA_COLUMNS = 22;-static const Int CHROMA_CELLS = CHROMA_ROWS * CHROMA_COLUMNS;+struct ChromaDevice+{+ const char *endpoint;+ const char *effect;+ Int rows;+ Int columns;+ Int firstCell;+};++static const Int KEYBOARD_ROWS = 6;+static const Int KEYBOARD_COLUMNS = 22;+static const Int KEYBOARD_CELLS = KEYBOARD_ROWS * KEYBOARD_COLUMNS;+static const Int MOUSE_CELLS = 9 * 7;+static const Int MOUSEPAD_CELLS = 15;++static const Int KEYBOARD_FIRST_CELL = 0;+static const Int MOUSE_FIRST_CELL = KEYBOARD_CELLS;+static const Int MOUSEPAD_FIRST_CELL = MOUSE_FIRST_CELL + MOUSE_CELLS;+static const Int CHROMA_CELLS = MOUSEPAD_FIRST_CELL + MOUSEPAD_CELLS;++static const ChromaDevice CHROMA_DEVICES[] =+{+ { "keyboard", "CHROMA_CUSTOM", KEYBOARD_ROWS, KEYBOARD_COLUMNS, KEYBOARD_FIRST_CELL },+ // The mouse takes CUSTOM2, which is the nine by seven grid; plain CUSTOM on a+ // mouse is the old seven-lamp effect and ignores most of the device.+ { "mouse", "CHROMA_CUSTOM2", 9, 7, MOUSE_FIRST_CELL },+ { "mousepad", "CHROMA_CUSTOM", 1, MOUSEPAD_CELLS, MOUSEPAD_FIRST_CELL },+};+static const Int CHROMA_DEVICE_COUNT = sizeof( CHROMA_DEVICES ) / sizeof( CHROMA_DEVICES[ 0 ] );static const char *CHROMA_HOST = "localhost";static const INTERNET_PORT CHROMA_PORT = 54235;static const char *CHROMA_INIT_PATH = "/razer/chromasdk";static const char *CHROMA_INIT_BODY ="{\"title\":\"Zero Hour Reforged\","- "\"description\":\"Command bar and base state on the keyboard\","+ "\"description\":\"Command bar, power and superweapons on the hardware\",""\"author\":{\"name\":\"Zero Hour Reforged\",\"contact\":\"https://github.com/olcayseygan/CnCGeneralsZH-Reforged\"},"- "\"device_supported\":[\"keyboard\"],"+ "\"device_supported\":[\"keyboard\",\"mouse\",\"mousepad\"],""\"category\":\"application\"}";static const DWORD CHROMA_TIMEOUT_MS = 500;@@ -54,28 +86,43 @@ static const DWORD CHROMA_SEND_INTERVAL_MS = 100;/// The session expires after ten idle seconds, so a still frame is resent well inside that.static const DWORD CHROMA_KEEPALIVE_MS = 4000;-/// Rows two to four of an ANSI board, in the order their keys sit on it. Every-/// command bar hotkey the game hands out is a letter or a digit, so this is the-/// whole map rather than a subset of one.+/// The four rows of an ANSI board, in the order their keys sit on it. Every+/// command bar hotkey the game hands out is a letter, and the digits carry the+/// power meter instead, so this is the whole map rather than a subset of one.static const char *CHROMA_KEY_ROWS[] = { "1234567890", "qwertyuiop", "asdfghjkl", "zxcvbnm" };static const Int CHROMA_KEY_ROW_COUNT = 4;+/// Row zero of the table is the digits, which the command bar does not get.+static const Int CHROMA_POWER_ROW = 0;+static const Int CHROMA_FIRST_HOTKEY_ROW = 1;/// Column zero is the logo strip and column one is escape, tab, caps and shift,/// so every one of these four rows starts at two. Row zero is the function keys.static const Int CHROMA_KEY_FIRST_COLUMN = 2;static const Int CHROMA_KEY_FIRST_ROW = 1;+/// The numpad is its own block on the right of the same grid.+static const Int NUMPAD_FIRST_ROW = 1;+static const Int NUMPAD_LAST_ROW = 5;+static const Int NUMPAD_FIRST_COLUMN = 18;+static const Int NUMPAD_LAST_COLUMN = 21;+static const Real AMBIENT_SCALE = 0.22f; ///< the unlit bed of player colourstatic const Real PRESSABLE_SCALE = 1.0f; ///< a command key that would fire if pressedstatic const Int UNDER_ATTACK_FRAMES = LOGICFRAMES_PER_SECOND * 4;static const Real UNDER_ATTACK_PULSE_HZ = 2.5f;static const Real UNDER_ATTACK_DEPTH = 0.75f; ///< how far the pulse drags the board to red+/// Half of this many frames lit, half dark.+static const Int BLINK_PERIOD_FRAMES = 16;+/// Walking every building the player owns is not a per-frame job, and a+/// superweapon's charge does not move fast enough to need one.+static const Int SUPERWEAPON_SCAN_INTERVAL_FRAMES = 15;+//------------------------------------------------------------------------------// The main thread writes s_pendingGrid, the worker reads it. One lock over the-// whole grid: it is 528 bytes copied ten times a second.+// The main thread writes s_pendingCells, the worker reads it. One lock over the+// whole array: it is 840 bytes copied ten times a second.//------------------------------------------------------------------------------static CRITICAL_SECTION s_gridLock;-static Int s_pendingGrid[ CHROMA_CELLS ];+static CRITICAL_SECTION s_cellLock;+static Int s_pendingCells[ CHROMA_CELLS ];static Bool s_workerRunning = FALSE;static volatile LONG s_workerShouldStop = 0;static HANDLE s_workerThread = NULL;@@ -89,7 +136,7 @@ Int chromaCellForKey( char key )for( Int column = 0; keys[ column ] != 0; ++column ){if( keys[ column ] == key )- return (CHROMA_KEY_FIRST_ROW + row) * CHROMA_COLUMNS+ return (CHROMA_KEY_FIRST_ROW + row) * KEYBOARD_COLUMNS+ CHROMA_KEY_FIRST_COLUMN + column;}}@@ -106,6 +153,12 @@ static Int chromaColorFromComponents( Real red, Real green, Real blue )return (b << 16) | (g << 8) | r;}+//-----------------------------------------------------------------------------+static Bool chromaBlinkIsOn( UnsignedInt frame )+{+ return ((frame / (BLINK_PERIOD_FRAMES / 2)) & 1) == 0;+}+//-----------------------------------------------------------------------------// The HTTP side. Everything below here runs on the worker thread.//-----------------------------------------------------------------------------@@ -162,24 +215,31 @@ static Bool chromaOpenSession( HINTERNET connection, char *host, Int hostBytes,_snprintf( host, hostBytes, "%s", parsedHost );host[ hostBytes - 1 ] = 0;- // Every frame goes to the keyboard under that root; the root itself only- // answers the session calls.- _snprintf( path, pathBytes, "%s/keyboard", parsedPath );+ _snprintf( path, pathBytes, "%s", parsedPath );path[ pathBytes - 1 ] = 0;return TRUE;}//------------------------------------------------------------------------------static void chromaBuildKeyboardBody( const Int *grid, char *body, Int bodyBytes )+/** A one-row device takes a flat array of colours, a taller one an array of rows. */+static void chromaBuildDeviceBody( const ChromaDevice &device, const Int *cells,+ char *body, Int bodyBytes ){- Int used = _snprintf( body, bodyBytes, "{\"effect\":\"CHROMA_CUSTOM\",\"param\":[" );- for( Int row = 0; row < CHROMA_ROWS && used > 0 && used < bodyBytes; ++row )+ const Bool nested = device.rows > 1;+ Int used = _snprintf( body, bodyBytes, "{\"effect\":\"%s\",\"param\":[", device.effect );+ for( Int row = 0; row < device.rows && used > 0 && used < bodyBytes; ++row ){- used += _snprintf( body + used, bodyBytes - used, row == 0 ? "[" : ",[" );- for( Int column = 0; column < CHROMA_COLUMNS && used > 0 && used < bodyBytes; ++column )- used += _snprintf( body + used, bodyBytes - used, column == 0 ? "%d" : ",%d",- grid[ row * CHROMA_COLUMNS + column ] );- used += _snprintf( body + used, bodyBytes - used, "]" );+ if( nested )+ used += _snprintf( body + used, bodyBytes - used, row == 0 ? "[" : ",[" );+ for( Int column = 0; column < device.columns && used > 0 && used < bodyBytes; ++column )+ {+ const Bool first = row == 0 && column == 0;+ used += _snprintf( body + used, bodyBytes - used,+ (nested ? column == 0 : first) ? "%d" : ",%d",+ cells[ row * device.columns + column ] );+ }+ if( nested )+ used += _snprintf( body + used, bodyBytes - used, "]" );}_snprintf( body + used, bodyBytes - used, "]}" );body[ bodyBytes - 1 ] = 0;@@ -188,13 +248,15 @@ static void chromaBuildKeyboardBody( const Int *grid, char *body, Int bodyBytes//-----------------------------------------------------------------------------static DWORD WINAPI chromaWorkerMain( LPVOID ){- // Big enough for six rows of twenty-two ten-digit numbers and the punctuation.- char body[ CHROMA_CELLS * 12 + 64 ];+ // Big enough for the largest device's cells as ten-digit numbers, plus the+ // punctuation and the effect name.+ char body[ KEYBOARD_CELLS * 12 + 128 ];char sessionHost[ 64 ];- char sessionPath[ 160 ];+ char sessionRoot[ 128 ];+ char devicePath[ CHROMA_DEVICE_COUNT ][ 160 ];INTERNET_PORT sessionPort = 0;Int sent[ CHROMA_CELLS ];- Int grid[ CHROMA_CELLS ];+ Int cells[ CHROMA_CELLS ];HINTERNET internet = InternetOpenA( "ZeroHourReforged", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 );HINTERNET handshake = NULL;@@ -212,7 +274,7 @@ static DWORD WINAPI chromaWorkerMain( LPVOID )// picked up; retrying on a timer would mean a second piece of state to own.Bool opened = handshake != NULL&& chromaOpenSession( handshake, sessionHost, sizeof( sessionHost ),- &sessionPort, sessionPath, sizeof( sessionPath ) );+ &sessionPort, sessionRoot, sizeof( sessionRoot ) );if( handshake != NULL )InternetCloseHandle( handshake );@@ -222,43 +284,64 @@ static DWORD WINAPI chromaWorkerMain( LPVOID )INTERNET_SERVICE_HTTP, 0, 0 );if( connection == NULL ){- DEBUG_LOG(( "Chroma: no Razer server on %s:%d, keyboard lighting is off for this run\n",+ DEBUG_LOG(( "Chroma: no Razer server on %s:%d, hardware lighting is off for this run\n",CHROMA_HOST, (Int)CHROMA_PORT ));if( internet != NULL )InternetCloseHandle( internet );return 0;}- DEBUG_LOG(( "Chroma: session open on %s:%d%s\n", sessionHost, (Int)sessionPort, sessionPath ));+ for( Int device = 0; device < CHROMA_DEVICE_COUNT; ++device )+ {+ _snprintf( devicePath[ device ], sizeof( devicePath[ device ] ), "%s/%s",+ sessionRoot, CHROMA_DEVICES[ device ].endpoint );+ devicePath[ device ][ sizeof( devicePath[ device ] ) - 1 ] = 0;+ }+ DEBUG_LOG(( "Chroma: session open on %s:%d%s\n", sessionHost, (Int)sessionPort, sessionRoot ));memset( sent, 0, sizeof( sent ) );DWORD lastSendMs = 0;+ Bool answersLogged = FALSE;while( InterlockedCompareExchange( &s_workerShouldStop, 0, 0 ) == 0 ){- EnterCriticalSection( &s_gridLock );- memcpy( grid, s_pendingGrid, sizeof( grid ) );- LeaveCriticalSection( &s_gridLock );+ EnterCriticalSection( &s_cellLock );+ memcpy( cells, s_pendingCells, sizeof( cells ) );+ LeaveCriticalSection( &s_cellLock );const DWORD nowMs = timeGetTime();- const Bool changed = memcmp( grid, sent, sizeof( grid ) ) != 0;- if( changed || nowMs - lastSendMs >= CHROMA_KEEPALIVE_MS )+ const Bool keepalive = lastSendMs == 0 || nowMs - lastSendMs >= CHROMA_KEEPALIVE_MS;+ Bool sentAnything = FALSE;+ for( Int device = 0; device < CHROMA_DEVICE_COUNT; ++device ){- chromaBuildKeyboardBody( grid, body, sizeof( body ) );- // The server answers every frame with a result code, and a refused frame- // looks exactly like a working one from here unless it is read back.+ const ChromaDevice &info = CHROMA_DEVICES[ device ];+ const Int deviceBytes = (Int)sizeof( Int ) * info.rows * info.columns;+ if( !keepalive && memcmp( cells + info.firstCell, sent + info.firstCell, deviceBytes ) == 0 )+ continue;++ chromaBuildDeviceBody( info, cells + info.firstCell, body, sizeof( body ) );+ // The server answers every frame with a result code, and a device it does+ // not have is refused rather than ignored, so the answer is worth a line+ // the first time round.char reply[ 64 ];- const Bool sentOk = chromaRequest( connection, "PUT", sessionPath, body, reply, sizeof( reply ) );- if( lastSendMs == 0 )+ const Bool sentOk = chromaRequest( connection, "PUT", devicePath[ device ],+ body, reply, sizeof( reply ) );+ if( !answersLogged ){- DEBUG_LOG(( "Chroma: first frame %s, answer %s\n",- sentOk ? "sent" : "refused", sentOk ? reply : "none" ));+ DEBUG_LOG(( "Chroma: %s %s, answer %s\n", info.endpoint,+ sentOk ? "took the frame" : "refused it", sentOk ? reply : "none" ));}- memcpy( sent, grid, sizeof( sent ) );- lastSendMs = nowMs;+ memcpy( sent + info.firstCell, cells + info.firstCell, deviceBytes );+ sentAnything = TRUE;}+ answersLogged = TRUE;+ if( sentAnything )+ lastSendMs = nowMs;Sleep( CHROMA_SEND_INTERVAL_MS );}+ // Without this the hardware holds the last frame sent until the session times+ // out, so the board stays lit for ten seconds after the game is gone.+ chromaRequest( connection, "DELETE", sessionRoot, "", NULL, 0 );InternetCloseHandle( connection );InternetCloseHandle( internet );return 0;@@ -267,8 +350,102 @@ static DWORD WINAPI chromaWorkerMain( LPVOID )//-----------------------------------------------------------------------------// The game state side. Everything below here runs on the main thread.//------------------------------------------------------------------------------static void chromaFillGrid( Int *grid )+struct SuperweaponScan{+ Real bestCharge; ///< 0 to 1, the nearest one to firing+ Bool anyReady;+};++//-----------------------------------------------------------------------------+static void scanOneSuperweapon( Object *obj, void *userData )+{+ SuperweaponScan *scan = (SuperweaponScan *)userData;++ if( !obj->isKindOf( KINDOF_FS_SUPERWEAPON )+ || obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION )+ || obj->testStatus( OBJECT_STATUS_SOLD )+ || obj->isEffectivelyDead() )+ return;++ 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 > scan->bestCharge )+ scan->bestCharge = charge;+ if( power->isReady() )+ scan->anyReady = TRUE;+ }+}++//-----------------------------------------------------------------------------+/** The most-charged superweapon the local player owns, re-counted twice a second+ * rather than every frame. */+static const SuperweaponScan &chromaSuperweaponState( Player *localPlayer, UnsignedInt frame )+{+ static SuperweaponScan scan = { 0.0f, FALSE };+ static UnsignedInt lastScanFrame = 0;++ if( frame >= lastScanFrame && frame - lastScanFrame < (UnsignedInt)SUPERWEAPON_SCAN_INTERVAL_FRAMES )+ return scan;++ lastScanFrame = frame;+ scan.bestCharge = 0.0f;+ scan.anyReady = FALSE;+ if( localPlayer != NULL )+ localPlayer->iterateObjects( scanOneSuperweapon, &scan );+ return scan;+}++//-----------------------------------------------------------------------------+Int chromaPowerSegments( Int production, Int consumption )+{+ if( production <= 0 )+ return 0;+ if( production < consumption )+ return CHROMA_POWER_BROWNOUT;++ const Real headroom = 1.0f - (Real)consumption / (Real)production;+ const Int segments = (Int)(headroom * CHROMA_POWER_SEGMENTS + 0.999f);+ return segments > CHROMA_POWER_SEGMENTS ? CHROMA_POWER_SEGMENTS : segments;+}++//-----------------------------------------------------------------------------+/** The digit row reads as a tank of power that empties: full green while there is+ * headroom, down to nothing as consumption catches production, and the whole row+ * blinking red once it has been passed. A player with no power plant at all+ * gets a dark row rather than a full or an empty one. */+static void chromaFillPowerRow( Int *cells, const Energy *energy, UnsignedInt frame )+{+ static const Int POWER_LIT = 0x00FF00; ///< green, in the BGR the device takes+ static const Int POWER_SHORT = 0x0000FF; ///< red+ static const Int POWER_DARK = 0x000000;++ const Int litSegments = chromaPowerSegments( energy->getProduction(), energy->getConsumption() );+ const Bool blinkOn = chromaBlinkIsOn( frame );++ const char *digits = CHROMA_KEY_ROWS[ CHROMA_POWER_ROW ];+ for( Int segment = 0; segment < CHROMA_POWER_SEGMENTS; ++segment )+ {+ Int color = POWER_DARK;+ if( litSegments == CHROMA_POWER_BROWNOUT )+ color = blinkOn ? POWER_SHORT : POWER_DARK;+ else if( segment < litSegments )+ color = POWER_LIT;+ cells[ chromaCellForKey( digits[ segment ] ) ] = color;+ }+}++//-----------------------------------------------------------------------------+static void chromaFillCells( Int *cells )+{+ // Warm white for anything about a superweapon, so it is never mistaken for the+ // green of the power row or the red of the alarm.+ static const Int SUPERWEAPON_COLOR = 0x0078DCFF;+Real red = 0.5f, green = 0.5f, blue = 0.5f;Player *localPlayer = ThePlayerList ? ThePlayerList->getLocalPlayer() : NULL;if( localPlayer != NULL )@@ -280,13 +457,14 @@ static void chromaFillGrid( Int *grid )blue = b / 255.0f;}+ const Bool inGame = TheGameLogic && TheGameLogic->isInGame();+ const UnsignedInt frame = TheGameLogic ? TheGameLogic->getFrame() : 0;+// How hard the board is being dragged towards red, zero when nothing is// shooting at us.Real alarm = 0.0f;- const Bool inGame = TheGameLogic && TheGameLogic->isInGame();if( inGame && localPlayer != NULL ){- const UnsignedInt frame = TheGameLogic->getFrame();const UnsignedInt attackedFrame = localPlayer->getAttackedFrame();if( attackedFrame > 0 && frame - attackedFrame < (UnsignedInt)UNDER_ATTACK_FRAMES ){@@ -301,11 +479,11 @@ static void chromaFillGrid( Int *grid )green * AMBIENT_SCALE * (1.0f - alarm),blue * AMBIENT_SCALE * (1.0f - alarm) );for( Int cell = 0; cell < CHROMA_CELLS; ++cell )- grid[ cell ] = ambient;+ cells[ cell ] = ambient;- // Outside a match there is no command bar to read, so the flat bed is the- // whole picture.- if( !inGame || TheHotKeyManager == NULL )+ // Outside a match there is no command bar, no power and no superweapon to+ // read, so the flat bed of colour is the whole picture.+ if( !inGame || localPlayer == NULL || TheHotKeyManager == NULL )return;const Int pressable = chromaColorFromComponents(@@ -314,19 +492,46 @@ static void chromaFillGrid( Int *grid )blue * PRESSABLE_SCALE * (1.0f - alarm) );const Int unavailable = chromaColorFromComponents( alarm, 0.0f, 0.0f );- for( Int row = 0; row < CHROMA_KEY_ROW_COUNT; ++row )+ for( Int row = CHROMA_FIRST_HOTKEY_ROW; row < CHROMA_KEY_ROW_COUNT; ++row ){const char *keys = CHROMA_KEY_ROWS[ row ];for( Int column = 0; keys[ column ] != 0; ++column ){- const char keyText[ 2 ] = { keys[ column ], 0 };Bool isPressable = FALSE;+ const char keyText[ 2 ] = { keys[ column ], 0 };if( !TheHotKeyManager->findHotKey( AsciiString( keyText ), &isPressable ) )continue;- grid[ chromaCellForKey( keys[ column ] ) ] = isPressable ? pressable : unavailable;+ cells[ chromaCellForKey( keys[ column ] ) ] = isPressable ? pressable : unavailable;}}++ chromaFillPowerRow( cells, localPlayer->getEnergy(), frame );++ // The numpad blinks while anything of yours can fire, and the mousepad strip+ // fills up as the nearest one charges.+ const SuperweaponScan &superweapons = chromaSuperweaponState( localPlayer, frame );+ const Bool blinkOn = chromaBlinkIsOn( frame );+ if( superweapons.anyReady )+ {+ const Int numpadColor = blinkOn ? SUPERWEAPON_COLOR : 0;+ for( Int row = NUMPAD_FIRST_ROW; row <= NUMPAD_LAST_ROW; ++row )+ for( Int column = NUMPAD_FIRST_COLUMN; column <= NUMPAD_LAST_COLUMN; ++column )+ cells[ row * KEYBOARD_COLUMNS + column ] = numpadColor;+ }++ Int *mousepad = cells + MOUSEPAD_FIRST_CELL;+ if( superweapons.anyReady )+ {+ for( Int led = 0; led < MOUSEPAD_CELLS; ++led )+ mousepad[ led ] = blinkOn ? SUPERWEAPON_COLOR : 0;+ }+ else if( superweapons.bestCharge > 0.0f )+ {+ const Int litLeds = (Int)(superweapons.bestCharge * MOUSEPAD_CELLS + 0.999f);+ for( Int led = 0; led < MOUSEPAD_CELLS; ++led )+ mousepad[ led ] = led < litLeds ? pressable : 0;+ }}//-----------------------------------------------------------------------------@@ -334,23 +539,23 @@ void updateChromaKeyboard( void ){if( !s_workerRunning ){- InitializeCriticalSection( &s_gridLock );+ InitializeCriticalSection( &s_cellLock );DWORD threadId = 0;s_workerThread = ::CreateThread( NULL, 0, chromaWorkerMain, NULL, 0, &threadId );if( s_workerThread == NULL ){- DeleteCriticalSection( &s_gridLock );+ DeleteCriticalSection( &s_cellLock );return;}s_workerRunning = TRUE;}- Int grid[ CHROMA_CELLS ];- chromaFillGrid( grid );+ Int cells[ CHROMA_CELLS ];+ chromaFillCells( cells );- EnterCriticalSection( &s_gridLock );- memcpy( s_pendingGrid, grid, sizeof( s_pendingGrid ) );- LeaveCriticalSection( &s_gridLock );+ EnterCriticalSection( &s_cellLock );+ memcpy( s_pendingCells, cells, sizeof( s_pendingCells ) );+ LeaveCriticalSection( &s_cellLock );}//-----------------------------------------------------------------------------@@ -363,6 +568,6 @@ void shutdownChromaKeyboard( void )WaitForSingleObject( s_workerThread, 2000 );CloseHandle( s_workerThread );s_workerThread = NULL;- DeleteCriticalSection( &s_gridLock );+ DeleteCriticalSection( &s_cellLock );s_workerRunning = FALSE;}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex e6bfc5c85..2c1e78428 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -13043,6 +13043,22 @@ TEST(chroma_keys_land_on_the_razer_grid)CHECK_EQ(chromaCellForKey(' '), -1);}+// The digit row is a tank that empties: ten green while nothing draws, fewer as+// the draw catches the supply, and the row goes to blinking red past it.+TEST(chroma_power_meter_empties_as_the_draw_catches_the_supply)+{+ CHECK_EQ(chromaPowerSegments(0, 0), 0);+ CHECK_EQ(chromaPowerSegments(0, 5), 0);+ CHECK_EQ(chromaPowerSegments(10, 0), 10);+ CHECK_EQ(chromaPowerSegments(10, 5), 5);+ CHECK_EQ(chromaPowerSegments(10, 9), 1);+ // Every scrap of headroom keeps a key alight, so the row never reads empty+ // while the base is still fully powered.+ CHECK_EQ(chromaPowerSegments(100, 99), 1);+ CHECK_EQ(chromaPowerSegments(10, 10), 0);+ CHECK_EQ(chromaPowerSegments(10, 11), CHROMA_POWER_BROWNOUT);+}+#include "test_camera_behavior.inc"#include "test_production_input.inc"#include "test_minimap_input.inc"