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));
41 int LuaVoxelManip::l_read_chunk(lua_State *L)
43 LuaVoxelManip *o = checkobject(L, 1);
45 v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
46 v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3));
47 sortBoxVerticies(bp1, bp2);
48 ManualMapVoxelManipulator *vm = o->vm;
49 vm->initialEmerge(bp1, bp2);
51 v3s16 emerged_p1 = vm->m_area.MinEdge;
52 v3s16 emerged_p2 = vm->m_area.MaxEdge;
54 int volume = vm->m_area.getVolume();
57 for (int i = 0; i != volume; i++) {
58 lua_Number cid = vm->m_data[i].getContent();
59 lua_pushnumber(L, cid);
60 lua_rawseti(L, -2, i + 1);
63 push_v3s16(L, emerged_p1);
64 push_v3s16(L, emerged_p2);
69 int LuaVoxelManip::l_write_chunk(lua_State *L)
71 LuaVoxelManip *o = checkobject(L, 1);
72 if (!lua_istable(L, 2))
75 ManualMapVoxelManipulator *vm = o->vm;
77 int volume = vm->m_area.getVolume();
78 for (int i = 0; i != volume; i++) {
79 lua_rawgeti(L, 2, i + 1);
80 content_t c = lua_tonumber(L, -1);
82 vm->m_data[i].setContent(c);
88 vm->blitBackAll(&o->modified_blocks);
93 int LuaVoxelManip::l_update_liquids(lua_State *L)
95 LuaVoxelManip *o = checkobject(L, 1);
97 ManualMapVoxelManipulator *vm = o->vm;
98 INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
99 Map *map = &(get_scriptapi(L)->getEnv()->getMap());
105 mg.updateLiquid(&map->m_transforming_liquid,
106 vm->m_area.MinEdge, vm->m_area.MaxEdge);
111 int LuaVoxelManip::l_calc_lighting(lua_State *L)
113 NO_MAP_LOCK_REQUIRED;
115 LuaVoxelManip *o = checkobject(L, 1);
116 v3s16 p1 = read_v3s16(L, 2);
117 v3s16 p2 = read_v3s16(L, 3);
118 sortBoxVerticies(p1, p2);
120 ManualMapVoxelManipulator *vm = o->vm;
121 INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
122 EmergeManager *emerge = STACK_TO_SERVER(L)->getEmergeManager();
127 mg.water_level = emerge->params->water_level;
129 mg.calcLighting(p1, p2);
134 int LuaVoxelManip::l_set_lighting(lua_State *L)
136 NO_MAP_LOCK_REQUIRED;
138 LuaVoxelManip *o = checkobject(L, 1);
139 v3s16 p1 = read_v3s16(L, 2);
140 v3s16 p2 = read_v3s16(L, 3);
141 sortBoxVerticies(p1, p2);
144 if (!lua_istable(L, 4))
147 light = getintfield_default(L, 4, "day", 0);
148 light |= getintfield_default(L, 4, "night", 0);
150 ManualMapVoxelManipulator *vm = o->vm;
155 mg.setLighting(p1, p2, light);
160 int LuaVoxelManip::l_update_map(lua_State *L)
162 LuaVoxelManip *o = checkobject(L, 1);
164 // TODO: Optimize this by using Mapgen::calcLighting() instead
165 std::map<v3s16, MapBlock *> lighting_mblocks;
166 std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
168 lighting_mblocks.insert(mblocks->begin(), mblocks->end());
170 Map *map = &(get_scriptapi(L)->getEnv()->getMap());
171 map->updateLighting(lighting_mblocks, *mblocks);
174 event.type = MEET_OTHER;
175 for (std::map<v3s16, MapBlock *>::iterator
176 it = mblocks->begin();
177 it != mblocks->end(); ++it)
178 event.modified_blocks.insert(it->first);
180 map->dispatchEvent(&event);
187 LuaVoxelManip::LuaVoxelManip(Map *map)
189 vm = new ManualMapVoxelManipulator(map);
192 LuaVoxelManip::~LuaVoxelManip()
198 // Creates an LuaVoxelManip and leaves it on top of stack
199 int LuaVoxelManip::create_object(lua_State *L)
201 NO_MAP_LOCK_REQUIRED;
203 Map *map = &(get_scriptapi(L)->getEnv()->getMap());
204 LuaVoxelManip *o = new LuaVoxelManip(map);
206 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
207 luaL_getmetatable(L, className);
208 lua_setmetatable(L, -2);
212 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
214 NO_MAP_LOCK_REQUIRED;
216 luaL_checktype(L, narg, LUA_TUSERDATA);
218 void *ud = luaL_checkudata(L, narg, className);
220 luaL_typerror(L, narg, className);
222 return *(LuaVoxelManip **)ud; // unbox pointer
225 void LuaVoxelManip::Register(lua_State *L)
228 int methodtable = lua_gettop(L);
229 luaL_newmetatable(L, className);
230 int metatable = lua_gettop(L);
232 lua_pushliteral(L, "__metatable");
233 lua_pushvalue(L, methodtable);
234 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
236 lua_pushliteral(L, "__index");
237 lua_pushvalue(L, methodtable);
238 lua_settable(L, metatable);
240 lua_pushliteral(L, "__gc");
241 lua_pushcfunction(L, gc_object);
242 lua_settable(L, metatable);
244 lua_pop(L, 1); // drop metatable
246 luaL_openlib(L, 0, methods, 0); // fill methodtable
247 lua_pop(L, 1); // drop methodtable
249 // Can be created from Lua (VoxelManip()
250 lua_register(L, className, create_object);
253 const char LuaVoxelManip::className[] = "VoxelManip";
254 const luaL_reg LuaVoxelManip::methods[] = {
255 luamethod(LuaVoxelManip, read_chunk),
256 luamethod(LuaVoxelManip, write_chunk),
257 luamethod(LuaVoxelManip, update_map),
258 luamethod(LuaVoxelManip, update_liquids),
259 luamethod(LuaVoxelManip, calc_lighting),
260 luamethod(LuaVoxelManip, set_lighting),
264 REGISTER_LUA_REF(LuaVoxelManip);