Hopfi

adv_turtle

May 16th, 2021 (edited)
212
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function act(x, y, z, model)
  2.     op = model[z][y][x]
  3.     if op == 0 then
  4.         -- do nothing
  5.     elseif op == 1 then
  6.         -- place dirt
  7.         turtle.placeDown()
  8.  
  9.         -- TODO: set slot of needed block, check if enough there, ..
  10.     elseif op == 2 then
  11.         -- place somethin else..
  12.     end
  13. end
  14.  
  15.  
  16. -- dir:
  17. -- 0 = +x
  18. -- 1 = +y
  19. -- 2 = -x
  20. -- 3 = -y
  21. function forward(dir, x, y, z)
  22.     while not turtle.forward() do
  23.         -- keep trying to go forward until possible..
  24.     end
  25.    
  26.     -- update coords according to current direction
  27.     if dir == 0 then
  28.         return (x+1),y,z
  29.     elseif dir == 1 then
  30.         return x, (y+1), z
  31.     elseif dir == 2 then
  32.         return (x-1),y,z
  33.     elseif dir == 3 then
  34.         return x,(y-z),z
  35.     end
  36. end
  37.  
  38. -- simulate going one step forwards
  39. function calcForward(dir, x, y, z)
  40.     -- update coords according to current direction
  41.     if dir == 0 then
  42.         return (x+1),y,z
  43.     elseif dir == 1 then
  44.         return x, (y+1), z
  45.     elseif dir == 2 then
  46.         return (x-1),y,z
  47.     elseif dir == 3 then
  48.         return x,(y-z),z
  49.     end
  50. end
  51.  
  52. function checkBoundaries(x,y,z,N,height)
  53.     x = x + 1
  54.     y = y + 1
  55.     z = z + 1
  56.     return ((x <= N) and (x >= 1) and (y <= N) and (y >= 1))
  57. end
  58.  
  59.  
  60. function left(dir)
  61.     turtle.turnLeft()
  62.     return ((dir+1) % 4)
  63. end
  64.  
  65. function right(dir)
  66.     turtle.turnRight()
  67.     return ((dir-1) % 4)
  68. end
  69.  
  70. function up(x, y, z)
  71.     while not turtle.up() do
  72.     end
  73.     return x,y,(z+1)
  74. end
  75.  
  76.  
  77. -- move to given coords and set given direction
  78. function moveTo(dir, x, y, z, dir_, x_, y_, z_)
  79.     delta_x = x_ - x
  80.     delta_y = y_ - y
  81.     delta_z = z_ - z
  82.  
  83.     for i=1, delta_z, 1 do
  84.         x,y,z = up(x,y,z)
  85.     end
  86.  
  87.     if delta_y >= 0 then
  88.         -- dir for y+ = 1
  89.         while dir ~= 1 do
  90.             dir = left(dir)
  91.         end
  92.  
  93.         for i=1, delta_y, 1 do
  94.             x,y,z = forward(dir,x,y,z)
  95.         end
  96.     else
  97.         -- dir for y- = 3
  98.         while dir ~= 3 do
  99.             dir = left(dir)
  100.         end
  101.  
  102.         for i=1, delta_y, 1 do
  103.             x,y,z = forward(dir,x,y,z)
  104.         end
  105.     end
  106.  
  107.     if delta_x >= 0 then
  108.         -- dir for x+ = 0
  109.         while dir ~= 0 do
  110.             dir = left(dir)
  111.         end
  112.  
  113.         for i=1, delta_x, 1 do
  114.             x,y,z = forward(dir,x,y,z)
  115.         end
  116.     else
  117.         -- dir for x- = 2
  118.         while dir ~= 2 do
  119.             dir = left(dir)
  120.         end
  121.  
  122.         for i=1, delta_x, 1 do
  123.             x,y,z = forward(dir,x,y,z)
  124.         end
  125.     end
  126.     -- fix direction
  127.     while dir ~= dir_ do
  128.         dir = left(dir)
  129.     end
  130.  
  131.     return dir,x,y,z
  132. end
  133.  
  134.  
  135.  
  136. function buildLayer(dir, x, y, z, N, height, layers)
  137.    
  138.     x_start = x
  139.     y_start = y
  140.     z_start = z
  141.     dir_start = dir
  142.  
  143.     blocks_to_place = N * N
  144.  
  145.     while blocks_to_place > 0 do
  146.         -- act on current coords
  147.         -- act(x, y, z, layers)
  148.         blocks_to_place = blocks_to_place - 1
  149.  
  150.         if blocks_to_place > 0 then
  151.             -- make next move
  152.             x1, y1, z1 = calcForward(x, y, z)
  153.             if checkBoundaries(x1, y1, z1, N, height) then
  154.                 x,y,z = forward(dir, x, y, z)
  155.             else
  156.                 possible_move = false
  157.                 -- turn left until move is possible..
  158.                 while not possible_move do
  159.                     dir = left(dir)
  160.                     x_next, y_next, z_next = calcForward(x, y, z)
  161.                     possible_move = checkBoundaries(x_next, y_next, z_next, N, height)
  162.                 end
  163.                 x,y,z = forward(dir, x, y, z)
  164.             end
  165.         end
  166.     end
  167.  
  168.     dir,x,y,z = moveTo(dir,x,y,z,dir_start, x_start, y_start, z_start)
  169.  
  170.     return dir,x,y,z
  171. end
  172.  
  173.  
  174. x_pos = 0
  175. y_pos = 0
  176. z_pos = 0
  177. dir = 0
  178.  
  179. -- 5 x 5 layer
  180. N = 5
  181. height = 4
  182.  
  183. layers = {}
  184.  
  185. for z=1,height,1 do
  186.     layers[z] = {}          
  187.     for y=1,N do
  188.         layers[z][y] = {}
  189.         for x=1,N do
  190.             layers[z][y][x] = 1 -- fill everything with dirt for now
  191.         end
  192.     end
  193. end
  194.  
  195.  
  196. turtle.refuel()
  197. dir, x_pos, y_pos, z_pos = buildLayer(dir, x_pos, y_pos, z_pos, height, layers)
  198. x_pos, y_pos, z_pos = up(x_pos, y_pos, z_pos)
  199. dir, x_pos, y_pos, z_pos = buildLayer(dir, x_pos, y_pos, z_pos, height, layers)
  200.  
  201.  
  202.  
  203.  
RAW Paste Data