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);
114 if (!o->is_mapgen_vm)
117 INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
118 Map *map = &(get_scriptapi(L)->getEnv()->getMap());
119 ManualMapVoxelManipulator *vm = o->vm;
125 mg.updateLiquid(&map->m_transforming_liquid,
126 vm->m_area.MinEdge, vm->m_area.MaxEdge);
131 int LuaVoxelManip::l_calc_lighting(lua_State *L)
133 NO_MAP_LOCK_REQUIRED;
135 LuaVoxelManip *o = checkobject(L, 1);
136 if (!o->is_mapgen_vm)
139 INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
140 EmergeManager *emerge = STACK_TO_SERVER(L)->getEmergeManager();
141 ManualMapVoxelManipulator *vm = o->vm;
146 mg.water_level = emerge->params->water_level;
148 mg.calcLighting(vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE,
149 vm->m_area.MaxEdge - v3s16(0, 1, 0) * MAP_BLOCKSIZE);
154 int LuaVoxelManip::l_set_lighting(lua_State *L)
156 NO_MAP_LOCK_REQUIRED;
158 LuaVoxelManip *o = checkobject(L, 1);
159 if (!o->is_mapgen_vm)
162 if (!lua_istable(L, 2))
166 light = (getintfield_default(L, 4, "day", 0) & 0x0F);
167 light |= (getintfield_default(L, 4, "night", 0) & 0x0F) << 8;
169 ManualMapVoxelManipulator *vm = o->vm;
174 mg.setLighting(vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE,
175 vm->m_area.MaxEdge - v3s16(0, 1, 0) * MAP_BLOCKSIZE,
181 int LuaVoxelManip::l_update_map(lua_State *L)
183 LuaVoxelManip *o = checkobject(L, 1);
187 // TODO: Optimize this by using Mapgen::calcLighting() instead
188 std::map<v3s16, MapBlock *> lighting_mblocks;
189 std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
191 lighting_mblocks.insert(mblocks->begin(), mblocks->end());
193 Map *map = &(get_scriptapi(L)->getEnv()->getMap());
194 map->updateLighting(lighting_mblocks, *mblocks);
197 event.type = MEET_OTHER;
198 for (std::map<v3s16, MapBlock *>::iterator
199 it = mblocks->begin();
200 it != mblocks->end(); ++it)
201 event.modified_blocks.insert(it->first);
203 map->dispatchEvent(&event);
210 LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
213 this->is_mapgen_vm = is_mg_vm;
216 LuaVoxelManip::LuaVoxelManip(Map *map)
218 this->vm = new ManualMapVoxelManipulator(map);
219 this->is_mapgen_vm = false;
222 LuaVoxelManip::~LuaVoxelManip()
228 // Creates an LuaVoxelManip and leaves it on top of stack
229 int LuaVoxelManip::create_object(lua_State *L)
231 NO_MAP_LOCK_REQUIRED;
233 Environment *env = get_scriptapi(L)->getEnv();
237 Map *map = &(env->getMap());
238 LuaVoxelManip *o = new LuaVoxelManip(map);
240 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
241 luaL_getmetatable(L, className);
242 lua_setmetatable(L, -2);
246 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
248 NO_MAP_LOCK_REQUIRED;
250 luaL_checktype(L, narg, LUA_TUSERDATA);
252 void *ud = luaL_checkudata(L, narg, className);
254 luaL_typerror(L, narg, className);
256 return *(LuaVoxelManip **)ud; // unbox pointer
259 void LuaVoxelManip::Register(lua_State *L)
262 int methodtable = lua_gettop(L);
263 luaL_newmetatable(L, className);
264 int metatable = lua_gettop(L);
266 lua_pushliteral(L, "__metatable");
267 lua_pushvalue(L, methodtable);
268 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
270 lua_pushliteral(L, "__index");
271 lua_pushvalue(L, methodtable);
272 lua_settable(L, metatable);
274 lua_pushliteral(L, "__gc");
275 lua_pushcfunction(L, gc_object);
276 lua_settable(L, metatable);
278 lua_pop(L, 1); // drop metatable
280 luaL_openlib(L, 0, methods, 0); // fill methodtable
281 lua_pop(L, 1); // drop methodtable
283 // Can be created from Lua (VoxelManip()
284 lua_register(L, className, create_object);
287 const char LuaVoxelManip::className[] = "VoxelManip";
288 const luaL_reg LuaVoxelManip::methods[] = {
289 luamethod(LuaVoxelManip, read_from_map),
290 luamethod(LuaVoxelManip, get_data),
291 luamethod(LuaVoxelManip, set_data),
292 luamethod(LuaVoxelManip, write_to_map),
293 luamethod(LuaVoxelManip, update_map),
294 luamethod(LuaVoxelManip, update_liquids),
295 luamethod(LuaVoxelManip, calc_lighting),
296 luamethod(LuaVoxelManip, set_lighting),
300 REGISTER_LUA_REF(LuaVoxelManip);