SHOW:
|
|
- or go back to the newest paste.
1 | -- Constants | |
2 | local FORWARD = "F" | |
3 | local BACK = "B" | |
4 | local LEFT = "L" | |
5 | - | local facing = 0 -- 0=forward(away from chest), 1=right, 2=back(toward chest), 3=left |
5 | + | local RIGHT = "R" |
6 | - | local startRow = true -- Track if we're starting from the front or back of the row |
6 | + | local UP = "U" |
7 | - | local miningLength = 0 -- Store length as a module-level variable |
7 | + | local DOWN = "D" |
8 | - | local forward = true -- Moved to module-level to preserve state between layers |
8 | + | |
9 | - | local resumePos = {x=0, y=0, z=0, facing=0} |
9 | + | |
10 | local currentX = 0 | |
11 | - | -- Add this at the top with other variables |
11 | + | |
12 | - | local ESSENTIAL_ITEMS = { |
12 | + | |
13 | - | ["minecraft:torch"] = 64, |
13 | + | local facing = 0 -- 0=+Y, 1=+X, 2=-Y, 3=-X |
14 | - | ["minecraft:cobblestone"] = 128, |
14 | + | |
15 | - | ["minecraft:dirt"] = 128, |
15 | + | -- Improved mining algorithm |
16 | - | ["minecraft:cobbled_deepslate"] = 128 |
16 | + | local function mine_room(L, W, H) |
17 | - | } |
17 | + | local full_path = {} |
18 | local base_pattern = "cross" | |
19 | - | local function setMiningLength(len) |
19 | + | |
20 | - | miningLength = len |
20 | + | -- Pattern selection logic |
21 | if W == 2 then | |
22 | base_pattern = "narrow" | |
23 | - | -- Utility functions |
23 | + | elseif L % 2 == 1 and W % 2 == 1 then |
24 | - | local function findItem(itemName) |
24 | + | base_pattern = "spiral" |
25 | - | for slot = 1, 16 do |
25 | + | elseif math.abs(L - W) <= 2 then |
26 | - | local item = turtle.getItemDetail(slot) |
26 | + | base_pattern = "square_snake" |
27 | - | if item and item.name == itemName then |
27 | + | |
28 | - | turtle.select(slot) |
28 | + | base_pattern = "rectangle_optim" |
29 | - | return true |
29 | + | |
30 | ||
31 | - | end |
31 | + | for layer = 1, H do |
32 | - | return false |
32 | + | local layer_path = {} |
33 | local start_x, start_y = currentX, currentY | |
34 | ||
35 | - | local function hasInventorySpace() |
35 | + | -- Layer mining patterns |
36 | - | for slot = 1, 16 do |
36 | + | if base_pattern == "spiral" then |
37 | - | if turtle.getItemCount(slot) == 0 then |
37 | + | local rings = math.floor(math.min(L, W) / 2) -- Correct Lua syntax |
38 | - | return true |
38 | + | for ring = 0, rings do |
39 | -- Right wall | |
40 | - | end |
40 | + | for _ = 1, W - (ring*2) - 1 do |
41 | - | return false |
41 | + | table.insert(layer_path, FORWARD) |
42 | currentX = currentX + 1 | |
43 | end | |
44 | - | local function handleLiquid(direction) |
44 | + | -- Bottom wall |
45 | - | if findItem("minecraft:cobblestone") or findItem("minecraft:dirt") or findItem("minecraft:cobbled_deepslate") then |
45 | + | for _ = 1, L - (ring*2) - 2 do |
46 | - | if direction == "up" then |
46 | + | table.insert(layer_path, RIGHT) |
47 | - | turtle.placeUp() |
47 | + | table.insert(layer_path, FORWARD) |
48 | - | elseif direction == "down" then |
48 | + | facing = (facing + 1) % 4 |
49 | - | turtle.placeDown() |
49 | + | currentY = currentY + 1 |
50 | end | |
51 | - | turtle.place() |
51 | + | -- Left wall |
52 | for _ = 1, W - (ring*2) - 1 do | |
53 | - | sleep(0.5) |
53 | + | table.insert(layer_path, BACK) |
54 | - | return true |
54 | + | currentX = currentX - 1 |
55 | - | end |
55 | + | |
56 | - | return false |
56 | + | -- Top wall |
57 | for _ = 1, L - (ring*2) - 2 do | |
58 | table.insert(layer_path, LEFT) | |
59 | - | local function turnToFacing(targetFacing) |
59 | + | table.insert(layer_path, BACK) |
60 | - | while facing ~= targetFacing do |
60 | + | facing = (facing - 1) % 4 |
61 | - | turtle.turnRight() |
61 | + | currentY = currentY - 1 |
62 | - | facing = (facing + 1) % 4 |
62 | + | |
63 | - | end |
63 | + | |
64 | elseif base_pattern == "narrow" then | |
65 | for row = 1, L do | |
66 | - | -- Optimized turning function that takes shortest path |
66 | + | table.insert(layer_path, FORWARD) |
67 | - | local function turnToFacingOptimal(targetFacing) |
67 | + | currentX = currentX + 1 |
68 | - | local currentFacing = facing |
68 | + | if row < L then |
69 | - | local rightTurns = (targetFacing - currentFacing) % 4 |
69 | + | table.insert(layer_path, RIGHT) |
70 | - | local leftTurns = (currentFacing - targetFacing) % 4 |
70 | + | table.insert(layer_path, FORWARD) |
71 | - | |
71 | + | table.insert(layer_path, RIGHT) |
72 | - | if rightTurns <= leftTurns then |
72 | + | facing = (facing + 2) % 4 |
73 | - | -- Turn right is shorter or equal |
73 | + | currentY = currentY + 1 |
74 | - | for i = 1, rightTurns do |
74 | + | |
75 | - | turtle.turnRight() |
75 | + | |
76 | - | facing = (facing + 1) % 4 |
76 | + | |
77 | - | end |
77 | + | local horizontal_first = W >= L |
78 | - | else |
78 | + | for pass = 1, 2 do |
79 | - | -- Turn left is shorter |
79 | + | local main_axis = horizontal_first and W or L |
80 | - | for i = 1, leftTurns do |
80 | + | local cross_axis = horizontal_first and L or W |
81 | - | turtle.turnLeft() |
81 | + | |
82 | - | facing = (facing - 1) % 4 |
82 | + | for i = 1, cross_axis do |
83 | - | end |
83 | + | for _ = 1, main_axis - 1 do |
84 | - | end |
84 | + | table.insert(layer_path, horizontal_first and FORWARD or RIGHT) |
85 | if horizontal_first then | |
86 | currentX = currentX + 1 | |
87 | - | local function tryMove(moveFunc, digFunc, direction) |
87 | + | else |
88 | - | -- First check if there's a liquid |
88 | + | facing = (facing + 1) % 4 |
89 | - | local detectFunc |
89 | + | currentY = currentY + 1 |
90 | - | if direction == "up" then |
90 | + | end |
91 | - | detectFunc = turtle.detectUp |
91 | + | |
92 | - | elseif direction == "down" then |
92 | + | |
93 | - | detectFunc = turtle.detectDown |
93 | + | if i < cross_axis then |
94 | table.insert(layer_path, horizontal_first and RIGHT or FORWARD) | |
95 | - | detectFunc = turtle.detect |
95 | + | if horizontal_first then |
96 | facing = (facing + 1) % 4 | |
97 | currentY = currentY + 1 | |
98 | - | -- Maximum attempts to handle falling blocks |
98 | + | else |
99 | - | local maxAttempts = 10 |
99 | + | currentX = currentX + 1 |
100 | - | local attempts = 0 |
100 | + | end |
101 | end | |
102 | - | while attempts < maxAttempts do |
102 | + | |
103 | - | attempts = attempts + 1 |
103 | + | horizontal_first = not horizontal_first |
104 | end | |
105 | - | if detectFunc() then |
105 | + | |
106 | - | -- Handle liquid first |
106 | + | |
107 | - | if handleLiquid(direction) then |
107 | + | -- Optimized layer return |
108 | - | digFunc() |
108 | + | local x_diff = currentX - start_x |
109 | - | if moveFunc() then |
109 | + | local y_diff = currentY - start_y |
110 | - | break |
110 | + | |
111 | if x_diff ~= 0 then | |
112 | table.insert(layer_path, x_diff > 0 and LEFT or RIGHT) | |
113 | - | digFunc() |
113 | + | facing = (facing + (x_diff > 0 and -1 or 1)) % 4 |
114 | - | if moveFunc() then |
114 | + | for _ = 1, math.abs(x_diff) do |
115 | - | break |
115 | + | table.insert(layer_path, BACK) |
116 | currentX = currentX - (x_diff > 0 and 1 or -1) | |
117 | end | |
118 | end | |
119 | - | if moveFunc() then |
119 | + | |
120 | - | break |
120 | + | if y_diff ~= 0 then |
121 | table.insert(layer_path, y_diff > 0 and LEFT or RIGHT) | |
122 | - | -- If movement failed but no block detected, try handling liquid |
122 | + | facing = (facing + (y_diff > 0 and -1 or 1)) % 4 |
123 | - | if handleLiquid(direction) then |
123 | + | for _ = 1, math.abs(y_diff) do |
124 | - | if moveFunc() then |
124 | + | table.insert(layer_path, BACK) |
125 | - | break |
125 | + | currentY = currentY - (y_diff > 0 and 1 or -1) |
126 | end | |
127 | end | |
128 | ||
129 | -- Vertical transition | |
130 | if layer < H then | |
131 | - | -- If we're still here after an attempt, brief pause before next try |
131 | + | table.insert(layer_path, UP) |
132 | - | -- This helps with falling blocks settling |
132 | + | |
133 | - | sleep(0.2) |
133 | + | |
134 | ||
135 | - | -- If we've tried several times and still can't move, it might be an infinite fall |
135 | + | table.insert(full_path, table.concat(layer_path)) |
136 | - | if attempts == maxAttempts then |
136 | + | |
137 | - | print("Warning: Possible falling blocks causing obstruction") |
137 | + | |
138 | - | return false |
138 | + | -- Final return to base |
139 | for _ = 1, H-1 do | |
140 | table.insert(full_path, DOWN) | |
141 | currentZ = currentZ - 1 | |
142 | - | -- Update position after successful move |
142 | + | |
143 | - | if attempts < maxAttempts then -- Only update if we actually moved |
143 | + | |
144 | - | if moveFunc == turtle.forward then |
144 | + | return {table.concat(full_path, ""), 1} |
145 | - | if facing == 0 then currentY = currentY - 1 |
145 | + | |
146 | - | elseif facing == 1 then currentX = currentX + 1 |
146 | + | |
147 | - | elseif facing == 2 then currentY = currentY + 1 |
147 | + | -- Updated direction handling |
148 | - | else currentX = currentX - 1 end |
148 | + | local function direction_to_value(direction) |
149 | - | elseif moveFunc == turtle.up then |
149 | + | return { |
150 | [FORWARD] = 0, | |
151 | - | elseif moveFunc == turtle.down then |
151 | + | [RIGHT] = 1, |
152 | [BACK] = 2, | |
153 | [LEFT] = 3, | |
154 | - | return true |
154 | + | [UP] = 4, |
155 | [DOWN] = 5 | |
156 | }[direction] or 0 | |
157 | - | return false |
157 | + | |
158 | ||
159 | -- Enhanced rotation logic | |
160 | - | -- Optimized turning function that takes shortest path |
160 | + | local function rotate(current_facing, target_direction) |
161 | - | local function turnToFacingOptimal(targetFacing) |
161 | + | local diff = (target_direction - current_facing) % 4 |
162 | - | local currentFacing = facing |
162 | + | if diff == 1 then |
163 | - | local rightTurns = (targetFacing - currentFacing) % 4 |
163 | + | |
164 | - | local leftTurns = (currentFacing - targetFacing) % 4 |
164 | + | elseif diff == 2 then |
165 | - | |
165 | + | |
166 | - | if rightTurns <= leftTurns then |
166 | + | |
167 | - | -- Turn right is shorter or equal |
167 | + | elseif diff == 3 then |
168 | - | for i = 1, rightTurns do |
168 | + | turtle.turnLeft() |
169 | - | turtle.turnRight() |
169 | + | |
170 | - | facing = (facing + 1) % 4 |
170 | + | |
171 | - | end |
171 | + | |
172 | - | else |
172 | + | -- Improved path executor |
173 | - | -- Turn left is shorter |
173 | + | local function dig_by_path(path) |
174 | - | for i = 1, leftTurns do |
174 | + | local current_facing = 0 |
175 | - | turtle.turnLeft() |
175 | + | for direction in path:gmatch(".") do |
176 | - | facing = (facing - 1) % 4 |
176 | + | local target_value = direction_to_value(direction) |
177 | - | end |
177 | + | |
178 | - | end |
178 | + | if direction == UP then |
179 | turtle.up() | |
180 | currentZ = currentZ + 1 | |
181 | - | local function returnToChest() |
181 | + | elseif direction == DOWN then |
182 | - | local returnFacing = facing |
182 | + | turtle.down() |
183 | - | local pathFound = false |
183 | + | |
184 | - | |
184 | + | |
185 | - | -- Try multiple vertical levels to find an open path |
185 | + | if target_value ~= current_facing then |
186 | - | local function tryPathAtLevel(targetZ) |
186 | + | rotate(current_facing, target_value) |
187 | - | -- Move to target Z level first |
187 | + | current_facing = target_value |
188 | - | while currentZ > targetZ do |
188 | + | |
189 | - | if not turtle.down() then return false end |
189 | + | |
190 | - | currentZ = currentZ - 1 |
190 | + | if turtle.detect() then |
191 | - | end |
191 | + | turtle.dig() |
192 | - | while currentZ < targetZ do |
192 | + | |
193 | - | if not turtle.up() then return false end |
193 | + | turtle.forward() |
194 | - | currentZ = currentZ + 1 |
194 | + | |
195 | - | end |
195 | + | -- Update position based on facing |
196 | - | |
196 | + | if current_facing == 0 then |
197 | - | -- Face toward chest (facing 2) |
197 | + | currentY = currentY + 1 |
198 | - | turnToFacingOptimal(2) |
198 | + | elseif current_facing == 1 then |
199 | - | |
199 | + | currentX = currentX + 1 |
200 | - | -- Try Y movement without digging |
200 | + | elseif current_facing == 2 then |
201 | - | local initialY = currentY |
201 | + | currentY = currentY - 1 |
202 | - | while currentY < 0 do |
202 | + | |
203 | - | if not turtle.forward() then |
203 | + | currentX = currentX - 1 |
204 | - | -- If blocked, restore position and return false |
204 | + | |
205 | - | while currentY < initialY do |
205 | + | |
206 | - | turtle.back() |
206 | + | |
207 | - | currentY = currentY + 1 |
207 | + | |
208 | - | end |
208 | + | |
209 | - | return false |
209 | + | -- Unified dig function |
210 | - | end |
210 | + | local function dig_room(L, W, H) |
211 | - | currentY = currentY + 1 |
211 | + | currentX, currentY, currentZ = 0, 0, 0 -- Reset position |
212 | - | end |
212 | + | local path_data = mine_room(L, W, H) |
213 | - | |
213 | + | print("Optimized Path: "..path_data[1]) |
214 | - | -- Try X movement without digging |
214 | + | dig_by_path(path_data[1]) |
215 | - | if currentX ~= 0 then |
215 | + | |
216 | - | if currentX > 0 then |
216 | + | |
217 | - | turnToFacingOptimal(3) -- face left |
217 | + | -- Main program remains the same |
218 | - | else |
218 | + | |
219 | - | turnToFacingOptimal(1) -- face right |
219 | + | |
220 | - | end |
220 | + | print("Usage: digRoom <length> <width> <height>") |
221 | - | |
221 | + | |
222 | - | local initialX = currentX |
222 | + | |
223 | - | while currentX ~= 0 do |
223 | + | |
224 | - | if not turtle.forward() then |
224 | + | |
225 | - | -- If blocked, restore position and return false |
225 | + | |
226 | - | while currentX ~= initialX do |
226 | + | |
227 | - | turtle.back() |
227 | + | |
228 | - | currentX = currentX + (currentX > 0 and 1 or -1) |
228 | + | |
229 | - | end |
229 | + | print("Invalid dimensions!") |
230 | - | return false |
230 | + | |
231 | - | end |
231 | + | |
232 | - | currentX = currentX + (currentX > 0 and -1 or 1) |
232 | + | |
233 | - | end |
233 | + | dig_room(length, width, height) |