Advertisement
Morverzhus

CC Library - Turtle Extras

Jan 16th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.84 KB | None | 0 0
  1. -- Turtle Slots:
  2. -- 1: Fuel
  3. -- 2-15: Inventory slots
  4. -- 16: Active Item
  5.  
  6. -- Fuel should always be on TOP
  7. -- Resources should always be on BOTTOM
  8.  
  9. local FUEL              = 1
  10. local ACTIVE    = 16
  11. local INVENTORY = 2
  12.  
  13. local CHEST = "minecraft:chest"
  14. local GENERIC_SAPLING = "[Ss][Aa][Pp][Ll][Ii][Nn][Gg]"
  15. local ORE = "[Oo][Rr][Ee]"
  16.  
  17. local DETECT = {
  18.     ["forward"] = "detect",
  19.     ["up"]      = "detectUp",
  20.     ["down"]    = "detectDown",
  21. }
  22.  
  23. local INSPECT = {
  24.     ["forward"] = "inspectFor",
  25.     ["up"]      = "inspectUpFor",
  26.     ["down"]    = "inspectDownFor",
  27. }
  28.  
  29. local ATTACK = {
  30.     ["forward"] = "attack",
  31.     ["up"]      = "attackUp",
  32.     ["down"]    = "attackDown",
  33. }
  34.  
  35. local DIG = {
  36.     ["forward"] = "dig",
  37.     ["down"]    = "digDown",
  38.     ["up"]      = "digUp",
  39. }
  40.  
  41. Turtle = (function()
  42.     local t = {}
  43.  
  44.     function t.move(direction, amount, onNode)
  45.         local detect    = turtle[DETECT[direction]]
  46.         local inspect   = turtle[INSPECT[direction]]
  47.         local dig       = turtle[DIG[direction]]
  48.         local move      = turtle[direction]
  49.  
  50.         for i = 1, amount, 1 do
  51.             if (detect()) then
  52.                 dig()
  53.                 if (not move()) then
  54.                     t.move(direction, 1)
  55.                 end
  56.             elseif (not move()) then
  57.                 t.kill(direction)
  58.                 if (not move()) then
  59.                     t.move(direction, 1)
  60.                 end
  61.             end
  62.  
  63.             if (onNode) then
  64.                 onNode()
  65.             end
  66.         end
  67.     end
  68.  
  69.     function t.forward(amount, onNode)
  70.         return t.move("forward", amount, onNode)
  71.     end
  72.  
  73.     function t.up(amount, onNode)
  74.         return t.move("up", amount, onNode)
  75.     end
  76.  
  77.     function t.down(amount, onNode)
  78.         return t.move("down", amount, onNode)
  79.     end
  80.  
  81.     function t.kill(direction)
  82.         local attack   = turtle[ATTACK[direction]]
  83.         local attacked = attack()
  84.  
  85.         while (attacked) do
  86.             attacked = attack()
  87.         end
  88.     end
  89.  
  90.     function t.killDown()
  91.         t.kill("down")
  92.     end
  93.  
  94.     function t.killUp()
  95.         t.kill("up")
  96.     end
  97.  
  98.     function t.quarry(width, length)
  99.         local clear = function()
  100.             if (t.inspectUpFor(ORE)) then
  101.                 turtle.digUp()
  102.             end
  103.  
  104.             if (t.inspectDownFor(ORE)) then
  105.                 turtle.digDown()
  106.             end
  107.         end
  108.  
  109.         while (true) do
  110.             t.down(3)
  111.             clear()
  112.             t.clearLayer(width, length, clear)
  113.         end
  114.     end
  115.  
  116.     function t.clearLayer(width, length, onNode)
  117.         for w = 1, width, 1 do
  118.             t.forward(length - 1, onNode)
  119.  
  120.             if (w % width == 0) then
  121.                 if (w % 2 == 0) then
  122.                     turtle.turnRight()
  123.                     t.forward(length - 1)
  124.                     turtle.turnRight()
  125.                 else
  126.                     turtle.turnLeft()
  127.                     t.forward(width - 1)
  128.                     turtle.turnLeft()
  129.                     t.forward(length - 1)
  130.                     t.turnAround()
  131.                 end
  132.             elseif (w % 2 == 0) then
  133.                 turtle.turnLeft()
  134.                 t.forward(1, onNode)
  135.                 turtle.turnLeft()
  136.             else
  137.                 turtle.turnRight()
  138.                 t.forward(1, onNode)
  139.                 turtle.turnRight()
  140.             end
  141.         end
  142.     end
  143.  
  144.     function t.moveToGround()
  145.         while not turtle.detectDown() do
  146.             turtle.down()
  147.         end
  148.     end
  149.  
  150.     function t.handle(...)
  151.         if (#arg == 1) then
  152.             arg[1]()
  153.         elseif (#arg == 2) then
  154.             _G[arg[1]][arg[2]]()
  155.         end
  156.     end
  157.  
  158.     function t.square(diameter, ...)
  159.         for i = 1, 4, 1 do
  160.             for j = 1, diameter - 1, 1 do
  161.                 turtle.forward()
  162.                 t.handle( unpack (arg) )
  163.             end
  164.             turtle.turnLeft()
  165.         end
  166.     end
  167.  
  168.     function t.fell()
  169.         if (t.inspectFor(GENERIC_SAPLING)) then return end
  170.  
  171.         while turtle.detect() do
  172.             turtle.dig()
  173.             turtle.digUp()
  174.             turtle.up()
  175.         end
  176.  
  177.         t.moveToGround()
  178.     end
  179.  
  180.     local function baseInspect(success, data, name, includeAir)
  181.         includeAir = includeAir or false
  182.  
  183.         if success then
  184.             if (string.match (data.name, name)) then
  185.                 return true
  186.             end
  187.         elseif (not success and includeAir) then
  188.             return true
  189.         end
  190.  
  191.         return false
  192.     end
  193.  
  194.     function t.inspectFor(name, includeAir)
  195.         includeAir = includeAir or false
  196.  
  197.         local success, data = turtle.inspect()
  198.  
  199.         return baseInspect(success, data, name, includeAir)
  200.     end
  201.  
  202.     function t.inspectDownFor(name, includeAir)
  203.         includeAir = includeAir or false
  204.  
  205.         local success, data = turtle.inspectDown()
  206.  
  207.         return baseInspect(success, data, name, includeAir)
  208.     end
  209.  
  210.     function t.inspectUpFor(name, includeAir)
  211.         includeAir = includeAir or false
  212.  
  213.         local success, data = turtle.inspectUp()
  214.  
  215.         return baseInspect(success, data, name)
  216.     end
  217.  
  218.     function t.checkAllSides(name, ...)
  219.         local handled = false
  220.  
  221.         for i = 1, 4, 1 do
  222.             turtle.turnRight()
  223.  
  224.             if (t.inspectFor(name) and not handled) then
  225.                 t.handle ( unpack (arg) )
  226.                 handled = true
  227.             end
  228.         end
  229.     end
  230.  
  231.     function t.refuel()
  232.         if (turtle.getFuelLevel() < 100) then
  233.             local count = turtle.getItemSpace(1)
  234.  
  235.             if (count > 0) then
  236.                 turtle.suckUp(count)
  237.             end
  238.  
  239.             turtle.select(FUEL)
  240.             return turtle.refuel(1)
  241.         else
  242.             return true
  243.         end
  244.     end
  245.  
  246.     function t.dump()
  247.         for i = 2, 15, 1 do
  248.             turtle.select(i)
  249.             turtle.drop()
  250.         end
  251.  
  252.         turtle.select(FUEL)
  253.     end
  254.  
  255.     function t.empty()
  256.         t.checkAllSides(CHEST, t.dump)
  257.     end
  258.  
  259.     function t.turnAround()
  260.         turtle.turnRight()
  261.         turtle.turnRight()
  262.     end
  263.  
  264.     return t
  265. end) ()
  266.  
  267. Botania = (function ()
  268.     local b = {}
  269.  
  270.     local LIVING_ROCK  = "Botania:livingrock"
  271.     local LIVING_STONE = "Botania:livingwood"
  272.  
  273.     function b.replace()
  274.         if (b.isLiving()) then
  275.             turtle.select(INVENTORY)
  276.             turtle.digDown()
  277.             turtle.select(ACTIVE)
  278.             turtle.placeDown()
  279.         end
  280.     end
  281.  
  282.     function b.isLiving()
  283.          return (Turtle.inspectDownFor(LIVING_ROCK, true) or Turtle.inspectDownFor(LIVING_STONE, true))
  284.     end
  285.  
  286.     function b.restock()
  287.         local count = turtle.getItemCount(ACTIVE)
  288.  
  289.         if (count < 64) then
  290.             turtle.select(ACTIVE)
  291.             if (turtle.suckDown(64 - count)) then
  292.                 return true
  293.             else
  294.                 return not (count < 8)
  295.             end
  296.         else
  297.             return true
  298.         end
  299.     end
  300.  
  301.     function b.living(offset)
  302.         offset = offset or 1
  303.  
  304.         -- Check to see if there is enough fuel
  305.         if (not Turtle.refuel()) then return end
  306.  
  307.         -- Check to see if there is enough stock
  308.         if (not b.restock()) then return end
  309.  
  310.         -- Move turtle to living square
  311.         Turtle.forward(offset)
  312.  
  313.         -- Harvest and replace the living blocks
  314.         Turtle.square(3, b.replace)
  315.  
  316.         -- Go back to position
  317.         Turtle.turnAround()
  318.         Turtle.forward(offset)
  319.         Turtle.turnAround()
  320.  
  321.         -- Empty contents
  322.         Turtle.empty()
  323.     end
  324.  
  325.     return b
  326. end) ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement