1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477 |
- /*
- Minetest
- Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
- 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 <algorithm>
- #include "lua_api/l_env.h"
- #include "lua_api/l_internal.h"
- #include "lua_api/l_nodemeta.h"
- #include "lua_api/l_nodetimer.h"
- #include "lua_api/l_noise.h"
- #include "lua_api/l_vmanip.h"
- #include "common/c_converter.h"
- #include "common/c_content.h"
- #include "scripting_server.h"
- #include "environment.h"
- #include "mapblock.h"
- #include "server.h"
- #include "nodedef.h"
- #include "daynightratio.h"
- #include "util/pointedthing.h"
- #include "mapgen/treegen.h"
- #include "emerge.h"
- #include "pathfinder.h"
- #include "face_position_cache.h"
- #include "remoteplayer.h"
- #include "server/luaentity_sao.h"
- #include "server/player_sao.h"
- #include "util/string.h"
- #include "translation.h"
- #ifndef SERVER
- #include "client/client.h"
- #endif
- const EnumString ModApiEnvMod::es_ClearObjectsMode[] =
- {
- {CLEAR_OBJECTS_MODE_FULL, "full"},
- {CLEAR_OBJECTS_MODE_QUICK, "quick"},
- {0, NULL},
- };
- const EnumString ModApiEnvMod::es_BlockStatusType[] =
- {
- {ServerEnvironment::BS_UNKNOWN, "unknown"},
- {ServerEnvironment::BS_EMERGING, "emerging"},
- {ServerEnvironment::BS_LOADED, "loaded"},
- {ServerEnvironment::BS_ACTIVE, "active"},
- {0, NULL},
- };
- ///////////////////////////////////////////////////////////////////////////////
- void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
- u32 active_object_count, u32 active_object_count_wider)
- {
- ServerScripting *scriptIface = env->getScriptIface();
- scriptIface->realityCheck();
- lua_State *L = scriptIface->getStack();
- sanity_check(lua_checkstack(L, 20));
- StackUnroller stack_unroller(L);
- int error_handler = PUSH_ERROR_HANDLER(L);
- // Get registered_abms
- lua_getglobal(L, "core");
- lua_getfield(L, -1, "registered_abms");
- luaL_checktype(L, -1, LUA_TTABLE);
- lua_remove(L, -2); // Remove core
- // Get registered_abms[m_id]
- lua_pushinteger(L, m_id);
- lua_gettable(L, -2);
- if(lua_isnil(L, -1))
- FATAL_ERROR("");
- lua_remove(L, -2); // Remove registered_abms
- scriptIface->setOriginFromTable(-1);
- // Call action
- luaL_checktype(L, -1, LUA_TTABLE);
- lua_getfield(L, -1, "action");
- luaL_checktype(L, -1, LUA_TFUNCTION);
- lua_remove(L, -2); // Remove registered_abms[m_id]
- push_v3s16(L, p);
- pushnode(L, n);
- lua_pushnumber(L, active_object_count);
- lua_pushnumber(L, active_object_count_wider);
- int result = lua_pcall(L, 4, 0, error_handler);
- if (result)
- scriptIface->scriptError(result, "LuaABM::trigger");
- lua_pop(L, 1); // Pop error handler
- }
- void LuaLBM::trigger(ServerEnvironment *env, v3s16 p, MapNode n)
- {
- ServerScripting *scriptIface = env->getScriptIface();
- scriptIface->realityCheck();
- lua_State *L = scriptIface->getStack();
- sanity_check(lua_checkstack(L, 20));
- StackUnroller stack_unroller(L);
- int error_handler = PUSH_ERROR_HANDLER(L);
- // Get registered_lbms
- lua_getglobal(L, "core");
- lua_getfield(L, -1, "registered_lbms");
- luaL_checktype(L, -1, LUA_TTABLE);
- lua_remove(L, -2); // Remove core
- // Get registered_lbms[m_id]
- lua_pushinteger(L, m_id);
- lua_gettable(L, -2);
- FATAL_ERROR_IF(lua_isnil(L, -1), "Entry with given id not found in registered_lbms table");
- lua_remove(L, -2); // Remove registered_lbms
- scriptIface->setOriginFromTable(-1);
- // Call action
- luaL_checktype(L, -1, LUA_TTABLE);
- lua_getfield(L, -1, "action");
- luaL_checktype(L, -1, LUA_TFUNCTION);
- lua_remove(L, -2); // Remove registered_lbms[m_id]
- push_v3s16(L, p);
- pushnode(L, n);
- int result = lua_pcall(L, 2, 0, error_handler);
- if (result)
- scriptIface->scriptError(result, "LuaLBM::trigger");
- lua_pop(L, 1); // Pop error handler
- }
- int LuaRaycast::l_next(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- bool csm = false;
- #ifndef SERVER
- csm = getClient(L) != nullptr;
- #endif
- LuaRaycast *o = checkObject<LuaRaycast>(L, 1);
- PointedThing pointed;
- env->continueRaycast(&o->state, &pointed);
- if (pointed.type == POINTEDTHING_NOTHING)
- lua_pushnil(L);
- else
- push_pointed_thing(L, pointed, csm, true);
- return 1;
- }
- int LuaRaycast::create_object(lua_State *L)
- {
- NO_MAP_LOCK_REQUIRED;
- bool objects = true;
- bool liquids = false;
- v3f pos1 = checkFloatPos(L, 1);
- v3f pos2 = checkFloatPos(L, 2);
- if (lua_isboolean(L, 3)) {
- objects = readParam<bool>(L, 3);
- }
- if (lua_isboolean(L, 4)) {
- liquids = readParam<bool>(L, 4);
- }
- LuaRaycast *o = new LuaRaycast(core::line3d<f32>(pos1, pos2),
- objects, liquids);
- *(void **) (lua_newuserdata(L, sizeof(void *))) = o;
- luaL_getmetatable(L, className);
- lua_setmetatable(L, -2);
- return 1;
- }
- int LuaRaycast::gc_object(lua_State *L)
- {
- LuaRaycast *o = *(LuaRaycast **) (lua_touserdata(L, 1));
- delete o;
- return 0;
- }
- void LuaRaycast::Register(lua_State *L)
- {
- static const luaL_Reg metamethods[] = {
- {"__call", l_next},
- {"__gc", gc_object},
- {0, 0}
- };
- registerClass(L, className, methods, metamethods);
- lua_register(L, className, create_object);
- }
- const char LuaRaycast::className[] = "Raycast";
- const luaL_Reg LuaRaycast::methods[] =
- {
- luamethod(LuaRaycast, next),
- { 0, 0 }
- };
- void LuaEmergeAreaCallback(v3s16 blockpos, EmergeAction action, void *param)
- {
- ScriptCallbackState *state = (ScriptCallbackState *)param;
- assert(state != NULL);
- assert(state->script != NULL);
- assert(state->refcount > 0);
- // state must be protected by envlock
- Server *server = state->script->getServer();
- MutexAutoLock envlock(server->m_env_mutex);
- state->refcount--;
- state->script->on_emerge_area_completion(blockpos, action, state);
- if (state->refcount == 0)
- delete state;
- }
- // Exported functions
- // set_node(pos, node)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_set_node(lua_State *L)
- {
- GET_ENV_PTR;
- // parameters
- v3s16 pos = read_v3s16(L, 1);
- MapNode n = readnode(L, 2);
- // Do it
- bool succeeded = env->setNode(pos, n);
- lua_pushboolean(L, succeeded);
- return 1;
- }
- // bulk_set_node([pos1, pos2, ...], node)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_bulk_set_node(lua_State *L)
- {
- GET_ENV_PTR;
- // parameters
- if (!lua_istable(L, 1)) {
- return 0;
- }
- s32 len = lua_objlen(L, 1);
- if (len == 0) {
- lua_pushboolean(L, true);
- return 1;
- }
- MapNode n = readnode(L, 2);
- // Do it
- bool succeeded = true;
- for (s32 i = 1; i <= len; i++) {
- lua_rawgeti(L, 1, i);
- if (!env->setNode(read_v3s16(L, -1), n))
- succeeded = false;
- lua_pop(L, 1);
- }
- lua_pushboolean(L, succeeded);
- return 1;
- }
- int ModApiEnvMod::l_add_node(lua_State *L)
- {
- return l_set_node(L);
- }
- // remove_node(pos)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_remove_node(lua_State *L)
- {
- GET_ENV_PTR;
- // parameters
- v3s16 pos = read_v3s16(L, 1);
- // Do it
- bool succeeded = env->removeNode(pos);
- lua_pushboolean(L, succeeded);
- return 1;
- }
- // swap_node(pos, node)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_swap_node(lua_State *L)
- {
- GET_ENV_PTR;
- // parameters
- v3s16 pos = read_v3s16(L, 1);
- MapNode n = readnode(L, 2);
- // Do it
- bool succeeded = env->swapNode(pos, n);
- lua_pushboolean(L, succeeded);
- return 1;
- }
- // get_node(pos)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_get_node(lua_State *L)
- {
- GET_ENV_PTR;
- // pos
- v3s16 pos = read_v3s16(L, 1);
- // Do it
- MapNode n = env->getMap().getNode(pos);
- // Return node
- pushnode(L, n);
- return 1;
- }
- // get_node_or_nil(pos)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_get_node_or_nil(lua_State *L)
- {
- GET_ENV_PTR;
- // pos
- v3s16 pos = read_v3s16(L, 1);
- // Do it
- bool pos_ok;
- MapNode n = env->getMap().getNode(pos, &pos_ok);
- if (pos_ok) {
- // Return node
- pushnode(L, n);
- } else {
- lua_pushnil(L);
- }
- return 1;
- }
- // get_node_light(pos, timeofday)
- // pos = {x=num, y=num, z=num}
- // timeofday: nil = current time, 0 = night, 0.5 = day
- int ModApiEnvMod::l_get_node_light(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- // Do it
- v3s16 pos = read_v3s16(L, 1);
- u32 time_of_day = env->getTimeOfDay();
- if(lua_isnumber(L, 2))
- time_of_day = 24000.0 * lua_tonumber(L, 2);
- time_of_day %= 24000;
- u32 dnr = time_to_daynight_ratio(time_of_day, true);
- bool is_position_ok;
- MapNode n = env->getMap().getNode(pos, &is_position_ok);
- if (is_position_ok) {
- const NodeDefManager *ndef = env->getGameDef()->ndef();
- lua_pushinteger(L, n.getLightBlend(dnr, ndef->getLightingFlags(n)));
- } else {
- lua_pushnil(L);
- }
- return 1;
- }
- // get_natural_light(pos, timeofday)
- // pos = {x=num, y=num, z=num}
- // timeofday: nil = current time, 0 = night, 0.5 = day
- int ModApiEnvMod::l_get_natural_light(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 pos = read_v3s16(L, 1);
- bool is_position_ok;
- MapNode n = env->getMap().getNode(pos, &is_position_ok);
- if (!is_position_ok)
- return 0;
- // If the daylight is 0, nothing needs to be calculated
- u8 daylight = n.param1 & 0x0f;
- if (daylight == 0) {
- lua_pushinteger(L, 0);
- return 1;
- }
- u32 time_of_day;
- if (lua_isnumber(L, 2)) {
- time_of_day = 24000.0 * lua_tonumber(L, 2);
- time_of_day %= 24000;
- } else {
- time_of_day = env->getTimeOfDay();
- }
- u32 dnr = time_to_daynight_ratio(time_of_day, true);
- // If it's the same as the artificial light, the sunlight needs to be
- // searched for because the value may not emanate from the sun
- if (daylight == n.param1 >> 4)
- daylight = env->findSunlight(pos);
- lua_pushinteger(L, dnr * daylight / 1000);
- return 1;
- }
- // place_node(pos, node)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_place_node(lua_State *L)
- {
- GET_ENV_PTR;
- ScriptApiItem *scriptIfaceItem = getScriptApi<ScriptApiItem>(L);
- Server *server = getServer(L);
- const NodeDefManager *ndef = server->ndef();
- IItemDefManager *idef = server->idef();
- v3s16 pos = read_v3s16(L, 1);
- MapNode n = readnode(L, 2);
- // Don't attempt to load non-loaded area as of now
- MapNode n_old = env->getMap().getNode(pos);
- if(n_old.getContent() == CONTENT_IGNORE){
- lua_pushboolean(L, false);
- return 1;
- }
- // Create item to place
- Optional<ItemStack> item = ItemStack(ndef->get(n).name, 1, 0, idef);
- // Make pointed position
- PointedThing pointed;
- pointed.type = POINTEDTHING_NODE;
- pointed.node_abovesurface = pos;
- pointed.node_undersurface = pos + v3s16(0,-1,0);
- // Place it with a NULL placer (appears in Lua as nil)
- bool success = scriptIfaceItem->item_OnPlace(item, nullptr, pointed);
- lua_pushboolean(L, success);
- return 1;
- }
- // dig_node(pos)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_dig_node(lua_State *L)
- {
- GET_ENV_PTR;
- ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
- v3s16 pos = read_v3s16(L, 1);
- // Don't attempt to load non-loaded area as of now
- MapNode n = env->getMap().getNode(pos);
- if(n.getContent() == CONTENT_IGNORE){
- lua_pushboolean(L, false);
- return 1;
- }
- // Dig it out with a NULL digger (appears in Lua as a
- // non-functional ObjectRef)
- bool success = scriptIfaceNode->node_on_dig(pos, n, NULL);
- lua_pushboolean(L, success);
- return 1;
- }
- // punch_node(pos)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_punch_node(lua_State *L)
- {
- GET_ENV_PTR;
- ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
- v3s16 pos = read_v3s16(L, 1);
- // Don't attempt to load non-loaded area as of now
- MapNode n = env->getMap().getNode(pos);
- if(n.getContent() == CONTENT_IGNORE){
- lua_pushboolean(L, false);
- return 1;
- }
- // Punch it with a NULL puncher (appears in Lua as a non-functional
- // ObjectRef)
- bool success = scriptIfaceNode->node_on_punch(pos, n, NULL, PointedThing());
- lua_pushboolean(L, success);
- return 1;
- }
- // get_node_max_level(pos)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_get_node_max_level(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- v3s16 pos = read_v3s16(L, 1);
- MapNode n = env->getMap().getNode(pos);
- lua_pushnumber(L, n.getMaxLevel(env->getGameDef()->ndef()));
- return 1;
- }
- // get_node_level(pos)
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_get_node_level(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- v3s16 pos = read_v3s16(L, 1);
- MapNode n = env->getMap().getNode(pos);
- lua_pushnumber(L, n.getLevel(env->getGameDef()->ndef()));
- return 1;
- }
- // set_node_level(pos, level)
- // pos = {x=num, y=num, z=num}
- // level: 0..63
- int ModApiEnvMod::l_set_node_level(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 pos = read_v3s16(L, 1);
- u8 level = 1;
- if(lua_isnumber(L, 2))
- level = lua_tonumber(L, 2);
- MapNode n = env->getMap().getNode(pos);
- lua_pushnumber(L, n.setLevel(env->getGameDef()->ndef(), level));
- env->setNode(pos, n);
- return 1;
- }
- // add_node_level(pos, level)
- // pos = {x=num, y=num, z=num}
- // level: -127..127
- int ModApiEnvMod::l_add_node_level(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 pos = read_v3s16(L, 1);
- s16 level = 1;
- if(lua_isnumber(L, 2))
- level = lua_tonumber(L, 2);
- MapNode n = env->getMap().getNode(pos);
- lua_pushnumber(L, n.addLevel(env->getGameDef()->ndef(), level));
- env->setNode(pos, n);
- return 1;
- }
- // find_nodes_with_meta(pos1, pos2)
- int ModApiEnvMod::l_find_nodes_with_meta(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- std::vector<v3s16> positions = env->getMap().findNodesWithMetadata(
- check_v3s16(L, 1), check_v3s16(L, 2));
- lua_createtable(L, positions.size(), 0);
- for (size_t i = 0; i != positions.size(); i++) {
- push_v3s16(L, positions[i]);
- lua_rawseti(L, -2, i + 1);
- }
- return 1;
- }
- // get_meta(pos)
- int ModApiEnvMod::l_get_meta(lua_State *L)
- {
- GET_ENV_PTR;
- // Do it
- v3s16 p = read_v3s16(L, 1);
- NodeMetaRef::create(L, p, env);
- return 1;
- }
- // get_node_timer(pos)
- int ModApiEnvMod::l_get_node_timer(lua_State *L)
- {
- GET_ENV_PTR;
- // Do it
- v3s16 p = read_v3s16(L, 1);
- NodeTimerRef::create(L, p, &env->getServerMap());
- return 1;
- }
- // add_entity(pos, entityname, [staticdata]) -> ObjectRef or nil
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_add_entity(lua_State *L)
- {
- GET_ENV_PTR;
- v3f pos = checkFloatPos(L, 1);
- const char *name = luaL_checkstring(L, 2);
- std::string staticdata = readParam<std::string>(L, 3, "");
- ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, staticdata);
- int objectid = env->addActiveObject(obj);
- // If failed to add, return nothing (reads as nil)
- if(objectid == 0)
- return 0;
- // If already deleted (can happen in on_activate), return nil
- if (obj->isGone())
- return 0;
- getScriptApiBase(L)->objectrefGetOrCreate(L, obj);
- return 1;
- }
- // add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
- // pos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_add_item(lua_State *L)
- {
- GET_ENV_PTR;
- // pos
- //v3f pos = checkFloatPos(L, 1);
- // item
- ItemStack item = read_item(L, 2,getServer(L)->idef());
- if(item.empty() || !item.isKnown(getServer(L)->idef()))
- return 0;
- int error_handler = PUSH_ERROR_HANDLER(L);
- // Use spawn_item to spawn a __builtin:item
- lua_getglobal(L, "core");
- lua_getfield(L, -1, "spawn_item");
- lua_remove(L, -2); // Remove core
- if(lua_isnil(L, -1))
- return 0;
- lua_pushvalue(L, 1);
- lua_pushstring(L, item.getItemString().c_str());
- PCALL_RESL(L, lua_pcall(L, 2, 1, error_handler));
- lua_remove(L, error_handler);
- return 1;
- }
- // get_connected_players()
- int ModApiEnvMod::l_get_connected_players(lua_State *L)
- {
- ServerEnvironment *env = (ServerEnvironment *) getEnv(L);
- if (!env) {
- log_deprecated(L, "Calling get_connected_players() at mod load time"
- " is deprecated");
- lua_createtable(L, 0, 0);
- return 1;
- }
- lua_createtable(L, env->getPlayerCount(), 0);
- u32 i = 0;
- for (RemotePlayer *player : env->getPlayers()) {
- if (player->getPeerId() == PEER_ID_INEXISTENT)
- continue;
- PlayerSAO *sao = player->getPlayerSAO();
- if (sao && !sao->isGone()) {
- getScriptApiBase(L)->objectrefGetOrCreate(L, sao);
- lua_rawseti(L, -2, ++i);
- }
- }
- return 1;
- }
- // get_player_by_name(name)
- int ModApiEnvMod::l_get_player_by_name(lua_State *L)
- {
- GET_ENV_PTR;
- // Do it
- const char *name = luaL_checkstring(L, 1);
- RemotePlayer *player = env->getPlayer(name);
- if (!player || player->getPeerId() == PEER_ID_INEXISTENT)
- return 0;
- PlayerSAO *sao = player->getPlayerSAO();
- if (!sao || sao->isGone())
- return 0;
- // Put player on stack
- getScriptApiBase(L)->objectrefGetOrCreate(L, sao);
- return 1;
- }
- // get_objects_inside_radius(pos, radius)
- int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
- {
- GET_ENV_PTR;
- ScriptApiBase *script = getScriptApiBase(L);
- // Do it
- v3f pos = checkFloatPos(L, 1);
- float radius = readParam<float>(L, 2) * BS;
- std::vector<ServerActiveObject *> objs;
- auto include_obj_cb = [](ServerActiveObject *obj){ return !obj->isGone(); };
- env->getObjectsInsideRadius(objs, pos, radius, include_obj_cb);
- int i = 0;
- lua_createtable(L, objs.size(), 0);
- for (const auto obj : objs) {
- // Insert object reference into table
- script->objectrefGetOrCreate(L, obj);
- lua_rawseti(L, -2, ++i);
- }
- return 1;
- }
- // get_objects_in_area(pos, minp, maxp)
- int ModApiEnvMod::l_get_objects_in_area(lua_State *L)
- {
- GET_ENV_PTR;
- ScriptApiBase *script = getScriptApiBase(L);
- v3f minp = read_v3f(L, 1) * BS;
- v3f maxp = read_v3f(L, 2) * BS;
- aabb3f box(minp, maxp);
- box.repair();
- std::vector<ServerActiveObject *> objs;
- auto include_obj_cb = [](ServerActiveObject *obj){ return !obj->isGone(); };
- env->getObjectsInArea(objs, box, include_obj_cb);
- int i = 0;
- lua_createtable(L, objs.size(), 0);
- for (const auto obj : objs) {
- // Insert object reference into table
- script->objectrefGetOrCreate(L, obj);
- lua_rawseti(L, -2, ++i);
- }
- return 1;
- }
- // set_timeofday(val)
- // val = 0...1
- int ModApiEnvMod::l_set_timeofday(lua_State *L)
- {
- GET_ENV_PTR;
- // Do it
- float timeofday_f = readParam<float>(L, 1);
- luaL_argcheck(L, timeofday_f >= 0.0f && timeofday_f <= 1.0f, 1,
- "value must be between 0 and 1");
- int timeofday_mh = (int)(timeofday_f * 24000.0f);
- // This should be set directly in the environment but currently
- // such changes aren't immediately sent to the clients, so call
- // the server instead.
- //env->setTimeOfDay(timeofday_mh);
- getServer(L)->setTimeOfDay(timeofday_mh);
- return 0;
- }
- // get_timeofday() -> 0...1
- int ModApiEnvMod::l_get_timeofday(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- // Do it
- int timeofday_mh = env->getTimeOfDay();
- float timeofday_f = (float)timeofday_mh / 24000.0f;
- lua_pushnumber(L, timeofday_f);
- return 1;
- }
- // get_day_count() -> int
- int ModApiEnvMod::l_get_day_count(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- lua_pushnumber(L, env->getDayCount());
- return 1;
- }
- // get_gametime()
- int ModApiEnvMod::l_get_gametime(lua_State *L)
- {
- GET_ENV_PTR;
- int game_time = env->getGameTime();
- lua_pushnumber(L, game_time);
- return 1;
- }
- void ModApiEnvMod::collectNodeIds(lua_State *L, int idx, const NodeDefManager *ndef,
- std::vector<content_t> &filter)
- {
- if (lua_istable(L, idx)) {
- lua_pushnil(L);
- while (lua_next(L, idx) != 0) {
- // key at index -2 and value at index -1
- luaL_checktype(L, -1, LUA_TSTRING);
- ndef->getIds(readParam<std::string>(L, -1), filter);
- // removes value, keeps key for next iteration
- lua_pop(L, 1);
- }
- } else if (lua_isstring(L, idx)) {
- ndef->getIds(readParam<std::string>(L, 3), filter);
- }
- }
- // find_node_near(pos, radius, nodenames, [search_center]) -> pos or nil
- // nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
- int ModApiEnvMod::l_find_node_near(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- const NodeDefManager *ndef = env->getGameDef()->ndef();
- Map &map = env->getMap();
- v3s16 pos = read_v3s16(L, 1);
- int radius = luaL_checkinteger(L, 2);
- std::vector<content_t> filter;
- collectNodeIds(L, 3, ndef, filter);
- int start_radius = (lua_isboolean(L, 4) && readParam<bool>(L, 4)) ? 0 : 1;
- #ifndef SERVER
- // Client API limitations
- if (Client *client = getClient(L))
- radius = client->CSMClampRadius(pos, radius);
- #endif
- for (int d = start_radius; d <= radius; d++) {
- const std::vector<v3s16> &list = FacePositionCache::getFacePositions(d);
- for (const v3s16 &i : list) {
- v3s16 p = pos + i;
- content_t c = map.getNode(p).getContent();
- if (CONTAINS(filter, c)) {
- push_v3s16(L, p);
- return 1;
- }
- }
- }
- return 0;
- }
- static void checkArea(v3s16 &minp, v3s16 &maxp)
- {
- auto volume = VoxelArea(minp, maxp).getVolume();
- // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
- if (volume > 4096000) {
- throw LuaError("Area volume exceeds allowed value of 4096000");
- }
- // Clamp to map range to avoid problems
- #define CLAMP(arg) core::clamp(arg, (s16)-MAX_MAP_GENERATION_LIMIT, (s16)MAX_MAP_GENERATION_LIMIT)
- minp = v3s16(CLAMP(minp.X), CLAMP(minp.Y), CLAMP(minp.Z));
- maxp = v3s16(CLAMP(maxp.X), CLAMP(maxp.Y), CLAMP(maxp.Z));
- #undef CLAMP
- }
- // find_nodes_in_area(minp, maxp, nodenames, [grouped])
- int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- v3s16 minp = read_v3s16(L, 1);
- v3s16 maxp = read_v3s16(L, 2);
- sortBoxVerticies(minp, maxp);
- const NodeDefManager *ndef = env->getGameDef()->ndef();
- Map &map = env->getMap();
- #ifndef SERVER
- if (Client *client = getClient(L)) {
- minp = client->CSMClampPos(minp);
- maxp = client->CSMClampPos(maxp);
- }
- #endif
- checkArea(minp, maxp);
- std::vector<content_t> filter;
- collectNodeIds(L, 3, ndef, filter);
- bool grouped = lua_isboolean(L, 4) && readParam<bool>(L, 4);
- if (grouped) {
- // create the table we will be returning
- lua_createtable(L, 0, filter.size());
- int base = lua_gettop(L);
- // create one table for each filter
- std::vector<u32> idx;
- idx.resize(filter.size());
- for (u32 i = 0; i < filter.size(); i++)
- lua_newtable(L);
- map.forEachNodeInArea(minp, maxp, [&](v3s16 p, MapNode n) -> bool {
- content_t c = n.getContent();
- auto it = std::find(filter.begin(), filter.end(), c);
- if (it != filter.end()) {
- // Calculate index of the table and append the position
- u32 filt_index = it - filter.begin();
- push_v3s16(L, p);
- lua_rawseti(L, base + 1 + filt_index, ++idx[filt_index]);
- }
- return true;
- });
- // last filter table is at top of stack
- u32 i = filter.size() - 1;
- do {
- if (idx[i] == 0) {
- // No such node found -> drop the empty table
- lua_pop(L, 1);
- } else {
- // This node was found -> put table into the return table
- lua_setfield(L, base, ndef->get(filter[i]).name.c_str());
- }
- } while (i-- != 0);
- assert(lua_gettop(L) == base);
- return 1;
- } else {
- std::vector<u32> individual_count;
- individual_count.resize(filter.size());
- lua_newtable(L);
- u32 i = 0;
- map.forEachNodeInArea(minp, maxp, [&](v3s16 p, MapNode n) -> bool {
- content_t c = n.getContent();
- auto it = std::find(filter.begin(), filter.end(), c);
- if (it != filter.end()) {
- push_v3s16(L, p);
- lua_rawseti(L, -2, ++i);
- u32 filt_index = it - filter.begin();
- individual_count[filt_index]++;
- }
- return true;
- });
- lua_createtable(L, 0, filter.size());
- for (u32 i = 0; i < filter.size(); i++) {
- lua_pushinteger(L, individual_count[i]);
- lua_setfield(L, -2, ndef->get(filter[i]).name.c_str());
- }
- return 2;
- }
- }
- // find_nodes_in_area_under_air(minp, maxp, nodenames) -> list of positions
- // nodenames: e.g. {"ignore", "group:tree"} or "default:dirt"
- int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
- {
- /* Note: A similar but generalized (and therefore slower) version of this
- * function could be created -- e.g. find_nodes_in_area_under -- which
- * would accept a node name (or ID?) or list of names that the "above node"
- * should be.
- * TODO
- */
- GET_PLAIN_ENV_PTR;
- v3s16 minp = read_v3s16(L, 1);
- v3s16 maxp = read_v3s16(L, 2);
- sortBoxVerticies(minp, maxp);
- const NodeDefManager *ndef = env->getGameDef()->ndef();
- Map &map = env->getMap();
- #ifndef SERVER
- if (Client *client = getClient(L)) {
- minp = client->CSMClampPos(minp);
- maxp = client->CSMClampPos(maxp);
- }
- #endif
- checkArea(minp, maxp);
- std::vector<content_t> filter;
- collectNodeIds(L, 3, ndef, filter);
- lua_newtable(L);
- u32 i = 0;
- v3s16 p;
- for (p.X = minp.X; p.X <= maxp.X; p.X++)
- for (p.Z = minp.Z; p.Z <= maxp.Z; p.Z++) {
- p.Y = minp.Y;
- content_t c = map.getNode(p).getContent();
- for (; p.Y <= maxp.Y; p.Y++) {
- v3s16 psurf(p.X, p.Y + 1, p.Z);
- content_t csurf = map.getNode(psurf).getContent();
- if (c != CONTENT_AIR && csurf == CONTENT_AIR &&
- CONTAINS(filter, c)) {
- push_v3s16(L, p);
- lua_rawseti(L, -2, ++i);
- }
- c = csurf;
- }
- }
- return 1;
- }
- // get_perlin(seeddiff, octaves, persistence, scale)
- // returns world-specific PerlinNoise
- int ModApiEnvMod::l_get_perlin(lua_State *L)
- {
- GET_ENV_PTR_NO_MAP_LOCK;
- NoiseParams params;
- if (lua_istable(L, 1)) {
- read_noiseparams(L, 1, ¶ms);
- } else {
- params.seed = luaL_checkint(L, 1);
- params.octaves = luaL_checkint(L, 2);
- params.persist = readParam<float>(L, 3);
- params.spread = v3f(1, 1, 1) * readParam<float>(L, 4);
- }
- params.seed += (int)env->getServerMap().getSeed();
- LuaPerlinNoise *n = new LuaPerlinNoise(¶ms);
- *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
- luaL_getmetatable(L, "PerlinNoise");
- lua_setmetatable(L, -2);
- return 1;
- }
- // get_perlin_map(noiseparams, size)
- // returns world-specific PerlinNoiseMap
- int ModApiEnvMod::l_get_perlin_map(lua_State *L)
- {
- GET_ENV_PTR_NO_MAP_LOCK;
- NoiseParams np;
- if (!read_noiseparams(L, 1, &np))
- return 0;
- v3s16 size = read_v3s16(L, 2);
- s32 seed = (s32)(env->getServerMap().getSeed());
- LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(&np, seed, size);
- *(void **)(lua_newuserdata(L, sizeof(void *))) = n;
- luaL_getmetatable(L, "PerlinNoiseMap");
- lua_setmetatable(L, -2);
- return 1;
- }
- // get_voxel_manip()
- // returns voxel manipulator
- int ModApiEnvMod::l_get_voxel_manip(lua_State *L)
- {
- GET_ENV_PTR;
- Map *map = &(env->getMap());
- LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
- new LuaVoxelManip(map, read_v3s16(L, 1), read_v3s16(L, 2)) :
- new LuaVoxelManip(map);
- *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
- luaL_getmetatable(L, "VoxelManip");
- lua_setmetatable(L, -2);
- return 1;
- }
- // clear_objects([options])
- // clear all objects in the environment
- // where options = {mode = "full" or "quick"}
- int ModApiEnvMod::l_clear_objects(lua_State *L)
- {
- GET_ENV_PTR;
- ClearObjectsMode mode = CLEAR_OBJECTS_MODE_QUICK;
- if (lua_istable(L, 1)) {
- mode = (ClearObjectsMode)getenumfield(L, 1, "mode",
- ModApiEnvMod::es_ClearObjectsMode, mode);
- }
- env->clearObjects(mode);
- return 0;
- }
- // line_of_sight(pos1, pos2) -> true/false, pos
- int ModApiEnvMod::l_line_of_sight(lua_State *L)
- {
- GET_PLAIN_ENV_PTR;
- // read position 1 from lua
- v3f pos1 = checkFloatPos(L, 1);
- // read position 2 from lua
- v3f pos2 = checkFloatPos(L, 2);
- v3s16 p;
- bool success = env->line_of_sight(pos1, pos2, &p);
- lua_pushboolean(L, success);
- if (!success) {
- push_v3s16(L, p);
- return 2;
- }
- return 1;
- }
- // fix_light(p1, p2)
- int ModApiEnvMod::l_fix_light(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 blockpos1 = getContainerPos(read_v3s16(L, 1), MAP_BLOCKSIZE);
- v3s16 blockpos2 = getContainerPos(read_v3s16(L, 2), MAP_BLOCKSIZE);
- ServerMap &map = env->getServerMap();
- std::map<v3s16, MapBlock *> modified_blocks;
- bool success = true;
- v3s16 blockpos;
- for (blockpos.X = blockpos1.X; blockpos.X <= blockpos2.X; blockpos.X++)
- for (blockpos.Y = blockpos1.Y; blockpos.Y <= blockpos2.Y; blockpos.Y++)
- for (blockpos.Z = blockpos1.Z; blockpos.Z <= blockpos2.Z; blockpos.Z++) {
- success = success & map.repairBlockLight(blockpos, &modified_blocks);
- }
- if (!modified_blocks.empty()) {
- MapEditEvent event;
- event.type = MEET_OTHER;
- for (auto &modified_block : modified_blocks)
- event.modified_blocks.insert(modified_block.first);
- map.dispatchEvent(event);
- }
- lua_pushboolean(L, success);
- return 1;
- }
- int ModApiEnvMod::l_raycast(lua_State *L)
- {
- return LuaRaycast::create_object(L);
- }
- // load_area(p1, [p2])
- // load mapblocks in area p1..p2, but do not generate map
- int ModApiEnvMod::l_load_area(lua_State *L)
- {
- GET_ENV_PTR;
- MAP_LOCK_REQUIRED;
- Map *map = &(env->getMap());
- v3s16 bp1 = getNodeBlockPos(check_v3s16(L, 1));
- if (!lua_istable(L, 2)) {
- map->emergeBlock(bp1);
- } else {
- v3s16 bp2 = getNodeBlockPos(check_v3s16(L, 2));
- sortBoxVerticies(bp1, bp2);
- for (s16 z = bp1.Z; z <= bp2.Z; z++)
- for (s16 y = bp1.Y; y <= bp2.Y; y++)
- for (s16 x = bp1.X; x <= bp2.X; x++) {
- map->emergeBlock(v3s16(x, y, z));
- }
- }
- return 0;
- }
- // emerge_area(p1, p2, [callback, context])
- // emerge mapblocks in area p1..p2, calls callback with context upon completion
- int ModApiEnvMod::l_emerge_area(lua_State *L)
- {
- GET_ENV_PTR;
- EmergeCompletionCallback callback = NULL;
- ScriptCallbackState *state = NULL;
- EmergeManager *emerge = getServer(L)->getEmergeManager();
- v3s16 bpmin = getNodeBlockPos(read_v3s16(L, 1));
- v3s16 bpmax = getNodeBlockPos(read_v3s16(L, 2));
- sortBoxVerticies(bpmin, bpmax);
- size_t num_blocks = VoxelArea(bpmin, bpmax).getVolume();
- if (num_blocks == 0)
- return 0;
- if (lua_isfunction(L, 3)) {
- callback = LuaEmergeAreaCallback;
- lua_pushvalue(L, 3);
- int callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
- lua_pushvalue(L, 4);
- int args_ref = luaL_ref(L, LUA_REGISTRYINDEX);
- state = new ScriptCallbackState;
- state->script = getServer(L)->getScriptIface();
- state->callback_ref = callback_ref;
- state->args_ref = args_ref;
- state->refcount = num_blocks;
- state->origin = getScriptApiBase(L)->getOrigin();
- }
- for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
- for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
- for (s16 x = bpmin.X; x <= bpmax.X; x++) {
- emerge->enqueueBlockEmergeEx(v3s16(x, y, z), PEER_ID_INEXISTENT,
- BLOCK_EMERGE_ALLOW_GEN | BLOCK_EMERGE_FORCE_QUEUE, callback, state);
- }
- return 0;
- }
- // delete_area(p1, p2)
- // delete mapblocks in area p1..p2
- int ModApiEnvMod::l_delete_area(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 bpmin = getNodeBlockPos(read_v3s16(L, 1));
- v3s16 bpmax = getNodeBlockPos(read_v3s16(L, 2));
- sortBoxVerticies(bpmin, bpmax);
- ServerMap &map = env->getServerMap();
- MapEditEvent event;
- event.type = MEET_OTHER;
- bool success = true;
- for (s16 z = bpmin.Z; z <= bpmax.Z; z++)
- for (s16 y = bpmin.Y; y <= bpmax.Y; y++)
- for (s16 x = bpmin.X; x <= bpmax.X; x++) {
- v3s16 bp(x, y, z);
- if (map.deleteBlock(bp)) {
- env->setStaticForActiveObjectsInBlock(bp, false);
- event.modified_blocks.insert(bp);
- } else {
- success = false;
- }
- }
- map.dispatchEvent(event);
- lua_pushboolean(L, success);
- return 1;
- }
- // find_path(pos1, pos2, searchdistance,
- // max_jump, max_drop, algorithm) -> table containing path
- int ModApiEnvMod::l_find_path(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 pos1 = read_v3s16(L, 1);
- v3s16 pos2 = read_v3s16(L, 2);
- unsigned int searchdistance = luaL_checkint(L, 3);
- unsigned int max_jump = luaL_checkint(L, 4);
- unsigned int max_drop = luaL_checkint(L, 5);
- PathAlgorithm algo = PA_PLAIN_NP;
- if (!lua_isnoneornil(L, 6)) {
- std::string algorithm = luaL_checkstring(L,6);
- if (algorithm == "A*")
- algo = PA_PLAIN;
- if (algorithm == "Dijkstra")
- algo = PA_DIJKSTRA;
- }
- std::vector<v3s16> path = get_path(&env->getServerMap(), env->getGameDef()->ndef(), pos1, pos2,
- searchdistance, max_jump, max_drop, algo);
- if (!path.empty()) {
- lua_createtable(L, path.size(), 0);
- int top = lua_gettop(L);
- unsigned int index = 1;
- for (const v3s16 &i : path) {
- lua_pushnumber(L,index);
- push_v3s16(L, i);
- lua_settable(L, top);
- index++;
- }
- return 1;
- }
- return 0;
- }
- // spawn_tree(pos, treedef)
- int ModApiEnvMod::l_spawn_tree(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 p0 = read_v3s16(L, 1);
- treegen::TreeDef tree_def;
- std::string trunk,leaves,fruit;
- const NodeDefManager *ndef = env->getGameDef()->ndef();
- if(lua_istable(L, 2))
- {
- getstringfield(L, 2, "axiom", tree_def.initial_axiom);
- getstringfield(L, 2, "rules_a", tree_def.rules_a);
- getstringfield(L, 2, "rules_b", tree_def.rules_b);
- getstringfield(L, 2, "rules_c", tree_def.rules_c);
- getstringfield(L, 2, "rules_d", tree_def.rules_d);
- getstringfield(L, 2, "trunk", trunk);
- tree_def.trunknode=ndef->getId(trunk);
- getstringfield(L, 2, "leaves", leaves);
- tree_def.leavesnode=ndef->getId(leaves);
- tree_def.leaves2_chance=0;
- getstringfield(L, 2, "leaves2", leaves);
- if (!leaves.empty()) {
- tree_def.leaves2node=ndef->getId(leaves);
- getintfield(L, 2, "leaves2_chance", tree_def.leaves2_chance);
- }
- getintfield(L, 2, "angle", tree_def.angle);
- getintfield(L, 2, "iterations", tree_def.iterations);
- if (!getintfield(L, 2, "random_level", tree_def.iterations_random_level))
- tree_def.iterations_random_level = 0;
- getstringfield(L, 2, "trunk_type", tree_def.trunk_type);
- getboolfield(L, 2, "thin_branches", tree_def.thin_branches);
- tree_def.fruit_chance=0;
- getstringfield(L, 2, "fruit", fruit);
- if (!fruit.empty()) {
- tree_def.fruitnode=ndef->getId(fruit);
- getintfield(L, 2, "fruit_chance",tree_def.fruit_chance);
- }
- tree_def.explicit_seed = getintfield(L, 2, "seed", tree_def.seed);
- }
- else
- return 0;
- ServerMap *map = &env->getServerMap();
- treegen::error e;
- if ((e = treegen::spawn_ltree (map, p0, ndef, tree_def)) != treegen::SUCCESS) {
- if (e == treegen::UNBALANCED_BRACKETS) {
- luaL_error(L, "spawn_tree(): closing ']' has no matching opening bracket");
- } else {
- luaL_error(L, "spawn_tree(): unknown error");
- }
- }
- return 1;
- }
- // transforming_liquid_add(pos)
- int ModApiEnvMod::l_transforming_liquid_add(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 p0 = read_v3s16(L, 1);
- env->getServerMap().transforming_liquid_add(p0);
- return 1;
- }
- // forceload_block(blockpos)
- // blockpos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_forceload_block(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 blockpos = read_v3s16(L, 1);
- env->getForceloadedBlocks()->insert(blockpos);
- return 0;
- }
- // compare_block_status(nodepos)
- int ModApiEnvMod::l_compare_block_status(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 nodepos = check_v3s16(L, 1);
- std::string condition_s = luaL_checkstring(L, 2);
- auto status = env->getBlockStatus(getNodeBlockPos(nodepos));
- int condition_i = -1;
- if (!string_to_enum(es_BlockStatusType, condition_i, condition_s))
- return 0; // Unsupported
- lua_pushboolean(L, status >= condition_i);
- return 1;
- }
- // forceload_free_block(blockpos)
- // blockpos = {x=num, y=num, z=num}
- int ModApiEnvMod::l_forceload_free_block(lua_State *L)
- {
- GET_ENV_PTR;
- v3s16 blockpos = read_v3s16(L, 1);
- env->getForceloadedBlocks()->erase(blockpos);
- return 0;
- }
- // get_translated_string(lang_code, string)
- int ModApiEnvMod::l_get_translated_string(lua_State * L)
- {
- GET_ENV_PTR;
- std::string lang_code = luaL_checkstring(L, 1);
- std::string string = luaL_checkstring(L, 2);
- auto *translations = getServer(L)->getTranslationLanguage(lang_code);
- string = wide_to_utf8(translate_string(utf8_to_wide(string), translations));
- lua_pushstring(L, string.c_str());
- return 1;
- }
- void ModApiEnvMod::Initialize(lua_State *L, int top)
- {
- API_FCT(set_node);
- API_FCT(bulk_set_node);
- API_FCT(add_node);
- API_FCT(swap_node);
- API_FCT(add_item);
- API_FCT(remove_node);
- API_FCT(get_node);
- API_FCT(get_node_or_nil);
- API_FCT(get_node_light);
- API_FCT(get_natural_light);
- API_FCT(place_node);
- API_FCT(dig_node);
- API_FCT(punch_node);
- API_FCT(get_node_max_level);
- API_FCT(get_node_level);
- API_FCT(set_node_level);
- API_FCT(add_node_level);
- API_FCT(add_entity);
- API_FCT(find_nodes_with_meta);
- API_FCT(get_meta);
- API_FCT(get_node_timer);
- API_FCT(get_connected_players);
- API_FCT(get_player_by_name);
- API_FCT(get_objects_in_area);
- API_FCT(get_objects_inside_radius);
- API_FCT(set_timeofday);
- API_FCT(get_timeofday);
- API_FCT(get_gametime);
- API_FCT(get_day_count);
- API_FCT(find_node_near);
- API_FCT(find_nodes_in_area);
- API_FCT(find_nodes_in_area_under_air);
- API_FCT(fix_light);
- API_FCT(load_area);
- API_FCT(emerge_area);
- API_FCT(delete_area);
- API_FCT(get_perlin);
- API_FCT(get_perlin_map);
- API_FCT(get_voxel_manip);
- API_FCT(clear_objects);
- API_FCT(spawn_tree);
- API_FCT(find_path);
- API_FCT(line_of_sight);
- API_FCT(raycast);
- API_FCT(transforming_liquid_add);
- API_FCT(forceload_block);
- API_FCT(forceload_free_block);
- API_FCT(compare_block_status);
- API_FCT(get_translated_string);
- }
- void ModApiEnvMod::InitializeClient(lua_State *L, int top)
- {
- API_FCT(get_node_light);
- API_FCT(get_timeofday);
- API_FCT(get_node_max_level);
- API_FCT(get_node_level);
- API_FCT(find_nodes_with_meta);
- API_FCT(find_node_near);
- API_FCT(find_nodes_in_area);
- API_FCT(find_nodes_in_area_under_air);
- API_FCT(line_of_sight);
- API_FCT(raycast);
- }
|