View difference between Paste ID: QHkJgMXK and EaKJ9Rd4
SHOW: | | - or go back to the newest paste.
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-
     
19+
     0.4.0: Tries to plant each row,
20
      alternating crops if possible
21
     0.4.1: alternation algorithm is
22
      repaired.  Wait time between
23
      harvests is changed from 40 to
24
      60 minutes.
25
      
26
TODO: 
27
- Ensure that there's enough fuel to 
28
    return from a given point, and use 
29-
- Allow use of block instead of row
29+
30
- Estimate fuel required for harvest,
31
    given row length & number of rows.
32
    Display.
33
- Given fuel and field size, estimate
34
    the number of harvests.
35
- Allow use of blocks instead of row
36
    parameter.
37
- Allow the turtle to start at right
38
    corner, depending which way is 
39
    open.
40
- Create a routine for automated field
41
    creation, given flat grassy area,
42
    a bucket and a source of water.
43
  - Estimate max field size plantable
44
    for two harvests, given the fuel.
45
  
46
This work is licensed under the 
47
 Creative Commons Attribution 4.0 
48
 International License. To view a copy 
49
 of this license, visit 
50
 http://creativecommons.org/licenses/by/4.0/ 
51
 or send a letter to Creative Commons, 
52
 444 Castro Street, Suite 900, Mountain 
53
 View, California, 94041, USA.
54
]]
55
56
-- Initial slots indicate what items 
57
-- the user deems plantable.
58
local plntblCnt = 1
59
60
-- Initializes the Plantable Count.
61
-- Called before any work is done, this
62
-- finds the last populated inventory
63
-- slot.  That and the ones below
64
-- will be used as reference.
65
-- @return plntblCnt
66
local function initPlntblCnt()
67
  local indx = 0
68
  local isPlntbl = true
69
  while isPlntbl and indx < 16 do
70
    indx = indx + 1
71
    turtle.select(indx)
72
    isPlntbl= turtle.getItemCount()> 0
73
  end
74
  plntblCnt = indx - 1
75
  return plntblCnt
76
end
77
78
-- For the non - reference slots,
79
-- find one that matches the
80
-- currently selected and match that.
81
-- Or return false.
82
local function selectNonRefSlot()
83
84
  local s = plntblCnt + 1
85
  local seedy = false
86
  while s < 16 and seedy == false do
87
    seedy = turtle.compareTo( s )
88
    if seedy then
89
      turtle.select(s)
90
    end
91
    s = s + 1
92
  end --while
93-
local function sow( rowIndex )
93+
94
end
95
96
-- Decides what to plant based the 
97
--  reference slots (plntblCnt)
98
-- @param rowIndex the current row
99
-- @block block # within the row, 
100
--  starting with 1.
101-
    local othr = ( (rowIndex + i )% plntblCnt) + 1
101+
-- @param prevRow an array of crop
102-
--    print( "rIdx="..rowIndex, "i="..i, 
102+
--  types for previous row
103-
--        "othr="..othr)
103+
local function sow( rowIndex, block, prevRow )
104-
    turtle.select(othr)
104+
105-
    if turtle.getItemCount(othr) > 1 then
105+
106
  -- Alternate among the plantables:
107
  -- loop through reference slots.
108
  local i = 0
109
  local refSlt = 0
110
  while i < plntblCnt and seedy == false do
111
    --use what was for a given reference slot
112
    refSlt = ( (rowIndex + i )% plntblCnt) + 1
113
    
114-
    turtle.place()
114+
    turtle.select(refSlt)
115
    if turtle.getItemCount(refSlt) > 1 then
116
      seedy = true
117
    else
118
      seedy = selectNonRefSlot()
119
    end
120
    i = i + 1
121
  end
122
  
123
  if seedy then
124
    if prevRow[block]== refSlt then
125
      prevRow[block] = 0
126
    else
127
      prevRow[block] = refSlt
128
      turtle.place()
129
    end -- if matches
130
  end -- if seedy
131
  
132
  return seedy
133
end
134
135
local function reapRow( lth )
136
  print( "Reaping a row ")
137
  
138
-- Loop until length is reached or
139
-- the way is blocked
140
  local keepGoing = true
141
  local placeR = 1
142
  
143
-- lth 0 just means that really 
144
-- the length is not yet determined 
145
  local firstRow = lth == 0
146
  while keepGoing do
147
    
148
    turtle.dig() -- reaps
149
    keepGoing = turtle.forward()
150
    
151
    -- sees if it can keepGoing
152
    if keepGoing then
153
      placeR = placeR + 1
154
      if placeR >= lth and lth > 0 then
155
        keepGoing = false
156
      end
157
      -- prevents infinite loop
158
      if placeR > 1023 then
159
        keepGoing = false
160
      end
161
    else
162
      if firstRow == false then
163-
local function sowRow(lnth, indx)
163+
164
        placeR = 0
165
        print("placeR="..placeR)
166
      end
167
    end
168
    
169
  end -- while loop
170
  
171
  lth = placeR
172
  return lth
173
end
174
175-
      seedy = sow(indx)
