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