Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GameData/KSPCommunityPartModules/KSPCommunityPartModules.ckan
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ description: >-

This mod is meant as community project, so feel free to propose additional module ideas by opening an issue, or contribute with a pull request on GitHub.
ksp_version_min: '1.12.3'
depends:
- name: Harmony2
supports:
- name: BoringCrewServices
- name: BluedogDB
9 changes: 7 additions & 2 deletions Source/KSPCommunityPartModules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="KSPBuildTools" Version="0.0.2-alpha.5" />
<PackageReference Include="KSPBuildTools" Version="0.0.2-alpha.7" />
</ItemGroup>
<ItemGroup>
<Compile Include="Modules\ModuleAutoCutDrogue.cs" />
Expand All @@ -41,6 +41,11 @@
<ItemGroup>
<Content Include="HOW_TO_BUILD.txt" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Reference Include="$(KSPRoot)/GameData/000_Harmony/0Harmony.dll">
<Private>False</Private>
<CKANIdentifier>Harmony2</CKANIdentifier>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
72 changes: 58 additions & 14 deletions Source/Modules/ModuleAutoCutDrogue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Originally By: Jsolson
Originally For: Bluedog Design Bureau
*/
using HarmonyLib;
using System;
using System.Linq;
using UnityEngine;

Expand All @@ -22,6 +24,10 @@ class ModuleAutoCutDrogue : PartModule
public bool triggered = false;

private ModuleParachute chute = null;

// Technically this creates an edge case where if two loaded vessels deploy a main parachute at the same time only one of the vessels will cut its drogue chutes,
// but I feel like its unlikely enough to not warrent the effort required to fix it.
private static int lastFrame;
private bool IsChuteDeployed(ModuleParachute pParachute) => pParachute.deploymentState == ModuleParachute.deploymentStates.DEPLOYED
|| pParachute.deploymentState == ModuleParachute.deploymentStates.SEMIDEPLOYED;
public override void OnStart(StartState state)
Expand All @@ -32,33 +38,71 @@ public override void OnStart(StartState state)

Fields[nameof(autoCutDrogue)].guiActive = !isDrogueChute;
Fields[nameof(autoCutDrogue)].guiActiveEditor = !isDrogueChute;

if (!isDrogueChute) ModuleParachuteEvents.OnDeployed += OnParachuteDeployed;
if (!isDrogueChute) ModuleParachuteEvents.OnRepacked += OnParachuteRepacked;
}

public void FixedUpdate()
public void OnDestroy()
{
if (isDrogueChute || chute == null)
{
this.isEnabled = false;
this.enabled = false;
return;
}
ModuleParachuteEvents.OnDeployed -= OnParachuteDeployed;
ModuleParachuteEvents.OnRepacked -= OnParachuteRepacked;
}

if (IsChuteDeployed(chute))
private void OnParachuteDeployed(ModuleParachute pChute)
{
if (autoCutDrogue && !triggered && pChute == chute)
{
if (!triggered)
if (lastFrame != Time.frameCount)
{
var drogues = vessel.FindPartModulesImplementing<ModuleAutoCutDrogue>().Where(d => d.isDrogueChute && d.chute != null);
foreach (ModuleAutoCutDrogue d in drogues)
{
if (IsChuteDeployed(d.chute)) d.chute.CutParachute();
}
triggered = true;
lastFrame = Time.frameCount;
}
triggered = true;
}
else if (chute.deploymentState == ModuleParachute.deploymentStates.STOWED)
{
triggered = false;
}
}

private void OnParachuteRepacked(ModuleParachute pChute)
{
if (pChute == chute && triggered) triggered = false;
}
}

[HarmonyPatch(typeof(ModuleParachute))]
internal class ModuleParachuteEvents
{
public static void ModuleManagerPostLoad()
{
Harmony harmony = new Harmony("KSPCommunityPartModules");
harmony.PatchAll();
}

public static event Action<ModuleParachute> OnDeployed;
public static event Action<ModuleParachute> OnRepacked;

[HarmonyPostfix]
[HarmonyPatch("OnParachuteSemiDeployed")]
static void RaiseEventOnSemiDeployed(ModuleParachute __instance)
{
OnDeployed?.Invoke(__instance);
}

[HarmonyPostfix]
[HarmonyPatch("OnParachuteFullyDeployed")]
static void RaiseEventOnFullyDeployed(ModuleParachute __instance)
{
OnDeployed?.Invoke(__instance);
}

[HarmonyPostfix]
[HarmonyPatch("Repack")]
static void RaiseEventOnRepack(ModuleParachute __instance)
{
OnRepacked?.Invoke(__instance);
}
}
}
Loading