Advertisement
Nokiyen

flatMining3

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