rdalkire

farm031

Apr 26th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.70 KB | None | 0 0
  1. --[[
  2.     com.ape42.turtle.Farm
  3.     tested on computercraft 1.63
  4.     @author R David Alkire, IGN ian_xw
  5.      Farms X rows that are spaced one
  6.      row apart, then sleeps, repeats.
  7. Changes by version:
  8.      0.1.0: Reaps one row.
  9.      0.2.0: Reaps & sows X rows, then
  10.       retrieves the harvest.
  11.      0.2.1: Seed selection defect is
  12.       corrected, so that you now get
  13.       a whole row of a given plantable,
  14.       if there are enough seeds.
  15.      0.2.2: Better stoppage for out-of
  16.       -fuel or if blocked.
  17.      0.3.0: Sleeps and runs twice more
  18.      0.3.1: Fixes a return-stop bug
  19.      
  20. TODO:
  21. - Ensure that there's enough fuel to
  22.     return from a given point, and use
  23.     it when needed.
  24. - Estimate fuel required for harvest,
  25.     given row length & number of rows.
  26.     Display.
  27. - Given fuel and field size, estimate
  28.     the number of harvests.
  29. - Allow use of block instead of row
  30.     parameter.
  31. - Allow the turtle to start at right
  32.     corner, depending which way is
  33.     open.
  34. - Create a routine for automated field
  35.     creation, given flat grassy area,
  36.     a bucket and a source of water.
  37.   - Estimate max field size plantable
  38.     for two harvests, given the fuel.
  39.  
  40. This work is licensed under the
  41.  Creative Commons Attribution 4.0
  42.  International License. To view a copy
  43.  of this license, visit
  44.  http://creativecommons.org/licenses/by/4.0/
  45.  or send a letter to Creative Commons,
  46.  444 Castro Street, Suite 900, Mountain
  47.  View, California, 94041, USA.
  48. ]]
  49.  
  50. -- Initial slots indicate what items
  51. -- the user deems plantable.
  52. local plntblCnt = 1
  53.  
  54. -- Initializes the Plantable Count.
  55. -- Called before any work is done, this
  56. -- finds the last populated inventory
  57. -- slot.  That and the ones below
  58. -- will be used as reference.
  59. -- @return plntblCnt
  60. local function initPlntblCnt()
  61.   local indx = 0
  62.   local isPlntbl = true
  63.   while isPlntbl and indx < 16 do
  64.     indx = indx + 1
  65.     turtle.select(indx)
  66.     isPlntbl= turtle.getItemCount()> 0
  67.   end
  68.   plntblCnt = indx - 1
  69.   return plntblCnt
  70. end
  71.  
  72. -- For the non - reference slots,
  73. -- find one that matches the
  74. -- currently selected and match that.
  75. -- Or return false.
  76. local function selectNonRefSlot()
  77.  
  78.   local s = plntblCnt + 1
  79.   local seedy = false
  80.   while s < 16 and seedy == false do
  81.     seedy = turtle.compareTo( s )
  82.     if seedy then
  83.       turtle.select(s)
  84.     end
  85.     s = s + 1
  86.   end --while
  87.   return seedy
  88. end
  89.  
  90. -- Decides what to plant based the
  91. --  reference slots (plntblCnt)
  92. -- @param rowIndex the current row
  93. local function sow( rowIndex )
  94.   local seedy = false
  95.  
  96.   -- Alternate among the plantables:
  97.   -- loop through reference slots.
  98.   local i = 0
  99.   while i < plntblCnt and seedy == false do
  100.     --use what was for a given reference slot
  101.     local othr = ( (rowIndex + i )% plntblCnt) + 1
  102. --    print( "rIdx="..rowIndex, "i="..i,
  103. --        "othr="..othr)
  104.     turtle.select(othr)
  105.     if turtle.getItemCount(othr) > 1 then
  106.       seedy = true
  107.     else
  108.       seedy = selectNonRefSlot()
  109.     end
  110.     i = i + 1
  111.   end
  112.  
  113.   if seedy then
  114.     turtle.place()
  115.   end
  116.  
  117.   return seedy
  118. end
  119.  
  120. local function reapRow( lth )
  121.   print( "Reaping a row ")
  122.  
  123. -- Loop until length is reached or
  124. -- the way is blocked
  125.   local keepGoing = true
  126.   local placeR = 1
  127.  
  128. -- lth 0 just means that really
  129. -- the length is not yet determined
  130.   local firstRow = lth == 0
  131.   while keepGoing do
  132.    
  133.     turtle.dig() -- reaps
  134.     keepGoing = turtle.forward()
  135.    
  136.     -- sees if it can keepGoing
  137.     if keepGoing then
  138.       placeR = placeR + 1
  139.       if placeR >= lth and lth > 0 then
  140.         keepGoing = false
  141.       end
  142.       -- prevents infinite loop
  143.       if placeR > 1023 then
  144.         keepGoing = false
  145.       end
  146.     else
  147.       if firstRow == false then
  148.         print("Stopped unexpectedly")
  149.         placeR = 0
  150.         print("placeR="..placeR)
  151.       end
  152.     end
  153.    
  154.   end -- while loop
  155.  
  156.   lth = placeR
  157.   return lth
  158. end
  159.  
  160. -- Backs up, tilling & planting
  161. -- @param lnth row length
  162. -- @param indx which row
  163. local function sowRow(lnth, indx)
  164.     print( "Sowing a row." )
  165.   local block = 1
  166.   local seedy = true
  167.   -- while it has length & seeds
  168.   while block < lnth and seedy do
  169.    
  170.     seedy = turtle.back()
  171.     turtle.dig()
  172.    
  173.     -- if there are still seeds
  174.     if seedy then
  175.       seedy = sow(indx)
  176.     else
  177.         print("sowRow(): seedy== false" )
  178.     end
  179.     block = block + 1
  180.   end
  181.  
  182.   -- return if there's seeds still.
  183.   return seedy
  184.  
  185. end
  186.  
  187. -- Harvests and plants every other row
  188. -- @param rows number of plantable rows
  189. -- @return found row length. If stopped
  190. --  unexpectedly this will be 0.
  191. local function reapAndSow( rows )
  192.   print("beginning reapAndSow()")
  193.   -- Find out how many slots are for
  194.   -- reference.
  195.   local plntbl = initPlntblCnt()
  196.   print("The first "..plntbl..
  197.     " slots \nare deemed plantable")
  198.    
  199.   print("rows = ".. rows)
  200.   local rowLength = 0
  201.   local rowIndex = 0
  202.   local isSeedy = true
  203.   local isFirst = true
  204.   while (rowIndex< rows) and isSeedy do
  205.     print( "rowIndex ".. rowIndex )
  206.    
  207.     -- If there was an unexpected
  208.     -- stop in previous row, then
  209.     -- rowLength will be its initial
  210.     -- value, 0
  211.     if rowLength<= 0 and isFirst== false then  
  212.       -- Stop loop
  213.       isSeedy = false
  214.     else
  215.       -- After the first row
  216.       if isFirst == false then
  217.         -- Move to the next row
  218.         turtle.turnRight()
  219.         turtle.forward()
  220.         turtle.forward()
  221.         turtle.turnLeft()
  222.       end
  223.       rowLength = reapRow( rowLength )
  224.       if rowLength > 0 then
  225.         isSeedy=sowRow(rowLength, rowIndex)
  226.         -- if stopped, zero-out.
  227.         rowLength= isSeedy and rowLength or 0
  228.       end
  229.     end
  230.     isFirst = false
  231.     rowIndex = rowIndex + 1
  232.   end
  233.   return rowLength
  234. end
  235.  
  236. local function returnAndStore(rows)
  237.   print( "Returning to the start.")
  238.  
  239.   turtle.turnLeft()
  240.   local canGo = true
  241.   local forwards = (rows - 1) * 2
  242.   for i= 1, forwards do
  243.     canGo = turtle.forward()
  244.   end
  245.  
  246.   turtle.turnLeft()
  247.   if canGo then
  248.     for i = 16, 1, -1 do
  249.       turtle.select(i)
  250.       if i > plntblCnt then
  251.         turtle.drop()
  252.       else
  253.         turtle.drop(
  254.             turtle.getItemCount(i)- 1)
  255.       end
  256.     end
  257.   end --canGo
  258.   turtle.turnLeft()
  259.   turtle.turnLeft()
  260.   return canGo
  261. end
  262.  
  263. local tArgs = {...}
  264. local rowCntFromUsr = 0
  265.  
  266. --[[ If user supplies a good number,
  267. this uses it as row count.  If user
  268. supplied a bad argument, it indicates.
  269. ]]
  270. local argCount = table.getn(tArgs)
  271. local badArg = false
  272. local badArgMsg = "Argument OK"
  273. if argCount > 0 then
  274.   rowCntFromUsr = tArgs[1]
  275.   if tonumber(rowCntFromUsr)==nil then
  276.     badArg = true
  277.     badArgMsg = "Argument not a number"
  278.   else
  279.     rowCntFromUsr = tonumber(rowCntFromUsr)
  280.   end
  281.  
  282. else
  283.   badArg = true
  284.   badArgMsg = "Supply how many rows"
  285. end
  286.  
  287. if badArg then
  288.   print( badArgMsg )
  289.   print("Put at least 1 of something" )
  290.   print(" plantable in first few slots.")
  291. else
  292.  
  293.   --[[ Start ]]
  294.   -- three times, if enough fuel
  295.   local n = 1
  296.   local okSoFar = true
  297.   while n<3 and okSoFar do
  298.     local startTime = os.clock();
  299.     local zeroStopped = reapAndSow( rowCntFromUsr )
  300.     if zeroStopped > 0 then
  301.       okSoFar = returnAndStore(rowCntFromUsr)
  302.       if okSoFar then
  303.         local endTime = os.clock()
  304.         local duration = endTime - startTime
  305.         local waitTime = (40* 60)-duration
  306.         local nextTime = endTime+ waitTime
  307.         print("harvest: "..(n).." of 3")
  308.         if n < 3 then
  309.           os.sleep(waitTime)
  310.         end
  311.       end --okSofar
  312.     else
  313.       okSoFar = false
  314.     end
  315.     n = n + 1
  316.   end -- of three times.
  317.  
  318.   if okSoFar == false then
  319.     print("Out of fuel or other stop.")
  320.   end
  321. end
Advertisement
Add Comment
Please, Sign In to add comment