Guido_Fe

Code

Jan 14th, 2017
97
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(c)
  17.     success=turtle.refuel()
  18.   end
  19.   if success then
  20.     f.writeLine("Refueled")
  21.   end
  22.   return success
  23. end
  24.  
  25. clearInventory = function()
  26.   for c=1, 16 do
  27.     turtle.select(c)
  28.     item=turtle.getItemDetail()    
  29.     if item then
  30.       if not check(item.name) then
  31.         turtle.drop()
  32.       end
  33.     end
  34.   end
  35. end
  36.  
  37. check=function (item)
  38.   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
  39.     return false
  40.     else
  41.       return true
  42.   end
  43. end
  44.  
  45. find = function ()
  46.   local success, item=turtle.inspectUp()
  47.   if success then
  48.     if check(item.name) then
  49.       turtle.select(1)
  50.       turtle.digUp()
  51.       f.write("Found up: ")
  52.       f.writeLine(item.name)
  53.     end
  54.   end
  55.   success, item=turtle.inspectDown()
  56.   if success then
  57.     if check(item.name) then
  58.       turtle.select(1)
  59.       turtle.digDown()
  60.       f.write("Found down: ")
  61.       f.writeLine(item.name)
  62.     end
  63.   end
  64.   turtle.turnLeft()
  65.   success, item=turtle.inspect()
  66.   if success then
  67.     if check(item.name) then
  68.       turtle.select(1)
  69.       turtle.dig()
  70.       f.write("Found left: ")
  71.       f.writeLine(item.name)  
  72.     end
  73.   end
  74.   turtle.turnRight()
  75.   turtle.turnRight()
  76.   success, item=turtle.inspect()
  77.   if success then
  78.     if check(item.name) then
  79.       turtle.select(1)
  80.       turtle.dig()
  81.       f.write("Found right: ")
  82.       f.writeLine(item.name)  
  83.     end
  84.   end
  85.   turtle.turnLeft()
  86.   success, item=turtle.inspect()
  87.   if success then
  88.     if check(item.name) then
  89.       turtle.select(1)
  90.       turtle.dig()
  91.       f.write("Found forward: ")
  92.       f.writeLine(item.name)  
  93.     end
  94.   end
  95. end
  96.  
  97. local x=0
  98. local y=0
  99. local state = "up"
  100. while x<maxX do --a
  101.   while y<maxY do
  102.     f.write("X=")
  103.     f.write(x)    
  104.     f.write(" Y=")
  105.     f.writeLine(y)
  106.     local leftY=maxY-y
  107.     local dist
  108.     if state=="up" then
  109.       dist=x+y
  110.     else
  111.       dist=x+leftY
  112.     end
  113.     local fuelLevel = turtle.getFuelLevel()
  114.     if fuelLevel<= dist then --c
  115.       local fuelFound=false
  116.       local currSelected=1
  117.       repeat
  118.         turtle.select(currSelected)
  119.         fuelFound = turtle.refuel()
  120.         currSelected = currSelected+1  
  121.       until fuelFound == true or currSelected>16
  122.       if fuelFound==false then
  123.         f.writeLine("Fuel not found")
  124.       else f.writeLine("Refueled")
  125.       end
  126.     end --c
  127.     local nCoal=0
  128.     freeSlots = 0
  129.     for c=1,16 do --d
  130.       local item=turtle.getItemDetail(c)
  131.       if item then
  132.         if item.name=="minecraft:coal" then
  133.           nCoal = nCoal+item.count
  134.         end
  135.         if not check(item.name) then
  136.           turtle.select(c)
  137.           turtle.drop()
  138.           freeSlots=freeSlots+1
  139.         end
  140.       else
  141.           freeSlots=freeSlots+1
  142.       end
  143.     end --
  144.     nCoal = nCoal*80+turtle.getFuelLevel()-4
  145.     noCoal=true
  146.     if nCoal<=dist or freeSlots <=1 then
  147.       if nCoal<=dist then
  148.         f.write("Not enough coal for the journey, returning home (left:")
  149.         f.write(nCoal)
  150.         f.write(", coord(")
  151.         f.write(x)
  152.         f.write(",")
  153.         f.write(y)
  154.         f.writeLine("))")
  155.       end
  156.       if freeSlots <=1 then f.writeLine("Not enough slots, returning home") end
  157.       if state=="up" then
  158.        turtle.turnLeft()
  159.        turtle.turnLeft()
  160.       else
  161.        y=leftY
  162.       end
  163.       stopx=x
  164.       stopy=y
  165.       while y>0 do
  166.         while turtle.detect() do
  167.           turtle.select(1)
  168.           turtle.dig()
  169.         end
  170.         clearInventory()
  171.         if turtle.getFuelLevel()<2 then refuel() end
  172.         turtle.forward()
  173.         y=y-1
  174.         f.write("X=")
  175.         f.write(x)
  176.         f.write(" Y=")
  177.         f.writeLine(y)
  178.       end
  179.       turtle.turnRight()
  180.       while x>0 do
  181.         while turtle.detect() do
  182.           turtle.select(1)
  183.           turtle.dig()
  184.         end
  185.         clearInventory()
  186.         if turtle.getFuelLevel()<2 then refuel() end
  187.         turtle.forward()
  188.         x=x-1
  189.         f.write("X=")
  190.         f.write(x)
  191.         f.write(" Y=")
  192.         f.writeLine(y)
  193.       end
  194.       success,storage=turtle.inspect()
  195.       if success and storage.name=="minecraft:chest" then
  196.         f.writeLine("Chest found")
  197.         for c=1,16 do
  198.           turtle.select(c)
  199.           item=turtle.getItemDetail()
  200.           if item and not item.name=="minecraft:coal" then
  201.             dropSuccess=turtle.drop()
  202.             if dropSuccess then
  203.               f.write(item.name)
  204.               f.writeLine(" dropped in chest")
  205.             else
  206.               f.writeLine("Chest full!! Shutting down...")
  207.               f.close()
  208.               os.shutdown()  
  209.             end
  210.           end
  211.         end
  212.         if noCoal then
  213.           success=turtle.suckUp()
  214.           if not success then
  215.             f.writeLine("no coal chest or coal, shutting down...")
  216.             f.close()
  217.             os.shutdown()
  218.           else f.writeLine("Refueled from chest")
  219.           end
  220.         end
  221.         f.write("Returning to the last point (")
  222.         f.write(stopx)
  223.         f.write(", ")
  224.         f.write(stopy)
  225.         f.writeLine(")")
  226.         turtle.turnRight()
  227.         turtle.turnRight()
  228.         while x<stopx do
  229.           find()
  230.           if turtle.getFuelLevel()<2 then refuel() end
  231.           while turtle.detect() do
  232.             turtle.select(1)
  233.             turtle.dig()
  234.           end
  235.           clearInventory()
  236.           turtle.forward()
  237.           x=x+1
  238.           f.write("X=")
  239.           f.write(x)
  240.           f.write(" Y=")
  241.           f.writeLine(y)
  242.         end
  243.         f.writeLine("Turning left for y")
  244.         turtle.turnLeft()
  245.         while y<stopy do
  246.           find()
  247.           if turtle.getFuelLevel()<2 then refuel() end
  248.           while turtle.detect() do
  249.             turtle.select(1)
  250.             turtle.dig()
  251.           end
  252.           clearInventory()
  253.           turtle.forward()
  254.           y=y+1
  255.           f.write("X=")
  256.           f.write(x)
  257.           f.write(" Y=")
  258.           f.writeLinw(y)
  259.         end
  260.         if state==down then
  261.           f.writeLine("Turning to the original state")
  262.           turtle.turnLeft()
  263.           turtle.turnLeft()
  264.           y=maxY-leftY
  265.         end
  266.       else
  267.         f.writeLine("No chest, shutting down...")
  268.         f.close()
  269.         os.shutdown()
  270.       end
  271.     end
  272.     find()
  273.     while turtle.detect() do
  274.       turtle.select(1)
  275.       turtle.dig()
  276.     end
  277.     turtle.forward()      
  278.     y=y+1
  279.   end
  280.   f.write("Ended x ")
  281.   f.writeLine(x)
  282.   if state=="up" then
  283.     turtle.turnRight()
  284.   else turtle.turnLeft()
  285.   end
  286.   for i=1, 3 do
  287.     find()
  288.     while turtle.detect() do
  289.       turtle.select(1)
  290.       turtle.dig()
  291.     end
  292.     turtle.forward()
  293.     x=x+1
  294.   end
  295.   if state=="up" then
  296.   turtle.turnRight()
  297.     state="down"
  298.   else
  299.     turtle.turnLeft()
  300.     state="up"
  301.   end
  302. end --a
  303. f.writeLine("Returning home")
  304. if state=="up" then
  305.   turtle.turnLeft()
  306.   turtle.turnLeft()
  307. else
  308.   y=maxY-y
  309. end
  310. while y>0 do
  311.   find()
  312.   if turtle.getFuelLevel()<2 then refuel() end
  313.   while turtle.detect() do
  314.     turtle.select(1)
  315.     turtle.dig()
  316.   end
  317.   turtle.forward()
  318.   y=y-1
  319.   f.write("X=") f.write(x) f.write(" Y=") f.writeLine(y)
  320.   clearInventory()
  321. end
  322. turtle.turnRight()
  323. while x>0 do
  324.   if turtle.getFuelLevel()<2 then refuel() end
  325.   find()
  326.   while turtle.detect() do
  327.     turtle.select(1)
  328.     turtle.dig()
  329.   end
  330.   turtle.forward()
  331.   x=x-1
  332.   f.write("X=") f.write(x) f.write(" Y=") f.writeLine(y)
  333.   clearInventory()
  334. end
  335. f.writeLine("Arrived! Dropping loot into chest")
  336. success, item = turtle.inspect()
  337. if  item.name == "minecraft:chest" then
  338.   f.writeLine("Chest found, beginning transfusion...")
  339.   for c=1, 16 do
  340.     turtle.select(c)
  341.     item=turtle.getItemDetail()
  342.     if item and item.name=="minecraft:coal" then
  343.       success1=turtle.dropUp()
  344.       t=1
  345.     else
  346.       if item then
  347.         success2=turtle.drop()
  348.         t=2
  349.       end
  350.     end
  351.     if item and (success1 and t==1 or success2 and t==2) then
  352.       f.write("Dropped ")
  353.       f.write(item.count)
  354.       f.write(" ")
  355.       f.writeLine(item.name)
  356.     else
  357.       if item and t==1 then
  358.         f.writeLine("Coal inventory full!!")
  359.       else
  360.         if item then
  361.           f.writeLine("Item invetory full!!")
  362.         end
  363.       end
  364.     end
  365.   end
  366. end
  367. f.writeLine("Shutting down...")
  368. f.close()
  369. os.shutdown()
Add Comment
Please, Sign In to add comment