Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- PASTE 2TLTxYmF
- local modem = peripheral.find("modem")
- modem.open(1)
- -- Check connection of this turtle
- local function checkConnection(channel, replyChannel, params, dist)
- modem.transmit(replyChannel,channel,"CheckConnection true")
- end
- -- Get this turtle's local map
- local function getMap(channel, replyChannel, params, dist)
- if ~fs.exists("map") then
- local file = fs.open("map","w")
- file.write("0 0")
- file.close()
- modem.transmit(replyChannel,channel,"GetMap 0 0")
- else
- local file = fs.open("map","r")
- modem.transmit(replyChannel,channel,"GetMap " .. file.readAll())
- file.close()
- end
- end
- -- Set this turtle's local map
- local function setMap(channel, replyChannel, params, dist)
- local file = fs.open("map","w")
- local content = ""
- for i,v in ipairs(params) do
- content = content .. v
- end
- file.write(content)
- file.close()
- end
- -- Move to absolute coordinates
- local function goAbsolute(channel, replyChannel, params, dist)
- end
- -- Move to relative coordinates
- local function goRelative(channel, replyChannel, params, dist)
- end
- -- Explore and map surroundings
- local function explore(channel, replyChannel, params, dist)
- end
- local COMMANDS = {
- {name = "CheckConnection", func = checkConnection},
- {name = "GetMap", func = getMap},
- {name = "SetMap", func = setMap}
- {name = "GoAbsolute", func = goAbsolute},
- {name = "GoRelative", func = goRelative},
- {name = "Explore", func = explore}
- }
- -- Main program logic
- while true do
- -- Wait for commands from server
- local event, peripheral_name, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- -- When a command is received, look through the COMMANDS table to see if it is valid
- local commandName = string.gsub(string.sub(message, 1, string.find(message, " ") or string.len(message)), " ", "")
- for i, v in ipairs(COMMANDS) do
- -- If the command is found, check for params then execute
- if v.name == commandName then
- -- Check for params
- local spaceI = string.find(message, " ")
- local params = {}
- if spaceI then
- local p = ""
- for i = spaceI + 1, string.len(message) do
- local curChar = string.sub(message, i, i)
- if curChar ~= " " then
- p = p .. curChar
- else
- params[#params + 1] = p
- p = ""
- end
- end
- params[#params + 1] = p
- end
- -- Execute the command
- v.func(channel, replyChannel, params, distance)
- end
- end
- end
Add Comment
Please, Sign In to add comment