SHOW:
|
|
- or go back to the newest paste.
1 | local tArgs = {...} | |
2 | - | if not fs.exists("refill") then |
2 | + | |
3 | - | shell.run("pastebin get biP6fFEi refill") |
3 | + | function delay() |
4 | sleep(.4) | |
5 | return true | |
6 | - | shell.run("refill") |
6 | + | |
7 | ||
8 | function recordStep(n) | |
9 | local h = fs.open("nObjective",'w') | |
10 | h.writeLine("nObjective = "..tostring(n)..";") | |
11 | h.close() | |
12 | end | |
13 | ||
14 | function recordPos(heightPos,widthPos,lengthPos,face) | |
15 | local h = fs.open("position","w") | |
16 | h.writeLine("heightPos = "..tostring(heightPos)..";") | |
17 | h.writeLine("widthPos = "..tostring(widthPos)..";") | |
18 | h.writeLine("lengthPos = "..tostring(lengthPos)..";") | |
19 | h.writeLine("face = ".."\""..tostring(face).."\""..";") | |
20 | h.close() | |
21 | end | |
22 | ||
23 | function updateVar(file,variable,newValue) | |
24 | - | local exists |
24 | + | local exists |
25 | - | local withinTable = false |
25 | + | if not fs.exists(file) then |
26 | - | if not fs.exists(file) then |
26 | + | error("file does not exist") |
27 | - | error("file does not exist") |
27 | + | |
28 | ||
29 | variable = tostring(variable) | |
30 | - | if variable:find("%.") then |
30 | + | newValue = textutils.serialize(newValue) |
31 | - | variable = variable:gsub("(.*%.)","") |
31 | + | --print(variable) |
32 | - | withinTable = true |
32 | + | |
33 | - | print(variable) |
33 | + | local string = fs.open(file,"r").readAll() |
34 | local pattern = variable.."=(.*;)" | |
35 | - | --if variable |
35 | + | local replace = variable.."="..newValue..";" |
36 | --print(string:find(pattern)) | |
37 | - | variable = tostring(variable) |
37 | + | |
38 | - | newValue = textutils.serialize(newValue) |
38 | + | exists = string:find(pattern) |
39 | - | --print(variable) |
39 | + | |
40 | if exists then | |
41 | - | local string = fs.open(file,"r").readAll() |
41 | + | local h = fs.open(file,"w") |
42 | - | local pattern,replace |
42 | + | string = string:gsub(pattern,replace) |
43 | - | if not withinTable then |
43 | + | h.write(string) |
44 | - | pattern = variable.." =(.-;)" |
44 | + | h.close() |
45 | - | replace = variable.." = "..newValue..";" |
45 | + | |
46 | local h = fs.open(file,"a") | |
47 | - | pattern = variable.." =(.-,)" |
47 | + | h.writeLine(replace) |
48 | - | replace = variable.." = "..newValue.."," |
48 | + | h.close() |
49 | - | print("pattern: ",pattern) |
49 | + | |
50 | - | print("replace: ",replace) |
50 | + | |
51 | ||
52 | - | --print(string:find(pattern)) |
52 | + | |
53 | function reinitialize() | |
54 | - | exists = string:find(pattern) |
54 | + | |
55 | widthPos = 0 | |
56 | - | if exists then |
56 | + | |
57 | - | local h = fs.open(file,"w") |
57 | + | |
58 | - | string = string:gsub(pattern,replace) |
58 | + | |
59 | - | h.write(string) |
59 | + | |
60 | - | h.close() |
60 | + | |
61 | - | --else |
61 | + | |
62 | - | --local h = fs.open(file,"a") |
62 | + | |
63 | - | --h.writeLine(replace) |
63 | + | |
64 | - | --h.close() |
64 | + | |
65 | if fs.exists("position") then | |
66 | shell.run("position") | |
67 | else | |
68 | reinitialize() | |
69 | end | |
70 | ||
71 | --[[Navigation function]]-- | |
72 | ||
73 | function goto(heightGoal,widthGoal,lengthGoal) | |
74 | shell.run("position") | |
75 | if turtle.getFuelLevel() < 200 then | |
76 | refill() | |
77 | - | if tonumber(tArgs[1]) then |
77 | + | |
78 | - | heightPos = tonumber(tArgs[1]) |
78 | + | |
79 | - | widthPos = tonumber(tArgs[2]) |
79 | + | |
80 | - | lengthPos = tonumber(tArgs[3]) |
80 | + | |
81 | - | face = tArgs[4] |
81 | + | |
82 | - | recordPos(heightPos,widthPos,lengthPos,face) |
82 | + | |
83 | while heightGoal < heightPos do | |
84 | down() | |
85 | end | |
86 | end | |
87 | if widthGoal > widthPos then | |
88 | turn("east") | |
89 | while widthGoal > widthPos do | |
90 | forward() | |
91 | end | |
92 | elseif widthGoal < widthPos then | |
93 | turn("west") | |
94 | while widthGoal < widthPos do | |
95 | - | --Navigation function-- |
95 | + | |
96 | end | |
97 | end | |
98 | - | local function matchHeight(heightGoal) |
98 | + | |
99 | turn("south") | |
100 | while lengthGoal > lengthPos do | |
101 | forward() | |
102 | - | end |
102 | + | |
103 | elseif lengthGoal < lengthPos then | |
104 | turn("north") | |
105 | while lengthGoal < lengthPos do | |
106 | forward() | |
107 | end | |
108 | end | |
109 | end | |
110 | - | local function matchWidth(widthGoal) |
110 | + | |
111 | - | if widthGoal < widthPos then |
111 | + | |
112 | - | turn("west") |
112 | + | |
113 | - | while widthGoal < widthPos do |
113 | + | |
114 | - | forward() |
114 | + | |
115 | - | end |
115 | + | |
116 | - | elseif widthGoal > widthPos then |
116 | + | |
117 | - | turn("east") |
117 | + | |
118 | - | while widthGoal > widthPos do |
118 | + | |
119 | - | forward() |
119 | + | |
120 | - | end |
120 | + | |
121 | - | end |
121 | + | |
122 | elseif dir == "backward" then | |
123 | if face == "north" then | |
124 | - | local function matchLength(lengthGoal) |
124 | + | |
125 | elseif face == "south" then | |
126 | lengthPos = lengthPos - 1 | |
127 | elseif face == "west" then | |
128 | widthPos = widthPos + 1 | |
129 | elseif face == "east" then | |
130 | widthPos = widthPos - 1 | |
131 | end | |
132 | elseif dir == "up" then | |
133 | heightPos = heightPos + 1 | |
134 | elseif dir == "down" then | |
135 | heightPos = heightPos - 1 | |
136 | elseif dir == "right" then | |
137 | if face == "north" then | |
138 | - | function goto2(heightGoal,widthGoal,lengthGoal) |
138 | + | |
139 | - | --shell.run("position") |
139 | + | |
140 | face = "south" | |
141 | - | assert(refill) |
141 | + | |
142 | face = "west" | |
143 | elseif face == "west" then | |
144 | face = "north" | |
145 | - | assert(face) |
145 | + | |
146 | - | matchHeight(heightGoal) |
146 | + | |
147 | - | if face == "south" or face == "north" then |
147 | + | |
148 | - | if face == "south" and lengthGoal > lengthPos then |
148 | + | |
149 | - | matchLength(lengthGoal) |
149 | + | |
150 | - | matchWidth(widthGoal) |
150 | + | |
151 | - | elseif face == "north" and lengthGoal < lengthPos then |
151 | + | |
152 | - | matchLength(lengthGoal) |
152 | + | |
153 | - | matchWidth(widthGoal) |
153 | + | |
154 | - | else |
154 | + | |
155 | - | matchWidth(widthGoal) |
155 | + | |
156 | - | matchLength(lengthGoal) |
156 | + | |
157 | - | end |
157 | + | |
158 | end | |
159 | - | if face == "west" and widthGoal < widthPos then |
159 | + | |
160 | - | matchWidth(widthGoal) |
160 | + | --[[Movement functions]]-- |
161 | - | matchLength(lengthGoal) |
161 | + | |
162 | - | elseif face == "east" and widthGoal > widthPos then |
162 | + | |
163 | - | matchWidth(widthGoal) |
163 | + | parallel.waitForAll(delay,function() while not turtle.forward() do turtle.dig() end return true end,function() update("forward") end) |
164 | - | matchLength(lengthGoal) |
164 | + | |
165 | - | else |
165 | + | |
166 | - | matchLength(lengthGoal) |
166 | + | |
167 | - | matchWidth(widthGoal) |
167 | + | parallel.waitForAll(delay,function() while not turtle.up() do turtle.digUp() end return true end,function() update("up") end) |
168 | - | end |
168 | + | |
169 | ||
170 | function down() | |
171 | parallel.waitForAll(delay,function() while not turtle.down() do turtle.digDown() end return true end,function() update("down") end) | |
172 | end | |
173 | - | --shell.run("position") |
173 | + | |
174 | - | assert(face) |
174 | + | |
175 | - | matchHeight(heightGoal) |
175 | + | parallel.waitForAll(delay,turtle.turnRight,function() update("right") end) |
176 | - | if face == "south" then |
176 | + | |
177 | - | if lengthGoal > lengthPos then |
177 | + | |
178 | - | while lengthGoal > lengthPos do |
178 | + | |
179 | - | forward() |
179 | + | parallel.waitForAll(delay,turtle.turnLeft,function() update("left") end) |
180 | - | end |
180 | + | |
181 | - | elseif lengthGoal < lengthPos then |
181 | + | |
182 | - | while lengthGoal < lengthPos do |
182 | + | |
183 | - | backward() |
183 | + | |
184 | - | end |
184 | + | |
185 | - | end |
185 | + | |
186 | - | matchWidth(widthGoal) |
186 | + | |
187 | - | elseif face == "north" then |
187 | + | |
188 | - | if lengthGoal < lengthPos then |
188 | + | |
189 | - | while lengthGoal < lengthPos do |
189 | + | |
190 | - | forward() |
190 | + | |
191 | - | end |
191 | + | |
192 | - | elseif lengthGoal > lengthPos then |
192 | + | |
193 | - | while lengthGoal > lengthPos do |
193 | + | |
194 | - | backward() |
194 | + | |
195 | - | end |
195 | + | |
196 | - | end |
196 | + | |
197 | - | matchWidth(widthGoal) |
197 | + | |
198 | - | elseif face == "west" then |
198 | + | |
199 | - | if widthGoal < widthPos then |
199 | + | |
200 | - | while widthGoal < widthPos do |
200 | + | |
201 | - | forward() |
201 | + | |
202 | - | end |
202 | + | |
203 | - | elseif widthGoal > widthPos then |
203 | + | |
204 | - | while widthGoal > widthPos do |
204 | + | |
205 | - | backward() |
205 | + | |
206 | - | end |
206 | + | |
207 | - | end |
207 | + | |
208 | - | matchLength(lengthGoal) |
208 | + | |
209 | - | elseif face == "east" then |
209 | + | |
210 | - | if widthGoal > widthPos then |
210 | + | |
211 | - | while widthGoal > widthPos do |
211 | + | |
212 | - | forward() |
212 | + | |
213 | - | end |
213 | + | |
214 | - | elseif widthGoal < widthPos then |
214 | + | |
215 | - | while widthGoal < widthPos do |
215 | + | |
216 | - | backward() |
216 | + | |
217 | - | end |
217 | + | |
218 | - | end |
218 | + | |
219 | - | matchLength(lengthGoal) |
219 | + | |
220 | left() | |
221 | end | |
222 | end | |
223 | end | |
224 | end | |
225 | ||
226 | function place() | |
227 | while not turtle.placeDown() do | |
228 | turtle.digDown() | |
229 | end | |
230 | return true | |
231 | end | |
232 | ||
233 | function saveInventory() | |
234 | ||
235 | end | |
236 | ||
237 | function updateInventory() | |
238 | --updates inventory table with whats in the turtle inventory | |
239 | local inventory = {} | |
240 | for i = 1,16 do | |
241 | inventory[i] = turtle.getItemDetail(i) | |
242 | end | |
243 | end | |
244 | ||
245 | ||
246 | function find(id,data,slots) | |
247 | --input numid or strname, numdata, and tabslots | |
248 | --output slotNumber | |
249 | --slots[id][data] = {name,damage,orientation} | |
250 | local exceptions = {"stairs","slabs","door","ladder","torch","lever","button","rail","chest","furnace"} | |
251 | local name, damage, orientation | |
252 | ||
253 | if type(id) == "number" then | |
254 | name = slots[id][data][1] | |
255 | damage = slots[id][data][2] | |
256 | orientation = data | |
257 | --convert to string ie minecraft:stone | |
258 | elseif type(id) == "string" then | |
259 | name = id | |
260 | damage = data | |
261 | orientation = data | |
262 | end | |
263 | --assert(name) | |
264 | if not name then | |
265 | --skips if not assigned in slots | |
266 | return nil | |
267 | end | |
268 | ||
269 | for i=1,16 do | |
270 | local item = turtle.getItemDetail(i) | |
271 | if item and item.name == name then | |
272 | --[[for s = 1,#exceptions do | |
273 | - | --Movement functions-- |
273 | + | if name:find(exceptions[s]) then |
274 | return i | |
275 | - | function courtesyOLD() |
275 | + | |
276 | - | --if not turtle then |
276 | + | end]] |
277 | ||
278 | - | if turtle.up() then |
278 | + | if item.damage == damage then |
279 | - | sleep(3) |
279 | + | |
280 | - | while not turtle.down() do |
280 | + | |
281 | - | sleep(1) |
281 | + | |
282 | - | end |
282 | + | |
283 | ||
284 | - | elseif turtle.down() then |
284 | + | |
285 | - | sleep(3) |
285 | + | error("not enough "..name..", "..damage) |
286 | - | while not turtle.up() do |
286 | + | |
287 | - | sleep(1) |
287 | + | |
288 | - | end |
288 | + | function findBackup(id,data,slots) |
289 | --input numid or strname, numdata, and tabslots | |
290 | - | elseif turtle.forward() then |
290 | + | |
291 | - | sleep(3) |
291 | + | |
292 | - | while not turtle.back() do |
292 | + | |
293 | - | sleep(1) |
293 | + | |
294 | - | end |
294 | + | |
295 | if type(id) == "number" then | |
296 | - | elseif turtle.back() then |
296 | + | |
297 | - | sleep(3) |
297 | + | |
298 | - | while not turtle.forward() do |
298 | + | |
299 | - | sleep(1) |
299 | + | |
300 | - | end |
300 | + | |
301 | name = id | |
302 | damage = data | |
303 | orientation = data | |
304 | end | |
305 | - | function courtesy() |
305 | + | |
306 | - | local choice = math.random(1,6) |
306 | + | |
307 | - | if choice == 1 then |
307 | + | |
308 | - | if turtle.up() then |
308 | + | return nil |
309 | - | update("up") |
309 | + | |
310 | - | sleep(3) |
310 | + | |
311 | - | while not turtle.down() do |
311 | + | |
312 | - | sleep(1) |
312 | + | turtle.select(i) |
313 | - | end |
313 | + | local item = turtle.getItemDetail() |
314 | - | update("down") |
314 | + | if item and item.name == name then |
315 | - | end |
315 | + | --[[for s = 1,#exceptions do |
316 | if name:find(exceptions[s]) then | |
317 | - | elseif choice == 2 then |
317 | + | return i |
318 | - | if turtle.down() then |
318 | + | |
319 | - | update("down") |
319 | + | end]] |
320 | - | sleep(3) |
320 | + | |
321 | - | while not turtle.up() do |
321 | + | if item.damage == damage then |
322 | - | sleep(1) |
322 | + | |
323 | - | end |
323 | + | |
324 | - | update("up") |
324 | + | |
325 | - | end |
325 | + | |
326 | ||
327 | - | elseif choice == 3 then |
327 | + | |
328 | - | if turtle.forward() then |
328 | + | error("not enough "..name..", "..damage) |
329 | - | update("forward") |
329 | + | |
330 | - | sleep(3) |
330 | + | |
331 | - | while not turtle.back() do |
331 | + | function smartPlace(wrench,orientation) |
332 | - | sleep(1) |
332 | + | |
333 | - | end |
333 | + | |
334 | - | update("backward") |
334 | + | |
335 | - | end |
335 | + | |
336 | if block then | |
337 | - | elseif choice == 4 then |
337 | + | |
338 | - | if turtle.back() then |
338 | + | if (block.name:lower():find("stairs") or block.name:lower():find("slab") or block.name:lower():find("chest")) and wrench then |
339 | - | update("backward") |
339 | + | |
340 | - | sleep(3) |
340 | + | |
341 | - | while not turtle.forward() do |
341 | + | |
342 | - | sleep(1) |
342 | + | |
343 | - | end |
343 | + | |
344 | - | update("forward") |
344 | + | if orient.metadata == orientation then |
345 | - | end |
345 | + | |
346 | end | |
347 | - | elseif choice == 5 then |
347 | + | |
348 | - | right() |
348 | + | local mem = turtle.getSelectedSlot() |
349 | - | if turtle.forward() then |
349 | + | turtle.select(wrench) |
350 | - | update("forward") |
350 | + | |
351 | - | sleep(3) |
351 | + | turtle.select(mem) |
352 | - | while not turtle.back() do |
352 | + | |
353 | - | sleep(1) |
353 | + | |
354 | - | end |
354 | + | |
355 | - | update("backward") |
355 | + | |
356 | - | end |
356 | + | |
357 | - | left() |
357 | + | |
358 | end | |
359 | - | elseif choice == 6 then |
359 | + | |
360 | - | left() |
360 | + | |
361 | - | if turtle.forward() then |
361 | + | |
362 | - | update("forward") |
362 | + | |
363 | - | sleep(3) |
363 | + | |
364 | - | while not turtle.back() do |
364 | + | |
365 | - | sleep(1) |
365 | + | |
366 | - | end |
366 | + | |
367 | - | update("backward") |
367 | + | |
368 | - | end |
368 | + | |
369 | - | right() |
369 | + | |
370 | local block = turtle.getItemDetail() | |
371 | if block then | |
372 | --lookup stairs or slabs | |
373 | if (block.name:lower():find("stairs") or block.name:lower():find("slab") or block.name:lower():find("chest")) and wrench and id~=324 and id~=96 then | |
374 | --place until metadata matches | |
375 | - | update("forward") |
375 | + | |
376 | - | while not turtle.forward() do |
376 | + | |
377 | - | if turtle.getFuelLevel() == 0 then |
377 | + | |
378 | - | error("out of fuel") |
378 | + | |
379 | if orient.metadata == data then | |
380 | - | local a,check = turtle.inspect() |
380 | + | |
381 | - | if a then |
381 | + | |
382 | - | if not check.name:lower():find("turtle") then |
382 | + | |
383 | - | turtle.dig() |
383 | + | local mem = turtle.getSelectedSlot() |
384 | - | else |
384 | + | turtle.select(wrench) |
385 | - | courtesy() |
385 | + | |
386 | - | end |
386 | + | turtle.select(mem) |
387 | end | |
388 | else | |
389 | place() | |
390 | end | |
391 | - | function backward() |
391 | + | |
392 | - | update("backward") |
392 | + | |
393 | - | while not turtle.back() do |
393 | + | |
394 | - | if turtle.getFuelLevel() == 0 then |
394 | + |