Advertisement
UrsusArctos

FtD Sea Vessel Automatic Stabilization

Aug 20th, 2015
958
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. --Automatic balancing and compensation
  2. vesselPitchCorrectTolerance = 5
  3. vesselRollCorrectTolerance = 5
  4. --How much tolerance you wish to have in your vessel pitch/roll, 5 meaning it'll deviate at most 5 degrees from level pitch/roll.
  5.  
  6. vesselHydrofoilPitchSpeed = 90
  7. --How fast you want your vessel's hydrofoils to pitch per GAME SECOND (it will slow down when game time slows down.)
  8. --This is in degrees per game second. Hydrofoils can be at most -45 or +45 degrees pitch.
  9. vesselPitchComp = true --Don't touch this. It's needed for calculation inside the code.
  10. function PitchCorrect(I)
  11.  
  12. currPitch = I:GetConstructPitch()
  13.  
  14. hydrofoilAftArray = {}
  15. hydrofoilForeArray = {}
  16. numHydrofoils = I:Component_GetCount(8)
  17. hydrofoilSpeed = (vesselHydrofoilPitchSpeed / 40)
  18.  
  19. j, k = 0, 0
  20.  
  21. for i=0,numHydrofoils do
  22. currHydrofoilPosition = I:Component_GetBlockInfo(8,i).LocalPositionRelativeToCom
  23.  
  24. if currHydrofoilPosition.z < 0 then
  25. hydrofoilAftArray[j] = i
  26. j = j + 1
  27. end
  28.  
  29. if currHydrofoilPosition.z > 0 then
  30. hydrofoilForeArray[k] = i
  31. k = k + 1
  32. end
  33.  
  34. end
  35. currHydrofoilAft = 0
  36. currHydrofoilFore = 0
  37. if table.getn(hydrofoilAftArray) > 0 then currHydrofoilAft = I:Component_GetFloatLogic(8, hydrofoilAftArray[1]) end
  38. if table.getn(hydrofoilForeArray) > 0 then currHydrofoilFore = I:Component_GetFloatLogic(8, hydrofoilForeArray[1]) end
  39.  
  40. if (currPitch > vesselPitchCorrectTolerance) and (currPitch <= 225) then
  41. I:RequestThrustControl(10)
  42. BouyancyCorrect(true, I)
  43. vesselPitchComp = false
  44.  
  45. if table.getn(hydrofoilAftArray) > 0 then
  46. currHydrofoilAft = currHydrofoilAft - hydrofoilSpeed
  47. for i=0,table.getn(hydrofoilAftArray) do
  48.  
  49. I:Component_SetFloatLogic(8, hydrofoilAftArray[i], currHydrofoilAft)
  50.  
  51. end
  52. end
  53.  
  54. if table.getn(hydrofoilForeArray) > 0 then
  55. currHydrofoilFore = currHydrofoilFore + hydrofoilSpeed
  56. for i=0,table.getn(hydrofoilForeArray) do
  57.  
  58. I:Component_SetFloatLogic(8, hydrofoilForeArray[i], currHydrofoilFore)
  59.  
  60. end
  61. end
  62.  
  63.  
  64. elseif (currPitch < (360 - vesselPitchCorrectTolerance)) and (currPitch > 225) then
  65. I:RequestThrustControl(11)
  66. BouyancyCorrect(true, I)
  67. vesselPitchComp = false
  68. if table.getn(hydrofoilAftArray) > 0 then
  69. currHydrofoilAft = currHydrofoilAft + hydrofoilSpeed
  70. for i=0,table.getn(hydrofoilAftArray) do
  71.  
  72. I:Component_SetFloatLogic(8, hydrofoilAftArray[i], currHydrofoilAft)
  73.  
  74. end
  75. end
  76.  
  77. if table.getn(hydrofoilForeArray) > 0 then
  78. currHydrofoilFore = currHydrofoilFore - hydrofoilSpeed
  79. for i=0,table.getn(hydrofoilForeArray) do
  80.  
  81. I:Component_SetFloatLogic(8, hydrofoilForeArray[i], currHydrofoilFore)
  82.  
  83. end
  84. end
  85.  
  86. else
  87. I:Component_SetFloatLogicAll(8,0)
  88. end
  89.  
  90. end
  91.  
  92. function RollCorrect(I)
  93.  
  94. currRoll = I:GetConstructRoll()
  95.  
  96. hydrofoilLeftArray = {}
  97. hydrofoilRightArray = {}
  98. numHydrofoils = I:Component_GetCount(8)
  99. hydrofoilSpeed = (vesselHydrofoilPitchSpeed / 40)
  100.  
  101. j, k = 0, 0
  102. for i=0,numHydrofoils do
  103. currHydrofoilPosition = I:Component_GetBlockInfo(8,i).LocalPositionRelativeToCom
  104.  
  105. if currHydrofoilPosition.x < 0 then
  106. hydrofoilLeftArray[j] = i
  107. j = j + 1
  108. end
  109.  
  110. if currHydrofoilPosition.x > 0 then
  111. hydrofoilRightArray[k] = i
  112. k = k + 1
  113. end
  114.  
  115. end
  116. currHydrofoilLeft = 0
  117. currHydrofoilRight = 0
  118. if table.getn(hydrofoilLeftArray) > 0 then currHydrofoilLeft = I:Component_GetFloatLogic(8, hydrofoilLeftArray[0]) end
  119. if table.getn(hydrofoilRightArray) > 0 then currHydrofoilRight = I:Component_GetFloatLogic(8, hydrofoilRightArray[0]) end
  120. if (currRoll > vesselRollCorrectTolerance) and (currRoll <= 180) then
  121. I:RequestThrustControl(6)
  122.  
  123. if table.getn(hydrofoilLeftArray) > 0 then
  124. currHydrofoilLeft = currHydrofoilLeft + hydrofoilSpeed
  125. for i=0,table.getn(hydrofoilLeftArray) do
  126.  
  127. I:Component_SetFloatLogic(8, hydrofoilLeftArray[i], currHydrofoilLeft)
  128.  
  129. end
  130. end
  131.  
  132. if table.getn(hydrofoilRightArray) > 0 then
  133. currHydrofoilRight = currHydrofoilRight - hydrofoilSpeed
  134. for i=0,table.getn(hydrofoilRightArray) do
  135.  
  136. I:Component_SetFloatLogic(8, hydrofoilRightArray[i], currHydrofoilRight)
  137.  
  138. end
  139. end
  140.  
  141.  
  142. elseif (currRoll < (360 - vesselRollCorrectTolerance)) and (currRoll > 180) then
  143. I:RequestThrustControl(7)
  144.  
  145. if table.getn(hydrofoilLeftArray) > 0 then
  146. currHydrofoilLeft = currHydrofoilLeft - hydrofoilSpeed
  147. for i=0,table.getn(hydrofoilLeftArray) do
  148.  
  149. I:Component_SetFloatLogic(8, hydrofoilLeftArray[i], currHydrofoilLeft)
  150.  
  151. end
  152.  
  153.  
  154. end
  155.  
  156. if table.getn(hydrofoilRightArray) > 0 then
  157. currHydrofoilRight = currHydrofoilRight + hydrofoilSpeed
  158. for i=0,table.getn(hydrofoilRightArray) do
  159.  
  160. I:Component_SetFloatLogic(8, hydrofoilRightArray[i], currHydrofoilRight)
  161.  
  162. end
  163. end
  164.  
  165. else
  166.  
  167. I:Component_SetFloatLogicAll(8,0)
  168.  
  169. end
  170.  
  171. end
  172.  
  173. function BouyancyCorrect(pitchComp, I)
  174.  
  175. currVertVelo = I:GetVelocityVector().y
  176. currBouyFactor = I:Component_GetFloatLogic(2,0)
  177.  
  178. if (currVertVelo < -0.1) and pitchComp then
  179.  
  180. currBouyFactor = currBouyFactor + 0.005
  181.  
  182. elseif (currVertVelo > 0.1) and pitchComp then
  183.  
  184. currBouyFactor = currBouyFactor - 0.005
  185.  
  186. elseif (currVertVelo < -0.1) and not pitchComp then
  187.  
  188. currBouyFactor = currBouyFactor - 0.005
  189.  
  190. elseif (currVertVelo > 0.1) and not pitchComp then
  191.  
  192. currBouyFactor = currBouyFactor + 0.005
  193.  
  194. end
  195.  
  196. I:Component_SetFloatLogicAll(2,currBouyFactor)
  197.  
  198. end
  199.  
  200.  
  201. function Update(I)
  202. vesselPitchComp = true
  203. PitchCorrect(I)
  204. RollCorrect(I)
  205. if vesselPitchComp == true then BouyancyCorrect(true, I) end
  206.  
  207. end
  208.  
  209. I:BindUpdateFunction(MyUpdateFunction)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement