LuaVoxelManip: Allow liquid updates in non-mapgen VoxelManip objects
[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         if (!o->is_mapgen_vm)
37                 delete o;
38         
39         return 0;
40 }
41
42 int LuaVoxelManip::l_read_from_map(lua_State *L)
43 {
44         LuaVoxelManip *o = checkobject(L, 1);
45         ManualMapVoxelManipulator *vm = o->vm;
46         
47         v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
48         v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3));
49         sortBoxVerticies(bp1, bp2);
50         
51         vm->initialEmerge(bp1, bp2);
52         
53         push_v3s16(L, vm->m_area.MinEdge);
54         push_v3s16(L, vm->m_area.MaxEdge);
55         
56         return 2;
57 }
58
59 int LuaVoxelManip::l_get_data(lua_State *L)
60 {
61         NO_MAP_LOCK_REQUIRED;
62
63         LuaVoxelManip *o = checkobject(L, 1);
64         ManualMapVoxelManipulator *vm = o->vm;
65         
66         int volume = vm->m_area.getVolume();
67         
68         lua_newtable(L);
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);
73         }
74         
75         return 1;
76 }
77
78 int LuaVoxelManip::l_set_data(lua_State *L)
79 {
80         NO_MAP_LOCK_REQUIRED;
81         
82         LuaVoxelManip *o = checkobject(L, 1);
83         ManualMapVoxelManipulator *vm = o->vm;
84         
85         if (!lua_istable(L, 2))
86                 return 0;
87         
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);
92                 
93                 vm->m_data[i].setContent(c);
94
95                 lua_pop(L, 1);
96         }
97                 
98         return 0;
99 }
100
101 int LuaVoxelManip::l_write_to_map(lua_State *L)
102 {
103         LuaVoxelManip *o = checkobject(L, 1);
104         ManualMapVoxelManipulator *vm = o->vm;
105
106         vm->blitBackAll(&o->modified_blocks);
107
108         return 0;       
109 }
110
111 int LuaVoxelManip::l_update_liquids(lua_State *L)
112 {
113         LuaVoxelManip *o = checkobject(L, 1);
114         
115         INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
116         Map *map = &(get_scriptapi(L)->getEnv()->getMap());
117         ManualMapVoxelManipulator *vm = o->vm;
118
119         Mapgen mg;
120         mg.vm   = vm;
121         mg.ndef = ndef;
122
123         mg.updateLiquid(&map->m_transforming_liquid,
124                         vm->m_area.MinEdge, vm->m_area.MaxEdge);
125
126         return 0;
127 }
128
129 int LuaVoxelManip::l_calc_lighting(lua_State *L)
130 {
131         NO_MAP_LOCK_REQUIRED;
132         
133         LuaVoxelManip *o = checkobject(L, 1);
134         if (!o->is_mapgen_vm)
135                 return 0;
136                 
137         INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
138         EmergeManager *emerge = STACK_TO_SERVER(L)->getEmergeManager();
139         ManualMapVoxelManipulator *vm = o->vm;
140
141         Mapgen mg;
142         mg.vm          = vm;
143         mg.ndef        = ndef;
144         mg.water_level = emerge->params->water_level;
145         
146         mg.calcLighting(vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE,
147                                         vm->m_area.MaxEdge - v3s16(0, 1, 0) * MAP_BLOCKSIZE);
148
149         return 0;
150 }
151
152 int LuaVoxelManip::l_set_lighting(lua_State *L)
153 {
154         NO_MAP_LOCK_REQUIRED;
155         
156         LuaVoxelManip *o = checkobject(L, 1);
157         if (!o->is_mapgen_vm)
158                 return 0;
159         
160         if (!lua_istable(L, 2))
161                 return 0;
162
163         u8 light;
164         light  = (getintfield_default(L, 4, "day",   0) & 0x0F);
165         light |= (getintfield_default(L, 4, "night", 0) & 0x0F) << 8;
166         
167         ManualMapVoxelManipulator *vm = o->vm;
168         
169         Mapgen mg;
170         mg.vm = vm;
171         
172         mg.setLighting(vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE,
173                                    vm->m_area.MaxEdge - v3s16(0, 1, 0) * MAP_BLOCKSIZE,
174                                    light);
175
176         return 0;
177 }
178
179 int LuaVoxelManip::l_update_map(lua_State *L)
180 {
181         LuaVoxelManip *o = checkobject(L, 1);
182         if (o->is_mapgen_vm)
183                 return 0;
184         
185         // TODO: Optimize this by using Mapgen::calcLighting() instead
186         std::map<v3s16, MapBlock *> lighting_mblocks;
187         std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
188         
189         lighting_mblocks.insert(mblocks->begin(), mblocks->end());
190         
191         Map *map = &(get_scriptapi(L)->getEnv()->getMap());
192         map->updateLighting(lighting_mblocks, *mblocks);
193
194         MapEditEvent event;
195         event.type = MEET_OTHER;
196         for (std::map<v3s16, MapBlock *>::iterator
197                 it = mblocks->begin();
198                 it != mblocks->end(); ++it)
199                 event.modified_blocks.insert(it->first);
200                 
201         map->dispatchEvent(&event);
202
203         mblocks->clear();
204
205         return 0;       
206 }
207
208 LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
209 {
210         this->vm           = mmvm;
211         this->is_mapgen_vm = is_mg_vm;
212 }
213
214 LuaVoxelManip::LuaVoxelManip(Map *map)
215 {
216         this->vm = new ManualMapVoxelManipulator(map);
217         this->is_mapgen_vm = false;
218 }
219
220 LuaVoxelManip::~LuaVoxelManip()
221 {
222         delete vm;
223 }
224
225 // LuaVoxelManip()
226 // Creates an LuaVoxelManip and leaves it on top of stack
227 int LuaVoxelManip::create_object(lua_State *L)
228 {
229         NO_MAP_LOCK_REQUIRED;
230         
231         Environment *env = get_scriptapi(L)->getEnv();
232         if (!env)
233                 return 0;
234                 
235         Map *map = &(env->getMap());
236         LuaVoxelManip *o = new LuaVoxelManip(map);
237         
238         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
239         luaL_getmetatable(L, className);
240         lua_setmetatable(L, -2);
241         return 1;
242 }
243
244 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
245 {
246         NO_MAP_LOCK_REQUIRED;
247         
248         luaL_checktype(L, narg, LUA_TUSERDATA);
249
250         void *ud = luaL_checkudata(L, narg, className);
251         if (!ud)
252                 luaL_typerror(L, narg, className);
253         
254         return *(LuaVoxelManip **)ud;  // unbox pointer
255 }
256
257 void LuaVoxelManip::Register(lua_State *L)
258 {
259         lua_newtable(L);
260         int methodtable = lua_gettop(L);
261         luaL_newmetatable(L, className);
262         int metatable = lua_gettop(L);
263
264         lua_pushliteral(L, "__metatable");
265         lua_pushvalue(L, methodtable);
266         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
267
268         lua_pushliteral(L, "__index");
269         lua_pushvalue(L, methodtable);
270         lua_settable(L, metatable);
271
272         lua_pushliteral(L, "__gc");
273         lua_pushcfunction(L, gc_object);
274         lua_settable(L, metatable);
275
276         lua_pop(L, 1);  // drop metatable
277
278         luaL_openlib(L, 0, methods, 0);  // fill methodtable
279         lua_pop(L, 1);  // drop methodtable
280
281         // Can be created from Lua (VoxelManip()
282         lua_register(L, className, create_object);
283 }
284
285 const char LuaVoxelManip::className[] = "VoxelManip";
286 const luaL_reg LuaVoxelManip::methods[] = {
287         luamethod(LuaVoxelManip, read_from_map),
288         luamethod(LuaVoxelManip, get_data),
289         luamethod(LuaVoxelManip, set_data),
290         luamethod(LuaVoxelManip, write_to_map),
291         luamethod(LuaVoxelManip, update_map),
292         luamethod(LuaVoxelManip, update_liquids),
293         luamethod(LuaVoxelManip, calc_lighting),
294         luamethod(LuaVoxelManip, set_lighting),
295         {0,0}
296 };
297
298 REGISTER_LUA_REF(LuaVoxelManip);