public bool IsCloser(TargetInfo otherTarget, MissileFire myMf) { float thisSqrDist = (position-myMf.transform.position).sqrMagnitude; float otherSqrDist = (otherTarget.position-myMf.transform.position).sqrMagnitude; return thisSqrDist < otherSqrDist; }
void RemoveTarget(TargetInfo target, BDArmorySettings.BDATeams team) { TargetDatabase[team].Remove(target); }
//change to a target info object bool SmartPickWeapon(TargetInfo target, float turretRange) { if(!target) { return false; } if(vessel.Landed) { foreach(var pilot in vessel.FindPartModulesImplementing<BDModulePilotAI>()) { if(pilot) { return false; } } } float distance = Vector3.Distance(transform.position+vessel.srf_velocity, target.position+target.velocity); //take velocity into account (test) if(distance < turretRange || (target.isMissile && distance < turretRange*1.5f)) { if((target.isMissile) && SwitchToLaser()) //need to favor ballistic for ground units { return true; } if(!targetMissiles && !vessel.Landed && target.isMissile) { return false; } if(SwitchToTurret(distance)) { //dont fire on missiles if airborne unless equipped with laser return true; } } if(distance > turretRange || !vessel.Landed) { //missiles if(!target.isLanded) { if(target.isMissile && !vessel.Landed) //don't fire on missiles if airborne { return false; } if(SwitchToAirMissile ()) //Use missiles if available { if(currentMissile.targetingMode == MissileLauncher.TargetingModes.Radar) { foreach(var rd in radars) { if(rd.canLock) { rd.EnableRadar(); break; } } } return true; } return SwitchToTurret(distance); //Long range turrets? } else { return SwitchToGroundMissile(); } } return false; }
bool TryPickAntiRad(TargetInfo target) { CycleWeapon(0); //go to start of array while(true) { CycleWeapon(true); if(selectedWeapon == null) return false; if(selectedWeapon.GetWeaponClass() == WeaponClasses.Missile) { foreach(var ml in selectedWeapon.GetPart().FindModulesImplementing<MissileLauncher>()) { if(ml.targetingMode == MissileLauncher.TargetingModes.AntiRad) { return true; } else { break; } } //return; } } }
bool CrossCheckWithRWR(TargetInfo v) { bool matchFound = false; if(rwr && rwr.rwrEnabled) { for(int i = 0; i < rwr.pingsData.Length; i++) { if(rwr.pingsData[i].exists && (rwr.pingWorldPositions[i] - v.position).magnitude < 20) { matchFound = true; break; } } } return matchFound; }
void SetTarget(TargetInfo target) { if(target) { if(currentTarget) { currentTarget.Disengage(this); } target.Engage(this); currentTarget = target; guardTarget = target.Vessel; } else { if(currentTarget) { currentTarget.Disengage(this); } guardTarget = null; currentTarget = null; } }
//change to a target info object bool SmartPickWeapon(TargetInfo target, float turretRange) { if(!target) { return false; } if(vessel.Landed) { foreach(var pilot in vessel.FindPartModulesImplementing<BDModulePilotAI>()) { if(pilot) { return false; } } } float distance = Vector3.Distance(transform.position+vessel.srf_velocity, target.position+target.velocity); //take velocity into account (test) //distance can be sqr if(distance < turretRange || (target.isMissile && distance < turretRange*1.5f)) { if((target.isMissile) && SwitchToLaser()) //need to favor ballistic for ground units { return true; } if(!targetMissiles && !vessel.Landed && target.isMissile) { return false; } if(SwitchToTurret(distance)) { //dont fire on missiles if airborne unless equipped with laser return true; } } if(distance > turretRange || !vessel.Landed) { //missiles if(!target.isLanded) { if(target.isMissile && !vessel.Landed) //don't fire on missiles if airborne { return false; } return SwitchToAirMissile(); } else { return SwitchToGroundMissile(); } } return false; }
public void SetOverrideTarget(TargetInfo target) { overrideTarget = target; targetScanTimer = -100; //force target update }
void SmartFindTarget() { List<TargetInfo> targetsTried = new List<TargetInfo>(); if (overrideTarget) //begin by checking the override target, since that takes priority { targetsTried.Add(overrideTarget); SetTarget(overrideTarget); if(SmartPickWeapon(overrideTarget, gunRange)) { if (BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging an override target with " + selectedWeapon); } overrideTimer = 15f; //overrideTarget = null; return; } else if (BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging an override target with failed to engage its override target!"); } } overrideTarget = null; //null the override target if it cannot be used //if AIRBORNE, try to engage airborne target first if(!vessel.LandedOrSplashed && !targetMissiles) { if (pilotAI && pilotAI.IsExtending) { TargetInfo potentialAirTarget = BDATargetManager.GetAirToAirTargetAbortExtend(this, 1500, 0.2f); if (potentialAirTarget) { targetsTried.Add(potentialAirTarget); SetTarget(potentialAirTarget); if (SmartPickWeapon(potentialAirTarget, gunRange)) { if (BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is aborting extend and engaging an incoming airborne target with " + selectedWeapon); } return; } } } else { TargetInfo potentialAirTarget = BDATargetManager.GetAirToAirTarget(this); if (potentialAirTarget) { targetsTried.Add(potentialAirTarget); SetTarget(potentialAirTarget); if (SmartPickWeapon(potentialAirTarget, gunRange)) { if (BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging an airborne target with " + selectedWeapon); } return; } } } } TargetInfo potentialTarget = null; //=========HIGH PRIORITY MISSILES============= //first engage any missiles targeting this vessel potentialTarget = BDATargetManager.GetMissileTarget(this, true); if(potentialTarget) { targetsTried.Add(potentialTarget); SetTarget(potentialTarget); if(SmartPickWeapon(potentialTarget, gunRange)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging incoming missile with " + selectedWeapon); } return; } } //then engage any missiles that are not engaged potentialTarget = BDATargetManager.GetUnengagedMissileTarget(this); if(potentialTarget) { targetsTried.Add(potentialTarget); SetTarget(potentialTarget); if(SmartPickWeapon(potentialTarget, gunRange)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging unengaged missile with " + selectedWeapon); } return; } } //=========END HIGH PRIORITY MISSILES============= //============VESSEL THREATS============ if(!targetMissiles) { //then try to engage enemies with least friendlies already engaging them potentialTarget = BDATargetManager.GetLeastEngagedTarget(this); if(potentialTarget) { targetsTried.Add(potentialTarget); SetTarget(potentialTarget); if(!BDArmorySettings.ALLOW_LEGACY_TARGETING) { if(CrossCheckWithRWR(potentialTarget) && TryPickAntiRad(potentialTarget)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging the least engaged radar target with " + selectedWeapon.GetShortName()); } return; } } if(SmartPickWeapon(potentialTarget, gunRange)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging the least engaged target with " + selectedWeapon.GetShortName()); } return; } } //then engage the closest enemy potentialTarget = BDATargetManager.GetClosestTarget(this); if(potentialTarget) { targetsTried.Add(potentialTarget); SetTarget(potentialTarget); if(!BDArmorySettings.ALLOW_LEGACY_TARGETING) { if(CrossCheckWithRWR(potentialTarget) && TryPickAntiRad(potentialTarget)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging the closest radar target with " + selectedWeapon.GetShortName()); } return; } } if(SmartPickWeapon(potentialTarget, gunRange)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging the closest target with " + selectedWeapon.GetShortName()); } return; } /* else { if(SmartPickWeapon(potentialTarget, 10000)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging the closest target with extended turret range (" + selectedWeapon.GetShortName() + ")"); } return; } } */ } } //============END VESSEL THREATS============ //============LOW PRIORITY MISSILES========= //try to engage least engaged hostile missiles first potentialTarget = BDATargetManager.GetMissileTarget(this); if(potentialTarget) { targetsTried.Add(potentialTarget); SetTarget(potentialTarget); if(SmartPickWeapon(potentialTarget, gunRange)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging a missile with " + selectedWeapon.GetShortName()); } return; } } //then try to engage closest hostile missile potentialTarget = BDATargetManager.GetClosestMissileTarget(this); if(potentialTarget) { targetsTried.Add(potentialTarget); SetTarget(potentialTarget); if(SmartPickWeapon(potentialTarget, gunRange)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging a missile with " + selectedWeapon.GetShortName()); } return; } } //==========END LOW PRIORITY MISSILES============= if(targetMissiles)//NO MISSILES BEYOND THIS POINT// { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is disengaging - no valid weapons"); } CycleWeapon(0); SetTarget(null); return; } //if nothing works, get all remaining targets and try weapons against them List<TargetInfo> finalTargets = BDATargetManager.GetAllTargetsExcluding(targetsTried, this); foreach(TargetInfo finalTarget in finalTargets) { SetTarget(finalTarget); if(SmartPickWeapon(finalTarget, gunRange)) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is engaging a final target with " + selectedWeapon.GetShortName()); } return; } } //no valid targets found if(potentialTarget == null || selectedWeapon == null) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log(vessel.vesselName + " is disengaging - no valid weapons"); } CycleWeapon(0); SetTarget(null); if(vesselRadarData && vesselRadarData.locked) { vesselRadarData.UnlockAllTargets(); } return; } Debug.Log ("Unhandled target case."); }
void GuardMode() { if(!gameObject.activeInHierarchy) { return; } if(BDArmorySettings.PEACE_MODE) { return; } if(!BDArmorySettings.ALLOW_LEGACY_TARGETING) { UpdateGuardViewScan(); } //setting turrets to guard mode if(selectedWeapon!=null && selectedWeapon.GetWeaponClass() == WeaponClasses.Gun) { foreach(var weapon in vessel.FindPartModulesImplementing<ModuleWeapon>()) //make this not have to go every frame { if(weapon.part.partInfo.title == selectedWeapon.GetPart().partInfo.title) { weapon.EnableWeapon(); weapon.aiControlled = true; weapon.maxAutoFireCosAngle = vessel.LandedOrSplashed ? 0.9993908f : 0.9975641f; //2 : 4 degrees } } } if(guardTarget) { //release target if out of range if(BDArmorySettings.ALLOW_LEGACY_TARGETING && (guardTarget.transform.position-transform.position).magnitude > guardRange) { SetTarget(null); } } else if(selectedWeapon!=null && selectedWeapon.GetWeaponClass() == WeaponClasses.Gun) { foreach(var weapon in vessel.FindPartModulesImplementing<ModuleWeapon>()) { if(weapon.part.partInfo.title == selectedWeapon.GetPart().partInfo.title) { weapon.autoFire = false; weapon.legacyTargetVessel = null; } } } if(missileIsIncoming) { if(!isLegacyCMing) { StartCoroutine(LegacyCMRoutine()); } targetScanTimer -= Time.fixedDeltaTime; //advance scan timing (increased urgency) } //scan and acquire new target if(Time.time-targetScanTimer > targetScanInterval) { targetScanTimer = Time.time; if(!guardFiringMissile) { SetTarget(null); if(BDArmorySettings.ALLOW_LEGACY_TARGETING) { ScanAllTargets(); } SmartFindTarget(); if(guardTarget == null || selectedWeapon == null) { SetCargoBays(); return; } //firing if(weaponIndex > 0) { if(selectedWeapon.GetWeaponClass() == WeaponClasses.Missile) { bool launchAuthorized = true; bool pilotAuthorized = true;//(!pilotAI || pilotAI.GetLaunchAuthorization(guardTarget, this)); float targetAngle = Vector3.Angle(-transform.forward, guardTarget.transform.position - transform.position); float targetDistance = Vector3.Distance(currentTarget.position, transform.position); MissileLaunchParams dlz = MissileLaunchParams.GetDynamicLaunchParams(currentMissile, guardTarget.srf_velocity, guardTarget.CoM); if(targetAngle > guardAngle / 2) //dont fire yet if target out of guard angle { launchAuthorized = false; } else if(targetDistance > dlz.maxLaunchRange || targetDistance < dlz.minLaunchRange) //fire the missile only if target is further than missiles min launch range { launchAuthorized = false; } if(missilesAway < maxMissilesOnTarget) { if(!guardFiringMissile && launchAuthorized && (pilotAuthorized || !BDArmorySettings.ALLOW_LEGACY_TARGETING)) { StartCoroutine(GuardMissileRoutine()); } } if(!launchAuthorized || !pilotAuthorized || missilesAway >= maxMissilesOnTarget) { targetScanTimer -= 0.5f * targetScanInterval; } } else if(selectedWeapon.GetWeaponClass() == WeaponClasses.Bomb) { if(!guardFiringMissile) { StartCoroutine(GuardBombRoutine()); } } else if(selectedWeapon.GetWeaponClass() == WeaponClasses.Gun || selectedWeapon.GetWeaponClass() == WeaponClasses.Rocket || selectedWeapon.GetWeaponClass() == WeaponClasses.DefenseLaser) { StartCoroutine(GuardTurretRoutine()); } } } SetCargoBays(); } if(overrideTimer > 0) { overrideTimer -= TimeWarp.fixedDeltaTime; } else { overrideTimer = 0; overrideTarget = null; } }
bool SmartPickWeapon(TargetInfo target, float turretRange) { if(!target) { return false; } if(pilotAI && pilotAI.pilotEnabled && vessel.LandedOrSplashed) { return false; } float distance = Vector3.Distance(transform.position+vessel.srf_velocity, target.position+target.velocity); //take velocity into account (test) Debug.Log(vessel.vesselName + " SmartPickWeapon: dist="+distance + ", turrentRange="+turretRange+", targetMIssile="+target.isMissile); if(distance < turretRange || (target.isMissile && distance < turretRange*1.5f)) { if((target.isMissile) && SwitchToLaser()) //need to favor ballistic for ground units { return true; } if(!targetMissiles && !vessel.LandedOrSplashed && target.isMissile) { return false; } if(SwitchToTurret(distance)) { //dont fire on missiles if airborne unless equipped with laser return true; } } if (distance > turretRange || !vessel.LandedOrSplashed) { //missiles if (!target.isLanded) { if (!targetMissiles && target.isMissile && !vessel.LandedOrSplashed) { //don't fire on missiles if airborne return false; } if (SwitchToAirMissile ()) { //Use missiles if available if (currentMissile.targetingMode == MissileLauncher.TargetingModes.Radar) { foreach (var rd in radars) { if (rd.canLock) { rd.EnableRadar (); break; } } } return true; } //return SwitchToTurret(distance); //Long range turrets? return false; } else { if (target.isMissile) { //TRY to pick guns or aa missiles for defense Debug.Log(vessel.vesselName + ": Trying to pick aa missiles for defense..."); if (SwitchToAirMissile ()) { if (currentMissile.targetingMode == MissileLauncher.TargetingModes.Radar) { foreach (var rd in radars) { if (rd.canLock) { rd.EnableRadar (); break; } } } return true; } else SwitchToTurret (distance); } if (target.Vessel.LandedOrSplashed) { if (SwitchToGroundMissile ()) { return true; } else if (SwitchToBomb ()) { return true; } } SwitchToTurret (distance); } } return false; }