From 9978f5af828550d819890fed1fc56d65838a2c4c Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Sun, 22 Jan 2017 00:20:55 +0100 Subject: [CSM] Add on_death, on_hp_modification & oh_damage_taken callbacks (#5093) * Add on_death callback * Add on_hp_modification & on_damage_taken callbacks * move preview code to preview.lua --- src/client.h | 2 ++ src/game.cpp | 7 +++---- src/network/clientpackethandler.cpp | 2 ++ src/script/cpp_api/s_client.cpp | 35 +++++++++++++++++++++++++++++++++++ src/script/cpp_api/s_client.h | 4 ++++ 5 files changed, 46 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/client.h b/src/client.h index 584b13a90..21aeb0575 100644 --- a/src/client.h +++ b/src/client.h @@ -562,6 +562,8 @@ public: m_chat_queue.push(input); } + ClientScripting *getScript() { return m_script; } + private: // Virtual methods from con::PeerHandler diff --git a/src/game.cpp b/src/game.cpp index 9868142f7..2e2a8e0c1 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -41,7 +41,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "guiKeyChangeMenu.h" #include "guiPasswordChange.h" #include "guiVolumeChange.h" -#include "hud.h" #include "mainmenumanager.h" #include "mapblock.h" #include "nodedef.h" // Needed for determining pointing to nodes @@ -61,6 +60,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "version.h" #include "minimap.h" #include "mapblock_mesh.h" +#include "script/clientscripting.h" #include "sound.h" @@ -3240,8 +3240,7 @@ void Game::processClientEvents(CameraOrientation *cam, float *damage_flash) if (event.type == CE_PLAYER_DAMAGE && client->getHP() != 0) { - //u16 damage = event.player_damage.amount; - //infostream<<"Player damage: "<getScript()->on_damage_taken(event.player_damage.amount); *damage_flash += 95.0 + 3.2 * event.player_damage.amount; *damage_flash = MYMIN(*damage_flash, 127.0); @@ -3259,7 +3258,7 @@ void Game::processClientEvents(CameraOrientation *cam, float *damage_flash) show_deathscreen(¤t_formspec, client, texture_src, device, &input->joystick); - chat_backend->addMessage(L"", L"You died."); + client->getScript()->on_death(); /* Handle visualization */ *damage_flash = 0; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index d0642c86a..97fa93e95 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -526,6 +526,8 @@ void Client::handleCommand_HP(NetworkPacket* pkt) player->hp = hp; + m_script->on_hp_modification(hp); + if (hp < oldhp) { // Add to ClientEvent queue ClientEvent event; diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index 08af8ebdc..f0676f4c2 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -59,3 +59,38 @@ bool ScriptApiClient::on_receiving_message(const std::string &message) bool ate = lua_toboolean(L, -1); return ate; } + +void ScriptApiClient::on_damage_taken(int32_t damage_amount) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_chat_messages + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_damage_taken"); + // Call callbacks + lua_pushinteger(L, damage_amount); + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); +} + +void ScriptApiClient::on_hp_modification(int32_t newhp) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_chat_messages + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_hp_modification"); + // Call callbacks + lua_pushinteger(L, newhp); + runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC); +} + +void ScriptApiClient::on_death() +{ + SCRIPTAPI_PRECHECKHEADER + + // Get registered shutdown hooks + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_death"); + // Call callbacks + runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); +} diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index 08fdd8fc0..3155c7e67 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -32,5 +32,9 @@ public: // Chat message handlers bool on_sending_message(const std::string &message); bool on_receiving_message(const std::string &message); + + void on_damage_taken(int32_t damage_amount); + void on_hp_modification(int32_t newhp); + void on_death(); }; #endif -- cgit v1.2.3