Skip to content

Commit db4e02e

Browse files
committed
Fixed AI integration with manual missile fire
1 parent 36dcf0e commit db4e02e

File tree

4 files changed

+58
-23
lines changed

4 files changed

+58
-23
lines changed

GameData/KCS/Changelog.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
v0.1.2
1+
v0.2.0
22

33
Patch notes:
44

55
- Added a smaller scanner based on the medium ore scanner.
6-
- Tech tree and cargo compatibility added to parts.
76
- Added automatic support for horizontal missile launches via RCS.
7+
- Missiles now point forward based on thrust direction.
8+
- Support for docking ports and radial decouplers as weapon separators.
89
- Added a target prioritisation setting based on a mass range, only applies to targets within 2x max weapons range.
10+
- Tech tree and cargo compatibility added to parts.
911
- Added a setting to toggle the automatic playing of animations on scanners.
1012
- Added a 'Salvo Interval' setting to the ship controller for controlling the time between missile salvos.
1113
- Added a setting to the ship controller to toggle evasion.
1214
- Added a 'Forward Fire Throttle Limit' setting to the ship controller to limit the throttle while missiles are launching from the front of the ship. Zero by default.
1315
- Added a setting for time between firework bursts.
14-
- Support for docking ports and radial decouplers as weapon separators.
15-
- Missiles now point forward based on thrust direction.
1616
- Support for angled thrusters in thrust calculations.
1717
- Improved support for RCS missiles.
1818
- Added savegame settings for gameplay preferences. Currently a retreat override and scanning range multiplers.
@@ -38,5 +38,6 @@ Patch notes:
3838
- Fixed a bug that was preventing firer collision avoidance for missiles.
3939
- Increased minimum value for firework burst spacing above 0.
4040
- Fixed manual fire for rockets.
41+
- Fixed AI integration with manual missile fire.
4142
- Removed Phoenia.
4243
- Sated the snow leopard.

Source/KerbalCombatSystems/MainController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ void OnGUI()
291291
}
292292

293293
private void DrawGUI() =>
294-
windowRect = GUILayout.Window(GUIUtility.GetControlID(FocusType.Passive), windowRect, FillWindow, "Kerbal Combat Systems", GUILayout.Height(0), GUILayout.Width(mode != "Log" ? windowWidth : windowWidth * 1.25f));
294+
windowRect = GUILayout.Window(GUIUtility.GetControlID(FocusType.Passive), windowRect, FillWindow, "KCS Beta v0.2.0", GUILayout.Height(0), GUILayout.Width(mode != "Log" ? windowWidth : windowWidth * 1.25f));
295295

