nSun

CC optimine

Apr 4th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. os.loadAPI("posapi")
  2. os.loadAPI("cookieapi")
  3.  
  4. -- Configuration de l'inventaire
  5. local slot = {}
  6. slot["torch"] = 16
  7. slot["chest"] = 15
  8. -- Direction d'origine (direction des tunnels)
  9. local startDir = posapi.getDir()
  10. local startX = posapi.x
  11. local startY = posapi.y
  12. local startZ = posapi.z
  13. -- Longueur des tunnels
  14. local length = 0
  15. -- Nombre de paliers
  16. local nbFloors = 0
  17.  
  18. -- Place une torche sur le mur
  19. function wallTorch()
  20.     turtle.select(slot.torch)
  21.     turtle.place()
  22.     turtle.select(1)
  23. end
  24.  
  25. -- -------------------
  26. -- Tunnel
  27. -- -------------------
  28.  
  29. -- Mine in a quarry pattern until we hit something we can't dig
  30. local depth = 0
  31. local collected = 0
  32.  
  33. local function collect()
  34.     collected = collected + 1
  35.     if math.fmod(collected, 25) == 0 then
  36.         print( "Mined "..collected.." items." )
  37.     end
  38. end
  39.  
  40. local function tryDig()
  41.     while turtle.detect() do
  42.         if turtle.dig() then
  43.             collect()
  44.             sleep(0.5)
  45.         else
  46.             return false
  47.         end
  48.     end
  49.     return true
  50. end
  51.  
  52. local function tryDigUp()
  53.     while turtle.detectUp() do
  54.         if turtle.digUp() then
  55.             collect()
  56.             sleep(0.5)
  57.         else
  58.             return false
  59.         end
  60.     end
  61.     return true
  62. end
  63.  
  64. local function tryDigDown()
  65.     while turtle.detectDown() do
  66.         if turtle.digDown() then
  67.             collect()
  68.             sleep(0.5)
  69.         else
  70.             return false
  71.         end
  72.     end
  73.     return true
  74. end
  75.  
  76. local function refuel()
  77.     local fuelLevel = turtle.getFuelLevel()
  78.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  79.         return
  80.     end
  81.    
  82.     local function tryRefuel()
  83.         for n=1,14 do -- Edit: slot 15 et 16 réservés
  84.             if turtle.getItemCount(n) > 0 then
  85.                 turtle.select(n)
  86.                 if turtle.refuel(1) then
  87.                     turtle.select(1)
  88.                     return true
  89.                 end
  90.             end
  91.         end
  92.         turtle.select(1)
  93.         return false
  94.     end
  95.    
  96.     if not tryRefuel() then
  97.         print( "Add more fuel to continue." )
  98.         while not tryRefuel() do
  99.             sleep(1)
  100.         end
  101.         print( "Resuming Tunnel." )
  102.     end
  103. end
  104.  
  105. local function tryUp()
  106.     refuel()
  107.     while not posapi.tup() do
  108.         if turtle.detectUp() then
  109.             if not tryDigUp() then
  110.                 return false
  111.             end
  112.         elseif turtle.attackUp() then
  113.             collect()
  114.         else
  115.             sleep( 0.5 )
  116.         end
  117.     end
  118.     return true
  119. end
  120.  
  121. local function tryDown()
  122.     refuel()
  123.     while not posapi.tdown() do
  124.         if turtle.detectDown() then
  125.             if not tryDigDown() then
  126.                 return false
  127.             end
  128.         elseif turtle.attackDown() then
  129.             collect()
  130.         else
  131.             sleep( 0.5 )
  132.         end
  133.     end
  134.     return true
  135. end
  136.  
  137. local function tryForward()
  138.     refuel()
  139.     while not posapi.tforward() do
  140.         if turtle.detect() then
  141.             if not tryDig() then
  142.                 return false
  143.             end
  144.         elseif turtle.attack() then
  145.             collect()
  146.         else
  147.             sleep( 0.5 )
  148.         end
  149.     end
  150.     return true
  151. end
  152.  
  153. local function tunnel()
  154.     print( "Tunnelling..." )
  155.     -- Alternateur torches
  156.     local torchAlt = false
  157.     -- 4 = Position de la première torch
  158.     local torchPos = 4
  159.    
  160.     for n=1,length do
  161.         turtle.placeDown()
  162.         tryDigUp()
  163.         posapi.tleft()
  164.         tryDig()
  165.         tryUp()
  166.         tryDig()
  167.         -- Place une torche à gauche
  168.         if n == torchPos and torchAlt then
  169.             wallTorch()
  170.             torchPos = torchPos + 9
  171.             torchAlt = not torchAlt
  172.         end
  173.         posapi.tright()
  174.         posapi.tright()
  175.         tryDig()
  176.         -- Place une torche à droite
  177.         if n == torchPos and not torchAlt then
  178.             wallTorch()
  179.             torchPos = torchPos + 9
  180.             torchAlt = not torchAlt
  181.         end
  182.         tryDown()
  183.         tryDig()
  184.         posapi.tleft()
  185.        
  186.         if n<length then
  187.             tryDig()
  188.             if not tryForward() then
  189.                 print( "Aborting Tunnel." )
  190.                 break
  191.             end
  192.         else
  193.             print( "Tunnel complete." )
  194.         end
  195.  
  196.     end
  197.    
  198. --[[
  199. print( "Returning to start..." )
  200.  
  201. -- Return to where we started
  202. turtle.turnLeft()
  203. turtle.turnLeft()
  204. while depth > 0 do
  205.     if turtle.forward() then
  206.         depth = depth - 1
  207.     else
  208.         turtle.dig()
  209.     end
  210. end
  211. turtle.turnRight()
  212. turtle.turnRight()
  213. ]]
  214.     depth = 0
  215.     print( "Tunnel complete." )
  216.     print( "Mined "..collected.." items total." )
  217. end
  218.  
  219. -- -------------------------------
  220. -- Escalier, goto next tunnel (up)
  221. -- -------------------------------
  222. local function nextTunnel()
  223.     local alt = ( posapi.getDir() == startDir )
  224.    
  225.     tryForward()
  226.     turtle.placeDown()
  227.     tryUp()
  228.     tryDigUp()
  229.     if alt then
  230.         posapi.tleft()
  231.     else
  232.         posapi.tright()
  233.     end
  234.     tryForward()
  235.     turtle.placeDown()
  236.     tryUp()
  237.     tryDigUp()
  238.    
  239.     tryForward()
  240.     turtle.placeDown()
  241.     tryDigUp()
  242.    
  243.     tryForward()
  244.     turtle.placeDown()
  245.     tryDigUp()
  246.    
  247.     tryForward()
  248.     turtle.placeDown()
  249.     tryDigUp()
  250.    
  251.     if alt then
  252.         posapi.tleft()
  253.     else
  254.         posapi.tright()
  255.     end
  256. end
  257.  
  258. -- ------
  259. -- INIT
  260. -- ------
  261. if not posapi.x
  262. or not posapi.y
  263. or not posapi.y then
  264.     posapi.calibrate()
  265. end
  266.  
  267. while not length or length <= 0 do
  268.     print("")
  269.     write("Longueur du tunnel ? ")
  270.     local input = read()
  271.     length = tonumber(input)
  272. end
  273.  
  274. while not nbFloors or nbFloors <= 0 do
  275.     print("")
  276.     write("Nombre de paliers ? ")
  277.     local input = read()
  278.     nbFloors = tonumber(input)
  279. end
  280.  
  281. -- Goto Start pos
  282.  
  283. for n=1,nbFloors do
  284.     print("Start floor "..n)
  285.     tunnel()
  286.     if n < nbFloors then
  287.         nextTunnel()
  288.     end
  289. end
Advertisement
Add Comment
Please, Sign In to add comment