SHOW:
|
|
- or go back to the newest paste.
1 | MiningTurtle = { | |
2 | position = { | |
3 | x = 0, | |
4 | y = 0, | |
5 | z = 0 | |
6 | }, | |
7 | direction = 0 | |
8 | } | |
9 | ||
10 | -- Check if the Turtle needs to be refueled ( <= refuelLevel ) | |
11 | -- if yes, try to refuel it taking the contents of the inventory | |
12 | -- if this is not possible or not enough, go back to base | |
13 | function MiningTurtle:requiredRefuel( refuelLevel, requiredFuelLevel ) | |
14 | ||
15 | refuelLevel = refuelLevel or 25 | |
16 | requiredFuelLevel = requiredFuelLevel or 500 | |
17 | ||
18 | -- should we refuel? | |
19 | if turtle.getFuelLevel() <= refuelLevel then | |
20 | ||
21 | -- iterate through our inventory | |
22 | for slot = 1,16 do | |
23 | ||
24 | -- select slot | |
25 | turtle.select(slot) | |
26 | ||
27 | -- refuel from active slot... | |
28 | if turtle.refuel( turtle.getItemCount( slot ) ) then | |
29 | ||
30 | end | |
31 | ||
32 | -- ...and check again | |
33 | if turtle.getFuelLevel() >= requiredFuelLevel then | |
34 | - | return |
34 | + | return true |
35 | end | |
36 | ||
37 | end | |
38 | ||
39 | -- check if we did not refueled enough alreay | |
40 | if not turtle.getFuelLevel() >= requiredFuelLevel then | |
41 | -- if yes, go back to base | |
42 | ||
43 | MiningTurtle:toBase() | |
44 | -- refuel: to be implemented!!!! | |
45 | end | |
46 | end | |
47 | ||
48 | return false | |
49 | end | |
50 | ||
51 | -- Go back to the base of the MiningTurtle | |
52 | function MiningTurtle:toBase( ) | |
53 | ||
54 | end | |
55 | ||
56 | -- Go to ground | |
57 | function MiningTurtle:toGround( ) | |
58 | ||
59 | while not turtle.detectDown() do | |
60 | MiningTurtle:down() | |
61 | end | |
62 | ||
63 | end | |
64 | ||
65 | -- forward | |
66 | function MiningTurtle:forward() | |
67 | local ret | |
68 | ||
69 | ret = turtle.forward() | |
70 | ||
71 | if ret then | |
72 | ||
73 | ||
74 | if self.direction == 0 then | |
75 | ||
76 | self.position.x = self.position.x + 1 | |
77 | ||
78 | elseif self.direction == 90 then | |
79 | ||
80 | self.position.y = self.position.y + 1 | |
81 | ||
82 | elseif self.direction == 180 then | |
83 | ||
84 | self.position.x = self.position.x - 1 | |
85 | ||
86 | elseif self.direction == 270 then | |
87 | ||
88 | self.position.y = self.position.y - 1 | |
89 | ||
90 | end | |
91 | ||
92 | end | |
93 | ||
94 | return ret | |
95 | end | |
96 | ||
97 | -- back | |
98 | function MiningTurtle:back() | |
99 | local ret | |
100 | ||
101 | ret = turtle.back() | |
102 | ||
103 | if ret then | |
104 | ||
105 | if self.direction == 0 then | |
106 | ||
107 | self.position.x = self.position.x - 1 | |
108 | ||
109 | elseif self.direction == 90 then | |
110 | ||
111 | self.position.y = self.position.y - 1 | |
112 | ||
113 | elseif self.direction == 180 then | |
114 | ||
115 | self.position.x = self.position.x + 1 | |
116 | ||
117 | elseif self.direction == 270 then | |
118 | ||
119 | self.position.y = self.position.y + 1 | |
120 | ||
121 | end | |
122 | ||
123 | end | |
124 | ||
125 | return ret | |
126 | end | |
127 | ||
128 | -- up | |
129 | function MiningTurtle:up() | |
130 | local ret | |
131 | ||
132 | ret = turtle.up() | |
133 | ||
134 | if ret then | |
135 | self.position.z = self.position.z + 1 | |
136 | end | |
137 | ||
138 | return ret | |
139 | end | |
140 | ||
141 | -- down | |
142 | function MiningTurtle:down() | |
143 | local ret | |
144 | ||
145 | ret = turtle.down() | |
146 | ||
147 | if ret then | |
148 | self.position.z = self.position.z - 1 | |
149 | end | |
150 | ||
151 | return ret | |
152 | end | |
153 | ||
154 | -- turn Left | |
155 | function MiningTurtle:turnLeft() | |
156 | local ret | |
157 | ||
158 | ret = turtle.turnLeft() | |
159 | ||
160 | if ret then | |
161 | self.direction = self.direction + 90 | |
162 | ||
163 | while self.direction >= 360 do | |
164 | self.direction = self.direction - 360 | |
165 | end | |
166 | end | |
167 | ||
168 | return ret | |
169 | end | |
170 | ||
171 | -- turn Right | |
172 | function MiningTurtle:turnRight() | |
173 | local ret | |
174 | ||
175 | ret = turtle.turnRight() | |
176 | ||
177 | if ret then | |
178 | self.direction = self.direction - 90 | |
179 | ||
180 | while self.direction < 0 do | |
181 | self.direction = self.direction + 360 | |
182 | end | |
183 | end | |
184 | ||
185 | return ret | |
186 | end | |
187 | ||
188 | -- Save forward | |
189 | function MiningTurtle:saveForward() | |
190 | ||
191 | while not MiningTurtle:forward() do | |
192 | turtle.dig() | |
193 | end | |
194 | end | |
195 | ||
196 | -- Save up | |
197 | function MiningTurtle:saveUp() | |
198 | ||
199 | while not MiningTurtle:up() do | |
200 | turtle.digUp() | |
201 | end | |
202 | end | |
203 | ||
204 | -- Save down | |
205 | function MiningTurtle:saveDown() | |
206 | ||
207 | while not MiningTurtle:down() do | |
208 | turtle.digDown() | |
209 | end | |
210 | end | |
211 | ||
212 | -- 180 degree turn | |
213 | function MiningTurtle:turn() | |
214 | ||
215 | MiningTurtle:turnRight() | |
216 | MiningTurtle:turnRight() | |
217 | end | |
218 | ||
219 | -- Make a tunnel | |
220 | function MiningTurtle:tunnel( length, height, width, ret ) | |
221 | ||
222 | height = height or 2 | |
223 | width = width or 1 | |
224 | ret = ret or false | |
225 | ||
226 | -- Length loop | |
227 | ||
228 | for i = 1,length do | |
229 | ||
230 | MiningTurtle:saveForward() | |
231 | ||
232 | if i % 2 == 0 then | |
233 | -- even | |
234 | ||
235 | MiningTurtle:turnLeft() | |
236 | ||
237 | for x = 0,width do | |
238 | ||
239 | for y = 0,height do | |
240 | ||
241 | if x % 2 == 0 then | |
242 | MiningTurtle:saveUp() | |
243 | else | |
244 | MiningTurtle:saveDown() | |
245 | end | |
246 | ||
247 | end | |
248 | ||
249 | if x < width then | |
250 | MiningTurtle:saveForward() | |
251 | end | |
252 | end | |
253 | ||
254 | MiningTurtle:turnRight() | |
255 | ||
256 | else | |
257 | -- odd | |
258 | ||
259 | MiningTurtle:turnRight() | |
260 | ||
261 | for x = 0,width do | |
262 | ||
263 | for y = 0,height do | |
264 | ||
265 | if x % 2 == 0 then | |
266 | MiningTurtle:saveUp() | |
267 | else | |
268 | MiningTurtle:saveDown() | |
269 | end | |
270 | ||
271 | end | |
272 | ||
273 | if x < width then | |
274 | MiningTurtle:saveForward() | |
275 | end | |
276 | end | |
277 | ||
278 | MiningTurtle:turnLeft() | |
279 | ||
280 | end | |
281 | ||
282 | end | |
283 | ||
284 | if ret then | |
285 | ||
286 | MiningTurtle:turn() | |
287 | end | |
288 | ||
289 | end | |
290 | ||
291 | ||
292 | -- | |
293 | function MiningTurtle:matrix( rows, cols ) | |
294 | ||
295 | ||
296 | ||
297 | end | |
298 | ||
299 | - | MiningTurtle:requiredRefuel() |
299 | + | |
300 | -- main | |
301 | ||
302 | while not MiningTurtle:requiredRefuel() do | |
303 | print("Waiting for MiningTurtle to have enough fuel") | |
304 | end | |
305 | ||
306 | MiningTurtle:toGround() | |
307 | MiningTurtle:tunnel(10) |