-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNavigation.lua
212 lines (179 loc) · 3.86 KB
/
Navigation.lua
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
-- VARIABLES --
-- Global coordinates
globalX = 0
globalY = 0
globalZ = 0
-- Chunk Coordinates
chunkX = 0
chunkY = 0
chunkZ = 0
-- Navigation
orientations = {"north", "east", "south", "west"}
orientation = -1
diffX = {0, 1, 0, -1}
diffZ = {-1, 0, 1, 0}
-- FUNCTIONS --
function look(dir)
while dir ~= orientations[orientation] do
right()
end
end
function checkFuel()
if turtle.getFuelLevel() < 1 then
unload()
busy = true
turtle.select(fuelEnderChestSlot)
turtle.place()
turtle.select(firstItemSlot)
for i = 1, refuelCount, 1 do
turtle.suck()
turtle.refuel()
end
turtle.select(fuelEnderChestSlot)
turtle.dig()
busy = false
turtle.select(1)
end
end
function goToChunkEdge()
if orientation == 1 then
while localZ > 0 do
forward()
end
elseif orientation == 2 then
while localX < 15 do
forward()
end
elseif orientation == 3 then
while localZ < 15 do
forward()
end
elseif orientation == 4 then
while localX > 0 do
forward()
end
else
print("I don't know what way i'm looking")
end
end
function forward()
checkFuel()
while not turtle.forward() do turtle.dig() turtle.attack() end
if orientation ~= -1 then
globalX = globalX + diffX[orientation]
globalZ = globalZ + diffZ[orientation]
localX = localX + diffX[orientation]
localZ = localZ + diffZ[orientation]
if localX > 15 then chunkX = chunkX + 1
elseif localX < 0 then chunkX = chunkX - 1
elseif localZ > 15 then chunkZ = chunkZ + 1
elseif localZ < 0 then chunkZ = chunkZ - 1 end
end
end
function unload()
busy = true
turtle.select(itemEnderchestSlot)
turtle.dig()
turtle.place()
for i = firstItemSlot,lastItemSlot,1 do
turtle.select(i)
turtle.drop()
end
turtle.select(itemEnderchestSlot)
turtle.dig()
turtle.select(1)
busy = false
end
function mineForward()
checkFuel()
turtle.digUp()
turtle.digDown()
if turtle.getItemCount(16) > 0 then unload() end
while not turtle.forward() do turtle.dig() turtle.attack() end
if orientation ~= -1 then
globalX = globalX + diffX[orientation]
globalZ = globalZ + diffZ[orientation]
end
end
function up()
while not turtle.up() do turtle.digUp() turtle.attackUp() end
globalY = globalY + 1
end
function down()
while not turtle.down() do turtle.digDown() turtle.attackDown() end
globalY = globalY - 1
end
function left()
if turtle.turnLeft() then
orientation = orientation - 1
orientation = (orientation - 1) % 4
orientation = orientation + 1
end
end
function right()
if turtle.turnRight() then
orientation = orientation - 1
orientation = (orientation + 1) % 4
orientation = orientation + 1
end
end
function goto(x, y, z)
while y > globalY do
up()
end
while x < globalX do
look("west")
forward()
end
while x > globalZ do
look("east")
forward()
end
while z < globalZ do
look("north")
forward()
end
while z > globalZ do
look("south")
forward()
end
while y < globalY do
down()
end
end
function getNextChunk()
tempX = globalX
tempZ = globalZ
tempX = tempX + diffX[orientation] * 16
tempZ = tempZ + diffZ[orientation] * 16
tempX = math.floor(tempX/16)
tempZ = math.floor(tempZ/16)
return textutils.serialize({tempX, tempZ})
end
function updatePosition()
globalX, globalY, globalZ = gps.locate()
chunkX = math.floor(globalX/16)
chunkY = math.floor(globalY/16)
chunkZ = math.floor(globalZ/16)
localX = globalX % 16
localY = globalY % 16
localZ = globalZ % 16
end
function updateOrientation()
updatePosition()
turtle.back()
nX, nY, nZ = gps.locate()
if nX > globalX then
orientation = 4
elseif nX < globalX then
orientation = 2
elseif nZ > globalZ then
orientation = 1
elseif nZ < globalZ then
orientation = 3
end
turtle.forward()
print("I'm at " .. globalX .. " " .. globalY .. " " .. globalZ)
print("Facing " .. orientations[orientation])
print("In chunk " .. chunkX .. " " .. chunkY .. " " .. chunkZ)
end