Advertisement
MoonlightOwl

Totoro Recursive Miner

Sep 14th, 2014
1,141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.13 KB | None | 0 0
  1. -- Totoro Recursive Miner 0.2 --
  2. local robot = require('robot')
  3. local event = require('event')
  4. local sides = require('sides')
  5. local computer = require('computer')
  6. local com = require('component')
  7. local gen = com.generator
  8. local cloader = com.chunkloader
  9.  
  10. INV_SIZE = 24
  11. TECH_SLOTS = 8
  12. MAX = 1024
  13.  
  14. local trash_slots = TECH_SLOTS - 1
  15. local chest_slot = TECH_SLOTS
  16. local empty_slot = TECH_SLOTS + 1
  17.  
  18. local stepside = {}
  19. stepside[0] = sides.up
  20. stepside[1] = sides.down
  21.  
  22. local loc = {x=0, y=0, z=0, d=0}
  23. local lode = {}
  24.  
  25. local a = {...}
  26. local l = tonumber(a[1]) or 10
  27. local h = tonumber(a[2]) or 4
  28.  
  29. if a[1] == '?' then
  30.   print("Usage: mine <length> [height]")
  31.   return
  32. end
  33.  
  34. -- ============================= N A V I G A T I O N ====================================== --
  35. function forward()
  36.   if robot.forward() then
  37.     if loc.d == 0 then loc.y = loc.y+1
  38.     elseif loc.d == 1 then loc.x = loc.x+1
  39.     elseif loc.d == 2 then loc.y = loc.y-1
  40.     else loc.x = loc.x-1 end
  41.     return true
  42.   else
  43.     return false
  44.   end
  45. end
  46. function up()
  47.   if robot.up() then
  48.     loc.z = loc.z+1
  49.     return true
  50.   else
  51.     return false
  52.   end
  53. end
  54. function down()
  55.   if robot.down() then
  56.     loc.z = loc.z-1
  57.     return true
  58.   else
  59.     return false
  60.   end
  61. end
  62. function turnRight()
  63.   loc.d = (loc.d+1)%4
  64.   robot.turnRight()
  65. end
  66. function turnAround()
  67.   loc.d = (loc.d+2)%4
  68.   robot.turnAround()
  69. end
  70. function turnLeft()
  71.   loc.d = (loc.d+3)%4
  72.   robot.turnLeft()
  73. end
  74.  
  75. -- ============================= F U E L  C O N T R O L ================================ --
  76. function check_fuel()
  77.   if (computer.maxEnergy() - computer.energy()) > 1000 then
  78.     for i=empty_slot, INV_SIZE do
  79.       robot.select(i)
  80.       if gen.insert() then break end
  81.     end
  82.   end
  83. end
  84.  
  85. -- ============================= I T E M  C O N T R O L ================================ --
  86. function check_loot()
  87.   for i=empty_slot, INV_SIZE do
  88.     if robot.count(i) == 0 then return false end
  89.   end
  90.   return true
  91. end
  92.  
  93. function unload()
  94.   -- place ender chest
  95.   robot.select(chest_slot)
  96.   robot.swing()
  97.   if robot.place() then
  98.     -- put items
  99.     for i=empty_slot, INV_SIZE do
  100.       robot.select(i)
  101.       while robot.drop() do end
  102.     end
  103.  
  104.     -- grab ender chest
  105.     robot.select(chest_slot)
  106.     robot.swing()
  107.   end
  108. end
  109.  
  110. -- =================================== M I N I N G ===================================== --
  111. function trash(side)
  112.   for i=1, trash_slots do
  113.     robot.select(i)
  114.     if side == sides.up then
  115.       a,t = robot.detectUp()
  116.       if t == 'air' or t=='liquid' then return true end
  117.       if robot.compareUp() then return true end
  118.     elseif side == sides.front then
  119.       a,t = robot.detect()
  120.       if t == 'air' or t=='liquid' then return true end
  121.       if robot.compare() then return true end
  122.     else
  123.       a,t = robot.detectDown()
  124.       if t == 'air' or t=='liquid' then return true end
  125.       if robot.compareDown() then return true end
  126.     end
  127.   end
  128.   return false
  129. end
  130.  
  131. function mine(side)
  132.   local direct = loc.d
  133.   local backdir = (direct+2)%4
  134.  
  135.   if side == sides.up then
  136.     c = 0
  137.     while not up() do
  138.       robot.swingUp()
  139.       c = c + 1
  140.       if c>30 then return end
  141.     end
  142.   elseif side == sides.front then
  143.     c = 0
  144.     while not forward() do
  145.       robot.swing()
  146.       c = c + 1
  147.       if c>30 then return end
  148.     end
  149.   elseif side == sides.down then
  150.     c = 0
  151.     while not down() do
  152.       robot.swingDown()
  153.       c = c + 1
  154.       if c>30 then return end
  155.     end
  156.   end
  157.  
  158.   lode[loc.x*MAX*MAX + loc.y*MAX + loc.z] = true
  159.  
  160.   -- check further direction
  161.   if lode[loc.x*MAX*MAX + loc.y*MAX + (loc.z+1)] == nil then
  162.     if not trash(sides.up) then mine(sides.up)
  163.       else lode[loc.x*MAX*MAX + loc.y*MAX + (loc.z+1)] = false end
  164.   end
  165.   if lode[loc.x*MAX*MAX + loc.y*MAX + (loc.z-1)] == nil then
  166.     if not trash(sides.down) then mine(sides.down)
  167.     else lode[loc.x*MAX*MAX + loc.y*MAX + (loc.z-1)] = false end
  168.   end
  169.  
  170.   for i=loc.d, loc.d+3 do
  171.     local a = i%4
  172.     local x = loc.x
  173.     local y = loc.y
  174.     if a == 0 then y = y + 1
  175.     elseif a == 1 then x = x + 1
  176.     elseif a == 2 then y = y - 1
  177.     else x = x - 1 end
  178.     if lode[x*MAX*MAX + y*MAX + loc.z] == nil then
  179.       while loc.d < a do turnRight() end
  180.       while loc.d > a do turnLeft() end
  181.       if not trash(sides.front) then mine(sides.front)
  182.       else lode[x*MAX*MAX + y*MAX + loc.z] = false end
  183.     end
  184.   end
  185.  
  186.   -- come back
  187.   if side == sides.up then
  188.     down()
  189.   elseif side == sides.front then
  190.     while loc.d < backdir do turnRight() end
  191.     while loc.d > backdir do turnLeft() end
  192.     while not forward() do robot.swing() end
  193.   elseif side == sides.down then
  194.     up()
  195.   end
  196. end
  197.  
  198. function go(side)
  199.   direct = loc.d
  200.   lode = {}
  201.   mine(side)
  202.   while loc.d < direct do turnRight() end
  203.   while loc.d > direct do turnLeft() end
  204. end
  205.  
  206. function step(progress)
  207.   -- check top/down
  208.   if not trash(stepside[progress%2]) then go(stepside[progress%2]) end
  209.   turnLeft()
  210.  
  211.   -- dig one column
  212.   for y=1, h do
  213.     -- check sides
  214.     if not trash(sides.front) then go(sides.front) end
  215.     turnAround()
  216.     if not trash(sides.front) then go(sides.front) end
  217.  
  218.     -- move up or down
  219.     if y<h then
  220.       if progress%2 == 1 then
  221.         while not up() do robot.swingUp() end
  222.       else
  223.         while not down() do robot.swingDown() end
  224.       end
  225.     end
  226.   end
  227.  
  228.   -- check down/top
  229.   if not trash(stepside[(progress+1)%2]) then go(stepside[(progress+1)%2]) end
  230.  
  231.   -- turn forward
  232.   if h%2 == 0 then
  233.     turnRight()
  234.   else
  235.     turnLeft()
  236.   end
  237.  
  238.   -- move forward
  239.   if progress<l then
  240.     c = 0
  241.     while not forward() do
  242.       robot.swing()
  243.       c = c + 1
  244.       if c>30 then return end
  245.     end
  246.   end
  247.  
  248.   -- fuel checking
  249.   check_fuel()
  250.   -- loot checking
  251.   if check_loot() then unload() end
  252. end
  253.  
  254. -- ===================================== M A I N ======================================= --
  255. cloader.setActive(true)
  256. print("[INFO] Chunk Loader is active.")
  257. print("[INFO] Press any key to break mining...")
  258.  
  259. for i=1, l do
  260.   step(i)
  261. end
  262.  
  263. while down() do end
  264.  
  265. cloader.setActive(false)
  266. print("[INFO] Chunk Loader deactivated.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement