This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
forked from brandonhenness/Advanced-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvancedorefinder
812 lines (764 loc) · 18 KB
/
advancedorefinder
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
-- Advanced Ore Finder by Henness
-- Version 3.0 2/5/2013
-- Config
local program = "Advanced Orefinder"
local version = "v3.0"
local author = "Henness"
screenw,screenh = term.getSize()
-- Functions
--[[
Most of the loc movement functions were written by Mikeemoo,
for his killer program. If you would like to see his program
you can download it from: http://pastebin.com/UCES36jD
]]
local loc = {}
loc = {
paused = false,
x = 0,
y = 0,
z = 0,
facing = 0,
facingToAxis = {
[0] = { axis = 'z', unit = 1 },
[1] = { axis = 'x', unit = -1 },
[2] = { axis = 'z', unit = -1 },
[3] = { axis = 'x', unit = 1 }
},
pause = function()
while loc.paused do
sleep(1)
end
end,
getAxisForFacing = function()
return loc.facingToAxis[loc.facing]['axis'], loc.facingToAxis[loc.facing]['unit']
end,
turnLeft = function()
loc.pause()
loc.facing = (loc.facing - 1) % 4
turtle.turnLeft()
end,
turnRight = function()
loc.pause()
loc.facing = (loc.facing + 1) % 4
turtle.turnRight()
end,
moveForward = function()
loc.pause()
if turtle.forward() then
local axis, unit = loc.getAxisForFacing()
loc[axis] = loc[axis] + unit
return true
end
return false
end,
face = function(faceto)
local dirdif = (faceto - loc.facing) % 4
if dirdif == 1 then
loc.turnRight()
elseif dirdif == 2 then
loc.turnRight()
loc.turnRight()
elseif dirdif == 3 then
loc.turnLeft()
end
end,
movez = function(d)
if d < loc.z then loc.face(2) elseif d > loc.z then loc.face(0) end
return loc.moveForward()
end,
movex = function(d)
if d < loc.x then loc.face(1) elseif d > loc.x then loc.face(3) end
return loc.moveForward()
end,
movey = function(d)
if d < loc.y then
return loc.moveDown()
end
return loc.moveUp()
end,
moveUp = function()
loc.pause()
if turtle.up() then
loc.y = loc.y + 1
return true
end
return false
end,
moveDown = function()
loc.pause()
if turtle.down() then
loc.y = loc.y - 1
return true
end
return false
end,
moveTo = function(x, y, z, facing)
if y > loc.y then
while loc.y < y do
while not loc.moveUp() do
if turtle.detectUp() then
turtle.select(1)
turtle.digUp()
else
turtle.attackUp()
end
end
end
end
while x ~= loc.x do
while not loc.movex(x) do
if turtle.detect() then
turtle.select(1)
turtle.dig()
else
turtle.attack()
end
end
end
while z ~= loc.z do
while not loc.movez(z) do
if turtle.detect() then
turtle.select(1)
turtle.dig()
else
turtle.attack()
end
end
end
if y < loc.y then
while loc.y > y do
while not loc.moveDown() do
if turtle.detectDown() then
turtle.select(1)
turtle.digDown()
else
turtle.attackDown()
end
end
end
end
loc.face(facing)
return true
end,
moveToPos = function(pos)
return loc.moveTo(pos.x, pos.y, pos.z, pos.facing)
end,
distanceTo = function(x, y, z)
return math.abs(x) - loc.x + math.abs(y) - loc.y + math.abs(z) - loc.z
end,
distanceToPos = function(pos)
return loc.distanceTo(pos.x, pos.y, pos.z)
end
}
-- Orefinder
function saveTable(table,name)
local file = fs.open(name,"w")
file.write(textutils.serialize(table))
file.close()
end
function loadTable(name)
local file = fs.open(name,"r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
function moveDown(h)
while not loc.movey(h) do
if turtle.detectDown() then
turtle.digDown()
if turtle.detectDown() then
return false
end
else
turtle.attackDown()
end
end
return true
end
function compareForward(a, b)
loc.pause()
for i=a,b do
turtle.select(i)
if turtle.compare() then
return false
end
end
return true
end
function findDown(d)
if not d then d = loc.y end
local h = loc.y - d
while loc.y ~= h do
if not moveDown(h) then
break
end
for j=1,4 do
if compareForward(1, _tSave.ignore) then
if not space() then
returnToChest()
end
turtle.select(1)
turtle.dig()
ores_mined = ores_mined + 1
end
loc.turnRight()
end
end
end
function refuel(ammount)
if turtle.getFuelLevel() == "unlimited" then
return true
end
local needed = ammount or loc.distanceToPos(_tSave.startPos) * 2 + 1
if turtle.getFuelLevel() < needed then
for n=_tSave.ignore+1,16 do
loc.pause()
if turtle.getItemCount(n) > 0 then
turtle.select(n)
if turtle.refuel(1) then
while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
turtle.refuel(1)
end
if turtle.getFuelLevel() >= needed then
turtle.select(1)
return true
end
end
end
end
turtle.select(1)
return false
end
return true
end
function unload()
for n=1,16 do
loc.pause()
if turtle.getItemCount(n) > 0 then
turtle.select(n)
if n <= _tSave.ignore then
if (not _tSave.safemode and n == 1) or n > 1 then
turtle.drop(turtle.getItemCount(n) - 1)
end
else
turtle.drop()
end
end
end
turtle.select(1)
end
function space()
for n=1,16 do
if turtle.getItemCount(n) == 0 then
return true
end
end
return false
end
function returnToChest()
local returnPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
local fuelNeeded = loc.distanceToPos(_tSave.startPos) * 2 + 1
loc.moveToPos(_tSave.startPos)
loc.face((loc.facing + 2) % 4)
if not refuel(fuelNeeded) then
unload()
while not refuel(fuelNeeded) do
loc.pause()
sleep(1)
end
else
unload()
end
loc.moveToPos(returnPos)
end
function forward()
if not space() then
returnToChest()
end
while not loc.moveForward() do
if turtle.detect() then
turtle.select(1)
turtle.dig()
else
turtle.attack()
end
end
end
function oreFinder()
loc.moveToPos(_tSave.returnPos)
while _tSave.w <= _tSave.width do
while _tSave.l <= _tSave.length do
if not refuel() or not space() then
returnToChest()
end
if math.fmod((loc.x + 4) - (loc.z - 1) * 2, 5) == 0 then
_tSave.returnPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
saveTable( _tSave, _sFile )
findDown()
loc.moveToPos(_tSave.returnPos)
if _tSave.safemode then
turtle.select(1)
turtle.placeDown()
end
end
turtle.digUp()
if _tSave.l ~= _tSave.length then
forward()
elseif _tSave.l == _tSave.length and _tSave.w ~= _tSave.width then
if loc.facing == _tSave.startPos.facing then
loc.turnRight()
forward()
loc.turnRight()
else
loc.turnLeft()
forward()
loc.turnLeft()
end
end
_tSave.l = _tSave.l + 1
end
_tSave.w = _tSave.w + 1
_tSave.l = 1
end
loc.moveToPos(_tSave.startPos)
returnToChest()
fs.delete(_sFile)
return true
end
-- GUI
function printNormal(str, xpos, ypos)
term.setCursorPos(xpos, ypos)
term.write (str)
end
function printReverse(str, xpos, ypos)
term.setCursorPos(xpos - (#str-1), ypos)
term.write (str)
end
function printLeft(str, ypos)
term.setCursorPos(1, ypos)
term.write(str)
end
function printCentered(str, ypos)
term.setCursorPos(screenw/2 - #str/2 + 1, ypos)
term.write(str)
end
function printRight(str, ypos)
term.setCursorPos(screenw - #str, ypos)
term.write(str)
end
function readLines( sLines, _sReplaceChar )
term.setCursorBlink( true )
local w, h = term.getSize()
local nPos = 0
local nSelect = 1
local vSelect = 1
local function redraw( _sCustomReplaceChar )
local nScroll = 0
if sLines[nSelect].sx + nPos >= w then
nScroll = (sLines[nSelect].sx + nPos) - w
end
term.setCursorPos( sLines[nSelect].sx, sLines[nSelect].sy )
local sReplace = _sCustomReplaceChar or _sReplaceChar
if sReplace then
term.write( string.rep(sReplace, string.len(sLines[nSelect].text) - nScroll) )
else
term.write( string.sub( sLines[nSelect].text, nScroll + 1 ) )
end
term.setCursorPos( sLines[nSelect].sx + nPos - nScroll, sLines[nSelect].sy )
end
for nVar, tVar in ipairs(sLines) do
nSelect = nVar
redraw()
if tVar.text ~= "" then
vSelect = nVar
end
end
nSelect = vSelect
nPos = #sLines[nSelect].text
redraw()
while true do
local sEvent, param = os.pullEvent()
if sEvent == "char" then
sLines[nSelect].text = string.sub( sLines[nSelect].text, 1, nPos ) .. param .. string.sub( sLines[nSelect].text, nPos + 1 )
nPos = nPos + 1
redraw()
elseif sEvent == "key" then
if param == keys.enter then
-- Enter
if nSelect < #sLines then
nSelect = nSelect + 1
if sLines[nSelect].text == "" then
nPos = 0
else
nPos = #sLines[nSelect].text
end
redraw()
else
break
end
elseif param == keys.left then
-- Left
if nPos > 0 then
nPos = nPos - 1
redraw()
end
elseif param == keys.right then
-- Right
if nPos < string.len(sLines[nSelect].text) then
nPos = nPos + 1
redraw()
end
elseif param == keys.up or param == keys.down then
-- Up or down
if param == keys.up then
-- Up
if nSelect > 1 then
nSelect = nSelect - 1
if sLines[nSelect].text == "" then
nPos = 0
else
nPos = #sLines[nSelect].text
end
redraw()
end
else
-- Down
if nSelect < #sLines then
nSelect = nSelect + 1
if sLines[nSelect].text == "" then
nPos = 0
else
nPos = #sLines[nSelect].text
end
redraw()
else
break
end
end
redraw()
elseif param == keys.backspace then
-- Backspace
if nPos > 0 then
redraw(" ");
sLines[nSelect].text = string.sub( sLines[nSelect].text, 1, nPos - 1 ) .. string.sub( sLines[nSelect].text, nPos + 1 )
nPos = nPos - 1
redraw()
end
elseif param == keys.home then
-- Home
nPos = 0
redraw()
elseif param == keys.delete then
if nPos < string.len(sLines[nSelect].text) then
redraw(" ");
sLines[nSelect].text = string.sub( sLines[nSelect].text, 1, nPos ) .. string.sub( sLines[nSelect].text, nPos + 2 )
redraw()
end
elseif param == keys["end"] then
-- End
nPos = string.len(sLines[nSelect].text)
redraw()
end
end
end
term.setCursorBlink( false )
return sLine
end
local function locate()
local x,y,z,facing
rednet.open("right")
x,y,z = gps.locate(3)
if x and y and z then
printCentered("Receiving coordinates from host...", 4)
forward()
local tmpx,tmpy,tmpz = gps.locate(3)
if tmpx > x then
facing = 3
elseif tmpx < x then
facing = 1
elseif tmpz > z then
facing = 0
elseif tmpz < z then
facing = 2
end
turtle.back()
return x,y,z,facing
else
printCentered("Unable to get GPS cords. Please,", 3)
printCentered("enter the turtles cords manually.", 4)
end
printLeft(" x: ", 6)
printLeft(" y: ", 7)
printLeft(" z: ", 8)
printLeft(" face: ", 9)
while true do
readLines( tCordsInput )
x,y,z,facing = tonumber(tCordsInput[1].text),tonumber(tCordsInput[2].text),tonumber(tCordsInput[3].text),tonumber(tCordsInput[4].text)
if x ~= nil and y ~= nil and y > 0 and y < 256 and z ~= nil and facing ~= nil and facing < 4 and facing >= 0 then
return x,y,z,facing
end
end
end
local function drawHeader()
printCentered(program, 1)
printLeft(string.rep("-", screenw), 2)
printLeft("["..version.."]"..string.rep("-", screenw-#author-#version-7).."[by "..author.."]", screenh)
end
local function drawMainMenu()
term.clear()
drawHeader()
printLeft(string.rep("-", screenw), screenh-2)
if loc.paused then
if select == 1 then
printCentered("[RESUME] REFUEL SAVE EXIT ", screenh-1)
elseif select == 2 then
printCentered(" RESUME [REFUEL] SAVE EXIT ", screenh-1)
elseif select == 3 then
printCentered(" RESUME REFUEL [SAVE] EXIT ", screenh-1)
elseif select == 4 then
printCentered(" RESUME REFUEL SAVE [EXIT]", screenh-1)
end
else
printCentered("[PAUSE]", screenh-1)
end
end
local function drawGPSMenu()
if select == 2 then
printCentered("[DONE]", screenh-1)
elseif select == 1 then
term.clear()
drawHeader()
printLeft(string.rep("-", screenw), screenh-2)
printCentered(" DONE ", screenh-1)
loc.x,loc.y,loc.z,loc.facing = locate()
select = 2
drawGPSMenu()
end
end
local function drawRefuelMenu()
local refuelList = {
"+-------------------------------+",
"| Are you sure you want to |",
"| refuel the turtle. |",
"| |",
"| YES NO |",
"+-------------------------------+"
}
for i=1,#refuelList do
if i == #refuelList-1 then
if select == 1 then
printCentered("| [YES] NO |", 3 + i)
elseif select == 2 then
printCentered("| YES [NO] |", 3 + i)
end
else
printCentered(refuelList[i], 3 + i)
end
end
end
local function drawRefuel()
local refuelList = {
"+-------------------------------+",
"| Turtle fuel level is |",
"| |",
"| |",
"| OK |",
"+-------------------------------+"
}
for i=1,#refuelList do
if i == #refuelList-1 then
printCentered("| [OK] |", 3 + i)
elseif i == #refuelList-3 then
printCentered(refuelList[i], 3 + i)
printCentered(fuel_level, 3 + i)
else
printCentered(refuelList[i], 3 + i)
end
end
end
local function drawSaveMenu()
local saveList = {
"+-------------------------------+",
"| Would you like to save? |",
"| |",
"| |",
"| YES NO |",
"+-------------------------------+"
}
for i=1,#saveList do
if i == #saveList-1 then
if select == 1 then
printCentered("| [YES] NO |", 3 + i)
elseif select == 2 then
printCentered("| YES [NO] |", 3 + i)
end
else
printCentered(saveList[i], 3 + i)
end
end
end
local function drawExitMenu()
local exitList = {
"+-------------------------------+",
"| Would you like to save |",
"| before exiting? |",
"| |",
"| YES NO |",
"+-------------------------------+"
}
for i=1,#exitList do
if i == #exitList-1 then
if select == 1 then
printCentered("| [YES] NO |", 3 + i)
elseif select == 2 then
printCentered("| YES [NO] |", 3 + i)
end
else
printCentered(exitList[i], 3 + i)
end
end
end
local function drawResultMenu()
local resultList = {
"+-------------------------------+",
"| |",
"| Ores mined this session! |",
"| |",
"| [OK] |",
"+-------------------------------+"
}
for i=1,#resultList do
if i == #resultList-1 then
printCentered("| [OK] |", 3 + i)
elseif i == #resultList-4 then
printCentered(resultList[i], 2 + i)
printCentered(ores_mined, 2 + i)
else
printCentered(resultList[i], 3 + i)
end
end
end
local function runOreFinder(_sSave)
_tSave = {}
_tSave = loadTable(_sSave)
local menustate = "gpsmenu"
local function rungui()
local mopt = {
["main"] = {
options = {"toggle_pause", "refuelmenu", "savemenu", "exitmenu"},
draw = drawMainMenu
},
["gpsmenu"] = {
options = {"locate", "exit"},
draw = drawGPSMenu
},
["refuelmenu"] = {
options = {"refuel", "main"},
draw = drawRefuelMenu
},
["refuel"] = {
options = {"main"},
draw = drawRefuel
},
["savemenu"] = {
options = {"save", "main"},
draw = drawSaveMenu
},
["exitmenu"] = {
options = {"save_exit", "exit"},
draw = drawExitMenu
},
["resultmenu"] = {
options = {"exit"},
draw = drawResultMenu
}
}
tCordsInput = {
{text = "", sx = 9, sy = 6},
{text = "", sx = 9, sy = 7},
{text = "", sx = 9, sy = 8},
{text = "", sx = 9, sy = 9}
}
select = 1
fuel_level = tostring(turtle.getFuelLevel())
ores_mined = 0
while true do
mopt[menustate].draw()
local sEvent, param = os.pullEvent()
if sEvent == "key" then
if param == keys.up and select > 1 then
select = select-1
elseif param == keys.down and select < #mopt[menustate].options then
if loc.paused then
select = select+1
end
elseif param == keys.left and select > 1 then
select = select-1
elseif param == keys.right and select < #mopt[menustate].options then
if loc.paused then
select = select+1
end
elseif param == keys.enter then
if mopt[menustate].options[select] == "save" or mopt[menustate].options[select] == "save_exit" then
saveTable(_tSave, _sSave)
if mopt[menustate].options[select] == "save_exit" then
break
end
menustate = "main"
elseif mopt[menustate].options[select] == "exit" then
break
elseif mopt[menustate].options[select] == "toggle_pause" then
if loc.paused then
loc.paused = false
else
loc.paused = true
end
elseif mopt[menustate].options[select] == "refuel" then
if turtle.getFuelLevel() ~= "unlimited" then
for i=_tSave.ignore+1,16 do
if turtle.getItemCount(i) > 0 then
turtle.select(i)
turtle.refuel(turtle.getItemCount(i))
end
end
turtle.select(1)
end
fuel_level = tostring(turtle.getFuelLevel())
menustate = mopt[menustate].options[select]
select = 1
else
menustate = mopt[menustate].options[select]
select = 1
end
end
end
end
end
rungui()
menustate = "main"
if not _tSave.startPos then
_tSave.startPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
_tSave.returnPos = {x = loc.x, y = loc.y, z = loc.z, facing = loc.facing}
end
saveTable(_tSave, _sFile)
parallel.waitForAny(oreFinder, rungui)
if not fs.exists(_sFile) then
menustate = "resultmenu"
rungui()
end
end
local tArgs = { ... }
if turtle.getFuelLevel() > 0 or turtle.getFuelLevel() == "unlimited" then
if #tArgs == 1 then
_sFile = tostring(tArgs[1])
if fs.exists(_sFile) then
runOreFinder(_sFile)
term.setCursorPos(1,1)
term.clear()
end
else
print("Usage: " .. shell.getRunningProgram() .. " <save file>")
end
else
print("Error, there must be fuel in the turtle!")
end