forked from huchenlei/auto-crossbreeding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoSpread.lua
263 lines (218 loc) · 6.43 KB
/
autoSpread.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
263
-- Set farmSize=6, and storageFarmSize=9 in config.lua to use this script.
local database = require("database")
local gps = require("gps")
local posUtil = require("posUtil")
local scanner = require("scanner")
local action = require("action")
local config = require("config")
local robot = require("robot");
local args = {...}
--[[
The script aims to transfer stats from a parent crop type to the crop type
desired but have low stats.
[P, _, X],
[_, T, _],
[X, _, P],
where P represents parent crop, T represents target crop. X represents a non-farmland
block.
According to IC2 breeding rule, The offspring of 2 plants have 45% chance to be either
the parent plants, and 10% chance to be a new type of crop.
Y-axis
5 [6, 7, 18, ...]
4 [5, 8, 17, ...]
3 [4, 9, 16, ...]
2 [3, 10, 15, ...]
1 [2, 11, 14, ...]
0 [1, 12, 13, ...]
1 2 3 ... X-axis
]]
local targetCrop;
-- The min stats requirement for target crop to be put into storage farm.
local targetCropStatsThreshold = 45;
-- Current lowest stats of target crop in the breeding cell.
local targetCropLowestStats;
local targetCropLowestStatsSlot;
local BreedingCell = {};
function BreedingCell.new(center)
local cell = {
center=center,
stats=nil,
};
function cell.slots()
local slots = {};
for dx = -1, 1 do
for dy = -1, 1 do
table.insert(slots, posUtil.globalToFarm({center[1] + dx, center[2] + dy}));
end
end
return slots;
end
function cell.isChildren(slot)
local pos = posUtil.farmToGlobal(slot);
local c = cell.center;
return math.abs(c[1] - pos[1]) + math.abs(c[2] - pos[2]) == 1;
end
function cell.isActive()
return stats == nil;
end
return cell;
end
-- Mapping from slot# to breeding cell.
local breedingCellMap = {};
local breedingCells = {};
for x=1, config.farmSize // 3 do
for y=1, config.farmSize // 3 do
-- for 6x6 farm, y = 1, 4; x = 2, 5
local centerX = 3 * (x - 1) + 2;
local centerY = 3 * (y - 1) + 1;
local cell = BreedingCell.new({centerX, centerY});
for _, slot in ipairs(cell.slots()) do
breedingCellMap[slot] = cell;
end
table.insert(breedingCells, cell);
end
end
local CropQueue = {};
function CropQueue.new(slotToStatMapping)
local q = {
stats=slotToStatMapping,
};
function q.updateStatsAtSlot(slot, stat)
if q.lowestStat > stat then
q.lowestStat = stat;
q.lowestStatSlot = slot;
end
q.stats[slot] = stat;
end
function q.updateLowest()
q.lowestStat = 64;
for slot, stat in pairs(q.stats) do
if stat < q.lowestStat then
q.lowestStat = stat;
q.lowestStatSlot = slot;
end
end
end
--[[ Try replace lowest stat slot in the queue with incoming pair.
Returns true if the replacement is successful. ]]
function q.replaceLowest(slot, stat)
if stat > q.lowestStat then
action.transplant(posUtil.farmToGlobal(slot), posUtil.farmToGlobal(q.lowestStatSlot));
q.stats[q.lowestStatSlot] = stat;
q.updateLowest();
return true;
end
return false;
end
q.updateLowest();
return q;
end
local targetCropQueue;
local function isWeed(crop)
return crop.name == "weed" or
crop.name == "Grass" or
crop.gr > 21 or
(crop.name == "venomilia" and crop.gr > 7);
end
local function checkChildren(slot, crop)
if crop.name == "air" then
action.placeCropStick(2);
return;
end
if (not config.assumeNoBareStick) and crop.name == "crop" then
action.placeCropStick();
return;
end
if not crop.isCrop then
return;
end
if isWeed(crop) then
action.deweed();
action.placeCropStick();
return;
end
if crop.name == targetCrop then
local stat = calculateStats(crop);
-- Populate breeding cells with high stats crop as priority.
if targetCropQueue.lowestStat < targetCropStatsThreshold then
if targetCropQueue.replaceLowest(slot, stat) then
return;
end
end
if stat >= targetCropStatsThreshold then
action.transplant(posUtil.farmToGlobal(slot), posUtil.storageToGlobal(database.nextStorageSlot()));
database.addToStorage(crop);
action.placeCropStick(2);
return;
end
end
action.deweed();
action.placeCropStick();
end
function calculateStats(crop)
return crop.gr + crop.ga - crop.re;
end
local function spreadOnce()
for slot=1, config.farmArea, 1 do
local farmPos = posUtil.farmToGlobal(slot);
gps.go(farmPos);
local crop = scanner.scan();
local cell = breedingCellMap[slot];
if cell.isChildren(slot) then
checkChildren(slot, crop);
end
if #database.getStorage() >= 81 then
return true;
end
if action.needCharge() then
action.charge()
end
end
return false
end
local function cleanup()
for slot=1, config.farmArea, 1 do
local farmPos = posUtil.farmToGlobal(slot);
gps.go(farmPos);
local cell = breedingCellMap[slot];
if cell.isChildren(slot) then
robot.swingDown();
if config.takeCareOfDrops then
robot.suckDown();
end
end
end
end
local function init()
gps.save();
local stats = {};
for i, cell in ipairs(breedingCells) do
local pos = cell.center;
local slot = posUtil.globalToFarm(pos);
gps.go(pos);
local crop = scanner.scan();
stats[slot] = calculateStats(crop);
if i == 1 then
targetCrop = crop.name;
print(string.format('Target crop recognized: %s.', targetCrop));
end
end
targetCropQueue = CropQueue.new(stats);
action.restockAll();
gps.resume();
end
local function main()
init()
while not spreadOnce() do
gps.go({0, 0})
action.restockAll()
end
gps.go({0,0})
if #args == 1 and args[1] == "docleanup" then
cleanup();
gps.go({0,0});
end
gps.turnTo(1)
print("Done.\nThe Farm is filled up.")
end
main()