Skip to content

Commit 78df890

Browse files
committed
factor out functions to get plant names and set individual farms
1 parent 80674f7 commit 78df890

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

plugins/autofarm.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,27 @@ class AutoFarm {
197197
}
198198
}
199199

200+
string get_plant_name(int plant_id)
201+
{
202+
df::plant_raw *raw = df::plant_raw::find(plant_id);
203+
if (raw)
204+
return raw->name;
205+
else
206+
return "NONE";
207+
}
208+
209+
void set_farm(color_ostream& out, int new_plant_id, df::building_farmplotst* farm, int season)
210+
{
211+
int old_plant_id = farm->plant_id[season];
212+
if (old_plant_id != new_plant_id)
213+
{
214+
farm->plant_id[season] = new_plant_id;
215+
out << "autofarm: changing farm #" << farm->id <<
216+
" from " << get_plant_name(old_plant_id) <<
217+
" to " << get_plant_name(new_plant_id) << endl;
218+
}
219+
}
220+
200221
void set_farms(color_ostream& out, set<int> plants, vector<df::building_farmplotst*> farms)
201222
{
202223
// this algorithm attempts to change as few farms as possible, while ensuring that
@@ -210,14 +231,7 @@ class AutoFarm {
210231
// if there were no farms, do nothing
211232
for (auto farm : farms)
212233
{
213-
int o = farm->plant_id[season];
214-
if (o != -1)
215-
{
216-
farm->plant_id[season] = -1;
217-
out << "autofarm: changing farm #" << farm->id <<
218-
" from " << ((o == -1) ? "NONE" : world->raws.plants.all[o]->name) <<
219-
" to NONE" << endl;
220-
}
234+
set_farm(out, -1, farm, season);
221235
}
222236
return;
223237
}
@@ -251,11 +265,7 @@ class AutoFarm {
251265
{
252266
// pick one of the excess farms and change it to plant this plant
253267
df::building_farmplotst* farm = toChange.front();
254-
int o = farm->plant_id[season];
255-
farm->plant_id[season] = n;
256-
out << "autofarm: changing farm #" << farm->id <<
257-
" from " << ((o == -1) ? "NONE" : world->raws.plants.all[o]->name) <<
258-
" to " << ((n == -1) ? "NONE" : world->raws.plants.all[n]->name) << endl;
268+
set_farm(out, n, farm, season);
259269
toChange.pop();
260270
if (c++ == min)
261271
extra--;

0 commit comments

Comments
 (0)