Advertisement
DOGGYWOOF

Untitled

Oct 6th, 2024 (edited)
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. -- Clear the terminal screen
  2. local function clear()
  3. term.clear()
  4. term.setCursorPos(1, 1)
  5. end
  6.  
  7. -- Draw the main GUI border and header
  8. local function drawBorder()
  9. term.setBackgroundColor(colors.black)
  10. term.setTextColor(colors.white)
  11. term.setCursorPos(1, 1)
  12. term.write(string.rep("=", 30)) -- Top border
  13. term.setCursorPos(1, 2)
  14. term.write("|" .. string.rep(" ", 28) .. "|") -- Empty space for header
  15. term.setCursorPos(1, 3)
  16. term.write("| Turtle Control Panel |") -- Title
  17. term.setCursorPos(1, 4)
  18. term.write("|" .. string.rep(" ", 28) .. "|") -- Empty space
  19. for i = 5, 8 do
  20. term.setCursorPos(1, i)
  21. term.write("|" .. string.rep(" ", 28) .. "|") -- Side borders
  22. end
  23. term.setCursorPos(1, 9)
  24. term.write(string.rep("=", 30)) -- Bottom border
  25. end
  26.  
  27. -- Display the fuel level
  28. local function displayFuelLevel()
  29. local fuelLevel = turtle.getFuelLevel()
  30. term.setCursorPos(3, 5)
  31. term.setTextColor(fuelLevel < 10 and colors.red or colors.green)
  32. term.write("Fuel Level: " .. fuelLevel .. " units")
  33. term.setTextColor(colors.white) -- Reset to default color
  34. end
  35.  
  36. -- Display the direction the turtle is facing
  37. local function displayDirection(direction)
  38. term.setCursorPos(3, 6)
  39. term.write("Direction: " .. ({"North", "East", "South", "West"})[direction + 1])
  40. end
  41.  
  42. -- Display the amount of backup fuel available
  43. local function displayBackupFuel()
  44. local coalCount = turtle.getItemCount(1) -- Assuming coal is in slot 1
  45. term.setCursorPos(3, 7)
  46. term.write("Backup Fuel (Coal): " .. coalCount)
  47. end
  48.  
  49. -- Load fuel into the turtle
  50. local function loadFuel()
  51. if turtle.getItemCount(1) > 0 then
  52. turtle.select(1)
  53. turtle.refuel()
  54. displayFuelLevel()
  55. else
  56. term.setCursorPos(3, 8)
  57. term.setTextColor(colors.red)
  58. term.write("No fuel in slot 1.")
  59. term.setTextColor(colors.white) -- Reset to default color
  60. end
  61. end
  62.  
  63. -- Move the turtle forward
  64. local function moveForward()
  65. if turtle.forward() then
  66. displayFuelLevel()
  67. else
  68. term.setCursorPos(3, 8)
  69. term.setTextColor(colors.red)
  70. term.write("Move failed!")
  71. term.setTextColor(colors.white) -- Reset to default color
  72. end
  73. end
  74.  
  75. -- Move the turtle backward
  76. local function moveBackward()
  77. if turtle.back() then
  78. displayFuelLevel()
  79. else
  80. term.setCursorPos(3, 8)
  81. term.setTextColor(colors.red)
  82. term.write("Move failed!")
  83. term.setTextColor(colors.white) -- Reset to default color
  84. end
  85. end
  86.  
  87. -- Turn the turtle left and update direction
  88. local function turnLeft(direction)
  89. turtle.turnLeft()
  90. direction = (direction - 1) % 4
  91. return direction
  92. end
  93.  
  94. -- Turn the turtle right and update direction
  95. local function turnRight(direction)
  96. turtle.turnRight()
  97. direction = (direction + 1) % 4
  98. return direction
  99. end
  100.  
  101. -- Initialize direction
  102. local direction = 0 -- 0: North, 1: East, 2: South, 3: West
  103.  
  104. -- Start the GUI
  105. clear()
  106. drawBorder()
  107. displayFuelLevel()
  108. displayDirection(direction)
  109. displayBackupFuel()
  110.  
  111. -- Initialize Rednet
  112. rednet.open("top") -- Change to the correct side where the modem is attached
  113.  
  114. -- Function to execute commands received over Rednet
  115. local function executeRemoteCommand(command)
  116. if command == "forward" then
  117. moveForward()
  118. elseif command == "backward" then
  119. moveBackward()
  120. elseif command == "left" then
  121. direction = turnLeft(direction)
  122. elseif command == "right" then
  123. direction = turnRight(direction)
  124. elseif command == "load" then
  125. loadFuel()
  126. elseif command == "exit" then
  127. return false -- Exit the loop
  128. end
  129. return true -- Continue running
  130. end
  131.  
  132. -- Main loop to handle user input and remote commands
  133. while true do
  134. local event, key = os.pullEvent("key")
  135.  
  136. -- Check for local key commands
  137. if key == keys.w then
  138. moveForward()
  139. elseif key == keys.s then
  140. moveBackward()
  141. elseif key == keys.a then
  142. direction = turnLeft(direction)
  143. elseif key == keys.d then
  144. direction = turnRight(direction)
  145. elseif key == keys.l then
  146. loadFuel()
  147. elseif key == keys.q then
  148. break -- Exit the program
  149. end
  150.  
  151. -- Check for remote commands
  152. local senderId, message = rednet.receive()
  153. if message then
  154. if not executeRemoteCommand(message) then
  155. break -- Exit if the command is 'exit'
  156. end
  157. end
  158.  
  159. -- Refresh the display
  160. clear()
  161. drawBorder()
  162. displayFuelLevel()
  163. displayDirection(direction)
  164. displayBackupFuel()
  165. end
  166.  
  167. -- Close Rednet when done
  168. rednet.close("top")
  169.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement