MoonlightOwl

Totoro Horizontal Miner 0.41

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