Advertisement
DOGGYWOOF

Untitled

Oct 6th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 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. -- Unload all fuel (like coal) from the turtle's inventory to the ground
  64. local function unloadFuel()
  65. for i = 1, 16 do
  66. turtle.select(i)
  67. local itemCount = turtle.getItemCount(i)
  68. if itemCount > 0 and turtle.getItemDetail(i) and turtle.getItemDetail(i).name == "minecraft:coal" then
  69. turtle.drop(itemCount) -- Drop all coal items in the selected slot
  70. term.setCursorPos(3, 8)
  71. term.setTextColor(colors.green)
  72. term.write("Unloaded " .. itemCount .. " coal from slot " .. i .. ".")
  73. term.setTextColor(colors.white) -- Reset to default color
  74. end
  75. end
  76. end
  77.  
  78. -- Move the turtle forward
  79. local function moveForward()
  80. if turtle.forward() then
  81. displayFuelLevel()
  82. else
  83. term.setCursorPos(3, 8)
  84. term.setTextColor(colors.red)
  85. term.write("Move failed!")
  86. term.setTextColor(colors.white) -- Reset to default color
  87. end
  88. end
  89.  
  90. -- Move the turtle backward
  91. local function moveBackward()
  92. if turtle.back() then
  93. displayFuelLevel()
  94. else
  95. term.setCursorPos(3, 8)
  96. term.setTextColor(colors.red)
  97. term.write("Move failed!")
  98. term.setTextColor(colors.white) -- Reset to default color
  99. end
  100. end
  101.  
  102. -- Turn the turtle left and update direction
  103. local function turnLeft(direction)
  104. turtle.turnLeft()
  105. direction = (direction - 1) % 4
  106. return direction
  107. end
  108.  
  109. -- Turn the turtle right and update direction
  110. local function turnRight(direction)
  111. turtle.turnRight()
  112. direction = (direction + 1) % 4
  113. return direction
  114. end
  115.  
  116. -- Initialize direction
  117. local direction = 0 -- 0: North, 1: East, 2: South, 3: West
  118.  
  119. -- Start the GUI
  120. clear()
  121. drawBorder()
  122. displayFuelLevel()
  123. displayDirection(direction)
  124. displayBackupFuel()
  125.  
  126. -- Main loop to handle user input
  127. while true do
  128. local event, key = os.pullEvent("key")
  129.  
  130. if key == keys.w then
  131. moveForward()
  132. elseif key == keys.s then
  133. moveBackward()
  134. elseif key == keys.a then
  135. direction = turnLeft(direction)
  136. elseif key == keys.d then
  137. direction = turnRight(direction)
  138. elseif key == keys.l then
  139. loadFuel()
  140. elseif key == keys.u then
  141. unloadFuel() -- Unload coal when 'U' is pressed
  142. elseif key == keys.q then
  143. break -- Exit the program
  144. end
  145.  
  146. -- Refresh the display
  147. clear()
  148. drawBorder()
  149. displayFuelLevel()
  150. displayDirection(direction)
  151. displayBackupFuel()
  152. end
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement