Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Mocking the APIs for editor support
- -- Remove before running in minecraft
- --[[
- local rednet = {
- open = function(_) end,
- close = function(_) end,
- receive = function(_)
- return 0, "ok"
- end,
- }
- local os = {
- sleep = function(_) end,
- }
- local gps = {
- locate = function(_)
- return 0, 0, 0
- end,
- }
- local fs = {
- exists = function(_)
- return false
- end,
- open = function(_, _)
- return {
- writeLine = function(_) end,
- close = function(_) end,
- }
- end,
- }
- --]]
- local function getSide(x, z)
- if (x <= 0) and (z <= 0) then
- return "NW"
- end
- if (x > 0) and (z > 0) then
- return "SE"
- end
- if (x <= 0) and (z > 0) then
- return "SW"
- end
- if (x > 0) and (z <= 0) then
- return "NE"
- end
- end
- local function nextCoordsEvery(x, z, limit)
- local side = getSide(x, z)
- if side == "NW" then
- if z == 0 then
- return x, z + 1, false
- end
- return x - 1, z + 1, false
- end
- if side == "SE" then
- if z == 1 then
- return x, z - 1, false
- end
- return x + 1, z - 1, false
- end
- if side == "SW" then
- if x == 0 then
- return x + 1, z, false
- end
- return x + 1, z + 1, false
- end
- if side == "NE" then
- if x == 1 then
- if math.abs(z) == limit then
- return x, z, true
- end
- return x - 1, z - 1, false
- end
- return x - 1, z - 1, false
- end
- end
- local function nextCoords(x, z, limits)
- local x1, z1, limitReached = nextCoordsEvery(x, z, limits)
- if limitReached then
- return x1, z1, true
- elseif math.fmod(x1, 2) == 0 then
- return nextCoordsEvery(x1, z1, limits)
- else
- return x1, z1, false
- end
- end
- local function locate()
- print("Getting the current location via GPS...")
- local x, y, z = gps.locate(1)
- if x == nil then
- print("Waiting for GPS signal")
- while x == nil do
- os.sleep(10)
- x, y, z = gps.locate(1)
- end
- end
- print("Got the current location via GPS!")
- return x, y, z
- end
- local NEXT_COORDS_FILE_NAME = ".eater_next_coords"
- local LIMIT = 10
- -- Init
- rednet.open("back")
- local x0, y0, z0 = locate()
- y0 = y0 - 1
- if not fs.exists(NEXT_COORDS_FILE_NAME) then
- local f = fs.open(NEXT_COORDS_FILE_NAME, "w")
- f.writeLine("0")
- f.writeLine("0")
- f.writeLine("false")
- f.close()
- end
- local f = fs.open(NEXT_COORDS_FILE_NAME, "r")
- local x, z = tonumber(f.readLine()), tonumber(f.readLine())
- local limitReached = f.readLine() == "true"
- -- Main loop
- while true do
- print("Waiting for a signal from a turtle...")
- local id, msg = rednet.receive()
- if msg == "eater_turtle ready" then
- print("Received ready signal from #" .. id .. " turtle")
- if limitReached then
- print("Nothing to order!")
- rednet.send(id, "eater_master done")
- else
- print("Ordering the turtle #" .. id .. " to go to " .. x .. ", " .. z)
- rednet.send(id, "eater_master go " .. x .. " " .. z)
- x, z, limitReached = nextCoords(x, z, LIMIT)
- local saveF = fs.open(NEXT_COORDS_FILE_NAME, "w")
- saveF.writeLine(tostring(x))
- saveF.writeLine(tostring(z))
- saveF.writeLine(tostring(limitReached))
- saveF.close()
- end
- elseif msg == "eater_turtle search" then
- print("Received search signal from #" .. id .. " turtle")
- print("Sending zero coordinates to the turtle #" .. id)
- rednet.send(id, "eater_master zero " .. x0 .. " " .. y0 .. " " .. z0)
- else
- print("Received unknown signal from #" .. id .. ", not sure what to do")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement