Advertisement
speedSpider3

CC Advanced Lumberjack

Aug 20th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.19 KB | None | 0 0
  1. tArgs = {...}
  2. if #tArgs ~= 1 then
  3.   print("Usage: lumberjack <length>")
  4.   return
  5. end
  6.  
  7. length = tonumber(tArgs[1])
  8. if length < 3 then
  9.   print("Length must be greater than 2")
  10.   return
  11. end
  12.  
  13. length = length - 1
  14.  
  15. depth = 0
  16. xPos, zPos = 0, 0
  17. xDir, zDir = 0, 1
  18.  
  19. function readProgress()
  20.   if not fs.exists("/tmp/prog.sav") then
  21.     return 1, 1
  22.   end
  23.  
  24.   local file = fs.open("/tmp/prog.sav", "r")
  25.  
  26.   local i = tonumber(file.readLine())
  27.   local j = tonumber(file.readLine())
  28.  
  29.   length = tonumber(file.readLine())
  30.   depth = tonumber(file.readLine())
  31.   xPos = tonumber(file.readLine())
  32.   zPos = tonumber(file.readLine())
  33.   xDir = tonumber(file.readLine())
  34.   zDir = tonumber(file.readLine())
  35.  
  36.   file.close()
  37.  
  38.   return i, j
  39. end
  40.  
  41. function writeProgress(i, j)
  42.   if not fs.exists("/tmp/") then
  43.     fs.makeDir("/tmp/")
  44.   end
  45.  
  46.   local file = fs.open("/tmp/prog.sav", "w")
  47.  
  48.   file.writeLine(i)
  49.   file.writeLine(j)
  50.   file.writeLine(length)
  51.   file.writeLine(depth)
  52.   file.writeLine(xPos)
  53.   file.writeLine(zPos)
  54.   file.writeLine(xDir)
  55.   file.writeLine(zDir)
  56.  
  57.   file.close()
  58. end
  59.  
  60. function isItemLeaves(item)
  61.   local i = string.find(item.name, ":") + 1
  62.   local name = string.sub(item.name, i)
  63.   return name == "leaves"
  64. end
  65.  
  66. function isLeaves()
  67.   local success, item = turtle.inspect()
  68.   if success then
  69.     return isItemLeaves(item)
  70.   else
  71.     return false
  72.   end
  73. end
  74.  
  75. function isLeavesUp()
  76.   local success, item = turtle.inspectUp()
  77.   if success then
  78.     return isItemLeaves(item)
  79.   else
  80.     return false
  81.   end
  82. end
  83.  
  84. function isLeavesDown()
  85.   local success, item = turtle.inspectDown()
  86.   if success then
  87.     return isItemLeaves(item)
  88.   else
  89.     return false
  90.   end
  91. end
  92.  
  93. function itemIsDirt(item)
  94.   local i = string.find(item.name, ":") + 1
  95.   local name = string.sub(item.name, i)
  96.   return name == "dirt"
  97. end
  98.  
  99. function isItemALog(item)
  100.   local i = string.find(item.name, ":") + 1
  101.   local name = string.sub(item.name, i)
  102.   return name == "log"
  103. end
  104.  
  105. function isLog()
  106.   local success, item = turtle.inspect()
  107.   if success then
  108.     return isItemALog(item)
  109.   else
  110.     return false
  111.   end
  112. end
  113.  
  114. function isLogUp()
  115.   local success, item = turtle.inspectUp()
  116.   if success then
  117.     return isItemALog(item)
  118.   else
  119.     return false
  120.   end
  121. end
  122.  
  123. function isLogDown()
  124.   local success, item = turtle.inspectDown()
  125.   if success then
  126.     return isItemALog(item)
  127.   else
  128.     return false
  129.   end
  130. end
  131.  
  132. function forward()
  133.   return turtle.forward()
  134. end
  135.  
  136. function back()
  137.   return turtle.back()
  138. end
  139.  
  140. function up()
  141.   local success = turtle.up()
  142.   if success then
  143.     depth = depth + 1
  144.   end
  145.   return success
  146. end
  147.  
  148. function down()
  149.   local success = turtle.down()
  150.   if success then
  151.     depth = depth - 1
  152.   end
  153.   return success
  154. end
  155.  
  156. function turnLeft()
  157.   xDir, zDir = -zDir, xDir
  158.   return turtle.turnLeft()
  159. end
  160.  
  161. function turnRight()
  162.   xDir, zDir = zDir, -xDir
  163.   return turtle.turnRight()
  164. end
  165.  
  166. function toGround()
  167.   while down() do end
  168. end
  169.  
  170. function startFelling()
  171.   while isLog() do
  172.     turtle.digDown()
  173.     down()
  174.   end
  175.  
  176.   up()
  177.   for i = 1, 16 do
  178.     if itemIsDirt(turtle.getItemDetail(i)) then
  179.       turtle.select(i)
  180.       turtle.placeDown()
  181.       break
  182.     end
  183.   end
  184.  
  185.   if isLog() then
  186.     turtle.dig()
  187.     forward()
  188.   end
  189.  
  190.   while isLogUp() do
  191.     turtle.digUp()
  192.     up()
  193.   end
  194.  
  195.   toGround()
  196.  
  197.   while isLogDown() do
  198.     turtle.digDown()
  199.     down()
  200.   end
  201.  
  202.   back()
  203. end
  204.  
  205. function tryForward()
  206.   while not forward() do
  207.     if isLog() then
  208.       startFelling()
  209.     elseif isLeaves() then
  210.       turtle.dig()
  211.     elseif turtle.detectUp() and not isLogUp() then
  212.       turtle.digUp()
  213.       up()
  214.     elseif not turtle.detectUp() then
  215.       up()
  216.     elseif isLogUp() then
  217.       back()
  218.       turtle.digUp()
  219.       up()
  220.       turtle.startFelling()
  221.     end
  222.   end
  223.  
  224.   xPos = xPos + xDir
  225.   zPos = zPos + zDir
  226.   toGround()
  227. end
  228.  
  229. function edgeTurnRight()
  230.   turnRight()
  231.   tryForward()
  232.   toGround()
  233.   turnRight()
  234. end
  235.  
  236. function edgeTurnLeft()
  237.   turnLeft()
  238.   tryForward()
  239.   toGround()
  240.   turnLeft()
  241. end
  242.  
  243. function ai()
  244.   local startI, startJ = readProgress()
  245.  
  246.   for i = startI, length + 1 do
  247.     for j = startJ, length do
  248.       tryForward()
  249.       writeProgress(i, j)
  250.     end
  251.  
  252.     startJ = 1
  253.     if i ~= length + 1 then
  254.       if i % 2 == 0 then
  255.         edgeTurnLeft()
  256.       else
  257.         edgeTurnRight()
  258.       end
  259.     end
  260.  
  261.     writeProgress(i, 1)
  262.   end
  263. end
  264.  
  265. function goto(x, y, z, xD, zD)
  266.   while depth ~= y do
  267.     if depth > y then
  268.       if not down() then
  269.         turtle.digDown()
  270.         turtle.attackDown()
  271.       end
  272.     else
  273.       if not up() then
  274.         turtle.digUp()
  275.         turtle.attackUp()
  276.       end
  277.     end
  278.   end
  279.   if xPos > x then
  280.     while xDir ~= -1 do
  281.       turnLeft()
  282.     end
  283.     while xPos > x do
  284.       if forward() then
  285.         xPos = xPos - 1
  286.       else
  287.         turtle.dig()
  288.             turtle.attack()
  289.       end
  290.     end
  291.   elseif xPos < x then
  292.     while xDir ~= 1 do
  293.       turnLeft()
  294.     end
  295.     while xPos > x do
  296.       if forward() then
  297.         xPos = xPos - 1
  298.       else
  299.         turtle.dig()
  300.         turtle.attack()
  301.       end
  302.     end
  303.   end
  304.   if zPos > z then
  305.     while zDir ~= -1 do
  306.       turnLeft()
  307.     end
  308.     while zPos > z do
  309.       if forward() then
  310.         zPos = zPos - 1
  311.       else
  312.         turtle.dig()
  313.             turtle.attack()
  314.       end
  315.     end
  316.   elseif zPos < z then
  317.     while zDir ~= 1 do
  318.       turnLeft()
  319.     end
  320.     while zPos > z do
  321.       if forward() then
  322.         zPos = zPos - 1
  323.       else
  324.         turtle.dig()
  325.             turtle.attack()
  326.       end
  327.     end
  328.   end
  329.   while zDir ~= zD or xDir ~= xD do
  330.      turnLeft()
  331.   end
  332. end
  333.  
  334. function dumpInv()
  335.   goto(0, 0, 0, 0, -1)
  336.   for i = 16, 1, -1 do
  337.     turtle.select(i)
  338.     if turtle.getItemCount() ~= 0 then
  339.       if itemIsDirt(turtle.getItemDetail()) then
  340.         if i == 1 then
  341.           if turtle.getItemCount() > 10 then
  342.             turtle.drop(turtle.getItemCount() - 10)
  343.           end
  344.         else
  345.           turtle.transferTo(1)
  346.         end
  347.       else
  348.         turtle.drop()
  349.       end
  350.     end
  351.   end
  352. end
  353.  
  354. ai()
  355. fs.delete("/tmp/prog.sav")
  356. dumpInv()
  357. turtle.select(1)
  358. goto(0, 0, 0, 0, -1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement