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
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 int num_messages = 0;
362 u8 version = readU8(is);
364 if (version == 1) { // In PROTOCOL_VERSION 14
365 m_name = deSerializeString(is);
366 m_is_player = readU8(is);
368 m_position = readV3F1000(is);
369 m_rotation = readV3F1000(is);
371 num_messages = readU8(is);
373 errorstream<<"GenericCAO: Unsupported init data version"
378 for (int i = 0; i < num_messages; i++) {
379 std::string message = deSerializeLongString(is);
380 processMessage(message);
383 m_rotation = wrapDegrees_0_360_v3f(m_rotation);
384 pos_translator.init(m_position);
385 rot_translator.init(m_rotation);
389 GenericCAO::~GenericCAO()
391 removeFromScene(true);
394 bool GenericCAO::getSelectionBox(aabb3f *toset) const
396 if (!m_prop.is_visible || !m_is_visible || m_is_local_player
397 || !m_prop.pointable) {
400 *toset = m_selection_box;
404 v3f GenericCAO::getPosition()
406 if (getParent() != NULL) {
407 scene::ISceneNode *node = getSceneNode();
409 return node->getAbsolutePosition();
413 return pos_translator.val_current;
416 const bool GenericCAO::isImmortal()
418 return itemgroup_get(getGroups(), "immortal");
421 scene::ISceneNode* GenericCAO::getSceneNode()
427 if (m_animated_meshnode) {
428 return m_animated_meshnode;
431 if (m_wield_meshnode) {
432 return m_wield_meshnode;
441 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
443 return m_animated_meshnode;
446 void GenericCAO::setChildrenVisible(bool toset)
448 for (u16 cao_id : m_children) {
449 GenericCAO *obj = m_env->getGenericCAO(cao_id);
451 obj->setVisible(toset);
456 void GenericCAO::setAttachments()
461 ClientActiveObject* GenericCAO::getParent() const
463 ClientActiveObject *obj = NULL;
465 u16 attached_id = m_env->attachement_parent_ids[getId()];
467 if ((attached_id != 0) &&
468 (attached_id != getId())) {
469 obj = m_env->getActiveObject(attached_id);
474 void GenericCAO::removeFromScene(bool permanent)
476 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
477 if((m_env != NULL) && (permanent))
479 for (u16 ci : m_children) {
480 if (m_env->attachement_parent_ids[ci] == getId()) {
481 m_env->attachement_parent_ids[ci] = 0;
486 m_env->attachement_parent_ids[getId()] = 0;
488 LocalPlayer* player = m_env->getLocalPlayer();
489 if (this == player->parent) {
490 player->parent = NULL;
491 player->isAttached = false;
496 m_meshnode->remove();
499 } else if (m_animated_meshnode) {
500 m_animated_meshnode->remove();
501 m_animated_meshnode->drop();
502 m_animated_meshnode = NULL;
503 } else if (m_wield_meshnode) {
504 m_wield_meshnode->remove();
505 m_wield_meshnode->drop();
506 m_wield_meshnode = NULL;
507 } else if (m_spritenode) {
508 m_spritenode->remove();
509 m_spritenode->drop();
514 m_client->getCamera()->removeNametag(m_nametag);
519 void GenericCAO::addToScene(ITextureSource *tsrc)
521 m_smgr = RenderingEngine::get_scene_manager();
523 if (getSceneNode() != NULL) {
527 m_visuals_expired = false;
529 if (!m_prop.is_visible) {
533 video::E_MATERIAL_TYPE material_type = (m_prop.use_texture_alpha) ?
534 video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
536 if (m_prop.visual == "sprite") {
537 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
538 m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
539 NULL, v2f(1, 1), v3f(0,0,0), -1);
540 m_spritenode->grab();
541 m_spritenode->setMaterialTexture(0,
542 tsrc->getTextureForMesh("unknown_node.png"));
543 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
544 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
545 m_spritenode->setMaterialType(material_type);
546 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
547 u8 li = m_last_light;
548 m_spritenode->setColor(video::SColor(255,li,li,li));
549 m_spritenode->setSize(m_prop.visual_size*BS);
551 const float txs = 1.0 / 1;
552 const float tys = 1.0 / 1;
553 setBillboardTextureMatrix(m_spritenode,
556 } else if (m_prop.visual == "upright_sprite") {
557 scene::SMesh *mesh = new scene::SMesh();
558 double dx = BS * m_prop.visual_size.X / 2;
559 double dy = BS * m_prop.visual_size.Y / 2;
560 u8 li = m_last_light;
561 video::SColor c(255, li, li, li);
564 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
565 video::S3DVertex vertices[4] = {
566 video::S3DVertex(-dx, -dy, 0, 0,0,0, c, 1,1),
567 video::S3DVertex( dx, -dy, 0, 0,0,0, c, 0,1),
568 video::S3DVertex( dx, dy, 0, 0,0,0, c, 0,0),
569 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 1,0),
572 // Move minimal Y position to 0 (feet position)
573 for (video::S3DVertex &vertex : vertices)
576 u16 indices[] = {0,1,2,2,3,0};
577 buf->append(vertices, 4, indices, 6);
579 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
580 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
581 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
582 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
584 mesh->addMeshBuffer(buf);
588 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
589 video::S3DVertex vertices[4] = {
590 video::S3DVertex( dx,-dy, 0, 0,0,0, c, 1,1),
591 video::S3DVertex(-dx,-dy, 0, 0,0,0, c, 0,1),
592 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 0,0),
593 video::S3DVertex( dx, dy, 0, 0,0,0, c, 1,0),
596 // Move minimal Y position to 0 (feet position)
597 for (video::S3DVertex &vertex : vertices)
600 u16 indices[] = {0,1,2,2,3,0};
601 buf->append(vertices, 4, indices, 6);
603 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
604 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
605 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
606 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
608 mesh->addMeshBuffer(buf);
611 m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
614 // Set it to use the materials of the meshbuffers directly.
615 // This is needed for changing the texture in the future
616 m_meshnode->setReadOnlyMaterials(true);
618 else if(m_prop.visual == "cube") {
619 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
620 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
621 m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
625 m_meshnode->setScale(v3f(m_prop.visual_size.X,
626 m_prop.visual_size.Y,
627 m_prop.visual_size.X));
628 u8 li = m_last_light;
629 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
631 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
632 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
633 m_meshnode->setMaterialType(material_type);
634 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
636 else if(m_prop.visual == "mesh") {
637 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
638 scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
641 m_animated_meshnode = RenderingEngine::get_scene_manager()->
642 addAnimatedMeshSceneNode(mesh, NULL);
643 m_animated_meshnode->grab();
644 mesh->drop(); // The scene node took hold of it
645 m_animated_meshnode->animateJoints(); // Needed for some animations
646 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
647 m_prop.visual_size.Y,
648 m_prop.visual_size.X));
649 u8 li = m_last_light;
651 // set vertex colors to ensure alpha is set
652 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
654 setAnimatedMeshColor(m_animated_meshnode, video::SColor(255,li,li,li));
656 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, true);
657 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
658 m_animated_meshnode->setMaterialType(material_type);
659 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
660 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
661 m_prop.backface_culling);
664 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
665 } else if (m_prop.visual == "wielditem" || m_prop.visual == "item") {
667 infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
668 if (m_prop.wield_item.empty()) {
669 // Old format, only textures are specified.
670 infostream << "textures: " << m_prop.textures.size() << std::endl;
671 if (!m_prop.textures.empty()) {
672 infostream << "textures[0]: " << m_prop.textures[0]
674 IItemDefManager *idef = m_client->idef();
675 item = ItemStack(m_prop.textures[0], 1, 0, idef);
678 infostream << "serialized form: " << m_prop.wield_item << std::endl;
679 item.deSerialize(m_prop.wield_item, m_client->idef());
681 m_wield_meshnode = new WieldMeshSceneNode(
682 RenderingEngine::get_scene_manager(), -1);
683 m_wield_meshnode->setItem(item, m_client,
684 (m_prop.visual == "wielditem"));
686 m_wield_meshnode->setScale(
687 v3f(m_prop.visual_size.X / 2, m_prop.visual_size.Y / 2,
688 m_prop.visual_size.X / 2));
689 u8 li = m_last_light;
690 m_wield_meshnode->setColor(video::SColor(255, li, li, li));
692 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
693 <<"\" not supported"<<std::endl;
696 /* don't update while punch texture modifier is active */
697 if (m_reset_textures_timer < 0)
698 updateTextures(m_current_texture_modifier);
700 scene::ISceneNode *node = getSceneNode();
701 if (node && !m_prop.nametag.empty() && !m_is_local_player) {
704 pos.Y = m_prop.selectionbox.MaxEdge.Y + 0.3f;
705 m_nametag = m_client->getCamera()->addNametag(node,
706 m_prop.nametag, m_prop.nametag_color,
712 updateBonePosition();
716 void GenericCAO::updateLight(u8 light_at_pos)
718 // Don't update light of attached one
719 if (getParent() != NULL) {
723 updateLightNoCheck(light_at_pos);
725 // Update light of all children
726 for (u16 i : m_children) {
727 ClientActiveObject *obj = m_env->getActiveObject(i);
729 obj->updateLightNoCheck(light_at_pos);
734 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
739 u8 li = decode_light(light_at_pos + m_glow);
740 if (li != m_last_light) {
742 video::SColor color(255,li,li,li);
744 setMeshColor(m_meshnode->getMesh(), color);
745 } else if (m_animated_meshnode) {
746 setAnimatedMeshColor(m_animated_meshnode, color);
747 } else if (m_wield_meshnode) {
748 m_wield_meshnode->setColor(color);
749 } else if (m_spritenode) {
750 m_spritenode->setColor(color);
755 v3s16 GenericCAO::getLightPosition()
758 return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
760 return floatToInt(m_position, BS);
763 void GenericCAO::updateNodePos()
765 if (getParent() != NULL)
768 scene::ISceneNode *node = getSceneNode();
771 v3s16 camera_offset = m_env->getCameraOffset();
772 node->setPosition(pos_translator.val_current - intToFloat(camera_offset, BS));
773 if (node != m_spritenode) { // rotate if not a sprite
774 v3f rot = m_is_local_player ? -m_rotation : -rot_translator.val_current;
775 node->setRotation(rot);
780 void GenericCAO::step(float dtime, ClientEnvironment *env)
782 // Handel model of local player instantly to prevent lags
783 if (m_is_local_player) {
784 LocalPlayer *player = m_env->getLocalPlayer();
786 int old_anim = player->last_animation;
787 float old_anim_speed = player->last_animation_speed;
788 m_position = player->getPosition();
789 m_rotation.Y = wrapDegrees_0_360(player->getYaw());
790 m_velocity = v3f(0,0,0);
791 m_acceleration = v3f(0,0,0);
792 pos_translator.val_current = m_position;
793 rot_translator.val_current = m_rotation;
794 const PlayerControl &controls = player->getPlayerControl();
796 bool walking = false;
797 if (controls.up || controls.down || controls.left || controls.right ||
798 controls.forw_move_joystick_axis != 0.f ||
799 controls.sidew_move_joystick_axis != 0.f)
802 f32 new_speed = player->local_animation_speed;
803 v2s32 new_anim = v2s32(0,0);
804 bool allow_update = false;
806 // increase speed if using fast or flying fast
807 if((g_settings->getBool("fast_move") &&
808 m_client->checkLocalPrivilege("fast")) &&
810 (!player->touching_ground &&
811 g_settings->getBool("free_move") &&
812 m_client->checkLocalPrivilege("fly"))))
814 // slowdown speed if sneeking
815 if (controls.sneak && walking)
818 if (walking && (controls.LMB || controls.RMB)) {
819 new_anim = player->local_animations[3];
820 player->last_animation = WD_ANIM;
822 new_anim = player->local_animations[1];
823 player->last_animation = WALK_ANIM;
824 } else if(controls.LMB || controls.RMB) {
825 new_anim = player->local_animations[2];
826 player->last_animation = DIG_ANIM;
829 // Apply animations if input detected and not attached
830 // or set idle animation
831 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
833 m_animation_range = new_anim;
834 m_animation_speed = new_speed;
835 player->last_animation_speed = m_animation_speed;
837 player->last_animation = NO_ANIM;
839 if (old_anim != NO_ANIM) {
840 m_animation_range = player->local_animations[0];
845 // Update local player animations
846 if ((player->last_animation != old_anim ||
847 m_animation_speed != old_anim_speed) &&
848 player->last_animation != NO_ANIM && allow_update)
854 if (m_visuals_expired && m_smgr) {
855 m_visuals_expired = false;
857 // Attachments, part 1: All attached objects must be unparented first,
858 // or Irrlicht causes a segmentation fault
859 for (auto ci = m_children.begin(); ci != m_children.end();) {
860 if (m_env->attachement_parent_ids[*ci] != getId()) {
861 ci = m_children.erase(ci);
864 ClientActiveObject *obj = m_env->getActiveObject(*ci);
866 scene::ISceneNode *child_node = obj->getSceneNode();
868 child_node->setParent(m_smgr->getRootSceneNode());
873 removeFromScene(false);
874 addToScene(m_client->tsrc());
876 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
877 for (u16 cao_id : m_children) {
878 // Get the object of the child
879 ClientActiveObject *obj = m_env->getActiveObject(cao_id);
881 obj->setAttachments();
885 // Make sure m_is_visible is always applied
886 scene::ISceneNode *node = getSceneNode();
888 node->setVisible(m_is_visible);
890 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
892 // Set these for later
893 m_position = getPosition();
894 m_velocity = v3f(0,0,0);
895 m_acceleration = v3f(0,0,0);
896 pos_translator.val_current = m_position;
898 if(m_is_local_player) // Update local player attachment position
900 LocalPlayer *player = m_env->getLocalPlayer();
901 player->overridePosition = getParent()->getPosition();
902 m_env->getLocalPlayer()->parent = getParent();
905 rot_translator.translate(dtime);
906 v3f lastpos = pos_translator.val_current;
910 aabb3f box = m_prop.collisionbox;
913 collisionMoveResult moveresult;
914 f32 pos_max_d = BS*0.125; // Distance per iteration
915 v3f p_pos = m_position;
916 v3f p_velocity = m_velocity;
917 moveresult = collisionMoveSimple(env,env->getGameDef(),
918 pos_max_d, box, m_prop.stepheight, dtime,
919 &p_pos, &p_velocity, m_acceleration,
920 this, m_prop.collideWithObjects);
923 m_velocity = p_velocity;
925 bool is_end_position = moveresult.collides;
926 pos_translator.update(m_position, is_end_position, dtime);
927 pos_translator.translate(dtime);
930 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
931 m_velocity += dtime * m_acceleration;
932 pos_translator.update(m_position, pos_translator.aim_is_end,
933 pos_translator.anim_time);
934 pos_translator.translate(dtime);
938 float moved = lastpos.getDistanceFrom(pos_translator.val_current);
939 m_step_distance_counter += moved;
940 if (m_step_distance_counter > 1.5f * BS) {
941 m_step_distance_counter = 0.0f;
942 if (!m_is_local_player && m_prop.makes_footstep_sound) {
943 const NodeDefManager *ndef = m_client->ndef();
944 v3s16 p = floatToInt(getPosition() +
945 v3f(0.0f, (m_prop.collisionbox.MinEdge.Y - 0.5f) * BS, 0.0f), BS);
946 MapNode n = m_env->getMap().getNodeNoEx(p);
947 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
948 // Reduce footstep gain, as non-local-player footsteps are
951 m_client->sound()->playSoundAt(spec, false, getPosition());
956 m_anim_timer += dtime;
957 if(m_anim_timer >= m_anim_framelength)
959 m_anim_timer -= m_anim_framelength;
961 if(m_anim_frame >= m_anim_num_frames)
967 if(m_reset_textures_timer >= 0)
969 m_reset_textures_timer -= dtime;
970 if(m_reset_textures_timer <= 0) {
971 m_reset_textures_timer = -1;
972 updateTextures(m_previous_texture_modifier);
975 if (!getParent() && std::fabs(m_prop.automatic_rotate) > 0.001) {
976 m_rotation.Y += dtime * m_prop.automatic_rotate * 180 / M_PI;
977 rot_translator.val_current = m_rotation;
981 if (!getParent() && m_prop.automatic_face_movement_dir &&
982 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)) {
984 float target_yaw = atan2(m_velocity.Z, m_velocity.X) * 180 / M_PI
985 + m_prop.automatic_face_movement_dir_offset;
986 float max_rotation_delta =
987 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
989 wrappedApproachShortest(m_rotation.Y, target_yaw, max_rotation_delta, 360.f);
990 rot_translator.val_current = m_rotation;
996 void GenericCAO::updateTexturePos()
1000 scene::ICameraSceneNode* camera =
1001 m_spritenode->getSceneManager()->getActiveCamera();
1004 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1005 - camera->getAbsolutePosition();
1006 cam_to_entity.normalize();
1008 int row = m_tx_basepos.Y;
1009 int col = m_tx_basepos.X;
1011 if (m_tx_select_horiz_by_yawpitch) {
1012 if (cam_to_entity.Y > 0.75)
1014 else if (cam_to_entity.Y < -0.75)
1018 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1019 float dir = mob_dir - m_rotation.Y;
1020 dir = wrapDegrees_180(dir);
1021 if (std::fabs(wrapDegrees_180(dir - 0)) <= 45.1f)
1023 else if(std::fabs(wrapDegrees_180(dir - 90)) <= 45.1f)
1025 else if(std::fabs(wrapDegrees_180(dir - 180)) <= 45.1f)
1027 else if(std::fabs(wrapDegrees_180(dir + 90)) <= 45.1f)
1034 // Animation goes downwards
1035 row += m_anim_frame;
1037 float txs = m_tx_size.X;
1038 float tys = m_tx_size.Y;
1039 setBillboardTextureMatrix(m_spritenode, txs, tys, col, row);
1043 void GenericCAO::updateTextures(std::string mod)
1045 ITextureSource *tsrc = m_client->tsrc();
1047 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1048 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1049 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1051 m_previous_texture_modifier = m_current_texture_modifier;
1052 m_current_texture_modifier = mod;
1053 m_glow = m_prop.glow;
1055 video::E_MATERIAL_TYPE material_type = (m_prop.use_texture_alpha) ?
1056 video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1059 if (m_prop.visual == "sprite") {
1060 std::string texturestring = "unknown_node.png";
1061 if (!m_prop.textures.empty())
1062 texturestring = m_prop.textures[0];
1063 texturestring += mod;
1064 m_spritenode->getMaterial(0).MaterialType = material_type;
1065 m_spritenode->setMaterialTexture(0,
1066 tsrc->getTextureForMesh(texturestring));
1068 // This allows setting per-material colors. However, until a real lighting
1069 // system is added, the code below will have no effect. Once MineTest
1070 // has directional lighting, it should work automatically.
1071 if (!m_prop.colors.empty()) {
1072 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1073 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1074 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1077 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1078 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1079 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1083 if (m_animated_meshnode) {
1084 if (m_prop.visual == "mesh") {
1085 for (u32 i = 0; i < m_prop.textures.size() &&
1086 i < m_animated_meshnode->getMaterialCount(); ++i) {
1087 std::string texturestring = m_prop.textures[i];
1088 if (texturestring.empty())
1089 continue; // Empty texture string means don't modify that material
1090 texturestring += mod;
1091 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1093 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1097 // Set material flags and texture
1098 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1099 material.MaterialType = material_type;
1100 material.TextureLayer[0].Texture = texture;
1101 material.setFlag(video::EMF_LIGHTING, true);
1102 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1103 material.setFlag(video::EMF_BACK_FACE_CULLING, m_prop.backface_culling);
1105 // don't filter low-res textures, makes them look blurry
1106 // player models have a res of 64
1107 const core::dimension2d<u32> &size = texture->getOriginalSize();
1108 const u32 res = std::min(size.Height, size.Width);
1109 use_trilinear_filter &= res > 64;
1110 use_bilinear_filter &= res > 64;
1112 m_animated_meshnode->getMaterial(i)
1113 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1114 m_animated_meshnode->getMaterial(i)
1115 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1116 m_animated_meshnode->getMaterial(i)
1117 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1119 for (u32 i = 0; i < m_prop.colors.size() &&
1120 i < m_animated_meshnode->getMaterialCount(); ++i)
1122 // This allows setting per-material colors. However, until a real lighting
1123 // system is added, the code below will have no effect. Once MineTest
1124 // has directional lighting, it should work automatically.
1125 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1126 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1127 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1133 if(m_prop.visual == "cube")
1135 for (u32 i = 0; i < 6; ++i)
1137 std::string texturestring = "unknown_node.png";
1138 if(m_prop.textures.size() > i)
1139 texturestring = m_prop.textures[i];
1140 texturestring += mod;
1143 // Set material flags and texture
1144 video::SMaterial& material = m_meshnode->getMaterial(i);
1145 material.MaterialType = material_type;
1146 material.setFlag(video::EMF_LIGHTING, false);
1147 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1148 material.setTexture(0,
1149 tsrc->getTextureForMesh(texturestring));
1150 material.getTextureMatrix(0).makeIdentity();
1152 // This allows setting per-material colors. However, until a real lighting
1153 // system is added, the code below will have no effect. Once MineTest
1154 // has directional lighting, it should work automatically.
1155 if(m_prop.colors.size() > i)
1157 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1158 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1159 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1162 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1163 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1164 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1166 } else if (m_prop.visual == "upright_sprite") {
1167 scene::IMesh *mesh = m_meshnode->getMesh();
1169 std::string tname = "unknown_object.png";
1170 if (!m_prop.textures.empty())
1171 tname = m_prop.textures[0];
1173 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1174 buf->getMaterial().setTexture(0,
1175 tsrc->getTextureForMesh(tname));
1177 // This allows setting per-material colors. However, until a real lighting
1178 // system is added, the code below will have no effect. Once MineTest
1179 // has directional lighting, it should work automatically.
1180 if(!m_prop.colors.empty()) {
1181 buf->getMaterial().AmbientColor = m_prop.colors[0];
1182 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1183 buf->getMaterial().SpecularColor = m_prop.colors[0];
1186 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1187 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1188 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1191 std::string tname = "unknown_object.png";
1192 if (m_prop.textures.size() >= 2)
1193 tname = m_prop.textures[1];
1194 else if (!m_prop.textures.empty())
1195 tname = m_prop.textures[0];
1197 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1198 buf->getMaterial().setTexture(0,
1199 tsrc->getTextureForMesh(tname));
1201 // This allows setting per-material colors. However, until a real lighting
1202 // system is added, the code below will have no effect. Once MineTest
1203 // has directional lighting, it should work automatically.
1204 if (m_prop.colors.size() >= 2) {
1205 buf->getMaterial().AmbientColor = m_prop.colors[1];
1206 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1207 buf->getMaterial().SpecularColor = m_prop.colors[1];
1208 setMeshColor(mesh, m_prop.colors[1]);
1209 } else if (!m_prop.colors.empty()) {
1210 buf->getMaterial().AmbientColor = m_prop.colors[0];
1211 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1212 buf->getMaterial().SpecularColor = m_prop.colors[0];
1213 setMeshColor(mesh, m_prop.colors[0]);
1216 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1217 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1218 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1224 void GenericCAO::updateAnimation()
1226 if (!m_animated_meshnode)
1229 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1230 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1231 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1232 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1233 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1234 m_animated_meshnode->setTransitionTime(m_animation_blend);
1235 // Requires Irrlicht 1.8 or greater
1236 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1237 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1238 m_animated_meshnode->setLoopMode(m_animation_loop);
1242 void GenericCAO::updateAnimationSpeed()
1244 if (!m_animated_meshnode)
1247 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1250 void GenericCAO::updateBonePosition()
1252 if (m_bone_position.empty() || !m_animated_meshnode)
1255 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1256 for(std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
1257 ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1258 std::string bone_name = (*ii).first;
1259 v3f bone_pos = (*ii).second.X;
1260 v3f bone_rot = (*ii).second.Y;
1261 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1264 bone->setPosition(bone_pos);
1265 bone->setRotation(bone_rot);
1270 void GenericCAO::updateAttachments()
1273 if (!getParent()) { // Detach or don't attach
1274 scene::ISceneNode *node = getSceneNode();
1276 v3f old_position = node->getAbsolutePosition();
1277 v3f old_rotation = node->getRotation();
1278 node->setParent(m_smgr->getRootSceneNode());
1279 node->setPosition(old_position);
1280 node->setRotation(old_rotation);
1281 node->updateAbsolutePosition();
1283 if (m_is_local_player) {
1284 LocalPlayer *player = m_env->getLocalPlayer();
1285 player->isAttached = false;
1290 scene::ISceneNode *my_node = getSceneNode();
1292 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1293 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1294 getParent()->getAnimatedMeshSceneNode();
1295 if (parent_animated_mesh_node && !m_attachment_bone.empty()) {
1296 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1299 if (my_node && parent_node) {
1300 my_node->setParent(parent_node);
1301 my_node->setPosition(m_attachment_position);
1302 my_node->setRotation(m_attachment_rotation);
1303 my_node->updateAbsolutePosition();
1305 if (m_is_local_player) {
1306 LocalPlayer *player = m_env->getLocalPlayer();
1307 player->isAttached = true;
1312 void GenericCAO::processMessage(const std::string &data)
1314 //infostream<<"GenericCAO: Got message"<<std::endl;
1315 std::istringstream is(data, std::ios::binary);
1317 u8 cmd = readU8(is);
1318 if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1319 m_prop = gob_read_set_properties(is);
1321 m_selection_box = m_prop.selectionbox;
1322 m_selection_box.MinEdge *= BS;
1323 m_selection_box.MaxEdge *= BS;
1325 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1326 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1328 if(!m_initial_tx_basepos_set){
1329 m_initial_tx_basepos_set = true;
1330 m_tx_basepos = m_prop.initial_sprite_basepos;
1332 if (m_is_local_player) {
1333 LocalPlayer *player = m_env->getLocalPlayer();
1334 player->makes_footstep_sound = m_prop.makes_footstep_sound;
1335 aabb3f collision_box = m_prop.collisionbox;
1336 collision_box.MinEdge *= BS;
1337 collision_box.MaxEdge *= BS;
1338 player->setCollisionbox(collision_box);
1339 player->setEyeHeight(m_prop.eye_height);
1340 player->setZoomFOV(m_prop.zoom_fov);
1343 if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
1344 m_prop.nametag = m_name;
1347 } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1348 // Not sent by the server if this object is an attachment.
1349 // We might however get here if the server notices the object being detached before the client.
1350 m_position = readV3F1000(is);
1351 m_velocity = readV3F1000(is);
1352 m_acceleration = readV3F1000(is);
1354 if (std::fabs(m_prop.automatic_rotate) < 0.001f)
1355 m_rotation = readV3F1000(is);
1359 m_rotation = wrapDegrees_0_360_v3f(m_rotation);
1360 bool do_interpolate = readU8(is);
1361 bool is_end_position = readU8(is);
1362 float update_interval = readF1000(is);
1364 // Place us a bit higher if we're physical, to not sink into
1365 // the ground due to sucky collision detection...
1367 m_position += v3f(0,0.002,0);
1369 if(getParent() != NULL) // Just in case
1374 if(!m_prop.physical)
1375 pos_translator.update(m_position, is_end_position, update_interval);
1377 pos_translator.init(m_position);
1379 rot_translator.update(m_rotation, false, update_interval);
1381 } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1382 std::string mod = deSerializeString(is);
1384 // immediatly reset a engine issued texture modifier if a mod sends a different one
1385 if (m_reset_textures_timer > 0) {
1386 m_reset_textures_timer = -1;
1387 updateTextures(m_previous_texture_modifier);
1389 updateTextures(mod);
1390 } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1391 v2s16 p = readV2S16(is);
1392 int num_frames = readU16(is);
1393 float framelength = readF1000(is);
1394 bool select_horiz_by_yawpitch = readU8(is);
1397 m_anim_num_frames = num_frames;
1398 m_anim_framelength = framelength;
1399 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1402 } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1403 float override_speed = readF1000(is);
1404 float override_jump = readF1000(is);
1405 float override_gravity = readF1000(is);
1406 // these are sent inverted so we get true when the server sends nothing
1407 bool sneak = !readU8(is);
1408 bool sneak_glitch = !readU8(is);
1409 bool new_move = !readU8(is);
1412 if(m_is_local_player)
1414 LocalPlayer *player = m_env->getLocalPlayer();
1415 player->physics_override_speed = override_speed;
1416 player->physics_override_jump = override_jump;
1417 player->physics_override_gravity = override_gravity;
1418 player->physics_override_sneak = sneak;
1419 player->physics_override_sneak_glitch = sneak_glitch;
1420 player->physics_override_new_move = new_move;
1422 } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1423 // TODO: change frames send as v2s32 value
1424 v2f range = readV2F1000(is);
1425 if (!m_is_local_player) {
1426 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1427 m_animation_speed = readF1000(is);
1428 m_animation_blend = readF1000(is);
1429 // these are sent inverted so we get true when the server sends nothing
1430 m_animation_loop = !readU8(is);
1433 LocalPlayer *player = m_env->getLocalPlayer();
1434 if(player->last_animation == NO_ANIM)
1436 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1437 m_animation_speed = readF1000(is);
1438 m_animation_blend = readF1000(is);
1439 // these are sent inverted so we get true when the server sends nothing
1440 m_animation_loop = !readU8(is);
1442 // update animation only if local animations present
1443 // and received animation is unknown (except idle animation)
1444 bool is_known = false;
1445 for (int i = 1;i<4;i++)
1447 if(m_animation_range.Y == player->local_animations[i].Y)
1451 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1456 } else if (cmd == GENERIC_CMD_SET_ANIMATION_SPEED) {
1457 m_animation_speed = readF1000(is);
1458 updateAnimationSpeed();
1459 } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1460 std::string bone = deSerializeString(is);
1461 v3f position = readV3F1000(is);
1462 v3f rotation = readV3F1000(is);
1463 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1465 updateBonePosition();
1466 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1467 u16 parent_id = readS16(is);
1468 u16 &old_parent_id = m_env->attachement_parent_ids[getId()];
1469 if (parent_id != old_parent_id) {
1470 if (GenericCAO *old_parent = m_env->getGenericCAO(old_parent_id)) {
1471 old_parent->m_children.erase(std::remove(
1472 m_children.begin(), m_children.end(),
1473 getId()), m_children.end());
1475 if (GenericCAO *new_parent = m_env->getGenericCAO(parent_id))
1476 new_parent->m_children.push_back(getId());
1478 old_parent_id = parent_id;
1481 m_attachment_bone = deSerializeString(is);
1482 m_attachment_position = readV3F1000(is);
1483 m_attachment_rotation = readV3F1000(is);
1485 // localplayer itself can't be attached to localplayer
1486 if (!m_is_local_player) {
1487 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1488 // Objects attached to the local player should be hidden by default
1489 m_is_visible = !m_attached_to_local;
1492 updateAttachments();
1493 } else if (cmd == GENERIC_CMD_PUNCHED) {
1494 /*s16 damage =*/ readS16(is);
1495 s16 result_hp = readS16(is);
1497 // Use this instead of the send damage to not interfere with prediction
1498 s16 damage = m_hp - result_hp;
1506 // TODO: Execute defined fast response
1507 // As there is no definition, make a smoke puff
1508 ClientSimpleObject *simple = createSmokePuff(
1509 m_smgr, m_env, m_position,
1510 m_prop.visual_size * BS);
1511 m_env->addSimpleObject(simple);
1512 } else if (m_reset_textures_timer < 0) {
1513 // TODO: Execute defined fast response
1514 // Flashing shall suffice as there is no definition
1515 m_reset_textures_timer = 0.05;
1517 m_reset_textures_timer += 0.05 * damage;
1518 updateTextures(m_current_texture_modifier + "^[brighten");
1521 } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1522 m_armor_groups.clear();
1523 int armor_groups_size = readU16(is);
1524 for(int i=0; i<armor_groups_size; i++)
1526 std::string name = deSerializeString(is);
1527 int rating = readS16(is);
1528 m_armor_groups[name] = rating;
1530 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1531 // Deprecated, for backwards compatibility only.
1532 readU8(is); // version
1533 m_prop.nametag_color = readARGB8(is);
1534 if (m_nametag != NULL) {
1535 m_nametag->nametag_color = m_prop.nametag_color;
1537 pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
1538 m_nametag->nametag_pos = pos;
1540 } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1541 u16 child_id = readU16(is);
1542 u8 type = readU8(is);
1544 if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
1545 childobj->processInitData(deSerializeLongString(is));
1547 m_env->addActiveObject(child_id, type, deSerializeLongString(is));
1550 warningstream << FUNCTION_NAME
1551 << ": unknown command or outdated client \""
1552 << +cmd << "\"" << std::endl;
1556 /* \pre punchitem != NULL
1558 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1559 float time_from_last_punch)
1561 assert(punchitem); // pre-condition
1562 const ToolCapabilities *toolcap =
1563 &punchitem->getToolCapabilities(m_client->idef());
1564 PunchDamageResult result = getPunchDamage(
1568 time_from_last_punch);
1570 if(result.did_punch && result.damage != 0)
1572 if(result.damage < m_hp)
1574 m_hp -= result.damage;
1577 // TODO: Execute defined fast response
1578 // As there is no definition, make a smoke puff
1579 ClientSimpleObject *simple = createSmokePuff(
1580 m_smgr, m_env, m_position,
1581 m_prop.visual_size * BS);
1582 m_env->addSimpleObject(simple);
1584 // TODO: Execute defined fast response
1585 // Flashing shall suffice as there is no definition
1586 if (m_reset_textures_timer < 0) {
1587 m_reset_textures_timer = 0.05;
1588 if (result.damage >= 2)
1589 m_reset_textures_timer += 0.05 * result.damage;
1590 updateTextures(m_current_texture_modifier + "^[brighten");
1597 std::string GenericCAO::debugInfoText()
1599 std::ostringstream os(std::ios::binary);
1600 os<<"GenericCAO hp="<<m_hp<<"\n";
1602 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1603 i != m_armor_groups.end(); ++i)
1605 os<<i->first<<"="<<i->second<<", ";
1612 GenericCAO proto_GenericCAO(NULL, NULL);