]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_vmanip.cpp
554a573430827d37a593f2670d5332587c8ac7b8
[dragonfireclient.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_vmanip.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_content.h"
24 #include "common/c_converter.h"
25 #include "emerge.h"
26 #include "environment.h"
27 #include "map.h"
28 #include "server.h"
29 #include "mapgen.h"
30
31 #define GET_ENV_PTR ServerEnvironment* env =                                   \
32                                 dynamic_cast<ServerEnvironment*>(getEnv(L));                   \
33                                 if (env == NULL) return 0
34
35 // garbage collector
36 int LuaVoxelManip::gc_object(lua_State *L)
37 {
38         LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
39         delete o;
40
41         return 0;
42 }
43
44 int LuaVoxelManip::l_read_from_map(lua_State *L)
45 {
46         LuaVoxelManip *o = checkobject(L, 1);
47         ManualMapVoxelManipulator *vm = o->vm;
48
49         v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
50         v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3));
51         sortBoxVerticies(bp1, bp2);
52
53         vm->initialEmerge(bp1, bp2);
54
55         push_v3s16(L, vm->m_area.MinEdge);
56         push_v3s16(L, vm->m_area.MaxEdge);
57
58         return 2;
59 }
60
61 int LuaVoxelManip::l_get_data(lua_State *L)
62 {
63         NO_MAP_LOCK_REQUIRED;
64
65         LuaVoxelManip *o = checkobject(L, 1);
66         ManualMapVoxelManipulator *vm = o->vm;
67
68         int volume = vm->m_area.getVolume();
69
70         lua_newtable(L);
71         for (int i = 0; i != volume; i++) {
72                 lua_Integer cid = vm->m_data[i].getContent();
73                 lua_pushinteger(L, cid);
74                 lua_rawseti(L, -2, i + 1);
75         }
76
77         return 1;
78 }
79
80 int LuaVoxelManip::l_set_data(lua_State *L)
81 {
82         NO_MAP_LOCK_REQUIRED;
83
84         LuaVoxelManip *o = checkobject(L, 1);
85         ManualMapVoxelManipulator *vm = o->vm;
86
87         if (!lua_istable(L, 2))
88                 return 0;
89
90         int volume = vm->m_area.getVolume();
91         for (int i = 0; i != volume; i++) {
92                 lua_rawgeti(L, 2, i + 1);
93                 content_t c = lua_tointeger(L, -1);
94
95                 vm->m_data[i].setContent(c);
96
97                 lua_pop(L, 1);
98         }
99
100         return 0;
101 }
102
103 int LuaVoxelManip::l_write_to_map(lua_State *L)
104 {
105         LuaVoxelManip *o = checkobject(L, 1);
106         ManualMapVoxelManipulator *vm = o->vm;
107
108         vm->blitBackAll(&o->modified_blocks);
109
110         return 0;
111 }
112
113 int LuaVoxelManip::l_get_node_at(lua_State *L)
114 {
115         NO_MAP_LOCK_REQUIRED;
116         GET_ENV_PTR;
117
118         LuaVoxelManip *o = checkobject(L, 1);
119         v3s16 pos        = read_v3s16(L, 2);
120
121         pushnode(L, o->vm->getNodeNoExNoEmerge(pos), env->getGameDef()->ndef());
122         return 1;
123 }
124
125 int LuaVoxelManip::l_set_node_at(lua_State *L)
126 {
127         NO_MAP_LOCK_REQUIRED;
128         GET_ENV_PTR;
129
130         LuaVoxelManip *o = checkobject(L, 1);
131         v3s16 pos        = read_v3s16(L, 2);
132         MapNode n        = readnode(L, 3, env->getGameDef()->ndef());
133
134         o->vm->setNodeNoEmerge(pos, n);
135
136         return 0;
137 }
138
139 int LuaVoxelManip::l_update_liquids(lua_State *L)
140 {
141         GET_ENV_PTR;
142
143         LuaVoxelManip *o = checkobject(L, 1);
144
145         Map *map = &(env->getMap());
146         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
147         ManualMapVoxelManipulator *vm = o->vm;
148
149         Mapgen mg;
150         mg.vm   = vm;
151         mg.ndef = ndef;
152
153         mg.updateLiquid(&map->m_transforming_liquid,
154                         vm->m_area.MinEdge, vm->m_area.MaxEdge);
155
156         return 0;
157 }
158
159 int LuaVoxelManip::l_calc_lighting(lua_State *L)
160 {
161         NO_MAP_LOCK_REQUIRED;
162
163         LuaVoxelManip *o = checkobject(L, 1);
164         if (!o->is_mapgen_vm)
165                 return 0;
166
167         INodeDefManager *ndef = getServer(L)->getNodeDefManager();
168         EmergeManager *emerge = getServer(L)->getEmergeManager();
169         ManualMapVoxelManipulator *vm = o->vm;
170
171         v3s16 p1 = lua_istable(L, 2) ? read_v3s16(L, 2) :
172                 vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE;
173         v3s16 p2 = lua_istable(L, 3) ? read_v3s16(L, 3) :
174                 vm->m_area.MaxEdge - v3s16(0, 1, 0) * MAP_BLOCKSIZE;
175         sortBoxVerticies(p1, p2);
176
177         Mapgen mg;
178         mg.vm          = vm;
179         mg.ndef        = ndef;
180         mg.water_level = emerge->params.water_level;
181
182         mg.calcLighting(p1, p2);
183
184         return 0;
185 }
186
187 int LuaVoxelManip::l_set_lighting(lua_State *L)
188 {
189         NO_MAP_LOCK_REQUIRED;
190
191         LuaVoxelManip *o = checkobject(L, 1);
192         if (!o->is_mapgen_vm)
193                 return 0;
194
195         if (!lua_istable(L, 2))
196                 return 0;
197
198         u8 light;
199         light  = (getintfield_default(L, 2, "day",   0) & 0x0F);
200         light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
201
202         ManualMapVoxelManipulator *vm = o->vm;
203
204         v3s16 p1 = lua_istable(L, 3) ? read_v3s16(L, 3) :
205                 vm->m_area.MinEdge + v3s16(0, 1, 0) * MAP_BLOCKSIZE;
206         v3s16 p2 = lua_istable(L, 4) ? read_v3s16(L, 4) :
207                 vm->m_area.MaxEdge - v3s16(0, 1, 0) * MAP_BLOCKSIZE;
208         sortBoxVerticies(p1, p2);
209
210         Mapgen mg;
211         mg.vm = vm;
212
213         mg.setLighting(p1, p2, light);
214
215         return 0;
216 }
217
218 int LuaVoxelManip::l_get_light_data(lua_State *L)
219 {
220         NO_MAP_LOCK_REQUIRED;
221
222         LuaVoxelManip *o = checkobject(L, 1);
223         ManualMapVoxelManipulator *vm = o->vm;
224
225         int volume = vm->m_area.getVolume();
226
227         lua_newtable(L);
228         for (int i = 0; i != volume; i++) {
229                 lua_Integer light = vm->m_data[i].param1;
230                 lua_pushinteger(L, light);
231                 lua_rawseti(L, -2, i + 1);
232         }
233
234         return 1;
235 }
236
237 int LuaVoxelManip::l_set_light_data(lua_State *L)
238 {
239         NO_MAP_LOCK_REQUIRED;
240
241         LuaVoxelManip *o = checkobject(L, 1);
242         ManualMapVoxelManipulator *vm = o->vm;
243
244         if (!lua_istable(L, 2))
245                 return 0;
246
247         int volume = vm->m_area.getVolume();
248         for (int i = 0; i != volume; i++) {
249                 lua_rawgeti(L, 2, i + 1);
250                 u8 light = lua_tointeger(L, -1);
251
252                 vm->m_data[i].param1 = light;
253
254                 lua_pop(L, 1);
255         }
256
257         return 0;
258 }
259
260 int LuaVoxelManip::l_get_param2_data(lua_State *L)
261 {
262         NO_MAP_LOCK_REQUIRED;
263
264         LuaVoxelManip *o = checkobject(L, 1);
265         ManualMapVoxelManipulator *vm = o->vm;
266
267         int volume = vm->m_area.getVolume();
268
269         lua_newtable(L);
270         for (int i = 0; i != volume; i++) {
271                 lua_Integer param2 = vm->m_data[i].param2;
272                 lua_pushinteger(L, param2);
273                 lua_rawseti(L, -2, i + 1);
274         }
275
276         return 1;
277 }
278
279 int LuaVoxelManip::l_set_param2_data(lua_State *L)
280 {
281         NO_MAP_LOCK_REQUIRED;
282
283         LuaVoxelManip *o = checkobject(L, 1);
284         ManualMapVoxelManipulator *vm = o->vm;
285
286         if (!lua_istable(L, 2))
287                 return 0;
288
289         int volume = vm->m_area.getVolume();
290         for (int i = 0; i != volume; i++) {
291                 lua_rawgeti(L, 2, i + 1);
292                 u8 param2 = lua_tointeger(L, -1);
293
294                 vm->m_data[i].param2 = param2;
295
296                 lua_pop(L, 1);
297         }
298
299         return 0;
300 }
301
302 int LuaVoxelManip::l_update_map(lua_State *L)
303 {
304         LuaVoxelManip *o = checkobject(L, 1);
305         if (o->is_mapgen_vm)
306                 return 0;
307
308         Environment *env = getEnv(L);
309         if (!env)
310                 return 0;
311
312         Map *map = &(env->getMap());
313
314         // TODO: Optimize this by using Mapgen::calcLighting() instead
315         std::map<v3s16, MapBlock *> lighting_mblocks;
316         std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
317
318         lighting_mblocks.insert(mblocks->begin(), mblocks->end());
319
320         map->updateLighting(lighting_mblocks, *mblocks);
321
322         MapEditEvent event;
323         event.type = MEET_OTHER;
324         for (std::map<v3s16, MapBlock *>::iterator
325                 it = mblocks->begin();
326                 it != mblocks->end(); ++it)
327                 event.modified_blocks.insert(it->first);
328
329         map->dispatchEvent(&event);
330
331         mblocks->clear();
332
333         return 0;
334 }
335
336 int LuaVoxelManip::l_was_modified(lua_State *L)
337 {
338         NO_MAP_LOCK_REQUIRED;
339
340         LuaVoxelManip *o = checkobject(L, 1);
341         ManualMapVoxelManipulator *vm = o->vm;
342
343         lua_pushboolean(L, vm->m_is_dirty);
344
345         return 1;
346 }
347
348 LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool is_mg_vm)
349 {
350         this->vm           = mmvm;
351         this->is_mapgen_vm = is_mg_vm;
352 }
353
354 LuaVoxelManip::LuaVoxelManip(Map *map)
355 {
356         this->vm = new ManualMapVoxelManipulator(map);
357         this->is_mapgen_vm = false;
358 }
359
360 LuaVoxelManip::~LuaVoxelManip()
361 {
362         if (!is_mapgen_vm)
363                 delete vm;
364 }
365
366 // LuaVoxelManip()
367 // Creates an LuaVoxelManip and leaves it on top of stack
368 int LuaVoxelManip::create_object(lua_State *L)
369 {
370         NO_MAP_LOCK_REQUIRED;
371
372         Environment *env = getEnv(L);
373         if (!env)
374                 return 0;
375
376         Map *map = &(env->getMap());
377         LuaVoxelManip *o = new LuaVoxelManip(map);
378
379         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
380         luaL_getmetatable(L, className);
381         lua_setmetatable(L, -2);
382         return 1;
383 }
384
385 LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
386 {
387         NO_MAP_LOCK_REQUIRED;
388
389         luaL_checktype(L, narg, LUA_TUSERDATA);
390
391         void *ud = luaL_checkudata(L, narg, className);
392         if (!ud)
393                 luaL_typerror(L, narg, className);
394
395         return *(LuaVoxelManip **)ud;  // unbox pointer
396 }
397
398 void LuaVoxelManip::Register(lua_State *L)
399 {
400         lua_newtable(L);
401         int methodtable = lua_gettop(L);
402         luaL_newmetatable(L, className);
403         int metatable = lua_gettop(L);
404
405         lua_pushliteral(L, "__metatable");
406         lua_pushvalue(L, methodtable);
407         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
408
409         lua_pushliteral(L, "__index");
410         lua_pushvalue(L, methodtable);
411         lua_settable(L, metatable);
412
413         lua_pushliteral(L, "__gc");
414         lua_pushcfunction(L, gc_object);
415         lua_settable(L, metatable);
416
417         lua_pop(L, 1);  // drop metatable
418
419         luaL_openlib(L, 0, methods, 0);  // fill methodtable
420         lua_pop(L, 1);  // drop methodtable
421
422         // Can be created from Lua (VoxelManip())
423         lua_register(L, className, create_object);
424 }
425
426 const char LuaVoxelManip::className[] = "VoxelManip";
427 const luaL_reg LuaVoxelManip::methods[] = {
428         luamethod(LuaVoxelManip, read_from_map),
429         luamethod(LuaVoxelManip, get_data),
430         luamethod(LuaVoxelManip, set_data),
431         luamethod(LuaVoxelManip, get_node_at),
432         luamethod(LuaVoxelManip, set_node_at),
433         luamethod(LuaVoxelManip, write_to_map),
434         luamethod(LuaVoxelManip, update_map),
435         luamethod(LuaVoxelManip, update_liquids),
436         luamethod(LuaVoxelManip, calc_lighting),
437         luamethod(LuaVoxelManip, set_lighting),
438         luamethod(LuaVoxelManip, get_light_data),
439         luamethod(LuaVoxelManip, set_light_data),
440         luamethod(LuaVoxelManip, get_param2_data),
441         luamethod(LuaVoxelManip, set_param2_data),
442         luamethod(LuaVoxelManip, was_modified),
443         {0,0}
444 };