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;
185 sortBoxVerticies(pmin, pmax);
186 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
187 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
192 mg.water_level = emerge->params.water_level;
194 mg.calcLighting(pmin, pmax, fpmin, fpmax);
199 int LuaVoxelManip::l_set_lighting(lua_State *L)
201 NO_MAP_LOCK_REQUIRED;
203 LuaVoxelManip *o = checkobject(L, 1);
204 if (!o->is_mapgen_vm)
207 if (!lua_istable(L, 2))
211 light = (getintfield_default(L, 2, "day", 0) & 0x0F);
212 light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
214 MMVManip *vm = o->vm;
216 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
217 v3s16 pmin = lua_istable(L, 3) ? check_v3s16(L, 3) : vm->m_area.MinEdge + yblock;
218 v3s16 pmax = lua_istable(L, 4) ? check_v3s16(L, 4) : vm->m_area.MaxEdge - yblock;
220 sortBoxVerticies(pmin, pmax);
221 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
222 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
227 mg.setLighting(light, pmin, pmax);
232 int LuaVoxelManip::l_get_light_data(lua_State *L)
234 NO_MAP_LOCK_REQUIRED;
236 LuaVoxelManip *o = checkobject(L, 1);
237 MMVManip *vm = o->vm;
239 u32 volume = vm->m_area.getVolume();
242 for (u32 i = 0; i != volume; i++) {
243 lua_Integer light = vm->m_data[i].param1;
244 lua_pushinteger(L, light);
245 lua_rawseti(L, -2, i + 1);
251 int LuaVoxelManip::l_set_light_data(lua_State *L)
253 NO_MAP_LOCK_REQUIRED;
255 LuaVoxelManip *o = checkobject(L, 1);
256 MMVManip *vm = o->vm;
258 if (!lua_istable(L, 2))
261 u32 volume = vm->m_area.getVolume();
262 for (u32 i = 0; i != volume; i++) {
263 lua_rawgeti(L, 2, i + 1);
264 u8 light = lua_tointeger(L, -1);
266 vm->m_data[i].param1 = light;
274 int LuaVoxelManip::l_get_param2_data(lua_State *L)
276 NO_MAP_LOCK_REQUIRED;
278 LuaVoxelManip *o = checkobject(L, 1);
279 MMVManip *vm = o->vm;
281 u32 volume = vm->m_area.getVolume();
284 for (u32 i = 0; i != volume; i++) {
285 lua_Integer param2 = vm->m_data[i].param2;
286 lua_pushinteger(L, param2);
287 lua_rawseti(L, -2, i + 1);
293 int LuaVoxelManip::l_set_param2_data(lua_State *L)
295 NO_MAP_LOCK_REQUIRED;
297 LuaVoxelManip *o = checkobject(L, 1);
298 MMVManip *vm = o->vm;
300 if (!lua_istable(L, 2))
303 u32 volume = vm->m_area.getVolume();
304 for (u32 i = 0; i != volume; i++) {
305 lua_rawgeti(L, 2, i + 1);
306 u8 param2 = lua_tointeger(L, -1);
308 vm->m_data[i].param2 = param2;
316 int LuaVoxelManip::l_update_map(lua_State *L)
320 LuaVoxelManip *o = checkobject(L, 1);
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 NO_MAP_LOCK_REQUIRED;
364 LuaVoxelManip *o = checkobject(L, 1);
366 push_v3s16(L, o->vm->m_area.MinEdge);
367 push_v3s16(L, o->vm->m_area.MaxEdge);
372 LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm)
375 this->is_mapgen_vm = is_mg_vm;
378 LuaVoxelManip::LuaVoxelManip(Map *map)
380 this->vm = new MMVManip(map);
381 this->is_mapgen_vm = false;
384 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
386 this->vm = new MMVManip(map);
387 this->is_mapgen_vm = false;
389 v3s16 bp1 = getNodeBlockPos(p1);
390 v3s16 bp2 = getNodeBlockPos(p2);
391 sortBoxVerticies(bp1, bp2);
392 vm->initialEmerge(bp1, bp2);
395 LuaVoxelManip::~LuaVoxelManip()
402 // Creates an LuaVoxelManip and leaves it on top of stack
403 int LuaVoxelManip::create_object(lua_State *L)
407 Map *map = &(env->getMap());
408 LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
409 new LuaVoxelManip(map, check_v3s16(L, 1), check_v3s16(L, 2)) :
410 new LuaVoxelManip(map);
412 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
413 luaL_getmetatable(L, className);
414 lua_setmetatable(L, -2);
418 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
420 NO_MAP_LOCK_REQUIRED;
422 luaL_checktype(L, narg, LUA_TUSERDATA);
424 void *ud = luaL_checkudata(L, narg, className);
426 luaL_typerror(L, narg, className);
428 return *(LuaVoxelManip **)ud; // unbox pointer
431 void LuaVoxelManip::Register(lua_State *L)
434 int methodtable = lua_gettop(L);
435 luaL_newmetatable(L, className);
436 int metatable = lua_gettop(L);
438 lua_pushliteral(L, "__metatable");
439 lua_pushvalue(L, methodtable);
440 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
442 lua_pushliteral(L, "__index");
443 lua_pushvalue(L, methodtable);
444 lua_settable(L, metatable);
446 lua_pushliteral(L, "__gc");
447 lua_pushcfunction(L, gc_object);
448 lua_settable(L, metatable);
450 lua_pop(L, 1); // drop metatable
452 luaL_openlib(L, 0, methods, 0); // fill methodtable
453 lua_pop(L, 1); // drop methodtable
455 // Can be created from Lua (VoxelManip())
456 lua_register(L, className, create_object);
459 const char LuaVoxelManip::className[] = "VoxelManip";
460 const luaL_reg LuaVoxelManip::methods[] = {
461 luamethod(LuaVoxelManip, read_from_map),
462 luamethod(LuaVoxelManip, get_data),
463 luamethod(LuaVoxelManip, set_data),
464 luamethod(LuaVoxelManip, get_node_at),
465 luamethod(LuaVoxelManip, set_node_at),
466 luamethod(LuaVoxelManip, write_to_map),
467 luamethod(LuaVoxelManip, update_map),
468 luamethod(LuaVoxelManip, update_liquids),
469 luamethod(LuaVoxelManip, calc_lighting),
470 luamethod(LuaVoxelManip, set_lighting),
471 luamethod(LuaVoxelManip, get_light_data),
472 luamethod(LuaVoxelManip, set_light_data),
473 luamethod(LuaVoxelManip, get_param2_data),
474 luamethod(LuaVoxelManip, set_param2_data),
475 luamethod(LuaVoxelManip, was_modified),
476 luamethod(LuaVoxelManip, get_emerged_area),