-
Notifications
You must be signed in to change notification settings - Fork 0
/
digger
217 lines (188 loc) · 3.52 KB
/
digger
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
local x=0;
local y=0;
local z=0;
local facing=0;
local checkpoints = {}
function myRight()
turtle.turnRight()
if facing == 0 then
facing = 3
else
facing = facing - 1
end
return facing
end
function myLeft()
turtle.turnLeft()
if facing == 3 then
facing = 0
else
facing = facing + 1
end
return facing
end
function setFacing(new_facing)
local turns = facing - new_facing
if math.abs( turns ) == 3 then
turns = (turns -2)*-1
end
if turns > 0 then
for i=1,math.abs(turns),1 do
myRight()
end
end
if turns < 0 then
for i=1,math.abs(turns),1 do
myLeft()
end
end
end
function redundantForward()
while not turtle.forward() do
if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
turtle.refuel()
end
if turtle.detect() then
turtle.dig()
end
end
end
function myForward(count)
for i=1,count,1 do
redundantForward()
end
end
function redundantUp()
while not turtle.up() do
if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
turtle.refuel()
end
if turtle.detectUp() then
turtle.digUp()
end
end
end
function myUp(count)
for i=1,count,1 do
redundantUp()
end
end
function redundantDown()
while not turtle.down() do
if turtle.getFuelLevel() ~= "unlimited" and turtle.getFuelLevel() < 1 then
turtle.refuel()
end
if turtle.detectDown() then
turtle.digDown()
end
end
end
function myDown(count)
for i=1,count,1 do
redundantDown()
end
end
function moveX(amount)
if amount < 0 then
setFacing( 1 )
else
setFacing( 3 )
end
myForward( math.abs(amount) )
x = x + amount
end
function moveY(amount)
if amount < 0 then
setFacing( 2 )
else
setFacing( 0 )
end
myForward( math.abs(amount) )
y = y + amount
end
function moveZ(amount)
if amount < 0 then
myDown( math.abs(amount) )
else
myUp( amount )
end
z = z + amount
end
function createCheckpoint()
local p = { x = x, y = y, z = z }
table.insert( checkpoints, p )
return( table.getn( checkpoints ) )
end
function returnToCheckpoint(index)
local p = checkpoints[index]
moveX((p["x"]-x))
moveY((p["y"]-y))
moveZ((p["z"]-z))
end
function box(x1,y1,z1,x2,y2,z2,mode)
--Assume start point is p1
local cols = x2 - x1
local rows = y2 - y1
local tiers = z2 - z1
local z_length = math.abs( tiers )
local z_dir = 1
if tiers < 0 then
z_dir = -1
end
local col_iter = 0
local tier_iter = 0
for j=0,math.abs(tiers),1 do
for i=0,math.abs(cols),1 do
if math.fmod(col_iter,2) == 0 then
moveY(rows)
else
moveY(rows*-1)
end
if i < math.abs(cols) then
if math.fmod(tier_iter,2) == 0 then
moveX(1)
else
moveX(-1)
end
end
col_iter = col_iter + 1
end
if j < z_length then
moveZ(z_dir)
end
tier_iter = tier_iter + 1
end
end
function staircase_a(levels, direction, stepsize)
for i=0,levels,1 do
local move_amt = stepsize
if i > ( levels - stepsize ) then
move_amt = stepsize - ( i - (levels - stepsize) )
end
moveY(move_amt)
if i < levels then
moveY((move_amt-1)*-1)
moveZ(direction)
end
end
end
function main()
write("Trying base..hit enter to start\n")
read()
staircase_a(6,-1,5)
moveY(-5)
cp1 = createCheckpoint()
moveX(-2)
setFacing(0)
box(0,0,0,4,9,3)
returnToCheckpoint(cp1)
staircase_a(6,-1,5)
moveY(-5)
cp2 = createCheckpoint()
moveX(-2)
setFacing(0)
box(0,0,0,4,9,3)
returnToCheckpoint(cp2)
write("done\n")
end
main()