View difference between Paste ID: TncVNv9C and 2E4dnyRZ
SHOW: | | - or go back to the newest paste.
1
event = require("event")
2
robot = require("robot")
3
term = require("term")
4
component = require("component")
5
6
local farmSize = "exit"
7
local X = 1
8
9
local function display(t)
10
  io.write(t)
11
  os.sleep(1)
12
end
13
14
local function askSize()
15
  term.clear()
16
  term.setCursor(1,1)
17
  io.write("How large does the farm need to be?\nType 'exit' if you want to exit the program.\n")
18
  farmSize = io.read()
19
  if farmSize == "exit" then
20
    os.exit()
21
  elseif type(tonumber(farmSize)) == "number" then
22
    if farmSize % 2 == 0 then 
23
      display("Working on it!")
24
      term.clear()
25
    else 
26
      display("Please enter an even number!")
27
      askSize()
28
    end
29
  else
30
    display("Please enter a number.")
31
    askSize()
32
  end
33
  term.clear()
34
end
35
36
local function getSeeds()
37
  repeat local y = robot.suckDown() 
38
  until y == false
39
end
40
41
local function check()
42
  robot.useDown()
43
  getSeeds()
44
  robot.forward()
45
end
46
47
local function checkX(n)
48
  for i=1, n do
49
  check()
50
  end
51
end
52
53
local function turnRight()
54
  robot.turnRight()
55
  robot.forward()
56
  robot.turnRight()
57
  robot.forward()
58
  X = 1
59
end
60
61
local function turnLeft()
62
  robot.turnLeft()
63
  robot.forward()
64
  robot.turnLeft()
65
  robot.forward()
66
  X = 0
67
end
68
69
local function turn()
70
  if X==0 then
71
    turnRight()
72
  else 
73
    turnLeft()
74
  end
75
end
76
77
local function size(n)
78
  for i=1, n do
79
  checkX(n)
80
  turn()
81
  end
82
end
83
84
local function dropOff()
85
  for i=1, 16 do
86
    robot.select(i)
87
    robot.dropDown()
88
  end
89
end
90
91
local function exitFarm()
92
  local c = 0
93
  term.clear()
94
  term.setCursor(1,1)
95
  io.write("Press 'e' to exit during the cool down period!")
96
  local a, b, c, d = event.pull(60, "key_down")
97
  if c ~= nil then
98
    if string.char(c) == "e" then
99
      term.clear()
100
      term.setCursor(1,1)
101
      display("Exitting!\n")
102
      os.exit()
103
    else
104
      os.sleep(60)
105
    end
106
  end
107
  term.clear()
108
end
109
110
term.clear()
111
term.setCursor(1,1)
112
display("Farming robot v.2\n")
113
askSize()
114
io.write("Farming...")
115
local S = tonumber(farmSize)
116
117
while true do
118
  robot.forward()
119
  size(S)
120
  robot.turnRight()
121
  checkX(S)
122
  robot.turnLeft()
123
  robot.back()
124
  dropOff()
125
  exitFarm()
126
end