Guest User

Untitled

a guest
Apr 22nd, 2026
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. -- airship_control.lua
  2. -- Modes:
  3. -- F = flight
  4. -- V = vertical anchor (hold Y only)
  5. -- A = full anchor (hold X/Y/Z)
  6. -- Q = quit
  7. --
  8. -- Works with CC:VS direct force control.
  9.  
  10. if not ship then
  11. error("No ship API found. Put this computer on a VS ship.")
  12. end
  13.  
  14. local running = true
  15. local mode = "flight" -- flight / vertical / full
  16. local status = "Ready"
  17.  
  18. local anchorY = nil
  19. local anchorPos = nil
  20.  
  21. local CFG = {
  22. gravityBias = 10.00,
  23.  
  24. -- Vertical mode
  25. Py_v = 3.2,
  26. Dy_v = 6.2,
  27. maxAccelY_v = 10.0,
  28.  
  29. -- Full anchor mode
  30. Pxy = 2.0,
  31. Dxy = 3.8,
  32. Py_f = 3.2,
  33. Dy_f = 6.2,
  34. maxAccelXY = 8.0,
  35. maxAccelY_f = 10.0,
  36.  
  37. fallbackDt = 0.05,
  38. }
  39.  
  40. local last = {
  41. pos = vector.new(0,0,0),
  42. vel = vector.new(0,0,0),
  43. mass = 0,
  44. err = vector.new(0,0,0),
  45. accel = vector.new(0,0,0),
  46. force = vector.new(0,0,0),
  47. }
  48.  
  49. local function clamp(v, mn, mx)
  50. if v < mn then return mn end
  51. if v > mx then return mx end
  52. return v
  53. end
  54.  
  55. local function clear()
  56. term.setCursorPos(1, 1)
  57. term.clear()
  58. end
  59.  
  60. local function setFlight()
  61. mode = "flight"
  62. anchorY = nil
  63. anchorPos = nil
  64. status = "Flight mode"
  65. end
  66.  
  67. local function setVerticalAnchor()
  68. local pos = ship.getWorldspacePosition()
  69. anchorY = pos.y
  70. anchorPos = nil
  71. mode = "vertical"
  72. status = ("Vertical anchor set at Y %.2f"):format(anchorY)
  73. end
  74.  
  75. local function setFullAnchor()
  76. local pos = ship.getWorldspacePosition()
  77. anchorPos = vector.new(pos.x, pos.y, pos.z)
  78. anchorY = pos.y
  79. mode = "full"
  80. status = ("Full anchor set at %.2f / %.2f / %.2f"):format(anchorPos.x, anchorPos.y, anchorPos.z)
  81. end
  82.  
  83. local function draw()
  84. clear()
  85. print("== Airship Control ==")
  86. print("Mode : " .. mode)
  87.  
  88. if anchorY then
  89. print(("Anchor Y: %.2f"):format(anchorY))
  90. else
  91. print("Anchor Y: nil")
  92. end
  93.  
  94. if anchorPos then
  95. print(("Anchor X: %.2f"):format(anchorPos.x))
  96. print(("Anchor Z: %.2f"):format(anchorPos.z))
  97. else
  98. print("Anchor X: nil")
  99. print("Anchor Z: nil")
  100. end
  101.  
  102. print(("Pos X : %.2f"):format(last.pos.x))
  103. print(("Pos Y : %.2f"):format(last.pos.y))
  104. print(("Pos Z : %.2f"):format(last.pos.z))
  105. print(("Vel X : %.3f"):format(last.vel.x))
  106. print(("Vel Y : %.3f"):format(last.vel.y))
  107. print(("Vel Z : %.3f"):format(last.vel.z))
  108. print(("Mass : %.1f"):format(last.mass))
  109. print("")
  110. print("F = flight")
  111. print("V = hold current Y")
  112. print("A = full anchor here")
  113. print("Q = quit")
  114. print("")
  115. print("Status: " .. status)
  116. end
  117.  
  118. local function physicsStep()
  119. local pos = ship.getWorldspacePosition()
  120. local vel = ship.getVelocity()
  121. local mass = ship.getMass()
  122.  
  123. last.pos = pos
  124. last.vel = vel
  125. last.mass = mass
  126. last.err = vector.new(0,0,0)
  127. last.accel = vector.new(0,0,0)
  128. last.force = vector.new(0,0,0)
  129.  
  130. if mode == "flight" then
  131. return
  132. end
  133.  
  134. local ax, ay, az = 0, 0, 0
  135.  
  136. if mode == "vertical" and anchorY then
  137. local errY = anchorY - pos.y
  138. ay = (CFG.Py_v * errY) - (CFG.Dy_v * vel.y)
  139. ay = clamp(ay, -CFG.maxAccelY_v, CFG.maxAccelY_v)
  140.  
  141. last.err = vector.new(0, errY, 0)
  142. last.accel = vector.new(0, ay, 0)
  143.  
  144. elseif mode == "full" and anchorPos then
  145. local err = anchorPos - pos
  146.  
  147. ax = (CFG.Pxy * err.x) - (CFG.Dxy * vel.x)
  148. ay = (CFG.Py_f * err.y) - (CFG.Dy_f * vel.y)
  149. az = (CFG.Pxy * err.z) - (CFG.Dxy * vel.z)
  150.  
  151. ax = clamp(ax, -CFG.maxAccelXY, CFG.maxAccelXY)
  152. ay = clamp(ay, -CFG.maxAccelY_f, CFG.maxAccelY_f)
  153. az = clamp(az, -CFG.maxAccelXY, CFG.maxAccelXY)
  154.  
  155. last.err = err
  156. last.accel = vector.new(ax, ay, az)
  157. end
  158.  
  159. local force = vector.new(
  160. mass * ax,
  161. mass * (CFG.gravityBias + ay),
  162. mass * az
  163. )
  164.  
  165. ship.applyWorldForce(force)
  166. last.force = force
  167. end
  168.  
  169. local function physicsLoop()
  170. while running do
  171. local ok = pcall(function()
  172. ship.pullPhysicsTicks()
  173. end)
  174.  
  175. if not ok then
  176. sleep(CFG.fallbackDt)
  177. end
  178.  
  179. local ok2, err = pcall(physicsStep)
  180. if not ok2 then
  181. status = "Physics error: " .. tostring(err)
  182. end
  183.  
  184. draw()
  185. end
  186. end
  187.  
  188. local function inputLoop()
  189. while running do
  190. local _, key = os.pullEvent("key")
  191.  
  192. if key == keys.q then
  193. running = false
  194. status = "Stopping..."
  195.  
  196. elseif key == keys.f then
  197. setFlight()
  198.  
  199. elseif key == keys.v then
  200. setVerticalAnchor()
  201.  
  202. elseif key == keys.a then
  203. setFullAnchor()
  204. end
  205. end
  206. end
  207.  
  208. draw()
  209. parallel.waitForAny(physicsLoop, inputLoop)
  210.  
  211. clear()
  212. print("Stopped.")
Advertisement
Add Comment
Please, Sign In to add comment