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
fix(gameengine): pick targets by the armour the weapon is made for
Olcay Seygan2f2670815b2e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a21796625..550ec9100 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -563,7 +563,7 @@ found and fixed, EA's own, not port damage.**- A SCUD or a Tomahawk flies one smooth curve to its target. A SCUD went straight up, flipped over the top at six degrees a frame, glided flat for a moment and then kinked again into its dive. Now it rolls over and comes down in a single sweep that only ever eases off, its sharpest turn 4.6 degrees a frame. The Scud Storm takes the same curve and lands within half a second of when it did. Replays recorded before this play back differently.- Machine gun fire looks fast. The tracers of a Humvee, a Technical or a Quad Cannon crawled across the field at 300 a second and took half a second to reach a target 150 away; they now cross it in a tenth of a second, at 1,500, as streaks twice as long. Rangers, Rebels, Gattling Tanks and Cannons, Comanches and the Spectre got the same treatment. Only the picture changed: every bullet lands when it always did.- The Toxin Tractor and the other stream weapons spray one straight line into a building again, not a zigzag.-- Units pick their own targets by what their weapons kill fastest. A Humvee shoots the Technical or the infantry before the tank, and a tank goes for the tank. Anything that is shooting one of your units still gets answered. Computer armies pick the same way.+- Units pick their own targets by the armour their weapon is made for. A tank goes for the tank, even with a Technical beside it that would die sooner; a Humvee shoots the infantry and the Technicals before the tank; rocket soldiers go for vehicles. Among targets in the same armour, the one that dies quickest goes first. Anything that is shooting one of your units still gets answered, unless something in range that its weapon is made for is nearly dead. Computer armies pick the same way.## Point defense lasers back on the planes, in clips on the tanksdiff --git a/GeneralsMD/Code/Data/Scenarios/targetpick.txt b/GeneralsMD/Code/Data/Scenarios/targetpick.txtindex 9d06175e5..23ddf508b 100644--- a/GeneralsMD/Code/Data/Scenarios/targetpick.txt+++ b/GeneralsMD/Code/Data/Scenarios/targetpick.txt@@ -1,17 +1,20 @@-# What a unit on attack move shoots first: what its weapon kills best for the time it costs, not-# the nearest thing nor the most expensive one (aiTargetTier, aiTargetKillRate in AI.cpp).+# What a unit on attack move shoots first: the armour its weapon is made for, then what it kills+# best for the time it costs, not the nearest thing nor the most expensive one (aiTargetTier,+# aiArmourAffinity, aiTargetKillRate in AI.cpp).#-# Three fights 750 apart on Alpine Assault, each attacker spawned 190 to 225 from its whole group,-# outside the group's reach, with the target the nearest-first rule would have picked nearest:+# Four fights on Alpine Assault, each attacker spawned 190 to 225 from its whole group, outside the+# group's reach, with the target the nearest-first rule would have picked nearest:# A: a Humvee, a Battlemaster nearest and Red Guards behind it -> a Red Guard# B: a Crusader, Red Guards nearest and a Battlemaster behind -> the Battlemaster# C: a Humvee, a Battlemaster nearest and a Technical behind -> the Technical-# Each TARGET PICK line names the pick, its tier and rate, how many candidates the search held-# and the nearest of them. The attack-move scan is the line whose count holds the whole group;-# the attackers' turrets also scan their own shorter sight and log a pick among what that holds-# (the Crusader's 'of 2' at frame 69, the Humvee's Battlemaster 'of 1' while it drives to its pick).-# Seen 2026-11-04: A frame 71 Red Guard over the nearest Battlemaster; B frame 75 the Battlemaster-# over the nearest Red Guard; C frame 198 the Technical over the nearest Battlemaster.+# D: a Crusader, a Technical nearest and a Battlemaster behind -> the Battlemaster, though the+# Technical dies in three shots to its seven+# Each TARGET PICK line names the pick, its tier, armour affinity and rate, how many candidates the+# search held and the nearest of them. The attack-move scan is the line whose count holds the whole+# group; the attackers' turrets also scan their own shorter sight and log a pick among what that+# holds (a Crusader's 'of 2' just after the order, the Humvee's Battlemaster 'of 1' while it drives+# to its pick). Seen 2026-11-04, each over the nearest: A frame 71 a Red Guard, B 75 the+# Battlemaster, C 198 the Technical, D 614 the Battlemaster (armour 1.53 against the Technical's 1.31).## generals.exe -headless -quickstart -noshellmap -observer -multiInstance -noFPSLimit# -map "Maps\Alpine Assault\Alpine Assault.map" -autoskirmish 2 -takeover@@ -30,7 +33,14 @@30 spawn 1 GLAVehicleTechnical 1 985 106030 spawn 0 SupW_AmericaVehicleHumvee 1 760 1060+32 attackmove 0 AmericaVehicleHumvee 1400 300++# D comes 440 from C, inside the Humvee's search, so it arrives after C's first pick+300 spawn 1 GLAVehicleTechnical 1 950 1500+300 spawn 1 ChinaTankBattleMaster 1 985 1500+300 spawn 0 SupW_AmericaTankCrusader 1 760 1500+302 attackmove 0 SupW_AmericaTankCrusader 1400 150032 attackmove 0 AmericaTankCrusader 1335 220032 attackmove 0 SupW_AmericaVehicleHumvee 1400 1060diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.hindex c661de1da..ff2cc8084 100644--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/AI.h@@ -396,6 +396,7 @@ void aiShotTiming( const WeaponTemplate *weapon, const WeaponBonus &bonus, AIShostruct AITargetRating{Int m_tier; ///< aiTargetTier+ Real m_affinity; ///< aiArmourAffinity of our best-suited weapon against its armourReal m_rate; ///< aiTargetKillRateReal m_distSqr; ///< the nearer of two equal rates, for the unit that can hurt nothing thereObjectID m_id; ///< the last tie, so every machine picks the same one@@ -403,18 +404,25 @@ struct AITargetRatingconst Int AI_QUICK_KILL_FRAMES = 10 * LOGICFRAMES_PER_SECOND;-/** 2 for whoever just shot us, and for an armed enemy already in reach that we kill within- * AI_QUICK_KILL_FRAMES; 1 for any other enemy that can shoot us; 0 for the rest. So a far target- * we counter never keeps a unit from turning on its shooter, and one we finish in seconds does. */-Int aiTargetTier( Bool shotMe, Bool threatensMe, Bool inRange, Real framesToKill );+/** 2 for whoever just shot us, and while something is shooting us for an armed enemy already in+ * reach that we kill within AI_QUICK_KILL_FRAMES; 1 for any other enemy that can shoot us; 0 for+ * the rest. So a far target never keeps a unit from turning on its shooter, and one in reach that+ * its weapon is made for and that dies in seconds does. */+Int aiTargetTier( Bool shotMe, Bool underFire, Bool threatensMe, Bool inRange, Real framesToKill );++/** How much a weapon is made for an armour: what the armour lets through of its damage type over the+ * mean of what it lets through of every combat damage type (coefficients, count of them). Tank+ * armour stops small arms and fire and lets a tank shell through, so the shell is made for it; a+ * truck stops less of everything, so the same shell is less its weapon. 0 for a mean of 0. */+Real aiArmourAffinity( Real mine, const Real *coefficients, Int count );/** What one target is worth per frame spent on it: value over the frames to kill it (aiFramesToKill* with our best weapon against its armour and health) plus the frames to drive gap world units at* speed into reach. 0 when we cannot hurt it, or it is out of reach of a unit that cannot move. */Real aiTargetKillRate( Real value, Real framesToKill, Real gap, Real speed );-/** Whether a takes the pick from b: the higher tier, then the higher rate, then the nearer, then the- * lower ObjectID. */+/** Whether a takes the pick from b: the higher tier, then the armour our weapon is made for (the+ * higher affinity), then the higher rate, then the nearer, then the lower ObjectID. */Bool aiTargetBeats( const AITargetRating &a, const AITargetRating &b );/** One pairing as 0..1, money for money: 0.5 an even trade, 1 when my side kills sixteen timesdiff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cppindex 77f8fcc63..00bdf7c5f 100644--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp@@ -42,6 +42,8 @@#include "Common/XferCRC.h"#include "GameLogic/AI.h"+#include "GameLogic/Armor.h"+#include "GameLogic/ArmorSet.h"#include "GameLogic/PartitionManager.h"#include "GameLogic/Module/AIUpdate.h"#include "GameLogic/Module/BodyModule.h"@@ -364,6 +366,26 @@ AI::AI( void )* does (AI_targetValue). Keyed by template, so cleared with every map in AI::reset. */static std::map<const ThingTemplate*, const ThingTemplate*> theVariationParents;+/** The damage types the shipped weapons fire at units, what aiArmourAffinity measures an armour+ * against. Healing, capture, subdual, pilot kills and the clean-up sprays are not combat. */+static const DamageType AI_COMBAT_DAMAGE[] =+{+ DAMAGE_EXPLOSION, DAMAGE_CRUSH, DAMAGE_ARMOR_PIERCING, DAMAGE_SMALL_ARMS, DAMAGE_GATTLING, DAMAGE_RADIATION,+ DAMAGE_FLAME, DAMAGE_SNIPER, DAMAGE_POISON, DAMAGE_PARTICLE_BEAM, DAMAGE_INFANTRY_MISSILE, DAMAGE_AURORA_BOMB,+ DAMAGE_LAND_MINE, DAMAGE_JET_MISSILES, DAMAGE_STEALTHJET_MISSILES, DAMAGE_MOLOTOV_COCKTAIL, DAMAGE_COMANCHE_VULCAN+};+static const Int AI_COMBAT_DAMAGE_COUNT = sizeof( AI_COMBAT_DAMAGE ) / sizeof( AI_COMBAT_DAMAGE[ 0 ] );++/** What a kind of unit's armour lets through of each damage type, its unconditioned ArmorSet (the+ * unit the build menu offers), read once per template (AI_armourOf). Keyed by template, so+ * cleared with every map in AI::reset. */+struct AIArmourRow+{+ Real m_coefficient[ DAMAGE_NUM_TYPES ];+ Real m_combat[ AI_COMBAT_DAMAGE_COUNT ];+};+static std::map<const ThingTemplate*, AIArmourRow> theArmourOfTemplate;+void AI::init( void ){m_nextGroupID = 0;@@ -403,6 +425,7 @@ void AI::reset( void )m_nextGroupID = 0;m_sweptGroupID = 0;theVariationParents.clear();+ theArmourOfTemplate.clear();m_nextFormationID = NO_FORMATION_ID;getNextFormationID(); // increment once past NO_FORMATION_ID. jba.m_lastBuildingPlacementFrame = 0;@@ -792,29 +815,40 @@ static void priorityFunc(Object *obj, void *userData)* can link straight to it.** Idle units took the nearest thing and attack move the most expensive one, so a Humvee spent- * half a minute plinking a Battlemaster while the Technical beside it drove off. The rate is the- * enemy's money a unit takes off per frame it spends on a target: the target's worth over the- * frames its best weapon needs to kill it, through its armour and with the health it has left- * (aiFramesToKill, the AI's matchup arithmetic), plus the frames to drive into reach. A Humvee- * gun does a quarter of its damage to tank armour and a tank shell a tenth of its to a soldier,- * so each takes what it is built for, and a wounded enemy is a quicker kill than a fresh one.+ * half a minute plinking a Battlemaster while the Technical beside it drove off. A unit now goes+ * for the armour its weapon is made for (aiArmourAffinity): a tank shell for tank armour, a+ * Humvee gun for soldiers and then trucks, a rocket soldier's missile for tanks. Among targets of+ * one armour the rate decides: the enemy's money a unit takes off per frame it spends on a target,+ * the target's worth over the frames its best weapon needs to kill it with the health it has left+ * (aiFramesToKill, the AI's matchup arithmetic), plus the frames to drive into reach. So a+ * wounded enemy goes before a fresh one of its kind.** Worth is 'ThreatValue' where a mod sets it and build cost where it does not; the shipped INI* sets it nowhere.*- * The tiers keep two old rules above the rate. Anything that can shoot back outranks anything+ * The tiers keep two old rules above the armour. Anything that can shoot back outranks anything* that cannot, so a group in an enemy base does not stop for the dozer while the artillery- * fires. And whoever just shot us outranks everything but an armed enemy already in reach that- * dies within AI_QUICK_KILL_FRAMES: a far target the unit counters never keeps it from turning- * on its shooter, a near one it finishes in seconds does.+ * fires. And whoever just shot us outranks everything but, while it shoots, an armed enemy+ * already in reach that dies within AI_QUICK_KILL_FRAMES, which then goes by armour against the+ * shooter: a far target never keeps a unit from turning on its shooter, and a Humvee under a+ * tank's fire still finishes the Technical beside it first.*/-Int aiTargetTier( Bool shotMe, Bool threatensMe, Bool inRange, Real framesToKill )+Int aiTargetTier( Bool shotMe, Bool underFire, Bool threatensMe, Bool inRange, Real framesToKill ){- if( shotMe || ( threatensMe && inRange && framesToKill >= 0.0f && framesToKill <= INT_TO_REAL( AI_QUICK_KILL_FRAMES ) ) )+ if( shotMe || ( underFire && threatensMe && inRange && framesToKill >= 0.0f && framesToKill <= INT_TO_REAL( AI_QUICK_KILL_FRAMES ) ) )return 2;return threatensMe ? 1 : 0;}+//-----------------------------------------------------------------------------+Real aiArmourAffinity( Real mine, const Real *coefficients, Int count )+{+ Real sum = 0.0f;+ for( Int i = 0; i < count; ++i )+ sum += coefficients[ i ];+ return sum > 0.0f ? mine * INT_TO_REAL( count ) / sum : 0.0f;+}+//-----------------------------------------------------------------------------Real aiTargetKillRate( Real value, Real framesToKill, Real gap, Real speed ){@@ -835,6 +869,8 @@ Bool aiTargetBeats( const AITargetRating &a, const AITargetRating &b ){if( a.m_tier != b.m_tier )return a.m_tier > b.m_tier;+ if( a.m_affinity != b.m_affinity )+ return a.m_affinity > b.m_affinity;if( a.m_rate != b.m_rate )return a.m_rate > b.m_rate;if( a.m_distSqr != b.m_distSqr )@@ -879,10 +915,28 @@ struct AITargetFacts{AITargetRating m_rating;Bool m_shotMe;+ Bool m_underFire;Bool m_inRange;Real m_framesToKill;};+static const AIArmourRow &AI_armourOf( const ThingTemplate *tmpl )+{+ std::map<const ThingTemplate*, AIArmourRow>::iterator known = theArmourOfTemplate.find( tmpl );+ if( known == theArmourOfTemplate.end() )+ {+ const ArmorTemplateSet *set = tmpl->findArmorTemplateSet( ArmorSetFlags() );+ const Armor armor( set != NULL ? set->getArmorTemplate() : NULL );+ AIArmourRow row;+ for( Int t = 0; t < DAMAGE_NUM_TYPES; ++t )+ row.m_coefficient[ t ] = armor.adjustDamage( (DamageType)t, 1.0f );+ for( Int i = 0; i < AI_COMBAT_DAMAGE_COUNT; ++i )+ row.m_combat[ i ] = row.m_coefficient[ AI_COMBAT_DAMAGE[ i ] ];+ known = theArmourOfTemplate.insert( std::make_pair( tmpl, row ) ).first;+ }+ return known->second;+}+/** What the unit choosing brings to every candidate: its weapons as they fire now, once per search* rather than once per candidate. */struct AIShooter@@ -937,30 +991,52 @@ static void AI_readShooter( const Object *me, AIShooter &shooter )? damage->in.m_sourceID : INVALID_ID;}-/** The most victim could rate, from its distance and worth alone: in reach if anything could be,- * killed in one shot of the fastest weapon. aiTargetBeats on it decides whether the rest is read. */+/** The best aiArmourAffinity among the weapons that may aim at victim. */+static Real AI_armourAffinity( const AIShooter &shooter, const Object *victim )+{+ const AIArmourRow &armour = AI_armourOf( victim->getTemplate() );+ const Int antiMask = WeaponSet::getVictimAntiMask( victim );+ Real best = 0.0f;+ for( Int slot = 0; slot < WEAPONSLOT_COUNT; ++slot )+ {+ const Weapon *weapon = shooter.m_weapon[ slot ];+ if( weapon == NULL || ( weapon->getAntiMask() & antiMask ) == 0 )+ continue;+ best = max( best, aiArmourAffinity( armour.m_coefficient[ weapon->getDamageType() ], armour.m_combat, AI_COMBAT_DAMAGE_COUNT ) );+ }+ return best;+}++/** The most victim could rate, from its distance, armour and worth alone: in reach if anything+ * could be, killed in one shot of the fastest weapon. aiTargetBeats on it decides whether the+ * rest is read. */static Int AI_targetCeilingTier( const AIShooter &shooter, const Object *victim, Real distSqr ){- return ( victim->getID() == shooter.m_shooterID || distSqr <= sqr( shooter.m_longestReach ) ) ? 2 : 1;+ if( victim->getID() == shooter.m_shooterID )+ return 2;+ return ( shooter.m_shooterID != INVALID_ID && distSqr <= sqr( shooter.m_longestReach ) ) ? 2 : 1;}-static AITargetRating AI_targetCeiling( const AIShooter &shooter, const Object *victim, Real distSqr, Int value )+static AITargetRating AI_targetCeiling( const AIShooter &shooter, const Object *victim, Real distSqr, Int value, Real affinity ){AITargetRating r;r.m_id = victim->getID();r.m_distSqr = distSqr;+ r.m_affinity = affinity;r.m_tier = AI_targetCeilingTier( shooter, victim, distSqr );const Real gap = distSqr <= sqr( shooter.m_longestReach ) ? 0.0f : sqrt( distSqr ) - shooter.m_longestRange;r.m_rate = aiTargetKillRate( INT_TO_REAL( value ), shooter.m_fastestShot, gap, shooter.m_speed );return r;}-static void AI_targetFacts( const AIShooter &shooter, const Object *victim, Real distSqr, Int value, AITargetFacts &facts )+static void AI_targetFacts( const AIShooter &shooter, const Object *victim, Real distSqr, Int value, Real affinity, AITargetFacts &facts ){const Object *me = shooter.m_me;facts.m_rating.m_id = victim->getID();facts.m_rating.m_distSqr = distSqr;+ facts.m_rating.m_affinity = affinity;facts.m_shotMe = victim->getID() == shooter.m_shooterID;+ facts.m_underFire = shooter.m_shooterID != INVALID_ID;// the weapon that kills it soonest, of those this unit may turn on it by itselfconst Real health = victim->getBodyModule()->getHealth();@@ -987,7 +1063,7 @@ static void AI_targetFacts( const AIShooter &shooter, const Object *victim, Realif( best >= 0 && !facts.m_inRange )gap = sqrt( distSqr ) - shooter.m_range[ best ];facts.m_rating.m_rate = aiTargetKillRate( INT_TO_REAL( value ), facts.m_framesToKill, gap, shooter.m_speed );- facts.m_rating.m_tier = aiTargetTier( facts.m_shotMe, TRUE, facts.m_inRange, facts.m_framesToKill );+ facts.m_rating.m_tier = aiTargetTier( facts.m_shotMe, facts.m_underFire, TRUE, facts.m_inRange, facts.m_framesToKill );}static Bool AI_threatens( Object *enemy, const Object *me )@@ -1002,8 +1078,8 @@ static Bool AI_threatens( Object *enemy, const Object *me )static AITargetRating AI_rateFully( const AIShooter &shooter, Object *victim, Real distSqr ){AITargetFacts facts;- AI_targetFacts( shooter, victim, distSqr, AI_targetValue( victim ), facts );- facts.m_rating.m_tier = aiTargetTier( facts.m_shotMe, AI_threatens( victim, shooter.m_me ), facts.m_inRange, facts.m_framesToKill );+ AI_targetFacts( shooter, victim, distSqr, AI_targetValue( victim ), AI_armourAffinity( shooter, victim ), facts );+ facts.m_rating.m_tier = aiTargetTier( facts.m_shotMe, facts.m_underFire, AI_threatens( victim, shooter.m_me ), facts.m_inRange, facts.m_framesToKill );return facts.m_rating;}@@ -1070,21 +1146,25 @@ Object *AI::pickBestTarget( const Object *me, SimpleObjectIterator *candidates,nearest = other;nearestDistSqr = distSqr;}- // the tier's ceiling is a distance and an id; the worth is a price lookup- if( best != NULL && AI_targetCeilingTier( shooter, other, distSqr ) < bestRating.m_tier )+ // the tier's ceiling is a distance and an id, the armour a cached row; the worth is a price lookup+ const Int ceilingTier = AI_targetCeilingTier( shooter, other, distSqr );+ if( best != NULL && ceilingTier < bestRating.m_tier )+ continue;+ const Real affinity = AI_armourAffinity( shooter, other );+ if( best != NULL && ceilingTier == bestRating.m_tier && affinity < bestRating.m_affinity )continue;const Int value = AI_targetValue( other );- if( best != NULL && !aiTargetBeats( AI_targetCeiling( shooter, other, distSqr, value ), bestRating ) )+ if( best != NULL && !aiTargetBeats( AI_targetCeiling( shooter, other, distSqr, value, affinity ), bestRating ) )continue;AITargetFacts facts;- AI_targetFacts( shooter, other, distSqr, value, facts );+ AI_targetFacts( shooter, other, distSqr, value, affinity, facts );if( best != NULL && !aiTargetBeats( facts.m_rating, bestRating ) )continue;if( !AI_filtersAllow( lateFilters, other ) )continue;if( !AI_threatens( other, me ) ){- facts.m_rating.m_tier = aiTargetTier( facts.m_shotMe, FALSE, facts.m_inRange, facts.m_framesToKill );+ facts.m_rating.m_tier = aiTargetTier( facts.m_shotMe, facts.m_underFire, FALSE, facts.m_inRange, facts.m_framesToKill );if( best != NULL && !aiTargetBeats( facts.m_rating, bestRating ) )continue;}@@ -1095,9 +1175,9 @@ Object *AI::pickBestTarget( const Object *me, SimpleObjectIterator *candidates,// -scenario only: the pick beside the nearest candidate, which targetpick.txt readsif( best != NULL && !TheGlobalData->m_scenarioFile.isEmpty() ){- DEBUG_LOG(( "TARGET PICK: frame %d '%s' %d picks '%s' %d tier %d rate %.4f of %d, nearest '%s' %d\n",+ DEBUG_LOG(( "TARGET PICK: frame %d '%s' %d picks '%s' %d tier %d armour %.2f rate %.4f of %d, nearest '%s' %d\n",TheGameLogic->getFrame(), me->getTemplate()->getName().str(), me->getID(), best->getTemplate()->getName().str(),- best->getID(), bestRating.m_tier, bestRating.m_rate, count, nearest->getTemplate()->getName().str(), nearest->getID() ));+ best->getID(), bestRating.m_tier, bestRating.m_affinity, bestRating.m_rate, count, nearest->getTemplate()->getName().str(), nearest->getID() ));}return best;}@@ -1284,18 +1364,22 @@ Object *AI::findClosestEnemy( const Object *me, Real range, UnsignedInt qualifiebestRating = AI_rateFully(shooter, bestEnemy, bestDistSqr);bestRated = TRUE;}- if (AI_targetCeilingTier(shooter, theEnemy, distSqr) < bestRating.m_tier)+ const Int ceilingTier = AI_targetCeilingTier(shooter, theEnemy, distSqr);+ if (ceilingTier < bestRating.m_tier)+ continue;+ const Real affinity = AI_armourAffinity(shooter, theEnemy);+ if (ceilingTier == bestRating.m_tier && affinity < bestRating.m_affinity)continue;const Int value = AI_targetValue(theEnemy);- if (!aiTargetBeats(AI_targetCeiling(shooter, theEnemy, distSqr, value), bestRating))+ if (!aiTargetBeats(AI_targetCeiling(shooter, theEnemy, distSqr, value, affinity), bestRating))continue;AITargetFacts facts;- AI_targetFacts(shooter, theEnemy, distSqr, value, facts);+ AI_targetFacts(shooter, theEnemy, distSqr, value, affinity, facts);if (!aiTargetBeats(facts.m_rating, bestRating) || !AI_filtersAllow(filters + numGatherFilters, theEnemy))continue;if (!AI_threatens(theEnemy, me)){- facts.m_rating.m_tier = aiTargetTier(facts.m_shotMe, FALSE, facts.m_inRange, facts.m_framesToKill);+ facts.m_rating.m_tier = aiTargetTier(facts.m_shotMe, facts.m_underFire, FALSE, facts.m_inRange, facts.m_framesToKill);if (!aiTargetBeats(facts.m_rating, bestRating))continue;}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex b20ff757e..d12e97558 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -3342,20 +3342,62 @@ TEST(attack_move_still_shoots_what_it_can_already_reach)CHECK( AIAttackMove_mayTakeTarget( true, true, false, 1030, 1030 ) );}-/* AI.cpp: a unit choosing its own target takes what its weapon kills best for the time it- costs, not the nearest thing (idle) or the most expensive one (attack move). The numbers are- the shipped INI's: HumveeGun 10 a shot every 12 frames, CrusaderTankGun 60 every 120; tank armour- takes a quarter of the Humvee's, truck armour half, a soldier a tenth of the tank's. */-static AITargetRating targetRating( Int tier, Real rate, Real distSqr, ObjectID id )+/* AI.cpp: a unit choosing its own target goes for the armour its weapon is made for, then for+ what it kills best for the time it costs, not the nearest thing (idle) or the most expensive one+ (attack move). The numbers are the shipped INI's: HumveeGun 10 a shot every 12 frames,+ CrusaderTankGun 60 every 120; the armour rows are Armor.ini's (HumanArmor with the fork's 30%+ for tank shells) over AI.cpp's combat damage types: explosion, crush, armour piercing, small+ arms, gattling, radiation, flame, sniper, poison, particle beam, infantry missile, Aurora bomb,+ land mine, jet missiles, stealth jet missiles, molotov, Comanche vulcan. */+static AITargetRating targetRating( Int tier, Real affinity, Real rate, Real distSqr, ObjectID id ){AITargetRating r;r.m_tier = tier;+ r.m_affinity = affinity;r.m_rate = rate;r.m_distSqr = distSqr;r.m_id = id;return r;}+TEST(a_unit_goes_for_the_armour_its_weapon_is_made_for)+{+ enum { PIERCING = 2, VULCAN = 16, MISSILE = 10, COUNT = 17 };+ const Real HUMAN[ COUNT ] = { 1.0f, 2.0f, 0.3f, 1.0f, 1.0f, 1.0f, 1.5f, 2.0f, 1.0f, 1.5f, 0.1f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };+ const Real TRUCK[ COUNT ] = { 1.0f, 0.5f, 1.0f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.5f, 1.0f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.5f };+ const Real TANK[ COUNT ] = { 1.0f, 0.5f, 1.0f, 0.25f, 0.1f, 0.5f, 0.25f, 0.0f, 0.25f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.25f };++ /* a tank shell does full damage to a Technical and a Battlemaster alike, but tank armour stops+ most other weapons and truck armour does not: the shell is made for the tank */+ CHECK_NEAR( 1.5315f, aiArmourAffinity( TANK[ PIERCING ], TANK, COUNT ), 0.001f );+ CHECK_NEAR( 1.3077f, aiArmourAffinity( TRUCK[ PIERCING ], TRUCK, COUNT ), 0.001f );+ CHECK( aiArmourAffinity( TANK[ PIERCING ], TANK, COUNT ) > aiArmourAffinity( TRUCK[ PIERCING ], TRUCK, COUNT ) );+ CHECK( aiArmourAffinity( TRUCK[ PIERCING ], TRUCK, COUNT ) > aiArmourAffinity( HUMAN[ PIERCING ], HUMAN, COUNT ) );++ /* a Humvee gun is made for soldiers, then trucks, last tanks */+ CHECK( aiArmourAffinity( HUMAN[ VULCAN ], HUMAN, COUNT ) > aiArmourAffinity( TRUCK[ VULCAN ], TRUCK, COUNT ) );+ CHECK( aiArmourAffinity( TRUCK[ VULCAN ], TRUCK, COUNT ) > aiArmourAffinity( TANK[ VULCAN ], TANK, COUNT ) );++ /* a rocket soldier's missile is made for vehicles, tanks first */+ CHECK( aiArmourAffinity( TANK[ MISSILE ], TANK, COUNT ) > aiArmourAffinity( TRUCK[ MISSILE ], TRUCK, COUNT ) );+ CHECK( aiArmourAffinity( TRUCK[ MISSILE ], TRUCK, COUNT ) > aiArmourAffinity( HUMAN[ MISSILE ], HUMAN, COUNT ) );++ /* an armour that lets nothing through is no weapon's */+ const Real NOTHING[ COUNT ] = { 0.0f };+ CHECK_NEAR( 0.0f, aiArmourAffinity( 0.0f, NOTHING, COUNT ), 0.0001f );++ /* so a Crusader takes the Battlemaster over the Technical, though the Technical (3 shots) dies+ in less than half the Battlemaster's time (7): the armour comes before the rate */+ AIShotPattern tank;+ tank.m_delayFrames = 120.0f;+ tank.m_damagePerShot = 60.0f;+ const Real onBattlemaster = aiTargetKillRate( 800.0f, aiFramesToKill( 400.0f, tank ), 0.0f, 2.0f );+ const Real onTechnical = aiTargetKillRate( 500.0f, aiFramesToKill( 180.0f, tank ), 0.0f, 2.0f );+ CHECK( onTechnical > onBattlemaster );+ CHECK( aiTargetBeats( targetRating( 1, aiArmourAffinity( TANK[ PIERCING ], TANK, COUNT ), onBattlemaster, 900.0f, (ObjectID)9 ),+ targetRating( 1, aiArmourAffinity( TRUCK[ PIERCING ], TRUCK, COUNT ), onTechnical, 100.0f, (ObjectID)2 ) ) );+}+TEST(a_unit_shoots_what_its_weapon_kills_best){AIShotPattern humvee;@@ -3396,44 +3438,50 @@ TEST(a_unit_shoots_what_its_weapon_kills_best)/* nothing it can hurt rates 0, which still beats having no target: a unit with nothing itcounters fires at whatever is there, the nearest first */CHECK_NEAR( 0.0f, aiTargetKillRate( 800.0f, AI_CANNOT_KILL, 0.0f, SPEED ), 0.0001f );- CHECK( aiTargetBeats( targetRating( 1, 0.0f, 100.0f, (ObjectID)9 ), targetRating( 1, 0.0f, 400.0f, (ObjectID)2 ) ) );+ CHECK( aiTargetBeats( targetRating( 1, 1.0f, 0.0f, 100.0f, (ObjectID)9 ), targetRating( 1, 1.0f, 0.0f, 400.0f, (ObjectID)2 ) ) );- /* the tiers: whoever just shot us is 2 wherever it stands; an armed enemy in reach that dies- within the quick-kill window is 2 too; any other armed one 1; one that cannot shoot back 0,- however quickly it dies */+ /* the tiers: whoever just shot us is 2 wherever it stands; while something shoots us, an armed+ enemy in reach that dies within the quick-kill window is 2 too; any other armed one 1; one+ that cannot shoot back 0, however quickly it dies. Not under fire, a quick kill is no tier of+ its own, so the armour decides: a Crusader does not take a Technical for dying fast */const Real QUICK = INT_TO_REAL( AI_QUICK_KILL_FRAMES );- CHECK_EQ( aiTargetTier( TRUE, TRUE, FALSE, 5000.0f ), 2 );- CHECK_EQ( aiTargetTier( TRUE, FALSE, FALSE, AI_CANNOT_KILL ), 2 );- CHECK_EQ( aiTargetTier( FALSE, TRUE, TRUE, QUICK ), 2 );- CHECK_EQ( aiTargetTier( FALSE, TRUE, TRUE, QUICK + 1.0f ), 1 );- CHECK_EQ( aiTargetTier( FALSE, TRUE, FALSE, 60.0f ), 1 );- CHECK_EQ( aiTargetTier( FALSE, TRUE, TRUE, AI_CANNOT_KILL ), 1 );- CHECK_EQ( aiTargetTier( FALSE, FALSE, TRUE, 60.0f ), 0 );+ CHECK_EQ( aiTargetTier( TRUE, TRUE, TRUE, FALSE, 5000.0f ), 2 );+ CHECK_EQ( aiTargetTier( TRUE, TRUE, FALSE, FALSE, AI_CANNOT_KILL ), 2 );+ CHECK_EQ( aiTargetTier( FALSE, TRUE, TRUE, TRUE, QUICK ), 2 );+ CHECK_EQ( aiTargetTier( FALSE, FALSE, TRUE, TRUE, QUICK ), 1 );+ CHECK_EQ( aiTargetTier( FALSE, TRUE, TRUE, TRUE, QUICK + 1.0f ), 1 );+ CHECK_EQ( aiTargetTier( FALSE, TRUE, TRUE, FALSE, 60.0f ), 1 );+ CHECK_EQ( aiTargetTier( FALSE, TRUE, TRUE, TRUE, AI_CANNOT_KILL ), 1 );+ CHECK_EQ( aiTargetTier( FALSE, TRUE, FALSE, TRUE, 60.0f ), 0 );/* so the Battlemaster shooting the Humvee beats a Technical it would have to drive to, and- loses to one already in reach that it finishes in seconds, on the rate */+ loses to one already in reach that it finishes in seconds, whose truck armour its gun is+ more made for than the tank's (affinities from the armour test above) */+ const Real HUMVEE_ON_TANK = 0.383f;+ const Real HUMVEE_ON_TRUCK = 0.654f;humvee.m_damagePerShot = 2.5f;const Real humveeTankFrames = aiFramesToKill( 400.0f, humvee );humvee.m_damagePerShot = 5.0f;const Real humveeTruckFrames = aiFramesToKill( 180.0f, humvee );CHECK_NEAR( 1920.0f, humveeTankFrames, 0.001f );CHECK_NEAR( 432.0f, humveeTruckFrames, 0.001f );- CHECK( aiTargetBeats( targetRating( aiTargetTier( TRUE, TRUE, TRUE, humveeTankFrames ), humveeOnTank, 0.0f, (ObjectID)9 ),- targetRating( aiTargetTier( FALSE, TRUE, FALSE, humveeTruckFrames ), humveeOnTruck, 0.0f, (ObjectID)2 ) ) );- CHECK( aiTargetBeats( targetRating( aiTargetTier( FALSE, TRUE, TRUE, humveeTruckFrames ), humveeOnTruck, 0.0f, (ObjectID)9 ),- targetRating( aiTargetTier( TRUE, TRUE, TRUE, humveeTankFrames ), humveeOnTank, 0.0f, (ObjectID)2 ) ) );-- /* armed beats unarmed whatever the rate: no stopping for the dozer under the artillery */- CHECK( aiTargetBeats( targetRating( 1, 0.01f, 0.0f, (ObjectID)9 ), targetRating( 0, 100.0f, 0.0f, (ObjectID)2 ) ) );-- /* a total order: tier, rate, distance, then the lower ObjectID, the same whichever is asked- first, so the pick never depends on the order the partition hands candidates out in */- const AITargetRating a = targetRating( 1, 0.5f, 100.0f, (ObjectID)2 );- const AITargetRating b = targetRating( 1, 0.5f, 100.0f, (ObjectID)9 );+ CHECK( aiTargetBeats( targetRating( aiTargetTier( TRUE, TRUE, TRUE, TRUE, humveeTankFrames ), HUMVEE_ON_TANK, humveeOnTank, 0.0f, (ObjectID)9 ),+ targetRating( aiTargetTier( FALSE, TRUE, TRUE, FALSE, humveeTruckFrames ), HUMVEE_ON_TRUCK, humveeOnTruck, 0.0f, (ObjectID)2 ) ) );+ CHECK( aiTargetBeats( targetRating( aiTargetTier( FALSE, TRUE, TRUE, TRUE, humveeTruckFrames ), HUMVEE_ON_TRUCK, humveeOnTruck, 0.0f, (ObjectID)9 ),+ targetRating( aiTargetTier( TRUE, TRUE, TRUE, TRUE, humveeTankFrames ), HUMVEE_ON_TANK, humveeOnTank, 0.0f, (ObjectID)2 ) ) );++ /* armed beats unarmed whatever the armour or rate: no stopping for the dozer under the artillery */+ CHECK( aiTargetBeats( targetRating( 1, 0.1f, 0.01f, 0.0f, (ObjectID)9 ), targetRating( 0, 2.0f, 100.0f, 0.0f, (ObjectID)2 ) ) );++ /* a total order: tier, armour, rate, distance, then the lower ObjectID, the same whichever is+ asked first, so the pick never depends on the order the partition hands candidates out in */+ const AITargetRating a = targetRating( 1, 1.0f, 0.5f, 100.0f, (ObjectID)2 );+ const AITargetRating b = targetRating( 1, 1.0f, 0.5f, 100.0f, (ObjectID)9 );CHECK( aiTargetBeats( a, b ) );CHECK( !aiTargetBeats( b, a ) );CHECK( !aiTargetBeats( a, a ) );- CHECK( aiTargetBeats( targetRating( 1, 0.6f, 900.0f, (ObjectID)9 ), a ) );+ CHECK( aiTargetBeats( targetRating( 1, 1.0f, 0.6f, 900.0f, (ObjectID)9 ), a ) );+ CHECK( aiTargetBeats( targetRating( 1, 1.1f, 0.1f, 900.0f, (ObjectID)9 ), a ) );}/* AI.cpp: the scripted-priority search a hunter runs over the whole map picks in one pass over