Advertisement
Necrotico

CC Dome Printer

May 4th, 2023 (edited)
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1.  
  2. local argv = {...}
  3.  
  4. local radius = tonumber(argv[1])
  5. local height = tonumber(argv[2])
  6.  
  7. local function check_fuel()
  8.     if turtle.getFuelLevel() < 80 then
  9.         turtle.select(1)
  10.         turtle.refuel(1)
  11.     end
  12. end
  13.  
  14. function distance( x1, y1, x2, y2 )
  15.     return math.sqrt( (x2-x1)^2 + (y2-y1)^2 )
  16. end
  17. function round(x)
  18.     local x_m = x % 1.0
  19.     if x_m >= 0.5 then
  20.         return math.ceil(x)
  21.     else
  22.         return math.floor(x)
  23.     end
  24. end
  25.  
  26. local function select_dirt()
  27.     for s=2, 16 do
  28.         turtle.select(s)
  29.         local i_name = turtle.getItemDetail()
  30.         if i_name then
  31.             i_name = i_name.name
  32.             if (i_name == "minecraft:dirt") or (i_name == "minecraft:sand") or (i_name == "minecraft:gravel") then
  33.                 break
  34.             end
  35.         end
  36.     end
  37. end
  38. local function select_glass()
  39.     for s=2, 16 do
  40.         turtle.select(s)
  41.         local item_detail = turtle.getItemDetail()
  42.         if item_detail and (item_detail.name == "minecraft:glass") then
  43.             break
  44.         end
  45.     end
  46. end
  47.  
  48. local last_layer = false
  49.  
  50. local function GenerateLayer(l)
  51.     local final_radius = radius
  52.     if l > height then
  53.         final_radius = radius - (l - height)
  54.     end
  55.  
  56.     local rtn = {}
  57.     local string_version = ""
  58.     local fill_block_count = 0
  59.     for x=-radius, radius do
  60.         for z=-radius, radius do
  61.             local rtn_id = tostring(x) .. "," .. tostring(z)
  62.             local l_d = distance(0, 0, x, z)
  63.             if round(l_d) == final_radius then
  64.                 rtn[rtn_id] = 1
  65.                 string_version = string_version .. "#"
  66.             elseif l_d < final_radius then
  67.                 rtn[rtn_id] = 2
  68.                 string_version = string_version .. "O"
  69.                 fill_block_count = fill_block_count + 1
  70.             else
  71.                 rtn[rtn_id] = 0
  72.                 string_version = string_version .. " "
  73.             end
  74.         end
  75.         if x < radius then
  76.             string_version = string_version .. "\n"
  77.         end
  78.     end
  79.  
  80.     if fill_block_count == 0 then
  81.         last_layer = true
  82.     end
  83.    
  84.     print(string_version)
  85.     return rtn
  86. end
  87.  
  88. check_fuel()
  89. turtle.forward()
  90. while turtle.down() do
  91.     check_fuel()
  92. end
  93.  
  94. local function attempt_place_down()
  95.     while not turtle.placeDown() do
  96.         print("Something in the way!")
  97.     end
  98. end
  99.  
  100. local current_layer = 0
  101. local next_dome_layer = nil
  102.  
  103. -- place layers
  104.  
  105. while true do
  106.     if last_layer then
  107.         break
  108.     end
  109.     current_layer = current_layer + 1
  110.     next_dome_layer = GenerateLayer(current_layer)
  111.     if turtle.detectUp() then
  112.         turtle.digUp()
  113.     end
  114.     check_fuel()
  115.     turtle.up()
  116.     local work_area = (radius * 2) + 1
  117.     local turn_dir = true
  118.     for x=1, work_area do
  119.         for z=1, work_area do
  120.             local cell_id = tostring(x - radius - 1) .. "," .. tostring(z - radius - 1)
  121.             if turtle.detectDown() then
  122.                 turtle.digDown()
  123.             end
  124.             if turtle.detect() then
  125.                 turtle.dig()
  126.             end
  127.             local target_block = next_dome_layer[cell_id]
  128.             if target_block == 1 then
  129.                 select_glass()
  130.                 attempt_place_down()
  131.             elseif target_block == 2 then
  132.                 select_dirt()
  133.                 attempt_place_down()
  134.             end
  135.             check_fuel()
  136.             if z < work_area then
  137.                 turtle.forward()
  138.             end
  139.         end
  140.         if x < work_area then
  141.             if turn_dir then
  142.                 turtle.turnRight()
  143.             else
  144.                 turtle.turnLeft()
  145.             end
  146.             if turtle.detect() then
  147.                 turtle.dig()
  148.             end
  149.             check_fuel()
  150.             turtle.forward()
  151.             if turn_dir then
  152.                 turtle.turnRight()
  153.             else
  154.                 turtle.turnLeft()
  155.             end
  156.             turn_dir = not turn_dir
  157.         end
  158.     end
  159.     if turn_dir then
  160.         turtle.turnLeft()
  161.         for x=2, work_area do
  162.             check_fuel()
  163.             turtle.forward()
  164.         end
  165.         turtle.turnLeft()
  166.         for z=2, work_area do
  167.             check_fuel()
  168.             turtle.forward()
  169.         end
  170.         turtle.turnLeft()
  171.         turtle.turnLeft()
  172.     else
  173.         turtle.turnRight()
  174.         for x=2, work_area do
  175.             check_fuel()
  176.             turtle.forward()
  177.         end
  178.         turtle.turnRight()
  179.     end
  180. end
  181.  
  182. print("FINISHED PRINTING!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement