-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobustTurtle_API.lua
251 lines (226 loc) · 4.38 KB
/
RobustTurtle_API.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
--Robust Turtle API by SpeedR
--Digging with gravel/sand detection
function dig()
local tries = 0
while turtle.detect() do
turtle.dig()
sleep(0.4)
tries = tries + 1
if tries>500 then
print("Error: dug for too long.")
return false
end
end
return true
end
function digUp()
local tries = 0
while turtle.detectUp() do
turtle.digUp()
sleep(0.4)
tries = tries + 1
if tries>500 then
print("Error: dug up for too long.")
return false
end
end
return true
end
function digDown()
local tries = 0
while turtle.detectDown() do
turtle.digDown()
sleep(0.4)
tries = tries + 1
if tries>500 then
print("Error: dug down for too long.")
return false
end
end
return true
end
--Traveling: Goes in the direction no matter what (almost)
--Will not be stopped by blocks or mobs
function forward(l)
l=l or 1
for i=1,l do
local tries = 0
while turtle.forward() ~= true do
turtle.dig()
turtle.attack()
sleep(0.2)
tries = tries + 1
if tries>500 then
print("Error: can't move forward.")
return false
end
end
end
return true
end
function up(l)
l=l or 1
for i=1,l do
local tries = 0
while turtle.up() ~= true do
turtle.digUp()
turtle.attackUp()
sleep(0.2)
tries = tries + 1
if tries>500 then
print("Error: can't move up.")
return false
end
end
end
return true
end
function down(l)
l=l or 1
for i=1,l do
local tries = 0
while turtle.down() ~= true do
turtle.digDown()
turtle.attackDown()
sleep(0.2)
tries = tries + 1
if tries>500 then
print("Error: can't move down.")
return false
end
end
end
return true
end
function back(l)
l=l or 1
for i=1,l do
if turtle.back() ~= true then
turnAround()
forward()
turnAround()
end
end
end
--Place blocks
--Does not place when there's already the right block.
function place(block)
turtle.select(block)
if turtle.compare()==false then
if turtle.getItemCount(block)==0 then
outOfResource(block)
end
dig()
turtle.place()
end
end
function placeUp(block)
turtle.select(block)
if turtle.compareUp()==false then
if turtle.getItemCount(block)==0 then
outOfResource(block)
end
digUp()
turtle.placeUp()
end
end
function placeDown(block)
turtle.select(block)
if turtle.compareDown()==false then
if turtle.getItemCount(block)==0 then
outOfResource(block)
end
digDown()
turtle.placeDown()
end
end
local function outOfResource()
print("Ran out of a resource. Block: ",block , ".")
print("Refill, then say something to proceed.")
read()
end
function placeRight(block)
turtle.turnRight()
place(block)
turtle.turnLeft()
end
function placeLeft(block)
turtle.turnLeft()
place(block)
turtle.turnRight()
end
function placeBack(block)
turnAround()
place(block)
turnAround()
end
--place row e.g. placeRow(up, marble, forward, 15)
function placeRow(placeDir, block, travelDir, l)
l=l or 1
for i=1,l do
if placeDir == "forward" then
place(block)
elseif placeDir == "up" then
placeUp(block)
elseif placeDir == "down" then
placeDown(block)
elseif placeDir == "right" then
placeRight(block)
elseif placeDir == "left" then
placeLeft(block)
elseif placeDir == "back" then
placeBack(block)
else
print('"', placeDir, '" is not a valid direction!')
return false
end
if travelDir == "forward" then
forward()
elseif travelDir == "up" then
up()
elseif travelDir == "down" then
down()
elseif travelDir == "right" then
strafeRight()
elseif travelDir == "left" then
strafeLeft()
elseif travelDir == "back" then
back()
else
print('"', travelDir, '" is not a valid direction!')
return false
end
end
return true
end
--Turning
function turnAround()
turtle.turnRight()
turtle.turnRight()
end
function right()
turtle.turnRight()
end
function left()
turtle.turnLeft()
end
function goRight(l)
l=l or 1
turtle.turnRight()
forward(l)
end
function goLeft(l)
l=l or 1
turtle.turnLeft()
forward(l)
end
function strafeRight(l)
l=l or 1
goRight(l)
turtle.turnLeft()
end
function strafeLeft(l)
l=l or 1
goLeft(l)
turtle.turnRight()
end