SHOW:
|
|
- or go back to the newest paste.
| 1 | - | -- Function to set the heading of the turtle -- |
| 1 | + | --[[ |
| 2 | Feel free to use any code or functions in the following paste under the condition | |
| 3 | that credit is given where credit is due. | |
| 4 | All code has been made by minecraft:montana_1 | |
| 5 | For comments or concerns, please email [email protected] | |
| 6 | ||
| 7 | Thanks! | |
| 8 | ]]-- | |
| 9 | - | local heading = "" |
| 9 | + | |
| 10 | - | if(turtle.detect()) then |
| 10 | + | --[[ |
| 11 | Code to-do list: | |
| 12 | *Auto chest deposit | |
| 13 | *Auto torch placement | |
| 14 | *Rednet flags | |
| 15 | *Main tunnel function | |
| 16 | *Finish branch function (located in another paste "CMining Rev 2" currently) | |
| 17 | *Various others | |
| 18 | Newest Features: | |
| 19 | *Ore priorities | |
| 20 | ]]-- | |
| 21 | ||
| 22 | -- Function to split string values into tables -- | |
| 23 | function split(inputstr, sep) | |
| 24 | if(inputstr == nil or inputstr == "") then | |
| 25 | return nil | |
| 26 | end | |
| 27 | if sep == nil then | |
| 28 | sep = "," | |
| 29 | end | |
| 30 | local t={} ; i=1
| |
| 31 | for str in string.gmatch(inputstr, "([^"..sep.."]+)") do | |
| 32 | t[i] = str | |
| 33 | i = i + 1 | |
| 34 | end | |
| 35 | return t | |
| 36 | end | |
| 37 | ||
| 38 | -- Function to add elements to priorities list -- | |
| 39 | function addList(name,mine,flag,drop,vein) | |
| 40 | if(fs.exists("listed")) then
| |
| 41 | xlist = fs.open("listed","r")
| |
| 42 | local x = split(xlist.readLine()) | |
| 43 | ||
| 44 | while(x[1] ~= nil) do | |
| 45 | if(name == x[1]) then | |
| 46 | return false | |
| 47 | end | |
| 48 | s = xlist.readLine() | |
| 49 | if(s ~= nil) then | |
| 50 | x = split(s) | |
| 51 | else | |
| 52 | break | |
| 53 | end | |
| 54 | end | |
| 55 | ||
| 56 | xlist.close() | |
| 57 | xlist = fs.open("listed","a")
| |
| 58 | else | |
| 59 | xlist = fs.open("listed","w")
| |
| 60 | end | |
| 61 | xlist.writeLine(name..","..mine..","..flag..","..drop..","..vein) | |
| 62 | xlist.close() | |
| 63 | return true | |
| 64 | end | |
| 65 | ||
| 66 | -- Function to get priorities list in file format as a list -- | |
| 67 | function lstToTable() | |
| 68 | if(fs.exists("listed")) then
| |
| 69 | ifile = fs.open("listed","r")
| |
| 70 | xlist = {}
| |
| 71 | x = split(ifile.readLine()) | |
| 72 | while(x ~= nil and x ~= "") do | |
| 73 | table.insert(xlist, x) | |
| 74 | x = split(ifile.readLine()) | |
| 75 | end | |
| 76 | else | |
| 77 | -- name,mine,flag,drop,vein | |
| 78 | xlist = {
| |
| 79 | {'iron_ore','1','0','0','1'},
| |
| 80 | {'coal_ore','1','0','0','1'},
| |
| 81 | - | -- Function to get the heading of the turtle -- |
| 81 | + | {'gold_ore','1','0','0','1'},
|
| 82 | {'lapis_ore','1','0','0','1'},
| |
| 83 | {'redstone_ore','1','0','0','1'},
| |
| 84 | {'lit_redstone_ore','1','0','0','1'},
| |
| 85 | - | error("Insufficient fuel")
|
| 85 | + | {'diamond_ore','1','0','0','1'},
|
| 86 | {'quartz_ore','1','0','0','1'},
| |
| 87 | {'emerald_ore','1','0','0','1'}
| |
| 88 | - | local heading = "" |
| 88 | + | } |
| 89 | - | if(turtle.detect()) then |
| 89 | + | for k,v in pairs(xlist) do |
| 90 | addList(v[1],v[2],v[3],v[4],v[5]) | |
| 91 | end | |
| 92 | end | |
| 93 | return xlist | |
| 94 | end | |
| 95 | ||
| 96 | -- Function to clear turtle inventory according to priorities list -- | |
| 97 | function clrInv(xtable) | |
| 98 | for i = 1, 16 do -- loop through the slots | |
| 99 | turtle.select(i) | |
| 100 | local x = turtle.getItemDetail(i) | |
| 101 | if(x ~= nil) then | |
| 102 | local istr = string.sub(x.name,string.find(x.name,":",0)+1) | |
| 103 | for key,value in pairs(xtable) do | |
| 104 | if(istr == value[1] and value[4] == "1") then | |
| 105 | turtle.drop() | |
| 106 | end | |
| 107 | end | |
| 108 | end | |
| 109 | end | |
| 110 | turtle.select(1) | |
| 111 | end | |
| 112 | ||
| 113 | - | local heading = getHeading() |
| 113 | + | -- Function to sort turtle inventory -- |
| 114 | function sortInv() | |
| 115 | for i = 1, 16 do -- loop through the slots | |
| 116 | turtle.select(i) | |
| 117 | if(turtle.getItemDetail(i) ~= nil) then | |
| 118 | for c = i, 16 do | |
| 119 | if(turtle.getItemDetail(c) ~= nil) then | |
| 120 | if(turtle.compareTo(c)) then | |
| 121 | turtle.select(c) | |
| 122 | turtle.transferTo(i) | |
| 123 | turtle.select(i) | |
| 124 | - | if(turtle.detectUp()) then |
| 124 | + | |
| 125 | end | |
| 126 | end | |
| 127 | end | |
| 128 | end | |
| 129 | - | if(turtle.detectDown()) then |
| 129 | + | |
| 130 | end | |
| 131 | ||
| 132 | -- Function to check for turtle inventory space -- | |
| 133 | function invSpace() | |
| 134 | for i = 1, 16 do | |
| 135 | turtle.select(i) | |
| 136 | local x = turtle.getItemDetail(i) | |
| 137 | if(x == nil or turtle.compare()) then | |
| 138 | turtle.select(1) | |
| 139 | return true | |
| 140 | end | |
| 141 | end | |
| 142 | turtle.select(1) | |
| 143 | return false | |
| 144 | end | |
| 145 | - | if(turtle.detect()) then |
| 145 | + | |
| 146 | -- Function to check for sufficient turtle fuel -- | |
| 147 | function sufficientFuel(vector1,vector2,fuel,remainder) | |
| 148 | ||
| 149 | local distance = math.abs(vector1.x - vector2.x) + math.abs(vector1.y - vector2.y) + math.abs(vector1.z - vector2.z) | |
| 150 | if(fuel <= distance + remainder) then | |
| 151 | return false | |
| 152 | else | |
| 153 | return true | |
| 154 | end | |
| 155 | end | |
| 156 | ||
| 157 | -- Function to consume turtle fuel -- | |
| 158 | function consumeFuel(maxFuel) -- Optionally add more fuel types and prioritise fuel types | |
| 159 | local refuel = false | |
| 160 | - | if(turtle.detect()) then |
| 160 | + | |
| 161 | turtle.select(i) -- change to the slot | |
| 162 | local x = turtle.getItemDetail(i) | |
| 163 | if(x ~= nil) then | |
| 164 | local istr = string.sub(x.name,string.find(x.name,":",0)+1) | |
| 165 | if(istr == "planks" or istr == "stick" or istr == "log") then | |
| 166 | turtle.refuel() | |
| 167 | refuel = true | |
| 168 | end | |
| 169 | if(turtle.getFuelLevel() < maxFuel and istr == "coal") then | |
| 170 | - | -- Function to check if turtle has sufficient fuel -- |
| 170 | + | |
| 171 | refuel = true | |
| 172 | end | |
| 173 | end | |
| 174 | end | |
| 175 | turtle.select(1) | |
| 176 | --print(turtle.getFuelLevel()) | |
| 177 | return refuel | |
| 178 | end | |
| 179 | ||
| 180 | -- Function to place torch -- | |
| 181 | function placeTorch(direction) -- Optionally add functionality to place torch when no torch can be placed | |
| 182 | - | -- Function to refuel turtle appropriately -- |
| 182 | + | local torch = false |
| 183 | for i = 1, 16 do -- loop through the slots | |
| 184 | - | function consumeFuel(maxFuel) |
| 184 | + | turtle.select(i) -- change to the slot |
| 185 | local x = turtle.getItemDetail(i) | |
| 186 | if(x ~= nil) then | |
| 187 | local istr = string.sub(x.name,string.find(x.name,":",0)+1) | |
| 188 | if(istr == "torch") then | |
| 189 | if(direction == "up") then | |
| 190 | if(turtle.placeUp()) then | |
| 191 | - | print(istr," t") |
| 191 | + | torch = true |
| 192 | turtle.select(1) | |
| 193 | end | |
| 194 | elseif(direction == "down")then | |
| 195 | if(turtle.placeDown()) then | |
| 196 | torch = true | |
| 197 | turtle.select(1) | |
| 198 | end | |
| 199 | else | |
| 200 | if(turtle.place()) then | |
| 201 | torch = true | |
| 202 | turtle.select(1) | |
| 203 | - | print(turtle.getFuelLevel()) |
| 203 | + | |
| 204 | end | |
| 205 | end | |
| 206 | end | |
| 207 | end | |
| 208 | - | -- Function to check for inv space -- |
| 208 | + | |
| 209 | return torch | |
| 210 | end | |
| 211 | ||
| 212 | -- Function to get turtle heading -- | |
| 213 | function getHeading() | |
| 214 | if(turtle.getFuelLevel() < 2) then | |
| 215 | if(not consumeFuel(400)) then | |
| 216 | error("Insufficient fuel")
| |
| 217 | end | |
| 218 | end | |
| 219 | local start = vector.new(gps.locate()) | |
| 220 | while(turtle.detect()) do | |
| 221 | turtle.dig() | |
| 222 | end | |
| 223 | turtle.forward() | |
| 224 | - | -- Function to sort inventory -- |
| 224 | + | |
| 225 | turtle.back() | |
| 226 | if(start.z - current.z > 0) then | |
| 227 | heading = 'N' | |
| 228 | elseif (start.z - current.z < 0) then | |
| 229 | heading = 'S' | |
| 230 | end | |
| 231 | if(start.x - current.x > 0) then | |
| 232 | heading = 'W' | |
| 233 | elseif (start.x - current.x < 0) then | |
| 234 | heading = 'E' | |
| 235 | end | |
| 236 | return heading | |
| 237 | end | |
| 238 | ||
| 239 | -- Function to set turtle heading -- | |
| 240 | function setHeading(heading,newHeading) | |
| 241 | if(heading ~= 'N' and heading ~= 'n' and heading ~= 'E' and heading ~= 'e' and heading ~= 'S' and heading ~= 's' and heading ~= 'W' and heading ~= 'w') then | |
| 242 | if(turtle.getFuelLevel() < 2) then | |
| 243 | error("Insufficient fuel")
| |
| 244 | end | |
| 245 | - | -- Function to mine entire ore veins -- |
| 245 | + | |
| 246 | while(turtle.detect()) do | |
| 247 | - | function mineVein(start,moves,back) |
| 247 | + | |
| 248 | end | |
| 249 | turtle.forward() | |
| 250 | local current = vector.new(gps.locate()) | |
| 251 | - | return false |
| 251 | + | |
| 252 | if(start.z - current.z > 0) then | |
| 253 | heading = 'N' | |
| 254 | elseif (start.z - current.z < 0) then | |
| 255 | heading = 'S' | |
| 256 | end | |
| 257 | - | return false |
| 257 | + | |
| 258 | heading = 'W' | |
| 259 | elseif (start.x - current.x < 0) then | |
| 260 | heading = 'E' | |
| 261 | end | |
| 262 | end | |
| 263 | ||
| 264 | if(heading ~= newHeading) then | |
| 265 | - | print(istr) |
| 265 | + | |
| 266 | - | if(istr == "iron_ore") then |
| 266 | + | |
| 267 | turtle.turnLeft() | |
| 268 | turtle.turnLeft() | |
| 269 | - | mineVein(start,moves+1,false) |
| 269 | + | |
| 270 | if(heading == 'E' or heading == 'e') then | |
| 271 | turtle.turnLeft() | |
| 272 | end | |
| 273 | if(heading == 'W' or heading == 'w') then | |
| 274 | turtle.turnRight() | |
| 275 | end | |
| 276 | end | |
| 277 | if(newHeading == 'E' or newHeading == 'e') then | |
| 278 | if(heading == 'S' or heading == 's') then | |
| 279 | turtle.turnLeft() | |
| 280 | - | print(istr) |
| 280 | + | |
| 281 | - | if(istr == "iron_ore") then |
| 281 | + | |
| 282 | turtle.turnRight() | |
| 283 | end | |
| 284 | - | mineVein(start,moves+1,false) |
| 284 | + | |
| 285 | turtle.turnRight() | |
| 286 | turtle.turnRight() | |
| 287 | end | |
| 288 | end | |
| 289 | if(newHeading == 'S' or newHeading == 's') then | |
| 290 | if(heading == 'N' or heading == 'n') then | |
| 291 | turtle.turnLeft() | |
| 292 | turtle.turnLeft() | |
| 293 | - | print(istr) |
| 293 | + | |
| 294 | - | if(istr == "iron_ore") then |
| 294 | + | |
| 295 | turtle.turnRight() | |
| 296 | end | |
| 297 | - | mineVein(start,moves+1,false) |
| 297 | + | |
| 298 | turtle.turnLeft() | |
| 299 | end | |
| 300 | end | |
| 301 | if(newHeading == 'W' or newHeading == 'w') then | |
| 302 | if(heading == 'S' or heading == 's') then | |
| 303 | turtle.turnRight() | |
| 304 | end | |
| 305 | - | print(istr) |
| 305 | + | |
| 306 | - | if(istr == "iron_ore") then |
| 306 | + | |
| 307 | turtle.turnLeft() | |
| 308 | end | |
| 309 | - | mineVein(start,moves+1,false) |
| 309 | + | |
| 310 | turtle.turnLeft() | |
| 311 | end | |
| 312 | end | |
| 313 | end | |
| 314 | end | |
| 315 | ||
| 316 | -- Function to go to a specific location -- | |
| 317 | - | print(istr) |
| 317 | + | |
| 318 | - | if(istr == "iron_ore") then |
| 318 | + | |
| 319 | heading = getHeading() | |
| 320 | end | |
| 321 | - | mineVein(start,moves+1,true) |
| 321 | + | |
| 322 | local current = vector.new(gps.locate()) | |
| 323 | if(location.x ~= current.x or location.y ~= current.y or location.z ~= current.z) then | |
| 324 | if(turtle.getFuelLevel() < 2) then | |
| 325 | - | -- |
| 325 | + | |
| 326 | end | |
| 327 | end | |
| 328 | while(location.y ~= current.y) do | |
| 329 | - | print(istr) |
| 329 | + | |
| 330 | - | if(istr == "iron_ore") then |
| 330 | + | while(turtle.detectUp()) do |
| 331 | turtle.digUp() | |
| 332 | end | |
| 333 | - | mineVein(start,moves+1,true) |
| 333 | + | |
| 334 | else | |
| 335 | while(turtle.detectDown()) do | |
| 336 | turtle.digDown() | |
| 337 | - | -- |
| 337 | + | |
| 338 | turtle.down() | |
| 339 | end | |
| 340 | current = vector.new(gps.locate()) | |
| 341 | end | |
| 342 | ||
| 343 | while(location.z ~= current.z) do | |
| 344 | - | print(istr) |
| 344 | + | |
| 345 | - | if(istr == "iron_ore") then |
| 345 | + | |
| 346 | heading = 'S' | |
| 347 | elseif(location.z < current.z) then | |
| 348 | - | mineVein(start,moves+1,false) |
| 348 | + | |
| 349 | heading = 'N' | |
| 350 | end | |
| 351 | while(turtle.detect()) do | |
| 352 | turtle.dig() | |
| 353 | end | |
| 354 | turtle.forward() | |
| 355 | current = vector.new(gps.locate()) | |
| 356 | - | print(istr) |
| 356 | + | |
| 357 | - | if(istr == "iron_ore") then |
| 357 | + | |
| 358 | while(location.x ~= current.x) do | |
| 359 | if(location.x > current.x) then | |
| 360 | - | mineVein(start,moves+1,true) |
| 360 | + | |
| 361 | heading = 'E' | |
| 362 | elseif(location.x < current.x) then | |
| 363 | setHeading(heading,'W') | |
| 364 | - | -- |
| 364 | + | |
| 365 | end | |
| 366 | while(turtle.detect()) do | |
| 367 | turtle.dig() | |
| 368 | - | print(istr) |
| 368 | + | |
| 369 | - | if(istr == "iron_ore") then |
| 369 | + | |
| 370 | current = vector.new(gps.locate()) | |
| 371 | end | |
| 372 | - | mineVein(start,moves+1,true) |
| 372 | + | |
| 373 | end | |
| 374 | ||
| 375 | -- Function to decide whether to mine whole ore vein based on priorities list -- | |
| 376 | - | -- |
| 376 | + | function shouldMineWhole(istr,xlist) -- must add functionality for flagging ores |
| 377 | for key,value in pairs(xlist) do | |
| 378 | if(istr == value[1] and value[5] == "1") then | |
| 379 | return true | |
| 380 | end | |
| 381 | end | |
| 382 | - | -- Starting variables -- |
| 382 | + | |
| 383 | - | -- (These are only temporary) -- |
| 383 | + | |
| 384 | ||
| 385 | - | level = 12 |
| 385 | + | -- Function to mine entire ore pocket -- |
| 386 | function mineVein(start,moves,back,xtable) -- This function needs work on flagging ore, also could be significantly more efficient | |
| 387 | - | limit = 16 |
| 387 | + | --Establish current GPS location-- |
| 388 | local current = vector.new(gps.locate()) | |
| 389 | --Check for sufficient fuel, if not, try to refuel, if refuel fails, function return false and | |
| 390 | - | --|-----------------------|-- |
| 390 | + | --recursion tree will collapse with turtle returning to where it started-- |
| 391 | - | --| ** Main function ** |-- |
| 391 | + | |
| 392 | - | --V-----------------------V-- |
| 392 | + | |
| 393 | return -2 | |
| 394 | - | -- Check if GPS_DATA file exists -- |
| 394 | + | |
| 395 | - | if(fs.exists("GPS_DATA")) then
|
| 395 | + | |
| 396 | - | -- GPS_DATA exists -- |
| 396 | + | --Check for inventory space, if no inventory space, try to create some. if no space can be created, |
| 397 | - | -- Check validity of GPS_DATA -- |
| 397 | + | --function return false and recursion tree will collapse, with turtle returning to where it started. |
| 398 | - | gpsData = fs.open("GPS_DATA","r")
|
| 398 | + | |
| 399 | - | local start = vector.new(tonumber(gpsData.readLine()),tonumber(gpsData.readLine()),tonumber(gpsData.readLine())) |
| 399 | + | |
| 400 | - | sHeading = gpsData.readLine() |
| 400 | + | clrInv(xtable) |
| 401 | - | if(start.x == nil or start.y == nil or start.z == nil) then |
| 401 | + | |
| 402 | - | -- GPS_DATA Invalid -- |
| 402 | + | return -1 |
| 403 | - | term.clear() |
| 403 | + | |
| 404 | - | term.setCursorPos(1,1) |
| 404 | + | |
| 405 | - | error("Invalid GPS information")
|
| 405 | + | |
| 406 | --Check above turtle for ores-- | |
| 407 | - | -- GPS_DATA Valid -- |
| 407 | + | |
| 408 | - | term.clear() |
| 408 | + | |
| 409 | - | term.setCursorPos(1,1) |
| 409 | + | |
| 410 | - | print("GPS_DATA exists, start: (",start.x,",",start.y,",",start.z,")")
|
| 410 | + | --print(istr) |
| 411 | - | print("Heading: ", sHeading)
|
| 411 | + | if(shouldMineWhole(istr,xtable)) then |
| 412 | - | -- Search for GPS signal -- |
| 412 | + | |
| 413 | turtle.forward() | |
| 414 | - | if(current.x == nil or current.y == nil or current.z == nil) then |
| 414 | + | mineVein(start,moves+1,false,xtable) |
| 415 | - | -- GPS signal could not be established -- |
| 415 | + | |
| 416 | - | error("Could not establish GPS signal, please ensure GPS servers are running and try again")
|
| 416 | + | |
| 417 | - | else |
| 417 | + | |
| 418 | - | -- GPS signal established -- |
| 418 | + | if(moves == 0) then |
| 419 | - | print("GPS locate, current: (",current.x,",",current.y,",",current.z,")")
|
| 419 | + | if(current.y == start.y + 1) then |
| 420 | - | -- Ensure y level is the same as starting y level -- |
| 420 | + | local success,data = turtle.inspectUp() |
| 421 | - | if(not sufficientFuel(current,start,turtle.getFuelLevel(),5)) then |
| 421 | + | if(success) then |
| 422 | - | if(not consumeFuel(400)) then |
| 422 | + | local istr = string.sub(data.name,string.find(data.name,":",0)+1) |
| 423 | - | error("Insufficient fuel!")
|
| 423 | + | --print(istr) |
| 424 | if(shouldMineWhole(istr,xtable)) then | |
| 425 | turtle.digUp() | |
| 426 | - | local cHeading = getHeading() |
| 426 | + | turtle.up() |
| 427 | - | print("Heading: ", cHeading)
|
| 427 | + | mineVein(start,moves+1,true,xtable) |
| 428 | - | if(start.y ~= current.y) then |
| 428 | + | turtle.down() |
| 429 | - | -- if y level is not the same -- |
| 429 | + | |
| 430 | - | cHeading = goToLocation(vector.new(current.x,start.y,current.z), cHeading) |
| 430 | + | |
| 431 | - | current = vector.new(gps.locate()) |
| 431 | + | |
| 432 | if(current.y == start.y) then | |
| 433 | - | end |
| 433 | + | local success,data = turtle.inspectDown() |
| 434 | - | -- Ensure z level is the same as starting y level -- |
| 434 | + | if(success) then |
| 435 | - | if(sHeading == 'N' or sHeading == 'n' or sHeading == 'S' or sHeading == 's') then |
| 435 | + | local istr = string.sub(data.name,string.find(data.name,":",0)+1) |
| 436 | - | if(start.z ~= current.z) then |
| 436 | + | --print(istr) |
| 437 | - | -- if z level is not the same -- |
| 437 | + | if(shouldMineWhole(istr,xtable)) then |
| 438 | - | cHeading = goToLocation(vector.new(start.x,current.y,current.z), cHeading) |
| 438 | + | turtle.digDown() |
| 439 | - | current = vector.new(gps.locate()) |
| 439 | + | turtle.down() |
| 440 | - | end |
| 440 | + | mineVein(start,moves+1,true,xtable) |
| 441 | - | end |
| 441 | + | turtle.up() |
| 442 | - | -- Ensure x level is the same as starting y level -- |
| 442 | + | |
| 443 | - | if(sHeading == 'E' or sHeading == 'e' or sHeading == 'W' or sHeading == 'w') then |
| 443 | + | |
| 444 | - | if(start.x ~= current.x) then |
| 444 | + | |
| 445 | - | -- if x level is not the same -- |
| 445 | + | |
| 446 | - | cHeading = goToLocation(vector.new(current.x,current.y,start.z), cHeading) |
| 446 | + | |
| 447 | - | current = vector.new(gps.locate()) |
| 447 | + | --will ensure turtle does not check sides on start. |
| 448 | - | end |
| 448 | + | |
| 449 | - | end |
| 449 | + | return 1 |
| 450 | - | setHeading(cHeading,sHeading) |
| 450 | + | |
| 451 | - | cHeading = sHeading |
| 451 | + | |
| 452 | - | done = false |
| 452 | + | |
| 453 | - | while(done == false) do |
| 453 | + | |
| 454 | - | current = vector.new(gps.locate()) |
| 454 | + | |
| 455 | - | if(current.x == nil or current.y == nil or current.z == nil) then |
| 455 | + | --print(istr) |
| 456 | - | -- GPS signal could not be established -- |
| 456 | + | if(shouldMineWhole(istr,xtable)) then |
| 457 | - | print("Could not establish GPS signal, please ensure GPS servers are running and try again")
|
| 457 | + | |
| 458 | - | error() |
| 458 | + | |
| 459 | - | else |
| 459 | + | mineVein(start,moves+1,false,xtable) |
| 460 | - | -- Ensure distance from start is less than limit -- |
| 460 | + | |
| 461 | - | -- Along the North/South line -- |
| 461 | + | |
| 462 | - | if(sHeading == 'N' or sHeading == 'n' or sHeading == 'S' or sHeading == 's')then |
| 462 | + | |
| 463 | - | -- Check if distance from start is within limit -- |
| 463 | + | |
| 464 | - | if(math.abs(start.z - current.z)>limit) then |
| 464 | + | |
| 465 | - | -- Distance from start is not within limit -- |
| 465 | + | |
| 466 | - | cHeading = goToLocation(vector.new(start.x,start.y,current.z),cHeading) |
| 466 | + | |
| 467 | - | cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading) |
| 467 | + | |
| 468 | - | done = true |
| 468 | + | --print(istr) |
| 469 | - | else |
| 469 | + | if(shouldMineWhole(istr,xtable)) then |
| 470 | - | -- Distance from start is within limit -- |
| 470 | + | |
| 471 | - | -- Check for sufficient fuel -- |
| 471 | + | |
| 472 | - | if(sufficientFuel(current,start,turtle.getFuelLevel(),5)) then |
| 472 | + | mineVein(start,moves+1,false,xtable) |
| 473 | - | if(invSpace()) then |
| 473 | + | |
| 474 | - | if(current.y == start.y) then |
| 474 | + | |
| 475 | - | --checkSides() |
| 475 | + | |
| 476 | - | if(turtle.detect()) then |
| 476 | + | |
| 477 | - | turtle.dig() |
| 477 | + | |
| 478 | - | end |
| 478 | + | |
| 479 | - | turtle.forward() |
| 479 | + | |
| 480 | - | --checkSides() |
| 480 | + | --print(istr) |
| 481 | - | if(turtle.detectUp()) then |
| 481 | + | if(shouldMineWhole(istr,xtable)) then |
| 482 | - | turtle.digUp() |
| 482 | + | |
| 483 | - | end |
| 483 | + | |
| 484 | - | turtle.up() |
| 484 | + | mineVein(start,moves+1,false,xtable) |
| 485 | - | elseif(current.y - 1 == start.y) then |
| 485 | + | |
| 486 | - | --checkSides() |
| 486 | + | |
| 487 | - | if(turtle.detect()) then |
| 487 | + | |
| 488 | - | turtle.dig() |
| 488 | + | |
| 489 | - | end |
| 489 | + | |
| 490 | - | turtle.forward() |
| 490 | + | |
| 491 | - | --checkSides() |
| 491 | + | |
| 492 | - | if(turtle.detectDown()) then |
| 492 | + | --print(istr) |
| 493 | - | turtle.digDown() |
| 493 | + | if(shouldMineWhole(istr,xtable)) then |
| 494 | - | end |
| 494 | + | |
| 495 | - | turtle.down() |
| 495 | + | |
| 496 | - | else |
| 496 | + | mineVein(start,moves+1,true,xtable) |
| 497 | - | cHeading = goToLocation(vector.new(current.x,start.y,current.z),cHeading) |
| 497 | + | |
| 498 | - | end |
| 498 | + | |
| 499 | - | else |
| 499 | + | |
| 500 | - | sortInv() |
| 500 | + | |
| 501 | - | if(not invSpace()) then |
| 501 | + | |
| 502 | - | cHeading = goToLocation(vector.new(start.x,start.y,current.z),cHeading) |
| 502 | + | |
| 503 | - | cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading) |
| 503 | + | --print(istr) |
| 504 | - | done = true |
| 504 | + | if(shouldMineWhole(istr,xtable)) then |
| 505 | - | end |
| 505 | + | |
| 506 | turtle.down() | |
| 507 | mineVein(start,moves+1,true,xtable) | |
| 508 | - | if(not consumeFuel(400)) then |
| 508 | + | |
| 509 | - | cHeading = goToLocation(vector.new(start.x,start.y,current.z),cHeading) |
| 509 | + | |
| 510 | - | cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading) |
| 510 | + | |
| 511 | - | done = true |
| 511 | + | |
| 512 | turtle.turnRight() | |
| 513 | turtle.turnRight() | |
| 514 | - | end |
| 514 | + | |
| 515 | - | -- Along the East/West line -- |
| 515 | + | |
| 516 | - | elseif(sHeading == 'E' or sHeading == 'e' or sHeading == 'W' or sHeading == 'w') then |
| 516 | + | |
| 517 | - | -- Check if distance from start is within limit -- |
| 517 | + | --print(istr) |
| 518 | - | if(math.abs(start.x - current.x)>limit) then |
| 518 | + | if(shouldMineWhole(istr,xtable)) then |
| 519 | - | -- Distance from start is not within limit -- |
| 519 | + | |
| 520 | - | cHeading = goToLocation(vector.new(current.x,start.y,start.z),cHeading) |
| 520 | + | |
| 521 | - | cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading) |
| 521 | + | mineVein(start,moves+1,false,xtable) |
| 522 | - | done = true |
| 522 | + | |
| 523 | - | else |
| 523 | + | |
| 524 | - | -- Distance from start is within limit -- |
| 524 | + | |
| 525 | - | -- Check for sufficient fuel -- |
| 525 | + | |
| 526 | - | if(sufficientFuel(current,start,turtle.getFuelLevel(),5)) then |
| 526 | + | |
| 527 | - | if(invSpace()) then |
| 527 | + | |
| 528 | - | if(current.y == start.y) then |
| 528 | + | |
| 529 | - | --checkSides() |
| 529 | + | --print(istr) |
| 530 | - | if(turtle.detect()) then |
| 530 | + | if(shouldMineWhole(istr,xtable)) then |
| 531 | - | turtle.dig() |
| 531 | + | |
| 532 | - | end |
| 532 | + | |
| 533 | - | turtle.forward() |
| 533 | + | mineVein(start,moves+1,true,xtable) |
| 534 | - | --checkSides() |
| 534 | + | |
| 535 | - | if(turtle.detectUp()) then |
| 535 | + | |
| 536 | - | turtle.digUp() |
| 536 | + | |
| 537 | - | end |
| 537 | + | |
| 538 | - | turtle.up() |
| 538 | + | |
| 539 | - | elseif(current.y - 1 == start.y) then |
| 539 | + | |
| 540 | - | --checkSides() |
| 540 | + | --print(istr) |
| 541 | - | if(turtle.detect()) then |
| 541 | + | if(shouldMineWhole(istr,xtable)) then |
| 542 | - | turtle.dig() |
| 542 | + | |
| 543 | - | end |
| 543 | + | |
| 544 | - | turtle.forward() |
| 544 | + | mineVein(start,moves+1,true,xtable) |
| 545 | - | --checkSides() |
| 545 | + | |
| 546 | - | if(turtle.detectDown()) then |
| 546 | + | |
| 547 | - | turtle.digDown() |
| 547 | + | |
| 548 | - | end |
| 548 | + | |
| 549 | - | turtle.down() |
| 549 | + | return 1 |
| 550 | - | else |
| 550 | + | |
| 551 | - | cHeading = goToLocation(vector.new(current.x,start.y,current.z),cHeading) |
| 551 | + | |
| 552 | - | end |
| 552 | + | -- Function to mine branch -- |
| 553 | - | else |
| 553 | + | function mineBranch(branchStart,branchHeading,currentHeading,branchLimit,fuelRemainder,plist,torchLength) -- Still needs optimization and functions to replace repeated blocks |
| 554 | - | sortInv() |
| 554 | + | -- Search for GPS signal -- |
| 555 | - | if(not invSpace()) then |
| 555 | + | |
| 556 | - | cHeading = goToLocation(vector.new(current.x,start.y,start.z),cHeading) |
| 556 | + | if(current.x == nil or current.y == nil or current.z == nil) then |
| 557 | - | cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading) |
| 557 | + | -- GPS signal could not be established -- |
| 558 | - | done = true |
| 558 | + | error("Could not establish GPS signal, please ensure GPS servers are running and try again")
|
| 559 | - | end |
| 559 | + | |
| 560 | -- GPS signal established -- | |
| 561 | if(not sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then | |
| 562 | - | if(not consumeFuel(400)) then |
| 562 | + | if(not consumeFuel(4000)) then |
| 563 | - | cHeading = goToLocation(vector.new(current.x,start.y,start.z),cHeading) |
| 563 | + | error("Insufficient fuel!")
|
| 564 | - | cHeading = goToLocation(vector.new(start.x,start.y,start.z),cHeading) |
| 564 | + | |
| 565 | - | done = true |
| 565 | + | |
| 566 | if(currentHeading ~= 'N' and currentHeading ~= 'n' and currentHeading ~= 'E' and currentHeading ~= 'e' and currentHeading ~= 'S' and currentHeading ~= 's' and currentHeading ~= 'W' and currentHeading ~= 'w') then | |
| 567 | currentHeading = getHeading() | |
| 568 | - | end |
| 568 | + | |
| 569 | - | end |
| 569 | + | |
| 570 | --print("Heading: ", currentHeading)
| |
| 571 | ||
| 572 | - | print("Mining complete")
|
| 572 | + | if(branchStart.y ~= current.y) then |
| 573 | -- If y level is not the same -- | |
| 574 | currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z), currentHeading) | |
| 575 | - | else |
| 575 | + | |
| 576 | - | -- GPS_DATA not found -- |
| 576 | + | |
| 577 | - | term.clear() |
| 577 | + | -- Ensure z level is the same as starting y level -- |
| 578 | - | term.setCursorPos(1,1) |
| 578 | + | if(branchHeading == 'N' or branchHeading == 'n' or branchHeading == 'S' or branchHeading == 's') then |
| 579 | - | print("File 'GPS_DATA' does not exist, please run program to initiate mining.")
|
| 579 | + | if(branchStart.z ~= current.z) then |
| 580 | - | error() |
| 580 | + | -- If z level is not the same -- |
| 581 | - | end |
| 581 | + | currentHeading = goToLocation(vector.new(branchStart.x,current.y,current.z), currentHeading) |
| 582 | current = vector.new(gps.locate()) | |
| 583 | end | |
| 584 | end | |
| 585 | -- Ensure x level is the same as starting y level -- | |
| 586 | if(branchHeading == 'E' or branchHeading == 'e' or branchHeading == 'W' or branchHeading == 'w') then | |
| 587 | if(branchStart.x ~= current.x) then | |
| 588 | -- If x level is not the same -- | |
| 589 | currentHeading = goToLocation(vector.new(current.x,current.y,branchStart.z), currentHeading) | |
| 590 | current = vector.new(gps.locate()) | |
| 591 | end | |
| 592 | end | |
| 593 | setHeading(currentHeading,branchHeading) | |
| 594 | currentHeading = branchHeading | |
| 595 | --[[ | |
| 596 | Done | |
| 597 | 0 : Not done, still mining | |
| 598 | 1 : Is done, mining was successful | |
| 599 | -1 : Inventory full | |
| 600 | -2 : Insufficient fuel | |
| 601 | ]]-- | |
| 602 | done = 0 | |
| 603 | while(done == 0) do | |
| 604 | current = vector.new(gps.locate()) | |
| 605 | if(current.x == nil or current.y == nil or current.z == nil) then | |
| 606 | -- GPS signal could not be established -- | |
| 607 | error("Could not establish GPS signal, please ensure GPS servers are running and try again")
| |
| 608 | else | |
| 609 | -- Ensure distance from branchStart is less than branchLimit -- | |
| 610 | -- Along the North/South line -- | |
| 611 | if(branchHeading == 'N' or branchHeading == 'n' or branchHeading == 'S' or branchHeading == 's')then | |
| 612 | -- Check if distance from branchStart is within branchLimit -- | |
| 613 | if(math.abs(branchStart.z - current.z)>=branchLimit) then | |
| 614 | -- Distance from branchStart is not within branchLimit -- | |
| 615 | currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading) | |
| 616 | currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading) | |
| 617 | done = 1 | |
| 618 | else | |
| 619 | -- Distance from branchStart is within branchLimit -- | |
| 620 | -- Check for sufficient fuel -- | |
| 621 | if(sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then | |
| 622 | if(invSpace()) then | |
| 623 | if(current.y == branchStart.y) then | |
| 624 | turtle.turnLeft() | |
| 625 | if(done == 1) then | |
| 626 | done = mineVein(branchStart,0,false,plist) | |
| 627 | end | |
| 628 | turtle.turnRight() | |
| 629 | turtle.turnRight() | |
| 630 | if(done == 1) then | |
| 631 | done = mineVein(branchStart,0,false,plist) | |
| 632 | end | |
| 633 | turtle.turnLeft() | |
| 634 | while(turtle.detect()) do | |
| 635 | turtle.dig() | |
| 636 | end | |
| 637 | turtle.forward() | |
| 638 | turtle.turnLeft() | |
| 639 | if(done == 1) then | |
| 640 | done = mineVein(branchStart,0,false,plist) | |
| 641 | end | |
| 642 | turtle.turnRight() | |
| 643 | turtle.turnRight() | |
| 644 | if(done == 1) then | |
| 645 | done = mineVein(branchStart,0,false,plist) | |
| 646 | end | |
| 647 | turtle.turnLeft() | |
| 648 | while(turtle.detectUp()) do | |
| 649 | turtle.digUp() | |
| 650 | end | |
| 651 | turtle.up() | |
| 652 | elseif(current.y - 1 == branchStart.y) then | |
| 653 | turtle.turnLeft() | |
| 654 | if(done == 1) then | |
| 655 | done = mineVein(branchStart,0,false,plist) | |
| 656 | end | |
| 657 | turtle.turnRight() | |
| 658 | turtle.turnRight() | |
| 659 | if(done == 1) then | |
| 660 | done = mineVein(branchStart,0,false,plist) | |
| 661 | end | |
| 662 | turtle.turnLeft() | |
| 663 | while(turtle.detect()) do | |
| 664 | turtle.dig() | |
| 665 | end | |
| 666 | turtle.forward() | |
| 667 | turtle.turnLeft() | |
| 668 | if(done == 1) then | |
| 669 | done = mineVein(branchStart,0,false,plist) | |
| 670 | end | |
| 671 | turtle.turnRight() | |
| 672 | turtle.turnRight() | |
| 673 | if(done == 1) then | |
| 674 | done = mineVein(branchStart,0,false,plist) | |
| 675 | end | |
| 676 | turtle.turnLeft() | |
| 677 | while(turtle.detectDown()) do | |
| 678 | turtle.digDown() | |
| 679 | end | |
| 680 | turtle.down() | |
| 681 | else | |
| 682 | currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z),currentHeading) | |
| 683 | end | |
| 684 | else | |
| 685 | sortInv() | |
| 686 | clrInv(plist) | |
| 687 | if(not invSpace()) then | |
| 688 | currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading) | |
| 689 | currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading) | |
| 690 | done = -1 | |
| 691 | end | |
| 692 | end | |
| 693 | else | |
| 694 | if(not consumeFuel(4000)) then | |
| 695 | currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,current.z),currentHeading) | |
| 696 | currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading) | |
| 697 | done = -2 | |
| 698 | end | |
| 699 | end | |
| 700 | end | |
| 701 | -- Along the East/West line -- | |
| 702 | elseif(branchHeading == 'E' or branchHeading == 'e' or branchHeading == 'W' or branchHeading == 'w') then | |
| 703 | -- Check if distance from branchStart is within branchLimit -- | |
| 704 | if(math.abs(branchStart.x - current.x)>=branchLimit) then | |
| 705 | -- Distance from branchStart is not within branchLimit -- | |
| 706 | currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading) | |
| 707 | currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading) | |
| 708 | done = 1 | |
| 709 | else | |
| 710 | -- Distance from branchStart is within branchLimit -- | |
| 711 | -- Check for sufficient fuel -- | |
| 712 | if(sufficientFuel(current,branchStart,turtle.getFuelLevel(),fuelRemainder + 5)) then | |
| 713 | if(invSpace()) then | |
| 714 | if(current.y == branchStart.y) then | |
| 715 | turtle.turnLeft() | |
| 716 | if(done == 1) then | |
| 717 | done = mineVein(branchStart,0,false,plist) | |
| 718 | end | |
| 719 | turtle.turnRight() | |
| 720 | turtle.turnRight() | |
| 721 | if(done == 1) then | |
| 722 | done = mineVein(branchStart,0,false,plist) | |
| 723 | end | |
| 724 | turtle.turnLeft() | |
| 725 | while(turtle.detect()) do | |
| 726 | turtle.dig() | |
| 727 | end | |
| 728 | turtle.forward() | |
| 729 | turtle.turnLeft() | |
| 730 | if(done == 1) then | |
| 731 | done = mineVein(branchStart,0,false,plist) | |
| 732 | end | |
| 733 | turtle.turnRight() | |
| 734 | turtle.turnRight() | |
| 735 | if(done == 1) then | |
| 736 | done = mineVein(branchStart,0,false,plist) | |
| 737 | end | |
| 738 | turtle.turnLeft() | |
| 739 | while(turtle.detectUp()) do | |
| 740 | turtle.digUp() | |
| 741 | end | |
| 742 | turtle.up() | |
| 743 | elseif(current.y - 1 == branchStart.y) then | |
| 744 | turtle.turnLeft() | |
| 745 | if(done == 1) then | |
| 746 | done = mineVein(branchStart,0,false,plist) | |
| 747 | end | |
| 748 | turtle.turnRight() | |
| 749 | turtle.turnRight() | |
| 750 | if(done == 1) then | |
| 751 | done = mineVein(branchStart,0,false,plist) | |
| 752 | end | |
| 753 | turtle.turnLeft() | |
| 754 | while(turtle.detect()) do | |
| 755 | turtle.dig() | |
| 756 | end | |
| 757 | turtle.forward() | |
| 758 | turtle.turnLeft() | |
| 759 | if(done == 1) then | |
| 760 | done = mineVein(branchStart,0,false,plist) | |
| 761 | end | |
| 762 | turtle.turnRight() | |
| 763 | turtle.turnRight() | |
| 764 | if(done == 1) then | |
| 765 | done = mineVein(branchStart,0,false,plist) | |
| 766 | end | |
| 767 | turtle.turnLeft() | |
| 768 | while(turtle.detectDown()) do | |
| 769 | turtle.digDown() | |
| 770 | end | |
| 771 | turtle.down() | |
| 772 | else | |
| 773 | currentHeading = goToLocation(vector.new(current.x,branchStart.y,current.z),currentHeading) | |
| 774 | end | |
| 775 | else | |
| 776 | sortInv() | |
| 777 | clrInv(plist) | |
| 778 | if(not invSpace()) then | |
| 779 | currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading) | |
| 780 | currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading) | |
| 781 | done = -1 | |
| 782 | end | |
| 783 | end | |
| 784 | else | |
| 785 | if(not consumeFuel(4000)) then | |
| 786 | currentHeading = goToLocation(vector.new(current.x,branchStart.y,branchStart.z),currentHeading) | |
| 787 | currentHeading = goToLocation(vector.new(branchStart.x,branchStart.y,branchStart.z),currentHeading) | |
| 788 | done = -2 | |
| 789 | end | |
| 790 | end | |
| 791 | end | |
| 792 | end | |
| 793 | end | |
| 794 | end | |
| 795 | return done | |
| 796 | end | |
| 797 | x = lstToTable() | |
| 798 | print(mineBranch(vector.new(gps.locate()),getHeading(),"",5,10,x,0)) |