#include <algorithm>
#include <cmath>
#include "client/shader.h"
+#include "client/minimap.h"
class Settings;
struct ToolCapabilities;
matrix.setTextureScale(txs, tys);
}
+// Evaluate transform chain recursively; irrlicht does not do this for us
+static void updatePositionRecursive(scene::ISceneNode *node)
+{
+ scene::ISceneNode *parent = node->getParent();
+ if (parent)
+ updatePositionRecursive(parent);
+ node->updateAbsolutePosition();
+}
+
/*
TestCAO
*/
void addToScene(ITextureSource *tsrc);
void removeFromScene(bool permanent);
void updateLight(u32 day_night_ratio);
- v3s16 getLightPosition();
void updateNodePos();
void step(float dtime, ClientEnvironment *env);
{
}
-v3s16 TestCAO::getLightPosition()
-{
- return floatToInt(m_position, BS);
-}
-
void TestCAO::updateNodePos()
{
if (!m_node)
m_is_local_player = true;
m_is_visible = false;
player->setCAO(this);
+
+ m_prop.show_on_minimap = false;
}
}
}
// PROTOCOL_VERSION >= 37
- m_name = deSerializeString(is);
+ m_name = deSerializeString16(is);
m_is_player = readU8(is);
m_id = readU16(is);
m_position = readV3F32(is);
const u8 num_messages = readU8(is);
for (int i = 0; i < num_messages; i++) {
- std::string message = deSerializeLongString(is);
+ std::string message = deSerializeString32(is);
processMessage(message);
}
for (u16 cao_id : m_attachment_child_ids) {
GenericCAO *obj = m_env->getGenericCAO(cao_id);
if (obj) {
- obj->setVisible(toset);
+ // Check if the entity is forced to appear in first person.
+ obj->setVisible(obj->m_force_visible ? true : toset);
}
}
}
-void GenericCAO::setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation)
+void GenericCAO::setAttachment(int parent_id, const std::string &bone,
+ v3f position, v3f rotation, bool force_visible)
{
int old_parent = m_attachment_parent_id;
m_attachment_parent_id = parent_id;
m_attachment_bone = bone;
m_attachment_position = position;
m_attachment_rotation = rotation;
+ m_force_visible = force_visible;
ClientActiveObject *parent = m_env->getActiveObject(parent_id);
if (parent)
parent->addAttachmentChild(m_id);
}
-
-
updateAttachments();
+
+ // Forcibly show attachments if required by set_attach
+ if (m_force_visible) {
+ m_is_visible = true;
+ } else if (!m_is_local_player) {
+ // Objects attached to the local player should be hidden in first person
+ m_is_visible = !m_attached_to_local ||
+ m_client->getCamera()->getCameraMode() != CAMERA_MODE_FIRST;
+ m_force_visible = false;
+ } else {
+ // Local players need to have this set,
+ // otherwise first person attachments fail.
+ m_is_visible = true;
+ }
}
void GenericCAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
- v3f *rotation) const
+ v3f *rotation, bool *force_visible) const
{
*parent_id = m_attachment_parent_id;
*bone = m_attachment_bone;
*position = m_attachment_position;
*rotation = m_attachment_rotation;
+ *force_visible = m_force_visible;
}
void GenericCAO::clearChildAttachments()
int child_id = *m_attachment_child_ids.begin();
if (ClientActiveObject *child = m_env->getActiveObject(child_id))
- child->setAttachment(0, "", v3f(), v3f());
+ child->setAttachment(0, "", v3f(), v3f(), false);
removeAttachmentChild(child_id);
}
void GenericCAO::clearParentAttachment()
{
if (m_attachment_parent_id)
- setAttachment(0, "", m_attachment_position, m_attachment_rotation);
+ setAttachment(0, "", m_attachment_position, m_attachment_rotation, false);
else
- setAttachment(0, "", v3f(), v3f());
+ setAttachment(0, "", v3f(), v3f(), false);
}
void GenericCAO::addAttachmentChild(int child_id)
m_client->getCamera()->removeNametag(m_nametag);
m_nametag = nullptr;
}
+
+ if (m_marker && m_client->getMinimap())
+ m_client->getMinimap()->removeMarker(&m_marker);
}
void GenericCAO::addToScene(ITextureSource *tsrc)
if (m_enable_shaders) {
IShaderSource *shader_source = m_client->getShaderSource();
- u32 shader_id = shader_source->getShader(
- "object_shader",
- (m_prop.use_texture_alpha) ? TILE_MATERIAL_ALPHA : TILE_MATERIAL_BASIC,
- NDT_NORMAL);
+ MaterialType material_type;
+
+ if (m_prop.shaded && m_prop.glow == 0)
+ material_type = (m_prop.use_texture_alpha) ?
+ TILE_MATERIAL_ALPHA : TILE_MATERIAL_BASIC;
+ else
+ material_type = (m_prop.use_texture_alpha) ?
+ TILE_MATERIAL_PLAIN_ALPHA : TILE_MATERIAL_PLAIN;
+
+ u32 shader_id = shader_source->getShader("object_shader", material_type, NDT_NORMAL);
m_material_type = shader_source->getShaderInfo(shader_id).material;
} else {
m_material_type = (m_prop.use_texture_alpha) ?
node->setParent(m_matrixnode);
updateNametag();
+ updateMarker();
updateNodePos();
updateAnimation();
updateBonePosition();
updateAttachments();
setNodeLight(m_last_light);
+ updateMeshCulling();
}
void GenericCAO::updateLight(u32 day_night_ratio)
return;
u8 light_at_pos = 0;
- bool pos_ok;
-
- v3s16 p = getLightPosition();
- MapNode n = m_env->getMap().getNode(p, &pos_ok);
- if (pos_ok)
- light_at_pos = n.getLightBlend(day_night_ratio, m_client->ndef());
- else
+ bool pos_ok = false;
+
+ v3s16 pos[3];
+ u16 npos = getLightPosition(pos);
+ for (u16 i = 0; i < npos; i++) {
+ bool this_ok;
+ MapNode n = m_env->getMap().getNode(pos[i], &this_ok);
+ if (this_ok) {
+ u8 this_light = n.getLightBlend(day_night_ratio, m_client->ndef());
+ light_at_pos = MYMAX(light_at_pos, this_light);
+ pos_ok = true;
+ }
+ }
+ if (!pos_ok)
light_at_pos = blend_light(day_night_ratio, LIGHT_SUN, 0);
u8 light = decode_light(light_at_pos + m_glow);
}
}
-v3s16 GenericCAO::getLightPosition()
+u16 GenericCAO::getLightPosition(v3s16 *pos)
{
- if (m_is_player)
- return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
+ const auto &box = m_prop.collisionbox;
+ pos[0] = floatToInt(m_position + box.MinEdge * BS, BS);
+ pos[1] = floatToInt(m_position + box.MaxEdge * BS, BS);
+
+ // Skip center pos if it falls into the same node as Min or MaxEdge
+ if ((box.MaxEdge - box.MinEdge).getLengthSQ() < 3.0f)
+ return 2;
+ pos[2] = floatToInt(m_position + box.getCenter() * BS, BS);
+ return 3;
+}
+
+void GenericCAO::updateMarker()
+{
+ if (!m_client->getMinimap())
+ return;
+
+ if (!m_prop.show_on_minimap) {
+ if (m_marker)
+ m_client->getMinimap()->removeMarker(&m_marker);
+ return;
+ }
+
+ if (m_marker)
+ return;
- return floatToInt(m_position, BS);
+ scene::ISceneNode *node = getSceneNode();
+ if (!node)
+ return;
+ m_marker = m_client->getMinimap()->addMarker(node);
}
void GenericCAO::updateNametag()
void GenericCAO::step(float dtime, ClientEnvironment *env)
{
- if (m_animated_meshnode) {
- m_animated_meshnode->animateJoints();
- updateBonePosition();
- }
-
// Handle model animations and update positions instantly to prevent lags
if (m_is_local_player) {
LocalPlayer *player = m_env->getLocalPlayer();
if (controls.sneak && walking)
new_speed /= 2;
- if (walking && (controls.LMB || controls.RMB)) {
+ if (walking && (controls.dig || controls.place)) {
new_anim = player->local_animations[3];
player->last_animation = WD_ANIM;
- } else if(walking) {
+ } else if (walking) {
new_anim = player->local_animations[1];
player->last_animation = WALK_ANIM;
- } else if(controls.LMB || controls.RMB) {
+ } else if (controls.dig || controls.place) {
new_anim = player->local_animations[2];
player->last_animation = DIG_ANIM;
}
// Update local player animations
if ((player->last_animation != old_anim ||
- m_animation_speed != old_anim_speed) &&
- player->last_animation != NO_ANIM && allow_update)
- updateAnimation();
+ m_animation_speed != old_anim_speed) &&
+ player->last_animation != NO_ANIM && allow_update)
+ updateAnimation();
}
}
rot_translator.val_current = m_rotation;
updateNodePos();
}
+
+ if (m_animated_meshnode) {
+ // Everything must be updated; the whole transform
+ // chain as well as the animated mesh node.
+ // Otherwise, bone attachments would be relative to
+ // a position that's one frame old.
+ if (m_matrixnode)
+ updatePositionRecursive(m_matrixnode);
+ m_animated_meshnode->updateAbsolutePosition();
+ m_animated_meshnode->animateJoints();
+ updateBonePosition();
+ }
}
void GenericCAO::updateTexturePos()
int row = m_tx_basepos.Y;
int col = m_tx_basepos.X;
+ // Yawpitch goes rightwards
if (m_tx_select_horiz_by_yawpitch) {
if (cam_to_entity.Y > 0.75)
col += 5;
float tys = m_tx_size.Y;
setBillboardTextureMatrix(m_spritenode, txs, tys, col, row);
}
+
+ else if (m_meshnode) {
+ if (m_prop.visual == "upright_sprite") {
+ int row = m_tx_basepos.Y;
+ int col = m_tx_basepos.X;
+
+ // Animation goes downwards
+ row += m_anim_frame;
+
+ const auto &tx = m_tx_size;
+ v2f t[4] = { // cf. vertices in GenericCAO::addToScene()
+ tx * v2f(col+1, row+1),
+ tx * v2f(col, row+1),
+ tx * v2f(col, row),
+ tx * v2f(col+1, row),
+ };
+ auto mesh = m_meshnode->getMesh();
+ setMeshBufferTextureCoords(mesh->getMeshBuffer(0), t, 4);
+ setMeshBufferTextureCoords(mesh->getMeshBuffer(1), t, 4);
+ }
+ }
}
// Do not pass by reference, see header.
}
}
- if (m_animated_meshnode) {
+ else if (m_animated_meshnode) {
if (m_prop.visual == "mesh") {
for (u32 i = 0; i < m_prop.textures.size() &&
i < m_animated_meshnode->getMaterialCount(); ++i) {
}
}
}
- if(m_meshnode)
- {
+
+ else if (m_meshnode) {
if(m_prop.visual == "cube")
{
for (u32 i = 0; i < 6; ++i)
setMeshColor(mesh, m_prop.colors[0]);
}
}
+ // Prevent showing the player after changing texture
+ if (m_is_local_player)
+ updateMeshCulling();
}
void GenericCAO::updateAnimation()
bone->updateAbsolutePosition();
}
}
+ // The following is needed for set_bone_pos to propagate to
+ // attached objects correctly.
+ // Irrlicht ought to do this, but doesn't when using EJUOR_CONTROL.
+ for (u32 i = 0; i < m_animated_meshnode->getJointCount(); ++i) {
+ auto bone = m_animated_meshnode->getJointNode(i);
+ // Look for the root bone.
+ if (bone && bone->getParent() == m_animated_meshnode) {
+ // Update entire skeleton.
+ bone->updateAbsolutePositionOfAllChildren();
+ break;
+ }
+ }
}
void GenericCAO::updateAttachments()
* - glow: handled by updateLight()
* - any other properties that do not change appearance
*/
+
+ bool uses_legacy_texture = new_.wield_item.empty() &&
+ (new_.visual == "wielditem" || new_.visual == "item");
// Ordered to compare primitive types before std::vectors
return old.backface_culling != new_.backface_culling ||
old.is_visible != new_.is_visible ||
old.mesh != new_.mesh ||
+ old.shaded != new_.shaded ||
old.use_texture_alpha != new_.use_texture_alpha ||
old.visual != new_.visual ||
old.visual_size != new_.visual_size ||
old.wield_item != new_.wield_item ||
- old.colors != new_.colors;
+ old.colors != new_.colors ||
+ (uses_legacy_texture && old.textures != new_.textures);
}
void GenericCAO::processMessage(const std::string &data)
u8 cmd = readU8(is);
if (cmd == AO_CMD_SET_PROPERTIES) {
ObjectProperties newprops;
+ newprops.show_on_minimap = m_is_player; // default
+
newprops.deSerialize(is);
// Check what exactly changed
if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
m_prop.nametag = m_name;
+ if (m_is_local_player)
+ m_prop.show_on_minimap = false;
if (expire_visuals) {
expireVisuals();
updateTextures(m_current_texture_modifier);
}
updateNametag();
+ updateMarker();
}
} else if (cmd == AO_CMD_UPDATE_POSITION) {
// Not sent by the server if this object is an attachment.
rot_translator.update(m_rotation, false, update_interval);
updateNodePos();
} else if (cmd == AO_CMD_SET_TEXTURE_MOD) {
- std::string mod = deSerializeString(is);
+ std::string mod = deSerializeString16(is);
// immediately reset a engine issued texture modifier if a mod sends a different one
if (m_reset_textures_timer > 0) {
m_animation_speed = readF32(is);
updateAnimationSpeed();
} else if (cmd == AO_CMD_SET_BONE_POSITION) {
- std::string bone = deSerializeString(is);
+ std::string bone = deSerializeString16(is);
v3f position = readV3F32(is);
v3f rotation = readV3F32(is);
m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
// updateBonePosition(); now called every step
} else if (cmd == AO_CMD_ATTACH_TO) {
u16 parent_id = readS16(is);
- std::string bone = deSerializeString(is);
+ std::string bone = deSerializeString16(is);
v3f position = readV3F32(is);
v3f rotation = readV3F32(is);
+ bool force_visible = readU8(is); // Returns false for EOF
- setAttachment(parent_id, bone, position, rotation);
-
- // localplayer itself can't be attached to localplayer
- if (!m_is_local_player)
- m_is_visible = !m_attached_to_local;
+ setAttachment(parent_id, bone, position, rotation, force_visible);
} else if (cmd == AO_CMD_PUNCHED) {
u16 result_hp = readU16(is);
int armor_groups_size = readU16(is);
for(int i=0; i<armor_groups_size; i++)
{
- std::string name = deSerializeString(is);
+ std::string name = deSerializeString16(is);
int rating = readS16(is);
m_armor_groups[name] = rating;
}
return os.str();
}
+void GenericCAO::updateMeshCulling()
+{
+ if (!m_is_local_player)
+ return;
+
+ const bool hidden = m_client->getCamera()->getCameraMode() == CAMERA_MODE_FIRST;
+
+ if (m_meshnode && m_prop.visual == "upright_sprite") {
+ u32 buffers = m_meshnode->getMesh()->getMeshBufferCount();
+ for (u32 i = 0; i < buffers; i++) {
+ video::SMaterial &mat = m_meshnode->getMesh()->getMeshBuffer(i)->getMaterial();
+ // upright sprite has no backface culling
+ mat.setFlag(video::EMF_FRONT_FACE_CULLING, hidden);
+ }
+ return;
+ }
+
+ irr::scene::ISceneNode *node = getSceneNode();
+ if (!node)
+ return;
+
+ if (hidden) {
+ // Hide the mesh by culling both front and
+ // back faces. Serious hackyness but it works for our
+ // purposes. This also preserves the skeletal armature.
+ node->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
+ true);
+ node->setMaterialFlag(video::EMF_FRONT_FACE_CULLING,
+ true);
+ } else {
+ // Restore mesh visibility.
+ node->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
+ m_prop.backface_culling);
+ node->setMaterialFlag(video::EMF_FRONT_FACE_CULLING,
+ false);
+ }
+}
+
// Prototype
GenericCAO proto_GenericCAO(NULL, NULL);