2013-05-25 00:51:02 +02:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-04-11 09:59:09 +02:00
|
|
|
#include <algorithm>
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "lua_api/l_env.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "lua_api/l_internal.h"
|
|
|
|
#include "lua_api/l_nodemeta.h"
|
|
|
|
#include "lua_api/l_nodetimer.h"
|
|
|
|
#include "lua_api/l_noise.h"
|
2013-06-25 17:02:02 +02:00
|
|
|
#include "lua_api/l_vmanip.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "common/c_converter.h"
|
|
|
|
#include "common/c_content.h"
|
2017-04-25 19:38:08 +02:00
|
|
|
#include "scripting_server.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "environment.h"
|
2017-08-18 19:25:07 +02:00
|
|
|
#include "mapblock.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "server.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "nodedef.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "daynightratio.h"
|
|
|
|
#include "util/pointedthing.h"
|
2017-11-08 23:56:20 +01:00
|
|
|
#include "mapgen/treegen.h"
|
2015-09-23 06:31:45 +02:00
|
|
|
#include "emerge.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "pathfinder.h"
|
2015-07-06 18:53:30 +02:00
|
|
|
#include "face_position_cache.h"
|
2017-08-18 19:25:07 +02:00
|
|
|
#include "remoteplayer.h"
|
2020-04-11 11:22:15 +02:00
|
|
|
#include "server/luaentity_sao.h"
|
2020-04-11 09:59:09 +02:00
|
|
|
#include "server/player_sao.h"
|
2020-04-25 07:20:00 +02:00
|
|
|
#include "util/string.h"
|
|
|
|
#include "translation.h"
|
2017-08-16 22:11:45 +02:00
|
|
|
#ifndef SERVER
|
2018-11-28 20:01:49 +01:00
|
|
|
#include "client/client.h"
|
2017-08-16 22:11:45 +02:00
|
|
|
#endif
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2016-02-08 22:20:04 +01:00
|
|
|
struct EnumString ModApiEnvMod::es_ClearObjectsMode[] =
|
|
|
|
{
|
|
|
|
{CLEAR_OBJECTS_MODE_FULL, "full"},
|
|
|
|
{CLEAR_OBJECTS_MODE_QUICK, "quick"},
|
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
2013-06-26 23:19:39 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
|
|
|
|
u32 active_object_count, u32 active_object_count_wider)
|
|
|
|
{
|
2017-01-21 15:02:08 +01:00
|
|
|
ServerScripting *scriptIface = env->getScriptIface();
|
2013-05-25 00:51:02 +02:00
|
|
|
scriptIface->realityCheck();
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
lua_State *L = scriptIface->getStack();
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(lua_checkstack(L, 20));
|
2013-05-25 00:51:02 +02:00
|
|
|
StackUnroller stack_unroller(L);
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
2013-11-05 18:06:15 +01:00
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get registered_abms
|
|
|
|
lua_getglobal(L, "core");
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_getfield(L, -1, "registered_abms");
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
2014-04-28 03:02:48 +02:00
|
|
|
lua_remove(L, -2); // Remove core
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get registered_abms[m_id]
|
2020-04-14 20:44:18 +02:00
|
|
|
lua_pushinteger(L, m_id);
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_gettable(L, -2);
|
2013-05-25 00:51:02 +02:00
|
|
|
if(lua_isnil(L, -1))
|
2015-03-06 11:21:51 +01:00
|
|
|
FATAL_ERROR("");
|
2014-04-28 03:02:48 +02:00
|
|
|
lua_remove(L, -2); // Remove registered_abms
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2015-08-12 04:27:54 +02:00
|
|
|
scriptIface->setOriginFromTable(-1);
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
// Call action
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
lua_getfield(L, -1, "action");
|
|
|
|
luaL_checktype(L, -1, LUA_TFUNCTION);
|
2014-04-28 03:02:48 +02:00
|
|
|
lua_remove(L, -2); // Remove registered_abms[m_id]
|
2013-05-25 00:51:02 +02:00
|
|
|
push_v3s16(L, p);
|
|
|
|
pushnode(L, n, env->getGameDef()->ndef());
|
|
|
|
lua_pushnumber(L, active_object_count);
|
|
|
|
lua_pushnumber(L, active_object_count_wider);
|
2015-08-05 06:49:35 +02:00
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int result = lua_pcall(L, 4, 0, error_handler);
|
2015-08-12 04:27:54 +02:00
|
|
|
if (result)
|
|
|
|
scriptIface->scriptError(result, "LuaABM::trigger");
|
2015-08-05 06:49:35 +02:00
|
|
|
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_pop(L, 1); // Pop error handler
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
2016-02-09 07:08:31 +01:00
|
|
|
void LuaLBM::trigger(ServerEnvironment *env, v3s16 p, MapNode n)
|
|
|
|
{
|
2017-01-21 15:02:08 +01:00
|
|
|
ServerScripting *scriptIface = env->getScriptIface();
|
2016-02-09 07:08:31 +01:00
|
|
|
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]
|
2020-04-14 20:44:18 +02:00
|
|
|
lua_pushinteger(L, m_id);
|
2016-02-09 07:08:31 +01:00
|
|
|
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, env->getGameDef()->ndef());
|
|
|
|
|
|
|
|
int result = lua_pcall(L, 2, 0, error_handler);
|
|
|
|
if (result)
|
|
|
|
scriptIface->scriptError(result, "LuaLBM::trigger");
|
|
|
|
|
|
|
|
lua_pop(L, 1); // Pop error handler
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:11:20 +02:00
|
|
|
int LuaRaycast::l_next(lua_State *L)
|
|
|
|
{
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2016-07-23 21:11:20 +02:00
|
|
|
|
2019-11-09 00:22:42 +01:00
|
|
|
bool csm = false;
|
|
|
|
#ifndef SERVER
|
|
|
|
csm = getClient(L) != nullptr;
|
|
|
|
#endif
|
|
|
|
|
2016-07-23 21:11:20 +02:00
|
|
|
LuaRaycast *o = checkobject(L, 1);
|
|
|
|
PointedThing pointed;
|
|
|
|
env->continueRaycast(&o->state, &pointed);
|
|
|
|
if (pointed.type == POINTEDTHING_NOTHING)
|
|
|
|
lua_pushnil(L);
|
|
|
|
else
|
2019-11-09 00:22:42 +01:00
|
|
|
push_pointed_thing(L, pointed, csm, true);
|
2016-07-23 21:11:20 +02:00
|
|
|
|
|
|
|
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)) {
|
2018-06-30 17:11:38 +02:00
|
|
|
objects = readParam<bool>(L, 3);
|
2016-07-23 21:11:20 +02:00
|
|
|
}
|
|
|
|
if (lua_isboolean(L, 4)) {
|
2018-06-30 17:11:38 +02:00
|
|
|
liquids = readParam<bool>(L, 4);
|
2016-07-23 21:11:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
LuaRaycast *LuaRaycast::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 *(LuaRaycast **) ud;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LuaRaycast::gc_object(lua_State *L)
|
|
|
|
{
|
|
|
|
LuaRaycast *o = *(LuaRaycast **) (lua_touserdata(L, 1));
|
|
|
|
delete o;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaRaycast::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);
|
|
|
|
|
|
|
|
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_pushliteral(L, "__call");
|
|
|
|
lua_pushcfunction(L, l_next);
|
|
|
|
lua_settable(L, metatable);
|
|
|
|
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
luaL_openlib(L, 0, methods, 0);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
lua_register(L, className, create_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char LuaRaycast::className[] = "Raycast";
|
|
|
|
const luaL_Reg LuaRaycast::methods[] =
|
|
|
|
{
|
|
|
|
luamethod(LuaRaycast, next),
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
2015-10-30 07:48:37 +01:00
|
|
|
void LuaEmergeAreaCallback(v3s16 blockpos, EmergeAction action, void *param)
|
|
|
|
{
|
|
|
|
ScriptCallbackState *state = (ScriptCallbackState *)param;
|
|
|
|
assert(state != NULL);
|
|
|
|
assert(state->script != NULL);
|
|
|
|
assert(state->refcount > 0);
|
|
|
|
|
2016-07-25 18:39:29 +02:00
|
|
|
// state must be protected by envlock
|
|
|
|
Server *server = state->script->getServer();
|
|
|
|
MutexAutoLock envlock(server->m_env_mutex);
|
|
|
|
|
2015-10-30 07:48:37 +01:00
|
|
|
state->refcount--;
|
|
|
|
|
|
|
|
state->script->on_emerge_area_completion(blockpos, action, state);
|
|
|
|
|
|
|
|
if (state->refcount == 0)
|
|
|
|
delete state;
|
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
// Exported functions
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// set_node(pos, node)
|
2013-05-25 00:51:02 +02:00
|
|
|
// pos = {x=num, y=num, z=num}
|
|
|
|
int ModApiEnvMod::l_set_node(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = env->getGameDef()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
// parameters
|
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
|
|
|
MapNode n = readnode(L, 2, ndef);
|
|
|
|
// Do it
|
|
|
|
bool succeeded = env->setNode(pos, n);
|
|
|
|
lua_pushboolean(L, succeeded);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-01-30 00:30:02 +01:00
|
|
|
// 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;
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = env->getGameDef()->ndef();
|
2018-01-30 00:30:02 +01:00
|
|
|
// 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, ndef);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiEnvMod::l_add_node(lua_State *L)
|
|
|
|
{
|
|
|
|
return l_set_node(L);
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// remove_node(pos)
|
2013-05-25 00:51:02 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// swap_node(pos, node)
|
2013-11-23 15:35:49 +01:00
|
|
|
// pos = {x=num, y=num, z=num}
|
|
|
|
int ModApiEnvMod::l_swap_node(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = env->getGameDef()->ndef();
|
2013-11-23 15:35:49 +01:00
|
|
|
// parameters
|
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
|
|
|
MapNode n = readnode(L, 2, ndef);
|
|
|
|
// Do it
|
|
|
|
bool succeeded = env->swapNode(pos, n);
|
|
|
|
lua_pushboolean(L, succeeded);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_node(pos)
|
2013-05-25 00:51:02 +02:00
|
|
|
// 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
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = env->getMap().getNode(pos);
|
2013-05-25 00:51:02 +02:00
|
|
|
// Return node
|
|
|
|
pushnode(L, n, env->getGameDef()->ndef());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_node_or_nil(pos)
|
2013-05-25 00:51:02 +02:00
|
|
|
// 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
|
2014-11-14 09:05:34 +01:00
|
|
|
bool pos_ok;
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = env->getMap().getNode(pos, &pos_ok);
|
2014-11-14 09:05:34 +01:00
|
|
|
if (pos_ok) {
|
2013-05-25 00:51:02 +02:00
|
|
|
// Return node
|
|
|
|
pushnode(L, n, env->getGameDef()->ndef());
|
2014-11-14 09:05:34 +01:00
|
|
|
} else {
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
2014-11-14 09:05:34 +01:00
|
|
|
return 1;
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_node_light(pos, timeofday)
|
2013-05-25 00:51:02 +02:00
|
|
|
// 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)
|
|
|
|
{
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// 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);
|
2014-11-14 09:05:34 +01:00
|
|
|
|
|
|
|
bool is_position_ok;
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = env->getMap().getNode(pos, &is_position_ok);
|
2014-11-14 09:05:34 +01:00
|
|
|
if (is_position_ok) {
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = env->getGameDef()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushinteger(L, n.getLightBlend(dnr, ndef));
|
2014-11-14 09:05:34 +01:00
|
|
|
} else {
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
2014-11-14 09:05:34 +01:00
|
|
|
return 1;
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
2020-10-06 20:49:46 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// place_node(pos, node)
|
2013-05-25 00:51:02 +02:00
|
|
|
// pos = {x=num, y=num, z=num}
|
|
|
|
int ModApiEnvMod::l_place_node(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
ScriptApiItem *scriptIfaceItem = getScriptApi<ScriptApiItem>(L);
|
|
|
|
Server *server = getServer(L);
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = server->ndef();
|
2013-08-11 04:09:45 +02:00
|
|
|
IItemDefManager *idef = server->idef();
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
2013-08-11 04:09:45 +02:00
|
|
|
MapNode n = readnode(L, 2, ndef);
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// Don't attempt to load non-loaded area as of now
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n_old = env->getMap().getNode(pos);
|
2013-05-25 00:51:02 +02:00
|
|
|
if(n_old.getContent() == CONTENT_IGNORE){
|
|
|
|
lua_pushboolean(L, false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Create item to place
|
2017-01-31 20:49:01 +01:00
|
|
|
ItemStack item(ndef->get(n).name, 1, 0, idef);
|
2013-05-25 00:51:02 +02:00
|
|
|
// Make pointed position
|
|
|
|
PointedThing pointed;
|
|
|
|
pointed.type = POINTEDTHING_NODE;
|
|
|
|
pointed.node_abovesurface = pos;
|
|
|
|
pointed.node_undersurface = pos + v3s16(0,-1,0);
|
2017-09-21 21:52:52 +02:00
|
|
|
// Place it with a NULL placer (appears in Lua as nil)
|
|
|
|
bool success = scriptIfaceItem->item_OnPlace(item, nullptr, pointed);
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushboolean(L, success);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// dig_node(pos)
|
2013-05-25 00:51:02 +02:00
|
|
|
// pos = {x=num, y=num, z=num}
|
|
|
|
int ModApiEnvMod::l_dig_node(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
|
|
|
|
|
|
|
// Don't attempt to load non-loaded area as of now
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = env->getMap().getNode(pos);
|
2013-05-25 00:51:02 +02:00
|
|
|
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)
|
2013-08-11 04:09:45 +02:00
|
|
|
bool success = scriptIfaceNode->node_on_dig(pos, n, NULL);
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushboolean(L, success);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// punch_node(pos)
|
2013-05-25 00:51:02 +02:00
|
|
|
// pos = {x=num, y=num, z=num}
|
|
|
|
int ModApiEnvMod::l_punch_node(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
ScriptApiNode *scriptIfaceNode = getScriptApi<ScriptApiNode>(L);
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
|
|
|
|
|
|
|
// Don't attempt to load non-loaded area as of now
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = env->getMap().getNode(pos);
|
2013-05-25 00:51:02 +02:00
|
|
|
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)
|
2014-01-24 01:21:01 +01:00
|
|
|
bool success = scriptIfaceNode->node_on_punch(pos, n, NULL, PointedThing());
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushboolean(L, success);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_node_max_level(pos)
|
2013-07-27 20:34:30 +02:00
|
|
|
// pos = {x=num, y=num, z=num}
|
|
|
|
int ModApiEnvMod::l_get_node_max_level(lua_State *L)
|
|
|
|
{
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2013-07-27 20:34:30 +02:00
|
|
|
|
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = env->getMap().getNode(pos);
|
2013-07-27 20:34:30 +02:00
|
|
|
lua_pushnumber(L, n.getMaxLevel(env->getGameDef()->ndef()));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_node_level(pos)
|
2013-07-27 20:34:30 +02:00
|
|
|
// pos = {x=num, y=num, z=num}
|
|
|
|
int ModApiEnvMod::l_get_node_level(lua_State *L)
|
|
|
|
{
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2013-07-27 20:34:30 +02:00
|
|
|
|
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = env->getMap().getNode(pos);
|
2013-07-27 20:34:30 +02:00
|
|
|
lua_pushnumber(L, n.getLevel(env->getGameDef()->ndef()));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// set_node_level(pos, level)
|
2013-07-28 15:11:59 +02:00
|
|
|
// 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);
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = env->getMap().getNode(pos);
|
2013-07-28 15:11:59 +02:00
|
|
|
lua_pushnumber(L, n.setLevel(env->getGameDef()->ndef(), level));
|
|
|
|
env->setNode(pos, n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// add_node_level(pos, level)
|
2013-07-27 20:34:30 +02:00
|
|
|
// pos = {x=num, y=num, z=num}
|
2020-05-19 21:08:37 +02:00
|
|
|
// level: -127..127
|
2013-07-27 20:34:30 +02:00
|
|
|
int ModApiEnvMod::l_add_node_level(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
2020-05-19 21:08:37 +02:00
|
|
|
s16 level = 1;
|
2013-07-27 20:34:30 +02:00
|
|
|
if(lua_isnumber(L, 2))
|
|
|
|
level = lua_tonumber(L, 2);
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = env->getMap().getNode(pos);
|
2013-07-27 20:34:30 +02:00
|
|
|
lua_pushnumber(L, n.addLevel(env->getGameDef()->ndef(), level));
|
|
|
|
env->setNode(pos, n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-05 20:30:46 +02:00
|
|
|
// find_nodes_with_meta(pos1, pos2)
|
|
|
|
int ModApiEnvMod::l_find_nodes_with_meta(lua_State *L)
|
|
|
|
{
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2015-05-05 20:30:46 +02:00
|
|
|
|
|
|
|
std::vector<v3s16> positions = env->getMap().findNodesWithMetadata(
|
|
|
|
check_v3s16(L, 1), check_v3s16(L, 2));
|
|
|
|
|
2020-04-14 20:44:18 +02:00
|
|
|
lua_createtable(L, positions.size(), 0);
|
2015-05-05 20:30:46 +02:00
|
|
|
for (size_t i = 0; i != positions.size(); i++) {
|
|
|
|
push_v3s16(L, positions[i]);
|
|
|
|
lua_rawseti(L, -2, i + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2013-07-27 20:34:30 +02:00
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_meta(pos)
|
2013-05-25 00:51:02 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_node_timer(pos)
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiEnvMod::l_get_node_timer(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
// Do it
|
|
|
|
v3s16 p = read_v3s16(L, 1);
|
2020-04-11 19:59:43 +02:00
|
|
|
NodeTimerRef::create(L, p, &env->getServerMap());
|
2013-05-25 00:51:02 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:39:45 +01:00
|
|
|
// add_entity(pos, entityname, [staticdata]) -> ObjectRef or nil
|
2013-05-25 00:51:02 +02:00
|
|
|
// 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);
|
2017-01-09 20:39:45 +01:00
|
|
|
const char *staticdata = luaL_optstring(L, 3, "");
|
2020-04-26 20:29:16 +02:00
|
|
|
|
2017-01-09 20:39:45 +01:00
|
|
|
ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, staticdata);
|
2013-05-25 00:51:02 +02:00
|
|
|
int objectid = env->addActiveObject(obj);
|
|
|
|
// If failed to add, return nothing (reads as nil)
|
|
|
|
if(objectid == 0)
|
|
|
|
return 0;
|
2020-04-26 20:29:16 +02:00
|
|
|
|
|
|
|
// If already deleted (can happen in on_activate), return nil
|
|
|
|
if (obj->isGone())
|
|
|
|
return 0;
|
2014-10-02 21:58:13 +02:00
|
|
|
getScriptApiBase(L)->objectrefGetOrCreate(L, obj);
|
2013-05-25 00:51:02 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// add_item(pos, itemstack or itemstring or table) -> ObjectRef or nil
|
2013-05-25 00:51:02 +02:00
|
|
|
// 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
|
2017-01-31 14:18:52 +01:00
|
|
|
ItemStack item = read_item(L, 2,getServer(L)->idef());
|
2013-05-25 00:51:02 +02:00
|
|
|
if(item.empty() || !item.isKnown(getServer(L)->idef()))
|
|
|
|
return 0;
|
2013-11-05 18:06:15 +01:00
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
2013-11-05 18:06:15 +01:00
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Use spawn_item to spawn a __builtin:item
|
|
|
|
lua_getglobal(L, "core");
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_getfield(L, -1, "spawn_item");
|
2014-04-28 03:02:48 +02:00
|
|
|
lua_remove(L, -2); // Remove core
|
2013-05-25 00:51:02 +02:00
|
|
|
if(lua_isnil(L, -1))
|
|
|
|
return 0;
|
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
lua_pushstring(L, item.getItemString().c_str());
|
2015-08-05 06:49:35 +02:00
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RESL(L, lua_pcall(L, 2, 1, error_handler));
|
2015-08-05 06:49:35 +02:00
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
lua_remove(L, error_handler);
|
2013-05-25 00:51:02 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-12 19:49:48 +01:00
|
|
|
// get_connected_players()
|
|
|
|
int ModApiEnvMod::l_get_connected_players(lua_State *L)
|
|
|
|
{
|
2020-03-10 20:32:04 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-02-12 19:49:48 +01:00
|
|
|
|
|
|
|
lua_createtable(L, env->getPlayerCount(), 0);
|
|
|
|
u32 i = 0;
|
|
|
|
for (RemotePlayer *player : env->getPlayers()) {
|
2020-02-25 16:08:47 +01:00
|
|
|
if (player->getPeerId() == PEER_ID_INEXISTENT)
|
|
|
|
continue;
|
2020-02-12 19:49:48 +01:00
|
|
|
PlayerSAO *sao = player->getPlayerSAO();
|
2020-03-03 20:44:03 +01:00
|
|
|
if (sao && !sao->isGone()) {
|
2020-02-12 19:49:48 +01:00
|
|
|
getScriptApiBase(L)->objectrefGetOrCreate(L, sao);
|
|
|
|
lua_rawseti(L, -2, ++i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_player_by_name(name)
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiEnvMod::l_get_player_by_name(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
// Do it
|
|
|
|
const char *name = luaL_checkstring(L, 1);
|
2020-02-12 19:49:48 +01:00
|
|
|
RemotePlayer *player = env->getPlayer(name);
|
2020-03-03 21:14:47 +01:00
|
|
|
if (!player || player->getPeerId() == PEER_ID_INEXISTENT)
|
|
|
|
return 0;
|
2013-05-25 00:51:02 +02:00
|
|
|
PlayerSAO *sao = player->getPlayerSAO();
|
2020-03-03 21:14:47 +01:00
|
|
|
if (!sao || sao->isGone())
|
|
|
|
return 0;
|
2013-05-25 00:51:02 +02:00
|
|
|
// Put player on stack
|
2014-10-02 21:58:13 +02:00
|
|
|
getScriptApiBase(L)->objectrefGetOrCreate(L, sao);
|
2013-05-25 00:51:02 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_objects_inside_radius(pos, radius)
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
2020-04-16 08:25:48 +02:00
|
|
|
ScriptApiBase *script = getScriptApiBase(L);
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// Do it
|
|
|
|
v3f pos = checkFloatPos(L, 1);
|
2018-06-04 22:38:07 +02:00
|
|
|
float radius = readParam<float>(L, 2) * BS;
|
2020-04-16 08:25:48 +02:00
|
|
|
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);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// set_timeofday(val)
|
2013-05-25 00:51:02 +02:00
|
|
|
// val = 0...1
|
|
|
|
int ModApiEnvMod::l_set_timeofday(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
// Do it
|
2018-06-04 22:38:07 +02:00
|
|
|
float timeofday_f = readParam<float>(L, 1);
|
2015-03-06 11:21:51 +01:00
|
|
|
sanity_check(timeofday_f >= 0.0 && timeofday_f <= 1.0);
|
2013-05-25 00:51:02 +02:00
|
|
|
int timeofday_mh = (int)(timeofday_f * 24000.0);
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_timeofday() -> 0...1
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiEnvMod::l_get_timeofday(lua_State *L)
|
|
|
|
{
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// Do it
|
|
|
|
int timeofday_mh = env->getTimeOfDay();
|
2017-03-17 07:48:29 +01:00
|
|
|
float timeofday_f = (float)timeofday_mh / 24000.0f;
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushnumber(L, timeofday_f);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-06 21:02:21 +01:00
|
|
|
// get_day_count() -> int
|
|
|
|
int ModApiEnvMod::l_get_day_count(lua_State *L)
|
|
|
|
{
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2016-03-06 21:02:21 +01:00
|
|
|
|
|
|
|
lua_pushnumber(L, env->getDayCount());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_gametime()
|
2013-09-08 10:54:36 +02:00
|
|
|
int ModApiEnvMod::l_get_gametime(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
int game_time = env->getGameTime();
|
|
|
|
lua_pushnumber(L, game_time);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-14 22:36:53 +02:00
|
|
|
void ModApiEnvMod::collectNodeIds(lua_State *L, int idx, const NodeDefManager *ndef,
|
|
|
|
std::vector<content_t> &filter)
|
2013-05-25 00:51:02 +02:00
|
|
|
{
|
2020-07-14 22:36:53 +02:00
|
|
|
if (lua_istable(L, idx)) {
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushnil(L);
|
2020-07-14 22:36:53 +02:00
|
|
|
while (lua_next(L, idx) != 0) {
|
2013-05-25 00:51:02 +02:00
|
|
|
// key at index -2 and value at index -1
|
|
|
|
luaL_checktype(L, -1, LUA_TSTRING);
|
2018-06-30 17:11:38 +02:00
|
|
|
ndef->getIds(readParam<std::string>(L, -1), filter);
|
2013-05-25 00:51:02 +02:00
|
|
|
// removes value, keeps key for next iteration
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2020-07-14 22:36:53 +02:00
|
|
|
} else if (lua_isstring(L, idx)) {
|
2018-06-30 17:11:38 +02:00
|
|
|
ndef->getIds(readParam<std::string>(L, 3), filter);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
2020-07-14 22:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2018-06-30 17:11:38 +02:00
|
|
|
int start_radius = (lua_isboolean(L, 4) && readParam<bool>(L, 4)) ? 0 : 1;
|
2017-07-18 21:39:55 +02:00
|
|
|
|
|
|
|
#ifndef SERVER
|
|
|
|
// Client API limitations
|
2020-05-14 21:16:45 +02:00
|
|
|
if (Client *client = getClient(L))
|
|
|
|
radius = client->CSMClampRadius(pos, radius);
|
2017-07-18 21:39:55 +02:00
|
|
|
#endif
|
|
|
|
|
2017-05-04 22:52:58 +02:00
|
|
|
for (int d = start_radius; d <= radius; d++) {
|
2020-07-14 22:36:53 +02:00
|
|
|
const std::vector<v3s16> &list = FacePositionCache::getFacePositions(d);
|
2017-08-20 13:30:50 +02:00
|
|
|
for (const v3s16 &i : list) {
|
2017-08-19 22:23:47 +02:00
|
|
|
v3s16 p = pos + i;
|
2020-07-14 22:36:53 +02:00
|
|
|
content_t c = map.getNode(p).getContent();
|
2017-09-11 15:51:40 +02:00
|
|
|
if (CONTAINS(filter, c)) {
|
2013-05-25 00:51:02 +02:00
|
|
|
push_v3s16(L, p);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-14 22:36:53 +02:00
|
|
|
// find_nodes_in_area(minp, maxp, nodenames, [grouped])
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
|
|
|
|
{
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
v3s16 minp = read_v3s16(L, 1);
|
|
|
|
v3s16 maxp = read_v3s16(L, 2);
|
2017-06-19 16:30:26 +02:00
|
|
|
sortBoxVerticies(minp, maxp);
|
|
|
|
|
2020-04-10 22:06:24 +02:00
|
|
|
const NodeDefManager *ndef = env->getGameDef()->ndef();
|
2020-07-14 22:36:53 +02:00
|
|
|
Map &map = env->getMap();
|
2020-04-10 22:06:24 +02:00
|
|
|
|
2019-11-09 00:22:42 +01:00
|
|
|
#ifndef SERVER
|
2020-05-14 21:16:45 +02:00
|
|
|
if (Client *client = getClient(L)) {
|
|
|
|
minp = client->CSMClampPos(minp);
|
|
|
|
maxp = client->CSMClampPos(maxp);
|
2019-11-09 00:22:42 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-06-19 16:30:26 +02:00
|
|
|
v3s16 cube = maxp - minp + 1;
|
2018-02-21 15:34:06 +01:00
|
|
|
// Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
|
|
|
|
if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 4096000) {
|
2017-06-19 16:30:26 +02:00
|
|
|
luaL_error(L, "find_nodes_in_area(): area volume"
|
2018-02-21 15:34:06 +01:00
|
|
|
" exceeds allowed value of 4096000");
|
2017-06-19 16:30:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-11 15:51:40 +02:00
|
|
|
std::vector<content_t> filter;
|
2020-07-14 22:36:53 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
v3s16 p;
|
|
|
|
for (p.X = minp.X; p.X <= maxp.X; p.X++)
|
|
|
|
for (p.Y = minp.Y; p.Y <= maxp.Y; p.Y++)
|
|
|
|
for (p.Z = minp.Z; p.Z <= maxp.Z; p.Z++) {
|
|
|
|
content_t c = map.getNode(p).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]);
|
|
|
|
}
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 22:36:53 +02:00
|
|
|
// 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);
|
2015-05-30 20:53:21 +02:00
|
|
|
|
2020-07-14 22:36:53 +02:00
|
|
|
assert(lua_gettop(L) == base);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
std::vector<u32> individual_count;
|
|
|
|
individual_count.resize(filter.size());
|
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
u32 i = 0;
|
|
|
|
v3s16 p;
|
|
|
|
for (p.X = minp.X; p.X <= maxp.X; p.X++)
|
|
|
|
for (p.Y = minp.Y; p.Y <= maxp.Y; p.Y++)
|
|
|
|
for (p.Z = minp.Z; p.Z <= maxp.Z; p.Z++) {
|
|
|
|
content_t c = env->getMap().getNode(p).getContent();
|
|
|
|
|
|
|
|
auto it = std::find(filter.begin(), filter.end(), c);
|
|
|
|
if (it != filter.end()) {
|
|
|
|
push_v3s16(L, p);
|
|
|
|
lua_rawseti(L, -2, ++i);
|
2017-09-11 15:51:40 +02:00
|
|
|
|
2020-07-14 22:36:53 +02:00
|
|
|
u32 filt_index = it - filter.begin();
|
|
|
|
individual_count[filt_index]++;
|
|
|
|
}
|
2017-06-19 16:30:26 +02:00
|
|
|
}
|
2020-07-14 22:36:53 +02:00
|
|
|
|
|
|
|
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;
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 12:42:00 +01:00
|
|
|
// 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)
|
2015-02-22 17:01:03 +01:00
|
|
|
{
|
2015-02-27 12:42:00 +01:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2015-02-22 17:01:03 +01:00
|
|
|
|
|
|
|
v3s16 minp = read_v3s16(L, 1);
|
|
|
|
v3s16 maxp = read_v3s16(L, 2);
|
2017-06-19 16:30:26 +02:00
|
|
|
sortBoxVerticies(minp, maxp);
|
|
|
|
|
2020-04-10 22:06:24 +02:00
|
|
|
const NodeDefManager *ndef = env->getGameDef()->ndef();
|
2020-07-14 22:36:53 +02:00
|
|
|
Map &map = env->getMap();
|
2020-04-10 22:06:24 +02:00
|
|
|
|
2019-11-09 00:22:42 +01:00
|
|
|
#ifndef SERVER
|
2020-05-14 21:16:45 +02:00
|
|
|
if (Client *client = getClient(L)) {
|
|
|
|
minp = client->CSMClampPos(minp);
|
|
|
|
maxp = client->CSMClampPos(maxp);
|
2019-11-09 00:22:42 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-06-19 16:30:26 +02:00
|
|
|
v3s16 cube = maxp - minp + 1;
|
2018-02-21 15:34:06 +01:00
|
|
|
// Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
|
|
|
|
if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 4096000) {
|
2017-06-19 16:30:26 +02:00
|
|
|
luaL_error(L, "find_nodes_in_area_under_air(): area volume"
|
2018-02-21 15:34:06 +01:00
|
|
|
" exceeds allowed value of 4096000");
|
2017-06-19 16:30:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-11 15:51:40 +02:00
|
|
|
std::vector<content_t> filter;
|
2020-07-14 22:36:53 +02:00
|
|
|
collectNodeIds(L, 3, ndef, filter);
|
2015-02-22 17:01:03 +01:00
|
|
|
|
|
|
|
lua_newtable(L);
|
2020-07-14 22:36:53 +02:00
|
|
|
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();
|
2017-06-19 16:30:26 +02:00
|
|
|
if (c != CONTENT_AIR && csurf == CONTENT_AIR &&
|
2017-09-11 15:51:40 +02:00
|
|
|
CONTAINS(filter, c)) {
|
2020-07-14 22:36:53 +02:00
|
|
|
push_v3s16(L, p);
|
2015-02-22 17:01:03 +01:00
|
|
|
lua_rawseti(L, -2, ++i);
|
|
|
|
}
|
|
|
|
c = csurf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_perlin(seeddiff, octaves, persistence, scale)
|
2013-05-25 00:51:02 +02:00
|
|
|
// returns world-specific PerlinNoise
|
|
|
|
int ModApiEnvMod::l_get_perlin(lua_State *L)
|
|
|
|
{
|
2015-10-25 05:45:18 +01:00
|
|
|
GET_ENV_PTR_NO_MAP_LOCK;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2014-12-12 08:02:26 +01:00
|
|
|
NoiseParams params;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2014-12-12 08:02:26 +01:00
|
|
|
if (lua_istable(L, 1)) {
|
|
|
|
read_noiseparams(L, 1, ¶ms);
|
|
|
|
} else {
|
|
|
|
params.seed = luaL_checkint(L, 1);
|
|
|
|
params.octaves = luaL_checkint(L, 2);
|
2018-06-04 22:38:07 +02:00
|
|
|
params.persist = readParam<float>(L, 3);
|
|
|
|
params.spread = v3f(1, 1, 1) * readParam<float>(L, 4);
|
2014-12-12 08:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
params.seed += (int)env->getServerMap().getSeed();
|
|
|
|
|
|
|
|
LuaPerlinNoise *n = new LuaPerlinNoise(¶ms);
|
2013-05-25 00:51:02 +02:00
|
|
|
*(void **)(lua_newuserdata(L, sizeof(void *))) = n;
|
|
|
|
luaL_getmetatable(L, "PerlinNoise");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_perlin_map(noiseparams, size)
|
2013-05-25 00:51:02 +02:00
|
|
|
// returns world-specific PerlinNoiseMap
|
|
|
|
int ModApiEnvMod::l_get_perlin_map(lua_State *L)
|
|
|
|
{
|
2015-10-25 05:45:18 +01:00
|
|
|
GET_ENV_PTR_NO_MAP_LOCK;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2014-12-08 03:57:12 +01:00
|
|
|
NoiseParams np;
|
|
|
|
if (!read_noiseparams(L, 1, &np))
|
2013-05-25 00:51:02 +02:00
|
|
|
return 0;
|
|
|
|
v3s16 size = read_v3s16(L, 2);
|
|
|
|
|
2016-06-04 07:35:37 +02:00
|
|
|
s32 seed = (s32)(env->getServerMap().getSeed());
|
2014-12-08 03:57:12 +01:00
|
|
|
LuaPerlinNoiseMap *n = new LuaPerlinNoiseMap(&np, seed, size);
|
2013-05-25 00:51:02 +02:00
|
|
|
*(void **)(lua_newuserdata(L, sizeof(void *))) = n;
|
|
|
|
luaL_getmetatable(L, "PerlinNoiseMap");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// get_voxel_manip()
|
2013-06-25 17:02:02 +02:00
|
|
|
// returns voxel manipulator
|
|
|
|
int ModApiEnvMod::l_get_voxel_manip(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
Map *map = &(env->getMap());
|
2014-12-29 07:31:37 +01:00
|
|
|
LuaVoxelManip *o = (lua_istable(L, 1) && lua_istable(L, 2)) ?
|
|
|
|
new LuaVoxelManip(map, read_v3s16(L, 1), read_v3s16(L, 2)) :
|
|
|
|
new LuaVoxelManip(map);
|
2014-12-28 05:09:36 +01:00
|
|
|
|
2013-06-26 23:19:39 +02:00
|
|
|
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
|
2013-06-25 17:02:02 +02:00
|
|
|
luaL_getmetatable(L, "VoxelManip");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-02-08 22:20:04 +01:00
|
|
|
// clear_objects([options])
|
2013-05-25 00:51:02 +02:00
|
|
|
// clear all objects in the environment
|
2016-02-08 22:20:04 +01:00
|
|
|
// where options = {mode = "full" or "quick"}
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiEnvMod::l_clear_objects(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
2017-11-21 03:55:33 +01:00
|
|
|
ClearObjectsMode mode = CLEAR_OBJECTS_MODE_QUICK;
|
2016-02-08 22:20:04 +01:00
|
|
|
if (lua_istable(L, 1)) {
|
|
|
|
mode = (ClearObjectsMode)getenumfield(L, 1, "mode",
|
|
|
|
ModApiEnvMod::es_ClearObjectsMode, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
env->clearObjects(mode);
|
2013-05-25 00:51:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-01 20:15:37 +01:00
|
|
|
// line_of_sight(pos1, pos2) -> true/false, pos
|
2015-01-15 22:20:05 +01:00
|
|
|
int ModApiEnvMod::l_line_of_sight(lua_State *L)
|
|
|
|
{
|
2020-04-10 22:06:24 +02:00
|
|
|
GET_PLAIN_ENV_PTR;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// read position 1 from lua
|
2013-05-26 18:23:22 +02:00
|
|
|
v3f pos1 = checkFloatPos(L, 1);
|
2013-05-25 00:51:02 +02:00
|
|
|
// read position 2 from lua
|
|
|
|
v3f pos2 = checkFloatPos(L, 2);
|
|
|
|
|
2013-12-11 21:33:39 +01:00
|
|
|
v3s16 p;
|
2016-12-01 20:15:37 +01:00
|
|
|
|
|
|
|
bool success = env->line_of_sight(pos1, pos2, &p);
|
2013-12-11 21:33:39 +01:00
|
|
|
lua_pushboolean(L, success);
|
|
|
|
if (!success) {
|
|
|
|
push_v3s16(L, p);
|
|
|
|
return 2;
|
|
|
|
}
|
2013-11-25 00:03:03 +01:00
|
|
|
return 1;
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
2017-03-11 17:07:04 +01:00
|
|
|
// 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);
|
|
|
|
}
|
2017-08-19 22:23:47 +02:00
|
|
|
if (!modified_blocks.empty()) {
|
2017-03-11 17:07:04 +01:00
|
|
|
MapEditEvent event;
|
|
|
|
event.type = MEET_OTHER;
|
2017-08-19 22:23:47 +02:00
|
|
|
for (auto &modified_block : modified_blocks)
|
|
|
|
event.modified_blocks.insert(modified_block.first);
|
2017-03-11 17:07:04 +01:00
|
|
|
|
2019-09-24 19:05:28 +02:00
|
|
|
map.dispatchEvent(event);
|
2017-03-11 17:07:04 +01:00
|
|
|
}
|
|
|
|
lua_pushboolean(L, success);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-23 21:11:20 +02:00
|
|
|
int ModApiEnvMod::l_raycast(lua_State *L)
|
|
|
|
{
|
|
|
|
return LuaRaycast::create_object(L);
|
|
|
|
}
|
|
|
|
|
2018-12-31 01:32:54 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-10-30 07:48:37 +01:00
|
|
|
// emerge_area(p1, p2, [callback, context])
|
|
|
|
// emerge mapblocks in area p1..p2, calls callback with context upon completion
|
2015-09-23 06:31:45 +02:00
|
|
|
int ModApiEnvMod::l_emerge_area(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
2015-10-30 07:48:37 +01:00
|
|
|
EmergeCompletionCallback callback = NULL;
|
|
|
|
ScriptCallbackState *state = NULL;
|
|
|
|
|
2015-09-23 06:31:45 +02:00
|
|
|
EmergeManager *emerge = getServer(L)->getEmergeManager();
|
|
|
|
|
|
|
|
v3s16 bpmin = getNodeBlockPos(read_v3s16(L, 1));
|
|
|
|
v3s16 bpmax = getNodeBlockPos(read_v3s16(L, 2));
|
|
|
|
sortBoxVerticies(bpmin, bpmax);
|
|
|
|
|
2015-10-30 07:48:37 +01:00
|
|
|
size_t num_blocks = VoxelArea(bpmin, bpmax).getVolume();
|
|
|
|
assert(num_blocks != 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();
|
|
|
|
}
|
|
|
|
|
2015-09-23 06:31:45 +02:00
|
|
|
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++) {
|
2015-10-30 07:48:37 +01:00
|
|
|
emerge->enqueueBlockEmergeEx(v3s16(x, y, z), PEER_ID_INEXISTENT,
|
|
|
|
BLOCK_EMERGE_ALLOW_GEN | BLOCK_EMERGE_FORCE_QUEUE, callback, state);
|
2015-09-23 06:31:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-15 22:20:05 +01:00
|
|
|
// 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);
|
2015-08-15 20:03:36 +02:00
|
|
|
if (map.deleteBlock(bp)) {
|
|
|
|
env->setStaticForActiveObjectsInBlock(bp, false);
|
2015-01-15 22:20:05 +01:00
|
|
|
event.modified_blocks.insert(bp);
|
2015-08-15 20:03:36 +02:00
|
|
|
} else {
|
2015-01-15 22:20:05 +01:00
|
|
|
success = false;
|
2015-08-15 20:03:36 +02:00
|
|
|
}
|
2015-01-15 22:20:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-24 19:05:28 +02:00
|
|
|
map.dispatchEvent(event);
|
2015-01-15 22:20:05 +01:00
|
|
|
lua_pushboolean(L, success);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// find_path(pos1, pos2, searchdistance,
|
2013-05-25 00:51:02 +02:00
|
|
|
// max_jump, max_drop, algorithm) -> table containing path
|
|
|
|
int ModApiEnvMod::l_find_path(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
2013-05-26 18:23:22 +02:00
|
|
|
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);
|
2016-04-01 01:52:17 +02:00
|
|
|
PathAlgorithm algo = PA_PLAIN_NP;
|
2020-03-05 12:07:52 +01:00
|
|
|
if (!lua_isnoneornil(L, 6)) {
|
2013-06-25 17:02:02 +02:00
|
|
|
std::string algorithm = luaL_checkstring(L,6);
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
if (algorithm == "A*")
|
2016-04-01 01:52:17 +02:00
|
|
|
algo = PA_PLAIN;
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
if (algorithm == "Dijkstra")
|
2016-04-01 01:52:17 +02:00
|
|
|
algo = PA_DIJKSTRA;
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 19:59:43 +02:00
|
|
|
std::vector<v3s16> path = get_path(&env->getServerMap(), env->getGameDef()->ndef(), pos1, pos2,
|
2016-04-01 01:52:17 +02:00
|
|
|
searchdistance, max_jump, max_drop, algo);
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2017-08-19 22:23:47 +02:00
|
|
|
if (!path.empty()) {
|
2020-04-14 20:44:18 +02:00
|
|
|
lua_createtable(L, path.size(), 0);
|
2013-05-25 00:51:02 +02:00
|
|
|
int top = lua_gettop(L);
|
|
|
|
unsigned int index = 1;
|
2017-08-20 13:30:50 +02:00
|
|
|
for (const v3s16 &i : path) {
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushnumber(L,index);
|
2017-08-19 22:23:47 +02:00
|
|
|
push_v3s16(L, i);
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_settable(L, top);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// spawn_tree(pos, treedef)
|
2013-05-25 00:51:02 +02:00
|
|
|
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;
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = env->getGameDef()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
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);
|
2017-08-19 22:23:47 +02:00
|
|
|
if (!leaves.empty()) {
|
2013-05-25 00:51:02 +02:00
|
|
|
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);
|
2014-08-07 07:39:12 +02:00
|
|
|
if (!getintfield(L, 2, "random_level", tree_def.iterations_random_level))
|
|
|
|
tree_def.iterations_random_level = 0;
|
2013-05-25 00:51:02 +02:00
|
|
|
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);
|
2017-08-19 22:23:47 +02:00
|
|
|
if (!fruit.empty()) {
|
2013-05-25 00:51:02 +02:00
|
|
|
tree_def.fruitnode=ndef->getId(fruit);
|
|
|
|
getintfield(L, 2, "fruit_chance",tree_def.fruit_chance);
|
|
|
|
}
|
2014-08-07 07:39:12 +02:00
|
|
|
tree_def.explicit_seed = getintfield(L, 2, "seed", tree_def.seed);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
2014-08-07 07:39:12 +02:00
|
|
|
|
2020-04-11 19:59:43 +02:00
|
|
|
ServerMap *map = &env->getServerMap();
|
2014-08-07 07:39:12 +02:00
|
|
|
treegen::error e;
|
2020-04-11 19:59:43 +02:00
|
|
|
if ((e = treegen::spawn_ltree (map, p0, ndef, tree_def)) != treegen::SUCCESS) {
|
2014-08-07 07:39:12 +02:00
|
|
|
if (e == treegen::UNBALANCED_BRACKETS) {
|
|
|
|
luaL_error(L, "spawn_tree(): closing ']' has no matching opening bracket");
|
|
|
|
} else {
|
|
|
|
luaL_error(L, "spawn_tree(): unknown error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// transforming_liquid_add(pos)
|
2013-07-27 20:34:30 +02:00
|
|
|
int ModApiEnvMod::l_transforming_liquid_add(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
v3s16 p0 = read_v3s16(L, 1);
|
|
|
|
env->getMap().transforming_liquid_add(p0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// forceload_block(blockpos)
|
2014-01-11 17:54:00 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// forceload_free_block(blockpos)
|
2014-01-11 17:54:00 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2013-07-27 20:34:30 +02:00
|
|
|
|
2020-04-25 07:20:00 +02:00
|
|
|
// 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);
|
2020-09-16 15:51:11 +02:00
|
|
|
|
|
|
|
auto *translations = getServer(L)->getTranslationLanguage(lang_code);
|
|
|
|
string = wide_to_utf8(translate_string(utf8_to_wide(string), translations));
|
2020-04-25 07:20:00 +02:00
|
|
|
lua_pushstring(L, string.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
void ModApiEnvMod::Initialize(lua_State *L, int top)
|
|
|
|
{
|
|
|
|
API_FCT(set_node);
|
2018-01-30 00:30:02 +01:00
|
|
|
API_FCT(bulk_set_node);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(add_node);
|
2013-11-23 15:35:49 +01:00
|
|
|
API_FCT(swap_node);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(add_item);
|
|
|
|
API_FCT(remove_node);
|
|
|
|
API_FCT(get_node);
|
|
|
|
API_FCT(get_node_or_nil);
|
|
|
|
API_FCT(get_node_light);
|
2020-10-06 20:49:46 +02:00
|
|
|
API_FCT(get_natural_light);
|
2013-08-11 04:09:45 +02:00
|
|
|
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);
|
2015-05-05 20:30:46 +02:00
|
|
|
API_FCT(find_nodes_with_meta);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(get_meta);
|
|
|
|
API_FCT(get_node_timer);
|
2020-02-12 19:49:48 +01:00
|
|
|
API_FCT(get_connected_players);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(get_player_by_name);
|
|
|
|
API_FCT(get_objects_inside_radius);
|
|
|
|
API_FCT(set_timeofday);
|
|
|
|
API_FCT(get_timeofday);
|
2013-09-08 10:54:36 +02:00
|
|
|
API_FCT(get_gametime);
|
2016-03-06 21:02:21 +01:00
|
|
|
API_FCT(get_day_count);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(find_node_near);
|
|
|
|
API_FCT(find_nodes_in_area);
|
2015-02-27 12:42:00 +01:00
|
|
|
API_FCT(find_nodes_in_area_under_air);
|
2017-03-11 17:07:04 +01:00
|
|
|
API_FCT(fix_light);
|
2018-12-31 01:32:54 +01:00
|
|
|
API_FCT(load_area);
|
2015-09-23 06:31:45 +02:00
|
|
|
API_FCT(emerge_area);
|
2015-01-15 22:20:05 +01:00
|
|
|
API_FCT(delete_area);
|
2013-08-11 04:09:45 +02:00
|
|
|
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);
|
2016-07-23 21:11:20 +02:00
|
|
|
API_FCT(raycast);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(transforming_liquid_add);
|
2014-01-11 17:54:00 +01:00
|
|
|
API_FCT(forceload_block);
|
|
|
|
API_FCT(forceload_free_block);
|
2020-04-25 07:20:00 +02:00
|
|
|
API_FCT(get_translated_string);
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
2017-03-17 07:48:29 +01:00
|
|
|
|
|
|
|
void ModApiEnvMod::InitializeClient(lua_State *L, int top)
|
|
|
|
{
|
2019-11-09 00:22:42 +01:00
|
|
|
API_FCT(get_node_light);
|
2017-03-17 07:48:29 +01:00
|
|
|
API_FCT(get_timeofday);
|
|
|
|
API_FCT(get_node_max_level);
|
|
|
|
API_FCT(get_node_level);
|
2019-11-09 00:22:42 +01:00
|
|
|
API_FCT(find_nodes_with_meta);
|
2017-03-17 07:48:29 +01:00
|
|
|
API_FCT(find_node_near);
|
2019-11-09 00:22:42 +01:00
|
|
|
API_FCT(find_nodes_in_area);
|
|
|
|
API_FCT(find_nodes_in_area_under_air);
|
|
|
|
API_FCT(line_of_sight);
|
|
|
|
API_FCT(raycast);
|
2017-03-17 07:48:29 +01:00
|
|
|
}
|