forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisery.cpp
More file actions
203 lines (178 loc) · 6.8 KB
/
Copy pathmisery.cpp
File metadata and controls
203 lines (178 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "PluginManager.h"
#include "Export.h"
#include "DataDefs.h"
#include "df/world.h"
#include "df/ui.h"
#include "df/unit.h"
#include "df/unit_thought.h"
#include "df/unit_thought_type.h"
#include <string>
#include <vector>
#include <map>
using namespace std;
using namespace DFHack;
DFHACK_PLUGIN("misery");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(world);
REQUIRE_GLOBAL(ui);
static int factor = 1;
static map<int, int> processedThoughtCountTable;
//keep track of fake thoughts so you can remove them if requested
static vector<std::pair<int,int> > fakeThoughts;
static int count;
const int maxCount = 1000;
command_result misery(color_ostream& out, vector<string>& parameters);
DFhackCExport command_result plugin_shutdown(color_ostream& out) {
factor = 1;
return CR_OK;
}
DFhackCExport command_result plugin_onupdate(color_ostream& out) {
static bool wasLoaded = false;
if ( factor == 1 || !world || !world->map.block_index ) {
if ( wasLoaded ) {
//we just unloaded the game: clear all data
factor = 1;
is_enabled = false;
processedThoughtCountTable.clear();
fakeThoughts.clear();
wasLoaded = false;
}
return CR_OK;
}
if ( !wasLoaded ) {
wasLoaded = true;
}
if ( count < maxCount ) {
count++;
return CR_OK;
}
count = 0;
int32_t race_id = ui->race_id;
int32_t civ_id = ui->civ_id;
for ( size_t a = 0; a < world->units.all.size(); a++ ) {
df::unit* unit = world->units.all[a]; //TODO: consider units.active
//living, native units only
if ( unit->race != race_id || unit->civ_id != civ_id )
continue;
if ( unit->flags1.bits.dead )
continue;
int processedThoughtCount;
map<int,int>::iterator i = processedThoughtCountTable.find(unit->id);
if ( i == processedThoughtCountTable.end() ) {
processedThoughtCount = unit->status.recent_events.size();
processedThoughtCountTable[unit->id] = processedThoughtCount;
} else {
processedThoughtCount = (*i).second;
}
if ( processedThoughtCount == unit->status.recent_events.size() ) {
continue;
} else if ( processedThoughtCount > unit->status.recent_events.size() ) {
processedThoughtCount = unit->status.recent_events.size();
}
//don't reprocess any old thoughts
vector<df::unit_thought*> newThoughts;
for ( size_t b = processedThoughtCount; b < unit->status.recent_events.size(); b++ ) {
df::unit_thought* oldThought = unit->status.recent_events[b];
const char* bob = ENUM_ATTR(unit_thought_type, value, oldThought->type);
if ( bob[0] != '-' ) {
//out.print("unit %4d: old thought value = %s\n", unit->id, bob);
continue;
}
/*out.print("unit %4d: Duplicating thought type %d (%s), value %s, age %d, subtype %d, severity %d\n",
unit->id,
oldThought->type.value,
ENUM_ATTR(unit_thought_type, caption, (oldThought->type)),
//df::enum_traits<df::unit_thought_type>::attr_table[oldThought->type].caption
bob,
oldThought->age,
oldThought->subtype,
oldThought->severity
);*/
//add duplicate thoughts to the temp list
for ( size_t c = 0; c < factor; c++ ) {
df::unit_thought* thought = new df::unit_thought;
thought->type = unit->status.recent_events[b]->type;
thought->age = unit->status.recent_events[b]->age;
thought->subtype = unit->status.recent_events[b]->subtype;
thought->severity = unit->status.recent_events[b]->severity;
newThoughts.push_back(thought);
}
}
for ( size_t b = 0; b < newThoughts.size(); b++ ) {
fakeThoughts.push_back(std::pair<int, int>(a, unit->status.recent_events.size()));
unit->status.recent_events.push_back(newThoughts[b]);
}
processedThoughtCountTable[unit->id] = unit->status.recent_events.size();
}
return CR_OK;
}
DFhackCExport command_result plugin_init(color_ostream& out, vector<PluginCommand> &commands) {
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;
}
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
{
if (enable != is_enabled)
{
is_enabled = enable;
factor = enable ? 2 : 1;
}
return CR_OK;
}
command_result misery(color_ostream &out, vector<string>& parameters) {
if ( !world || !world->map.block_index ) {
out.printerr("misery can only be enabled in fortress mode with a fully-loaded game.\n");
return CR_FAILURE;
}
if ( parameters.size() < 1 || parameters.size() > 2 ) {
return CR_WRONG_USAGE;
}
if ( parameters[0] == "disable" ) {
if ( parameters.size() > 1 ) {
return CR_WRONG_USAGE;
}
factor = 1;
is_enabled = false;
return CR_OK;
} else if ( parameters[0] == "enable" ) {
is_enabled = true;
factor = 2;
if ( parameters.size() == 2 ) {
int a = atoi(parameters[1].c_str());
if ( a <= 1 ) {
out.printerr("Second argument must be a positive integer.\n");
return CR_WRONG_USAGE;
}
factor = a;
}
} else if ( parameters[0] == "clear" ) {
for ( size_t a = 0; a < fakeThoughts.size(); a++ ) {
int dorfIndex = fakeThoughts[a].first;
int thoughtIndex = fakeThoughts[a].second;
world->units.all[dorfIndex]->status.recent_events[thoughtIndex]->age = 1000000;
}
fakeThoughts.clear();
} else {
int a = atoi(parameters[0].c_str());
if ( a < 1 ) {
return CR_WRONG_USAGE;
}
factor = a;
is_enabled = factor > 1;
}
return CR_OK;
}