-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiningTurtle.lua
211 lines (180 loc) · 4.63 KB
/
MiningTurtle.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
local robot = require("robot")
local term = require("term")
local computer = require("computer")
term.write("x: ")
xDimension = term.read()
term.write("y: ")
yDimension = term.read()
term.write("z: ")
zDimension = term.read()
currentX = 0
currentY = 0
currentZ = 0
currentlyReturning = false
directions = { "N", "E", "S", "W" }
currentDirectionIndex = 0
function deepCopy(x)
return x
end
function turn(isLeftTurn)
if isLeftTurn then
currentDirectionIndex = currentDirectionIndex - 1
robot.turnLeft()
else
currentDirectionIndex = currentDirectionIndex + 1
robot.turnRight()
end
end
function shouldReturn()
local totalEnergy = computer.maxEnergy()
local currentEnergy = computer.energy()
local energyPercentage = (currentEnergy / totalEnergy) * 100
if energyPercentage < 10 then
return true
end
local isInventoryFull = true
for i = 1, robot.inventorySize() do
if robot.count(i) == 0 then
isInventoryFull = false
break
end
end
return isInventoryFull
end
function move()
robot.forward()
if (directions[(currentDirectionIndex % 4)+1] == "N") then
currentX = currentX + 1
elseif (directions[(currentDirectionIndex % 4)+1] == "E") then
currentY = currentY + 1
elseif (directions[(currentDirectionIndex % 4)+1] == "S") then
currentX = currentX - 1
elseif (directions[(currentDirectionIndex % 4)+1] == "W") then
currentY = currentY - 1
end
if (not currentlyReturning) and shouldReturn() then
currentlyReturning = true
local lastMiningX = deepCopy(currentX)
local lastMiningY = deepCopy(currentY)
local lastMiningZ = deepCopy(currentZ)
local lastDirection = deepCopy(currentDirectionIndex)
returnToOrigin()
returnToLastMiningPosition(lastMiningX, lastMiningY, lastMiningZ, lastDirection)
currentlyReturning = false
end
end
function dropItems()
for i = 1, robot.inventorySize() do
robot.select(i)
robot.drop()
end
end
function waitForCharging()
local lastEnergy = 0
while lastEnergy + 50 < computer.energy() do
lastEnergy = computer.energy()
os.sleep(1)
end
end
function returnToOrigin()
while currentZ ~= 0 do
while robot.detectUp() do
robot.swingUp()
end
robot.up()
currentZ = currentZ - 1
end
while directions[(currentDirectionIndex % 4)+1] ~= "W" do
turn(false)
end
while currentY ~= 0 do
while robot.detect() do
robot.swing()
end
move()
end
turn(true)
while currentX ~= 0 do
while robot.detect() do
robot.swing()
end
move()
end
dropItems()
waitForCharging()
turn(false)
turn(false)
end
function returnToLastMiningPosition(lastMiningX, lastMiningY, lastMiningZ, lastDirection)
while currentX ~= lastMiningX do
while robot.detect() do
robot.swing()
end
move()
end
turn(false)
while currentY ~= lastMiningY do
while robot.detect() do
robot.swing()
end
move()
end
while currentZ ~= lastMiningZ do
while robot.detectDown() do
robot.swingDown()
end
robot.down()
currentZ = currentZ + 1
end
while currentDirectionIndex % 4 ~= lastDirection % 4 do
turn(false)
end
end
turnIterator = 0
for iz = zDimension, 1, -1 do
for iy = yDimension, 1, -1 do
for ix = xDimension - 1, 1, -1 do
while robot.detect() do
robot.swing()
end
move()
end
if iy ~= 1 then
if turnIterator % 2 == 0 then
turn(false)
while robot.detect() do
robot.swing()
end
move()
turn(false)
else
turn(true)
while robot.detect() do
robot.swing()
end
move()
turn(true)
end
end
turnIterator = turnIterator + 1
end
if iz ~= 1 then
while robot.detectDown() do
robot.swingDown()
end
robot.down()
currentZ = currentZ + 1
turn(true)
turn(true)
end
if currentY == 0 and currentX == 0 then
turnIterator = 0
elseif currentY ~= 0 and currentX == 0 then
turnIterator = 1
elseif currentY == 0 and currentX ~= 0 then
turnIterator = 1
else
turnIterator = 0
end
end
returnToOrigin()