2016-12-10 16:22:28 -07:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2012-08-20 23:33:04 -06:00
|
|
|
|
|
|
|
#include "DataDefs.h"
|
2016-12-10 16:22:28 -07:00
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
|
|
|
|
|
|
|
#include "modules/Units.h"
|
|
|
|
|
|
|
|
#include "df/emotion_type.h"
|
2012-08-20 23:33:04 -06:00
|
|
|
#include "df/ui.h"
|
|
|
|
#include "df/unit.h"
|
2016-12-10 16:22:28 -07:00
|
|
|
#include "df/unit_personality.h"
|
|
|
|
#include "df/unit_soul.h"
|
2012-08-20 23:33:04 -06:00
|
|
|
#include "df/unit_thought_type.h"
|
2016-12-10 16:22:28 -07:00
|
|
|
#include "df/world.h"
|
2012-08-20 23:33:04 -06:00
|
|
|
|
2014-08-11 14:07:52 -06:00
|
|
|
using namespace std;
|
2012-08-20 23:33:04 -06:00
|
|
|
using namespace DFHack;
|
|
|
|
|
2014-12-06 16:47:35 -07:00
|
|
|
DFHACK_PLUGIN("misery");
|
2013-09-30 03:19:51 -06:00
|
|
|
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
|
|
|
|
|
2014-12-06 16:47:35 -07:00
|
|
|
REQUIRE_GLOBAL(world);
|
|
|
|
REQUIRE_GLOBAL(ui);
|
2016-12-10 16:22:28 -07:00
|
|
|
REQUIRE_GLOBAL(cur_year);
|
|
|
|
REQUIRE_GLOBAL(cur_year_tick);
|
2014-12-06 16:47:35 -07:00
|
|
|
|
2016-12-10 16:22:28 -07:00
|
|
|
typedef df::unit_personality::T_emotions Emotion;
|
2012-08-20 23:33:04 -06:00
|
|
|
|
2016-12-10 16:22:28 -07:00
|
|
|
static int factor = 1;
|
|
|
|
static int tick = 0;
|
|
|
|
const int INTERVAL = 1000;
|
2012-08-20 23:33:04 -06:00
|
|
|
|
|
|
|
command_result misery(color_ostream& out, vector<string>& parameters);
|
2016-12-10 16:22:28 -07:00
|
|
|
void add_misery(df::unit *unit);
|
|
|
|
void clear_misery(df::unit *unit);
|
|
|
|
|
|
|
|
const int FAKE_EMOTION_FLAG = (1 << 30);
|
|
|
|
const int STRENGTH_MULTIPLIER = 100;
|
|
|
|
|
|
|
|
bool is_valid_unit (df::unit *unit) {
|
|
|
|
if (!Units::isOwnRace(unit) || !Units::isOwnCiv(unit))
|
|
|
|
return false;
|
2018-06-14 08:30:35 -06:00
|
|
|
if (!Units::isActive(unit))
|
2016-12-10 16:22:28 -07:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool is_fake_emotion (Emotion *e) {
|
|
|
|
return e->flags.whole & FAKE_EMOTION_FLAG;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_misery (df::unit *unit) {
|
|
|
|
// Add a fake miserable thought
|
|
|
|
// Remove any fake ones that already exist
|
|
|
|
if (!unit || !unit->status.current_soul)
|
|
|
|
return;
|
|
|
|
clear_misery(unit);
|
|
|
|
auto &emotions = unit->status.current_soul->personality.emotions;
|
|
|
|
Emotion *e = new Emotion;
|
|
|
|
e->type = df::emotion_type::MISERY;
|
|
|
|
e->thought = df::unit_thought_type::SoapyBath;
|
|
|
|
e->flags.whole |= FAKE_EMOTION_FLAG;
|
|
|
|
emotions.push_back(e);
|
|
|
|
|
|
|
|
for (Emotion *e : emotions) {
|
|
|
|
if (is_fake_emotion(e)) {
|
|
|
|
e->year = *cur_year;
|
|
|
|
e->year_tick = *cur_year_tick;
|
|
|
|
e->strength = STRENGTH_MULTIPLIER * factor;
|
|
|
|
e->severity = STRENGTH_MULTIPLIER * factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_misery (df::unit *unit) {
|
|
|
|
if (!unit || !unit->status.current_soul)
|
|
|
|
return;
|
|
|
|
auto &emotions = unit->status.current_soul->personality.emotions;
|
|
|
|
auto it = remove_if(emotions.begin(), emotions.end(), [](Emotion *e) {
|
|
|
|
if (is_fake_emotion(e)) {
|
|
|
|
delete e;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
emotions.erase(it, emotions.end());
|
|
|
|
}
|
2012-08-20 23:33:04 -06:00
|
|
|
|
|
|
|
DFhackCExport command_result plugin_shutdown(color_ostream& out) {
|
2016-12-10 16:22:28 -07:00
|
|
|
factor = 0;
|
2012-08-23 20:21:09 -06:00
|
|
|
return CR_OK;
|
2012-08-20 23:33:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onupdate(color_ostream& out) {
|
2012-08-23 20:21:09 -06:00
|
|
|
static bool wasLoaded = false;
|
2016-12-10 16:22:28 -07:00
|
|
|
if ( factor == 0 || !world || !world->map.block_index ) {
|
2012-08-23 20:21:09 -06:00
|
|
|
if ( wasLoaded ) {
|
|
|
|
//we just unloaded the game: clear all data
|
2016-12-10 16:22:28 -07:00
|
|
|
factor = 0;
|
2013-09-30 03:19:51 -06:00
|
|
|
is_enabled = false;
|
2012-08-23 20:21:09 -06:00
|
|
|
wasLoaded = false;
|
|
|
|
}
|
|
|
|
return CR_OK;
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-08-23 20:21:09 -06:00
|
|
|
if ( !wasLoaded ) {
|
|
|
|
wasLoaded = true;
|
|
|
|
}
|
2012-08-27 13:06:41 -06:00
|
|
|
|
2016-12-10 16:22:28 -07:00
|
|
|
if ( tick < INTERVAL ) {
|
|
|
|
tick++;
|
2012-08-27 14:05:23 -06:00
|
|
|
return CR_OK;
|
2012-08-27 13:06:41 -06:00
|
|
|
}
|
2016-12-10 16:22:28 -07:00
|
|
|
tick = 0;
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2016-12-10 16:22:28 -07:00
|
|
|
//TODO: consider units.active
|
|
|
|
for (df::unit *unit : world->units.all) {
|
|
|
|
if (is_valid_unit(unit)) {
|
|
|
|
add_misery(unit);
|
2012-08-23 20:21:09 -06:00
|
|
|
}
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-08-23 20:21:09 -06:00
|
|
|
return CR_OK;
|
2012-08-20 23:33:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_init(color_ostream& out, vector<PluginCommand> &commands) {
|
2012-08-23 20:21:09 -06:00
|
|
|
commands.push_back(PluginCommand("misery", "increase the intensity of negative dwarven thoughts",
|
|
|
|
&misery, false,
|
|
|
|
"misery: When enabled, every new negative dwarven thought will be multiplied by a factor (2 by default).\n"
|
|
|
|
"Usage:\n"
|
|
|
|
" misery enable n\n"
|
|
|
|
" enable misery with optional magnitude n. If specified, n must be positive.\n"
|
|
|
|
" misery n\n"
|
|
|
|
" same as \"misery enable n\"\n"
|
|
|
|
" misery enable\n"
|
|
|
|
" same as \"misery enable 2\"\n"
|
|
|
|
" misery disable\n"
|
|
|
|
" stop adding new negative thoughts. This will not remove existing duplicated thoughts. Equivalent to \"misery 1\"\n"
|
|
|
|
" misery clear\n"
|
|
|
|
" remove fake thoughts added in this session of DF. Saving makes them permanent! Does not change factor.\n\n"
|
|
|
|
));
|
|
|
|
return CR_OK;
|
2012-08-20 23:33:04 -06:00
|
|
|
}
|
|
|
|
|
2013-09-30 03:19:51 -06:00
|
|
|
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
|
|
|
|
{
|
|
|
|
if (enable != is_enabled)
|
|
|
|
{
|
|
|
|
is_enabled = enable;
|
2016-12-10 16:22:28 -07:00
|
|
|
factor = enable ? 1 : 0;
|
|
|
|
tick = INTERVAL;
|
2013-09-30 03:19:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-20 23:33:04 -06:00
|
|
|
command_result misery(color_ostream &out, vector<string>& parameters) {
|
2014-08-11 14:07:52 -06:00
|
|
|
if ( !world || !world->map.block_index ) {
|
2012-08-23 20:21:09 -06:00
|
|
|
out.printerr("misery can only be enabled in fortress mode with a fully-loaded game.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-08-23 20:21:09 -06:00
|
|
|
if ( parameters.size() < 1 || parameters.size() > 2 ) {
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-08-23 20:21:09 -06:00
|
|
|
if ( parameters[0] == "disable" ) {
|
|
|
|
if ( parameters.size() > 1 ) {
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
2016-12-10 16:22:28 -07:00
|
|
|
factor = 0;
|
2013-09-30 03:19:51 -06:00
|
|
|
is_enabled = false;
|
2012-08-23 20:21:09 -06:00
|
|
|
return CR_OK;
|
|
|
|
} else if ( parameters[0] == "enable" ) {
|
2013-09-30 03:19:51 -06:00
|
|
|
is_enabled = true;
|
2016-12-10 16:22:28 -07:00
|
|
|
factor = 1;
|
2012-08-23 20:21:09 -06:00
|
|
|
if ( parameters.size() == 2 ) {
|
2013-09-30 03:19:51 -06:00
|
|
|
int a = atoi(parameters[1].c_str());
|
2016-12-10 16:22:28 -07:00
|
|
|
if ( a < 1 ) {
|
2012-08-23 20:21:09 -06:00
|
|
|
out.printerr("Second argument must be a positive integer.\n");
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
2013-09-30 03:19:51 -06:00
|
|
|
factor = a;
|
2012-08-23 20:21:09 -06:00
|
|
|
}
|
2016-12-10 16:22:28 -07:00
|
|
|
tick = INTERVAL;
|
2012-08-23 20:21:09 -06:00
|
|
|
} else if ( parameters[0] == "clear" ) {
|
2016-12-10 16:22:28 -07:00
|
|
|
for (df::unit *unit : world->units.all) {
|
|
|
|
if (is_valid_unit(unit)) {
|
|
|
|
clear_misery(unit);
|
|
|
|
}
|
2012-08-23 20:21:09 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int a = atoi(parameters[0].c_str());
|
2016-12-10 16:22:28 -07:00
|
|
|
if ( a < 0 ) {
|
2012-08-23 20:21:09 -06:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
}
|
|
|
|
factor = a;
|
2016-12-10 16:22:28 -07:00
|
|
|
is_enabled = factor > 0;
|
2012-08-23 20:21:09 -06:00
|
|
|
}
|
2015-02-14 20:53:06 -07:00
|
|
|
|
2012-08-23 20:21:09 -06:00
|
|
|
return CR_OK;
|
2012-08-20 23:33:04 -06:00
|
|
|
}
|
2012-08-27 13:06:41 -06:00
|
|
|
|