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): six honest difficulty rungs and a role axis

Olcay Seyganb256a6d43d48
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 96a82d04d..33f559349 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,14 @@ found and fixed — EA's own, not port damage.**
- It scouts. There was no such thing as scouting in the computer opponent - it never needed to look, because it could already see everything. One cheap unit now tours the enemy start positions and keeps going round for the rest of the match, replaced out of spare change when it dies. Every difficulty scouts; an opponent that never looks reads as broken, not as easy.
- Nor does it read your base off the map. Where your base is, what it is worth, which supply dock to expand to and where to aim a superweapon all came off a walk of your object list, in the shroud, from the first second of the match. The computer now only counts what it can see, plus the buildings it has already found - buildings do not walk away. Before it has scouted you, all it knows is where you started, which is on the map preview anyway.
+## Six difficulty levels, and none of them cheat
+
+- Easy, Steady, Medium, Hard, Brutal, Merciless. Medium to Brutal used to be a cliff; the steps in between are where a player actually needs them.
+- Every level plays by your rules. No level gets extra money, cheaper units, faster building, longer vision or tougher units - in either direction. What changes is what the computer is allowed to decide: how often it looks at the map, how long it takes to react, whether it counters what you field, whether it masses before attacking, whether it pulls damaged units out.
+- Each step up switches on exactly one new thing, so you can say in a sentence what you get for climbing. Easy is slow and brave. Steady saves its damaged units. Medium expands on its own. Hard counters your build. Brutal masses and commits. Merciless is the ceiling - everything on, no hesitation.
+- Making Easy easier means giving it worse decisions, never less money. That is the whole promise.
+- The computer also picks a personality each match and keeps it: one plays for the attack, the other for the base. Same resources, spent differently.
+
## Attack-move actually attacks
- Ctrl on the click advances the group at the slowest unit's pace.
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/WellKnownKeys.h b/GeneralsMD/Code/GameEngine/Include/Common/WellKnownKeys.h
index 5bd6e810d..b668f887a 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/WellKnownKeys.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/WellKnownKeys.h
@@ -879,6 +879,15 @@ DEFINE_KEY(multiplayerStartIndex)
*/
DEFINE_KEY(skirmishDifficulty)
+/**
+ Which: Player Properties
+ Type: Int
+ Usage: (optional) which rung of the six-step AI ladder this player plays at (AISkillLevel).
+ skirmishDifficulty stays a GameDifficulty and answers "which scripts run"; this answers
+ "how well does it play". See AI-ROADMAP.md D7.
+*/
+DEFINE_KEY(skirmishAISkill)
+
/**
Which: Player Properties
Type: Bool
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
index 36a538ab8..5ff3c9690 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h
@@ -133,6 +133,69 @@ public:
+//-------------------------------------------------------------------------------------------------
+/** Six rungs of how well the AI plays. Deliberately NOT GameDifficulty, which stays at three
+ * values: that enum answers "which scripts run" and is baked into every shipped map and .scb,
+ * into the campaign's stat bonus tables and into network messages. This one answers "how well
+ * does it play", and every rung of it plays by the player's rules - no income, build-rate, vision
+ * or stat handicap at any level, in either direction. See AI-ROADMAP.md sections D6, D7 and D10.
+ */
+//-------------------------------------------------------------------------------------------------
+enum AISkillLevel
+{
+ AISKILL_EASY = 0, ///< slow and brave: looks around, but reacts far too late
+ AISKILL_STEADY, ///< + unit-level retreat, adaptive harvesters
+ AISKILL_MEDIUM, ///< + closest-target focus, expands on its own
+ AISKILL_HARD, ///< + counters what you field, groups fire together
+ AISKILL_BRUTAL, ///< + masses before attacking, team retreat, defended expansions
+ AISKILL_MERCILESS, ///< + influence-map attack lanes, no reaction delay
+
+ AISKILL_COUNT
+};
+
+//-------------------------------------------------------------------------------------------------
+/** What the AI is trying to do, as opposed to how well it does it. A preference, never a bonus:
+ * the roles have to be about equally strong or the axis silently becomes a difficulty setting. */
+//-------------------------------------------------------------------------------------------------
+enum AIRole
+{
+ AIROLE_AGGRESSIVE = 0,
+ AIROLE_DEFENSIVE,
+
+ AIROLE_COUNT
+};
+
+//-------------------------------------------------------------------------------------------------
+/** One rung of the ladder. Every new AI behaviour adds a field here rather than a
+ * switch(difficulty) somewhere, so the whole ladder can be read, tuned and measured in one place.
+ *
+ * Nothing in here may exceed the AI's honest baseline - the top rung IS the baseline and the
+ * lower rungs are handicaps on its *decisions*. If a field ever appears that multiplies income,
+ * build rate, vision or unit stats, the design has drifted. */
+//-------------------------------------------------------------------------------------------------
+struct AIDifficultyProfile
+{
+ // Perception. Never fog: only how diligently it looks and how fast it acts on what it saw.
+ Real m_scoutIntervalSeconds; ///< how often the scout is re-tasked; every rung scouts
+ Int m_maxScouts;
+ Real m_reactionDelaySeconds; ///< lag between "something happened" and answering it
+ Real m_decisionIntervalSeconds; ///< how often it re-evaluates - the AI's APM
+
+ // Decision quality
+ Real m_counterCompositionWeight; ///< 0 = ignore what the enemy fields (EA's behaviour)
+ Bool m_massBeforeAttacking;
+ Real m_retreatTtkRatio; ///< allied time-to-kill / enemy TTK below which it pulls back
+ Bool m_retreatIndividualUnits;
+ Bool m_useInfluenceMapForAttackLane;
+ Bool m_focusFire;
+
+ // Economy: decisions only, never multipliers
+ Bool m_adaptiveHarvesters;
+ Bool m_selfTriggeredExpansion;
+ Bool m_defendExpansions;
+ Int m_cashHoardThreshold; ///< above this, spend faster; 0 = never hurry
+};
+
class TAiData : public Snapshot
{
public:
@@ -166,6 +229,8 @@ public:
UnsignedInt m_guardEnemyReturnScanRate; // rate to scan for enemies while guarding but returning
Real m_wallHeight; // Height of special wall units can walk on top of.
+
+ AIDifficultyProfile m_skill[AISKILL_COUNT]; ///< the ladder; defaults in TAiData(), overridable from INI
Real m_alertRangeModifier; // When a unit is alert, its range will be modified by this value
Real m_aggressiveRangeModifier; // When a unit is aggressive, its range will be modified by this value
@@ -293,6 +358,11 @@ public:
static Real s_lastPlayerUpdateMS; ///< see getLastPlayerUpdateMS
const TAiData *getAiData() {return m_aiData;}
+ /// The rung's knobs. Bounds-checked, so a bad index reads as the baseline rather than as memory.
+ const AIDifficultyProfile *getDifficultyProfile( AISkillLevel level ) const;
+
+ static void parseSkillLevel(INI *ini, void *instance, void *store, const void *userData);
+
// Note: Does not work for things that do not have AI. (This is in AI.h, after all)
static Real getAdjustedVisionRangeForObject(const Object *object, Int factorsToConsider);
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index 108298cf7..a69ebd89a 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -33,6 +33,7 @@
#include "Common/GameMemory.h"
#include "Common/Snapshot.h"
+#include "GameLogic/AI.h" // AISkillLevel, AIRole and the difficulty profile the ladder reads
enum { INVALID_SKILLSET_SELECTION = -1 };
@@ -199,6 +200,18 @@ public:
/// Difficulty level for this player.
GameDifficulty getAIDifficulty(void) const;
void setAIDifficulty(GameDifficulty difficulty) {m_difficulty = difficulty;}
+
+ /** Which rung of the six-step ladder this AI plays at, and what it is trying to do. Two
+ * independent axes: the rung says how well, the role says what. See AI-ROADMAP.md D6/D8. */
+ AISkillLevel getAISkillLevel(void) const {return m_skillLevel;}
+ void setAISkillLevel(AISkillLevel level) {m_skillLevel = level;}
+ AIRole getAIRole(void) const {return m_role;}
+
+ /// This rung's knobs, for any behaviour that has a difficulty-dependent decision to make.
+ const AIDifficultyProfile *getSkillProfile(void) const;
+
+ /// What a seat that predates the ladder (an old save, a replay, a script) plays at.
+ static AISkillLevel skillLevelForDifficulty(GameDifficulty difficulty);
void buildBySupplies(Int minimumCash, const AsciiString &thingName ); ///< Builds a building by supplies.
void buildSpecificBuildingNearestTeam( const AsciiString &thingName, const Team *team );
void buildUpgrade(const AsciiString &upgrade ); ///< Builds an upgrade.
@@ -304,6 +317,9 @@ protected:
Player *m_player; ///< the Player we represent
+ AISkillLevel m_skillLevel; ///< rung of the ladder: how well this AI plays
+ AIRole m_role; ///< what it is trying to do; rolled once, kept for the match
+
ObjectID m_scoutID; ///< the unit currently touring the map for us
Int m_scoutTimer; ///< frames until the next scouting check
Int m_scoutTarget; ///< which player's start position the scout is walking to
diff --git a/GeneralsMD/Code/GameEngine/Include/GameNetwork/GameInfo.h b/GeneralsMD/Code/GameEngine/Include/GameNetwork/GameInfo.h
index 4fead6fe4..d0cbbdca6 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameNetwork/GameInfo.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameNetwork/GameInfo.h
@@ -47,7 +47,15 @@ enum SlotState
// fork: an opponent seat with nothing behind it - no AI is ever created for it and you take
// it over in game with Shift-Ctrl-T. Appended on purpose: the values above are written into
// save games and replays.
- SLOT_TAKEOVER
+ SLOT_TAKEOVER,
+
+ // fork: the three rungs that fill in the ladder between Easy, Medium and Brutal. Appended for
+ // the same reason - never inserted. Each maps onto one of the three GameDifficulty values for
+ // script gating (GameLogic::startNewGame); what they really select is the AISkillLevel, which is
+ // what decides how well the AI plays. See AI-ROADMAP.md D6/D7.
+ SLOT_STEADY_AI,
+ SLOT_HARD_AI,
+ SLOT_MERCILESS_AI
};
enum
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
index 89730da3c..5a41da844 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/CommandLine.cpp
@@ -1000,13 +1000,25 @@ Int parseAIDifficulty(char *args[], int num)
{
if (TheWritableGlobalData && num > 1)
{
+ //
+ // The six rungs of the ladder. Anything unrecognised is still the top rung, so a batch
+ // script written before the ladder existed keeps meaning what it meant.
+ //
AsciiString difficulty = args[1];
if (difficulty.compareNoCase("easy") == 0)
TheWritableGlobalData->m_autoSkirmishAIState = SLOT_EASY_AI;
+ else if (difficulty.compareNoCase("steady") == 0)
+ TheWritableGlobalData->m_autoSkirmishAIState = SLOT_STEADY_AI;
else if (difficulty.compareNoCase("medium") == 0 || difficulty.compareNoCase("med") == 0)
TheWritableGlobalData->m_autoSkirmishAIState = SLOT_MED_AI;
- else
+ else if (difficulty.compareNoCase("hard") == 0)
+ TheWritableGlobalData->m_autoSkirmishAIState = SLOT_HARD_AI;
+ else if (difficulty.compareNoCase("brutal") == 0)
TheWritableGlobalData->m_autoSkirmishAIState = SLOT_BRUTAL_AI;
+ else if (difficulty.compareNoCase("merciless") == 0)
+ TheWritableGlobalData->m_autoSkirmishAIState = SLOT_MERCILESS_AI;
+ else
+ TheWritableGlobalData->m_autoSkirmishAIState = SLOT_MERCILESS_AI;
}
return 2;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
index 3345b1363..7c2ee52fc 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/RTS/Player.cpp
@@ -916,9 +916,20 @@ void Player::initFromDict(const Dict* d)
{
difficulty = (GameDifficulty) diffInt;
}
+ Bool skillExists = false;
+ Int skillInt = d->getInt(TheKey_skirmishAISkill, &skillExists);
if (m_ai)
{
m_ai->setAIDifficulty(difficulty);
+ //
+ // The rung, if the seat named one. A save or replay from before the ladder existed does
+ // not, so fall back on the three-value difficulty: that is what those matches were
+ // actually played at.
+ //
+ if (skillExists)
+ m_ai->setAISkillLevel((AISkillLevel)skillInt);
+ else
+ m_ai->setAISkillLevel(AIPlayer::skillLevelForDifficulty(difficulty));
}
if (!found)
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/GameInfoWindow.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/GameInfoWindow.cpp
index 9923c809b..8795f5479 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/GameInfoWindow.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/GameInfoWindow.cpp
@@ -171,11 +171,27 @@ void RefreshGameInfoWindow(GameInfo *gameInfo, UnicodeString gameName)
addedRow = GadgetListBoxAddEntryText(listBoxPlayers,TheGameText->fetch("GUI:MediumAI"),playerColor,-1, 1);
break;
}
- case SLOT_BRUTAL_AI:
+ case SLOT_HARD_AI:
{
addedRow = GadgetListBoxAddEntryText(listBoxPlayers,TheGameText->fetch("GUI:HardAI"),playerColor,-1, 1);
break;
}
+ // the rungs the ladder added have no shipped string; plain English rather than MISSING:
+ case SLOT_STEADY_AI:
+ {
+ addedRow = GadgetListBoxAddEntryText(listBoxPlayers,UnicodeString(L"Steady AI"),playerColor,-1, 1);
+ break;
+ }
+ case SLOT_BRUTAL_AI:
+ {
+ addedRow = GadgetListBoxAddEntryText(listBoxPlayers,UnicodeString(L"Brutal AI"),playerColor,-1, 1);
+ break;
+ }
+ case SLOT_MERCILESS_AI:
+ {
+ addedRow = GadgetListBoxAddEntryText(listBoxPlayers,UnicodeString(L"Merciless AI"),playerColor,-1, 1);
+ break;
+ }
default:
break;
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ScoreScreen.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ScoreScreen.cpp
index 5f2436873..208901195 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ScoreScreen.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/ScoreScreen.cpp
@@ -1217,11 +1217,14 @@ static void updateSkirmishBattleHonors(SkirmishBattleHonors& stats)
const GameSlot *slot = TheGameInfo->getConstSlot(i);
if (slot->isAI() && !isSlotLocalAlly(TheGameInfo, slot))
{
- if (TheGameInfo->getConstSlot(i)->getState() == SLOT_EASY_AI)
+ // the three new rungs count towards the medal of the tier they sit in, so the challenge
+ // medals keep meaning what they meant before the ladder was widened
+ SlotState st = TheGameInfo->getConstSlot(i)->getState();
+ if (st == SLOT_EASY_AI || st == SLOT_STEADY_AI)
++numEasy;
- if (TheGameInfo->getConstSlot(i)->getState() == SLOT_MED_AI)
+ if (st == SLOT_MED_AI || st == SLOT_HARD_AI)
++numMedium;
- if (TheGameInfo->getConstSlot(i)->getState() == SLOT_BRUTAL_AI)
+ if (st == SLOT_BRUTAL_AI || st == SLOT_MERCILESS_AI)
++numBrutal;
}
else if (slot->isAI())
diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp
index d59c5074e..3742c3322 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/GUI/GUICallbacks/Menus/SkirmishGameOptionsMenu.cpp
@@ -1147,14 +1147,27 @@ void InitSkirmishGameGadgets( void )
// A seat with no AI behind it, for testing: Shift-Ctrl-T in game hands it to you.
GadgetComboBoxAddEntry(comboBoxPlayer[i],TheGameText->fetch("GUI:HumanSlot"),white);
GadgetComboBoxSetItemData(comboBoxPlayer[i], 2, (void *)SLOT_TAKEOVER);
+ //
+ // The six-rung ladder (AI-ROADMAP.md D6). Every rung plays by the player's rules - the
+ // steps are decisions the AI is or is not allowed to make, never money, build speed,
+ // vision or unit stats. Three of the six have no shipped string, so they read as plain
+ // English rather than as MISSING:; the three that do keep it, and EA's "HardAI" string
+ // now labels the rung that is actually called Hard.
+ //
GadgetComboBoxAddEntry(comboBoxPlayer[i],TheGameText->fetch("GUI:EasyAI"),white);
GadgetComboBoxSetItemData(comboBoxPlayer[i], 3, (void *)SLOT_EASY_AI);
+ GadgetComboBoxAddEntry(comboBoxPlayer[i],UnicodeString(L"Steady AI"),white);
+ GadgetComboBoxSetItemData(comboBoxPlayer[i], 4, (void *)SLOT_STEADY_AI);
GadgetComboBoxAddEntry(comboBoxPlayer[i],TheGameText->fetch("GUI:MediumAI"),white);
- GadgetComboBoxSetItemData(comboBoxPlayer[i], 4, (void *)SLOT_MED_AI);
+ GadgetComboBoxSetItemData(comboBoxPlayer[i], 5, (void *)SLOT_MED_AI);
GadgetComboBoxAddEntry(comboBoxPlayer[i],TheGameText->fetch("GUI:HardAI"),white);
- GadgetComboBoxSetItemData(comboBoxPlayer[i], 5, (void *)SLOT_BRUTAL_AI);
- // six entries now, one more than the layout's list box was sized for
- GadgetComboBoxSetMaxDisplay(comboBoxPlayer[i], 6);
+ GadgetComboBoxSetItemData(comboBoxPlayer[i], 6, (void *)SLOT_HARD_AI);
+ GadgetComboBoxAddEntry(comboBoxPlayer[i],UnicodeString(L"Brutal AI"),white);
+ GadgetComboBoxSetItemData(comboBoxPlayer[i], 7, (void *)SLOT_BRUTAL_AI);
+ GadgetComboBoxAddEntry(comboBoxPlayer[i],UnicodeString(L"Merciless AI"),white);
+ GadgetComboBoxSetItemData(comboBoxPlayer[i], 8, (void *)SLOT_MERCILESS_AI);
+ // nine entries now; the layout's list box was sized for five
+ GadgetComboBoxSetMaxDisplay(comboBoxPlayer[i], 9);
GadgetComboBoxSetSelectedPos(comboBoxPlayer[i],0);
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
index 1dc59960e..99fa5b884 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp
@@ -182,6 +182,8 @@ static const FieldParse TheAIFieldParseTable[] =
{ "SideInfo", AI::parseSideInfo, NULL, NULL },
+ { "SkillLevel", AI::parseSkillLevel, NULL, NULL },
+
{ "SkirmishBuildList", AI::parseSkirmishBuildList, NULL, NULL },
@@ -1002,6 +1004,78 @@ Real AI::getAdjustedVisionRangeForObject(const Object *object, Int factorsToCons
}
//-------------------------------------------------------------------------------------------------
+//-------------------------------------------------------------------------------------------------
+/** The ladder, from AI-ROADMAP.md D6. Each rung switches on exactly one new capability and takes
+ * one step off the reaction delay, so every rung's identity is a sentence and a rung that plays
+ * wrong has exactly one new thing in it.
+ *
+ * Read the columns down, not across: nothing here is money, build speed, vision or unit stats.
+ *
+ * scoutS maxSc react decis cntr mass ttk indiv infl focus harv expand guard hoard */
+static const AIDifficultyProfile s_defaultSkillLadder[ AISKILL_COUNT ] =
+{
+ /* Easy */ { 90.0f, 1, 15.0f, 10.0f, 0.00f, FALSE, 0.00f, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 0 },
+ /* Steady */ { 75.0f, 1, 10.0f, 8.0f, 0.00f, FALSE, 0.50f, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, 0 },
+ /* Medium */ { 60.0f, 1, 6.0f, 5.0f, 0.25f, FALSE, 0.60f, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, 10000 },
+ /* Hard */ { 45.0f, 2, 3.0f, 4.0f, 0.60f, FALSE, 0.70f, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, 8000 },
+ /* Brutal */ { 30.0f, 2, 1.0f, 2.0f, 0.85f, TRUE, 0.80f, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, 5000 },
+ /* Merciless */ { 25.0f, 2, 0.0f, 1.5f, 1.00f, TRUE, 0.85f, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 4000 }
+};
+
+//-------------------------------------------------------------------------------------------------
+const AIDifficultyProfile *AI::getDifficultyProfile( AISkillLevel level ) const
+{
+ if( m_aiData == NULL )
+ return &s_defaultSkillLadder[ AISKILL_MERCILESS ];
+ if( level < 0 || level >= AISKILL_COUNT )
+ level = AISKILL_MERCILESS; // the baseline; a bad index must not read as a handicap
+ return &m_aiData->m_skill[ level ];
+}
+
+//-------------------------------------------------------------------------------------------------
+/** SkillLevel = Brutal ... End, in AI.ini: tuning the ladder without a recompile, which is what
+ * the headless batch runner wants. Anything the block does not mention keeps its default. */
+//-------------------------------------------------------------------------------------------------
+void AI::parseSkillLevel(INI *ini, void *instance, void* /*store*/, const void* /*userData*/)
+{
+ static const FieldParse myFieldParse[] =
+ {
+ { "ScoutIntervalSeconds", INI::parseReal, NULL, offsetof( AIDifficultyProfile, m_scoutIntervalSeconds ) },
+ { "MaxScouts", INI::parseInt, NULL, offsetof( AIDifficultyProfile, m_maxScouts ) },
+ { "ReactionDelaySeconds", INI::parseReal, NULL, offsetof( AIDifficultyProfile, m_reactionDelaySeconds ) },
+ { "DecisionIntervalSeconds", INI::parseReal, NULL, offsetof( AIDifficultyProfile, m_decisionIntervalSeconds ) },
+ { "CounterCompositionWeight", INI::parseReal, NULL, offsetof( AIDifficultyProfile, m_counterCompositionWeight ) },
+ { "MassBeforeAttacking", INI::parseBool, NULL, offsetof( AIDifficultyProfile, m_massBeforeAttacking ) },
+ { "RetreatTtkRatio", INI::parseReal, NULL, offsetof( AIDifficultyProfile, m_retreatTtkRatio ) },
+ { "RetreatIndividualUnits", INI::parseBool, NULL, offsetof( AIDifficultyProfile, m_retreatIndividualUnits ) },
+ { "UseInfluenceMapForAttackLane", INI::parseBool, NULL, offsetof( AIDifficultyProfile, m_useInfluenceMapForAttackLane ) },
+ { "FocusFire", INI::parseBool, NULL, offsetof( AIDifficultyProfile, m_focusFire ) },
+ { "AdaptiveHarvesters", INI::parseBool, NULL, offsetof( AIDifficultyProfile, m_adaptiveHarvesters ) },
+ { "SelfTriggeredExpansion", INI::parseBool, NULL, offsetof( AIDifficultyProfile, m_selfTriggeredExpansion ) },
+ { "DefendExpansions", INI::parseBool, NULL, offsetof( AIDifficultyProfile, m_defendExpansions ) },
+ { "CashHoardThreshold", INI::parseInt, NULL, offsetof( AIDifficultyProfile, m_cashHoardThreshold ) },
+ { NULL, NULL, NULL, 0 }
+ };
+
+ static const char *names[ AISKILL_COUNT ] =
+ { "Easy", "Steady", "Medium", "Hard", "Brutal", "Merciless" };
+
+ AsciiString name( ini->getNextToken() );
+ Int which = -1;
+ for( Int i = 0; i < AISKILL_COUNT; ++i )
+ if( name.compareNoCase( names[ i ] ) == 0 )
+ which = i;
+
+ if( which < 0 )
+ {
+ DEBUG_LOG(("AI.ini SkillLevel '%s' is not one of Easy/Steady/Medium/Hard/Brutal/Merciless\n",
+ name.str()));
+ which = AISKILL_MERCILESS; // parse it somewhere rather than throw the block on the floor
+ }
+
+ ini->initFromINI( &((TAiData*)instance)->m_skill[ which ], myFieldParse );
+}
+
TAiData::TAiData() :
m_next(NULL),
m_sideInfo(NULL),
@@ -1053,6 +1127,9 @@ m_maxRetaliateDistance(210.0f),
m_retaliateFriendsRadius(120.0f)
//
{
+ // the ladder starts at its shipped defaults; an AI.ini SkillLevel block overrides one rung
+ for (Int i = 0; i < AISKILL_COUNT; ++i)
+ m_skill[i] = s_defaultSkillLadder[i];
}
//-------------------------------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 814ac0bae..37ceea7a1 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -133,7 +133,9 @@ m_teamSeconds(10),
m_curWarehouseID(INVALID_ID),
m_scoutID(INVALID_ID),
m_scoutTimer(1),
-m_scoutTarget(0)
+m_scoutTarget(0),
+m_skillLevel(AISKILL_MERCILESS),
+m_role(AIROLE_AGGRESSIVE)
{
m_frameLastBuildingBuilt = TheGameLogic->getFrame();
p->setCanBuildUnits(false); // turn off ai production by default.
@@ -146,6 +148,16 @@ m_scoutTarget(0)
m_baseCenter.zero();
m_baseCenterSet = false;
m_difficulty = TheScriptEngine->getGlobalDifficulty();
+ m_skillLevel = skillLevelForDifficulty( m_difficulty );
+
+ //
+ // Role is rolled once and kept for the whole match. Sins is explicit that its AI does not flip
+ // personality mid-game, and that consistency is what makes an opponent readable and therefore
+ // counterable - an AI that turtles for ten minutes and then rushes is not deep, it is noise.
+ // GameLogicRandomValue, so a replay rolls the same one.
+ //
+ m_role = (GameLogicRandomValue( 0, AIROLE_COUNT - 1 ) == 0) ? AIROLE_AGGRESSIVE : AIROLE_DEFENSIVE;
+
m_teamSeconds = TheAI->getAiData()->m_teamSeconds;
//
@@ -3493,7 +3505,20 @@ void AIPlayer::doScouting( void )
Coord3D goal;
if( nextScoutTarget( &goal ) )
+ {
ai->aiMoveToPosition( &goal, CMD_FROM_AI );
+
+ //
+ // ... and then leave it alone for this rung's scouting interval before the next leg. This
+ // is the ladder's honest perception knob: every rung scouts, they differ in how diligently.
+ // Ninety seconds between legs on Easy is a scout that wanders; twenty-five on Merciless is
+ // one that keeps the map current.
+ //
+ const AIDifficultyProfile *profile = getSkillProfile();
+ Int interval = REAL_TO_INT_CEIL( profile->m_scoutIntervalSeconds * LOGICFRAMES_PER_SECOND );
+ if( interval > m_scoutTimer )
+ m_scoutTimer = interval;
+ }
}
//----------------------------------------------------------------------------------------------------------
@@ -3554,6 +3579,26 @@ enum GameDifficulty AIPlayer::getAIDifficulty(void) const
return m_difficulty;
}
+//-------------------------------------------------------------------------------------------------
+/** The rung a seat that predates the ladder plays at - an old save, a replay, a scripted campaign
+ * player. The three that existed map to the three the lobby used to offer. */
+//-------------------------------------------------------------------------------------------------
+AISkillLevel AIPlayer::skillLevelForDifficulty( GameDifficulty difficulty )
+{
+ switch( difficulty )
+ {
+ case DIFFICULTY_EASY: return AISKILL_EASY;
+ case DIFFICULTY_NORMAL: return AISKILL_MEDIUM;
+ default: return AISKILL_BRUTAL;
+ }
+}
+
+//-------------------------------------------------------------------------------------------------
+const AIDifficultyProfile *AIPlayer::getSkillProfile( void ) const
+{
+ return TheAI->getDifficultyProfile( m_skillLevel );
+}
+
//----------------------------------------------------------------------------------------------------------
/**
@@ -3654,7 +3699,7 @@ void AIPlayer::xfer( Xfer *xfer )
{
// version
- XferVersion currentVersion = 2; // 2: the scout
+ XferVersion currentVersion = 3; // 2: the scout 3: skill rung and role
XferVersion version = currentVersion;
xfer->xferVersion( &version, currentVersion );
@@ -3816,6 +3861,13 @@ void AIPlayer::xfer( Xfer *xfer )
xfer->xferInt( &m_scoutTarget );
}
+ // the ladder rung and the role, which are rolled once and must come back the same way
+ if( version >= 3 )
+ {
+ xfer->xferUser( &m_skillLevel, sizeof( AISkillLevel ) );
+ xfer->xferUser( &m_role, sizeof( AIRole ) );
+ }
+
xfer->xferUser( m_structuresToRepair, sizeof( ObjectID ) * MAX_STRUCTURES_TO_REPAIR );
xfer->xferObjectID( &m_repairDozer );
xfer->xferInt( &m_structuresInQueue );
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index f822fe8a1..85ec8b2f2 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -1423,10 +1423,31 @@ void GameLogic::startNewGame( Bool loadingSaveGame )
if (isSkirmishOrSkirmishReplay)
{
d.setBool(TheKey_playerIsSkirmish, true);
+ //
+ // Two answers from one seat. skirmishDifficulty stays a GameDifficulty, because that
+ // is what gates the scripts baked into every shipped map and .scb; skirmishAISkill is
+ // the rung of the six-step ladder, which is what decides how well the AI plays.
+ // See AI-ROADMAP.md D7.
+ //
switch (slot->getState()) {
- case SLOT_EASY_AI : d.setInt(TheKey_skirmishDifficulty, DIFFICULTY_EASY); break;
- case SLOT_MED_AI : d.setInt(TheKey_skirmishDifficulty, DIFFICULTY_NORMAL); break;
- case SLOT_BRUTAL_AI : d.setInt(TheKey_skirmishDifficulty, DIFFICULTY_HARD); break;
+ case SLOT_EASY_AI:
+ d.setInt(TheKey_skirmishDifficulty, DIFFICULTY_EASY);
+ d.setInt(TheKey_skirmishAISkill, AISKILL_EASY); break;
+ case SLOT_STEADY_AI:
+ d.setInt(TheKey_skirmishDifficulty, DIFFICULTY_EASY);
+ d.setInt(TheKey_skirmishAISkill, AISKILL_STEADY); break;
+ case SLOT_MED_AI:
+ d.setInt(TheKey_skirmishDifficulty, DIFFICULTY_NORMAL);
+ d.setInt(TheKey_skirmishAISkill, AISKILL_MEDIUM); break;
+ case SLOT_HARD_AI:
+ d.setInt(TheKey_skirmishDifficulty, DIFFICULTY_NORMAL);
+ d.setInt(TheKey_skirmishAISkill, AISKILL_HARD); break;
+ case SLOT_BRUTAL_AI:
+ d.setInt(TheKey_skirmishDifficulty, DIFFICULTY_HARD);
+ d.setInt(TheKey_skirmishAISkill, AISKILL_BRUTAL); break;
+ case SLOT_MERCILESS_AI:
+ d.setInt(TheKey_skirmishDifficulty, DIFFICULTY_HARD);
+ d.setInt(TheKey_skirmishAISkill, AISKILL_MERCILESS); break;
default: break; // no setting.
}
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameInfo.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameInfo.cpp
index abc501c38..adbdc94ed 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameInfo.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameInfo.cpp
@@ -271,7 +271,9 @@ Bool GameSlot::isAI( void ) const
// SLOT_TAKEOVER counts here on purpose: to the lobby it is an ordinary opponent seat that the
// host owns and that no network peer is expected to check in from. It differs in one place
// only - the player it creates is human (GameLogic::startNewGame), so no AI is ever attached.
- return m_state == SLOT_EASY_AI || m_state == SLOT_MED_AI || m_state == SLOT_BRUTAL_AI || m_state == SLOT_TAKEOVER;
+ return m_state == SLOT_EASY_AI || m_state == SLOT_MED_AI || m_state == SLOT_BRUTAL_AI ||
+ m_state == SLOT_STEADY_AI || m_state == SLOT_HARD_AI || m_state == SLOT_MERCILESS_AI ||
+ m_state == SLOT_TAKEOVER;
}
Bool GameSlot::isPlayer( AsciiString userName ) const
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/LobbyUtils.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/LobbyUtils.cpp
index 8f0353c46..72ce6bd64 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/LobbyUtils.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/LobbyUtils.cpp
@@ -334,10 +334,22 @@ static void gameTooltip(GameWindow *window,
tooltip.concat(L'\n');
tooltip.concat(TheGameText->fetch("GUI:MediumAI"));
break;
- case SLOT_BRUTAL_AI:
+ case SLOT_HARD_AI:
tooltip.concat(L'\n');
tooltip.concat(TheGameText->fetch("GUI:HardAI"));
break;
+ case SLOT_STEADY_AI:
+ tooltip.concat(L'\n');
+ tooltip.concat(UnicodeString(L"Steady AI"));
+ break;
+ case SLOT_BRUTAL_AI:
+ tooltip.concat(L'\n');
+ tooltip.concat(UnicodeString(L"Brutal AI"));
+ break;
+ case SLOT_MERCILESS_AI:
+ tooltip.concat(L'\n');
+ tooltip.concat(UnicodeString(L"Merciless AI"));
+ break;
}
}
}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/PeerDefs.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/PeerDefs.cpp
index c4a94f92f..1d02ce1b5 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/PeerDefs.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/GameSpy/PeerDefs.cpp
@@ -249,6 +249,15 @@ void GameSpyInfo::setGameOptions( void )
case SLOT_MED_AI:
playerName = "CM";
break;
+ case SLOT_STEADY_AI:
+ playerName = "CS";
+ break;
+ case SLOT_HARD_AI:
+ playerName = "CD";
+ break;
+ case SLOT_MERCILESS_AI:
+ playerName = "CX";
+ break;
case SLOT_BRUTAL_AI:
playerName = "CH";
break;
diff --git a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp
index fb5101556..e77593b77 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp
@@ -981,10 +981,21 @@ AsciiString LANAPI::createSlotString( void )
}
else if (slot->isAI())
{
+ //
+ // The missing "else" on the second test meant an easy AI was set to "CE," and then
+ // immediately overwritten with "CB," - every AI seat but a medium one advertised itself
+ // to the lobby as brutal.
+ //
if (slot->getState() == SLOT_EASY_AI)
str = "CE,";
- if (slot->getState() == SLOT_MED_AI)
+ else if (slot->getState() == SLOT_STEADY_AI)
+ str = "CS,";
+ else if (slot->getState() == SLOT_MED_AI)
str = "CM,";
+ else if (slot->getState() == SLOT_HARD_AI)
+ str = "CH,";
+ else if (slot->getState() == SLOT_MERCILESS_AI)
+ str = "CX,";
else
str = "CB,";
}
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 380337908..1d81ac82c 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -66,6 +66,7 @@
#include "GameClient/ParticleSys.h"
#include "GameClient/FXList.h"
#include "GameClient/ControlBar.h"
+#include "GameLogic/AI.h"
#include "GameLogic/Module/ProductionUpdate.h"
#include "GameLogic/Module/SupplyTruckAIUpdate.h"
#include "GameClient/InGameUI.h"
@@ -5783,3 +5784,53 @@ TEST(ceil_and_floor_are_exact_on_whole_numbers)
CHECK_EQ( 0, REAL_TO_INT_FLOOR( 0.9f ) );
CHECK_EQ( -1, REAL_TO_INT_FLOOR( -0.1f ) );
}
+
+
+/** The difficulty ladder is a table, not thirty scattered switch statements, so the one thing worth
+ asserting about it is that it is actually a ladder: every rung looks harder than the one below,
+ and no rung is a cheat. A fat-fingered row in the table is otherwise invisible until somebody
+ plays a hundred matches.
+
+ TheAI does not exist in this binary, so this reads the shipped defaults the way TAiData's
+ constructor does. */
+TEST(the_difficulty_ladder_climbs_in_every_direction_it_should)
+{
+ TAiData data;
+
+ for( Int i = 1; i < AISKILL_COUNT; ++i )
+ {
+ const AIDifficultyProfile &lower = data.m_skill[ i - 1 ];
+ const AIDifficultyProfile &upper = data.m_skill[ i ];
+
+ // perception: looks more often, acts sooner, thinks more often - never sees more
+ CHECK( upper.m_scoutIntervalSeconds <= lower.m_scoutIntervalSeconds );
+ CHECK( upper.m_reactionDelaySeconds <= lower.m_reactionDelaySeconds );
+ CHECK( upper.m_decisionIntervalSeconds <= lower.m_decisionIntervalSeconds );
+ CHECK( upper.m_maxScouts >= lower.m_maxScouts );
+
+ // decisions: counters harder, holds on to its units longer
+ CHECK( upper.m_counterCompositionWeight >= lower.m_counterCompositionWeight );
+ CHECK( upper.m_retreatTtkRatio >= lower.m_retreatTtkRatio );
+
+ // capabilities only ever switch on as you climb, never off again
+ CHECK( upper.m_massBeforeAttacking >= lower.m_massBeforeAttacking );
+ CHECK( upper.m_retreatIndividualUnits >= lower.m_retreatIndividualUnits );
+ CHECK( upper.m_useInfluenceMapForAttackLane >= lower.m_useInfluenceMapForAttackLane );
+ CHECK( upper.m_focusFire >= lower.m_focusFire );
+ CHECK( upper.m_adaptiveHarvesters >= lower.m_adaptiveHarvesters );
+ CHECK( upper.m_selfTriggeredExpansion >= lower.m_selfTriggeredExpansion );
+ CHECK( upper.m_defendExpansions >= lower.m_defendExpansions );
+ }
+
+ // the ends of the ladder are what they say they are
+ CHECK_EQ( 0.0f, data.m_skill[ AISKILL_EASY ].m_counterCompositionWeight );
+ CHECK_EQ( 1.0f, data.m_skill[ AISKILL_MERCILESS ].m_counterCompositionWeight );
+ CHECK_EQ( 0.0f, data.m_skill[ AISKILL_MERCILESS ].m_reactionDelaySeconds );
+
+ // every rung scouts. An AI that never looks reads as broken, not as easy.
+ for( Int i = 0; i < AISKILL_COUNT; ++i )
+ {
+ CHECK( data.m_skill[ i ].m_scoutIntervalSeconds > 0.0f );
+ CHECK( data.m_skill[ i ].m_maxScouts >= 1 );
+ }
+}