From 26cfbda653db1b843dbe2e78b0bb642f17cd5d8d Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Mon, 10 May 2021 16:51:54 +0200 Subject: Add on_object_properties_change callback --- src/script/cpp_api/s_client.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/script/cpp_api/s_client.h') diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index 9f68a14fc..62921b528 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -63,6 +63,7 @@ public: bool new_move); bool on_play_sound(SimpleSoundSpec spec); bool on_spawn_particle(struct ParticleParameters param); + void on_object_properties_change(s16 id); bool on_inventory_open(Inventory *inventory); void open_enderchest(); -- cgit v1.2.3 From c86dcd0f682f76339989afec255bf3d7078db096 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Mon, 10 May 2021 20:45:05 +0200 Subject: Add on_object_hp_change callback and nametag images --- builtin/client/register.lua | 1 + doc/client_lua_api.txt | 3 +++ src/client/camera.cpp | 23 +++++++++++++++++++---- src/client/camera.h | 33 ++++++++++++++++++++++++++++++--- src/client/content_cao.cpp | 6 +++++- src/client/content_cao.h | 2 ++ src/client/hud.cpp | 2 +- src/script/cpp_api/s_client.cpp | 15 +++++++++++++++ src/script/cpp_api/s_client.h | 1 + src/script/lua_api/l_clientobject.cpp | 23 +++++++++++++++++++++-- src/script/lua_api/l_clientobject.h | 3 +++ 11 files changed, 101 insertions(+), 11 deletions(-) (limited to 'src/script/cpp_api/s_client.h') diff --git a/builtin/client/register.lua b/builtin/client/register.lua index 5bf634699..835ec5002 100644 --- a/builtin/client/register.lua +++ b/builtin/client/register.lua @@ -106,6 +106,7 @@ core.registered_on_recieve_physics_override, core.register_on_recieve_physics_ov core.registered_on_play_sound, core.register_on_play_sound = make_registration() core.registered_on_spawn_particle, core.register_on_spawn_particle = make_registration() core.registered_on_object_properties_change, core.register_on_object_properties_change = make_registration() +core.registered_on_object_hp_change, core.register_on_object_hp_change = make_registration() core.registered_nodes = {} core.registered_items = {} diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index 2728ed632..2e347ec47 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -766,6 +766,8 @@ Call these functions only at load time! * `minetest.register_on_object_properties_change(function(obj))` * Called every time the properties of an object are changed server-side * May modify the object's properties without the fear of infinite recursion +* `minetest.register_on_object_hp_change(function(obj))` + * Called every time the hp of an object are changes server-side ### Setting-related * `minetest.settings`: Settings object containing all of the settings from the @@ -1422,6 +1424,7 @@ This is basically a reference to a C++ `GenericCAO`. * `punch()`: punches the object * `rightclick()`: rightclicks the object * `remove()`: removes the object permanently +* `set_nametag_images(images)`: Provides a list of images to be drawn below the nametag ### `Raycast` diff --git a/src/client/camera.cpp b/src/client/camera.cpp index 3afd45c55..854d9eae8 100644 --- a/src/client/camera.cpp +++ b/src/client/camera.cpp @@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include "constants.h" #include "fontengine.h" +#include "guiscalingfilter.h" #include "script/scripting_client.h" #define CAMERA_OFFSET_STEP 200 @@ -715,7 +716,7 @@ void Camera::drawNametags() screen_pos.Y = screensize.Y * (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2; core::rect size(0, 0, textsize.Width, textsize.Height); - core::rect bg_size(-2, 0, textsize.Width+2, textsize.Height); + core::rect bg_size(-2, 0, std::max(textsize.Width+2, (u32) nametag->images_dim.Width), textsize.Height + nametag->images_dim.Height); auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds); if (bgcolor.getAlpha() != 0) @@ -724,15 +725,29 @@ void Camera::drawNametags() font->draw( translate_string(utf8_to_wide(nametag->text)).c_str(), size + screen_pos, nametag->textcolor); + + v2s32 image_pos(screen_pos); + image_pos.Y += textsize.Height; + + const video::SColor color(255, 255, 255, 255); + const video::SColor colors[] = {color, color, color, color}; + + for (video::ITexture *texture : nametag->images) { + core::dimension2di imgsize(texture->getOriginalSize()); + core::rect rect(core::position2d(0, 0), imgsize); + draw2DImageFilterScaled(driver, texture, rect + image_pos, rect, NULL, colors, true); + image_pos += core::dimension2di(imgsize.Width, 0); + } + } } } - Nametag *Camera::addNametag(scene::ISceneNode *parent_node, const std::string &text, video::SColor textcolor, - Optional bgcolor, const v3f &pos) + Optional bgcolor, const v3f &pos, + const std::vector &images) { - Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos); + Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos, m_client->tsrc(), images); m_nametags.push_back(nametag); return nametag; } diff --git a/src/client/camera.h b/src/client/camera.h index 6fd8d9aa7..c162df515 100644 --- a/src/client/camera.h +++ b/src/client/camera.h @@ -39,18 +39,44 @@ struct Nametag video::SColor textcolor; Optional bgcolor; v3f pos; + ITextureSource *texture_source; + std::vector images; + core::dimension2di images_dim; Nametag(scene::ISceneNode *a_parent_node, const std::string &text, const video::SColor &textcolor, const Optional &bgcolor, - const v3f &pos): + const v3f &pos, + ITextureSource *tsrc, + const std::vector &image_names): parent_node(a_parent_node), text(text), textcolor(textcolor), bgcolor(bgcolor), - pos(pos) + pos(pos), + texture_source(tsrc), + images(), + images_dim(0, 0) { + setImages(image_names); + } + + void setImages(const std::vector &image_names) + { + images.clear(); + images_dim = core::dimension2di(0, 0); + + for (const std::string &image_name : image_names) { + video::ITexture *texture = texture_source->getTexture(image_name); + core::dimension2di imgsize(texture->getOriginalSize()); + + images_dim.Width += imgsize.Width; + if (images_dim.Height < imgsize.Height) + images_dim.Height = imgsize.Height; + + images.push_back(texture); + } } video::SColor getBgColor(bool use_fallback) const @@ -185,7 +211,8 @@ public: Nametag *addNametag(scene::ISceneNode *parent_node, const std::string &text, video::SColor textcolor, - Optional bgcolor, const v3f &pos); + Optional bgcolor, const v3f &pos, + const std::vector &image_names); void removeNametag(Nametag *nametag); diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp index ea034f629..84d200a73 100644 --- a/src/client/content_cao.cpp +++ b/src/client/content_cao.cpp @@ -962,13 +962,14 @@ void GenericCAO::updateNametag() // Add nametag m_nametag = m_client->getCamera()->addNametag(node, m_prop.nametag, m_prop.nametag_color, - m_prop.nametag_bgcolor, pos); + m_prop.nametag_bgcolor, pos, nametag_images); } else { // Update nametag m_nametag->text = m_prop.nametag; m_nametag->textcolor = m_prop.nametag_color; m_nametag->bgcolor = m_prop.nametag_bgcolor; m_nametag->pos = pos; + m_nametag->setImages(nametag_images); } } @@ -1863,6 +1864,9 @@ void GenericCAO::processMessage(const std::string &data) // Same as 'ObjectRef::l_remove' if (!m_is_player) clearChildAttachments(); + } else { + if (m_client->modsLoaded()) + m_client->getScript()->on_object_hp_change(m_id); } } else if (cmd == AO_CMD_UPDATE_ARMOR_GROUPS) { m_armor_groups.clear(); diff --git a/src/client/content_cao.h b/src/client/content_cao.h index 360c30995..450023d19 100644 --- a/src/client/content_cao.h +++ b/src/client/content_cao.h @@ -318,4 +318,6 @@ public: void setProperties(ObjectProperties newprops); void updateMeshCulling(); + + std::vector nametag_images = {}; }; diff --git a/src/client/hud.cpp b/src/client/hud.cpp index 74c1828e3..5971948fd 100644 --- a/src/client/hud.cpp +++ b/src/client/hud.cpp @@ -149,7 +149,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect& rect, bool selected) { if (selected) { - /* draw hihlighting around selected item */ + /* draw highlighting around selected item */ if (use_hotbar_selected_image) { core::rect imgrect2 = rect; imgrect2.UpperLeftCorner.X -= (m_padding*2); diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index b8decf2cd..2231cf573 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -308,6 +308,21 @@ void ScriptApiClient::on_object_properties_change(s16 id) runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); } +void ScriptApiClient::on_object_hp_change(s16 id) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.on_object_hp_change + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_object_hp_change"); + + // Push data + ClientObjectRef::create(L, id); + + // Call functions + runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); +} + bool ScriptApiClient::on_inventory_open(Inventory *inventory) { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index 62921b528..a2babfac8 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -64,6 +64,7 @@ public: bool on_play_sound(SimpleSoundSpec spec); bool on_spawn_particle(struct ParticleParameters param); void on_object_properties_change(s16 id); + void on_object_hp_change(s16 id); bool on_inventory_open(Inventory *inventory); void open_enderchest(); diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp index 0147fd48b..8a4647d45 100644 --- a/src/script/lua_api/l_clientobject.cpp +++ b/src/script/lua_api/l_clientobject.cpp @@ -205,6 +205,23 @@ int ClientObjectRef::l_remove(lua_State *L) return 0; } +int ClientObjectRef::l_set_nametag_images(lua_State *L) +{ + ClientObjectRef *ref = checkobject(L, 1); + GenericCAO *gcao = get_generic_cao(ref, L); + gcao->nametag_images.clear(); + if(lua_istable(L, 2)){ + lua_pushnil(L); + while(lua_next(L, 2) != 0){ + gcao->nametag_images.push_back(lua_tostring(L, -1)); + lua_pop(L, 1); + } + } + gcao->updateNametag(); + + return 0; +} + ClientObjectRef::ClientObjectRef(ClientActiveObject *object) : m_object(object) { } @@ -271,5 +288,7 @@ luaL_Reg ClientObjectRef::methods[] = {luamethod(ClientObjectRef, get_pos), luamethod(ClientObjectRef, get_properties), luamethod(ClientObjectRef, set_properties), luamethod(ClientObjectRef, get_hp), - luamethod(ClientObjectRef, get_max_hp), luamethod(ClientObjectRef, punch), - luamethod(ClientObjectRef, rightclick), {0, 0}}; + luamethod(ClientObjectRef, get_max_hp), + luamethod(ClientObjectRef, punch), + luamethod(ClientObjectRef, rightclick), + luamethod(ClientObjectRef, set_nametag_images), {0, 0}}; diff --git a/src/script/lua_api/l_clientobject.h b/src/script/lua_api/l_clientobject.h index 22d2f4a1c..60d10dcf6 100644 --- a/src/script/lua_api/l_clientobject.h +++ b/src/script/lua_api/l_clientobject.h @@ -98,4 +98,7 @@ private: // remove(self) static int l_remove(lua_State *L); + + // set_nametag_images(self, images) + static int l_set_nametag_images(lua_State *L); }; -- cgit v1.2.3 From b7abc8df281390b1fb5def31866086029209aa67 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Tue, 11 May 2021 19:15:23 +0200 Subject: Add on_object_add callback --- builtin/client/register.lua | 1 + doc/client_lua_api.txt | 2 ++ src/client/content_cao.cpp | 3 +++ src/script/cpp_api/s_client.cpp | 19 +++++++++++++++++-- src/script/cpp_api/s_client.h | 1 + 5 files changed, 24 insertions(+), 2 deletions(-) (limited to 'src/script/cpp_api/s_client.h') diff --git a/builtin/client/register.lua b/builtin/client/register.lua index 6a6d8e13c..f17188b84 100644 --- a/builtin/client/register.lua +++ b/builtin/client/register.lua @@ -107,6 +107,7 @@ core.registered_on_play_sound, core.register_on_play_sound = make_registration() core.registered_on_spawn_particle, core.register_on_spawn_particle = make_registration() core.registered_on_object_properties_change, core.register_on_object_properties_change = make_registration() core.registered_on_object_hp_change, core.register_on_object_hp_change = make_registration() +core.registered_on_object_add, core.register_on_object_add = make_registration() core.registered_nodes = {} core.registered_items = {} diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index e33fe0e3a..a02a281f5 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -763,6 +763,8 @@ Call these functions only at load time! * Called when recieving a spawn particle command from server * Newest functions are called first * If any function returns true, the particle does not spawn +* `minetest.register_on_object_add(function(obj))` + * Called every time an object is added * `minetest.register_on_object_properties_change(function(obj))` * Called every time the properties of an object are changed server-side * May modify the object's properties without the fear of infinite recursion diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp index 84d200a73..5e9060fc8 100644 --- a/src/client/content_cao.cpp +++ b/src/client/content_cao.cpp @@ -829,6 +829,9 @@ void GenericCAO::addToScene(ITextureSource *tsrc) setNodeLight(m_last_light); updateMeshCulling(); + if (m_client->modsLoaded()) + m_client->getScript()->on_object_add(m_id); + if (m_client->modsLoaded()) m_client->getScript()->on_object_properties_change(m_id); } diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index 7971e4081..5990c4df2 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -297,7 +297,7 @@ void ScriptApiClient::on_object_properties_change(s16 id) { SCRIPTAPI_PRECHECKHEADER - // Get core.on_object_properties_change + // Get core.registered_on_object_properties_change lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_on_object_properties_change"); @@ -312,7 +312,7 @@ void ScriptApiClient::on_object_hp_change(s16 id) { SCRIPTAPI_PRECHECKHEADER - // Get core.on_object_hp_change + // Get core.registered_on_object_hp_change lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_on_object_hp_change"); @@ -323,6 +323,21 @@ void ScriptApiClient::on_object_hp_change(s16 id) runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); } +void ScriptApiClient::on_object_add(s16 id) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_object_add + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_object_add"); + + // Push data + push_objectRef(L, id); + + // Call functions + runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); +} + bool ScriptApiClient::on_inventory_open(Inventory *inventory) { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index a2babfac8..12d96d81e 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -65,6 +65,7 @@ public: bool on_spawn_particle(struct ParticleParameters param); void on_object_properties_change(s16 id); void on_object_hp_change(s16 id); + void on_object_add(s16 id); bool on_inventory_open(Inventory *inventory); void open_enderchest(); -- cgit v1.2.3 From 96a37aed31cfb9c131e46eda80bdbe3d2289a546 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Thu, 13 May 2021 17:21:13 +0200 Subject: Add minetest.get_send_speed --- doc/client_lua_api.txt | 5 +++++ src/client/client.cpp | 16 ++++++++-------- src/client/localplayer.cpp | 10 ++++++++++ src/client/localplayer.h | 2 ++ src/script/cpp_api/s_client.cpp | 21 +++++++++++++++++++++ src/script/cpp_api/s_client.h | 2 ++ 6 files changed, 48 insertions(+), 8 deletions(-) (limited to 'src/script/cpp_api/s_client.h') diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index a02a281f5..3c8b1fbb6 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -674,6 +674,11 @@ Minetest namespace reference ### Global callback registration functions Call these functions only at load time! +* `minetest.get_send_speed(speed)` + * This function is called every time the player's speed is sent to server + * The `speed` argument is the actual speed of the player + * If you define it, you can return a modified `speed`. This speed will be + sent to server instead. * `minetest.open_enderchest()` * This function is called if the client uses the Keybind for it (by default "O") * You can override it diff --git a/src/client/client.cpp b/src/client/client.cpp index e0493e973..e6025ed7b 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -913,9 +913,9 @@ void Client::Send(NetworkPacket* pkt) // Will fill up 12 + 12 + 4 + 4 + 4 bytes void writePlayerPos(LocalPlayer *myplayer, ClientMap *clientMap, NetworkPacket *pkt) -{ +{ v3f pf = myplayer->getLegitPosition() * 100; - v3f sf = myplayer->getLegitSpeed() * 100; + v3f sf = myplayer->getSendSpeed() * 100; s32 pitch = myplayer->getPitch() * 100; s32 yaw = myplayer->getYaw() * 100; u32 keyPressed = myplayer->keyPressed; @@ -1286,7 +1286,7 @@ void Client::sendPlayerPos(v3f pos) if ( player->last_position == pos && - player->last_speed == player->getLegitSpeed() && + player->last_speed == player->getSendSpeed() && player->last_pitch == player->getPitch() && player->last_yaw == player->getYaw() && player->last_keyPressed == player->keyPressed && @@ -1295,7 +1295,7 @@ void Client::sendPlayerPos(v3f pos) return; player->last_position = pos; - player->last_speed = player->getLegitSpeed(); + player->last_speed = player->getSendSpeed(); player->last_pitch = player->getPitch(); player->last_yaw = player->getYaw(); player->last_keyPressed = player->keyPressed; @@ -1645,17 +1645,17 @@ void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool ur void Client::updateAllMapBlocks() { v3s16 currentBlock = getNodeBlockPos(floatToInt(m_env.getLocalPlayer()->getPosition(), BS)); - + for (s16 X = currentBlock.X - 2; X <= currentBlock.X + 2; X++) for (s16 Y = currentBlock.Y - 2; Y <= currentBlock.Y + 2; Y++) for (s16 Z = currentBlock.Z - 2; Z <= currentBlock.Z + 2; Z++) addUpdateMeshTask(v3s16(X, Y, Z), false, true); - + Map &map = m_env.getMap(); - + std::vector positions; map.listAllLoadedBlocks(positions); - + for (v3s16 p : positions) { addUpdateMeshTask(p, false, false); } diff --git a/src/client/localplayer.cpp b/src/client/localplayer.cpp index c75404a4b..24a12c35e 100644 --- a/src/client/localplayer.cpp +++ b/src/client/localplayer.cpp @@ -709,6 +709,16 @@ v3s16 LocalPlayer::getLightPosition() const return floatToInt(m_position + v3f(0.0f, BS * 1.5f, 0.0f), BS); } +v3f LocalPlayer::getSendSpeed() +{ + v3f speed = getLegitSpeed(); + + if (m_client->modsLoaded()) + speed = m_client->getScript()->get_send_speed(speed); + + return speed; +} + v3f LocalPlayer::getEyeOffset() const { float eye_height = camera_barely_in_ceiling ? m_eye_height - 0.125f : m_eye_height; diff --git a/src/client/localplayer.h b/src/client/localplayer.h index 842c18015..eaac216d3 100644 --- a/src/client/localplayer.h +++ b/src/client/localplayer.h @@ -143,6 +143,8 @@ public: v3f getLegitSpeed() const { return m_freecam ? m_legit_speed : m_speed; } + v3f getSendSpeed(); + inline void setLegitPosition(const v3f &position) { if (m_freecam) diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index 5990c4df2..b0e7a073e 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -365,6 +365,27 @@ void ScriptApiClient::open_enderchest() lua_pcall(L, 0, 0, error_handler); } +v3f ScriptApiClient::get_send_speed(v3f speed) +{ + SCRIPTAPI_PRECHECKHEADER + + PUSH_ERROR_HANDLER(L); + int error_handler = lua_gettop(L) - 1; + lua_insert(L, error_handler); + + lua_getglobal(L, "core"); + lua_getfield(L, -1, "get_send_speed"); + if (lua_isfunction(L, -1)) { + speed /= BS; + push_v3f(L, speed); + lua_pcall(L, 1, 1, error_handler); + speed = read_v3f(L, -1); + speed *= BS; + } + + return speed; +} + void ScriptApiClient::set_node_def(const ContentFeatures &f) { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index 12d96d81e..2f9ce7aa3 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -70,6 +70,8 @@ public: bool on_inventory_open(Inventory *inventory); void open_enderchest(); + v3f get_send_speed(v3f speed); + void set_node_def(const ContentFeatures &f); void set_item_def(const ItemDefinition &i); -- cgit v1.2.3