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_base.h"
22 #include "lua_api/l_vmanip.h"
26 #include "cpp_api/scriptapi.h"
27 #include "common/c_converter.h"
30 #include "common/c_internal.h"
33 int LuaVoxelManip::gc_object(lua_State *L)
35 LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
42 int LuaVoxelManip::l_read_from_map(lua_State *L)
44 LuaVoxelManip *o = checkobject(L, 1);
45 ManualMapVoxelManipulator *vm = o->vm;
47 v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
48 v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3));
49 sortBoxVerticies(bp1, bp2);
51 vm->initialEmerge(bp1, bp2);
53 push_v3s16(L, vm->m_area.MinEdge);
54 push_v3s16(L, vm->m_area.MaxEdge);
59 int LuaVoxelManip::l_get_data(lua_State *L)
63 LuaVoxelManip *o = checkobject(L, 1);
64 ManualMapVoxelManipulator *vm = o->vm;
66 int volume = vm->m_area.getVolume();
69 for (int i = 0; i != volume; i++) {
70 lua_Integer cid = vm->m_data[i].getContent();
71 lua_pushinteger(L, cid);
72 lua_rawseti(L, -2, i + 1);
78 int LuaVoxelManip::l_set_data(lua_State *L)
82 LuaVoxelManip *o = checkobject(L, 1);
83 ManualMapVoxelManipulator *vm = o->vm;
85 if (!lua_istable(L, 2))
88 int volume = vm->m_area.getVolume();
89 for (int i = 0; i != volume; i++) {
90 lua_rawgeti(L, 2, i + 1);
91 content_t c = lua_tointeger(L, -1);
93 vm->m_data[i].setContent(c);
101 int LuaVoxelManip::l_write_to_map(lua_State *L)
103 LuaVoxelManip *o = checkobject(L, 1);
104 ManualMapVoxelManipulator *vm = o->vm;
106 vm->blitBackAll(&o->modified_blocks);
111 int LuaVoxelManip::l_update_liquids(lua_State *L)
113 LuaVoxelManip *o = checkobject(L, 1);
115 ManualMapVoxelManipulator *vm = o->vm;
116 INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
117 Map *map = &(get_scriptapi(L)->getEnv()->getMap());
123 mg.updateLiquid(&map->m_transforming_liquid,
124 vm->m_area.MinEdge, vm->m_area.MaxEdge);
129 int LuaVoxelManip::l_calc_lighting(lua_State *L)
131 NO_MAP_LOCK_REQUIRED;
133 LuaVoxelManip *o = checkobject(L, 1);
134 v3s16 p1 = read_v3s16(L, 2);
135 v3s16 p2 = read_v3s16(L, 3);
136 sortBoxVerticies(p1, p2);
138 ManualMapVoxelManipulator *vm = o->vm;
139 INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
140 EmergeManager *emerge = STACK_TO_SERVER(L)->getEmergeManager();
145 mg.water_level = emerge->params->water_level;
147 mg.calcLighting(p1, p2);
152 int LuaVoxelManip::l_set_lighting(lua_State *L)
154 NO_MAP_LOCK_REQUIRED;
156 LuaVoxelManip *o = checkobject(L, 1);
157 v3s16 p1 = read_v3s16(L, 2);
158 v3s16 p2 = read_v3s16(L, 3);
159 sortBoxVerticies(p1, p2);
162 if (!lua_istable(L, 4))
165 light = getintfield_default(L, 4, "day", 0);
166 light |= getintfield_default(L, 4, "night", 0);
168 ManualMapVoxelManipulator *vm = o->vm;
173 mg.setLighting(p1, p2, light);
178 int LuaVoxelManip::l_update_map(lua_State *L)
180 LuaVoxelManip *o = checkobject(L, 1);
182 // TODO: Optimize this by using Mapgen::calcLighting() instead
183 std::map<v3s16, MapBlock *> lighting_mblocks;
184 std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
186 lighting_mblocks.insert(mblocks->begin(), mblocks->end());
188 Map *map = &(get_scriptapi(L)->getEnv()->getMap());
189 map->updateLighting(lighting_mblocks, *mblocks);
192 event.type = MEET_OTHER;
193 for (std::map<v3s16, MapBlock *>::iterator
194 it = mblocks->begin();
195 it != mblocks->end(); ++it)
196 event.modified_blocks.insert(it->first);
198 map->dispatchEvent(&event);
205 LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool dogc)
211 LuaVoxelManip::LuaVoxelManip(Map *map)
213 vm = new ManualMapVoxelManipulator(map);
216 LuaVoxelManip::~LuaVoxelManip()
222 // Creates an LuaVoxelManip and leaves it on top of stack
223 int LuaVoxelManip::create_object(lua_State *L)
225 NO_MAP_LOCK_REQUIRED;
227 Environment *env = get_scriptapi(L)->getEnv();
231 Map *map = &(env->getMap());
232 LuaVoxelManip *o = new LuaVoxelManip(map);
234 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
235 luaL_getmetatable(L, className);
236 lua_setmetatable(L, -2);
240 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
242 NO_MAP_LOCK_REQUIRED;
244 luaL_checktype(L, narg, LUA_TUSERDATA);
246 void *ud = luaL_checkudata(L, narg, className);
248 luaL_typerror(L, narg, className);
250 return *(LuaVoxelManip **)ud; // unbox pointer
253 void LuaVoxelManip::Register(lua_State *L)
256 int methodtable = lua_gettop(L);
257 luaL_newmetatable(L, className);
258 int metatable = lua_gettop(L);
260 lua_pushliteral(L, "__metatable");
261 lua_pushvalue(L, methodtable);
262 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
264 lua_pushliteral(L, "__index");
265 lua_pushvalue(L, methodtable);
266 lua_settable(L, metatable);
268 lua_pushliteral(L, "__gc");
269 lua_pushcfunction(L, gc_object);
270 lua_settable(L, metatable);
272 lua_pop(L, 1); // drop metatable
274 luaL_openlib(L, 0, methods, 0); // fill methodtable
275 lua_pop(L, 1); // drop methodtable
277 // Can be created from Lua (VoxelManip()
278 lua_register(L, className, create_object);
281 const char LuaVoxelManip::className[] = "VoxelManip";
282 const luaL_reg LuaVoxelManip::methods[] = {
283 luamethod(LuaVoxelManip, read_from_map),
284 luamethod(LuaVoxelManip, get_data),
285 luamethod(LuaVoxelManip, set_data),
286 luamethod(LuaVoxelManip, write_to_map),
287 luamethod(LuaVoxelManip, update_map),
288 luamethod(LuaVoxelManip, update_liquids),
289 luamethod(LuaVoxelManip, calc_lighting),
290 luamethod(LuaVoxelManip, set_lighting),
294 REGISTER_LUA_REF(LuaVoxelManip);