1010
1111#include " df/block_column_print_infost.h"
1212#include " df/construction.h"
13+ #include " df/entity_plot_invasion_mapst.h"
14+ #include " df/historical_entity.h"
15+ #include " df/invasion_info.h"
1316#include " df/map_block.h"
1417#include " df/map_block_column.h"
18+ #include " df/plotinfost.h"
19+ #include " df/plot_invasion_mapst.h"
1520#include " df/world.h"
1621#include " df/z_level_flags.h"
1722
@@ -28,6 +33,7 @@ using namespace df::enums;
2833DFHACK_PLUGIN (" infinite-sky" );
2934DFHACK_PLUGIN_IS_ENABLED (is_enabled);
3035
36+ REQUIRE_GLOBAL (plotinfo);
3137REQUIRE_GLOBAL (world);
3238
3339namespace DFHack {
@@ -130,7 +136,7 @@ static void constructionEventHandler(color_ostream &out, void *ptr) {
130136 doInfiniteSky (out, 1 );
131137}
132138
133- void doInfiniteSky (color_ostream& out, int32_t howMany ) {
139+ void addBlockColumns (color_ostream& out, int32_t quantity ) {
134140 int32_t z_count_block = world->map .z_count_block ;
135141 df::map_block ****block_index = world->map .block_index ;
136142
@@ -141,14 +147,14 @@ void doInfiniteSky(color_ostream& out, int32_t howMany) {
141147 last_air_layer.forCoord ([&](df::coord bpos) {
142148 // Allocate a new block column and copy over data from the old
143149 df::map_block **blockColumn =
144- new df::map_block *[z_count_block + howMany ];
150+ new df::map_block *[z_count_block + quantity ];
145151 memcpy (blockColumn, block_index[bpos.x ][bpos.y ],
146152 z_count_block * sizeof (df::map_block *));
147153 delete[] block_index[bpos.x ][bpos.y ];
148154 block_index[bpos.x ][bpos.y ] = blockColumn;
149155
150156 df::map_block *last_air_block = blockColumn[bpos.z ];
151- for (int32_t count = 0 ; count < howMany ; count++) {
157+ for (int32_t count = 0 ; count < quantity ; count++) {
152158 df::map_block *air_block = new df::map_block ();
153159 std::fill (&air_block->tiletype [0 ][0 ],
154160 &air_block->tiletype [0 ][0 ] + (16 * 16 ),
@@ -209,19 +215,55 @@ void doInfiniteSky(color_ostream& out, int32_t howMany) {
209215 });
210216
211217 // Update global z level flags
212- df::z_level_flags *flags = new df::z_level_flags[z_count_block + howMany ];
218+ df::z_level_flags *flags = new df::z_level_flags[z_count_block + quantity ];
213219 memcpy (flags, world->map_extras .z_level_flags ,
214220 z_count_block * sizeof (df::z_level_flags));
215- for (int32_t count = 0 ; count < howMany ; count++) {
221+ for (int32_t count = 0 ; count < quantity ; count++) {
216222 flags[z_count_block + count].whole = 0 ;
217223 flags[z_count_block + count].bits .update = 1 ;
218224 }
219- world->map .z_count_block += howMany ;
220- world->map .z_count += howMany ;
225+ world->map .z_count_block += quantity ;
226+ world->map .z_count += quantity ;
221227 delete[] world->map_extras .z_level_flags ;
222228 world->map_extras .z_level_flags = flags;
223229}
224230
231+ void updateInvasionMap (color_ostream &out, int32_t new_height, df::plot_invasion_mapst& map) {
232+ if (map.blockz == 0 )
233+ return ; // Unused invasion map
234+ if (map.blockz >= new_height)
235+ return ; // No change required
236+
237+ cuboid blocks (0 , 0 , 0 , map.blockx - 1 , map.blocky - 1 , 0 );
238+ blocks.forCoord ([&](df::coord bpos) {
239+ // Create new vertical block
240+ df::pim_blockst **new_block = new df::pim_blockst *[new_height]();
241+ memcpy (new_block, map.block_index [bpos.x ][bpos.y ], map.blockz * sizeof (df::pim_blockst*));
242+ // Fill new block with nullptr (no information)
243+ std::fill_n (&new_block[map.blockz ], new_height - map.blockz , nullptr );
244+ delete[] map.block_index [bpos.x ][bpos.y ];
245+ map.block_index [bpos.x ][bpos.y ] = new_block;
246+ return true ;
247+ });
248+
249+ map.blockz = new_height;
250+ }
251+
252+ void doInfiniteSky (color_ostream &out, int32_t quantity) {
253+ addBlockColumns (out, quantity);
254+
255+ for (auto & invasion : plotinfo->invasions .list ) {
256+ updateInvasionMap (out, world->map .z_count , invasion->map );
257+ }
258+ for (auto & entity : world->entities .all ) {
259+ for (auto & map : entity->plot_invasion_map ) {
260+ if (map->site_id != plotinfo->site_id )
261+ continue ;
262+ updateInvasionMap (out, world->map .z_count , map->map );
263+ }
264+ }
265+ }
266+
225267struct infinitesky_options {
226268 // whether to display help
227269 bool help = false ;
0 commit comments