Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local WHEREIS_CHANNEL = 420
- local CHEST_BLOCK = "ironchest:BlockIronChest"
- local activities = {
- COLLECT = 1, -- suck up the contents of the chest, then DROP
- DROP = 2, -- drop left, then drop right, then forward until the invetory is empty or movement is blocked
- RETURN = 3, -- go back to the chest until movement is blocked
- }
- local directions = {
- HOME_AWAY = 0,
- CHEST_RIGHT = 1,
- HOME_TOWARD = 2,
- CHEST_LEFT = 3
- }
- -- fix the modem bug
- if not peripheral.find("modem") then
- -- first find an empty slot
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- turtle.select(i)
- break
- elseif i == 16 then
- turtle.select(i)
- turtle.drop()
- end
- end
- turtle.equipLeft()
- turtle.equipLeft()
- turtle.equipRight()
- turtle.equipRight()
- end
- local state
- local function loadState()
- local h = fs.open("organiser.state", "r")
- if h then
- state = textutils.unserialise(h.readAll())
- h.close()
- else
- state = {
- activity = activities.COLLECT,
- direction = directions.HOME_AWAY,
- startFuel = turtle.getFuelLevel()
- }
- end
- end
- local function saveState()
- local h = fs.open("organiser.state", "w")
- if h then
- h.write(textutils.serialise(state))
- h.close()
- end
- end
- local function turnTo(direction)
- local difference = direction - state.direction
- if math.abs(difference) >= 3 then
- difference = (difference - 2 * (difference / math.abs(difference))) * -1
- end
- if difference > 0 then
- for i = 1, difference do
- turtle.turnLeft()
- state.direction = (state.direction + 1) % 4
- saveState()
- end
- elseif difference < 0 then
- for i = 1, math.abs(difference) do
- turtle.turnRight()
- state.direction = (state.direction - 1) % 4
- saveState()
- end
- end
- end
- local function setActivity(activity)
- if not activity then error('act') end
- state.activity = activity
- saveState()
- end
- local funcs = {}
- local function activity(activity)
- setActivity(activity)
- return funcs[activity]()
- end
- funcs[activities.COLLECT] = function()
- print("Collecting the melon...")
- turnTo(directions.HOME_TOWARD)
- local okay = turtle.suck()
- if not okay then
- print("Nothing to collect...")
- -- there is nothing to collect, wait 10 seconds and try again
- sleep(10)
- return activity(activities.COLLECT)
- end
- while turtle.suck() do end
- return activity(activities.DROP)
- end
- local function drop() -- return false if the chest is full, return true if the turtle is empty
- for i = 1, 16 do
- local okay, err = turtle.drop()
- if not okay then
- if err == "No space for items" then
- return false
- else
- return true
- end
- end
- end
- return true
- end
- funcs[activities.DROP] = function()
- print("Dropping the melon in chests")
- while true do
- turnTo(directions.CHEST_RIGHT)
- if drop() then
- break
- end
- turnTo(directions.CHEST_LEFT)
- if drop() then
- break
- end
- turnTo(directions.HOME_AWAY)
- if not turtle.forward() then
- break
- end
- end
- return activity(activities.RETURN)
- end
- funcs[activities.RETURN] = function()
- print("Heading toward chest")
- turnTo(directions.HOME_TOWARD)
- while turtle.forward() do end
- return activity(activities.COLLECT)
- end
- -- depart from the idle chest location
- function resume()
- print("Resuming...")
- loadState()
- activity(state.activity)
- end
- parallel.waitForAll(function()
- resume()
- while true do
- sleep(delay)
- print("Starting fresh...")
- state.startFuel = turtle.getFuelLevel()
- state.isChestRight = false
- saveState()
- activity(activities.COLLECT)
- end
- end, function()
- local modem = peripheral.find("modem")
- modem.open(WHEREIS_CHANNEL)
- while true do
- local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
- if channel == WHEREIS_CHANNEL and message == "WHEREIS" then
- modem.transmit(replyChannel, channel, {os.getComputerID(), os.getComputerLabel(), gps.locate()})
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement