SHOW:
|
|
- or go back to the newest paste.
| 1 | - | --[[ com.ape42.turtle.Farm 0.8.1 |
| 1 | + | --[[ |
| 2 | - | For computercraft 1.63 |
| 2 | + | |
| 3 | crops according to the reference | |
| 4 | slots, which are the first | |
| 5 | contiguous slots that have seeds. | |
| 6 | If it doesn't have enough seed to | |
| 7 | alternate, it will leave a gap. | |
| 8 | There are two globals constants - | |
| 9 | to sleep GROW_WAIT minutes, and | |
| 10 | repeat MAX_REPEATS or until out | |
| 11 | of fuel. | |
| 12 | @author R David Alkire, IGN ian_xw | |
| 13 | ||
| 14 | 0.8.2 Return trip is in reverse and | |
| 15 | includes digging for more farmland. | |
| 16 | - | - Make it so the 'running' column is |
| 16 | + | |
| 17 | - | farmland. Turn robot around and |
| 17 | + | |
| 18 | - | till backwards. |
| 18 | + | - Change pattern so it zigzags. Be up |
| 19 | - | - Give more explicit usage instructions |
| 19 | + | one block so it doesn't have to |
| 20 | - | and include a readme |
| 20 | + | turn around at each step. This |
| 21 | should be almost twice as fuel | |
| 22 | efficient. | |
| 23 | - Add melon/pumpkin slots, handling. | |
| 24 | - Sugarcane. | |
| 25 | - Cocoa beans, maybe. | |
| 26 | - Allow the turtle to start at right | |
| 27 | corner, depending which way is | |
| 28 | open. | |
| 29 | - Create a routine for automated field | |
| 30 | creation, given flat grassy area, | |
| 31 | a bucket and a source of water. | |
| 32 | ||
| 33 | This work is licensed under the | |
| 34 | Creative Commons Attribution 4.0 | |
| 35 | International License. To view a copy | |
| 36 | of this license, visit | |
| 37 | http://creativecommons.org/licenses/by/4.0/ | |
| 38 | or send a letter to Creative Commons, | |
| 39 | 444 Castro Street, Suite 900, Mountain | |
| 40 | View, California, 94041, USA. | |
| 41 | ]] | |
| 42 | ||
| 43 | -- The wait time between beginning of | |
| 44 | -- each harvest, in minutes. | |
| 45 | GROW_WAIT = 40 | |
| 46 | ||
| 47 | -- The number of times to farm the | |
| 48 | -- field, baring other constraints | |
| 49 | MAX_REPEATS = 3 | |
| 50 | ||
| 51 | -- Initial slots indicate what items | |
| 52 | -- the user deems plantable. | |
| 53 | local plntblCnt = 1 | |
| 54 | ||
| 55 | -- Initializes the Plantable Count. | |
| 56 | -- Called before any work is done, this | |
| 57 | -- finds the last populated inventory | |
| 58 | -- slot. That and the ones below | |
| 59 | -- will be used as reference. | |
| 60 | -- @return plntblCnt | |
| 61 | local function initPlntblCnt() | |
| 62 | local indx = 0 | |
| 63 | local isPlntbl = true | |
| 64 | while isPlntbl and indx < 16 do | |
| 65 | indx = indx + 1 | |
| 66 | turtle.select(indx) | |
| 67 | isPlntbl= turtle.getItemCount()> 0 | |
| 68 | end | |
| 69 | plntblCnt = indx - 1 | |
| 70 | return plntblCnt | |
| 71 | end | |
| 72 | ||
| 73 | -- For the non - reference slots, | |
| 74 | -- find one that matches the | |
| 75 | -- currently selected and match that. | |
| 76 | -- Or return false. | |
| 77 | local function selectNonRefSlot() | |
| 78 | ||
| 79 | local s = plntblCnt + 1 | |
| 80 | local seedy = false | |
| 81 | while s < 16 and seedy == false do | |
| 82 | seedy = turtle.compareTo( s ) | |
| 83 | if seedy then | |
| 84 | turtle.select(s) | |
| 85 | end | |
| 86 | s = s + 1 | |
| 87 | end --while | |
| 88 | return seedy | |
| 89 | end | |
| 90 | ||
| 91 | -- Decides what to plant based the | |
| 92 | -- reference slots (plntblCnt) | |
| 93 | -- @param rowIndex the current row | |
| 94 | -- @block block # within the row, | |
| 95 | -- starting with 1. | |
| 96 | -- @param prevRow an array of crop | |
| 97 | -- types for previous row | |
| 98 | local function sow( rowIndex, block, | |
| 99 | prevRow ) | |
| 100 | local seedy = false | |
| 101 | ||
| 102 | -- Alternate among the plantables: | |
| 103 | -- loop through reference slots. | |
| 104 | local i = 0 | |
| 105 | local refSlt = 0 | |
| 106 | while i < plntblCnt and seedy == false do | |
| 107 | --use what was for a given reference slot | |
| 108 | refSlt = ( (rowIndex + i )% plntblCnt) + 1 | |
| 109 | ||
| 110 | turtle.select(refSlt) | |
| 111 | if turtle.getItemCount(refSlt) > 1 then | |
| 112 | seedy = true | |
| 113 | else | |
| 114 | seedy = selectNonRefSlot() | |
| 115 | end | |
| 116 | i = i + 1 | |
| 117 | end | |
| 118 | ||
| 119 | if seedy then | |
| 120 | if prevRow[block]== refSlt then | |
| 121 | prevRow[block] = 0 | |
| 122 | else | |
| 123 | prevRow[block] = refSlt | |
| 124 | turtle.place() | |
| 125 | end -- if matches | |
| 126 | else | |
| 127 | print("Apparently, out of seeds")
| |
| 128 | end -- if seedy | |
| 129 | ||
| 130 | return seedy | |
| 131 | end | |
| 132 | ||
| 133 | -- Harvests a row: Digs (hoes) and | |
| 134 | -- moves forward the length or until | |
| 135 | -- the way is blocked | |
| 136 | -- @param lth the expected length of | |
| 137 | -- the row or 0 if not yet determined | |
| 138 | -- @param firstRow | |
| 139 | -- @return how long the row was, or | |
| 140 | -- 0 if there was an unexpected stop | |
| 141 | -- due to fuel outage after first row. | |
| 142 | local function reapRow( lth, firstRow ) | |
| 143 | -- print( "Reaping a row ") | |
| 144 | ||
| 145 | -- Loop until length is reached or | |
| 146 | -- the way is blocked | |
| 147 | local keepGoing = true | |
| 148 | local placeR = 1 | |
| 149 | ||
| 150 | if firstRow then | |
| 151 | local fuel = turtle.getFuelLevel() | |
| 152 | local maxLth = fuel/2 - 1 | |
| 153 | if lth > maxLth then | |
| 154 | lth = maxLth | |
| 155 | print(string.format("First row. "
| |
| 156 | .."fuel %d, max length %d", | |
| 157 | fuel, lth)) | |
| 158 | end | |
| 159 | end | |
| 160 | while keepGoing do | |
| 161 | ||
| 162 | turtle.dig() -- reaps | |
| 163 | keepGoing = turtle.forward() | |
| 164 | ||
| 165 | -- sees if it can keepGoing | |
| 166 | if keepGoing then | |
| 167 | placeR = placeR + 1 | |
| 168 | if placeR >= lth and lth > 0 then | |
| 169 | keepGoing = false | |
| 170 | end | |
| 171 | -- prevents infinite loop | |
| 172 | if placeR > 1023 then | |
| 173 | keepGoing = false | |
| 174 | end | |
| 175 | else | |
| 176 | if firstRow == false then | |
| 177 | print("Stopped unexpectedly")
| |
| 178 | placeR = 0 | |
| 179 | print("placeR="..placeR)
| |
| 180 | end | |
| 181 | end | |
| 182 | ||
| 183 | end -- while loop | |
| 184 | ||
| 185 | lth = placeR | |
| 186 | return lth | |
| 187 | end | |
| 188 | ||
| 189 | -- Backs up, tilling & planting | |
| 190 | -- @param lnth row length | |
| 191 | -- @param indx which row | |
| 192 | -- @param prevRow array of crop plantings | |
| 193 | -- for the previous row | |
| 194 | -- @return true if successful. False | |
| 195 | -- means seeds ran out, or there was | |
| 196 | -- a blockage. | |
| 197 | local function sowRow(lnth, indx, prevRow) | |
| 198 | -- print( "Sowing a row." ) | |
| 199 | local block = 1 | |
| 200 | local seedy = true | |
| 201 | ||
| 202 | -- while it has length & seeds | |
| 203 | while block < lnth do | |
| 204 | ||
| 205 | turtle.back() | |
| 206 | turtle.dig() | |
| 207 | ||
| 208 | -- if there are still seeds | |
| 209 | if seedy then | |
| 210 | seedy = sow(indx, block, prevRow) | |
| 211 | end | |
| 212 | block = block + 1 | |
| 213 | end | |
| 214 | ||
| 215 | -- return if there's seeds still. | |
| 216 | return seedy | |
| 217 | ||
| 218 | end | |
| 219 | ||
| 220 | -- Determines whether there's enough | |
| 221 | -- fuel to: move to another row, | |
| 222 | -- reap it, sow it, and return to | |
| 223 | -- base | |
| 224 | -- @param rowIndex, rowLength | |
| 225 | local function isFuelOKForNextRow( | |
| 226 | rowIndex, rowLength) | |
| 227 | ||
| 228 | local fuelNeed = 1 + rowIndex | |
| 229 | + ( rowLength * 2 ) | |
| 230 | ||
| 231 | local fuelLevel = turtle.getFuelLevel() | |
| 232 | ||
| 233 | local isEnough= fuelLevel >= fuelNeed | |
| 234 | if isEnough ~= true then | |
| 235 | print("Not enough fuel for next "
| |
| 236 | .."row and return trip.") | |
| 237 | print( string.format( | |
| 238 | "fuelNeed %d, rowIndex %d, " | |
| 239 | .."rowLength %d", fuelNeed, | |
| 240 | rowIndex, rowLength ) ) | |
| 241 | ||
| 242 | print( string.format( "fuelLevel %d", | |
| 243 | fuelLevel ) ) | |
| 244 | ||
| 245 | end | |
| 246 | ||
| 247 | return isEnough | |
| 248 | ||
| 249 | end | |
| 250 | ||
| 251 | -- Checks fuel, and if good, moves to | |
| 252 | -- next row, reaps and sows. | |
| 253 | -- @param rowLength, isFirst, prevRow, | |
| 254 | -- rowIndex. | |
| 255 | -- prevRow is array of ints, each | |
| 256 | -- a reference item slot, from, you | |
| 257 | -- guessed it: the previous row. | |
| 258 | -- @return isFuelOK, rowLength, isSeedy, | |
| 259 | -- isBlocked. isSeedy means there | |
| 260 | -- are still seeds to be planted. | |
| 261 | -- isBlocked means it was blocked | |
| 262 | -- when moving from one row to the | |
| 263 | -- next, which is meant to be a | |
| 264 | -- normal scene. | |
| 265 | local function moveOnAndDoOne( rowLength, | |
| 266 | isFirst, prevRow, rowIndex ) | |
| 267 | ||
| 268 | local isFuelOK = true | |
| 269 | local isSeedy = true | |
| 270 | local isBlocked = false | |
| 271 | ||
| 272 | -- After the first row | |
| 273 | if isFirst == false then | |
| 274 | ||
| 275 | isFuelOK = isFuelOKForNextRow( | |
| 276 | rowIndex, rowLength ) | |
| 277 | ||
| 278 | if isFuelOK then | |
| 279 | -- Move to the next row | |
| 280 | turtle.turnRight() | |
| 281 | isBlocked=turtle.forward()==false | |
| 282 | turtle.turnLeft() | |
| 283 | end | |
| 284 | ||
| 285 | end -- if after first | |
| 286 | ||
| 287 | if isFuelOK and isSeedy and | |
| 288 | not isBlocked then | |
| 289 | ||
| 290 | rowLength= reapRow( rowLength, isFirst ) | |
| 291 | if isFirst then | |
| 292 | -- initializes previous array | |
| 293 | for i = 1, rowLength do | |
| 294 | prevRow[i] = 0 | |
| 295 | end | |
| 296 | end | |
| 297 | ||
| 298 | if rowLength > 0 then | |
| 299 | isSeedy=sowRow( rowLength, rowIndex, prevRow) | |
| 300 | end | |
| 301 | ||
| 302 | end -- if fuel OK | |
| 303 | ||
| 304 | return isFuelOK, rowLength, isSeedy, | |
| 305 | isBlocked | |
| 306 | ||
| 307 | end | |
| 308 | ||
| 309 | -- Harvests and plants the rows | |
| 310 | -- @param width/ rows number of | |
| 311 | -- expected plantable rows | |
| 312 | -- @return number of rows planted. | |
| 313 | -- If stopped unexpectedly this will | |
| 314 | -- be 0. | |
| 315 | local function reapAndSow( rows ) | |
| 316 | -- print("beginning reapAndSow()")
| |
| 317 | -- Find out how many slots are for | |
| 318 | -- reference. | |
| 319 | local plntbl = initPlntblCnt() | |
| 320 | ||
| 321 | print("The first "..plntbl..
| |
| 322 | " slots \nare deemed plantable") | |
| 323 | ||
| 324 | local prevRow = {}
| |
| 325 | print("rows = ".. rows)
| |
| 326 | local rowLength = rows | |
| 327 | local rowIndex = 0 | |
| 328 | local isSeedy = true | |
| 329 | local isFuelOK = true | |
| 330 | local isFirst = true | |
| 331 | local isBlocked = false | |
| 332 | ||
| 333 | while (rowIndex< rows) and isSeedy | |
| 334 | and isFuelOK and not isBlocked do | |
| 335 | ||
| 336 | -- print( "rowIndex ".. rowIndex ) | |
| 337 | ||
| 338 | -- If there was an unexpected | |
| 339 | -- stop in previous row, then | |
| 340 | -- rowLength will be its initial | |
| 341 | -- value, 0 | |
| 342 | if rowLength<= 0 and isFirst== false then | |
| 343 | -- Stops loop | |
| 344 | isSeedy = false | |
| 345 | else | |
| 346 | ||
| 347 | isFuelOK, rowLength, isSeedy, | |
| 348 | isBlocked = | |
| 349 | moveOnAndDoOne( rowLength, | |
| 350 | isFirst, prevRow, rowIndex ) | |
| 351 | ||
| 352 | --[[ TODO clean out when ready | |
| 353 | print( string.format( | |
| 354 | "in farm(), after " | |
| 355 | .."moveOnAndDoOne( rowLength %d, " | |
| 356 | .."isFirst %s, rowIndex %d)", | |
| 357 | rowLength, tostring(isFirst), | |
| 358 | rowIndex ) ) | |
| 359 | ||
| 360 | print( string.format( | |
| 361 | "Which returned isFuelOK %s, " | |
| 362 | .."rowLength %d, isSeedy %s", | |
| 363 | tostring( isFuelOK ), rowLength, | |
| 364 | tostring(isSeedy) ) ) | |
| 365 | ]] | |
| 366 | ||
| 367 | end -- if rowLength > 0 & not first | |
| 368 | ||
| 369 | isFirst = false | |
| 370 | rowIndex = rowIndex + 1 | |
| 371 | ||
| 372 | end -- while rows & isSeedy | |
| 373 | ||
| 374 | -- The number of rows traversed | |
| 375 | -- or 0 if unexpected problem | |
| 376 | local rtrnVal = 0 | |
| 377 | if rowLength > 0 then | |
| 378 | -- If fuel was too low, it did not | |
| 379 | -- move to next row, so therefore | |
| 380 | -- its row is one less than otherwise | |
| 381 | if isFuelOK and not isBlocked then | |
| 382 | rtrnVal = rowIndex | |
| 383 | else | |
| 384 | rtrnVal = rowIndex - 1 | |
| 385 | end | |
| 386 | else | |
| 387 | rtrnVal = 0 | |
| 388 | end | |
| 389 | ||
| 390 | return rtrnVal | |
| 391 | ||
| 392 | end | |
| 393 | ||
| 394 | -- Returns to the start and drops all | |
| 395 | -- inventory except one of each | |
| 396 | -- reference item. (It's assumed there | |
| 397 | -- are hoppers & a chest.) | |
| 398 | -- @param rows how many rows are | |
| 399 | -- planted. | |
| 400 | -- @return true if successful. False | |
| 401 | -- means there was blockage or fuel | |
| 402 | -- outage. | |
| 403 | - | local forwards = (rows - 1) |
| 403 | + | |
| 404 | ||
| 405 | turtle.turnRight() | |
| 406 | local canGo = true | |
| 407 | - | .."rows %d, forwards %d", |
| 407 | + | local steps = (rows - 1) |
| 408 | - | rows, forwards )) |
| 408 | + | |
| 409 | print( string.format( | |
| 410 | "returnAndStore(rows): " | |
| 411 | - | while (stp <= forwards) and canGo do |
| 411 | + | .."rows %d, steps %d", |
| 412 | - | canGo = turtle.forward() |
| 412 | + | rows, steps )) |
| 413 | ||
| 414 | local stp = 1 | |
| 415 | while (stp <= steps) and canGo do | |
| 416 | canGo = turtle.back() | |
| 417 | turtle.dig() | |
| 418 | stp = stp + 1 | |
| 419 | end | |
| 420 | ||
| 421 | turtle.turnRight() | |
| 422 | ||
| 423 | if canGo then | |
| 424 | for i = 16, 1, -1 do | |
| 425 | turtle.select(i) | |
| 426 | if i > plntblCnt then | |
| 427 | turtle.drop() | |
| 428 | else | |
| 429 | turtle.drop( | |
| 430 | turtle.getItemCount(i)- 1) | |
| 431 | end | |
| 432 | end | |
| 433 | else | |
| 434 | print("couldn't store")
| |
| 435 | end --canGo | |
| 436 | turtle.turnLeft() | |
| 437 | turtle.turnLeft() | |
| 438 | return canGo | |
| 439 | end | |
| 440 | ||
| 441 | -- This prints a message about the fuel | |
| 442 | -- situation. | |
| 443 | -- @param n is how many plantings have | |
| 444 | -- been completed so far | |
| 445 | -- @param fuelStart | |
| 446 | -- @param widthFromUsr is how wide the | |
| 447 | -- farm should have been according to | |
| 448 | -- the user. | |
| 449 | local function printFuelMsg(n, fuelStart, | |
| 450 | widthFromUsr) | |
| 451 | ||
| 452 | local fuelLevel = turtle.getFuelLevel() | |
| 453 | ||
| 454 | print("after harvest: "..(n)
| |
| 455 | .." of ".. MAX_REPEATS) | |
| 456 | ||
| 457 | print("fuelLevel ".. fuelLevel)
| |
| 458 | local fuelPerTurn = (fuelStart-fuelLevel) | |
| 459 | print( "fuelPerTurn ".. fuelPerTurn ) | |
| 460 | ||
| 461 | local fltTurns = fuelLevel/fuelPerTurn | |
| 462 | local floorTrns = math.floor( fltTurns ) | |
| 463 | local frction = fltTurns - floorTrns | |
| 464 | local rowsPart = frction * widthFromUsr | |
| 465 | ||
| 466 | print( string.format( | |
| 467 | "There would be enough fuel " | |
| 468 | .."for %d more turns " | |
| 469 | .."plus %d rows.", | |
| 470 | floorTrns, rowsPart )) | |
| 471 | ||
| 472 | if fuelLevel< fuelPerTurn and n < MAX_REPEATS then | |
| 473 | local fuelNeeded = (1 - frction) * fuelPerTurn | |
| 474 | local coals = math.ceil(fuelNeeded/80); | |
| 475 | ||
| 476 | print( string.format( | |
| 477 | "To finish the next harvest " | |
| 478 | .."as well or better than this one, " | |
| 479 | .."%d more fuel units are " | |
| 480 | .."needed. That's %d pieces " | |
| 481 | .."of coal at least. " | |
| 482 | .."Slot #9 please", fuelNeeded, | |
| 483 | coals ) ) | |
| 484 | ||
| 485 | end | |
| 486 | ||
| 487 | end | |
| 488 | ||
| 489 | -- Farms the field repeatedly up to | |
| 490 | -- MAX_REPEATS, or the fuel runs out, | |
| 491 | -- or there's some unexpected stop. | |
| 492 | -- @param widthFromUsr is the width | |
| 493 | -- of the square farm, according to | |
| 494 | -- user. If 0, this will estimate | |
| 495 | -- the maximum width according to | |
| 496 | -- fuel. | |
| 497 | local function farm( widthFromUsr ) | |
| 498 | ||
| 499 | --[[ Start ]] | |
| 500 | -- three times, if enough fuel | |
| 501 | local n = 1 | |
| 502 | local okSoFar = true | |
| 503 | while n<= MAX_REPEATS and okSoFar do | |
| 504 | local startTime = os.clock(); | |
| 505 | turtle.select(9) | |
| 506 | turtle.refuel() | |
| 507 | local fuelStart = turtle.getFuelLevel() | |
| 508 | print("fuel at start = ".. fuelStart)
| |
| 509 | ||
| 510 | local maxLnth = math.floor( | |
| 511 | math.sqrt(2* fuelStart)/ 2) | |
| 512 | ||
| 513 | print("Estimated max field "
| |
| 514 | .. "side \nfor full cycle: " | |
| 515 | .. maxLnth) | |
| 516 | ||
| 517 | if widthFromUsr == 0 then | |
| 518 | widthFromUsr = maxLnth | |
| 519 | end | |
| 520 | ||
| 521 | local rowsDone = reapAndSow( widthFromUsr ) | |
| 522 | okSoFar = returnAndStore(rowsDone) | |
| 523 | if rowsDone > 0 then | |
| 524 | if okSoFar then | |
| 525 | ||
| 526 | printFuelMsg(n, fuelStart, widthFromUsr ) | |
| 527 | ||
| 528 | local endTime = os.clock() | |
| 529 | local duration = endTime - startTime | |
| 530 | local waitTime = ( GROW_WAIT*60)-duration | |
| 531 | local nextTime = endTime+ waitTime | |
| 532 | if (n < MAX_REPEATS) and okSoFar then | |
| 533 | os.sleep(waitTime) | |
| 534 | end | |
| 535 | end --okSofar | |
| 536 | else | |
| 537 | okSoFar = false | |
| 538 | end | |
| 539 | n = n + 1 | |
| 540 | end -- of MAX_REPEATS loop | |
| 541 | ||
| 542 | if okSoFar == false then | |
| 543 | print("Stopped.")
| |
| 544 | end | |
| 545 | ||
| 546 | end | |
| 547 | ||
| 548 | local function main( tArgs ) | |
| 549 | local widthFromUsr = 0 | |
| 550 | ||
| 551 | --[[ If user supplies a good number, | |
| 552 | this uses it as row count. If user | |
| 553 | supplied a bad argument, it indicates. | |
| 554 | ]] | |
| 555 | local argCount = table.getn(tArgs) | |
| 556 | local badArg = false | |
| 557 | local badArgMsg = "Argument OK" | |
| 558 | if argCount > 0 then | |
| 559 | widthFromUsr = tArgs[1] | |
| 560 | if tonumber(widthFromUsr)==nil then | |
| 561 | badArg = true | |
| 562 | badArgMsg = "Argument not a number" | |
| 563 | else | |
| 564 | widthFromUsr = tonumber(widthFromUsr) | |
| 565 | end | |
| 566 | ||
| 567 | else | |
| 568 | badArg = true | |
| 569 | badArgMsg = "Supply width of farm." | |
| 570 | end | |
| 571 | ||
| 572 | if badArg then | |
| 573 | print( badArgMsg ) | |
| 574 | print("Put at least 1 of something" )
| |
| 575 | print(" plantable in first few slots.")
| |
| 576 | else | |
| 577 | farm( widthFromUsr ) | |
| 578 | end | |
| 579 | ||
| 580 | end | |
| 581 | ||
| 582 | local tArgs = {...}
| |
| 583 | main(tArgs) |