Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Controlling Computer Script
- local modem = peripheral.find("modem")
- if not modem then
- error("No modem found! Attach a modem.")
- end
- local turtleChannel = 23
- local replyChannel = 24
- modem.open(replyChannel)
- -- Set up the turtle's initial state
- local turtlePos = { x = 0, y = 0, z = 0 }
- local turtleFacing = 0 -- 0 = North, 1 = East, 2 = South, 3 = West
- -- Save scan data to disk
- local function saveScanData(data)
- local file = fs.open("scan_data.txt", "a")
- if file then
- for _, block in ipairs(data) do
- file.writeLine(textutils.serialize(block))
- end
- file.close()
- print("Scan data saved to 'scan_data.txt'.")
- else
- print("Failed to save scan data.")
- end
- end
- -- Update turtle position based on movement
- local function updatePosition(direction)
- if direction == "forward" then
- if turtleFacing == 0 then
- turtlePos.z = turtlePos.z - 1
- elseif turtleFacing == 1 then
- turtlePos.x = turtlePos.x + 1
- elseif turtleFacing == 2 then
- turtlePos.z = turtlePos.z + 1
- elseif turtleFacing == 3 then
- turtlePos.x = turtlePos.x - 1
- end
- elseif direction == "up" then
- turtlePos.y = turtlePos.y + 1
- elseif direction == "down" then
- turtlePos.y = turtlePos.y - 1
- end
- end
- -- Rotate facing direction
- local function updateFacing(turn)
- if turn == "left" then
- turtleFacing = (turtleFacing - 1) % 4
- elseif turn == "right" then
- turtleFacing = (turtleFacing + 1) % 4
- end
- end
- -- Send a command and handle the response with a timeout
- local function sendCommand(command)
- print("Sending command:", textutils.serialize(command))
- modem.transmit(turtleChannel, replyChannel, textutils.serialize(command))
- local startTime = os.clock()
- local event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- -- Check if the response is coming within a reasonable time frame
- while os.clock() - startTime < 5 do -- Timeout after 5 seconds
- if replyChannel == turtleChannel then
- print("Received message:", message) -- Debugging line
- return textutils.unserialize(message)
- end
- event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- end
- print("No response received within timeout.") -- Debugging line
- return nil -- No response
- end
- -- Render the display
- local function renderDisplay(fuel, coords)
- term.clear()
- term.setCursorPos(1, 1)
- print(string.format("Fuel: %.2f%%", fuel))
- term.setCursorPos(term.getSize() - 12, 1)
- print(string.format("Coords: X=%d Y=%d Z=%d", coords.x, coords.y, coords.z))
- term.setCursorPos(1, term.getSize() - 2)
- term.clearLine()
- term.setCursorPos(math.floor(term.getSize() / 2) - 15, term.getSize() - 2)
- print("Controls: W=Forward A/D=Turn Q/E=Up/Down Space+Dir=Dig R=Save")
- end
- -- Main loop
- local fuelLevel = 0
- local fuelLimit = 0
- while true do
- -- Periodically request fuel level and fuel limit
- local fuelResponse = sendCommand({ action = "getFuel" })
- if fuelResponse then
- fuelLevel = fuelResponse.fuel or 0
- fuelLimit = fuelResponse.limit or 0
- end
- -- Calculate fuel percentage if limit is available
- local fuelPercentage = (fuelLimit > 0) and (fuelLevel / fuelLimit * 100) or 0
- renderDisplay(fuelPercentage, turtlePos)
- local event, key = os.pullEvent("key")
- local command = {}
- -- Define the key-command mappings in a table
- local keyMappings = {
- [keys.q] = { action = "move", direction = "down" },
- [keys.e] = { action = "move", direction = "up" },
- [keys.w] = { action = "move", direction = "forward" },
- [keys.a] = { action = "turn", direction = "left" },
- [keys.s] = { action = "move", direction = "back" },
- [keys.d] = { action = "turn", direction = "right" },
- [keys.space] = { action = "move", direction = "digDown" }, --turtle.digDown
- [keys.r] = "scan" -- Special handling for scan
- }
- -- Listen for the key event
- if keyMappings[key] then
- if keyMappings[key] == "dig" then
- print("Press direction key to dig")
- local _, digKey = os.pullEvent("key")
- if keyMappings[digKey] and keyMappings[digKey].action == "move" then
- command = { action = "dig", direction = keyMappings[digKey].direction }
- end
- elseif keyMappings[key] == "scan" then
- local scanResponse = sendCommand({ action = "scan" })
- if scanResponse and scanResponse.blocks then
- saveScanData(scanResponse.blocks)
- end
- else
- command = keyMappings[key]
- end
- end
- if next(command) ~= nil then
- local response = sendCommand(command)
- if response then
- if response.success then
- if command.action == "move" then
- updatePosition(command.direction)
- elseif command.action == "turn" then
- updateFacing(command.direction)
- end
- renderDisplay(fuelPercentage, turtlePos) -- Refresh display after action
- else
- print("Command failed:", textutils.serialize(command))
- end
- else
- print("No response for command:", textutils.serialize(command))
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement