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"
32 int LuaVoxelManip::gc_object(lua_State *L)
34 LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
40 int LuaVoxelManip::l_read_from_map(lua_State *L)
44 LuaVoxelManip *o = checkobject(L, 1);
47 v3s16 bp1 = getNodeBlockPos(check_v3s16(L, 2));
48 v3s16 bp2 = getNodeBlockPos(check_v3s16(L, 3));
49 sortBoxVerticies(bp1, bp2);
51 vm->initialEmerge(bp1, bp2);
53 push_v3s16(L, vm->m_area.MinEdge);
54 push_v3s16(L, vm->m_area.MaxEdge);
59 int LuaVoxelManip::l_get_data(lua_State *L)
63 LuaVoxelManip *o = checkobject(L, 1);
64 bool use_buffer = lua_istable(L, 2);
68 u32 volume = vm->m_area.getVolume();
75 for (u32 i = 0; i != volume; i++) {
76 lua_Integer cid = vm->m_data[i].getContent();
77 lua_pushinteger(L, cid);
78 lua_rawseti(L, -2, i + 1);
84 int LuaVoxelManip::l_set_data(lua_State *L)
88 LuaVoxelManip *o = checkobject(L, 1);
91 if (!lua_istable(L, 2))
94 u32 volume = vm->m_area.getVolume();
95 for (u32 i = 0; i != volume; i++) {
96 lua_rawgeti(L, 2, i + 1);
97 content_t c = lua_tointeger(L, -1);
99 vm->m_data[i].setContent(c);
107 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;
123 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
125 LuaVoxelManip *o = checkobject(L, 1);
126 v3s16 pos = check_v3s16(L, 2);
128 pushnode(L, o->vm->getNodeNoExNoEmerge(pos), ndef);
132 int LuaVoxelManip::l_set_node_at(lua_State *L)
134 NO_MAP_LOCK_REQUIRED;
136 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
138 LuaVoxelManip *o = checkobject(L, 1);
139 v3s16 pos = check_v3s16(L, 2);
140 MapNode n = readnode(L, 3, ndef);
142 o->vm->setNodeNoEmerge(pos, n);
147 int LuaVoxelManip::l_update_liquids(lua_State *L)
151 LuaVoxelManip *o = checkobject(L, 1);
153 Map *map = &(env->getMap());
154 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
155 MMVManip *vm = o->vm;
161 mg.updateLiquid(&map->m_transforming_liquid,
162 vm->m_area.MinEdge, vm->m_area.MaxEdge);
167 int LuaVoxelManip::l_calc_lighting(lua_State *L)
169 NO_MAP_LOCK_REQUIRED;
171 LuaVoxelManip *o = checkobject(L, 1);
172 if (!o->is_mapgen_vm)
175 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
176 EmergeManager *emerge = getServer(L)->getEmergeManager();
177 MMVManip *vm = o->vm;
179 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
180 v3s16 fpmin = vm->m_area.MinEdge;
181 v3s16 fpmax = vm->m_area.MaxEdge;
182 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) : fpmin + yblock;
183 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) : fpmax - yblock;
184 bool propagate_shadow = lua_isboolean(L, 4) ? lua_toboolean(L, 4) : true;
186 sortBoxVerticies(pmin, pmax);
187 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
188 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
193 mg.water_level = emerge->mgparams->water_level;
195 mg.calcLighting(pmin, pmax, fpmin, fpmax, propagate_shadow);
200 int LuaVoxelManip::l_set_lighting(lua_State *L)
202 NO_MAP_LOCK_REQUIRED;
204 LuaVoxelManip *o = checkobject(L, 1);
205 if (!o->is_mapgen_vm)
208 if (!lua_istable(L, 2))
212 light = (getintfield_default(L, 2, "day", 0) & 0x0F);
213 light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
215 MMVManip *vm = o->vm;
217 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
218 v3s16 pmin = lua_istable(L, 3) ? check_v3s16(L, 3) : vm->m_area.MinEdge + yblock;
219 v3s16 pmax = lua_istable(L, 4) ? check_v3s16(L, 4) : vm->m_area.MaxEdge - yblock;
221 sortBoxVerticies(pmin, pmax);
222 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
223 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
228 mg.setLighting(light, pmin, pmax);
233 int LuaVoxelManip::l_get_light_data(lua_State *L)
235 NO_MAP_LOCK_REQUIRED;
237 LuaVoxelManip *o = checkobject(L, 1);
238 MMVManip *vm = o->vm;
240 u32 volume = vm->m_area.getVolume();
243 for (u32 i = 0; i != volume; i++) {
244 lua_Integer light = vm->m_data[i].param1;
245 lua_pushinteger(L, light);
246 lua_rawseti(L, -2, i + 1);
252 int LuaVoxelManip::l_set_light_data(lua_State *L)
254 NO_MAP_LOCK_REQUIRED;
256 LuaVoxelManip *o = checkobject(L, 1);
257 MMVManip *vm = o->vm;
259 if (!lua_istable(L, 2))
262 u32 volume = vm->m_area.getVolume();
263 for (u32 i = 0; i != volume; i++) {
264 lua_rawgeti(L, 2, i + 1);
265 u8 light = lua_tointeger(L, -1);
267 vm->m_data[i].param1 = light;
275 int LuaVoxelManip::l_get_param2_data(lua_State *L)
277 NO_MAP_LOCK_REQUIRED;
279 LuaVoxelManip *o = checkobject(L, 1);
280 bool use_buffer = lua_istable(L, 2);
282 MMVManip *vm = o->vm;
284 u32 volume = vm->m_area.getVolume();
291 for (u32 i = 0; i != volume; i++) {
292 lua_Integer param2 = vm->m_data[i].param2;
293 lua_pushinteger(L, param2);
294 lua_rawseti(L, -2, i + 1);
300 int LuaVoxelManip::l_set_param2_data(lua_State *L)
302 NO_MAP_LOCK_REQUIRED;
304 LuaVoxelManip *o = checkobject(L, 1);
305 MMVManip *vm = o->vm;
307 if (!lua_istable(L, 2))
310 u32 volume = vm->m_area.getVolume();
311 for (u32 i = 0; i != volume; i++) {
312 lua_rawgeti(L, 2, i + 1);
313 u8 param2 = lua_tointeger(L, -1);
315 vm->m_data[i].param2 = param2;
323 int LuaVoxelManip::l_update_map(lua_State *L)
327 LuaVoxelManip *o = checkobject(L, 1);
331 Map *map = &(env->getMap());
333 // TODO: Optimize this by using Mapgen::calcLighting() instead
334 std::map<v3s16, MapBlock *> lighting_mblocks;
335 std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
337 lighting_mblocks.insert(mblocks->begin(), mblocks->end());
339 map->updateLighting(lighting_mblocks, *mblocks);
342 event.type = MEET_OTHER;
343 for (std::map<v3s16, MapBlock *>::iterator
344 it = mblocks->begin();
345 it != mblocks->end(); ++it)
346 event.modified_blocks.insert(it->first);
348 map->dispatchEvent(&event);
355 int LuaVoxelManip::l_was_modified(lua_State *L)
357 NO_MAP_LOCK_REQUIRED;
359 LuaVoxelManip *o = checkobject(L, 1);
360 MMVManip *vm = o->vm;
362 lua_pushboolean(L, vm->m_is_dirty);
367 int LuaVoxelManip::l_get_emerged_area(lua_State *L)
369 NO_MAP_LOCK_REQUIRED;
371 LuaVoxelManip *o = checkobject(L, 1);
373 push_v3s16(L, o->vm->m_area.MinEdge);
374 push_v3s16(L, o->vm->m_area.MaxEdge);
379 LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm)
382 this->is_mapgen_vm = is_mg_vm;
385 LuaVoxelManip::LuaVoxelManip(Map *map)
387 this->vm = new MMVManip(map);
388 this->is_mapgen_vm = false;
391 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
393 this->vm = new MMVManip(map);
394 this->is_mapgen_vm = false;
396 v3s16 bp1 = getNodeBlockPos(p1);
397 v3s16 bp2 = getNodeBlockPos(p2);
398 sortBoxVerticies(bp1, bp2);
399 vm->initialEmerge(bp1, bp2);
402 LuaVoxelManip::~LuaVoxelManip()
409 // Creates an LuaVoxelManip and leaves it on top of stack
410 int LuaVoxelManip::create_object(lua_State *L)
414 Map *map = &(env->getMap());
415 LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
416 new LuaVoxelManip(map, check_v3s16(L, 1), check_v3s16(L, 2)) :
417 new LuaVoxelManip(map);
419 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
420 luaL_getmetatable(L, className);
421 lua_setmetatable(L, -2);
425 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
427 NO_MAP_LOCK_REQUIRED;
429 luaL_checktype(L, narg, LUA_TUSERDATA);
431 void *ud = luaL_checkudata(L, narg, className);
433 luaL_typerror(L, narg, className);
435 return *(LuaVoxelManip **)ud; // unbox pointer
438 void LuaVoxelManip::Register(lua_State *L)
441 int methodtable = lua_gettop(L);
442 luaL_newmetatable(L, className);
443 int metatable = lua_gettop(L);
445 lua_pushliteral(L, "__metatable");
446 lua_pushvalue(L, methodtable);
447 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
449 lua_pushliteral(L, "__index");
450 lua_pushvalue(L, methodtable);
451 lua_settable(L, metatable);
453 lua_pushliteral(L, "__gc");
454 lua_pushcfunction(L, gc_object);
455 lua_settable(L, metatable);
457 lua_pop(L, 1); // drop metatable
459 luaL_openlib(L, 0, methods, 0); // fill methodtable
460 lua_pop(L, 1); // drop methodtable
462 // Can be created from Lua (VoxelManip())
463 lua_register(L, className, create_object);
466 const char LuaVoxelManip::className[] = "VoxelManip";
467 const luaL_reg LuaVoxelManip::methods[] = {
468 luamethod(LuaVoxelManip, read_from_map),
469 luamethod(LuaVoxelManip, get_data),
470 luamethod(LuaVoxelManip, set_data),
471 luamethod(LuaVoxelManip, get_node_at),
472 luamethod(LuaVoxelManip, set_node_at),
473 luamethod(LuaVoxelManip, write_to_map),
474 luamethod(LuaVoxelManip, update_map),
475 luamethod(LuaVoxelManip, update_liquids),
476 luamethod(LuaVoxelManip, calc_lighting),
477 luamethod(LuaVoxelManip, set_lighting),
478 luamethod(LuaVoxelManip, get_light_data),
479 luamethod(LuaVoxelManip, set_light_data),
480 luamethod(LuaVoxelManip, get_param2_data),
481 luamethod(LuaVoxelManip, set_param2_data),
482 luamethod(LuaVoxelManip, was_modified),
483 luamethod(LuaVoxelManip, get_emerged_area),