View difference between Paste ID: FMx5qRXF and eV1sY0CV
SHOW: | | - or go back to the newest paste.
1
-- Program for Farming Turtle from ComputerCraft mod.
2
-- AutoFarm Ver 1.0
3
-- Copyright  (c)  2015  Alexander Andreyev <aravsar@ya.ru>
4
--   Farming 9x9 field
5
-- I S x x x x x x x x
6
--   x x x x x x x x x
7
--   x x x x x x x x x
8
--   x x x x x x x x x
9
--   x x x x W x x x x
10
--   x x x x x x x x x
11
--   x x x x x x x x x
12
--   x x x x x x x x x
13
--   x x x x x x x x x
14
-- S - start position
15
-- I - possible inventory with which interacts turtle in the end (do_end_stuff)
16
-- W - water block in center
17
-- The turtle moves clockwise from S to W by spiral, 
18
-- moves back to the S, drop stuff to I and stops.
19
-- The turtle must be in one block above the field.
20
21
-- MAIN PROGRAM
22
args      = {...}
23
chest_name_part = 'Chest' -- a part of name of the I chest
24
rings     = {5,3}
25
fuel_slot = 16
26
27
function _refuel(fuel)
28
  turtle.select(fuel_slot)
29
  turtle.refuel(fuel)
30
end
31
32
function process_field(do_stuff, do_end_stuff)
33
  for i = 1, #rings do
34
    local n = rings[i]
35
    for j = 1, 4 do
36
      if j == 4 then n = n - 1 end
37
      for k = 1, n-1 do
38
        do_stuff()
39
        turtle.forward()
40
      end
41
      turtle.turnRight()
42
    end
43
    do_stuff()
44
    turtle.forward()
45
  end
46
  -- moving back on start position
47
  turtle.turnLeft()
48
  for i = 1, 4 do turtle.forward() end
49
  turtle.turnLeft()
50
  for i = 1, 4 do turtle.forward() end
51
52
  do_end_stuff()
53
54
  turtle.turnLeft()
55
  turtle.turnLeft()
56
end
57
58
-- PLANTING
59
plant_slots = {1,2,3,4}
60
61
function plant()
62
  for i = 1, #plant_slots do
63
    turtle.select(plant_slots[i])
64
    if turtle.getItemCount(plant_slots[i]) > 0 then
65
      turtle.placeDown()
66
      return
67
    end
68
  end
69
end
70
function planting()
71
  local s,_ = turtle.inspectDown()
72
  if s == false then
73
    turtle.digDown()
74
    plant()
75
  end
76
end
77
function on_end_planting()
78
end
79
80
-- HARVESTING
81
harvest_slots = {5,6,7,8}
82
wheat = {name = 'minecraft:wheat', metadata = 7} -- growed up wheat
83
84
function harvest(object)
85
  local _,m = turtle.inspectDown()
86
  if m.name ~= object.name or m.metadata ~= object.metadata then return end
87
88
  for i = 1, #harvest_slots do
89
    turtle.select(harvest_slots[i])
90
    if turtle.getItemSpace(harvest_slots[i]) > 0 then
91
      turtle.digDown()
92
      return
93
    end
94
  end
95
end
96
function harvesting()
97
  harvest(wheat)
98
end
99
function on_end_harvesting()
100
  local _,m = turtle.inspect()
101
  if m.name:find(chest_name_part) == nil then return end
102
  for i = 1, #harvest_slots do
103
    turtle.select(harvest_slots[i])
104
    turtle.drop()
105
  end
106
end
107
108
-- HARVEST AND PLANT
109
function harvest_and_plant( ... )
110
  harvesting()
111
  planting()
112
end
113
114
-- Controlling program
115
if args[1] == 'p' then
116
  _refuel(2)
117
  process_field(planting, on_end_planting)
118
elseif args[1] == 'h' then
119
  _refuel(2)
120
  process_field(harvesting, on_end_harvesting)
121
elseif args[1] == 'ph' then
122
  _refuel(2)
123
  process_field(harvest_and_plant, on_end_harvesting)
124
else
125
  print('p  - for planting')
126
  print('h  - for harvest')
127
  print('ph - for harvest and plant')
128
end