1
1
2
2
#include " Core.h"
3
- #include " Console.h"
4
3
#include " DataDefs.h"
5
4
#include " Debug.h"
6
5
#include " Export.h"
7
6
#include " PluginManager.h"
8
7
9
8
#include " modules/World.h"
9
+ #include " modules/EventManager.h"
10
10
11
11
#include " df/construction.h"
12
- #include " df/game_mode.h"
13
12
#include " df/map_block.h"
14
13
#include " df/map_block_column.h"
15
14
#include " df/world.h"
@@ -44,6 +43,9 @@ enum ConfigValues {
44
43
45
44
command_result infiniteSky (color_ostream &out, std::vector <std::string> & parameters);
46
45
46
+ static void constructionEventHandler (color_ostream& out, void * ptr);
47
+ EventManager::EventHandler handler (plugin_self, constructionEventHandler,0 );
48
+
47
49
DFhackCExport command_result plugin_init (color_ostream &out,
48
50
std::vector<PluginCommand> &commands) {
49
51
commands.push_back (PluginCommand (
@@ -52,6 +54,10 @@ DFhackCExport command_result plugin_init(color_ostream &out,
52
54
return CR_OK;
53
55
}
54
56
57
+ void cleanup () {
58
+ EventManager::unregister (EventManager::EventType::CONSTRUCTION, handler);
59
+ }
60
+
55
61
DFhackCExport command_result plugin_enable (color_ostream &out, bool enable) {
56
62
if (!Core::getInstance ().isMapLoaded () || !World::isFortressMode ()) {
57
63
out.printerr (" Cannot enable %s without a loaded fort.\n " , plugin_name);
@@ -63,6 +69,13 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
63
69
.print (" %s from the API; persisting\n " ,
64
70
is_enabled ? " enabled" : " disabled" );
65
71
config.set_bool (CONFIG_IS_ENABLED, is_enabled);
72
+
73
+ if (enable) {
74
+ EventManager::registerListener (
75
+ EventManager::EventType::CONSTRUCTION, handler);
76
+ } else {
77
+ cleanup ();
78
+ }
66
79
} else {
67
80
DEBUG (control, out)
68
81
.print (" %s from the API, but already %s; no action\n " ,
@@ -73,6 +86,11 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
73
86
return CR_OK;
74
87
}
75
88
89
+ DFhackCExport command_result plugin_shutdown (color_ostream &out) {
90
+ cleanup ();
91
+ return CR_OK;
92
+ }
93
+
76
94
DFhackCExport command_result plugin_load_site_data (color_ostream &out) {
77
95
config = World::GetPersistentSiteData (CONFIG_KEY);
78
96
@@ -83,10 +101,9 @@ DFhackCExport command_result plugin_load_site_data(color_ostream &out) {
83
101
config.set_bool (CONFIG_IS_ENABLED, is_enabled);
84
102
}
85
103
86
- // we have to copy our enabled flag into the global plugin variable, but
87
- // all the other state we can directly read/modify from the persistent
88
- // data structure.
89
- is_enabled = config.get_bool (CONFIG_IS_ENABLED);
104
+
105
+ // Call plugin_enable to set value to ensure the event handler is properly registered
106
+ plugin_enable (out, config.get_bool (CONFIG_IS_ENABLED));
90
107
DEBUG (control, out)
91
108
.print (" loading persisted enabled state: %s\n " ,
92
109
is_enabled ? " true" : " false" );
@@ -105,35 +122,13 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out,
105
122
return CR_OK;
106
123
}
107
124
108
- static size_t constructionSize = 0 ;
109
125
void doInfiniteSky (color_ostream& out, int32_t howMany);
110
126
111
- DFhackCExport command_result plugin_onupdate ( color_ostream &out )
112
- {
113
- if ( !Core::getInstance ().isMapLoaded () )
114
- return CR_OK;
115
- {
116
- t_gamemodes mode;
117
- if ( !World::ReadGameMode (mode) )
118
- return CR_FAILURE;
119
- if ( mode.g_mode != df::enums::game_mode::DWARF )
120
- return CR_OK;
121
- }
127
+ static void constructionEventHandler (color_ostream &out, void *ptr) {
128
+ df::construction *constr = (df::construction *)ptr;
122
129
123
- if ( world->event .constructions .size () == constructionSize )
124
- return CR_OK;
125
- int32_t zNow = world->map .z_count_block ;
126
- for ( size_t a = constructionSize; a < world->event .constructions .size (); a++ ) {
127
- df::construction* construct = world->event .constructions [a];
128
- if ( construct->pos .z +2 < zNow )
129
- continue ;
130
+ if (constr->pos .z >= world->map .z_count_block - 2 )
130
131
doInfiniteSky (out, 1 );
131
- zNow = world->map .z_count_block ;
132
- // /break;
133
- }
134
- constructionSize = world->event .constructions .size ();
135
-
136
- return CR_OK;
137
132
}
138
133
139
134
void doInfiniteSky (color_ostream& out, int32_t howMany) {
@@ -242,17 +237,6 @@ command_result infiniteSky (color_ostream &out, std::vector <std::string> & para
242
237
is_enabled ? " enabled" : " disabled" );
243
238
return CR_OK;
244
239
}
245
- if (parameters[0 ] == " enable" ) {
246
- plugin_enable (out, true );
247
- out.print (" Construction monitoring enabled.\n " );
248
- return CR_OK;
249
- }
250
- if (parameters[0 ] == " disable" ) {
251
- plugin_enable (out, false );
252
- out.print (" Construction monitoring disabled.\n " );
253
- constructionSize = 0 ;
254
- return CR_OK;
255
- }
256
240
int32_t howMany = 0 ;
257
241
howMany = atoi (parameters[0 ].c_str ());
258
242
out.print (" InfiniteSky: creating %d new z-level%s of sky.\n " , howMany, howMany == 1 ? " " : " s" );
0 commit comments