]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/cpp_api/s_player.cpp
[CSM] Add core.get_timeofday & core.get_day_count env calls (#5401)
[minetest.git] / src / script / cpp_api / s_player.cpp
index 81bfd45058b9a7bd1cb0c4d59cbcec5abac6fa3e..a8c07476cfa9ea5439770d5a019a318dc2549980 100644 (file)
@@ -19,6 +19,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "cpp_api/s_player.h"
 #include "cpp_api/s_internal.h"
+#include "common/c_converter.h"
+#include "common/c_content.h"
 #include "util/string.h"
 
 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
@@ -30,7 +32,7 @@ void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
        lua_getfield(L, -1, "registered_on_newplayers");
        // Call callbacks
        objectrefGetOrCreate(L, player);
-       script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
+       runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
 }
 
 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
@@ -42,7 +44,49 @@ void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
        lua_getfield(L, -1, "registered_on_dieplayers");
        // Call callbacks
        objectrefGetOrCreate(L, player);
-       script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
+       runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
+}
+
+bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
+               ServerActiveObject *hitter,
+               float time_from_last_punch,
+               const ToolCapabilities *toolcap,
+               v3f dir,
+               s16 damage)
+{
+       SCRIPTAPI_PRECHECKHEADER
+       // Get core.registered_on_punchplayers
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "registered_on_punchplayers");
+       // Call callbacks
+       objectrefGetOrCreate(L, player);
+       objectrefGetOrCreate(L, hitter);
+       lua_pushnumber(L, time_from_last_punch);
+       push_tool_capabilities(L, *toolcap);
+       push_v3f(L, dir);
+       lua_pushnumber(L, damage);
+       runCallbacks(6, RUN_CALLBACKS_MODE_OR);
+       return lua_toboolean(L, -1);
+}
+
+s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
+       s16 hp_change)
+{
+       SCRIPTAPI_PRECHECKHEADER
+
+       int error_handler = PUSH_ERROR_HANDLER(L);
+
+       // Get core.registered_on_player_hpchange
+       lua_getglobal(L, "core");
+       lua_getfield(L, -1, "registered_on_player_hpchange");
+       lua_remove(L, -2);
+
+       objectrefGetOrCreate(L, player);
+       lua_pushnumber(L, hp_change);
+       PCALL_RES(lua_pcall(L, 2, 1, error_handler));
+       hp_change = lua_tointeger(L, -1);
+       lua_pop(L, 2); // Pop result and error handler
+       return hp_change;
 }
 
 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
@@ -54,12 +98,15 @@ bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
        lua_getfield(L, -1, "registered_on_respawnplayers");
        // Call callbacks
        objectrefGetOrCreate(L, player);
-       script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
+       runCallbacks(1, RUN_CALLBACKS_MODE_OR);
        bool positioning_handled_by_some = lua_toboolean(L, -1);
        return positioning_handled_by_some;
 }
 
-bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::string &reason)
+bool ScriptApiPlayer::on_prejoinplayer(
+       const std::string &name,
+       const std::string &ip,
+       std::string *reason)
 {
        SCRIPTAPI_PRECHECKHEADER
 
@@ -68,9 +115,9 @@ bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::st
        lua_getfield(L, -1, "registered_on_prejoinplayers");
        lua_pushstring(L, name.c_str());
        lua_pushstring(L, ip.c_str());
-       script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
+       runCallbacks(2, RUN_CALLBACKS_MODE_OR);
        if (lua_isstring(L, -1)) {
-               reason.assign(lua_tostring(L, -1));
+               reason->assign(lua_tostring(L, -1));
                return true;
        }
        return false;
@@ -85,10 +132,11 @@ void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
        lua_getfield(L, -1, "registered_on_joinplayers");
        // Call callbacks
        objectrefGetOrCreate(L, player);
-       script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
+       runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
 }
 
-void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
+void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
+               bool timeout)
 {
        SCRIPTAPI_PRECHECKHEADER
 
@@ -97,7 +145,8 @@ void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
        lua_getfield(L, -1, "registered_on_leaveplayers");
        // Call callbacks
        objectrefGetOrCreate(L, player);
-       script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
+       lua_pushboolean(L, timeout);
+       runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
 }
 
 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
@@ -113,12 +162,12 @@ void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
        lua_newtable(L);
        lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
        lua_setfield(L, -2, "type");
-       script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
+       runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
 }
 
 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
                const std::string &formname,
-               const std::map<std::string, std::string> &fields)
+               const StringMap &fields)
 {
        SCRIPTAPI_PRECHECKHEADER
 
@@ -132,17 +181,19 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
        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;
+       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);
        }
-       script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
+       runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
 }
-ScriptApiPlayer::~ScriptApiPlayer() {
+
+ScriptApiPlayer::~ScriptApiPlayer()
+{
 }