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(ai): fire ready powers, keep spotters back, skip borrowed units

Olcay Seygane50498006786
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/ThingFactory.h b/GeneralsMD/Code/GameEngine/Include/Common/ThingFactory.h
index 772cea330..d2f2e31a5 100644
--- a/GeneralsMD/Code/GameEngine/Include/Common/ThingFactory.h
+++ b/GeneralsMD/Code/GameEngine/Include/Common/ThingFactory.h
@@ -94,6 +94,10 @@ public:
that one in its place, so a pilot, a drone or a paradrop the American data spawns comes out Turkish. */
const ThingTemplate *findSideReskin( const ThingTemplate *tmplate, const AsciiString &side );
+ /** Is side built out of another side's reskins (Turkey out of America's)? Its AI runs that side's
+ scripts, so a unit the scripts name that has no reskin for side is one it can never field. */
+ Bool sideBorrowsTemplates( const AsciiString &side );
+
/** request a new drawable using the given template.
this will throw an exception on failure; it will never return null.
*/
@@ -137,6 +141,8 @@ private:
/// findSideReskin's answers, NULL ones included; reset() empties it with the map's overrides
std::map< std::pair< const ThingTemplate*, NameKeyType >, const ThingTemplate* > m_sideReskins;
+ /// sideBorrowsTemplates' answers; reset() empties it with m_sideReskins
+ std::map< NameKeyType, Bool > m_sidesBorrowing;
};
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
index 205c220c9..d88984df1 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AIPlayer.h
@@ -333,6 +333,9 @@ protected:
* tech building so one of them blowing up leaves the tree standing. */
virtual void doSuperweapons(void);
+ /** Fire every ready special power whose template says AIFiresWhenReady, once its science is bought. */
+ void doReadySpecialPowers(void);
+
Bool enemyDirection(Coord3D *dir); ///< unit vector from this base towards the nearest enemy's best known address
Bool isHeldExpansion(const Object *warehouse); ///< our supply center stands at it, and it is nearer our base than any enemy's
Bool isOurSideOfMap(const Coord3D *pos); ///< no nearer any living enemy's base than ours
@@ -637,6 +640,7 @@ protected:
void restoreMood(Object *obj, TacticalStep *step);
Bool pickTacticalSpot(const Object *obj, const Coord3D *from, const Coord3D *awayFrom, Real distance,
const Coord3D *mustReach, Real reach, Coord3D *spot);
+ void spotterStandOff(Object *obj, TacticalStep *step); ///< a spotter backs out of the nearest armed enemy's reach, keeping it in sight
};
#endif // _AI_PLAYER_H_
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingFactory.cpp b/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingFactory.cpp
index 31749d0ca..f52851dd4 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingFactory.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/Thing/ThingFactory.cpp
@@ -257,6 +257,7 @@ void ThingFactory::reset( void )
m_nextTemplateID = m_firstTemplate ? (UnsignedShort)(m_firstTemplate->getTemplateID() + 1) : 1;
// it may hold the overrides just deleted
m_sideReskins.clear();
+ m_sidesBorrowing.clear();
} // end reset
//-------------------------------------------------------------------------------------------------
@@ -396,6 +397,24 @@ const ThingTemplate *ThingFactory::findSideReskin( const ThingTemplate *tmplate,
return found;
}
+//=============================================================================
+Bool ThingFactory::sideBorrowsTemplates( const AsciiString &side )
+{
+ const NameKeyType key = NAMEKEY( side );
+ std::map< NameKeyType, Bool >::const_iterator it = m_sidesBorrowing.find( key );
+ if (it != m_sidesBorrowing.end())
+ return it->second;
+
+ Bool borrows = FALSE;
+ for (const ThingTemplate *t = m_firstTemplate; t != NULL && !borrows; t = t->friend_getNextTemplate())
+ {
+ const ThingTemplate *source = t->friend_getReskinnedFrom();
+ borrows = t->getDefaultOwningSide() == side && source != NULL && source->getDefaultOwningSide() != side;
+ }
+ m_sidesBorrowing[ key ] = borrows;
+ return borrows;
+}
+
//=============================================================================
Drawable *ThingFactory::newDrawable(const ThingTemplate *tmplate, DrawableStatus statusBits)
{
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
index 5f2215699..67e65ceb8 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp
@@ -2222,6 +2222,18 @@ Object *AIPlayer::findFactory(const ThingTemplate *thing, Bool busyOK)
return NULL;
}
+// ------------------------------------------------------------------------------------------------
+/** A side built out of another side's reskins runs that side's scripts, and a unit they name with
+ * no reskin for this side (America's Stealth Fighter, for Turkey) is left out of the team rather
+ * than keeping the whole team off the build list. */
+// ------------------------------------------------------------------------------------------------
+static Bool sideCanNeverField( const Player *player, const ThingTemplate *thing )
+{
+ const AsciiString &side = player->getSide();
+ return thing->getDefaultOwningSide() != side && TheThingFactory->sideBorrowsTemplates( side ) &&
+ TheThingFactory->findSideReskin( thing, side ) == NULL;
+}
+
// ------------------------------------------------------------------------------------------------
/** Return true if team can be considered for building */
// ------------------------------------------------------------------------------------------------
@@ -2229,13 +2241,20 @@ Bool AIPlayer::isPossibleToBuildTeam( TeamPrototype *proto, Bool requireIdleFact
{
/* Make sure we have at least one idle factory, and factories for all unit types. */
Bool anyIdle = false;
+ Bool anyFielded = false;
+ Bool anyLeftOut = false;
Int cost=0;
notEnoughMoney = false;
for( int i=0; i<proto->getTemplateInfo()->m_numUnitsInfo; i++ )
{
const TCreateUnitsInfo *unitInfo = &proto->getTemplateInfo()->m_unitsInfo[0];
const ThingTemplate *thing = TheThingFactory->findTemplate( unitInfo[i].unitThingName );
+ if (thing && sideCanNeverField(m_player, thing)) {
+ anyLeftOut = true;
+ continue;
+ }
if (thing) {
+ anyFielded = true;
Int thingCost = thing->calcCostToBuild(m_player);
if (NULL == findFactory(thing, true)) {
// Couldn't find a factory.
@@ -2248,6 +2267,8 @@ Bool AIPlayer::isPossibleToBuildTeam( TeamPrototype *proto, Bool requireIdleFact
cost += thingCost * ((unitInfo[i].maxUnits+unitInfo[i].minUnits)/2.0f);
}
}
+ if (anyLeftOut && !anyFielded)
+ return false; // nothing in it this side can field
cost *= TheAI->getAiData()->m_teamResourcesToBuild;
if (m_player->getMoney()->countMoney() < cost) {
notEnoughMoney = true;
@@ -3707,7 +3728,7 @@ void AIPlayer::buildSpecificAITeam( TeamPrototype *teamProto, Bool priorityBuild
for( i=0; i<teamProto->getTemplateInfo()->m_numUnitsInfo; i++ )
{
const ThingTemplate *thing = TheThingFactory->findTemplate( unitInfo[i].unitThingName );
- if (thing)
+ if (thing && !sideCanNeverField(m_player, thing))
{
int count = unitInfo[i].maxUnits-unitInfo[i].minUnits;
if (count>0) {
@@ -3725,7 +3746,7 @@ void AIPlayer::buildSpecificAITeam( TeamPrototype *teamProto, Bool priorityBuild
for( i=0; i<teamProto->getTemplateInfo()->m_numUnitsInfo; i++ )
{
const ThingTemplate *thing = TheThingFactory->findTemplate( unitInfo[i].unitThingName );
- if (thing)
+ if (thing && !sideCanNeverField(m_player, thing))
{
int count = unitInfo[i].minUnits;
WorkOrder *order = newInstance(WorkOrder);
@@ -4509,6 +4530,7 @@ void AIPlayer::update( void )
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.
+ AI_PHASE( AIP_ECONOMY, doReadySpecialPowers() ); // The powers no script knows to fire, as soon as they are ready.
AI_PHASE( AIP_WAVE, doWaves() ); // Send the parked attack teams out together.
AI_PHASE( AIP_TACTICS, doTactics() ); // Fight each unit from where it is strongest.
AI_PHASE( AIP_TACTICS, doTransports() ); // Put the helicopters' riders down at the fight.
@@ -5952,6 +5974,57 @@ void AIPlayer::doSuperweapons( void )
buildAsap( superweapon );
}
+//----------------------------------------------------------------------------------------------------------
+static void collectObjects( Object *obj, void *userData )
+{
+ ((std::vector<Object*> *)userData)->push_back( obj );
+}
+
+//----------------------------------------------------------------------------------------------------------
+/** The skirmish scripts fire the powers they name and nothing else, so a side that runs another
+ * side's scripts (Turkey runs America's) never touched its own. A power marked AIFiresWhenReady goes
+ * off the moment it is ready and the science behind it is bought, at the target the scripts' own
+ * "fire at most cost" would pick. */
+//----------------------------------------------------------------------------------------------------------
+static const Int SPECIAL_POWER_CHECK_RATE = 2 * LOGICFRAMES_PER_SECOND;
+
+void AIPlayer::doReadySpecialPowers( void )
+{
+ const Int phase = computeUpdatePhase( m_player->getPlayerIndex(), SPECIAL_POWER_CHECK_RATE );
+ if( (TheGameLogic->getFrame() + phase) % SPECIAL_POWER_CHECK_RATE != 0 )
+ return;
+ Player *enemy = getAiEnemy();
+ if( enemy == NULL )
+ return;
+
+ std::vector<Object*> owned;
+ m_player->iterateObjects( collectObjects, &owned );
+ for( std::vector<Object*>::iterator it = owned.begin(); it != owned.end(); ++it )
+ {
+ Object *obj = *it;
+ if( obj->isEffectivelyDead() || obj->isDisabled() )
+ continue;
+ for( BehaviorModule **m = obj->getBehaviorModules(); *m; ++m )
+ {
+ SpecialPowerModuleInterface *power = (*m)->getSpecialPower();
+ if( power == NULL || !power->getSpecialPowerTemplate()->isFiredByAIWhenReady() || !power->isReady() )
+ continue;
+ const ScienceType science = power->getRequiredScience();
+ if( science != SCIENCE_INVALID && !m_player->hasScience( science ) )
+ continue;
+
+ const SpecialPowerTemplate *tmpl = power->getSpecialPowerTemplate();
+ const Real radius = max( 50.0f, tmpl->getRadiusCursorRadius() );
+ Coord3D target;
+ if( !m_player->computeSuperweaponTarget( tmpl, &target, enemy->getPlayerIndex(), radius ) || target.lengthSqr() <= 0.0f )
+ continue;
+ DEBUG_LOG(("AI POWER frame %d player %d fires '%s' at (%.0f,%.0f)\n", TheGameLogic->getFrame(),
+ m_player->getPlayerIndex(), tmpl->getName().str(), target.x, target.y));
+ power->doSpecialPowerAtLocation( &target, INVALID_ANGLE, COMMAND_FIRED_BY_SCRIPT );
+ }
+ }
+}
+
//----------------------------------------------------------------------------------------------------------
/** A money unit from every factory that trains one and has at most one thing in its queue. An
* idle-only rule measured as two hackers in a whole match, because a barracks feeding the army is
@@ -9977,6 +10050,64 @@ void AIPlayer::doTransports( void )
}
}
+//----------------------------------------------------------------------------------------------------------
+void AIPlayer::spotterStandOff( Object *obj, TacticalStep *step )
+{
+ AIUpdateInterface *ai = obj->getAI();
+ const Coord3D *pos = obj->getPosition();
+ const UnsignedInt now = TheGameLogic->getFrame();
+ const Real sight = obj->getVisionRange();
+
+ Object *threat = NULL;
+ Real threatReach = 0.0f;
+ Real threatDistSqr = 0.0f;
+ PartitionFilterPlayerAffiliation enemies( m_player, ALLOW_ENEMIES, true );
+ PartitionFilterAlive alive;
+ PartitionFilter *filters[] = { &enemies, &alive, NULL };
+ ObjectIterator *iter = ThePartitionManager->iterateObjectsInRange( pos, sight, FROM_CENTER_2D, filters );
+ MemoryPoolObjectHolder hold( iter );
+ for( Object *enemy = iter->first(); enemy; enemy = iter->next() )
+ {
+ if( enemy->getControllingPlayer() == m_player || m_player->getRelationship( enemy->getTeam() ) != ENEMIES )
+ continue;
+ if( enemy->isKindOf( KINDOF_PROJECTILE ) || !observerKnowsAbout( enemy, m_player->getPlayerIndex() ) )
+ continue;
+ const Real enemyRange = groundAttackRange( enemy );
+ if( enemyRange <= 0.0f )
+ continue;
+ const Real reach = enemyRange + Weapon_elevationRangeBonus( enemyRange, firingHeight( enemy ) - pos->z );
+ const Real distSqr = sqr( enemy->getPosition()->x - pos->x ) + sqr( enemy->getPosition()->y - pos->y );
+ if( distSqr > sqr( reach + KITE_WATCH_MARGIN + ai->getCurLocomotorSpeed() * KITE_TURN_FRAMES ) )
+ continue;
+ if( threat == NULL || distSqr < threatDistSqr )
+ {
+ threat = enemy;
+ threatReach = reach;
+ threatDistSqr = distSqr;
+ }
+ }
+ if( threat == NULL )
+ return;
+
+ const Real standoff = min( threatReach + KITE_MARGIN, sight );
+ if( threatDistSqr >= sqr( standoff - KITE_MARGIN ) )
+ return; // already standing off: a step would only slide it sideways
+ Coord3D spot;
+ if( !pickTacticalSpot( obj, pos, threat->getPosition(), standoff, threat->getPosition(), sight, &spot ) )
+ return;
+ DEBUG_LOG(("AI TACTICS frame %d player %d keeps spotter '%s' out of '%s' reach %.0f, (%.0f,%.0f) to (%.0f,%.0f)\n",
+ now, m_player->getPlayerIndex(), obj->getTemplate()->getName().str(), threat->getTemplate()->getName().str(), threatReach,
+ pos->x, pos->y, spot.x, spot.y));
+ // when the step is over it attack-moves to the origin; the spot, so it does not walk back in
+ step->target = INVALID_ID;
+ step->origin = spot;
+ const Real speed = max( ai->getCurLocomotorSpeed(), 0.1f );
+ const Real walk = sqrt( sqr( spot.x - pos->x ) + sqr( spot.y - pos->y ) );
+ step->resumeFrame = now + min<UnsignedInt>( KITE_STEP_MAX_FRAMES, KITE_TURN_FRAMES + REAL_TO_INT_CEIL( walk / speed ) );
+ step->rejoin = TRUE;
+ stepCalmly( obj, step, &spot );
+}
+
//----------------------------------------------------------------------------------------------------------
void AIPlayer::tacticsFor( Object *obj )
{
@@ -10105,6 +10236,14 @@ void AIPlayer::tacticsFor( Object *obj )
return;
}
+ // a spotter is the eyes of the guns behind it, and its carbine is for show: it stays just out of
+ // reach of every armed enemy it can see and keeps that enemy in sight, rather than trade shots
+ if( obj->getTemplate()->hasSpotterSight() )
+ {
+ spotterStandOff( obj, step );
+ return;
+ }
+
// kite: the nearest thing that is about to reach it and that it outranges
Object *closing = NULL;
Real closingRange = 0.0f;