Add LuaVoxelManip
[oweals/minetest.git] / src / script / lua_api / l_vmanip.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
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.
9
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.
14
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.
18 */
19
20
21 #include "lua_api/l_base.h"
22 #include "lua_api/l_vmanip.h"
23
24 ///////
25
26 #include "cpp_api/scriptapi.h"
27 #include "common/c_converter.h"
28 #include "server.h"
29 #include "emerge.h"
30 #include "common/c_internal.h"
31
32 // garbage collector
33 int LuaVoxelManip::gc_object(lua_State *L)
34 {
35         LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
36         delete o;
37         
38         return 0;
39 }
40
41 int LuaVoxelManip::l_read_chunk(lua_State *L)
42 {
43         LuaVoxelManip *o = checkobject(L, 1);
44         
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);
50         
51         v3s16 emerged_p1 = vm->m_area.MinEdge;
52         v3s16 emerged_p2 = vm->m_area.MaxEdge;
53         
54         int volume = vm->m_area.getVolume();
55         
56         lua_newtable(L);
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);
61         }
62         
63         push_v3s16(L, emerged_p1);
64         push_v3s16(L, emerged_p2);
65         
66         return 3;
67 }
68
69 int LuaVoxelManip::l_write_chunk(lua_State *L)
70 {
71         LuaVoxelManip *o = checkobject(L, 1);
72         if (!lua_istable(L, 2))
73                 return 0;
74         
75         ManualMapVoxelManipulator *vm = o->vm;
76         
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);
81                 
82                 vm->m_data[i].setContent(c);
83
84                 lua_pop(L, 1);
85                 
86         }
87
88         vm->blitBackAll(&o->modified_blocks);
89         
90         return 0;
91 }
92
93 int LuaVoxelManip::l_update_liquids(lua_State *L)
94 {
95         LuaVoxelManip *o = checkobject(L, 1);
96         
97         ManualMapVoxelManipulator *vm = o->vm;
98         INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
99         Map *map = &(get_scriptapi(L)->getEnv()->getMap());
100
101         Mapgen mg;
102         mg.vm   = vm;
103         mg.ndef = ndef;
104
105         mg.updateLiquid(&map->m_transforming_liquid,
106                         vm->m_area.MinEdge, vm->m_area.MaxEdge);
107
108         return 0;
109 }
110
111 int LuaVoxelManip::l_calc_lighting(lua_State *L)
112 {
113         NO_MAP_LOCK_REQUIRED;
114         
115         LuaVoxelManip *o = checkobject(L, 1);
116         v3s16 p1 = read_v3s16(L, 2);
117         v3s16 p2 = read_v3s16(L, 3);
118         sortBoxVerticies(p1, p2);
119         
120         ManualMapVoxelManipulator *vm = o->vm;
121         INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
122         EmergeManager *emerge = STACK_TO_SERVER(L)->getEmergeManager();
123         
124         Mapgen mg;
125         mg.vm          = vm;
126         mg.ndef        = ndef;
127         mg.water_level = emerge->params->water_level;
128         
129         mg.calcLighting(p1, p2);
130
131         return 0;
132 }
133
134 int LuaVoxelManip::l_set_lighting(lua_State *L)
135 {
136         NO_MAP_LOCK_REQUIRED;
137         
138         LuaVoxelManip *o = checkobject(L, 1);
139         v3s16 p1 = read_v3s16(L, 2);
140         v3s16 p2 = read_v3s16(L, 3);
141         sortBoxVerticies(p1, p2);
142         
143         u8 light;
144         if (!lua_istable(L, 4))
145                 return 0;
146
147         light  = getintfield_default(L, 4, "day", 0);
148         light |= getintfield_default(L, 4, "night", 0);
149         
150         ManualMapVoxelManipulator *vm = o->vm;
151         
152         Mapgen mg;
153         mg.vm = vm;
154         
155         mg.setLighting(p1, p2, light);
156
157         return 0;
158 }
159
160 int LuaVoxelManip::l_update_map(lua_State *L)
161 {
162         LuaVoxelManip *o = checkobject(L, 1);
163         
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;
167         
168         lighting_mblocks.insert(mblocks->begin(), mblocks->end());
169         
170         Map *map = &(get_scriptapi(L)->getEnv()->getMap());
171         map->updateLighting(lighting_mblocks, *mblocks);
172
173         MapEditEvent event;
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);
179                 
180         map->dispatchEvent(&event);
181
182         mblocks->clear();
183
184         return 0;       
185 }
186
187 LuaVoxelManip::LuaVoxelManip(Map *map)
188 {
189         vm = new ManualMapVoxelManipulator(map);
190 }
191
192 LuaVoxelManip::~LuaVoxelManip()
193 {
194         delete vm;
195 }
196
197 // LuaVoxelManip()
198 // Creates an LuaVoxelManip and leaves it on top of stack
199 int LuaVoxelManip::create_object(lua_State *L)
200 {
201         NO_MAP_LOCK_REQUIRED;
202         
203         Map *map = &(get_scriptapi(L)->getEnv()->getMap());
204         LuaVoxelManip *o = new LuaVoxelManip(map);
205         
206         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
207         luaL_getmetatable(L, className);
208         lua_setmetatable(L, -2);
209         return 1;
210 }
211
212 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
213 {
214         NO_MAP_LOCK_REQUIRED;
215         
216         luaL_checktype(L, narg, LUA_TUSERDATA);
217
218         void *ud = luaL_checkudata(L, narg, className);
219         if (!ud)
220                 luaL_typerror(L, narg, className);
221         
222         return *(LuaVoxelManip **)ud;  // unbox pointer
223 }
224
225 void LuaVoxelManip::Register(lua_State *L)
226 {
227         lua_newtable(L);
228         int methodtable = lua_gettop(L);
229         luaL_newmetatable(L, className);
230         int metatable = lua_gettop(L);
231
232         lua_pushliteral(L, "__metatable");
233         lua_pushvalue(L, methodtable);
234         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
235
236         lua_pushliteral(L, "__index");
237         lua_pushvalue(L, methodtable);
238         lua_settable(L, metatable);
239
240         lua_pushliteral(L, "__gc");
241         lua_pushcfunction(L, gc_object);
242         lua_settable(L, metatable);
243
244         lua_pop(L, 1);  // drop metatable
245
246         luaL_openlib(L, 0, methods, 0);  // fill methodtable
247         lua_pop(L, 1);  // drop methodtable
248
249         // Can be created from Lua (VoxelManip()
250         lua_register(L, className, create_object);
251 }
252
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),
261         {0,0}
262 };
263
264 REGISTER_LUA_REF(LuaVoxelManip);