-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsense.lua
More file actions
81 lines (70 loc) · 1.73 KB
/
sense.lua
File metadata and controls
81 lines (70 loc) · 1.73 KB
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
mine_block = {
["minecraft:diamond_ore"] = true, --diamond ore
["minecraft:iron_ore"] = true, --iron ore
["minecraft:gold_ore"] = true, --gold ore
["minecraft:redstone_ore"] = true, --redstone ore
["ic2:resource"] = true, --IC Ores
["thermalfoundation:ore"] = true, --Thermal Foundation Ores
}
function sense()
dir = {N = 0, E = 1, S = 2, W = 3}
turn = {L = 0, R = 1}
if wantBlock() then
queue = {dir.W}
queueLast = 0
facing = dir.E
turtle.dig()
turtle.forward()
while (true) do
for i = 0,3,1 do
--decide which direction to turn
turnDir = turn.R
if i == 0 then turnDir = turn.L end
--turn that way and see if we want to mine
facing = turnTo(turnDir, facing)
if wantBlock() then
queueLast = queueLast + 1
queue[queueLast] = invertDir(facing)
turtle.dig()
turtle.forward()
break
end
--if facing where we came from all blocks are exhausted, start going back
if facing == queue[queueLast] then
turtle.forward()
queueLast = queueLast - 1
break
end
end
end
--mined vein and returned
print("done")
end
end
function turnTo(turnDir, curDir)
ret = curDir
if turnDir == turn.L then
turtle.turnLeft()
if (curDir == dir.N) then ret = dir.W
else ret = curDir - 1 end
end
if turnDir == turn.R then
turtle.turnRight()
if (curDir == dir.W) then ret = dir.N
else ret = curDir + 1 end
end
return ret
end
function invertDir(dir)
newDir = dir - 2
if newDir < 0 then
newDir = newDir + 4
end
return newDir
end
--returns true if the block we're looking at is something we want
function wantBlock()
success, data = turtle.inspect()
return success and mine_block[data.name]
end
sense()