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 | if refuelLevel == nil then | |
| 16 | refuelLevel = 25 | |
| 17 | end | |
| 18 | ||
| 19 | if requiredFuelLevel == nil then | |
| 20 | requiredFuelLevel = 500 | |
| 21 | end | |
| 22 | ||
| 23 | -- should we refuel? | |
| 24 | if turtle.getFuelLevel() <= refuelLevel then | |
| 25 | ||
| 26 | -- iterate through our inventory | |
| 27 | for slot = 1,16 do | |
| 28 | ||
| 29 | -- select slot | |
| 30 | turtle.select(slot) | |
| 31 | ||
| 32 | -- refuel from active slot... | |
| 33 | if turtle.refuel( turtle.getItemCount( slot ) ) then | |
| 34 | ||
| 35 | end | |
| 36 | ||
| 37 | -- ...and check again | |
| 38 | if turtle.getFuelLevel() >= requiredFuelLevel then | |
| 39 | return true | |
| 40 | end | |
| 41 | ||
| 42 | end | |
| 43 | ||
| 44 | -- check if we did not refueled enough alreay | |
| 45 | if not (turtle.getFuelLevel() >= requiredFuelLevel) then | |
| 46 | -- if yes, go back to base | |
| 47 | ||
| 48 | MiningTurtle:toBase() | |
| 49 | -- refuel: to be implemented!!!! | |
| 50 | end | |
| 51 | ||
| 52 | return false | |
| 53 | end | |
| 54 | ||
| 55 | return true | |
| 56 | end | |
| 57 | ||
| 58 | -- Go back to the base of the MiningTurtle | |
| 59 | function MiningTurtle:toBase( ) | |
| 60 | ||
| 61 | end | |
| 62 | ||
| 63 | -- Go to ground | |
| 64 | function MiningTurtle:toGround( ) | |
| 65 | ||
| 66 | while not turtle.detectDown() do | |
| 67 | MiningTurtle:down() | |
| 68 | end | |
| 69 | ||
| 70 | end | |
| 71 | ||
| 72 | -- Go the our "base" Ground (z = 0) | |
| 73 | function MiningTurtle:toBaseLevel( ) | |
| 74 | ||
| 75 | while self.position.z > 0 do | |
| 76 | MiningTurtle:saveDown() | |
| 77 | end | |
| 78 | ||
| 79 | end | |
| 80 | ||
| 81 | -- forward | |
| 82 | function MiningTurtle:forward() | |
| 83 | local ret | |
| 84 | ||
| 85 | ret = turtle.forward() | |
| 86 | ||
| 87 | if ret then | |
| 88 | ||
| 89 | ||
| 90 | if self.direction == 0 then | |
| 91 | ||
| 92 | self.position.x = self.position.x + 1 | |
| 93 | ||
| 94 | elseif self.direction == 90 then | |
| 95 | ||
| 96 | self.position.y = self.position.y + 1 | |
| 97 | ||
| 98 | elseif self.direction == 180 then | |
| 99 | ||
| 100 | self.position.x = self.position.x - 1 | |
| 101 | ||
| 102 | elseif self.direction == 270 then | |
| 103 | ||
| 104 | self.position.y = self.position.y - 1 | |
| 105 | ||
| 106 | end | |
| 107 | ||
| 108 | end | |
| 109 | ||
| 110 | MiningTurtle:outPos() | |
| 111 | ||
| 112 | return ret | |
| 113 | end | |
| 114 | ||
| 115 | -- back | |
| 116 | function MiningTurtle:back() | |
| 117 | local ret | |
| 118 | ||
| 119 | ret = turtle.back() | |
| 120 | ||
| 121 | if ret then | |
| 122 | ||
| 123 | if self.direction == 0 then | |
| 124 | ||
| 125 | self.position.x = self.position.x - 1 | |
| 126 | ||
| 127 | elseif self.direction == 90 then | |
| 128 | ||
| 129 | self.position.y = self.position.y - 1 | |
| 130 | ||
| 131 | elseif self.direction == 180 then | |
| 132 | ||
| 133 | self.position.x = self.position.x + 1 | |
| 134 | ||
| 135 | elseif self.direction == 270 then | |
| 136 | ||
| 137 | self.position.y = self.position.y + 1 | |
| 138 | ||
| 139 | end | |
| 140 | ||
| 141 | end | |
| 142 | ||
| 143 | MiningTurtle:outPos() | |
| 144 | ||
| 145 | return ret | |
| 146 | end | |
| 147 | ||
| 148 | -- up | |
| 149 | function MiningTurtle:up() | |
| 150 | local ret | |
| 151 | ||
| 152 | ret = turtle.up() | |
| 153 | ||
| 154 | if ret then | |
| 155 | self.position.z = self.position.z + 1 | |
| 156 | end | |
| 157 | ||
| 158 | MiningTurtle:outPos() | |
| 159 | ||
| 160 | return ret | |
| 161 | end | |
| 162 | ||
| 163 | -- down | |
| 164 | function MiningTurtle:down() | |
| 165 | local ret | |
| 166 | ||
| 167 | ret = turtle.down() | |
| 168 | ||
| 169 | if ret then | |
| 170 | self.position.z = self.position.z - 1 | |
| 171 | end | |
| 172 | ||
| 173 | MiningTurtle:outPos() | |
| 174 | ||
| 175 | return ret | |
| 176 | end | |
| 177 | ||
| 178 | -- turn Left | |
| 179 | function MiningTurtle:turnLeft() | |
| 180 | local ret | |
| 181 | ||
| 182 | ret = turtle.turnLeft() | |
| 183 | ||
| 184 | if ret then | |
| 185 | self.direction = self.direction + 90 | |
| 186 | ||
| 187 | while self.direction >= 360 do | |
| 188 | self.direction = self.direction - 360 | |
| 189 | end | |
| 190 | end | |
| 191 | ||
| 192 | MiningTurtle:outPos() | |
| 193 | ||
| 194 | return ret | |
| 195 | end | |
| 196 | ||
| 197 | -- turn Right | |
| 198 | function MiningTurtle:turnRight() | |
| 199 | local ret | |
| 200 | ||
| 201 | ret = turtle.turnRight() | |
| 202 | ||
| 203 | if ret then | |
| 204 | self.direction = self.direction - 90 | |
| 205 | ||
| 206 | while self.direction < 0 do | |
| 207 | self.direction = self.direction + 360 | |
| 208 | end | |
| 209 | end | |
| 210 | ||
| 211 | MiningTurtle:outPos() | |
| 212 | ||
| 213 | return ret | |
| 214 | end | |
| 215 | ||
| 216 | -- Save forward | |
| 217 | function MiningTurtle:saveForward() | |
| 218 | ||
| 219 | while not MiningTurtle:forward() do | |
| 220 | turtle.dig() | |
| 221 | end | |
| 222 | end | |
| 223 | ||
| 224 | -- Save up | |
| 225 | function MiningTurtle:saveUp() | |
| 226 | ||
| 227 | while not MiningTurtle:up() do | |
| 228 | turtle.digUp() | |
| 229 | end | |
| 230 | end | |
| 231 | ||
| 232 | -- Save down | |
| 233 | function MiningTurtle:saveDown() | |
| 234 | ||
| 235 | while not MiningTurtle:down() do | |
| 236 | turtle.digDown() | |
| 237 | end | |
| 238 | end | |
| 239 | ||
| 240 | -- 180 degree turn | |
| 241 | function MiningTurtle:turn() | |
| 242 | ||
| 243 | MiningTurtle:turnRight() | |
| 244 | MiningTurtle:turnRight() | |
| 245 | end | |
| 246 | ||
| 247 | -- output pos data | |
| 248 | function MiningTurtle:outPos() | |
| 249 | ||
| 250 | - | print("Pos: " .. self.position.x .. "|" .. self.position.y .. "|" .. self.position.z .. " o: " .. self.orientation .. " f: " .. turtle.getFuelLevel() )
|
| 250 | + | print("Pos: " .. tostring(self.position.x) .. "|" .. tostring(self.position.y) .. "|" .. tostring(self.position.z) .. " o: " .. tostring(self.orientation) .. " f: " .. tostring(turtle.getFuelLevel()) )
|
| 251 | ||
| 252 | end | |
| 253 | ||
| 254 | -- Turn to orientation | |
| 255 | function MiningTurtle:turnTo( newOrientation ) | |
| 256 | while self.orientation ~= newOrientation do | |
| 257 | if math.abs(self.orientation - newOrientation) < 180 then | |
| 258 | MiningTurtle:turnRight() | |
| 259 | else | |
| 260 | MiningTurtle:turnLeft() | |
| 261 | end | |
| 262 | end | |
| 263 | end | |
| 264 | ||
| 265 | -- Go to | |
| 266 | function MiningTurtle:goTo( x, y, z ) | |
| 267 | print("GoTo: " .. x .. "|" .. y .. "|" .. z)
| |
| 268 | ||
| 269 | ||
| 270 | while not ( (x - self.position.x) == 0 and (y - self.position.y) == 0 and (z - self.position.z) == 0 ) do | |
| 271 | ||
| 272 | if (x - self.position.x) < 0 then | |
| 273 | MiningTurtle:turnTo(0) | |
| 274 | MiningTurtle:saveForward() | |
| 275 | elseif (x - self.position.x) > 0 then | |
| 276 | MiningTurtle:turnTo(180) | |
| 277 | MiningTurtle:saveForward() | |
| 278 | end | |
| 279 | ||
| 280 | if (y - self.position.y) < 0 then | |
| 281 | MiningTurtle:turnTo(90) | |
| 282 | MiningTurtle:saveForward() | |
| 283 | elseif (y - self.position.y) > 0 then | |
| 284 | MiningTurtle:turnTo(180) | |
| 285 | MiningTurtle:saveForward() | |
| 286 | end | |
| 287 | ||
| 288 | if (z - self.position.z) < 0 then | |
| 289 | MiningTurtle:saveDown() | |
| 290 | elseif (z - self.position.z) > 0 then | |
| 291 | MiningTurtle:saveUp() | |
| 292 | end | |
| 293 | ||
| 294 | end | |
| 295 | ||
| 296 | end | |
| 297 | ||
| 298 | -- Make a tunnel | |
| 299 | function MiningTurtle:tunnel( length, height, width, ret ) | |
| 300 | ||
| 301 | -- default values for parameters, only length is required (!) | |
| 302 | if height == nil then | |
| 303 | height = 2 | |
| 304 | end | |
| 305 | ||
| 306 | if width == nil then | |
| 307 | width = 1 | |
| 308 | end | |
| 309 | ||
| 310 | if ret == nil then | |
| 311 | ret = false | |
| 312 | end | |
| 313 | ||
| 314 | -- Save our current positon back for later return | |
| 315 | ||
| 316 | returnPos = self.position | |
| 317 | returnOrientation = self.orientation | |
| 318 | ||
| 319 | -- Place the turtle at the lower left corner of the new tunnel (so that we were starting from the middle of the new tunnel) | |
| 320 | -- We only need this, if we have a tonnel with a width > 2 (<=2 means we are already on the right position) | |
| 321 | ||
| 322 | if width > 2 then | |
| 323 | ||
| 324 | MiningTurtle:turnLeft() | |
| 325 | ||
| 326 | for i = 0,math.floor( width / 2 ) do | |
| 327 | MiningTurtle:saveForward() | |
| 328 | end | |
| 329 | ||
| 330 | MiningTurtle:turnRight() | |
| 331 | end | |
| 332 | ||
| 333 | -- Length loop | |
| 334 | ||
| 335 | for i = 0,length-1 do | |
| 336 | ||
| 337 | MiningTurtle:saveForward() | |
| 338 | ||
| 339 | if i % 2 == 0 then | |
| 340 | -- even | |
| 341 | ||
| 342 | -- only change direction if we mine a tunnel wider than 1 block | |
| 343 | if width > 1 then | |
| 344 | MiningTurtle:turnRight() | |
| 345 | end | |
| 346 | ||
| 347 | for x = 0,width-1 do | |
| 348 | ||
| 349 | MiningTurtle:tick() | |
| 350 | ||
| 351 | for y = 0,height-3 do | |
| 352 | ||
| 353 | if x % 2 == 0 then | |
| 354 | MiningTurtle:saveUp() | |
| 355 | else | |
| 356 | MiningTurtle:saveDown() | |
| 357 | end | |
| 358 | ||
| 359 | end | |
| 360 | ||
| 361 | turtle.digUp() | |
| 362 | ||
| 363 | if x < width-1 then | |
| 364 | MiningTurtle:saveForward() | |
| 365 | end | |
| 366 | end | |
| 367 | ||
| 368 | -- same as above | |
| 369 | if width > 1 then | |
| 370 | MiningTurtle:turnLeft() | |
| 371 | end | |
| 372 | ||
| 373 | else | |
| 374 | -- odd | |
| 375 | ||
| 376 | if width > 1 then | |
| 377 | MiningTurtle:turnLeft() | |
| 378 | end | |
| 379 | ||
| 380 | for x = 0,width-1 do | |
| 381 | ||
| 382 | MiningTurtle:tick() | |
| 383 | ||
| 384 | for y = 0,height-3 do | |
| 385 | ||
| 386 | if x % 2 == 1 or width % 2 == 0 then | |
| 387 | MiningTurtle:saveUp() | |
| 388 | else | |
| 389 | MiningTurtle:saveDown() | |
| 390 | end | |
| 391 | ||
| 392 | end | |
| 393 | ||
| 394 | turtle.digUp() | |
| 395 | ||
| 396 | if x < width-1 then | |
| 397 | MiningTurtle:saveForward() | |
| 398 | end | |
| 399 | end | |
| 400 | ||
| 401 | if width > 1 then | |
| 402 | MiningTurtle:turnRight() | |
| 403 | end | |
| 404 | ||
| 405 | end | |
| 406 | ||
| 407 | end | |
| 408 | ||
| 409 | if ret then | |
| 410 | ||
| 411 | -- Turn around (180°) | |
| 412 | MiningTurtle:turn() | |
| 413 | ||
| 414 | -- Go up to not remove torches on the floow | |
| 415 | MiningTurtle.saveUp() | |
| 416 | ||
| 417 | MiningTurtle.goTo( returnPos.x, returnPos.y, returnPos.z ) | |
| 418 | ||
| 419 | -- Turn around again | |
| 420 | MiningTurtle:turnTo( returnOrientation ) | |
| 421 | ||
| 422 | end | |
| 423 | ||
| 424 | end | |
| 425 | ||
| 426 | function MiningTurtle:tick() | |
| 427 | ||
| 428 | -- refuel loop | |
| 429 | while not MiningTurtle:requiredRefuel( 20, 500 ) do | |
| 430 | sleep(1) | |
| 431 | end | |
| 432 | end | |
| 433 | ||
| 434 | -- | |
| 435 | function MiningTurtle:matrix( rows, cols ) | |
| 436 | ||
| 437 | MiningTurtle:tick() | |
| 438 | ||
| 439 | MiningTurtle:toBaseLevel() | |
| 440 | ||
| 441 | MiningTurtle:tunnel(30, 2, 1, true) | |
| 442 | ||
| 443 | ||
| 444 | end | |
| 445 | ||
| 446 | ||
| 447 | ||
| 448 | ||
| 449 | ||
| 450 | -- main | |
| 451 | MiningTurtle:matrix( 10, 10 ) |