Advertisement
Nokiyen

flatMining2

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