Advertisement
Guest User

BoatParts r455 Jan28 2015

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