allow buildingplan to be "always on"

develop
Myk Taylor 2023-01-17 21:55:27 -08:00
parent bc42f7c73c
commit 7d8066f3c7
No known key found for this signature in database
2 changed files with 18 additions and 30 deletions

@ -81,6 +81,9 @@
# Allow DFHack tools to overlay functionality and information on the DF screen # Allow DFHack tools to overlay functionality and information on the DF screen
enable overlay 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) # Dwarf Manipulator (simple in-game Dwarf Therapist replacement)
#enable manipulator #enable manipulator

@ -46,7 +46,6 @@ static const string CONFIG_KEY = string(plugin_name) + "/config";
static const string BLD_CONFIG_KEY = string(plugin_name) + "/building"; static const string BLD_CONFIG_KEY = string(plugin_name) + "/building";
enum ConfigValues { enum ConfigValues {
CONFIG_IS_ENABLED = 0,
CONFIG_BLOCKS = 1, CONFIG_BLOCKS = 1,
CONFIG_BOULDERS = 2, CONFIG_BOULDERS = 2,
CONFIG_LOGS = 3, CONFIG_LOGS = 3,
@ -79,12 +78,12 @@ public:
PlannedBuilding(color_ostream &out, df::building *building) : id(building->id) { PlannedBuilding(color_ostream &out, df::building *building) : id(building->id) {
DEBUG(status,out).print("creating persistent data for building %d\n", id); DEBUG(status,out).print("creating persistent data for building %d\n", id);
config = DFHack::World::AddPersistentData(BLD_CONFIG_KEY); bld_config = DFHack::World::AddPersistentData(BLD_CONFIG_KEY);
set_config_val(config, BLD_CONFIG_ID, id); set_config_val(bld_config, BLD_CONFIG_ID, id);
} }
PlannedBuilding(DFHack::PersistentDataItem &config) PlannedBuilding(DFHack::PersistentDataItem &bld_config)
: config(config), id(get_config_val(config, BLD_CONFIG_ID)) { } : bld_config(bld_config), id(get_config_val(bld_config, BLD_CONFIG_ID)) { }
void remove(color_ostream &out); void remove(color_ostream &out);
@ -102,7 +101,7 @@ public:
} }
private: private:
DFHack::PersistentDataItem config; DFHack::PersistentDataItem bld_config;
}; };
static PersistentDataItem config; static PersistentDataItem config;
@ -144,16 +143,10 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector <Plugin
} }
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) { DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
if (!Core::getInstance().isWorldLoaded()) {
out.printerr("Cannot enable %s without a loaded world.\n", plugin_name);
return CR_FAILURE;
}
if (enable != is_enabled) { if (enable != is_enabled) {
is_enabled = enable; is_enabled = enable;
DEBUG(status,out).print("%s from the API; persisting\n", DEBUG(status,out).print("%s from the API; persisting\n",
is_enabled ? "enabled" : "disabled"); is_enabled ? "enabled" : "disabled");
set_config_bool(config, CONFIG_IS_ENABLED, is_enabled);
} else { } else {
DEBUG(status,out).print("%s from the API, but already %s; no action\n", DEBUG(status,out).print("%s from the API, but already %s; no action\n",
is_enabled ? "enabled" : "disabled", is_enabled ? "enabled" : "disabled",
@ -174,24 +167,17 @@ DFhackCExport command_result plugin_load_data (color_ostream &out) {
if (!config.isValid()) { if (!config.isValid()) {
DEBUG(status,out).print("no config found in this save; initializing\n"); DEBUG(status,out).print("no config found in this save; initializing\n");
config = World::AddPersistentData(CONFIG_KEY); config = World::AddPersistentData(CONFIG_KEY);
set_config_bool(config, CONFIG_IS_ENABLED, is_enabled);
set_config_bool(config, CONFIG_BLOCKS, true); set_config_bool(config, CONFIG_BLOCKS, true);
set_config_bool(config, CONFIG_BOULDERS, true); set_config_bool(config, CONFIG_BOULDERS, true);
set_config_bool(config, CONFIG_LOGS, true); set_config_bool(config, CONFIG_LOGS, true);
set_config_bool(config, CONFIG_BARS, false); set_config_bool(config, CONFIG_BARS, false);
} }
// we have to copy our enabled flag into the global plugin variable, but DEBUG(status,out).print("loading persisted state\n");
// all the other state we can directly read/modify from the persistent
// data structure.
is_enabled = get_config_bool(config, CONFIG_IS_ENABLED);
DEBUG(status,out).print("loading persisted enabled state: %s\n",
is_enabled ? "true" : "false");
vector<PersistentDataItem> building_configs;
World::GetPersistentData(&building_configs, BLD_CONFIG_KEY);
planned_buildings.clear(); planned_buildings.clear();
tasks.clear(); tasks.clear();
vector<PersistentDataItem> building_configs;
World::GetPersistentData(&building_configs, BLD_CONFIG_KEY);
const size_t num_building_configs = building_configs.size(); const size_t num_building_configs = building_configs.size();
for (size_t idx = 0; idx < num_building_configs; ++idx) for (size_t idx = 0; idx < num_building_configs; ++idx)
registerPlannedBuilding(out, PlannedBuilding(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) { DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) {
if (event == DFHack::SC_WORLD_UNLOADED) { if (event == DFHack::SC_WORLD_UNLOADED) {
if (is_enabled) { DEBUG(status,out).print("world unloaded; clearing state for %s\n", plugin_name);
DEBUG(status,out).print("world unloaded; disabling %s\n", planned_buildings.clear();
plugin_name); tasks.clear();
is_enabled = false;
planned_buildings.clear();
tasks.clear();
}
} }
return CR_OK; return CR_OK;
} }
@ -215,6 +197,9 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
static bool cycle_requested = false; static bool cycle_requested = false;
DFhackCExport command_result plugin_onupdate(color_ostream &out) { DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (!Core::getInstance().isWorldLoaded())
return CR_OK;
if (is_enabled && if (is_enabled &&
(cycle_requested || world->frame_counter - cycle_timestamp >= CYCLE_TICKS)) (cycle_requested || world->frame_counter - cycle_timestamp >= CYCLE_TICKS))
do_cycle(out); do_cycle(out);
@ -245,7 +230,7 @@ static command_result do_command(color_ostream &out, vector<string> &parameters)
CoreSuspender suspend; CoreSuspender suspend;
if (!Core::getInstance().isWorldLoaded()) { 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; return CR_FAILURE;
} }