Advertisement
Telmelt777

Quarry 2.0

Jul 17th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.43 KB | None | 0 0
  1. function orientation()
  2.  while turtle.detect() == true do
  3.   turtle.dig()
  4.  end
  5.  local vect1 = vector.new(gps.locate())
  6.  turtle.forward()
  7.  local vect2 = vector.new(gps.locate())
  8.  turtle.turnRight()
  9.  turtle.turnRight()
  10.  turtle.forward()
  11.  if vect2.x - vect1.x > 0 then
  12.   turtle.turnLeft()
  13.  elseif vect2.x - vect1.x < 0 then
  14.   turtle.turnRight()
  15.  else
  16.   if vect2.z - vect1.z < 0 then
  17.    sleep(0)
  18.   else
  19.    turtle.turnRight()
  20.    turtle.turnRight()
  21.   end
  22.  end
  23. end
  24.  
  25. function taille()
  26.  local taille = 64
  27.  if fs.exists("dim") == false then
  28.   h = fs.open("dim", "w")
  29.   print("Taille de la quarry: ")
  30.   taille = read()
  31.   h.write(taille)
  32.   h.close()
  33.  end
  34.  local h = fs.open("dim","r")
  35.  taille = tonumber(h.readAll())
  36.  h.close()
  37.  return taille
  38. end
  39.  
  40. function enregistrement()
  41.  rednet.open("right")
  42.  local vectRec = vector.new(gps.locate())
  43.  h = fs.open("pos", "w")
  44.  h.write(vectRec.x.."\n"..vectRec.y.."\n"..vectRec.z)
  45.  h.close()
  46.  os.reboot()
  47. end
  48.  
  49. function localisation()
  50.  rednet.open("right")
  51.  local vectLoc = vector.new(gps.locate())
  52.  local sens, x, y, z = 5, 5, 5, 5
  53.  if turtle.down() == true then
  54.   if turtle.digDown() == true then
  55.    sens = 1
  56.   else
  57.    sens = 0
  58.   end
  59.   turtle.up()
  60.  else
  61.   sens = 1
  62.  end
  63.  h = fs.open("pos", "r")
  64.  local x = vectLoc.x - tonumber(h.readLine())
  65.  local y = vectLoc.y - tonumber(h.readLine())
  66.  local z = vectLoc.z - tonumber(h.readLine())
  67.  h.close()
  68.  return x, y, z, sens
  69. end
  70.  
  71. function locDown()
  72.  rednet.open("right")
  73.  local vectLocDown = vector.new(gps.locate())
  74.  h = fs.open("pos", "r")
  75.  h.readLine()
  76.  local y = vectLocDown.y - tonumber(h.readLine())
  77.  h.close()
  78.  return y
  79. end
  80.  
  81. function restart(x, y, z)
  82.  local x, y, z, sens = 5, 5, 5, 5
  83.  if fs.exists("pos") == false then
  84.   enregistrement()
  85.  else
  86.   local h = fs.open("pos","r")
  87.   if h.readAll() ~= "" then
  88.    h.close()
  89.    x, y, z, sens = localisation()
  90.    return x, y, z, sens
  91.   else
  92.    h.close()
  93.    enregistrement()
  94.   end
  95.  end
  96. end
  97.  
  98. function boiteNoire(id)
  99.  local nbReboot, nbRecharge, nbRefuel, nbDecharge, nbPuits = 5, 5, 5, 5, 5
  100.  if fs.exists("BN") == false then
  101.   h = fs.open("BN", "w")
  102.   h.write("Nombre de redemmarrages:\n0\n\nNombre de rechargements:\n0\n\nNombre de pleins effectues:\n0\n\nNombre de dechargements:\n0\n\nNombre de puits creuses:\n0")
  103.   h.close()
  104.  end
  105.  h = fs.open("BN", "r")
  106.  h.readLine()
  107.  nbReboot = h.readLine()
  108.  h.readLine()
  109.  h.readLine()
  110.  nbRecharge = h.readLine()
  111.  h.readLine()
  112.  h.readLine()
  113.  nbRefuel = h.readLine()
  114.  h.readLine()
  115.  h.readLine()
  116.  nbDecharge = h.readLine()
  117.  h.readLine()
  118.  h.readLine()
  119.  nbPuits = h.readLine()
  120.  h.close()
  121.  if id == 1 then
  122.   nbReboot = nbReboot + 1
  123.  elseif id == 2 then
  124.   nbRecharge = nbRecharge + 1
  125.  elseif id == 3 then
  126.   nbRefuel = nbRefuel + 1
  127.  elseif id == 4 then
  128.   nbDecharge = nbDecharge + 1
  129.  elseif id == 5 then
  130.   nbPuits = nbPuits + 1
  131.  else
  132.   print("Erreur ID!!!")
  133.  end
  134.  h = fs.open("BN", "w")
  135.  h.write("Nombre de redemmarrages:\n"..nbReboot.."\n\nNombre de rechargements:\n"..nbRecharge.."\n\nNombre de pleins effectues:\n"..nbRefuel.."\n\nNombre de dechargements:\n"..nbDecharge.."\n\nNombre de puits creuses:\n"..nbPuits)
  136.  h.close()
  137. end
  138.  
  139. function dechargement()
  140.  local slot = 1
  141.  turtle.select(16)
  142.  turtle.digUp()
  143.  turtle.placeUp()
  144.  while slot <= 13 do
  145.   turtle.select(slot)
  146.   if turtle.dropUp() == true then
  147.    sleep(0)
  148.    slot = slot + 1
  149.   else
  150.    sleep(10)
  151.   end
  152.  end
  153.  turtle.select(16)
  154.  turtle.digUp()
  155.  turtle.select(1)
  156.  boiteNoire(4)
  157.  return true
  158. end
  159.  
  160. function recharge()
  161.  turtle.digUp()
  162.  turtle.select(15)
  163.  turtle.placeUp()
  164.  turtle.select(14)
  165.  if turtle.suckUp() == false then
  166.   print("Manque de combustible!!!")
  167.   return false
  168.  else
  169.   sleep(0)
  170.  end
  171.  turtle.select(15)
  172.  turtle.digUp()
  173.  turtle.select(1)
  174.  boiteNoire(2)
  175.  return true
  176. end
  177.  
  178. function checkSlot()
  179.  if turtle.getItemCount(13) > 0 then
  180.   if dechargement() == true then
  181.    print("Dechargement effectue")
  182.    return true
  183.   else
  184.    return false
  185.   end
  186.  else
  187.   return true
  188.  end
  189. end
  190.  
  191. function checkFuel()
  192.  if turtle.getFuelLevel() == 0 then
  193.   turtle.select(14)
  194.   turtle.refuel(1)
  195.   boiteNoire(3)
  196.   if turtle.getItemCount(14) == 0 then
  197.    if recharge() == false then
  198.     return false
  199.    else
  200.     turtle.select(1)
  201.     return true
  202.    end
  203.   else
  204.    turtle.select(1)
  205.    return true
  206.   end
  207.   turtle.select(1)
  208.  else
  209.   return true
  210.  end
  211. end
  212.  
  213. function remonte()
  214.  local y = locDown()
  215.  while y < 0 do
  216.   if turtle.up() == true then
  217.    checkFuel()
  218.    y = y + 1
  219.   else
  220.    sleep(5)
  221.   end
  222.  end
  223. end
  224.  
  225. function dig()
  226.  turtle.select(1)
  227.  turtle.digDown()
  228.  while turtle.down() == true do
  229.   turtle.select(1)
  230.   while turtle.detect() == true do
  231.    turtle.dig()
  232.   end
  233.   turtle.turnRight()
  234.   turtle.turnRight()
  235.   while turtle.detect() == true do
  236.    turtle.dig()
  237.   end
  238.   turtle.turnRight()
  239.   turtle.turnRight()
  240.   turtle.digDown()
  241.   checkSlot()
  242.   checkFuel()
  243.  end
  244.  remonte()
  245. end
  246.  
  247. function checkPos()
  248.  local y = locDown()
  249.  if y > 0 then
  250.   print("ERREUR POSITION!!!")
  251.   return false
  252.  elseif y < 0 then
  253.   while y ~= 0 do
  254.    remonte()
  255.    y = locDown()
  256.    if y > 0 then
  257.     print("ERREUR POSITION!!!")
  258.     return false
  259.    elseif y < 0 then
  260.     sleep(0)
  261.    else
  262.     return true
  263.    end
  264.   end
  265.  else
  266.   return true
  267.  end
  268. end
  269.  
  270. function check()
  271.  if checkSlot() == true and checkFuel() == true and checkPos() == true then
  272.   return true
  273.  else
  274.   turtle.shutdown()
  275.  end
  276. end
  277.  
  278. function retour(t)
  279.  turtle.turnRight()
  280.  turtle.turnRight()
  281.  while t > 0 do
  282.   turtle.forward()
  283.   t = t - 1
  284.   checkFuel()
  285.  end
  286.  turtle.turnRight()
  287.  while turtle.detect() == true do
  288.   turtle.dig()
  289.   check()
  290.  end
  291.  turtle.forward()
  292.  turtle.turnRight()
  293.  check()
  294.  return t
  295. end
  296.  
  297. function mouvement(x, z, taille)
  298.  local i = 3
  299.  if z + 3 > taille then
  300.   z = retour(z)
  301.   x = x + 1
  302.  else
  303.   while i > 0 do
  304.    turtle.select(1)
  305.    while turtle.detect() == true do
  306.     turtle.dig()
  307.     check()
  308.    end
  309.    turtle.forward()
  310.    check()
  311.    i = i - 1
  312.   end
  313.   z = z + 3
  314.  end
  315.  return x, z
  316. end
  317.  
  318. function repos(x, y, z, sens, taille)
  319.  orientation()
  320.  if y < 0 then
  321.   if sens == 1 then
  322.    while turtle.detect() == true do
  323.     turtle.dig()
  324.    end
  325.    turtle.turnRight()
  326.    turtle.turnRight()
  327.    while turtle.detect() == true do
  328.     turtle.dig()
  329.    end
  330.    turtle.turnRight()
  331.    turtle.turnRight()
  332.    dig()
  333.    x, z = mouvement(x, z, taille)
  334.   else
  335.    remonte(z)
  336.    x, z = mouvement(x, z, taille)
  337.   end
  338.  elseif y > 0 then
  339.   print("ERREUR POSITION!!!")
  340.   turtle.shutdown()
  341.  else
  342.   if sens == 1 then
  343.    dig()
  344.    x, z = mouvement(x, z, taille)
  345.   else
  346.    remonte()
  347.    x, z = mouvement(x, z, taille)
  348.   end
  349.  end
  350.  return x, y, z
  351. end
  352.  
  353. function fin(x, z)
  354.  turtle.turnRight()
  355.  while x > 0 do
  356.   turtle.forward()
  357.   x = x - 1
  358.  end
  359.  turtle.turnRight()
  360.  while z > 0 do
  361.   turtle.forward()
  362.   z = z - 1
  363.  end
  364. end
  365.  
  366. function fonctionnement(x, y, z, sens, taille)
  367.  while x <= taille and z <= taille do
  368.   if check() == true then
  369.    y = 0
  370.    boiteNoire(5)
  371.    dig()
  372.    x, z = mouvement(x, z, taille)
  373.   end
  374.  end
  375.  fin(x, z)
  376. end
  377.  
  378.  
  379. boiteNoire(1)
  380. local taille = taille()
  381. checkFuel()
  382. turtle.select(1)
  383. write("\n\nCalcul de la position en cours...")
  384. local x, y, z, sens = restart(x, y, z)
  385. write(" Position obtenue.\n\nRepositionnement pour fonctionnement normal...")
  386. x, y, z = repos(x, y, z, sens, taille)
  387. write("Quarry repositionnee.\n\nDebut du fonctionnement normal de la quarry...")
  388. fonctionnement(x, y, z, sens, taille)
  389. print("Fin normale du fonctionnement...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement