-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy patharmy-controller-sanity.cpp
More file actions
135 lines (115 loc) · 4.62 KB
/
army-controller-sanity.cpp
File metadata and controls
135 lines (115 loc) · 4.62 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
#include "Debug.h"
#include "PluginManager.h"
#include "df/world.h"
#include "df/army_controller.h"
#include "df/army.h"
#include "df/historical_entity.h"
#include "df/unit.h"
#include "df/global_objects.h"
#include <unordered_set>
DFHACK_PLUGIN("army-controller-sanity");
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
REQUIRE_GLOBAL(army_controller_next_id);
REQUIRE_GLOBAL(world);
REQUIRE_GLOBAL(pause_state);
REQUIRE_GLOBAL(cur_year_tick);
namespace DFHack {
DBG_DECLARE(army_controller_sanity, log, DebugCategory::LWARNING);
}
namespace {
bool checkArmyControllerSanity()
{
// army controllers are found:
// in viewscreen_worldst (army_controller (list), last_hover_ac) (not checked)
// in entitst (army_controller (list))
// in armyst (controller)
// in unitst (army_controller)
//
// master list is in army_controller_handlerst
static size_t last_ac_vec_size = 0;
static int last_army_controller_next_id = 0;
if (last_army_controller_next_id == *army_controller_next_id &&
last_ac_vec_size == world->army_controllers.all.size())
return true;
std::unordered_set<df::army_controller*> ac_set{};
for (auto ac : world->army_controllers.all)
{
ac_set.insert(ac);
}
bool ok = true;
for (auto ent : world->entities.all)
{
for (auto ac : ent->army_controllers)
{
if (ac_set.count(ac) == 0) {
WARN(log).print("acValidationError: Bad controller {} found in entity id {}\n", static_cast<void*>(ac), ent->id);
ok = false;
}
if (ac_set.count(ac) != 0 && ac->entity_id != ent->id)
{
WARN(log).print("acValidationError: Army controller {} has entity id {} but is linked from entity with id {}\n", ac->id, ac->entity_id, ent->id);
}
}
}
for (auto ar : world->armies.all)
{
auto ac = ar->controller;
if (ac && ac_set.count(ac) == 0) {
WARN(log).print("acValidationError: Bad controller {} found in army id {}\n", static_cast<void*>(ac), ar->id);
ok = false;
}
else if (ac && ac->id != ar->controller_id)
{
WARN(log).print("acValidationError: controller {} id mismatch ({} != {}) in army {}\n", static_cast<void*>(ac), ar->controller_id, ac->id, ar->id);
ok = false;
}
else if (!ac && ar->controller_id != -1)
{
WARN(log).print("acValidationError: army {} has nonzero controller {} but controller pointer is null\n", ar->id, ar->controller_id);
ok = false;
}
}
for (auto un : world->units.all)
{
auto ac = un->enemy.army_controller;
if (ac && ac_set.count(ac) == 0) {
WARN(log).print("acValidationError: Bad controller {} found in unit id {}\n", static_cast<void*>(ac), un->id);
ok = false;
}
else if (ac && ac->id != un->enemy.army_controller_id)
{
WARN(log).print("acValidationError: controller {} id mismatch ({} != {}) in unit {}\n", static_cast<void*>(ac), un->enemy.army_controller_id, ac->id, un->id);
ok = false;
}
else if (!ac && un->enemy.army_controller_id != -1)
{
WARN(log).print("acValidationError: unit {} has has nonzero controller {} but controller pointer is null\n", un->id, un->enemy.army_controller_id);
ok = false;
}
}
last_army_controller_next_id = *army_controller_next_id;
last_ac_vec_size = world->army_controllers.all.size();
INFO(log).print("acValidation: controller count = {}, next id = {}, season tick count = {}\n",
last_ac_vec_size, last_army_controller_next_id, *cur_year_tick);
return ok;
}
}
DFhackCExport DFHack::command_result plugin_init(DFHack::color_ostream& out, std::vector <DFHack::PluginCommand>& commands)
{
return DFHack::CR_OK;
}
DFhackCExport DFHack::command_result plugin_enable(DFHack::color_ostream& out, bool enable)
{
is_enabled = enable;
return DFHack::CR_OK;
}
DFhackCExport DFHack::command_result plugin_onupdate(DFHack::color_ostream& out)
{
bool ok = checkArmyControllerSanity();
if (!ok) {
ERR(log).print("Army controller sanity check failed! Game pause forced.\n");
*pause_state = true;
is_enabled = false;
}
return DFHack::CR_OK;
}