Advertisement
Nokiyen

flatMining2

May 16th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.53 KB | None | 0 0
  1. --[[
  2. ***********
  3.  * flatMining
  4.  *
  5.  * mine in shape of a flat space.
  6.  * slot1~4 = ore
  7.  * slot5 = Cobblestone, slot6 = chest, slot7,8 = torch *
  8.  *
  9. **********
  10. ]]
  11.  
  12.  
  13.  
  14. -- get arguments.
  15. local tArgs = { ... }
  16. -- define vars.
  17. local area = tArgs[1]
  18. local side = 16
  19. local oreSlot1 = 1
  20. local oreSlot2 = 2
  21. local oreSlot3 = 3
  22. local oreSlot4 = 4
  23. local cobbleSlot = 5
  24. local chestSlot = 6
  25. local torchSlot1 = 7
  26. local torchSlot2 = 8
  27.  
  28.  
  29. --define functions.
  30. function mineStraight(line)
  31.  
  32.     local residue = area*side-2
  33.     local height = 1
  34.  
  35.     while residue > 0 do
  36.         if compare('down') then
  37.             dig('down')
  38.         end
  39.         if compare('up') then
  40.             dig('up')
  41.         end
  42.         drop()
  43.         torch(line, residue)
  44.         if turtle.detect() == false then
  45.             move('front')
  46.             residue = residue - 1
  47.         elseif compare('front') then
  48.             dig('front')
  49.             move('front')
  50.             residue = residue - 1
  51.         else
  52.             dig('up')
  53.             move('up')
  54.             height = height + 1
  55.             residue, height = detour(residue, height)
  56.         end
  57.     end
  58.  
  59.     while height > 1 do
  60.         dig('down')
  61.         move('down')
  62.         height = height - 1
  63.     end
  64.  
  65.     if compare('down') then
  66.         dig('down')
  67.     end
  68.     if compare('up') then
  69.         dig('up')
  70.     end
  71.  
  72.     dig('front')
  73.     move('front')
  74.  
  75. end
  76.  
  77. function compare(dir)
  78.  
  79.     local currentFunc = turtle.compare
  80.     if dir == 'up' then
  81.         currentFunc = turtle.compareUp
  82.     elseif dir == 'down' then
  83.         currentFunc = turtle.compareDown
  84.     end
  85.  
  86.     local flag = false
  87.  
  88.     turtle.select(oreSlot1)
  89.     flag = currentFunc()
  90.     turtle.select(oreSlot2)
  91.     flag = currentFunc()
  92.     turtle.select(oreSlot3)
  93.     flag = currentFunc()
  94.     turtle.select(oreSlot4)
  95.     flag = currentFunc()
  96.  
  97.     if flag == true then
  98.         return false
  99.     else
  100.         return true
  101.     end
  102.  
  103. end
  104.  
  105. function detour(residue, height)
  106.  
  107.     while compare('front') == false and turtle.detect() == true do
  108.         dig('up')
  109.         move('up')
  110.         height = height + 1
  111.     end
  112.     dig('front')
  113.     move('front')
  114.     residue = residue -1
  115.  
  116.     if residue == 0 then
  117.         return residue, height
  118.     end
  119.  
  120.     while height > 1 do
  121.         if compare('front') == false and turtle.detect() == true then
  122.             while compare('front') == false and turtle.detect() == true do
  123.                 dig('up')
  124.                 move('up')
  125.                 height = height + 1
  126.             end
  127.             dig('front')
  128.             move('front')
  129.             residue = residue - 1
  130.         elseif compare('front') or turtle.detect() == false then
  131.             dig('front')
  132.             move('front')
  133.             residue = residue - 1
  134.             while compare('down') or turtle.detectDown() == false do
  135.                 dig('down')
  136.                 move('down')
  137.                 height = height - 1
  138.                 if height == 1 then
  139.                     return residue, height
  140.                 end
  141.             end
  142.         end
  143.  
  144.         if residue == 0 then
  145.             return residue, height
  146.         end
  147.     end
  148.     return residue, height
  149.  
  150. end
  151.  
  152. function turn(dir)
  153.  
  154.     if compare('down') then
  155.         dig('down')
  156.     end
  157.     if compare('up') then
  158.         dig('up')
  159.     end
  160.  
  161.     local currentFunc
  162.     if dir%2 == 0 then
  163.         currentFunc = turtle.turnLeft
  164.     else
  165.         currentFunc = turtle.turnRight
  166.     end
  167.  
  168.     currentFunc()
  169.     dig('front')
  170.     move('front')
  171.     currentFunc()
  172.    
  173. end
  174.  
  175. function drop()
  176.  
  177.     turtle.select(cobbleSlot)
  178.     turtle.drop(turtle.getItemCount(cobbleSlot)-1)
  179.     if turtle.getItemCount(16) ~= 0 then
  180.         turtle.select(chestSlot)
  181.         if turtle.placeDown() then
  182.             for i=8, 16, 1 do
  183.                 turtle.select(torchSlot1)
  184.                 if turtle.compareTo(i) == false then
  185.                     turtle.select(i)
  186.                     turtle.dropDown()
  187.                 end
  188.             end
  189.         end
  190.     end
  191.     turtle.select(1)
  192.  
  193. end
  194.  
  195. function dig(dir)
  196.  
  197.     local currentDig = turtle.dig
  198.     local currentDetect = turtle.detect
  199.     if dir == 'up' then
  200.         currentDig = turtle.digUp
  201.         currentDetect = turtle.detectUp
  202.     elseif dir == 'down' then
  203.         currentDig = turtle.digDown
  204.         currentDetect = turtle.detectDown
  205.     end
  206.  
  207.     local times = 0
  208.     while currentDetect() do
  209.         currentDig()
  210.         times = times + 1
  211.         if times == 15 then
  212.             break
  213.         end
  214.         sleep(1)
  215.     end
  216.  
  217. end
  218.  
  219. function move(dir)
  220.  
  221.     local currentMove = turtle.forward
  222.     local currentAttack = turtle.attack
  223.     if dir == 'up' then
  224.         currentMove = turtle.up
  225.         currentAttack = turtle.attackUp
  226.     elseif dir == 'down' then
  227.         currentMove = turtle.down
  228.         currentAttack = turtle.attackDown
  229.     end
  230.  
  231.     local times = 0
  232.     while currentMove() == false do
  233.         currentAttack()
  234.         times = times + 1
  235.         if times == 15 then
  236.             return false
  237.         end
  238.     end
  239.     return true
  240.  
  241. end
  242.  
  243. function torch(line, residue)
  244.  
  245.     if (line-3)%7 ~= 0 or residue%7 ~= 0 then
  246.         return
  247.     end
  248.  
  249.     turtle.select(torchSlot1)
  250.     if turtle.compareTo(torchSlot2) then
  251.         turtle.select(torchSlot2)
  252.     end
  253.     turtle.placeDown()
  254.     turtle.select(1)
  255.  
  256. end
  257.  
  258.  
  259. --main thread.
  260. for i=1, area*side-1, 1 do
  261.     print('start line'..i..'.')
  262.     mineStraight(i)
  263.     turn(i+1)
  264. end
  265. mineStraight(area*side)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement