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_converter.h"
25 #include "environment.h"
31 int LuaVoxelManip::gc_object(lua_State *L)
33 LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
39 int LuaVoxelManip::l_read_from_map(lua_State *L)
41 LuaVoxelManip *o = checkobject(L, 1);
42 ManualMapVoxelManipulator *vm = o->vm;
44 v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
45 v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3));
46 sortBoxVerticies(bp1, bp2);
48 vm->initialEmerge(bp1, bp2);
50 push_v3s16(L, vm->m_area.MinEdge);
51 push_v3s16(L, vm->m_area.MaxEdge);
56 int LuaVoxelManip::l_get_data(lua_State *L)
60 LuaVoxelManip *o = checkobject(L, 1);
61 ManualMapVoxelManipulator *vm = o->vm;
63 int volume = vm->m_area.getVolume();
66 for (int i = 0; i != volume; i++) {
67 lua_Integer cid = vm->m_data[i].getContent();
68 lua_pushinteger(L, cid);
69 lua_rawseti(L, -2, i + 1);
75 int LuaVoxelManip::l_set_data(lua_State *L)
79 LuaVoxelManip *o = checkobject(L, 1);
80 ManualMapVoxelManipulator *vm = o->vm;
82 if (!lua_istable(L, 2))
85 int volume = vm->m_area.getVolume();
86 for (int i = 0; i != volume; i++) {
87 lua_rawgeti(L, 2, i + 1);
88 content_t c = lua_tointeger(L, -1);
90 vm->m_data[i].setContent(c);
98 int LuaVoxelManip::l_write_to_map(lua_State *L)
100 LuaVoxelManip *o = checkobject(L, 1);
101 ManualMapVoxelManipulator *vm = o->vm;
103 vm->blitBackAll(&o->modified_blocks);
108 int LuaVoxelManip::l_update_liquids(lua_State *L)
110 LuaVoxelManip *o = checkobject(L, 1);
112 Environment *env = getEnv(L);
116 Map *map = &(env->getMap());
117 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
118 ManualMapVoxelManipulator *vm = o->vm;
124 mg.updateLiquid(&map->m_transforming_liquid,
125 vm->m_area.MinEdge, vm->m_area.MaxEdge);
130 int LuaVoxelManip::l_calc_lighting(lua_State *L)
132 NO_MAP_LOCK_REQUIRED;
134 LuaVoxelManip *o = checkobject(L, 1);
135 if (!o->is_mapgen_vm)
138 INodeDefManager *ndef = getServer(L)->getNodeDefManager();
139 EmergeManager *emerge = getServer(L)->getEmergeManager();
140 ManualMapVoxelManipulator *vm = o->vm;
142 v3s16 p1 = lua_istable(L, 2) ? read_v3s16(L, 2) :
143 vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE;
144 v3s16 p2 = lua_istable(L, 3) ? read_v3s16(L, 3) :
145 vm->m_area.MaxEdge - v3s16(0, 1, 0) * MAP_BLOCKSIZE;
146 sortBoxVerticies(p1, p2);
151 mg.water_level = emerge->params.water_level;
153 mg.calcLighting(p1, p2);
158 int LuaVoxelManip::l_set_lighting(lua_State *L)
160 NO_MAP_LOCK_REQUIRED;
162 LuaVoxelManip *o = checkobject(L, 1);
163 if (!o->is_mapgen_vm)
166 if (!lua_istable(L, 2))
170 light = (getintfield_default(L, 2, "day", 0) & 0x0F);
171 light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
173 ManualMapVoxelManipulator *vm = o->vm;
175 v3s16 p1 = lua_istable(L, 3) ? read_v3s16(L, 3) :
176 vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE;
177 v3s16 p2 = lua_istable(L, 4) ? read_v3s16(L, 4) :
178 vm->m_area.MaxEdge - v3s16(0, 1, 0) * MAP_BLOCKSIZE;
179 sortBoxVerticies(p1, p2);
184 mg.setLighting(p1, p2, light);
189 int LuaVoxelManip::l_get_light_data(lua_State *L)
191 NO_MAP_LOCK_REQUIRED;
193 LuaVoxelManip *o = checkobject(L, 1);
194 ManualMapVoxelManipulator *vm = o->vm;
196 int volume = vm->m_area.getVolume();
199 for (int i = 0; i != volume; i++) {
200 lua_Integer light = vm->m_data[i].param1;
201 lua_pushinteger(L, light);
202 lua_rawseti(L, -2, i + 1);
208 int LuaVoxelManip::l_set_light_data(lua_State *L)
210 NO_MAP_LOCK_REQUIRED;
212 LuaVoxelManip *o = checkobject(L, 1);
213 ManualMapVoxelManipulator *vm = o->vm;
215 if (!lua_istable(L, 2))
218 int volume = vm->m_area.getVolume();
219 for (int i = 0; i != volume; i++) {
220 lua_rawgeti(L, 2, i + 1);
221 u8 light = lua_tointeger(L, -1);
223 vm->m_data[i].param1 = light;
231 int LuaVoxelManip::l_get_param2_data(lua_State *L)
233 NO_MAP_LOCK_REQUIRED;
235 LuaVoxelManip *o = checkobject(L, 1);
236 ManualMapVoxelManipulator *vm = o->vm;
238 int volume = vm->m_area.getVolume();
241 for (int i = 0; i != volume; i++) {
242 lua_Integer param2 = vm->m_data[i].param2;
243 lua_pushinteger(L, param2);
244 lua_rawseti(L, -2, i + 1);
250 int LuaVoxelManip::l_set_param2_data(lua_State *L)
252 NO_MAP_LOCK_REQUIRED;
254 LuaVoxelManip *o = checkobject(L, 1);
255 ManualMapVoxelManipulator *vm = o->vm;
257 if (!lua_istable(L, 2))
260 int volume = vm->m_area.getVolume();
261 for (int i = 0; i != volume; i++) {
262 lua_rawgeti(L, 2, i + 1);
263 u8 param2 = lua_tointeger(L, -1);
265 vm->m_data[i].param2 = param2;
273 int LuaVoxelManip::l_update_map(lua_State *L)
275 LuaVoxelManip *o = checkobject(L, 1);
279 Environment *env = getEnv(L);
283 Map *map = &(env->getMap());
285 // TODO: Optimize this by using Mapgen::calcLighting() instead
286 std::map<v3s16, MapBlock *> lighting_mblocks;
287 std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
289 lighting_mblocks.insert(mblocks->begin(), mblocks->end());
291 map->updateLighting(lighting_mblocks, *mblocks);
294 event.type = MEET_OTHER;
295 for (std::map<v3s16, MapBlock *>::iterator
296 it = mblocks->begin();
297 it != mblocks->end(); ++it)
298 event.modified_blocks.insert(it->first);
300 map->dispatchEvent(&event);
307 LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
310 this->is_mapgen_vm = is_mg_vm;
313 LuaVoxelManip::LuaVoxelManip(Map *map)
315 this->vm = new ManualMapVoxelManipulator(map);
316 this->is_mapgen_vm = false;
319 LuaVoxelManip::~LuaVoxelManip()
326 // Creates an LuaVoxelManip and leaves it on top of stack
327 int LuaVoxelManip::create_object(lua_State *L)
329 NO_MAP_LOCK_REQUIRED;
331 Environment *env = getEnv(L);
335 Map *map = &(env->getMap());
336 LuaVoxelManip *o = new LuaVoxelManip(map);
338 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
339 luaL_getmetatable(L, className);
340 lua_setmetatable(L, -2);
344 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
346 NO_MAP_LOCK_REQUIRED;
348 luaL_checktype(L, narg, LUA_TUSERDATA);
350 void *ud = luaL_checkudata(L, narg, className);
352 luaL_typerror(L, narg, className);
354 return *(LuaVoxelManip **)ud; // unbox pointer
357 void LuaVoxelManip::Register(lua_State *L)
360 int methodtable = lua_gettop(L);
361 luaL_newmetatable(L, className);
362 int metatable = lua_gettop(L);
364 lua_pushliteral(L, "__metatable");
365 lua_pushvalue(L, methodtable);
366 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
368 lua_pushliteral(L, "__index");
369 lua_pushvalue(L, methodtable);
370 lua_settable(L, metatable);
372 lua_pushliteral(L, "__gc");
373 lua_pushcfunction(L, gc_object);
374 lua_settable(L, metatable);
376 lua_pop(L, 1); // drop metatable
378 luaL_openlib(L, 0, methods, 0); // fill methodtable
379 lua_pop(L, 1); // drop methodtable
381 // Can be created from Lua (VoxelManip())
382 lua_register(L, className, create_object);
385 const char LuaVoxelManip::className[] = "VoxelManip";
386 const luaL_reg LuaVoxelManip::methods[] = {
387 luamethod(LuaVoxelManip, read_from_map),
388 luamethod(LuaVoxelManip, get_data),
389 luamethod(LuaVoxelManip, set_data),
390 luamethod(LuaVoxelManip, write_to_map),
391 luamethod(LuaVoxelManip, update_map),
392 luamethod(LuaVoxelManip, update_liquids),
393 luamethod(LuaVoxelManip, calc_lighting),
394 luamethod(LuaVoxelManip, set_lighting),
395 luamethod(LuaVoxelManip, get_light_data),
396 luamethod(LuaVoxelManip, set_light_data),
397 luamethod(LuaVoxelManip, get_param2_data),
398 luamethod(LuaVoxelManip, set_param2_data),