-
Notifications
You must be signed in to change notification settings - Fork 0
/
t.lua
201 lines (176 loc) · 3.7 KB
/
t.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
--[[
Advanced Turtle API
--]]
-- Item selection
-- check if turtle has a specific item and select it
function selectItem(item_name)
for i = 1, 16 do
turtle.select(i)
local item = turtle.getItemDetail()
if item and item.name == item_name then
return true
end
end
return false
end
-- Digging with falling block detection
function dig()
local tries = 0
while turtle.detect() do
turtle.dig()
sleep(0.4)
tries = tries + 1
if tries > 64 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 > 64 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 > 64 then
print('Error: dug down for too long')
return false
end
end
return true
end
-- Traveling: attempts to move in a direction
function forward(amount)
amount = amount or 1
for i = 1, amount do
local tries = 0
while not turtle.forward() do
turtle.dig()
turtle.attack()
sleep(0.2)
tries = tries + 1
if tries > 64 then
print('Error: can\'t move forward')
return false
end
end
end
return true
end
function up(amount)
amount = amount or 1
for i = 1, amount do
local tries = 0
while not turtle.up() do
turtle.digUp()
turtle.attackUp()
sleep(0.2)
tries = tries + 1
if tries > 64 then
print('Error: can\'t move up')
return false
end
end
end
return true
end
function down(amount)
amount = amount or 1
for i = 1, amount do
local tries = 0
while not turtle.down() do
turtle.digDown()
turtle.attackDown()
sleep(0.2)
tries = tries + 1
if tries > 64 then
print('Error: can\'t move down')
return false
end
end
end
return true
end
function back(amount)
amount = amount or 1
for i = 1, amount do
if not turtle.back() then
turnAround()
forward()
turnAround()
end
end
end
-- Place items: Doesn't place when the item is already placed
function place()
if not turtle.compare() then
dig()
turtle.place()
end
end
function placeUp()
if not turtle.compareUp() then
digUp()
turtle.placeUp()
end
end
function placeDown()
if not turtle.compareDown() then
digDown()
turtle.placeDown()
end
end
function placeRight()
turtle.turnRight()
place()
turtle.turnLeft()
end
function placeLeft()
turtle.turnLeft()
place()
turtle.turnRight()
end
function placeBack()
turnAround()
place()
turnAround()
end
-- Turning
function turnAround()
turtle.turnRight()
turtle.turnRight()
end
function goRight(amount)
amount = amount or 1
turtle.turnRight()
forward(amount)
end
function goLeft(amount)
amount = amount or 1
turtle.turnLeft()
forward(amount)
end
function strafeRight(amount)
amount = amount or 1
goRight(amount)
turtle.turnLeft()
end
function strafeLeft(amount)
amount = amount or 1
goLeft(amount)
turtle.turnRight()
end