From 9eee3c3f465c071bb9908749cf48be3c131a1bdf Mon Sep 17 00:00:00 2001 From: BlockMen Date: Fri, 20 Nov 2015 23:46:33 +0100 Subject: [PATCH] Add option to give every object a nametag or change the nametag text of players --- doc/lua_api.txt | 26 +++++---- src/content_cao.cpp | 22 ++++---- src/content_cao.h | 1 - src/content_sao.cpp | 24 +-------- src/content_sao.h | 11 ++-- src/object_properties.cpp | 11 +++- src/object_properties.h | 2 + src/script/common/c_content.cpp | 13 +++++ src/script/lua_api/l_object.cpp | 96 +++++++++++++++++++-------------- 9 files changed, 111 insertions(+), 95 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 56dc84d24..45a47c9ab 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -2544,6 +2544,19 @@ This is basically a reference to a C++ `ServerActiveObject` * `set_properties(object property table)` * `get_properties()`: returns object property table * `is_player()`: returns true for players, false otherwise +* `get_nametag_attributes()` + * returns a table with the attributes of the nametag of an object + * { + color = {a=0..255, r=0..255, g=0..255, b=0..255}, + text = "", + } +* `set_nametag_attributes(attributes)` + * sets the attributes of the nametag of an object + * `attributes`: + { + color = ColorSpec, + text = "My Nametag", + } ##### LuaEntitySAO-only (no-op for other objects) * `setvelocity({x=num, y=num, z=num})` @@ -2644,17 +2657,6 @@ This is basically a reference to a C++ `ServerActiveObject` * in first person view * in third person view (max. values `{x=-10/10,y=-10,15,z=-5/5}`) * `get_eye_offset()`: returns offset_first and offset_third -* `get_nametag_attributes()` - * returns a table with the attributes of the nametag of the player - * { - color = {a=0..255, r=0..255, g=0..255, b=0..255}, - } -* `set_nametag_attributes(attributes)` - * sets the attributes of the nametag of the player - * `attributes`: - { - color = ColorSpec, - } ### `InvRef` An `InvRef` is a reference to an inventory. @@ -3232,6 +3234,8 @@ Definition tables automatic_face_movement_dir = 0.0, -- ^ automatically set yaw to movement direction; offset in degrees; false to disable backface_culling = true, -- false to disable backface_culling for model + nametag = "", -- by default empty, for players their name is shown if empty + nametag_color = , -- sets color of nametag as ColorSpec } ### Entity definition (`register_entity`) diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 72f6145b0..ed4e3b713 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -551,7 +551,6 @@ GenericCAO::GenericCAO(IGameDef *gamedef, ClientEnvironment *env): m_animated_meshnode(NULL), m_wield_meshnode(NULL), m_spritenode(NULL), - m_nametag_color(video::SColor(255, 255, 255, 255)), m_textnode(NULL), m_position(v3f(0,10*BS,0)), m_velocity(v3f(0,0,0)), @@ -972,19 +971,19 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc, updateTextures(""); scene::ISceneNode *node = getSceneNode(); - if (node && m_is_player && !m_is_local_player) { + if (node && m_prop.nametag != "" && !m_is_local_player) { // Add a text node for showing the name gui::IGUIEnvironment* gui = irr->getGUIEnvironment(); - std::wstring wname = utf8_to_wide(m_name); + std::wstring nametag_text = utf8_to_wide(m_prop.nametag); m_textnode = smgr->addTextSceneNode(gui->getSkin()->getFont(), - wname.c_str(), m_nametag_color, node); + nametag_text.c_str(), m_prop.nametag_color, node); m_textnode->grab(); m_textnode->setPosition(v3f(0, BS*1.1, 0)); // Enforce hiding nametag, // because if freetype is enabled, a grey // shadow can remain. - m_textnode->setVisible(m_nametag_color.getAlpha() > 0); + m_textnode->setVisible(m_prop.nametag_color.getAlpha() > 0); } updateNodePos(); @@ -1594,6 +1593,9 @@ void GenericCAO::processMessage(const std::string &data) m_tx_basepos = m_prop.initial_sprite_basepos; } + if ((m_is_player && !m_is_local_player) && m_prop.nametag == "") + m_prop.nametag = m_name; + expireVisuals(); } else if(cmd == GENERIC_CMD_UPDATE_POSITION) @@ -1772,15 +1774,15 @@ void GenericCAO::processMessage(const std::string &data) m_armor_groups[name] = rating; } } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) { + // Deprecated, for backwards compatibility only. readU8(is); // version - m_nametag_color = readARGB8(is); + m_prop.nametag_color = readARGB8(is); if (m_textnode != NULL) { - m_textnode->setTextColor(m_nametag_color); + m_textnode->setTextColor(m_prop.nametag_color); // Enforce hiding nametag, - // because if freetype is enabled, a grey - // shadow can remain. - m_textnode->setVisible(m_nametag_color.getAlpha() > 0); + // because if freetype is enabled, a grey shadow can remain. + m_textnode->setVisible(m_prop.nametag_color.getAlpha() > 0); } } } diff --git a/src/content_cao.h b/src/content_cao.h index 299d6c73e..d9145c5f1 100644 --- a/src/content_cao.h +++ b/src/content_cao.h @@ -70,7 +70,6 @@ private: scene::IAnimatedMeshSceneNode *m_animated_meshnode; WieldMeshSceneNode *m_wield_meshnode; scene::IBillboardSceneNode *m_spritenode; - video::SColor m_nametag_color; scene::ITextSceneNode* m_textnode; v3f m_position; v3f m_velocity; diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 0fa806c1a..0a1cfe770 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -757,8 +757,6 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_, m_bone_position_sent(false), m_attachment_parent_id(0), m_attachment_sent(false), - m_nametag_color(video::SColor(255, 255, 255, 255)), - m_nametag_sent(false), // public m_physics_override_speed(1), m_physics_override_jump(1), @@ -857,7 +855,7 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) os< getAttachmentChildIds(); ObjectProperties* accessObjectProperties(); void notifyObjectPropertiesModified(); - void setNametagColor(video::SColor color); - video::SColor getNametagColor(); /* Inventory interface @@ -292,7 +290,7 @@ public: private: std::string getPropertyPacket(); - + Player *m_player; u16 m_peer_id; Inventory *m_inventory; @@ -333,8 +331,6 @@ private: v3f m_attachment_rotation; bool m_attachment_sent; - video::SColor m_nametag_color; - bool m_nametag_sent; public: float m_physics_override_speed; @@ -346,4 +342,3 @@ public: }; #endif - diff --git a/src/object_properties.cpp b/src/object_properties.cpp index dc1eddf4e..3cec51672 100644 --- a/src/object_properties.cpp +++ b/src/object_properties.cpp @@ -43,7 +43,9 @@ ObjectProperties::ObjectProperties(): stepheight(0), automatic_face_movement_dir(false), automatic_face_movement_dir_offset(0.0), - backface_culling(true) + backface_culling(true), + nametag(""), + nametag_color(255, 255, 255, 255) { textures.push_back("unknown_object.png"); colors.push_back(video::SColor(255,255,255,255)); @@ -76,6 +78,9 @@ std::string ObjectProperties::dump() os<<", makes_footstep_sound="<backface_culling); + getstringfield(L, -1, "nametag", prop->nametag); + lua_getfield(L, -1, "nametag_color"); + if (!lua_isnil(L, -1)) { + video::SColor color = prop->nametag_color; + if (read_color(L, -1, &color)) + prop->nametag_color = color; + } + lua_pop(L, 1); } /******************************************************************************/ @@ -261,6 +269,11 @@ void push_object_properties(lua_State *L, ObjectProperties *prop) lua_setfield(L, -2, "automatic_face_movement_dir"); lua_pushboolean(L, prop->backface_culling); lua_setfield(L, -2, "backface_culling"); + lua_pushlstring(L, prop->nametag.c_str(), prop->nametag.size()); + lua_setfield(L, -2, "nametag"); + lua_newtable(L); + push_ARGB8(L, prop->nametag_color); + lua_setfield(L, -2, "nametag_color"); } /******************************************************************************/ diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 52190d60e..6d6614e7d 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -767,6 +767,59 @@ int ObjectRef::l_is_player(lua_State *L) return 1; } +// set_nametag_attributes(self, attributes) +int ObjectRef::l_set_nametag_attributes(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + ObjectRef *ref = checkobject(L, 1); + ServerActiveObject *co = getobject(ref); + + if (co == NULL) + return 0; + ObjectProperties *prop = co->accessObjectProperties(); + if (!prop) + return 0; + + lua_getfield(L, 2, "color"); + if (!lua_isnil(L, -1)) { + video::SColor color = prop->nametag_color; + read_color(L, -1, &color); + prop->nametag_color = color; + } + lua_pop(L, 1); + + std::string nametag = getstringfield_default(L, 2, "text", ""); + if (nametag != "") + prop->nametag = nametag; + + co->notifyObjectPropertiesModified(); + lua_pushboolean(L, true); + return 1; +} + +// get_nametag_attributes(self) +int ObjectRef::l_get_nametag_attributes(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + ObjectRef *ref = checkobject(L, 1); + ServerActiveObject *co = getobject(ref); + + if (co == NULL) + return 0; + ObjectProperties *prop = co->accessObjectProperties(); + if (!prop) + return 0; + + video::SColor color = prop->nametag_color; + + lua_newtable(L); + push_ARGB8(L, color); + lua_setfield(L, -2, "color"); + lua_pushstring(L, prop->nametag.c_str()); + lua_setfield(L, -2, "text"); + return 1; +} + /* LuaEntitySAO-only */ // setvelocity(self, {x=num, y=num, z=num}) @@ -1593,45 +1646,6 @@ int ObjectRef::l_get_day_night_ratio(lua_State *L) return 1; } -// set_nametag_attributes(self, attributes) -int ObjectRef::l_set_nametag_attributes(lua_State *L) -{ - NO_MAP_LOCK_REQUIRED; - ObjectRef *ref = checkobject(L, 1); - PlayerSAO *playersao = getplayersao(ref); - if (playersao == NULL) - return 0; - - lua_getfield(L, 2, "color"); - if (!lua_isnil(L, -1)) { - video::SColor color = playersao->getNametagColor(); - if (!read_color(L, -1, &color)) - return 0; - playersao->setNametagColor(color); - } - - lua_pushboolean(L, true); - return 1; -} - -// get_nametag_attributes(self) -int ObjectRef::l_get_nametag_attributes(lua_State *L) -{ - NO_MAP_LOCK_REQUIRED; - ObjectRef *ref = checkobject(L, 1); - PlayerSAO *playersao = getplayersao(ref); - if (playersao == NULL) - return 0; - - video::SColor color = playersao->getNametagColor(); - - lua_newtable(L); - push_ARGB8(L, color); - lua_setfield(L, -2, "color"); - - return 1; -} - ObjectRef::ObjectRef(ServerActiveObject *object): m_object(object) { @@ -1719,6 +1733,8 @@ const luaL_reg ObjectRef::methods[] = { luamethod(ObjectRef, set_detach), luamethod(ObjectRef, set_properties), luamethod(ObjectRef, get_properties), + luamethod(ObjectRef, set_nametag_attributes), + luamethod(ObjectRef, get_nametag_attributes), // LuaEntitySAO-only luamethod(ObjectRef, setvelocity), luamethod(ObjectRef, getvelocity), @@ -1768,7 +1784,5 @@ const luaL_reg ObjectRef::methods[] = { luamethod(ObjectRef, get_local_animation), luamethod(ObjectRef, set_eye_offset), luamethod(ObjectRef, get_eye_offset), - luamethod(ObjectRef, set_nametag_attributes), - luamethod(ObjectRef, get_nametag_attributes), {0,0} };