Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle Slots:
- -- 1: Fuel
- -- 2-15: Inventory slots
- -- 16: Active Item
- -- Fuel should always be on TOP
- -- Resources should always be on BOTTOM
- local FUEL = 1
- local ACTIVE = 16
- local INVENTORY = 2
- local CHEST = "minecraft:chest"
- local GENERIC_SAPLING = "[Ss][Aa][Pp][Ll][Ii][Nn][Gg]"
- local ORE = "[Oo][Rr][Ee]"
- local DETECT = {
- ["forward"] = "detect",
- ["up"] = "detectUp",
- ["down"] = "detectDown",
- }
- local INSPECT = {
- ["forward"] = "inspectFor",
- ["up"] = "inspectUpFor",
- ["down"] = "inspectDownFor",
- }
- local ATTACK = {
- ["forward"] = "attack",
- ["up"] = "attackUp",
- ["down"] = "attackDown",
- }
- local DIG = {
- ["forward"] = "dig",
- ["down"] = "digDown",
- ["up"] = "digUp",
- }
- Turtle = (function()
- local t = {}
- function t.move(direction, amount, onNode)
- local detect = turtle[DETECT[direction]]
- local inspect = turtle[INSPECT[direction]]
- local dig = turtle[DIG[direction]]
- local move = turtle[direction]
- for i = 1, amount, 1 do
- if (detect()) then
- dig()
- if (not move()) then
- t.move(direction, 1)
- end
- elseif (not move()) then
- t.kill(direction)
- if (not move()) then
- t.move(direction, 1)
- end
- end
- if (onNode) then
- onNode()
- end
- end
- end
- function t.forward(amount, onNode)
- return t.move("forward", amount, onNode)
- end
- function t.up(amount, onNode)
- return t.move("up", amount, onNode)
- end
- function t.down(amount, onNode)
- return t.move("down", amount, onNode)
- end
- function t.kill(direction)
- local attack = turtle[ATTACK[direction]]
- local attacked = attack()
- while (attacked) do
- attacked = attack()
- end
- end
- function t.killDown()
- t.kill("down")
- end
- function t.killUp()
- t.kill("up")
- end
- function t.quarry(width, length)
- local clear = function()
- if (t.inspectUpFor(ORE)) then
- turtle.digUp()
- end
- if (t.inspectDownFor(ORE)) then
- turtle.digDown()
- end
- end
- while (true) do
- t.down(3)
- clear()
- t.clearLayer(width, length, clear)
- end
- end
- function t.clearLayer(width, length, onNode)
- for w = 1, width, 1 do
- t.forward(length - 1, onNode)
- if (w % width == 0) then
- if (w % 2 == 0) then
- turtle.turnRight()
- t.forward(length - 1)
- turtle.turnRight()
- else
- turtle.turnLeft()
- t.forward(width - 1)
- turtle.turnLeft()
- t.forward(length - 1)
- t.turnAround()
- end
- elseif (w % 2 == 0) then
- turtle.turnLeft()
- t.forward(1, onNode)
- turtle.turnLeft()
- else
- turtle.turnRight()
- t.forward(1, onNode)
- turtle.turnRight()
- end
- end
- end
- function t.moveToGround()
- while not turtle.detectDown() do
- turtle.down()
- end
- end
- function t.handle(...)
- if (#arg == 1) then
- arg[1]()
- elseif (#arg == 2) then
- _G[arg[1]][arg[2]]()
- end
- end
- function t.square(diameter, ...)
- for i = 1, 4, 1 do
- for j = 1, diameter - 1, 1 do
- turtle.forward()
- t.handle( unpack (arg) )
- end
- turtle.turnLeft()
- end
- end
- function t.fell()
- if (t.inspectFor(GENERIC_SAPLING)) then return end
- while turtle.detect() do
- turtle.dig()
- turtle.digUp()
- turtle.up()
- end
- t.moveToGround()
- end
- local function baseInspect(success, data, name, includeAir)
- includeAir = includeAir or false
- if success then
- if (string.match (data.name, name)) then
- return true
- end
- elseif (not success and includeAir) then
- return true
- end
- return false
- end
- function t.inspectFor(name, includeAir)
- includeAir = includeAir or false
- local success, data = turtle.inspect()
- return baseInspect(success, data, name, includeAir)
- end
- function t.inspectDownFor(name, includeAir)
- includeAir = includeAir or false
- local success, data = turtle.inspectDown()
- return baseInspect(success, data, name, includeAir)
- end
- function t.inspectUpFor(name, includeAir)
- includeAir = includeAir or false
- local success, data = turtle.inspectUp()
- return baseInspect(success, data, name)
- end
- function t.checkAllSides(name, ...)
- local handled = false
- for i = 1, 4, 1 do
- turtle.turnRight()
- if (t.inspectFor(name) and not handled) then
- t.handle ( unpack (arg) )
- handled = true
- end
- end
- end
- function t.refuel()
- if (turtle.getFuelLevel() < 100) then
- local count = turtle.getItemSpace(1)
- if (count > 0) then
- turtle.suckUp(count)
- end
- turtle.select(FUEL)
- return turtle.refuel(1)
- else
- return true
- end
- end
- function t.dump()
- for i = 2, 15, 1 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(FUEL)
- end
- function t.empty()
- t.checkAllSides(CHEST, t.dump)
- end
- function t.turnAround()
- turtle.turnRight()
- turtle.turnRight()
- end
- return t
- end) ()
- Botania = (function ()
- local b = {}
- local LIVING_ROCK = "Botania:livingrock"
- local LIVING_STONE = "Botania:livingwood"
- function b.replace()
- if (b.isLiving()) then
- turtle.select(INVENTORY)
- turtle.digDown()
- turtle.select(ACTIVE)
- turtle.placeDown()
- end
- end
- function b.isLiving()
- return (Turtle.inspectDownFor(LIVING_ROCK, true) or Turtle.inspectDownFor(LIVING_STONE, true))
- end
- function b.restock()
- local count = turtle.getItemCount(ACTIVE)
- if (count < 64) then
- turtle.select(ACTIVE)
- if (turtle.suckDown(64 - count)) then
- return true
- else
- return not (count < 8)
- end
- else
- return true
- end
- end
- function b.living(offset)
- offset = offset or 1
- -- Check to see if there is enough fuel
- if (not Turtle.refuel()) then return end
- -- Check to see if there is enough stock
- if (not b.restock()) then return end
- -- Move turtle to living square
- Turtle.forward(offset)
- -- Harvest and replace the living blocks
- Turtle.square(3, b.replace)
- -- Go back to position
- Turtle.turnAround()
- Turtle.forward(offset)
- Turtle.turnAround()
- -- Empty contents
- Turtle.empty()
- end
- return b
- end) ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement