forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnestboxes.cpp
More file actions
124 lines (104 loc) · 3.14 KB
/
Copy pathnestboxes.cpp
File metadata and controls
124 lines (104 loc) · 3.14 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
#include "Core.h"
#include "Console.h"
#include "Export.h"
#include "PluginManager.h"
#include "DataDefs.h"
#include "df/world.h"
#include "df/ui.h"
#include "df/building_nest_boxst.h"
#include "df/building_type.h"
#include "df/buildings_other_id.h"
#include "df/global_objects.h"
#include "df/item.h"
#include "df/unit.h"
#include "df/building.h"
#include "df/items_other_id.h"
#include "df/creature_raw.h"
#include "modules/MapCache.h"
#include "modules/Items.h"
using std::vector;
using std::string;
using std::endl;
using namespace DFHack;
using namespace df::enums;
using df::global::world;
using df::global::ui;
static command_result nestboxes(color_ostream &out, vector <string> & parameters);
DFHACK_PLUGIN("nestboxes");
DFHACK_PLUGIN_IS_ENABLED(enabled);
static void eggscan(color_ostream &out)
{
CoreSuspender suspend;
for (df::building *build : world->buildings.other[df::buildings_other_id::NEST_BOX])
{
auto type = build->getType();
if (df::enums::building_type::NestBox == type)
{
bool fertile = false;
df::building_nest_boxst *nb = virtual_cast<df::building_nest_boxst>(build);
if (nb->claimed_by != -1)
{
df::unit* u = df::unit::find(nb->claimed_by);
if (u && u->pregnancy_timer > 0)
fertile = true;
}
for (size_t j = 1; j < nb->contained_items.size(); j++)
{
df::item* item = nb->contained_items[j]->item;
if (item->flags.bits.forbid != fertile)
{
item->flags.bits.forbid = fertile;
out << item->getStackSize() << " eggs " << (fertile ? "forbidden" : "unforbidden.") << endl;
}
}
}
}
}
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
if (world && ui) {
commands.push_back(
PluginCommand("nestboxes", "Automatically scan for and forbid fertile eggs incubating in a nestbox.",
nestboxes, false,
"To enable: nestboxes enable\n"
"To disable: nestboxes disable\n"
"There is no other configuration.\n"
)
);
}
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
return CR_OK;
}
DFhackCExport command_result plugin_onupdate(color_ostream &out)
{
if (!enabled)
return CR_OK;
static unsigned cnt = 0;
if ((++cnt % 5) != 0)
return CR_OK;
eggscan(out);
return CR_OK;
}
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable)
{
enabled = enable;
return CR_OK;
}
static command_result nestboxes(color_ostream &out, vector <string> & parameters)
{
CoreSuspender suspend;
if (parameters.size() == 1) {
if (parameters[0] == "enable")
enabled = true;
else if (parameters[0] == "disable")
enabled = false;
else
return CR_WRONG_USAGE;
} else {
out << "Plugin " << (enabled ? "enabled" : "disabled") << "." << endl;
}
return CR_OK;
}