static bool ValidateMultiAttackOrder(MultiTargetAttackOrderInfo order, AbstractActor unit) { AIUtil.LogAI("Multiattack validation", unit); for (int subAttackIndex = 0; subAttackIndex < order.SubTargetOrders.Count; ++subAttackIndex) { AttackOrderInfo subOrder = order.SubTargetOrders[subAttackIndex]; AIUtil.LogAI(string.Format("SubAttack #{0}: target {1} {2}", subAttackIndex, subOrder.TargetUnit.GUID, subOrder.TargetUnit.DisplayName)); foreach (Weapon w in subOrder.Weapons) { AIUtil.LogAI(string.Format(" Weapon {0}", w.Name)); } } List <string> targetGUIDs = new List <string>(); foreach (AttackOrderInfo subOrder in order.SubTargetOrders) { string thisGUID = subOrder.TargetUnit.GUID; if (targetGUIDs.IndexOf(thisGUID) != -1) { // found duplicated target GUIDs AIUtil.LogAI("Multiattack error: Duplicated target GUIDs", unit); return(false); } foreach (Weapon w in subOrder.Weapons) { if (!w.CanFire) { AIUtil.LogAI("Multiattack error: weapon that cannot fire", unit); return(false); } ICombatant target = subOrder.TargetUnit; if (!unit.Combat.LOFCache.UnitHasLOFToTargetAtTargetPosition( unit, target, w.MaxRange, unit.CurrentPosition, unit.CurrentRotation, target.CurrentPosition, target.CurrentRotation, w.IndirectFireCapable)) { AIUtil.LogAI("Multiattack error: weapon that cannot fire", unit); return(false); } } } AIUtil.LogAI("Multiattack validates OK", unit); return(true); }
override protected BehaviorTreeResults Tick() { const int laserCount = 2; List <Weapon> lasers = new List <Weapon>(); for (int wi = 0; wi < unit.Weapons.Count; ++wi) { if (lasers.Count >= laserCount) { break; } Weapon w = unit.Weapons[wi]; // TODO? (dlecompte) make this more specific if (w.WeaponCategoryValue.IsEnergy) { lasers.Add(w); } } if ((lasers.Count == 0) || (unit.BehaviorTree.enemyUnits.Count == 0)) { return(BehaviorTreeResults.BehaviorTreeResultsFromBoolean(false)); } AttackOrderInfo attackOrder = new AttackOrderInfo(unit.BehaviorTree.enemyUnits[0]); attackOrder.Weapons = lasers; attackOrder.TargetUnit = unit.BehaviorTree.enemyUnits[0]; BehaviorTreeResults results = new BehaviorTreeResults(BehaviorNodeState.Success); results.orderInfo = attackOrder; return(results); }
// Evaluate all possible attacks for the attacker and target based upon their current position. Returns the total damage the target will take, // which will be compared against all other targets to determine the optimal attack to make public static float MakeAttackOrderForTarget(AbstractActor attackerAA, ICombatant target, bool isStationary, out BehaviorTreeResults order) { Mod.Log.Debug?.Write($"Evaluating AttackOrder from ({CombatantUtils.Label(attackerAA)}) against ({CombatantUtils.Label(target)} at position: ({target.CurrentPosition})"); // If the unit has no visibility to the target from the current position, they can't attack. Return immediately. if (!AIUtil.UnitHasVisibilityToTargetFromCurrentPosition(attackerAA, target)) { order = BehaviorTreeResults.BehaviorTreeResultsFromBoolean(false); return(0f); } Mech attackerMech = attackerAA as Mech; float currentHeat = attackerMech == null ? 0f : (float)attackerMech.CurrentHeat; float acceptableHeat = attackerMech == null ? float.MaxValue : AIUtil.GetAcceptableHeatLevelForMech(attackerMech); Mod.Log.Debug?.Write($" heat: current: {currentHeat} acceptable: {acceptableHeat}"); //float weaponToHitThreshold = attackerAA.BehaviorTree.weaponToHitThreshold; // Filter weapons that cannot contribute to the battle CandidateWeapons candidateWeapons = new CandidateWeapons(attackerAA, target); Mech targetMech = target as Mech; bool targetIsEvasive = targetMech != null && targetMech.IsEvasive; List <List <CondensedWeapon> >[] weaponSetsByAttackType = { new List <List <CondensedWeapon> >() { }, new List <List <CondensedWeapon> >() { }, new List <List <CondensedWeapon> >() { } }; // Note: Disabled the evasion fractional checking that Vanilla uses. Should make units more free with ammunition against evasive foes //float evasiveToHitFraction = AIHelper.GetBehaviorVariableValue(attackerAA.BehaviorTree, BehaviorVariableName.Float_EvasiveToHitFloor).FloatVal / 100f; // TODO: Reappropriate BehaviorVariableName.Float_EvasiveToHitFloor as floor for all shots? // Build three sets of sets; ranged, melee, dfa. Each set contains a set of weapons O(n^2) here weaponSetsByAttackType[0] = AEHelper.MakeWeaponSets(candidateWeapons.RangedWeapons); // Evaluate melee attacks string cannotEngageInMeleeMsg = ""; if (attackerMech == null || !attackerMech.CanEngageTarget(target, out cannotEngageInMeleeMsg)) { Mod.Log.Debug?.Write($" attacker cannot melee, or cannot engage due to: '{cannotEngageInMeleeMsg}'"); } else { // Check Retaliation // TODO: Retaliation should consider all possible attackers, not just the attacker // TODO: Retaliation should consider how much damage you do with melee vs. non-melee - i.e. punchbots should probably prefer punching over weak weapons fire // TODO: Should consider if heat would be reduced by melee attack if (AEHelper.MeleeDamageOutweighsRisk(attackerMech, target)) { // Generate base list List <List <CondensedWeapon> > meleeWeaponSets = AEHelper.MakeWeaponSets(candidateWeapons.MeleeWeapons); // Add melee weapons to each set CondensedWeapon cMeleeWeapon = new CondensedWeapon(attackerMech.MeleeWeapon); for (int i = 0; i < meleeWeaponSets.Count; i++) { meleeWeaponSets[i].Add(cMeleeWeapon); } weaponSetsByAttackType[1] = meleeWeaponSets; } else { Mod.Log.Debug?.Write($" potential melee retaliation too high, skipping melee."); } } WeaponHelper.FilterWeapons(attackerAA, target, out List <Weapon> rangedWeps, out List <Weapon> meleeWeps, out List <Weapon> dfaWeps); AttackDetails attackDetails = new AttackDetails(attacker: attackerAA, target: target as AbstractActor, attackPos: attackerAA.CurrentPosition, targetPos: target.CurrentPosition, useRevengeBonus: true); AttackEvaluation rangedAE = RangedCalculator.OptimizeAttack(attackDetails, rangedWeps); AttackEvaluation meleeAE = MeleeCalculator.OptimizeAttack(meleeWeps, attackerAA, target); AttackEvaluation dfaAE = DFACalculator.OptimizeAttack(dfaWeps, attackerAA, target); List <AttackEvaluation> allAttackSolutions = new List <AttackEvaluation>() { rangedAE, meleeAE, dfaAE }; Mod.Log.Debug?.Write(string.Format("found {0} different attack solutions", allAttackSolutions.Count)); // TODO: Apply mode - CleverGirlHelper.ApplyAmmoMode(wep, cWeapon.ammoAndMode); // Find the attack with the best damage across all attacks float bestRangedEDam = 0f; float bestMeleeEDam = 0f; float bestDFAEDam = 0f; for (int m = 0; m < allAttackSolutions.Count; m++) { AttackEvaluation attackEvaluation = allAttackSolutions[m]; Mod.Log.Debug?.Write($"evaluated attack of type {attackEvaluation.AttackType} with {attackEvaluation.WeaponList.Count} weapons, " + $"damage EV of {attackEvaluation.ExpectedDamage}, heat {attackEvaluation.HeatGenerated}"); switch (attackEvaluation.AttackType) { case AIUtil.AttackType.Shooting: bestRangedEDam = Mathf.Max(bestRangedEDam, attackEvaluation.ExpectedDamage); break; case AIUtil.AttackType.Melee: bestMeleeEDam = Mathf.Max(bestMeleeEDam, attackEvaluation.ExpectedDamage); break; case AIUtil.AttackType.DeathFromAbove: bestDFAEDam = Mathf.Max(bestDFAEDam, attackEvaluation.ExpectedDamage); break; default: Debug.Log("unknown attack type: " + attackEvaluation.AttackType); break; } } Mod.Log.Debug?.Write($"best shooting: {bestRangedEDam} melee: {bestMeleeEDam} dfa: {bestDFAEDam}"); //float existingTargetDamageForOverheat = AIHelper.GetBehaviorVariableValue(attackerAA.BehaviorTree, BehaviorVariableName.Float_ExistingTargetDamageForOverheatAttack).FloatVal; AbstractActor targetActor = target as AbstractActor; List <PathNode> meleeDestsForTarget = attackerMech.Pathing.GetMeleeDestsForTarget(targetActor); // LOGIC: Now, evaluate every set of attacks in the list for (int n = 0; n < allAttackSolutions.Count; n++) { AttackEvaluator.AttackEvaluation attackEvaluation2 = allAttackSolutions[n]; Mod.Log.Debug?.Write("------"); Mod.Log.Debug?.Write($"Evaluating attack solution #{n} vs target: {CombatantUtils.Label(targetActor)}"); // TODO: Do we really need this spam? StringBuilder weaponListSB = new StringBuilder(); weaponListSB.Append(" Weapons: ("); foreach (Weapon weapon3 in attackEvaluation2.WeaponList) { weaponListSB.Append("'"); weaponListSB.Append(weapon3.Name); weaponListSB.Append("', "); } weaponListSB.Append(")"); Mod.Log.Debug?.Write(weaponListSB.ToString()); if (attackEvaluation2.WeaponList.Count == 0) { Mod.Log.Debug?.Write("SOLUTION REJECTED - no weapons!"); } // TODO: Does heatGenerated account for jump heat? // TODO: Does not rollup heat! bool willCauseOverheat = attackEvaluation2.HeatGenerated + currentHeat > acceptableHeat; Mod.Log.Debug?.Write($"heat generated: {attackEvaluation2.HeatGenerated} current: {currentHeat} acceptable: {acceptableHeat} willOverheat: {willCauseOverheat}"); if (willCauseOverheat && attackerMech.OverheatWillCauseDeath()) { Mod.Log.Debug?.Write("SOLUTION REJECTED - overheat would cause own death"); continue; } // TODO: Check for acceptable damage from overheat - as per below //bool flag6 = num4 >= existingTargetDamageForOverheat; //Mod.Log.Debug?.Write("but enough damage for overheat attack? " + flag6); //bool flag7 = attackEvaluation2.lowestHitChance >= weaponToHitThreshold; //Mod.Log.Debug?.Write("but enough accuracy for overheat attack? " + flag7); //if (willCauseOverheat && (!flag6 || !flag7)) { // Mod.Log.Debug?.Write("SOLUTION REJECTED - not enough damage or accuracy on an attack that will overheat"); // continue; //} // LOGIC: If we have some damage from an attack, can we improve upon it as a morale / called shot / multi-attack? if (attackEvaluation2.ExpectedDamage > 0f) { BehaviorTreeResults behaviorTreeResults = new BehaviorTreeResults(BehaviorNodeState.Success); // LOGIC: Check for a morale attack (based on available morale) - target must be shutdown or knocked down //CalledShotAttackOrderInfo offensivePushAttackOrderInfo = AEHelper.MakeOffensivePushOrder(attackerAA, attackEvaluation2, target); //if (offensivePushAttackOrderInfo != null) { // behaviorTreeResults.orderInfo = offensivePushAttackOrderInfo; // behaviorTreeResults.debugOrderString = attackerAA.DisplayName + " using offensive push"; //} // LOGIC: Check for a called shot - target must be shutdown or knocked down //CalledShotAttackOrderInfo calledShotAttackOrderInfo = AEHelper.MakeCalledShotOrder(attackerAA, attackEvaluation2, target, false); //if (calledShotAttackOrderInfo != null) { // behaviorTreeResults.orderInfo = calledShotAttackOrderInfo; // behaviorTreeResults.debugOrderString = attackerAA.DisplayName + " using called shot"; //} // LOGIC: Check for multi-attack that will fit within our heat boundaries //MultiTargetAttackOrderInfo multiAttackOrderInfo = MultiAttack.MakeMultiAttackOrder(attackerAA, attackEvaluation2, enemyUnitIndex); //if (!willCauseOverheat && multiAttackOrderInfo != null) { // Multi-attack in RT / BTA only makes sense to: // 1. maximize breaching shot (which ignores cover/etc) if you a single weapon // 2. spread status effects around while firing on a single target // 3. maximizing total damage across N targets, while sacrificing potential damage at a specific target // 3a. Especially with set sof weapons across range brackets, where you can split short-range weapons and long-range weapons // behaviorTreeResults.orderInfo = multiAttackOrderInfo; // behaviorTreeResults.debugOrderString = attackerAA.DisplayName + " using multi attack"; //} AttackOrderInfo attackOrderInfo = new AttackOrderInfo(target) { Weapons = attackEvaluation2.WeaponList, TargetUnit = target }; AIUtil.AttackType attackType = attackEvaluation2.AttackType; List <PathNode> dfaDestinations = attackerAA.JumpPathing.GetDFADestsForTarget(targetActor); if (attackType == AIUtil.AttackType.DeathFromAbove) { attackOrderInfo.IsDeathFromAbove = true; attackOrderInfo.Weapons.Remove(attackerMech.MeleeWeapon); attackOrderInfo.Weapons.Remove(attackerMech.DFAWeapon); attackOrderInfo.AttackFromLocation = attackerMech.FindBestPositionToMeleeFrom(targetActor, dfaDestinations); } else if (attackType == AIUtil.AttackType.Melee) { attackOrderInfo.IsMelee = true; attackOrderInfo.Weapons.Remove(attackerMech.MeleeWeapon); attackOrderInfo.Weapons.Remove(attackerMech.DFAWeapon); attackOrderInfo.AttackFromLocation = attackerMech.FindBestPositionToMeleeFrom(targetActor, meleeDestsForTarget); } behaviorTreeResults.orderInfo = attackOrderInfo; behaviorTreeResults.debugOrderString = $" using attack type: {attackEvaluation2.AttackType} against: {target.DisplayName}"; Mod.Log.Debug?.Write("attack order: " + behaviorTreeResults.debugOrderString); order = behaviorTreeResults; return(attackEvaluation2.ExpectedDamage); } Mod.Log.Debug?.Write("Rejecting attack for not having any expected damage"); } Mod.Log.Debug?.Write("There are no targets I can shoot at without overheating."); order = null; return(0f); }
/// <summary> /// Attempt to kill the primary target. /// If there are not any weapons left over, we're done. /// Use the leftover weapons to try to kill secondary targets. /// If there are weapons left over and no kills to be had, assign one to each evasive target. /// If there are still weapons remaining, distribute randomly. /// </summary> /// <param name="unit"></param> /// <param name="evaluatedAttack"></param> /// <param name="primaryTargetIndex"></param> /// <returns>multi attack order, if possible, or null if a multi-attack doesn't make sense or is not possible</returns> public static MultiTargetAttackOrderInfo MakeMultiAttackOrder(AbstractActor unit, AttackEvaluator.AttackEvaluation evaluatedAttack, int primaryTargetIndex) { if ((unit.MaxTargets <= 1) || (evaluatedAttack.AttackType != AIUtil.AttackType.Shooting)) { // cannot multi-attack return(null); } ICombatant primaryTarget = unit.BehaviorTree.enemyUnits[primaryTargetIndex]; /// indices into unit.BehaviorTree.enemyUnits for secondary targets. List <int> potentialSecondaryTargetIndices = new List <int>(); Dictionary <string, bool> attackGeneratedForTargetGUID = new Dictionary <string, bool>(); for (int i = 0; i < unit.BehaviorTree.enemyUnits.Count; ++i) { ICombatant target = unit.BehaviorTree.enemyUnits[i]; bool isPrimary = (target.GUID == primaryTarget.GUID); attackGeneratedForTargetGUID[target.GUID] = isPrimary; if (isPrimary || (target.IsDead) || unit.VisibilityToTargetUnit(target) != VisibilityLevel.LOSFull) { continue; } // make sure not to permit duplicate targets for (int dupIndex = 0; dupIndex < i; ++dupIndex) { if (unit.BehaviorTree.enemyUnits[dupIndex].GUID == target.GUID) { continue; } } potentialSecondaryTargetIndices.Add(i); } if (potentialSecondaryTargetIndices.Count == 0) { // no other targets available, fall back to doing a single attack return(null); } float overkillThresholdPercentage = unit.BehaviorTree.GetBehaviorVariableValue(BehaviorVariableName.Float_MultiTargetOverkillThreshold).FloatVal; float overkillThresholdFrac = overkillThresholdPercentage / 100.0f; List <Weapon> weaponsToKillPrimaryTarget = PartitionWeaponListToKillTarget(unit, evaluatedAttack.WeaponList, primaryTarget, overkillThresholdFrac); if ((weaponsToKillPrimaryTarget == null) || (weaponsToKillPrimaryTarget.Count == 0)) { // can't kill primary target, so fall back to single attack. return(null); } List <Weapon> weaponsSetAside = ListRemainder(evaluatedAttack.WeaponList, weaponsToKillPrimaryTarget); if ((weaponsSetAside == null) || (weaponsSetAside.Count == 0)) { // can exactly kill the primary target with a single attack, don't bother multiattacking. return(null); } Dictionary <string, List <Weapon> > weaponListsByTargetGUID = new Dictionary <string, List <Weapon> >(); for (int i = 0; i < unit.BehaviorTree.enemyUnits.Count; ++i) { ICombatant target = unit.BehaviorTree.enemyUnits[i]; weaponListsByTargetGUID[target.GUID] = new List <Weapon>(); } // Do the initial allocation to the first enemy unit weaponListsByTargetGUID[primaryTarget.GUID] = weaponsToKillPrimaryTarget; // Now, walk through the potential secondary targets and see if the set aside weapons could kill any of them. // If we find a kill, we want to set aside the weapons necessary for that kill. // if we find multiple kills, pick one, set aside those weapons, and solve again // with the remaining weapons and repeat until we can't find any more kills, or // we use up our number of attacks. // HACK optimization - only go through the list once, which biases our attacks // to prefer index #0. int usedAttacks = 1; for (int secondaryUnitDoubleIndex = 0; secondaryUnitDoubleIndex < potentialSecondaryTargetIndices.Count; ++secondaryUnitDoubleIndex) { if ((usedAttacks == unit.MaxTargets) || (weaponsSetAside.Count == 0)) { break; } int secondaryUnitActualIndex = potentialSecondaryTargetIndices[secondaryUnitDoubleIndex]; ICombatant secondaryUnit = unit.BehaviorTree.enemyUnits[secondaryUnitActualIndex]; if (attackGeneratedForTargetGUID[secondaryUnit.GUID]) { continue; } List <Weapon> weaponsToKillSecondaryTarget = PartitionWeaponListToKillTarget(unit, weaponsSetAside, secondaryUnit, overkillThresholdFrac); if ((weaponsToKillSecondaryTarget == null) || (weaponsToKillSecondaryTarget.Count == 0)) { continue; } // we've found a potential kill weaponsSetAside = ListRemainder(weaponsSetAside, weaponsToKillSecondaryTarget); weaponListsByTargetGUID[secondaryUnit.GUID] = weaponsToKillSecondaryTarget; ++usedAttacks; attackGeneratedForTargetGUID[secondaryUnit.GUID] = true; } // Now, look for targets that have evasive pips that we could strip off. // Only use a single weapon per target for (int secondaryUnitDoubleIndex = 0; secondaryUnitDoubleIndex < potentialSecondaryTargetIndices.Count; ++secondaryUnitDoubleIndex) { int secondaryUnitActualIndex = potentialSecondaryTargetIndices[secondaryUnitDoubleIndex]; ICombatant secondaryUnit = unit.BehaviorTree.enemyUnits[secondaryUnitActualIndex]; if ((usedAttacks == unit.MaxTargets) || (weaponsSetAside.Count == 0)) { break; } if (attackGeneratedForTargetGUID[secondaryUnit.GUID]) { // we already generated an attack for this target continue; } Mech secondaryMech = secondaryUnit as Mech; if ((secondaryMech != null) && (secondaryMech.IsEvasive)) { Weapon stripWeapon = FindWeaponToHitTarget(unit, weaponsSetAside, secondaryMech); if (stripWeapon != null) { List <Weapon> stripList = new List <Weapon>(); stripList.Add(stripWeapon); weaponListsByTargetGUID[secondaryUnit.GUID] = stripList; weaponsSetAside = ListRemainder(weaponsSetAside, stripList); ++usedAttacks; attackGeneratedForTargetGUID[secondaryUnit.GUID] = true; } } } // Now, if we've got extra weapons, let's send them to target 0. if (weaponsSetAside.Count > 0) { weaponListsByTargetGUID[primaryTarget.GUID].AddRange(weaponsSetAside); weaponsSetAside.Clear(); } int targetCount = 0; foreach (string guid in weaponListsByTargetGUID.Keys) { if (attackGeneratedForTargetGUID[guid]) { ++targetCount; Debug.Assert(weaponListsByTargetGUID[guid].Count > 0); } } // if, after all of that, we didn't end up having more than one target, go home and just do a single attack. if (targetCount <= 1) { return(null); } MultiTargetAttackOrderInfo multiTargetOrder = new MultiTargetAttackOrderInfo(); foreach (string guid in weaponListsByTargetGUID.Keys) { if (!attackGeneratedForTargetGUID[guid]) { continue; } ICombatant target = null; for (int i = 0; i < unit.BehaviorTree.enemyUnits.Count; ++i) { ICombatant testTarget = unit.BehaviorTree.enemyUnits[i]; if (testTarget.GUID == guid) { target = testTarget; break; } } Debug.Assert(target != null); if (target == null) { continue; } AttackOrderInfo auxAttackOrder = new AttackOrderInfo(target); for (int wi = 0; wi < weaponListsByTargetGUID[guid].Count; ++wi) { auxAttackOrder.AddWeapon(weaponListsByTargetGUID[guid][wi]); } multiTargetOrder.AddAttack(auxAttackOrder); } if (!ValidateMultiAttackOrder(multiTargetOrder, unit)) { return(null); } return(multiTargetOrder); }