Advertisement
squidingtin

computer

Nov 25th, 2024 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.19 KB | None | 0 0
  1. -- Controlling Computer Script
  2. local modem = peripheral.find("modem")
  3. if not modem then
  4.     error("No modem found! Attach a modem.")
  5. end
  6.  
  7. local turtleChannel = 23
  8. local replyChannel = 24
  9. modem.open(replyChannel)
  10.  
  11. -- Set up the turtle's initial state
  12. local turtlePos = { x = 0, y = 0, z = 0 }
  13. local turtleFacing = 0 -- 0 = North, 1 = East, 2 = South, 3 = West
  14.  
  15. -- Save scan data to disk
  16. local function saveScanData(data)
  17.     local file = fs.open("scan_data.txt", "a")
  18.     if file then
  19.         for _, block in ipairs(data) do
  20.             file.writeLine(textutils.serialize(block))
  21.         end
  22.         file.close()
  23.         print("Scan data saved to 'scan_data.txt'.")
  24.     else
  25.         print("Failed to save scan data.")
  26.     end
  27. end
  28.  
  29. -- Update turtle position based on movement
  30. local function updatePosition(direction)
  31.     if direction == "forward" then
  32.         if turtleFacing == 0 then
  33.             turtlePos.z = turtlePos.z - 1
  34.         elseif turtleFacing == 1 then
  35.             turtlePos.x = turtlePos.x + 1
  36.         elseif turtleFacing == 2 then
  37.             turtlePos.z = turtlePos.z + 1
  38.         elseif turtleFacing == 3 then
  39.             turtlePos.x = turtlePos.x - 1
  40.         end
  41.     elseif direction == "up" then
  42.         turtlePos.y = turtlePos.y + 1
  43.     elseif direction == "down" then
  44.         turtlePos.y = turtlePos.y - 1
  45.     end
  46. end
  47.  
  48. -- Rotate facing direction
  49. local function updateFacing(turn)
  50.     if turn == "left" then
  51.         turtleFacing = (turtleFacing - 1) % 4
  52.     elseif turn == "right" then
  53.         turtleFacing = (turtleFacing + 1) % 4
  54.     end
  55. end
  56.  
  57. -- Send a command and handle the response with a timeout
  58. local function sendCommand(command)
  59.     print("Sending command:", textutils.serialize(command))
  60.     modem.transmit(turtleChannel, replyChannel, textutils.serialize(command))
  61.     local startTime = os.clock()
  62.     local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  63.    
  64.     -- Check if the response is coming within a reasonable time frame
  65.     while os.clock() - startTime < 5 do  -- Timeout after 5 seconds
  66.         if replyChannel == turtleChannel then
  67.             print("Received message:", message)  -- Debugging line
  68.             return textutils.unserialize(message)
  69.         end
  70.         event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  71.     end
  72.    
  73.     print("No response received within timeout.")  -- Debugging line
  74.     return nil  -- No response
  75. end
  76.  
  77. -- Render the display
  78. local function renderDisplay(fuel, coords)
  79.     term.clear()
  80.     term.setCursorPos(1, 1)
  81.     print(string.format("Fuel: %.2f%%", fuel))
  82.  
  83.     term.setCursorPos(term.getSize() - 12, 1)
  84.     print(string.format("Coords: X=%d Y=%d Z=%d", coords.x, coords.y, coords.z))
  85.  
  86.     term.setCursorPos(1, term.getSize() - 2)
  87.     term.clearLine()
  88.     term.setCursorPos(math.floor(term.getSize() / 2) - 15, term.getSize() - 2)
  89.     print("Controls: W=Forward A/D=Turn Q/E=Up/Down Space+Dir=Dig R=Save")
  90. end
  91.  
  92. -- Main loop
  93. local fuelLevel = 0
  94. local fuelLimit = 0
  95. while true do
  96.     -- Periodically request fuel level and fuel limit
  97.     local fuelResponse = sendCommand({ action = "getFuel" })
  98.     if fuelResponse then
  99.         fuelLevel = fuelResponse.fuel or 0
  100.         fuelLimit = fuelResponse.limit or 0
  101.     end
  102.  
  103.     -- Calculate fuel percentage if limit is available
  104.     local fuelPercentage = (fuelLimit > 0) and (fuelLevel / fuelLimit * 100) or 0
  105.  
  106.     renderDisplay(fuelPercentage, turtlePos)
  107.  
  108.     local event, key = os.pullEvent("key")
  109.     local command = {}
  110.  
  111.     -- Define the key-command mappings in a table
  112.     local keyMappings = {
  113.         [keys.q] = { action = "move", direction = "down" },
  114.         [keys.e] = { action = "move", direction = "up" },
  115.         [keys.w] = { action = "move", direction = "forward" },
  116.         [keys.a] = { action = "turn", direction = "left" },
  117.         [keys.s] = { action = "move", direction = "back" },
  118.         [keys.d] = { action = "turn", direction = "right" },
  119.         [keys.space] = { action = "move", direction = "digDown" }, --turtle.digDown
  120.         [keys.r] = "scan" -- Special handling for scan
  121.     }
  122.  
  123.     -- Listen for the key event
  124.     if keyMappings[key] then
  125.         if keyMappings[key] == "dig" then
  126.             print("Press direction key to dig")
  127.             local _, digKey = os.pullEvent("key")
  128.             if keyMappings[digKey] and keyMappings[digKey].action == "move" then
  129.                 command = { action = "dig", direction = keyMappings[digKey].direction }
  130.             end
  131.         elseif keyMappings[key] == "scan" then
  132.             local scanResponse = sendCommand({ action = "scan" })
  133.             if scanResponse and scanResponse.blocks then
  134.                 saveScanData(scanResponse.blocks)
  135.             end
  136.         else
  137.             command = keyMappings[key]
  138.         end
  139.     end
  140.  
  141.  
  142.     if next(command) ~= nil then
  143.         local response = sendCommand(command)
  144.         if response then
  145.             if response.success then
  146.                 if command.action == "move" then
  147.                     updatePosition(command.direction)
  148.                 elseif command.action == "turn" then
  149.                     updateFacing(command.direction)
  150.                 end
  151.                 renderDisplay(fuelPercentage, turtlePos)  -- Refresh display after action
  152.             else
  153.                 print("Command failed:", textutils.serialize(command))
  154.             end
  155.         else
  156.             print("No response for command:", textutils.serialize(command))
  157.         end
  158.     end
  159. end
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement