Fix periodic events in several plugins

Also stop running buildingplan/fortplan while loading a world, and stop running
autogems while paused

Fixes #1286
develop
lethosor 2018-05-29 23:24:02 -04:00
parent cf4b790608
commit f57327e7c8
11 changed files with 15 additions and 55 deletions

@ -37,6 +37,11 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
================================================================================
# Future
## Fixes
- `autochop`, `autodump`, `autogems`, `automelt`, `autotrade`, `buildingplan`, `dwarfmonitor`, `fix-unit-occupancy`, `fortplan`, `stockflow`: fix issues with periodic tasks not working for some time after save/load cycles
- `buildingplan`, `fortplan`: stop running before a world has fully loaded
- `autogems`: stop running repeatedly when paused
## Misc Improvements
- `mousequery`:
- migrated several features from TWBT's fork

@ -864,16 +864,12 @@ DFhackCExport command_result plugin_onupdate (color_ostream &out)
if(!Maps::IsValid())
return CR_OK;
static decltype(world->frame_counter) last_frame_count = 0;
if (DFHack::World::ReadPauseState())
return CR_OK;
if (world->frame_counter - last_frame_count < 1200) // Check every day
if (world->frame_counter % 1200 != 0) // Check every day
return CR_OK;
last_frame_count = world->frame_counter;
do_autochop();
return CR_OK;

@ -160,16 +160,12 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
if(!Maps::IsValid())
return CR_OK;
static decltype(world->frame_counter) last_frame_count = 0;
if (DFHack::World::ReadPauseState())
return CR_OK;
if (world->frame_counter - last_frame_count < DELTA_TICKS)
if (world->frame_counter % DELTA_TICKS != 0)
return CR_OK;
last_frame_count = world->frame_counter;
monitor.doCycle();
return CR_OK;

@ -41,7 +41,6 @@ typedef int32_t mat_index;
typedef std::map<mat_index, int> gem_map;
bool running = false;
decltype(world->frame_counter) last_frame_count = 0;
const char *tagline = "Creates a new Workshop Order setting, automatically cutting rough gems.";
const char *usage = (
" enable autogems\n"
@ -218,8 +217,7 @@ void create_jobs() {
}
DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (running && (world->frame_counter - last_frame_count >= DELTA_TICKS)) {
last_frame_count = world->frame_counter;
if (running && !World::ReadPauseState() && (world->frame_counter % DELTA_TICKS == 0)) {
create_jobs();
}
@ -336,7 +334,6 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_chan
// Determine whether auto gem cutting has been disabled for this fort.
auto config = World::GetPersistentData(CONFIG_KEY);
running = config.isValid() && !config.ival(0);
last_frame_count = world->frame_counter;
read_config(out);
}
} else if (event == DFHack::SC_MAP_UNLOADED) {

@ -168,16 +168,12 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
if(!Maps::IsValid())
return CR_OK;
static decltype(world->frame_counter) last_frame_count = 0;
if (DFHack::World::ReadPauseState())
return CR_OK;
if (world->frame_counter - last_frame_count < DELTA_TICKS)
if (world->frame_counter % DELTA_TICKS != 0)
return CR_OK;
last_frame_count = world->frame_counter;
monitor.doCycle();
return CR_OK;

@ -339,16 +339,12 @@ DFhackCExport command_result plugin_onupdate ( color_ostream &out )
if(!Maps::IsValid())
return CR_OK;
static decltype(world->frame_counter) last_frame_count = 0;
if (DFHack::World::ReadPauseState())
return CR_OK;
if (world->frame_counter - last_frame_count < DELTA_TICKS)
if (world->frame_counter % DELTA_TICKS != 0)
return CR_OK;
last_frame_count = world->frame_counter;
monitor.doCycle();
return CR_OK;

@ -25,10 +25,8 @@ static bool is_planmode_enabled(df::building_type type)
#define DAY_TICKS 1200
DFhackCExport command_result plugin_onupdate(color_ostream &out)
{
static decltype(world->frame_counter) last_frame_count = 0;
if ((world->frame_counter - last_frame_count) >= DAY_TICKS/2)
if (Maps::IsValid() && !World::ReadPauseState() && world->frame_counter % (DAY_TICKS/2) == 0)
{
last_frame_count = world->frame_counter;
planner.doCycle();
roomMonitor.doCycle();
}

@ -1843,8 +1843,6 @@ DFhackCExport command_result plugin_onupdate (color_ostream &out)
if(!Maps::IsValid())
return CR_OK;
static decltype(world->frame_counter) last_frame_count = 0;
bool is_paused = DFHack::World::ReadPauseState();
if (is_paused)
{
@ -1855,10 +1853,8 @@ DFhackCExport command_result plugin_onupdate (color_ostream &out)
}
else
{
if (world->frame_counter - last_frame_count < DELTA_TICKS)
if (world->frame_counter % DELTA_TICKS != 0)
return CR_OK;
last_frame_count = world->frame_counter;
}
update_dwarf_stats(is_paused);

@ -211,27 +211,13 @@ DFhackCExport command_result plugin_enable (color_ostream &out, bool enable)
DFhackCExport command_result plugin_onupdate (color_ostream &out)
{
static unsigned tick = UINT_MAX;
static decltype(world->frame_counter) old_world_frame = 0;
if (is_enabled && World::isFortressMode())
{
// only increment tick when the world has changed
if (old_world_frame != world->frame_counter)
if (world->frame_counter % run_interval == 0 && !World::ReadPauseState())
{
old_world_frame = world->frame_counter;
tick++;
}
if (tick > run_interval)
{
tick = 0;
fix_unit_occupancy(out);
}
}
else
{
tick = INT_MAX;
old_world_frame = 0;
}
return CR_OK;
}

@ -88,10 +88,8 @@ DFhackCExport command_result plugin_init ( color_ostream &out, vector <PluginCom
#define DAY_TICKS 1200
DFhackCExport command_result plugin_onupdate(color_ostream &out)
{
static decltype(world->frame_counter) last_frame_count = 0;
if ((world->frame_counter - last_frame_count) >= DAY_TICKS/2)
if (Maps::IsValid() && !World::ReadPauseState() && world->frame_counter % (DAY_TICKS/2) == 0)
{
last_frame_count = world->frame_counter;
planner.doCycle();
}

@ -247,16 +247,12 @@ DFhackCExport command_result plugin_onupdate(color_ostream &out) {
if (!Maps::IsValid())
return CR_OK;
static decltype(world->frame_counter) last_frame_count = 0;
if (DFHack::World::ReadPauseState())
return CR_OK;
if (world->frame_counter - last_frame_count < DELTA_TICKS)
if (world->frame_counter % DELTA_TICKS != 0)
return CR_OK;
last_frame_count = world->frame_counter;
helper.cycle(out);
return CR_OK;