Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local rChan = 7322
- local sChan = 7321
- local modem = peripheral.wrap("right")
- modem.open(rChan)
- local slot = 1
- turtle.select(1)
- local function select(s)
- slot = s
- turtle.select(s)
- end
- -- TODO: Check if it really IS a modem
- local function send(msg)
- modem.transmit(sChan, rChan, msg)
- end
- local dir = 0
- local dirs = {
- up = 0,
- right = 1,
- down = 2,
- left = 3
- }
- local function turnRight()
- dir = dir + 1
- if dir == 4 then dir = 0 end
- turtle.turnRight()
- send("right")
- end
- local function turnLeft()
- dir = dir - 1
- if dir == -1 then dir = 3 end
- turtle.turnLeft()
- send("left")
- end
- local function faceDirection(d)
- if d == dir then return
- else
- if (d == 0 and dir == 3) or (d > dir and not (d == 3 and dir == 0)) then
- while d ~= dir do
- turnRight()
- end
- else
- while d ~= dir do
- turnLeft()
- end
- end
- end
- end
- local function quickLook()
- local found = turtle.detect()
- if found == true then found = 0 else found = -1 end
- send("block " .. found)
- end
- local function identifyLook()
- if turtle.detect() == false then return end
- for i=9,16 do
- select(i)
- -- turtle.compare() returns true if comparing nothing with nothing, we don't want that.
- if turtle.compare() and turtle.getItemCount(slot) > 0 then
- send("iblock " .. slot)
- return
- end
- end
- quickLook()
- end
- local function search()
- for i=1,4 do quickLook() turnRight()
- end
- end
- local function identifySearch()
- for i=1,4 do
- identifyLook()
- turnRight()
- end
- end
- local function forward()
- quickLook()
- if turtle.forward() then
- send("forward")
- quickLook()
- end
- end
- local function mine()
- for i=9,16 do
- select(i)
- if turtle.getItemSpace(i) ~= 0 and (turtle.getItemCount(i) == 0 or turtle.compare() == true) then
- if turtle.dig() then
- send("mine " .. slot .. " " .. turtle.getItemCount(slot))
- quickLook()
- else
- send("cantmine")
- end
- return
- end
- end
- send("nospace")
- end
- local function place(sl)
- if turtle.getItemCount(sl) == 0 then
- send("emptyslot")
- return
- end
- select(sl)
- if turtle.place() then
- send("place")
- identifyLook()
- else
- send("cantplace")
- end
- end
- local commands = {
- search = search,
- isearch = identifySearch,
- up = function()
- faceDirection(0)
- forward()
- end,
- down = function()
- faceDirection(2)
- forward()
- end,
- left = function()
- faceDirection(3)
- forward()
- end,
- right = function()
- faceDirection(1)
- forward()
- end,
- mine = mine,
- place = function(args)
- place(tonumber(args[2]))
- end
- }
- local function split(str)
- ret = {}
- for i in string.gmatch(str, "%S+") do
- table.insert(ret, i)
- end
- return ret
- end
- term.clear()
- term.setCursorPos(1, 1)
- write([[Welcome to the turtle "Roguelike"
- Run the other program on an advanced computer in order to control this turtle.
- The computer needs to have a modem attached at the top.
- (Modem channels 7322 and 7311 are used)
- To quit, press 'q'.]])
- while true do
- local ev, side, c, rc, msg, dist = os.pullEvent()
- if ev == "modem_message" then
- local smsg = split(msg)
- if commands[smsg[1]] ~= nil then
- commands[smsg[1]](smsg)
- end
- elseif ev == "char" and side == "q" then
- term.clear()
- term.setCursorPos(1, 1)
- return
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment