forked from huchenlei/auto-crossbreeding
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathaction.lua
262 lines (205 loc) · 5.86 KB
/
action.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
local component = require('component')
local robot = require('robot')
local sides = require('sides')
local computer = require('computer')
local os = require('os')
local database = require('database')
local gps = require('gps')
local config = require('config')
local scanner = require('scanner')
local events = require('events')
local inventory_controller = component.inventory_controller
local redstone = component.redstone
local restockAll, cleanUp -- Forward declaration
local function needCharge()
return computer.energy() / computer.maxEnergy() < config.needChargeLevel
end
local function fullyCharged()
return computer.energy() / computer.maxEnergy() > 0.99
end
local function fullInventory()
for i=1, robot.inventorySize() do
if robot.count(i) == 0 then
return false
end
end
return true
end
local function restockStick()
local selectedSlot = robot.select()
gps.go(config.stickContainerPos)
robot.select(robot.inventorySize() + config.stickSlot)
for i=1, inventory_controller.getInventorySize(sides.down) do
os.sleep(0)
inventory_controller.suckFromSlot(sides.down, i, 64-robot.count())
if robot.count() == 64 then
break
end
end
robot.select(selectedSlot)
end
local function dumpInventory()
local selectedSlot = robot.select()
gps.go(config.storagePos)
for i=1, (robot.inventorySize() + config.storageStopSlot) do
os.sleep(0)
if robot.count(i) > 0 then
robot.select(i)
for e=1, inventory_controller.getInventorySize(sides.down) do
if inventory_controller.getStackInSlot(sides.down, e) == nil then
inventory_controller.dropIntoSlot(sides.down, e)
break
end
end
end
end
robot.select(selectedSlot)
end
local function placeCropStick(count)
local selectedSlot = robot.select()
if count == nil then
count = 1
end
if robot.count(robot.inventorySize() + config.stickSlot) < count + 1 then
gps.save()
restockStick()
gps.resume()
end
robot.select(robot.inventorySize() + config.stickSlot)
inventory_controller.equip()
for _=1, count do
robot.useDown()
end
inventory_controller.equip()
robot.select(selectedSlot)
end
local function deweed()
local selectedSlot = robot.select()
if config.keepDrops and fullInventory() then
gps.save()
dumpInventory()
gps.resume()
end
robot.select(robot.inventorySize() + config.spadeSlot)
inventory_controller.equip()
robot.useDown()
if config.keepDrops then
robot.suckDown()
end
inventory_controller.equip()
robot.select(selectedSlot)
end
local function pulseDown()
redstone.setOutput(sides.down, 15)
os.sleep(0.1)
redstone.setOutput(sides.down, 0)
end
local function transplant(src, dest)
local selectedSlot = robot.select()
gps.save()
robot.select(robot.inventorySize() + config.binderSlot)
inventory_controller.equip()
-- Transfer to relay location
gps.go(src)
robot.useDown(sides.down, true)
gps.go(config.dislocatorPos)
pulseDown()
-- Transfer crop to destination
robot.useDown(sides.down, true)
gps.go(dest)
local crop = scanner.scan()
if crop.name == 'air' then
placeCropStick()
elseif crop.isCrop == false then
database.addToStorage(crop)
gps.go(gps.storageSlotToPos(database.nextStorageSlot()))
placeCropStick()
end
robot.useDown(sides.down, true)
gps.go(config.dislocatorPos)
pulseDown()
-- Reprime binder
robot.useDown(sides.down, true)
-- Destroy original crop
inventory_controller.equip()
gps.go(config.relayFarmlandPos)
robot.swingDown()
if config.KeepDrops then
robot.suckDown()
end
gps.resume()
robot.select(selectedSlot)
end
function cleanUp()
for slot=1, config.workingFarmArea, 1 do
-- Scan
gps.go(gps.workingSlotToPos(slot))
local crop = scanner.scan()
-- Remove all children and empty parents
if slot % 2 == 0 or crop.name == 'emptyCrop' then
robot.swingDown()
-- Remove bad parents
elseif crop.isCrop and crop.name ~= 'air' then
if scanner.isWeed(crop, 'working') then
robot.swingDown()
end
end
-- Pickup
if config.KeepDrops then
robot.suckDown()
end
end
events.setNeedCleanup(false)
restockAll()
end
local function primeBinder()
local selectedSlot = robot.select()
robot.select(robot.inventorySize() + config.binderSlot)
inventory_controller.equip()
-- Use binder at start to reset it, if already primed
robot.useDown(sides.down, true)
gps.go(config.dislocatorPos)
robot.useDown(sides.down)
inventory_controller.equip()
robot.select(selectedSlot)
end
local function charge()
gps.go(config.chargerPos)
gps.turnTo(1)
repeat
os.sleep(0.5)
if events.needExit() then
if events.needCleanup() and config.cleanUp then
events.setNeedCleanup(false)
cleanUp()
end
os.exit() -- Exit here to leave robot in starting position
end
until fullyCharged()
end
function restockAll()
dumpInventory()
restockStick()
charge()
end
local function initWork()
events.initEvents()
events.hookEvents()
charge()
database.resetStorage()
primeBinder()
restockAll()
end
return {
needCharge = needCharge,
charge = charge,
restockStick = restockStick,
dumpInventory = dumpInventory,
restockAll = restockAll,
placeCropStick = placeCropStick,
deweed = deweed,
pulseDown = pulseDown,
transplant = transplant,
cleanUp = cleanUp,
initWork = initWork
}