View difference between Paste ID: FYsUL3eW and Jyr9FZ7x
SHOW: | | - or go back to the newest paste.
1
local tArgs={...}
2
local turtleId
3
local isWirelessTurtle
4
local messageOutputFileName
5
6
7
local function printUsage()
8
  write([[
9
Usage:
10
farm <length> <width> [timer]
11
  <length> is the length of each row 
12
of the field
13
  <width> is the number of rows
14
  [timer] is an optional number of 
15
seconds to wait before repeating. 
16
-- Press any key to continue --]])
17
  os.pullEvent("key")
18
  print([[ 
19
                                 
20
Placement
21
The turtle should begin above a chest facing towards the first row, and will move right for each subsequent row. The chest should contain the item to be planted. If the plant has seeds different from the harvested items, like wheat, place a 2nd chest above the turtle for the output. 
22
Note: If area is not already plowed or
23
planted it must be a farming turtle.]])
24
end
25
  
26
  
27
if #tArgs<2 then
28
  printUsage()
29
  return
30
end
31
32
local length=tonumber(tArgs[1])
33
local width=tonumber(tArgs[2])
34
local repeatTime=tonumber(tArgs[3]) or 0
35
36
37
isWirelessTurtle = peripheral.isPresent("right")
38
if (isWirelessTurtle == true) then
39
  turtleId = os.getComputerLabel()
40
  rednet.open("right")
41
end
42
43
function writeMessage(message)
44
    print(message)
45
46
    -- If this turtle has a modem, then write the message to red net
47
    if (isWirelessTurtle == true) then
48
      if (turtleId == nil) then
49
        rednet.broadcast(message)
50
      else
51
        -- Broadcast the message (prefixed with the turtle's id)
52
        rednet.broadcast("[".. turtleId.."] "..message)
53
      end
54
    end
55
56
    if (messageOutputFileName ~= nil) then
57
      -- Open file, write message and close file (flush doesn't seem to work!)
58
      local outputFile = io.open(messageOutputFileName, "a")
59
      outputFile:write(message)
60
      outputFile:write("\n")
61
      outputFile:close()
62
    end
63
end
64
65
66
local function repeatUntil(func)
67
  while not func() do sleep(.5) end
68
end
69
70
local function step()
71-
  repeatUntil(turtle.forward)
71+
	if turtle.detect() 
72
	then turtle.dig() 
73
     end
74
  turtle.forward()
75
  --dry soil occassionally untils between dig and plant,
76
  --so repeat until successful on planting (or out of things to plant)
77
  local tryCount=0
78
  repeat
79
    turtle.digDown()
80
    tryCount=tryCount+1
81
  until turtle.placeDown() or turtle.getItemCount(1)==0 or tryCount==3
82
end
83
84
85
86
local function waitForKey(prompt)
87
	writeMessage(prompt)
88
  os.pullEvent("key")
89
end
90
91
92
local function harvest()
93
  --do I have enough fuel?
94
  local cost=(length+1)*width
95
  --if odd rows, add return trip
96
  if width%2==1 then
97
    cost=cost+length
98
  end
99
  while type(turtle.getFuelLevel())=="number" and cost>turtle.getFuelLevel() do
100
    waitForKey("Insufficient fuel. Place fuel in any slot and press a key to continue...")
101
102
    for i=2,16 do
103
      turtle.select(i)
104
      if turtle.refuel() then
105
        break
106
      end
107
    end
108
    turtle.select(1)
109
    term.clear()
110
  end
111
  writeMessage("Harvesting...")
112
  
113
  step()
114
  local turnNext=turtle.turnRight
115
  local turnOther=turtle.turnLeft
116
  for row=1,width do
117
    for pos=1,length-1 do
118
      step()
119
    end
120
    if row<width then
121
      turnNext()
122
      step()
123
      turnNext()
124
    end
125
    local temp=turnNext
126
    turnNext=turnOther
127
    turnOther=temp
128
  end
129
  
130
  --return to start position
131
  --if odd number of rows, we're at far end of rows, return
132
  if width%2==1 then
133
    for i=1,length do
134
      --if we hit something, just wait a bit and retry;
135
      repeatUntil(turtle.back)
136
    end
137
    turtle.turnLeft()
138
  else
139
    --was even rows, so at right end of rows facing opposite way
140
    repeatUntil(turtle.forward)
141
    turtle.turnRight()
142
  end
143
  --head back to first row
144
  for i=1,width-1 do
145
    repeatUntil(turtle.forward)
146
  end
147
  --turn to face starting direction
148
  turtle.turnRight()
149
  
150
  --unload!
151
  writeMessage("Unloading...")
152
  --anythign matching what's in slot 1 goes down, rest goes up
153
  for i=2,16 do
154
    turtle.select(i)
155
    if turtle.getItemCount(i)>0 then
156
      if turtle.compareTo(1) then
157
        while not turtle.dropDown() do
158
          waitForKey("input chest full, empty it and press any key to continue...")
159
	writeMessage("input chest full, empty it and press any key to continue...")
160
        end
161
      else
162
        while not turtle.dropUp() do
163
          waitForKey("output chest full, empty it and press any key to continue...")
164
	writeMessage("output chest full, empty it and press any key to continue...")
165
        end
166
      end
167
    end
168
  end
169
  turtle.select(1)
170
end
171
172
--load up with stuffs
173
turtle.select(1)
174
turtle.suckDown()
175
176
if repeatTime>0 then
177
  while true do
178
    harvest()
179
    term.clear()
180
    local i=repeatTime
181
    while i>0 do
182
      local tick,interval=1,"second"
183
      if i>3600 then
184
        tick,interval=3600,"hour"
185
      elseif i>60 then
186
        tick,interval=60,"minute"
187
      end
188
      local count=math.floor(i/tick)
189
      if count==1 and tick>1 then
190
        tick=i-tick
191
      else
192
        interval=interval.."s"
193
      end
194
      term.setCursorPos(1,1)
195
      term.clearLine()
196
      write("Harvesting again in "..count.." "..interval)
197
      sleep(tick)
198
      i=i-tick
199
    end
200
  end
201
else
202
  harvest()
203
  --drop seeds back
204
  turtle.select(1)
205
  turtle.dropDown()
206
end