-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovement.lua
More file actions
290 lines (244 loc) · 7.88 KB
/
movement.lua
File metadata and controls
290 lines (244 loc) · 7.88 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
--variable setup
local direction = 1
local locationVector = nil
local dimVector = nil
local row = 0
local layerCount = 0
--rednet setup (this section sets up the wireless capabilities of the robot)
rednet.open("left")
repeat
local _, message = rednet.receive("locationVector") --receives a message from the pocket computer
locationVector = message
until locationVector ~= nil -- will repeat until locationVector is assigned a valuei fe
repeat
local _, message = rednet.receive("dimVector") --receives a message from the pocket computer
dimVector = message
until dimVector ~= nil
--check for turtle
local function inspectBlock()
local has_frontBlock, frontBlock = turtle.inspect()
if has_frontBlock == false then
return
end
if frontBlock.name == "computercraft:turtle_advanced" then
print("Turtle in front!")
os.sleep(1)
end
local has_upBlock, upBlock = turtle.inspectUp()
if has_upBlock == false then
return
end
if upBlock.name == "computercraft:turtle_advanced" then
print("Turtle above")
os.sleep(1)
end
local has_downBlock, downBlock = turtle.inspectDown()
if has_upBlock == false then
return
end
if downBlock.name == "computercraft:turtle_advanced" then
print("Turtle below!")
os.sleep(1)
end
end
--calibration sequence (section that setup the robots ability to read cardinal directions)
local directionChange = {
[1] = vector.new(0, 0, -1), -- North
[2] = vector.new(1, 0, 0), -- East
[3] = vector.new(0, 0, 1), -- South
[4] = vector.new(-1, 0, 0) -- West
}
local currentLocation = vector.new(gps.locate())
print(currentLocation.x, currentLocation.y, currentLocation.z)
while turtle.detect() do
inspectBlock()
turtle.dig()
end
turtle.forward()
local newLocation = vector.new(gps.locate())
local vectorDiff = newLocation - currentLocation
local function directionCalc() --calculates the current cardinal direction of the turtle using change in coordinate movement
for k, v in pairs(directionChange) do
if vectorDiff == directionChange[k] then
direction = k
end
end
end
directionCalc()
--basic movement section
local function right()
turtle.turnRight()
direction = direction + 1
if direction == 5 then
direction = 1
end
end
local function left()
turtle.turnLeft()
direction = direction - 1
if direction == 0 then
direction = 4
end
end
--movement calculation
local diffLocation = vector.new(locationVector.x - currentLocation.x, locationVector.y - currentLocation.y, locationVector.z - currentLocation.z)
print(diffLocation.x, diffLocation.y, diffLocation.z)
local function calcDiffX() --calculates the difference between the current location and the desired location (x)
if diffLocation.x > 0 then
while direction ~= 2 do --turns left until direction is correct (east)
left()
end
while diffLocation.x ~= 0 do
while turtle.detect() do
inspectBlock()
turtle.dig()
end
turtle.forward()
currentLocation = vector.new(gps.locate()) --constantly updates the current location once a cycle has completed
diffLocation = locationVector - currentLocation
end
end
if diffLocation.x < 0 then
while direction ~= 4 do --turns left until direction is correct (west)
left()
end
while diffLocation.x ~= 0 do
while turtle.detect() do
inspectBlock()
turtle.dig()
end
turtle.forward()
currentLocation = vector.new(gps.locate()) --constantly updates the current location once a cycle has completed
diffLocation = locationVector - currentLocation
end
end
end
local function calcDiffY() --calculates the difference between the current location and the desired location (y)
if diffLocation.y > 0 then --if the target is above ground, then the turtle will begin to move upwards
while diffLocation.y ~= 0 do
while turtle.detectUp() do
inspectBlock()
turtle.digUp()
end
turtle.up()
currentLocation = vector.new(gps.locate()) --constantly updates the current location once a cycle has completed
diffLocation = locationVector - currentLocation
end
end
if diffLocation.y < 0 then --if the target is below ground, then the turtle will begin to move downwards
while diffLocation.y ~= 0 do
while turtle.detectDown() do
inspectBlock()
turtle.digDown()
end
turtle.down()
currentLocation = vector.new(gps.locate()) --constantly updates the current location once a cycle has completed
diffLocation = locationVector - currentLocation
end
end
end
local function calcDiffZ() --calculates the difference between the current location and the desired location (z)
if diffLocation.z > 0 then
while direction ~= 3 do --turns left until direction is correct (south)
left()
end
while diffLocation.z ~= 0 do
while turtle.detect() do
inspectBlock()
turtle.dig()
end
turtle.forward()
currentLocation = vector.new(gps.locate()) --constantly updates the current location once a cycle has completed
diffLocation = locationVector - currentLocation
end
end
if diffLocation.z < 0 then
while direction ~= 1 do --turns left until direction is correct (north)
left()
end
while diffLocation.z ~= 0 do
while turtle.detect() do
inspectBlock()
turtle.dig()
end
turtle.forward()
currentLocation = vector.new(gps.locate()) --constantly updates the current location once a cycle has completed
diffLocation = locationVector - currentLocation
end
end
end
while diffLocation ~= vector.new(0, 0, 0) do
calcDiffX()
calcDiffY()
calcDiffZ()
end
--mining functions
local function clearLength()
print("Clearing a row")
for i = 1, dimVector.x - 1, 1 do
while turtle.detect() do
inspectBlock()
turtle.dig()
end
turtle.forward()
end
row = row + 1
end
local function depositLayer()
if row % 2 == 0 then
right()
turtle.forward()
left()
print("Depositing")
end
if row % 2 == 1 then
left()
turtle.forward()
right()
print("Depositing")
end
turtle.select(1)
turtle.place()
turtle.refuel()
for i = 1, 16, 1 do
turtle.select(i)
turtle.drop()
end
turtle.select(1)
turtle.dig()
end
local function clearLayer()
for i = 1, dimVector.z, 1 do
clearLength()
if row % 2 == 0 then
right()
while turtle.detect() do
inspectBlock()
turtle.dig()
end
turtle.forward()
right()
end
if row % 2 == 1 then
left()
while turtle.detect() do
inspectBlock()
turtle.dig()
end
turtle.forward()
left()
end
end
os.sleep(3)
depositLayer()
turtle.digDown()
turtle.down()
layerCount = layerCount + 1
row = row - 1
end
--exection
turtle.digDown()
turtle.down()
while layerCount ~= dimVector.y do
clearLayer()
end