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 <ICameraSceneNode.h>
21 #include <ITextSceneNode.h>
22 #include <IBillboardSceneNode.h>
23 #include <IMeshManipulator.h>
24 #include <IAnimatedMeshSceneNode.h>
25 #include "content_cao.h"
26 #include "util/numeric.h" // For IntervalLimiter & setPitchYawRoll
27 #include "util/serialize.h"
28 #include "util/basic_macros.h"
29 #include "client/sound.h"
30 #include "client/tile.h"
31 #include "environment.h"
32 #include "collision.h"
34 #include "serialization.h" // For decompressZlib
35 #include "clientobject.h"
39 #include "content_cso.h"
42 #include "localplayer.h"
44 #include "camera.h" // CameraModes
46 #include "wieldmesh.h"
49 #include "client/renderingengine.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(u8 light_at_pos);
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(u8 light_at_pos)
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 "genericobject.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);
357 void GenericCAO::processInitData(const std::string &data)
359 std::istringstream is(data, std::ios::binary);
360 const u8 version = readU8(is);
363 errorstream << "GenericCAO: Unsupported init data version"
368 // PROTOCOL_VERSION >= 37
369 m_name = deSerializeString(is);
370 m_is_player = readU8(is);
372 m_position = readV3F32(is);
373 m_rotation = readV3F32(is);
375 const u8 num_messages = readU8(is);
377 for (int i = 0; i < num_messages; i++) {
378 std::string message = deSerializeLongString(is);
379 processMessage(message);
382 m_rotation = wrapDegrees_0_360_v3f(m_rotation);
383 pos_translator.init(m_position);
384 rot_translator.init(m_rotation);
388 GenericCAO::~GenericCAO()
390 removeFromScene(true);
393 bool GenericCAO::getSelectionBox(aabb3f *toset) const
395 if (!m_prop.is_visible || !m_is_visible || m_is_local_player
396 || !m_prop.pointable) {
399 *toset = m_selection_box;
403 v3f GenericCAO::getPosition()
405 if (getParent() != nullptr) {
407 return m_matrixnode->getAbsolutePosition();
411 return pos_translator.val_current;
414 const bool GenericCAO::isImmortal()
416 return itemgroup_get(getGroups(), "immortal");
419 scene::ISceneNode* GenericCAO::getSceneNode()
425 if (m_animated_meshnode) {
426 return m_animated_meshnode;
429 if (m_wield_meshnode) {
430 return m_wield_meshnode;
439 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
441 return m_animated_meshnode;
444 void GenericCAO::setChildrenVisible(bool toset)
446 for (u16 cao_id : m_children) {
447 GenericCAO *obj = m_env->getGenericCAO(cao_id);
449 obj->setVisible(toset);
454 void GenericCAO::setAttachments()
459 ClientActiveObject* GenericCAO::getParent() const
461 ClientActiveObject *obj = NULL;
463 u16 attached_id = m_env->attachement_parent_ids[getId()];
465 if ((attached_id != 0) &&
466 (attached_id != getId())) {
467 obj = m_env->getActiveObject(attached_id);
472 void GenericCAO::removeFromScene(bool permanent)
474 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
475 if((m_env != NULL) && (permanent))
477 for (u16 ci : m_children) {
478 if (m_env->attachement_parent_ids[ci] == getId()) {
479 m_env->attachement_parent_ids[ci] = 0;
484 m_env->attachement_parent_ids[getId()] = 0;
486 LocalPlayer* player = m_env->getLocalPlayer();
487 if (this == player->parent) {
488 player->parent = nullptr;
489 player->isAttached = false;
494 m_meshnode->remove();
496 m_meshnode = nullptr;
497 } else if (m_animated_meshnode) {
498 m_animated_meshnode->remove();
499 m_animated_meshnode->drop();
500 m_animated_meshnode = nullptr;
501 } else if (m_wield_meshnode) {
502 m_wield_meshnode->remove();
503 m_wield_meshnode->drop();
504 m_wield_meshnode = nullptr;
505 } else if (m_spritenode) {
506 m_spritenode->remove();
507 m_spritenode->drop();
508 m_spritenode = nullptr;
512 m_matrixnode->remove();
513 m_matrixnode->drop();
514 m_matrixnode = nullptr;
518 m_client->getCamera()->removeNametag(m_nametag);
523 void GenericCAO::addToScene(ITextureSource *tsrc)
525 m_smgr = RenderingEngine::get_scene_manager();
527 if (getSceneNode() != NULL) {
531 m_visuals_expired = false;
533 if (!m_prop.is_visible) {
537 video::E_MATERIAL_TYPE material_type = (m_prop.use_texture_alpha) ?
538 video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
540 if (m_prop.visual == "sprite") {
541 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
542 m_matrixnode = RenderingEngine::get_scene_manager()->
543 addDummyTransformationSceneNode();
544 m_matrixnode->grab();
545 m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
546 m_matrixnode, v2f(1, 1), v3f(0,0,0), -1);
547 m_spritenode->grab();
548 m_spritenode->setMaterialTexture(0,
549 tsrc->getTextureForMesh("unknown_node.png"));
550 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
551 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
552 m_spritenode->setMaterialType(material_type);
553 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
554 u8 li = m_last_light;
555 m_spritenode->setColor(video::SColor(255,li,li,li));
556 m_spritenode->setSize(v2f(m_prop.visual_size.X,
557 m_prop.visual_size.Y) * BS);
559 const float txs = 1.0 / 1;
560 const float tys = 1.0 / 1;
561 setBillboardTextureMatrix(m_spritenode,
564 } else if (m_prop.visual == "upright_sprite") {
565 scene::SMesh *mesh = new scene::SMesh();
566 double dx = BS * m_prop.visual_size.X / 2;
567 double dy = BS * m_prop.visual_size.Y / 2;
568 u8 li = m_last_light;
569 video::SColor c(255, li, li, li);
572 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
573 video::S3DVertex vertices[4] = {
574 video::S3DVertex(-dx, -dy, 0, 0,0,0, c, 1,1),
575 video::S3DVertex( dx, -dy, 0, 0,0,0, c, 0,1),
576 video::S3DVertex( dx, dy, 0, 0,0,0, c, 0,0),
577 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 1,0),
580 // Move minimal Y position to 0 (feet position)
581 for (video::S3DVertex &vertex : vertices)
584 u16 indices[] = {0,1,2,2,3,0};
585 buf->append(vertices, 4, indices, 6);
587 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
588 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
589 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
590 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
592 mesh->addMeshBuffer(buf);
596 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
597 video::S3DVertex vertices[4] = {
598 video::S3DVertex( dx,-dy, 0, 0,0,0, c, 1,1),
599 video::S3DVertex(-dx,-dy, 0, 0,0,0, c, 0,1),
600 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 0,0),
601 video::S3DVertex( dx, dy, 0, 0,0,0, c, 1,0),
604 // Move minimal Y position to 0 (feet position)
605 for (video::S3DVertex &vertex : vertices)
608 u16 indices[] = {0,1,2,2,3,0};
609 buf->append(vertices, 4, indices, 6);
611 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
612 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
613 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
614 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
616 mesh->addMeshBuffer(buf);
619 m_matrixnode = RenderingEngine::get_scene_manager()->
620 addDummyTransformationSceneNode();
621 m_matrixnode->grab();
622 m_meshnode = RenderingEngine::get_scene_manager()->
623 addMeshSceneNode(mesh, m_matrixnode);
626 // Set it to use the materials of the meshbuffers directly.
627 // This is needed for changing the texture in the future
628 m_meshnode->setReadOnlyMaterials(true);
629 } else if (m_prop.visual == "cube") {
630 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
631 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
632 m_matrixnode = RenderingEngine::get_scene_manager()->
633 addDummyTransformationSceneNode(nullptr);
634 m_matrixnode->grab();
635 m_meshnode = RenderingEngine::get_scene_manager()->
636 addMeshSceneNode(mesh, m_matrixnode);
640 m_meshnode->setScale(m_prop.visual_size);
641 u8 li = m_last_light;
642 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
644 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
645 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
646 m_meshnode->setMaterialType(material_type);
647 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
648 } else if (m_prop.visual == "mesh") {
649 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
650 scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
652 m_matrixnode = RenderingEngine::get_scene_manager()->
653 addDummyTransformationSceneNode(nullptr);
654 m_matrixnode->grab();
655 m_animated_meshnode = RenderingEngine::get_scene_manager()->
656 addAnimatedMeshSceneNode(mesh, m_matrixnode);
657 m_animated_meshnode->grab();
658 mesh->drop(); // The scene node took hold of it
659 m_animated_meshnode->animateJoints(); // Needed for some animations
660 m_animated_meshnode->setScale(m_prop.visual_size);
661 u8 li = m_last_light;
663 // set vertex colors to ensure alpha is set
664 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
666 setAnimatedMeshColor(m_animated_meshnode, video::SColor(255,li,li,li));
668 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, true);
669 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
670 m_animated_meshnode->setMaterialType(material_type);
671 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
672 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
673 m_prop.backface_culling);
675 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
676 } else if (m_prop.visual == "wielditem" || m_prop.visual == "item") {
678 infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
679 if (m_prop.wield_item.empty()) {
680 // Old format, only textures are specified.
681 infostream << "textures: " << m_prop.textures.size() << std::endl;
682 if (!m_prop.textures.empty()) {
683 infostream << "textures[0]: " << m_prop.textures[0]
685 IItemDefManager *idef = m_client->idef();
686 item = ItemStack(m_prop.textures[0], 1, 0, idef);
689 infostream << "serialized form: " << m_prop.wield_item << std::endl;
690 item.deSerialize(m_prop.wield_item, m_client->idef());
692 m_matrixnode = RenderingEngine::get_scene_manager()->
693 addDummyTransformationSceneNode(nullptr);
694 m_matrixnode->grab();
695 m_wield_meshnode = new WieldMeshSceneNode(
696 RenderingEngine::get_scene_manager(), -1);
697 m_wield_meshnode->setParent(m_matrixnode);
698 m_wield_meshnode->setItem(item, m_client,
699 (m_prop.visual == "wielditem"));
701 m_wield_meshnode->setScale(m_prop.visual_size / 2.0f);
702 u8 li = m_last_light;
703 m_wield_meshnode->setColor(video::SColor(255, li, li, li));
705 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
706 <<"\" not supported"<<std::endl;
709 /* don't update while punch texture modifier is active */
710 if (m_reset_textures_timer < 0)
711 updateTextures(m_current_texture_modifier);
713 scene::ISceneNode *node = getSceneNode();
714 if (node && !m_prop.nametag.empty() && !m_is_local_player) {
717 pos.Y = m_prop.selectionbox.MaxEdge.Y + 0.3f;
718 m_nametag = m_client->getCamera()->addNametag(node,
719 m_prop.nametag, m_prop.nametag_color,
725 updateBonePosition();
729 void GenericCAO::updateLight(u8 light_at_pos)
731 // Don't update light of attached one
732 if (getParent() != NULL) {
736 updateLightNoCheck(light_at_pos);
738 // Update light of all children
739 for (u16 i : m_children) {
740 ClientActiveObject *obj = m_env->getActiveObject(i);
742 obj->updateLightNoCheck(light_at_pos);
747 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
752 u8 li = decode_light(light_at_pos + m_glow);
753 if (li != m_last_light) {
755 video::SColor color(255,li,li,li);
757 setMeshColor(m_meshnode->getMesh(), color);
758 } else if (m_animated_meshnode) {
759 setAnimatedMeshColor(m_animated_meshnode, color);
760 } else if (m_wield_meshnode) {
761 m_wield_meshnode->setColor(color);
762 } else if (m_spritenode) {
763 m_spritenode->setColor(color);
768 v3s16 GenericCAO::getLightPosition()
771 return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
773 return floatToInt(m_position, BS);
776 void GenericCAO::updateNodePos()
778 if (getParent() != NULL)
781 scene::ISceneNode *node = getSceneNode();
784 v3s16 camera_offset = m_env->getCameraOffset();
785 v3f pos = pos_translator.val_current -
786 intToFloat(camera_offset, BS);
787 getPosRotMatrix().setTranslation(pos);
788 if (node != m_spritenode) { // rotate if not a sprite
789 v3f rot = m_is_local_player ? -m_rotation : -rot_translator.val_current;
790 setPitchYawRoll(getPosRotMatrix(), rot);
795 void GenericCAO::step(float dtime, ClientEnvironment *env)
797 // Handel model of local player instantly to prevent lags
798 if (m_is_local_player) {
799 LocalPlayer *player = m_env->getLocalPlayer();
801 int old_anim = player->last_animation;
802 float old_anim_speed = player->last_animation_speed;
803 m_position = player->getPosition();
804 m_rotation.Y = wrapDegrees_0_360(player->getYaw());
805 m_velocity = v3f(0,0,0);
806 m_acceleration = v3f(0,0,0);
807 pos_translator.val_current = m_position;
808 rot_translator.val_current = m_rotation;
809 const PlayerControl &controls = player->getPlayerControl();
811 bool walking = false;
812 if (controls.up || controls.down || controls.left || controls.right ||
813 controls.forw_move_joystick_axis != 0.f ||
814 controls.sidew_move_joystick_axis != 0.f)
817 f32 new_speed = player->local_animation_speed;
818 v2s32 new_anim = v2s32(0,0);
819 bool allow_update = false;
821 // increase speed if using fast or flying fast
822 if((g_settings->getBool("fast_move") &&
823 m_client->checkLocalPrivilege("fast")) &&
825 (!player->touching_ground &&
826 g_settings->getBool("free_move") &&
827 m_client->checkLocalPrivilege("fly"))))
829 // slowdown speed if sneeking
830 if (controls.sneak && walking)
833 if (walking && (controls.LMB || controls.RMB)) {
834 new_anim = player->local_animations[3];
835 player->last_animation = WD_ANIM;
837 new_anim = player->local_animations[1];
838 player->last_animation = WALK_ANIM;
839 } else if(controls.LMB || controls.RMB) {
840 new_anim = player->local_animations[2];
841 player->last_animation = DIG_ANIM;
844 // Apply animations if input detected and not attached
845 // or set idle animation
846 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
848 m_animation_range = new_anim;
849 m_animation_speed = new_speed;
850 player->last_animation_speed = m_animation_speed;
852 player->last_animation = NO_ANIM;
854 if (old_anim != NO_ANIM) {
855 m_animation_range = player->local_animations[0];
860 // Update local player animations
861 if ((player->last_animation != old_anim ||
862 m_animation_speed != old_anim_speed) &&
863 player->last_animation != NO_ANIM && allow_update)
869 if (m_visuals_expired && m_smgr) {
870 m_visuals_expired = false;
872 // Attachments, part 1: All attached objects must be unparented first,
873 // or Irrlicht causes a segmentation fault
874 for (auto ci = m_children.begin(); ci != m_children.end();) {
875 if (m_env->attachement_parent_ids[*ci] != getId()) {
876 ci = m_children.erase(ci);
879 ClientActiveObject *obj = m_env->getActiveObject(*ci);
881 scene::ISceneNode *child_node = obj->getSceneNode();
882 // The node's parent is always an IDummyTraformationSceneNode,
883 // so we need to reparent that one instead.
885 child_node->getParent()->setParent(m_smgr->getRootSceneNode());
890 removeFromScene(false);
891 addToScene(m_client->tsrc());
893 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
894 for (u16 cao_id : m_children) {
895 // Get the object of the child
896 ClientActiveObject *obj = m_env->getActiveObject(cao_id);
898 obj->setAttachments();
902 // Make sure m_is_visible is always applied
903 scene::ISceneNode *node = getSceneNode();
905 node->setVisible(m_is_visible);
907 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
909 // Set these for later
910 m_position = getPosition();
911 m_velocity = v3f(0,0,0);
912 m_acceleration = v3f(0,0,0);
913 pos_translator.val_current = m_position;
915 if(m_is_local_player) // Update local player attachment position
917 LocalPlayer *player = m_env->getLocalPlayer();
918 player->overridePosition = getParent()->getPosition();
919 m_env->getLocalPlayer()->parent = getParent();
922 rot_translator.translate(dtime);
923 v3f lastpos = pos_translator.val_current;
927 aabb3f box = m_prop.collisionbox;
930 collisionMoveResult moveresult;
931 f32 pos_max_d = BS*0.125; // Distance per iteration
932 v3f p_pos = m_position;
933 v3f p_velocity = m_velocity;
934 moveresult = collisionMoveSimple(env,env->getGameDef(),
935 pos_max_d, box, m_prop.stepheight, dtime,
936 &p_pos, &p_velocity, m_acceleration,
937 this, m_prop.collideWithObjects);
940 m_velocity = p_velocity;
942 bool is_end_position = moveresult.collides;
943 pos_translator.update(m_position, is_end_position, dtime);
944 pos_translator.translate(dtime);
947 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
948 m_velocity += dtime * m_acceleration;
949 pos_translator.update(m_position, pos_translator.aim_is_end,
950 pos_translator.anim_time);
951 pos_translator.translate(dtime);
955 float moved = lastpos.getDistanceFrom(pos_translator.val_current);
956 m_step_distance_counter += moved;
957 if (m_step_distance_counter > 1.5f * BS) {
958 m_step_distance_counter = 0.0f;
959 if (!m_is_local_player && m_prop.makes_footstep_sound) {
960 const NodeDefManager *ndef = m_client->ndef();
961 v3s16 p = floatToInt(getPosition() +
962 v3f(0.0f, (m_prop.collisionbox.MinEdge.Y - 0.5f) * BS, 0.0f), BS);
963 MapNode n = m_env->getMap().getNodeNoEx(p);
964 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
965 // Reduce footstep gain, as non-local-player footsteps are
968 m_client->sound()->playSoundAt(spec, false, getPosition());
973 m_anim_timer += dtime;
974 if(m_anim_timer >= m_anim_framelength)
976 m_anim_timer -= m_anim_framelength;
978 if(m_anim_frame >= m_anim_num_frames)
984 if(m_reset_textures_timer >= 0)
986 m_reset_textures_timer -= dtime;
987 if(m_reset_textures_timer <= 0) {
988 m_reset_textures_timer = -1;
989 updateTextures(m_previous_texture_modifier);
992 if (!getParent() && std::fabs(m_prop.automatic_rotate) > 0.001) {
993 m_rotation.Y += dtime * m_prop.automatic_rotate * 180 / M_PI;
994 rot_translator.val_current = m_rotation;
998 if (!getParent() && m_prop.automatic_face_movement_dir &&
999 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)) {
1001 float target_yaw = atan2(m_velocity.Z, m_velocity.X) * 180 / M_PI
1002 + m_prop.automatic_face_movement_dir_offset;
1003 float max_rotation_delta =
1004 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1006 wrappedApproachShortest(m_rotation.Y, target_yaw, max_rotation_delta, 360.f);
1007 rot_translator.val_current = m_rotation;
1013 void GenericCAO::updateTexturePos()
1017 scene::ICameraSceneNode* camera =
1018 m_spritenode->getSceneManager()->getActiveCamera();
1021 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1022 - camera->getAbsolutePosition();
1023 cam_to_entity.normalize();
1025 int row = m_tx_basepos.Y;
1026 int col = m_tx_basepos.X;
1028 if (m_tx_select_horiz_by_yawpitch) {
1029 if (cam_to_entity.Y > 0.75)
1031 else if (cam_to_entity.Y < -0.75)
1035 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1036 float dir = mob_dir - m_rotation.Y;
1037 dir = wrapDegrees_180(dir);
1038 if (std::fabs(wrapDegrees_180(dir - 0)) <= 45.1f)
1040 else if(std::fabs(wrapDegrees_180(dir - 90)) <= 45.1f)
1042 else if(std::fabs(wrapDegrees_180(dir - 180)) <= 45.1f)
1044 else if(std::fabs(wrapDegrees_180(dir + 90)) <= 45.1f)
1051 // Animation goes downwards
1052 row += m_anim_frame;
1054 float txs = m_tx_size.X;
1055 float tys = m_tx_size.Y;
1056 setBillboardTextureMatrix(m_spritenode, txs, tys, col, row);
1060 void GenericCAO::updateTextures(std::string mod)
1062 ITextureSource *tsrc = m_client->tsrc();
1064 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1065 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1066 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1068 m_previous_texture_modifier = m_current_texture_modifier;
1069 m_current_texture_modifier = mod;
1070 m_glow = m_prop.glow;
1072 video::E_MATERIAL_TYPE material_type = (m_prop.use_texture_alpha) ?
1073 video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1076 if (m_prop.visual == "sprite") {
1077 std::string texturestring = "unknown_node.png";
1078 if (!m_prop.textures.empty())
1079 texturestring = m_prop.textures[0];
1080 texturestring += mod;
1081 m_spritenode->getMaterial(0).MaterialType = material_type;
1082 m_spritenode->getMaterial(0).MaterialTypeParam = 0.5f;
1083 m_spritenode->setMaterialTexture(0,
1084 tsrc->getTextureForMesh(texturestring));
1086 // This allows setting per-material colors. However, until a real lighting
1087 // system is added, the code below will have no effect. Once MineTest
1088 // has directional lighting, it should work automatically.
1089 if (!m_prop.colors.empty()) {
1090 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1091 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1092 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1095 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1096 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1097 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1101 if (m_animated_meshnode) {
1102 if (m_prop.visual == "mesh") {
1103 for (u32 i = 0; i < m_prop.textures.size() &&
1104 i < m_animated_meshnode->getMaterialCount(); ++i) {
1105 std::string texturestring = m_prop.textures[i];
1106 if (texturestring.empty())
1107 continue; // Empty texture string means don't modify that material
1108 texturestring += mod;
1109 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1111 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1115 // Set material flags and texture
1116 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1117 material.MaterialType = material_type;
1118 material.MaterialTypeParam = 0.5f;
1119 material.TextureLayer[0].Texture = texture;
1120 material.setFlag(video::EMF_LIGHTING, true);
1121 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1122 material.setFlag(video::EMF_BACK_FACE_CULLING, m_prop.backface_culling);
1124 // don't filter low-res textures, makes them look blurry
1125 // player models have a res of 64
1126 const core::dimension2d<u32> &size = texture->getOriginalSize();
1127 const u32 res = std::min(size.Height, size.Width);
1128 use_trilinear_filter &= res > 64;
1129 use_bilinear_filter &= res > 64;
1131 m_animated_meshnode->getMaterial(i)
1132 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1133 m_animated_meshnode->getMaterial(i)
1134 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1135 m_animated_meshnode->getMaterial(i)
1136 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1138 for (u32 i = 0; i < m_prop.colors.size() &&
1139 i < m_animated_meshnode->getMaterialCount(); ++i)
1141 // This allows setting per-material colors. However, until a real lighting
1142 // system is added, the code below will have no effect. Once MineTest
1143 // has directional lighting, it should work automatically.
1144 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1145 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1146 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1152 if(m_prop.visual == "cube")
1154 for (u32 i = 0; i < 6; ++i)
1156 std::string texturestring = "unknown_node.png";
1157 if(m_prop.textures.size() > i)
1158 texturestring = m_prop.textures[i];
1159 texturestring += mod;
1162 // Set material flags and texture
1163 video::SMaterial& material = m_meshnode->getMaterial(i);
1164 material.MaterialType = material_type;
1165 material.MaterialTypeParam = 0.5f;
1166 material.setFlag(video::EMF_LIGHTING, false);
1167 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1168 material.setTexture(0,
1169 tsrc->getTextureForMesh(texturestring));
1170 material.getTextureMatrix(0).makeIdentity();
1172 // This allows setting per-material colors. However, until a real lighting
1173 // system is added, the code below will have no effect. Once MineTest
1174 // has directional lighting, it should work automatically.
1175 if(m_prop.colors.size() > i)
1177 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1178 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1179 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1182 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1183 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1184 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1186 } else if (m_prop.visual == "upright_sprite") {
1187 scene::IMesh *mesh = m_meshnode->getMesh();
1189 std::string tname = "unknown_object.png";
1190 if (!m_prop.textures.empty())
1191 tname = m_prop.textures[0];
1193 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1194 buf->getMaterial().setTexture(0,
1195 tsrc->getTextureForMesh(tname));
1197 // This allows setting per-material colors. However, until a real lighting
1198 // system is added, the code below will have no effect. Once MineTest
1199 // has directional lighting, it should work automatically.
1200 if(!m_prop.colors.empty()) {
1201 buf->getMaterial().AmbientColor = m_prop.colors[0];
1202 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1203 buf->getMaterial().SpecularColor = m_prop.colors[0];
1206 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1207 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1208 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1211 std::string tname = "unknown_object.png";
1212 if (m_prop.textures.size() >= 2)
1213 tname = m_prop.textures[1];
1214 else if (!m_prop.textures.empty())
1215 tname = m_prop.textures[0];
1217 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1218 buf->getMaterial().setTexture(0,
1219 tsrc->getTextureForMesh(tname));
1221 // This allows setting per-material colors. However, until a real lighting
1222 // system is added, the code below will have no effect. Once MineTest
1223 // has directional lighting, it should work automatically.
1224 if (m_prop.colors.size() >= 2) {
1225 buf->getMaterial().AmbientColor = m_prop.colors[1];
1226 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1227 buf->getMaterial().SpecularColor = m_prop.colors[1];
1228 setMeshColor(mesh, m_prop.colors[1]);
1229 } else if (!m_prop.colors.empty()) {
1230 buf->getMaterial().AmbientColor = m_prop.colors[0];
1231 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1232 buf->getMaterial().SpecularColor = m_prop.colors[0];
1233 setMeshColor(mesh, m_prop.colors[0]);
1236 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1237 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1238 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1244 void GenericCAO::updateAnimation()
1246 if (!m_animated_meshnode)
1249 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1250 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1251 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1252 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1253 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1254 m_animated_meshnode->setTransitionTime(m_animation_blend);
1255 // Requires Irrlicht 1.8 or greater
1256 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1257 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1258 m_animated_meshnode->setLoopMode(m_animation_loop);
1262 void GenericCAO::updateAnimationSpeed()
1264 if (!m_animated_meshnode)
1267 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1270 void GenericCAO::updateBonePosition()
1272 if (m_bone_position.empty() || !m_animated_meshnode)
1275 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1276 for(std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
1277 ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1278 std::string bone_name = (*ii).first;
1279 v3f bone_pos = (*ii).second.X;
1280 v3f bone_rot = (*ii).second.Y;
1281 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1284 bone->setPosition(bone_pos);
1285 bone->setRotation(bone_rot);
1290 void GenericCAO::updateAttachments()
1292 ClientActiveObject *parent = getParent();
1293 if (!parent) { // Detach or don't attach
1295 v3f old_pos = m_matrixnode->getAbsolutePosition();
1296 m_matrixnode->setParent(m_smgr->getRootSceneNode());
1297 getPosRotMatrix().setTranslation(old_pos);
1298 m_matrixnode->updateAbsolutePosition();
1300 if (m_is_local_player) {
1301 LocalPlayer *player = m_env->getLocalPlayer();
1302 player->isAttached = false;
1307 scene::ISceneNode *parent_node = parent->getSceneNode();
1308 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1309 parent->getAnimatedMeshSceneNode();
1310 if (parent_animated_mesh_node && !m_attachment_bone.empty()) {
1311 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1314 if (m_matrixnode && parent_node) {
1315 m_matrixnode->setParent(parent_node);
1316 getPosRotMatrix().setTranslation(m_attachment_position);
1317 //setPitchYawRoll(getPosRotMatrix(), m_attachment_rotation);
1318 // use Irrlicht eulers instead
1319 getPosRotMatrix().setRotationDegrees(m_attachment_rotation);
1320 m_matrixnode->updateAbsolutePosition();
1322 if (m_is_local_player) {
1323 LocalPlayer *player = m_env->getLocalPlayer();
1324 player->isAttached = true;
1329 void GenericCAO::processMessage(const std::string &data)
1331 //infostream<<"GenericCAO: Got message"<<std::endl;
1332 std::istringstream is(data, std::ios::binary);
1334 u8 cmd = readU8(is);
1335 if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1336 m_prop = gob_read_set_properties(is);
1338 m_selection_box = m_prop.selectionbox;
1339 m_selection_box.MinEdge *= BS;
1340 m_selection_box.MaxEdge *= BS;
1342 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1343 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1345 if(!m_initial_tx_basepos_set){
1346 m_initial_tx_basepos_set = true;
1347 m_tx_basepos = m_prop.initial_sprite_basepos;
1349 if (m_is_local_player) {
1350 LocalPlayer *player = m_env->getLocalPlayer();
1351 player->makes_footstep_sound = m_prop.makes_footstep_sound;
1352 aabb3f collision_box = m_prop.collisionbox;
1353 collision_box.MinEdge *= BS;
1354 collision_box.MaxEdge *= BS;
1355 player->setCollisionbox(collision_box);
1356 player->setEyeHeight(m_prop.eye_height);
1357 player->setZoomFOV(m_prop.zoom_fov);
1360 if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
1361 m_prop.nametag = m_name;
1364 } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1365 // Not sent by the server if this object is an attachment.
1366 // We might however get here if the server notices the object being detached before the client.
1367 m_position = readV3F32(is);
1368 m_velocity = readV3F32(is);
1369 m_acceleration = readV3F32(is);
1371 if (std::fabs(m_prop.automatic_rotate) < 0.001f)
1372 m_rotation = readV3F32(is);
1376 m_rotation = wrapDegrees_0_360_v3f(m_rotation);
1377 bool do_interpolate = readU8(is);
1378 bool is_end_position = readU8(is);
1379 float update_interval = readF32(is);
1381 // Place us a bit higher if we're physical, to not sink into
1382 // the ground due to sucky collision detection...
1384 m_position += v3f(0,0.002,0);
1386 if(getParent() != NULL) // Just in case
1391 if(!m_prop.physical)
1392 pos_translator.update(m_position, is_end_position, update_interval);
1394 pos_translator.init(m_position);
1396 rot_translator.update(m_rotation, false, update_interval);
1398 } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1399 std::string mod = deSerializeString(is);
1401 // immediatly reset a engine issued texture modifier if a mod sends a different one
1402 if (m_reset_textures_timer > 0) {
1403 m_reset_textures_timer = -1;
1404 updateTextures(m_previous_texture_modifier);
1406 updateTextures(mod);
1407 } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1408 v2s16 p = readV2S16(is);
1409 int num_frames = readU16(is);
1410 float framelength = readF32(is);
1411 bool select_horiz_by_yawpitch = readU8(is);
1414 m_anim_num_frames = num_frames;
1415 m_anim_framelength = framelength;
1416 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1419 } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1420 float override_speed = readF32(is);
1421 float override_jump = readF32(is);
1422 float override_gravity = readF32(is);
1423 // these are sent inverted so we get true when the server sends nothing
1424 bool sneak = !readU8(is);
1425 bool sneak_glitch = !readU8(is);
1426 bool new_move = !readU8(is);
1429 if(m_is_local_player)
1431 LocalPlayer *player = m_env->getLocalPlayer();
1432 player->physics_override_speed = override_speed;
1433 player->physics_override_jump = override_jump;
1434 player->physics_override_gravity = override_gravity;
1435 player->physics_override_sneak = sneak;
1436 player->physics_override_sneak_glitch = sneak_glitch;
1437 player->physics_override_new_move = new_move;
1439 } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1440 // TODO: change frames send as v2s32 value
1441 v2f range = readV2F32(is);
1442 if (!m_is_local_player) {
1443 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1444 m_animation_speed = readF32(is);
1445 m_animation_blend = readF32(is);
1446 // these are sent inverted so we get true when the server sends nothing
1447 m_animation_loop = !readU8(is);
1450 LocalPlayer *player = m_env->getLocalPlayer();
1451 if(player->last_animation == NO_ANIM)
1453 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1454 m_animation_speed = readF32(is);
1455 m_animation_blend = readF32(is);
1456 // these are sent inverted so we get true when the server sends nothing
1457 m_animation_loop = !readU8(is);
1459 // update animation only if local animations present
1460 // and received animation is unknown (except idle animation)
1461 bool is_known = false;
1462 for (int i = 1;i<4;i++)
1464 if(m_animation_range.Y == player->local_animations[i].Y)
1468 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1473 } else if (cmd == GENERIC_CMD_SET_ANIMATION_SPEED) {
1474 m_animation_speed = readF32(is);
1475 updateAnimationSpeed();
1476 } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1477 std::string bone = deSerializeString(is);
1478 v3f position = readV3F32(is);
1479 v3f rotation = readV3F32(is);
1480 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1482 updateBonePosition();
1483 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1484 u16 parent_id = readS16(is);
1485 u16 &old_parent_id = m_env->attachement_parent_ids[getId()];
1486 if (parent_id != old_parent_id) {
1487 if (GenericCAO *old_parent = m_env->getGenericCAO(old_parent_id)) {
1488 old_parent->m_children.erase(std::remove(
1489 m_children.begin(), m_children.end(),
1490 getId()), m_children.end());
1492 if (GenericCAO *new_parent = m_env->getGenericCAO(parent_id))
1493 new_parent->m_children.push_back(getId());
1495 old_parent_id = parent_id;
1498 m_attachment_bone = deSerializeString(is);
1499 m_attachment_position = readV3F32(is);
1500 m_attachment_rotation = readV3F32(is);
1502 // localplayer itself can't be attached to localplayer
1503 if (!m_is_local_player) {
1504 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1505 // Objects attached to the local player should be hidden by default
1506 m_is_visible = !m_attached_to_local;
1509 updateAttachments();
1510 } else if (cmd == GENERIC_CMD_PUNCHED) {
1511 u16 result_hp = readU16(is);
1513 // Use this instead of the send damage to not interfere with prediction
1514 s32 damage = (s32)m_hp - (s32)result_hp;
1522 // TODO: Execute defined fast response
1523 // As there is no definition, make a smoke puff
1524 ClientSimpleObject *simple = createSmokePuff(
1525 m_smgr, m_env, m_position,
1526 v2f(m_prop.visual_size.X, m_prop.visual_size.Y) * BS);
1527 m_env->addSimpleObject(simple);
1528 } else if (m_reset_textures_timer < 0) {
1529 // TODO: Execute defined fast response
1530 // Flashing shall suffice as there is no definition
1531 m_reset_textures_timer = 0.05;
1533 m_reset_textures_timer += 0.05 * damage;
1534 updateTextures(m_current_texture_modifier + "^[brighten");
1537 } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1538 m_armor_groups.clear();
1539 int armor_groups_size = readU16(is);
1540 for(int i=0; i<armor_groups_size; i++)
1542 std::string name = deSerializeString(is);
1543 int rating = readS16(is);
1544 m_armor_groups[name] = rating;
1546 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1547 // Deprecated, for backwards compatibility only.
1548 readU8(is); // version
1549 m_prop.nametag_color = readARGB8(is);
1550 if (m_nametag != NULL) {
1551 m_nametag->nametag_color = m_prop.nametag_color;
1553 pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
1554 m_nametag->nametag_pos = pos;
1556 } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1557 u16 child_id = readU16(is);
1558 u8 type = readU8(is);
1560 if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
1561 childobj->processInitData(deSerializeLongString(is));
1563 m_env->addActiveObject(child_id, type, deSerializeLongString(is));
1566 warningstream << FUNCTION_NAME
1567 << ": unknown command or outdated client \""
1568 << +cmd << "\"" << std::endl;
1572 /* \pre punchitem != NULL
1574 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1575 float time_from_last_punch)
1577 assert(punchitem); // pre-condition
1578 const ToolCapabilities *toolcap =
1579 &punchitem->getToolCapabilities(m_client->idef());
1580 PunchDamageResult result = getPunchDamage(
1584 time_from_last_punch);
1586 if(result.did_punch && result.damage != 0)
1588 if(result.damage < m_hp)
1590 m_hp -= result.damage;
1593 // TODO: Execute defined fast response
1594 // As there is no definition, make a smoke puff
1595 ClientSimpleObject *simple = createSmokePuff(
1596 m_smgr, m_env, m_position,
1597 v2f(m_prop.visual_size.X, m_prop.visual_size.Y) * BS);
1598 m_env->addSimpleObject(simple);
1600 // TODO: Execute defined fast response
1601 // Flashing shall suffice as there is no definition
1602 if (m_reset_textures_timer < 0) {
1603 m_reset_textures_timer = 0.05;
1604 if (result.damage >= 2)
1605 m_reset_textures_timer += 0.05 * result.damage;
1606 updateTextures(m_current_texture_modifier + "^[brighten");
1613 std::string GenericCAO::debugInfoText()
1615 std::ostringstream os(std::ios::binary);
1616 os<<"GenericCAO hp="<<m_hp<<"\n";
1618 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1619 i != m_armor_groups.end(); ++i)
1621 os<<i->first<<"="<<i->second<<", ";
1628 GenericCAO proto_GenericCAO(NULL, NULL);