Track world and map changes separately for plugin_onstatechange

develop
Petr Mrázek 2012-04-01 02:56:54 +02:00
parent 209b261284
commit 7ff728b6fc
8 changed files with 81 additions and 15 deletions

@ -609,6 +609,7 @@ Core::Core()
HotkeyCond = 0; HotkeyCond = 0;
misc_data_mutex=0; misc_data_mutex=0;
last_world_data_ptr = NULL; last_world_data_ptr = NULL;
last_local_map_ptr = NULL;
top_viewscreen = NULL; top_viewscreen = NULL;
screen_window = NULL; screen_window = NULL;
server = NULL; server = NULL;
@ -869,16 +870,41 @@ int Core::Update()
// detect if the game was loaded or unloaded in the meantime // detect if the game was loaded or unloaded in the meantime
void *new_wdata = NULL; void *new_wdata = NULL;
if (df::global::world) { void *new_mapdata = NULL;
if (df::global::world)
{
df::world_data *wdata = df::global::world->world_data; df::world_data *wdata = df::global::world->world_data;
// when the game is unloaded, world_data isn't deleted, but its contents are // when the game is unloaded, world_data isn't deleted, but its contents are
if (wdata && !wdata->sites.empty()) if (wdata && !wdata->sites.empty())
new_wdata = wdata; new_wdata = wdata;
new_mapdata = df::global::world->map.block_index;
} }
// if the world changes
if (new_wdata != last_world_data_ptr) { if (new_wdata != last_world_data_ptr)
{
// we check for map change too
bool mapchange = new_mapdata != last_local_map_ptr;
// and if the world is going away, we report the map change first
if(!new_wdata && mapchange)
{
last_local_map_ptr = new_mapdata;
plug_mgr->OnStateChange(out, new_mapdata ? SC_MAP_LOADED : SC_MAP_UNLOADED);
}
// and if the world is appearing, we report map change after that
plug_mgr->OnStateChange(out, new_wdata ? SC_WORLD_LOADED : SC_WORLD_UNLOADED);
if(new_wdata && mapchange)
{
last_local_map_ptr = new_mapdata;
plug_mgr->OnStateChange(out, new_mapdata ? SC_MAP_LOADED : SC_MAP_UNLOADED);
}
// update tracking variable
last_world_data_ptr = new_wdata; last_world_data_ptr = new_wdata;
plug_mgr->OnStateChange(out, new_wdata ? SC_GAME_LOADED : SC_GAME_UNLOADED); }
// otherwise just check for map change...
else if (new_mapdata != last_local_map_ptr)
{
last_local_map_ptr = new_mapdata;
plug_mgr->OnStateChange(out, new_mapdata ? SC_MAP_LOADED : SC_MAP_UNLOADED);
} }
// detect if the viewscreen changed // detect if the viewscreen changed

@ -126,6 +126,7 @@ namespace DFHack
std::vector<std::string> ListKeyBindings(std::string keyspec); std::vector<std::string> ListKeyBindings(std::string keyspec);
bool isWorldLoaded() { return (last_world_data_ptr != NULL); } bool isWorldLoaded() { return (last_world_data_ptr != NULL); }
bool isMapLoaded() { return (last_local_map_ptr != NULL && last_world_data_ptr != NULL); }
static df::viewscreen *getTopViewscreen() { return getInstance().top_viewscreen; } static df::viewscreen *getTopViewscreen() { return getInstance().top_viewscreen; }
@ -194,7 +195,10 @@ namespace DFHack
int UnicodeAwareSym(const SDL::KeyboardEvent& ke); int UnicodeAwareSym(const SDL::KeyboardEvent& ke);
bool SelectHotkey(int key, int modifiers); bool SelectHotkey(int key, int modifiers);
void *last_world_data_ptr; // for state change tracking // for state change tracking
void *last_world_data_ptr;
// for state change tracking
void *last_local_map_ptr;
df::viewscreen *top_viewscreen; df::viewscreen *top_viewscreen;
// Very important! // Very important!
bool started; bool started;

@ -52,8 +52,10 @@ namespace DFHack
enum state_change_event enum state_change_event
{ {
SC_GAME_LOADED, SC_WORLD_LOADED,
SC_GAME_UNLOADED, SC_WORLD_UNLOADED,
SC_MAP_LOADED,
SC_MAP_UNLOADED,
SC_VIEWSCREEN_CHANGED SC_VIEWSCREEN_CHANGED
}; };
struct DFHACK_EXPORT PluginCommand struct DFHACK_EXPORT PluginCommand

@ -101,8 +101,8 @@ static bool in_transient_swap = false;
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{ {
switch (event) { switch (event) {
case SC_GAME_LOADED: case SC_WORLD_LOADED:
case SC_GAME_UNLOADED: case SC_WORLD_UNLOADED:
in_transient_swap = false; in_transient_swap = false;
break; break;
default: default:

@ -22,6 +22,7 @@ bool final_flag = true;
bool timering = false; bool timering = false;
bool trackmenu_flg = false; bool trackmenu_flg = false;
bool trackpos_flg = false; bool trackpos_flg = false;
bool statetrack = false;
int32_t last_designation[3] = {-30000, -30000, -30000}; int32_t last_designation[3] = {-30000, -30000, -30000};
int32_t last_mouse[2] = {-1, -1}; int32_t last_mouse[2] = {-1, -1};
uint32_t last_menu = 0; uint32_t last_menu = 0;
@ -31,6 +32,7 @@ command_result kittens (color_ostream &out, vector <string> & parameters);
command_result ktimer (color_ostream &out, vector <string> & parameters); command_result ktimer (color_ostream &out, vector <string> & parameters);
command_result trackmenu (color_ostream &out, vector <string> & parameters); command_result trackmenu (color_ostream &out, vector <string> & parameters);
command_result trackpos (color_ostream &out, vector <string> & parameters); command_result trackpos (color_ostream &out, vector <string> & parameters);
command_result trackstate (color_ostream &out, vector <string> & parameters);
command_result colormods (color_ostream &out, vector <string> & parameters); command_result colormods (color_ostream &out, vector <string> & parameters);
command_result zoom (color_ostream &out, vector <string> & parameters); command_result zoom (color_ostream &out, vector <string> & parameters);
@ -42,6 +44,7 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <Plug
commands.push_back(PluginCommand("ktimer","Measure time between game updates and console lag (toggle).",ktimer)); commands.push_back(PluginCommand("ktimer","Measure time between game updates and console lag (toggle).",ktimer));
commands.push_back(PluginCommand("trackmenu","Track menu ID changes (toggle).",trackmenu)); commands.push_back(PluginCommand("trackmenu","Track menu ID changes (toggle).",trackmenu));
commands.push_back(PluginCommand("trackpos","Track mouse and designation coords (toggle).",trackpos)); commands.push_back(PluginCommand("trackpos","Track mouse and designation coords (toggle).",trackpos));
commands.push_back(PluginCommand("trackstate","Track world and map state (toggle).",trackstate));
commands.push_back(PluginCommand("colormods","Dump colormod vectors.",colormods)); commands.push_back(PluginCommand("colormods","Dump colormod vectors.",colormods));
commands.push_back(PluginCommand("zoom","Zoom to x y z.",zoom)); commands.push_back(PluginCommand("zoom","Zoom to x y z.",zoom));
return CR_OK; return CR_OK;
@ -57,6 +60,31 @@ DFhackCExport command_result plugin_shutdown ( color_ostream &out )
return CR_OK; return CR_OK;
} }
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
switch (event) {
case SC_MAP_LOADED:
out << "Map loaded" << endl;
break;
case SC_MAP_UNLOADED:
out << "Map unloaded" << endl;
break;
case SC_WORLD_LOADED:
out << "World loaded" << endl;
break;
case SC_WORLD_UNLOADED:
out << "World unloaded" << endl;
break;
case SC_VIEWSCREEN_CHANGED:
out << "Screen changed" << endl;
break;
default:
out << "Something else is happening, nobody knows what..." << endl;
break;
}
return CR_OK;
}
DFhackCExport command_result plugin_onupdate ( color_ostream &out ) DFhackCExport command_result plugin_onupdate ( color_ostream &out )
{ {
if(timering == true) if(timering == true)
@ -128,6 +156,12 @@ command_result trackpos (color_ostream &out, vector <string> & parameters)
return CR_OK; return CR_OK;
} }
command_result trackstate ( color_ostream& out, vector< string >& parameters )
{
statetrack = !statetrack;
return CR_OK;
}
command_result colormods (color_ostream &out, vector <string> & parameters) command_result colormods (color_ostream &out, vector <string> & parameters)
{ {
CoreSuspender suspend; CoreSuspender suspend;

@ -48,8 +48,8 @@ DFhackCExport command_result plugin_shutdown ( 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)
{ {
switch (event) { switch (event) {
case SC_GAME_LOADED: case SC_MAP_LOADED:
case SC_GAME_UNLOADED: //Make sure our plugin's variables are clean case SC_MAP_UNLOADED: //Make sure our plugin's variables are clean
followedUnit = 0; followedUnit = 0;
prevX=prevY=prevZ = -1; prevX=prevY=prevZ = -1;
prevMenuWidth = 0; prevMenuWidth = 0;

@ -272,8 +272,8 @@ DFhackCExport command_result plugin_init(color_ostream &out, vector<PluginComman
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event) DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{ {
switch (event) { switch (event) {
case SC_GAME_LOADED: case SC_MAP_LOADED:
case SC_GAME_UNLOADED: case SC_MAP_UNLOADED:
if (running) if (running)
out.printerr("seedwatch deactivated due to game load/unload\n"); out.printerr("seedwatch deactivated due to game load/unload\n");
running = false; running = false;

@ -138,11 +138,11 @@ DFhackCExport command_result plugin_shutdown (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)
{ {
switch (event) { switch (event) {
case SC_GAME_LOADED: case SC_MAP_LOADED:
cleanup_state(out); cleanup_state(out);
init_state(out); init_state(out);
break; break;
case SC_GAME_UNLOADED: case SC_MAP_UNLOADED:
cleanup_state(out); cleanup_state(out);
break; break;
default: default: