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);
114 ServerMap *map = &(env->getServerMap());
115 if (o->is_mapgen_vm) {
116 o->vm->blitBackAll(&(o->modified_blocks));
118 voxalgo::blit_back_with_light(map, o->vm,
119 &(o->modified_blocks));
123 event.type = MEET_OTHER;
124 for (std::map<v3s16, MapBlock *>::iterator it = o->modified_blocks.begin();
125 it != o->modified_blocks.end(); ++it)
126 event.modified_blocks.insert(it->first);
128 map->dispatchEvent(&event);
130 o->modified_blocks.clear();
134 int LuaVoxelManip::l_get_node_at(lua_State *L)
136 NO_MAP_LOCK_REQUIRED;
138 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
140 LuaVoxelManip *o = checkobject(L, 1);
141 v3s16 pos = check_v3s16(L, 2);
143 pushnode(L, o->vm->getNodeNoExNoEmerge(pos), ndef);
147 int LuaVoxelManip::l_set_node_at(lua_State *L)
149 NO_MAP_LOCK_REQUIRED;
151 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
153 LuaVoxelManip *o = checkobject(L, 1);
154 v3s16 pos = check_v3s16(L, 2);
155 MapNode n = readnode(L, 3, ndef);
157 o->vm->setNodeNoEmerge(pos, n);
162 int LuaVoxelManip::l_update_liquids(lua_State *L)
166 LuaVoxelManip *o = checkobject(L, 1);
168 Map *map = &(env->getMap());
169 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
170 MMVManip *vm = o->vm;
176 mg.updateLiquid(&map->m_transforming_liquid,
177 vm->m_area.MinEdge, vm->m_area.MaxEdge);
182 int LuaVoxelManip::l_calc_lighting(lua_State *L)
184 NO_MAP_LOCK_REQUIRED;
186 LuaVoxelManip *o = checkobject(L, 1);
187 if (!o->is_mapgen_vm)
190 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
191 EmergeManager *emerge = getServer(L)->getEmergeManager();
192 MMVManip *vm = o->vm;
194 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
195 v3s16 fpmin = vm->m_area.MinEdge;
196 v3s16 fpmax = vm->m_area.MaxEdge;
197 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) : fpmin + yblock;
198 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) : fpmax - yblock;
199 bool propagate_shadow = lua_isboolean(L, 4) ? lua_toboolean(L, 4) : true;
201 sortBoxVerticies(pmin, pmax);
202 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
203 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
208 mg.water_level = emerge->mgparams->water_level;
210 mg.calcLighting(pmin, pmax, fpmin, fpmax, propagate_shadow);
215 int LuaVoxelManip::l_set_lighting(lua_State *L)
217 NO_MAP_LOCK_REQUIRED;
219 LuaVoxelManip *o = checkobject(L, 1);
220 if (!o->is_mapgen_vm)
223 if (!lua_istable(L, 2))
227 light = (getintfield_default(L, 2, "day", 0) & 0x0F);
228 light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
230 MMVManip *vm = o->vm;
232 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
233 v3s16 pmin = lua_istable(L, 3) ? check_v3s16(L, 3) : vm->m_area.MinEdge + yblock;
234 v3s16 pmax = lua_istable(L, 4) ? check_v3s16(L, 4) : vm->m_area.MaxEdge - yblock;
236 sortBoxVerticies(pmin, pmax);
237 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
238 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
243 mg.setLighting(light, pmin, pmax);
248 int LuaVoxelManip::l_get_light_data(lua_State *L)
250 NO_MAP_LOCK_REQUIRED;
252 LuaVoxelManip *o = checkobject(L, 1);
253 MMVManip *vm = o->vm;
255 u32 volume = vm->m_area.getVolume();
258 for (u32 i = 0; i != volume; i++) {
259 lua_Integer light = vm->m_data[i].param1;
260 lua_pushinteger(L, light);
261 lua_rawseti(L, -2, i + 1);
267 int LuaVoxelManip::l_set_light_data(lua_State *L)
269 NO_MAP_LOCK_REQUIRED;
271 LuaVoxelManip *o = checkobject(L, 1);
272 MMVManip *vm = o->vm;
274 if (!lua_istable(L, 2))
277 u32 volume = vm->m_area.getVolume();
278 for (u32 i = 0; i != volume; i++) {
279 lua_rawgeti(L, 2, i + 1);
280 u8 light = lua_tointeger(L, -1);
282 vm->m_data[i].param1 = light;
290 int LuaVoxelManip::l_get_param2_data(lua_State *L)
292 NO_MAP_LOCK_REQUIRED;
294 LuaVoxelManip *o = checkobject(L, 1);
295 bool use_buffer = lua_istable(L, 2);
297 MMVManip *vm = o->vm;
299 u32 volume = vm->m_area.getVolume();
306 for (u32 i = 0; i != volume; i++) {
307 lua_Integer param2 = vm->m_data[i].param2;
308 lua_pushinteger(L, param2);
309 lua_rawseti(L, -2, i + 1);
315 int LuaVoxelManip::l_set_param2_data(lua_State *L)
317 NO_MAP_LOCK_REQUIRED;
319 LuaVoxelManip *o = checkobject(L, 1);
320 MMVManip *vm = o->vm;
322 if (!lua_istable(L, 2))
325 u32 volume = vm->m_area.getVolume();
326 for (u32 i = 0; i != volume; i++) {
327 lua_rawgeti(L, 2, i + 1);
328 u8 param2 = lua_tointeger(L, -1);
330 vm->m_data[i].param2 = param2;
338 int LuaVoxelManip::l_update_map(lua_State *L)
343 int LuaVoxelManip::l_was_modified(lua_State *L)
345 NO_MAP_LOCK_REQUIRED;
347 LuaVoxelManip *o = checkobject(L, 1);
348 MMVManip *vm = o->vm;
350 lua_pushboolean(L, vm->m_is_dirty);
355 int LuaVoxelManip::l_get_emerged_area(lua_State *L)
357 NO_MAP_LOCK_REQUIRED;
359 LuaVoxelManip *o = checkobject(L, 1);
361 push_v3s16(L, o->vm->m_area.MinEdge);
362 push_v3s16(L, o->vm->m_area.MaxEdge);
367 LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm)
370 this->is_mapgen_vm = is_mg_vm;
373 LuaVoxelManip::LuaVoxelManip(Map *map)
375 this->vm = new MMVManip(map);
376 this->is_mapgen_vm = false;
379 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
381 this->vm = new MMVManip(map);
382 this->is_mapgen_vm = false;
384 v3s16 bp1 = getNodeBlockPos(p1);
385 v3s16 bp2 = getNodeBlockPos(p2);
386 sortBoxVerticies(bp1, bp2);
387 vm->initialEmerge(bp1, bp2);
390 LuaVoxelManip::~LuaVoxelManip()
397 // Creates an LuaVoxelManip and leaves it on top of stack
398 int LuaVoxelManip::create_object(lua_State *L)
402 Map *map = &(env->getMap());
403 LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
404 new LuaVoxelManip(map, check_v3s16(L, 1), check_v3s16(L, 2)) :
405 new LuaVoxelManip(map);
407 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
408 luaL_getmetatable(L, className);
409 lua_setmetatable(L, -2);
413 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
415 NO_MAP_LOCK_REQUIRED;
417 luaL_checktype(L, narg, LUA_TUSERDATA);
419 void *ud = luaL_checkudata(L, narg, className);
421 luaL_typerror(L, narg, className);
423 return *(LuaVoxelManip **)ud; // unbox pointer
426 void LuaVoxelManip::Register(lua_State *L)
429 int methodtable = lua_gettop(L);
430 luaL_newmetatable(L, className);
431 int metatable = lua_gettop(L);
433 lua_pushliteral(L, "__metatable");
434 lua_pushvalue(L, methodtable);
435 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
437 lua_pushliteral(L, "__index");
438 lua_pushvalue(L, methodtable);
439 lua_settable(L, metatable);
441 lua_pushliteral(L, "__gc");
442 lua_pushcfunction(L, gc_object);
443 lua_settable(L, metatable);
445 lua_pop(L, 1); // drop metatable
447 luaL_openlib(L, 0, methods, 0); // fill methodtable
448 lua_pop(L, 1); // drop methodtable
450 // Can be created from Lua (VoxelManip())
451 lua_register(L, className, create_object);
454 const char LuaVoxelManip::className[] = "VoxelManip";
455 const luaL_reg LuaVoxelManip::methods[] = {
456 luamethod(LuaVoxelManip, read_from_map),
457 luamethod(LuaVoxelManip, get_data),
458 luamethod(LuaVoxelManip, set_data),
459 luamethod(LuaVoxelManip, get_node_at),
460 luamethod(LuaVoxelManip, set_node_at),
461 luamethod(LuaVoxelManip, write_to_map),
462 luamethod(LuaVoxelManip, update_map),
463 luamethod(LuaVoxelManip, update_liquids),
464 luamethod(LuaVoxelManip, calc_lighting),
465 luamethod(LuaVoxelManip, set_lighting),
466 luamethod(LuaVoxelManip, get_light_data),
467 luamethod(LuaVoxelManip, set_light_data),
468 luamethod(LuaVoxelManip, get_param2_data),
469 luamethod(LuaVoxelManip, set_param2_data),
470 luamethod(LuaVoxelManip, was_modified),
471 luamethod(LuaVoxelManip, get_emerged_area),