296296
private void FillWindow(int windowID)
297297
{

Source/KerbalCombatSystems/MissileGuidance.cs

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public class ModuleMissile : ModuleWeapon
1616
private Vessel firer;
1717
private float igniteDelay;
1818
private float terminalVelocity;
19-
private ModuleWeaponController targetWeapon;
2019
private bool isInterceptor;
2120
private int shutoffDistance;
21+
private ModuleWeaponController targetWeapon;
2222

2323
// Missile guidance variables.
2424

@@ -34,7 +34,6 @@ public class ModuleMissile : ModuleWeapon
3434
private bool drift;
3535
public float maxAcceleration;
3636
private Vector3 rcs;
37-
private float targetSize;
3837
private Vector3 propulsionVector;
3938

4039
// Components
@@ -44,7 +43,9 @@ public class ModuleMissile : ModuleWeapon
4443
private ModuleWeaponController controller;
4544
private List<ModuleRCSFX> rcsThrusters;
4645
private List<ModuleEngines> engines;
47-
private int partCount = -1;
46+
47+
//private float targetSize;
48+
//private int partCount = -1;
4849

4950
// Debugging line variables.
5051

@@ -53,6 +54,45 @@ public class ModuleMissile : ModuleWeapon
5354

5455
private IEnumerator Launch()
5556
{
57+
// 0. Failsafes for manual fire.
58+
// todo: some of this should probably transferred to the weapon controller.
59+
60+
if (controller.target == null && vessel.targetObject == null)
61+
yield break;
62+
63+
if (controller.target == null)
64+
{
65+
// The missile was fired manually.
66+
67+
target = vessel.targetObject.GetVessel();
68+
controller.target = target;
69+
70+
ModuleShipController firerController = FindController(firer);
71+
if (firerController != null)
72+
{
73+
// Don't require a ship controller and only consider ship-side limitations
74+
// if a ship controller exists. It is probably more fun this way.
75+
76+
controller.side = firerController.side;
77+
78+
if (firerController.maxDetectionRange == 0)
79+
firerController.UpdateDetectionRange();
80+
81+
if (FromTo(vessel, target).magnitude > firerController.maxDetectionRange)
82+
yield break;
83+
}
84+
85+
ModuleShipController targetController = FindController(target);
86+
if (targetController != null && !targetController.incomingWeapons.Contains(controller))
87+
targetController.AddIncoming(controller);
88+
}
89+
else
90+
target = controller.target;
91+
92+
if (!KCSController.weaponsInFlight.Contains(controller))
93+
KCSController.weaponsInFlight.Add(controller);
94+
95+
5696
// 1. Separate from firer.
5797

5898
// find decoupler
@@ -129,9 +169,8 @@ private IEnumerator Launch()
129169

130170
maxThrust = propulsionVector.magnitude;
131171
maxAcceleration = maxThrust / vessel.GetTotalMass();
132-
133-
// Set the in-game target.
134172
vessel.targetObject = target;
173+
shutoffDistance = isInterceptor ? 3 : 10;
135174

136175

137176
// 3. Start moving away from firer.
@@ -300,22 +339,20 @@ private IEnumerator Launch()
300339
interceptLine = KCSDebug.CreateLine(Color.cyan);
301340
thrustLine = KCSDebug.CreateLine(new Color(255f / 255f, 165f / 255f, 0f, 1f)); //orange
302341

303-
// enable autopilot
304-
engageAutopilot = true;
305-
306-
shutoffDistance = isInterceptor ? 3 : 10;
307-
var targetController = FindController(target);
308-
targetSize = targetController != null ? targetController.averagedSize : AveragedSize(target);
309-
partCount = vessel.parts.Count;
310-
342+
// Rename the new vessel.
311343
string oldName = vessel.vesselName;
312344
string missileName = controller.weaponCode == "" ? "Missile" : controller.weaponCode;
313345
string firerName = ShortenName(firer.vesselName);
314346
vessel.vesselName = !isInterceptor ? $"{missileName} ({firerName} >> {ShortenName(target.vesselName)})" : $"Interceptor ({firerName})";
315347
GameEvents.onVesselRename.Fire(new GameEvents.HostedFromToAction<Vessel, string>(vessel, oldName, vessel.vesselName));
316348

349+
engageAutopilot = true;
317350
controller.launched = true;
318351

352+
//var targetController = FindController(target);
353+
//targetSize = targetController != null ? targetController.averagedSize : AveragedSize(target);
354+
//partCount = vessel.parts.Count;
355+
319356
//if (isInterceptor)
320357
//{
321358
// prediction = GameObject.CreatePrimitive(PrimitiveType.Sphere);
@@ -415,9 +452,6 @@ public override void Setup()
415452
{
416453
controller = part.FindModuleImplementing<ModuleWeaponController>();
417454

418-
if (controller.target == null && vessel.targetObject == null) return;
419-
target = controller.target ?? vessel.targetObject.GetVessel();
420-
421455
terminalVelocity = controller.terminalVelocity;
422456
isInterceptor = controller.isInterceptor;
423457
targetWeapon = controller.targetWeapon;

Source/KerbalCombatSystems/ShipController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class ModuleShipController : PartModule
3131
internal KCSFlightController fc;
3232

3333
public Vessel target;
34-
private ModuleShipController targetController;
34+
internal ModuleShipController targetController;
3535

3636
private Coroutine shipControllerCoroutine;
3737
private Coroutine behaviourCoroutine;
@@ -958,7 +958,7 @@ private bool CheckWithdraw()
958958
return Mathf.Abs(RelVel(vessel, nearest.vessel).magnitude) < 200;
959959
}
960960

961-
private void UpdateDetectionRange()
961+
internal void UpdateDetectionRange()
962962
{
963963
var sensors = vessel.FindPartModulesImplementing<ModuleObjectTracking>();
964964

0 commit comments

Comments
 (0)