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 "mapgen/mapgen.h"
31 #include "voxelalgorithms.h"
34 int LuaVoxelManip::gc_object(lua_State *L)
36 LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
42 int LuaVoxelManip::l_read_from_map(lua_State *L)
46 LuaVoxelManip *o = checkobject(L, 1);
49 v3s16 bp1 = getNodeBlockPos(check_v3s16(L, 2));
50 v3s16 bp2 = getNodeBlockPos(check_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 bool use_buffer = lua_istable(L, 2);
70 u32 volume = vm->m_area.getVolume();
77 for (u32 i = 0; i != volume; i++) {
78 lua_Integer cid = vm->m_data[i].getContent();
79 lua_pushinteger(L, cid);
80 lua_rawseti(L, -2, i + 1);
86 int LuaVoxelManip::l_set_data(lua_State *L)
90 LuaVoxelManip *o = checkobject(L, 1);
93 if (!lua_istable(L, 2))
94 throw LuaError("VoxelManip:set_data called with missing parameter");
96 u32 volume = vm->m_area.getVolume();
97 for (u32 i = 0; i != volume; i++) {
98 lua_rawgeti(L, 2, i + 1);
99 content_t c = lua_tointeger(L, -1);
101 vm->m_data[i].setContent(c);
109 int LuaVoxelManip::l_write_to_map(lua_State *L)
113 LuaVoxelManip *o = checkobject(L, 1);
114 bool update_light = !lua_isboolean(L, 2) || readParam<bool>(L, 2);
116 ServerMap *map = &(env->getServerMap());
117 if (o->is_mapgen_vm || !update_light) {
118 o->vm->blitBackAll(&(o->modified_blocks));
120 voxalgo::blit_back_with_light(map, o->vm,
121 &(o->modified_blocks));
125 event.type = MEET_OTHER;
126 for (const auto &modified_block : o->modified_blocks)
127 event.modified_blocks.insert(modified_block.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 const NodeDefManager *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 const NodeDefManager *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 const NodeDefManager *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) {
189 warningstream << "VoxelManip:calc_lighting called for a non-mapgen "
190 "VoxelManip object" << std::endl;
194 const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
195 EmergeManager *emerge = getServer(L)->getEmergeManager();
196 MMVManip *vm = o->vm;
198 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
199 v3s16 fpmin = vm->m_area.MinEdge;
200 v3s16 fpmax = vm->m_area.MaxEdge;
201 v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) : fpmin + yblock;
202 v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) : fpmax - yblock;
203 bool propagate_shadow = !lua_isboolean(L, 4) || readParam<bool>(L, 4);
205 sortBoxVerticies(pmin, pmax);
206 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
207 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
212 mg.water_level = emerge->mgparams->water_level;
214 mg.calcLighting(pmin, pmax, fpmin, fpmax, propagate_shadow);
219 int LuaVoxelManip::l_set_lighting(lua_State *L)
221 NO_MAP_LOCK_REQUIRED;
223 LuaVoxelManip *o = checkobject(L, 1);
224 if (!o->is_mapgen_vm) {
225 warningstream << "VoxelManip:set_lighting called for a non-mapgen "
226 "VoxelManip object" << std::endl;
230 if (!lua_istable(L, 2))
231 throw LuaError("VoxelManip:set_lighting called with missing parameter");
234 light = (getintfield_default(L, 2, "day", 0) & 0x0F);
235 light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
237 MMVManip *vm = o->vm;
239 v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
240 v3s16 pmin = lua_istable(L, 3) ? check_v3s16(L, 3) : vm->m_area.MinEdge + yblock;
241 v3s16 pmax = lua_istable(L, 4) ? check_v3s16(L, 4) : vm->m_area.MaxEdge - yblock;
243 sortBoxVerticies(pmin, pmax);
244 if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
245 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
250 mg.setLighting(light, pmin, pmax);
255 int LuaVoxelManip::l_get_light_data(lua_State *L)
257 NO_MAP_LOCK_REQUIRED;
259 LuaVoxelManip *o = checkobject(L, 1);
260 MMVManip *vm = o->vm;
262 u32 volume = vm->m_area.getVolume();
265 for (u32 i = 0; i != volume; i++) {
266 lua_Integer light = vm->m_data[i].param1;
267 lua_pushinteger(L, light);
268 lua_rawseti(L, -2, i + 1);
274 int LuaVoxelManip::l_set_light_data(lua_State *L)
276 NO_MAP_LOCK_REQUIRED;
278 LuaVoxelManip *o = checkobject(L, 1);
279 MMVManip *vm = o->vm;
281 if (!lua_istable(L, 2))
282 throw LuaError("VoxelManip:set_light_data called with missing "
285 u32 volume = vm->m_area.getVolume();
286 for (u32 i = 0; i != volume; i++) {
287 lua_rawgeti(L, 2, i + 1);
288 u8 light = lua_tointeger(L, -1);
290 vm->m_data[i].param1 = light;
298 int LuaVoxelManip::l_get_param2_data(lua_State *L)
300 NO_MAP_LOCK_REQUIRED;
302 LuaVoxelManip *o = checkobject(L, 1);
303 bool use_buffer = lua_istable(L, 2);
305 MMVManip *vm = o->vm;
307 u32 volume = vm->m_area.getVolume();
314 for (u32 i = 0; i != volume; i++) {
315 lua_Integer param2 = vm->m_data[i].param2;
316 lua_pushinteger(L, param2);
317 lua_rawseti(L, -2, i + 1);
323 int LuaVoxelManip::l_set_param2_data(lua_State *L)
325 NO_MAP_LOCK_REQUIRED;
327 LuaVoxelManip *o = checkobject(L, 1);
328 MMVManip *vm = o->vm;
330 if (!lua_istable(L, 2))
331 throw LuaError("VoxelManip:set_param2_data called with missing "
334 u32 volume = vm->m_area.getVolume();
335 for (u32 i = 0; i != volume; i++) {
336 lua_rawgeti(L, 2, i + 1);
337 u8 param2 = lua_tointeger(L, -1);
339 vm->m_data[i].param2 = param2;
347 int LuaVoxelManip::l_update_map(lua_State *L)
352 int LuaVoxelManip::l_was_modified(lua_State *L)
354 NO_MAP_LOCK_REQUIRED;
356 LuaVoxelManip *o = checkobject(L, 1);
357 MMVManip *vm = o->vm;
359 lua_pushboolean(L, vm->m_is_dirty);
364 int LuaVoxelManip::l_get_emerged_area(lua_State *L)
366 NO_MAP_LOCK_REQUIRED;
368 LuaVoxelManip *o = checkobject(L, 1);
370 push_v3s16(L, o->vm->m_area.MinEdge);
371 push_v3s16(L, o->vm->m_area.MaxEdge);
376 LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm) :
377 is_mapgen_vm(is_mg_vm),
382 LuaVoxelManip::LuaVoxelManip(Map *map) : vm(new MMVManip(map))
386 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
388 vm = new MMVManip(map);
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),