1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-11-17 23:05:07 -05:00

Fix issue 1527

https://github.com/minetest/minetest/issues/1527
This commit is contained in:
Craig Robbins 2014-07-30 11:39:24 +10:00 committed by kwolekr
parent 17345404ba
commit 6929206bc2

View File

@ -31,9 +31,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
int LuaVoxelManip::gc_object(lua_State *L) int LuaVoxelManip::gc_object(lua_State *L)
{ {
LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1)); LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
if (!o->is_mapgen_vm) delete o;
delete o;
return 0; return 0;
} }
@ -41,16 +40,16 @@ int LuaVoxelManip::l_read_from_map(lua_State *L)
{ {
LuaVoxelManip *o = checkobject(L, 1); LuaVoxelManip *o = checkobject(L, 1);
ManualMapVoxelManipulator *vm = o->vm; ManualMapVoxelManipulator *vm = o->vm;
v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2)); v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3)); v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3));
sortBoxVerticies(bp1, bp2); sortBoxVerticies(bp1, bp2);
vm->initialEmerge(bp1, bp2); vm->initialEmerge(bp1, bp2);
push_v3s16(L, vm->m_area.MinEdge); push_v3s16(L, vm->m_area.MinEdge);
push_v3s16(L, vm->m_area.MaxEdge); push_v3s16(L, vm->m_area.MaxEdge);
return 2; return 2;
} }
@ -60,39 +59,39 @@ int LuaVoxelManip::l_get_data(lua_State *L)
LuaVoxelManip *o = checkobject(L, 1); LuaVoxelManip *o = checkobject(L, 1);
ManualMapVoxelManipulator *vm = o->vm; ManualMapVoxelManipulator *vm = o->vm;
int volume = vm->m_area.getVolume(); int volume = vm->m_area.getVolume();
lua_newtable(L); lua_newtable(L);
for (int i = 0; i != volume; i++) { for (int i = 0; i != volume; i++) {
lua_Integer cid = vm->m_data[i].getContent(); lua_Integer cid = vm->m_data[i].getContent();
lua_pushinteger(L, cid); lua_pushinteger(L, cid);
lua_rawseti(L, -2, i + 1); lua_rawseti(L, -2, i + 1);
} }
return 1; return 1;
} }
int LuaVoxelManip::l_set_data(lua_State *L) int LuaVoxelManip::l_set_data(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
LuaVoxelManip *o = checkobject(L, 1); LuaVoxelManip *o = checkobject(L, 1);
ManualMapVoxelManipulator *vm = o->vm; ManualMapVoxelManipulator *vm = o->vm;
if (!lua_istable(L, 2)) if (!lua_istable(L, 2))
return 0; return 0;
int volume = vm->m_area.getVolume(); int volume = vm->m_area.getVolume();
for (int i = 0; i != volume; i++) { for (int i = 0; i != volume; i++) {
lua_rawgeti(L, 2, i + 1); lua_rawgeti(L, 2, i + 1);
content_t c = lua_tointeger(L, -1); content_t c = lua_tointeger(L, -1);
vm->m_data[i].setContent(c); vm->m_data[i].setContent(c);
lua_pop(L, 1); lua_pop(L, 1);
} }
return 0; return 0;
} }
@ -103,7 +102,7 @@ int LuaVoxelManip::l_write_to_map(lua_State *L)
vm->blitBackAll(&o->modified_blocks); vm->blitBackAll(&o->modified_blocks);
return 0; return 0;
} }
int LuaVoxelManip::l_update_liquids(lua_State *L) int LuaVoxelManip::l_update_liquids(lua_State *L)
@ -131,7 +130,7 @@ int LuaVoxelManip::l_update_liquids(lua_State *L)
int LuaVoxelManip::l_calc_lighting(lua_State *L) int LuaVoxelManip::l_calc_lighting(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
LuaVoxelManip *o = checkobject(L, 1); LuaVoxelManip *o = checkobject(L, 1);
if (!o->is_mapgen_vm) if (!o->is_mapgen_vm)
return 0; return 0;
@ -150,7 +149,7 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L)
mg.vm = vm; mg.vm = vm;
mg.ndef = ndef; mg.ndef = ndef;
mg.water_level = emerge->params.water_level; mg.water_level = emerge->params.water_level;
mg.calcLighting(p1, p2); mg.calcLighting(p1, p2);
return 0; return 0;
@ -159,20 +158,20 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L)
int LuaVoxelManip::l_set_lighting(lua_State *L) int LuaVoxelManip::l_set_lighting(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
LuaVoxelManip *o = checkobject(L, 1); LuaVoxelManip *o = checkobject(L, 1);
if (!o->is_mapgen_vm) if (!o->is_mapgen_vm)
return 0; return 0;
if (!lua_istable(L, 2)) if (!lua_istable(L, 2))
return 0; return 0;
u8 light; u8 light;
light = (getintfield_default(L, 2, "day", 0) & 0x0F); light = (getintfield_default(L, 2, "day", 0) & 0x0F);
light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4; light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
ManualMapVoxelManipulator *vm = o->vm; ManualMapVoxelManipulator *vm = o->vm;
v3s16 p1 = lua_istable(L, 3) ? read_v3s16(L, 3) : v3s16 p1 = lua_istable(L, 3) ? read_v3s16(L, 3) :
vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE; vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE;
v3s16 p2 = lua_istable(L, 4) ? read_v3s16(L, 4) : v3s16 p2 = lua_istable(L, 4) ? read_v3s16(L, 4) :
@ -181,7 +180,7 @@ int LuaVoxelManip::l_set_lighting(lua_State *L)
Mapgen mg; Mapgen mg;
mg.vm = vm; mg.vm = vm;
mg.setLighting(p1, p2, light); mg.setLighting(p1, p2, light);
return 0; return 0;
@ -276,7 +275,7 @@ int LuaVoxelManip::l_update_map(lua_State *L)
LuaVoxelManip *o = checkobject(L, 1); LuaVoxelManip *o = checkobject(L, 1);
if (o->is_mapgen_vm) if (o->is_mapgen_vm)
return 0; return 0;
Environment *env = getEnv(L); Environment *env = getEnv(L);
if (!env) if (!env)
return 0; return 0;
@ -286,9 +285,9 @@ int LuaVoxelManip::l_update_map(lua_State *L)
// TODO: Optimize this by using Mapgen::calcLighting() instead // TODO: Optimize this by using Mapgen::calcLighting() instead
std::map<v3s16, MapBlock *> lighting_mblocks; std::map<v3s16, MapBlock *> lighting_mblocks;
std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks; std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
lighting_mblocks.insert(mblocks->begin(), mblocks->end()); lighting_mblocks.insert(mblocks->begin(), mblocks->end());
map->updateLighting(lighting_mblocks, *mblocks); map->updateLighting(lighting_mblocks, *mblocks);
MapEditEvent event; MapEditEvent event;
@ -297,12 +296,12 @@ int LuaVoxelManip::l_update_map(lua_State *L)
it = mblocks->begin(); it = mblocks->begin();
it != mblocks->end(); ++it) it != mblocks->end(); ++it)
event.modified_blocks.insert(it->first); event.modified_blocks.insert(it->first);
map->dispatchEvent(&event); map->dispatchEvent(&event);
mblocks->clear(); mblocks->clear();
return 0; return 0;
} }
LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm) LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
@ -319,7 +318,8 @@ LuaVoxelManip::LuaVoxelManip(Map *map)
LuaVoxelManip::~LuaVoxelManip() LuaVoxelManip::~LuaVoxelManip()
{ {
delete vm; if (!is_mapgen_vm)
delete vm;
} }
// LuaVoxelManip() // LuaVoxelManip()
@ -327,14 +327,14 @@ LuaVoxelManip::~LuaVoxelManip()
int LuaVoxelManip::create_object(lua_State *L) int LuaVoxelManip::create_object(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
Environment *env = getEnv(L); Environment *env = getEnv(L);
if (!env) if (!env)
return 0; return 0;
Map *map = &(env->getMap()); Map *map = &(env->getMap());
LuaVoxelManip *o = new LuaVoxelManip(map); LuaVoxelManip *o = new LuaVoxelManip(map);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o; *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className); luaL_getmetatable(L, className);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
@ -344,13 +344,13 @@ int LuaVoxelManip::create_object(lua_State *L)
LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg) LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
luaL_checktype(L, narg, LUA_TUSERDATA); luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className); void *ud = luaL_checkudata(L, narg, className);
if (!ud) if (!ud)
luaL_typerror(L, narg, className); luaL_typerror(L, narg, className);
return *(LuaVoxelManip **)ud; // unbox pointer return *(LuaVoxelManip **)ud; // unbox pointer
} }