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->params.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 MMVManip *vm = o->vm;
282 u32 volume = vm->m_area.getVolume();
285 for (u32 i = 0; i != volume; i++) {
286 lua_Integer param2 = vm->m_data[i].param2;
287 lua_pushinteger(L, param2);
288 lua_rawseti(L, -2, i + 1);
294 int LuaVoxelManip::l_set_param2_data(lua_State *L)
296 NO_MAP_LOCK_REQUIRED;
298 LuaVoxelManip *o = checkobject(L, 1);
299 MMVManip *vm = o->vm;
301 if (!lua_istable(L, 2))
304 u32 volume = vm->m_area.getVolume();
305 for (u32 i = 0; i != volume; i++) {
306 lua_rawgeti(L, 2, i + 1);
307 u8 param2 = lua_tointeger(L, -1);
309 vm->m_data[i].param2 = param2;
317 int LuaVoxelManip::l_update_map(lua_State *L)
321 LuaVoxelManip *o = checkobject(L, 1);
325 Map *map = &(env->getMap());
327 // TODO: Optimize this by using Mapgen::calcLighting() instead
328 std::map<v3s16, MapBlock *> lighting_mblocks;
329 std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
331 lighting_mblocks.insert(mblocks->begin(), mblocks->end());
333 map->updateLighting(lighting_mblocks, *mblocks);
336 event.type = MEET_OTHER;
337 for (std::map<v3s16, MapBlock *>::iterator
338 it = mblocks->begin();
339 it != mblocks->end(); ++it)
340 event.modified_blocks.insert(it->first);
342 map->dispatchEvent(&event);
349 int LuaVoxelManip::l_was_modified(lua_State *L)
351 NO_MAP_LOCK_REQUIRED;
353 LuaVoxelManip *o = checkobject(L, 1);
354 MMVManip *vm = o->vm;
356 lua_pushboolean(L, vm->m_is_dirty);
361 int LuaVoxelManip::l_get_emerged_area(lua_State *L)
363 NO_MAP_LOCK_REQUIRED;
365 LuaVoxelManip *o = checkobject(L, 1);
367 push_v3s16(L, o->vm->m_area.MinEdge);
368 push_v3s16(L, o->vm->m_area.MaxEdge);
373 LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm)
376 this->is_mapgen_vm = is_mg_vm;
379 LuaVoxelManip::LuaVoxelManip(Map *map)
381 this->vm = new MMVManip(map);
382 this->is_mapgen_vm = false;
385 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
387 this->vm = new MMVManip(map);
388 this->is_mapgen_vm = false;
390 v3s16 bp1 = getNodeBlockPos(p1);
391 v3s16 bp2 = getNodeBlockPos(p2);
392 sortBoxVerticies(bp1, bp2);
393 vm->initialEmerge(bp1, bp2);
396 LuaVoxelManip::~LuaVoxelManip()
403 // Creates an LuaVoxelManip and leaves it on top of stack
404 int LuaVoxelManip::create_object(lua_State *L)
408 Map *map = &(env->getMap());
409 LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
410 new LuaVoxelManip(map, check_v3s16(L, 1), check_v3s16(L, 2)) :
411 new LuaVoxelManip(map);
413 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
414 luaL_getmetatable(L, className);
415 lua_setmetatable(L, -2);
419 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
421 NO_MAP_LOCK_REQUIRED;
423 luaL_checktype(L, narg, LUA_TUSERDATA);
425 void *ud = luaL_checkudata(L, narg, className);
427 luaL_typerror(L, narg, className);
429 return *(LuaVoxelManip **)ud; // unbox pointer
432 void LuaVoxelManip::Register(lua_State *L)
435 int methodtable = lua_gettop(L);
436 luaL_newmetatable(L, className);
437 int metatable = lua_gettop(L);
439 lua_pushliteral(L, "__metatable");
440 lua_pushvalue(L, methodtable);
441 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
443 lua_pushliteral(L, "__index");
444 lua_pushvalue(L, methodtable);
445 lua_settable(L, metatable);
447 lua_pushliteral(L, "__gc");
448 lua_pushcfunction(L, gc_object);
449 lua_settable(L, metatable);
451 lua_pop(L, 1); // drop metatable
453 luaL_openlib(L, 0, methods, 0); // fill methodtable
454 lua_pop(L, 1); // drop methodtable
456 // Can be created from Lua (VoxelManip())
457 lua_register(L, className, create_object);
460 const char LuaVoxelManip::className[] = "VoxelManip";
461 const luaL_reg LuaVoxelManip::methods[] = {
462 luamethod(LuaVoxelManip, read_from_map),
463 luamethod(LuaVoxelManip, get_data),
464 luamethod(LuaVoxelManip, set_data),
465 luamethod(LuaVoxelManip, get_node_at),
466 luamethod(LuaVoxelManip, set_node_at),
467 luamethod(LuaVoxelManip, write_to_map),
468 luamethod(LuaVoxelManip, update_map),
469 luamethod(LuaVoxelManip, update_liquids),
470 luamethod(LuaVoxelManip, calc_lighting),
471 luamethod(LuaVoxelManip, set_lighting),
472 luamethod(LuaVoxelManip, get_light_data),
473 luamethod(LuaVoxelManip, set_light_data),
474 luamethod(LuaVoxelManip, get_param2_data),
475 luamethod(LuaVoxelManip, set_param2_data),
476 luamethod(LuaVoxelManip, was_modified),
477 luamethod(LuaVoxelManip, get_emerged_area),