1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
Minetest
Copyright (C) 2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "lua_api/l_base.h"
#include "lua_api/l_vmanip.h"
///////
#include "cpp_api/scriptapi.h"
#include "common/c_converter.h"
#include "server.h"
#include "emerge.h"
#include "common/c_internal.h"
// garbage collector
int LuaVoxelManip::gc_object(lua_State *L)
{
LuaVoxelManip *o = *(LuaVoxelManip **)(lua_touserdata(L, 1));
if (o->do_gc)
delete o;
return 0;
}
int LuaVoxelManip::l_read_from_map(lua_State *L)
{
LuaVoxelManip *o = checkobject(L, 1);
ManualMapVoxelManipulator *vm = o->vm;
v3s16 bp1 = getNodeBlockPos(read_v3s16(L, 2));
v3s16 bp2 = getNodeBlockPos(read_v3s16(L, 3));
sortBoxVerticies(bp1, bp2);
vm->initialEmerge(bp1, bp2);
push_v3s16(L, vm->m_area.MinEdge);
push_v3s16(L, vm->m_area.MaxEdge);
return 2;
}
int LuaVoxelManip::l_get_data(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaVoxelManip *o = checkobject(L, 1);
ManualMapVoxelManipulator *vm = o->vm;
int volume = vm->m_area.getVolume();
lua_newtable(L);
for (int i = 0; i != volume; i++) {
lua_Integer cid = vm->m_data[i].getContent();
lua_pushinteger(L, cid);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
int LuaVoxelManip::l_set_data(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaVoxelManip *o = checkobject(L, 1);
ManualMapVoxelManipulator *vm = o->vm;
if (!lua_istable(L, 2))
return 0;
int volume = vm->m_area.getVolume();
for (int i = 0; i != volume; i++) {
lua_rawgeti(L, 2, i + 1);
content_t c = lua_tointeger(L, -1);
vm->m_data[i].setContent(c);
lua_pop(L, 1);
}
return 0;
}
int LuaVoxelManip::l_write_to_map(lua_State *L)
{
LuaVoxelManip *o = checkobject(L, 1);
ManualMapVoxelManipulator *vm = o->vm;
vm->blitBackAll(&o->modified_blocks);
return 0;
}
int LuaVoxelManip::l_update_liquids(lua_State *L)
{
LuaVoxelManip *o = checkobject(L, 1);
ManualMapVoxelManipulator *vm = o->vm;
INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
Map *map = &(get_scriptapi(L)->getEnv()->getMap());
Mapgen mg;
mg.vm = vm;
mg.ndef = ndef;
mg.updateLiquid(&map->m_transforming_liquid,
vm->m_area.MinEdge, vm->m_area.MaxEdge);
return 0;
}
int LuaVoxelManip::l_calc_lighting(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaVoxelManip *o = checkobject(L, 1);
v3s16 p1 = read_v3s16(L, 2);
v3s16 p2 = read_v3s16(L, 3);
sortBoxVerticies(p1, p2);
ManualMapVoxelManipulator *vm = o->vm;
INodeDefManager *ndef = STACK_TO_SERVER(L)->getNodeDefManager();
EmergeManager *emerge = STACK_TO_SERVER(L)->getEmergeManager();
Mapgen mg;
mg.vm = vm;
mg.ndef = ndef;
mg.water_level = emerge->params->water_level;
mg.calcLighting(p1, p2);
return 0;
}
int LuaVoxelManip::l_set_lighting(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaVoxelManip *o = checkobject(L, 1);
v3s16 p1 = read_v3s16(L, 2);
v3s16 p2 = read_v3s16(L, 3);
sortBoxVerticies(p1, p2);
u8 light;
if (!lua_istable(L, 4))
return 0;
light = getintfield_default(L, 4, "day", 0);
light |= getintfield_default(L, 4, "night", 0);
ManualMapVoxelManipulator *vm = o->vm;
Mapgen mg;
mg.vm = vm;
mg.setLighting(p1, p2, light);
return 0;
}
int LuaVoxelManip::l_update_map(lua_State *L)
{
LuaVoxelManip *o = checkobject(L, 1);
// TODO: Optimize this by using Mapgen::calcLighting() instead
std::map<v3s16, MapBlock *> lighting_mblocks;
std::map<v3s16, MapBlock *> *mblocks = &o->modified_blocks;
lighting_mblocks.insert(mblocks->begin(), mblocks->end());
Map *map = &(get_scriptapi(L)->getEnv()->getMap());
map->updateLighting(lighting_mblocks, *mblocks);
MapEditEvent event;
event.type = MEET_OTHER;
for (std::map<v3s16, MapBlock *>::iterator
it = mblocks->begin();
it != mblocks->end(); ++it)
event.modified_blocks.insert(it->first);
map->dispatchEvent(&event);
mblocks->clear();
return 0;
}
LuaVoxelManip::LuaVoxelManip(ManualMapVoxelManipulator *mmvm, bool dogc)
{
this->vm = mmvm;
this->do_gc = dogc;
}
LuaVoxelManip::LuaVoxelManip(Map *map)
{
vm = new ManualMapVoxelManipulator(map);
}
LuaVoxelManip::~LuaVoxelManip()
{
delete vm;
}
// LuaVoxelManip()
// Creates an LuaVoxelManip and leaves it on top of stack
int LuaVoxelManip::create_object(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
Environment *env = get_scriptapi(L)->getEnv();
if (!env)
return 0;
Map *map = &(env->getMap());
LuaVoxelManip *o = new LuaVoxelManip(map);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
return 1;
}
LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
{
NO_MAP_LOCK_REQUIRED;
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(LuaVoxelManip **)ud; // unbox pointer
}
void LuaVoxelManip::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable); // hide metatable from Lua getmetatable()
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1); // drop metatable
luaL_openlib(L, 0, methods, 0); // fill methodtable
lua_pop(L, 1); // drop methodtable
// Can be created from Lua (VoxelManip()
lua_register(L, className, create_object);
}
const char LuaVoxelManip::className[] = "VoxelManip";
const luaL_reg LuaVoxelManip::methods[] = {
luamethod(LuaVoxelManip, read_from_map),
luamethod(LuaVoxelManip, get_data),
luamethod(LuaVoxelManip, set_data),
luamethod(LuaVoxelManip, write_to_map),
luamethod(LuaVoxelManip, update_map),
luamethod(LuaVoxelManip, update_liquids),
luamethod(LuaVoxelManip, calc_lighting),
luamethod(LuaVoxelManip, set_lighting),
{0,0}
};
REGISTER_LUA_REF(LuaVoxelManip);
|