3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "content_cao.h"
21 #include <IBillboardSceneNode.h>
22 #include <ICameraSceneNode.h>
23 #include <ITextSceneNode.h>
24 #include <IMeshManipulator.h>
25 #include <IAnimatedMeshSceneNode.h>
26 #include "client/client.h"
27 #include "client/renderingengine.h"
28 #include "client/sound.h"
29 #include "client/tile.h"
30 #include "util/basic_macros.h"
31 #include "util/numeric.h" // For IntervalLimiter & setPitchYawRoll
32 #include "util/serialize.h"
33 #include "camera.h" // CameraModes
34 #include "collision.h"
35 #include "content_cso.h"
36 #include "environment.h"
38 #include "localplayer.h"
42 #include "serialization.h" // For decompressZlib
46 #include "wieldmesh.h"
49 #include "client/shader.h"
52 struct ToolCapabilities;
54 std::unordered_map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
57 void SmoothTranslator<T>::init(T current)
60 val_current = current;
63 anim_time_counter = 0;
68 void SmoothTranslator<T>::update(T new_target, bool is_end_position, float update_interval)
70 aim_is_end = is_end_position;
71 val_old = val_current;
72 val_target = new_target;
73 if (update_interval > 0) {
74 anim_time = update_interval;
76 if (anim_time < 0.001 || anim_time > 1.0)
77 anim_time = anim_time_counter;
79 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
81 anim_time_counter = 0;
85 void SmoothTranslator<T>::translate(f32 dtime)
87 anim_time_counter = anim_time_counter + dtime;
88 T val_diff = val_target - val_old;
90 if (anim_time > 0.001)
91 moveratio = anim_time_counter / anim_time;
92 f32 move_end = aim_is_end ? 1.0 : 1.5;
94 // Move a bit less than should, to avoid oscillation
95 moveratio = std::min(moveratio * 0.8f, move_end);
96 val_current = val_old + val_diff * moveratio;
99 void SmoothTranslatorWrapped::translate(f32 dtime)
101 anim_time_counter = anim_time_counter + dtime;
102 f32 val_diff = std::abs(val_target - val_old);
103 if (val_diff > 180.f)
104 val_diff = 360.f - val_diff;
107 if (anim_time > 0.001)
108 moveratio = anim_time_counter / anim_time;
109 f32 move_end = aim_is_end ? 1.0 : 1.5;
111 // Move a bit less than should, to avoid oscillation
112 moveratio = std::min(moveratio * 0.8f, move_end);
113 wrappedApproachShortest(val_current, val_target,
114 val_diff * moveratio, 360.f);
117 void SmoothTranslatorWrappedv3f::translate(f32 dtime)
119 anim_time_counter = anim_time_counter + dtime;
122 val_diff_v3f.X = std::abs(val_target.X - val_old.X);
123 val_diff_v3f.Y = std::abs(val_target.Y - val_old.Y);
124 val_diff_v3f.Z = std::abs(val_target.Z - val_old.Z);
126 if (val_diff_v3f.X > 180.f)
127 val_diff_v3f.X = 360.f - val_diff_v3f.X;
129 if (val_diff_v3f.Y > 180.f)
130 val_diff_v3f.Y = 360.f - val_diff_v3f.Y;
132 if (val_diff_v3f.Z > 180.f)
133 val_diff_v3f.Z = 360.f - val_diff_v3f.Z;
136 if (anim_time > 0.001)
137 moveratio = anim_time_counter / anim_time;
138 f32 move_end = aim_is_end ? 1.0 : 1.5;
140 // Move a bit less than should, to avoid oscillation
141 moveratio = std::min(moveratio * 0.8f, move_end);
142 wrappedApproachShortest(val_current.X, val_target.X,
143 val_diff_v3f.X * moveratio, 360.f);
145 wrappedApproachShortest(val_current.Y, val_target.Y,
146 val_diff_v3f.Y * moveratio, 360.f);
148 wrappedApproachShortest(val_current.Z, val_target.Z,
149 val_diff_v3f.Z * moveratio, 360.f);
156 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
157 float txs, float tys, int col, int row)
159 video::SMaterial& material = bill->getMaterial(0);
160 core::matrix4& matrix = material.getTextureMatrix(0);
161 matrix.setTextureTranslate(txs*col, tys*row);
162 matrix.setTextureScale(txs, tys);
169 class TestCAO : public ClientActiveObject
172 TestCAO(Client *client, ClientEnvironment *env);
173 virtual ~TestCAO() = default;
175 ActiveObjectType getType() const
177 return ACTIVEOBJECT_TYPE_TEST;
180 static ClientActiveObject* create(Client *client, ClientEnvironment *env);
182 void addToScene(ITextureSource *tsrc);
183 void removeFromScene(bool permanent);
184 void updateLight(u32 day_night_ratio);
185 v3s16 getLightPosition();
186 void updateNodePos();
188 void step(float dtime, ClientEnvironment *env);
190 void processMessage(const std::string &data);
192 bool getCollisionBox(aabb3f *toset) const { return false; }
194 scene::IMeshSceneNode *m_node;
199 TestCAO proto_TestCAO(NULL, NULL);
201 TestCAO::TestCAO(Client *client, ClientEnvironment *env):
202 ClientActiveObject(0, client, env),
204 m_position(v3f(0,10*BS,0))
206 ClientActiveObject::registerType(getType(), create);
209 ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
211 return new TestCAO(client, env);
214 void TestCAO::addToScene(ITextureSource *tsrc)
219 //video::IVideoDriver* driver = smgr->getVideoDriver();
221 scene::SMesh *mesh = new scene::SMesh();
222 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
223 video::SColor c(255,255,255,255);
224 video::S3DVertex vertices[4] =
226 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
227 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
228 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
229 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
231 u16 indices[] = {0,1,2,2,3,0};
232 buf->append(vertices, 4, indices, 6);
234 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
235 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
236 buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
237 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
238 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
239 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
241 mesh->addMeshBuffer(buf);
243 m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
248 void TestCAO::removeFromScene(bool permanent)
257 void TestCAO::updateLight(u32 day_night_ratio)
261 v3s16 TestCAO::getLightPosition()
263 return floatToInt(m_position, BS);
266 void TestCAO::updateNodePos()
271 m_node->setPosition(m_position);
272 //m_node->setRotation(v3f(0, 45, 0));
275 void TestCAO::step(float dtime, ClientEnvironment *env)
279 v3f rot = m_node->getRotation();
280 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
281 rot.Y += dtime * 180;
282 m_node->setRotation(rot);
286 void TestCAO::processMessage(const std::string &data)
288 infostream<<"TestCAO: Got data: "<<data<<std::endl;
289 std::istringstream is(data, std::ios::binary);
307 #include "clientobject.h"
309 GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
310 ClientActiveObject(0, client, env)
312 if (client == NULL) {
313 ClientActiveObject::registerType(getType(), create);
319 bool GenericCAO::getCollisionBox(aabb3f *toset) const
323 //update collision box
324 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
325 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
327 toset->MinEdge += m_position;
328 toset->MaxEdge += m_position;
336 bool GenericCAO::collideWithObjects() const
338 return m_prop.collideWithObjects;
341 void GenericCAO::initialize(const std::string &data)
343 infostream<<"GenericCAO: Got init data"<<std::endl;
344 processInitData(data);
347 // Check if it's the current player
348 LocalPlayer *player = m_env->getLocalPlayer();
349 if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
350 m_is_local_player = true;
351 m_is_visible = false;
352 player->setCAO(this);
356 m_enable_shaders = g_settings->getBool("enable_shaders");
359 void GenericCAO::processInitData(const std::string &data)
361 std::istringstream is(data, std::ios::binary);
362 const u8 version = readU8(is);
365 errorstream << "GenericCAO: Unsupported init data version"
370 // PROTOCOL_VERSION >= 37
371 m_name = deSerializeString(is);
372 m_is_player = readU8(is);
374 m_position = readV3F32(is);
375 m_rotation = readV3F32(is);
378 const u8 num_messages = readU8(is);
380 for (int i = 0; i < num_messages; i++) {
381 std::string message = deSerializeLongString(is);
382 processMessage(message);
385 m_rotation = wrapDegrees_0_360_v3f(m_rotation);
386 pos_translator.init(m_position);
387 rot_translator.init(m_rotation);
391 GenericCAO::~GenericCAO()
393 removeFromScene(true);
396 bool GenericCAO::getSelectionBox(aabb3f *toset) const
398 if (!m_prop.is_visible || !m_is_visible || m_is_local_player
399 || !m_prop.pointable) {
402 *toset = m_selection_box;
406 const v3f GenericCAO::getPosition() const
409 return pos_translator.val_current;
411 // Calculate real position in world based on MatrixNode
413 v3s16 camera_offset = m_env->getCameraOffset();
414 return m_matrixnode->getAbsolutePosition() +
415 intToFloat(camera_offset, BS);
421 const bool GenericCAO::isImmortal()
423 return itemgroup_get(getGroups(), "immortal");
426 scene::ISceneNode *GenericCAO::getSceneNode() const
432 if (m_animated_meshnode) {
433 return m_animated_meshnode;
436 if (m_wield_meshnode) {
437 return m_wield_meshnode;
446 scene::IAnimatedMeshSceneNode *GenericCAO::getAnimatedMeshSceneNode() const
448 return m_animated_meshnode;
451 void GenericCAO::setChildrenVisible(bool toset)
453 for (u16 cao_id : m_attachment_child_ids) {
454 GenericCAO *obj = m_env->getGenericCAO(cao_id);
456 obj->setVisible(toset);
461 void GenericCAO::setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation)
463 int old_parent = m_attachment_parent_id;
464 m_attachment_parent_id = parent_id;
465 m_attachment_bone = bone;
466 m_attachment_position = position;
467 m_attachment_rotation = rotation;
469 ClientActiveObject *parent = m_env->getActiveObject(parent_id);
471 if (parent_id != old_parent) {
472 if (auto *o = m_env->getActiveObject(old_parent))
473 o->removeAttachmentChild(m_id);
475 parent->addAttachmentChild(m_id);
482 void GenericCAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
485 *parent_id = m_attachment_parent_id;
486 *bone = m_attachment_bone;
487 *position = m_attachment_position;
488 *rotation = m_attachment_rotation;
491 void GenericCAO::clearChildAttachments()
493 // Cannot use for-loop here: setAttachment() modifies 'm_attachment_child_ids'!
494 while (!m_attachment_child_ids.empty()) {
495 int child_id = *m_attachment_child_ids.begin();
497 if (ClientActiveObject *child = m_env->getActiveObject(child_id))
498 child->setAttachment(0, "", v3f(), v3f());
500 removeAttachmentChild(child_id);
504 void GenericCAO::clearParentAttachment()
506 if (m_attachment_parent_id)
507 setAttachment(0, "", m_attachment_position, m_attachment_rotation);
509 setAttachment(0, "", v3f(), v3f());
512 void GenericCAO::addAttachmentChild(int child_id)
514 m_attachment_child_ids.insert(child_id);
517 void GenericCAO::removeAttachmentChild(int child_id)
519 m_attachment_child_ids.erase(child_id);
522 ClientActiveObject* GenericCAO::getParent() const
524 return m_attachment_parent_id ? m_env->getActiveObject(m_attachment_parent_id) :
528 void GenericCAO::removeFromScene(bool permanent)
530 // Should be true when removing the object permanently
531 // and false when refreshing (eg: updating visuals)
532 if (m_env && permanent) {
533 // The client does not know whether this object does re-appear to
534 // a later time, thus do not clear child attachments.
536 clearParentAttachment();
540 m_meshnode->remove();
542 m_meshnode = nullptr;
543 } else if (m_animated_meshnode) {
544 m_animated_meshnode->remove();
545 m_animated_meshnode->drop();
546 m_animated_meshnode = nullptr;
547 } else if (m_wield_meshnode) {
548 m_wield_meshnode->remove();
549 m_wield_meshnode->drop();
550 m_wield_meshnode = nullptr;
551 } else if (m_spritenode) {
552 m_spritenode->remove();
553 m_spritenode->drop();
554 m_spritenode = nullptr;
558 m_matrixnode->remove();
559 m_matrixnode->drop();
560 m_matrixnode = nullptr;
564 m_client->getCamera()->removeNametag(m_nametag);
569 void GenericCAO::addToScene(ITextureSource *tsrc)
571 m_smgr = RenderingEngine::get_scene_manager();
573 if (getSceneNode() != NULL) {
577 m_visuals_expired = false;
579 if (!m_prop.is_visible)
582 infostream << "GenericCAO::addToScene(): " << m_prop.visual << std::endl;
584 if (m_enable_shaders) {
585 IShaderSource *shader_source = m_client->getShaderSource();
586 u32 shader_id = shader_source->getShader(
588 (m_prop.use_texture_alpha) ? TILE_MATERIAL_ALPHA : TILE_MATERIAL_BASIC,
590 m_material_type = shader_source->getShaderInfo(shader_id).material;
592 m_material_type = (m_prop.use_texture_alpha) ?
593 video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
596 auto grabMatrixNode = [this] {
597 m_matrixnode = RenderingEngine::get_scene_manager()->
598 addDummyTransformationSceneNode();
599 m_matrixnode->grab();
602 auto setSceneNodeMaterial = [this] (scene::ISceneNode *node) {
603 node->setMaterialFlag(video::EMF_LIGHTING, false);
604 node->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
605 node->setMaterialFlag(video::EMF_FOG_ENABLE, true);
606 node->setMaterialType(m_material_type);
608 if (m_enable_shaders) {
609 node->setMaterialFlag(video::EMF_GOURAUD_SHADING, false);
610 node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
614 if (m_prop.visual == "sprite") {
616 m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
617 m_matrixnode, v2f(1, 1), v3f(0,0,0), -1);
618 m_spritenode->grab();
619 m_spritenode->setMaterialTexture(0,
620 tsrc->getTextureForMesh("unknown_node.png"));
622 setSceneNodeMaterial(m_spritenode);
624 m_spritenode->setSize(v2f(m_prop.visual_size.X,
625 m_prop.visual_size.Y) * BS);
627 const float txs = 1.0 / 1;
628 const float tys = 1.0 / 1;
629 setBillboardTextureMatrix(m_spritenode,
632 } else if (m_prop.visual == "upright_sprite") {
634 scene::SMesh *mesh = new scene::SMesh();
635 double dx = BS * m_prop.visual_size.X / 2;
636 double dy = BS * m_prop.visual_size.Y / 2;
637 video::SColor c(0xFFFFFFFF);
640 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
641 video::S3DVertex vertices[4] = {
642 video::S3DVertex(-dx, -dy, 0, 0,0,1, c, 1,1),
643 video::S3DVertex( dx, -dy, 0, 0,0,1, c, 0,1),
644 video::S3DVertex( dx, dy, 0, 0,0,1, c, 0,0),
645 video::S3DVertex(-dx, dy, 0, 0,0,1, c, 1,0),
648 // Move minimal Y position to 0 (feet position)
649 for (video::S3DVertex &vertex : vertices)
652 u16 indices[] = {0,1,2,2,3,0};
653 buf->append(vertices, 4, indices, 6);
655 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
656 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
657 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
658 buf->getMaterial().MaterialType = m_material_type;
660 if (m_enable_shaders) {
661 buf->getMaterial().EmissiveColor = c;
662 buf->getMaterial().setFlag(video::EMF_GOURAUD_SHADING, false);
663 buf->getMaterial().setFlag(video::EMF_NORMALIZE_NORMALS, true);
667 mesh->addMeshBuffer(buf);
671 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
672 video::S3DVertex vertices[4] = {
673 video::S3DVertex( dx,-dy, 0, 0,0,-1, c, 1,1),
674 video::S3DVertex(-dx,-dy, 0, 0,0,-1, c, 0,1),
675 video::S3DVertex(-dx, dy, 0, 0,0,-1, c, 0,0),
676 video::S3DVertex( dx, dy, 0, 0,0,-1, c, 1,0),
679 // Move minimal Y position to 0 (feet position)
680 for (video::S3DVertex &vertex : vertices)
683 u16 indices[] = {0,1,2,2,3,0};
684 buf->append(vertices, 4, indices, 6);
686 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
687 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
688 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
689 buf->getMaterial().MaterialType = m_material_type;
691 if (m_enable_shaders) {
692 buf->getMaterial().EmissiveColor = c;
693 buf->getMaterial().setFlag(video::EMF_GOURAUD_SHADING, false);
694 buf->getMaterial().setFlag(video::EMF_NORMALIZE_NORMALS, true);
698 mesh->addMeshBuffer(buf);
701 m_meshnode = RenderingEngine::get_scene_manager()->
702 addMeshSceneNode(mesh, m_matrixnode);
705 // Set it to use the materials of the meshbuffers directly.
706 // This is needed for changing the texture in the future
707 m_meshnode->setReadOnlyMaterials(true);
708 } else if (m_prop.visual == "cube") {
710 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
711 m_meshnode = RenderingEngine::get_scene_manager()->
712 addMeshSceneNode(mesh, m_matrixnode);
716 m_meshnode->setScale(m_prop.visual_size);
717 m_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
718 m_prop.backface_culling);
720 setSceneNodeMaterial(m_meshnode);
721 } else if (m_prop.visual == "mesh") {
723 scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
725 m_animated_meshnode = RenderingEngine::get_scene_manager()->
726 addAnimatedMeshSceneNode(mesh, m_matrixnode);
727 m_animated_meshnode->grab();
728 mesh->drop(); // The scene node took hold of it
729 m_animated_meshnode->animateJoints(); // Needed for some animations
730 m_animated_meshnode->setScale(m_prop.visual_size);
732 // set vertex colors to ensure alpha is set
733 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(0xFFFFFFFF));
735 setAnimatedMeshColor(m_animated_meshnode, video::SColor(0xFFFFFFFF));
737 setSceneNodeMaterial(m_animated_meshnode);
739 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
740 m_prop.backface_culling);
742 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
743 } else if (m_prop.visual == "wielditem" || m_prop.visual == "item") {
746 if (m_prop.wield_item.empty()) {
747 // Old format, only textures are specified.
748 infostream << "textures: " << m_prop.textures.size() << std::endl;
749 if (!m_prop.textures.empty()) {
750 infostream << "textures[0]: " << m_prop.textures[0]
752 IItemDefManager *idef = m_client->idef();
753 item = ItemStack(m_prop.textures[0], 1, 0, idef);
756 infostream << "serialized form: " << m_prop.wield_item << std::endl;
757 item.deSerialize(m_prop.wield_item, m_client->idef());
759 m_wield_meshnode = new WieldMeshSceneNode(
760 RenderingEngine::get_scene_manager(), -1);
761 m_wield_meshnode->setItem(item, m_client,
762 (m_prop.visual == "wielditem"));
764 m_wield_meshnode->setScale(m_prop.visual_size / 2.0f);
765 m_wield_meshnode->setColor(video::SColor(0xFFFFFFFF));
767 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
768 <<"\" not supported"<<std::endl;
771 /* don't update while punch texture modifier is active */
772 if (m_reset_textures_timer < 0)
773 updateTextures(m_current_texture_modifier);
775 scene::ISceneNode *node = getSceneNode();
777 if (node && m_matrixnode)
778 node->setParent(m_matrixnode);
783 updateBonePosition();
785 setNodeLight(m_last_light);
788 void GenericCAO::updateLight(u32 day_night_ratio)
793 v3s16 p = getLightPosition();
794 MapNode n = m_env->getMap().getNode(p, &pos_ok);
796 light_at_pos = n.getLightBlend(day_night_ratio, m_client->ndef());
798 light_at_pos = blend_light(day_night_ratio, LIGHT_SUN, 0);
800 u8 light = decode_light(light_at_pos);
801 if (light != m_last_light) {
802 m_last_light = light;
807 void GenericCAO::setNodeLight(u8 light)
809 video::SColor color(255, light, light, light);
811 if (m_prop.visual == "wielditem" || m_prop.visual == "item") {
812 if (m_wield_meshnode)
813 m_wield_meshnode->setNodeLightColor(color);
817 if (m_enable_shaders) {
818 if (m_prop.visual == "upright_sprite") {
822 scene::IMesh *mesh = m_meshnode->getMesh();
823 for (u32 i = 0; i < mesh->getMeshBufferCount(); ++i) {
824 scene::IMeshBuffer *buf = mesh->getMeshBuffer(i);
825 buf->getMaterial().EmissiveColor = color;
828 scene::ISceneNode *node = getSceneNode();
832 for (u32 i = 0; i < node->getMaterialCount(); ++i) {
833 video::SMaterial &material = node->getMaterial(i);
834 material.EmissiveColor = color;
839 setMeshColor(m_meshnode->getMesh(), color);
840 } else if (m_animated_meshnode) {
841 setAnimatedMeshColor(m_animated_meshnode, color);
842 } else if (m_spritenode) {
843 m_spritenode->setColor(color);
848 v3s16 GenericCAO::getLightPosition()
851 return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
853 return floatToInt(m_position, BS);
856 void GenericCAO::updateNametag()
858 if (m_is_local_player) // No nametag for local player
861 if (m_prop.nametag.empty()) {
864 m_client->getCamera()->removeNametag(m_nametag);
870 scene::ISceneNode *node = getSceneNode();
875 pos.Y = m_prop.selectionbox.MaxEdge.Y + 0.3f;
878 m_nametag = m_client->getCamera()->addNametag(node,
879 m_prop.nametag, m_prop.nametag_color, pos);
882 m_nametag->nametag_text = m_prop.nametag;
883 m_nametag->nametag_color = m_prop.nametag_color;
884 m_nametag->nametag_pos = pos;
888 void GenericCAO::updateNodePos()
890 if (getParent() != NULL)
893 scene::ISceneNode *node = getSceneNode();
896 v3s16 camera_offset = m_env->getCameraOffset();
897 v3f pos = pos_translator.val_current -
898 intToFloat(camera_offset, BS);
899 getPosRotMatrix().setTranslation(pos);
900 if (node != m_spritenode) { // rotate if not a sprite
901 v3f rot = m_is_local_player ? -m_rotation : -rot_translator.val_current;
902 setPitchYawRoll(getPosRotMatrix(), rot);
907 void GenericCAO::step(float dtime, ClientEnvironment *env)
909 if (m_animated_meshnode) {
910 m_animated_meshnode->animateJoints();
911 updateBonePosition();
914 // Handle model animations and update positions instantly to prevent lags
915 if (m_is_local_player) {
916 LocalPlayer *player = m_env->getLocalPlayer();
917 m_position = player->getPosition();
918 pos_translator.val_current = m_position;
919 m_rotation.Y = wrapDegrees_0_360(player->getYaw());
920 rot_translator.val_current = m_rotation;
923 int old_anim = player->last_animation;
924 float old_anim_speed = player->last_animation_speed;
925 m_velocity = v3f(0,0,0);
926 m_acceleration = v3f(0,0,0);
927 const PlayerControl &controls = player->getPlayerControl();
929 bool walking = false;
930 if (controls.up || controls.down || controls.left || controls.right ||
931 controls.forw_move_joystick_axis != 0.f ||
932 controls.sidew_move_joystick_axis != 0.f)
935 f32 new_speed = player->local_animation_speed;
936 v2s32 new_anim = v2s32(0,0);
937 bool allow_update = false;
939 // increase speed if using fast or flying fast
940 if((g_settings->getBool("fast_move") &&
941 m_client->checkLocalPrivilege("fast")) &&
943 (!player->touching_ground &&
944 g_settings->getBool("free_move") &&
945 m_client->checkLocalPrivilege("fly"))))
947 // slowdown speed if sneeking
948 if (controls.sneak && walking)
951 if (walking && (controls.LMB || controls.RMB)) {
952 new_anim = player->local_animations[3];
953 player->last_animation = WD_ANIM;
955 new_anim = player->local_animations[1];
956 player->last_animation = WALK_ANIM;
957 } else if(controls.LMB || controls.RMB) {
958 new_anim = player->local_animations[2];
959 player->last_animation = DIG_ANIM;
962 // Apply animations if input detected and not attached
963 // or set idle animation
964 if ((new_anim.X + new_anim.Y) > 0 && !getParent()) {
966 m_animation_range = new_anim;
967 m_animation_speed = new_speed;
968 player->last_animation_speed = m_animation_speed;
970 player->last_animation = NO_ANIM;
972 if (old_anim != NO_ANIM) {
973 m_animation_range = player->local_animations[0];
978 // Update local player animations
979 if ((player->last_animation != old_anim ||
980 m_animation_speed != old_anim_speed) &&
981 player->last_animation != NO_ANIM && allow_update)
987 if (m_visuals_expired && m_smgr) {
988 m_visuals_expired = false;
990 // Attachments, part 1: All attached objects must be unparented first,
991 // or Irrlicht causes a segmentation fault
992 for (u16 cao_id : m_attachment_child_ids) {
993 ClientActiveObject *obj = m_env->getActiveObject(cao_id);
995 scene::ISceneNode *child_node = obj->getSceneNode();
996 // The node's parent is always an IDummyTraformationSceneNode,
997 // so we need to reparent that one instead.
999 child_node->getParent()->setParent(m_smgr->getRootSceneNode());
1003 removeFromScene(false);
1004 addToScene(m_client->tsrc());
1006 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1007 for (u16 cao_id : m_attachment_child_ids) {
1008 ClientActiveObject *obj = m_env->getActiveObject(cao_id);
1010 obj->updateAttachments();
1014 // Make sure m_is_visible is always applied
1015 scene::ISceneNode *node = getSceneNode();
1017 node->setVisible(m_is_visible);
1019 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1021 // Set these for later
1022 m_position = getPosition();
1023 m_velocity = v3f(0,0,0);
1024 m_acceleration = v3f(0,0,0);
1025 pos_translator.val_current = m_position;
1026 pos_translator.val_target = m_position;
1028 rot_translator.translate(dtime);
1029 v3f lastpos = pos_translator.val_current;
1033 aabb3f box = m_prop.collisionbox;
1036 collisionMoveResult moveresult;
1037 f32 pos_max_d = BS*0.125; // Distance per iteration
1038 v3f p_pos = m_position;
1039 v3f p_velocity = m_velocity;
1040 moveresult = collisionMoveSimple(env,env->getGameDef(),
1041 pos_max_d, box, m_prop.stepheight, dtime,
1042 &p_pos, &p_velocity, m_acceleration,
1043 this, m_prop.collideWithObjects);
1046 m_velocity = p_velocity;
1048 bool is_end_position = moveresult.collides;
1049 pos_translator.update(m_position, is_end_position, dtime);
1051 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1052 m_velocity += dtime * m_acceleration;
1053 pos_translator.update(m_position, pos_translator.aim_is_end,
1054 pos_translator.anim_time);
1056 pos_translator.translate(dtime);
1059 float moved = lastpos.getDistanceFrom(pos_translator.val_current);
1060 m_step_distance_counter += moved;
1061 if (m_step_distance_counter > 1.5f * BS) {
1062 m_step_distance_counter = 0.0f;
1063 if (!m_is_local_player && m_prop.makes_footstep_sound) {
1064 const NodeDefManager *ndef = m_client->ndef();
1065 v3s16 p = floatToInt(getPosition() +
1066 v3f(0.0f, (m_prop.collisionbox.MinEdge.Y - 0.5f) * BS, 0.0f), BS);
1067 MapNode n = m_env->getMap().getNode(p);
1068 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1069 // Reduce footstep gain, as non-local-player footsteps are
1072 m_client->sound()->playSoundAt(spec, false, getPosition());
1077 m_anim_timer += dtime;
1078 if(m_anim_timer >= m_anim_framelength)
1080 m_anim_timer -= m_anim_framelength;
1082 if(m_anim_frame >= m_anim_num_frames)
1088 if(m_reset_textures_timer >= 0)
1090 m_reset_textures_timer -= dtime;
1091 if(m_reset_textures_timer <= 0) {
1092 m_reset_textures_timer = -1;
1093 updateTextures(m_previous_texture_modifier);
1097 if (!getParent() && std::fabs(m_prop.automatic_rotate) > 0.001) {
1098 // This is the child node's rotation. It is only used for automatic_rotate.
1099 v3f local_rot = node->getRotation();
1100 local_rot.Y = modulo360f(local_rot.Y - dtime * core::RADTODEG *
1101 m_prop.automatic_rotate);
1102 node->setRotation(local_rot);
1105 if (!getParent() && m_prop.automatic_face_movement_dir &&
1106 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)) {
1107 float target_yaw = atan2(m_velocity.Z, m_velocity.X) * 180 / M_PI
1108 + m_prop.automatic_face_movement_dir_offset;
1109 float max_rotation_per_sec =
1110 m_prop.automatic_face_movement_max_rotation_per_sec;
1112 if (max_rotation_per_sec > 0) {
1113 wrappedApproachShortest(m_rotation.Y, target_yaw,
1114 dtime * max_rotation_per_sec, 360.f);
1116 // Negative values of max_rotation_per_sec mean disabled.
1117 m_rotation.Y = target_yaw;
1120 rot_translator.val_current = m_rotation;
1125 void GenericCAO::updateTexturePos()
1129 scene::ICameraSceneNode* camera =
1130 m_spritenode->getSceneManager()->getActiveCamera();
1133 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1134 - camera->getAbsolutePosition();
1135 cam_to_entity.normalize();
1137 int row = m_tx_basepos.Y;
1138 int col = m_tx_basepos.X;
1140 if (m_tx_select_horiz_by_yawpitch) {
1141 if (cam_to_entity.Y > 0.75)
1143 else if (cam_to_entity.Y < -0.75)
1147 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1148 float dir = mob_dir - m_rotation.Y;
1149 dir = wrapDegrees_180(dir);
1150 if (std::fabs(wrapDegrees_180(dir - 0)) <= 45.1f)
1152 else if(std::fabs(wrapDegrees_180(dir - 90)) <= 45.1f)
1154 else if(std::fabs(wrapDegrees_180(dir - 180)) <= 45.1f)
1156 else if(std::fabs(wrapDegrees_180(dir + 90)) <= 45.1f)
1163 // Animation goes downwards
1164 row += m_anim_frame;
1166 float txs = m_tx_size.X;
1167 float tys = m_tx_size.Y;
1168 setBillboardTextureMatrix(m_spritenode, txs, tys, col, row);
1172 // Do not pass by reference, see header.
1173 void GenericCAO::updateTextures(std::string mod)
1175 ITextureSource *tsrc = m_client->tsrc();
1177 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1178 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1179 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1181 m_previous_texture_modifier = m_current_texture_modifier;
1182 m_current_texture_modifier = mod;
1183 m_glow = m_prop.glow;
1186 if (m_prop.visual == "sprite") {
1187 std::string texturestring = "unknown_node.png";
1188 if (!m_prop.textures.empty())
1189 texturestring = m_prop.textures[0];
1190 texturestring += mod;
1191 m_spritenode->getMaterial(0).MaterialType = m_material_type;
1192 m_spritenode->getMaterial(0).MaterialTypeParam = 0.5f;
1193 m_spritenode->setMaterialTexture(0,
1194 tsrc->getTextureForMesh(texturestring));
1196 // This allows setting per-material colors. However, until a real lighting
1197 // system is added, the code below will have no effect. Once MineTest
1198 // has directional lighting, it should work automatically.
1199 if (!m_prop.colors.empty()) {
1200 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1201 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1202 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1205 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1206 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1207 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1211 if (m_animated_meshnode) {
1212 if (m_prop.visual == "mesh") {
1213 for (u32 i = 0; i < m_prop.textures.size() &&
1214 i < m_animated_meshnode->getMaterialCount(); ++i) {
1215 std::string texturestring = m_prop.textures[i];
1216 if (texturestring.empty())
1217 continue; // Empty texture string means don't modify that material
1218 texturestring += mod;
1219 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1221 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1225 // Set material flags and texture
1226 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1227 material.MaterialType = m_material_type;
1228 material.MaterialTypeParam = 0.5f;
1229 material.TextureLayer[0].Texture = texture;
1230 material.setFlag(video::EMF_LIGHTING, true);
1231 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1232 material.setFlag(video::EMF_BACK_FACE_CULLING, m_prop.backface_culling);
1234 // don't filter low-res textures, makes them look blurry
1235 // player models have a res of 64
1236 const core::dimension2d<u32> &size = texture->getOriginalSize();
1237 const u32 res = std::min(size.Height, size.Width);
1238 use_trilinear_filter &= res > 64;
1239 use_bilinear_filter &= res > 64;
1241 m_animated_meshnode->getMaterial(i)
1242 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1243 m_animated_meshnode->getMaterial(i)
1244 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1245 m_animated_meshnode->getMaterial(i)
1246 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1248 for (u32 i = 0; i < m_prop.colors.size() &&
1249 i < m_animated_meshnode->getMaterialCount(); ++i)
1251 // This allows setting per-material colors. However, until a real lighting
1252 // system is added, the code below will have no effect. Once MineTest
1253 // has directional lighting, it should work automatically.
1254 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1255 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1256 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1262 if(m_prop.visual == "cube")
1264 for (u32 i = 0; i < 6; ++i)
1266 std::string texturestring = "unknown_node.png";
1267 if(m_prop.textures.size() > i)
1268 texturestring = m_prop.textures[i];
1269 texturestring += mod;
1272 // Set material flags and texture
1273 video::SMaterial& material = m_meshnode->getMaterial(i);
1274 material.MaterialType = m_material_type;
1275 material.MaterialTypeParam = 0.5f;
1276 material.setFlag(video::EMF_LIGHTING, false);
1277 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1278 material.setTexture(0,
1279 tsrc->getTextureForMesh(texturestring));
1280 material.getTextureMatrix(0).makeIdentity();
1282 // This allows setting per-material colors. However, until a real lighting
1283 // system is added, the code below will have no effect. Once MineTest
1284 // has directional lighting, it should work automatically.
1285 if(m_prop.colors.size() > i)
1287 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1288 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1289 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1292 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1293 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1294 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1296 } else if (m_prop.visual == "upright_sprite") {
1297 scene::IMesh *mesh = m_meshnode->getMesh();
1299 std::string tname = "unknown_object.png";
1300 if (!m_prop.textures.empty())
1301 tname = m_prop.textures[0];
1303 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1304 buf->getMaterial().setTexture(0,
1305 tsrc->getTextureForMesh(tname));
1307 // This allows setting per-material colors. However, until a real lighting
1308 // system is added, the code below will have no effect. Once MineTest
1309 // has directional lighting, it should work automatically.
1310 if(!m_prop.colors.empty()) {
1311 buf->getMaterial().AmbientColor = m_prop.colors[0];
1312 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1313 buf->getMaterial().SpecularColor = m_prop.colors[0];
1316 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1317 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1318 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1321 std::string tname = "unknown_object.png";
1322 if (m_prop.textures.size() >= 2)
1323 tname = m_prop.textures[1];
1324 else if (!m_prop.textures.empty())
1325 tname = m_prop.textures[0];
1327 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1328 buf->getMaterial().setTexture(0,
1329 tsrc->getTextureForMesh(tname));
1331 // This allows setting per-material colors. However, until a real lighting
1332 // system is added, the code below will have no effect. Once MineTest
1333 // has directional lighting, it should work automatically.
1334 if (m_prop.colors.size() >= 2) {
1335 buf->getMaterial().AmbientColor = m_prop.colors[1];
1336 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1337 buf->getMaterial().SpecularColor = m_prop.colors[1];
1338 } else if (!m_prop.colors.empty()) {
1339 buf->getMaterial().AmbientColor = m_prop.colors[0];
1340 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1341 buf->getMaterial().SpecularColor = m_prop.colors[0];
1344 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1345 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1346 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1348 // Set mesh color (only if lighting is disabled)
1349 if (!m_prop.colors.empty() && m_glow < 0)
1350 setMeshColor(mesh, m_prop.colors[0]);
1355 void GenericCAO::updateAnimation()
1357 if (!m_animated_meshnode)
1360 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1361 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1362 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1363 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1364 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1365 m_animated_meshnode->setTransitionTime(m_animation_blend);
1366 // Requires Irrlicht 1.8 or greater
1367 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1368 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1369 m_animated_meshnode->setLoopMode(m_animation_loop);
1373 void GenericCAO::updateAnimationSpeed()
1375 if (!m_animated_meshnode)
1378 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1381 void GenericCAO::updateBonePosition()
1383 if (m_bone_position.empty() || !m_animated_meshnode)
1386 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1387 for (auto &it : m_bone_position) {
1388 std::string bone_name = it.first;
1389 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1391 bone->setPosition(it.second.X);
1392 bone->setRotation(it.second.Y);
1396 // search through bones to find mistakenly rotated bones due to bug in Irrlicht
1397 for (u32 i = 0; i < m_animated_meshnode->getJointCount(); ++i) {
1398 irr::scene::IBoneSceneNode *bone = m_animated_meshnode->getJointNode(i);
1402 //If bone is manually positioned there is no need to perform the bug check
1404 for (auto &it : m_bone_position) {
1405 if (it.first == bone->getName()) {
1413 // Workaround for Irrlicht bug
1414 // We check each bone to see if it has been rotated ~180deg from its expected position due to a bug in Irricht
1415 // when using EJUOR_CONTROL joint control. If the bug is detected we update the bone to the proper position
1416 // and update the bones transformation.
1417 v3f bone_rot = bone->getRelativeTransformation().getRotationDegrees();
1418 float offset = fabsf(bone_rot.X - bone->getRotation().X);
1419 if (offset > 179.9f && offset < 180.1f) {
1420 bone->setRotation(bone_rot);
1421 bone->updateAbsolutePosition();
1426 void GenericCAO::updateAttachments()
1428 ClientActiveObject *parent = getParent();
1430 m_attached_to_local = parent && parent->isLocalPlayer();
1433 Following cases exist:
1434 m_attachment_parent_id == 0 && !parent
1435 This object is not attached
1436 m_attachment_parent_id != 0 && parent
1437 This object is attached
1438 m_attachment_parent_id != 0 && !parent
1439 This object will be attached as soon the parent is known
1440 m_attachment_parent_id == 0 && parent
1444 if (!parent) { // Detach or don't attach
1446 v3f old_pos = getPosition();
1448 m_matrixnode->setParent(m_smgr->getRootSceneNode());
1449 getPosRotMatrix().setTranslation(old_pos);
1450 m_matrixnode->updateAbsolutePosition();
1455 scene::ISceneNode *parent_node = parent->getSceneNode();
1456 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1457 parent->getAnimatedMeshSceneNode();
1458 if (parent_animated_mesh_node && !m_attachment_bone.empty()) {
1459 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1462 if (m_matrixnode && parent_node) {
1463 m_matrixnode->setParent(parent_node);
1464 getPosRotMatrix().setTranslation(m_attachment_position);
1465 //setPitchYawRoll(getPosRotMatrix(), m_attachment_rotation);
1466 // use Irrlicht eulers instead
1467 getPosRotMatrix().setRotationDegrees(m_attachment_rotation);
1468 m_matrixnode->updateAbsolutePosition();
1473 bool GenericCAO::visualExpiryRequired(const ObjectProperties &new_) const
1475 const ObjectProperties &old = m_prop;
1476 // Ordered to compare primitive types before std::vectors
1477 return old.backface_culling != new_.backface_culling ||
1478 old.initial_sprite_basepos != new_.initial_sprite_basepos ||
1479 old.is_visible != new_.is_visible ||
1480 old.mesh != new_.mesh ||
1481 old.spritediv != new_.spritediv ||
1482 old.use_texture_alpha != new_.use_texture_alpha ||
1483 old.visual != new_.visual ||
1484 old.visual_size != new_.visual_size ||
1485 old.wield_item != new_.wield_item ||
1486 old.colors != new_.colors ||
1487 old.textures != new_.textures;
1490 void GenericCAO::processMessage(const std::string &data)
1492 //infostream<<"GenericCAO: Got message"<<std::endl;
1493 std::istringstream is(data, std::ios::binary);
1495 u8 cmd = readU8(is);
1496 if (cmd == AO_CMD_SET_PROPERTIES) {
1497 ObjectProperties newprops;
1498 newprops.deSerialize(is);
1500 // Check what exactly changed
1501 bool expire_visuals = visualExpiryRequired(newprops);
1504 m_prop = std::move(newprops);
1506 m_selection_box = m_prop.selectionbox;
1507 m_selection_box.MinEdge *= BS;
1508 m_selection_box.MaxEdge *= BS;
1510 m_tx_size.X = 1.0f / m_prop.spritediv.X;
1511 m_tx_size.Y = 1.0f / m_prop.spritediv.Y;
1513 if(!m_initial_tx_basepos_set){
1514 m_initial_tx_basepos_set = true;
1515 m_tx_basepos = m_prop.initial_sprite_basepos;
1517 if (m_is_local_player) {
1518 LocalPlayer *player = m_env->getLocalPlayer();
1519 player->makes_footstep_sound = m_prop.makes_footstep_sound;
1520 aabb3f collision_box = m_prop.collisionbox;
1521 collision_box.MinEdge *= BS;
1522 collision_box.MaxEdge *= BS;
1523 player->setCollisionbox(collision_box);
1524 player->setEyeHeight(m_prop.eye_height);
1525 player->setZoomFOV(m_prop.zoom_fov);
1528 if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
1529 m_prop.nametag = m_name;
1532 if (expire_visuals) {
1535 infostream << "GenericCAO: properties updated but expiring visuals"
1536 << " not necessary" << std::endl;
1538 } else if (cmd == AO_CMD_UPDATE_POSITION) {
1539 // Not sent by the server if this object is an attachment.
1540 // We might however get here if the server notices the object being detached before the client.
1541 m_position = readV3F32(is);
1542 m_velocity = readV3F32(is);
1543 m_acceleration = readV3F32(is);
1544 m_rotation = readV3F32(is);
1546 m_rotation = wrapDegrees_0_360_v3f(m_rotation);
1547 bool do_interpolate = readU8(is);
1548 bool is_end_position = readU8(is);
1549 float update_interval = readF32(is);
1551 // Place us a bit higher if we're physical, to not sink into
1552 // the ground due to sucky collision detection...
1554 m_position += v3f(0,0.002,0);
1556 if(getParent() != NULL) // Just in case
1561 if(!m_prop.physical)
1562 pos_translator.update(m_position, is_end_position, update_interval);
1564 pos_translator.init(m_position);
1566 rot_translator.update(m_rotation, false, update_interval);
1568 } else if (cmd == AO_CMD_SET_TEXTURE_MOD) {
1569 std::string mod = deSerializeString(is);
1571 // immediately reset a engine issued texture modifier if a mod sends a different one
1572 if (m_reset_textures_timer > 0) {
1573 m_reset_textures_timer = -1;
1574 updateTextures(m_previous_texture_modifier);
1576 updateTextures(mod);
1577 } else if (cmd == AO_CMD_SET_SPRITE) {
1578 v2s16 p = readV2S16(is);
1579 int num_frames = readU16(is);
1580 float framelength = readF32(is);
1581 bool select_horiz_by_yawpitch = readU8(is);
1584 m_anim_num_frames = num_frames;
1585 m_anim_framelength = framelength;
1586 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1589 } else if (cmd == AO_CMD_SET_PHYSICS_OVERRIDE) {
1590 float override_speed = readF32(is);
1591 float override_jump = readF32(is);
1592 float override_gravity = readF32(is);
1593 // these are sent inverted so we get true when the server sends nothing
1594 bool sneak = !readU8(is);
1595 bool sneak_glitch = !readU8(is);
1596 bool new_move = !readU8(is);
1599 if(m_is_local_player)
1601 LocalPlayer *player = m_env->getLocalPlayer();
1602 player->physics_override_speed = override_speed;
1603 player->physics_override_jump = override_jump;
1604 player->physics_override_gravity = override_gravity;
1605 player->physics_override_sneak = sneak;
1606 player->physics_override_sneak_glitch = sneak_glitch;
1607 player->physics_override_new_move = new_move;
1609 } else if (cmd == AO_CMD_SET_ANIMATION) {
1610 // TODO: change frames send as v2s32 value
1611 v2f range = readV2F32(is);
1612 if (!m_is_local_player) {
1613 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1614 m_animation_speed = readF32(is);
1615 m_animation_blend = readF32(is);
1616 // these are sent inverted so we get true when the server sends nothing
1617 m_animation_loop = !readU8(is);
1620 LocalPlayer *player = m_env->getLocalPlayer();
1621 if(player->last_animation == NO_ANIM)
1623 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1624 m_animation_speed = readF32(is);
1625 m_animation_blend = readF32(is);
1626 // these are sent inverted so we get true when the server sends nothing
1627 m_animation_loop = !readU8(is);
1629 // update animation only if local animations present
1630 // and received animation is unknown (except idle animation)
1631 bool is_known = false;
1632 for (int i = 1;i<4;i++)
1634 if(m_animation_range.Y == player->local_animations[i].Y)
1638 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1643 } else if (cmd == AO_CMD_SET_ANIMATION_SPEED) {
1644 m_animation_speed = readF32(is);
1645 updateAnimationSpeed();
1646 } else if (cmd == AO_CMD_SET_BONE_POSITION) {
1647 std::string bone = deSerializeString(is);
1648 v3f position = readV3F32(is);
1649 v3f rotation = readV3F32(is);
1650 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1652 // updateBonePosition(); now called every step
1653 } else if (cmd == AO_CMD_ATTACH_TO) {
1654 u16 parent_id = readS16(is);
1655 std::string bone = deSerializeString(is);
1656 v3f position = readV3F32(is);
1657 v3f rotation = readV3F32(is);
1659 setAttachment(parent_id, bone, position, rotation);
1661 // localplayer itself can't be attached to localplayer
1662 if (!m_is_local_player)
1663 m_is_visible = !m_attached_to_local;
1664 } else if (cmd == AO_CMD_PUNCHED) {
1665 u16 result_hp = readU16(is);
1667 // Use this instead of the send damage to not interfere with prediction
1668 s32 damage = (s32)m_hp - (s32)result_hp;
1672 if (m_is_local_player)
1673 m_env->getLocalPlayer()->hp = m_hp;
1679 // TODO: Execute defined fast response
1680 // As there is no definition, make a smoke puff
1681 ClientSimpleObject *simple = createSmokePuff(
1682 m_smgr, m_env, m_position,
1683 v2f(m_prop.visual_size.X, m_prop.visual_size.Y) * BS);
1684 m_env->addSimpleObject(simple);
1685 } else if (m_reset_textures_timer < 0 && !m_prop.damage_texture_modifier.empty()) {
1686 m_reset_textures_timer = 0.05;
1688 m_reset_textures_timer += 0.05 * damage;
1689 updateTextures(m_current_texture_modifier + m_prop.damage_texture_modifier);
1694 // Same as 'Server::DiePlayer'
1695 clearParentAttachment();
1696 // Same as 'ObjectRef::l_remove'
1698 clearChildAttachments();
1700 } else if (cmd == AO_CMD_UPDATE_ARMOR_GROUPS) {
1701 m_armor_groups.clear();
1702 int armor_groups_size = readU16(is);
1703 for(int i=0; i<armor_groups_size; i++)
1705 std::string name = deSerializeString(is);
1706 int rating = readS16(is);
1707 m_armor_groups[name] = rating;
1709 } else if (cmd == AO_CMD_SPAWN_INFANT) {
1710 u16 child_id = readU16(is);
1711 u8 type = readU8(is); // maybe this will be useful later
1714 addAttachmentChild(child_id);
1715 } else if (cmd == AO_CMD_OBSOLETE1) {
1716 // Don't do anything and also don't log a warning
1718 warningstream << FUNCTION_NAME
1719 << ": unknown command or outdated client \""
1720 << +cmd << "\"" << std::endl;
1724 /* \pre punchitem != NULL
1726 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1727 float time_from_last_punch)
1729 assert(punchitem); // pre-condition
1730 const ToolCapabilities *toolcap =
1731 &punchitem->getToolCapabilities(m_client->idef());
1732 PunchDamageResult result = getPunchDamage(
1736 time_from_last_punch);
1738 if(result.did_punch && result.damage != 0)
1740 if(result.damage < m_hp)
1742 m_hp -= result.damage;
1745 // TODO: Execute defined fast response
1746 // As there is no definition, make a smoke puff
1747 ClientSimpleObject *simple = createSmokePuff(
1748 m_smgr, m_env, m_position,
1749 v2f(m_prop.visual_size.X, m_prop.visual_size.Y) * BS);
1750 m_env->addSimpleObject(simple);
1752 if (m_reset_textures_timer < 0 && !m_prop.damage_texture_modifier.empty()) {
1753 m_reset_textures_timer = 0.05;
1754 if (result.damage >= 2)
1755 m_reset_textures_timer += 0.05 * result.damage;
1756 updateTextures(m_current_texture_modifier + m_prop.damage_texture_modifier);
1763 std::string GenericCAO::debugInfoText()
1765 std::ostringstream os(std::ios::binary);
1766 os<<"GenericCAO hp="<<m_hp<<"\n";
1768 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1769 i != m_armor_groups.end(); ++i)
1771 os<<i->first<<"="<<i->second<<", ";
1778 GenericCAO proto_GenericCAO(NULL, NULL);