Skip to content

Commit 8334100

Browse files
committedDec 28, 2014
LuaVoxelManip: Add option to allocate blank data
1 parent ae2721f commit 8334100

File tree

6 files changed

+60
-4
lines changed

6 files changed

+60
-4
lines changed
 

‎doc/lua_api.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,8 @@ minetest.get_perlin(seeddiff, octaves, persistence, scale)
15941594
^ Return world-specific perlin noise (int(worldseed)+seeddiff)
15951595
minetest.get_voxel_manip()
15961596
^ Return voxel manipulator object
1597+
minetest.get_voxel_manip(p1, p2)
1598+
^ Return voxel manipulator object with blank data preallocated
15971599
minetest.set_gen_notify(flags, {deco_ids})
15981600
^ Set the types of on-generate notifications that should be collected
15991601
^ flags is a flag field with the available flags:
@@ -2192,6 +2194,7 @@ methods:
21922194
VoxelManip: An interface to the MapVoxelManipulator for Lua
21932195
- Can be created via VoxelManip()
21942196
- Also minetest.get_voxel_manip()
2197+
- Specify a pmin, pmax in either to allocate a blank chunk of data prefilled with cignore
21952198
methods:
21962199
- read_from_map(p1, p2): Reads a chunk of map from the map containing the region formed by p1 and p2.
21972200
^ returns actual emerged pmin, actual emerged pmax
@@ -2223,6 +2226,7 @@ methods:
22232226
- update_liquids(): Update liquid flow
22242227
- was_modified(): Returns true or false if the data in the voxel manipulator had been modified since
22252228
the last read from map, due to a call to minetest.set_data() on the loaded area elsewhere
2229+
- get_emerged_area(): Returns actual emerged pmin, actual emerged pmax
22262230

22272231
VoxelArea: A helper class for voxel areas
22282232
- Can be created via VoxelArea:new{MinEdge=pmin, MaxEdge=pmax}

‎src/map.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -3597,8 +3597,31 @@ ManualMapVoxelManipulator::~ManualMapVoxelManipulator()
35973597
{
35983598
}
35993599

3600+
void ManualMapVoxelManipulator::initializeBlank(v3s16 blockpos_min,
3601+
v3s16 blockpos_max)
3602+
{
3603+
// Units of these are MapBlocks
3604+
v3s16 pmin = blockpos_min;
3605+
v3s16 pmax = blockpos_max;
3606+
3607+
VoxelArea block_area_nodes(pmin * MAP_BLOCKSIZE,
3608+
(pmax + 1) * MAP_BLOCKSIZE - v3s16(1,1,1));
3609+
3610+
addArea(block_area_nodes);
3611+
u32 extent = m_area.getVolume();
3612+
for (u32 i = 0; i != extent; i++)
3613+
m_data[i] = MapNode(CONTENT_IGNORE);
3614+
3615+
for (s32 z = pmin.Z; z <= pmax.Z; z++)
3616+
for (s32 y = pmin.Y; y <= pmax.Y; y++)
3617+
for (s32 x = pmin.X; x <= pmax.X; x++)
3618+
m_loaded_blocks[v3s16(x, y, z)] = 0;
3619+
3620+
m_is_dirty = false;
3621+
}
3622+
36003623
void ManualMapVoxelManipulator::initialEmerge(v3s16 blockpos_min,
3601-
v3s16 blockpos_max, bool load_if_inexistent)
3624+
v3s16 blockpos_max, bool load_if_inexistent)
36023625
{
36033626
TimeTaker timer1("initialEmerge", &emerge_time);
36043627

‎src/map.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -550,12 +550,14 @@ class ManualMapVoxelManipulator : public VoxelManipulator
550550
void setMap(Map *map)
551551
{m_map = map;}
552552

553+
void initializeBlank(v3s16 pmin, v3s16 pmax);
554+
553555
void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
554-
bool load_if_inexistent = true);
556+
bool load_if_inexistent = true);
555557

556558
// This is much faster with big chunks of generated data
557559
void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
558-
bool overwrite_generated = true);
560+
bool overwrite_generated = true);
559561

560562
bool m_is_dirty;
561563

‎src/script/lua_api/l_env.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,13 @@ int ModApiEnvMod::l_get_voxel_manip(lua_State *L)
638638
Map *map = &(env->getMap());
639639
LuaVoxelManip *o = new LuaVoxelManip(map);
640640

641+
if (lua_istable(L, 1) && lua_istable(L, 2)) {
642+
v3s16 p1 = getNodeBlockPos(read_v3s16(L, 1));
643+
v3s16 p2 = getNodeBlockPos(read_v3s16(L, 2));
644+
sortBoxVerticies(p1, p2);
645+
o->vm->initializeBlank(p1, p2);
646+
}
647+
641648
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
642649
luaL_getmetatable(L, "VoxelManip");
643650
lua_setmetatable(L, -2);

‎src/script/lua_api/l_vmanip.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ int LuaVoxelManip::l_was_modified(lua_State *L)
345345
return 1;
346346
}
347347

348+
int LuaVoxelManip::l_get_emerged_area(lua_State *L)
349+
{
350+
LuaVoxelManip *o = checkobject(L, 1);
351+
352+
push_v3s16(L, o->vm->m_area.MinEdge);
353+
push_v3s16(L, o->vm->m_area.MaxEdge);
354+
355+
return 2;
356+
}
357+
348358
LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
349359
{
350360
this->vm = mmvm;
@@ -376,6 +386,13 @@ int LuaVoxelManip::create_object(lua_State *L)
376386
Map *map = &(env->getMap());
377387
LuaVoxelManip *o = new LuaVoxelManip(map);
378388

389+
if (lua_istable(L, 1) && lua_istable(L, 2)) {
390+
v3s16 p1 = getNodeBlockPos(read_v3s16(L, 1));
391+
v3s16 p2 = getNodeBlockPos(read_v3s16(L, 2));
392+
sortBoxVerticies(p1, p2);
393+
o->vm->initializeBlank(p1, p2);
394+
}
395+
379396
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
380397
luaL_getmetatable(L, className);
381398
lua_setmetatable(L, -2);
@@ -440,5 +457,6 @@ const luaL_reg LuaVoxelManip::methods[] = {
440457
luamethod(LuaVoxelManip, get_param2_data),
441458
luamethod(LuaVoxelManip, set_param2_data),
442459
luamethod(LuaVoxelManip, was_modified),
460+
luamethod(LuaVoxelManip, get_emerged_area),
443461
{0,0}
444462
};

‎src/script/lua_api/l_vmanip.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class ManualMapVoxelManipulator;
3333
*/
3434
class LuaVoxelManip : public ModApiBase {
3535
private:
36-
ManualMapVoxelManipulator *vm;
3736
std::map<v3s16, MapBlock *> modified_blocks;
3837
bool is_mapgen_vm;
3938

@@ -62,8 +61,11 @@ class LuaVoxelManip : public ModApiBase {
6261
static int l_set_param2_data(lua_State *L);
6362

6463
static int l_was_modified(lua_State *L);
64+
static int l_get_emerged_area(lua_State *L);
6565

6666
public:
67+
ManualMapVoxelManipulator *vm;
68+
6769
LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mapgen_vm);
6870
LuaVoxelManip(Map *map);
6971
~LuaVoxelManip();

0 commit comments

Comments
 (0)