bool TargetInTurretRange(ModuleWeapon weapon, float tolerance) { if(!guardTarget) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log("Checking turret range but no guard target"); } return false; } if(weapon.yawRange == 360) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log("Checking turret range - turret has full swivel"); } return true; } Transform turretTransform = weapon.turretBaseTransform; Vector3 direction = guardTarget.transform.position-turretTransform.position; Vector3 directionYaw = Vector3.ProjectOnPlane(direction, turretTransform.up); Vector3 directionPitch = Vector3.ProjectOnPlane(direction, turretTransform.right); float angleYaw = Vector3.Angle(turretTransform.forward, directionYaw); //float anglePitch = Vector3.Angle(-turret.transform.forward, directionPitch); float signedAnglePitch = Misc.SignedAngle(turretTransform.forward, directionPitch, turretTransform.up); if(Mathf.Abs(signedAnglePitch) > 90) { signedAnglePitch -= Mathf.Sign(signedAnglePitch)*180; } bool withinPitchRange = (signedAnglePitch > weapon.minPitch && signedAnglePitch < weapon.maxPitch); if(angleYaw < (weapon.yawRange/2)+tolerance && withinPitchRange) { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log("Checking turret range - target is INSIDE gimbal limits! signedAnglePitch: " + signedAnglePitch + ", minPitch: " + weapon.minPitch + ", maxPitch: " + weapon.maxPitch); } return true; } else { if(BDArmorySettings.DRAW_DEBUG_LABELS) { Debug.Log("Checking turret range - target is OUTSIDE gimbal limits! signedAnglePitch: " + signedAnglePitch + ", minPitch: " + weapon.minPitch + ", maxPitch: " + weapon.maxPitch + ", angleYaw: " + angleYaw); } return false; } }
void FlyToTargetVessel(FlightCtrlState s, Vessel v) { Vector3 target = v.CoM; MissileLauncher missile = null; Vector3 vectorToTarget = v.transform.position - vesselTransform.position; float distanceToTarget = vectorToTarget.magnitude; if (weaponManager) { missile = weaponManager.currentMissile; if (missile != null) { if (missile.targetingMode == MissileLauncher.TargetingModes.Heat && !weaponManager.heatTarget.exists) { target += v.srf_velocity.normalized * 10; } else { target = MissileGuidance.GetAirToAirFireSolution(missile, v); } if (Vector3.Angle(target - vesselTransform.position, vesselTransform.forward) < 15) { steerMode = SteerModes.Aiming; } } else { ModuleWeapon weapon = weaponManager.currentGun; if (weapon != null) { //target -= 1.30f*weapon.GetLeadOffset(); Vector3 leadOffset = weapon.GetLeadOffset(); float targetAngVel = 1.65f * Vector3.Angle(v.transform.position - vessel.transform.position, v.transform.position + (vessel.srf_velocity) - vessel.transform.position); debugString += "\ntargetAngVel: " + targetAngVel; float magnifier = Mathf.Clamp(targetAngVel, 1.25f, 5); target -= magnifier * leadOffset; float angleToLead = Vector3.Angle(vesselTransform.up, target - vesselTransform.position); if (distanceToTarget < 1600 && angleToLead < 20) { steerMode = SteerModes.Aiming; //steer to aim } } } } float targetDot = Vector3.Dot(vesselTransform.up, v.transform.position - vessel.transform.position); //manage speed when close to enemy float finalMaxSpeed = ((distanceToTarget - 100) / 8) + (float)v.srfSpeed; AdjustThrottle(finalMaxSpeed, true); if ((targetDot < 0 || vessel.srfSpeed > finalMaxSpeed) && distanceToTarget < 800) //distance is less than 800m { debugString += ("\nEnemy on tail. Braking"); AdjustThrottle(minSpeed, true); } if (missile != null && targetDot > 0 && distanceToTarget < 300 && vessel.srfSpeed > 130) { extending = true; lastTargetPosition = v.transform.position; } FlyToPosition(s, target); }
bool CheckAmmo(ModuleWeapon weapon) { string ammoName = weapon.ammoName; foreach(Part p in vessel.parts) { foreach(var resource in p.Resources.list) { if(resource.resourceName == ammoName) { if(resource.amount > 0) { return true; } } } } return false; }