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): skirmish ai garrisons its firing buildings

Olcay Seygan9b251929fc81
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 094bc9566..91985e17c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -194,6 +194,7 @@ found and fixed — EA's own, not port damage.**
- Its helicopters fight. The computer's plan parks every Comanche and Helix on guard, and there they stayed for the whole match. Now when an attack leaves, every helicopter left on guard goes with it and flies over the army at its pace: 97 of them over eight four-player matches, where none had ever left before. A Helix buys its own upgrades, and infantry waiting for the attack climbs into any helicopter it can shoot out of.
- And its helicopters carry people. On Hard, infantry waiting for the attack now boards any helicopter with a seat, not only one it can shoot out of, and a helicopter carrying riders who cannot shoot from it puts them down just short of the fight, on the first ground none of your guns it knows about can reach, from where they go on at you on foot. The boarding never worked before this either: a helicopter the plan had put on guard stayed in the air, so the riders walked up to it and turned away. The same Black Lotus was sent to one Helix three times in two minutes. A helicopter now lands for its riders. The capturer flies too: when the building it is after is a long walk away and a helicopter is waiting at home, it gets in, and the helicopter puts it down beside the building. China's attacks are mostly tanks, so its Helixes still leave with one rider more often than five.
- Its infantry rides in its vehicles. The computer's plan tells a group with a Humvee to climb in and, in the same breath, to stand guard, and the guard order won before anyone took a step: over two Hard four-player matches not one soldier boarded anything, Humvee, Battle Bus or helicopter. Now soldiers standing at home climb into their own group's Humvee or Battle Bus on every difficulty, and the group goes wherever its plan sends it with them shooting from inside. Soldiers waiting for the next attack fill any Humvee, Battle Bus or helicopter at home they can shoot out of, an Overlord or Emperor carrying its battle bunker included, and that vehicle leaves with the attack. A soldier who could not shoot from a vehicle never rides in one, except in a helicopter on Hard, which puts him down short of the fight as before. China's attacks on Hard carry no infantry at all, so its Helixes flew off with the waves empty, 14 times over those two matches, though both had bought the bunker by the middle of the match. Medium and Hard now train soldiers for any helicopter seat they could shoot from that stands empty, an attack ready to leave waits up to twenty seconds for them to climb in, and an empty bunkered Helix whose soldiers are still on their way stays behind for the next attack rather than fly off unarmed. The Air Force General on Medium and Hard also buys up to two Combat Chinooks, where it only ever bought the supply Chinook before. They stay off the supply runs and fill with Rangers. On Hard they fly with the attacks, all eight shooting from inside; Medium mostly keeps them at home, loaded. Replays recorded before this change play out differently.
+- Its buildings have men in them. The computer built Bunkers and Fire Bases, put up its Palace as GLA, and left every one of them empty for the whole match. Now the soldiers guarding its base and the ones standing idle at home climb into its own buildings they can shoot out of, and on Medium and Hard it trains more for the empty seats once it has money to spare. While its base is being hit, the men at home also take any empty civilian building in the base. The soldiers gathered for an attack are never taken off it for this. Over eight Hard matches on Twilight Flame, 173 soldiers went in, each of them once, 88 of them into a Palace and the rest into Fire Bases and Bunkers, with the matches' economy and number of attacks where they were. A first version bought five Red Guards for China's first Bunker a minute into the match, and the computer's own plan, finding the bank empty, sold the whole base in two of eight matches; it now waits for spare money. Replays recorded before this change play out differently.
- Its Chinooks fly the army. A USA attack walked the whole road, at the pace of its slowest tank. Medium and Hard now buy plain Chinooks for nothing but lift, more of them the bigger the attack and the longer the road: they load tanks and soldiers at home, put them down on the attack's road short of anything the computer has seen that can shoot, and go home for the next load while the attack is under way. The drop is one a straight flight from the base reaches over quiet ground, after the first version flew seven loaded Chinooks in one match straight at a drop beyond an enemy base and lost every one. A Chinook hit hard on the way puts its load down where it is. Neither these nor the Air Force General's Combat Chinooks go on the supply runs unless nothing else is left to gather and nothing else can be bought, and the Combat Chinook is no longer ever bought as a gatherer.
- Its Helixes have jobs. Every Helix the computer owned bought its upgrades in button order, so each one carried a battle bunker whatever it was up against, with napalm on top. A Helix in one of its attack or guard groups now looks at what it knows of your army first and takes the gattling cannon when most of your strength is on foot, the bunker when most of it is in vehicles, and the soldiers trained to sit in that bunker are Tank Hunters rather than Red Guards. Against GLA over four Hard matches that came out 15 bunkers to 6 cannons, each decided on the split at that moment. China on Medium and Hard also buys two Helixes as healers, a third once its army passes thirty units, each under a Propaganda Tower. They hang behind the attack over whichever of its units is most hurt, carry nobody, and pull away when they are hit or when anti-air they know of comes near; over twelve Hard matches they spent 93% of their time out of the base further from the nearest enemy gun than it could shoot. They used to bunch up over the same tank, which got nothing out of it: a unit inside two towers heals no faster than inside one. Each healer now keeps its spot at least a tower's reach, 150, from the others', going to the most hurt unit nobody else's tower already covers, or stepping sideways behind the attack until it is clear of theirs. Two more Helixes take the napalm bomb, or the Nuke General's nuclear one, and raid your base, a factory first and otherwise whatever pays for it or powers it. A raid comes in from the side your known anti-air does not cover, looks for a new way in when a gun turns up ahead, and turns for home when it is hit below half health. Over the same twelve matches 126 raids went out, 23 dropped their bomb, five buildings fell after a drop, and 31 raiders were shot down on a raid. A raider also stops bombing once its run is over. A Helix told to bomb a spot drops another bomb under itself every ten seconds until it gets a different order, and the computer's own orders never counted as one, so a raider that had bombed your base went on dropping napalm all the way home, over your soldiers on the way, and then on its own base for the rest of the match. Over eight Hard matches against America, 104 of 121 bombs fell nowhere near the building they were meant for. On the same matches now, each of the 14 bombs dropped lands on its building, and a raid that turns back before its drop no longer flies out again to finish it once it is home. The Infantry General's Helix has no tower, so it only raids. Replays recorded before this change play out differently.
- It comes at you from two sides. An attack big enough to be two sends a third of its strength down the second-quietest road, so you cannot meet the whole thing in one place.
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index e3f3f99a7..3b1572ebb 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -357,6 +357,7 @@ protected:
AsciiString secondApproachLabel(const Coord3D *from, const AsciiString &taken, Int pathSuffix); ///< the quietest other road, or empty
Bool loadGunships(void); ///< infantry boards the transports it can shoot out of: the wave's anything at home, a team's its own; TRUE while a firing gunship is still filling
Int buyGunshipRiders(Int freeSeats); ///< Medium and up train the men for the firing seats nobody fills; how many are in training
+ void garrisonBuildings(void); ///< the home guard's infantry into our own buildings it shoots out of and the empty ones at home
void buyGunshipChinook(void); ///< Medium and up buy a supply-center transport the riders shoot out of, as a gunship
void doShuttles(void); ///< the transport Chinooks load at home, fly the wave's ground units to its road and come back
void buyTransportChinook(void); ///< one more transport Chinook while the last wave needs more lift than there is
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 477ddad0c..e21f84e63 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -7734,6 +7734,7 @@ void AIPlayer::doWaves( void )
++teams;
}
const Bool gunshipFilling = loadGunships();
+ garrisonBuildings(); // after the gunships: the wave's riders first, the home guard's leftovers into buildings
if( first < 0 )
return;
// no wave walks out of a base that is being hit
@@ -7919,10 +7920,22 @@ static void collectBoardingGoals( Object *obj, void *userData )
* has no gun and was sent to a Helix three times. Not a Stinger Site's soldiers either,
* who stand on the default team, cannot be selected and go back to their site: the first version
* sent the same eight at one Battle Bus 316 times in a match. */
+/** A member of something else's band: an Angry Mob's rioters, who go back to their mob wherever they
+ * are sent. The first garrison pass sent the same four rioters at one Palace 333 times in a match,
+ * and the mob itself, which never goes in, 21 times. */
+static Bool isSlaved( const Object *obj )
+{
+ for( BehaviorModule **m = obj->getBehaviorModules(); *m; ++m )
+ if( (*m)->getSlavedUpdateInterface() )
+ return TRUE;
+ return FALSE;
+}
+
Bool AIPlayer::isGunshipRider( const Object *obj ) const
{
return obj->isKindOf( KINDOF_INFANTRY ) && !obj->isKindOf( KINDOF_HERO ) && obj->isSelectable() && !obj->isContained() &&
- !obj->isEffectivelyDead() && obj->getAI() && leavesToFinish( obj ) && !isMassUnit( obj->getID() );
+ !obj->isEffectivelyDead() && obj->getAI() && leavesToFinish( obj ) && !isMassUnit( obj->getID() ) && !isSlaved( obj ) &&
+ !obj->isKindOf( KINDOF_MOB_NEXUS );
}
/** An infantryman who rides out with the army, where loadGunships puts him. */
@@ -8100,6 +8113,135 @@ Bool AIPlayer::loadGunships( void )
return walking || (freeSeats > 0 && training > 0);
}
+/** One of our own buildings whose men shoot out of it - a Bunker, a Fire Base, a Palace - finished, not
+ * knocked out by an EMP or a hacker, and with a seat free. Not a tunnel or a heal bay. */
+static void findFiringBuilding( Object *obj, void *userData )
+{
+ ContainModuleInterface *contain = obj->getContain();
+ if( obj->isKindOf( KINDOF_STRUCTURE ) && contain && !contain->isTunnelContain() && !contain->isHealContain() &&
+ (Int)contain->getContainCount() < contain->getContainMax() && contain->isPassengerAllowedToFire() &&
+ !obj->isEffectivelyDead() && !obj->testStatus( OBJECT_STATUS_UNDER_CONSTRUCTION ) && !obj->testStatus( OBJECT_STATUS_SOLD ) )
+ ((std::vector<Object *> *)userData)->push_back( obj );
+}
+
+//----------------------------------------------------------------------------------------------------------
+/** The computer built Bunkers and Fire Bases and never put a man in one, and walked past every empty
+ * farmhouse in its own base while the enemy's rebels moved in. The home guard fills them: the
+ * default team's infantry standing at home and the base defence teams'. Our own firing buildings take
+ * both, and Medium and up train men for their empty seats out of the bank (buyGunshipRiders). An empty
+ * neutral building at home takes the base defence teams' men, and the default team's only while the
+ * base is hit: the default team's idle men at home are the next wave's (addHomeStrays), and a farmhouse
+ * in a quiet base is not worth a wave's infantry. A wave's own men and an attack team's are never taken.
+ * Runs after loadGunships on the same pass, so a man it has sent to a gunship is no longer idle here. */
+//----------------------------------------------------------------------------------------------------------
+void AIPlayer::garrisonBuildings( void )
+{
+ if( !isSkirmishAI() || !m_baseCenterSet )
+ return;
+ std::vector<Object *> seats;
+ m_player->iterateObjects( findFiringBuilding, &seats );
+ const size_t own = seats.size();
+ const Real reach = 2.0f * m_baseRadius;
+ {
+ PartitionFilterAlive filterAlive;
+ PartitionFilterOnMap filterOnMap;
+ PartitionFilterGarrisonableByPlayer filterGarrison( m_player, TRUE, CMD_FROM_AI );
+ PartitionFilter *filters[] = { &filterAlive, &filterOnMap, &filterGarrison, 0 };
+ MemoryPoolObjectHolder hold;
+ SimpleObjectIterator *nearby = ThePartitionManager->iterateObjectsInRange( &m_baseCenter, reach, FROM_CENTER_2D, filters );
+ hold.hold( nearby );
+ for( Object *b = nearby->first(); b; b = nearby->next() )
+ if( b->getControllingPlayer() != m_player && b->getContain()->isPassengerAllowedToFire() )
+ seats.push_back( b );
+ }
+ if( seats.empty() )
+ return;
+
+ // seats nobody is walking to, as loadGunships counts them
+ std::vector<ObjectID> goals;
+ m_player->iterateObjects( collectBoardingGoals, &goals );
+ std::vector<Int> free;
+ for( std::vector<Object *>::const_iterator s = seats.begin(); s != seats.end(); ++s )
+ free.push_back( (*s)->getContain()->getContainMax() - (Int)(*s)->getContain()->getContainCount() -
+ (Int)std::count( goals.begin(), goals.end(), (*s)->getID() ) );
+
+ const Bool underAttack = isBaseUnderAttack();
+ const Team *defaultTeam = m_player->getDefaultTeam();
+ std::vector<GunshipRider> riders;
+ for( Player::PlayerTeamList::const_iterator t = m_player->getPlayerTeams()->begin(); t != m_player->getPlayerTeams()->end(); ++t )
+ {
+ const TeamTemplateInfo *info = (*t)->getTemplateInfo();
+ const Bool guard = info && (info->m_isBaseDefense || info->m_isPerimeterDefense);
+ for( DLINK_ITERATOR<Team> iter = (*t)->iterate_TeamInstanceList(); !iter.done(); iter.advance() )
+ {
+ Team *team = iter.cur();
+ const Bool isDefault = team == defaultTeam;
+ if( !isDefault && (!guard || !team->isActive()) )
+ continue;
+ for( DLINK_ITERATOR<Object> m = team->iterate_TeamMemberList(); !m.done(); m.advance() )
+ {
+ Object *rider = m.cur();
+ if( !isGunshipRider( rider ) || isFallingBack( rider->getID() ) || !isAtHome( rider->getPosition() ) )
+ continue;
+ const StateID state = rider->getAI()->getCurrentStateID();
+ if( state != AI_IDLE && state != AI_GUARD && state != AI_GUARD_RETALIATE )
+ continue;
+ GunshipRider entry = { rider, isDefault ? "default" : "guard" };
+ riders.push_back( entry );
+ }
+ }
+ }
+
+ for( std::vector<GunshipRider>::const_iterator r = riders.begin(); r != riders.end(); ++r )
+ {
+ Object *rider = r->obj;
+ const Bool stray = strcmp( r->from, "default" ) == 0;
+ Int nearest = -1;
+ Real nearestSqr = 0.0f;
+ for( size_t g = 0; g < seats.size(); ++g )
+ {
+ const Bool mine = g < own;
+ if( free[ g ] <= 0 || (!mine && stray && !underAttack) )
+ continue;
+ ContainModuleInterface *contain = seats[ g ]->getContain();
+ if( !contain->isValidContainerFor( rider, TRUE ) || !contain->isPassengerAllowedToFire( rider->getID() ) )
+ continue;
+ const Real distSqr = sqr( seats[ g ]->getPosition()->x - rider->getPosition()->x ) + sqr( seats[ g ]->getPosition()->y - rider->getPosition()->y );
+ if( distSqr > sqr( reach ) )
+ continue;
+ // our own building before a neutral one, then the nearest; a tie goes to the first in list order
+ const Bool nearestMine = nearest >= 0 && (size_t)nearest < own;
+ if( nearest < 0 || (mine && !nearestMine) || (mine == nearestMine && distSqr < nearestSqr) )
+ {
+ nearest = (Int)g;
+ nearestSqr = distSqr;
+ }
+ }
+ if( nearest < 0 )
+ continue;
+ Object *building = seats[ nearest ];
+ --free[ nearest ];
+ DEBUG_LOG(("AI GARRISON frame %d player %d puts '%s' %d into '%s' %d, %d of %d seats taken, %s, %s%s\n", TheGameLogic->getFrame(),
+ m_player->getPlayerIndex(), rider->getTemplate()->getName().str(), rider->getID(), building->getTemplate()->getName().str(), building->getID(),
+ building->getContain()->getContainCount(), building->getContain()->getContainMax(), (size_t)nearest < own ? "own" : "neutral",
+ r->from, underAttack ? ", base under attack" : ""));
+ rider->getAI()->aiEnter( building, CMD_FROM_AI );
+ }
+
+ // men for our own empty firing seats at home: a trained rider comes out at home, and one for a Bunker at
+ // an expansion would stand at home with nothing to walk to. Only out of money above the cash hoard: the
+ // first version bought five Red Guards for China's first Bunker a minute in, and the skirmish script,
+ // finding the bank and the trucks short a few seconds later, sold the whole base in two of eight matches.
+ // The script's own Bunker team fills the Bunker anyway, in time
+ if( (Int)m_player->getMoney()->countMoney() <= getSkillProfile()->m_cashHoardThreshold )
+ return;
+ Int freeSeats = 0;
+ for( size_t g = 0; g < own; ++g )
+ if( free[ g ] > 0 && isAtHome( seats[ g ]->getPosition() ) )
+ freeSeats += free[ g ];
+ buyGunshipRiders( freeSeats );
+}
+
/** The infantry a barracks trains to ride in a gunship: the first fighter on its buttons, so a Red
* Guard, a Ranger, a Rebel or a Mini-Gunner. Not a worker, a hacker or a hero. Against an enemy
* mostly on wheels and tracks, armour names the vehicle most of his strength is in, and the rider is whichever of them