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