Advertisement
PetrAxeman

cc: tweaked - petr_axeman - dturtle_extension

Apr 19th, 2024 (edited)
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. local dturtle = {}
  2.  
  3. function dturtle.gets_lengths (height, width, reversed)
  4.     reversed = reversed or false
  5.     h = height
  6.     w = width - 1
  7.     lengths = {}
  8.  
  9.     while true do
  10.         if h == 0 then
  11.             return lengths
  12.         end
  13.        
  14.         if h == height and reversed ~= true then
  15.             lengths[#lengths + 1] = h - 1
  16.         else
  17.             lengths[#lengths + 1] = h
  18.         end
  19.         h = h - 1
  20.  
  21.         if w == 0 then
  22.             return lengths
  23.         end
  24.  
  25.         lengths[#lengths + 1] = w
  26.         w = w - 1
  27.     end
  28. end
  29.  
  30.  
  31. function dturtle.gets_lengths_reversed (height, width)
  32.     lengths = dturtle.gets_lengths(height, width, true)
  33.     reversed_lengths = {}
  34.     counter = 1
  35.     for i = #lengths,1,-1 do
  36.         if i ~= 1 then
  37.             reversed_lengths[counter] = lengths[i]
  38.         else
  39.             reversed_lengths[counter] = lengths[i] - 1
  40.         end
  41.         counter = counter + 1
  42.     end
  43.     return reversed_lengths
  44. end
  45.  
  46.  
  47. function dturtle.gets_face_direction (h, w)
  48.     if h >= w then
  49.         if w % 2 ~= 0 then
  50.             return 1
  51.         else
  52.             return 3
  53.         end
  54.     else
  55.         if h % 2 ~= 0 then
  56.             return 2
  57.         else
  58.             return 4
  59.         end
  60.     end
  61. end
  62.  
  63.  
  64. function dturtle.gets_position (h, w)
  65.     if w > h then
  66.         w = h
  67.     end
  68.    
  69.     if w % 2 == 0 then
  70.         return math.floor(w / 2), math.floor((w / 2) - 1)
  71.     else
  72.         return math.floor((w - 1) / 2), h - math.floor((w - 1) / 2)
  73.     end
  74. end
  75.  
  76.  
  77. function dturtle.dig_spiral (turn, lengths)
  78.     for l = 1, #lengths do
  79.         for p = 1, lengths[l] do
  80.             turtle.dig()
  81.             turtle.forward()
  82.         end
  83.         turn()
  84.     end
  85. end
  86.  
  87.  
  88. function dturtle.exit_from_center (height, width)
  89.     x_pos, y_pos = dturtle.gets_position(height, width)
  90.     direction = dturtle.gets_face_direction(height, width)
  91.     if direction == 2 then
  92.         turtle.turnLeft()
  93.     elseif direction == 3 then
  94.         turtle.turnLeft()
  95.         turtle.turnLeft()
  96.     elseif direction == 4 then
  97.         turtle.turnRight()
  98.     end
  99.    
  100.     for i = 1, y_pos do
  101.         turtle.back()
  102.     end
  103.     turtle.turnLeft()
  104.     for i = 1, x_pos do
  105.         turtle.forward()
  106.     end
  107.     turtle.turnRight()
  108. end
  109.  
  110.  
  111. function dturtle.dig_area (height, width, depth)
  112.     local state = 1
  113.     turtle.dig()
  114.     turtle.forward()
  115.     for i = 1, depth do
  116.         if i % 2 == 0 then
  117.             lengths = dturtle.gets_lengths_reversed(height, width)
  118.             turtle.turnRight()
  119.             turtle.turnRight()
  120.             dturtle.dig_spiral(turtle.turnLeft, lengths)
  121.             turtle.turnRight()
  122.         else
  123.             lengths = dturtle.gets_lengths(height, width)
  124.             if i ~= 1 then
  125.                 turtle.turnRight()
  126.                 turtle.turnRight()
  127.             end
  128.             dturtle.dig_spiral(turtle.turnRight, lengths)
  129.             turtle.turnLeft()
  130.         end
  131.         if i ~= depth then
  132.             turtle.digDown()
  133.             turtle.down()
  134.         end
  135.     end
  136.     if depth % 2 ~= 0 then
  137.         dturtle.exit_from_center(height, width, depth)
  138.     end
  139.     if depth ~= 1 then
  140.         for i = 1, depth - 1 do
  141.             turtle.up()
  142.         end
  143.     end
  144.     direction = dturtle.gets_face_direction(height, width)
  145.     if direction == 3 then
  146.         turtle.turnRight()
  147.         turtle.turnRight()
  148.     end
  149.     turtle.back()
  150. end
  151.  
  152. return dturtle
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement