Skip to content

Commit a0bd8ee

Browse files
committed
Implement automatic z-level additions using EventType::CONSTRUCTION
1 parent 837b93c commit a0bd8ee

File tree

1 file changed

+26
-42
lines changed

1 file changed

+26
-42
lines changed

plugins/infiniteSky.cpp

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11

22
#include "Core.h"
3-
#include "Console.h"
43
#include "DataDefs.h"
54
#include "Debug.h"
65
#include "Export.h"
76
#include "PluginManager.h"
87

98
#include "modules/World.h"
9+
#include "modules/EventManager.h"
1010

1111
#include "df/construction.h"
12-
#include "df/game_mode.h"
1312
#include "df/map_block.h"
1413
#include "df/map_block_column.h"
1514
#include "df/world.h"
@@ -44,6 +43,9 @@ enum ConfigValues {
4443

4544
command_result infiniteSky (color_ostream &out, std::vector <std::string> & parameters);
4645

46+
static void constructionEventHandler(color_ostream& out, void* ptr);
47+
EventManager::EventHandler handler(plugin_self, constructionEventHandler,0);
48+
4749
DFhackCExport command_result plugin_init(color_ostream &out,
4850
std::vector<PluginCommand> &commands) {
4951
commands.push_back(PluginCommand(
@@ -52,6 +54,10 @@ DFhackCExport command_result plugin_init(color_ostream &out,
5254
return CR_OK;
5355
}
5456

57+
void cleanup() {
58+
EventManager::unregister(EventManager::EventType::CONSTRUCTION, handler);
59+
}
60+
5561
DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
5662
if (!Core::getInstance().isMapLoaded() || !World::isFortressMode()) {
5763
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) {
6369
.print("%s from the API; persisting\n",
6470
is_enabled ? "enabled" : "disabled");
6571
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+
}
6679
} else {
6780
DEBUG(control, out)
6881
.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) {
7386
return CR_OK;
7487
}
7588

89+
DFhackCExport command_result plugin_shutdown(color_ostream &out) {
90+
cleanup();
91+
return CR_OK;
92+
}
93+
7694
DFhackCExport command_result plugin_load_site_data(color_ostream &out) {
7795
config = World::GetPersistentSiteData(CONFIG_KEY);
7896

@@ -83,10 +101,9 @@ DFhackCExport command_result plugin_load_site_data(color_ostream &out) {
83101
config.set_bool(CONFIG_IS_ENABLED, is_enabled);
84102
}
85103

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));
90107
DEBUG(control, out)
91108
.print("loading persisted enabled state: %s\n",
92109
is_enabled ? "true" : "false");
@@ -105,35 +122,13 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out,
105122
return CR_OK;
106123
}
107124

108-
static size_t constructionSize = 0;
109125
void doInfiniteSky(color_ostream& out, int32_t howMany);
110126

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;
122129

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)
130131
doInfiniteSky(out, 1);
131-
zNow = world->map.z_count_block;
132-
///break;
133-
}
134-
constructionSize = world->event.constructions.size();
135-
136-
return CR_OK;
137132
}
138133

139134
void doInfiniteSky(color_ostream& out, int32_t howMany) {
@@ -242,17 +237,6 @@ command_result infiniteSky (color_ostream &out, std::vector <std::string> & para
242237
is_enabled ? "enabled" : "disabled");
243238
return CR_OK;
244239
}
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-
}
256240
int32_t howMany = 0;
257241
howMany = atoi(parameters[0].c_str());
258242
out.print("InfiniteSky: creating %d new z-level%s of sky.\n", howMany, howMany == 1 ? "" : "s" );

0 commit comments

Comments
 (0)