Skip to content

Commit d295136

Browse files
authored
Merge pull request DFHack#5560 from chdoc/nestboxes-burrow
[nestboxes] allow limiting egg protection to a designated burrow
2 parents 3e37083 + 7af372a commit d295136

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

docs/changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Template for new versions:
5555

5656
## New Features
5757

58+
- `nestboxes`: allow limiting egg protection to nestboxes inside a designated burrow
59+
5860
## Fixes
5961
- ``Units::getReadableName`` will no longer append a comma to the names of histfigs with no profession
6062

docs/plugins/nestboxes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,16 @@ This plugin will automatically scan for and forbid fertile eggs incubating in a
1010
nestbox so that dwarves won't come to collect them for eating. The eggs will
1111
hatch normally, even when forbidden.
1212

13+
You can control which eggs are collected and which eggs are protected by placing
14+
the nestboxes whose contents should be protected inside a burrow and running
15+
``nestboxes burrow <burrow_name>``. The default behavior of protecting all
16+
fertile eggs in nest boxes can be reestablished by running ``nestboxes all``.
17+
1318
Usage
1419
-----
1520

1621
::
1722

1823
enable nestboxes
24+
nestboxes burrow <burrow_name>
25+
nestbox all

plugins/nestboxes.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#include "Debug.h"
22
#include "PluginManager.h"
33

4+
#include "modules/Burrows.h"
45
#include "modules/Items.h"
56
#include "modules/Job.h"
67
#include "modules/Persistence.h"
78
#include "modules/World.h"
89

910
#include "df/buildingitemst.h"
1011
#include "df/building_nest_boxst.h"
12+
#include "df/burrow.h"
1113
#include "df/item.h"
1214
#include "df/item_eggst.h"
1315
#include "df/unit.h"
@@ -34,16 +36,24 @@ static PersistentDataItem config;
3436

3537
enum ConfigValues {
3638
CONFIG_IS_ENABLED = 0,
39+
CONFIG_BURROW = 1
3740
};
3841

3942
static const int32_t CYCLE_TICKS = 7; // need to react quickly when eggs are laid/unforbidden
4043
static int32_t cycle_timestamp = 0; // world->frame_counter at last cycle
4144

4245
static void do_cycle(color_ostream &out);
4346

47+
static command_result do_command(color_ostream &out, std::vector<string> &parameters);
4448
DFhackCExport command_result plugin_init(color_ostream &out, std::vector <PluginCommand> &commands) {
4549
DEBUG(control,out).print("initializing %s\n", plugin_name);
4650

51+
// provide a configuration interface for the plugin
52+
commands.push_back(PluginCommand(
53+
plugin_name,
54+
"Protect fertile eggs incubating in a nestbox.",
55+
do_command));
56+
4757
return CR_OK;
4858
}
4959

@@ -108,6 +118,65 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out) {
108118
return CR_OK;
109119
}
110120

121+
/////////////////////////////////////////////////////
122+
// configuration logic
123+
//
124+
125+
static void setBurrow(color_ostream &out, const string &burrow_name){
126+
auto burrow = Burrows::findByName(burrow_name);
127+
if (burrow) {
128+
config.set_int(CONFIG_BURROW, burrow->id);
129+
} else {
130+
config.set_int(CONFIG_BURROW, -1);
131+
}
132+
}
133+
134+
static df::burrow* getBurrow(color_ostream &out){
135+
int id = config.get_int(CONFIG_BURROW);
136+
auto burrow = df::burrow::find(id);
137+
if (!burrow) {
138+
config.set_int(CONFIG_BURROW, -1);
139+
}
140+
return burrow;
141+
}
142+
143+
static void printStatus(color_ostream &out){
144+
if (!is_enabled)
145+
out.print("%s is disabled\n", plugin_name);
146+
else {
147+
out.print("%s is enabled\n", plugin_name);
148+
auto burrow = getBurrow(out);
149+
if (burrow)
150+
{
151+
out.print("only protecting eggs inside burrow: %s\n", burrow->name.c_str());
152+
}
153+
else
154+
{
155+
out.print("protecting all fertile eggs\n");
156+
}
157+
}
158+
}
159+
160+
static command_result do_command(color_ostream &out, std::vector<string> &parameters){
161+
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode()) {
162+
out.printerr("Cannot run %s without a loaded fort.\n", plugin_name);
163+
return CR_FAILURE;
164+
}
165+
166+
if (parameters.size() == 0 || parameters[0] == "status")
167+
{
168+
printStatus(out);
169+
return CR_OK;
170+
} else if (parameters.size() > 1 && parameters[0] == "burrow") {
171+
setBurrow(out, parameters[1]);
172+
return CR_OK;
173+
} else if (parameters[0] == "all") {
174+
config.set_int(CONFIG_BURROW, -1);
175+
return CR_OK;
176+
} else {
177+
return CR_WRONG_USAGE;
178+
}
179+
}
111180
/////////////////////////////////////////////////////
112181
// cycle logic
113182
//
@@ -118,6 +187,9 @@ static void do_cycle(color_ostream &out) {
118187
// mark that we have recently run
119188
cycle_timestamp = world->frame_counter;
120189

190+
// see if egg protection is limited to a burrow
191+
auto burrow = getBurrow(out);
192+
121193
for (df::building_nest_boxst *nb : world->buildings.other.NEST_BOX) {
122194
for (auto &contained_item : nb->contained_items) {
123195
if (contained_item->use_mode == df::building_item_role_type::PERM)
@@ -126,6 +198,9 @@ static void do_cycle(color_ostream &out) {
126198
bool fertile = item->egg_flags.bits.fertile;
127199
if (item->flags.bits.forbid == fertile)
128200
continue;
201+
if (burrow && !Burrows::isAssignedTile(burrow, Items::getPosition(item))) {
202+
continue;
203+
}
129204
item->flags.bits.forbid = fertile;
130205
if (fertile && item->flags.bits.in_job) {
131206
// cancel any job involving the egg

0 commit comments

Comments
 (0)