3 Copyright (C) 2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "lua_api/l_vmanip.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_content.h"
24 #include "common/c_converter.h"
26 #include "environment.h"
31 #define GET_ENV_PTR ServerEnvironment* env = \
32 dynamic_cast<ServerEnvironment*>(getEnv(L)); \
33 if (env == NULL) return 0
36 int LuaVoxelManip::gc_object(lua_State *L)
38 LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
44 int LuaVoxelManip::l_read_from_map(lua_State *L)
46 LuaVoxelManip *o = checkobject(L, 1);
49 v3s16 bp1 = getNodeBlockPos(check_v3s16(L, 2));
50 v3s16 bp2 = getNodeBlockPos(check_v3s16(L, 3));
51 sortBoxVerticies(bp1, bp2);
53 vm->initialEmerge(bp1, bp2);
55 push_v3s16(L, vm->m_area.MinEdge);
56 push_v3s16(L, vm->m_area.MaxEdge);
61 int LuaVoxelManip::l_get_data(lua_State *L)
65 LuaVoxelManip *o = checkobject(L, 1);
66 bool use_buffer = lua_istable(L, 2);
70 u32 volume = vm->m_area.getVolume();
77 for (u32 i = 0; i != volume; i++) {
78 lua_Integer cid = vm->m_data[i].getContent();
79 lua_pushinteger(L, cid);
80 lua_rawseti(L, -2, i + 1);
86 int LuaVoxelManip::l_set_data(lua_State *L)
90 LuaVoxelManip *o = checkobject(L, 1);
93 if (!lua_istable(L, 2))
96 u32 volume = vm->m_area.getVolume();
97 for (u32 i = 0; i != volume; i++) {
98 lua_rawgeti(L, 2, i + 1);
99 content_t c = lua_tointeger(L, -1);
101 vm->m_data[i].setContent(c);
109 int LuaVoxelManip::l_write_to_map(lua_State *L)
111 LuaVoxelManip *o = checkobject(L, 1);
112 MMVManip *vm = o->vm;
114 vm->blitBackAll(&o->modified_blocks);
119 int LuaVoxelManip::l_get_node_at(lua_State *L)
121 NO_MAP_LOCK_REQUIRED;
124 LuaVoxelManip *o = checkobject(L, 1);
125 v3s16 pos = check_v3s16(L, 2);
127 pushnode(L, o->vm->getNodeNoExNoEmerge(pos), env->getGameDef()->ndef());
131 int LuaVoxelManip::l_set_node_at(lua_State *L)
133 NO_MAP_LOCK_REQUIRED;
136 LuaVoxelManip *o = checkobject(L, 1);
137 v3s16 pos = check_v3s16(L, 2);
138 MapNode n = readnode(L, 3, env->getGameDef()->ndef());
140 o->vm->setNodeNoEmerge(pos, n);
145 int LuaVoxelManip::l_update_liquids(lua_State *L)
149 LuaVoxelManip *o = checkobject(L, 1);
151 Map *map = &(env->getMap());
152 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
153 MMVManip *vm = o->vm;
159 mg.updateLiquid(&map->m_transforming_liquid,
160 vm->m_area.MinEdge, vm->m_area.MaxEdge);
165 int LuaVoxelManip::l_calc_lighting(lua_State *L)
167 NO_MAP_LOCK_REQUIRED;
169 LuaVoxelManip *o = checkobject(L, 1);
170 if (!o->is_mapgen_vm)
173 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
174 EmergeManager *emerge = getServer(L)->getEmergeManager();
175 MMVManip *vm = o->vm;
177 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
178 v3s16 fpmin = vm->m_area.MinEdge;
179 v3s16 fpmax = vm->m_area.MaxEdge;
180 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) : fpmin + yblock;
181 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) : fpmax - yblock;
183 sortBoxVerticies(pmin, pmax);
184 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
185 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
190 mg.water_level = emerge->params.water_level;
192 mg.calcLighting(pmin, pmax, fpmin, fpmax);
197 int LuaVoxelManip::l_set_lighting(lua_State *L)
199 NO_MAP_LOCK_REQUIRED;
201 LuaVoxelManip *o = checkobject(L, 1);
202 if (!o->is_mapgen_vm)
205 if (!lua_istable(L, 2))
209 light = (getintfield_default(L, 2, "day", 0) & 0x0F);
210 light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
212 MMVManip *vm = o->vm;
214 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
215 v3s16 pmin = lua_istable(L, 3) ? check_v3s16(L, 3) : vm->m_area.MinEdge + yblock;
216 v3s16 pmax = lua_istable(L, 4) ? check_v3s16(L, 4) : vm->m_area.MaxEdge - yblock;
218 sortBoxVerticies(pmin, pmax);
219 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
220 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
225 mg.setLighting(light, pmin, pmax);
230 int LuaVoxelManip::l_get_light_data(lua_State *L)
232 NO_MAP_LOCK_REQUIRED;
234 LuaVoxelManip *o = checkobject(L, 1);
235 MMVManip *vm = o->vm;
237 u32 volume = vm->m_area.getVolume();
240 for (u32 i = 0; i != volume; i++) {
241 lua_Integer light = vm->m_data[i].param1;
242 lua_pushinteger(L, light);
243 lua_rawseti(L, -2, i + 1);
249 int LuaVoxelManip::l_set_light_data(lua_State *L)
251 NO_MAP_LOCK_REQUIRED;
253 LuaVoxelManip *o = checkobject(L, 1);
254 MMVManip *vm = o->vm;
256 if (!lua_istable(L, 2))
259 u32 volume = vm->m_area.getVolume();
260 for (u32 i = 0; i != volume; i++) {
261 lua_rawgeti(L, 2, i + 1);
262 u8 light = lua_tointeger(L, -1);
264 vm->m_data[i].param1 = light;
272 int LuaVoxelManip::l_get_param2_data(lua_State *L)
274 NO_MAP_LOCK_REQUIRED;
276 LuaVoxelManip *o = checkobject(L, 1);
277 MMVManip *vm = o->vm;
279 u32 volume = vm->m_area.getVolume();
282 for (u32 i = 0; i != volume; i++) {
283 lua_Integer param2 = vm->m_data[i].param2;
284 lua_pushinteger(L, param2);
285 lua_rawseti(L, -2, i + 1);
291 int LuaVoxelManip::l_set_param2_data(lua_State *L)
293 NO_MAP_LOCK_REQUIRED;
295 LuaVoxelManip *o = checkobject(L, 1);
296 MMVManip *vm = o->vm;
298 if (!lua_istable(L, 2))
301 u32 volume = vm->m_area.getVolume();
302 for (u32 i = 0; i != volume; i++) {
303 lua_rawgeti(L, 2, i + 1);
304 u8 param2 = lua_tointeger(L, -1);
306 vm->m_data[i].param2 = param2;
314 int LuaVoxelManip::l_update_map(lua_State *L)
316 LuaVoxelManip *o = checkobject(L, 1);
320 Environment *env = getEnv(L);
324 Map *map = &(env->getMap());
326 // TODO: Optimize this by using Mapgen::calcLighting() instead
327 std::map<v3s16, MapBlock *> lighting_mblocks;
328 std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
330 lighting_mblocks.insert(mblocks->begin(), mblocks->end());
332 map->updateLighting(lighting_mblocks, *mblocks);
335 event.type = MEET_OTHER;
336 for (std::map<v3s16, MapBlock *>::iterator
337 it = mblocks->begin();
338 it != mblocks->end(); ++it)
339 event.modified_blocks.insert(it->first);
341 map->dispatchEvent(&event);
348 int LuaVoxelManip::l_was_modified(lua_State *L)
350 NO_MAP_LOCK_REQUIRED;
352 LuaVoxelManip *o = checkobject(L, 1);
353 MMVManip *vm = o->vm;
355 lua_pushboolean(L, vm->m_is_dirty);
360 int LuaVoxelManip::l_get_emerged_area(lua_State *L)
362 LuaVoxelManip *o = checkobject(L, 1);
364 push_v3s16(L, o->vm->m_area.MinEdge);
365 push_v3s16(L, o->vm->m_area.MaxEdge);
370 LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm)
373 this->is_mapgen_vm = is_mg_vm;
376 LuaVoxelManip::LuaVoxelManip(Map *map)
378 this->vm = new MMVManip(map);
379 this->is_mapgen_vm = false;
382 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
384 this->vm = new MMVManip(map);
385 this->is_mapgen_vm = false;
387 v3s16 bp1 = getNodeBlockPos(p1);
388 v3s16 bp2 = getNodeBlockPos(p2);
389 sortBoxVerticies(bp1, bp2);
390 vm->initialEmerge(bp1, bp2);
393 LuaVoxelManip::~LuaVoxelManip()
400 // Creates an LuaVoxelManip and leaves it on top of stack
401 int LuaVoxelManip::create_object(lua_State *L)
403 NO_MAP_LOCK_REQUIRED;
405 Environment *env = getEnv(L);
409 Map *map = &(env->getMap());
410 LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
411 new LuaVoxelManip(map, check_v3s16(L, 1), check_v3s16(L, 2)) :
412 new LuaVoxelManip(map);
414 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
415 luaL_getmetatable(L, className);
416 lua_setmetatable(L, -2);
420 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
422 NO_MAP_LOCK_REQUIRED;
424 luaL_checktype(L, narg, LUA_TUSERDATA);
426 void *ud = luaL_checkudata(L, narg, className);
428 luaL_typerror(L, narg, className);
430 return *(LuaVoxelManip **)ud; // unbox pointer
433 void LuaVoxelManip::Register(lua_State *L)
436 int methodtable = lua_gettop(L);
437 luaL_newmetatable(L, className);
438 int metatable = lua_gettop(L);
440 lua_pushliteral(L, "__metatable");
441 lua_pushvalue(L, methodtable);
442 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
444 lua_pushliteral(L, "__index");
445 lua_pushvalue(L, methodtable);
446 lua_settable(L, metatable);
448 lua_pushliteral(L, "__gc");
449 lua_pushcfunction(L, gc_object);
450 lua_settable(L, metatable);
452 lua_pop(L, 1); // drop metatable
454 luaL_openlib(L, 0, methods, 0); // fill methodtable
455 lua_pop(L, 1); // drop methodtable
457 // Can be created from Lua (VoxelManip())
458 lua_register(L, className, create_object);
461 const char LuaVoxelManip::className[] = "VoxelManip";
462 const luaL_reg LuaVoxelManip::methods[] = {
463 luamethod(LuaVoxelManip, read_from_map),
464 luamethod(LuaVoxelManip, get_data),
465 luamethod(LuaVoxelManip, set_data),
466 luamethod(LuaVoxelManip, get_node_at),
467 luamethod(LuaVoxelManip, set_node_at),
468 luamethod(LuaVoxelManip, write_to_map),
469 luamethod(LuaVoxelManip, update_map),
470 luamethod(LuaVoxelManip, update_liquids),
471 luamethod(LuaVoxelManip, calc_lighting),
472 luamethod(LuaVoxelManip, set_lighting),
473 luamethod(LuaVoxelManip, get_light_data),
474 luamethod(LuaVoxelManip, set_light_data),
475 luamethod(LuaVoxelManip, get_param2_data),
476 luamethod(LuaVoxelManip, set_param2_data),
477 luamethod(LuaVoxelManip, was_modified),
478 luamethod(LuaVoxelManip, get_emerged_area),