diff --git a/data/init/dfhack.tools.init b/data/init/dfhack.tools.init index 741298109..fdad95be6 100644 --- a/data/init/dfhack.tools.init +++ b/data/init/dfhack.tools.init @@ -81,6 +81,9 @@ # Allow DFHack tools to overlay functionality and information on the DF screen enable overlay +# Allow buildings to be placed now and built later when materials are available +enable buildingplan + # Dwarf Manipulator (simple in-game Dwarf Therapist replacement) #enable manipulator diff --git a/plugins/buildingplan.cpp b/plugins/buildingplan.cpp index 7080be4d5..09313c21a 100644 --- a/plugins/buildingplan.cpp +++ b/plugins/buildingplan.cpp @@ -46,7 +46,6 @@ static const string CONFIG_KEY = string(plugin_name) + "/config"; static const string BLD_CONFIG_KEY = string(plugin_name) + "/building"; enum ConfigValues { - CONFIG_IS_ENABLED = 0, CONFIG_BLOCKS = 1, CONFIG_BOULDERS = 2, CONFIG_LOGS = 3, @@ -79,12 +78,12 @@ public: PlannedBuilding(color_ostream &out, df::building *building) : id(building->id) { DEBUG(status,out).print("creating persistent data for building %d\n", id); - config = DFHack::World::AddPersistentData(BLD_CONFIG_KEY); - set_config_val(config, BLD_CONFIG_ID, id); + bld_config = DFHack::World::AddPersistentData(BLD_CONFIG_KEY); + set_config_val(bld_config, BLD_CONFIG_ID, id); } - PlannedBuilding(DFHack::PersistentDataItem &config) - : config(config), id(get_config_val(config, BLD_CONFIG_ID)) { } + PlannedBuilding(DFHack::PersistentDataItem &bld_config) + : bld_config(bld_config), id(get_config_val(bld_config, BLD_CONFIG_ID)) { } void remove(color_ostream &out); @@ -102,7 +101,7 @@ public: } private: - DFHack::PersistentDataItem config; + DFHack::PersistentDataItem bld_config; }; static PersistentDataItem config; @@ -144,16 +143,10 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector building_configs; - World::GetPersistentData(&building_configs, BLD_CONFIG_KEY); + DEBUG(status,out).print("loading persisted state\n"); planned_buildings.clear(); tasks.clear(); + vector building_configs; + World::GetPersistentData(&building_configs, BLD_CONFIG_KEY); const size_t num_building_configs = building_configs.size(); for (size_t idx = 0; idx < num_building_configs; ++idx) registerPlannedBuilding(out, PlannedBuilding(building_configs[idx])); @@ -201,13 +187,9 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) { DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) { if (event == DFHack::SC_WORLD_UNLOADED) { - if (is_enabled) { - DEBUG(status,out).print("world unloaded; disabling %s\n", - plugin_name); - is_enabled = false; - planned_buildings.clear(); - tasks.clear(); - } + DEBUG(status,out).print("world unloaded; clearing state for %s\n", plugin_name); + planned_buildings.clear(); + tasks.clear(); } return CR_OK; } @@ -215,6 +197,9 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan static bool cycle_requested = false; DFhackCExport command_result plugin_onupdate(color_ostream &out) { + if (!Core::getInstance().isWorldLoaded()) + return CR_OK; + if (is_enabled && (cycle_requested || world->frame_counter - cycle_timestamp >= CYCLE_TICKS)) do_cycle(out); @@ -245,7 +230,7 @@ static command_result do_command(color_ostream &out, vector ¶meters) CoreSuspender suspend; if (!Core::getInstance().isWorldLoaded()) { - out.printerr("Cannot run %s without a loaded world.\n", plugin_name); + out.printerr("Cannot configure %s without a loaded world.\n", plugin_name); return CR_FAILURE; }