Skip to content

Commit

Permalink
Add option to give every object a nametag
Browse files Browse the repository at this point in the history
or change the nametag text of players
  • Loading branch information
BlockMen committed Dec 15, 2015
1 parent 19f73e4 commit 9eee3c3
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 95 deletions.
26 changes: 15 additions & 11 deletions doc/lua_api.txt
Expand Up @@ -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})`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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`)
Expand Down
22 changes: 12 additions & 10 deletions src/content_cao.cpp
Expand Up @@ -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)),
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/content_cao.h
Expand Up @@ -70,7 +70,6 @@ class GenericCAO : public ClientActiveObject
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;
Expand Down
24 changes: 1 addition & 23 deletions src/content_sao.cpp
Expand Up @@ -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),
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1396,4 +1375,3 @@ bool PlayerSAO::getCollisionBox(aabb3f *toset) {
bool PlayerSAO::collideWithObjects(){
return true;
}

11 changes: 3 additions & 8 deletions src/content_sao.h
Expand Up @@ -91,13 +91,13 @@ class LuaEntitySAO : public ServerActiveObject
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;
Expand Down Expand Up @@ -213,8 +213,6 @@ class PlayerSAO : public ServerActiveObject
std::set<int> getAttachmentChildIds();
ObjectProperties* accessObjectProperties();
void notifyObjectPropertiesModified();
void setNametagColor(video::SColor color);
video::SColor getNametagColor();

/*
Inventory interface
Expand Down Expand Up @@ -292,7 +290,7 @@ class PlayerSAO : public ServerActiveObject

private:
std::string getPropertyPacket();

Player *m_player;
u16 m_peer_id;
Inventory *m_inventory;
Expand Down Expand Up @@ -333,8 +331,6 @@ class PlayerSAO : public ServerActiveObject
v3f m_attachment_rotation;
bool m_attachment_sent;

video::SColor m_nametag_color;
bool m_nametag_sent;

public:
float m_physics_override_speed;
Expand All @@ -346,4 +342,3 @@ class PlayerSAO : public ServerActiveObject
};

#endif

11 changes: 10 additions & 1 deletion src/object_properties.cpp
Expand Up @@ -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));
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/object_properties.h
Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions src/script/common/c_content.cpp
Expand Up @@ -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);
}

/******************************************************************************/
Expand Down Expand Up @@ -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");
}

/******************************************************************************/
Expand Down

0 comments on commit 9eee3c3

Please sign in to comment.