Guido_Fe

Robot

Jan 8th, 2017
115
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 or freeSlots <=1 then
  133.       if nCoal<=dist then
  134.         f.write("Not enough coal for the journey, returning home (left:")
  135.         f.write(nCoal)
  136.         f.write(", coord(")
  137.         f.write(x)
  138.         f.write(",")
  139.         f.write(y)
  140.         f.writeLine("))")
  141.       end
  142.       stopx=x
  143.       stopy=y
  144.       if freeSlots <=1 then f.writeLine("Not enough slots, returning home") end
  145.       if state=="up" then
  146.        turtle.turnLeft()
  147.        turtle.turnLeft()
  148.       end
  149.       while y>0 do
  150.         while turtle.detect() do
  151.           turtle.select(1)
  152.           turtle.dig()
  153.         end
  154.         clearInventory()
  155.         if turtle.getFuelLevel()<2 then refuel() end
  156.         turtle.forward()
  157.         y=y-1
  158.       end
  159.       turtle.turnRight()
  160.       while x>0 do
  161.         while turtle.detect() do
  162.           turtle.select(1)
  163.           turtle.dig()
  164.         end
  165.         clearInventory()
  166.         if turtle.getFuelLevel()<2 then refuel() end
  167.         turtle.forward()
  168.         x=x-1
  169.       end
  170.       success,storage=turtle.inspect()
  171.       if success and storage.name=="minecraft:chest" then
  172.         for c=1,16 do
  173.           turtle.select(c)
  174.           item=turtle.getItemDetail()
  175.           if item and not item.name=="minecraft:coal" then
  176.             dropSuccess=turtle.drop()
  177.             if dropSuccess then
  178.               f.write(item.name)
  179.               f.writeLine(" dropped in chest")
  180.             else
  181.               f.writeLine("Chest full!! Shutting down...")
  182.               f.close()
  183.               os.shutdown()  
  184.             end
  185.           end
  186.         end
  187.         if noCoal then
  188.           success=turtle.suckUp()
  189.           if not success then
  190.             f.writeLine("no coal chest or coal, shutting down...")
  191.             f.close()
  192.             os.shutdown()
  193.           end
  194.         end
  195.         turtle.turnRight()
  196.         turtle.turnRight()
  197.         while x<stopx do
  198.           find()
  199.           if turtle.getFuelLevel()<2 then refuel() end
  200.           while turtle.detect() do
  201.             turtle.select(1)
  202.             turtle.dig()
  203.           end
  204.           clearInventory()
  205.           turtle.forward()
  206.           x=x+1
  207.         end
  208.         turtle.turnLeft()
  209.         while y<stopy do
  210.           find()
  211.           if turtle.getFuelLevel()<2 then refuel() end
  212.           while turtle.detect() do
  213.             turtle.select(1)
  214.             turtle.dig()
  215.           end
  216.           clearInventory()
  217.           turtle.forward()
  218.           y=y+1
  219.         end
  220.         if state==down then
  221.           turtle.turnLeft()
  222.           turtle.turnLeft()
  223.         end
  224.       else
  225.         f.writeLine("No chest, shutting down...")
  226.         os.shutdown()
  227.       end
  228.     end
  229.     find()
  230.     while turtle.detect() do
  231.       turtle.select(1)
  232.       turtle.dig()
  233.     end
  234.     turtle.forward()      
  235.   end
  236.   f.write("Ended x ")
  237.   f.writeLine(x)
  238.   if state=="up" then
  239.     turtle.turnRight()
  240.   else turtle.turnLeft()
  241.   end
  242.   for i=1, 3 do
  243.     find()
  244.     while turtle.detect() do
  245.       turtle.select(1)
  246.       turtle.dig()
  247.     end
  248.     turtle.forward()
  249.     x=x+1
  250.   end
  251.   if state=="up" then
  252.   turtle.turnRight()
  253.     state="down"
  254.   else
  255.     turtle.turnLeft()
  256.     state="up"
  257.   end
  258. end --a
  259. f.writeLine("Returning home")
  260. if state=="up" then
  261.   turtle.turnLeft()
  262.   turtle.turnLeft()
  263. end
  264. while y>0 do
  265.   find()
  266.   if turtle.getFuelLevel()<2 then refuel() end
  267.   while turtle.detect() do
  268.     turtle.select(1)
  269.     turtle.dig()
  270.   end
  271.   turtle.forward()
  272.   y=y-1
  273.   clearInventory()
  274. end
  275. turtle.turnRight()
  276. while x>0 do
  277.   if turtle.getFuelLevel()<2 then refuel() end
  278.   find()
  279.   while turtle.detect() do
  280.     turtle.select(1)
  281.     turtle.dig()
  282.   end
  283.   turtle.forward()
  284.   x=x-1
  285.   clearInventory()
  286. end
  287. f.writeLine("Arrived! Shutting down...")
  288. f.close()
  289. os.shutdown()
Add Comment
Please, Sign In to add comment