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"
30 #include "voxelalgorithms.h"
33 int LuaVoxelManip::gc_object(lua_State *L)
35 LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
41 int LuaVoxelManip::l_read_from_map(lua_State *L)
45 LuaVoxelManip *o = checkobject(L, 1);
48 v3s16 bp1 = getNodeBlockPos(check_v3s16(L, 2));
49 v3s16 bp2 = getNodeBlockPos(check_v3s16(L, 3));
50 sortBoxVerticies(bp1, bp2);
52 vm->initialEmerge(bp1, bp2);
54 push_v3s16(L, vm->m_area.MinEdge);
55 push_v3s16(L, vm->m_area.MaxEdge);
60 int LuaVoxelManip::l_get_data(lua_State *L)
64 LuaVoxelManip *o = checkobject(L, 1);
65 bool use_buffer = lua_istable(L, 2);
69 u32 volume = vm->m_area.getVolume();
76 for (u32 i = 0; i != volume; i++) {
77 lua_Integer cid = vm->m_data[i].getContent();
78 lua_pushinteger(L, cid);
79 lua_rawseti(L, -2, i + 1);
85 int LuaVoxelManip::l_set_data(lua_State *L)
89 LuaVoxelManip *o = checkobject(L, 1);
92 if (!lua_istable(L, 2))
95 u32 volume = vm->m_area.getVolume();
96 for (u32 i = 0; i != volume; i++) {
97 lua_rawgeti(L, 2, i + 1);
98 content_t c = lua_tointeger(L, -1);
100 vm->m_data[i].setContent(c);
108 int LuaVoxelManip::l_write_to_map(lua_State *L)
112 LuaVoxelManip *o = checkobject(L, 1);
113 bool update_light = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : true;
115 ServerMap *map = &(env->getServerMap());
116 if (o->is_mapgen_vm || !update_light) {
117 o->vm->blitBackAll(&(o->modified_blocks));
119 voxalgo::blit_back_with_light(map, o->vm,
120 &(o->modified_blocks));
124 event.type = MEET_OTHER;
125 for (std::map<v3s16, MapBlock *>::iterator it = o->modified_blocks.begin();
126 it != o->modified_blocks.end(); ++it)
127 event.modified_blocks.insert(it->first);
129 map->dispatchEvent(&event);
131 o->modified_blocks.clear();
135 int LuaVoxelManip::l_get_node_at(lua_State *L)
137 NO_MAP_LOCK_REQUIRED;
139 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
141 LuaVoxelManip *o = checkobject(L, 1);
142 v3s16 pos = check_v3s16(L, 2);
144 pushnode(L, o->vm->getNodeNoExNoEmerge(pos), ndef);
148 int LuaVoxelManip::l_set_node_at(lua_State *L)
150 NO_MAP_LOCK_REQUIRED;
152 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
154 LuaVoxelManip *o = checkobject(L, 1);
155 v3s16 pos = check_v3s16(L, 2);
156 MapNode n = readnode(L, 3, ndef);
158 o->vm->setNodeNoEmerge(pos, n);
163 int LuaVoxelManip::l_update_liquids(lua_State *L)
167 LuaVoxelManip *o = checkobject(L, 1);
169 Map *map = &(env->getMap());
170 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
171 MMVManip *vm = o->vm;
177 mg.updateLiquid(&map->m_transforming_liquid,
178 vm->m_area.MinEdge, vm->m_area.MaxEdge);
183 int LuaVoxelManip::l_calc_lighting(lua_State *L)
185 NO_MAP_LOCK_REQUIRED;
187 LuaVoxelManip *o = checkobject(L, 1);
188 if (!o->is_mapgen_vm)
191 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
192 EmergeManager *emerge = getServer(L)->getEmergeManager();
193 MMVManip *vm = o->vm;
195 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
196 v3s16 fpmin = vm->m_area.MinEdge;
197 v3s16 fpmax = vm->m_area.MaxEdge;
198 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) : fpmin + yblock;
199 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) : fpmax - yblock;
200 bool propagate_shadow = lua_isboolean(L, 4) ? lua_toboolean(L, 4) : true;
202 sortBoxVerticies(pmin, pmax);
203 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
204 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
209 mg.water_level = emerge->mgparams->water_level;
211 mg.calcLighting(pmin, pmax, fpmin, fpmax, propagate_shadow);
216 int LuaVoxelManip::l_set_lighting(lua_State *L)
218 NO_MAP_LOCK_REQUIRED;
220 LuaVoxelManip *o = checkobject(L, 1);
221 if (!o->is_mapgen_vm)
224 if (!lua_istable(L, 2))
228 light = (getintfield_default(L, 2, "day", 0) & 0x0F);
229 light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
231 MMVManip *vm = o->vm;
233 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
234 v3s16 pmin = lua_istable(L, 3) ? check_v3s16(L, 3) : vm->m_area.MinEdge + yblock;
235 v3s16 pmax = lua_istable(L, 4) ? check_v3s16(L, 4) : vm->m_area.MaxEdge - yblock;
237 sortBoxVerticies(pmin, pmax);
238 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
239 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
244 mg.setLighting(light, pmin, pmax);
249 int LuaVoxelManip::l_get_light_data(lua_State *L)
251 NO_MAP_LOCK_REQUIRED;
253 LuaVoxelManip *o = checkobject(L, 1);
254 MMVManip *vm = o->vm;
256 u32 volume = vm->m_area.getVolume();
259 for (u32 i = 0; i != volume; i++) {
260 lua_Integer light = vm->m_data[i].param1;
261 lua_pushinteger(L, light);
262 lua_rawseti(L, -2, i + 1);
268 int LuaVoxelManip::l_set_light_data(lua_State *L)
270 NO_MAP_LOCK_REQUIRED;
272 LuaVoxelManip *o = checkobject(L, 1);
273 MMVManip *vm = o->vm;
275 if (!lua_istable(L, 2))
278 u32 volume = vm->m_area.getVolume();
279 for (u32 i = 0; i != volume; i++) {
280 lua_rawgeti(L, 2, i + 1);
281 u8 light = lua_tointeger(L, -1);
283 vm->m_data[i].param1 = light;
291 int LuaVoxelManip::l_get_param2_data(lua_State *L)
293 NO_MAP_LOCK_REQUIRED;
295 LuaVoxelManip *o = checkobject(L, 1);
296 bool use_buffer = lua_istable(L, 2);
298 MMVManip *vm = o->vm;
300 u32 volume = vm->m_area.getVolume();
307 for (u32 i = 0; i != volume; i++) {
308 lua_Integer param2 = vm->m_data[i].param2;
309 lua_pushinteger(L, param2);
310 lua_rawseti(L, -2, i + 1);
316 int LuaVoxelManip::l_set_param2_data(lua_State *L)
318 NO_MAP_LOCK_REQUIRED;
320 LuaVoxelManip *o = checkobject(L, 1);
321 MMVManip *vm = o->vm;
323 if (!lua_istable(L, 2))
326 u32 volume = vm->m_area.getVolume();
327 for (u32 i = 0; i != volume; i++) {
328 lua_rawgeti(L, 2, i + 1);
329 u8 param2 = lua_tointeger(L, -1);
331 vm->m_data[i].param2 = param2;
339 int LuaVoxelManip::l_update_map(lua_State *L)
344 int LuaVoxelManip::l_was_modified(lua_State *L)
346 NO_MAP_LOCK_REQUIRED;
348 LuaVoxelManip *o = checkobject(L, 1);
349 MMVManip *vm = o->vm;
351 lua_pushboolean(L, vm->m_is_dirty);
356 int LuaVoxelManip::l_get_emerged_area(lua_State *L)
358 NO_MAP_LOCK_REQUIRED;
360 LuaVoxelManip *o = checkobject(L, 1);
362 push_v3s16(L, o->vm->m_area.MinEdge);
363 push_v3s16(L, o->vm->m_area.MaxEdge);
368 LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm) : vm(mmvm), is_mapgen_vm(is_mg_vm)
372 LuaVoxelManip::LuaVoxelManip(Map *map) : vm(new MMVManip(map))
376 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
378 vm = new MMVManip(map);
380 v3s16 bp1 = getNodeBlockPos(p1);
381 v3s16 bp2 = getNodeBlockPos(p2);
382 sortBoxVerticies(bp1, bp2);
383 vm->initialEmerge(bp1, bp2);
386 LuaVoxelManip::~LuaVoxelManip()
393 // Creates an LuaVoxelManip and leaves it on top of stack
394 int LuaVoxelManip::create_object(lua_State *L)
398 Map *map = &(env->getMap());
399 LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
400 new LuaVoxelManip(map, check_v3s16(L, 1), check_v3s16(L, 2)) :
401 new LuaVoxelManip(map);
403 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
404 luaL_getmetatable(L, className);
405 lua_setmetatable(L, -2);
409 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
411 NO_MAP_LOCK_REQUIRED;
413 luaL_checktype(L, narg, LUA_TUSERDATA);
415 void *ud = luaL_checkudata(L, narg, className);
417 luaL_typerror(L, narg, className);
419 return *(LuaVoxelManip **)ud; // unbox pointer
422 void LuaVoxelManip::Register(lua_State *L)
425 int methodtable = lua_gettop(L);
426 luaL_newmetatable(L, className);
427 int metatable = lua_gettop(L);
429 lua_pushliteral(L, "__metatable");
430 lua_pushvalue(L, methodtable);
431 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
433 lua_pushliteral(L, "__index");
434 lua_pushvalue(L, methodtable);
435 lua_settable(L, metatable);
437 lua_pushliteral(L, "__gc");
438 lua_pushcfunction(L, gc_object);
439 lua_settable(L, metatable);
441 lua_pop(L, 1); // drop metatable
443 luaL_openlib(L, 0, methods, 0); // fill methodtable
444 lua_pop(L, 1); // drop methodtable
446 // Can be created from Lua (VoxelManip())
447 lua_register(L, className, create_object);
450 const char LuaVoxelManip::className[] = "VoxelManip";
451 const luaL_Reg LuaVoxelManip::methods[] = {
452 luamethod(LuaVoxelManip, read_from_map),
453 luamethod(LuaVoxelManip, get_data),
454 luamethod(LuaVoxelManip, set_data),
455 luamethod(LuaVoxelManip, get_node_at),
456 luamethod(LuaVoxelManip, set_node_at),
457 luamethod(LuaVoxelManip, write_to_map),
458 luamethod(LuaVoxelManip, update_map),
459 luamethod(LuaVoxelManip, update_liquids),
460 luamethod(LuaVoxelManip, calc_lighting),
461 luamethod(LuaVoxelManip, set_lighting),
462 luamethod(LuaVoxelManip, get_light_data),
463 luamethod(LuaVoxelManip, set_light_data),
464 luamethod(LuaVoxelManip, get_param2_data),
465 luamethod(LuaVoxelManip, set_param2_data),
466 luamethod(LuaVoxelManip, was_modified),
467 luamethod(LuaVoxelManip, get_emerged_area),