Guido_Fe

Untitled

Jan 7th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local maxX,maxY = ...
  2. maxX= tonumber(maxX)
  3. maxY= tonumber(maxY)
  4. f=fs.open("log","a")
  5. f.writeLine("--------------------")
  6. f.write("maxX= ")
  7. f.write(maxX)
  8. f.write(" maxY= ")
  9. f.writeLine(maxY)
  10. noCoal=false
  11.  
  12. refuel=function()
  13.   success=false
  14.   c=1
  15.   while not success and c<16 do
  16.     turtle.select(1)
  17.     success=turtle.refuel()
  18.   end
  19.   return success
  20. end
  21.  
  22. clearInventory = function()
  23.   for c=1, 16 do
  24.     turtle.select(c)
  25.     item=turtle.getItemDetail()    
  26.     if item then
  27.       if not check(item.name) then
  28.         turtle.drop()
  29.       end
  30.     end
  31.   end
  32. end
  33.  
  34. check=function (item)
  35.   if item=="minecraft:chest" or item:match "minecraft:.*stone.*" or item:match "minecraft:dirt.*" or item=="minecraft:gravel" or item:match "chisel:.*" or item=="minecraft:torch" then
  36.     return false
  37.     else
  38.       return true
  39.   end
  40. end
  41.  
  42. find = function ()
  43.   local success, item=turtle.inspectUp()
  44.   if success then
  45.     if check(item.name) then
  46.       turtle.select(1)
  47.       turtle.digUp()
  48.       f.write("Found up: ")
  49.       f.writeLine(item.name)
  50.     end
  51.   end
  52.   success, item=turtle.inspectDown()
  53.   if success then
  54.     if check(item.name) then
  55.       turtle.select(1)
  56.       turtle.digDown()
  57.       f.write("Found down: ")
  58.       f.writeLine(item.name)
  59.     end
  60.   end
  61.   turtle.turnLeft()
  62.   success, item=turtle.inspect()
  63.   if success then
  64.     if check(item.name) then
  65.       turtle.select(1)
  66.       turtle.dig()
  67.       f.write("Found left: ")
  68.       f.writeLine(item.name)  
  69.     end
  70.   end
  71.   turtle.turnRight()
  72.   turtle.turnRight()
  73.   success, item=turtle.inspect()
  74.   if success then
  75.     if check(item.name) then
  76.       turtle.select(1)
  77.       turtle.dig()
  78.       f.write("Found right: ")
  79.       f.writeLine(item.name)  
  80.     end
  81.   end
  82.   turtle.turnLeft()
  83.   success, item=turtle.inspect()
  84.   if success then
  85.     if check(item.name) then
  86.       turtle.select(1)
  87.       turtle.dig()
  88.       f.write("Found forward: ")
  89.       f.writeLine(item.name)  
  90.     end
  91.   end
  92. end
  93.  
  94. local x=0
  95. local y=0
  96. local state = "up"
  97. while x<maxX do --a
  98.   for y=0, maxY do --b
  99.     local fuelLevel = turtle.getFuelLevel()
  100.     if fuelLevel<= (x+y) then --c
  101.       local fuelFound=false
  102.       local currSelected=1
  103.       repeat
  104.         turtle.select(currSelected)
  105.         fuelFound = turtle.refuel()
  106.         currSelected = currSelected+1  
  107.       until fuelFound == true or currSelected>16
  108.       if fuelFound==false then
  109.         f.writeLine("Fuel not found")
  110.       end
  111.     end --c
  112.     local nCoal=0
  113.     freeSlots = 0
  114.     for c=1,16 do --d
  115.       local item=turtle.getItemDetail(c)
  116.       if item then
  117.         if item.name=="minecraft:coal" then
  118.           nCoal = nCoal+item.count
  119.         end
  120.         if not check(item.name) then
  121.           turtle.select(c)
  122.           turtle.drop()
  123.           freeSlots=freeSlots+1
  124.         end
  125.       else
  126.           freeSlots=freeSlots+1
  127.       end
  128.     end --
  129.     nCoal = nCoal*80+turtle.getFuelLevel()-4
  130.     local dist=x+y
  131.     noCoal=true
  132.     if nCoal<=dist then
  133.       f.write("Not enough coal for the journey, returning home (left:")
  134.       f.write(nCoal)
  135.       f.write(", coord(")
  136.       f.write(x)
  137.       f.write(",")
  138.       f.write(y)
  139.       f.writeLine("))")
  140.     end
  141.     stopx=x
  142.     stopy=y
  143.     if freeSlots <=1 then f.writeLine("Not enough slots, returning home") end
  144.     if state=="up" then
  145.      turtle.turnLeft()
  146.      turtle.turnLeft()
  147.     end
  148.     while y>0 do
  149.       while turtle.detect() do
  150.         turtle.select(1)
  151.         turtle.dig()
  152.       end
  153.       clearInventory()
  154.       if turtle.getFuelLevel()<2 then refuel() end
  155.       turtle.forward()
  156.       y=y-1
  157.     end
  158.     turtle.turnRight()
  159.     while x>0 do
  160.       while turtle.detect() do
  161.         turtle.select(1)
  162.         turtle.dig()
  163.       end
  164.       clearInventory()
  165.       if turtle.getFuelLevel()<2 then refuel() end
  166.       turtle.forward()
  167.       x=x-1
  168.     end
  169.     success,storage=turtle.inspect()
  170.     if success and storage.name=="minecraft:chest" then
  171.       for c=1,16 do
  172.         turtle.select(c)
  173.         item=turtle.getItemDetail()
  174.         if item and not item.name=="minecraft:coal" then
  175.           dropSuccess=turtle.drop()
  176.           if dropSuccess then
  177.             f.write(item.name)
  178.             f.writeLine(" dropped in chest")
  179.           else
  180.             f.writeLine("Chest full!! Shutting down...")
  181.             f.close()
  182.             os.shutdown()  
  183.           end
  184.         end
  185.       end
  186.       if noCoal then success=turtle.suckUp()
  187.         if not success then
  188.           f.writeLine("no coal chest or coal, shutting down...")
  189.           f.close()
  190.           os.shutdown()
  191.         end
  192.       end
  193.       turtle.turnRight()
  194.       turtle.turnRight()
  195.       while x<stopx do
  196.         find()
  197.         if turtle.getFuelLevel()<2 then refuel() end
  198.         while turtle.detect() do
  199.           turtle.select(1)
  200.           turtle.dig()
  201.         end
  202.         clearInventory()
  203.         turtle.forward()
  204.         x=x+1
  205.       end
  206.       turtle.turnLeft()
  207.       while y<stopy do
  208.         find()
  209.         if turtle.getFuelLevel()<2 then refuel() end
  210.         while turtle.detect() do
  211.           turtle.select(1)
  212.           turtle.dig()
  213.         end
  214.         clearInventory()
  215.         turtle.forward()
  216.         y=y+1
  217.       end
  218.       if state==down then
  219.         turtle.turnLeft()
  220.         turtle.turnLeft()
  221.       end
  222.     else
  223.       f.writeLine("No chest, shutting down...")
  224.       os.shutdown()
  225.     end
  226.     find()
  227.     while turtle.detect() do
  228.       turtle.select(1)
  229.       turtle.dig()
  230.     end
  231.     turtle.forward()      
  232.   end
  233.   f.write("Ended x ")
  234.   f.writeLine(x)
  235.   if state=="up" then
  236.     turtle.turnRight()
  237.   else turtle.turnLeft()
  238.   end
  239.   for i=1, 3 do
  240.     find()
  241.     while turtle.detect() do
  242.       turtle.select(1)
  243.       turtle.dig()
  244.     end
  245.     turtle.forward()
  246.     x=x+1
  247.   end
  248.   if state=="up" then
  249.   turtle.turnRight()
  250.     state="down"
  251.   else
  252.     turtle.turnLeft()
  253.     state="up"
  254.   end
  255. end --a
  256. f.writeLine("Returning home")
  257. if state=="up" then
  258.   turtle.turnLeft()
  259.   turtle.turnLeft()
  260. end
  261. while y>0 do
  262.   find()
  263.   if turtle.getFuelLevel()<2 then refuel() end
  264.   while turtle.detect() do
  265.     turtle.select(1)
  266.     turtle.dig()
  267.   end
  268.   turtle.forward()
  269.   y=y-1
  270.   clearInventory()
  271. end
  272. turtle.turnRight()
  273. while x>0 do
  274.   if turtle.getFuelLevel()<2 then refuel() end
  275.   find()
  276.   while turtle.detect() do
  277.     turtle.select(1)
  278.     turtle.dig()
  279.   end
  280.   turtle.forward()
  281.   x=x-1
  282.   clearInventory()
  283. end
  284. f.writeLine("Arrived! Shutting down...")
  285. f.close()
  286. os.shutdown()
Add Comment
Please, Sign In to add comment