]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_vmanip.cpp
Add MapSettingsManager and new mapgen setting script API functions
[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         bool propagate_shadow = lua_isboolean(L, 4) ? lua_toboolean(L, 4) : true;
185
186         sortBoxVerticies(pmin, pmax);
187         if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
188                 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
189
190         Mapgen mg;
191         mg.vm          = vm;
192         mg.ndef        = ndef;
193         mg.water_level = emerge->mgparams->water_level;
194
195         mg.calcLighting(pmin, pmax, fpmin, fpmax, propagate_shadow);
196
197         return 0;
198 }
199
200 int LuaVoxelManip::l_set_lighting(lua_State *L)
201 {
202         NO_MAP_LOCK_REQUIRED;
203
204         LuaVoxelManip *o = checkobject(L, 1);
205         if (!o->is_mapgen_vm)
206                 return 0;
207
208         if (!lua_istable(L, 2))
209                 return 0;
210
211         u8 light;
212         light  = (getintfield_default(L, 2, "day",   0) & 0x0F);
213         light |= (getintfield_default(L, 2, "night", 0) & 0x0F) << 4;
214
215         MMVManip *vm = o->vm;
216
217         v3s16 yblock = v3s16(0, 1, 0) * MAP_BLOCKSIZE;
218         v3s16 pmin = lua_istable(L, 3) ? check_v3s16(L, 3) : vm->m_area.MinEdge + yblock;
219         v3s16 pmax = lua_istable(L, 4) ? check_v3s16(L, 4) : vm->m_area.MaxEdge - yblock;
220
221         sortBoxVerticies(pmin, pmax);
222         if (!vm->m_area.contains(VoxelArea(pmin, pmax)))
223                 throw LuaError("Specified voxel area out of VoxelManipulator bounds");
224
225         Mapgen mg;
226         mg.vm = vm;
227
228         mg.setLighting(light, pmin, pmax);
229
230         return 0;
231 }
232
233 int LuaVoxelManip::l_get_light_data(lua_State *L)
234 {
235         NO_MAP_LOCK_REQUIRED;
236
237         LuaVoxelManip *o = checkobject(L, 1);
238         MMVManip *vm = o->vm;
239
240         u32 volume = vm->m_area.getVolume();
241
242         lua_newtable(L);
243         for (u32 i = 0; i != volume; i++) {
244                 lua_Integer light = vm->m_data[i].param1;
245                 lua_pushinteger(L, light);
246                 lua_rawseti(L, -2, i + 1);
247         }
248
249         return 1;
250 }
251
252 int LuaVoxelManip::l_set_light_data(lua_State *L)
253 {
254         NO_MAP_LOCK_REQUIRED;
255
256         LuaVoxelManip *o = checkobject(L, 1);
257         MMVManip *vm = o->vm;
258
259         if (!lua_istable(L, 2))
260                 return 0;
261
262         u32 volume = vm->m_area.getVolume();
263         for (u32 i = 0; i != volume; i++) {
264                 lua_rawgeti(L, 2, i + 1);
265                 u8 light = lua_tointeger(L, -1);
266
267                 vm->m_data[i].param1 = light;
268
269                 lua_pop(L, 1);
270         }
271
272         return 0;
273 }
274
275 int LuaVoxelManip::l_get_param2_data(lua_State *L)
276 {
277         NO_MAP_LOCK_REQUIRED;
278
279         LuaVoxelManip *o = checkobject(L, 1);
280         MMVManip *vm = o->vm;
281
282         u32 volume = vm->m_area.getVolume();
283
284         lua_newtable(L);
285         for (u32 i = 0; i != volume; i++) {
286                 lua_Integer param2 = vm->m_data[i].param2;
287                 lua_pushinteger(L, param2);
288                 lua_rawseti(L, -2, i + 1);
289         }
290
291         return 1;
292 }
293
294 int LuaVoxelManip::l_set_param2_data(lua_State *L)
295 {
296         NO_MAP_LOCK_REQUIRED;
297
298         LuaVoxelManip *o = checkobject(L, 1);
299         MMVManip *vm = o->vm;
300
301         if (!lua_istable(L, 2))
302                 return 0;
303
304         u32 volume = vm->m_area.getVolume();
305         for (u32 i = 0; i != volume; i++) {
306                 lua_rawgeti(L, 2, i + 1);
307                 u8 param2 = lua_tointeger(L, -1);
308
309                 vm->m_data[i].param2 = param2;
310
311                 lua_pop(L, 1);
312         }
313
314         return 0;
315 }
316
317 int LuaVoxelManip::l_update_map(lua_State *L)
318 {
319         GET_ENV_PTR;
320
321         LuaVoxelManip *o = checkobject(L, 1);
322         if (o->is_mapgen_vm)
323                 return 0;
324
325         Map *map = &(env->getMap());
326
327         // TODO: Optimize this by using Mapgen::calcLighting() instead
328         std::map<v3s16, MapBlock *> lighting_mblocks;
329         std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
330
331         lighting_mblocks.insert(mblocks->begin(), mblocks->end());
332
333         map->updateLighting(lighting_mblocks, *mblocks);
334
335         MapEditEvent event;
336         event.type = MEET_OTHER;
337         for (std::map<v3s16, MapBlock *>::iterator
338                 it = mblocks->begin();
339                 it != mblocks->end(); ++it)
340                 event.modified_blocks.insert(it->first);
341
342         map->dispatchEvent(&event);
343
344         mblocks->clear();
345
346         return 0;
347 }
348
349 int LuaVoxelManip::l_was_modified(lua_State *L)
350 {
351         NO_MAP_LOCK_REQUIRED;
352
353         LuaVoxelManip *o = checkobject(L, 1);
354         MMVManip *vm = o->vm;
355
356         lua_pushboolean(L, vm->m_is_dirty);
357
358         return 1;
359 }
360
361 int LuaVoxelManip::l_get_emerged_area(lua_State *L)
362 {
363         NO_MAP_LOCK_REQUIRED;
364
365         LuaVoxelManip *o = checkobject(L, 1);
366
367         push_v3s16(L, o->vm->m_area.MinEdge);
368         push_v3s16(L, o->vm->m_area.MaxEdge);
369
370         return 2;
371 }
372
373 LuaVoxelManip::LuaVoxelManip(MMVManip *mmvm, bool is_mg_vm)
374 {
375         this->vm           = mmvm;
376         this->is_mapgen_vm = is_mg_vm;
377 }
378
379 LuaVoxelManip::LuaVoxelManip(Map *map)
380 {
381         this->vm = new MMVManip(map);
382         this->is_mapgen_vm = false;
383 }
384
385 LuaVoxelManip::LuaVoxelManip(Map *map, v3s16 p1, v3s16 p2)
386 {
387         this->vm = new MMVManip(map);
388         this->is_mapgen_vm = false;
389
390         v3s16 bp1 = getNodeBlockPos(p1);
391         v3s16 bp2 = getNodeBlockPos(p2);
392         sortBoxVerticies(bp1, bp2);
393         vm->initialEmerge(bp1, bp2);
394 }
395
396 LuaVoxelManip::~LuaVoxelManip()
397 {
398         if (!is_mapgen_vm)
399                 delete vm;
400 }
401
402 // LuaVoxelManip()
403 // Creates an LuaVoxelManip and leaves it on top of stack
404 int LuaVoxelManip::create_object(lua_State *L)
405 {
406         GET_ENV_PTR;
407
408         Map *map = &(env->getMap());
409         LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
410                 new LuaVoxelManip(map, check_v3s16(L, 1), 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[] = {
462         luamethod(LuaVoxelManip, read_from_map),
463         luamethod(LuaVoxelManip, get_data),
464         luamethod(LuaVoxelManip, set_data),
465         luamethod(LuaVoxelManip, get_node_at),
466         luamethod(LuaVoxelManip, set_node_at),
467         luamethod(LuaVoxelManip, write_to_map),
468         luamethod(LuaVoxelManip, update_map),
469         luamethod(LuaVoxelManip, update_liquids),
470         luamethod(LuaVoxelManip, calc_lighting),
471         luamethod(LuaVoxelManip, set_lighting),
472         luamethod(LuaVoxelManip, get_light_data),
473         luamethod(LuaVoxelManip, set_light_data),
474         luamethod(LuaVoxelManip, get_param2_data),
475         luamethod(LuaVoxelManip, set_param2_data),
476         luamethod(LuaVoxelManip, was_modified),
477         luamethod(LuaVoxelManip, get_emerged_area),
478         {0,0}
479 };