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
3537enum ConfigValues {
3638 CONFIG_IS_ENABLED = 0 ,
39+ CONFIG_BURROW = 1
3740};
3841
3942static const int32_t CYCLE_TICKS = 7 ; // need to react quickly when eggs are laid/unforbidden
4043static int32_t cycle_timestamp = 0 ; // world->frame_counter at last cycle
4144
4245static void do_cycle (color_ostream &out);
4346
47+ static command_result do_command (color_ostream &out, std::vector<string> ¶meters);
4448DFhackCExport 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> ¶meters){
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