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(gameengine): start an eight-player skirmish from the command line

Olcay Seygan6d42746c4675
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1806241c5..bf63538f1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1127,6 +1127,13 @@ sake — the 2003 code is still the 2003 code.
every build. They are not decoration: **most of the bugs in this document were found by those tests,
not by reading the code.** Linking proves nothing; running proves something.
+**The game can now play itself.** One command line starts an eight-player skirmish with no menus:
+seven computer opponents, random factions on random corners of the map, and you watching from an
+observer's camera with nothing to command. Nothing holds the simulation back either, so a match that
+would take half an hour runs in a fraction of it. That is how a fault that shows up once in a
+hundred thousand orders gets found - not by playing carefully, but by letting the machine play
+badly, thousands of times, and recording every match it plays.
+
**Every fix was proved non-vacuous.** Put the bug back, watch the exact test fail, restore the fix.
A test that passes both ways is worth nothing, and there are none of those here.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
index dbd51013b..a8b76af42 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h
@@ -346,6 +346,9 @@ public:
Bool m_buildMapCache;
AsciiString m_initialFile; ///< If this is specified, load a specific map/replay from the command-line
AsciiString m_pendingFile; ///< If this is specified, use this map at the next game start
+ Int m_autoSkirmishPlayers; ///< -autoskirmish <n>: start an n player skirmish straight from the command line (0 = off)
+ Int m_autoSkirmishAIState; ///< SlotState the AI slots of an auto-started skirmish get (-aidiff)
+ Bool m_autoSkirmishObserver; ///< -observer: the local slot of an auto-started skirmish watches instead of playing
Int m_maxParticleCount; ///< maximum number of particles that can exist
Int m_maxFieldParticleCount; ///< maximum number of field-type particles that can exist (roughly)
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
index 99f42235a..241eabc6d 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
@@ -32,6 +32,7 @@
#include "Common/Version.h"
#include "GameClient/TerrainVisual.h" // for TERRAIN_LOD_MIN definition
#include "GameClient/GameText.h"
+#include "GameNetwork/GameInfo.h" // for the SlotState -autoskirmish hands the AI slots
#ifdef _INTERNAL
// for occasional debugging...
@@ -99,8 +100,15 @@ static void ConvertShortMapPathToLongMapPath(AsciiString &mapName)
token.removeLastChar();
token.removeLastChar();
- actualpath.concat(token);
- actualpath.concat('\\');
+ // "maps\foo.map" is the short form and grows the directory the map lives in; a path that
+ // already names that directory ("maps\foo\foo.map") is left alone instead of growing a third.
+ AsciiString dir = token;
+ dir.concat('\\');
+ if (!actualpath.endsWithNoCase(dir.str()))
+ {
+ actualpath.concat(token);
+ actualpath.concat('\\');
+ }
actualpath.concat(token);
actualpath.concat(".map");
@@ -385,10 +393,13 @@ Int parseNoShadows(char *args[], int)
Int parseMapName(char *args[], int num)
{
- if (TheWritableGlobalData && num == 2)
+ // num is what is *left* on the command line, so the original "== 2" quietly ignored the map
+ // whenever another option followed it, and returning 1 left the file name to be re-parsed.
+ if (TheWritableGlobalData && num > 1)
{
TheWritableGlobalData->m_mapName.set( args[ 1 ] );
ConvertShortMapPathToLongMapPath(TheWritableGlobalData->m_mapName);
+ return 2;
}
return 1;
}
@@ -929,6 +940,44 @@ Int parseSeed(char *args[], int num)
return 2;
}
+Int parseAutoSkirmish(char *args[], int num)
+{
+ if (TheWritableGlobalData && num > 1)
+ {
+ Int players = atoi(args[1]);
+ if (players < 2)
+ players = 2;
+ if (players > MAX_SLOTS)
+ players = MAX_SLOTS;
+ TheWritableGlobalData->m_autoSkirmishPlayers = players;
+ }
+ return 2;
+}
+
+Int parseAIDifficulty(char *args[], int num)
+{
+ if (TheWritableGlobalData && num > 1)
+ {
+ AsciiString difficulty = args[1];
+ if (difficulty.compareNoCase("easy") == 0)
+ TheWritableGlobalData->m_autoSkirmishAIState = SLOT_EASY_AI;
+ else if (difficulty.compareNoCase("medium") == 0 || difficulty.compareNoCase("med") == 0)
+ TheWritableGlobalData->m_autoSkirmishAIState = SLOT_MED_AI;
+ else
+ TheWritableGlobalData->m_autoSkirmishAIState = SLOT_BRUTAL_AI;
+ }
+ return 2;
+}
+
+Int parseObserver(char *args[], int num)
+{
+ if (TheWritableGlobalData)
+ {
+ TheWritableGlobalData->m_autoSkirmishObserver = TRUE;
+ }
+ return 1;
+}
+
Int parseIncrAGPBuf(char *args[], int num)
{
if (TheWritableGlobalData)
@@ -1171,7 +1220,6 @@ static CommandLineParam params[] =
#if (defined(_DEBUG) || defined(_INTERNAL))
{ "-noaudio", parseNoAudio },
- { "-map", parseMapName },
{ "-nomusic", parseNoMusic },
{ "-novideo", parseNoVideo },
{ "-noLogOrCrash", parseNoLogOrCrash },
@@ -1240,9 +1288,7 @@ static CommandLineParam params[] =
{ "-noShellAnim", parseNoWindowAnimation },
{ "-winCursors", parseWinCursors },
{ "-constantDebug", parseConstantDebug },
- { "-seed", parseSeed },
{ "-noagpfix", parseIncrAGPBuf },
- { "-noFPSLimit", parseNoFPSLimit },
{ "-dumpAssetUsage", parseDumpAssetUsage },
{ "-jumpToFrame", parseJumpToFrame },
{ "-updateImages", parseUpdateImages },
@@ -1251,6 +1297,16 @@ static CommandLineParam params[] =
#endif
+ /* Outside the _DEBUG/_INTERNAL block on purpose: an unattended Release run is driven entirely
+ from the command line. -autoskirmish needs a map to play, -seed makes the run repeatable,
+ and without -noFPSLimit the simulation is held to the frame rate a human watches at. */
+ { "-map", parseMapName },
+ { "-seed", parseSeed },
+ { "-noFPSLimit", parseNoFPSLimit },
+ { "-autoskirmish", parseAutoSkirmish },
+ { "-aidiff", parseAIDifficulty },
+ { "-observer", parseObserver },
+
//-allAdvice feature
//{ "-allAdvice", parseAllAdvice },
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
index 3966fdc3c..a46550bfd 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GameEngine.cpp
@@ -101,6 +101,7 @@
#include "GameClient/Drawable.h"
#include "GameClient/GUICallbacks.h"
+#include "GameNetwork/GameInfo.h"
#include "GameNetwork/NetworkInterface.h"
#include "GameNetwork/WOLBrowser/WebBrowser.h"
#include "GameNetwork/LANAPI.h"
@@ -254,6 +255,104 @@ void GameEngine::setFramesPerSecondLimit( Int fps )
m_maxFPS = fps;
}
+/** -----------------------------------------------------------------------------------------------
+ * -autoskirmish <n>: build a skirmish slot list from the command line and launch it without going
+ * through the menus. This is reallyDoStart() in SkirmishGameOptionsMenu.cpp minus the GUI: the
+ * slots only have to say who is occupied and who is an AI, because GameLogic::startNewGame resolves
+ * a -1 faction, colour and start position itself (populateRandomSideAndColor,
+ * populateRandomStartPosition). Meant for unattended runs - eight AI players fighting at whatever
+ * frame rate the machine gives, with the local slot watching.
+ */
+static void startAutoSkirmish( void )
+{
+ AsciiString mapName = TheGlobalData->m_mapName;
+ if (mapName.isEmpty())
+ {
+ DEBUG_LOG(("-autoskirmish: no -map was given\n"));
+ return;
+ }
+
+ const MapMetaData *md = TheMapCache->findMap( mapName );
+ if (md == NULL)
+ {
+ DEBUG_LOG(("-autoskirmish: '%s' is not in the map cache\n", mapName.str()));
+ return;
+ }
+ if (!md->m_isMultiplayer)
+ {
+ DEBUG_LOG(("-autoskirmish: '%s' is not a multiplayer map\n", mapName.str()));
+ return;
+ }
+
+ Int numPlayers = TheGlobalData->m_autoSkirmishPlayers;
+ if (numPlayers > md->m_numPlayers)
+ {
+ DEBUG_LOG(("-autoskirmish: '%s' holds %d players, not %d\n", mapName.str(), md->m_numPlayers, numPlayers));
+ numPlayers = md->m_numPlayers;
+ }
+
+ if (TheSkirmishGameInfo == NULL)
+ {
+ TheSkirmishGameInfo = NEW SkirmishGameInfo;
+ }
+ TheSkirmishGameInfo->init();
+ TheSkirmishGameInfo->clearSlotList();
+ TheSkirmishGameInfo->reset();
+ TheSkirmishGameInfo->enterGame();
+
+ UnicodeString localName;
+ localName.translate( AsciiString( "Player" ) );
+ for( Int i = 0; i < numPlayers; i++ )
+ {
+ GameSlot slot;
+ if (i == 0)
+ {
+ slot.setState( SLOT_PLAYER, localName );
+ slot.setName( localName );
+ /* An observer has no units and no base, so the eight seats a map has all go to the AI:
+ the local slot only carries the camera. */
+ slot.setPlayerTemplate( TheGlobalData->m_autoSkirmishObserver ? PLAYERTEMPLATE_OBSERVER
+ : PLAYERTEMPLATE_RANDOM );
+ }
+ else
+ {
+ slot.setState( (SlotState)TheGlobalData->m_autoSkirmishAIState );
+ slot.setPlayerTemplate( PLAYERTEMPLATE_RANDOM );
+ }
+ slot.setColor( -1 ); // -1 is "random" to populateRandomSideAndColor
+ slot.setStartPos( -1 ); // and to populateRandomStartPosition
+ slot.setTeamNumber( -1 ); // -1 is "no team", so everybody fights everybody
+ TheSkirmishGameInfo->setSlot( i, slot );
+ }
+ TheSkirmishGameInfo->setLocalIP( TheSkirmishGameInfo->getSlot(0)->getIP() );
+ TheSkirmishGameInfo->setMap( mapName );
+
+ /* -seed makes the whole run repeatable: the seed drives the factions, the colours, the start
+ positions and every logic random draw after them, so the same command line replays the same
+ match. */
+ const Int seed = (TheGlobalData->m_fixedSeed >= 0) ? TheGlobalData->m_fixedSeed : GetTickCount();
+ TheSkirmishGameInfo->setSeed( seed );
+ TheSkirmishGameInfo->startGame( 0 );
+
+ TheWritableGlobalData->m_shellMapOn = FALSE;
+ TheWritableGlobalData->m_playIntro = FALSE;
+
+ /* The menu passes the game speed slider's position here; -noFPSLimit has already put 30000 in
+ m_framesPerSecondLimit, which is what makes this run as fast as the machine can. */
+ const Int maxFPS = TheGlobalData->m_framesPerSecondLimit;
+
+ InitRandom( seed );
+ GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_NEW_GAME );
+ msg->appendIntegerArgument( GAME_SKIRMISH );
+ msg->appendIntegerArgument( DIFFICULTY_NORMAL );
+ msg->appendIntegerArgument( 0 );
+ msg->appendIntegerArgument( maxFPS );
+
+ DEBUG_LOG(("-autoskirmish: %d players on '%s', seed %d, up to %d fps, local slot %s\n",
+ numPlayers, mapName.str(), seed, maxFPS,
+ TheGlobalData->m_autoSkirmishObserver ? "observing" : "playing"));
+}
+
/** -----------------------------------------------------------------------------------------------
* Initialize the game engine by initializing the GameLogic and GameClient.
*/
@@ -662,6 +761,10 @@ void GameEngine::init( int argc, char *argv[] )
TheRecorder->playbackFile(fname);
}
}
+ else if (TheGlobalData->m_autoSkirmishPlayers > 0)
+ {
+ startAutoSkirmish();
+ }
//
if (TheMapCache && TheGlobalData->m_shellMapOn)
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
index 6633e5767..aa8235d0b 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp
@@ -55,6 +55,7 @@
#include "GameClient/TerrainVisual.h"
#include "GameNetwork/FirewallHelper.h"
+#include "GameNetwork/GameInfo.h" // for the SlotState an auto-started skirmish gives its AI slots
// PUBLIC DATA ////////////////////////////////////////////////////////////////////////////////////
GlobalData* TheWritableGlobalData = NULL; ///< The global data singleton
@@ -667,6 +668,9 @@ GlobalData::GlobalData()
m_constantDebugUpdate = FALSE;
m_showTeamDot = FALSE;
m_fixedSeed = -1; // disabled
+ m_autoSkirmishPlayers = 0; // no skirmish from the command line
+ m_autoSkirmishAIState = SLOT_BRUTAL_AI;
+ m_autoSkirmishObserver = FALSE;
m_horizontalScrollSpeedFactor = 1.0;
m_verticalScrollSpeedFactor = 1.0;
diff --git a/GeneralsMD/Code/Main/WinMain.cpp b/GeneralsMD/Code/Main/WinMain.cpp
index 3ac401eed..0db321505 100644
--- a/GeneralsMD/Code/Main/WinMain.cpp
+++ b/GeneralsMD/Code/Main/WinMain.cpp
@@ -939,12 +939,12 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
** Convert WinMain arguments to simple main argc and argv
*/
int argc = 1;
- char * argv[20];
+ char * argv[32];
argv[0] = NULL;
char *token;
token = nextParam(lpCmdLine, "\" ");
- while (argc < 20 && token != NULL) {
+ while (argc < 32 && token != NULL) {
argv[argc++] = strtrim(token);
//added a preparse step for this flag because it affects window creation style
if (stricmp(token,"-win")==0)