Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Superglue #10

Merged
merged 13 commits into from
Sep 16, 2021
143 changes: 143 additions & 0 deletions Source/Welding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Based on the https://github.com/UmbraSpaceIndustries/Konstruction/tree/master/Source/Konstruction/Konstruction/Welding
// GPLV3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Reflection;
using PreFlightTests;
using TestScripts;

namespace Kaboom
{

public class Welding
{
Vessel vessel;
Part part;
public Welding(Vessel vessel, Part part)
{
this.vessel = vessel;
this.part = part;
}

public void MergeParts(bool compress)
{
if (vessel.rootPart == part)
{
ScreenMessages.PostScreenMessage("You cannot weld the root part!");
return;
}

var wData = LoadWeldingData();
if (wData == null)
return;

PerformWeld(wData, compress);
}

private WeldingData LoadWeldingData(bool silent = false)
{
/**********************
*
* (root)-...-LPA==KGP==LPB
*
* LPA==LPB
*
**********************/

var wData = new WeldingData();
wData.KaboomGluedPart = part;


if (part.attachNodes.Count == 2)
{

wData.LinkedPartA = part.parent;

foreach(var n in part.attachNodes)
if (n.attachedPart != part.parent)
wData.LinkedPartB = n.attachedPart;
}

if (wData.LinkedPartA == null || wData.LinkedPartB == null)
{
if (!silent)
ScreenMessages.PostScreenMessage("This part need to have 2 parts on attachment nodes");
return null;
}

if (wData.KaboomGluedPart == vessel.rootPart)
{
if (!silent)
ScreenMessages.PostScreenMessage("This part is the root part! Cancelling");
return null;
}

return wData;
}


private Vector3 GetOffset(WeldingData wData)
yalov marked this conversation as resolved.
Show resolved Hide resolved
{
var nodeA = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartA, wData.KaboomGluedPart);
var nodeB = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartB, wData.KaboomGluedPart);

Vector3 offset = nodeA.position - nodeB.position;
Debug.Log("offset: " + offset);
return offset;
}

private void PerformWeld(WeldingData wData, bool compress)
{
var nodeA = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartA, wData.KaboomGluedPart);
var nodeB = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartB, wData.KaboomGluedPart);

var offset = GetOffset(wData);


WeldingNodeUtilities.DetachPart(wData.KaboomGluedPart);

WeldingNodeUtilities.SwapLinks(
wData.LinkedPartA,
wData.KaboomGluedPart,
wData.LinkedPartB);

WeldingNodeUtilities.SwapLinks(
wData.LinkedPartB,
wData.KaboomGluedPart,
wData.LinkedPartA);

wData.KaboomGluedPart.SetCollisionIgnores();

WeldingNodeUtilities.SpawnStructures(wData.LinkedPartA, nodeA);
WeldingNodeUtilities.SpawnStructures(wData.LinkedPartB, nodeB);


if (compress)
{
WeldingNodeUtilities.MovePart(wData.LinkedPartB, offset);
}


PartJoint newJoint = PartJoint.Create(
wData.LinkedPartB,
wData.LinkedPartA,
nodeB,
nodeA,
AttachModes.STACK);

wData.LinkedPartB.attachJoint = newJoint;

SoftExplode(wData.KaboomGluedPart);
}

private static void SoftExplode(Part thisPart)
{
thisPart.explosionPotential = 0.1f;
thisPart.explode();
}
}
}
12 changes: 12 additions & 0 deletions Source/WeldingData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Based on the https://github.com/UmbraSpaceIndustries/Konstruction/tree/master/Source/Konstruction/Konstruction/Welding
// GPLV3

namespace Kaboom
{
public class WeldingData
{
public Part LinkedPartA { get; set; }
public Part LinkedPartB { get; set; }
public Part KaboomGluedPart { get; set; }
}
}
85 changes: 85 additions & 0 deletions Source/WeldingNodeUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Based on the https://github.com/UmbraSpaceIndustries/Konstruction/tree/master/Source/Konstruction/Konstruction/Welding
// GPLV3

using System.Linq;
using UnityEngine;
//using USITools;

