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);
47 ManualMapVoxelManipulator *vm = o->vm;
49 v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
50 v3s16 bp2 = getNodeBlockPos(read_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 ManualMapVoxelManipulator *vm = o->vm;
68 int volume = vm->m_area.getVolume();
71 for (int i = 0; i != volume; i++) {
72 lua_Integer cid = vm->m_data[i].getContent();
73 lua_pushinteger(L, cid);
74 lua_rawseti(L, -2, i + 1);
80 int LuaVoxelManip::l_set_data(lua_State *L)
84 LuaVoxelManip *o = checkobject(L, 1);
85 ManualMapVoxelManipulator *vm = o->vm;
87 if (!lua_istable(L, 2))
90 int volume = vm->m_area.getVolume();
91 for (int i = 0; i != volume; i++) {
92 lua_rawgeti(L, 2, i + 1);
93 content_t c = lua_tointeger(L, -1);
95 vm->m_data[i].setContent(c);
103 int LuaVoxelManip::l_write_to_map(lua_State *L)
105 LuaVoxelManip *o = checkobject(L, 1);
106 ManualMapVoxelManipulator *vm = o->vm;
108 vm->blitBackAll(&o->modified_blocks);
113 int LuaVoxelManip::l_get_node_at(lua_State *L)
115 NO_MAP_LOCK_REQUIRED;
118 LuaVoxelManip *o = checkobject(L, 1);
119 v3s16 pos = read_v3s16(L, 2);
121 pushnode(L, o->vm->getNodeNoExNoEmerge(pos), env->getGameDef()->ndef());
125 int LuaVoxelManip::l_set_node_at(lua_State *L)
127 NO_MAP_LOCK_REQUIRED;
130 LuaVoxelManip *o = checkobject(L, 1);
131 v3s16 pos = read_v3s16(L, 2);
132 MapNode n = readnode(L, 3, env->getGameDef()->ndef());
134 o->vm->setNodeNoEmerge(pos, n);
139 int LuaVoxelManip::l_update_liquids(lua_State *L)
143 LuaVoxelManip *o = checkobject(L, 1);
145 Map *map = &(env->getMap());
146 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
147 ManualMapVoxelManipulator *vm = o->vm;
153 mg.updateLiquid(&map->m_transforming_liquid,
154 vm->m_area.MinEdge, vm->m_area.MaxEdge);
159 int LuaVoxelManip::l_calc_lighting(lua_State *L)
161 NO_MAP_LOCK_REQUIRED;
163 LuaVoxelManip *o = checkobject(L, 1);
164 if (!o->is_mapgen_vm)
167 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
168 EmergeManager *emerge = getServer(L)->getEmergeManager();
169 ManualMapVoxelManipulator *vm = o->vm;
171 v3s16 p1 = lua_istable(L, 2) ? read_v3s16(L, 2) : vm->m_area.MinEdge;
172 v3s16 p2 = lua_istable(L, 3) ? read_v3s16(L, 3) : vm->m_area.MaxEdge;
173 sortBoxVerticies(p1, p2);
174 if (!vm->m_area.contains(VoxelArea(p1, p2)))
175 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
180 mg.water_level = emerge->params.water_level;
182 // Mapgen::calcLighting assumes the coordinates of
183 // the central chunk; correct for this
185 p1 + v3s16(1, 1, 1) * MAP_BLOCKSIZE,
186 p2 - v3s16(1, 1, 1) * MAP_BLOCKSIZE);
191 int LuaVoxelManip::l_set_lighting(lua_State *L)
193 NO_MAP_LOCK_REQUIRED;
195 LuaVoxelManip *o = checkobject(L, 1);
196 if (!o->is_mapgen_vm)
199 if (!lua_istable(L, 2))
203 light = (getintfield_default(L, 2, "day", 0) & 0x0F);
204 light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
206 ManualMapVoxelManipulator *vm = o->vm;
208 v3s16 p1 = lua_istable(L, 3) ? read_v3s16(L, 3) : vm->m_area.MinEdge;
209 v3s16 p2 = lua_istable(L, 4) ? read_v3s16(L, 4) : vm->m_area.MaxEdge;
210 sortBoxVerticies(p1, p2);
211 if (!vm->m_area.contains(VoxelArea(p1, p2)))
212 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
217 mg.setLighting(p1, p2, light);
222 int LuaVoxelManip::l_get_light_data(lua_State *L)
224 NO_MAP_LOCK_REQUIRED;
226 LuaVoxelManip *o = checkobject(L, 1);
227 ManualMapVoxelManipulator *vm = o->vm;
229 int volume = vm->m_area.getVolume();
232 for (int i = 0; i != volume; i++) {
233 lua_Integer light = vm->m_data[i].param1;
234 lua_pushinteger(L, light);
235 lua_rawseti(L, -2, i + 1);
241 int LuaVoxelManip::l_set_light_data(lua_State *L)
243 NO_MAP_LOCK_REQUIRED;
245 LuaVoxelManip *o = checkobject(L, 1);
246 ManualMapVoxelManipulator *vm = o->vm;
248 if (!lua_istable(L, 2))
251 int volume = vm->m_area.getVolume();
252 for (int i = 0; i != volume; i++) {
253 lua_rawgeti(L, 2, i + 1);
254 u8 light = lua_tointeger(L, -1);
256 vm->m_data[i].param1 = light;
264 int LuaVoxelManip::l_get_param2_data(lua_State *L)
266 NO_MAP_LOCK_REQUIRED;
268 LuaVoxelManip *o = checkobject(L, 1);
269 ManualMapVoxelManipulator *vm = o->vm;
271 int volume = vm->m_area.getVolume();
274 for (int i = 0; i != volume; i++) {
275 lua_Integer param2 = vm->m_data[i].param2;
276 lua_pushinteger(L, param2);
277 lua_rawseti(L, -2, i + 1);
283 int LuaVoxelManip::l_set_param2_data(lua_State *L)
285 NO_MAP_LOCK_REQUIRED;
287 LuaVoxelManip *o = checkobject(L, 1);
288 ManualMapVoxelManipulator *vm = o->vm;
290 if (!lua_istable(L, 2))
293 int volume = vm->m_area.getVolume();
294 for (int i = 0; i != volume; i++) {
295 lua_rawgeti(L, 2, i + 1);
296 u8 param2 = lua_tointeger(L, -1);
298 vm->m_data[i].param2 = param2;
306 int LuaVoxelManip::l_update_map(lua_State *L)
308 LuaVoxelManip *o = checkobject(L, 1);
312 Environment *env = getEnv(L);
316 Map *map = &(env->getMap());
318 // TODO: Optimize this by using Mapgen::calcLighting() instead
319 std::map<v3s16, MapBlock *> lighting_mblocks;
320 std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
322 lighting_mblocks.insert(mblocks->begin(), mblocks->end());
324 map->updateLighting(lighting_mblocks, *mblocks);
327 event.type = MEET_OTHER;
328 for (std::map<v3s16, MapBlock *>::iterator
329 it = mblocks->begin();
330 it != mblocks->end(); ++it)
331 event.modified_blocks.insert(it->first);
333 map->dispatchEvent(&event);
340 int LuaVoxelManip::l_was_modified(lua_State *L)
342 NO_MAP_LOCK_REQUIRED;
344 LuaVoxelManip *o = checkobject(L, 1);
345 ManualMapVoxelManipulator *vm = o->vm;
347 lua_pushboolean(L, vm->m_is_dirty);
352 int LuaVoxelManip::l_get_emerged_area(lua_State *L)
354 LuaVoxelManip *o = checkobject(L, 1);
356 push_v3s16(L, o->vm->m_area.MinEdge);
357 push_v3s16(L, o->vm->m_area.MaxEdge);
362 LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
365 this->is_mapgen_vm = is_mg_vm;
368 LuaVoxelManip::LuaVoxelManip(Map *map)
370 this->vm = new ManualMapVoxelManipulator(map);
371 this->is_mapgen_vm = false;
374 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
376 this->vm = new ManualMapVoxelManipulator(map);
377 this->is_mapgen_vm = false;
379 v3s16 bp1 = getNodeBlockPos(p1);
380 v3s16 bp2 = getNodeBlockPos(p2);
381 sortBoxVerticies(bp1, bp2);
382 vm->initialEmerge(bp1, bp2);
385 LuaVoxelManip::~LuaVoxelManip()
392 // Creates an LuaVoxelManip and leaves it on top of stack
393 int LuaVoxelManip::create_object(lua_State *L)
395 NO_MAP_LOCK_REQUIRED;
397 Environment *env = getEnv(L);
401 Map *map = &(env->getMap());
402 LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
403 new LuaVoxelManip(map, read_v3s16(L, 1), read_v3s16(L, 2)) :
404 new LuaVoxelManip(map);
406 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
407 luaL_getmetatable(L, className);
408 lua_setmetatable(L, -2);
412 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
414 NO_MAP_LOCK_REQUIRED;
416 luaL_checktype(L, narg, LUA_TUSERDATA);
418 void *ud = luaL_checkudata(L, narg, className);
420 luaL_typerror(L, narg, className);
422 return *(LuaVoxelManip **)ud; // unbox pointer
425 void LuaVoxelManip::Register(lua_State *L)
428 int methodtable = lua_gettop(L);
429 luaL_newmetatable(L, className);
430 int metatable = lua_gettop(L);
432 lua_pushliteral(L, "__metatable");
433 lua_pushvalue(L, methodtable);
434 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
436 lua_pushliteral(L, "__index");
437 lua_pushvalue(L, methodtable);
438 lua_settable(L, metatable);
440 lua_pushliteral(L, "__gc");
441 lua_pushcfunction(L, gc_object);
442 lua_settable(L, metatable);
444 lua_pop(L, 1); // drop metatable
446 luaL_openlib(L, 0, methods, 0); // fill methodtable
447 lua_pop(L, 1); // drop methodtable
449 // Can be created from Lua (VoxelManip())
450 lua_register(L, className, create_object);
453 const char LuaVoxelManip::className[] = "VoxelManip";
454 const luaL_reg LuaVoxelManip::methods[] = {
455 luamethod(LuaVoxelManip, read_from_map),
456 luamethod(LuaVoxelManip, get_data),
457 luamethod(LuaVoxelManip, set_data),
458 luamethod(LuaVoxelManip, get_node_at),
459 luamethod(LuaVoxelManip, set_node_at),
460 luamethod(LuaVoxelManip, write_to_map),
461 luamethod(LuaVoxelManip, update_map),
462 luamethod(LuaVoxelManip, update_liquids),
463 luamethod(LuaVoxelManip, calc_lighting),
464 luamethod(LuaVoxelManip, set_lighting),
465 luamethod(LuaVoxelManip, get_light_data),
466 luamethod(LuaVoxelManip, set_light_data),
467 luamethod(LuaVoxelManip, get_param2_data),
468 luamethod(LuaVoxelManip, set_param2_data),
469 luamethod(LuaVoxelManip, was_modified),
470 luamethod(LuaVoxelManip, get_emerged_area),