1 /*
2 script/lua_api/l_env.cpp
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 */
5
6 /*
7 This file is part of Freeminer.
8
9 Freeminer is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Freeminer is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Freeminer. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "lua_api/l_env.h"
24 #include "lua_api/l_internal.h"
25 #include "lua_api/l_nodemeta.h"
26 #include "lua_api/l_nodetimer.h"
27 #include "lua_api/l_noise.h"
28 #include "lua_api/l_vmanip.h"
29 #include "common/c_converter.h"
30 #include "common/c_content.h"
31 #include "scripting_game.h"
32 #include "environment.h"
33 #include "server.h"
34 #include "nodedef.h"
35 #include "daynightratio.h"
36 #include "util/pointedthing.h"
37 #include "content_sao.h"
38 #include "treegen.h"
39 #include "pathfinder.h"
40 #include <unordered_set>
41
42 #define GET_ENV_PTR ServerEnvironment* env = \
43 dynamic_cast<ServerEnvironment*>(getEnv(L)); \
44 if (env == NULL) return 0
45
46 ///////////////////////////////////////////////////////////////////////////////
47
48 v3s16 start_pos;
49
trigger(ServerEnvironment * env,v3s16 p,MapNode n,u32 active_object_count,u32 active_object_count_wider,MapNode neighbor,bool activate)50 void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
51 u32 active_object_count, u32 active_object_count_wider, MapNode neighbor, bool activate)
52 {
53 GameScripting *scriptIface = env->getScriptIface();
54 auto _script_lock = std::unique_lock<std::recursive_mutex> (scriptIface->m_luastackmutex);
55 scriptIface->realityCheck();
56
57 lua_State *L = scriptIface->getStack();
58 assert(lua_checkstack(L, 20));
59 StackUnroller stack_unroller(L);
60
61 lua_pushcfunction(L, script_error_handler);
62 int errorhandler = lua_gettop(L);
63
64 // Get registered_abms
65 lua_getglobal(L, "core");
66 lua_getfield(L, -1, "registered_abms");
67 luaL_checktype(L, -1, LUA_TTABLE);
68 lua_remove(L, -2); // Remove core
69
70 // Get registered_abms[m_id]
71 lua_pushnumber(L, m_id);
72 lua_gettable(L, -2);
73 if(lua_isnil(L, -1))
74 assert(0);
75 lua_remove(L, -2); // Remove registered_abms
76
77 // Call action
78 luaL_checktype(L, -1, LUA_TTABLE);
79 lua_getfield(L, -1, "action");
80 luaL_checktype(L, -1, LUA_TFUNCTION);
81 lua_remove(L, -2); // Remove registered_abms[m_id]
82 push_v3s16(L, p);
83 pushnode(L, n, env->getGameDef()->ndef());
84 lua_pushnumber(L, active_object_count);
85 lua_pushnumber(L, active_object_count_wider);
86 pushnode(L, neighbor, env->getGameDef()->ndef());
87 lua_pushboolean(L, activate);
88 if(lua_pcall(L, 6, 0, errorhandler))
89 script_error(L);
90 lua_pop(L, 1); // Pop error handler
91 }
92
93 // Exported functions
94
95 // set_node(pos, node, fast)
96 // pos = {x=num, y=num, z=num}
l_set_node(lua_State * L)97 int ModApiEnvMod::l_set_node(lua_State *L)
98 {
99 GET_ENV_PTR;
100
101 INodeDefManager *ndef = env->getGameDef()->ndef();
102 // parameters
103 v3s16 pos = read_v3s16(L, 1);
104 MapNode n = readnode(L, 2, ndef);
105 // Do it
106 bool succeeded = env->setNode(pos, n, lua_tonumber(L, 3));
107 lua_pushboolean(L, succeeded);
108 return 1;
109 }
110
l_add_node(lua_State * L)111 int ModApiEnvMod::l_add_node(lua_State *L)
112 {
113 return l_set_node(L);
114 }
115
116 // remove_node(pos, fast)
117 // pos = {x=num, y=num, z=num}
l_remove_node(lua_State * L)118 int ModApiEnvMod::l_remove_node(lua_State *L)
119 {
120 GET_ENV_PTR;
121
122 // parameters
123 v3s16 pos = read_v3s16(L, 1);
124 // Do it
125 bool succeeded = env->removeNode(pos, lua_tonumber(L, 2));
126 lua_pushboolean(L, succeeded);
127 return 1;
128 }
129
130 // swap_node(pos, node)
131 // pos = {x=num, y=num, z=num}
l_swap_node(lua_State * L)132 int ModApiEnvMod::l_swap_node(lua_State *L)
133 {
134 GET_ENV_PTR;
135
136 INodeDefManager *ndef = env->getGameDef()->ndef();
137 // parameters
138 v3s16 pos = read_v3s16(L, 1);
139 MapNode n = readnode(L, 2, ndef);
140 // Do it
141 bool succeeded = env->swapNode(pos, n);
142 lua_pushboolean(L, succeeded);
143 return 1;
144 }
145
146 // get_node(pos)
147 // pos = {x=num, y=num, z=num}
l_get_node(lua_State * L)148 int ModApiEnvMod::l_get_node(lua_State *L)
149 {
150 GET_ENV_PTR;
151
152 // pos
153 v3s16 pos = read_v3s16(L, 1);
154 // Do it
155 MapNode n = env->getMap().getNodeNoEx(pos);
156 // Return node
157 pushnode(L, n, env->getGameDef()->ndef());
158 return 1;
159 }
160
161 // get_node_or_nil(pos)
162 // pos = {x=num, y=num, z=num}
l_get_node_or_nil(lua_State * L)163 int ModApiEnvMod::l_get_node_or_nil(lua_State *L)
164 {
165 GET_ENV_PTR;
166
167 // pos
168 v3s16 pos = read_v3s16(L, 1);
169 // Do it
170 bool pos_ok;
171 MapNode n = env->getMap().getNodeNoEx(pos, &pos_ok);
172 if (pos_ok) {
173 // Return node
174 pushnode(L, n, env->getGameDef()->ndef());
175 } else {
176 lua_pushnil(L);
177 }
178 return 1;
179 }
180
181 // get_node_light(pos, timeofday)
182 // pos = {x=num, y=num, z=num}
183 // timeofday: nil = current time, 0 = night, 0.5 = day
l_get_node_light(lua_State * L)184 int ModApiEnvMod::l_get_node_light(lua_State *L)
185 {
186 GET_ENV_PTR;
187
188 // Do it
189 v3s16 pos = read_v3s16(L, 1);
190 u32 time_of_day = env->getTimeOfDay();
191 if(lua_isnumber(L, 2))
192 time_of_day = 24000.0 * lua_tonumber(L, 2);
193 time_of_day %= 24000;
194 u32 dnr = time_to_daynight_ratio(time_of_day, true);
195
196 bool is_position_ok;
197 MapNode n = env->getMap().getNodeNoEx(pos, &is_position_ok);
198 if (is_position_ok) {
199 INodeDefManager *ndef = env->getGameDef()->ndef();
200 lua_pushinteger(L, n.getLightBlend(dnr, ndef));
201 } else {
202 lua_pushnil(L);
203 }
204 return 1;
205 }
206
207 // place_node(pos, node)
208 // pos = {x=num, y=num, z=num}
l_place_node(lua_State * L)209 int ModApiEnvMod::l_place_node(lua_State *L)
210 {
211 GET_ENV_PTR;
212
213 ScriptApiItem *scriptIfaceItem = getScriptApi<ScriptApiItem>(L);
214 Server *server = getServer(L);
215 INodeDefManager *ndef = server->ndef();
216 IItemDefManager *idef = server->idef();
217
218 v3s16 pos = read_v3s16(L, 1);
219 MapNode n = readnode(L, 2, ndef);
220
221 // Don't attempt to load non-loaded area as of now
222 MapNode n_old = env->getMap().getNodeNoEx(pos);
223 if(n_old.getContent() == CONTENT_IGNORE){
224 lua_pushboolean(L, false);
225 return 1;
226 }
227 // Create item to place
228 ItemStack item(ndef->get(n).name, 1, 0, "", idef);
229 // Make pointed position
230 PointedThing pointed;
231 pointed.type = POINTEDTHING_NODE;
232 pointed.node_abovesurface = pos;
233 pointed.node_undersurface = pos + v3s16(0,-1,0);
234 // Place it with a NULL placer (appears in Lua as a non-functional
235 // ObjectRef)
236 bool success = scriptIfaceItem->item_OnPlace(item, NULL, pointed);
237 lua_pushboolean(L, success);
238 return 1;
239 }
240
241 // dig_node(pos)
242 // pos = {x=num, y=num, z=num}
l_dig_node(lua_State * L)243 int ModApiEnvMod::l_dig_node(lua_State *L)
244 {
245 GET_ENV_PTR;
246
247 ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
248
249 v3s16 pos = read_v3s16(L, 1);
250
251 // Don't attempt to load non-loaded area as of now
252 MapNode n = env->getMap().getNodeNoEx(pos);
253 if(n.getContent() == CONTENT_IGNORE){
254 lua_pushboolean(L, false);
255 return 1;
256 }
257 // Dig it out with a NULL digger (appears in Lua as a
258 // non-functional ObjectRef)
259 bool success = scriptIfaceNode->node_on_dig(pos, n, NULL);
260 lua_pushboolean(L, success);
261 return 1;
262 }
263
264 // punch_node(pos)
265 // pos = {x=num, y=num, z=num}
l_punch_node(lua_State * L)266 int ModApiEnvMod::l_punch_node(lua_State *L)
267 {
268 GET_ENV_PTR;
269
270 ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
271
272 v3s16 pos = read_v3s16(L, 1);
273
274 // Don't attempt to load non-loaded area as of now
275 MapNode n = env->getMap().getNodeNoEx(pos);
276 if(n.getContent() == CONTENT_IGNORE){
277 lua_pushboolean(L, false);
278 return 1;
279 }
280 // Punch it with a NULL puncher (appears in Lua as a non-functional
281 // ObjectRef)
282 bool success = scriptIfaceNode->node_on_punch(pos, n, NULL, PointedThing());
283 lua_pushboolean(L, success);
284 return 1;
285 }
286
287 // get_node_max_level(pos)
288 // pos = {x=num, y=num, z=num}
l_get_node_max_level(lua_State * L)289 int ModApiEnvMod::l_get_node_max_level(lua_State *L)
290 {
291 GET_ENV_PTR;
292
293 v3s16 pos = read_v3s16(L, 1);
294 MapNode n = env->getMap().getNodeNoEx(pos);
295 lua_pushnumber(L, n.getMaxLevel(env->getGameDef()->ndef()));
296 return 1;
297 }
298
299 // get_node_level(pos)
300 // pos = {x=num, y=num, z=num}
l_get_node_level(lua_State * L)301 int ModApiEnvMod::l_get_node_level(lua_State *L)
302 {
303 GET_ENV_PTR;
304
305 v3s16 pos = read_v3s16(L, 1);
306 MapNode n = env->getMap().getNodeNoEx(pos);
307 lua_pushnumber(L, n.getLevel(env->getGameDef()->ndef()));
308 return 1;
309 }
310
311 // set_node_level(pos, level)
312 // pos = {x=num, y=num, z=num}
313 // level: 0..63
l_set_node_level(lua_State * L)314 int ModApiEnvMod::l_set_node_level(lua_State *L)
315 {
316 GET_ENV_PTR;
317
318 v3s16 pos = read_v3s16(L, 1);
319 u8 level = 1;
320 if(lua_isnumber(L, 2))
321 level = lua_tonumber(L, 2);
322 MapNode n = env->getMap().getNodeNoEx(pos);
323 if(n.getContent() == CONTENT_IGNORE){
324 lua_pushnumber(L, 0);
325 return 1;
326 }
327 lua_pushnumber(L, n.setLevel(env->getGameDef()->ndef(), level));
328 env->setNode(pos, n);
329 return 1;
330 }
331
332 // add_node_level(pos, level)
333 // pos = {x=num, y=num, z=num}
334 // level: 0..63
l_add_node_level(lua_State * L)335 int ModApiEnvMod::l_add_node_level(lua_State *L)
336 {
337 GET_ENV_PTR;
338
339 v3s16 pos = read_v3s16(L, 1);
340 u8 level = 1;
341 bool compress = 0;
342 if(lua_isnumber(L, 2))
343 level = lua_tonumber(L, 2);
344 if(lua_isnumber(L, 3))
345 compress = lua_tonumber(L, 3);
346 MapNode n = env->getMap().getNodeNoEx(pos);
347 if(n.getContent() == CONTENT_IGNORE){
348 lua_pushnumber(L, 0);
349 return 1;
350 }
351 lua_pushnumber(L, n.addLevel(env->getGameDef()->ndef(), level, compress));
352 env->setNode(pos, n);
353 return 1;
354 }
355
356 // freeze_melt(pos, direction)
357 // pos = {x=num, y=num, z=num}
358 // direction: -1 (freeze), 1 (melt)
l_freeze_melt(lua_State * L)359 int ModApiEnvMod::l_freeze_melt(lua_State *L)
360 {
361 GET_ENV_PTR;
362
363 v3s16 pos = read_v3s16(L, 1);
364 int direction = 1;
365 if(lua_isnumber(L, 2))
366 direction = lua_tonumber(L, 2);
367 MapNode n = env->getMap().getNodeNoEx(pos);
368 if(n.getContent() == CONTENT_IGNORE){
369 lua_pushnumber(L, 0);
370 return 1;
371 }
372 lua_pushnumber(L, n.freeze_melt(env->getGameDef()->ndef(), direction));
373 env->setNode(pos, n);
374 return 1;
375 }
376
377
378 // get_meta(pos)
l_get_meta(lua_State * L)379 int ModApiEnvMod::l_get_meta(lua_State *L)
380 {
381 GET_ENV_PTR;
382
383 // Do it
384 v3s16 p = read_v3s16(L, 1);
385 NodeMetaRef::create(L, p, env);
386 return 1;
387 }
388
389 // get_node_timer(pos)
l_get_node_timer(lua_State * L)390 int ModApiEnvMod::l_get_node_timer(lua_State *L)
391 {
392 GET_ENV_PTR;
393
394 // Do it
395 v3s16 p = read_v3s16(L, 1);
396 NodeTimerRef::create(L, p, env);
397 return 1;
398 }
399
400 // add_entity(pos, entityname) -> ObjectRef or nil
401 // pos = {x=num, y=num, z=num}
l_add_entity(lua_State * L)402 int ModApiEnvMod::l_add_entity(lua_State *L)
403 {
404 GET_ENV_PTR;
405
406 // pos
407 v3f pos = checkFloatPos(L, 1);
408 // content
409 const char *name = luaL_checkstring(L, 2);
410 // Do it
411 ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, "");
412 int objectid = env->addActiveObject(obj);
413 // If failed to add, return nothing (reads as nil)
414 if(objectid == 0)
415 return 0;
416 // Return ObjectRef
417 getScriptApiBase(L)->objectrefGetOrCreate(L, obj);
418 return 1;
419 }
420
421 // add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
422 // pos = {x=num, y=num, z=num}
l_add_item(lua_State * L)423 int ModApiEnvMod::l_add_item(lua_State *L)
424 {
425 GET_ENV_PTR;
426
427 // pos
428 //v3f pos = checkFloatPos(L, 1);
429 // item
430 ItemStack item = read_item(L, 2,getServer(L));
431 if(item.empty() || !item.isKnown(getServer(L)->idef()))
432 return 0;
433
434 lua_pushcfunction(L, script_error_handler);
435 int errorhandler = lua_gettop(L);
436
437 // Use spawn_item to spawn a __builtin:item
438 lua_getglobal(L, "core");
439 lua_getfield(L, -1, "spawn_item");
440 lua_remove(L, -2); // Remove core
441 if(lua_isnil(L, -1))
442 return 0;
443 lua_pushvalue(L, 1);
444 lua_pushstring(L, item.getItemString().c_str());
445 if(lua_pcall(L, 2, 1, errorhandler))
446 script_error(L);
447 lua_remove(L, errorhandler); // Remove error handler
448 return 1;
449 /*lua_pushvalue(L, 1);
450 lua_pushstring(L, "__builtin:item");
451 lua_pushstring(L, item.getItemString().c_str());
452 return l_add_entity(L);*/
453 /*// Do it
454 ServerActiveObject *obj = createItemSAO(env, pos, item.getItemString());
455 int objectid = env->addActiveObject(obj);
456 // If failed to add, return nothing (reads as nil)
457 if(objectid == 0)
458 return 0;
459 // Return ObjectRef
460 objectrefGetOrCreate(L, obj);
461 return 1;*/
462 }
463
464 // get_player_by_name(name)
l_get_player_by_name(lua_State * L)465 int ModApiEnvMod::l_get_player_by_name(lua_State *L)
466 {
467 GET_ENV_PTR;
468
469 // Do it
470 const char *name = luaL_checkstring(L, 1);
471 Player *player = env->getPlayer(name);
472 if(player == NULL){
473 lua_pushnil(L);
474 return 1;
475 }
476 PlayerSAO *sao = player->getPlayerSAO();
477 if(sao == NULL){
478 lua_pushnil(L);
479 return 1;
480 }
481 // Put player on stack
482 getScriptApiBase(L)->objectrefGetOrCreate(L, sao);
483 return 1;
484 }
485
486 // get_objects_inside_radius(pos, radius)
l_get_objects_inside_radius(lua_State * L)487 int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
488 {
489 GET_ENV_PTR;
490
491 // Do it
492 v3f pos = checkFloatPos(L, 1);
493 float radius = luaL_checknumber(L, 2) * BS;
494 std::set<u16> ids = env->getObjectsInsideRadius(pos, radius);
495 ScriptApiBase *script = getScriptApiBase(L);
496 lua_createtable(L, ids.size(), 0);
497 std::set<u16>::const_iterator iter = ids.begin();
498 for(u32 i = 0; iter != ids.end(); iter++) {
499 ServerActiveObject *obj = env->getActiveObject(*iter);
500 // Insert object reference into table
501 script->objectrefGetOrCreate(L, obj);
502 lua_rawseti(L, -2, ++i);
503 }
504 return 1;
505 }
506
507 // set_timeofday(val)
508 // val = 0...1
l_set_timeofday(lua_State * L)509 int ModApiEnvMod::l_set_timeofday(lua_State *L)
510 {
511 GET_ENV_PTR;
512
513 // Do it
514 float timeofday_f = luaL_checknumber(L, 1);
515 assert(timeofday_f >= 0.0 && timeofday_f <= 1.0);
516 int timeofday_mh = (int)(timeofday_f * 24000.0);
517 // This should be set directly in the environment but currently
518 // such changes aren't immediately sent to the clients, so call
519 // the server instead.
520 //env->setTimeOfDay(timeofday_mh);
521 getServer(L)->setTimeOfDay(timeofday_mh);
522 return 0;
523 }
524
525 // get_timeofday() -> 0...1
l_get_timeofday(lua_State * L)526 int ModApiEnvMod::l_get_timeofday(lua_State *L)
527 {
528 GET_ENV_PTR;
529
530 // Do it
531 int timeofday_mh = env->getTimeOfDay();
532 float timeofday_f = (float)timeofday_mh / 24000.0;
533 lua_pushnumber(L, timeofday_f);
534 return 1;
535 }
536
537 // get_gametime()
l_get_gametime(lua_State * L)538 int ModApiEnvMod::l_get_gametime(lua_State *L)
539 {
540 GET_ENV_PTR;
541
542 int game_time = env->getGameTime();
543 lua_pushnumber(L, game_time);
544 return 1;
545 }
546
547
548 // find_node_near(pos, radius, nodenames) -> pos or nil
549 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
l_find_node_near(lua_State * L)550 int ModApiEnvMod::l_find_node_near(lua_State *L)
551 {
552 GET_ENV_PTR;
553
554 INodeDefManager *ndef = getServer(L)->ndef();
555 v3s16 pos = read_v3s16(L, 1);
556 int radius = luaL_checkinteger(L, 2);
557 std::unordered_set<content_t> filter;
558 if(lua_istable(L, 3)){
559 int table = 3;
560 lua_pushnil(L);
561 while(lua_next(L, table) != 0){
562 // key at index -2 and value at index -1
563 luaL_checktype(L, -1, LUA_TSTRING);
564 ndef->getIds(lua_tostring(L, -1), filter);
565 // removes value, keeps key for next iteration
566 lua_pop(L, 1);
567 }
568 } else if(lua_isstring(L, 3)){
569 ndef->getIds(lua_tostring(L, 3), filter);
570 }
571
572 for(int d=1; d<=radius; d++){
573 std::list<v3s16> list;
574 getFacePositions(list, d);
575 for(std::list<v3s16>::iterator i = list.begin();
576 i != list.end(); ++i){
577 v3s16 p = pos + (*i);
578 content_t c = env->getMap().getNodeNoEx(p).getContent();
579 if(filter.count(c) != 0){
580 push_v3s16(L, p);
581 return 1;
582 }
583 }
584 }
585 return 0;
586 }
587
588 // find_nodes_in_area(minp, maxp, nodenames) -> list of positions
589 // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
l_find_nodes_in_area(lua_State * L)590 int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
591 {
592 GET_ENV_PTR;
593
594 INodeDefManager *ndef = getServer(L)->ndef();
595 v3s16 minp = read_v3s16(L, 1);
596 v3s16 maxp = read_v3s16(L, 2);
597 std::unordered_set<content_t> filter;
598 if(lua_istable(L, 3)){
599 int table = 3;
600 lua_pushnil(L);
601 while(lua_next(L, table) != 0){
602 // key at index -2 and value at index -1
603 luaL_checktype(L, -1, LUA_TSTRING);
604 ndef->getIds(lua_tostring(L, -1), filter);
605 // removes value, keeps key for next iteration
606 lua_pop(L, 1);
607 }
608 } else if(lua_isstring(L, 3)){
609 ndef->getIds(lua_tostring(L, 3), filter);
610 }
611
612 lua_newtable(L);
613 u64 i = 0;
614 for(s16 x = minp.X; x <= maxp.X; x++)
615 for(s16 y = minp.Y; y <= maxp.Y; y++)
616 for(s16 z = minp.Z; z <= maxp.Z; z++) {
617 v3s16 p(x, y, z);
618 content_t c = env->getMap().getNodeNoEx(p).getContent();
619 if(filter.count(c) != 0) {
620 push_v3s16(L, p);
621 lua_rawseti(L, -2, ++i);
622 }
623 }
624 return 1;
625 }
626
627 // get_perlin(seeddiff, octaves, persistence, scale)
628 // returns world-specific PerlinNoise
l_get_perlin(lua_State * L)629 int ModApiEnvMod::l_get_perlin(lua_State *L)
630 {
631 GET_ENV_PTR;
632
633 int seeddiff = luaL_checkint(L, 1);
634 int octaves = luaL_checkint(L, 2);
635 float persistence = luaL_checknumber(L, 3);
636 float scale = luaL_checknumber(L, 4);
637
638 LuaPerlinNoise *n = new LuaPerlinNoise(seeddiff + int(env->getServerMap().getSeed()), octaves, persistence, scale);
639 *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
640 luaL_getmetatable(L, "PerlinNoise");
641 lua_setmetatable(L, -2);
642 return 1;
643 }
644
645 // get_perlin_map(noiseparams, size)
646 // returns world-specific PerlinNoiseMap
l_get_perlin_map(lua_State * L)647 int ModApiEnvMod::l_get_perlin_map(lua_State *L)
648 {
649 GET_ENV_PTR;
650
651 NoiseParams *np = read_noiseparams(L, 1);
652 if (!np)
653 return 0;
654 v3s16 size = read_v3s16(L, 2);
655
656 int seed = (int)(env->getServerMap().getSeed());
657 LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(np, seed, size);
658 *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
659 luaL_getmetatable(L, "PerlinNoiseMap");
660 lua_setmetatable(L, -2);
661 return 1;
662 }
663
664 // get_voxel_manip()
665 // returns voxel manipulator
l_get_voxel_manip(lua_State * L)666 int ModApiEnvMod::l_get_voxel_manip(lua_State *L)
667 {
668 GET_ENV_PTR;
669
670 Map *map = &(env->getMap());
671 LuaVoxelManip *o = new LuaVoxelManip(map);
672
673 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
674 luaL_getmetatable(L, "VoxelManip");
675 lua_setmetatable(L, -2);
676 return 1;
677 }
678
679 // clear_objects()
680 // clear all objects in the environment
l_clear_objects(lua_State * L)681 int ModApiEnvMod::l_clear_objects(lua_State *L)
682 {
683 GET_ENV_PTR;
684
685 env->clearAllObjects();
686 return 0;
687 }
688
689 // line_of_sight(pos1, pos2, stepsize) -> true/false, pos
l_line_of_sight(lua_State * L)690 int ModApiEnvMod::l_line_of_sight(lua_State *L) {
691 float stepsize = 1.0;
692
693 GET_ENV_PTR;
694
695 // read position 1 from lua
696 v3f pos1 = checkFloatPos(L, 1);
697 // read position 2 from lua
698 v3f pos2 = checkFloatPos(L, 2);
699 //read step size from lua
700 if (lua_isnumber(L, 3)) {
701 stepsize = lua_tonumber(L, 3);
702 }
703
704 v3s16 p;
705 bool success = env->line_of_sight(pos1, pos2, stepsize, &p);
706 lua_pushboolean(L, success);
707 if (!success) {
708 push_v3s16(L, p);
709 return 2;
710 }
711 return 1;
712 }
713
714 // find_path(pos1, pos2, searchdistance,
715 // max_jump, max_drop, algorithm) -> table containing path
l_find_path(lua_State * L)716 int ModApiEnvMod::l_find_path(lua_State *L)
717 {
718 GET_ENV_PTR;
719
720 v3s16 pos1 = read_v3s16(L, 1);
721 v3s16 pos2 = read_v3s16(L, 2);
722 unsigned int searchdistance = luaL_checkint(L, 3);
723 unsigned int max_jump = luaL_checkint(L, 4);
724 unsigned int max_drop = luaL_checkint(L, 5);
725 Algorithm algo = A_STAR;
726 if (!lua_isnil(L, 6)) {
727 std::string algorithm = luaL_checkstring(L,6);
728 }
729
730 std::vector<v3s16> path =
731 getPath(env, pos1, pos2, searchdistance,
732 max_jump, max_drop, algo, ADJACENCY_4);
733
734 if (path.size() > 0)
735 {
736 lua_newtable(L);
737 int top = lua_gettop(L);
738 unsigned int index = 1;
739 for (std::vector<v3s16>::iterator i = path.begin(); i != path.end();i++)
740 {
741 lua_pushnumber(L,index);
742 push_v3s16(L, *i);
743 lua_settable(L, top);
744 index++;
745 }
746 return 1;
747 }
748
749 return 0;
750 }
751
752 // get_surface(basepos,yoffset,walkable_only=false)
l_get_surface(lua_State * L)753 int ModApiEnvMod::l_get_surface(lua_State *L)
754 {
755 GET_ENV_PTR;
756
757 v3s16 basepos = read_v3s16(L, 1);
758 int max_y = luaL_checkint(L, 2);
759 bool walkable_only = false;
760
761 if (!lua_isnil(L,3)) {
762 walkable_only = lua_toboolean(L, -1);
763 }
764
765 int result = env->getMap().getSurface(basepos,max_y,walkable_only);
766
767 if (result >= basepos.Y) {
768 lua_pushnumber(L,result);
769 return 1;
770 }
771
772 lua_pushnil(L);
773 return 1;
774 }
775
776 // spawn_tree(pos, treedef)
l_spawn_tree(lua_State * L)777 int ModApiEnvMod::l_spawn_tree(lua_State *L)
778 {
779 GET_ENV_PTR;
780
781 v3s16 p0 = read_v3s16(L, 1);
782
783 treegen::TreeDef tree_def;
784 std::string trunk,leaves,fruit;
785 INodeDefManager *ndef = env->getGameDef()->ndef();
786
787 if(lua_istable(L, 2))
788 {
789 getstringfield(L, 2, "axiom", tree_def.initial_axiom);
790 getstringfield(L, 2, "rules_a", tree_def.rules_a);
791 getstringfield(L, 2, "rules_b", tree_def.rules_b);
792 getstringfield(L, 2, "rules_c", tree_def.rules_c);
793 getstringfield(L, 2, "rules_d", tree_def.rules_d);
794 getstringfield(L, 2, "trunk", trunk);
795 tree_def.trunknode=ndef->getId(trunk);
796 getstringfield(L, 2, "leaves", leaves);
797 tree_def.leavesnode=ndef->getId(leaves);
798 tree_def.leaves2_chance=0;
799 getstringfield(L, 2, "leaves2", leaves);
800 if (leaves !="")
801 {
802 tree_def.leaves2node=ndef->getId(leaves);
803 getintfield(L, 2, "leaves2_chance", tree_def.leaves2_chance);
804 }
805 getintfield(L, 2, "angle", tree_def.angle);
806 getintfield(L, 2, "iterations", tree_def.iterations);
807 if (!getintfield(L, 2, "random_level", tree_def.iterations_random_level))
808 tree_def.iterations_random_level = 0;
809 getstringfield(L, 2, "trunk_type", tree_def.trunk_type);
810 getboolfield(L, 2, "thin_branches", tree_def.thin_branches);
811 tree_def.fruit_chance=0;
812 getstringfield(L, 2, "fruit", fruit);
813 if (fruit != "")
814 {
815 tree_def.fruitnode=ndef->getId(fruit);
816 getintfield(L, 2, "fruit_chance",tree_def.fruit_chance);
817 }
818 tree_def.explicit_seed = getintfield(L, 2, "seed", tree_def.seed);
819 }
820 else
821 return 0;
822
823 treegen::error e;
824 if ((e = treegen::spawn_ltree (env, p0, ndef, tree_def)) != treegen::SUCCESS) {
825 if (e == treegen::UNBALANCED_BRACKETS) {
826 luaL_error(L, "spawn_tree(): closing ']' has no matching opening bracket");
827 } else {
828 luaL_error(L, "spawn_tree(): unknown error");
829 }
830 }
831
832 return 1;
833 }
834
835 // transforming_liquid_add(pos)
l_transforming_liquid_add(lua_State * L)836 int ModApiEnvMod::l_transforming_liquid_add(lua_State *L)
837 {
838 GET_ENV_PTR;
839
840 auto pos = read_v3POS(L, 1);
841 env->getMap().transforming_liquid_push_back(pos);
842 return 1;
843 }
844
845 // freeminer.get_heat(pos)
846 // pos = {x=num, y=num, z=num}
l_get_heat(lua_State * L)847 int ModApiEnvMod::l_get_heat(lua_State *L)
848 {
849 GET_ENV_PTR;
850
851 auto pos = read_v3POS(L, 1);
852 lua_pushnumber(L, env->getServerMap().updateBlockHeat(env, pos));
853 return 1;
854 }
855
856 // freeminer.get_humidity(pos)
857 // pos = {x=num, y=num, z=num}
l_get_humidity(lua_State * L)858 int ModApiEnvMod::l_get_humidity(lua_State *L)
859 {
860 GET_ENV_PTR;
861
862 auto pos = read_v3POS(L, 1);
863 lua_pushnumber(L, env->getServerMap().updateBlockHumidity(env, pos));
864 return 1;
865 }
866
867 // forceload_block(blockpos)
868 // blockpos = {x=num, y=num, z=num}
l_forceload_block(lua_State * L)869 int ModApiEnvMod::l_forceload_block(lua_State *L)
870 {
871 GET_ENV_PTR;
872
873 v3s16 blockpos = read_v3s16(L, 1);
874 env->getForceloadedBlocks()->insert(blockpos);
875 return 0;
876 }
877
878 // forceload_free_block(blockpos)
879 // blockpos = {x=num, y=num, z=num}
l_forceload_free_block(lua_State * L)880 int ModApiEnvMod::l_forceload_free_block(lua_State *L)
881 {
882 GET_ENV_PTR;
883
884 v3s16 blockpos = read_v3s16(L, 1);
885 env->getForceloadedBlocks()->erase(blockpos);
886 return 0;
887 }
888
889 // get_us_time()
l_get_us_time(lua_State * L)890 int ModApiEnvMod::l_get_us_time(lua_State *L)
891 {
892 lua_pushnumber(L, porting::getTimeUs());
893 return 1;
894 }
895
Initialize(lua_State * L,int top)896 void ModApiEnvMod::Initialize(lua_State *L, int top)
897 {
898 API_FCT(set_node);
899 API_FCT(add_node);
900 API_FCT(swap_node);
901 API_FCT(add_item);
902 API_FCT(remove_node);
903 API_FCT(get_node);
904 API_FCT(get_node_or_nil);
905 API_FCT(get_node_light);
906 API_FCT(place_node);
907 API_FCT(dig_node);
908 API_FCT(punch_node);
909 API_FCT(get_node_max_level);
910 API_FCT(get_node_level);
911 API_FCT(set_node_level);
912 API_FCT(add_node_level);
913 API_FCT(freeze_melt);
914 API_FCT(add_entity);
915 API_FCT(get_meta);
916 API_FCT(get_node_timer);
917 API_FCT(get_player_by_name);
918 API_FCT(get_objects_inside_radius);
919 API_FCT(set_timeofday);
920 API_FCT(get_timeofday);
921 API_FCT(get_gametime);
922 API_FCT(find_node_near);
923 API_FCT(find_nodes_in_area);
924 API_FCT(get_perlin);
925 API_FCT(get_perlin_map);
926 API_FCT(get_voxel_manip);
927 API_FCT(clear_objects);
928 API_FCT(spawn_tree);
929 API_FCT(find_path);
930 API_FCT(line_of_sight);
931 API_FCT(transforming_liquid_add);
932 API_FCT(get_heat);
933 API_FCT(get_humidity);
934 API_FCT(get_surface);
935 API_FCT(forceload_block);
936 API_FCT(forceload_free_block);
937 API_FCT(get_us_time);
938 }
939