-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon
212 lines (198 loc) · 4.48 KB
/
common
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
local common = {}
FUEL_SLOT = 16
direction = "north"
x = 0
y = 0
z = 0
function reFuel(xF,yF,zF)
local fuelLevel = turtle.getFuelLevel()
if fuelLevel < 100 then
navigate(xF,yF,zF)
setDirection("south")
turtle.select(FUEL_SLOT)
turtle.refuel(1)
local fuelSpace = turtle.getItemSpace()
turtle.suck(fuelSpace)
end
end
function toBoolean(str)
local bool = false
if str == "true" then
bool = true
end
return bool
end
function splitStr(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function turnLeft()
if direction == "north" then
direction = "west"
elseif direction == "west" then
direction = "south"
elseif direction == "south" then
direction = "east"
else
direction = "north"
end
turtle.turnLeft()
end
function turnRight()
if direction == "north" then
direction = "east"
elseif direction == "east" then
direction = "south"
elseif direction == "south" then
direction = "west"
else
direction = "north"
end
turtle.turnRight()
end
function forward()
if direction == "north" then
z = z - 1
elseif direction == "south" then
z = z + 1
elseif direction == "east" then
x = x + 1
else
x = x - 1
end
turtle.forward()
end
function backward()
if direction == "north" then
z = z + 1
elseif direction == "south" then
z = z - 1
elseif direction == "east" then
x = x - 1
else
x = x + 1
end
turtle.back()
end
function up()
y = y + 1
turtle.up()
end
function down()
y = y - 1
turtle.down()
end
function setDirection(directionF)
--print("Turning to " .. directionF .. " currently " .. direction)
if direction == "north" then
if directionF == "south" then
turnRight()
turnRight()
elseif directionF == "west" then
turnLeft()
elseif directionF == "east" then
turnRight()
else
return
end
elseif direction == "south" then
if directionF == "north" then
turnRight()
turnRight()
elseif directionF == "west" then
turnRight()
elseif directionF == "east" then
turnLeft()
else
return
end
elseif direction == "east" then
if directionF == "west" then
turnRight()
turnRight()
elseif directionF == "north" then
turnLeft()
elseif directionF == "south" then
turnRight()
else
return
end
else
if directionF == "east" then
turnRight()
turnRight()
elseif directionF == "south" then
turnLeft()
elseif directionF == "north" then
turnRight()
else
return
end
end
end
function gpsInit()
x, y, z = gps.locate()
print("My location: " .. x .. ", " .. y .. ", " .. z)
turtle.forward()
xD, yD, zD = gps.locate()
if xD < x then direction = "west" end
if xD > x then direction = "east" end
if zD < z then direction = "north" end
if zD > z then direction = "south" end
turtle.back()
print("My direction: " .. direction)
end
function navigate(xF,yF,zF)
-- Navigate y
local yDis = yF - y;
if yDis > 0 then
print("Moving up " .. yDis)
for i=yDis,1,-1 do
up()
end
else
print("Moving down " .. yDis)
for i=yDis,-1,1 do
down()
end
end
setDirection("north")
-- Navigate z
local zDis = zF - z
if zDis > 0 then
print("Moving north " .. zDis)
for i=zDis,1,-1 do
print("Moving forward ")
backward()
end
else
print("Moving south " .. zDis)
for i=zDis,-1,1 do
print("Moving backward ")
forward()
end
end
setDirection("east")
-- Navigate x
local xDis = xF - x
if xDis > 0 then
print("Moving east " .. xDis)
for i=xDis,1,-1 do
print("Moving forward ")
forward()
end
else
print("Moving west " .. xDis)
for i=xDis,-1,1 do
print("Moving backward ")
backward()
end
end
end
return common