forked from xyqyear/auto-crossbreeding
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaction.lua
241 lines (211 loc) · 6.08 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
local component = require("component")
local robot = require("robot")
local computer = require("computer")
local inventory_controller = component.inventory_controller
local os = require("os")
local sides = require("sides")
local gps = require("gps")
local config = require("config")
local signal = require("signal")
local scanner = require("scanner")
local posUtil = require("posUtil")
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 charge(resume)
if resume ~= false then
gps.save()
end
gps.go(config.chargerPos)
repeat
os.sleep(0.5)
until fullyCharged()
if resume ~= false then
gps.resume()
end
end
local function restockStick(resume)
local selectedSlot = robot.select()
if resume ~= false then
gps.save()
end
gps.go(config.stickContainerPos)
robot.select(robot.inventorySize()+config.stickSlot)
for i=1, inventory_controller.getInventorySize(sides.down) do
inventory_controller.suckFromSlot(sides.down, i, 64-robot.count())
if robot.count() == 64 then
break
end
end
if resume ~= false then
gps.resume()
end
robot.select(selectedSlot)
end
local function dumpInventory(resume)
local selectedSlot = robot.select()
if resume ~= false then
gps.save()
end
gps.go(config.storagePos)
for i=1, robot.inventorySize()+config.storageStopSlot do
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
if resume ~= false then
gps.resume()
end
robot.select(selectedSlot)
end
local function restockAll()
gps.save()
if config.takeCareOfDrops then
dumpInventory()
end
restockStick(false)
charge(false)
gps.resume()
end
local function placeCropStick(count)
if count == nil then
count = 1
end
local selectedSlot = robot.select()
if robot.count(robot.inventorySize()+config.stickSlot) < count + 1 then
restockStick()
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.takeCareOfDrops and fullInventory() then
dumpInventory()
end
robot.select(robot.inventorySize()+config.spadeSlot)
inventory_controller.equip()
robot.useDown()
if config.takeCareOfDrops then
robot.suckDown()
end
inventory_controller.equip()
robot.select(selectedSlot)
end
local function transplant(src, dest)
local selectedSlot = robot.select()
gps.save()
robot.select(robot.inventorySize()+config.binderSlot)
inventory_controller.equip()
-- transfer the crop to the relay location
gps.go(config.dislocatorPos)
robot.useDown(sides.down)
gps.go(src)
robot.useDown(sides.down, true) -- sneak-right-click on crops to prevent harvesting
gps.go(config.dislocatorPos)
signal.pulseDown()
-- transfer the crop to the destination
robot.useDown(sides.down)
gps.go(dest)
if scanner.scan().name == "air" then
placeCropStick()
end
robot.useDown(sides.down, true)
gps.go(config.dislocatorPos)
signal.pulseDown()
-- destroy the original crop
gps.go(config.relayFarmlandPos)
deweed()
robot.swingDown()
if config.takeCareOfDrops then
robot.suckDown()
end
inventory_controller.equip()
gps.resume()
robot.select(selectedSlot)
end
local function transplantToMultifarm(src, dest)
local globalDest = posUtil.multifarmPosToGlobalPos(dest)
local optimalDislocatorSet = posUtil.findOptimalDislocator(dest)
local dislocatorPos = optimalDislocatorSet[1]
local relayFarmlandPos = optimalDislocatorSet[2]
local selectedSlot = robot.select()
gps.save()
if robot.count(robot.inventorySize()+config.stickSlot) < 2 then
restockStick()
end
robot.select(robot.inventorySize()+config.binderSlot)
inventory_controller.equip()
-- transfer the crop to the relay location
gps.go(config.elevatorPos)
gps.down(3)
gps.go(dislocatorPos)
robot.useDown(sides.down)
gps.go(config.elevatorPos)
gps.up(3)
gps.go(src)
robot.useDown(sides.down, true) -- sneak-right-click on crops to prevent harvesting
gps.go(config.elevatorPos)
gps.down(3)
gps.go(dislocatorPos)
signal.pulseDown()
if not (relayFarmlandPos[1] == globalDest[1] and relayFarmlandPos[2] == globalDest[2]) then
-- transfer the crop to the destination
robot.useDown(sides.down)
gps.go(globalDest)
placeCropStick()
robot.useDown(sides.down, true)
gps.go(dislocatorPos)
signal.pulseDown()
-- destroy the original crop
gps.go(relayFarmlandPos)
robot.swingDown()
end
gps.go(config.elevatorPos)
gps.up(3)
inventory_controller.equip()
gps.resume()
robot.select(selectedSlot)
end
local function destroyAll()
for slot=2, config.farmArea, 2 do
gps.go(posUtil.farmToGlobal(slot))
robot.swingDown()
if config.takeCareOfDrops then
robot.suckDown()
end
end
end
return {
needCharge = needCharge,
charge = charge,
restockStick = restockStick,
restockAll = restockAll,
placeCropStick = placeCropStick,
deweed = deweed,
transplant = transplant,
transplantToMultifarm = transplantToMultifarm,
destroyAll = destroyAll
}