rdalkire

farm061

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