forked from DylanTaylor1/GTNH-CropAutomation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.lua
204 lines (165 loc) · 4.73 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
local component = require('component')
local robot = require('robot')
local sides = require('sides')
local computer = require('computer')
local os = require('os')
local gps = require('gps')
local config = require('config')
local signal = require('signal')
local scanner = require('scanner')
local posUtil = require('posUtil')
local database = require('database')
local inventory_controller = component.inventory_controller
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()
gps.go(config.chargerPos)
gps.turnTo(1)
repeat
os.sleep(0.5)
until fullyCharged()
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
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
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 restockAll()
dumpInventory()
restockStick()
charge()
gps.turnTo(1)
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.keepDrops and fullInventory() then
dumpInventory()
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 transplant(src, dest)
local selectedSlot = robot.select()
gps.save()
robot.select(robot.inventorySize()+config.binderSlot)
inventory_controller.equip()
-- TRANSFER TO RELAY LOCATION
gps.go(config.dislocatorPos)
robot.useDown(sides.down)
gps.go(src)
robot.useDown(sides.down, true)
gps.go(config.dislocatorPos)
signal.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(posUtil.storageSlotToPos(database.nextStorageSlot()))
placeCropStick()
end
robot.useDown(sides.down, true)
gps.go(config.dislocatorPos)
signal.pulseDown()
-- 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
local function cleanUp()
for slot=1, config.workingFarmArea, 1 do
-- Scan
gps.go(posUtil.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
restockAll()
end
return {
needCharge = needCharge,
charge = charge,
restockStick = restockStick,
dumpInventory = dumpInventory,
restockAll = restockAll,
placeCropStick = placeCropStick,
deweed = deweed,
transplant = transplant,
cleanUp = cleanUp
}