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 "cpp_api/s_node.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "cpp_api/s_internal.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "common/c_converter.h"
|
|
|
|
#include "common/c_content.h"
|
|
|
|
#include "nodedef.h"
|
|
|
|
#include "server.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "environment.h"
|
2014-01-24 01:21:01 +01:00
|
|
|
#include "util/pointedthing.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
|
2017-06-10 13:24:05 +02:00
|
|
|
// Should be ordered exactly like enum NodeDrawType in nodedef.h
|
2013-05-25 00:51:02 +02:00
|
|
|
struct EnumString ScriptApiNode::es_DrawType[] =
|
|
|
|
{
|
|
|
|
{NDT_NORMAL, "normal"},
|
|
|
|
{NDT_AIRLIKE, "airlike"},
|
|
|
|
{NDT_LIQUID, "liquid"},
|
|
|
|
{NDT_FLOWINGLIQUID, "flowingliquid"},
|
|
|
|
{NDT_GLASSLIKE, "glasslike"},
|
|
|
|
{NDT_ALLFACES, "allfaces"},
|
|
|
|
{NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
|
|
|
|
{NDT_TORCHLIKE, "torchlike"},
|
|
|
|
{NDT_SIGNLIKE, "signlike"},
|
|
|
|
{NDT_PLANTLIKE, "plantlike"},
|
|
|
|
{NDT_FENCELIKE, "fencelike"},
|
|
|
|
{NDT_RAILLIKE, "raillike"},
|
|
|
|
{NDT_NODEBOX, "nodebox"},
|
2017-06-10 13:24:05 +02:00
|
|
|
{NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
|
|
|
|
{NDT_FIRELIKE, "firelike"},
|
|
|
|
{NDT_GLASSLIKE_FRAMED_OPTIONAL, "glasslike_framed_optional"},
|
2014-10-15 04:13:53 +02:00
|
|
|
{NDT_MESH, "mesh"},
|
2017-05-11 22:24:12 +02:00
|
|
|
{NDT_PLANTLIKE_ROOTED, "plantlike_rooted"},
|
2013-05-25 00:51:02 +02:00
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EnumString ScriptApiNode::es_ContentParamType2[] =
|
|
|
|
{
|
|
|
|
{CPT2_NONE, "none"},
|
|
|
|
{CPT2_FULL, "full"},
|
|
|
|
{CPT2_FLOWINGLIQUID, "flowingliquid"},
|
|
|
|
{CPT2_FACEDIR, "facedir"},
|
|
|
|
{CPT2_WALLMOUNTED, "wallmounted"},
|
2013-07-13 19:48:14 +02:00
|
|
|
{CPT2_LEVELED, "leveled"},
|
2015-10-02 23:36:28 +02:00
|
|
|
{CPT2_DEGROTATE, "degrotate"},
|
2015-12-11 07:58:11 +01:00
|
|
|
{CPT2_MESHOPTIONS, "meshoptions"},
|
2017-01-12 15:46:30 +01:00
|
|
|
{CPT2_COLOR, "color"},
|
|
|
|
{CPT2_COLORED_FACEDIR, "colorfacedir"},
|
|
|
|
{CPT2_COLORED_WALLMOUNTED, "colorwallmounted"},
|
2017-03-29 04:40:30 +02:00
|
|
|
{CPT2_GLASSLIKE_LIQUID_LEVEL, "glasslikeliquidlevel"},
|
2021-03-30 00:25:11 +02:00
|
|
|
{CPT2_COLORED_DEGROTATE, "colordegrotate"},
|
2022-09-16 13:18:55 +02:00
|
|
|
{CPT2_4DIR, "4dir"},
|
|
|
|
{CPT2_COLORED_4DIR, "color4dir"},
|
2013-05-25 00:51:02 +02:00
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EnumString ScriptApiNode::es_LiquidType[] =
|
|
|
|
{
|
|
|
|
{LIQUID_NONE, "none"},
|
|
|
|
{LIQUID_FLOWING, "flowing"},
|
|
|
|
{LIQUID_SOURCE, "source"},
|
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EnumString ScriptApiNode::es_ContentParamType[] =
|
|
|
|
{
|
|
|
|
{CPT_NONE, "none"},
|
|
|
|
{CPT_LIGHT, "light"},
|
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EnumString ScriptApiNode::es_NodeBoxType[] =
|
|
|
|
{
|
|
|
|
{NODEBOX_REGULAR, "regular"},
|
|
|
|
{NODEBOX_FIXED, "fixed"},
|
|
|
|
{NODEBOX_WALLMOUNTED, "wallmounted"},
|
2013-07-13 19:48:14 +02:00
|
|
|
{NODEBOX_LEVELED, "leveled"},
|
Nodebox: Allow nodeboxes to "connect"
We introduce a new nodebox type "connected", and allow these nodes to
have optional nodeboxes that connect it to other connecting nodeboxes.
This is all done at scenedraw time in the client. The client will
inspect the surrounding nodes and if they are to be connected to,
it will draw the appropriate connecting nodeboxes to make those
connections.
In the node_box definition, we have to specify separate nodeboxes for
each valid connection. This allows us to make nodes that connect only
horizontally (the common case) by providing optional nodeboxes for +x,
-x, +z, -z directions. Or this allows us to make wires that can connect
up and down, by providing nodeboxes that connect it up and down (+y,
-y) as well.
The optional nodeboxes can be arrays. They are named "connect_top,
"connect_bottom", "connect_front", "connect_left", "connect_back" and
"connect_right". Here, "front" means the south facing side of the node
that has facedir = 0.
Additionally, a "fixed" nodebox list present will always be drawn,
so one can make a central post, for instance. This "fixed" nodebox
can be omitted, or it can be an array of nodeboxes.
Collision boxes are also updated in exactly the same fashion, which
allows you to walk over the upper extremities of the individual
node boxes, or stand really close to them. You can also walk up
node noxes that are small in height, all as expected, and unlike the
NDT_FENCELIKE nodes.
I've posted a screenshot demonstrating the flexibility at
http://i.imgur.com/zaJq8jo.png
In the screenshot, all connecting nodes are of this new subtype.
Transparent textures render incorrectly, Which I don't think is
related to this text, as other nodeboxes also have issues with this.
A protocol bump is performed in order to be able to send older clients
a nodeblock that is usable for them. In order to avoid abuse of users
we send older clients a "full-size" node, so that it's impossible for
them to try and walk through a fence or wall that's created in this
fashion. This was tested with a pre-bump client connected against a
server running the new protocol.
These nodes connect to other nodes, and you can select which ones
those are by specifying node names (or group names) in the
connects_to string array:
connects_to = { "group:fence", "default:wood" }
By default, nodes do not connect to anything, allowing you to create
nodes that always have to be paired in order to connect. lua_api.txt
is updated to reflect the extension to the node_box API.
Example lua code needed to generate these nodes can be found here:
https://gist.github.com/sofar/b381c8c192c8e53e6062
2016-02-25 09:16:31 +01:00
|
|
|
{NODEBOX_CONNECTED, "connected"},
|
2013-05-25 00:51:02 +02:00
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
2021-01-17 01:56:50 +01:00
|
|
|
struct EnumString ScriptApiNode::es_TextureAlphaMode[] =
|
|
|
|
{
|
|
|
|
{ALPHAMODE_OPAQUE, "opaque"},
|
|
|
|
{ALPHAMODE_CLIP, "clip"},
|
|
|
|
{ALPHAMODE_BLEND, "blend"},
|
|
|
|
{0, NULL},
|
|
|
|
};
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
|
2020-05-26 17:38:31 +02:00
|
|
|
ServerActiveObject *puncher, const PointedThing &pointed)
|
2013-05-25 00:51:02 +02:00
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = getServer()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// Push callback function on stack
|
2017-10-07 15:11:07 +02:00
|
|
|
if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch", &p))
|
2013-05-25 00:51:02 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Call function
|
|
|
|
push_v3s16(L, p);
|
2022-10-19 00:01:44 +02:00
|
|
|
pushnode(L, node);
|
2014-10-02 21:58:13 +02:00
|
|
|
objectrefGetOrCreate(L, puncher);
|
2014-01-24 01:21:01 +01:00
|
|
|
pushPointedThing(pointed);
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 4, 0, error_handler));
|
|
|
|
lua_pop(L, 1); // Pop error handler
|
2013-05-25 00:51:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
|
|
|
|
ServerActiveObject *digger)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = getServer()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// Push callback function on stack
|
2017-10-07 15:11:07 +02:00
|
|
|
if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig", &p))
|
2013-05-25 00:51:02 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Call function
|
|
|
|
push_v3s16(L, p);
|
2022-10-19 00:01:44 +02:00
|
|
|
pushnode(L, node);
|
2014-10-02 21:58:13 +02:00
|
|
|
objectrefGetOrCreate(L, digger);
|
2021-01-31 19:49:51 +01:00
|
|
|
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
|
|
|
|
|
|
|
|
// nil is treated as true for backwards compat
|
|
|
|
bool result = lua_isnil(L, -1) || lua_toboolean(L, -1);
|
|
|
|
|
|
|
|
lua_pop(L, 2); // Pop error handler and result
|
|
|
|
|
|
|
|
return result;
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = getServer()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// Push callback function on stack
|
2017-10-07 15:11:07 +02:00
|
|
|
if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct", &p))
|
2013-05-25 00:51:02 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Call function
|
|
|
|
push_v3s16(L, p);
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 1, 0, error_handler));
|
|
|
|
lua_pop(L, 1); // Pop error handler
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = getServer()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// Push callback function on stack
|
2017-10-07 15:11:07 +02:00
|
|
|
if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct", &p))
|
2013-05-25 00:51:02 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Call function
|
|
|
|
push_v3s16(L, p);
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 1, 0, error_handler));
|
|
|
|
lua_pop(L, 1); // Pop error handler
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
2017-04-20 08:10:39 +02:00
|
|
|
bool ScriptApiNode::node_on_flood(v3s16 p, MapNode node, MapNode newnode)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = getServer()->ndef();
|
2017-04-20 08:10:39 +02:00
|
|
|
|
|
|
|
// Push callback function on stack
|
2017-10-07 15:11:07 +02:00
|
|
|
if (!getItemCallback(ndef->get(node).name.c_str(), "on_flood", &p))
|
2017-04-20 08:10:39 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Call function
|
|
|
|
push_v3s16(L, p);
|
2022-10-19 00:01:44 +02:00
|
|
|
pushnode(L, node);
|
|
|
|
pushnode(L, newnode);
|
2017-04-20 08:10:39 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
|
|
|
|
lua_remove(L, error_handler);
|
2018-06-30 17:11:38 +02:00
|
|
|
return readParam<bool>(L, -1, false);
|
2017-04-20 08:10:39 +02:00
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = getServer()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// Push callback function on stack
|
2017-10-07 15:11:07 +02:00
|
|
|
if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct", &p))
|
2013-05-25 00:51:02 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Call function
|
|
|
|
push_v3s16(L, p);
|
2022-10-19 00:01:44 +02:00
|
|
|
pushnode(L, node);
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 2, 0, error_handler));
|
|
|
|
lua_pop(L, 1); // Pop error handler
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = getServer()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// Push callback function on stack
|
2017-10-07 15:11:07 +02:00
|
|
|
if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer", &p))
|
2013-05-25 00:51:02 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Call function
|
|
|
|
push_v3s16(L, p);
|
|
|
|
lua_pushnumber(L,dtime);
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 2, 1, error_handler));
|
|
|
|
lua_remove(L, error_handler);
|
2018-06-30 17:11:38 +02:00
|
|
|
return readParam<bool>(L, -1, false);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiNode::node_on_receive_fields(v3s16 p,
|
|
|
|
const std::string &formname,
|
2015-05-19 08:24:14 +02:00
|
|
|
const StringMap &fields,
|
2013-05-25 00:51:02 +02:00
|
|
|
ServerActiveObject *sender)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2018-02-10 21:04:16 +01:00
|
|
|
const NodeDefManager *ndef = getServer()->ndef();
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
// If node doesn't exist, we don't know what callback to call
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode node = getEnv()->getMap().getNode(p);
|
2014-04-15 19:30:46 +02:00
|
|
|
if (node.getContent() == CONTENT_IGNORE)
|
2013-05-25 00:51:02 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Push callback function on stack
|
2017-10-07 15:11:07 +02:00
|
|
|
if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields", &p))
|
2013-05-25 00:51:02 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Call function
|
2013-11-05 18:06:15 +01:00
|
|
|
push_v3s16(L, p); // pos
|
|
|
|
lua_pushstring(L, formname.c_str()); // formname
|
|
|
|
lua_newtable(L); // fields
|
2015-05-19 08:24:14 +02:00
|
|
|
StringMap::const_iterator it;
|
2017-04-25 10:17:53 +02:00
|
|
|
for (it = fields.begin(); it != fields.end(); ++it) {
|
2014-04-15 19:30:46 +02:00
|
|
|
const std::string &name = it->first;
|
|
|
|
const std::string &value = it->second;
|
2013-05-25 00:51:02 +02:00
|
|
|
lua_pushstring(L, name.c_str());
|
|
|
|
lua_pushlstring(L, value.c_str(), value.size());
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
2014-10-02 21:58:13 +02:00
|
|
|
objectrefGetOrCreate(L, sender); // player
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 4, 0, error_handler));
|
|
|
|
lua_pop(L, 1); // Pop error handler
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|