-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripper
418 lines (405 loc) · 11.2 KB
/
stripper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
os.loadAPI("Logger");
os.loadAPI("SimpleGPS");
SimpleGPS.setFuelSlot(0);
SimpleGPS.setRefPosition(0,0,0);
Logger.start("stripper.log",Logger.LogLevel.Info);
local tArgs = {...}
local sizx = tonumber(tArgs[1]);
local sizy = tonumber(tArgs[2]);
local depth = tonumber(tArgs[3]);
depth = depth or -3;
sizx = sizx or 15;
sizy = sizy or 15;
local digup = depth>0;
-- CONSTANTS
--slots
local slot_chest = 12;
local slot_compare = {13,14,15,16}
local slot_firstreserved = slot_chest;
local faces = {
["down"] = 1;
["up"] = 2;
["forward"] = 3;
["right"] = 4;
["backward"]= 5;
["left"] = 6;
}
-- init gps
SimpleGPS.setRefOrientation(SimpleGPS.Orientation.forward);
function waitforuserinput()
sInput = nil;
print("Press ENTER to continue..");
while true do
sInput = read();
if (sInput ~= nil or sInput ~= "") then
break;
end
end
end
function yesnodialog(msg)
while (true) do
if (msg~=nil) then
print(msg);
end
print("Press Y(es) or N(o) to continue.");
local event, param1 = os.pullEvent ("char") -- limit os.pullEvent to the char event
if (param1=="Y" or param1=="y") then
return true;
elseif (param1=="N" or param1=="n") then
return false;
else
print("Your input: " .. param1);
print("Incorrect input. Press Y or N!");
sleep(1.5);
end
end
end
function isok2mine(direct) -- 1: down, // 2: up // other: forward
for ii, slotii in ipairs(slot_compare) do
if (isslot(slotii,direct)) then
return true
end
end
return false;
end
function isslot(slotnum, direct)-- 1: down, // 2: up // other: forward
turtle.select(slotnum);
if (direct==1) then
return turtle.compareUp();
elseif (direct==2) then
return turtle.compareDown();
else
return turtle.compare();
end
end
function getDirection(src,dest)
return (dest>src);
end
function reachedDestination(astart,acur,adest)
return ((acur==adest) or getDirection(astart,adest)~=getDirection(acur,adest));
end
function gotoy(destY)
-- goto y=dest, x=xstart, z=zcurrent
-- only mine the blocks in the comparison slots
-- avoid other blocks by wrapping around them (go left, follow the block countour)
local startY = SimpleGPS.getY();
while (not reachedDestination(startY,SimpleGPS.getY(),destY)) do
if (not cautiousdigy(getDirection(startY,destY))) then
goaroundy(destY);
end
end
end
function gotox(destX)
-- goto x=dest, y=ystart,z=zcurrent
-- only mine the blocks in the comparison slots
-- avoid other blocks by wrapping around them (go left, follow the block countour)
local startX = SimpleGPS.getX();
while (not reachedDestination(startX,SimpleGPS.getX(),destX)) do
if (not cautiousdigx(getDirection(startX,destX))) then
goaroundy(destX);
end
end
end
function bool2str(b)
if b then
return 'true';
else
return 'false';
end
end
function xor(a,b)
return a~=b;
end
function goaroundy(desty)
local startX = SimpleGPS.getX();
local startY = SimpleGPS.getY();
while true do
if (cautiousdigforward()) then
if ((SimpleGPS.getX()==startX) and (getDirection(startY,desty)==getDirection(startY,SimpleGPS.getY()))) then
SimpleGPS.turnleft(1);
break
end
SimpleGPS.turnright(1);
else
SimpleGPS.turnleft(1);
end
end
end
function goaroundx(destx)
local startX = SimpleGPS.getX();
local startY = SimpleGPS.getY();
while true do
if (cautiousdigforward()) then
if ((SimpleGPS.getY()==startY) and (getDirection(startX,destx)==getDirection(startX,SimpleGPS.getX()))) then
SimpleGPS.turnleft(1);
break
end
SimpleGPS.turnright(1);
else
SimpleGPS.turnleft(1);
end
end
end
function cautiousdigx(trueifplus)
if trueifplus then
SimpleGPS.face(SimpleGPS.Orientation.right);
else
SimpleGPS.face(SimpleGPS.Orientation.left);
end
return cautiousdigforward();
end
function cautiousdigy(trueifplus)
if trueifplus then
SimpleGPS.face(SimpleGPS.Orientation.forward);
else
SimpleGPS.face(SimpleGPS.Orientation.backward);
end
return cautiousdigforward();
end
function cautiousdigforward()
if (turtle.detect()) then
if (isok2mine()) then
return SimpleGPS.digforward(1);
else
return false;
end
else
return SimpleGPS.moveforward(1);
end
end
function getItemIdx(slotnum)
turtle.select(slotnum);
for ii, slotii in ipairs(slot_compare) do
if (turtle.compareTo(slotii)) then
return ii;
end
end
return nil;
end
function all(arr)
for ii, arrii in ipairs(arr) do
if ((arrii==false) or (arrii==nil)) then
return false;
end
end
return true;
end
function any(arr)
for ii, arrii in ipairs(arr) do
if (arrii==true) then
return true;
end
end
return false;
end
function createFalse(L)
a={};
for ii=1,L do
a[ii]=false;
end
return a;
end
function isFullInventory()
-- if there is an empty slot, it is not full
local isemptyslot = false;
for ii=1,slot_firstreserved-1 do
if (turtle.getItemCount(ii)==0) then
isemptyslot=true
break;
end
end
if (isemptyslot) then
return false;
else
local spaceleft=createFalse(table.getn(slot_compare));
local item_idx;
for ii=1,16 do
if (turtle.getItemSpace(ii)>=1) then
item_idx=getItemIdx(ii);
if (item_idx~=nil) then
spaceleft[item_idx]=true;
end
end
end
return not all(spaceleft);
end
end
function slot2chest(slotnum, direct, keepcount)
keepcount = keepcount or 0;
local blocks_to_drop;
while true do
blocks_to_drop = turtle.getItemCount(slotnum)-keepcount;
if (blocks_to_drop>=1) then
turtle.select(slotnum);
if (direct==faces.up) then
if (turtle.dropUp(blocks_to_drop)) then break;end
elseif (direct==faces.down) then
if (turtle.dropDown(blocks_to_drop)) then break;end
else
-- assume we're already facing the correct direction
if (turtle.drop(blocks_to_drop)) then break;end
end
Logger.logdebug("Dropoff chest is full, let's wait 10s");
sleep(10);
else
break;
end
end
return true;
end
function cleardirect(direct)
-- note: sand/gravel/other falling blocks can fall, and _will_ be mined, I don't care if they're in the allowed-to-mine list or not
if (direct==faces.up) then
while(turtle.detectUp()) do
turtle.digUp();
sleep(SimpleGPS.block_fall_time);
end
elseif (direct==faces.down) then
turtle.digDown();
else
SimpleGPS.face(direct-3);
while(turtle.detect()) do
turtle.dig();
sleep(SimpleGPS.block_fall_time);
end
end
end
function placeslot(slotnum, direct)
local num_attempts = 0;
turtle.select(slotnum);
while(true) do
if (direct==faces.down) then
if (turtle.placeDown()) then
return true;
end
elseif (direct==faces.up) then
if (turtle.placeUp()) then
return true;
end
else
SimpleGPS.face(direct-3);
if (turtle.place()) then
return true;
end
end
num_attempts = num_attempts + 1
if (num_attempts>20) then
Logger.logerror("I'm stuck while placing slot " .. slotnum)
return false
end
end
end
function dump2chestAt(chestspot)
Logger.loginfo('Dumping items into enderchest');
cleardirect(chestspot);
-- place the chest
if (not placeslot(slot_chest,chestspot)) then
return false;
end
-- drop normal slots
for ii=1,slot_firstreserved-1 do
slot2chest(ii,chestspot,0);
end
for ii, slotii in ipairs(slot_compare) do
slot2chest(slotii,chestspot,1);
end
-- collect chest
turtle.select(slot_chest);
if (chestspot==faces.down) then
turtle.digDown();
elseif (chestspot==faces.up) then
turtle.digUp();
else
SimpleGPS.face(chestspot-3);
turtle.dig();
end
return true;
end
function inv2enderchest()
-- find empty spot to place the chest
local chestspot; -- 1 --> 6: down, up, forward, left, back, right
if (not turtle.detectUp()) then
chestspot = faces.up;
elseif (isok2mine(faces.up)) then
chestspot = faces.up;
elseif (not turtle.detectDown()) then
chestspot = faces.down;
elseif (isok2mine(faces.down)) then
chestspot = faces.down;
else
for ii=1,4 do
if (not turtle.detect()) then
chestspot = SimpleGPS.getOrientation()+3;
break;
elseif (isok2mine(faces.forward)) then
chestspot = SimpleGPS.getOrientation()+3;
break;
end
SimpleGPS.turnleft();
end
end
if (chestspot==nil) then
Logger.logerror("Can't place enderchest");
return false;
end
Logger.logdebug("chestspot is " .. chestspot);
return dump2chestAt(chestspot);
end
function checkFullInventory()
if (isFullInventory()) then
if (not inv2enderchest()) then
Logger.logerror("Inventory is full, cannot dump, MISSION ABORTED");
return false
end
else
Logger.logdebug("Inventory is not full");
end
return true
end
function gohome(startX, startY)
gotox(startX);
gotoy(startY);
SimpleGPS.face(SimpleGPS.Orientation.forward);
end
function main()
local startX = SimpleGPS.getX();
local startY = SimpleGPS.getY();
local xx;
for zz=1,math.abs(depth) do
xx = 1;
while(xx<=sizx) do
gotox(startX+xx-1);
if (not checkFullInventory()) then
gohome(startX, startY);
return;
end
gotoy(startY+sizy-1);
xx=xx+1;
if (xx<=sizx) then
gotox(startX+xx-1);
if (not checkFullInventory()) then
gohome(startX, startY);
return;
end
gotoy(startY);
xx=xx+1;
end
end
gohome(startX, startY);
if (digup) then
SimpleGPS.digz(1);
else
SimpleGPS.digz(-1);
end
end
SimpleGPS.face(SimpleGPS.Orientation.forward);
end
term.clear()
term.setCursorPos(1, 1)
print("Make sure the four bottom slots contain the allowed blocks to mine!");
print("");
if (yesnodialog("Do you want to start?")) then
print("Starting, please stand back!");
main();
end
Logger.stop();