Advertisement
Guest User

BoatParts R4_5

a guest
Aug 30th, 2014
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 116.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7.  
  8. [KSPAddon(KSPAddon.Startup.EditorAny, false)]
  9. public class biggerEditor : MonoBehaviour
  10. {
  11. public SPHCamera SPHcam;
  12. public VABCamera VABcam;
  13.  
  14. void Start()
  15. {
  16. if (!HighLogic.LoadedSceneIsFlight)
  17. {
  18. Debug.Log("Checking main cam");
  19. //Debug.Log("assigning sphcam");
  20. SPHcam = Camera.main.GetComponent<SPHCamera>();
  21. //Debug.Log("altering sphcam / " + SPHcam.enabled);
  22. SPHcam.maxHeight = 300f;
  23. SPHcam.maxDisplaceX = 2000f;
  24. SPHcam.maxDisplaceZ = 2000f;
  25.  
  26.  
  27. //Debug.Log("assigning vabcam");
  28. VABcam = Camera.main.GetComponent<VABCamera>();
  29. //Debug.Log("altering vabcam / " + VABcam.enabled);
  30. VABcam.maxHeight = 2000f;
  31.  
  32.  
  33.  
  34.  
  35. EditorLogic[] elogic = (EditorLogic[])FindObjectsOfType(typeof(EditorLogic));
  36. Debug.Log("Found logic: " + elogic.Length);
  37. for (int i = 0; i < elogic.Length; i++)
  38. {
  39. Debug.Log(elogic[i].gameObject.name);
  40. Debug.Log("Bounds" + elogic[i].editorBounds.extents);
  41. elogic[i].editorBounds.extents = new Vector3(1000f, 200f, 1000f);
  42. }
  43. }
  44.  
  45. }
  46. }
  47.  
  48.  
  49. //********************************************
  50.  
  51. using System;
  52. using System.Collections.Generic;
  53. using System.Linq;
  54. using System.Text;
  55. using System.Threading.Tasks;
  56. using UnityEngine;
  57.  
  58.  
  59. class otherBP : PartModule
  60. {
  61. [KSPField]
  62. public string partType = "none";
  63.  
  64. [KSPField]
  65. public float invWaterOffset = 0f;
  66.  
  67. public float crewTimer;
  68. public float crewCoolDown = 10f;
  69.  
  70. public float crushTimer = 1f;
  71. public float crushCoolDown = 15f;
  72.  
  73. //public double partAltitude;
  74.  
  75. //public AudioSource hullStress;
  76.  
  77. public FXGroup crushSound = null;
  78.  
  79. void Start()
  80. {
  81. if (partType == "crewBunk")
  82. {
  83. Transform curtainTopClosed = this.part.FindModelTransform("curtainTopClosed");
  84. curtainTopClosed.GetComponents<MeshRenderer>().FirstOrDefault().enabled = false;
  85. Transform curtainBottomClosed = this.part.FindModelTransform("curtainBottomClosed");
  86. curtainBottomClosed.GetComponents<MeshRenderer>().FirstOrDefault().enabled = false;
  87.  
  88. }
  89.  
  90.  
  91. if (!GameDatabase.Instance.ExistsAudioClip("BoatPartsR3/Sounds/subPressure")) return;
  92. //print("getting past the sound condition 1");
  93.  
  94. crushSound.audio = gameObject.AddComponent<AudioSource>();
  95.  
  96. if (crushSound.audio != null)
  97. {
  98. //print("getting past the sound condition 2");
  99. crushSound.audio.volume = GameSettings.SHIP_VOLUME;
  100. crushSound.audio.rolloffMode = AudioRolloffMode.Logarithmic;
  101. crushSound.audio.dopplerLevel = 0f;
  102. crushSound.audio.panLevel = 1f;
  103. crushSound.audio.clip = GameDatabase.Instance.GetAudioClip("BoatPartsR3/Sounds/subPressure");
  104. crushSound.audio.loop = false;
  105. crushSound.audio.playOnAwake = false;
  106. }
  107.  
  108. }
  109.  
  110. public void FixedUpdate()
  111. {
  112.  
  113. if (!HighLogic.LoadedSceneIsFlight) return;
  114. {
  115. crewTimer -= Time.deltaTime; // we will limit how often this checks/changes drag.
  116. if (crewTimer <= 0f)
  117. {
  118. crewTimer = crewCoolDown;
  119. if (partType == "crewBunk")
  120. {
  121. if (this.part.protoModuleCrew.Count == 1)
  122. {
  123. //must be one guy in there sleeping in the top
  124. Transform curtainTopClosed = this.part.FindModelTransform("curtainTopClosed");
  125. curtainTopClosed.GetComponents<MeshRenderer>().FirstOrDefault().enabled = true;
  126. Transform curtainTopOpen = this.part.FindModelTransform("curtainTopOpen");
  127. curtainTopOpen.GetComponents<MeshRenderer>().FirstOrDefault().enabled = false;
  128. Transform curtainBottomClosed = this.part.FindModelTransform("curtainBottomClosed");
  129. curtainBottomClosed.GetComponents<MeshRenderer>().FirstOrDefault().enabled = false;
  130. Transform curtainBottomOpen = this.part.FindModelTransform("curtainBottomOpen");
  131. curtainBottomOpen.GetComponents<MeshRenderer>().FirstOrDefault().enabled = true;
  132.  
  133. }
  134. else if (this.part.protoModuleCrew.Count == 2)
  135. {
  136. //must be two guys in there sleeping in both spots
  137. Transform curtainTopClosed = this.part.FindModelTransform("curtainTopClosed");
  138. curtainTopClosed.GetComponents<MeshRenderer>().FirstOrDefault().enabled = true;
  139. Transform curtainTopOpen = this.part.FindModelTransform("curtainTopOpen");
  140. curtainTopOpen.GetComponents<MeshRenderer>().FirstOrDefault().enabled = false;
  141. Transform curtainBottomClosed = this.part.FindModelTransform("curtainBottomClosed");
  142. curtainBottomClosed.GetComponents<MeshRenderer>().FirstOrDefault().enabled = true;
  143. Transform curtainBottomOpen = this.part.FindModelTransform("curtainBottomOpen");
  144. curtainBottomOpen.GetComponents<MeshRenderer>().FirstOrDefault().enabled = false;
  145.  
  146. }
  147. else if (this.part.protoModuleCrew.Count == 0)
  148. {
  149. //all empty
  150. Transform curtainTopClosed = this.part.FindModelTransform("curtainTopClosed");
  151. curtainTopClosed.GetComponents<MeshRenderer>().FirstOrDefault().enabled = false;
  152. Transform curtainTopOpen = this.part.FindModelTransform("curtainTopOpen");
  153. curtainTopOpen.GetComponents<MeshRenderer>().FirstOrDefault().enabled = true;
  154. Transform curtainBottomClosed = this.part.FindModelTransform("curtainBottomClosed");
  155. curtainBottomClosed.GetComponents<MeshRenderer>().FirstOrDefault().enabled = false;
  156. Transform curtainBottomOpen = this.part.FindModelTransform("curtainBottomOpen");
  157. curtainBottomOpen.GetComponents<MeshRenderer>().FirstOrDefault().enabled = true;
  158.  
  159. }
  160.  
  161.  
  162.  
  163. }
  164. }
  165.  
  166.  
  167.  
  168. //if (partType == "invertedWater" || partType == "deepWater")
  169. //{
  170. // partAltitude = Vector3.Distance(this.part.collider.transform.position, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  171. // double cameraAltitude = Vector3.Distance(Camera.main.transform.position, FlightGlobals.ActiveVessel.mainBody.position) - (FlightGlobals.ActiveVessel.mainBody.Radius);
  172. // Vector3d thisWayUp = (this.vessel.rigidbody.position - this.vessel.mainBody.position).normalized;
  173.  
  174.  
  175.  
  176. // float distToWater = ((float)this.vessel.altitude + invWaterOffset) * -1f; // try this without the reverser!
  177.  
  178.  
  179.  
  180. // if (cameraAltitude < 0.0d && cameraAltitude > -550.0d && FlightGlobals.ActiveVessel == this.vessel)
  181. // {
  182.  
  183. // Transform activateInvWater = this.part.FindModelTransform("invertWater");
  184. // Transform activateDeepWater = this.part.FindModelTransform("deepWater");
  185.  
  186.  
  187. // Vector3 invertWaterOffset = thisWayUp * distToWater; //new Vector3(distToWater, 0f, 0f);
  188.  
  189. // activateInvWater.transform.position = this.part.transform.position + invertWaterOffset; //set posi for invertwat
  190.  
  191. // activateInvWater.transform.LookAt(vessel.mainBody.position);
  192.  
  193. // activateInvWater.gameObject.SetActive(true);
  194.  
  195. // activateDeepWater.gameObject.SetActive(false);
  196.  
  197.  
  198. // RenderSettings.fog = true;
  199. // RenderSettings.fogColor = new Color(0, 0.1f, 0.25f, 0.75f);
  200. // RenderSettings.fogDensity = 0.0003f;
  201. // RenderSettings.fogStartDistance = 300f;
  202. // RenderSettings.fogEndDistance = 2500f;
  203. // RenderSettings.fogMode = FogMode.ExponentialSquared;
  204.  
  205. // this.part.SetHighlightColor(Color.clear);
  206. // this.part.SetHighlight(false);
  207.  
  208. // Camera.main.clearFlags = CameraClearFlags.Depth;
  209.  
  210.  
  211.  
  212. // }
  213. // else if (cameraAltitude >= 0.0f)
  214. // {
  215. // Transform activateInvWater = this.part.FindModelTransform("invertWater");
  216. // activateInvWater.gameObject.SetActive(false);
  217. // Transform activateDeepvWater = this.part.FindModelTransform("deepWater");
  218. // activateDeepvWater.gameObject.SetActive(false);
  219. // RenderSettings.fog = false;
  220. // Camera.main.clearFlags = CameraClearFlags.Depth;
  221.  
  222.  
  223. // }
  224. //else if (cameraAltitude <= -550.0d && FlightGlobals.ActiveVessel == this.vessel && partAltitude <= -550.0d)
  225. //{
  226.  
  227. // Transform activateInvWater = this.part.FindModelTransform("invertWater");
  228. // activateInvWater.gameObject.SetActive(false);
  229. // Transform activateDeepWater = this.part.FindModelTransform("deepWater");
  230. // activateDeepWater.gameObject.SetActive(true);
  231.  
  232. // activateDeepWater.transform.position = this.part.transform.position; // set posi for deepWat
  233.  
  234. // RenderSettings.fog = false;
  235. // RenderSettings.fogColor = Color.black;
  236. // RenderSettings.fogDensity = 0.005f;
  237. // RenderSettings.fogStartDistance = 300f;
  238. // RenderSettings.fogEndDistance = 400f;
  239. // RenderSettings.fogMode = FogMode.ExponentialSquared;
  240.  
  241. // Camera.main.clearFlags = CameraClearFlags.Depth;
  242.  
  243. // crushTimer -= Time.deltaTime; // we will limit how often this sounds the warning and later checks for damage.
  244. // if (crushTimer <= 0f)
  245. // {
  246. // crushTimer = crushCoolDown;
  247. // crushSound.audio.Play();
  248. // ScreenMessages.PostScreenMessage(new ScreenMessage("Exceeding Safe Hull Pressure", 2f, ScreenMessageStyle.UPPER_CENTER));
  249. // ScreenMessages.PostScreenMessage(new ScreenMessage("Estimated Depth " + Mathf.Round(distToWater) + " m", 4f, ScreenMessageStyle.UPPER_CENTER));
  250. // ScreenMessages.PostScreenMessage(new ScreenMessage("Rate of Climb " + Mathf.Round((float)this.vessel.verticalSpeed) + " m/s", 6f, ScreenMessageStyle.UPPER_CENTER));
  251. // }
  252.  
  253. //}
  254.  
  255.  
  256.  
  257. //}
  258. }
  259.  
  260.  
  261. }
  262.  
  263. }
  264.  
  265.  
  266. //******************************************
  267.  
  268. public class idFloatCode : PartModule
  269. {
  270.  
  271. public Transform buoyancyLever;
  272.  
  273. public Vector3 actionPoint = new Vector3(0f, 0f, 0f);
  274. public Vector3 actionPoint2 = new Vector3(0f, 0f, 0f);
  275. public Vector3 actionPoint3 = new Vector3(0f, 0f, 0f);
  276. public Vector3 actionPoint4 = new Vector3(0f, 0f, 0f);
  277.  
  278. public Vector3 apLift, ap2Lift, ap3Lift, ap4Lift;
  279.  
  280. public double actionPointAltitude, actionPoint2Altitude, actionPoint3Altitude, actionPoint4Altitude; // new!
  281.  
  282. public double hullBottom = 0.0, hullDraught = 0.0;
  283.  
  284. [KSPField(guiActive = false, isPersistant = true)]
  285. public float hullYSize = 0f;
  286.  
  287. [KSPField]
  288. public bool colDetect = false; // off by default. Set in CFG.
  289.  
  290. public float gravityForce = 9.8f;
  291.  
  292. public double partAltitude = 0.0d;
  293.  
  294. [KSPField(guiActive = false, isPersistant = true)]
  295. public float manualDisplacementVolume = 0f; //alternate volume method.
  296.  
  297. [KSPField]
  298. public float manualHullYSize = 0f;// alternate Y size method.
  299.  
  300. public float floatMulti = 0.425f; // or no mulitplier!
  301.  
  302. public float returnToDrag = 0f;
  303.  
  304. [KSPField]
  305. public float floatPowerScale = 1f;//lower for larger ship parts...
  306.  
  307. [KSPField(guiActive = false, isPersistant = true)]
  308. public float objVolume;
  309.  
  310. [KSPField(guiActive = false, isPersistant = true)]
  311. public float restoreToVolume;
  312.  
  313. public bool wasInWater = false;
  314.  
  315. public float lastHitTime = 0f;
  316.  
  317. public float waterImpactTolerance = 1.75f;
  318.  
  319. public bool hullFractured = false;// this is temporary and only affects a floating part - is it sunk?
  320. public float hullExplosionResistance = 150f;// this is the impact speed required for the sink check...
  321. public float impactMass = 0f, collisionForce = 0f;
  322.  
  323. public float startDragTimer = 10f;
  324.  
  325. public float splashTimer = 2f;
  326. public float splashCooldown = 2f;
  327.  
  328. public float dragTimer = 0.5f;
  329. public float dragCoolDown = 0.5f; //affects the time between drag checks and the smoothing between drag values.
  330.  
  331. [KSPField]
  332. public float stabilForce = 10f;
  333.  
  334. [KSPField]
  335. public float stabilOffset = 2f;
  336.  
  337. //public float floatPowerTimer = 0f;
  338. //public float floatPowerCool = 0.125f; //affects the time between water displacement checks and smoothing.
  339.  
  340. public bool splashed = false;
  341.  
  342. //public float setShipTimer = 10f;
  343.  
  344. [KSPField]
  345. public bool floatBypass = false;
  346.  
  347. [KSPField]
  348. public bool bypassACPart = false;
  349.  
  350. [KSPField]
  351. public float floatPartType = 0f; //0 is normal 1 is bow or stern 2 is main hull/float 3 is very small ship 4 is
  352.  
  353. [KSPField]
  354. public bool isMultiPointFloat = false;
  355.  
  356. [KSPField(isPersistant = true, guiActiveEditor = true, guiActive = false, guiName = "Uses WakeFX: ")]
  357. public bool useWakeFX = false;
  358.  
  359. public GameObject wakeFX;
  360.  
  361. public Transform wakeTPoint;
  362.  
  363. public bool initialized = false;
  364.  
  365. private ModuleEngines nukeEngine = new ModuleEngines();
  366.  
  367. [KSPField]
  368. public double floatHeight = 0.0d; //change the level where the part will maintain float
  369.  
  370. [KSPField]
  371. public float coefDragInWater = 0.008f;
  372.  
  373. [KSPField]
  374. public float counterLever = 0.25f;
  375.  
  376. public double waterDisplacement = 0.0; //gets calculated later.
  377.  
  378. [KSPField(guiActive = false, isPersistant = true)]
  379. public bool checkShipOnce = true;
  380.  
  381. [KSPField(guiActive = false, guiName = "Part Volume", isPersistant = true)]
  382. public float thisPartMeshVolume;
  383.  
  384. public float SignedVolumeOfTriangle(Vector3 p1, Vector3 p2, Vector3 p3)
  385. {
  386. float v321 = p3.x * p2.y * p1.z;
  387. float v231 = p2.x * p3.y * p1.z;
  388. float v312 = p3.x * p1.y * p2.z;
  389. float v132 = p1.x * p3.y * p2.z;
  390. float v213 = p2.x * p1.y * p3.z;
  391. float v123 = p1.x * p2.y * p3.z;
  392.  
  393. return (1.0f / 6.0f) * (-v321 + v231 + v312 - v132 - v213 + v123);
  394. }
  395.  
  396. float VolumeOfMesh(Mesh mesh)
  397. {
  398. float volume = 0;
  399.  
  400. Vector3[] vertices = mesh.vertices;
  401. int[] triangles = mesh.triangles;
  402.  
  403. for (int i = 0; i < mesh.triangles.Length; i += 3)
  404. {
  405. Vector3 p1 = vertices[triangles[i + 0]];
  406. Vector3 p2 = vertices[triangles[i + 1]];
  407. Vector3 p3 = vertices[triangles[i + 2]];
  408. volume += SignedVolumeOfTriangle(p1, p2, p3);
  409. }
  410.  
  411. return Mathf.Abs(volume);
  412. }
  413.  
  414.  
  415. public override void OnStart(StartState state)
  416. {
  417. base.OnStart(state);
  418. if (!HighLogic.LoadedSceneIsFlight) return;
  419. {
  420.  
  421. print(this.vessel.parts.Count);
  422.  
  423. foreach (Part makeFCP in this.vessel.parts) // add float code to any docked aircraft and set to acbypass.
  424. {
  425. if (!makeFCP.Modules.Contains("idFloatCode") && !makeFCP.vessel.isEVA && !makeFCP.Modules.Contains("ModuleCommand"))
  426. {
  427. idFloatCode addedFC = makeFCP.gameObject.AddComponent<idFloatCode>();
  428. addedFC.floatBypass = true;
  429. addedFC.coefDragInWater = 6f;
  430. addedFC.bypassACPart = true;
  431. addedFC.waterImpactTolerance = 1.05f;
  432. addedFC.initialized = true;
  433. addedFC.buoyancyLever = this.part.transform;
  434. addedFC.startDragTimer = 10f;
  435.  
  436. if (addedFC != null)
  437. {
  438. print("Added float code to :" + makeFCP.partName + "water impact spd : " + makeFCP.crashTolerance * addedFC.waterImpactTolerance);
  439. }
  440.  
  441.  
  442. }
  443. }
  444.  
  445. initialized = true;
  446. buoyancyLever = this.part.transform;
  447. startDragTimer = 10f;
  448.  
  449. }
  450.  
  451. }
  452.  
  453.  
  454. public void Update()
  455. {
  456. if (!HighLogic.LoadedSceneIsFlight) return;
  457. {
  458. if (part.partBuoyancy != null)
  459. {
  460. Destroy(part.partBuoyancy);
  461. //Debug.Log("Remove Stock Float Nightmare");
  462. }
  463. }
  464.  
  465.  
  466. }
  467.  
  468. public void FixedUpdate()
  469. {
  470. if (!HighLogic.LoadedSceneIsFlight || !initialized) return;
  471. {
  472.  
  473. # region StartDragTimer
  474.  
  475.  
  476. if (startDragTimer > 0f)
  477. {
  478. startDragTimer -= 1f;
  479.  
  480. if (startDragTimer < 1f)
  481. {
  482. startDragTimer = 0f; //must become exactly 0.
  483. }
  484.  
  485.  
  486.  
  487. float smoothDragTarget = coefDragInWater * ((float)hullDraught * 0.5f); // ramp up drag by the amount of hull surface touching the water and the rotational speed.
  488. if (smoothDragTarget > 0f)
  489. {
  490. base.rigidbody.drag = smoothDragTarget; //smooth the adjustment.
  491. print("Load Drag Enabled!");
  492. }
  493. if (this.part.rigidbody != null)
  494. {
  495. this.part.rigidbody.detectCollisions = false;
  496. }
  497. }
  498. else
  499. {
  500. if (this.part.rigidbody != null)
  501. {
  502. this.part.rigidbody.detectCollisions = true;
  503. }
  504. }
  505.  
  506.  
  507. #endregion
  508.  
  509.  
  510. if (this.part.transform != null && this.vessel.mainBody != null && useWakeFX == false)
  511. {
  512. partAltitude = Vector3.Distance(this.part.transform.position, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  513. }
  514. else if (this.part.transform != null && this.vessel.mainBody != null && useWakeFX == true)
  515. {
  516.  
  517. try
  518. {
  519. wakeTPoint = this.part.FindModelTransform("wakePoint");
  520. }
  521. catch
  522. {
  523. print("Problem locating wakePoint");
  524. }
  525.  
  526.  
  527. if (wakeTPoint != null)
  528. {
  529. partAltitude = Vector3.Distance(wakeTPoint.position, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  530. }
  531. else
  532. {
  533. partAltitude = Vector3.Distance(this.part.transform.position, vessel.mainBody.position) - (float)vessel.mainBody.Radius; //use this as backup incase wakeTpoint is null for some reason.
  534. }
  535.  
  536. if (wakeTPoint.position == this.part.transform.position)
  537. {
  538. print("what the hell is going on!");
  539. }
  540. }
  541.  
  542.  
  543.  
  544. #region Float Bypass false
  545. if (floatBypass == false)
  546. {
  547. //#region stabilizer test
  548.  
  549. //if (rigidbody != null)
  550. //{
  551. // rigidbody.AddForceAtPosition(((this.vessel.transform.position - this.vessel.mainBody.position).normalized) * stabilForce, (this.vessel.transform.position - this.vessel.mainBody.position) * stabilOffset);
  552. // rigidbody.AddForceAtPosition((-(this.vessel.transform.position - this.vessel.mainBody.position).normalized) * stabilForce, -(this.vessel.transform.position - this.vessel.mainBody.position) * stabilOffset);
  553. //}
  554.  
  555. //#endregion
  556.  
  557. if (checkShipOnce == true) // persistant.. so only ever runs once per ship instance.
  558. {
  559. if (useWakeFX == true)
  560. {
  561. Debug.Log("Yes this should be using WakeFX!!!");
  562. }
  563.  
  564. if (manualDisplacementVolume == 0f && this.part.collider != null)
  565. {
  566. objVolume = (this.part.collider.bounds.size.y * this.part.collider.bounds.size.x * this.part.collider.bounds.size.z);
  567.  
  568. if (objVolume < 1f)
  569. {
  570. checkMeshVolume();
  571. objVolume = thisPartMeshVolume;
  572. }
  573.  
  574.  
  575.  
  576.  
  577. }
  578. else if (manualDisplacementVolume != 0f)
  579. {
  580. objVolume = manualDisplacementVolume;
  581. }
  582.  
  583.  
  584. restoreToVolume = objVolume;
  585.  
  586. if (manualHullYSize == 0f)
  587. {
  588. hullYSize = 3f;
  589.  
  590. }
  591. else if (manualHullYSize != 0f)
  592. {
  593. hullYSize = manualHullYSize;
  594. }
  595.  
  596.  
  597. checkShipOnce = false;
  598. }
  599.  
  600. Vector3d thisWayUp = (this.vessel.transform.position - this.vessel.mainBody.position).normalized;
  601.  
  602. dragTimer -= Time.deltaTime; // we will limit how often this checks/changes drag.
  603. if (dragTimer <= 0f)
  604. {
  605. dragTimer = dragCoolDown;
  606. if (FlightGlobals.ActiveVessel == this.vessel && this.part.WaterContact == true)
  607. {
  608. float smoothDragTarget = coefDragInWater * ((float)hullDraught * 0.0005f); // ramp up drag by the amount of hull surface touching the water and the rotational speed.
  609. if (smoothDragTarget > 0f)
  610. {
  611. base.rigidbody.drag = smoothDragTarget; //smooth the adjustment.
  612. //print(smoothDragTarget);
  613. }
  614. else if (smoothDragTarget <= 0f)
  615. {
  616. base.rigidbody.drag = coefDragInWater;
  617. }
  618.  
  619. }
  620. else if (FlightGlobals.ActiveVessel != this.vessel && this.part.WaterContact == true && base.rigidbody != null)
  621. {
  622. float smoothDragTarget = coefDragInWater + ((float)hullDraught * 0.75f) + ((float)this.vessel.verticalSpeed * 2.75f); //carrier as stationary.
  623. if (smoothDragTarget > 0f) base.rigidbody.drag = smoothDragTarget;
  624. else if (smoothDragTarget <= 0f) base.rigidbody.drag = coefDragInWater + 0.75f;
  625.  
  626. }
  627. }
  628.  
  629. if (hullYSize < 1f)
  630. {
  631. hullYSize = 1f;
  632. }
  633. if (objVolume < 1f)
  634. {
  635. print("WARNING!!! " + this.part.partName + " is not calculating any volume, a base of 1m3 has been assigned.");
  636. objVolume = 1f;
  637. }
  638.  
  639. hullBottom = partAltitude - (partAltitude - (hullYSize * 0.5d)); //alt of the bottom of the ship
  640. hullDraught = (partAltitude - hullBottom) * -1f;//reverse it
  641. if (hullDraught > hullYSize) hullDraught = hullYSize;//the max draught is the y height of the float collider.
  642. waterDisplacement = (double)objVolume * (hullDraught / hullYSize); //displacement is proportionate to the part of the float under water.
  643.  
  644.  
  645. if (floatPartType == 2)
  646. {
  647. try // Find the transform from the unity setup.
  648. {
  649. buoyancyLever = this.part.FindModelTransform("buoyPoint"); //placed gameobject in part - simply the point where force is applied.
  650. actionPoint = buoyancyLever.position;
  651. }
  652. catch
  653. {
  654. actionPoint = this.part.CenterOfBuoyancy;
  655. //Debug.Log("couldn't find custom buoyPoint in model! using part center.");
  656. }
  657.  
  658. if (partAltitude <= floatHeight)//floatPartType 2 uses the floatHeight variable to set float level... where Type 1 and 3 are preset.
  659. {
  660.  
  661. Vector3 uplift = thisWayUp * ((((waterDisplacement * floatPowerScale) * floatMulti)) - this.vessel.verticalSpeed);
  662. this.part.rigidbody.AddForceAtPosition(uplift, actionPoint);
  663.  
  664. }
  665. else if (partAltitude > 0.0d)
  666. {
  667. Vector3 uplift = -thisWayUp * (gravityForce);
  668. this.part.rigidbody.AddForceAtPosition(uplift, this.part.transform.position);
  669. }
  670. }
  671. else if (floatPartType == 3) // small craft designation
  672. {
  673. try // Find the transform from the unity setup.
  674. {
  675. buoyancyLever = this.part.FindModelTransform("buoyPoint"); //placed gameobject in part - simply the point where force is applied.
  676. actionPoint = buoyancyLever.position;
  677. }
  678. catch
  679. {
  680. actionPoint = this.part.CenterOfBuoyancy;
  681. //Debug.Log("couldn't find custom buoyPoint in model! using part center.");
  682. }
  683.  
  684. if (partAltitude <= -0.125d)
  685. {
  686. Vector3 uplift = thisWayUp * ((((waterDisplacement * floatPowerScale) * floatMulti)) - this.vessel.verticalSpeed);
  687. this.part.rigidbody.AddForceAtPosition(uplift, actionPoint);
  688. }
  689. else if (partAltitude > -0.125d)
  690. {
  691. Vector3 uplift = -thisWayUp * (gravityForce);
  692. this.part.rigidbody.AddForceAtPosition(uplift, this.part.rigidbody.transform.position);
  693. }
  694. }
  695. else if (floatPartType == 4 || isMultiPointFloat == true)//4 is a multifloat part with named colliders 'levelCollider1 or 2 etc.' edit... multi-point bool allowing other part types to take advantage of this.
  696. {
  697. try // Find the transform from the unity setup.
  698. {
  699. buoyancyLever = this.part.FindModelTransform("levelCollider1"); //placed gameobject in part - simply the point where force is applied.
  700. actionPoint = buoyancyLever.position;
  701. actionPointAltitude = Vector3.Distance(actionPoint, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  702. }
  703. catch
  704. {
  705. actionPoint = this.part.CenterOfBuoyancy;
  706. Debug.Log("couldn't find levelCollider1 in model! using part center.");
  707. }
  708.  
  709. try // Find the transform from the unity setup.
  710. {
  711. buoyancyLever = this.part.FindModelTransform("levelCollider2"); //placed gameobject in part - simply the point where force is applied.
  712. actionPoint2 = buoyancyLever.position;
  713. actionPoint2Altitude = Vector3.Distance(actionPoint2, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  714. }
  715. catch
  716. {
  717. actionPoint2 = this.part.CenterOfBuoyancy;
  718. Debug.Log("couldn't find levelCollider2 in model! using part center.");
  719. }
  720. try // Find the transform from the unity setup.
  721. {
  722. buoyancyLever = this.part.FindModelTransform("levelCollider3"); //placed gameobject in part - simply the point where force is applied.
  723. actionPoint3 = buoyancyLever.position;
  724. actionPoint3Altitude = Vector3.Distance(actionPoint3, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  725. }
  726. catch
  727. {
  728. actionPoint3 = this.part.CenterOfBuoyancy;
  729. //Debug.Log("couldn't find levelCollider3 in model! using part center.");
  730. }
  731.  
  732. try // Find the transform from the unity setup.
  733. {
  734. buoyancyLever = this.part.FindModelTransform("levelCollider4"); //placed gameobject in part - simply the point where force is applied.
  735. actionPoint4 = buoyancyLever.position;
  736. actionPoint4Altitude = Vector3.Distance(actionPoint4, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  737. }
  738. catch
  739. {
  740. actionPoint4 = this.part.CenterOfBuoyancy;
  741. //Debug.Log("couldn't find levelCollider4 in model! using part center.");
  742. }
  743.  
  744. if (floatPartType != 1)//not a bow or stern
  745. {
  746. if (actionPointAltitude < 0.0d)
  747. {
  748. double onePortion = ((-actionPointAltitude + 1.0) * 2f); // if the corner is higher than center it will return a lower multiplier.
  749. if (onePortion < 0.0) onePortion = 0.0;
  750. if (onePortion > 5.0) onePortion = 5.0;
  751. apLift = thisWayUp * (((((waterDisplacement * floatPowerScale) * (float)onePortion)) - (this.vessel.verticalSpeed * 0.1f)) * 0.25f);
  752. //this.part.rigidbody.AddForceAtPosition(apLift, actionPoint);
  753. }
  754. else if (actionPointAltitude > 0.0d) // this is downward force on the whole part if it's center is over waterlevel.
  755. {
  756. Vector3 uplift = -thisWayUp * (gravityForce * ((this.part.mass * 10f) * 0.25f));
  757. this.part.rigidbody.AddForceAtPosition(uplift, actionPoint);
  758. }
  759.  
  760. if (actionPoint2Altitude < 0.0d)
  761. {
  762. double twoPortion = ((-actionPoint2Altitude + 1.0) * 2f); // if the corner is higher than center it will return a lower multiplier.
  763. if (twoPortion < 0.0) twoPortion = 0.0;
  764. if (twoPortion > 5.0) twoPortion = 5.0;
  765. ap2Lift = thisWayUp * (((((waterDisplacement * floatPowerScale) * (float)twoPortion)) - (this.vessel.verticalSpeed * 0.1f)) * 0.25f);
  766. //this.part.rigidbody.AddForceAtPosition(ap2Lift, actionPoint2);
  767. }
  768. else if (actionPoint2Altitude > 0.0d) // this is downward force on the whole part if it's center is over waterlevel.
  769. {
  770. Vector3 uplift = -thisWayUp * (gravityForce * ((this.part.mass * 10f) * 0.25f));
  771. this.part.rigidbody.AddForceAtPosition(uplift, actionPoint2);
  772. }
  773.  
  774. if (actionPoint3Altitude < 0.0d)
  775. {
  776. double threePortion = ((-actionPoint3Altitude + 1.0) * 2f); // if the corner is higher than center it will return a lower multiplier.
  777. if (threePortion < 0.0) threePortion = 0.0;
  778. if (threePortion > 5.0) threePortion = 5.0;
  779. ap3Lift = thisWayUp * (((((waterDisplacement * floatPowerScale) * (float)threePortion)) - (this.vessel.verticalSpeed * 0.1f)) * 0.25f);
  780. //this.part.rigidbody.AddForceAtPosition(ap3Lift, actionPoint3);
  781.  
  782. }
  783. else if (actionPoint3Altitude > 0.0d) // this is downward force on the whole part if it's center is over waterlevel.
  784. {
  785. Vector3 uplift = -thisWayUp * (gravityForce * ((this.part.mass * 10f) * 0.25f));
  786. this.part.rigidbody.AddForceAtPosition(uplift, actionPoint3);
  787. }
  788.  
  789. if (actionPoint4Altitude < 0.0d)
  790. {
  791. double fourPortion = ((-actionPoint4Altitude + 1.0) * 2f); // if the corner is higher than center it will return a lower multiplier.
  792. if (fourPortion < 0.0) fourPortion = 0.0;
  793. if (fourPortion > 5.0) fourPortion = 5.0;
  794. ap4Lift = thisWayUp * (((((waterDisplacement * floatPowerScale) * (float)fourPortion)) - (this.vessel.verticalSpeed * 0.1f)) * 0.25f);
  795. //this.part.rigidbody.AddForceAtPosition(ap4Lift, actionPoint4);
  796. }
  797. else if (actionPoint4Altitude > 0.0d) // this is downward force on the corner part mass if this corner is over waterlevel.
  798. {
  799. Vector3 uplift = -thisWayUp * (gravityForce * ((this.part.mass * 10f) * 0.25f));
  800. this.part.rigidbody.AddForceAtPosition(uplift, actionPoint4);
  801. }
  802.  
  803. if (partAltitude < 0.0d)
  804. {
  805. this.part.rigidbody.AddForceAtPosition(apLift, actionPoint);
  806. this.part.rigidbody.AddForceAtPosition(ap2Lift, actionPoint2);
  807. this.part.rigidbody.AddForceAtPosition(ap3Lift, actionPoint3);
  808. this.part.rigidbody.AddForceAtPosition(ap4Lift, actionPoint4);
  809.  
  810. }
  811. if (partAltitude >= 0.0d) // this is downward force on the whole part mass if it's center is at or over the waterlevel.
  812. {
  813. Vector3 uplift = -thisWayUp * (gravityForce * (this.part.mass * 10f)); //orig
  814.  
  815. this.part.rigidbody.AddForceAtPosition(uplift, this.part.rigidbody.transform.position); //2nd orig
  816. //print("Applying all part gravity boost");
  817. }
  818.  
  819. }
  820. else if (floatPartType == 1)// nested - even the multi-point bow or stern needs offset.
  821. {
  822. if (partAltitude <= 0.0d)
  823. {
  824.  
  825. Vector3 uplift = thisWayUp * ((((waterDisplacement * floatPowerScale) * floatMulti)) - this.vessel.verticalSpeed);
  826. this.part.rigidbody.AddForceAtPosition(uplift, actionPoint);
  827. }
  828. if (partAltitude <= 0.0d)
  829. {
  830. Vector3 uplift = thisWayUp * ((((waterDisplacement * floatPowerScale) * floatMulti)) - this.vessel.verticalSpeed);
  831. this.part.rigidbody.AddForceAtPosition(uplift, actionPoint2);
  832. }
  833. else if (partAltitude > 0.5d)
  834. {
  835.  
  836. Vector3 uplift = -thisWayUp * (gravityForce * (this.part.mass * 10f));
  837. this.part.rigidbody.AddForceAtPosition(uplift, this.part.rigidbody.transform.position);
  838. }
  839. }
  840.  
  841.  
  842. }
  843. else if (floatPartType == 1 && isMultiPointFloat == false)//the single point bow
  844. {
  845. try // Find the transform from the unity setup.
  846. {
  847. buoyancyLever = this.part.FindModelTransform("buoyPoint"); //placed gameobject in part - simply the point where force is applied.
  848. actionPoint = buoyancyLever.position;
  849. }
  850. catch
  851. {
  852. actionPoint = this.part.CenterOfBuoyancy;
  853. //Debug.Log("couldn't find custom buoyPoint in model! using part center.");
  854. }
  855.  
  856. if (partAltitude <= 0.0d)
  857. {
  858. Vector3 uplift = thisWayUp * ((((waterDisplacement * floatPowerScale) * floatMulti) - this.part.rigidbody.velocity.y) - this.vessel.verticalSpeed);
  859. this.part.rigidbody.AddForceAtPosition(uplift, actionPoint);
  860. }
  861. else if (partAltitude > 0.0d)
  862. {
  863. Vector3 uplift = -thisWayUp * (gravityForce);
  864. this.part.rigidbody.AddForceAtPosition(uplift, this.part.rigidbody.transform.position);
  865. }
  866. }
  867.  
  868.  
  869. }
  870. #endregion
  871.  
  872. if (floatBypass == true)
  873. {
  874.  
  875. BypassSplashed();
  876.  
  877. }
  878.  
  879.  
  880. #region Water Splash and Contact
  881.  
  882. if (partAltitude <= 0.0d)// && this.vessel.altitude < 20.0d)
  883. {
  884. # region WakeFX
  885. if (useWakeFX == true)
  886. {
  887.  
  888.  
  889. if (wakeFX == null)
  890. {
  891. //setWakeFX(); //pick the right effect - right now just one
  892.  
  893. try
  894. {
  895. wakeFX = GameDatabase.Instance.GetModel("BoatPartsR5/Effects/IDWakeFX/model");
  896. }
  897. catch
  898. {
  899. print("Problem locating wakeFX");
  900. }
  901.  
  902. if (wakeFX != null)
  903. {
  904.  
  905. wakeFX.SetActive(true);
  906. if (wakeTPoint != null)
  907. {
  908. wakeFX.transform.position = wakeTPoint.position;
  909. wakeFX.transform.parent = wakeTPoint;
  910. wakeFX.transform.LookAt(-wakeTPoint.transform.right);
  911. }
  912. else
  913. {
  914. wakeFX.transform.position = this.part.transform.position;
  915. wakeFX.transform.parent = this.part.transform;
  916. wakeFX.transform.LookAt(-this.part.transform.right);
  917. }
  918. wakeFX.gameObject.layer = 0;
  919.  
  920.  
  921. }
  922.  
  923. }
  924.  
  925. if (this.vessel.horizontalSrfSpeed < 5f)
  926. {
  927.  
  928. KSPParticleEmitter findKspEmitter = wakeFX.GetComponentInChildren<KSPParticleEmitter>();
  929. if (findKspEmitter != null)
  930. {
  931. findKspEmitter.enabled = false;
  932. findKspEmitter.emit = false;
  933. //print("Found an KSPEmitter");
  934. }
  935.  
  936.  
  937. }
  938. else
  939. {
  940. KSPParticleEmitter findKspEmitter = wakeFX.GetComponentInChildren<KSPParticleEmitter>();
  941. if (findKspEmitter != null)
  942. {
  943. findKspEmitter.enabled = true;
  944. findKspEmitter.emit = true;
  945. //print("Found an KSPEmitter");
  946. findKspEmitter.transform.rotation = Quaternion.identity;
  947. findKspEmitter.sizeGrow = (float)this.vessel.horizontalSrfSpeed * 0.0025f;
  948.  
  949.  
  950. }
  951. }
  952.  
  953. }
  954. # endregion
  955. //print("check1");
  956.  
  957. splashed = true;
  958. wasInWater = true;
  959. if (this.part != null)
  960. {
  961. this.part.WaterContact = true;
  962. this.part.vessel.Splashed = true;
  963. }
  964.  
  965. //print("check2");
  966. PartResourceList resourceList = this.part.Resources;
  967. foreach (PartResource resource in resourceList)
  968. {
  969. if (resource.resourceName == "IntakeAir" && this.part.vessel.IsControllable)
  970. {
  971. resource.amount = 0.0;
  972. //print("Found Intake Air - flooding with water!");
  973. }
  974. }
  975. //print("check3");
  976. foreach (Part checkPart in this.vessel.parts) // add float code to any docked aircraft and set to bypass.
  977. {
  978. if (checkPart.Modules.Contains("ModuleResourceIntake") && checkPart.vessel.IsControllable)
  979. {
  980. ModuleResourceIntake floodIntake = checkPart.FindModulesImplementing<ModuleResourceIntake>().FirstOrDefault();
  981. if (floodIntake != null && floodIntake.resourceName == "IntakeAir")
  982. {
  983. floodIntake.isEnabled = false;
  984. floodIntake.intakeEnabled = false;
  985. floodIntake.enabled = false;
  986. }
  987.  
  988. }
  989. }
  990. //print("check4");
  991. if (this.part.rigidbody != null)
  992. {
  993. float checkWImpact = this.part.crashTolerance * this.waterImpactTolerance;
  994. if ((float)this.vessel.horizontalSrfSpeed > checkWImpact || this.part.rigidbody.velocity.magnitude > checkWImpact) // && startDragTimer <= 0f
  995. {
  996. //print("Part splashed down at " + this.part.rigidbody.velocity.magnitude);
  997. GameEvents.onCrashSplashdown.Fire(new EventReport(FlightEvents.SPLASHDOWN_CRASH, this.part, this.part.partInfo.title, "ocean", 0, "Terminal Water Impact!!!"));
  998. if (this.part.explosionPotential >= 0.5f)
  999. {
  1000. FXMonger.Explode(this.part, this.part.transform.position, this.part.explosionPotential);
  1001. }
  1002. else
  1003. {
  1004. FXMonger.Splash(this.part.transform.position, this.part.rigidbody.velocity.magnitude / 75f);
  1005. }
  1006. this.part.Die();
  1007.  
  1008. return;
  1009. }
  1010. }
  1011.  
  1012.  
  1013.  
  1014. splashTimer -= Time.deltaTime;
  1015. if (splashTimer <= 0f)
  1016. {
  1017. splashTimer = splashCooldown;
  1018. if (this.part.vessel.rootPart.rigidbody != null)
  1019. {
  1020. if (this.part.vessel.rootPart.rigidbody.velocity.magnitude > 50f)
  1021. {
  1022. if (Vector3.Distance(base.transform.position, FlightGlobals.camera_position) < 50f)
  1023. {
  1024. FXMonger.Splash(base.transform.position, base.rigidbody.velocity.magnitude / 100f);
  1025.  
  1026. }
  1027. }
  1028. }
  1029. }
  1030. //print("check6");
  1031.  
  1032. }
  1033. else
  1034. {
  1035. if (partAltitude > 0.0d && wasInWater == true)
  1036. {
  1037. //wakeFX = null;
  1038. splashed = false;
  1039. if (this.part != null)
  1040. {
  1041. this.part.WaterContact = false;
  1042. this.part.vessel.Splashed = false;
  1043. this.part.vessel.checkSplashed();
  1044. if (this.part.rigidbody != null)
  1045. {
  1046. this.part.rigidbody.drag = 0.0001f;
  1047. returnToDrag = 0.0001f;
  1048. }
  1049. }
  1050. }
  1051. }
  1052. # endregion
  1053.  
  1054. //if (this.vessel.horizontalSrfSpeed < 1f) wakeFX = null;
  1055.  
  1056. }
  1057.  
  1058.  
  1059. }
  1060.  
  1061. public void BypassSplashed()
  1062.  
  1063. {
  1064.  
  1065.  
  1066. if (this.partAltitude <= 0.0d)
  1067. {
  1068. if (this.part.rigidbody != null)
  1069. {
  1070. this.part.rigidbody.drag = coefDragInWater;
  1071. }
  1072.  
  1073.  
  1074. }
  1075. else
  1076. {
  1077.  
  1078. if (this.part.rigidbody != null)
  1079. {
  1080. this.part.rigidbody.drag = returnToDrag;
  1081. if (bypassACPart == false)
  1082. {
  1083. this.part.rigidbody.drag = coefDragInWater * 2.25f;
  1084. waterImpactTolerance = 1.7f;
  1085. }
  1086. else
  1087. {
  1088. waterImpactTolerance = 1.05f;
  1089. }
  1090. }
  1091. }
  1092. }
  1093.  
  1094.  
  1095. public void ApplyDamage(float damageState)
  1096. {
  1097.  
  1098. if (damageState < 1f)//normal/fixed state.... floats
  1099. {
  1100. //this.floatBypass = false;
  1101. this.objVolume = restoreToVolume * damageState; // should reduce float ability as it gets damaged.
  1102. }
  1103. if (damageState == 1f)//normal/fixed state.... floats
  1104. {
  1105. checkShipOnce = true;
  1106. }
  1107. //print("idFloatCode: Setting damage state to " + damageState + " object volume is now " + objVolume);
  1108. }
  1109.  
  1110.  
  1111. public void checkMeshVolume()
  1112. {
  1113. float runningTotal = 0f;
  1114. MeshFilter[] getAllMeshes = base.part.GetComponentsInChildren<MeshFilter>();
  1115. foreach (MeshFilter mF in getAllMeshes)
  1116. {
  1117. if (mF != null)
  1118. {
  1119. runningTotal += VolumeOfMesh(mF.sharedMesh);
  1120. }
  1121. }
  1122.  
  1123. //Mesh mesh = this.part.GetComponentInChildren<MeshFilter>().sharedMesh;
  1124. //thisPartMeshVolume = VolumeOfMesh(mesh);
  1125.  
  1126. thisPartMeshVolume = runningTotal;
  1127.  
  1128. //print("Backup method: The mesh volume of the " + this.part.partName + " mesh is " + thisPartMeshVolume + " m3");
  1129.  
  1130. }
  1131.  
  1132.  
  1133.  
  1134.  
  1135. }
  1136.  
  1137. //****************************************************
  1138.  
  1139. public class neutralBuoy : PartModule
  1140. {
  1141. [KSPField(guiActive = true, guiName = "Auto Buoy Active")]
  1142. public bool autoNeutralOn = false;
  1143.  
  1144. public string verticalDirection = "neutral";
  1145. public bool madeAdjustment = false;
  1146. public float adjTimer = 1f;
  1147. public float adjTimerCooldown = 1f;
  1148. public int autoNMode = 1;
  1149.  
  1150. [KSPEvent(name = "ActivateAutoNeutralBuoy", active = true, guiActive = true, guiName = "Activate Auto Neutral")]
  1151. public void doAutoNeutral()
  1152. {
  1153. if (verticalDirection != "neutral")//if it's already neutral don't activate.
  1154. {
  1155. autoNeutralOn = true; // this enables the process. - should shut off automatically when it reaches neutral status.
  1156. }
  1157. }
  1158.  
  1159. [KSPAction("Auto Neutral Buoyancy")]
  1160. public void toggleTheAutoNeutralSystem(KSPActionParam param)
  1161. {
  1162. doAutoNeutral();
  1163. }
  1164.  
  1165.  
  1166. public void FixedUpdate()
  1167. {
  1168. if (!HighLogic.LoadedSceneIsFlight) return;
  1169. {
  1170. adjTimer -= Time.deltaTime;
  1171. if (adjTimer <= 0f)
  1172. {
  1173. adjTimer = adjTimerCooldown;//reset timer
  1174.  
  1175. if (this.vessel.verticalSpeed > 0.01d)//find the direction
  1176. {
  1177. verticalDirection = "up";
  1178. }
  1179. else if (this.vessel.verticalSpeed < -0.01d)
  1180. {
  1181. verticalDirection = "down";
  1182. }
  1183. else if (this.vessel.verticalSpeed < 0.01d && this.vessel.verticalSpeed > -0.01d)
  1184. {
  1185. verticalDirection = "neutral";
  1186. autoNeutralOn = false; // shut off the process.
  1187. }
  1188.  
  1189. madeAdjustment = false;
  1190. }
  1191.  
  1192.  
  1193.  
  1194.  
  1195. if (verticalDirection != "neutral" && autoNeutralOn == true && madeAdjustment == false)
  1196. {
  1197. if (verticalDirection == "up")
  1198. {
  1199. PartResourceList resourceList = this.part.Resources;
  1200. foreach (PartResource resource in resourceList)
  1201. {
  1202. if (resource.resourceName == "WaterBallast")
  1203. {
  1204. if (vessel.verticalSpeed > 1.0d)
  1205. {
  1206. resource.amount = resource.amount + 1.5d;
  1207. //print("Found WaterBallast - adding this resource!");
  1208. }
  1209. else if (vessel.verticalSpeed < 1.0d)
  1210. {
  1211. resource.amount = resource.amount + 0.18d;
  1212. //print("Found WaterBallast - adding this resource!");
  1213. }
  1214. }
  1215. if (resource.resourceName == "BallastGas")
  1216. {
  1217. if (vessel.verticalSpeed > 1.0d)
  1218. {
  1219. resource.amount = resource.amount - 1.5d;
  1220. //print("Found BallastGas - removing this resource!");
  1221. }
  1222. else if (vessel.verticalSpeed < 1.0d)
  1223. {
  1224. resource.amount = resource.amount - 0.18d;
  1225. //print("Found BallastGas - removing this resource!");
  1226. }
  1227.  
  1228. }
  1229. }
  1230.  
  1231. }
  1232. else if (verticalDirection == "down")
  1233. {
  1234. PartResourceList resourceList = this.part.Resources;
  1235. foreach (PartResource resource in resourceList)
  1236. {
  1237. if (resource.resourceName == "WaterBallast")
  1238. {
  1239. if (vessel.verticalSpeed < -1.0d)
  1240. {
  1241. resource.amount = resource.amount - 1.5d;
  1242. //print("Found WaterBallast - removing this resource!");
  1243. }
  1244. if (vessel.verticalSpeed > -1.0d)
  1245. {
  1246. resource.amount = resource.amount - 0.18d;
  1247. //print("Found WaterBallast - removing this resource!");
  1248. }
  1249. }
  1250. if (resource.resourceName == "BallastGas")
  1251. {
  1252. if (vessel.verticalSpeed < -1.0d)
  1253. {
  1254. resource.amount = resource.amount + 1.5d;
  1255. //print("Found BallastGas - adding this resource!");
  1256. }
  1257. if (vessel.verticalSpeed > -1.0d)
  1258. {
  1259. resource.amount = resource.amount + 0.18d;
  1260. //print("Found BallastGas - adding this resource!");
  1261. }
  1262. }
  1263. }
  1264.  
  1265. }
  1266.  
  1267. madeAdjustment = true;
  1268. }
  1269.  
  1270.  
  1271.  
  1272. }
  1273.  
  1274.  
  1275. }
  1276. }
  1277.  
  1278. public class neutralPitch : PartModule
  1279. {
  1280. public bool doItOnce = true;
  1281. public Transform bowPoint;
  1282. public Transform sternPoint;
  1283. public Part bowPart;
  1284. public Part sternPart;
  1285.  
  1286. [KSPField(guiActive = true, guiName = "Auto Pitch Active")]
  1287. public bool autoNeutralOn = false;
  1288.  
  1289. public string dominantPitch = "neutral";
  1290. public bool madeAdjustment = false;
  1291. public float adjTimer = 3f;
  1292. public float adjTimerCooldown = 3f;
  1293. //public int autoNMode = 1;
  1294.  
  1295. public double bowAltitude = 0.0;
  1296. public double sternAltitude = 0.0;
  1297. public double altDif = 0.0;
  1298.  
  1299. [KSPEvent(name = "UseAutoPitch", active = true, guiActive = true, guiName = "Auto Pitch")]
  1300. public void doPitchLevel()
  1301. {
  1302. if (autoNeutralOn == false)
  1303. {
  1304. autoNeutralOn = true;
  1305. }
  1306. else if (autoNeutralOn == true)
  1307. {
  1308. autoNeutralOn = false;
  1309. }
  1310. }
  1311.  
  1312. [KSPAction("Auto Pitch Control")]
  1313. public void toggleAutoPitch(KSPActionParam param)
  1314. {
  1315. doPitchLevel();
  1316. }
  1317.  
  1318.  
  1319. public void FixedUpdate()
  1320. {
  1321. if (HighLogic.LoadedSceneIsFlight)
  1322. {
  1323. if (doItOnce == true)
  1324. {
  1325. print("Running doItOnce");
  1326.  
  1327. print(this.vessel.parts.Count);
  1328.  
  1329. foreach (Part p in this.vessel.parts)
  1330. {
  1331.  
  1332. if (p.FindModelTransform("bowPoint") == true)
  1333. {
  1334. bowPoint = p.FindModelTransform("bowPoint");
  1335. bowPart = p;
  1336. }
  1337.  
  1338. if (p.FindModelTransform("sternPoint") == true)
  1339. {
  1340. sternPoint = p.FindModelTransform("sternPoint");
  1341. sternPart = p;
  1342. }
  1343.  
  1344. }
  1345.  
  1346.  
  1347. if (sternPoint != null && bowPoint != null)
  1348. {
  1349. doItOnce = false;
  1350. print("Found both Bow and Stern Points");
  1351. }
  1352. }
  1353.  
  1354.  
  1355. if (bowPoint != null && sternPoint != null && this.vessel.mainBody != null)
  1356. {
  1357. bowAltitude = Vector3.Distance(bowPoint.position, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  1358. sternAltitude = Vector3.Distance(sternPoint.position, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  1359.  
  1360. }
  1361.  
  1362. adjTimer -= Time.deltaTime;
  1363. if (adjTimer <= 0f)
  1364. {
  1365. adjTimer = adjTimerCooldown;//reset timer
  1366.  
  1367. if (bowAltitude < sternAltitude - 0.125)//find the pitch
  1368. {
  1369. dominantPitch = "negative"; //bow down
  1370. }
  1371. else if (bowAltitude > sternAltitude + 0.125)
  1372. {
  1373. dominantPitch = "positive";
  1374. }
  1375. else if (bowAltitude > sternAltitude - 0.125 && bowAltitude < sternAltitude + 0.125)
  1376. {
  1377. dominantPitch = "neutral";
  1378. //autoNeutralOn = false; // shut off the process.
  1379. }
  1380.  
  1381. madeAdjustment = false;
  1382. }
  1383.  
  1384.  
  1385.  
  1386.  
  1387. if (dominantPitch != "neutral" && autoNeutralOn == true && madeAdjustment == false)
  1388. {
  1389. if (dominantPitch == "negative")//bow down
  1390. {
  1391. //print("inside negative");
  1392. bool proceedExchange = false;
  1393. double exchAmount = 0.0d;
  1394. if (bowPoint != null)
  1395. {
  1396. PartResourceList resourceListBow = bowPart.Resources;
  1397. foreach (PartResource resource in resourceListBow)
  1398. {
  1399. if (resource.resourceName == "WaterBallast")
  1400. {
  1401. float cRModifier = 1f;
  1402. if (this.vessel.GetTotalMass() > 40f)
  1403. {
  1404. cRModifier = 5f;
  1405. }
  1406. else
  1407. {
  1408. cRModifier = 1f;
  1409. }
  1410. if (altDif > 1.5d && resource.amount >= (0.25d * cRModifier))
  1411. {
  1412. resource.amount = resource.amount - (0.25d * cRModifier);
  1413. proceedExchange = true;
  1414. exchAmount = (0.25d * cRModifier);
  1415. //print("Found WaterBallast - removing this resource!");
  1416. }
  1417. else if (altDif < 1.5d && resource.amount >= (0.08d * cRModifier) || altDif > 1.5d && resource.amount < (0.25d * cRModifier) && resource.amount >= (0.08d * cRModifier))
  1418. {
  1419. resource.amount = resource.amount - (0.08d * cRModifier);
  1420. proceedExchange = true;
  1421. exchAmount = (0.08d * cRModifier);
  1422. //print("Found WaterBallast - removing this resource!");
  1423. }
  1424. }
  1425. }
  1426. }
  1427. if (sternPoint != null)
  1428. {
  1429. PartResourceList resourceListStern = sternPart.Resources;
  1430. foreach (PartResource resource in resourceListStern)
  1431. {
  1432. if (resource.resourceName == "WaterBallast" && proceedExchange == true)
  1433. {
  1434. resource.amount = resource.amount + exchAmount;
  1435. //print("Found WaterBallast - adding this resource!");
  1436. }
  1437. }
  1438. }
  1439. }
  1440. else if (dominantPitch == "positive")//bow up
  1441. {
  1442. //print("inside positive");
  1443. bool proceedExchange = false;
  1444. double exchAmount = 0.0d;
  1445. if (sternPoint != null)
  1446. {
  1447. PartResourceList resourceListStern = sternPart.Resources;
  1448. foreach (PartResource resource in resourceListStern)
  1449. {
  1450. if (resource.resourceName == "WaterBallast")
  1451. {
  1452. float cRModifier = 1f;
  1453. if (this.vessel.GetTotalMass() > 40f)
  1454. {
  1455. cRModifier = 5f;
  1456. }
  1457. else
  1458. {
  1459. cRModifier = 1f;
  1460. }
  1461. if (altDif > 1.5d && resource.amount >= (0.25d * cRModifier))
  1462. {
  1463. resource.amount = resource.amount - (0.25d * cRModifier);
  1464. proceedExchange = true;
  1465. exchAmount = (0.25d * cRModifier);
  1466. //print("Found WaterBallast - removing this resource!");
  1467. }
  1468. else if (altDif < 1.5d && resource.amount >= (0.08d * cRModifier) || altDif > 1.5d && resource.amount < (0.25d * cRModifier) && resource.amount >= (0.08d * cRModifier))
  1469. {
  1470. resource.amount = resource.amount - (0.08d * cRModifier);
  1471. proceedExchange = true;
  1472. exchAmount = (0.08d * cRModifier);
  1473. //print("Found WaterBallast - removing this resource!");
  1474. }
  1475. }
  1476. }
  1477. }
  1478. if (bowPoint != null)
  1479. {
  1480. PartResourceList resourceListBow = bowPart.Resources;
  1481. foreach (PartResource resource in resourceListBow)
  1482. {
  1483. if (resource.resourceName == "WaterBallast" && proceedExchange == true)
  1484. {
  1485. resource.amount = resource.amount + exchAmount;
  1486. //print("Found WaterBallast - adding this resource!");
  1487. }
  1488. }
  1489. }
  1490.  
  1491. }
  1492.  
  1493. madeAdjustment = true;
  1494. }
  1495.  
  1496.  
  1497.  
  1498. }
  1499.  
  1500.  
  1501. }
  1502. }
  1503.  
  1504. //************************************************
  1505.  
  1506. [KSPAddon(KSPAddon.Startup.Flight, false)]
  1507. public class rangeBooster : MonoBehaviour
  1508. {
  1509. public bool doItOnce = true;
  1510. public bool LRMouseBypass = false;
  1511. public bool targeterOverride = false;
  1512. public int overrideCategory = 0;
  1513. public float rangeToRCast;
  1514. private Vessel tVessel = null;
  1515. private Vessel pVessel = null;
  1516.  
  1517. public static rangeBooster Instance = null;
  1518.  
  1519. void Start()
  1520. {
  1521. pVessel = FlightGlobals.ActiveVessel;
  1522.  
  1523. Vessel.loadDistance = 10250f; //10250f
  1524. Vessel.unloadDistance = 10000f; //10000f
  1525. rangeToRCast = 10000f;
  1526.  
  1527. Instance = this;
  1528.  
  1529. var sceneVessels = FindObjectsOfType(typeof(Vessel)) as Vessel[];
  1530. if (sceneVessels != null)
  1531. {
  1532. foreach (var v in sceneVessels)
  1533. {
  1534. v.distanceLandedPackThreshold = 9900f;
  1535. v.distanceLandedUnpackThreshold = 10150f;
  1536. v.distancePackThreshold = 9990f;
  1537. v.distanceUnpackThreshold = 10150f;
  1538.  
  1539. }
  1540. }
  1541.  
  1542.  
  1543. }
  1544.  
  1545.  
  1546.  
  1547. public void Update()
  1548. {
  1549.  
  1550. if (targeterOverride == false && LRMouseBypass == false)
  1551. {
  1552. if (Vessel.unloadDistance < 10000f || Vessel.loadDistance < 10250f)
  1553. {
  1554.  
  1555. Vessel.loadDistance = 10250f;
  1556. Vessel.unloadDistance = 10000f;
  1557. rangeToRCast = 10000f;
  1558.  
  1559. var sceneVessels = FindObjectsOfType(typeof(Vessel)) as Vessel[];
  1560. if (sceneVessels != null)
  1561. {
  1562. foreach (var v in sceneVessels)
  1563. {
  1564.  
  1565. v.distanceLandedPackThreshold = 9950f;
  1566. v.distanceLandedUnpackThreshold = 10150f;
  1567. v.distancePackThreshold = 9950f;
  1568. v.distanceUnpackThreshold = 10150f;
  1569.  
  1570. }
  1571. }
  1572. }
  1573. }
  1574. if (targeterOverride == true || LRMouseBypass == true)
  1575. {
  1576. if (overrideCategory == 1)
  1577. {
  1578. if (Vessel.unloadDistance < 19000f || Vessel.loadDistance < 19250f)
  1579. {
  1580.  
  1581. Vessel.loadDistance = 19250f;
  1582. Vessel.unloadDistance = 19000f;
  1583. rangeToRCast = 19000f;
  1584.  
  1585. var sceneVessels = FindObjectsOfType(typeof(Vessel)) as Vessel[];
  1586. if (sceneVessels != null)
  1587. {
  1588. foreach (var v in sceneVessels)
  1589. {
  1590. v.distanceLandedPackThreshold = 18950f;
  1591. v.distanceLandedUnpackThreshold = 19150f;
  1592. v.distancePackThreshold = 18950f;
  1593. v.distanceUnpackThreshold = 19150f;
  1594. }
  1595. }
  1596. }
  1597. }
  1598.  
  1599. if (overrideCategory == 2)
  1600. {
  1601. if (Vessel.unloadDistance < 49000f || Vessel.loadDistance < 49250f)
  1602. {
  1603.  
  1604. Vessel.loadDistance = 49250f;
  1605. Vessel.unloadDistance = 49000f;
  1606. rangeToRCast = 49000f;
  1607.  
  1608. var sceneVessels = FindObjectsOfType(typeof(Vessel)) as Vessel[];
  1609. if (sceneVessels != null)
  1610. {
  1611. foreach (var v in sceneVessels)
  1612. {
  1613. v.distanceLandedPackThreshold = 48950f;
  1614. v.distanceLandedUnpackThreshold = 49150f;
  1615. v.distancePackThreshold = 48950f;
  1616. v.distanceUnpackThreshold = 49150f;
  1617. }
  1618. }
  1619. }
  1620. }
  1621.  
  1622. if (overrideCategory == 3)
  1623. {
  1624. if (Vessel.unloadDistance < 99000f || Vessel.loadDistance < 99250f)
  1625. {
  1626.  
  1627. Vessel.loadDistance = 99250f;
  1628. Vessel.unloadDistance = 99000f;
  1629. rangeToRCast = 99000f;
  1630.  
  1631. var sceneVessels = FindObjectsOfType(typeof(Vessel)) as Vessel[];
  1632. if (sceneVessels != null)
  1633. {
  1634. foreach (var v in sceneVessels)
  1635. {
  1636. v.distanceLandedPackThreshold = 98950f;
  1637. v.distanceLandedUnpackThreshold = 99150f;
  1638. v.distancePackThreshold = 98950f;
  1639. v.distanceUnpackThreshold = 99150f;
  1640. }
  1641. }
  1642. }
  1643. }
  1644. }
  1645.  
  1646.  
  1647. }
  1648.  
  1649. public void FixedUpdate()
  1650. {
  1651.  
  1652.  
  1653. if (pVessel != FlightGlobals.ActiveVessel)
  1654. {
  1655. tVessel = null;
  1656. pVessel = FlightGlobals.ActiveVessel;
  1657. }
  1658.  
  1659.  
  1660. if (HighLogic.LoadedSceneIsFlight && !FlightGlobals.ActiveVessel.isEVA && FlightGlobals.ActiveVessel.IsControllable)
  1661. {
  1662.  
  1663. if (FlightGlobals.fetch.vesselTargetMode != VesselTargetModes.None && doItOnce == true && tVessel != FlightGlobals.ActiveVessel)
  1664. {
  1665. //tVessel = FlightGlobals.ActiveVessel.targetObject.GetVessel();
  1666. ITargetable target = FlightGlobals.fetch.VesselTarget;
  1667. if (target != null)
  1668. {
  1669. tVessel = target.GetVessel();
  1670.  
  1671. float distToTarget = Vector3.Distance(tVessel.transform.position, FlightGlobals.ActiveVessel.transform.position);
  1672.  
  1673. if (distToTarget > 9800f)
  1674. {
  1675. targeterOverride = true;
  1676.  
  1677. if (distToTarget < 19800f)
  1678. {
  1679. overrideCategory = 1;
  1680. }
  1681. if (distToTarget >= 19800f && distToTarget < 49800f)
  1682. {
  1683. overrideCategory = 2;
  1684. }
  1685. if (distToTarget >= 49800f && distToTarget < 99800f)
  1686. {
  1687. overrideCategory = 3;
  1688. }
  1689. doItOnce = false;
  1690. }
  1691. }
  1692.  
  1693. }
  1694. if (FlightGlobals.fetch.vesselTargetMode == VesselTargetModes.None && LRMouseBypass == false)
  1695. {
  1696. doItOnce = true;
  1697. targeterOverride = false;
  1698. overrideCategory = 0;
  1699.  
  1700. }
  1701. else if (FlightGlobals.fetch.vesselTargetMode == VesselTargetModes.None && LRMouseBypass == true)
  1702. {
  1703. overrideCategory = 3;
  1704. //Debug.Log("LongRange RangeBooster setting enabled!");
  1705. doItOnce = false;
  1706. }
  1707.  
  1708.  
  1709. }
  1710. else
  1711. {
  1712. tVessel = null;
  1713. }
  1714.  
  1715.  
  1716. }
  1717. }
  1718.  
  1719. //************************************************************
  1720.  
  1721. public class BoatInterface : PartModule
  1722. {
  1723. private bool setSaveTrip = false;
  1724.  
  1725. [KSPField(isPersistant = true)]
  1726. private bool setCheckItOnce = false;
  1727.  
  1728. //public AudioSource hornSound;
  1729. public FXGroup soundGroup;
  1730. public FXGroup soundBGroup;
  1731.  
  1732. [KSPField(isPersistant = true, guiActiveEditor = true, guiActive = false, guiName = "M2WTimer: "), UI_FloatRange(minValue = 5f, maxValue = 12f, stepIncrement = 1f)]
  1733. public float timeLockFix = 5f;// wait 5 seconds between moving and making the ship unlock.
  1734.  
  1735. [KSPField(isPersistant = true)]
  1736. public bool lockFixed = false;
  1737.  
  1738. [KSPField(isPersistant = true)]
  1739. public bool flipForwardUp = false;
  1740.  
  1741. [KSPField(isPersistant = true)]
  1742. public bool flipLeftRight = false;
  1743.  
  1744. [KSPField(isPersistant = true)]
  1745. public bool flipRightForward = false;
  1746.  
  1747. [KSPField(isPersistant = true, guiActiveEditor = true, guiActive = false, guiName = "Move2Water: "), UI_Toggle(controlEnabled = true, disabledText = "No", enabledText = "Yes")]
  1748. public bool moveAtLaunch = true; // for overriding with SPH option menu
  1749.  
  1750. [KSPField]
  1751. public float stabilForce = 10f;
  1752.  
  1753. [KSPField]
  1754. public float stabilOffset = 2f;
  1755.  
  1756. [KSPField(isPersistant = true, guiActiveEditor = true), UI_FloatRange(minValue = -1f, maxValue = 1f, stepIncrement = 0.01f)]
  1757. public float latitude = -0.039751f;
  1758. [KSPField(isPersistant = true, guiActiveEditor = true), UI_FloatRange(minValue = 285f, maxValue = 286f, stepIncrement = 0.01f)]
  1759. public float longitude = 285.639486f;
  1760. [KSPField(isPersistant = true, guiActiveEditor = true)]
  1761. public float altitude = 1f;
  1762. [KSPField(isPersistant = true, guiActiveEditor = true), UI_FloatRange(minValue = -20f, maxValue = 20f, stepIncrement = 1f)]
  1763. public float altitudeShift = 0f;
  1764.  
  1765. //[KSPField]
  1766. //public Vector3 launchPosition = new Vector3(-2294.581f, 7.605f, -3458.309f);
  1767.  
  1768. [KSPField(isPersistant = true)]
  1769. public bool hasLaunched = false;
  1770.  
  1771. [KSPField]
  1772. public bool hornActive = true;
  1773.  
  1774. [KSPField]
  1775. public float dOLRMulti = 1f;
  1776.  
  1777. [KSPField(isPersistant = true, guiActiveEditor = true, guiActive = true, guiName = "DepthWarning: "), UI_Toggle(controlEnabled = false, disabledText = "No", enabledText = "Yes")]
  1778. public bool depthWarning = false; // permits sound and warning texts.
  1779.  
  1780. [KSPField]
  1781. public float calibrateDepth = 0f; //set in cfg.
  1782.  
  1783. public bool showCurrentPos = false;
  1784.  
  1785. public float depthDTimer;
  1786. public float depthDCoolDown = 15f;
  1787.  
  1788. //public float oldDrag;
  1789.  
  1790. public float impactDTimer;
  1791. public float impactDCoolDown = 1.2f;
  1792.  
  1793. [KSPField(guiActive = true, guiName = "Depth Under Ship")]
  1794. public float depthUnderShip; // this variable contains the depth under the ship.
  1795. [KSPField(guiActive = true, guiName = "Ship Total Mass")]
  1796. public float shipsTotalMass; // this variable contains the reverse of the current heading.
  1797. [KSPField(guiActive = true, guiName = "Reverse Heading")]
  1798. public double shipsRevHeading; // this variable contains the reverse of the current heading.
  1799. [KSPField]
  1800. public float shipsRudderSet = 0; // this variable contains the reverse of the current heading.
  1801.  
  1802. [KSPEvent(name = "soundHorn", active = true, guiActive = false, guiName = "Sound Horn")]
  1803. public void doShipSoundHorn()
  1804. {
  1805. soundGroup.audio.Play();
  1806. }
  1807.  
  1808. [KSPEvent(name = "PrintCurrentPosition", active = true, guiActive = true, guiName = "Print Position")]
  1809. public void doPrintPosition()
  1810. {
  1811. showCurrentPos = true; // this prints the current 3d world position ONE time.
  1812. }
  1813.  
  1814.  
  1815. [KSPEvent(name = "ShowRudder5PortSet", active = true, guiActive = true, guiName = "Set Rudder 5 Deg to Port")]
  1816. public void CalculateShipsRudderPORTlowSet()
  1817. {
  1818. shipsRudderSet = -0.167f; // 1 would be 100% of 30 which is the rudder max deg .167 is close to 5 degrees.
  1819. FlightInputHandler.state.yawTrim = shipsRudderSet;
  1820.  
  1821. //print(shipsRevHeading); //use this to check it's working.
  1822. //PopupDialog.SpawnPopupDialog("Set Ships Rudder", "Rudder set to Port " + shipsRudderSet, "Confirm", false, HighLogic.Skin);
  1823. ScreenMessages.PostScreenMessage(new ScreenMessage("Rudder set to Port " + shipsRudderSet, 2f, ScreenMessageStyle.UPPER_CENTER));
  1824. }
  1825.  
  1826. [KSPEvent(name = "ShowRudder5StarSet", active = true, guiActive = true, guiName = "Set Rudder 5 Deg to Starboard")]
  1827. public void CalculateShipsRudderSTARlowSet()
  1828. {
  1829. shipsRudderSet = 0.167f;
  1830. FlightInputHandler.state.yawTrim = shipsRudderSet;
  1831.  
  1832. //print(shipsRevHeading); //use this to check it's working.
  1833. //PopupDialog.SpawnPopupDialog("Set Ships Rudder", "Rudder set to Starboard " + shipsRudderSet, "Confirm", false, HighLogic.Skin);
  1834. ScreenMessages.PostScreenMessage(new ScreenMessage("Rudder set to Starboard " + shipsRudderSet, 2f, ScreenMessageStyle.UPPER_CENTER));
  1835. }
  1836.  
  1837. [KSPAction("Sound Ship Horn")]
  1838. public void actSoundShipHorn(KSPActionParam param)
  1839. {
  1840. doShipSoundHorn();
  1841. }
  1842.  
  1843.  
  1844. public override void OnStart(PartModule.StartState state)
  1845. {
  1846. base.OnStart(state);
  1847.  
  1848. //oldDrag = this.vessel.rigidbody.drag;
  1849.  
  1850. //if (HighLogic.LoadedSceneIsFlight && moveAtLaunch == true)
  1851. //{
  1852.  
  1853. // if (!hasLaunched && setCheckItOnce == false)
  1854. // {
  1855. // Debug.Log("moving craft");
  1856. // moveCraft();
  1857. // hasLaunched = true;
  1858. // }
  1859. //}
  1860.  
  1861. setCheckItOnce = false;
  1862.  
  1863.  
  1864.  
  1865. #region Sound effects
  1866. if (!GameDatabase.Instance.ExistsAudioClip("BoatPartsR3/Sounds/R3CarrierHorn")) return;
  1867. //print("getting past the sound condition 1");
  1868. soundGroup.audio = gameObject.AddComponent<AudioSource>();
  1869.  
  1870. if (soundGroup.audio != null)
  1871. {
  1872. //print("getting past the sound condition 2");
  1873. soundGroup.audio.volume = GameSettings.SHIP_VOLUME;
  1874. soundGroup.audio.rolloffMode = AudioRolloffMode.Logarithmic;
  1875. soundGroup.audio.dopplerLevel = 0f;
  1876. soundGroup.audio.panLevel = 1f;
  1877. soundGroup.audio.clip = GameDatabase.Instance.GetAudioClip("BoatPartsR3/Sounds/R3CarrierHorn");
  1878. soundGroup.audio.loop = false;
  1879. soundGroup.audio.playOnAwake = false;
  1880. }
  1881.  
  1882. if (!GameDatabase.Instance.ExistsAudioClip("BoatPartsR3/Sounds/Klaxon")) return;
  1883. //print("getting past the sound condition 1");
  1884. soundBGroup.audio = gameObject.AddComponent<AudioSource>();
  1885.  
  1886. if (soundBGroup.audio != null)
  1887. {
  1888. //print("getting past the sound condition 2");
  1889. soundBGroup.audio.volume = GameSettings.SHIP_VOLUME;
  1890. soundBGroup.audio.rolloffMode = AudioRolloffMode.Logarithmic;
  1891. soundBGroup.audio.dopplerLevel = 0f;
  1892. soundBGroup.audio.panLevel = 1f;
  1893. soundBGroup.audio.clip = GameDatabase.Instance.GetAudioClip("BoatPartsR3/Sounds/Klaxon");
  1894. soundBGroup.audio.loop = false;
  1895. soundBGroup.audio.playOnAwake = false;
  1896. }
  1897.  
  1898. #endregion
  1899.  
  1900. }
  1901.  
  1902.  
  1903. public override void OnUpdate()
  1904. {
  1905. base.OnUpdate();
  1906.  
  1907. if (!HighLogic.LoadedSceneIsFlight) return;
  1908. {
  1909.  
  1910.  
  1911.  
  1912.  
  1913. if (this.vessel.heightFromTerrain >= 0)
  1914. {
  1915. depthUnderShip = Mathf.Round(this.vessel.heightFromTerrain - calibrateDepth);
  1916. }
  1917. else if (this.vessel.heightFromTerrain < 0)
  1918. {
  1919. depthUnderShip = 600f;
  1920. }
  1921.  
  1922. if (depthWarning == true && this.vessel == FlightGlobals.ActiveVessel) //mainly for subs.
  1923. {
  1924. depthDTimer -= Time.deltaTime; // this is a casual info popup
  1925. if (depthDTimer <= 0f)
  1926. {
  1927. depthDTimer = depthDCoolDown;
  1928.  
  1929. if(depthUnderShip > 100f && depthUnderShip < 300f)//accurate reading
  1930. {
  1931. ScreenMessages.PostScreenMessage(new ScreenMessage("Depth Below Keel " + depthUnderShip + " m", 3f, ScreenMessageStyle.UPPER_RIGHT));
  1932. }
  1933.  
  1934. if (depthUnderShip < 100f && depthUnderShip > 0f)//accurate reading
  1935. {
  1936. ScreenMessages.PostScreenMessage(new ScreenMessage("Shallow Water! " + depthUnderShip + " m", 3f, ScreenMessageStyle.UPPER_RIGHT));
  1937. }
  1938.  
  1939. }
  1940. impactDTimer -= Time.deltaTime; // we check this more often as it's critical
  1941. if (impactDTimer <= 0f)
  1942. {
  1943. impactDTimer = impactDCoolDown;
  1944. if (depthUnderShip < 20f && depthUnderShip > 0f && this.vessel.horizontalSrfSpeed > 5.0d)//show constantly for now - Will also need to play warning sound.
  1945. {
  1946. soundBGroup.audio.Play();
  1947. ScreenMessages.PostScreenMessage(new ScreenMessage("Impact Warning!!! " + depthUnderShip + " m", 2.5f, ScreenMessageStyle.UPPER_CENTER));
  1948. }
  1949. }
  1950. }
  1951.  
  1952. if (showCurrentPos == true)
  1953. {
  1954. //print(vessel.GetWorldPos3D());
  1955. showCurrentPos = false;
  1956. Debug.Log("Coordinates: lat " + vessel.latitude + ", long " + vessel.longitude + ", alt " + vessel.altitude);
  1957.  
  1958. }
  1959.  
  1960. if (FlightGlobals.ship_heading <= 180)
  1961. {
  1962. shipsRevHeading = Mathf.Round(FlightGlobals.ship_heading + 180);
  1963. }
  1964. else shipsRevHeading = Mathf.Round(FlightGlobals.ship_heading - 180);
  1965.  
  1966.  
  1967. if (setCheckItOnce == false)
  1968. {
  1969. if (FlightGlobals.ActiveVessel.situation == Vessel.Situations.SPLASHED) //once the ship becomes splashed the first time it will toggle the setSaveTrip condition for the onSave attempt override.
  1970. {
  1971. setSaveTrip = true;
  1972. setCheckItOnce = true; // this stops this from ever running again.
  1973. }
  1974. }
  1975.  
  1976. if (Input.GetKeyUp(KeyCode.KeypadMultiply) || timeLockFix <= 0f && lockFixed == false) //press key return to ksc then choose ship from the station - should be unlocked.
  1977. {
  1978. this.vessel.situation = Vessel.Situations.SPLASHED;
  1979. this.vessel.Landed = false;
  1980. this.vessel.Splashed = true;
  1981. //this.vessel.GoOnRails();
  1982. //this.vessel.rigidbody.WakeUp();
  1983. //this.vessel.ResumeStaging();
  1984. base.vessel.landedAt = "";
  1985. InputLockManager.ClearControlLocks();
  1986. lockFixed = true;
  1987.  
  1988. //ScreenMessages.PostScreenMessage(new ScreenMessage("Moving to the water!", 5f, ScreenMessageStyle.UPPER_CENTER));
  1989.  
  1990. }
  1991.  
  1992.  
  1993.  
  1994. }
  1995.  
  1996.  
  1997. }
  1998.  
  1999. public void FixedUpdate()
  2000. {
  2001. if (!HighLogic.LoadedSceneIsFlight) return;
  2002. {
  2003. shipsTotalMass = Mathf.Round(this.vessel.GetTotalMass());
  2004.  
  2005. if (timeLockFix > 0f && !lockFixed)
  2006. {
  2007. timeLockFix -= Time.deltaTime;
  2008. moveCraft();
  2009.  
  2010. }
  2011. else hasLaunched = true;
  2012.  
  2013. if (hornActive == true)
  2014. {
  2015. Events["doShipSoundHorn"].guiActive = true;
  2016. }
  2017. else if (hornActive == false)
  2018. {
  2019. Events["doShipSoundHorn"].guiActive = false;
  2020. }
  2021.  
  2022. Vector3 forward = FlightGlobals.ActiveVessel.transform.up;
  2023. Vector3 heading = FlightGlobals.ActiveVessel.transform.right;
  2024. Vector3 velDirection = FlightGlobals.ActiveVessel.rigidbody.velocity;//.normalized;
  2025.  
  2026. if (flipForwardUp == true)
  2027. {
  2028. forward = FlightGlobals.ActiveVessel.transform.forward;
  2029. }
  2030.  
  2031. if (flipRightForward == true)
  2032. {
  2033. heading = FlightGlobals.ActiveVessel.transform.forward;
  2034. }
  2035.  
  2036. //if (Vector3.Dot(forward, velDirection) > 0f)
  2037. //{
  2038. // print("Moving Forward"); // lets see what this is.
  2039. //}
  2040. //else if(Vector3.Dot(forward, velDirection) < 0f)
  2041. //{
  2042. // print("Moving Reverse");
  2043. //}
  2044.  
  2045. #region vessel stabilizer test
  2046.  
  2047. stabilForce = shipsTotalMass * 0.15f;
  2048. stabilOffset = stabilForce * 0.5f;
  2049.  
  2050. if (this.vessel.rigidbody != null)
  2051. {
  2052. this.vessel.rigidbody.AddForceAtPosition(((this.vessel.transform.position - this.vessel.mainBody.position).normalized) * stabilForce, (this.vessel.transform.position - this.vessel.mainBody.position) * stabilOffset);
  2053. this.vessel.rigidbody.AddForceAtPosition((-(this.vessel.transform.position - this.vessel.mainBody.position).normalized) * stabilForce, -(this.vessel.transform.position - this.vessel.mainBody.position) * stabilOffset);
  2054. }
  2055.  
  2056. #endregion
  2057.  
  2058. if (this.vessel.horizontalSrfSpeed > 1.0d && this.vessel.IsControllable)
  2059. {
  2060.  
  2061.  
  2062. if (flipLeftRight == true)
  2063. {
  2064. if (Vector3.Dot(-heading, velDirection) > 3f && Vector3.Dot(-heading, velDirection) <= 15f)
  2065. {
  2066. //print("Drifting Right"); // lets see what this is.
  2067. this.vessel.rigidbody.AddForce(heading.normalized * (dOLRMulti * ( 0.5f *shipsTotalMass)));
  2068.  
  2069. }
  2070. else if (Vector3.Dot(-heading, velDirection) > 15f && Vector3.Dot(-heading, velDirection) <= 45f)
  2071. {
  2072. //print("Drifting Right"); // lets see what this is.
  2073. this.vessel.rigidbody.AddForce(heading.normalized * (dOLRMulti * (1.25f * shipsTotalMass)));
  2074.  
  2075. }
  2076. else if (Vector3.Dot(-heading, velDirection) > 45f && Vector3.Dot(-heading, velDirection) < 90f)
  2077. {
  2078. //print("Drifting Right"); // lets see what this is.
  2079. this.vessel.rigidbody.AddForce(heading.normalized * (dOLRMulti * (1.8f * shipsTotalMass)));
  2080.  
  2081. }
  2082. else if (Vector3.Dot(-heading, velDirection) < -3f && Vector3.Dot(-heading, velDirection) >= -15f)
  2083. {
  2084. //print("Drifting Left");
  2085. this.vessel.rigidbody.AddForce(heading.normalized * -(dOLRMulti * (0.5f * shipsTotalMass)));
  2086.  
  2087. }
  2088. else if (Vector3.Dot(-heading, velDirection) < -15f && Vector3.Dot(-heading, velDirection) >= -45f)
  2089. {
  2090. //print("Drifting Left");
  2091. this.vessel.rigidbody.AddForce(heading.normalized * -(dOLRMulti * (1.25f * shipsTotalMass)));
  2092.  
  2093. }
  2094. else if (Vector3.Dot(-heading, velDirection) < -45f && Vector3.Dot(-heading, velDirection) > -90f)
  2095. {
  2096. //print("Drifting Left");
  2097. this.vessel.rigidbody.AddForce(heading.normalized * -(dOLRMulti * (1.8f * shipsTotalMass)));
  2098.  
  2099. }
  2100.  
  2101. }
  2102. else
  2103. {
  2104. if (Vector3.Dot(heading, velDirection) > 3f && Vector3.Dot(heading, velDirection) <= 15f)
  2105. {
  2106. //print("Drifting Right"); // lets see what this is.
  2107. this.vessel.rigidbody.AddForce(heading.normalized * (dOLRMulti * (0.5f * shipsTotalMass)));
  2108.  
  2109. }
  2110. else if (Vector3.Dot(heading, velDirection) > 15f && Vector3.Dot(heading, velDirection) <= 45f)
  2111. {
  2112. //print("Drifting Right"); // lets see what this is.
  2113. this.vessel.rigidbody.AddForce(heading.normalized * (dOLRMulti * (1.25f * shipsTotalMass)));
  2114.  
  2115. }
  2116. else if (Vector3.Dot(heading, velDirection) > 45f && Vector3.Dot(heading, velDirection) < 90f)
  2117. {
  2118. //print("Drifting Right"); // lets see what this is.
  2119. this.vessel.rigidbody.AddForce(heading.normalized * (dOLRMulti * (1.8f * shipsTotalMass)));
  2120.  
  2121. }
  2122. else if (Vector3.Dot(heading, velDirection) < -3f && Vector3.Dot(heading, velDirection) >= -15f)
  2123. {
  2124. //print("Drifting Left");
  2125. this.vessel.rigidbody.AddForce(heading.normalized * -(dOLRMulti * (0.5f * shipsTotalMass)));
  2126.  
  2127. }
  2128. else if (Vector3.Dot(heading, velDirection) < -15f && Vector3.Dot(heading, velDirection) >= -45f)
  2129. {
  2130. //print("Drifting Left");
  2131. this.vessel.rigidbody.AddForce(heading.normalized * -(dOLRMulti * (1.25f * shipsTotalMass)));
  2132.  
  2133. }
  2134. else if (Vector3.Dot(heading, velDirection) < -45f && Vector3.Dot(heading, velDirection) > -90f)
  2135. {
  2136. //print("Drifting Left");
  2137. this.vessel.rigidbody.AddForce(heading.normalized * -(dOLRMulti * (1.8f * shipsTotalMass)));
  2138.  
  2139. }
  2140.  
  2141.  
  2142. }
  2143. //print(Vector3.Dot(heading, velDirection));
  2144.  
  2145. }
  2146.  
  2147.  
  2148. }
  2149.  
  2150.  
  2151. }
  2152.  
  2153. public override void OnSave(ConfigNode node) // this is the attempt to override the game saving the carrier as LANDED... LANDED = BAD :)
  2154. {
  2155. base.OnSave(node);
  2156. if (!HighLogic.LoadedSceneIsFlight || setSaveTrip == false) return; //if the scene is not a flight or the condition is false... get out/don't do anything.
  2157. {
  2158. this.vessel.situation = Vessel.Situations.SPLASHED;
  2159. this.vessel.Landed = false;
  2160. this.vessel.Splashed = true;
  2161.  
  2162.  
  2163. }
  2164.  
  2165. }
  2166. public Vector3d calculateLaunchPosition()
  2167. {
  2168. return vessel.mainBody.GetWorldSurfacePosition((double)latitude, (double)longitude, (double)(altitude + altitudeShift));
  2169. }
  2170.  
  2171. public void moveCraft()
  2172. {
  2173. if (this.vessel != null && moveAtLaunch == true)
  2174. {
  2175.  
  2176. //Debug.Log("Move to Water: " + launchPosition);
  2177. //vessel.SetPosition(launchPosition, true);
  2178.  
  2179. vessel.SetPosition(calculateLaunchPosition(), true);
  2180. if (!vessel.rigidbody.isKinematic)
  2181. {
  2182. vessel.rigidbody.velocity = Vector3.zero;
  2183. vessel.rigidbody.angularVelocity = Vector3.zero;
  2184. }
  2185. }
  2186.  
  2187. }
  2188.  
  2189.  
  2190.  
  2191.  
  2192. }
  2193.  
  2194. //*************************************************
  2195.  
  2196. public class aircraftLift : PartModule
  2197. {
  2198.  
  2199. //private bool tripLift = false;
  2200. //[KSPField (isPersistant = true)]
  2201. //public float liftSpeed = 0.05f; // this variable controls the speed of the lift via the CFG.
  2202.  
  2203. //public AudioSource liftSound;
  2204. public FXGroup soundGroup;
  2205.  
  2206. public bool tripSoundPlay = false;
  2207.  
  2208. private ModuleAnimateGeneric liftOperation = new ModuleAnimateGeneric();
  2209.  
  2210. [KSPEvent(active = true, guiName = "Operate ", guiActiveUnfocused = true, externalToEVAOnly = true, guiActive = false, unfocusedRange = 20f)]
  2211. public void actuateAirLift()
  2212. {
  2213. doLiftOp();
  2214. }
  2215.  
  2216. //[KSPEvent(active = false, guiName = "Decrease Speed", guiActiveUnfocused = true, externalToEVAOnly = false, guiActive = false)]
  2217. //public void incSpdAirLift()
  2218. //{
  2219. // if (liftSpeed > 0)
  2220. // {
  2221. // liftSpeed = liftSpeed - 0.01f;
  2222. // print(liftSpeed);
  2223. // }
  2224.  
  2225. // if (liftSpeed <= 0)
  2226. // {
  2227. // liftSpeed = 0.01f;
  2228. // print(liftSpeed);
  2229. // }
  2230. //}
  2231.  
  2232. //[KSPEvent(active = false, guiName = "Increase Speed", guiActiveUnfocused = true, externalToEVAOnly = false, guiActive = false)]
  2233. //public void decSpdAirLift()
  2234. //{
  2235. // if (liftSpeed < 1)
  2236. // {
  2237. // liftSpeed = liftSpeed + 0.01f;
  2238. // print(liftSpeed);
  2239. // }
  2240.  
  2241. // if (liftSpeed >= 1)
  2242. // {
  2243. // liftSpeed = 1f;
  2244. // print(liftSpeed);
  2245. // }
  2246.  
  2247. //}
  2248.  
  2249. public override void OnStart(PartModule.StartState state)
  2250. {
  2251. base.OnStart(state);
  2252.  
  2253. liftOperation = this.part.Modules.OfType<ModuleAnimateGeneric>().FirstOrDefault();
  2254. //liftOperation.animSpeed = liftSpeed;
  2255.  
  2256. #region Sound effects
  2257. if (!GameDatabase.Instance.ExistsAudioClip("BoatPartsR3/Sounds/R3CarrierLift")) return;
  2258. //print("getting past the sound condition 1");
  2259. soundGroup.audio = gameObject.AddComponent<AudioSource>();
  2260.  
  2261. if (soundGroup.audio != null)
  2262. {
  2263. //print("getting past the sound condition 2");
  2264. soundGroup.audio.volume = GameSettings.SHIP_VOLUME;
  2265. soundGroup.audio.rolloffMode = AudioRolloffMode.Logarithmic;
  2266. soundGroup.audio.dopplerLevel = 0f;
  2267. soundGroup.audio.panLevel = 1f;
  2268. soundGroup.audio.clip = GameDatabase.Instance.GetAudioClip("BoatPartsR3/Sounds/R3CarrierLift");
  2269. soundGroup.audio.loop = true;
  2270. soundGroup.audio.playOnAwake = false;
  2271. }
  2272.  
  2273. #endregion
  2274.  
  2275. }
  2276.  
  2277. public void FixedUpdate()
  2278. {
  2279. if (!HighLogic.LoadedSceneIsFlight) return;
  2280. {
  2281. if (liftOperation.animTime >= 0.02f && liftOperation.animTime <= 0.98f)
  2282. {
  2283. if (tripSoundPlay == false)
  2284. {
  2285. soundGroup.audio.Play();
  2286. tripSoundPlay = true;
  2287. }
  2288. }
  2289.  
  2290. if (liftOperation.animTime <= 0.01f || liftOperation.animTime >= 0.99f)
  2291. {
  2292. soundGroup.audio.Stop();
  2293. tripSoundPlay = false;
  2294. }
  2295. }
  2296. }
  2297.  
  2298. private void doLiftOp()
  2299. {
  2300. //liftOperation.animSpeed = liftSpeed;
  2301. liftOperation.Toggle();
  2302. soundGroup.audio.Play();
  2303. //print(liftSpeed);
  2304. }
  2305. }
  2306.  
  2307. //**********************************************
  2308.  
  2309. public class nuclearEngine : PartModule
  2310. {
  2311. [KSPField]
  2312. public float revOutput = 0.5f; // this variable is how much final thrust is produced while in reverse... in percent.
  2313.  
  2314. [KSPField]
  2315. public float maxRpm;
  2316.  
  2317. [KSPField]
  2318. public float thrustScale = 1f;
  2319.  
  2320. [KSPField(guiActive = true, guiActiveEditor = false, guiName = "Thrust ")]
  2321. public float maxThrust;
  2322.  
  2323. [KSPField]
  2324. public string thrustDirection = "up";
  2325.  
  2326. public Vector3 useThrustDir = new Vector3(0f,0f,0f);
  2327.  
  2328. public Transform bowPoint;
  2329.  
  2330. [KSPField]
  2331. public float heatProduction = 200f;
  2332.  
  2333. [KSPField(isPersistant = true, guiActiveEditor = true, guiActive = true, guiName = "EngineActive "), UI_Toggle(controlEnabled = false, disabledText = "No", enabledText = "Yes")]
  2334. public bool engineActive = false;
  2335.  
  2336. [KSPField(isPersistant = true, guiActiveEditor = true, guiActive = true, guiName = "RPM Limit %"), UI_FloatRange(minValue = 0.1f, maxValue = 1f, stepIncrement = 0.1f)]
  2337. public float fwdLimiter = 1f;
  2338.  
  2339. [KSPField(isPersistant = true, guiActiveEditor = true, guiActive = true, guiName = "R-RPM Limit %"), UI_FloatRange(minValue = 0.1f, maxValue = 1f, stepIncrement = 0.1f)]
  2340. public float revLimiter = 1f;
  2341.  
  2342. [KSPField]
  2343. public bool hasScrew = false;
  2344.  
  2345. [KSPField]
  2346. public bool hasHsUD = false;//subs only
  2347.  
  2348. [KSPField]
  2349. public float hSUDMulti = 1.2f;
  2350.  
  2351. [KSPField]
  2352. public bool useTTransform = false;
  2353.  
  2354. [KSPField(isPersistant = true)]
  2355. public float storeOldMaxThrust;
  2356.  
  2357. [KSPField(isPersistant = true)]
  2358. public float storeOldHeatProd;
  2359.  
  2360. [KSPField(isPersistant = true)]
  2361. public float storeOldThrustScale;
  2362.  
  2363. public GameObject propWash;
  2364.  
  2365. [KSPField(isPersistant = true)]
  2366. public bool doItOnce = true;
  2367.  
  2368. public Transform engThrustTransform;
  2369.  
  2370. public float oOfWaterLimiter = 1f;
  2371.  
  2372. public bool soundTripped = false;
  2373.  
  2374. public bool isUnderWater = false;
  2375.  
  2376. public double partAltitude;
  2377. public double bowAltitude;
  2378.  
  2379. public FXGroup soundGroup;
  2380.  
  2381.  
  2382. //public float currentThrust = 0f;
  2383. [KSPField(guiActive = true, guiName = "Propulsion Reverse", isPersistant = true)]
  2384. public bool togRevThrust = false;
  2385. //public Vector3 revEAngle;
  2386.  
  2387. private ModuleAnimateGeneric shipsEngine = new ModuleAnimateGeneric();
  2388.  
  2389. [KSPEvent(name = "togEngine", active = true, guiActive = false, guiName = "Toggle Engine")]
  2390. public void setShipEngine()
  2391. {
  2392. if (engineActive == true)
  2393. {
  2394. engineActive = false;
  2395. ScreenMessages.PostScreenMessage(new ScreenMessage("Engine Decactivated ", 1.25f, ScreenMessageStyle.UPPER_CENTER));
  2396.  
  2397. }
  2398. else
  2399. {
  2400. engineActive = true;
  2401. ScreenMessages.PostScreenMessage(new ScreenMessage("Engine Active ", 1.25f, ScreenMessageStyle.UPPER_CENTER));
  2402. //PopupDialog.SpawnPopupDialog("Nuclear Propulsion", "Set Back ", "Close", false, HighLogic.Skin);
  2403.  
  2404. }
  2405. }
  2406.  
  2407. [KSPEvent(name = "setReverse", active = true, guiActive = true, guiName = "Reverse Propulsion")]
  2408. public void setShipEngineReverse()
  2409. {
  2410. if (togRevThrust == true)
  2411. {
  2412. togRevThrust = false;
  2413. ScreenMessages.PostScreenMessage(new ScreenMessage("Propulsion Ahead ", 1.25f, ScreenMessageStyle.UPPER_CENTER));
  2414. //PopupDialog.SpawnPopupDialog("Nuclear Propulsion", "Set Ahead ", "Close", false, HighLogic.Skin);
  2415.  
  2416. }
  2417. else
  2418. {
  2419. togRevThrust = true;
  2420. ScreenMessages.PostScreenMessage(new ScreenMessage("Propulsion Reverse ", 1.25f, ScreenMessageStyle.UPPER_CENTER));
  2421. //PopupDialog.SpawnPopupDialog("Nuclear Propulsion", "Set Back ", "Close", false, HighLogic.Skin);
  2422.  
  2423. }
  2424. }
  2425.  
  2426. [KSPAction("Toggle Reverse Propulsion")]
  2427. public void toggleTheFoldingSystem(KSPActionParam param)
  2428. {
  2429. setShipEngineReverse();
  2430. }
  2431.  
  2432. [KSPAction("Toggle Engine")]
  2433. public void toggleTheEngineSystem(KSPActionParam param)
  2434. {
  2435. setShipEngine();
  2436. }
  2437.  
  2438. public override void OnStart(PartModule.StartState state)
  2439. {
  2440. base.OnStart(state);
  2441.  
  2442.  
  2443.  
  2444. #region Sound and FX
  2445.  
  2446. if (hasScrew == true)
  2447. {
  2448. Transform turnSubScrew = part.FindModelTransform("subScrew");
  2449. engThrustTransform = part.FindModelTransform("thrustTransform");
  2450.  
  2451. propWash = (GameObject)GameObject.Instantiate(UnityEngine.Resources.Load("Effects/fx_smokeTrail_medium"));
  2452. //propWash.transform.localScale = new Vector3(this.part.rescaleFactor, this.part.rescaleFactor, this.part.rescaleFactor); //will this be affected by rescaling in KSP?
  2453. propWash.particleEmitter.emit = false;
  2454. propWash.transform.parent = turnSubScrew;
  2455. propWash.transform.localPosition = new Vector3(0, 0, 0);
  2456. propWash.transform.localRotation = this.part.transform.rotation;
  2457. propWash.particleEmitter.useWorldSpace = true;
  2458. propWash.particleEmitter.minSize = 1.25f * (this.part.rescaleFactor * 0.75f);
  2459. propWash.particleEmitter.maxSize = 1.5f * (this.part.rescaleFactor * 0.75f);
  2460. propWash.particleEmitter.localVelocity = new Vector3(0, 0, 0);
  2461. propWash.particleEmitter.minEnergy = 1.4f * (this.part.rescaleFactor * 0.75f);
  2462. propWash.particleEmitter.maxEnergy = 1.8f * (this.part.rescaleFactor * 0.75f); // how long do the particles linger?
  2463. propWash.particleEmitter.minEmission = 50f;
  2464. propWash.particleEmitter.angularVelocity = 0.0075f;
  2465. propWash.particleEmitter.rndRotation = true;
  2466.  
  2467.  
  2468. if (!GameDatabase.Instance.ExistsAudioClip("BoatPartsR3/Sounds/propFX")) return;
  2469. //print("getting past the sound condition 1");
  2470. soundGroup.audio = gameObject.AddComponent<AudioSource>();
  2471.  
  2472. if (soundGroup.audio != null)
  2473. {
  2474. //print("getting past the sound condition 2");
  2475. soundGroup.audio.volume = GameSettings.SHIP_VOLUME;
  2476. soundGroup.audio.rolloffMode = AudioRolloffMode.Linear;
  2477. soundGroup.audio.dopplerLevel = 1f;
  2478. soundGroup.audio.panLevel = 0.85f;
  2479. soundGroup.audio.clip = GameDatabase.Instance.GetAudioClip("BoatPartsR3/Sounds/propFX");
  2480. soundGroup.audio.loop = true;
  2481. soundGroup.audio.playOnAwake = false;
  2482. }
  2483. #endregion
  2484.  
  2485.  
  2486. /* emitter.minSize = blastradius * 1.5f; //other possibilities
  2487. emitter.maxSize = blastradius * 2;
  2488. emitter.minEnergy = blastradius * 0.15f;
  2489. emitter.maxEnergy = blastradius * 0.2f;
  2490. emitter.rndVelocity = Vector3.one * 0.6f;
  2491. particleEmitter.angularVelocity = 100;
  2492.  
  2493. fx_gasBurst_white //other fx types
  2494. fx_gasJet_tiny
  2495. fx_gasJet_white
  2496. fx_smokeTrail_light
  2497. fx_smokeTrail_medium
  2498. fx_splashdown
  2499. fx_waterSurface*/
  2500.  
  2501. }
  2502.  
  2503. }
  2504.  
  2505. public void FixedUpdate()
  2506. {
  2507. if (!HighLogic.LoadedSceneIsFlight) return;
  2508. {
  2509. if (this.vessel == FlightGlobals.ActiveVessel && hasScrew == true)
  2510. {
  2511. Transform turnSubScrew = part.FindModelTransform("subScrew");
  2512.  
  2513. if (useTTransform == false)
  2514. {
  2515. if (thrustDirection == "up")
  2516. {
  2517. useThrustDir = turnSubScrew.transform.up;
  2518. }
  2519. else if (thrustDirection == "down")
  2520. {
  2521. useThrustDir = -turnSubScrew.transform.up;
  2522. }
  2523. else if (thrustDirection == "forward")
  2524. {
  2525. useThrustDir = turnSubScrew.transform.forward;
  2526. }
  2527. else if (thrustDirection == "backward")
  2528. {
  2529. useThrustDir = -turnSubScrew.transform.forward;
  2530. }
  2531. else if (thrustDirection == "right")
  2532. {
  2533. useThrustDir = turnSubScrew.transform.right;
  2534. }
  2535. else if (thrustDirection == "left")
  2536. {
  2537. useThrustDir = -turnSubScrew.transform.right;
  2538. }
  2539. }
  2540. else
  2541. {
  2542. if (thrustDirection == "up")
  2543. {
  2544. useThrustDir = engThrustTransform.transform.up;
  2545. }
  2546. else if (thrustDirection == "down")
  2547. {
  2548. useThrustDir = -engThrustTransform.transform.up;
  2549. }
  2550. else if (thrustDirection == "forward")
  2551. {
  2552. useThrustDir = engThrustTransform.transform.forward;
  2553. }
  2554. else if (thrustDirection == "backward")
  2555. {
  2556. useThrustDir = -engThrustTransform.transform.forward;
  2557. }
  2558. else if (thrustDirection == "right")
  2559. {
  2560. useThrustDir = engThrustTransform.transform.right;
  2561. }
  2562. else if (thrustDirection == "left")
  2563. {
  2564. useThrustDir = -engThrustTransform.transform.right;
  2565. }
  2566. }
  2567.  
  2568. //print(useThrustDir);
  2569.  
  2570. if (doItOnce == true)
  2571. {
  2572. storeOldMaxThrust = maxRpm;
  2573. storeOldHeatProd = heatProduction;
  2574. storeOldThrustScale = thrustScale;
  2575. doItOnce = false;
  2576.  
  2577. foreach (Part p in this.vessel.parts)
  2578. {
  2579.  
  2580. if (p.FindModelTransform("bowPoint") == true)
  2581. {
  2582.  
  2583. bowPoint = p.FindModelTransform("bowPoint");
  2584.  
  2585. }
  2586.  
  2587. }
  2588.  
  2589. }
  2590.  
  2591. bool doAlterThrust = false;
  2592.  
  2593. if (this.vessel.altitude < -1.0 && partAltitude <= -8.0)
  2594. {
  2595. isUnderWater = true;
  2596. doAlterThrust = true;
  2597. }
  2598. else if (this.vessel.altitude > -1.0 && partAltitude > -8.0)
  2599. {
  2600. isUnderWater = false;
  2601. thrustScale = storeOldThrustScale;
  2602. }
  2603.  
  2604. if (hasHsUD == true && doAlterThrust == true) //be sure this isn't engaged on top of the water... that would be bad!
  2605. {
  2606. //thrustScale = Mathf.SmoothStep(storeOldThrustScale, hSUDMulti, 5f);
  2607. if (thrustScale < hSUDMulti)
  2608. {
  2609. thrustScale = storeOldThrustScale + 0.001f;
  2610. }
  2611. }
  2612.  
  2613. if (engineActive == true)
  2614. {
  2615. if (togRevThrust == false)
  2616. {
  2617. float currentSpeed = ((maxRpm * FlightInputHandler.state.mainThrottle) * Time.deltaTime);
  2618. turnSubScrew.transform.Rotate(Vector3.forward * currentSpeed);
  2619. maxThrust = ((this.part.mass * 0.35f) * (((maxRpm * thrustScale) * FlightInputHandler.state.mainThrottle) * fwdLimiter) * oOfWaterLimiter);
  2620. this.part.temperature *= (heatProduction * Time.deltaTime);
  2621. }
  2622. else if (togRevThrust == true)
  2623. {
  2624. float currentSpeed = ((-maxRpm * FlightInputHandler.state.mainThrottle) * Time.deltaTime);
  2625. turnSubScrew.transform.Rotate(Vector3.forward * currentSpeed);
  2626. maxThrust = ((this.part.mass * 0.35f) * (((maxRpm * thrustScale) * FlightInputHandler.state.mainThrottle) * revLimiter) * oOfWaterLimiter) * -1f;
  2627. this.part.temperature *= (heatProduction * Time.deltaTime);
  2628. }
  2629. }
  2630. else
  2631. {
  2632. maxThrust = 0f;
  2633. }
  2634.  
  2635. partAltitude = Vector3.Distance(engThrustTransform.position, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  2636. if (bowPoint != null)
  2637. {
  2638. bowAltitude = Vector3.Distance(bowPoint.position, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  2639. }
  2640.  
  2641. if (bowPoint != null && bowAltitude > 0.0d)
  2642. {
  2643. oOfWaterLimiter = 0f;
  2644. }
  2645. else if (bowPoint != null && bowAltitude < 0.0d || bowPoint == null)
  2646. {
  2647. oOfWaterLimiter = 1f;
  2648.  
  2649. }
  2650.  
  2651. if (partAltitude > 0.0d) //no more engine power out of the water!
  2652. {
  2653.  
  2654. propWash.particleEmitter.emit = false;
  2655.  
  2656. }
  2657. else if (partAltitude <= 0.0d)
  2658. {
  2659. //engine.maxThrust = storeOldMaxThrust;
  2660.  
  2661.  
  2662. if (this.rigidbody != null)
  2663. {
  2664. this.rigidbody.AddForceAtPosition((useThrustDir * maxThrust) * Time.deltaTime, turnSubScrew.position, ForceMode.Impulse);
  2665. }
  2666.  
  2667. soundGroup.audio.volume = (GameSettings.SHIP_VOLUME * FlightGlobals.ActiveVessel.ctrlState.mainThrottle); //nice! :)
  2668.  
  2669. if (FlightGlobals.ActiveVessel.ctrlState.mainThrottle >= 0.1f && engineActive == true)
  2670. {
  2671. if (soundTripped == false)
  2672. {
  2673. soundTripped = true;
  2674. soundGroup.audio.Play();
  2675.  
  2676. }
  2677. propWash.particleEmitter.emit = true;
  2678.  
  2679. }
  2680. else if (FlightGlobals.ActiveVessel.ctrlState.mainThrottle < 0.1f || engineActive == false)
  2681. {
  2682. if (soundTripped == true)
  2683. {
  2684. soundTripped = false;
  2685. soundGroup.audio.Stop();
  2686. }
  2687. propWash.particleEmitter.emit = false;
  2688.  
  2689. }
  2690.  
  2691. }
  2692.  
  2693.  
  2694.  
  2695. }
  2696. }
  2697. }
  2698.  
  2699. public void ApplyDamage(float damageState)
  2700. {
  2701. float chanceJammed = rndNumber.rndRoll(0, 100);
  2702. if (chanceJammed < 12f)
  2703. {
  2704. heatProduction *= rndNumber.rndRoll(1,4); // causes a massive heat buildup.
  2705. }
  2706.  
  2707. if (damageState < 1f)//damaged
  2708. {
  2709. maxRpm = storeOldMaxThrust * damageState; // reduce RPM instead - as it gets damaged.
  2710. }
  2711. if (damageState == 1f)//normal/fixed state.... floats
  2712. {
  2713. maxRpm = storeOldMaxThrust;
  2714. heatProduction = storeOldHeatProd;
  2715. }
  2716. //print("idFloatCode: Setting damage state to " + damageState + " object volume is now " + objVolume);
  2717. }
  2718. }
  2719.  
  2720. public class rndNumber
  2721. {
  2722.  
  2723. public static System.Random rnd = new System.Random();
  2724. public static int rndRoll(int min, int max)
  2725. {
  2726. return rnd.Next(min, max);
  2727. }
  2728.  
  2729. public static System.Random rnd2 = new System.Random();
  2730. public static double rndDouble(double min, double max)
  2731. {
  2732. return rnd2.NextDouble();
  2733. }
  2734.  
  2735.  
  2736. }
  2737.  
  2738. //*************************************
  2739.  
  2740. public class IDrudder : PartModule //Thanks to Snjo for his ideas.
  2741. {
  2742. [KSPField]
  2743. public string animatedPart = "obj_ctrlSrf";
  2744. [KSPField]
  2745. public float range = 30f;
  2746. [KSPField]
  2747. public float manualRudderArea = 0f;
  2748. [KSPField]
  2749. public Vector3 pivotAxis = new Vector3(1f,0f,0f);
  2750. [KSPField]
  2751. public Vector3 useInputAxis = new Vector3(0f,0f,1f); //pitch, roll or yaw
  2752. [KSPField]
  2753. public string forceAxis = "forward";
  2754. [KSPField]
  2755. public int requiresWaterContact = 0;
  2756.  
  2757.  
  2758. private Transform rudderTransform;
  2759. private Transform rudderDefaultTransform;
  2760. private FlightCtrlState ctrl;
  2761. private float input = 0;
  2762. private float power = 0f;
  2763. private float rudderArea = 0f;
  2764. private float powerScale = 0.375f;
  2765. private float origAngDrag = 0f;
  2766. //*********************************************new damage code - for skillful
  2767. public float dmgModifier = 1f;
  2768. public float origRange = 30f;
  2769. private bool isJammed = false;
  2770. private float jammedAngle;
  2771. //private Transform rudderDirection;
  2772.  
  2773.  
  2774. public override void OnStart(PartModule.StartState state)
  2775. {
  2776. base.OnStart(state);
  2777. rudderTransform = part.FindModelTransform(animatedPart);
  2778. if (rudderTransform != null)
  2779. {
  2780. rudderDefaultTransform = new GameObject().transform;
  2781. rudderDefaultTransform.localRotation = rudderTransform.localRotation;
  2782. }
  2783.  
  2784. origAngDrag = this.part.angularDrag;
  2785. origRange = range;
  2786.  
  2787. //rudderDirection = this.part.FindModelTransform("thrustTransform");
  2788. }
  2789.  
  2790. public void FixedUpdate()
  2791. {
  2792. base.OnFixedUpdate();
  2793. if (!HighLogic.LoadedSceneIsFlight || !vessel.isActiveVessel) return;
  2794.  
  2795. if (manualRudderArea > 0f) //only uses this if it's specified in the cfg, otherwise calculates size from the first convex collider in the part/model.
  2796. {
  2797. rudderArea = manualRudderArea;
  2798. }
  2799. else
  2800. {
  2801. rudderArea = (this.part.collider.bounds.size.y * this.part.collider.bounds.size.x * this.part.collider.bounds.size.z); // should be metres3.
  2802. }
  2803.  
  2804. if (isJammed == true)
  2805. {
  2806. FlightInputHandler.state.yawTrim = jammedAngle;
  2807. }
  2808.  
  2809. power = (((float)Math.Sqrt(vessel.horizontalSrfSpeed)) * rudderArea) * dmgModifier;//gets more power from speed.
  2810. if (power < 0f) power = 0.01f; // geezus!
  2811.  
  2812. double partAltitude = Vector3.Distance(this.part.collider.transform.position, vessel.mainBody.position) - (float)vessel.mainBody.Radius;
  2813.  
  2814. ctrl = vessel.ctrlState;
  2815.  
  2816. Vector3 ctrlInputVector = new Vector3(ctrl.pitch, ctrl.roll, ctrl.yaw);
  2817. Vector3 inputVector = new Vector3(ctrlInputVector.x * useInputAxis.x, ctrlInputVector.y * useInputAxis.y, ctrlInputVector.z * useInputAxis.z);
  2818. if (inputVector.x != 0) input = inputVector.x;
  2819. else if (inputVector.y != 0) input = inputVector.y;
  2820. else input = inputVector.z;
  2821.  
  2822. if (input != 0f)
  2823. {
  2824. if (partAltitude <= 0.0d || requiresWaterContact == 0)
  2825. {
  2826. float forcetoAdd = (input * power * ((range * 0.1f)+1f)) * powerScale;
  2827.  
  2828. this.part.angularDrag = (float)this.vessel.horizontalSrfSpeed;
  2829. //Vector3 zeroSideMotion = new Vector3(0f, 0f, rigidbody.velocity.z);
  2830. //this.rigidbody.velocity = zeroSideMotion;
  2831.  
  2832. Vector3 transformDirection = new Vector3();
  2833. switch (forceAxis)
  2834. {
  2835. case "right":
  2836. transformDirection = transform.right;
  2837. break;
  2838. case "left":
  2839. transformDirection = -transform.right;
  2840. break;
  2841. case "up":
  2842. transformDirection = transform.up;
  2843. break;
  2844. case "down":
  2845. transformDirection = -transform.up;
  2846. break;
  2847. case "forward":
  2848. transformDirection = transform.forward;
  2849. break;
  2850. case "back":
  2851. transformDirection = -transform.forward;
  2852. break;
  2853. }
  2854.  
  2855.  
  2856.  
  2857. base.rigidbody.AddForceAtPosition(transformDirection * forcetoAdd, rudderTransform.position); // was using rudderTransform direction, this will be adapted later.
  2858. base.rigidbody.AddForce(-Vector3.Project(rigidbody.velocity, transform.right)); //add * resistance multiplier for reduction if needed.
  2859.  
  2860. if (rudderTransform != null)
  2861. {
  2862. rudderTransform.localRotation = rudderDefaultTransform.localRotation;
  2863. rudderTransform.Rotate(pivotAxis * input * range);
  2864. }
  2865. }
  2866. }
  2867. else
  2868. {
  2869. this.part.angularDrag = origAngDrag;
  2870.  
  2871. if (rudderTransform != null)
  2872. {
  2873. rudderTransform.localRotation = rudderDefaultTransform.localRotation;
  2874. }
  2875. }
  2876. }
  2877.  
  2878. public void ApplyDamage(float damageState)
  2879. {
  2880. float chanceJammed = rndNumber.rndRoll(0, 100);
  2881. if (chanceJammed < 12f)
  2882. {
  2883. isJammed = true;
  2884. jammedAngle = (float)rndNumber.rndDouble(0.05, 0.7); // 1 would be 100% of 30 which is the rudder max deg .167 is close to 5 degrees.
  2885.  
  2886. }
  2887.  
  2888. if (damageState < 1f)//normal/fixed state.... floats
  2889. {
  2890. this.origRange *= damageState; // should reduce float ability as it gets damaged.
  2891. }
  2892. if (damageState == 1f)//normal/fixed state.... floats
  2893. {
  2894. range = origRange;
  2895. isJammed = false;
  2896. }
  2897. //print("idFloatCode: Setting damage state to " + damageState + " object volume is now " + objVolume);
  2898. }
  2899. }
  2900.  
  2901. //*********************end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement