Advertisement
Henness

AdvancedLogger

Mar 1st, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.87 KB | None | 0 0
  1. -- Advanced Logger by Henness
  2. -- Version 1.0 3/1/2015
  3.  
  4. -- Config
  5. local program = "Advanced Logger"
  6. local version = "v1.0"
  7. local author = "Henness"
  8. local area = 100
  9. local trees = {"minecraft:log","Natura:tree"}
  10. local blacklist = {"minecraft:stone","minecraft:grass","minecraft:dirt","minecraft:sand","minecraft:gravel"}
  11. local whitelist = {"minecraft:tallgrass","BiomesOPlenty:foliage","minecraft:leaves","minecraft:deadbush","minecraft:yellow_flower","minecraft:red_flower","minecraft:brown_mushroom","minecraft:red_mushroom","minecraft:vine","minecraft:waterlily"}
  12.  
  13. local loc = {}
  14. loc = {
  15.     paused = false,
  16.    
  17.     x = 0,
  18.     y = 0,
  19.     z = 0,
  20.     facing = 0,
  21.    
  22.     facingToAxis = {
  23.         [0] = { axis = 'z', unit = 1 },
  24.         [1] = { axis = 'x', unit = -1 },
  25.         [2] = { axis = 'z', unit = -1 },
  26.         [3] = { axis = 'x', unit = 1 }
  27.     },
  28.    
  29.     pause = function()
  30.         while loc.paused do
  31.             sleep(1)
  32.         end
  33.     end,
  34.    
  35.     getAxisForFacing = function()
  36.         return loc.facingToAxis[loc.facing]['axis'], loc.facingToAxis[loc.facing]['unit']
  37.     end,
  38.  
  39.     turnLeft = function()
  40.         loc.pause()
  41.         loc.facing = (loc.facing - 1) % 4
  42.         turtle.turnLeft()
  43.     end,
  44.  
  45.     turnRight = function()
  46.         loc.pause()
  47.         loc.facing = (loc.facing + 1) % 4
  48.         turtle.turnRight()
  49.     end,
  50.  
  51.     moveForward = function()
  52.         loc.pause()
  53.         if turtle.forward() then
  54.             local axis, unit = loc.getAxisForFacing()
  55.             loc[axis] = loc[axis] + unit
  56.             return true
  57.         end
  58.         return false
  59.     end,
  60.    
  61.     moveBack = function()
  62.         loc.pause()
  63.         if turtle.back() then
  64.             local axis, unit = loc.getAxisForFacing()
  65.             loc[axis] = loc[axis] - unit
  66.             return true
  67.         end
  68.         return false
  69.     end,
  70.  
  71.     face = function(faceto)
  72.         local dirdif = (faceto - loc.facing) % 4
  73.         if dirdif == 1 then
  74.             loc.turnRight()
  75.         elseif dirdif == 2 then
  76.             loc.turnRight()
  77.             loc.turnRight()
  78.         elseif dirdif == 3 then
  79.             loc.turnLeft()
  80.         end
  81.     end,
  82.  
  83.     movez = function(d)
  84.         if d < loc.z then loc.face(2) elseif d > loc.z then loc.face(0) end
  85.         return loc.moveForward()
  86.     end,
  87.  
  88.     movex = function(d)
  89.         if d < loc.x then loc.face(1) elseif d > loc.x then loc.face(3) end
  90.         return loc.moveForward()
  91.     end,
  92.  
  93.     movey = function(d)
  94.         if d < loc.y then
  95.             return loc.moveDown()
  96.         end
  97.         return loc.moveUp()
  98.     end,
  99.  
  100.     moveUp = function()
  101.         loc.pause()
  102.         if turtle.up() then
  103.             loc.y = loc.y + 1
  104.             return true
  105.         end
  106.         return false
  107.     end,
  108.  
  109.     moveDown = function()
  110.         loc.pause()
  111.         if turtle.down() then
  112.             loc.y = loc.y - 1
  113.             return true
  114.         end
  115.         return false
  116.     end,
  117.  
  118.     moveTo = function(x, y, z, facing)
  119.         if y > loc.y then
  120.             while loc.y < y do
  121.                 while not loc.moveUp() do
  122.                     if turtle.detectUp() then
  123.                         turtle.select(1)
  124.                         turtle.digUp()
  125.                     else
  126.                         turtle.attackUp()
  127.                     end
  128.                 end
  129.             end
  130.         end
  131.         while x ~= loc.x do
  132.             while not loc.movex(x) do
  133.                 if turtle.detect() then
  134.                     turtle.select(1)
  135.                     turtle.dig()
  136.                 else
  137.                     turtle.attack()
  138.                 end
  139.             end
  140.         end
  141.         while z ~= loc.z do
  142.             while not loc.movez(z) do
  143.                 if turtle.detect() then
  144.                     turtle.select(1)
  145.                     turtle.dig()
  146.                 else
  147.                     turtle.attack()
  148.                 end
  149.             end
  150.         end
  151.         if y < loc.y then
  152.             while loc.y > y do
  153.                 while not loc.moveDown() do
  154.                     if turtle.detectDown() then
  155.                         turtle.select(1)
  156.                         turtle.digDown()
  157.                     else
  158.                         turtle.attackDown()
  159.                     end
  160.                 end
  161.             end
  162.         end
  163.         loc.face(facing)
  164.         return true
  165.     end,
  166.    
  167.     moveToPos = function(pos)
  168.         return loc.moveTo(pos.x, pos.y, pos.z, pos.facing)
  169.     end,
  170.  
  171.     distanceTo = function(x, y, z)
  172.         return math.abs(x) - loc.x + math.abs(y) - loc.y + math.abs(z) - loc.z
  173.     end,
  174.  
  175.     distanceToPos = function(pos)
  176.         return loc.distanceTo(pos.x, pos.y, pos.z)
  177.     end
  178. }
  179.  
  180. function saveTable(table,name) -- Save table to file for access later
  181.     local file = fs.open(name,"w")
  182.     file.write(textutils.serialize(table))
  183.     file.close()
  184. end
  185.  
  186. function loadTable(name) -- Load table from file, returns a table
  187.     local file = fs.open(name,"r")
  188.     local data = file.readAll()
  189.     file.close()
  190.     return textutils.unserialize(data)
  191. end
  192.  
  193. function isTree(direction)
  194.     if direction == "up" then
  195.         isBlock, blockData = turtle.inspectUp()
  196.     elseif direction == "down" then
  197.         isBlock, blockData = turtle.inspectDown()
  198.     else
  199.         isBlock, blockData = turtle.inspect()
  200.     end
  201.     if isBlock then
  202.         for key,value in pairs(trees) do
  203.             if value == blockData.name then
  204.                 return true
  205.             end
  206.         end
  207.     end
  208.     return false
  209. end
  210.  
  211. function isBlacklist(direction)
  212.     if direction == "up" then
  213.         isBlock, blockData = turtle.inspectUp()
  214.     elseif direction == "down" then
  215.         isBlock, blockData = turtle.inspectDown()
  216.     else
  217.         isBlock, blockData = turtle.inspect()
  218.     end
  219.     if isBlock then
  220.         for key,value in pairs(blacklist) do
  221.             if value == blockData.name then
  222.                 return true
  223.             end
  224.         end
  225.     end
  226.     return false
  227. end
  228.  
  229. function isWhitelist(direction)
  230.     if direction == "up" then
  231.         isBlock, blockData = turtle.inspectUp()
  232.     elseif direction == "down" then
  233.         isBlock, blockData = turtle.inspectDown()
  234.     else
  235.         isBlock, blockData = turtle.inspect()
  236.     end
  237.     if isBlock then
  238.         for key,value in pairs(whitelist) do
  239.             if value == blockData.name then
  240.                 return true
  241.             end
  242.         end
  243.     end
  244.     return false
  245. end
  246.  
  247. function refuel()
  248.     if turtle.getFuelLevel() < turtle.getFuelLimit() then
  249.         for i=1,16 do
  250.             turtle.select(i)
  251.             turtle.refuel()
  252.             if turtle.getFuelLevel() == turtle.getFuelLimit() then
  253.                 break
  254.             end
  255.         end
  256.         turtle.select(1)
  257.     end
  258. end
  259.  
  260. function chopTree()
  261.     if isTree("up") then
  262.         while isTree("up") do
  263.             turtle.digUp()
  264.             if not loc.moveUp() then
  265.                 turtle.attackUp()
  266.             end
  267.         end
  268.         refuel()
  269.         while not turtle.inspectDown() do
  270.             while not loc.moveDown() do
  271.                 turtle.attackDown()
  272.             end
  273.         end
  274.         return true
  275.     end
  276.     return false
  277. end
  278.  
  279. function advance()
  280.     while not loc.moveForward() do
  281.         if turtle.inspect() then
  282.             if isBlacklist() then
  283.                 while not loc.moveUp() do
  284.                     if turtle.inspectUp() then
  285.                         turtle.digUp()
  286.                     else
  287.                         turtle.attack()
  288.                     end
  289.                 end
  290.             else
  291.                 turtle.dig()
  292.             end
  293.         else
  294.             turtle.attack()
  295.         end
  296.     end
  297.     while not turtle.inspectDown() or isWhitelist("down") do
  298.         while not loc.moveDown() do
  299.             if turtle.inspectDown() then
  300.                 turtle.digDown()
  301.             else
  302.                 turtle.attackDown()
  303.             end
  304.         end
  305.         if loc.y <= 60 then
  306.             break
  307.         end
  308.     end
  309. end
  310.  
  311. function runLogger()
  312.     local cArea = 1
  313.     while cArea <= area do
  314.         for i=1, cArea do
  315.             advance()
  316.             chopTree()
  317.         end
  318.         loc.turnRight()
  319.         for i=1, cArea do
  320.             advance()
  321.             chopTree()
  322.         end
  323.         loc.turnRight()
  324.         cArea = cArea + 1
  325.     end
  326. end
  327.  
  328. local tArgs = { ... }
  329. if turtle.getFuelLevel() > 0 or turtle.getFuelLevel() == "unlimited" then
  330.     if #tArgs == 4 then
  331.         loc.x = tonumber(tArgs[1])
  332.         loc.y = tonumber(tArgs[2])
  333.         loc.z = tonumber(tArgs[3])
  334.         loc.face = tonumber(tArgs[4])
  335.         runLogger()
  336.         term.setCursorPos(1,1)
  337.         term.clear()
  338.     else
  339.         error("Usage: advancedlogger x y z face")
  340.     end
  341. else
  342.     error("Error, there must be fuel in the turtle!")
  343. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement