Advertisement
Guest User

ex

a guest
Aug 11th, 2014
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.56 KB | None | 0 0
  1. args = {...}
  2.  
  3. widthx=0
  4. widthy=0
  5. depth=0
  6.  
  7. x=0
  8. y=0
  9. d=0
  10.  
  11. xdir=1
  12. ydir=0
  13.  
  14. xdirSurface = 1
  15. ydirSurface = 0
  16.  
  17. onWayUp=0
  18.  
  19. ignoreCount=0
  20.  
  21. onWayHome=0
  22.  
  23. lastSpotx=0
  24. lastSpoty=0
  25. lastSpotd=0
  26.  
  27. function getCountFreeSlots()
  28.   freeslots = 0
  29.   for i=ignoreCount+1,15 do
  30.     if turtle.getItemCount(i)==0 then
  31.       freeslots=freeslots+1
  32.     end
  33.   end
  34.   return freeslots
  35. end
  36.  
  37. function checkInventory()
  38.   if getCountFreeSlots() == 0 then
  39.     goHome()
  40.   end
  41.   if onWayHome==1 then
  42.     goHome()
  43.   end
  44.   if onWayHome==-1 then
  45.     goBack()
  46.   end
  47. end
  48.  
  49. function right()
  50.   if turtle.turnRight() then
  51.     help=xdir
  52.     xdir=-ydir
  53.     ydir=help
  54.   end
  55.   save()
  56. end
  57.  
  58. function left()
  59.   if turtle.turnLeft() then
  60.     help=xdir
  61.     xdir=ydir
  62.     ydir=-help
  63.   end
  64.   save()
  65. end
  66.  
  67. function forward()
  68.   refuel()
  69.   while not turtle.forward() do
  70.     os.sleep(1)
  71.     turtle.dig()
  72.     checkInventory()
  73.   end
  74.   x=x+xdir
  75.   y=y+ydir
  76.   save()
  77. end
  78.  
  79. function forwardAndDig()
  80.   refuel()
  81.   if isRabbitHoleSpot() then
  82.     rabbitHole()
  83.   end
  84.   while not turtle.forward() do
  85.     os.sleep(1)
  86.     turtle.dig()
  87.     checkInventory()
  88.   end
  89.   x=x+xdir
  90.   y=y+ydir
  91.   save()
  92. end
  93.  
  94. function up()
  95.   refuel()
  96.   while turtle.up() == false do
  97.     os.sleep(1)
  98.     turtle.digUp()
  99.     checkInventory()
  100.   end
  101.   d=d-1
  102.   save()
  103. end
  104.  
  105. function down()
  106.   refuel()
  107.   while turtle.down() == false do
  108.     os.sleep(1)
  109.     if not turtle.digDown() then
  110.       return false
  111.     end
  112.     checkInventory()
  113.   end
  114.   d=d+1
  115.   save()
  116.   return true
  117. end
  118.  
  119. function isRabbitHoleSpot()
  120.   if ((x + ((y*2) % 5)) % 5) == 0 then
  121.     return true
  122.   else
  123.     return false
  124.   end
  125. end
  126.  
  127. function rabbitHole()
  128.   if onWayUp == 0 then
  129.     xdirSurface=xdir
  130.     ydirSurface=ydir
  131.     while d~=depth and down() do
  132.       rotaDigger()
  133.     end
  134.     onWayUp=1
  135.   end
  136.   if onWayUp==1 then
  137.     while d ~= 0 do
  138.       up()
  139.     end
  140.     while not (xdir == xdirSurface and ydir == ydirSurface) do
  141.       left()
  142.     end
  143.  
  144.     for i=1,ignoreCount do
  145.       if turtle.getItemCount(i) > 1 then
  146.         turtle.select(i)
  147.         turtle.placeDown()
  148.         i=ignoreCount
  149.       end
  150.     end
  151.     turtle.select(1)
  152.     onWayUp=0
  153.   end
  154.  
  155. end
  156.  
  157. function isIgnoreBlock()
  158.   for i = 1,ignoreCount do
  159.     turtle.select(i)
  160.     if turtle.compare() then
  161.       return true
  162.     end
  163.   end
  164.   return false
  165. end
  166.  
  167. function rotaDigger()
  168.   for i=1,3 do
  169.     if isIgnoreBlock() == false then
  170.       turtle.dig()
  171.       checkInventory()
  172.     end
  173.     left()
  174.   end
  175.   if isIgnoreBlock() == false then
  176.     turtle.dig()
  177.     checkInventory()
  178.   end
  179. end
  180.  
  181. function goHome()
  182.   if not onWayHome == 1 then
  183.     lastSpotx=x
  184.     lastSpoty=y
  185.     lastSpotd=d
  186.         onWayHome=1
  187.     end
  188.  
  189.   if d ~= 0 then
  190.     onWayUp=1
  191.     RabbitHole()
  192.   end
  193.   while not (xdir==-1) do
  194.     left()
  195.   end
  196.   while not (x==0) do
  197.     forward()
  198.   end
  199.   while not (ydir==-1) do
  200.     left()
  201.   end
  202.   while not (y==0) do
  203.     forward()
  204.   end
  205.   while not (xdir==-1) do
  206.     left()
  207.   end
  208.   clearInventory()
  209.   onWayHome=-1
  210. end
  211.  
  212. function goBack()
  213.   if d ~= 0 then
  214.     onWayUp=1
  215.     RabbitHole()
  216.   end
  217.   while not (xdir==1) do
  218.     left()
  219.   end
  220.   while not (x==lastSpotx) do
  221.     foward()
  222.   end
  223.   while not (ydir==1) do
  224.     left()
  225.   end
  226.   while not (y==lastSpoty) do
  227.     forward()
  228.   end
  229.   while not (d==lastSpotd) do
  230.     down()
  231.   end
  232.   onWayHome=0
  233. end
  234.  
  235. function refuel()
  236.   while turtle.getFuelLevel() < 80 do
  237.     for i=ignoreCount+1,15 do
  238.       turtle.select(i)
  239.       if turtle.compareTo(16) then
  240.         turtle.transferTo(16)
  241.       end
  242.     end
  243.     turtle.select(16)
  244.     turtle.refuel(turtle.getItemCount(16)-1)
  245.   end
  246.   turtle.select(1)
  247. end
  248.  
  249. function clearInventory()
  250.   for i=ignoreCount+1,15 do
  251.     turtle.select(i)
  252.     if turtle.compareTo(16) then
  253.       turtle.transferTo(16)
  254.     end
  255.   end
  256.   for i=ignoreCount+1,15 do
  257.     turtle.select(i)
  258.     turtle.drop()
  259.   end
  260. end
  261.  
  262. function uTurn()
  263.   if y == widthy-1 then
  264.     return
  265.   end
  266.   if xdir == 1 and x == widthx-1 then
  267.     right()
  268.     forwardAndDig()
  269.     right()
  270.     return
  271.   end
  272.   if xdir == -1 and x == 0 then
  273.     left()
  274.     forwardAndDig()
  275.     left()
  276.     return
  277.   end
  278.   if (xdir==0 and (x==0 or x==widthx-1)) then
  279.     if x==0 then
  280.       if y%2==1 then
  281.         forwardAndDig()
  282.       end
  283.     left()
  284.     return
  285.     end
  286.     if x==widthx-1 then
  287.       if y%2==0 then
  288.         forwardAndDig()
  289.       end
  290.       right()
  291.       return
  292.     end
  293.   end
  294. end
  295.  
  296. function readFromSave()
  297.   if fs.exists("ex.save") then
  298.     h=fs.open("ex.save","r")
  299.     if h ~= nil then
  300.       widthx=tonumber(h.readLine())
  301.       widthy=tonumber(h.readLine())
  302.       depth=tonumber(h.readLine())
  303.       x=tonumber(h.readLine())
  304.       y=tonumber(h.readLine())
  305.       d=tonumber(h.readLine())
  306.       xdir=tonumber(h.readLine())
  307.       ydir=tonumber(h.readLine())
  308.       xdirSurface=tonumber(h.readLine())
  309.       ydirSurface=tonumber(h.readLine())
  310.       onWayUp=tonumber(h.readLine())
  311.       ignoreCount=tonumber(h.readLine())
  312.       onWayHome=tonumber(h.readLine())
  313.       lastSpotx=tonumber(h.readLine())
  314.       lastSpoty=tonumber(h.readLine())
  315.       lastSpotd=tonumber(h.readLine())
  316.       h.close()
  317.       return true
  318.     else
  319.       return false
  320.     end
  321.   else
  322.     return false
  323.   end
  324. end
  325.  
  326. function save()
  327.   h=fs.open("ex.save","w")
  328.   if h~= nil then
  329.     h.writeLine(widthx)
  330.     h.writeLine(widthy)
  331.     h.writeLine(depth)
  332.     h.writeLine(x)
  333.     h.writeLine(y)
  334.     h.writeLine(d)
  335.     h.writeLine(xdir)
  336.     h.writeLine(ydir)
  337.     h.writeLine(xdirSurface)
  338.     h.writeLine(ydirSurface)
  339.     h.writeLine(onWayUp)
  340.     h.writeLine(IgnoreCount)
  341.     h.writeLine(onWayHome)
  342.     h.writeLine(lastSpotx)
  343.     h.writeLine(lastSpoty)
  344.     h.writeLine(lastSpotd)
  345.     h.flush()
  346.     h.close()
  347.     os.sleep(0.2)
  348.   end
  349. end
  350.  
  351. function main()
  352.   if args[1] == nil or args[1] == "" then
  353.     print("Arguments: widht-X, width-Y, depth,")
  354.     print("           ignore-block-count")
  355.     print("        OR 'continue' to continue")
  356.     print("           previous excavation")
  357.     print("Put ignore-blocks")
  358.     print("  in slot 1 to #ignoreblockcount")
  359.     print("  and fuel in slot 16")
  360.     return
  361.   end
  362.   if args[1] == "continue" then
  363.    if readFromSave() == false then
  364.      return 1
  365.    end
  366.   else
  367.     widthx=tonumber(args[1])
  368.     widthy=tonumber(args[2])
  369.     depth=tonumber(args[3])
  370.     ignoreCount=tonumber(args[4])
  371.   end
  372.   repeat
  373.     forwardAndDig()
  374.     uTurn()
  375.     term.clear()
  376.   until y == widthy-1 and ((xdir==-1 and x==0) or (xdir==1 and x==widthx-1))
  377.  
  378.   goHome()
  379.   onWayHome=0
  380.   clearInventory()
  381. end
  382.  
  383. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement