]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/cpp_api/s_node.cpp
Add player inventory callbacks
[dragonfireclient.git] / src / script / cpp_api / s_node.cpp
index 49a825cacb690b40c583ef7905800e57500ea0ba..11c08811fe50e890aeb4e70f56f7acfbc653dbac 100644 (file)
@@ -18,12 +18,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "cpp_api/s_node.h"
+#include "cpp_api/s_internal.h"
 #include "common/c_converter.h"
 #include "common/c_content.h"
 #include "nodedef.h"
 #include "server.h"
+#include "environment.h"
+#include "util/pointedthing.h"
 
 
+// Should be ordered exactly like enum NodeDrawType in nodedef.h
 struct EnumString ScriptApiNode::es_DrawType[] =
        {
                {NDT_NORMAL, "normal"},
@@ -31,7 +35,6 @@ struct EnumString ScriptApiNode::es_DrawType[] =
                {NDT_LIQUID, "liquid"},
                {NDT_FLOWINGLIQUID, "flowingliquid"},
                {NDT_GLASSLIKE, "glasslike"},
-               {NDT_GLASSLIKE_FRAMED, "glasslike_framed"},
                {NDT_ALLFACES, "allfaces"},
                {NDT_ALLFACES_OPTIONAL, "allfaces_optional"},
                {NDT_TORCHLIKE, "torchlike"},
@@ -40,6 +43,11 @@ struct EnumString ScriptApiNode::es_DrawType[] =
                {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"},
+               {NDT_PLANTLIKE_ROOTED, "plantlike_rooted"},
                {0, NULL},
        };
 
@@ -51,6 +59,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},
        };
 
@@ -75,32 +89,30 @@ struct EnumString ScriptApiNode::es_NodeBoxType[] =
                {NODEBOX_FIXED, "fixed"},
                {NODEBOX_WALLMOUNTED, "wallmounted"},
                {NODEBOX_LEVELED, "leveled"},
+               {NODEBOX_CONNECTED, "connected"},
                {0, NULL},
        };
 
-ScriptApiNode::ScriptApiNode() {
-}
-
-ScriptApiNode::~ScriptApiNode() {
-}
-
 bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
-               ServerActiveObject *puncher)
+               ServerActiveObject *puncher, PointedThing pointed)
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       INodeDefManager *ndef = getServer()->ndef();
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       const NodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
-       if(!getItemCallback(ndef->get(node).name.c_str(), "on_punch"))
+       if (!getItemCallback(ndef->get(node).name.c_str(), "on_punch", &p))
                return false;
 
        // Call function
        push_v3s16(L, p);
        pushnode(L, node, ndef);
-       objectrefGetOrCreate(puncher);
-       if(lua_pcall(L, 3, 0, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
+       objectrefGetOrCreate(L, puncher);
+       pushPointedThing(pointed);
+       PCALL_RES(lua_pcall(L, 4, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
        return true;
 }
 
@@ -109,18 +121,20 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       INodeDefManager *ndef = getServer()->ndef();
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       const NodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
-       if(!getItemCallback(ndef->get(node).name.c_str(), "on_dig"))
+       if (!getItemCallback(ndef->get(node).name.c_str(), "on_dig", &p))
                return false;
 
        // Call function
        push_v3s16(L, p);
        pushnode(L, node, ndef);
-       objectrefGetOrCreate(digger);
-       if(lua_pcall(L, 3, 0, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
+       objectrefGetOrCreate(L, digger);
+       PCALL_RES(lua_pcall(L, 3, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
        return true;
 }
 
@@ -128,108 +142,131 @@ void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       INodeDefManager *ndef = getServer()->ndef();
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       const NodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
-       if(!getItemCallback(ndef->get(node).name.c_str(), "on_construct"))
+       if (!getItemCallback(ndef->get(node).name.c_str(), "on_construct", &p))
                return;
 
        // Call function
        push_v3s16(L, p);
-       if(lua_pcall(L, 1, 0, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
+       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
 
-       INodeDefManager *ndef = getServer()->ndef();
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       const NodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
-       if(!getItemCallback(ndef->get(node).name.c_str(), "on_destruct"))
+       if (!getItemCallback(ndef->get(node).name.c_str(), "on_destruct", &p))
                return;
 
        // Call function
        push_v3s16(L, p);
-       if(lua_pcall(L, 1, 0, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
+       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);
+
+       const NodeDefManager *ndef = getServer()->ndef();
+
+       // Push callback function on stack
+       if (!getItemCallback(ndef->get(node).name.c_str(), "on_flood", &p))
+               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);
 }
 
 void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
 {
        SCRIPTAPI_PRECHECKHEADER
 
-       INodeDefManager *ndef = getServer()->ndef();
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       const NodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
-       if(!getItemCallback(ndef->get(node).name.c_str(), "after_destruct"))
+       if (!getItemCallback(ndef->get(node).name.c_str(), "after_destruct", &p))
                return;
 
        // Call function
        push_v3s16(L, p);
        pushnode(L, node, ndef);
-       if(lua_pcall(L, 2, 0, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
+       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
 
-       INodeDefManager *ndef = getServer()->ndef();
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       const NodeDefManager *ndef = getServer()->ndef();
 
        // Push callback function on stack
-       if(!getItemCallback(ndef->get(node).name.c_str(), "on_timer"))
+       if (!getItemCallback(ndef->get(node).name.c_str(), "on_timer", &p))
                return false;
 
        // Call function
        push_v3s16(L, p);
        lua_pushnumber(L,dtime);
-       if(lua_pcall(L, 2, 1, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
-       if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
-               return true;
-
-       return false;
+       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);
 }
 
 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
 
-       INodeDefManager *ndef = getServer()->ndef();
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       const NodeDefManager *ndef = getServer()->ndef();
 
        // If node doesn't exist, we don't know what callback to call
        MapNode node = getEnv()->getMap().getNodeNoEx(p);
-       if(node.getContent() == CONTENT_IGNORE)
+       if (node.getContent() == CONTENT_IGNORE)
                return;
 
        // Push callback function on stack
-       if(!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields"))
+       if (!getItemCallback(ndef->get(node).name.c_str(), "on_receive_fields", &p))
                return;
 
        // Call function
-       // param 1
-       push_v3s16(L, p);
-       // param 2
-       lua_pushstring(L, formname.c_str());
-       // param 3
-       lua_newtable(L);
-       for(std::map<std::string, std::string>::const_iterator
-                       i = fields.begin(); i != fields.end(); i++){
-               const std::string &name = i->first;
-               const std::string &value = i->second;
+       push_v3s16(L, p);                    // pos
+       lua_pushstring(L, formname.c_str()); // formname
+       lua_newtable(L);                     // fields
+       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);
        }
-       // param 4
-       objectrefGetOrCreate(sender);
-       if(lua_pcall(L, 4, 0, 0))
-               scriptError("error: %s", lua_tostring(L, -1));
+       objectrefGetOrCreate(L, sender);        // player
+       PCALL_RES(lua_pcall(L, 4, 0, error_handler));
+       lua_pop(L, 1);  // Pop error handler
 }
-