forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseedwatch.cpp
More file actions
308 lines (281 loc) · 8.75 KB
/
Copy pathseedwatch.cpp
File metadata and controls
308 lines (281 loc) · 8.75 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Dynamically enables and disables cooking restrictions for plants and seeds
// in order to limit the number of seeds available for each crop type
// With thanks to peterix for DFHack and Quietust for information
// http://www.bay12forums.com/smf/index.php?topic=91166.msg2605147#msg2605147
#include <map>
#include <string>
#include <vector>
#include "Console.h"
#include "Core.h"
#include "Export.h"
#include "PluginManager.h"
#include "modules/World.h"
#include "modules/Materials.h"
#include "modules/Kitchen.h"
#include "VersionInfo.h"
#include "df/world.h"
#include "df/plant_raw.h"
#include "df/item_flags.h"
#include "df/items_other_id.h"
using namespace std;
using namespace DFHack;
using namespace df::enums;
DFHACK_PLUGIN("seedwatch");
DFHACK_PLUGIN_IS_ENABLED(running); // whether seedwatch is counting the seeds or not
REQUIRE_GLOBAL(world);
const int buffer = 20; // seed number buffer - 20 is reasonable
// abbreviations for the standard plants
map<string, string> abbreviations;
bool ignoreSeeds(df::item_flags& f) // seeds with the following flags should not be counted
{
return
f.bits.dump ||
f.bits.forbid ||
f.bits.garbage_collect ||
f.bits.hidden ||
f.bits.hostile ||
f.bits.on_fire ||
f.bits.rotten ||
f.bits.trader ||
f.bits.in_building ||
f.bits.in_job;
};
// searches abbreviations, returns expansion if so, returns original if not
string searchAbbreviations(string in)
{
if(abbreviations.count(in) > 0)
{
return abbreviations[in];
}
else
{
return in;
}
};
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
{
if(enable == true)
{
if(Core::getInstance().isWorldLoaded())
{
running = true;
out.print("seedwatch supervision started.\n");
} else {
out.printerr(
"This plugin needs a fortress to be loaded and will deactivate automatically otherwise.\n"
"Activate with 'seedwatch start' after you load the game.\n"
);
}
} else {
running = false;
out.print("seedwatch supervision stopped.\n");
}
return CR_OK;
}
command_result df_seedwatch(color_ostream &out, vector<string>& parameters)
{
CoreSuspender suspend;
map<string, int32_t> plantIDs;
for(size_t i = 0; i < world->raws.plants.all.size(); ++i)
{
auto & plant = world->raws.plants.all[i];
if (plant->material_defs.type[plant_material_def::seed] != -1)
plantIDs[plant->id] = i;
}
t_gamemodes gm;
World::ReadGameMode(gm);// FIXME: check return value
// if game mode isn't fortress mode
if(gm.g_mode != game_mode::DWARF || !World::isFortressMode(gm.g_type))
{
// just print the help
return CR_WRONG_USAGE;
}
string par;
int limit;
switch(parameters.size())
{
case 0:
return CR_WRONG_USAGE;
case 1:
par = parameters[0];
if ((par == "help") || (par == "?"))
{
return CR_WRONG_USAGE;
}
else if(par == "start")
{
plugin_enable(out, true);
}
else if(par == "stop")
{
plugin_enable(out, false);
}
else if(par == "clear")
{
Kitchen::clearLimits();
out.print("seedwatch watchlist cleared\n");
}
else if(par == "info")
{
out.print("seedwatch Info:\n");
if(running)
{
out.print("seedwatch is supervising. Use 'disable seedwatch' to stop supervision.\n");
}
else
{
out.print("seedwatch is not supervising. Use 'enable seedwatch' to start supervision.\n");
}
map<int32_t, int16_t> watchMap;
Kitchen::fillWatchMap(watchMap);
if(watchMap.empty())
{
out.print("The watch list is empty.\n");
}
else
{
out.print("The watch list is:\n");
for(auto i = watchMap.begin(); i != watchMap.end(); ++i)
{
out.print("%s : %u\n", world->raws.plants.all[i->first]->id.c_str(), i->second);
}
}
}
else if(par == "debug")
{
map<int32_t, int16_t> watchMap;
Kitchen::fillWatchMap(watchMap);
Kitchen::debug_print(out);
}
else
{
string token = searchAbbreviations(par);
if(plantIDs.count(token) > 0)
{
Kitchen::removeLimit(plantIDs[token]);
out.print("%s is not being watched\n", token.c_str());
}
else
{
out.print("%s has not been found as a material.\n", token.c_str());
}
}
break;
case 2:
limit = atoi(parameters[1].c_str());
if(limit < 0) limit = 0;
if(parameters[0] == "all")
{
for(auto & entry : plantIDs)
Kitchen::setLimit(entry.second, limit);
}
else
{
string token = searchAbbreviations(parameters[0]);
if(plantIDs.count(token) > 0)
{
Kitchen::setLimit(plantIDs[token], limit);
out.print("%s is being watched.\n", token.c_str());
}
else
{
out.print("%s has not been found as a material.\n", token.c_str());
}
}
break;
default:
return CR_WRONG_USAGE;
break;
}
return CR_OK;
}
DFhackCExport command_result plugin_init(color_ostream &out, vector<PluginCommand>& commands)
{
commands.push_back(PluginCommand(
"seedwatch",
"Toggles seed cooking based on quantity available.",
df_seedwatch));
// fill in the abbreviations map, with abbreviations for the standard plants
abbreviations["bs"] = "SLIVER_BARB";
abbreviations["bt"] = "TUBER_BLOATED";
abbreviations["bw"] = "WEED_BLADE";
abbreviations["cw"] = "GRASS_WHEAT_CAVE";
abbreviations["dc"] = "MUSHROOM_CUP_DIMPLE";
abbreviations["fb"] = "BERRIES_FISHER";
abbreviations["hr"] = "ROOT_HIDE";
abbreviations["kb"] = "BULB_KOBOLD";
abbreviations["lg"] = "GRASS_LONGLAND";
abbreviations["mr"] = "ROOT_MUCK";
abbreviations["pb"] = "BERRIES_PRICKLE";
abbreviations["ph"] = "MUSHROOM_HELMET_PLUMP";
abbreviations["pt"] = "GRASS_TAIL_PIG";
abbreviations["qb"] = "BUSH_QUARRY";
abbreviations["rr"] = "REED_ROPE";
abbreviations["rw"] = "WEED_RAT";
abbreviations["sb"] = "BERRY_SUN";
abbreviations["sp"] = "POD_SWEET";
abbreviations["vh"] = "HERB_VALLEY";
abbreviations["ws"] = "BERRIES_STRAW";
abbreviations["wv"] = "VINE_WHIP";
return CR_OK;
}
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
if (event == SC_MAP_UNLOADED) {
if (running)
out.print("seedwatch deactivated due to game unload\n");
running = false;
}
return CR_OK;
}
DFhackCExport command_result plugin_onupdate(color_ostream &out)
{
if (running)
{
// reduce processing rate
static int counter = 0;
if (++counter < 500)
return CR_OK;
counter = 0;
t_gamemodes gm;
World::ReadGameMode(gm);// FIXME: check return value
// if game mode isn't fortress mode
if(gm.g_mode != game_mode::DWARF || !World::isFortressMode(gm.g_type))
{
// stop running.
running = false;
out.printerr("seedwatch deactivated due to game mode switch\n");
return CR_OK;
}
// this is dwarf mode, continue
map<int32_t, int16_t> seedCount; // the number of seeds
// count all seeds and plants by RAW material
for(size_t i = 0; i < world->items.other[items_other_id::SEEDS].size(); ++i)
{
df::item *item = world->items.other[items_other_id::SEEDS][i];
MaterialInfo mat(item);
if (!mat.isPlant())
continue;
if (!ignoreSeeds(item->flags))
++seedCount[mat.plant->index];
}
map<int32_t, int16_t> watchMap;
Kitchen::fillWatchMap(watchMap);
for(auto i = watchMap.begin(); i != watchMap.end(); ++i)
{
if(seedCount[i->first] <= i->second)
{
Kitchen::denyPlantSeedCookery(i->first);
}
else if(i->second + buffer < seedCount[i->first])
{
Kitchen::allowPlantSeedCookery(i->first);
}
}
}
return CR_OK;
}
DFhackCExport command_result plugin_shutdown(Core* pCore)
{
return CR_OK;
}