Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local COAL, COMPOST, SOIL, BROWN, RED = "quark:charcoal_block", "farmersdelight:organic_compost", "farmersdelight:rich_soil", "minecraft:brown_mushroom", "minecraft:red_mushroom"
- local CHESTS = {
- COAL={x=-419, y=71, z=-532, heading=1},
- COMPOST={x=-419, y=71, z=-533, heading=1},
- SOIL={x=-419, y=71, z=-534, heading=1},
- BROWN={x=-419, y=72, z=-534, heading=1},
- RED={x=-419, y=73, z=-534, heading=1}
- }
- local LANES = {
- {x=-420, y=71, z=-533, heading=3, length=24, left=true, right=true},
- {x=-420, y=71, z=-530, heading=3, length=24, right=true},
- {x=-420, y=71, z=-536, heading=3, length=24, left=true}
- }
- local LANES_LENGTH = 3
- local laneIndex = 1
- function calculatePositionHeading()
- write("Calculating position... ")
- local curX, curY, curZ = gps.locate()
- print("Done!")
- write("Calculating compass heading... ")
- local turns = 0 -- How many right turns the turtle just did to find an empty spot.
- while turtle.detect() do
- turtle.turnRight()
- turns = turns + 1
- end
- turtle.forward()
- local newX, _, newZ = gps.locate()
- -- Calculates heading.
- local heading
- if newZ < curZ then heading = 0
- elseif newX > curX then heading = 1
- elseif newZ > curZ then heading = 2
- elseif newX < curX then heading = 3 end
- -- Returns to old position and heading.
- turtle.back()
- for _=1, turns % 4 do
- turtle.turnLeft()
- end
- heading = (heading - turns) % 4
- print("Done!")
- return {x=curX, y=curY, z=curZ, heading=heading}
- end
- local pos = calculatePositionHeading()
- function setOffset(distance)
- if pos.heading == 0 then pos.z = pos.z - distance
- elseif pos.heading == 1 then pos.x = pos.x + distance
- elseif pos.heading == 2 then pos.z = pos.z + distance
- elseif pos.heading == 3 then pos.x = pos.x - distance end
- end
- function offsetForward(newPos)
- if pos.heading == 0 then return pos.z - newPos.z
- elseif pos.heading == 1 then return newPos.x - pos.x
- elseif pos.heading == 2 then return newPos.z - pos.z
- elseif pos.heading == 3 then return pos.x - newPos.x end
- end
- function offsetSide(newPos) -- TODO: Re-use code better.
- if pos.heading == 0 then return pos.x - newPos.x
- elseif pos.heading == 1 then return pos.z - newPos.z
- elseif pos.heading == 2 then return newPos.x - pos.x
- elseif pos.heading == 3 then return newPos.z - pos.z end
- end
- function forward()
- local moved = turtle.forward()
- if moved then setOffset(1) end
- return moved
- end
- function back()
- local moved = turtle.forward()
- if moved then setOffset(-1) end
- return moved
- end
- function up()
- local moved = turtle.up()
- if moved then pos.y = pos.y + 1 end
- return moved
- end
- function down()
- local moved = turtle.down()
- if moved then pos.y = pos.y - 1 end
- return moved
- end
- function turnRight()
- local moved = turtle.turnRight()
- if moved then pos.heading = (pos.heading + 1) % 4 end
- return moved
- end
- function turnLeft()
- local moved = turtle.turnLeft()
- if moved then pos.heading = (pos.heading - 1) % 4 end
- return moved
- end
- function turnToward(newPos)
- local moved = false
- if offsetSide(newPos) > 0 then moved = turnLeft()
- else moved = turnRight() end
- return moved
- end
- function turnTo(newHeading)
- if (pos.heading - newHeading) % 4 == 1 then turnLeft()
- else while pos.heading ~= newHeading do turnRight() end end
- end
- function goTo(newPos)
- while true do
- while offsetForward(newPos) > 0 do if not forward() then break end end
- if pos.x == newPos.x and pos.y == newPos.y and pos.z == newPos.z then break end
- turnToward(newPos)
- while pos.y < newPos.y do if not up() then break end end
- while pos.y > newPos.y do if not down() then break end end
- while offsetForward(newPos) < 0 do turnToward(newPos) end
- end
- end
- function goToChest(chest)
- goTo(chest)
- turnTo(chest.heading)
- end
- function selectItem(name)
- for i=1, 16 do
- local item = turtle.getItemDetail(i)
- if item ~= nil and item.name == name then
- turtle.select(i)
- return true
- end
- end
- return false
- end
- function countItem(name)
- local sum = 0
- for i=1, 16 do
- local item = turtle.getItemDetail(i)
- if item ~= nil and item.name == name then
- sum = sum + item.count
- end
- end
- return sum
- end
- function tendBlock()
- local exists, block = turtle.inspect()
- if block.name == SOIL then
- write("Found rich soil... ")
- selectItem(SOIL)
- turtle.dig()
- exists = false
- end
- if not exists then
- if not selectItem(COMPOST) then
- print("No organic compost!")
- return
- end
- turtle.place()
- print("Placed organic compost!")
- if countItem(BROWN) >= 1 then
- up()
- local slot = turtle.getSelectedSlot() + 1
- if slot <= 16 then turtle.select(slot) end
- local item = turtle.getItemDetail()
- if item == nil or item.name ~= BROWN then
- selectItem(BROWN)
- print("Found mushroom")
- end
- turtle.place()
- down()
- end
- end
- end
- while true do
- goTo({x=-419, y=71, z=-529})
- if countItem(SOIL) > 0 then
- write("Dumping rich soil... ")
- goToChest(CHESTS.SOIL)
- selectItem(SOIL)
- turtle.drop()
- print("Done!")
- end
- if countItem(BROWN) > 32 then
- write("Dumping rich brown mushrooms... ")
- goToChest(CHESTS.BROWN)
- selectItem(BROWN)
- turtle.drop()
- print("Done!")
- end
- if turtle.getFuelLevel() < 10000 then
- selectItem(COAL)
- turtle.refuel()
- end
- if countItem(COAL) < 32 then
- write("Refueling coal... ")
- goToChest(CHESTS.COAL)
- selectItem(COAL)
- turtle.suck(turtle.getItemSpace())
- print("Done!")
- end
- if countItem(COMPOST) < 32 then
- write("Refueling organic compost... ")
- goToChest(CHESTS.COMPOST)
- selectItem(COMPOST)
- turtle.suck(64 - countItem(COMPOST))
- print("Done!")
- end
- local lane = LANES[laneIndex]
- write("Moving to lane "..laneIndex.."... ")
- goTo(lane)
- print("Done!")
- turnTo(lane.heading)
- print("Tending to lane "..laneIndex.."... ")
- for _=1, lane.length do
- if lane.left then
- turnLeft()
- tendBlock()
- turnRight()
- end
- if lane.right then
- turnRight()
- tendBlock()
- turnLeft()
- end
- if not forward() then
- write("Aborted (blocked)")
- break
- end
- end
- laneIndex = (laneIndex % LANES_LENGTH) + 1
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement