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