diff --git a/builtin/client/preview.lua b/builtin/client/preview.lua index cf416226d..0d7d50a0d 100644 --- a/builtin/client/preview.lua +++ b/builtin/client/preview.lua @@ -1,6 +1,6 @@ -- This is an example function to ensure it's working properly, should be removed before merge core.register_on_shutdown(function() - print("shutdown client") + print("[PREVIEW] shutdown client") end) -- This is an example function to ensure it's working properly, should be removed before merge @@ -15,17 +15,25 @@ core.register_on_sending_chat_messages(function(message) return false end) +-- This is an example function to ensure it's working properly, should be removed before merge core.register_on_hp_modification(function(hp) print("[PREVIEW] HP modified " .. hp) end) +-- This is an example function to ensure it's working properly, should be removed before merge core.register_on_damage_taken(function(hp) print("[PREVIEW] Damage taken " .. hp) end) +-- This is an example function to ensure it's working properly, should be removed before merge +core.register_globalstep(function(dtime) + -- print("[PREVIEW] globalstep " .. dtime) +end) + -- This is an example function to ensure it's working properly, should be removed before merge core.register_chatcommand("dump", { func = function(name, param) return true, dump(_G) end, }) + diff --git a/builtin/client/register.lua b/builtin/client/register.lua index ddaf4f424..8b60c1222 100644 --- a/builtin/client/register.lua +++ b/builtin/client/register.lua @@ -55,6 +55,7 @@ local function make_registration() return t, registerfunc end +core.registered_globalsteps, core.register_globalstep = make_registration() 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() diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index 0024306cf..c7329b0d8 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -678,6 +678,8 @@ Helper functions ### Global callback registration functions Call these functions only at load time! +* `minetest.register_globalstep(func(dtime))` + * Called every client environment step, usually interval of 0.1s * `minetest.register_on_shutdown(func())` * Called before client shutdown * **Warning**: If the client terminates abnormally (i.e. crashes), the registered diff --git a/src/client.cpp b/src/client.cpp index 3a3e0673e..3b8074252 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -263,6 +263,7 @@ Client::Client( g_settings->getBool("enable_parallax_occlusion")); m_script = new ClientScripting(this); + m_env.setScript(m_script); } void Client::initMods() diff --git a/src/client.h b/src/client.h index 21aeb0575..ff8aea591 100644 --- a/src/client.h +++ b/src/client.h @@ -488,13 +488,19 @@ public: // Get event from queue. CE_NONE is returned if queue is empty. ClientEvent getClientEvent(); - bool accessDenied() - { return m_access_denied; } + bool accessDenied() const { return m_access_denied; } - bool reconnectRequested() { return m_access_denied_reconnect; } + bool reconnectRequested() const { return m_access_denied_reconnect; } - std::string accessDeniedReason() - { return m_access_denied_reason; } + void setFatalError(const std::string &reason) + { + m_access_denied = true; + m_access_denied_reason = reason; + } + + // Renaming accessDeniedReason to better name could be good as it's used to + // disconnect client when CSM failed. + const std::string &accessDeniedReason() const { return m_access_denied_reason; } bool itemdefReceived() { return m_itemdef_received; } diff --git a/src/clientenvironment.cpp b/src/clientenvironment.cpp index b32a02f2d..4116c973c 100644 --- a/src/clientenvironment.cpp +++ b/src/clientenvironment.cpp @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "clientenvironment.h" #include "clientsimpleobject.h" #include "clientmap.h" +#include "clientscripting.h" #include "mapblock_mesh.h" #include "event.h" #include "collision.h" @@ -42,6 +43,7 @@ ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr, m_smgr(smgr), m_texturesource(texturesource), m_client(client), + m_script(NULL), m_irr(irr) { char zero = 0; @@ -243,6 +245,8 @@ void ClientEnvironment::step(float dtime) } } + m_script->environment_step(dtime); + /* A quick draft of lava damage */ diff --git a/src/clientenvironment.h b/src/clientenvironment.h index b30a7a6d7..c273ede54 100644 --- a/src/clientenvironment.h +++ b/src/clientenvironment.h @@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., class ClientSimpleObject; class ClientMap; +class ClientScripting; class ClientActiveObject; class GenericCAO; class LocalPlayer; @@ -75,6 +76,7 @@ public: ClientMap & getClientMap(); Client *getGameDef() { return m_client; } + void setScript(ClientScripting *script) { m_script = script; } void step(f32 dtime); @@ -176,6 +178,7 @@ private: scene::ISceneManager *m_smgr; ITextureSource *m_texturesource; Client *m_client; + ClientScripting *m_script; IrrlichtDevice *m_irr; UNORDERED_MAP m_active_objects; std::vector m_simple_objects; diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index f0676f4c2..ce88d67e3 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "s_client.h" #include "s_internal.h" +#include "client.h" void ScriptApiClient::on_shutdown() { @@ -94,3 +95,20 @@ void ScriptApiClient::on_death() // Call callbacks runCallbacks(0, RUN_CALLBACKS_MODE_FIRST); } + +void ScriptApiClient::environment_step(float dtime) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_globalsteps + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_globalsteps"); + // Call callbacks + lua_pushnumber(L, dtime); + try { + runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); + } catch (LuaError &e) { + getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n" + + script_get_backtrace(L)); + } +} diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index 3155c7e67..3d373f97c 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -36,5 +36,7 @@ public: void on_damage_taken(int32_t damage_amount); void on_hp_modification(int32_t newhp); void on_death(); + + void environment_step(float dtime); }; #endif