Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local delay = 0
- local price = 16
- local startsEven = false
- local WHEREIS_CHANNEL = 420
- local DIRECTION_CALIBRATION_BLOCK = "minecraft:sandstone"
- local CROP_START_VARIANT = {
- [true] = "smooth_granite",
- [false] = "smooth_andesite"
- }
- local CROP_END_VARIANT = {
- [true] = "smooth_andesite",
- [false] = "smooth_granite"
- }
- local CROP_SWITCH_VARIANT = {
- [true] = "andesite",
- [false] = "granite"
- }
- local TRANSIT_VERTICAL_ENTER_VARIANT = "smooth_diorite"
- local MELON_BLOCK = "minecraft:melon_block"
- local CHEST_BLOCK = "minecraft:chest"
- local STONE_BLOCK = "minecraft:stone"
- local activities = {
- CHEST_LEAVE = 1, -- spin until sandstone, note as even direction. go forward one then CROP_FIRST.
- CROP_FIRST = 2, -- go along the crops until a wall
- CROP_SWITCH = 3, -- switch row until left is ditorite
- CROP_SECOND = 4, -- go along crops until granite (odd), andesite (even)
- TRANSIT_HORIZONTAL = 5, -- go toward chest until below is andesite (CROP_FIRST) or above is ditorite (turn and foward, then TRANSIT_VERTICAL_UP)
- TRANSIT_VERTICAL_UP = 6, -- go up until front is plank, rotate twice, switch odd/even then CROP_FIRST. otherwise, if above is blocked then
- TRANSIT_VERTICAL_UP_START = 7, -- go up one block to clear the previous level
- TRANSIT_VERTICAL_DOWN = 8, -- go down until blocked, then CHEST_RETURN
- CHEST_RETURN = 9, -- check below if chest, if so drop everything, else spin to not even direction and repeat
- CHEST_DROP = 10, -- check below if chest, if so drop everything, else spin to not even direction and repeat
- }
- 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("melon.state", "r")
- if h then
- state = textutils.unserialise(h.readAll())
- h.close()
- else
- state = {
- activity = activities.CHEST_LEAVE,
- direction = directions.CROP_TOWARD,
- isEven = startsEven,
- startFuel = turtle.getFuelLevel()
- }
- end
- end
- local function saveState()
- local h = fs.open("melon.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.CHEST_LEAVE] = function()
- turnTo(directions.CROP_TOWARD)
- turtle.forward()
- return activity(activities.CROP_FIRST)
- end
- local function crop()
- while true do
- local isBlock, block = turtle.inspectDown()
- if isBlock and block.name == MELON_BLOCK then
- -- there's a melon below us, break it and til the ground
- turtle.digDown()
- turtle.digDown()
- elseif isBlock and block.name == STONE_BLOCK and block.state.variant == CROP_END_VARIANT[state.isEven] then
- -- we're at the end block, TRANSIT_HORIZONTAL
- return activity(activities.TRANSIT_HORIZONTAL)
- end
- if turtle.inspect() then
- -- there's a block in front of us, CROP_SWITCH
- return activity(activities.CROP_SWITCH)
- else
- turtle.forward()
- end
- end
- end
- funcs[activities.CROP_FIRST] = function()
- print("Collecting crop row #1...")
- turnTo(directions.CROP_TOWARD)
- return crop()
- end
- funcs[activities.CROP_SWITCH] = function()
- print("Switching crop row...")
- while true do
- turnTo(directions.CROP_TOWARD)
- local isBlock, block = turtle.inspect()
- if isBlock and block.name == STONE_BLOCK and block.state.variant == CROP_SWITCH_VARIANT[state.isEven] then
- -- we're at the block where the next row starts, CROP_SECOND
- return activity(activities.CROP_SECOND)
- else
- turnTo(state.isEven and directions.CHEST_TOWARD or directions.CHEST_AWAY)
- turtle.forward()
- end
- end
- end
- funcs[activities.CROP_SECOND] = function()
- print("Collecting crop row #2...")
- turnTo(directions.CROP_AWAY)
- return crop()
- end
- funcs[activities.TRANSIT_HORIZONTAL] = function()
- print("Travelling horizontally to next crop...")
- turnTo(state.isEven and directions.CHEST_TOWARD or directions.CHEST_AWAY)
- while true do
- local isBlock, block = turtle.inspectDown()
- if isBlock and block.name == STONE_BLOCK and block.state.variant == CROP_START_VARIANT[state.isEven] then
- -- we're at the block where the next crop starts, CROP_FIRST
- return activity(activities.CROP_FIRST)
- else
- local isBlockAbove, blockAbove = turtle.inspectUp()
- if isBlockAbove and blockAbove.name == STONE_BLOCK and blockAbove.state.variant == TRANSIT_VERTICAL_ENTER_VARIANT then
- -- we're at the end of the row, TRANSIT_VERTICAL_UP
- turnTo(directions.CROP_AWAY)
- turtle.forward()
- return activity(activities.TRANSIT_VERTICAL_UP_START)
- else
- turtle.forward()
- end
- end
- end
- end
- funcs[activities.TRANSIT_VERTICAL_UP_START] = function()
- -- we need to do this because otherwise if it reloads just after it moves into this spot it'll stay on the same level
- print("Leaving previous layer...")
- turtle.up()
- return activity(activities.TRANSIT_VERTICAL_UP)
- end
- funcs[activities.TRANSIT_VERTICAL_UP] = function()
- print("Travelling vertically to next crop layer...")
- turnTo(directions.CROP_TOWARD)
- while true do
- local isBlock, block = turtle.inspect()
- if not isBlock then
- -- we've arrived at the new level, CROP_FIRST
- state.isEven = not state.isEven
- return activity(activities.CROP_FIRST)
- else
- if turtle.inspectUp() then
- -- there's a block in the way, so we're at the maximum available layer, TRANSIT_VERTICAL_DOWN
- return activity(activities.TRANSIT_VERTICAL_DOWN)
- else
- turtle.up()
- end
- end
- end
- end
- funcs[activities.TRANSIT_VERTICAL_DOWN] = function()
- print("Travelling vertically back to the starting layer...")
- while true do
- local isBlock, block = turtle.inspectDown()
- if isBlock then
- -- we've arrived at the chest layer
- state.isEven = false
- if block.name == CHEST_BLOCK then
- -- we're right above the chest, CROP_DROP
- return activity(activities.CHEST_DROP)
- else
- -- we need to move along to the chest
- return activity(activities.CHEST_RETURN)
- end
- else
- turtle.down()
- end
- end
- end
- funcs[activities.CHEST_RETURN] = function()
- print("Travelling horizontally back to the chest...")
- turnTo(startsEven and directions.CHEST_AWAY or directions.CHEST_TOWARD)
- while true do
- local isBlock, block = turtle.inspectDown()
- if isBlock and block.name == CHEST_BLOCK then
- -- we're right above the chest, CROP_DROP
- return activity(activities.CHEST_DROP)
- else
- turtle.forward()
- end
- end
- end
- funcs[activities.CHEST_DROP] = function()
- print("Dropping the crop in the chest...")
- local count = 0
- for i = 1, 16 do
- count = count + turtle.getItemCount(i)
- turtle.select(i)
- turtle.dropDown()
- end
- print("Harvested " .. count .. " melon.")
- print(string.format("Value: $%.2f", (count / 64) * price))
- print("Fuel consumption: " .. state.startFuel - turtle.getFuelLevel())
- 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.isEven = startsEven
- saveState()
- activity(activities.CHEST_LEAVE)
- 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