Skip to content

Commit 3ffb5f5

Browse files
committedMay 17, 2015
Add optional buffer param for bulk data array writes in Lua
1 parent 28b2476 commit 3ffb5f5

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed
 

Diff for: ‎doc/lua_api.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -2647,7 +2647,9 @@ for 2D noise, and it must be must be larger than 1 for 3D noise (otherwise
26472647
of 3D noise with values starting at `pos={x=,y=,z=}`
26482648
* `get2dMap_flat(pos)`: returns a flat `<size.x * size.y>` element array of 2D noise
26492649
with values starting at `pos={x=,y=}`
2650+
* if the param `buffer` is present, this table will be used to store the result instead
26502651
* `get3dMap_flat(pos)`: Same as `get2dMap_flat`, but 3D noise
2652+
* if the param `buffer` is present, this table will be used to store the result instead
26512653

26522654
### `VoxelManip`
26532655
An interface to the `MapVoxelManipulator` for Lua.
@@ -2665,8 +2667,9 @@ The map will be pre-loaded if two positions are passed to either.
26652667
the `VoxelManip` at that position
26662668
* `set_node_at(pos, node)`: Sets a specific `MapNode` in the `VoxelManip` at
26672669
that position
2668-
* `get_data()`: Gets the data read into the `VoxelManip` object
2669-
* returns raw node data is in the form of an array of node content IDs
2670+
* `get_data(buffer)`: Gets the data read into the `VoxelManip` object
2671+
* returns raw node data in the form of an array of node content IDs
2672+
* if the param `buffer` is present, this table will be used to store the result instead
26702673
* `set_data(data)`: Sets the data contents of the `VoxelManip` object
26712674
* `update_map()`: Update map after writing chunk back to map.
26722675
* To be used only by `VoxelManip` objects created by the mod itself;

Diff for: ‎src/script/lua_api/l_noise.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
174174
n->perlinMap2D(p.X, p.Y);
175175

176176
lua_newtable(L);
177-
for (int y = 0; y != n->sy; y++) {
177+
for (u32 y = 0; y != n->sy; y++) {
178178
lua_newtable(L);
179-
for (int x = 0; x != n->sx; x++) {
179+
for (u32 x = 0; x != n->sx; x++) {
180180
lua_pushnumber(L, n->result[i++]);
181181
lua_rawseti(L, -2, x + 1);
182182
}
@@ -191,14 +191,19 @@ int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L)
191191
NO_MAP_LOCK_REQUIRED;
192192

193193
LuaPerlinNoiseMap *o = checkobject(L, 1);
194-
v2f p = check_v2f(L, 2);
194+
v2f p = check_v2f(L, 2);
195+
bool use_buffer = lua_istable(L, 3);
195196

196197
Noise *n = o->noise;
197198
n->perlinMap2D(p.X, p.Y);
198199

199200
size_t maplen = n->sx * n->sy;
200201

201-
lua_newtable(L);
202+
if (use_buffer)
203+
lua_pushvalue(L, 3);
204+
else
205+
lua_newtable(L);
206+
202207
for (size_t i = 0; i != maplen; i++) {
203208
lua_pushnumber(L, n->result[i]);
204209
lua_rawseti(L, -2, i + 1);
@@ -222,11 +227,11 @@ int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
222227
n->perlinMap3D(p.X, p.Y, p.Z);
223228

224229
lua_newtable(L);
225-
for (int z = 0; z != n->sz; z++) {
230+
for (u32 z = 0; z != n->sz; z++) {
226231
lua_newtable(L);
227-
for (int y = 0; y != n->sy; y++) {
232+
for (u32 y = 0; y != n->sy; y++) {
228233
lua_newtable(L);
229-
for (int x = 0; x != n->sx; x++) {
234+
for (u32 x = 0; x != n->sx; x++) {
230235
lua_pushnumber(L, n->result[i++]);
231236
lua_rawseti(L, -2, x + 1);
232237
}
@@ -243,7 +248,8 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
243248
NO_MAP_LOCK_REQUIRED;
244249

245250
LuaPerlinNoiseMap *o = checkobject(L, 1);
246-
v3f p = check_v3f(L, 2);
251+
v3f p = check_v3f(L, 2);
252+
bool use_buffer = lua_istable(L, 3);
247253

248254
if (!o->m_is3d)
249255
return 0;
@@ -253,7 +259,11 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
253259

254260
size_t maplen = n->sx * n->sy * n->sz;
255261

256-
lua_newtable(L);
262+
if (use_buffer)
263+
lua_pushvalue(L, 3);
264+
else
265+
lua_newtable(L);
266+
257267
for (size_t i = 0; i != maplen; i++) {
258268
lua_pushnumber(L, n->result[i]);
259269
lua_rawseti(L, -2, i + 1);

Diff for: ‎src/script/lua_api/l_vmanip.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,18 @@ int LuaVoxelManip::l_get_data(lua_State *L)
6363
NO_MAP_LOCK_REQUIRED;
6464

6565
LuaVoxelManip *o = checkobject(L, 1);
66+
bool use_buffer = lua_istable(L, 2);
67+
6668
MMVManip *vm = o->vm;
6769

68-
int volume = vm->m_area.getVolume();
70+
u32 volume = vm->m_area.getVolume();
6971

70-
lua_newtable(L);
71-
for (int i = 0; i != volume; i++) {
72+
if (use_buffer)
73+
lua_pushvalue(L, 2);
74+
else
75+
lua_newtable(L);
76+
77+
for (u32 i = 0; i != volume; i++) {
7278
lua_Integer cid = vm->m_data[i].getContent();
7379
lua_pushinteger(L, cid);
7480
lua_rawseti(L, -2, i + 1);
@@ -87,8 +93,8 @@ int LuaVoxelManip::l_set_data(lua_State *L)
8793
if (!lua_istable(L, 2))
8894
return 0;
8995

90-
int volume = vm->m_area.getVolume();
91-
for (int i = 0; i != volume; i++) {
96+
u32 volume = vm->m_area.getVolume();
97+
for (u32 i = 0; i != volume; i++) {
9298
lua_rawgeti(L, 2, i + 1);
9399
content_t c = lua_tointeger(L, -1);
94100

@@ -228,10 +234,10 @@ int LuaVoxelManip::l_get_light_data(lua_State *L)
228234
LuaVoxelManip *o = checkobject(L, 1);
229235
MMVManip *vm = o->vm;
230236

231-
int volume = vm->m_area.getVolume();
237+
u32 volume = vm->m_area.getVolume();
232238

233239
lua_newtable(L);
234-
for (int i = 0; i != volume; i++) {
240+
for (u32 i = 0; i != volume; i++) {
235241
lua_Integer light = vm->m_data[i].param1;
236242
lua_pushinteger(L, light);
237243
lua_rawseti(L, -2, i + 1);
@@ -250,8 +256,8 @@ int LuaVoxelManip::l_set_light_data(lua_State *L)
250256
if (!lua_istable(L, 2))
251257
return 0;
252258

253-
int volume = vm->m_area.getVolume();
254-
for (int i = 0; i != volume; i++) {
259+
u32 volume = vm->m_area.getVolume();
260+
for (u32 i = 0; i != volume; i++) {
255261
lua_rawgeti(L, 2, i + 1);
256262
u8 light = lua_tointeger(L, -1);
257263

@@ -270,10 +276,10 @@ int LuaVoxelManip::l_get_param2_data(lua_State *L)
270276
LuaVoxelManip *o = checkobject(L, 1);
271277
MMVManip *vm = o->vm;
272278

273-
int volume = vm->m_area.getVolume();
279+
u32 volume = vm->m_area.getVolume();
274280

275281
lua_newtable(L);
276-
for (int i = 0; i != volume; i++) {
282+
for (u32 i = 0; i != volume; i++) {
277283
lua_Integer param2 = vm->m_data[i].param2;
278284
lua_pushinteger(L, param2);
279285
lua_rawseti(L, -2, i + 1);
@@ -292,8 +298,8 @@ int LuaVoxelManip::l_set_param2_data(lua_State *L)
292298
if (!lua_istable(L, 2))
293299
return 0;
294300

295-
int volume = vm->m_area.getVolume();
296-
for (int i = 0; i != volume; i++) {
301+
u32 volume = vm->m_area.getVolume();
302+
for (u32 i = 0; i != volume; i++) {
297303
lua_rawgeti(L, 2, i + 1);
298304
u8 param2 = lua_tointeger(L, -1);
299305

0 commit comments

Comments
 (0)
Please sign in to comment.