-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtle_loc_man.lua
237 lines (196 loc) · 5.37 KB
/
turtle_loc_man.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
--Turtle location manager by oedze
local _settingsFolder = "./.oedzeturtle"
local _locationFolder = _settingsFolder .. "/location"
if turtle.tlm_loaded then
print("turtle location manager was already loaded, skipping init")
return
end
local _directionNames = {"north", "east", "south", "west"}
--Direction, 0 = NOrth, 1 = East, 2 = Sout, 3= West
local _location = {
x = 0,
y = 0,
z = 0,
direction = 0
}
local function saveLocation()
local file = fs.open(_locationFolder, "w")
file.write(textutils.serialize(_location))
file.close()
end
if fs.exists(_locationFolder) then
--print("locationfolder exists, getting location....")
local file = fs.open(_locationFolder, "r")
local x = file.readAll()
--print("x", x)
_location = textutils.unserialize(x)
--print("Location: ", location)
file.close()
else
--print("Locationfolder doensn't exists, creatingz ")
saveLocation()
end
if not fs.exists(_settingsFolder) then
fs.makeDir(_settingsFolder)
else
--print("Dir exists")
end
local oldTurnLeft = turtle.turnLeft
local oldTurnRight = turtle.turnRight
local oldUp = turtle.up
local oldDown = turtle.down
local oldForward = turtle.forward
local oldBack = turtle.back
local function mutateLocation(localForwardDiff, localYDiff, localRotation)
--//print("Mutation: [" .. localForwardDiff .. ", " .. localYDiff .. ", " .. localRotation .. "]")
_location.direction = _location.direction + localRotation
if(_location.direction > 3) then _location.direction = _location.direction - 4 end
if(_location.direction < 0) then _location.direction = _location.direction + 4 end
_location.y = _location.y + localYDiff
if _location.direction == 0 then
--//print("Negative z")
_location.z = _location.z - localForwardDiff
end
if _location.direction == 1 then
--//print("Positive x")
_location.x = _location.x + localForwardDiff
end
if _location.direction == 2 then
--//print("Positive z")
_location.z = _location.z + localForwardDiff
end
if _location.direction == 3 then
--print("Negative x")
_location.x = _location.x - localForwardDiff
end
saveLocation()
end
local function forward()
local success = oldForward()
if success then mutateLocation(1, 0, 0) end
return success
end
local function back()
local success = oldBack()
if success then mutateLocation(-1, 0, 0) end
return success
end
local function up()
local success = oldUp()
if success then mutateLocation(0, 1, 0) end
return success
end
local function down()
local success = oldDown()
if success then mutateLocation(0, -1, 0) end
return success
end
local function turnRight()
oldTurnRight()
mutateLocation(0, 0, 1)
return success
end
local function turnLeft()
oldTurnLeft()
mutateLocation(0, 0, -1)
return success
end
local function getLocation ()
return _location
end
local function getDirectionName(direction)
if direction > 3 then direction = direction - 4 end
if direction < 0 then direction = direction + 4 end
return _directionNames[direction + 1]
end
local function setLocation(location)
_location = location
saveLocation()
end
local _tempLocation = {
x = nil,
y = nil,
z = nil,
direction = nil
}
local function validateLocation()
x, y, z = gps.locate()
if x == nil then
return nil
end
_tempLocation.x = x
_tempLocation.y = y
_tempLocation.z = z
_tempLocation.direction = 0 --Unknown
local turtleLocation = getLocation()
if turtleLocation.x == x and turtleLocation.y == y and turtleLocation.z == z then
--assume direction is right
return false
else
setLocation(_tempLocation)
return true
end
end
local function getDirection(xDiff, zDiff)
if xDiff > 0 then
return 1 --East
end
if xDiff < 0 then
return 3 --West
end
if(zDiff > 0) then
return 2 --South
end
if(zDiff < 0) then
return 0 --North
end
return nil
end
local function findDirection()
local curX, curY, curZ = gps.locate()
if(curX == nil) then
return false
end
local rotated = 0
local found = false
local turtleDirection = 0
while found == false and rotated < 4 do
if oldForward() then
found = true
local newX, newY, newZ = gps.locate()
oldBack()
turtleDirection = getDirection(newX - curX, newZ - curZ)
else
oldTurnRight()
rotated = rotated + 1
end
end
for rot = 1, rotated do
oldTurnLeft()
end
return (turtleDirection - rotated) % 4
end
local function storePositionAndDirection()
local updated = validateLocation()
if(updated == true) then
_location.direction = findDirection()
saveLocation()
end
end
function validateDirection()
_location.direction = findDirection()
saveLocation()
end
turtle.turnLeft = turnLeft
turtle.turnRight = turnRight
turtle.down = down
turtle.up = up
turtle.forward = forward
turtle.back = back
turtle.getDirectionName = getDirectionName
turtle.getLocation = getLocation
turtle.setLocation = setLocation
turtle.validateLocation = storeLocation
turtle.validateDirection = validateDirection
turtle.validateLocationAndRotation = storePositionAndDirection
turtle.tlm_loaded = true