Advertisement
SomeoneNew666

Minecraft ComputerCraft Room Mining Program3

Feb 22nd, 2025 (edited)
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Constants
  2. local FORWARD = "F"
  3. local BACK = "B"
  4. local LEFT = "L"
  5. local RIGHT = "R"
  6. local UP = "U"
  7. local DOWN = "D"
  8.  
  9. -- Position tracking
  10. local currentX = 0
  11. local currentY = 0
  12. local currentZ = 0
  13. local facing = 0 -- 0=+Y, 1=+X, 2=-Y, 3=-X
  14.  
  15. -- Improved mining algorithm
  16. local function mine_room(L, W, H)
  17.     local full_path = {}
  18.     local base_pattern = "cross"
  19.    
  20.     -- Pattern selection logic
  21.     if W == 2 then
  22.         base_pattern = "narrow"
  23.     elseif L % 2 == 1 and W % 2 == 1 then
  24.         base_pattern = "spiral"
  25.     elseif math.abs(L - W) <= 2 then
  26.         base_pattern = "square_snake"
  27.     else
  28.         base_pattern = "rectangle_optim"
  29.     end
  30.  
  31.     for layer = 1, H do
  32.         local layer_path = {}
  33.         local start_x, start_y = currentX, currentY
  34.        
  35.         -- Layer mining patterns
  36.         if base_pattern == "spiral" then
  37.             local rings = math.floor(math.min(L, W) / 2)  -- Correct Lua syntax
  38.             for ring = 0, rings do
  39.                 -- Right wall
  40.                 for _ = 1, W - (ring*2) - 1 do
  41.                     table.insert(layer_path, FORWARD)
  42.                     currentX = currentX + 1
  43.                 end
  44.                 -- Bottom wall
  45.                 for _ = 1, L - (ring*2) - 2 do
  46.                     table.insert(layer_path, RIGHT)
  47.                     table.insert(layer_path, FORWARD)
  48.                     facing = (facing + 1) % 4
  49.                     currentY = currentY + 1
  50.                 end
  51.                 -- Left wall
  52.                 for _ = 1, W - (ring*2) - 1 do
  53.                     table.insert(layer_path, BACK)
  54.                     currentX = currentX - 1
  55.                 end
  56.                 -- Top wall
  57.                 for _ = 1, L - (ring*2) - 2 do
  58.                     table.insert(layer_path, LEFT)
  59.                     table.insert(layer_path, BACK)
  60.                     facing = (facing - 1) % 4
  61.                     currentY = currentY - 1
  62.                 end
  63.             end
  64.         elseif base_pattern == "narrow" then
  65.             for row = 1, L do
  66.                 table.insert(layer_path, FORWARD)
  67.                 currentX = currentX + 1
  68.                 if row < L then
  69.                     table.insert(layer_path, RIGHT)
  70.                     table.insert(layer_path, FORWARD)
  71.                     table.insert(layer_path, RIGHT)
  72.                     facing = (facing + 2) % 4
  73.                     currentY = currentY + 1
  74.                 end
  75.             end
  76.         else
  77.             local horizontal_first = W >= L
  78.             for pass = 1, 2 do
  79.                 local main_axis = horizontal_first and W or L
  80.                 local cross_axis = horizontal_first and L or W
  81.                
  82.                 for i = 1, cross_axis do
  83.                     for _ = 1, main_axis - 1 do
  84.                         table.insert(layer_path, horizontal_first and FORWARD or RIGHT)
  85.                         if horizontal_first then
  86.                             currentX = currentX + 1
  87.                         else
  88.                             facing = (facing + 1) % 4
  89.                             currentY = currentY + 1
  90.                         end
  91.                     end
  92.                    
  93.                     if i < cross_axis then
  94.                         table.insert(layer_path, horizontal_first and RIGHT or FORWARD)
  95.                         if horizontal_first then
  96.                             facing = (facing + 1) % 4
  97.                             currentY = currentY + 1
  98.                         else
  99.                             currentX = currentX + 1
  100.                         end
  101.                     end
  102.                 end
  103.                 horizontal_first = not horizontal_first
  104.             end
  105.         end
  106.  
  107.         -- Optimized layer return
  108.         local x_diff = currentX - start_x
  109.         local y_diff = currentY - start_y
  110.        
  111.         if x_diff ~= 0 then
  112.             table.insert(layer_path, x_diff > 0 and LEFT or RIGHT)
  113.             facing = (facing + (x_diff > 0 and -1 or 1)) % 4
  114.             for _ = 1, math.abs(x_diff) do
  115.                 table.insert(layer_path, BACK)
  116.                 currentX = currentX - (x_diff > 0 and 1 or -1)
  117.             end
  118.         end
  119.        
  120.         if y_diff ~= 0 then
  121.             table.insert(layer_path, y_diff > 0 and LEFT or RIGHT)
  122.             facing = (facing + (y_diff > 0 and -1 or 1)) % 4
  123.             for _ = 1, math.abs(y_diff) do
  124.                 table.insert(layer_path, BACK)
  125.                 currentY = currentY - (y_diff > 0 and 1 or -1)
  126.             end
  127.         end
  128.  
  129.         -- Vertical transition
  130.         if layer < H then
  131.             table.insert(layer_path, UP)
  132.             currentZ = currentZ + 1
  133.         end
  134.        
  135.         table.insert(full_path, table.concat(layer_path))
  136.     end
  137.  
  138.     -- Final return to base
  139.     for _ = 1, H-1 do
  140.         table.insert(full_path, DOWN)
  141.         currentZ = currentZ - 1
  142.     end
  143.    
  144.     return {table.concat(full_path, ""), 1}
  145. end
  146.  
  147. -- Updated direction handling
  148. local function direction_to_value(direction)
  149.     return {
  150.         [FORWARD] = 0,
  151.         [RIGHT] = 1,
  152.         [BACK] = 2,
  153.         [LEFT] = 3,
  154.         [UP] = 4,
  155.         [DOWN] = 5
  156.     }[direction] or 0
  157. end
  158.  
  159. -- Enhanced rotation logic
  160. local function rotate(current_facing, target_direction)
  161.     local diff = (target_direction - current_facing) % 4
  162.     if diff == 1 then
  163.         turtle.turnRight()
  164.     elseif diff == 2 then
  165.         turtle.turnRight()
  166.         turtle.turnRight()
  167.     elseif diff == 3 then
  168.         turtle.turnLeft()
  169.     end
  170. end
  171.  
  172. -- Improved path executor
  173. local function dig_by_path(path)
  174.     local current_facing = 0
  175.     for direction in path:gmatch(".") do
  176.         local target_value = direction_to_value(direction)
  177.        
  178.         if direction == UP then
  179.             turtle.up()
  180.             currentZ = currentZ + 1
  181.         elseif direction == DOWN then
  182.             turtle.down()
  183.             currentZ = currentZ - 1
  184.         else
  185.             if target_value ~= current_facing then
  186.                 rotate(current_facing, target_value)
  187.                 current_facing = target_value
  188.             end
  189.            
  190.             if turtle.detect() then
  191.                 turtle.dig()
  192.             end
  193.             turtle.forward()
  194.            
  195.             -- Update position based on facing
  196.             if current_facing == 0 then
  197.                 currentY = currentY + 1
  198.             elseif current_facing == 1 then
  199.                 currentX = currentX + 1
  200.             elseif current_facing == 2 then
  201.                 currentY = currentY - 1
  202.             else
  203.                 currentX = currentX - 1
  204.             end
  205.         end
  206.     end
  207. end
  208.  
  209. -- Unified dig function
  210. local function dig_room(L, W, H)
  211.     currentX, currentY, currentZ = 0, 0, 0  -- Reset position
  212.     local path_data = mine_room(L, W, H)
  213.     print("Optimized Path: "..path_data[1])
  214.     dig_by_path(path_data[1])
  215. end
  216.  
  217. -- Main program remains the same
  218. local args = {...}
  219. if #args < 3 then
  220.     print("Usage: digRoom <length> <width> <height>")
  221.     return
  222. end
  223.  
  224. local length = tonumber(args[1])
  225. local width = tonumber(args[2])
  226. local height = tonumber(args[3])
  227.  
  228. if not (length and width and height) or length < 1 or width < 1 or height < 1 then
  229.     print("Invalid dimensions!")
  230.     return
  231. end
  232.  
  233. dig_room(length, width, height)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement