dfhack/plugins/devel/kittens.cpp

284 lines
8.2 KiB
C++

#include <vector>
#include <string>
2011-12-31 04:48:42 -07:00
#include "Console.h"
#include "Core.h"
2011-12-31 04:48:42 -07:00
#include "Export.h"
#include "MiscUtils.h"
#include "PluginManager.h"
#include "modules/Gui.h"
2011-12-31 04:48:42 -07:00
#include "modules/Items.h"
#include "modules/Maps.h"
#include "df/caste_raw.h"
#include "df/creature_raw.h"
#include "df/world.h"
using std::vector;
using std::string;
using namespace DFHack;
DFHACK_PLUGIN("kittens");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(ui);
REQUIRE_GLOBAL(world);
//FIXME: possible race conditions with calling kittens from the IO thread and shutdown from Core.
2015-04-01 15:02:33 -06:00
volatile bool shutdown_flag = false;
volatile bool final_flag = true;
bool timering = false;
bool trackmenu_flg = false;
bool trackpos_flg = false;
bool statetrack = false;
int32_t last_designation[3] = {-30000, -30000, -30000};
int32_t last_mouse[2] = {-1, -1};
df::ui_sidebar_mode last_menu = df::ui_sidebar_mode::Default;
uint64_t timeLast = 0;
2012-03-10 06:25:00 -07:00
command_result kittens (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 trackpos (color_ostream &out, vector <string> & parameters);
command_result trackstate (color_ostream &out, vector <string> & parameters);
2012-03-10 06:25:00 -07:00
command_result colormods (color_ostream &out, vector <string> & parameters);
2011-06-19 20:29:38 -06:00
2012-03-10 06:25:00 -07:00
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
2015-04-01 15:02:33 -06:00
commands.push_back(PluginCommand("nyan","NYAN CAT INVASION!",kittens));
commands.push_back(PluginCommand("ktimer","Measure time between game updates and console lag.",ktimer));
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("trackstate","Track world and map state (toggle).",trackstate));
2012-01-26 21:54:26 -07:00
commands.push_back(PluginCommand("colormods","Dump colormod vectors.",colormods));
return CR_OK;
}
2012-03-10 06:25:00 -07:00
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
shutdown_flag = true;
while(!final_flag)
{
2012-03-10 06:25:00 -07:00
Core::getInstance().getConsole().msleep(60);
}
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
if(!statetrack)
return CR_OK;
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;
}
2012-03-10 06:25:00 -07:00
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
{
if(timering == true)
{
uint64_t time2 = GetTimeMs64();
2011-07-17 03:06:45 -06:00
// harmless potential data race here...
uint64_t delta = time2-timeLast;
2011-07-17 03:06:45 -06:00
// harmless potential data race here...
timeLast = time2;
out.print("Time delta = %d ms\n", int(delta));
}
if(trackmenu_flg)
{
if (last_menu != ui->main.mode)
{
last_menu = ui->main.mode;
2012-03-10 06:25:00 -07:00
out.print("Menu: %d\n",last_menu);
}
}
if(trackpos_flg)
{
int32_t desig_x, desig_y, desig_z;
Gui::getDesignationCoords(desig_x,desig_y,desig_z);
if(desig_x != last_designation[0] || desig_y != last_designation[1] || desig_z != last_designation[2])
{
last_designation[0] = desig_x;
last_designation[1] = desig_y;
last_designation[2] = desig_z;
2012-03-10 06:25:00 -07:00
out.print("Designation: %d %d %d\n",desig_x, desig_y, desig_z);
}
int mouse_x, mouse_y;
Gui::getMousePos(mouse_x,mouse_y);
if(mouse_x != last_mouse[0] || mouse_y != last_mouse[1])
{
last_mouse[0] = mouse_x;
last_mouse[1] = mouse_y;
2012-03-10 06:25:00 -07:00
out.print("Mouse: %d %d\n",mouse_x, mouse_y);
}
}
return CR_OK;
}
2012-03-10 06:25:00 -07:00
command_result trackmenu (color_ostream &out, vector <string> & parameters)
{
if(trackmenu_flg)
{
trackmenu_flg = false;
return CR_OK;
}
else
{
trackmenu_flg = true;
is_enabled = true;
last_menu = ui->main.mode;
out.print("Menu: %d\n",last_menu);
return CR_OK;
}
}
2012-03-10 06:25:00 -07:00
command_result trackpos (color_ostream &out, vector <string> & parameters)
{
trackpos_flg = !trackpos_flg;
is_enabled = true;
return CR_OK;
}
2012-01-26 21:54:26 -07:00
command_result trackstate ( color_ostream& out, vector< string >& parameters )
{
statetrack = !statetrack;
return CR_OK;
}
2012-03-10 06:25:00 -07:00
command_result colormods (color_ostream &out, vector <string> & parameters)
{
2012-03-10 06:25:00 -07:00
CoreSuspender suspend;
auto & vec = world->raws.creatures.alphabetic;
for(df::creature_raw* rawlion : vec)
{
2012-01-26 21:54:26 -07:00
df::caste_raw * caste = rawlion->caste[0];
out.print("%s\nCaste addr %p\n",rawlion->creature_id.c_str(), &caste->color_modifiers);
for(size_t j = 0; j < caste->color_modifiers.size();j++)
{
out.print("mod %zd: %p\n", j, caste->color_modifiers[j]);
}
}
return CR_OK;
}
2012-03-10 06:25:00 -07:00
command_result ktimer (color_ostream &out, vector <string> & parameters)
{
if(timering)
{
timering = false;
return CR_OK;
}
uint64_t timestart = GetTimeMs64();
2012-03-10 06:25:00 -07:00
{
CoreSuspender suspend;
}
uint64_t timeend = GetTimeMs64();
out.print("Time to suspend = %d ms\n", int(timeend - timestart));
2011-07-17 03:06:45 -06:00
// harmless potential data race here...
timeLast = timeend;
timering = true;
is_enabled = true;
return CR_OK;
}
2012-03-10 06:25:00 -07:00
command_result kittens (color_ostream &out, vector <string> & parameters)
2011-06-19 20:29:38 -06:00
{
2015-04-01 15:02:33 -06:00
if (parameters.size() >= 1)
{
if (parameters[0] == "stop")
{
shutdown_flag = true;
while(!final_flag)
{
Core::getInstance().getConsole().msleep(60);
}
shutdown_flag = false;
return CR_OK;
}
}
final_flag = false;
2015-04-01 15:02:33 -06:00
if (!out.is_console())
return CR_FAILURE;
2012-03-10 06:25:00 -07:00
Console &con = static_cast<Console&>(out);
// http://evilzone.org/creative-arts/nyan-cat-ascii/
const char * nyan []=
{
"NYAN NYAN NYAN NYAN NYAN NYAN NYAN",
"+ o + o ",
" + o + +",
"o +",
" o + + +",
"+ o o + o",
"-_-_-_-_-_-_-_,------, o ",
"_-_-_-_-_-_-_-| /\\_/\\ ",
"-_-_-_-_-_-_-~|__( ^ .^) + + ",
"_-_-_-_-_-_-_-\"\" \"\" ",
"+ o o + o",
" + +",
"o o o o +",
" o +",
"+ + o o + ",
"NYAN NYAN NYAN NYAN NYAN NYAN NYAN",
0
};
2011-06-19 20:29:38 -06:00
const char * kittenz1 []=
{
" ____",
" (. \\",
" \\ | ",
" \\ |___(\\--/)",
" __/ ( . . )",
" \"'._. '-.O.'",
" '-. \\ \"|\\",
" '.,,/'.,,mrf",
0
};
con.cursor(false);
con.clear();
Console::color_value color = COLOR_BLUE;
2011-06-19 20:29:38 -06:00
while(1)
{
if(shutdown_flag)
{
final_flag = true;
con.reset_color();
con << std::endl << "NYAN!" << std::endl << std::flush;
return CR_OK;
}
con.color(color);
2011-06-19 20:29:38 -06:00
int index = 0;
const char * kit = nyan[index];
con.gotoxy(1,1);
//con << "Your DF is now full of kittens!" << std::endl;
2011-06-19 20:29:38 -06:00
while (kit != 0)
{
con.gotoxy(1,1+index);
con << kit << std::endl;
2011-06-19 20:29:38 -06:00
index++;
kit = nyan[index];
2011-06-19 20:29:38 -06:00
}
con.flush();
con.msleep(60);
color = Console::color_value(int(color) + 1);
if(color > COLOR_MAX)
color = COLOR_BLUE;
2011-06-19 20:29:38 -06:00
}
}