Matt0st

digging

Feb 25th, 2023 (edited)
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.37 KB | Gaming | 0 0
  1. currentPos = {x = 0, y = 0, z = 0} -- position actuel
  2. currentRot = 0; -- rotation actuel
  3.  
  4. function line(width)
  5.   local fuelLevel = turtle.getFuelLevel()
  6.  
  7.   if fuelLevel == "unlimited" or fuelLevel >= 100 then
  8.       for i=2,width do
  9.             turtle.dig()
  10.             forward()
  11.       end
  12.   else
  13.       local success, er = turtle.refuel()
  14.      
  15.        if not success then
  16.            print("Error: could not refuel")
  17.            return
  18.        else
  19.            line(width)
  20.        end
  21.                        
  22.     end
  23. end
  24.  
  25. function square(size)
  26.   if turtle.getFuelLevel() < size * 2 then
  27.       turtle.refuel()
  28.   end
  29.  
  30.   for i=1,size do
  31.     line(size)
  32.     if i < size then
  33.         if i % 2 == 1 then
  34.               rotation("right")
  35.               turtle.dig()
  36.               forward()
  37.               rotation("right")
  38.               else
  39.               rotation("left")
  40.               turtle.dig()
  41.               forward()
  42.               rotation("left")
  43.         end
  44.     end
  45.   end
  46.  
  47. end
  48.  
  49. function cube(size, direction)
  50.    
  51.     for i=1,size do
  52.         square(size)
  53.         gotoPoint({x = 0, y = currentPos.y, z = 0})
  54.         gotoRotation(0)
  55.         if direction == "up" then
  56.           turtle.digUp()
  57.           fly("up")
  58.         else
  59.           turtle.digDown()
  60.           fly("down")
  61.         end
  62.     end
  63.    
  64. end
  65.  
  66. function forward()
  67.   if currentRot == 0 then
  68.     currentPos.x = currentPos.x + 1
  69.   elseif currentRot == 90 then
  70.     currentPos.z = currentPos.z + 1
  71.   elseif currentRot == 180  then
  72.     currentPos.x = currentPos.x - 1
  73.   elseif currentRot == 270 then
  74.     currentPos.z = currentPos.z - 1
  75.   else
  76.     print("Error rotation is not valid : " .. currentRot .. " x:" .. currentPos.x .. " y:" .. currentPos.y .. " z:" .. currentPos.z)
  77.     return
  78.   end
  79.  
  80.   turtle.forward()
  81.  
  82. end
  83.  
  84. function fly(direction)
  85.  
  86.   if direction == "up" then
  87.     currentPos.y = currentPos.y + 1
  88.     turtle.up()
  89.   elseif direction == "down" then
  90.     currentPos.y = currentPos.y - 1
  91.     turtle.down()
  92.   else
  93.     print("Error fly to " .. direction .. " y:" .. currentPos.y)
  94.     return
  95.   end
  96.  
  97. end
  98.  
  99. function rotation(direction)
  100.  
  101.   if (direction == "left") then
  102.     currentRot = currentRot + 90
  103.     if currentRot > 270 then
  104.       currentRot = 0
  105.     end
  106.     turtle.turnLeft()
  107.   elseif (direction == "right") then
  108.     currentRot = currentRot - 90
  109.     if currentRot < 0 then
  110.       currentRot = 270
  111.     end
  112.     turtle.turnRight()
  113.   else
  114.     print("Error rotate to " .. direction .. " rotation:" .. currentRot)
  115.     return
  116.   end
  117.  
  118. end
  119.  
  120. function gotoRotation(angle)
  121.  
  122. local time = 0
  123.  
  124.   while currentRot ~= angle and time < 3000 do
  125.     print("rotation:" .. currentRot .. " time:" .. time)
  126.     rotation("left")
  127.     time = time + 1
  128.   end
  129.  
  130. end
  131.  
  132. function gotoPoint(point)
  133.  
  134.   local time = 0
  135.  
  136.   while currentPos.y ~= point.y and time < 3000 do
  137.     print("positionY:" .. currentPos.y .. " time:" .. time)
  138.     if currentPos.y > point.y then
  139.       fly("down")
  140.     elseif currentPos.y < point.y then
  141.       fly("up")
  142.     end
  143.   end
  144.  
  145.   time = 0
  146.  
  147.   while currentPos.x ~= point.x and time < 3000 do
  148.         print("positionX:" .. currentPos.x .. " time:" .. time)
  149.     if currentPos.x > point.x then
  150.      
  151.       gotoRotation(180)
  152.  
  153.       forward()
  154.  
  155.     elseif currentPos.x < point.x then
  156.  
  157.       gotoRotation(0)
  158.  
  159.       forward()
  160.  
  161.     end
  162.   end
  163.  
  164.   time = 0
  165.  
  166.     while currentPos.z ~= point.z and time < 3000 do
  167.           print("positionZ:" .. currentPos.z .. " time:" .. time)
  168.     if currentPos.z > point.z then
  169.      
  170.       gotoRotation(270)
  171.  
  172.       forward()
  173.  
  174.     elseif currentPos.z < point.z then
  175.            
  176.       gotoRotation(90)
  177.  
  178.       forward()
  179.  
  180.     end
  181.   end
  182. end
  183.  
  184. -- Vérifier si un argument est fourni
  185. if #arg < 1 then
  186.   print("Usage:")
  187.     print("  turtle <command> [arguments]")
  188.     return
  189. end
  190.  
  191. local command = arg[1]
  192.  
  193. -- Vérifier si le premier argument est valide
  194. if command ~= "line" and command ~= "square" and command ~= "cube" then
  195.   print("Invalid command.")
  196.   return
  197. end
  198.  
  199. -- Vérifier si les arguments nécessaires sont fournis pour la commande 'line'
  200. if command == "line" and (#arg < 2 or not tonumber(arg[2])) then
  201.   print("Usage:")
  202.     print("  turtle line <length>")
  203.     return
  204. end
  205.  
  206.  -- Vérifier si les arguments nécessaires sont fournis pour la commande 'square' ou 'cube'
  207.  if (command == "square" or command == "cube") and (#arg < 2 or not tonumber(arg[2])) then
  208.    print("Usage:")
  209.      print("  turtle square <size>")
  210.      print("  turtle cube <size>")
  211.      return
  212.  end
  213.  
  214.  -- Vérifier si un argument facultatif est fourni pour la commande 'cube'
  215.  if command == "cube" and #arg >= 3 and arg[3] ~= "up" and arg[3] ~= "down" then
  216.    print("Usage:")
  217.    print("  turtle cube <size> [direction]")
  218.    print("Direction can be 'up' or 'down'.")
  219.    return
  220.  end
  221.  
  222. -- Exécuter la commande appropriée avec les arguments fournis
  223. if command == "line" then -- LINE
  224.    local length = tonumber(arg[2])
  225.    line(length)
  226.    gotoPoint({x = 0, y = 0, z = 0})
  227.    gotoRotation(0)
  228.  
  229. elseif command == "square" then -- SQUARE
  230.   local size = tonumber(arg[2])
  231.   square(size)
  232.   gotoPoint({x = 0, y = 0, z = 0})
  233.   gotoRotation(0)
  234.  
  235. elseif command == "cube" then -- CUBE
  236.   local size = tonumber(arg[2])
  237.   local direction = arg[3] or "down"
  238.   cube(size, direction)
  239.   gotoPoint({x = 0, y = 0, z = 0})
  240.   gotoRotation(0)
  241.  
  242. end        
Advertisement
Add Comment
Please, Sign In to add comment