Add option to give every object a nametag 3393/head
authorBlockMen <nmuelll@web.de>
Fri, 20 Nov 2015 22:46:33 +0000 (23:46 +0100)
committerBlockMen <nmuelll@web.de>
Tue, 15 Dec 2015 22:32:19 +0000 (23:32 +0100)
or change the nametag text of players

doc/lua_api.txt
src/content_cao.cpp
src/content_cao.h
src/content_sao.cpp
src/content_sao.h
src/object_properties.cpp
src/object_properties.h
src/script/common/c_content.cpp
src/script/lua_api/l_object.cpp

index 56dc84d24aea13f73bdca4ba2d6866f907da3982..45a47c9ab2a9eb01f517125d26ec0c9848de10cf 100644 (file)
@@ -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 = <color>, -- sets color of nametag as ColorSpec
     }
 
 ### Entity definition (`register_entity`)
index 72f6145b067c25b6179e63fb902e471b58bcd0f3..ed4e3b7133180750e7eda70b7dc32dbe4cef88ed 100644 (file)
@@ -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);
                }
        }
 }
index 299d6c73e0a54a34b77d541c5a2526521d17fa88..d9145c5f12bc6042e022a5bff6dc1aa49dd7c16b 100644 (file)
@@ -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;
index 0fa806c1a11c483c155bdf5f0dc02fade756e469..0a1cfe770f4ec39736aa0aed21ca0a354979a176 100644 (file)
@@ -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<<serializeLongString(gob_cmd_update_physics_override(m_physics_override_speed,
                                m_physics_override_jump, m_physics_override_gravity, m_physics_override_sneak,
                                m_physics_override_sneak_glitch)); // 5
-               os << serializeLongString(gob_cmd_update_nametag_attributes(m_nametag_color)); // 6
+               os << serializeLongString(gob_cmd_update_nametag_attributes(m_prop.nametag_color)); // 6 (GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) : Deprecated, for backwards compatibility only.
        }
        else
        {
@@ -1012,14 +1010,6 @@ void PlayerSAO::step(float dtime, bool send_recommended)
                ActiveObjectMessage aom(getId(), true, str);
                m_messages_out.push(aom);
        }
-
-       if (m_nametag_sent == false) {
-               m_nametag_sent = true;
-               std::string str = gob_cmd_update_nametag_attributes(m_nametag_color);
-               // create message and add to list
-               ActiveObjectMessage aom(getId(), true, str);
-               m_messages_out.push(aom);
-       }
 }
 
 void PlayerSAO::setBasePosition(const v3f &position)
@@ -1270,17 +1260,6 @@ void PlayerSAO::notifyObjectPropertiesModified()
        m_properties_sent = false;
 }
 
-void PlayerSAO::setNametagColor(video::SColor color)
-{
-       m_nametag_color = color;
-       m_nametag_sent = false;
-}
-
-video::SColor PlayerSAO::getNametagColor()
-{
-       return m_nametag_color;
-}
-
 Inventory* PlayerSAO::getInventory()
 {
        return m_inventory;
@@ -1396,4 +1375,3 @@ bool PlayerSAO::getCollisionBox(aabb3f *toset) {
 bool PlayerSAO::collideWithObjects(){
        return true;
 }
-
index 1f0a68cd859ae80993f0389c84caa2f419beedc7..44d40d332733d307266280166dac52f8110572bf 100644 (file)
@@ -91,13 +91,13 @@ private:
        std::string m_init_state;
        bool m_registered;
        struct ObjectProperties m_prop;
-       
+
        s16 m_hp;
        v3f m_velocity;
        v3f m_acceleration;
        float m_yaw;
        ItemGroupList m_armor_groups;
-       
+
        bool m_properties_sent;
        float m_last_sent_yaw;
        v3f m_last_sent_position;
@@ -213,8 +213,6 @@ public:
        std::set<int> 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
-
index dc1eddf4eeae2c296a82aa4eb76b2d7c891f2302..3cec51672a99713e3b31513910b983930414e55e 100644 (file)
@@ -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="<<makes_footstep_sound;
        os<<", automatic_rotate="<<automatic_rotate;
        os<<", backface_culling="<<backface_culling;
+       os << ", nametag=" << nametag;
+       os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
+                       << "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
        return os.str();
 }
 
@@ -109,6 +114,8 @@ void ObjectProperties::serialize(std::ostream &os) const
        writeU8(os, automatic_face_movement_dir);
        writeF1000(os, automatic_face_movement_dir_offset);
        writeU8(os, backface_culling);
+       os << serializeString(nametag);
+       writeARGB8(os, nametag_color);
        // Add stuff only at the bottom.
        // Never remove anything, because we don't want new versions of this
 }
@@ -146,6 +153,8 @@ void ObjectProperties::deSerialize(std::istream &is)
                        automatic_face_movement_dir = readU8(is);
                        automatic_face_movement_dir_offset = readF1000(is);
                        backface_culling = readU8(is);
+                       nametag = deSerializeString(is);
+                       nametag_color = readARGB8(is);
                }catch(SerializationError &e){}
        }
        else
index eee3be228f37b886df10cb6d0eb7d383c151f754..5dd3f57a14c2b4ae4bfe48a541fcc3f099ec6834 100644 (file)
@@ -48,6 +48,8 @@ struct ObjectProperties
        bool automatic_face_movement_dir;
        f32 automatic_face_movement_dir_offset;
        bool backface_culling;
+       std::string nametag;
+       video::SColor nametag_color;
 
 
        ObjectProperties();
index fe429b5d40203a14fcca972472f60379d6b1d790..4d277ec592505c82abfd330689e525db92ca0016 100644 (file)
@@ -201,6 +201,14 @@ void read_object_properties(lua_State *L, int index,
        }
        lua_pop(L, 1);
        getboolfield(L, -1, "backface_culling", prop->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");
 }
 
 /******************************************************************************/
index 52190d60e47923cc9b1bad827eddb88387ae1f03..6d6614e7da9e3a36d3518c5eddf275d4db3524c0 100644 (file)
@@ -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}
 };