Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local MAX_LEVEL = 2
- local price = 16
- local WHEREIS_CHANNEL = 420
- local LEVEL_DETECTION_BLOCK = "minecraft:planks"
- local CHEST_BLOCK = "ironchest:BlockIronChest"
- local CHEST_POSITION_BLOCK = "minecraft:sandstone"
- local RIGHT_CHEST_TYPE = "smooth_sandstone"
- local activities = {
- TRANSIT_DOWN = 1, -- go down until current level == target level. when meeting a plank block increase current level
- TRANSIT_OUT = 2, -- back out of the vertical track until bottom is sandstone
- CHEST_IN = 3, -- turn toward the chest and travel forward until the chest is met
- CHEST_COLLECT = 4, -- fill up the turtle
- CHECK_OUT = 5, -- go backwards from the chest until bottom is sandstone
- TRANSIT_IN = 6, -- turn toward the vertical track until front is not air
- TRANSIT_UP = 7, -- goes up until above is not air
- CHEST_DROP = 8, -- drop entire inventory up. set current level to zero. increase target level by one, wrapping if needed
- }
- local directions = {
- CROP_TOWARD = 0,
- CHEST_TOWARD = 1,
- CROP_AWAY = 2,
- CHEST_AWAY = 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("mover.state", "r")
- if h then
- state = textutils.unserialise(h.readAll())
- h.close()
- else
- state = {
- activity = activities.TRANSIT_DOWN,
- direction = directions.CROP_TOWARD,
- currentLevel = 0,
- targetLevel = 1,
- startFuel = turtle.getFuelLevel(),
- isChestRight = false
- }
- end
- end
- local function saveState()
- local h = fs.open("mover.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.TRANSIT_DOWN] = function()
- print("Descending to level " .. state.targetLevel)
- turnTo(directions.CROP_TOWARD)
- while true do
- local isBlock, block = turtle.inspect()
- if isBlock and block.name == LEVEL_DETECTION_BLOCK then
- state.currentLevel = state.currentLevel + 1
- print("Level: " .. state.currentLevel)
- if state.currentLevel == state.targetLevel then
- return activity(activities.TRANSIT_OUT)
- end
- end
- turtle.down()
- end
- end
- funcs[activities.TRANSIT_OUT] = function()
- print("Leaving vertical track")
- turnTo(directions.CROP_TOWARD)
- while true do
- local isBlock, block = turtle.inspectDown()
- if isBlock and block.name == CHEST_POSITION_BLOCK then
- state.isChestRight = block.state.type == RIGHT_CHEST_TYPE
- saveState()
- return activity(activities.CHEST_IN)
- else
- turtle.back()
- end
- end
- end
- funcs[activities.CHEST_IN] = function()
- print("Heading toward chest")
- turnTo(state.isChestRight and directions.CHEST_AWAY or directions.CHEST_TOWARD)
- while true do
- local isBlock, block = turtle.inspect()
- if isBlock then
- return activity(activities.CHEST_COLLECT)
- else
- turtle.forward()
- end
- end
- end
- funcs[activities.CHEST_COLLECT] = function()
- print("Collecting items...")
- turnTo(state.isChestRight and directions.CHEST_AWAY or directions.CHEST_TOWARD)
- while turtle.suck() do end
- return activity(activities.CHECK_OUT)
- end
- funcs[activities.CHECK_OUT] = function()
- print("Leaving chest...")
- turnTo(state.isChestRight and directions.CHEST_AWAY or directions.CHEST_TOWARD)
- while true do
- local isBlock, block = turtle.inspectDown()
- if isBlock and block.name == CHEST_POSITION_BLOCK then
- return activity(activities.TRANSIT_IN)
- else
- turtle.back()
- end
- end
- end
- funcs[activities.TRANSIT_IN] = function()
- print("Heading toward vertical track...")
- turnTo(directions.CROP_TOWARD)
- while true do
- local isBlock, block = turtle.inspect()
- if isBlock then
- return activity(activities.TRANSIT_UP)
- else
- turtle.forward()
- end
- end
- end
- funcs[activities.TRANSIT_UP] = function()
- print("Travelling up to the drop chest...")
- turnTo(directions.CROP_TOWARD)
- while true do
- local isBlock, block = turtle.inspectUp()
- if isBlock and block.name == CHEST_BLOCK then
- return activity(activities.CHEST_DROP)
- else
- turtle.up()
- end
- end
- end
- funcs[activities.CHEST_DROP] = function()
- print("Dropping the items in the chest...")
- local count = 0
- for i = 1, 16 do
- count = count + turtle.getItemCount(i)
- turtle.select(i)
- turtle.dropUp()
- end
- print("Transported " .. count .. " melon.")
- print(string.format("Value: $%.2f", (count / 64) * price))
- print("Fuel consumption: " .. state.startFuel - turtle.getFuelLevel())
- state.currentLevel = 0
- state.targetLevel = state.targetLevel + 1
- if state.targetLevel > MAX_LEVEL then
- state.targetLevel = 1
- end
- print("Now targeting: " .. state.targetLevel)
- return
- 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.TRANSIT_DOWN)
- 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