175+
176
-- @param lnth row length
177
-- @param indx which row
178
-- @param prevRow array of crop plantings
179
--  for the previous row
180
local function sowRow(lnth, indx, prevRow)
181
	print( "Sowing a row." )
182
  local block = 1
183
  local seedy = true
184
  
185
  -- while it has length & seeds
186
  while block < lnth and seedy do
187
    
188
    seedy = turtle.back()
189
    turtle.dig()
190
191
    -- if there are still seeds
192
    if seedy then
193
      seedy = sow(indx, block, prevRow)
194
    else
195
    	print("sowRow(): seedy== false" )
196
    end
197
    block = block + 1
198
  end
199
  
200
  -- return if there's seeds still.
201
  return seedy
202
  
203
end
204
205
-- Harvests and plants every other row
206
-- @param rows number of plantable rows
207
-- @return found row length. If stopped
208
--  unexpectedly this will be 0.
209
local function reapAndSow( rows )
210
  print("beginning reapAndSow()")
211
  -- Find out how many slots are for
212
  -- reference.
213
  local plntbl = initPlntblCnt()
214
  
215
  print("The first "..plntbl..
216
    " slots \nare deemed plantable")
217
  
218
  local prevRow = {}
219
  print("rows = ".. rows)
220
  local rowLength = 0
221
  local rowIndex = 0
222
  local isSeedy = true
223
  local isFirst = true
224
  
225-
        isSeedy=sowRow(rowLength, rowIndex)
225+
226
    print( "rowIndex ".. rowIndex )
227
    
228
    -- If there was an unexpected 
229
    -- stop in previous row, then 
230
    -- rowLength will be its initial
231
    -- value, 0
232
    if rowLength<= 0 and isFirst== false then   
233
      -- Stop loop
234
      isSeedy = false
235
    else
236
      -- After the first row
237
      if isFirst == false then
238
        -- Move to the next row
239
        turtle.turnRight()
240
        turtle.forward()
241-
  local forwards = (rows - 1) * 2
241+
242
      end
243
      rowLength = reapRow( rowLength )
244
      if isFirst then
245
        -- initializes previous array
246
        for i = 1, rowLength do
247
          prevRow[i] = 0
248
        end
249
      end
250
      if rowLength > 0 then
251
        isSeedy=sowRow(rowLength, rowIndex, prevRow)
252
        -- if stopped, zero-out.
253
        rowLength= isSeedy and rowLength or 0
254
      end
255
    end --else
256
    isFirst = false
257
    rowIndex = rowIndex + 1
258
  end -- while
259
  return rowLength
260
end 
261
262
local function returnAndStore(rows)
263
  print( "Returning to the start.")
264
  
265
  turtle.turnLeft()
266
  local canGo = true
267
  local forwards = (rows - 1)
268
  for i= 1, forwards do
269
    canGo = turtle.forward()
270
  end
271
  
272
  turtle.turnLeft()
273
  if canGo then
274
    for i = 16, 1, -1 do
275
      turtle.select(i)
276
      if i > plntblCnt then
277
        turtle.drop()
278
      else
279
        turtle.drop( 
280
            turtle.getItemCount(i)- 1)
281
      end
282
    end
283
  end --canGo
284
  turtle.turnLeft()
285
  turtle.turnLeft()
286
  return canGo
287
end
288
289
local tArgs = {...}
290
local rowCntFromUsr = 0
291
292
--[[ If user supplies a good number, 
293
this uses it as row count.  If user
294
supplied a bad argument, it indicates.
295
]]
296
local argCount = table.getn(tArgs)
297
local badArg = false
298
local badArgMsg = "Argument OK"
299
if argCount > 0 then
300
  rowCntFromUsr = tArgs[1]
301
  if tonumber(rowCntFromUsr)==nil then
302
    badArg = true
303
    badArgMsg = "Argument not a number"
304
  else 
305-
        local waitTime = (40* 60)-duration
305+
306
  end
307
  
308
else
309
  badArg = true
310
  badArgMsg = "Supply how many rows"
311
end
312
313
if badArg then
314
  print( badArgMsg )
315
  print("Put at least 1 of something" )
316
  print(" plantable in first few slots.")
317
else
318
319
  --[[ Start ]]
320
  -- three times, if enough fuel
321
  local n = 1
322
  local okSoFar = true
323
  while n<3 and okSoFar do
324
    local startTime = os.clock();
325
    local zeroStopped = reapAndSow( rowCntFromUsr )
326
    if zeroStopped > 0 then
327
      okSoFar = returnAndStore(rowCntFromUsr)
328
      if okSoFar then
329
        local endTime = os.clock()
330
        local duration = endTime - startTime
331
        local waitTime = (60* 60)-duration
332
        local nextTime = endTime+ waitTime
333
        print("harvest: "..(n).." of 3")
334
        if n < 3 then
335
          os.sleep(waitTime)
336
        end
337
      end --okSofar
338
    else
339
      okSoFar = false
340
    end
341
    n = n + 1
342
  end -- of three times.
343
  
344
  if okSoFar == false then
345
    print("Out of fuel or other stop.")
346
  end
347
end