]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/cpp_api/s_node.cpp
Order es_DrawType exactly like enum NodeDrawType in nodedef.h (#5946)
[dragonfireclient.git] / src / script / cpp_api / s_node.cpp
index 8c9b46c2a88277990f7c7af197fda7393da30dd3..d1b2723dfa21baad418b91c6a903b72735d90f2d 100644 (file)
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "util/pointedthing.h"
 
 
+// Should be ordered exactly like enum NodeDrawType in nodedef.h
 struct EnumString ScriptApiNode::es_DrawType[] =
        {
                {NDT_NORMAL, "normal"},
@@ -34,17 +35,18 @@ struct EnumString ScriptApiNode::es_DrawType[] =
                {NDT_LIQUID, "liquid"},
                {NDT_FLOWINGLIQUID, "flowingliquid"},
                {NDT_GLASSLIKE, "glasslike"},
-               {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
-               {NDT_GLASSLIKE_FRAMED_OPTIONAL, "glasslike_framed_optional"},
                {NDT_ALLFACES, "allfaces"},
                {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
                {NDT_TORCHLIKE, "torchlike"},
                {NDT_SIGNLIKE, "signlike"},
                {NDT_PLANTLIKE, "plantlike"},
-               {NDT_FIRELIKE, "firelike"},
                {NDT_FENCELIKE, "fencelike"},
                {NDT_RAILLIKE, "raillike"},
                {NDT_NODEBOX, "nodebox"},
+               {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
+               {NDT_FIRELIKE, "firelike"},
+               {NDT_GLASSLIKE_FRAMED_OPTIONAL, "glasslike_framed_optional"},
+               {NDT_MESH, "mesh"},
                {0, NULL},
        };
 
@@ -56,6 +58,12 @@ struct EnumString ScriptApiNode::es_ContentParamType2[] =
                {CPT2_FACEDIR, "facedir"},
                {CPT2_WALLMOUNTED, "wallmounted"},
                {CPT2_LEVELED, "leveled"},
+               {CPT2_DEGROTATE, "degrotate"},
+               {CPT2_MESHOPTIONS, "meshoptions"},
+               {CPT2_COLOR, "color"},
+               {CPT2_COLORED_FACEDIR, "colorfacedir"},
+               {CPT2_COLORED_WALLMOUNTED, "colorwallmounted"},
+               {CPT2_GLASSLIKE_LIQUID_LEVEL, "glasslikeliquidlevel"},
                {0, NULL},
        };
 
@@ -80,6 +88,7 @@ struct EnumString ScriptApiNode::es_NodeBoxType[] =
                {NODEBOX_FIXED, "fixed"},
                {NODEBOX_WALLMOUNTED, "wallmounted"},
                {NODEBOX_LEVELED, "leveled"},
+               {NODEBOX_CONNECTED, "connected"},
                {0, NULL},
        };
 
@@ -94,6 +103,8 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        INodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
@@ -103,10 +114,10 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
        // Call function
        push_v3s16(L, p);
        pushnode(L, node, ndef);
-       objectrefGetOrCreate(puncher);
+       objectrefGetOrCreate(L, puncher);
        pushPointedThing(pointed);
-       if (lua_pcall(L, 4, 0, m_errorhandler))
-               scriptError();
+       PCALL_RES(lua_pcall(L, 4, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
        return true;
 }
 
@@ -115,6 +126,8 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        INodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
@@ -124,9 +137,9 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
        // Call function
        push_v3s16(L, p);
        pushnode(L, node, ndef);
-       objectrefGetOrCreate(digger);
-       if (lua_pcall(L, 3, 0, m_errorhandler))
-               scriptError();
+       objectrefGetOrCreate(L, digger);
+       PCALL_RES(lua_pcall(L, 3, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
        return true;
 }
 
@@ -134,6 +147,8 @@ void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        INodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
@@ -142,14 +157,16 @@ void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
 
        // Call function
        push_v3s16(L, p);
-       if (lua_pcall(L, 1, 0, m_errorhandler))
-               scriptError();
+       PCALL_RES(lua_pcall(L, 1, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        INodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
@@ -158,14 +175,37 @@ void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
 
        // Call function
        push_v3s16(L, p);
-       if (lua_pcall(L, 1, 0, m_errorhandler))
-               scriptError();
+       PCALL_RES(lua_pcall(L, 1, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
+}
+
+bool ScriptApiNode::node_on_flood(v3s16 p, MapNode node, MapNode newnode)
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       INodeDefManager *ndef = getServer()->ndef();
+
+       // Push callback function on stack
+       if (!getItemCallback(ndef->get(node).name.c_str(), "on_flood"))
+               return false;
+
+       // Call function
+       push_v3s16(L, p);
+       pushnode(L, node, ndef);
+       pushnode(L, newnode, ndef);
+       PCALL_RES(lua_pcall(L, 3, 1, error_handler));
+       lua_remove(L, error_handler);
+       return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
 }
 
 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        INodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
@@ -175,14 +215,16 @@ void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
        // Call function
        push_v3s16(L, p);
        pushnode(L, node, ndef);
-       if (lua_pcall(L, 2, 0, m_errorhandler))
-               scriptError();
+       PCALL_RES(lua_pcall(L, 2, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        INodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
@@ -192,18 +234,20 @@ bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
        // Call function
        push_v3s16(L, p);
        lua_pushnumber(L,dtime);
-       if (lua_pcall(L, 2, 1, m_errorhandler))
-               scriptError();
+       PCALL_RES(lua_pcall(L, 2, 1, error_handler));
+       lua_remove(L, error_handler);
        return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
 }
 
 void ScriptApiNode::node_on_receive_fields(v3s16 p,
                const std::string &formname,
-               const std::map<std::string, std::string> &fields,
+               const StringMap &fields,
                ServerActiveObject *sender)
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        INodeDefManager *ndef = getServer()->ndef();
 
        // If node doesn't exist, we don't know what callback to call
@@ -219,36 +263,39 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
        push_v3s16(L, p);                    // pos
        lua_pushstring(L, formname.c_str()); // formname
        lua_newtable(L);                     // fields
-       std::map<std::string, std::string>::const_iterator it;
-       for (it = fields.begin(); it != fields.end(); it++){
+       StringMap::const_iterator it;
+       for (it = fields.begin(); it != fields.end(); ++it) {
                const std::string &name = it->first;
                const std::string &value = it->second;
                lua_pushstring(L, name.c_str());
                lua_pushlstring(L, value.c_str(), value.size());
                lua_settable(L, -3);
        }
-       objectrefGetOrCreate(sender);        // player
-       if (lua_pcall(L, 4, 0, m_errorhandler))
-               scriptError();
+       objectrefGetOrCreate(L, sender);        // player
+       PCALL_RES(lua_pcall(L, 4, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 void ScriptApiNode::node_falling_update(v3s16 p)
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        lua_getglobal(L, "nodeupdate");
        push_v3s16(L, p);
-       if (lua_pcall(L, 1, 0, m_errorhandler))
-               scriptError();
+       PCALL_RES(lua_pcall(L, 1, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
 
 void ScriptApiNode::node_falling_update_single(v3s16 p)
 {
        SCRIPTAPI_PRECHECKHEADER
 
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
        lua_getglobal(L, "nodeupdate_single");
        push_v3s16(L, p);
-       if (lua_pcall(L, 1, 0, m_errorhandler))
-               scriptError();
+       PCALL_RES(lua_pcall(L, 1, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
-