namespace Kaboom
{
public static class WeldingNodeUtilities
{
public static void SpawnStructures(Part thisPart, AttachNode thisNode)
{
var structList = thisPart.FindModulesImplementing<ModuleStructuralNode>();
foreach (var s in structList.Where(s => s.attachNodeNames.Contains(thisNode.id)))
{
s.SpawnStructure();
}
}

public static void DetachPart(Part thisPart)
{
thisPart.parent = null;
thisPart.attachJoint.DestroyJoint();
thisPart.children.Clear();

foreach (var an in thisPart.attachNodes)
{
an.attachedPart = null;
}
thisPart.topNode.attachedPart = null;
}

public static void MovePart(Part thisPart, Vector3 offset)
{
if (thisPart.Rigidbody != null && thisPart.physicalSignificance == Part.PhysicalSignificance.FULL)
thisPart.transform.position += offset;

thisPart.UpdateOrgPosAndRot(thisPart.vessel.rootPart);

foreach (var p in thisPart.children)
MovePart(p, offset);
}


public static void SwapLinks(Part thisPart, Part oldPart, Part newPart)
{
if (thisPart.parent != null && thisPart.parent == oldPart)
thisPart.parent = newPart;

if(thisPart.topNode != null && thisPart.topNode.attachedPart == oldPart)
thisPart.topNode.attachedPart = newPart;

if (thisPart.attachJoint != null)
{
if (thisPart.attachJoint.Child == oldPart || thisPart.attachJoint.Parent == oldPart)
{
thisPart.attachJoint.DestroyJoint();
}
}

foreach (var an in thisPart.attachNodes.Where(an => an.attachedPart == oldPart))
{
an.attachedPart = newPart;
}

if (!thisPart.children.Contains(oldPart))
return;

thisPart.children.Remove(oldPart);
thisPart.children.Add(newPart);
}

public static float GetPartThickness(Part thisPart)
{
var diff = thisPart.attachNodes[0].position - thisPart.attachNodes[1].position;
return (diff).magnitude;
}

public static AttachNode GetLinkingNode(Part p1, Part p2)
{
return p1.attachNodes.FirstOrDefault(an => an.attachedPart == p2);
}
}
}
47 changes: 41 additions & 6 deletions source/Kaboom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Text;
using UnityEngine;
using KSP.Localization;

namespace Kaboom
{
/// <summary>
Expand All @@ -12,7 +14,7 @@ public class ModuleKaboom : PartModule
{
[KSPField(isPersistant = true, guiActiveEditor = true, guiActive = true, guiName = "Kaboom delay",
groupDisplayName = "<color=red><b>Switch Safety Cover</b></color>", groupName = "Kaboom", groupStartCollapsed = true,
guiUnits = "Seconds"),
guiUnits = " Seconds"),
UI_FloatRange(minValue = 0f, maxValue = 30f, stepIncrement = 1f)]

public float delay = 0;
Expand All @@ -23,6 +25,27 @@ public class ModuleKaboom : PartModule
[KSPField(isPersistant = true)]
public double kaboomTime;

[KSPField(isPersistant = true)]
public bool isGlued = false;

[KSPField(isPersistant = true, guiName = "Superglue", guiActive = true, guiActiveEditor = true, groupName = "Kaboom")]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yalov "Superglue it" love it - keeps up the kampy/cheeky/not taking itself too seriously attitude of KaboOom!

public string gluedText = Localizer.Format("#autoLOC_6001071"); /*Disabled*/


[KSPEvent(guiName = "Toggle Superglue", guiActive = true, guiActiveEditor = true, groupName = "Kaboom", active = true)]
public void GluedEvent()
{
isGlued = !isGlued;
if (isGlued)
{
gluedText = Localizer.Format("#autoLOC_6001072")/*Enabled*/;
}
else
{
gluedText = Localizer.Format("#autoLOC_6001071")/*Disabled*/;
}
}

[KSPEvent(guiActive = true, guiActiveUnfocused = true, unfocusedRange = 5f, guiName = "Kaboom!", groupName = "Kaboom", active = true)]
public void KaboomEvent()
{
Expand All @@ -36,8 +59,7 @@ public void CancelKaboomEvent()
}

[KSPAction("Kaboom!")]
public void KaboomAction(KSPActionParam param)
=> KaboomIt();
public void KaboomAction(KSPActionParam _) => KaboomIt();

private void KaboomIt()
{
Expand All @@ -47,7 +69,7 @@ private void KaboomIt()

if (delay == 0)
{
part.explode();
Proceed();
}
else
{
Expand All @@ -57,6 +79,19 @@ private void KaboomIt()
}
}

private void Proceed()
{
if (isGlued)
{
var k = new Welding(vessel, part);
k.MergeParts(true);
}
else
{
part.explode();
}
}

private void CancelKaboomIt()
{
Events["CancelKaboomEvent"].active = false;
Expand All @@ -73,10 +108,10 @@ public override void OnUpdate()
if (Planetarium.GetUniversalTime() >= kaboomTime)
{
timerActive = false;
part.explode();
Proceed();
}
}
base.OnUpdate();
//base.OnUpdate();
}
}
}