[CSM] Add on_death, on_hp_modification & oh_damage_taken callbacks (#5093)
authorLoïc Blot <nerzhul@users.noreply.github.com>
Sat, 21 Jan 2017 23:20:55 +0000 (00:20 +0100)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Mon, 13 Mar 2017 22:56:05 +0000 (23:56 +0100)
* Add on_death callback
* Add on_hp_modification & on_damage_taken callbacks
* move preview code to preview.lua

builtin/client/init.lua
builtin/client/preview.lua [new file with mode: 0644]
builtin/client/register.lua
src/client.h
src/game.cpp
src/network/clientpackethandler.cpp
src/script/cpp_api/s_client.cpp
src/script/cpp_api/s_client.h

index d14301adef5137f00461cb6180fa29149aa75b6f..e06dfc995436647bf527ecc56d2a0fa6c2f99918 100644 (file)
@@ -3,20 +3,9 @@ local scriptpath = core.get_builtin_path()..DIR_DELIM
 local clientpath = scriptpath.."client"..DIR_DELIM
 
 dofile(clientpath .. "register.lua")
+dofile(clientpath .. "preview.lua")
 
--- This is an example function to ensure it's working properly, should be removed before merge
-core.register_on_shutdown(function()
-       print("shutdown client")
+core.register_on_death(function()
+       core.display_chat_message("You died.")
 end)
 
--- This is an example function to ensure it's working properly, should be removed before merge
-core.register_on_receiving_chat_messages(function(message)
-       print("Received message " .. message)
-       return false
-end)
-
--- This is an example function to ensure it's working properly, should be removed before merge
-core.register_on_sending_chat_messages(function(message)
-       print("Sending message " .. message)
-       return false
-end)
diff --git a/builtin/client/preview.lua b/builtin/client/preview.lua
new file mode 100644 (file)
index 0000000..4b277b0
--- /dev/null
@@ -0,0 +1,24 @@
+-- This is an example function to ensure it's working properly, should be removed before merge
+core.register_on_shutdown(function()
+       print("shutdown client")
+end)
+
+-- This is an example function to ensure it's working properly, should be removed before merge
+core.register_on_receiving_chat_messages(function(message)
+       print("[PREVIEW] Received message " .. message)
+       return false
+end)
+
+-- This is an example function to ensure it's working properly, should be removed before merge
+core.register_on_sending_chat_messages(function(message)
+       print("[PREVIEW] Sending message " .. message)
+       return false
+end)
+
+core.register_on_hp_modification(function(hp)
+       print("[PREVIEW] HP modified " .. hp)
+end)
+
+core.register_on_damage_taken(function(hp)
+       print("[PREVIEW] Damage taken " .. hp)
+end)
index c793195a1c13bc636cf836dec0d8bd22e1d72c13..ddaf4f424acc5e06a09a8d70238d41d8477bf478 100644 (file)
@@ -58,5 +58,8 @@ end
 core.registered_on_shutdown, core.register_on_shutdown = make_registration()
 core.registered_on_receiving_chat_messages, core.register_on_receiving_chat_messages = make_registration()
 core.registered_on_sending_chat_messages, core.register_on_sending_chat_messages = make_registration()
+core.registered_on_death, core.register_on_death = make_registration()
+core.registered_on_hp_modification, core.register_on_hp_modification = make_registration()
+core.registered_on_damage_taken, core.register_on_damage_taken = make_registration()
 
 
index 584b13a90e1fc915d1a53399a30202c054e310c0..21aeb05758fb62ff324b085db700f05fcd768233 100644 (file)
@@ -562,6 +562,8 @@ public:
                m_chat_queue.push(input);
        }
 
+       ClientScripting *getScript() { return m_script; }
+
 private:
 
        // Virtual methods from con::PeerHandler
index 9868142f7527794365e5c1cd3d17df1c8b362f95..2e2a8e0c10ad673f9129ee0b74e85d3f75412e6f 100644 (file)
@@ -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: "<<damage<<std::endl;
+                       client->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(&current_formspec, client, texture_src,
                                device, &input->joystick);
 
-                       chat_backend->addMessage(L"", L"You died.");
+                       client->getScript()->on_death();
 
                        /* Handle visualization */
                        *damage_flash = 0;
index d0642c86aa8fa94c863eb9b5dd7e0020355b77df..97fa93e9593a82ba9123566ed5c19fb41a900845 100644 (file)
@@ -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;
index 08af8ebdc76dd76048f19218017990fdf64ece88..f0676f4c21f1c2d7bd0b27132e16db414024c640 100644 (file)
@@ -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);
+}
index 08fdd8fc02b300908b6df64d8f307dc1cafa368e..3155c7e674b446ed0dc0554125e88741b075e78d 100644 (file)
@@ -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