MegiRicky

shimakazePocketHelm

Sep 10th, 2024 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.98 KB | None | 0 0
  1. -- Define default base channel
  2. local defaultBaseChannel = 200
  3.  
  4. -- Prompt user for base channel input
  5. print("Enter base channel (press Enter to use default: " .. defaultBaseChannel .. "):")
  6. local inputChannel = read()
  7. if inputChannel == "" then
  8. inputChannel = 200
  9. end
  10. inputChannel = tonumber(inputChannel)
  11.  
  12. local statusChannel = inputChannel
  13. local controlChannel = inputChannel + 1
  14.  
  15. -- Initialize peripherals
  16. local modem = peripheral.wrap("back")
  17. local controller = peripheral.find("tweaked_controller")
  18.  
  19. if not modem then
  20. error("modem not found")
  21. end
  22.  
  23. -- Open modem channel
  24. modem.open(statusChannel)
  25.  
  26. -- Initialize variables
  27. local stressCapacity, stressUsed, fuelUsed, fuelCapacity, energyCurrent, energyCapacity = 0, 0, 0, 0, 0, 0
  28. local turnLevel, rpm = 0, 0
  29. local shipYaw, shipSpeed = 0, 0
  30. local waypoints = {}
  31. local mode = "display"
  32.  
  33. local function toDegrees(radians)
  34. return radians * (180 / math.pi)
  35. end
  36.  
  37. -- Function to convert degrees to a bearing
  38. local function toBearing(degrees)
  39. local bearing = degrees % 360
  40. if bearing < 0 then
  41. bearing = 360 + bearing
  42. end
  43. return bearing
  44. end
  45.  
  46. -- Listen for coordinates
  47. local function listenForCoordinates()
  48. while true do
  49. local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  50. if senderChannel == statusChannel then
  51. local data = textutils.unserialize(message)
  52. if data then
  53. stressUsed = data.stress and data.stress.usedSU or stressUsed
  54. stressCapacity = data.stress and data.stress.SUCapacity or stressCapacity
  55. fuelUsed = data.fuel and data.fuel.totalBlazeCakes or fuelUsed
  56. fuelCapacity = data.fuel and data.fuel.total or fuelCapacity
  57. energyCurrent = data.accumulator and data.accumulator.accEnergy or energyCurrent
  58. energyCapacity = data.accumulator and data.accumulator.accCapacity or energyCapacity
  59. shipYaw = data.yaw or shipYaw
  60. shipSpeed = data.speed or shipSpeed
  61. end
  62. end
  63. end
  64. end
  65.  
  66. -- Display the data in text form
  67. local function displayData()
  68. while true do
  69. if mode == "display" then
  70. term.clear()
  71. term.setCursorPos(1, 1)
  72.  
  73. -- Display Stress Data
  74. if stressUsed and stressCapacity then
  75. print("Stress: " .. stressUsed .. "/" .. stressCapacity)
  76. else
  77. print("Stress: Not connected")
  78. end
  79.  
  80. -- Display Fuel Data
  81. if fuelUsed and fuelCapacity then
  82. print("Fuel: " .. fuelUsed .. "/" .. fuelCapacity)
  83. else
  84. print("Fuel: Not connected")
  85. end
  86.  
  87. -- Display Energy Data
  88. if energyCurrent and energyCapacity then
  89. print("Energy: " .. energyCurrent .. "/" .. energyCapacity)
  90. else
  91. print("Energy: Not connected")
  92. end
  93.  
  94. -- Display Speed and Heading
  95. print("Speed: " .. shipSpeed .. " knots")
  96. print("Heading: " .. shipYaw)
  97.  
  98. -- Display Turn Level and RPM
  99. print("Turn Level: " .. turnLevel)
  100. print("RPM: " .. rpm)
  101. print("statusChannel: "..statusChannel)
  102. print("controlChannel"..controlChannel)
  103.  
  104. -- Display Waypoints
  105. if #waypoints > 0 then
  106. print("Waypoints:")
  107. for i, waypoint in ipairs(waypoints) do
  108. print("W" .. i .. ": (" .. waypoint.x .. ", " .. waypoint.z .. ")")
  109. end
  110. else
  111. print("No waypoints")
  112. end
  113. end
  114.  
  115. sleep() -- Update every second
  116. end
  117. end
  118.  
  119. -- Update the RPM and transmit control signals
  120. -- Update the RPM and transmit control signals
  121. local function updateRPMAndTurn(rpm, turnLevel)
  122. local leftSpeed = rpm
  123. local rightSpeed = rpm
  124.  
  125. if turnLevel == -2 then
  126. leftSpeed = 0
  127. elseif turnLevel == -3 then
  128. leftSpeed = -256
  129. rightSpeed = 256
  130. elseif turnLevel == 2 then
  131. rightSpeed = 0
  132. elseif turnLevel == 3 then
  133. rightSpeed = -256
  134. leftSpeed = 256
  135. end
  136.  
  137. local controlData = {
  138. leftSpeed = leftSpeed,
  139. rightSpeed = rightSpeed,
  140. turnLevel = turnLevel
  141. }
  142. modem.transmit(controlChannel, controlChannel, controlData)
  143. end
  144.  
  145.  
  146. -- Handle key events for WASD controls and adding waypoints
  147. local function handleKeyPress()
  148. while true do
  149. local event, key = os.pullEvent("key")
  150.  
  151. if key == keys.w then
  152. rpm = rpm + 16
  153. if rpm > 256 then rpm = 256 end
  154. elseif key == keys.s then
  155. rpm = rpm - 16
  156. if rpm < -256 then rpm = -256 end
  157. elseif key == keys.a then
  158. turnLevel = turnLevel - 1
  159. if turnLevel < -3 then turnLevel = -3 end
  160. elseif key == keys.d then
  161. turnLevel = turnLevel + 1
  162. if turnLevel > 3 then turnLevel = 3 end
  163. elseif key == keys.r then
  164. mode = "waypoint"
  165. print("Enter waypoint coordinates (x, z):")
  166. local input = read()
  167. local x, z = input:match("([^,]+),([^,]+)")
  168. x = tonumber(x)
  169. z = tonumber(z)
  170. if x and z then
  171. table.insert(waypoints, {x = x, z = z})
  172. print("Waypoint added: (" .. x .. ", " .. z .. ")")
  173. else
  174. print("Invalid input. Please enter coordinates as 'x, z'")
  175. end
  176. mode = "display"
  177. elseif key == keys.q then
  178. if rpm > 0 then
  179. rpm = rpm - 16
  180. elseif rpm < 0 then
  181. rpm = rpm + 16
  182. end
  183.  
  184. if turnLevel < 0 then
  185. turnLevel = turnLeft + 1
  186. elseif turnLevel > 0 then
  187. turnLevel = turnLevel - 1
  188. end
  189. end
  190.  
  191. updateRPMAndTurn(rpm, turnLevel)
  192. end
  193. end
  194.  
  195. local function calculateBearingAndAdjustTurning()
  196. if #waypoints == 0 then
  197. return -- No waypoints to navigate to
  198. end
  199.  
  200. local pos = ship.getWorldspacePosition()
  201. local waypoint = waypoints[1] -- Assuming navigating to the first waypoint
  202.  
  203. -- Calculate the true bearing
  204. local deltaX = waypoint.x - pos.x
  205. local deltaZ = waypoint.z - pos.z
  206. local trueBearing = math.deg(math.atan2(-deltaX, deltaZ)) - 180
  207.  
  208. if trueBearing < 0 then
  209. trueBearing = trueBearing + 360
  210. end
  211.  
  212. -- Calculate deltaYaw
  213. local deltaYaw = trueBearing - shipYaw
  214. if deltaYaw > 180 then
  215. deltaYaw = deltaYaw - 360
  216. elseif deltaYaw < -180 then
  217. deltaYaw = deltaYaw + 360
  218. end
  219.  
  220. -- Calculate the distance to the waypoint
  221. local distance = math.sqrt(deltaX^2 + deltaZ^2)
  222.  
  223. -- Check if the ship is within 200 blocks of the waypoint
  224. if distance <= 200 then
  225. table.remove(waypoints, 1) -- Remove the first waypoint from the list
  226. return -- Exit the function early since the waypoint is reached
  227. end
  228.  
  229. -- Adjust turning level based on distance and deltaYaw
  230. if math.abs(deltaYaw) > 5 then
  231. if (distance < 500 and math.abs(deltaYaw) > 10) or math.abs(deltaYaw) > 70 then
  232. turnLevel = -3
  233. elseif (distance < 1000 and math.abs(deltaYaw) > 10) or math.abs(deltaYaw) > 40 then
  234. turnLevel = -2
  235. else
  236. turnLevel = -1
  237. end
  238.  
  239. if deltaYaw > 0 then
  240. turnLevel = -turnLevel -- Turn right
  241. end
  242. else
  243. turnLevel = 0
  244. end
  245. updateRPMAndTurn(rpm, turnLevel) -- Update turning with the calculated turn level
  246. end
  247.  
  248.  
  249. local modem = peripheral.find("modem") or error("No modem attached", 0)
  250.  
  251. print("Input the controlChannel, default = 500")
  252. local controlChannel = io.read()
  253. if controlChannel == "" then
  254. controlChannel = 500
  255. end
  256. controlChannel = tonumber(controlChannel)
  257. modem.open(controlChannel) -- Open a channel to communicate
  258.  
  259. print("Remote control started. Use WASD keys to control movement, e(up) q(down) for suspension, Space for cannon, Shift for autocannon, T for smoke grenades, Tab to switch cannon control mode.")
  260.  
  261. local controls = {
  262. accelerate = false,
  263. decelerate = false,
  264. turnLeft = false,
  265. turnRight = false,
  266. suspensionUp = false,
  267. suspensionDown = false,
  268. fireCannon = false,
  269. fireAutocannon = false,
  270. launchSmokeGrenade = false,
  271. cannonControlMode = "manual",
  272. cannonUp = false,
  273. cannonDown = false,
  274. cannonLeft = false,
  275. cannonRight = false
  276. }
  277.  
  278. local keyMap = {
  279. w = "accelerate",
  280. s = "decelerate",
  281. a = "turnLeft",
  282. d = "turnRight",
  283. e = "suspensionUp",
  284. q = "suspensionDown",
  285. space = "fireCannon",
  286. leftShift = "fireAutocannon",
  287. t = "launchSmokeGrenade",
  288. tab = "switchMode",
  289. up = "cannonUp",
  290. down = "cannonDown",
  291. left = "cannonLeft",
  292. right = "cannonRight"
  293. }
  294.  
  295. local function sendControls()
  296. modem.transmit(controlChannel, controlChannel, controls)
  297. end
  298.  
  299. local function VLScontrol()
  300. while true do
  301. local event, param1, param2 = os.pullEvent()
  302.  
  303. if event == "key" then
  304. -- Key press event
  305. for k, control in pairs(keyMap) do
  306. if param1 == keys[k] then
  307. if control == "switchMode" then
  308. -- Switch cannon control mode when Tab is pressed
  309. if controls.cannonControlMode == "manual" then
  310. controls.cannonControlMode = "mouseAim"
  311. print("Switched to Mouse Aim mode")
  312. else
  313. controls.cannonControlMode = "manual"
  314. print("Switched to Manual Control mode")
  315. end
  316. else
  317. controls[control] = true
  318. end
  319. sendControls()
  320. print("Key pressed:", k, control)
  321. end
  322. end
  323.  
  324. elseif event == "key_up" then
  325. -- Key release event
  326. for k, control in pairs(keyMap) do
  327. if param1 == keys[k] then
  328. if control ~= "switchMode" then
  329. controls[control] = false
  330. sendControls()
  331. print("Key released:", k, control)
  332. end
  333. end
  334. end
  335. end
  336. end
  337. end
  338.  
  339. -- Main control loop
  340. parallel.waitForAny(
  341. function()
  342. -- Main loop
  343. while true do
  344. calculateBearingAndAdjustTurning() -- Adjust turning based on waypoint
  345. sleep()
  346. end
  347. end,
  348. displayData,
  349. listenForCoordinates,
  350. handleKeyPress,
  351. VLScontrol
  352. )
  353.  
Advertisement
Add Comment
Please, Sign In to add comment