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.

feat(gameclient): drop-down console on the key above tab

Olcay Seygan714ad39d701c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ce9aab238..ff913bf17 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -654,6 +654,13 @@ 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 console, on the key above Tab
+
+- Press the key above Tab and a black panel drops over the top of the screen with a prompt waiting at the bottom of it. Type into it and the game ignores the keys it would otherwise have read as orders, so a word starting with Q does not queue a tank. Escape or the same key again puts it away.
+- `help` lists what it takes, `clear` empties it, `echo` says a line back. The up and down arrows walk back through the last thirty-two things you typed.
+- It opens in the main menu as well as in a match. Nothing it does yet touches the rules of a game, so it is safe to leave open while one runs.
+- The general's promotion screen moved off that key onto the backslash beside it.
+
## Your Razer hardware plays along
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.
diff --git a/GeneralsMD/Code/Data/INI/CommandMap.ini b/GeneralsMD/Code/Data/INI/CommandMap.ini
index 9ad9cd092..7e5576576 100644
--- a/GeneralsMD/Code/Data/INI/CommandMap.ini
+++ b/GeneralsMD/Code/Data/INI/CommandMap.ini
@@ -351,9 +351,11 @@ CommandMap END_WAYPOINTS
UseableIn = GAME
End
-; the general's promotion screen, on the key above tab
+; the general's promotion screen. It used to sit on the key above tab; the console took that
+; key, and takes it in the translator before the command map is consulted, so a binding there
+; would never fire again.
CommandMap TOGGLE_PURCHASE_SCIENCE
- Key = KEY_TICK
+ Key = KEY_BACKSLASH
Transition = DOWN
Modifiers = NONE
UseableIn = GAME
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/GameConsole.h b/GeneralsMD/Code/GameEngine/Include/GameClient/GameConsole.h
new file mode 100644
index 000000000..35d8b22e7
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/GameConsole.h
@@ -0,0 +1,96 @@
+/*
+** 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/>.
+*/
+
+// FILE: GameConsole.h //////////////////////////////////////////////////////////////////////////
+// Desc: Drop-down command console on the key above Tab, drawn straight to the display rather
+// than through a .wnd layout, so it works in the shell and in a match alike.
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#pragma once
+
+#ifndef __GAMECONSOLE_H_
+#define __GAMECONSOLE_H_
+
+#include "Common/MessageStream.h"
+#include "Common/AsciiString.h"
+#include "Common/UnicodeString.h"
+
+#include <deque>
+#include <vector>
+
+class DisplayString;
+class GameFont;
+
+/**
+ * The console's own scrollback, input line and command table. One global, TheGameConsole,
+ * created beside the rest of the client overlays in GameClient::init.
+ */
+class GameConsole
+{
+public:
+
+ GameConsole();
+ ~GameConsole();
+
+ Bool isOpen( void ) const { return m_isOpen; }
+ void toggle( void );
+ void close( void );
+
+ /// Feed one raw key event in. The console owns every key while it is open.
+ void handleKey( UnsignedByte key, UnsignedShort keyState );
+
+ /// Append one line to the scrollback.
+ void printLine( UnicodeString line );
+ void printLine( AsciiString line );
+
+ /// Draw the panel. Called from the display's 2D overlay pass.
+ void render( void );
+
+private:
+
+ void submitInputLine( void );
+ void runCommand( AsciiString commandLine );
+ void recallHistory( Int direction );
+
+ typedef std::deque<UnicodeString> ScrollbackLines;
+ typedef std::vector<AsciiString> CommandHistory;
+
+ Bool m_isOpen;
+ UnicodeString m_inputLine;
+ ScrollbackLines m_scrollback;
+ CommandHistory m_history;
+ Int m_historyCursor; ///< index into m_history, or m_history.size() when typing fresh
+
+ GameFont *m_font;
+ DisplayString **m_lineStrings; ///< one per drawable row, reused every frame
+ Int m_lineStringCount;
+};
+
+extern GameConsole *TheGameConsole;
+
+/**
+ * Sits ahead of the window system on the message stream so the console gets the key above Tab
+ * before anything bound in CommandMap.ini does, and swallows every key while it is open.
+ */
+class GameConsoleTranslator : public GameMessageTranslator
+{
+public:
+ virtual GameMessageDisposition translateGameMessage( const GameMessage *msg );
+};
+
+#endif // __GAMECONSOLE_H_
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index 9adfb3349..90d2c0700 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -307,6 +307,12 @@ protected:
void queueCapturer(void); ///< ... or build the cheapest thing that can take one
void queueSupportUnit(const ThingTemplate *tmpl, const char *what); ///< one cheap unit, outside the team system
+ /** The enemy's tanks are worth more taken than shot: one hijacker walks off with one, and Black
+ * Lotus shuts one down for nothing. Neither had ever been used by a computer player. */
+ virtual void doHijack(void);
+ Object *findHijacker(void); ///< a unit of ours that takes a vehicle by walking into it
+ Object *nearestStealableVehicle(const Coord3D *from, Real reach); ///< the closest enemy vehicle in sight; reach <= 0 is the map
+
/** Past the hoard, buy what the build list never had: another production building beside the last
* expansion when every one of a kind is busy, another income building when production keeps up,
* and money units (China's hackers) from any factory standing idle. */
@@ -425,6 +431,8 @@ protected:
UnsignedInt m_startIntelFrame; ///< frame the above was last brought up to date
ObjectID m_capturerID; ///< the unit currently out taking tech buildings for us
Int m_captureTimer; ///< frames until the next look for something to capture
+ ObjectID m_hijackerID; ///< the thief currently out after an enemy vehicle
+ Int m_hijackTimer; ///< frames until the next look for a vehicle to take
Int m_retreatTimer; ///< frames until the next look at how the fights are going
Int m_expandTimer; ///< frames until the next look for somewhere to expand to
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
index 95f7f643f..85e3adfec 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GameClient.cpp
@@ -57,6 +57,7 @@
#include "GameClient/PlayerColorScheme.h"
#include "GameClient/GameWindowManager.h"
#include "GameClient/GlobalLanguage.h"
+#include "GameClient/GameConsole.h"
#include "GameClient/GraphDraw.h"
#include "GameClient/GUICommandTranslator.h"
#include "GameClient/HeaderTemplate.h"
@@ -177,6 +178,9 @@ GameClient::GameClient()
//-------------------------------------------------------------------------------------------------
GameClient::~GameClient()
{
+ delete TheGameConsole;
+ TheGameConsole = NULL;
+
#ifdef PERF_TIMERS
delete TheGraphDraw;
TheGraphDraw = NULL;
@@ -347,6 +351,8 @@ void GameClient::init( void )
//
// since we only allocate one of each, don't bother pooling 'em
+ // ahead of the window system, so the console owns the key above Tab and every key it types
+ m_translators[ m_numTranslators++ ] = TheMessageStream->attachTranslator( MSGNEW("GameClientSubsystem") GameConsoleTranslator, 5 );
m_translators[ m_numTranslators++ ] = TheMessageStream->attachTranslator( MSGNEW("GameClientSubsystem") WindowTranslator, 10 );
m_translators[ m_numTranslators++ ] = TheMessageStream->attachTranslator( MSGNEW("GameClientSubsystem") MetaEventTranslator, 20 );
m_translators[ m_numTranslators++ ] = TheMessageStream->attachTranslator( MSGNEW("GameClientSubsystem") HotKeyTranslator, 25 );
@@ -496,6 +502,8 @@ void GameClient::init( void )
TheGraphDraw = new GraphDraw;
#endif
+ TheGameConsole = new GameConsole;
+
} // end init
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GameConsole.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GameConsole.cpp
new file mode 100644
index 000000000..184bad171
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GameConsole.cpp
@@ -0,0 +1,314 @@
+/*
+** 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/>.
+*/
+
+// FILE: GameConsole.cpp ////////////////////////////////////////////////////////////////////////
+// Desc: The drop-down console. Drawn with drawFillRect and DisplayStrings the way GraphDraw is,
+// rather than through a .wnd layout, so it needs no data file and works in the shell too.
+///////////////////////////////////////////////////////////////////////////////////////////////////
+
+#include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
+
+#include "GameClient/GameConsole.h"
+
+#include "GameClient/Color.h"
+#include "GameClient/Display.h"
+#include "GameClient/DisplayString.h"
+#include "GameClient/DisplayStringManager.h"
+#include "GameClient/GameFont.h"
+#include "GameClient/KeyDefs.h"
+#include "GameClient/Keyboard.h"
+
+GameConsole *TheGameConsole = NULL;
+
+static const char *CONSOLE_FONT_NAME = "Courier";
+static const Int CONSOLE_FONT_POINT_SIZE = 12;
+static const Bool CONSOLE_FONT_BOLD = FALSE;
+
+static const Real CONSOLE_HEIGHT_FRACTION = 0.45f; ///< share of the screen the panel covers
+static const Int CONSOLE_PADDING = 6;
+static const Int CONSOLE_LINE_GAP = 2; ///< pixels between rows on top of the font height
+static const Int CONSOLE_EDGE_THICKNESS = 2;
+
+static const Int CONSOLE_MAX_ROWS = 64; ///< display strings kept alive, one per drawable row
+static const Int CONSOLE_SCROLLBACK_LIMIT = 256;
+static const Int CONSOLE_HISTORY_LIMIT = 32;
+
+static const WideChar *CONSOLE_PROMPT = L"> ";
+static const WideChar *CONSOLE_CURSOR = L"_";
+static const WideChar CONSOLE_FIRST_PRINTABLE_CHAR = L' ';
+
+static const Color CONSOLE_PANEL_COLOR = GameMakeColor( 0, 0, 0, 225 );
+static const Color CONSOLE_EDGE_COLOR = GameMakeColor( 90, 90, 90, 255 );
+static const Color CONSOLE_INPUT_COLOR = GameMakeColor( 255, 255, 255, 255 );
+static const Color CONSOLE_TEXT_COLOR = GameMakeColor( 190, 190, 190, 255 );
+static const Color CONSOLE_SHADOW_COLOR = GameMakeColor( 0, 0, 0, 0 );
+
+//-------------------------------------------------------------------------------------------------
+GameConsole::GameConsole()
+ : m_isOpen( FALSE ),
+ m_historyCursor( 0 ),
+ m_font( NULL ),
+ m_lineStrings( NULL ),
+ m_lineStringCount( 0 )
+{
+ m_font = TheFontLibrary->getFont( AsciiString( CONSOLE_FONT_NAME ),
+ CONSOLE_FONT_POINT_SIZE,
+ CONSOLE_FONT_BOLD );
+
+ m_lineStrings = new DisplayString *[ CONSOLE_MAX_ROWS ];
+ for( Int row = 0; row < CONSOLE_MAX_ROWS; ++row )
+ {
+ m_lineStrings[ row ] = TheDisplayStringManager->newDisplayString();
+ m_lineStrings[ row ]->setFont( m_font );
+ }
+ m_lineStringCount = CONSOLE_MAX_ROWS;
+
+ printLine( AsciiString( "Zero Hour Reforged console. 'help' lists what there is." ) );
+}
+
+//-------------------------------------------------------------------------------------------------
+GameConsole::~GameConsole()
+{
+ for( Int row = 0; row < m_lineStringCount; ++row )
+ TheDisplayStringManager->freeDisplayString( m_lineStrings[ row ] );
+
+ delete [] m_lineStrings;
+ m_lineStrings = NULL;
+ m_lineStringCount = 0;
+}
+
+//-------------------------------------------------------------------------------------------------
+void GameConsole::toggle( void )
+{
+ m_isOpen = !m_isOpen;
+ if( !m_isOpen )
+ m_inputLine.clear();
+}
+
+//-------------------------------------------------------------------------------------------------
+void GameConsole::close( void )
+{
+ m_isOpen = FALSE;
+ m_inputLine.clear();
+}
+
+//-------------------------------------------------------------------------------------------------
+void GameConsole::printLine( UnicodeString line )
+{
+ m_scrollback.push_back( line );
+ while( (Int)m_scrollback.size() > CONSOLE_SCROLLBACK_LIMIT )
+ m_scrollback.pop_front();
+}
+
+//-------------------------------------------------------------------------------------------------
+void GameConsole::printLine( AsciiString line )
+{
+ UnicodeString wide;
+ wide.translate( line );
+ printLine( wide );
+}
+
+//-------------------------------------------------------------------------------------------------
+void GameConsole::handleKey( UnsignedByte key, UnsignedShort keyState )
+{
+ if( !BitTest( keyState, KEY_STATE_DOWN ) )
+ return;
+
+ switch( key )
+ {
+ case KEY_ESC:
+ close();
+ return;
+
+ case KEY_ENTER:
+ case KEY_KPENTER:
+ submitInputLine();
+ return;
+
+ case KEY_BACKSPACE:
+ m_inputLine.removeLastChar();
+ return;
+
+ case KEY_UP:
+ recallHistory( -1 );
+ return;
+
+ case KEY_DOWN:
+ recallHistory( 1 );
+ return;
+ }
+
+ // a modified key is a shortcut, not something to type
+ if( BitTest( keyState, KEY_STATE_CONTROL | KEY_STATE_ALT ) )
+ return;
+
+ const Int shiftedTable = BitTest( keyState, KEY_STATE_SHIFT ) ? 1 : 0;
+ const WideChar printable = TheKeyboard->getPrintableKey( key, shiftedTable );
+ if( printable >= CONSOLE_FIRST_PRINTABLE_CHAR )
+ m_inputLine.concat( printable );
+}
+
+//-------------------------------------------------------------------------------------------------
+void GameConsole::recallHistory( Int direction )
+{
+ if( m_history.empty() )
+ return;
+
+ m_historyCursor += direction;
+ if( m_historyCursor < 0 )
+ m_historyCursor = 0;
+ if( m_historyCursor > (Int)m_history.size() )
+ m_historyCursor = (Int)m_history.size();
+
+ if( m_historyCursor == (Int)m_history.size() )
+ m_inputLine.clear();
+ else
+ m_inputLine.translate( m_history[ m_historyCursor ] );
+}
+
+//-------------------------------------------------------------------------------------------------
+void GameConsole::submitInputLine( void )
+{
+ AsciiString commandLine;
+ commandLine.translate( m_inputLine );
+ commandLine.trim();
+ m_inputLine.clear();
+
+ if( commandLine.isEmpty() )
+ return;
+
+ UnicodeString typed;
+ typed.translate( commandLine );
+ UnicodeString echoed( CONSOLE_PROMPT );
+ echoed.concat( typed );
+ printLine( echoed );
+
+ m_history.push_back( commandLine );
+ while( (Int)m_history.size() > CONSOLE_HISTORY_LIMIT )
+ m_history.erase( m_history.begin() );
+ m_historyCursor = (Int)m_history.size();
+
+ runCommand( commandLine );
+}
+
+//-------------------------------------------------------------------------------------------------
+void GameConsole::runCommand( AsciiString commandLine )
+{
+ AsciiString arguments = commandLine;
+ AsciiString command;
+ arguments.nextToken( &command );
+ command.toLower();
+ arguments.trim();
+
+ if( command == "help" )
+ {
+ printLine( AsciiString( "help this list" ) );
+ printLine( AsciiString( "clear empty the scrollback" ) );
+ printLine( AsciiString( "echo <text> print the text back" ) );
+ return;
+ }
+
+ if( command == "clear" )
+ {
+ m_scrollback.clear();
+ return;
+ }
+
+ if( command == "echo" )
+ {
+ printLine( arguments );
+ return;
+ }
+
+ UnicodeString unknown( L"unknown command: " );
+ UnicodeString name;
+ name.translate( command );
+ unknown.concat( name );
+ printLine( unknown );
+}
+
+//-------------------------------------------------------------------------------------------------
+void GameConsole::render( void )
+{
+ if( !m_isOpen )
+ return;
+
+ const Int screenWidth = TheDisplay->getWidth();
+ const Int panelHeight = REAL_TO_INT( TheDisplay->getHeight() * CONSOLE_HEIGHT_FRACTION );
+ const Int lineHeight = m_font->height + CONSOLE_LINE_GAP;
+
+ TheDisplay->drawFillRect( 0, 0, screenWidth, panelHeight, CONSOLE_PANEL_COLOR );
+ TheDisplay->drawFillRect( 0, panelHeight - CONSOLE_EDGE_THICKNESS,
+ screenWidth, CONSOLE_EDGE_THICKNESS, CONSOLE_EDGE_COLOR );
+
+ Int y = panelHeight - CONSOLE_EDGE_THICKNESS - CONSOLE_PADDING - lineHeight;
+
+ UnicodeString prompt( CONSOLE_PROMPT );
+ prompt.concat( m_inputLine );
+ prompt.concat( CONSOLE_CURSOR );
+ m_lineStrings[ 0 ]->setText( prompt );
+ m_lineStrings[ 0 ]->draw( CONSOLE_PADDING, y, CONSOLE_INPUT_COLOR, CONSOLE_SHADOW_COLOR );
+
+ Int rowsThatFit = (y - CONSOLE_PADDING) / lineHeight;
+ if( rowsThatFit > m_lineStringCount - 1 )
+ rowsThatFit = m_lineStringCount - 1;
+
+ Int row = 0;
+ ScrollbackLines::reverse_iterator it = m_scrollback.rbegin();
+ while( row < rowsThatFit && it != m_scrollback.rend() )
+ {
+ y -= lineHeight;
+ m_lineStrings[ row + 1 ]->setText( *it );
+ m_lineStrings[ row + 1 ]->draw( CONSOLE_PADDING, y, CONSOLE_TEXT_COLOR, CONSOLE_SHADOW_COLOR );
+ ++row;
+ ++it;
+ }
+}
+
+//-------------------------------------------------------------------------------------------------
+GameMessageDisposition GameConsoleTranslator::translateGameMessage( const GameMessage *msg )
+{
+ if( TheGameConsole == NULL )
+ return KEEP_MESSAGE;
+
+ switch( msg->getType() )
+ {
+ case GameMessage::MSG_RAW_KEY_DOWN:
+ case GameMessage::MSG_RAW_KEY_UP:
+ {
+ const UnsignedByte key = msg->getArgument( 0 )->integer;
+ const UnsignedShort keyState = msg->getArgument( 1 )->integer;
+
+ // the key above Tab belongs to the console whether it is open or shut
+ if( key == KEY_TICK )
+ {
+ if( BitTest( keyState, KEY_STATE_DOWN ) && !BitTest( keyState, KEY_STATE_AUTOREPEAT ) )
+ TheGameConsole->toggle();
+ return DESTROY_MESSAGE;
+ }
+
+ if( !TheGameConsole->isOpen() )
+ return KEEP_MESSAGE;
+
+ TheGameConsole->handleKey( key, keyState );
+ return DESTROY_MESSAGE;
+ }
+ }
+
+ return KEEP_MESSAGE;
+}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index cad2a6179..7b4e26b33 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -68,6 +68,7 @@
#include "GameLogic/PartitionManager.h"
#include "Common/ActionManager.h" // canCaptureBuilding, for the tech buildings
#include "GameLogic/Module/SpecialPowerModule.h" // ... and the module that does it
+#include "GameLogic/Module/CollideModule.h" // ... and the collide that takes a vehicle by touching it
#include "GameLogic/Module/ContainModule.h"
#include "GameLogic/Module/JetAIUpdate.h" // a Comanche is a jet with no runway
#include <map>
@@ -185,6 +186,10 @@ static const Int SCOUT_CHECK_RATE = 2 * LOGICFRAMES_PER_SECOND;
* this is a check, not a re-path. */
static const Int CAPTURE_CHECK_RATE = 5 * LOGICFRAMES_PER_SECOND;
+/** How often the AI looks for a vehicle to take. Same rhythm as the capture check, and the target
+ * has to be in sight, so a faster clock would only re-order what is already walking. */
+static const Int HIJACK_CHECK_RATE = 5 * LOGICFRAMES_PER_SECOND;
+
#ifdef DEBUG_LOGGING
/** Milliseconds between two performance counter readings. Used by the per-job AI profile below and
* by the sub-timers inside processBaseBuilding, which is why it lives up here. */
@@ -298,6 +303,8 @@ m_role(AIROLE_AGGRESSIVE)
m_startIntelFrame = 0;
m_capturerID = INVALID_ID;
m_captureTimer = 1;
+ m_hijackerID = INVALID_ID;
+ m_hijackTimer = 1;
for( Int held = 0; held < MAX_HELD_TEAMS; ++held )
{
@@ -377,6 +384,7 @@ m_role(AIROLE_AGGRESSIVE)
const Int SPREAD = 2 * LOGICFRAMES_PER_SECOND;
m_expandTimer += computeUpdatePhase( playerIndex, SPREAD );
m_captureTimer += computeUpdatePhase( playerIndex, SPREAD );
+ m_hijackTimer += computeUpdatePhase( playerIndex, SPREAD );
m_retreatTimer += computeUpdatePhase( playerIndex, SPREAD );
// The scout check's own cycle is already only two seconds, so its full cycle is the window.
m_scoutTimer += computeUpdatePhase( playerIndex, SCOUT_CHECK_RATE );
@@ -4250,6 +4258,7 @@ void AIPlayer::update( void )
AI_PHASE( AIP_RETREAT, doRetreats() ); // Break off the fights we are losing.
AI_PHASE( AIP_EXPAND, doExpansion() ); // Go and take the money that is lying around.
AI_PHASE( AIP_CAPTURE, doCapture() ); // ... and the money that is standing around.
+ AI_PHASE( AIP_CAPTURE, doHijack() ); // Take the enemy's tanks rather than shoot them.
AI_PHASE( AIP_ECONOMY, doEconomy() ); // ... and the money sitting in the bank.
AI_PHASE( AIP_POWER, doPower() ); // Keep the lights on, and out of reach.
AI_PHASE( AIP_ECONOMY, doSuperweapons() ); // The big guns, as soon as they can be bought.
@@ -5945,6 +5954,8 @@ Object *AIPlayer::findScout( void )
continue;
if( obj->getAI() == NULL )
continue;
+ if( obj->getID() == m_capturerID || obj->getID() == m_hijackerID )
+ continue; // it has a job; two owners giving one unit orders is how both jobs stall
Bool alreadyScouting = FALSE;
for( Int slot = 0; slot < MAX_AI_SCOUTS; ++slot )
@@ -6584,6 +6595,255 @@ void AIPlayer::queueCapturer( void )
queueSupportUnit( cheapestCapturerTemplate( m_player ), "CAPTURE" );
}
+/** How far a vehicle hacker will go for a target. About a unit's sight: Black Lotus shuts a tank
+ * down for a few seconds, which is worth nothing if she spends a minute walking to it and dies
+ * standing next to it. */
+static const Real VEHICLE_HACK_REACH = 250.0f;
+
+//----------------------------------------------------------------------------------------------------------
+/** This one takes a vehicle by walking into it. Asked of the object rather than of a faction table,
+ * so it lands on the GLA Hijacker and on anything a mod gave the same collide to. */
+static Bool carriesHijackModule( const Object *obj )
+{
+ for( BehaviorModule **m = obj->getBehaviorModules(); *m; ++m )
+ {
+ const CollideModuleInterface *collide = (*m)->getCollide();
+ if( collide && collide->isHijackedVehicleCrateCollide() )
+ return TRUE;
+ }
+ return FALSE;
+}
+
+//----------------------------------------------------------------------------------------------------------
+/** The cheapest thing this player can build that can do it. The module list is the test here too -
+ * the hijack has no command button to read, because the player just clicks the tank. */
+static const ThingTemplate *cheapestHijackerTemplate( Player *player )
+{
+ const ThingTemplate *best = NULL;
+ Int bestCost = 0;
+
+ for( const ThingTemplate *t = TheThingFactory->firstTemplate(); t; t = t->friend_getNextTemplate() )
+ {
+ if( !t->isKindOf( KINDOF_INFANTRY ) || !player->canBuild( t ) )
+ continue;
+
+ const Int cost = t->calcCostToBuild( player );
+ if( cost <= 0 || (best != NULL && cost >= bestCost) )
+ continue;
+
+ Bool canHijack = FALSE;
+ const ModuleInfo &modules = t->getBehaviorModuleInfo();
+ for( Int i = 0; i < modules.getCount(); ++i )
+ {
+ if( modules.getNthName( i ).compareNoCase( "ConvertToHijackedVehicleCrateCollide" ) == 0 )
+ canHijack = TRUE;
+ }
+ if( !canHijack )
+ continue;
+
+ best = t;
+ bestCost = cost;
+ }
+ return best;
+}
+
+//----------------------------------------------------------------------------------------------------------
+/**
+ * A thief of ours with nothing to do. Default team only, the same rule findCapturer and findScout
+ * follow: one that arrived with an attack team has a job already.
+ */
+Object *AIPlayer::findHijacker( void )
+{
+ Team *team = m_player->getDefaultTeam();
+ if( team == NULL )
+ return NULL;
+
+ for( DLINK_ITERATOR<Object> iter = team->iterate_TeamMemberList(); !iter.done(); iter.advance() )
+ {
+ Object *obj = iter.cur();
+ if( obj == NULL || obj->isEffectivelyDead() || obj->isContained() || obj->getAI() == NULL )
+ continue;
+ if( obj->getID() == m_capturerID )
+ continue; // out taking a derrick
+ if( !carriesHijackModule( obj ) )
+ continue;
+
+ Bool busy = FALSE;
+ for( Int slot = 0; slot < MAX_AI_SCOUTS; ++slot )
+ if( m_scoutID[ slot ] == obj->getID() )
+ busy = TRUE; // the map still has to be looked at
+ if( busy )
+ continue;
+
+ return obj;
+ }
+ return NULL;
+}
+
+//----------------------------------------------------------------------------------------------------------
+/**
+ * The closest enemy vehicle in sight, or none within reach. reach <= 0 is the whole map.
+ *
+ * A vehicle goes back to SHROUDED the moment our vision leaves it, so observerKnowsAbout reads here
+ * as "we can see it now" - the only honest rule for a target that drives off while we walk to it.
+ *
+ * Nearest, not richest: a thief walks, and the Humvee it reaches is worth more than the Overlord it
+ * dies halfway to.
+ */
+Object *AIPlayer::nearestStealableVehicle( const Coord3D *from, Real reach )
+{
+ if( ThePlayerList == NULL || from == NULL )
+ return NULL;
+
+ const Int myNdx = m_player->getPlayerIndex();
+ const Real reachSqr = reach * reach;
+ Object *best = NULL;
+ Real bestDistSqr = 0.0f;
+
+ const Int playerCount = ThePlayerList->getPlayerCount();
+ for( Int i = 0; i < playerCount; ++i )
+ {
+ Player *p = ThePlayerList->getNthPlayer( i );
+ if( p == NULL || p == m_player )
+ continue;
+ if( m_player->getRelationship( p->getDefaultTeam() ) != ENEMIES )
+ continue; // a civilian's parked car is not a prize, and an ally's tank is not either
+
+ for( Player::PlayerTeamList::const_iterator it = p->getPlayerTeams()->begin();
+ it != p->getPlayerTeams()->end(); ++it )
+ {
+ for( DLINK_ITERATOR<Team> teamIter = (*it)->iterate_TeamInstanceList(); !teamIter.done(); teamIter.advance() )
+ {
+ Team *team = teamIter.cur();
+ if( team == NULL )
+ continue;
+
+ for( DLINK_ITERATOR<Object> objIter = team->iterate_TeamMemberList(); !objIter.done(); objIter.advance() )
+ {
+ Object *obj = objIter.cur();
+ if( obj == NULL || obj->isEffectivelyDead() || obj->isContained() )
+ continue;
+ if( !obj->isKindOf( KINDOF_VEHICLE ) )
+ continue;
+ if( obj->isKindOf( KINDOF_AIRCRAFT ) || obj->isKindOf( KINDOF_DRONE ) )
+ continue; // neither a hijacker nor a hacker is allowed either of those
+ if( !observerKnowsAbout( obj, myNdx ) )
+ continue;
+
+ const Coord3D *at = obj->getPosition();
+ const Real dx = at->x - from->x;
+ const Real dy = at->y - from->y;
+ const Real distSqr = dx*dx + dy*dy;
+ if( reach > 0.0f && distSqr > reachSqr )
+ continue;
+ if( best == NULL || distSqr < bestDistSqr )
+ {
+ best = obj;
+ bestDistSqr = distSqr;
+ }
+ }
+ }
+ }
+ }
+ return best;
+}
+
+//----------------------------------------------------------------------------------------------------------
+/** Black Lotus, or whoever else the data gave the vehicle hack to, standing about with it ready. */
+static void findIdleVehicleHacker( Object *obj, void *userData )
+{
+ Object **hacker = (Object **)userData;
+ if( *hacker || obj->isEffectivelyDead() || obj->isContained() )
+ return;
+ if( !obj->hasSpecialPower( SPECIAL_BLACKLOTUS_DISABLE_VEHICLE_HACK ) )
+ return;
+ if( obj->testStatus( OBJECT_STATUS_IS_USING_ABILITY ) )
+ return;
+ AIUpdateInterface *ai = obj->getAI();
+ if( ai == NULL || !ai->isIdle() )
+ return;
+ *hacker = obj;
+}
+
+//----------------------------------------------------------------------------------------------------------
+/**
+ * Take the enemy's vehicles instead of shooting at them.
+ *
+ * A hijacker costs a few hundred and walks off with a tank, and no computer player had ever built
+ * one: the skirmish scripts name their teams by template and not one of the shipped teams asks for
+ * it. So one thief at a time, bought out of spare change the way the capturer is, and only while
+ * there is something in sight to take. It is the same money argument as the derrick - the cheapest
+ * unit in the game trading itself for the most expensive one on the field.
+ *
+ * The vehicle hack rides along here because it is the same decision made for free: Black Lotus
+ * arrives with the script's teams already, her hack costs nothing and sits ready most of the match,
+ * and the AI has never once used it. She only takes what is already in front of her.
+ */
+void AIPlayer::doHijack( void )
+{
+ if( --m_hijackTimer > 0 )
+ return;
+ m_hijackTimer = HIJACK_CHECK_RATE;
+
+ if( m_player == NULL || !m_player->isPlayableSide() )
+ return;
+
+ Object *hacker = NULL;
+ m_player->iterateObjects( findIdleVehicleHacker, &hacker );
+ if( hacker )
+ {
+ Object *victim = nearestStealableVehicle( hacker->getPosition(), VEHICLE_HACK_REACH );
+ SpecialPowerModuleInterface *mod = hacker->findSpecialPowerModuleInterface( SPECIAL_BLACKLOTUS_DISABLE_VEHICLE_HACK );
+ if( victim && mod && TheActionManager->canDisableVehicleViaHacking( hacker, victim, CMD_FROM_AI ) )
+ {
+ mod->doSpecialPowerAtObject( victim, 0 );
+ DEBUG_LOG(("AI player %d hacks a '%s'\n", m_player->getPlayerIndex(),
+ victim->getTemplate()->getName().str()));
+ }
+ }
+
+ Object *thief = TheGameLogic->findObjectByID( m_hijackerID );
+ if( thief && (thief->isEffectivelyDead() || thief->getControllingPlayer() != m_player) )
+ thief = NULL; // dead, or it is sitting in the tank it took and is somebody else's problem
+ if( thief == NULL )
+ {
+ m_hijackerID = INVALID_ID;
+ thief = findHijacker();
+ if( thief )
+ m_hijackerID = thief->getID();
+ }
+
+ Coord3D from = m_baseCenter;
+ if( thief )
+ from = *thief->getPosition();
+
+ Object *target = nearestStealableVehicle( &from, 0.0f );
+ if( target == NULL )
+ return; // nothing in sight; the thief waits where it is rather than walk into the fog
+
+ if( thief == NULL )
+ {
+ // only pay for one when there is something to spend it on
+ queueSupportUnit( cheapestHijackerTemplate( m_player ), "HIJACK" );
+ return;
+ }
+
+ AIUpdateInterface *ai = thief->getAI();
+ if( ai == NULL || !ai->isIdle() )
+ return; // still on its way to the last one
+
+ //
+ // canHijackVehicle refuses through the shroud, and aiEnter is the same order the player's click
+ // ends up as: the walk is the attack, and the collide module does the rest on contact.
+ //
+ if( TheActionManager->canHijackVehicle( thief, target, CMD_FROM_AI ) )
+ {
+ ai->aiEnter( target, CMD_FROM_AI );
+ DEBUG_LOG(("AI player %d sends a '%s' after a '%s'\n", m_player->getPlayerIndex(),
+ thief->getTemplate()->getName().str(), target->getTemplate()->getName().str()));
+ }
+}
+
//----------------------------------------------------------------------------------------------------------
/**
* Keep one unit looking at the map. Cheap by construction: one unit, ordered only when it has
@@ -6894,7 +7154,7 @@ void AIPlayer::xfer( Xfer *xfer )
{
// version
- XferVersion currentVersion = 7; // 2: scout 3: rung and role 4: scouting stamps 5: the capturer 6: parked waves 7: superweapon aims
+ XferVersion currentVersion = 8; // 2: scout 3: rung and role 4: scouting stamps 5: the capturer 6: parked waves 7: superweapon aims 8: the hijacker
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -7081,6 +7341,12 @@ void AIPlayer::xfer( Xfer *xfer )
xfer->xferObjectID( &m_capturerID );
xfer->xferInt( &m_captureTimer );
}
+ // the thief, for the same reason: a loaded game does not buy a second one
+ if( version >= 8 )
+ {
+ xfer->xferObjectID( &m_hijackerID );
+ xfer->xferInt( &m_hijackTimer );
+ }
// the attack teams parked at the staging point, so a loaded game sends the same wave
if( version >= 6 )
{
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
index 0009eecf5..56d794222 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DDisplay.cpp
@@ -63,6 +63,7 @@ static void drawFramerateBar(void);
#include "GameClient/Drawable.h"
#include "GameClient/GameText.h"
+#include "GameClient/GameConsole.h"
#include "GameClient/GraphDraw.h"
#include "GameClient/Line2D.h"
#include "GameClient/Mouse.h"
@@ -2497,6 +2498,10 @@ AGAIN:
TheGraphDraw->render();
TheGraphDraw->clear();
#endif
+ // last of the 2D overlays, so the console covers everything it drops over
+ if( TheGameConsole )
+ TheGameConsole->render();
+
if (s_screenShotPending)
{
s_screenShotPending = FALSE;