-
Notifications
You must be signed in to change notification settings - Fork 0
/
mineFlat
189 lines (189 loc) · 4.42 KB
/
mineFlat
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
args = {...}
---------------------------------------
--Constants
---------------------------------------
inventorySize = 16
maxItemIgnore = 3
dropPeriod = 60
stepCounter = 0
mineCounter = 0
displayWidth = 39
---------------------------------------
--Variables
---------------------------------------
heading = 0
currentCoordinate = vector.new(0, 0, 0)
ignoreLength = 0
width = nil
height = nil
depthTo = nil
necessaryFuel = nil
isMovingRight = true
startingFromBottom = true
progress = 0
startingLayer = 0
---------------------------------------
--Parse arguments
---------------------------------------
if #args == 1 then
radius = tonumber(args[1])
necessaryFuel = (2*radius+1)^2 - 1
else
print("Usage: mineFlat [radius]")
error()
end
if turtle.getFuelLevel() < necessaryFuel then
print("Needs ", necessaryFuel, " to complete")
end
---------------------------------------
--Functions
---------------------------------------
function turnLeft()
turtle.turnLeft()
heading = (heading + 1) % 4
end
function turnRight()
turtle.turnRight()
heading = (heading - 1) % 4
end
function mine()
while turtle.detect() do
if turtle.dig() then
for m = 0, math.floor(displayWidth*(mineCounter+1)/necessaryFuel) - math.floor(displayWidth*mineCounter/necessaryFuel) do
write("-")
end
mineCounter = mineCounter + 1
if mineCounter % dropPeriod == 0 then
cleanInventory()
end
else
print("Cannot dig")
sleep(3)
end
end
end
function mineDown()
while not turtle.down() do
turtle.digDown()
end
currentCoordinate.z = currentCoordinate.z - 1
stepCounter = stepCounter + 1
end
function turnTo(ang)
if heading == ang then
return
elseif (heading + 1) % 4 == ang then
turnLeft()
elseif (heading - 1) % 4 == ang then
turnRight()
else
turnRight()
turnRight()
end
end
function updatePosition()
if heading == 0 then
currentCoordinate.x = currentCoordinate.x + 1
elseif heading == 1 then
currentCoordinate.y = currentCoordinate.y + 1
elseif heading == 2 then
currentCoordinate.x = currentCoordinate.x - 1
elseif heading == 3 then
currentCoordinate.y = currentCoordinate.y - 1
else
print("Error in updatePosition(direction) - case 1")
end
end
function step()
hasSufficientFuel()
if turtle.forward() then
stepCounter = stepCounter + 1
updatePosition(direction)
return true
end
return false
end
function go(amount)
for k = 1, amount do
while not step() do
mine()
end
end
end
function goHome()
gotoPos(vector.new(0, 0, 0))
turnTo(0)
print("Got through ", j, " layers")
print("Mined a total of ", mineCounter, " blocks")
print("Travelled a distance of ", stepCounter, " meters")
print("Ending fuel: ", turtle.getFuelLevel())
error()
end
function hasSufficientFuel()
if math.abs(currentCoordinate.x) + math.abs(currentCoordinate.y) + math.abs(currentCoordinate.z) >= turtle.getFuelLevel() then
print("\nNot enough fuel. Going home")
goHome()
end
end
function isInventoryFull()
if turtle.getItemCount(inventorySize) > 0 then
print("\nInventory full - terminating")
goHome()
end
end
function cleanInventory()
for z = 1, ignoreLength do
turtle.select(z)
turtle.drop(turtle.getItemCount(z) - 1)
for y = ignoreLength + 1, inventorySize do
if turtle.getItemCount(y) > 0 and turtle.compareTo(y) then
turtle.select(y)
turtle.drop()
turtle.select(z)
end
end
end
turtle.select(1)
end
--------------------------------
--Initialize
---------------------------------------
while ignoreLength <= maxItemIgnore and turtle.getItemCount(ignoreLength + 1) > 0 do
ignoreLength = ignoreLength + 1
end
if turtle.getItemCount(ignoreLength + 1) > 0 then
print("Aborting: turtle needs to be emptied before use")
error()
elseif ignoreLength == 0 then
print("Place blocks to ignore in slots 1-", maxItemIgnore)
elseif ignoreLength > maxItemIgnore then
print("Aborting: turtle needs to be emptied before use")
error()
elseif ignoreLength < maxItemIgnore and turtle.getItemCount(ignoreLength + 1) > 0 then
print("Aborting: turtle needs to be emptied before use")
error()
end
if ignoreLength == 0 then
print("Not ignoring any blocks")
elseif ignoreLength == 1 then
print("Ignoring item in slot 1")
else
print("Ignoring items in slots 1-", ignoreLength)
end
---------------------------------------
--Main program
---------------------------------------
for i = 1, displayWidth do
sleep(0.1)
write("-")
end
write("\n")
turtle.select(1)
mineDown()
while j <= radius do
for k = 1, 2 do
go(math.ceil(j/2))
turnRight()
end
end
goHome()