Advertisement
asarium

Untitled

Dec 23rd, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. #Conditional Hooks
  2.  
  3. $Application: FS2_Open
  4. $On Game Init:
  5. [
  6. debrisGravity = {}
  7.  
  8. function debrisGravity:init()
  9. self.gravity = 9.81
  10. self.enabled = false
  11.  
  12. self.ignoredObjects = {}
  13. self.groundShip = nil
  14. end
  15.  
  16. function debrisGravity:setGravityValue(val)
  17. if (type(val) ~= "number") then
  18. ba.warning("Invalid gravity value type '" .. type(val) .. "'!")
  19. return
  20. end
  21.  
  22. self.gravity = val
  23. end
  24.  
  25. function debrisGravity:setEnabled(bool)
  26. if (type(bool) ~= "boolean") then
  27. ba.warning("Invalid enabled state variable type '" .. type(val) .. "'!")
  28. return
  29. end
  30.  
  31. self.enabled = bool
  32. end
  33.  
  34. function debrisGravity:setGroundShip(ship)
  35. if (not ship:isValid()) then
  36. ba.warning("Specified ground ship is not valid!")
  37. return
  38. end
  39.  
  40. self.groundShip = ship
  41. end
  42.  
  43. function debrisGravity:isEnabled()
  44. if (not self.enabled) then
  45. return false
  46. end
  47.  
  48. return self.gravity ~= 0
  49. end
  50.  
  51. function debrisGravity:isDisabled(ship)
  52. -- This needs a special function added to the FSO code but is certainly the better way to do it
  53. -- return ship.Disabled
  54.  
  55. local disabled = true
  56.  
  57. local pos = ship.Position
  58.  
  59. for i=1, #ship do
  60. local subsys = ship[i]
  61.  
  62. if (subsys:isValid()) then
  63. local engineIndex = subsys.Name:lower():find("engine")
  64. if (engineIndex and engineIndex == 1) then -- This is most likely a hack!
  65. if (subsys.HitpointsLeft > 0) then
  66. disabled = false
  67. break
  68. end
  69. end
  70. end
  71. end
  72.  
  73. return disabled
  74. end
  75.  
  76. function debrisGravity:setIgnored(obj, bool)
  77. if (not obj or not obj:isValid()) then
  78. return
  79. end
  80.  
  81. if (bool) then
  82. self.ignoredObjects[obj:getSignature()] = obj
  83. else
  84. self.ignoredObjects[obj:getSignature()] = nil
  85. end
  86. end
  87.  
  88. function debrisGravity:isIgnored(obj)
  89. if (not obj:isValid()) then
  90. return false
  91. end
  92.  
  93. return self.ignoredObjects[obj:getSignature()] ~= nil
  94. end
  95.  
  96. function debrisGravity:applyGravity(obj, time)
  97. if (not obj:isValid()) then
  98. return
  99. end
  100.  
  101. if (self:isIgnored(obj)) then
  102. return
  103. end
  104.  
  105. local vec = ba.createVector(0, -1, 0)
  106. vec = vec * self.gravity * time
  107. local newVec = obj.Physics.Velocity + vec
  108.  
  109. obj.Physics.Velocity = newVec
  110. end
  111.  
  112. function debrisGravity:onFrame(time)
  113. if (not self:isEnabled()) then
  114. return
  115. end
  116.  
  117. for i=1, #mn.Debris do
  118. local debris = mn.Debris[i]
  119.  
  120. self:applyGravity(debris, time)
  121. end
  122.  
  123. for i=1, #mn.Ships do
  124. local ship = mn.Ships[i]
  125.  
  126. if (ship.FlagAffectedByGravity) then
  127. if (ship:hasShipExploded() ~= 0 or self:isDisabled(ship)) then
  128. self:applyGravity(ship, time)
  129. end
  130. end
  131. end
  132. end
  133.  
  134. function debrisGravity:onDebrisCollision()
  135. if (not self.groundShip or not self.groundShip:isValid()) then
  136. return
  137. end
  138.  
  139. if (hv.Self == self.groundShip) then
  140. hv.Debris.Physics.Velocity = ba.createVector(0,0,0)
  141.  
  142. self:setIgnored(hv.Debris, true)
  143.  
  144. ba.error("Added debris id " .. tostring(hv.Debris:getSignature()) .. " to ignored objects...")
  145. end
  146. end
  147.  
  148. debrisGravity:init()
  149.  
  150. function g_setG(value)
  151. if type(value) ~= "number" then
  152. ba.warning("Invalid type given to g_setG!")
  153. return
  154. end
  155.  
  156. debrisGravity:setGravityValue(value)
  157. end
  158.  
  159. function g_enable()
  160. debrisGravity:setEnabled(true)
  161. end
  162.  
  163. function g_disable()
  164. debrisGravity:setEnabled(false)
  165. end
  166.  
  167. function g_sGr(value)
  168. local ship = mn.Ships[value]
  169.  
  170. if ship == nil or not ship:isValid() then
  171. ba.warning("Invalid value given to g_sGr! '" .. tostring(value) .. "' is not a valid to identify a ship!")
  172. return
  173. end
  174.  
  175. debrisGravity:setGroundShip(ship)
  176. end
  177.  
  178. function g_ign(ship)
  179. local ship = mn.Ships[value]
  180.  
  181. if ship == nil or not ship:isValid() then
  182. ba.warning("Invalid value given to g_ign! '" .. tostring(value) .. "' is not a valid to identify a ship!")
  183. return
  184. end
  185.  
  186. debrisGravity:setIgnored(ship, true)
  187. end
  188.  
  189. function g_nign(ship)
  190. local ship = mn.Ships[value]
  191.  
  192. if ship == nil or not ship:isValid() then
  193. ba.warning("Invalid value given to g_nign! '" .. tostring(value) .. "' is not a valid to identify a ship!")
  194. return
  195. end
  196.  
  197. debrisGravity:setIgnored(ship, false)
  198. end
  199. ]
  200.  
  201. $On Debris Collision:
  202. [
  203. debrisGravity:onDebrisCollision()
  204. ]
  205.  
  206. $State: GS_STATE_GAME_PLAY
  207. $On Frame:
  208. [
  209. debrisGravity:onFrame(ba.getFrametime())
  210. ]
  211.  
  212. #End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement