Skip to content

Commit 69ba0b6

Browse files
committed
Implement suggested changes
1 parent 160f3fa commit 69ba0b6

File tree

2 files changed

+87
-103
lines changed

2 files changed

+87
-103
lines changed

plugins/infinite-sky.cpp

Lines changed: 80 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11

22
#include "Core.h"
3-
#include "DataDefs.h"
43
#include "Debug.h"
5-
#include "Export.h"
64
#include "LuaTools.h"
75
#include "PluginManager.h"
86

9-
#include "modules/World.h"
107
#include "modules/EventManager.h"
8+
#include "modules/Maps.h"
9+
#include "modules/World.h"
1110

1211
#include "df/construction.h"
1312
#include "df/map_block.h"
@@ -19,7 +18,8 @@
1918
#include <string>
2019
#include <vector>
2120

22-
using namespace std;
21+
using std::string;
22+
using std::vector;
2323

2424
using namespace DFHack;
2525
using namespace df::enums;
@@ -45,7 +45,7 @@ enum ConfigValues {
4545
command_result infiniteSky (color_ostream &out, std::vector <std::string> & parameters);
4646

4747
static void constructionEventHandler(color_ostream& out, void* ptr);
48-
EventManager::EventHandler handler(plugin_self, constructionEventHandler,0);
48+
EventManager::EventHandler handler(plugin_self, constructionEventHandler,11);
4949

5050
DFhackCExport command_result plugin_init(color_ostream &out,
5151
std::vector<PluginCommand> &commands) {
@@ -87,11 +87,6 @@ DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) {
8787
return CR_OK;
8888
}
8989

90-
DFhackCExport command_result plugin_shutdown(color_ostream &out) {
91-
cleanup();
92-
return CR_OK;
93-
}
94-
9590
DFhackCExport command_result plugin_load_site_data(color_ostream &out) {
9691
config = World::GetPersistentSiteData(CONFIG_KEY);
9792

@@ -102,7 +97,6 @@ DFhackCExport command_result plugin_load_site_data(color_ostream &out) {
10297
config.set_bool(CONFIG_IS_ENABLED, is_enabled);
10398
}
10499

105-
106100
// Call plugin_enable to set value to ensure the event handler is properly registered
107101
plugin_enable(out, config.get_bool(CONFIG_IS_ENABLED));
108102
DEBUG(control, out)
@@ -118,6 +112,7 @@ DFhackCExport command_result plugin_onstatechange(color_ostream &out,
118112
DEBUG(control, out)
119113
.print("world unloaded; disabling %s\n", plugin_name);
120114
is_enabled = false;
115+
cleanup();
121116
}
122117
}
123118
return CR_OK;
@@ -134,86 +129,84 @@ static void constructionEventHandler(color_ostream &out, void *ptr) {
134129

135130
void doInfiniteSky(color_ostream& out, int32_t howMany) {
136131
CoreSuspender suspend;
137-
int32_t x_count_block = world->map.x_count_block;
138-
int32_t y_count_block = world->map.y_count_block;
139132
int32_t z_count_block = world->map.z_count_block;
140133
df::map_block ****block_index = world->map.block_index;
141134

142-
for (int32_t a = 0; a < x_count_block; a++) {
143-
for (int32_t b = 0; b < y_count_block; b++) {
144-
// Allocate a new block column and copy over data from the old
145-
df::map_block **blockColumn =
146-
new df::map_block *[z_count_block + howMany];
147-
memcpy(blockColumn, block_index[a][b],
148-
z_count_block * sizeof(df::map_block *));
149-
delete[] block_index[a][b];
150-
block_index[a][b] = blockColumn;
151-
152-
df::map_block *last_air_block = blockColumn[z_count_block - 1];
153-
for (int32_t count = 0; count < howMany; count++) {
154-
df::map_block *air_block = new df::map_block();
155-
std::fill(&air_block->tiletype[0][0],
156-
&air_block->tiletype[0][0] + (16 * 16),
157-
df::tiletype::OpenSpace);
158-
159-
// Set block positions properly (based on prior air layer)
160-
air_block->map_pos = last_air_block->map_pos;
161-
air_block->map_pos.z += count + 1;
162-
air_block->region_pos = last_air_block->region_pos;
163-
164-
// Copy other potentially important metadata from prior air
165-
// layer
166-
std::memcpy(air_block->lighting, last_air_block->lighting,
167-
sizeof(air_block->lighting));
168-
std::memcpy(air_block->temperature_1,
169-
last_air_block->temperature_1,
170-
sizeof(air_block->temperature_1));
171-
std::memcpy(air_block->temperature_2,
172-
last_air_block->temperature_2,
173-
sizeof(air_block->temperature_2));
174-
std::memcpy(air_block->region_offset,
175-
last_air_block->region_offset,
176-
sizeof(air_block->region_offset));
177-
178-
// Create tile designations to inform lighting and
179-
// outside markers
180-
df::tile_designation designation{};
181-
designation.bits.light = true;
182-
designation.bits.outside = true;
183-
std::fill(&air_block->designation[0][0],
184-
&air_block->designation[0][0] + (16 * 16),
185-
designation);
186-
187-
blockColumn[z_count_block + count] = air_block;
188-
world->map.map_blocks.push_back(air_block);
189-
190-
// deal with map_block_column stuff even though it'd probably be
191-
// fine
192-
df::map_block_column *column = world->map.column_index[a][b];
193-
if (!column) {
194-
DEBUG(cycle, out)
195-
.print("%s, line %d: column is null (%d, %d).\n",
196-
__FILE__, __LINE__, a, b);
197-
continue;
198-
}
199-
df::map_block_column::T_unmined_glyphs *glyphs =
200-
new df::map_block_column::T_unmined_glyphs;
201-
glyphs->x[0] = 0;
202-
glyphs->x[1] = 1;
203-
glyphs->x[2] = 2;
204-
glyphs->x[3] = 3;
205-
glyphs->y[0] = 0;
206-
glyphs->y[1] = 0;
207-
glyphs->y[2] = 0;
208-
glyphs->y[3] = 0;
209-
glyphs->tile[0] = 'e';
210-
glyphs->tile[1] = 'x';
211-
glyphs->tile[2] = 'p';
212-
glyphs->tile[3] = '^';
213-
column->unmined_glyphs.push_back(glyphs);
135+
cuboid last_air_layer(
136+
0, 0, world->map.z_count_block - 1,
137+
world->map.x_count_block - 1, world->map.y_count_block - 1, world->map.z_count_block - 1);
138+
139+
last_air_layer.forCoord([&](df::coord bpos) {
140+
// Allocate a new block column and copy over data from the old
141+
df::map_block **blockColumn =
142+
new df::map_block *[z_count_block + howMany];
143+
memcpy(blockColumn, block_index[bpos.x][bpos.y],
144+
z_count_block * sizeof(df::map_block *));
145+
delete[] block_index[bpos.x][bpos.y];
146+
block_index[bpos.x][bpos.y] = blockColumn;
147+
148+
df::map_block *last_air_block = blockColumn[bpos.z];
149+
for (int32_t count = 0; count < howMany; count++) {
150+
df::map_block *air_block = new df::map_block();
151+
std::fill(&air_block->tiletype[0][0],
152+
&air_block->tiletype[0][0] + (16 * 16),
153+
df::tiletype::OpenSpace);
154+
155+
// Set block positions properly (based on prior air layer)
156+
air_block->map_pos = last_air_block->map_pos;
157+
air_block->map_pos.z += count + 1;
158+
air_block->region_pos = last_air_block->region_pos;
159+
160+
// Copy other potentially important metadata from prior air
161+
// layer
162+
std::memcpy(air_block->lighting, last_air_block->lighting,
163+
sizeof(air_block->lighting));
164+
std::memcpy(air_block->temperature_1, last_air_block->temperature_1,
165+
sizeof(air_block->temperature_1));
166+
std::memcpy(air_block->temperature_2, last_air_block->temperature_2,
167+
sizeof(air_block->temperature_2));
168+
std::memcpy(air_block->region_offset, last_air_block->region_offset,
169+
sizeof(air_block->region_offset));
170+
171+
// Create tile designations to inform lighting and
172+
// outside markers
173+
df::tile_designation designation{};
174+
designation.bits.light = true;
175+
designation.bits.outside = true;
176+
std::fill(&air_block->designation[0][0],
177+
&air_block->designation[0][0] + (16 * 16), designation);
178+
179+
blockColumn[z_count_block + count] = air_block;
180+
world->map.map_blocks.push_back(air_block);
181+
182+
// deal with map_block_column stuff even though it'd probably be
183+
// fine
184+
df::map_block_column *column =
185+
world->map.column_index[bpos.x][bpos.y];
186+
if (!column) {
187+
DEBUG(cycle, out)
188+
.print("%s, line %d: column is null (%d, %d).\n", __FILE__,
189+
__LINE__, bpos.x, bpos.y);
190+
continue;
214191
}
192+
df::map_block_column::T_unmined_glyphs *glyphs =
193+
new df::map_block_column::T_unmined_glyphs;
194+
glyphs->x[0] = 0;
195+
glyphs->x[1] = 1;
196+
glyphs->x[2] = 2;
197+
glyphs->x[3] = 3;
198+
glyphs->y[0] = 0;
199+
glyphs->y[1] = 0;
200+
glyphs->y[2] = 0;
201+
glyphs->y[3] = 0;
202+
glyphs->tile[0] = 'e';
203+
glyphs->tile[1] = 'x';
204+
glyphs->tile[2] = 'p';
205+
glyphs->tile[3] = '^';
206+
column->unmined_glyphs.push_back(glyphs);
215207
}
216-
}
208+
return true;
209+
});
217210

218211
// Update global z level flags
219212
df::z_level_flags *flags = new df::z_level_flags[z_count_block + howMany];
@@ -258,7 +251,7 @@ command_result infiniteSky(color_ostream &out,
258251
opts.help)
259252
return CR_WRONG_USAGE;
260253

261-
if (opts.n != 0) {
254+
if (opts.n > 0) {
262255
out.print("Infinite-sky: creating %d new z-level%s of sky.\n", opts.n,
263256
opts.n == 1 ? "" : "s");
264257
doInfiniteSky(out, opts.n);

plugins/lua/infinite-sky.lua

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@ local _ENV = mkmodule('plugins.infinite-sky')
22

33
local argparse = require('argparse')
44

5-
local function process_args(opts, args)
6-
if args[1] == 'help' then
5+
function parse_commandline(opts, args)
6+
local positionals = argparse.processArgsGetopt(args, {
7+
{'h', 'help', handler=function() opts.help = true end},
8+
})
9+
if opts.help or positionals[1] == 'help' then
710
opts.help = true
811
return
912
end
10-
11-
if args[1] ~= nil then
12-
opts.n = argparse.positiveInt(args[1])
13-
return
13+
if positionals[1] then
14+
opts.n = argparse.positiveInt(positionals[1])
1415
end
15-
16-
return argparse.processArgsGetopt(args, {
17-
{'h', 'help', handler=function() opts.help = true end},
18-
})
19-
end
20-
21-
function parse_commandline(opts, args)
22-
local positionals = process_args(opts, args)
23-
24-
if opts.help then return end
2516
end
2617

2718
return _ENV

0 commit comments

Comments
 (0)