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