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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "scripting_game.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "environment.h"
|
|
|
|
#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"
|
|
|
|
#include "content_sao.h"
|
|
|
|
#include "treegen.h"
|
|
|
|
#include "pathfinder.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define GET_ENV_PTR ServerEnvironment* env = \
|
|
|
|
dynamic_cast<ServerEnvironment*>(getEnv(L)); \
|
|
|
|
if( env == NULL) return 0
|
2013-11-25 00:03:03 +01:00
|
|
|
|
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)
|
|
|
|
{
|
2013-08-11 04:09:45 +02:00
|
|
|
GameScripting *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();
|
2013-05-25 00:51:02 +02:00
|
|
|
assert(lua_checkstack(L, 20));
|
|
|
|
StackUnroller stack_unroller(L);
|
|
|
|
|
2013-11-05 18:06:15 +01:00
|
|
|
lua_pushcfunction(L, script_error_handler);
|
|
|
|
int errorhandler = lua_gettop(L);
|
|
|
|
|
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]
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushnumber(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))
|
|
|
|
assert(0);
|
2014-04-28 03:02:48 +02:00
|
|
|
lua_remove(L, -2); // Remove registered_abms
|
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);
|
2013-11-05 18:06:15 +01:00
|
|
|
if(lua_pcall(L, 4, 0, errorhandler))
|
|
|
|
script_error(L);
|
|
|
|
lua_pop(L, 1); // Pop error handler
|
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;
|
|
|
|
|
|
|
|
INodeDefManager *ndef = env->getGameDef()->ndef();
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
INodeDefManager *ndef = env->getGameDef()->ndef();
|
|
|
|
// 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
|
|
|
|
MapNode n = env->getMap().getNodeNoEx(pos);
|
|
|
|
// 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
|
|
|
|
try{
|
|
|
|
MapNode n = env->getMap().getNode(pos);
|
|
|
|
// Return node
|
|
|
|
pushnode(L, n, env->getGameDef()->ndef());
|
|
|
|
return 1;
|
|
|
|
} catch(InvalidPositionException &e)
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
GET_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);
|
|
|
|
try{
|
|
|
|
MapNode n = env->getMap().getNode(pos);
|
|
|
|
INodeDefManager *ndef = env->getGameDef()->ndef();
|
|
|
|
lua_pushinteger(L, n.getLightBlend(dnr, ndef));
|
|
|
|
return 1;
|
|
|
|
} catch(InvalidPositionException &e)
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
|
|
|
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);
|
|
|
|
INodeDefManager *ndef = server->ndef();
|
|
|
|
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
|
|
|
|
MapNode n_old = env->getMap().getNodeNoEx(pos);
|
|
|
|
if(n_old.getContent() == CONTENT_IGNORE){
|
|
|
|
lua_pushboolean(L, false);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Create item to place
|
|
|
|
ItemStack item(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 a non-functional
|
|
|
|
// ObjectRef)
|
2013-08-11 04:09:45 +02:00
|
|
|
bool success = scriptIfaceItem->item_OnPlace(item, NULL, 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
|
|
|
|
MapNode n = env->getMap().getNodeNoEx(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)
|
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
|
|
|
|
MapNode n = env->getMap().getNodeNoEx(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)
|
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)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
|
|
|
MapNode n = env->getMap().getNodeNoEx(pos);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
|
|
|
MapNode n = env->getMap().getNodeNoEx(pos);
|
|
|
|
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);
|
|
|
|
MapNode n = env->getMap().getNodeNoEx(pos);
|
|
|
|
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}
|
2013-07-28 15:11:59 +02:00
|
|
|
// level: 0..63
|
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);
|
|
|
|
u8 level = 1;
|
|
|
|
if(lua_isnumber(L, 2))
|
|
|
|
level = lua_tonumber(L, 2);
|
|
|
|
MapNode n = env->getMap().getNodeNoEx(pos);
|
|
|
|
lua_pushnumber(L, n.addLevel(env->getGameDef()->ndef(), level));
|
|
|
|
env->setNode(pos, n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
NodeTimerRef::create(L, p, env);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// add_entity(pos, entityname) -> 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;
|
|
|
|
|
|
|
|
// pos
|
|
|
|
v3f pos = checkFloatPos(L, 1);
|
|
|
|
// content
|
|
|
|
const char *name = luaL_checkstring(L, 2);
|
|
|
|
// Do it
|
|
|
|
ServerActiveObject *obj = new LuaEntitySAO(env, pos, name, "");
|
|
|
|
int objectid = env->addActiveObject(obj);
|
|
|
|
// If failed to add, return nothing (reads as nil)
|
|
|
|
if(objectid == 0)
|
|
|
|
return 0;
|
|
|
|
// Return ObjectRef
|
2013-08-11 04:09:45 +02:00
|
|
|
getScriptApiBase(L)->objectrefGetOrCreate(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
|
|
|
|
ItemStack item = read_item(L, 2,getServer(L));
|
|
|
|
if(item.empty() || !item.isKnown(getServer(L)->idef()))
|
|
|
|
return 0;
|
2013-11-05 18:06:15 +01:00
|
|
|
|
|
|
|
lua_pushcfunction(L, script_error_handler);
|
|
|
|
int errorhandler = lua_gettop(L);
|
|
|
|
|
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());
|
2013-11-05 18:06:15 +01:00
|
|
|
if(lua_pcall(L, 2, 1, errorhandler))
|
|
|
|
script_error(L);
|
|
|
|
lua_remove(L, errorhandler); // Remove error handler
|
2013-05-25 00:51:02 +02:00
|
|
|
return 1;
|
|
|
|
/*lua_pushvalue(L, 1);
|
|
|
|
lua_pushstring(L, "__builtin:item");
|
|
|
|
lua_pushstring(L, item.getItemString().c_str());
|
|
|
|
return l_add_entity(L);*/
|
|
|
|
/*// Do it
|
|
|
|
ServerActiveObject *obj = createItemSAO(env, pos, item.getItemString());
|
|
|
|
int objectid = env->addActiveObject(obj);
|
|
|
|
// If failed to add, return nothing (reads as nil)
|
|
|
|
if(objectid == 0)
|
|
|
|
return 0;
|
|
|
|
// Return ObjectRef
|
|
|
|
objectrefGetOrCreate(L, obj);
|
|
|
|
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);
|
|
|
|
Player *player = env->getPlayer(name);
|
|
|
|
if(player == NULL){
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
PlayerSAO *sao = player->getPlayerSAO();
|
|
|
|
if(sao == NULL){
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// Put player on stack
|
2013-08-11 04:09:45 +02:00
|
|
|
getScriptApiBase(L)->objectrefGetOrCreate(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;
|
|
|
|
|
|
|
|
// Do it
|
|
|
|
v3f pos = checkFloatPos(L, 1);
|
|
|
|
float radius = luaL_checknumber(L, 2) * BS;
|
|
|
|
std::set<u16> ids = env->getObjectsInsideRadius(pos, radius);
|
2013-11-30 18:11:07 +01:00
|
|
|
ScriptApiBase *script = getScriptApiBase(L);
|
|
|
|
lua_createtable(L, ids.size(), 0);
|
|
|
|
std::set<u16>::const_iterator iter = ids.begin();
|
|
|
|
for(u32 i = 0; iter != ids.end(); iter++) {
|
|
|
|
ServerActiveObject *obj = env->getActiveObject(*iter);
|
2013-05-25 00:51:02 +02:00
|
|
|
// Insert object reference into table
|
2013-11-30 18:11:07 +01:00
|
|
|
script->objectrefGetOrCreate(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
|
|
|
|
float timeofday_f = luaL_checknumber(L, 1);
|
|
|
|
assert(timeofday_f >= 0.0 && timeofday_f <= 1.0);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
// Do it
|
|
|
|
int timeofday_mh = env->getTimeOfDay();
|
|
|
|
float timeofday_f = (float)timeofday_mh / 24000.0;
|
|
|
|
lua_pushnumber(L, timeofday_f);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// find_node_near(pos, radius, nodenames) -> pos or nil
|
2013-05-25 00:51:02 +02:00
|
|
|
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
|
|
|
|
int ModApiEnvMod::l_find_node_near(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
INodeDefManager *ndef = getServer(L)->ndef();
|
|
|
|
v3s16 pos = read_v3s16(L, 1);
|
|
|
|
int radius = luaL_checkinteger(L, 2);
|
|
|
|
std::set<content_t> filter;
|
|
|
|
if(lua_istable(L, 3)){
|
|
|
|
int table = 3;
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(lua_next(L, table) != 0){
|
|
|
|
// key at index -2 and value at index -1
|
|
|
|
luaL_checktype(L, -1, LUA_TSTRING);
|
|
|
|
ndef->getIds(lua_tostring(L, -1), filter);
|
|
|
|
// removes value, keeps key for next iteration
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
} else if(lua_isstring(L, 3)){
|
|
|
|
ndef->getIds(lua_tostring(L, 3), filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int d=1; d<=radius; d++){
|
|
|
|
std::list<v3s16> list;
|
|
|
|
getFacePositions(list, d);
|
|
|
|
for(std::list<v3s16>::iterator i = list.begin();
|
|
|
|
i != list.end(); ++i){
|
|
|
|
v3s16 p = pos + (*i);
|
|
|
|
content_t c = env->getMap().getNodeNoEx(p).getContent();
|
|
|
|
if(filter.count(c) != 0){
|
|
|
|
push_v3s16(L, p);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// find_nodes_in_area(minp, maxp, nodenames) -> list of positions
|
2013-05-25 00:51:02 +02:00
|
|
|
// nodenames: eg. {"ignore", "group:tree"} or "default:dirt"
|
|
|
|
int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
INodeDefManager *ndef = getServer(L)->ndef();
|
|
|
|
v3s16 minp = read_v3s16(L, 1);
|
|
|
|
v3s16 maxp = read_v3s16(L, 2);
|
|
|
|
std::set<content_t> filter;
|
|
|
|
if(lua_istable(L, 3)){
|
|
|
|
int table = 3;
|
|
|
|
lua_pushnil(L);
|
|
|
|
while(lua_next(L, table) != 0){
|
|
|
|
// key at index -2 and value at index -1
|
|
|
|
luaL_checktype(L, -1, LUA_TSTRING);
|
|
|
|
ndef->getIds(lua_tostring(L, -1), filter);
|
|
|
|
// removes value, keeps key for next iteration
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
} else if(lua_isstring(L, 3)){
|
|
|
|
ndef->getIds(lua_tostring(L, 3), filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_newtable(L);
|
2013-11-30 18:11:07 +01:00
|
|
|
u64 i = 0;
|
|
|
|
for(s16 x = minp.X; x <= maxp.X; x++)
|
|
|
|
for(s16 y = minp.Y; y <= maxp.Y; y++)
|
|
|
|
for(s16 z = minp.Z; z <= maxp.Z; z++) {
|
|
|
|
v3s16 p(x, y, z);
|
2013-05-25 00:51:02 +02:00
|
|
|
content_t c = env->getMap().getNodeNoEx(p).getContent();
|
2013-11-30 18:11:07 +01:00
|
|
|
if(filter.count(c) != 0) {
|
2013-05-25 00:51:02 +02:00
|
|
|
push_v3s16(L, p);
|
2013-11-30 18:11:07 +01:00
|
|
|
lua_rawseti(L, -2, ++i);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
int seeddiff = luaL_checkint(L, 1);
|
|
|
|
int octaves = luaL_checkint(L, 2);
|
|
|
|
float persistence = luaL_checknumber(L, 3);
|
|
|
|
float scale = luaL_checknumber(L, 4);
|
|
|
|
|
|
|
|
LuaPerlinNoise *n = new LuaPerlinNoise(seeddiff + int(env->getServerMap().getSeed()), octaves, persistence, scale);
|
|
|
|
*(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)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
NoiseParams *np = read_noiseparams(L, 1);
|
|
|
|
if (!np)
|
|
|
|
return 0;
|
|
|
|
v3s16 size = read_v3s16(L, 2);
|
|
|
|
|
|
|
|
int seed = (int)(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;
|
|
|
|
}
|
|
|
|
|
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());
|
2013-06-26 23:19:39 +02:00
|
|
|
LuaVoxelManip *o = new LuaVoxelManip(map);
|
2013-11-25 00:03:03 +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;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// clear_objects()
|
2013-05-25 00:51:02 +02:00
|
|
|
// clear all objects in the environment
|
|
|
|
int ModApiEnvMod::l_clear_objects(lua_State *L)
|
|
|
|
{
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
env->clearAllObjects();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// line_of_sight(pos1, pos2, stepsize) -> true/false, pos
|
2013-05-25 00:51:02 +02:00
|
|
|
int ModApiEnvMod::l_line_of_sight(lua_State *L) {
|
|
|
|
float stepsize = 1.0;
|
|
|
|
|
|
|
|
GET_ENV_PTR;
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
//read step size from lua
|
2013-11-25 00:03:03 +01:00
|
|
|
if (lua_isnumber(L, 3)) {
|
2013-06-25 17:02:02 +02:00
|
|
|
stepsize = lua_tonumber(L, 3);
|
2013-11-25 00:03:03 +01:00
|
|
|
}
|
2013-05-25 00:51:02 +02:00
|
|
|
|
2013-12-11 21:33:39 +01:00
|
|
|
v3s16 p;
|
|
|
|
bool success = env->line_of_sight(pos1, pos2, stepsize, &p);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
2013-05-25 00:51:02 +02:00
|
|
|
algorithm algo = A_PLAIN_NP;
|
2013-06-25 17:02:02 +02:00
|
|
|
if (!lua_isnil(L, 6)) {
|
|
|
|
std::string algorithm = luaL_checkstring(L,6);
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
if (algorithm == "A*")
|
|
|
|
algo = A_PLAIN;
|
|
|
|
|
|
|
|
if (algorithm == "Dijkstra")
|
|
|
|
algo = DIJKSTRA;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<v3s16> path =
|
|
|
|
get_Path(env,pos1,pos2,searchdistance,max_jump,max_drop,algo);
|
|
|
|
|
|
|
|
if (path.size() > 0)
|
|
|
|
{
|
|
|
|
lua_newtable(L);
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
unsigned int index = 1;
|
|
|
|
for (std::vector<v3s16>::iterator i = path.begin(); i != path.end();i++)
|
|
|
|
{
|
|
|
|
lua_pushnumber(L,index);
|
|
|
|
push_v3s16(L, *i);
|
|
|
|
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;
|
|
|
|
INodeDefManager *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 !="")
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
getintfield(L, 2, "random_level", tree_def.iterations_random_level);
|
|
|
|
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 != "")
|
|
|
|
{
|
|
|
|
tree_def.fruitnode=ndef->getId(fruit);
|
|
|
|
getintfield(L, 2, "fruit_chance",tree_def.fruit_chance);
|
|
|
|
}
|
|
|
|
getintfield(L, 2, "seed", tree_def.seed);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
treegen::spawn_ltree (env, p0, ndef, tree_def);
|
|
|
|
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
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
void ModApiEnvMod::Initialize(lua_State *L, int top)
|
|
|
|
{
|
|
|
|
API_FCT(set_node);
|
|
|
|
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);
|
|
|
|
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(get_meta);
|
|
|
|
API_FCT(get_node_timer);
|
|
|
|
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);
|
2013-08-11 04:09:45 +02:00
|
|
|
API_FCT(find_node_near);
|
|
|
|
API_FCT(find_nodes_in_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(transforming_liquid_add);
|
2014-01-11 17:54:00 +01:00
|
|
|
API_FCT(forceload_block);
|
|
|
|
API_FCT(forceload_free_block);
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|