From 80674f76977f38ad1750531d9e990c73957fd1cd Mon Sep 17 00:00:00 2001 From: abstern <58846922+abstern@users.noreply.github.com> Date: Tue, 12 Jan 2021 21:37:51 +0100 Subject: [PATCH 1/2] [feature] autofarm: fallow farms when no further plants requested --- plugins/autofarm.cpp | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/plugins/autofarm.cpp b/plugins/autofarm.cpp index dd7aca977..a50f761d2 100644 --- a/plugins/autofarm.cpp +++ b/plugins/autofarm.cpp @@ -201,11 +201,26 @@ public: { // this algorithm attempts to change as few farms as possible, while ensuring that // the number of farms planting each eligible plant is "as equal as possible" - - if (farms.empty() || plants.empty()) - return; // do nothing if there are no farms or no plantable plants - + int season = *df::global::cur_season; + + if (farms.empty() || plants.empty()) + { + // if no more plants were requested, fallow all farms + // if there were no farms, do nothing + for (auto farm : farms) + { + int o = farm->plant_id[season]; + if (o != -1) + { + farm->plant_id[season] = -1; + out << "autofarm: changing farm #" << farm->id << + " from " << ((o == -1) ? "NONE" : world->raws.plants.all[o]->name) << + " to NONE" << endl; + } + } + return; + } int min = farms.size() / plants.size(); // the number of farms that should plant each eligible plant, rounded down int extra = farms.size() - min * plants.size(); // the remainder that cannot be evenly divided From 78df8902172b2d6518014bf78357b03b961afb8b Mon Sep 17 00:00:00 2001 From: abstern <58846922+abstern@users.noreply.github.com> Date: Wed, 13 Jan 2021 08:55:48 +0100 Subject: [PATCH 2/2] factor out functions to get plant names and set individual farms --- plugins/autofarm.cpp | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/plugins/autofarm.cpp b/plugins/autofarm.cpp index a50f761d2..b20158ce2 100644 --- a/plugins/autofarm.cpp +++ b/plugins/autofarm.cpp @@ -197,6 +197,27 @@ public: } } + string get_plant_name(int plant_id) + { + df::plant_raw *raw = df::plant_raw::find(plant_id); + if (raw) + return raw->name; + else + return "NONE"; + } + + void set_farm(color_ostream& out, int new_plant_id, df::building_farmplotst* farm, int season) + { + int old_plant_id = farm->plant_id[season]; + if (old_plant_id != new_plant_id) + { + farm->plant_id[season] = new_plant_id; + out << "autofarm: changing farm #" << farm->id << + " from " << get_plant_name(old_plant_id) << + " to " << get_plant_name(new_plant_id) << endl; + } + } + void set_farms(color_ostream& out, set plants, vector farms) { // this algorithm attempts to change as few farms as possible, while ensuring that @@ -210,14 +231,7 @@ public: // if there were no farms, do nothing for (auto farm : farms) { - int o = farm->plant_id[season]; - if (o != -1) - { - farm->plant_id[season] = -1; - out << "autofarm: changing farm #" << farm->id << - " from " << ((o == -1) ? "NONE" : world->raws.plants.all[o]->name) << - " to NONE" << endl; - } + set_farm(out, -1, farm, season); } return; } @@ -251,11 +265,7 @@ public: { // pick one of the excess farms and change it to plant this plant df::building_farmplotst* farm = toChange.front(); - int o = farm->plant_id[season]; - farm->plant_id[season] = n; - out << "autofarm: changing farm #" << farm->id << - " from " << ((o == -1) ? "NONE" : world->raws.plants.all[o]->name) << - " to " << ((n == -1) ? "NONE" : world->raws.plants.all[n]->name) << endl; + set_farm(out, n, farm, season); toChange.pop(); if (c++ == min) extra--;