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): hard ai sends attack teams idle at home
Olcay Seygane3d57ed09110
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 21ac6d635..b19397b5b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -83,6 +83,7 @@ found and fixed — EA's own, not port damage.**- Hard builds past its plan. When every war factory, barracks or airfield it owns is busy it puts down another, beside its newest expansion so the army comes out nearer the fighting. Buildings that pay out on a timer, supply drop zones and black markets, go up one after another as soon as the bank is past a small cushion, whatever else it is buying; they used to wait until production had nothing to ask for, which on a busy map was never, and over twelve four-player matches the count went from 5 to 95. China trains a hacker from every barracks that has room in its queue, not one barracks at a time: 120 hackers across those matches became 375. A hacker left standing still is put to work. It stops at six, an internet center's four plus a couple, because nothing counted them before: a Hard Tank General trained 67 over twenty thousand frames, about two thirds of everything it spent, and every one of them went through the barracks queue its army was waiting in. A player watching it from the other side of the map said China's tank general builds nothing but infantry, and he was right. Seven now, and the teams at the top of its build list are tanks. The limit grows with the match: one hacker for every four fighting units, never more than its Internet Centers have seats for plus four working outside, and never fewer than four. A computer that has not built an army does not buy an economy instead, and one with a full Internet Center and ninety units behind it keeps eight.- Hard picks its road into your base. Of the three approaches it takes the one with the fewest guns it has seen along it, and keeps to the one its plan named until it has scouted something. Aiming at your money instead was tried first and walked it straight past your army.- Hard sends its attack groups out together. A group too small to be a wave on its own waits on the near side of its base until about seven tanks' worth has gathered, or a minute and a half has passed, and then everything waiting leaves on one road at the pace of its slowest unit. A lone artillery piece or a single bomb truck used to drive across the map by itself, and nearly a third of Hard's units died with barely anyone of their own nearby.+- Hard stops piling its army up at home. Its plan only sends an attack group while the signal for the next wave is up, and only a group standing in the base at that moment. A group finished a minute later guarded the base until the signal came round again, and one that fell back from a lost fight guarded it for good: one GLA stood 38 of its Angry Mob by its own buildings with 70,000 in the bank. China's Dragon Tank group never left at all, because the attack order its plan names for it is missing from EA's file. Now any attack group standing idle at home joins the next wave. Rich computers also stopped building extra copies of the groups that only ever guard, four sets of tunnel guards where the plan asked for one. Over eight Hard matches on Twilight Flame, the units standing idle at home late in the match went from 43 to 17, the waves sent went from 134 to 319, and kills went up by a tenth. Replays recorded before this change play out differently.- It puts more harvesters on a supply centre with more piles around it, instead of the same three numbers everywhere.- It keeps its lights on, and keeps them out of reach. Its plan carries a fixed number of power plants and nothing ever adds one, so a raid that took two of them left the base underpowered - radar dark, base defences off, superweapon clock stopped - until a rebuild timer came round and put the new plant back on the spot the raid already knew the way to. It now buys a plant when the margin gets thin rather than when the meter reads zero, and puts it on the far side of the base from whoever it is fighting.- Spare cash also buys a gun on the side the trouble comes from. Production and income go up around the middle of the base, the power goes behind it, and base defences go out in front, so money that keeps arriving thickens the base in the order a base wants to be thick in. Front means towards the nearest enemy, which is not the same as the one it has decided to attack - that one can be across the map, and a wall built facing it has its back to the neighbour. The walls grow with the army. One gun every minute and a half, never more than ten, used to be the rule; now the computer may stand one gun for every four fighting units it has, so a big army comes home to a fortress and a beaten one rebuilds its tanks before its bunkers. Letting it buy guns on every pass instead was tried first: 241 guns over twelve four-player matches, and they ate the army, down to 60 kills a match from 73. Tied to the army, 71 guns, and the army is the biggest of any version tried, 78 kills a match.diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.hindex 9dc5620d7..2385681dd 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h@@ -42,6 +42,10 @@ enum { INVALID_SKILLSET_SELECTION = -1 };class BuildListInfo;// this header used to compile only behind whoever happened to include Team.h firstclass TeamPrototype;+class TeamTemplateInfo;++/// the skirmish scripts send this team at the enemy at some point, rather than keeping it home on guard+Bool aiTeamAttacks(const TeamTemplateInfo *info);/*** When a team is selected for training, a list of these@@ -339,6 +343,7 @@ protected:Real knownFirepowerAlongPath(Waypoint *way); ///< what this AI has seen that can shoot, along an approachAsciiString secondApproachLabel(const Coord3D *from, const AsciiString &taken, Int pathSuffix); ///< the quietest other road, or emptyvoid loadGunships(void); ///< the parked wave's infantry boards the gunships at home+ void sendIdleAttackTeams(void); ///< attack teams standing at home join the next wave instead of waiting for the script's signalReal knownFirepowerNear(const Coord3D *pos); ///< what this AI has seen that can shoot, near a pointBool forwardHoldPoint(const AsciiString &approach, Int pathSuffix, const Coord3D *enemyPos, Coord3D *hold); ///< where a wave gathers on its roadvoid sendWave(AIGroup *wave, const AsciiString &approach, Int pathSuffix, Int teams, Real power, UnsignedInt heldFrames);diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cppindex 058517447..594258891 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp@@ -5751,6 +5751,60 @@ static Real teamPower( Team *team )return power;}+/** A script, or one it hands the team on to, that sends the team at the enemy: an attack order, a wait+ * on the flag the skirmish scripts raise to launch a wave, or a count towards the next one. Each+ * player's copy of the scripts carries its index on every name, "_LAUNCH_ATTACK3". China's 5th wave+ * Dragon Tanks only count: the attack script their team names is not in the file, so they guarded+ * the base for the whole match. */+static Bool scriptAttacks( const Script *script, Int depth )+{+ if( script == NULL || depth > 4 )+ return FALSE;+ for( OrCondition *orCond = script->getOrCondition(); orCond; orCond = orCond->getNextOrCondition() )+ for( Condition *cond = orCond->getFirstAndCondition(); cond; cond = cond->getNext() )+ if( cond->getConditionType() == Condition::FLAG && cond->getParameter( 0 )->getString().startsWith( "_LAUNCH_ATTACK" ) )+ return TRUE;+ for( Int branch = 0; branch < 2; ++branch )+ {+ for( ScriptAction *action = branch ? script->getFalseAction() : script->getAction(); action; action = action->getNext() )+ {+ switch( action->getActionType() )+ {+ case ScriptAction::SKIRMISH_FOLLOW_APPROACH_PATH:+ case ScriptAction::SKIRMISH_MOVE_TO_APPROACH_PATH:+ return TRUE;+ }+ for( Int i = 0; i < action->getNumParameters(); ++i )+ {+ const Parameter *param = action->getParameter( i );+ if( param->getParameterType() == Parameter::COUNTER && param->getString().startsWith( "_COUNTER_FOR_ATTACK" ) )+ return TRUE;+ // only a script the team is set to run; following every script a script names reached the+ // subroutine lists and called the garrison teams and the base expanders attackers+ const ScriptAction::ScriptActionType type = action->getActionType();+ if( (type == ScriptAction::TEAM_EXECUTE_SEQUENTIAL_SCRIPT || type == ScriptAction::TEAM_EXECUTE_SEQUENTIAL_SCRIPT_LOOPING) &&+ param->getParameterType() == Parameter::SCRIPT &&+ scriptAttacks( TheScriptEngine->findScriptByName( param->getString() ), depth + 1 ) )+ return TRUE;+ }+ }+ }+ return FALSE;+}++/** A team the skirmish scripts send at the enemy at some point, as against one they keep at home on+ * guard: the tunnel guards, the fire base crew, the palace garrison. */+Bool aiTeamAttacks( const TeamTemplateInfo *info )+{+ if( scriptAttacks( TheScriptEngine->findScriptByName( info->m_scriptOnCreate ), 0 ) ||+ scriptAttacks( TheScriptEngine->findScriptByName( info->m_scriptOnIdle ), 0 ) )+ return TRUE;+ for( Int i = 0; i < MAX_GENERIC_SCRIPTS; ++i )+ if( scriptAttacks( TheScriptEngine->findScriptByName( info->m_teamGenericScripts[ i ] ), 0 ) )+ return TRUE;+ return FALSE;+}+//----------------------------------------------------------------------------------------------------------/** C2, second attempt. Teams went out as they came off the line - a lone artillery piece, a single bomb* truck, one helicopter - and three brutal matches on Winter Wolf counted 29% of the AI's units dying@@ -5842,6 +5896,8 @@ void AIPlayer::doWaves( void )if( (TheGameLogic->getFrame() + computeUpdatePhase( m_player->getPlayerIndex(), WAVE_CHECK_RATE )) % WAVE_CHECK_RATE != 0 )return;+ sendIdleAttackTeams();+Real power = 0.0f;Int first = -1;Int teams = 0;@@ -6055,6 +6111,77 @@ void AIPlayer::sendWave( AIGroup *wave, const AsciiString &approach, Int pathSufsendWaveThroughTunnels( wave, ¢er, way );}+//----------------------------------------------------------------------------------------------------------+/** The owner watched the computer pile its army up at home and never use it. The skirmish scripts send+ * an attack team only while their launch flag is up, and only a team standing inside the base when it+ * goes up; a team finished a moment later guards the base until the next launch, and one that came+ * back from a lost fight guards it for good. Eight Brutal matches on Twilight Flame had 43 units idle+ * at home on average after frame 18000, 23 of them in attack teams, one GLA at 38 Angry Mob members with+ * 70,000 banked. So an attack team that stands idle at home is called up here: it parks for the next+ * wave, or goes on its own when it is a wave by itself or the staging point is full. The guards stay,+ * since the scripts never send them. The same eight matches then had 17 idle at home, 9 in attack+ * teams, and sent 319 waves instead of 134. */+//----------------------------------------------------------------------------------------------------------+void AIPlayer::sendIdleAttackTeams( void )+{+ if( !isSkirmishAI() || !getSkillProfile()->m_massBeforeAttacking || !m_baseCenterSet )+ return;+ Player *enemy = getAiEnemy();+ if( enemy == NULL )+ return;+ const Int pathSuffix = enemy->getMpStartIndex() + 1;+ const AsciiString center( "Center" );+ const Real reachSqr = sqr( 2.0f * m_baseRadius );++ for( Player::PlayerTeamList::const_iterator t = m_player->getPlayerTeams()->begin(); t != m_player->getPlayerTeams()->end(); ++t )+ {+ const TeamTemplateInfo *info = (*t)->getTemplateInfo();+ if( info->m_isBaseDefense || info->m_isPerimeterDefense || !aiTeamAttacks( info ) )+ continue;+ for( DLINK_ITERATOR<Team> iter = (*t)->iterate_TeamInstanceList(); !iter.done(); iter.advance() )+ {+ Team *team = iter.cur();+ if( !team->isActive() )+ continue; // still being built; it leaves when it is whole+ Bool parked = FALSE;+ for( Int i = 0; i < MAX_HELD_TEAMS; ++i )+ if( m_heldUsed[ i ] && m_heldTeam[ i ] == team->getID() )+ parked = TRUE;+ if( parked )+ continue;++ // most of the members that can walk are at home with nothing to do. Not all of them: an Angry+ // Mob's members never stop milling round their nexus, and one straggler out on the map kept 22+ // of them at home for a minute+ Int members = 0;+ Int waiting = 0;+ for( DLINK_ITERATOR<Object> m = team->iterate_TeamMemberList(); !m.done(); m.advance() )+ {+ Object *obj = m.cur();+ if( obj->isEffectivelyDead() || obj->getAI() == NULL || obj->isContained() || obj->isKindOf( KINDOF_IMMOBILE ) )+ continue;+ ++members;+ const StateID state = obj->getAI()->getCurrentStateID();+ const Bool idle = state == AI_IDLE || state == AI_GUARD || state == AI_GUARD_RETALIATE;+ if( idle && sqr( obj->getPosition()->x - m_baseCenter.x ) + sqr( obj->getPosition()->y - m_baseCenter.y ) <= reachSqr )+ ++waiting;+ }+ if( waiting == 0 || 2 * waiting < members )+ continue;++ DEBUG_LOG(("AI WAVE frame %d player %d calls up '%s', %d units idle at home\n", TheGameLogic->getFrame(),+ m_player->getPlayerIndex(), team->getName().str(), waiting));+ if( holdTeamForWave( team, center, pathSuffix ) )+ continue;+ AIGroup *group = TheAI->createGroup();+ team->getTeamAsAIGroup( group );+ Coord3D from;+ group->getCenter( &from );+ sendWave( group, chooseApproachLabel( &from, center, pathSuffix ), pathSuffix, 1, teamPower( team ), 0 );+ }+ }+}+//----------------------------------------------------------------------------------------------------------/** A GLA wave whose tunnels come up nearer the end of its approach path than the path itself goes* takes them, the way a player's move order does (TunnelTracker::findTunnelShortcut), and comes updiff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cppindex b006a6ee2..4de8bcc29 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AISkirmishPlayer.cpp@@ -407,10 +407,11 @@ Bool AISkirmishPlayer::isAGoodIdeaToBuildTeam( TeamPrototype *proto )if (hasEnoughMoneyUnitsFor(proto)) {return false;}- // check build limit, which a hoard raises for attack teams; defence teams keep the data's+ // check build limit, which a hoard raises for attack teams; defence teams keep the data's, and so do+ // the guard teams the scripts never send, or a rich AI stood four copies of its tunnel guards at homeconst TeamTemplateInfo *info = proto->getTemplateInfo();Int allowedInstances = info->m_maxInstances;- if (!info->m_isBaseDefense && !info->m_isPerimeterDefense) {+ if (!info->m_isBaseDefense && !info->m_isPerimeterDefense && aiTeamAttacks(info)) {allowedInstances = aiHoardAllowedTeamInstances( allowedInstances, m_player->getMoney()->countMoney(),getSkillProfile()->m_cashHoardThreshold );}