forked from DylanTaylor1/GTNH-CropAutomation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoTier.lua
184 lines (148 loc) · 4.9 KB
/
autoTier.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
local gps = require('gps')
local action = require('action')
local database = require('database')
local scanner = require('scanner')
local posUtil = require('posUtil')
local config = require('config')
local breedRound = 0
local lowestTier
local lowestTierSlot
local lowestStat
local lowestStatSlot
-- =================== MINOR FUNCTIONS ======================
local function updateLowest()
local farm = database.getFarm()
lowestTier = 99
lowestTierSlot = 0
lowestStat = 99
lowestStatSlot = 0
-- Find lowest tier slot
for slot=1, config.workingFarmArea, 2 do
local crop = farm[slot]
if crop.isCrop then
if crop.name == 'air' or crop.name == 'emptyCrop' then
lowestTier = 0
lowestTierSlot = slot
break
elseif crop.tier < lowestTier then
lowestTier = crop.tier
lowestTierSlot = slot
end
end
end
-- Find lowest stat slot amongst the lowest tier
if config.statWhileTiering then
for slot=1, config.workingFarmArea, 2 do
local crop = farm[slot]
if crop.isCrop then
if crop.name == 'air' or crop.name == 'emptyCrop' then
lowestStat = 0
lowestStatSlot = slot
break
elseif crop.tier == lowestTier then
local stat = crop.gr + crop.ga - crop.re
if stat < lowestStat then
lowestStat = stat
lowestStatSlot = slot
end
end
end
end
end
end
local function checkChild(slot, crop)
if crop.isCrop and crop.name ~= 'emptyCrop' then
if crop.name == 'air' then
action.placeCropStick(2)
elseif scanner.isWeed(crop, 'working') then
action.deweed()
action.placeCropStick()
-- Seen before, tier up working farm
elseif database.existInStorage(crop) then
local stat = crop.gr + crop.ga - crop.re
if crop.tier > lowestTier then
action.transplant(posUtil.workingSlotToPos(slot), posUtil.workingSlotToPos(lowestTierSlot))
action.placeCropStick(2)
database.updateFarm(lowestTierSlot, crop)
updateLowest()
-- Not higher tier, stat up working farm
elseif (config.statWhileTiering and crop.tier == lowestTier and stat > lowestStat) then
action.transplant(posUtil.workingSlotToPos(slot), posUtil.workingSlotToPos(lowestStatSlot))
action.placeCropStick(2)
database.updateFarm(lowestStatSlot, crop)
updateLowest()
else
action.deweed()
action.placeCropStick()
end
-- Not seen before, move to storage
else
action.transplant(posUtil.workingSlotToPos(slot), posUtil.storageSlotToPos(database.nextStorageSlot()))
action.placeCropStick(2)
database.addToStorage(crop)
end
end
end
local function checkParent(slot, crop)
if crop.isCrop and crop.name ~= 'air' and crop.name ~= 'emptyCrop' then
if scanner.isWeed(crop, 'working') then
action.deweed()
database.updateFarm(slot, {isCrop=true, name='emptyCrop'})
updateLowest()
end
end
end
-- ====================== THE LOOP ======================
local function tierOnce()
for slot=1, config.workingFarmArea, 1 do
-- Terminal Condition
if breedRound > config.maxBreedRound then
print('autoTier: Max Breeding Round Reached!')
return false
end
-- Terminal Condition
if #database.getStorage() >= config.storageFarmArea then
print('autoTier: Storage Full!')
return false
end
-- Terminal Condition
if lowestTier >= config.autoTierThreshold then
print('autoTier: Minimum Tier Threshold Reached!')
return false
end
-- Scan
gps.go(posUtil.workingSlotToPos(slot))
local crop = scanner.scan()
if slot % 2 == 0 then
checkChild(slot, crop)
else
checkParent(slot, crop)
end
if action.needCharge() then
action.charge()
end
end
return true
end
-- ======================== MAIN ========================
local function init()
database.resetStorage()
database.scanFarm()
action.restockAll()
updateLowest()
print(string.format('autoTier: Target Tier %s', config.autoTierThreshold))
end
local function main()
init()
-- Loop
while tierOnce() do
breedRound = breedRound + 1
action.restockAll()
end
-- Finish
if config.cleanUp then
action.cleanUp()
end
print('autoTier: Complete!')
end
main()