MegiRicky

USS NewJersey helm

Aug 8th, 2024 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.56 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.  
  8. -- If the user inputs a number, use it as the base channel, otherwise use the default
  9. local baseChannel = tonumber(inputChannel) or defaultBaseChannel
  10.  
  11. -- Calculate channels based on the base channel
  12. local stressChannel = baseChannel
  13. local leftEngineChannel = baseChannel + 1
  14. local rightEngineChannel = baseChannel + 2
  15. local fuelChannel = baseChannel + 3
  16. local accChannel = baseChannel + 4
  17.  
  18. -- Initialize peripherals
  19. local monitor = peripheral.find("monitor")
  20. local modem = peripheral.wrap("top")
  21. local controller = peripheral.find("tweaked_controller")
  22.  
  23. if not monitor then
  24. error("monitor not found")
  25. elseif not modem then
  26. error("modem not found")
  27. elseif not controller then
  28. error("controller not found")
  29. end
  30.  
  31. -- Open modem channels
  32. modem.open(stressChannel)
  33. modem.open(leftEngineChannel)
  34. modem.open(rightEngineChannel)
  35. modem.open(fuelChannel)
  36. modem.open(accChannel)
  37.  
  38. -- Define secret key for message validation
  39. local secretKey = "YourSecretKey123"
  40. local function isValidMessage(message)
  41. local key, msg = tostring(message):match("^(%w+)%:(.+)$")
  42. return key == secretKey, msg
  43. end
  44.  
  45. -- Initialize variables
  46. local stressCapacity, stressUsed, stress, fuel, energy, rpm, energyCurrent, energyCapacity, fuelUsed, fuelCapacity = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  47.  
  48. local w, h = monitor.getSize()
  49. local turnLevel = 0
  50. local rpm = 0
  51. local displayState = "main"
  52. local shipYaw = 0
  53. local shipSpeed = 0
  54.  
  55. local function toDegrees(radians)
  56. return radians * (180 / math.pi)
  57. end
  58.  
  59. -- Function to convert degrees to a bearing
  60. local function toBearing(degrees)
  61. local bearing = degrees % 360
  62. if bearing < 0 then
  63. bearing = 360 + bearing
  64. end
  65. return bearing
  66. end
  67.  
  68. -- Listen for coordinates
  69. local function listenForCoordinates()
  70. while true do
  71. local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  72. local valid, validatedMessage = isValidMessage(message)
  73. if valid then
  74. if senderChannel == stressChannel then
  75. stress = validatedMessage
  76. elseif senderChannel == fuelChannel then
  77. fuel = validatedMessage
  78. elseif senderChannel == accChannel then
  79. energy = validatedMessage
  80. end
  81. end
  82. end
  83. end
  84.  
  85. -- Draw a progress bar
  86. local function drawProgressBar(x, y, length, currentValue, maxValue, barColor, bgColor)
  87. local fillLength = math.floor((currentValue / maxValue) * length)
  88. local fill = string.rep(" ", fillLength)
  89. local empty = string.rep(" ", length - fillLength)
  90.  
  91. monitor.setCursorPos(x, y)
  92. monitor.setBackgroundColor(barColor or colors.white)
  93. monitor.write(fill)
  94. monitor.setBackgroundColor(bgColor or colors.black)
  95. monitor.write(empty)
  96. end
  97.  
  98. local function drawTurning(x, y, turnLevel)
  99. local maxTurnLevel = 3
  100. local barLength = 10 -- Length of the turning bar
  101.  
  102. -- Calculate the position of the turning indicator
  103. local turnPos = math.floor((turnLevel + maxTurnLevel) / (maxTurnLevel * 2) * barLength)
  104.  
  105. -- Draw the turning bar
  106. for i = 0, barLength do
  107. monitor.setCursorPos(x + i, y)
  108. if i == turnPos then
  109. monitor.setBackgroundColor(colors.black) -- Background color for the indicator
  110. monitor.write("=") -- Turning indicator
  111. else
  112. if i > turnPos then
  113. monitor.setBackgroundColor(colors.red) -- Background color to the right of the indicator
  114. else
  115. monitor.setBackgroundColor(colors.blue) -- Background color to the left of the indicator
  116. end
  117. monitor.write(" ")
  118. end
  119. end
  120.  
  121. -- Draw the label
  122. monitor.setCursorPos(x+2, y - 1)
  123. monitor.setBackgroundColor(colors.lightGray)
  124. monitor.write("Turning")
  125. monitor.setCursorPos(x+2, y + 1)
  126. monitor.write("Level: " .. turnLevel)
  127. monitor.setCursorPos(x + 3, y + 2)
  128. monitor.write("- / +")
  129.  
  130. -- Reset background color
  131. monitor.setBackgroundColor(colors.lightGray)
  132. end
  133.  
  134. -- Round a number to two decimal places
  135. local function round(num)
  136. return math.floor(num * 100 + 0.5) / 100
  137. end
  138.  
  139. -- Draw the throttle indicator
  140. local function drawThrottle(x, y, rpm, label)
  141. local maxRPM = 256
  142. local minRPM = -256
  143. local barLength = 10 -- Length of the throttle bar
  144.  
  145. -- Calculate the position of the throttle indicator
  146. local throttlePos = math.floor((rpm - minRPM) / (maxRPM - minRPM) * barLength)
  147.  
  148. -- Draw the throttle bar
  149. for i = 0, barLength do
  150. monitor.setCursorPos(x, y + i)
  151. if i == throttlePos then
  152. monitor.setBackgroundColor(colors.black) -- Background color for the indicator
  153. monitor.write("=") -- Throttle indicator
  154. else
  155. if i > throttlePos then
  156. monitor.setBackgroundColor(colors.red) -- Background color below the indicator
  157. else
  158. monitor.setBackgroundColor(colors.blue) -- Background color above the indicator
  159. end
  160. monitor.write(" ")
  161. end
  162. end
  163.  
  164. -- Draw the label
  165. monitor.setCursorPos(x - 3, y - 1)
  166. monitor.setBackgroundColor(colors.lightGray)
  167. monitor.write(label)
  168. monitor.setCursorPos(x - 3, y + barLength + 1)
  169. monitor.write("RPM: " .. rpm)
  170. monitor.setCursorPos(x - 2, y + barLength + 2)
  171. monitor.write("- / +")
  172.  
  173. -- Reset background color
  174. monitor.setBackgroundColor(colors.lightGray)
  175. end
  176.  
  177. -- Update the RPM based on user input
  178. local function updateRPM(rpm, turnLevel)
  179. local maxRPM = 256
  180. local minRPM = -256
  181. if rpm > maxRPM then
  182. rpm = maxRPM
  183. elseif rpm < minRPM then
  184. rpm = minRPM
  185. end
  186. local leftRPM = rpm
  187. local rightRPM = rpm
  188.  
  189. if turnLevel > 3 then
  190. turnLevel = 3
  191. elseif turnLevel < -3 then
  192. turnLevel = -3
  193. end
  194. if turnLevel == 0 then
  195. modem.transmit(leftEngineChannel, 0, secretKey .. ":" .. rpm)
  196. modem.transmit(rightEngineChannel, 0, secretKey .. ":" .. rpm)
  197. redstone.setOutput("front",false)
  198. redstone.setOutput("back",false)
  199. elseif turnLevel < 0 then
  200. redstone.setOutput("front",false)
  201. redstone.setOutput("back",true)
  202. if turnLevel == -2 then
  203. modem.transmit(leftEngineChannel, 0, secretKey .. ":" .. 0)
  204. modem.transmit(rightEngineChannel, 0, secretKey .. ":" .. rpm)
  205. elseif turnLevel == -3 then
  206. modem.transmit(leftEngineChannel, 0, secretKey .. ":" .. -256)
  207. modem.transmit(rightEngineChannel, 0, secretKey .. ":" .. 256)
  208. else
  209. modem.transmit(leftEngineChannel, 0, secretKey .. ":" .. rpm)
  210. modem.transmit(rightEngineChannel, 0, secretKey .. ":" .. rpm)
  211. end
  212. elseif turnLevel > 0 then
  213. redstone.setOutput("front",true)
  214. redstone.setOutput("back",false)
  215. if turnLevel == 2 then
  216. modem.transmit(leftEngineChannel, 0, secretKey .. ":" .. rpm)
  217. modem.transmit(rightEngineChannel, 0, secretKey .. ":" .. 0)
  218. elseif turnLevel == 3 then
  219. modem.transmit(leftEngineChannel, 0, secretKey .. ":" .. 256)
  220. modem.transmit(rightEngineChannel, 0, secretKey .. ":" .. -256)
  221. else
  222. modem.transmit(leftEngineChannel, 0, secretKey .. ":" .. rpm)
  223. modem.transmit(rightEngineChannel, 0, secretKey .. ":" .. rpm)
  224. end
  225. end
  226. end
  227.  
  228. local waypoints = {}
  229. local function addWaypoint(x, z, color)
  230. local newWaypoint = {
  231. x = x,
  232. z = z,
  233. color = color or colors.purple
  234. }
  235. table.insert(waypoints, newWaypoint)
  236. end
  237.  
  238. local function displayWaypoints()
  239. local startY = 5
  240. for i, waypoint in ipairs(waypoints) do
  241. local pos = ship.getWorldspacePosition()
  242. local deltaX = waypoint.x - pos.x
  243. local deltaZ = waypoint.z - pos.z
  244. local distance = math.sqrt(deltaX^2 + deltaZ^2)
  245. monitor.setCursorPos(20, startY + i - 1)
  246. monitor.write("W" .. i .. ": (" .. waypoint.x .. ", " .. waypoint.z .. ")".." Dist: "..round(distance))
  247. monitor.write(" Delete")
  248. end
  249. end
  250.  
  251. local function drawWaypointPage()
  252. monitor.clear()
  253. monitor.setCursorPos(1, 1)
  254. monitor.write("Open the computer terminal")
  255. monitor.setCursorPos(1, 2)
  256. monitor.write("to input waypoint coordinates.")
  257. end
  258.  
  259. -- Listen for touch events to increase or decrease RPM
  260. local function handleTouch()
  261. while true do
  262. local event, side, x, y = os.pullEvent("monitor_touch")
  263. if y == 22 then -- Adjust based on the throttle position
  264. if x == 4 and rpm > -256 then -- Throttle decrease position
  265. rpm = rpm - 16 -- Decrease RPM
  266. elseif x == 8 and rpm < 256 then -- Throttle increase position
  267. rpm = rpm + 16 -- Increase RPM
  268. end
  269. end
  270. if y == 7 then -- Adjust based on the throttle position
  271. if x == 4 and turnLevel > -3 then -- Throttle decrease position
  272. turnLevel = turnLevel - 1 -- Decrease RPM
  273. elseif x == 8 and turnLevel < 3 then -- Throttle increase position
  274. turnLevel = turnLevel + 1 -- Increase RPM
  275. end
  276. end
  277. if y == 4 and x > 20 then
  278. displayState = "waypoints"
  279. drawWaypointPage()
  280. print("Enter waypoint coordinates (x, z)")
  281. print("Note: Waypoint within 200 blocks will be automatically deleted")
  282. local input = read()
  283. local x, z = input:match("([^,]+),([^,]+)")
  284. x = tonumber(x)
  285. z = tonumber(z)
  286. if x and z then
  287. addWaypoint(x, z)
  288. displayState = "main" -- Switch back to main display after adding waypoint
  289. else
  290. print("Invalid input. Please enter coordinates as 'x, z'")
  291. end
  292. end
  293. local startY = 5
  294. for i, waypoint in ipairs(waypoints) do
  295. if y == startY + i - 1 and x > #("Waypoint " .. i .. ": (" .. waypoint.x .. ", " .. waypoint.z .. ")") then
  296. table.remove(waypoints, i)
  297. break
  298. end
  299. end
  300. end
  301. end
  302.  
  303. -- Listen for controller input to increase or decrease RPM
  304. local function handleController()
  305. while true do
  306. local yAxis = controller.getAxis(2)
  307. if yAxis < 0 and rpm > -256 then
  308. rpm = rpm - 16
  309. elseif yAxis > 0 and rpm < 256 then
  310. rpm = rpm + 16
  311. end
  312. sleep(0.1)
  313. local xAxis = controller.getAxis(1)
  314. if xAxis < 0 and turnLevel > -3 then
  315. turnLevel = turnLevel - 1
  316. elseif xAxis > 0 and turnLevel < 3 then
  317. turnLevel = turnLevel + 1
  318. end
  319. end
  320. end
  321.  
  322. local function drawMainControls()
  323. monitor.clear()
  324. monitor.setTextScale(0.5)
  325. local matrix = ship.getRotationMatrix()
  326. shipYaw = toBearing(toDegrees(math.atan2(-matrix[1][3], matrix[3][3]))) - 180
  327. if shipYaw < 0 then
  328. shipYaw = shipYaw + 360
  329. end
  330. local velocity = ship.getVelocity()
  331. shipSpeed = math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2) * 1.94384449
  332. local pos = ship.getWorldspacePosition() -- ship position table
  333.  
  334. -- Check if the required variables are updated by the listenForCoordinates function
  335. if stress and energy and fuel and shipYaw and shipSpeed then
  336. local stressUsed, stressCapacity = string.match(stress, "(%d+)%:(%d+%.?%d*)")
  337. local energyCurrent, energyCapacity = string.match(energy, "(%d+)%:(%d+%.?%d*)")
  338. local fuelUsed, fuelCapacity = string.match(fuel, "(%d+)%:(%d+%.?%d*)") -- Parses "2816 / 46080"
  339.  
  340. -- Convert to numbers
  341. stressUsed = tonumber(stressUsed)
  342. stressCapacity = tonumber(stressCapacity)
  343. energyCurrent = tonumber(energyCurrent)
  344. energyCapacity = tonumber(energyCapacity)
  345. fuelUsed = tonumber(fuelUsed)
  346. fuelCapacity = tonumber(fuelCapacity)
  347.  
  348. -- Stress progress bar
  349. if stressUsed and stressCapacity then
  350. monitor.setCursorPos(1, 1)
  351. monitor.setTextColor(colors.white)
  352. monitor.write("Stress: ")
  353. drawProgressBar(9, 1, 10, stressUsed, stressCapacity, colors.red, colors.lightGray)
  354. monitor.setCursorPos(20, 1) -- Adjust as necessary for your display size
  355. monitor.write(stressUsed .. "/" .. stressCapacity)
  356. else
  357. monitor.setCursorPos(1, 1)
  358. monitor.write("Stress: Not connected")
  359. end
  360.  
  361. -- Fuel progress bar
  362. if fuelUsed and fuelCapacity then
  363. monitor.setCursorPos(1, 2)
  364. monitor.setTextColor(colors.white)
  365. monitor.write("Fuel: " .. fuelUsed .. "/" .. fuelCapacity .. " will last: " .. round(fuelUsed / (72 / 160)) .. "s")
  366. else
  367. monitor.setCursorPos(1, 2)
  368. monitor.write("Fuel: Not connected")
  369. end
  370.  
  371. -- Energy progress bar
  372. if energyCurrent and energyCapacity then
  373. monitor.setCursorPos(1, 3)
  374. monitor.setTextColor(colors.white)
  375. monitor.write("Energy: ")
  376. drawProgressBar(9, 3, 10, energyCurrent, energyCapacity, colors.green, colors.lightGray)
  377. monitor.setCursorPos(20, 3) -- Adjust as necessary for your display size
  378. monitor.write(energyCurrent .. "/" .. energyCapacity)
  379. else
  380. monitor.setCursorPos(1, 3)
  381. monitor.write("Energy: Not connected")
  382. end
  383.  
  384. -- Speed
  385. monitor.setCursorPos(40, 1)
  386. print(shipYaw)
  387. monitor.write("Speed: " .. round(shipSpeed) .. " knots")
  388.  
  389. -- Heading
  390. monitor.setCursorPos(40, 2)
  391. shipYaw = toBearing(toDegrees(math.atan2(-matrix[1][3], matrix[3][3]))) - 180
  392. if shipYaw < 0 then
  393. shipYaw = shipYaw + 360
  394. end
  395. monitor.write("Heading: " .. round(shipYaw))
  396. end
  397.  
  398. -- Waypoint
  399. monitor.setCursorPos(20, 4)
  400. monitor.write("Add waypoint")
  401.  
  402. -- Draw engine throttle
  403. drawThrottle(6, 10, rpm, "Throttle")
  404. drawTurning(1, 5, turnLevel)
  405. displayWaypoints()
  406. updateRPM(rpm, turnLevel)
  407. end
  408.  
  409. local function calculateBearingAndAdjustTurning()
  410. if #waypoints == 0 then
  411. return -- No waypoints to navigate to
  412. end
  413.  
  414. local pos = ship.getWorldspacePosition()
  415. local waypoint = waypoints[1] -- Assuming navigating to the first waypoint
  416.  
  417. -- Calculate the true bearing
  418. local deltaX = waypoint.x - pos.x
  419. local deltaZ = waypoint.z - pos.z
  420. local trueBearing = math.deg(math.atan2(-deltaX, deltaZ)) - 180
  421.  
  422. if trueBearing < 0 then
  423. trueBearing = trueBearing + 360
  424. end
  425. print("trueBearing: " .. trueBearing)
  426.  
  427. -- Calculate deltaYaw
  428. local deltaYaw = trueBearing - shipYaw
  429. if deltaYaw > 180 then
  430. deltaYaw = deltaYaw - 360
  431. elseif deltaYaw < -180 then
  432. deltaYaw = deltaYaw + 360
  433. end
  434. print("deltaYaw: " .. deltaYaw)
  435.  
  436. -- Calculate the distance to the waypoint
  437. local distance = math.sqrt(deltaX^2 + deltaZ^2)
  438.  
  439. -- Check if the ship is within 200 blocks of the waypoint
  440. if distance <= 200 then
  441. table.remove(waypoints, 1) -- Remove the first waypoint from the list
  442. return -- Exit the function early since the waypoint is reached
  443. end
  444.  
  445. -- Adjust turning level based on distance and deltaYaw
  446. if math.abs(deltaYaw) > 5 then
  447. if (distance < 500 and math.abs(deltaYaw) > 10) or math.abs(deltaYaw) > 70 then
  448. turnLevel = -3
  449. elseif (distance < 1000 and math.abs(deltaYaw) > 10) or math.abs(deltaYaw) > 40 then
  450. turnLevel = -2
  451. else
  452. turnLevel = -1
  453. end
  454.  
  455. if deltaYaw > 0 then
  456. turnLevel = -turnLevel -- Turn right
  457. end
  458. else
  459. turnLevel = 0
  460. end
  461. updateRPM(rpm, turnLevel) -- Update turning with the calculated turn level
  462. end
  463.  
  464. -- Main control loop
  465. parallel.waitForAny(
  466. function()
  467. -- Main loop
  468. while true do
  469. if displayState == "main" then
  470. drawMainControls()
  471. calculateBearingAndAdjustTurning() -- Adjust turning based on waypoint
  472. end
  473. sleep(0.1)
  474. end
  475. end,
  476. handleTouch,
  477. handleController,
  478. listenForCoordinates
  479. )
  480.  
Advertisement
Add Comment
Please, Sign In to add comment