3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "content_cao.h"
21 #include <IBillboardSceneNode.h>
22 #include <ICameraSceneNode.h>
23 #include <ITextSceneNode.h>
24 #include <IMeshManipulator.h>
25 #include <IAnimatedMeshSceneNode.h>
26 #include "client/client.h"
27 #include "client/renderingengine.h"
28 #include "client/sound.h"
29 #include "client/tile.h"
30 #include "util/basic_macros.h"
31 #include "util/numeric.h" // For IntervalLimiter & setPitchYawRoll
32 #include "util/serialize.h"
33 #include "camera.h" // CameraModes
34 #include "collision.h"
35 #include "content_cso.h"
36 #include "environment.h"
38 #include "localplayer.h"
42 #include "serialization.h" // For decompressZlib
46 #include "wieldmesh.h"
49 #include "client/shader.h"
52 struct ToolCapabilities;
54 std::unordered_map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
57 void SmoothTranslator<T>::init(T current)
60 val_current = current;
63 anim_time_counter = 0;
68 void SmoothTranslator<T>::update(T new_target, bool is_end_position, float update_interval)
70 aim_is_end = is_end_position;
71 val_old = val_current;
72 val_target = new_target;
73 if (update_interval > 0) {
74 anim_time = update_interval;
76 if (anim_time < 0.001 || anim_time > 1.0)
77 anim_time = anim_time_counter;
79 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
81 anim_time_counter = 0;
85 void SmoothTranslator<T>::translate(f32 dtime)
87 anim_time_counter = anim_time_counter + dtime;
88 T val_diff = val_target - val_old;
90 if (anim_time > 0.001)
91 moveratio = anim_time_counter / anim_time;
92 f32 move_end = aim_is_end ? 1.0 : 1.5;
94 // Move a bit less than should, to avoid oscillation
95 moveratio = std::min(moveratio * 0.8f, move_end);
96 val_current = val_old + val_diff * moveratio;
99 void SmoothTranslatorWrapped::translate(f32 dtime)
101 anim_time_counter = anim_time_counter + dtime;
102 f32 val_diff = std::abs(val_target - val_old);
103 if (val_diff > 180.f)
104 val_diff = 360.f - val_diff;
107 if (anim_time > 0.001)
108 moveratio = anim_time_counter / anim_time;
109 f32 move_end = aim_is_end ? 1.0 : 1.5;
111 // Move a bit less than should, to avoid oscillation
112 moveratio = std::min(moveratio * 0.8f, move_end);
113 wrappedApproachShortest(val_current, val_target,
114 val_diff * moveratio, 360.f);
117 void SmoothTranslatorWrappedv3f::translate(f32 dtime)
119 anim_time_counter = anim_time_counter + dtime;
122 val_diff_v3f.X = std::abs(val_target.X - val_old.X);
123 val_diff_v3f.Y = std::abs(val_target.Y - val_old.Y);
124 val_diff_v3f.Z = std::abs(val_target.Z - val_old.Z);
126 if (val_diff_v3f.X > 180.f)
127 val_diff_v3f.X = 360.f - val_diff_v3f.X;
129 if (val_diff_v3f.Y > 180.f)
130 val_diff_v3f.Y = 360.f - val_diff_v3f.Y;
132 if (val_diff_v3f.Z > 180.f)
133 val_diff_v3f.Z = 360.f - val_diff_v3f.Z;
136 if (anim_time > 0.001)
137 moveratio = anim_time_counter / anim_time;
138 f32 move_end = aim_is_end ? 1.0 : 1.5;
140 // Move a bit less than should, to avoid oscillation
141 moveratio = std::min(moveratio * 0.8f, move_end);
142 wrappedApproachShortest(val_current.X, val_target.X,
143 val_diff_v3f.X * moveratio, 360.f);
145 wrappedApproachShortest(val_current.Y, val_target.Y,
146 val_diff_v3f.Y * moveratio, 360.f);
148 wrappedApproachShortest(val_current.Z, val_target.Z,
149 val_diff_v3f.Z * moveratio, 360.f);
156 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
157 float txs, float tys, int col, int row)
159 video::SMaterial& material = bill->getMaterial(0);
160 core::matrix4& matrix = material.getTextureMatrix(0);
161 matrix.setTextureTranslate(txs*col, tys*row);
162 matrix.setTextureScale(txs, tys);
169 class TestCAO : public ClientActiveObject
172 TestCAO(Client *client, ClientEnvironment *env);
173 virtual ~TestCAO() = default;
175 ActiveObjectType getType() const
177 return ACTIVEOBJECT_TYPE_TEST;
180 static ClientActiveObject* create(Client *client, ClientEnvironment *env);
182 void addToScene(ITextureSource *tsrc);
183 void removeFromScene(bool permanent);
184 void updateLight(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"
308 #include "clientobject.h"
310 GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
311 ClientActiveObject(0, client, env)
313 if (client == NULL) {
314 ClientActiveObject::registerType(getType(), create);
320 bool GenericCAO::getCollisionBox(aabb3f *toset) const
324 //update collision box
325 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
326 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
328 toset->MinEdge += m_position;
329 toset->MaxEdge += m_position;
337 bool GenericCAO::collideWithObjects() const
339 return m_prop.collideWithObjects;
342 void GenericCAO::initialize(const std::string &data)
344 infostream<<"GenericCAO: Got init data"<<std::endl;
345 processInitData(data);
348 // Check if it's the current player
349 LocalPlayer *player = m_env->getLocalPlayer();
350 if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
351 m_is_local_player = true;
352 m_is_visible = false;
353 player->setCAO(this);
357 m_enable_shaders = g_settings->getBool("enable_shaders");
360 void GenericCAO::processInitData(const std::string &data)
362 std::istringstream is(data, std::ios::binary);
363 const u8 version = readU8(is);
366 errorstream << "GenericCAO: Unsupported init data version"
371 // PROTOCOL_VERSION >= 37
372 m_name = deSerializeString(is);
373 m_is_player = readU8(is);
375 m_position = readV3F32(is);
376 m_rotation = readV3F32(is);
379 const u8 num_messages = readU8(is);
381 for (int i = 0; i < num_messages; i++) {
382 std::string message = deSerializeLongString(is);
383 processMessage(message);
386 m_rotation = wrapDegrees_0_360_v3f(m_rotation);
387 pos_translator.init(m_position);
388 rot_translator.init(m_rotation);
392 GenericCAO::~GenericCAO()
394 removeFromScene(true);
397 bool GenericCAO::getSelectionBox(aabb3f *toset) const
399 if (!m_prop.is_visible || !m_is_visible || m_is_local_player
400 || !m_prop.pointable) {
403 *toset = m_selection_box;
407 const v3f GenericCAO::getPosition() const
410 return pos_translator.val_current;
412 // Calculate real position in world based on MatrixNode
414 v3s16 camera_offset = m_env->getCameraOffset();
415 return m_matrixnode->getAbsolutePosition() +
416 intToFloat(camera_offset, BS);
422 const bool GenericCAO::isImmortal()
424 return itemgroup_get(getGroups(), "immortal");
427 scene::ISceneNode *GenericCAO::getSceneNode() const
433 if (m_animated_meshnode) {
434 return m_animated_meshnode;
437 if (m_wield_meshnode) {
438 return m_wield_meshnode;
447 scene::IAnimatedMeshSceneNode *GenericCAO::getAnimatedMeshSceneNode() const
449 return m_animated_meshnode;
452 void GenericCAO::setChildrenVisible(bool toset)
454 for (u16 cao_id : m_attachment_child_ids) {
455 GenericCAO *obj = m_env->getGenericCAO(cao_id);
457 obj->setVisible(toset);
462 void GenericCAO::setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation)
464 int old_parent = m_attachment_parent_id;
465 m_attachment_parent_id = parent_id;
466 m_attachment_bone = bone;
467 m_attachment_position = position;
468 m_attachment_rotation = rotation;
470 ClientActiveObject *parent = m_env->getActiveObject(parent_id);
472 if (parent_id != old_parent) {
473 if (auto *o = m_env->getActiveObject(old_parent))
474 o->removeAttachmentChild(m_id);
476 parent->addAttachmentChild(m_id);
482 void GenericCAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
485 *parent_id = m_attachment_parent_id;
486 *bone = m_attachment_bone;
487 *position = m_attachment_position;
488 *rotation = m_attachment_rotation;
491 void GenericCAO::clearChildAttachments()
493 // Cannot use for-loop here: setAttachment() modifies 'm_attachment_child_ids'!
494 while (!m_attachment_child_ids.empty()) {
495 int child_id = *m_attachment_child_ids.begin();
497 if (ClientActiveObject *child = m_env->getActiveObject(child_id))
498 child->setAttachment(0, "", v3f(), v3f());
500 removeAttachmentChild(child_id);
504 void GenericCAO::clearParentAttachment()
506 if (m_attachment_parent_id)
507 setAttachment(0, "", m_attachment_position, m_attachment_rotation);
509 setAttachment(0, "", v3f(), v3f());
512 void GenericCAO::addAttachmentChild(int child_id)
514 m_attachment_child_ids.insert(child_id);
517 void GenericCAO::removeAttachmentChild(int child_id)
519 m_attachment_child_ids.erase(child_id);
522 ClientActiveObject* GenericCAO::getParent() const
524 return m_attachment_parent_id ? m_env->getActiveObject(m_attachment_parent_id) :
528 void GenericCAO::removeFromScene(bool permanent)
530 // Should be true when removing the object permanently
531 // and false when refreshing (eg: updating visuals)
532 if (m_env && permanent) {
533 // The client does not know whether this object does re-appear to
534 // a later time, thus do not clear child attachments.
536 clearParentAttachment();
540 m_meshnode->remove();
542 m_meshnode = nullptr;
543 } else if (m_animated_meshnode) {
544 m_animated_meshnode->remove();
545 m_animated_meshnode->drop();
546 m_animated_meshnode = nullptr;
547 } else if (m_wield_meshnode) {
548 m_wield_meshnode->remove();
549 m_wield_meshnode->drop();
550 m_wield_meshnode = nullptr;
551 } else if (m_spritenode) {
552 m_spritenode->remove();
553 m_spritenode->drop();
554 m_spritenode = nullptr;
558 m_matrixnode->remove();
559 m_matrixnode->drop();
560 m_matrixnode = nullptr;
564 m_client->getCamera()->removeNametag(m_nametag);
569 void GenericCAO::addToScene(ITextureSource *tsrc)
571 m_smgr = RenderingEngine::get_scene_manager();
573 if (getSceneNode() != NULL) {
577 m_visuals_expired = false;
579 if (!m_prop.is_visible) {
583 if (m_enable_shaders) {
584 IShaderSource *shader_source = m_client->getShaderSource();
585 u32 shader_id = shader_source->getShader(
589 m_material_type = shader_source->getShaderInfo(shader_id).material;
591 m_material_type = (m_prop.use_texture_alpha) ?
592 video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
595 auto grabMatrixNode = [this] {
596 infostream << "GenericCAO::addToScene(): " << m_prop.visual << std::endl;
597 m_matrixnode = RenderingEngine::get_scene_manager()->
598 addDummyTransformationSceneNode();
599 m_matrixnode->grab();
602 auto setSceneNodeMaterial = [this] (scene::ISceneNode *node) {
603 node->setMaterialFlag(video::EMF_LIGHTING, false);
604 node->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
605 node->setMaterialFlag(video::EMF_FOG_ENABLE, true);
606 node->setMaterialType(m_material_type);
608 if (m_enable_shaders) {
609 node->setMaterialFlag(video::EMF_GOURAUD_SHADING, false);
610 node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
614 if (m_prop.visual == "sprite") {
616 m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
617 m_matrixnode, v2f(1, 1), v3f(0,0,0), -1);
618 m_spritenode->grab();
619 m_spritenode->setMaterialTexture(0,
620 tsrc->getTextureForMesh("unknown_node.png"));
622 setSceneNodeMaterial(m_spritenode);
624 u8 li = m_last_light;
625 m_spritenode->setColor(video::SColor(255,li,li,li));
626 m_spritenode->setSize(v2f(m_prop.visual_size.X,
627 m_prop.visual_size.Y) * BS);
629 const float txs = 1.0 / 1;
630 const float tys = 1.0 / 1;
631 setBillboardTextureMatrix(m_spritenode,
634 } else if (m_prop.visual == "upright_sprite") {
636 scene::SMesh *mesh = new scene::SMesh();
637 double dx = BS * m_prop.visual_size.X / 2;
638 double dy = BS * m_prop.visual_size.Y / 2;
639 u8 li = m_last_light;
640 video::SColor c(255, li, li, li);
643 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
644 video::S3DVertex vertices[4] = {
645 video::S3DVertex(-dx, -dy, 0, 0,0,1, c, 1,1),
646 video::S3DVertex( dx, -dy, 0, 0,0,1, c, 0,1),
647 video::S3DVertex( dx, dy, 0, 0,0,1, c, 0,0),
648 video::S3DVertex(-dx, dy, 0, 0,0,1, c, 1,0),
651 // Move minimal Y position to 0 (feet position)
652 for (video::S3DVertex &vertex : vertices)
655 u16 indices[] = {0,1,2,2,3,0};
656 buf->append(vertices, 4, indices, 6);
658 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
659 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
660 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
661 buf->getMaterial().MaterialType = m_material_type;
663 if (m_enable_shaders) {
664 buf->getMaterial().EmissiveColor = c;
665 buf->getMaterial().setFlag(video::EMF_GOURAUD_SHADING, false);
666 buf->getMaterial().setFlag(video::EMF_NORMALIZE_NORMALS, true);
670 mesh->addMeshBuffer(buf);
674 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
675 video::S3DVertex vertices[4] = {
676 video::S3DVertex( dx,-dy, 0, 0,0,-1, c, 1,1),
677 video::S3DVertex(-dx,-dy, 0, 0,0,-1, c, 0,1),
678 video::S3DVertex(-dx, dy, 0, 0,0,-1, c, 0,0),
679 video::S3DVertex( dx, dy, 0, 0,0,-1, c, 1,0),
682 // Move minimal Y position to 0 (feet position)
683 for (video::S3DVertex &vertex : vertices)
686 u16 indices[] = {0,1,2,2,3,0};
687 buf->append(vertices, 4, indices, 6);
689 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
690 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
691 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
692 buf->getMaterial().MaterialType = m_material_type;
694 if (m_enable_shaders) {
695 buf->getMaterial().EmissiveColor = c;
696 buf->getMaterial().setFlag(video::EMF_GOURAUD_SHADING, false);
697 buf->getMaterial().setFlag(video::EMF_NORMALIZE_NORMALS, true);
701 mesh->addMeshBuffer(buf);
704 m_meshnode = RenderingEngine::get_scene_manager()->
705 addMeshSceneNode(mesh, m_matrixnode);
708 // Set it to use the materials of the meshbuffers directly.
709 // This is needed for changing the texture in the future
710 m_meshnode->setReadOnlyMaterials(true);
711 } else if (m_prop.visual == "cube") {
713 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
714 m_meshnode = RenderingEngine::get_scene_manager()->
715 addMeshSceneNode(mesh, m_matrixnode);
719 m_meshnode->setScale(m_prop.visual_size);
720 u8 li = m_last_light;
721 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
723 setSceneNodeMaterial(m_meshnode);
724 } else if (m_prop.visual == "mesh") {
726 scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
728 m_animated_meshnode = RenderingEngine::get_scene_manager()->
729 addAnimatedMeshSceneNode(mesh, m_matrixnode);
730 m_animated_meshnode->grab();
731 mesh->drop(); // The scene node took hold of it
732 m_animated_meshnode->animateJoints(); // Needed for some animations
733 m_animated_meshnode->setScale(m_prop.visual_size);
734 u8 li = m_last_light;
736 // set vertex colors to ensure alpha is set
737 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
739 setAnimatedMeshColor(m_animated_meshnode, video::SColor(255,li,li,li));
741 setSceneNodeMaterial(m_animated_meshnode);
743 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
744 m_prop.backface_culling);
746 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
747 } else if (m_prop.visual == "wielditem" || m_prop.visual == "item") {
750 if (m_prop.wield_item.empty()) {
751 // Old format, only textures are specified.
752 infostream << "textures: " << m_prop.textures.size() << std::endl;
753 if (!m_prop.textures.empty()) {
754 infostream << "textures[0]: " << m_prop.textures[0]
756 IItemDefManager *idef = m_client->idef();
757 item = ItemStack(m_prop.textures[0], 1, 0, idef);
760 infostream << "serialized form: " << m_prop.wield_item << std::endl;
761 item.deSerialize(m_prop.wield_item, m_client->idef());
763 m_wield_meshnode = new WieldMeshSceneNode(
764 RenderingEngine::get_scene_manager(), -1);
765 m_wield_meshnode->setItem(item, m_client,
766 (m_prop.visual == "wielditem"));
768 m_wield_meshnode->setScale(m_prop.visual_size / 2.0f);
769 u8 li = m_last_light;
770 m_wield_meshnode->setColor(video::SColor(255, li, li, li));
772 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
773 <<"\" not supported"<<std::endl;
776 /* don't update while punch texture modifier is active */
777 if (m_reset_textures_timer < 0)
778 updateTextures(m_current_texture_modifier);
780 scene::ISceneNode *node = getSceneNode();
782 if (node && m_matrixnode)
783 node->setParent(m_matrixnode);
785 if (node && !m_prop.nametag.empty() && !m_is_local_player) {
788 pos.Y = m_prop.selectionbox.MaxEdge.Y + 0.3f;
789 m_nametag = m_client->getCamera()->addNametag(node,
790 m_prop.nametag, m_prop.nametag_color,
796 updateBonePosition();
800 void GenericCAO::updateLight(u8 light_at_pos)
802 // Don't update light of attached one
803 if (getParent() != NULL) {
807 updateLightNoCheck(light_at_pos);
809 // Update light of all children
810 for (u16 i : m_attachment_child_ids) {
811 ClientActiveObject *obj = m_env->getActiveObject(i);
813 obj->updateLightNoCheck(light_at_pos);
818 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
823 u8 li = decode_light(light_at_pos + m_glow);
825 if (li != m_last_light) {
827 video::SColor color(255,li,li,li);
829 if (m_enable_shaders) {
830 scene::ISceneNode *node = getSceneNode();
832 if (node == nullptr) {
836 if (m_prop.visual == "upright_sprite") {
837 scene::IMesh *mesh = m_meshnode->getMesh();
838 for (u32 i = 0; i < mesh->getMeshBufferCount(); ++i) {
839 scene::IMeshBuffer* buf = mesh->getMeshBuffer(i);
840 video::SMaterial& material = buf->getMaterial();
841 material.EmissiveColor = color;
844 for (u32 i = 0; i < node->getMaterialCount(); ++i) {
845 video::SMaterial& material = node->getMaterial(i);
846 material.EmissiveColor = color;
851 setMeshColor(m_meshnode->getMesh(), color);
852 } else if (m_animated_meshnode) {
853 setAnimatedMeshColor(m_animated_meshnode, color);
854 } else if (m_wield_meshnode) {
855 m_wield_meshnode->setColor(color);
856 } else if (m_spritenode) {
857 m_spritenode->setColor(color);
863 v3s16 GenericCAO::getLightPosition()
866 return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
868 return floatToInt(m_position, BS);
871 void GenericCAO::updateNodePos()
873 if (getParent() != NULL)
876 scene::ISceneNode *node = getSceneNode();
879 v3s16 camera_offset = m_env->getCameraOffset();
880 v3f pos = pos_translator.val_current -
881 intToFloat(camera_offset, BS);
882 getPosRotMatrix().setTranslation(pos);
883 if (node != m_spritenode) { // rotate if not a sprite
884 v3f rot = m_is_local_player ? -m_rotation : -rot_translator.val_current;
885 setPitchYawRoll(getPosRotMatrix(), rot);
890 void GenericCAO::step(float dtime, ClientEnvironment *env)
892 // Handle model animations and update positions instantly to prevent lags
893 if (m_is_local_player) {
894 LocalPlayer *player = m_env->getLocalPlayer();
895 m_position = player->getPosition();
896 pos_translator.val_current = m_position;
897 m_rotation.Y = wrapDegrees_0_360(player->getYaw());
898 rot_translator.val_current = m_rotation;
901 int old_anim = player->last_animation;
902 float old_anim_speed = player->last_animation_speed;
903 m_velocity = v3f(0,0,0);
904 m_acceleration = v3f(0,0,0);
905 const PlayerControl &controls = player->getPlayerControl();
907 bool walking = false;
908 if (controls.up || controls.down || controls.left || controls.right ||
909 controls.forw_move_joystick_axis != 0.f ||
910 controls.sidew_move_joystick_axis != 0.f)
913 f32 new_speed = player->local_animation_speed;
914 v2s32 new_anim = v2s32(0,0);
915 bool allow_update = false;
917 // increase speed if using fast or flying fast
918 if((g_settings->getBool("fast_move") &&
919 m_client->checkLocalPrivilege("fast")) &&
921 (!player->touching_ground &&
922 g_settings->getBool("free_move") &&
923 m_client->checkLocalPrivilege("fly"))))
925 // slowdown speed if sneeking
926 if (controls.sneak && walking)
929 if (walking && (controls.LMB || controls.RMB)) {
930 new_anim = player->local_animations[3];
931 player->last_animation = WD_ANIM;
933 new_anim = player->local_animations[1];
934 player->last_animation = WALK_ANIM;
935 } else if(controls.LMB || controls.RMB) {
936 new_anim = player->local_animations[2];
937 player->last_animation = DIG_ANIM;
940 // Apply animations if input detected and not attached
941 // or set idle animation
942 if ((new_anim.X + new_anim.Y) > 0 && !getParent()) {
944 m_animation_range = new_anim;
945 m_animation_speed = new_speed;
946 player->last_animation_speed = m_animation_speed;
948 player->last_animation = NO_ANIM;
950 if (old_anim != NO_ANIM) {
951 m_animation_range = player->local_animations[0];
956 // Update local player animations
957 if ((player->last_animation != old_anim ||
958 m_animation_speed != old_anim_speed) &&
959 player->last_animation != NO_ANIM && allow_update)
965 if (m_visuals_expired && m_smgr) {
966 m_visuals_expired = false;
968 // Attachments, part 1: All attached objects must be unparented first,
969 // or Irrlicht causes a segmentation fault
970 for (u16 cao_id : m_attachment_child_ids) {
971 ClientActiveObject *obj = m_env->getActiveObject(cao_id);
973 scene::ISceneNode *child_node = obj->getSceneNode();
974 // The node's parent is always an IDummyTraformationSceneNode,
975 // so we need to reparent that one instead.
977 child_node->getParent()->setParent(m_smgr->getRootSceneNode());
981 removeFromScene(false);
982 addToScene(m_client->tsrc());
984 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
985 for (u16 cao_id : m_attachment_child_ids) {
986 ClientActiveObject *obj = m_env->getActiveObject(cao_id);
988 obj->updateAttachments();
992 // Make sure m_is_visible is always applied
993 scene::ISceneNode *node = getSceneNode();
995 node->setVisible(m_is_visible);
997 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
999 // Set these for later
1000 m_position = getPosition();
1001 m_velocity = v3f(0,0,0);
1002 m_acceleration = v3f(0,0,0);
1003 pos_translator.val_current = m_position;
1004 pos_translator.val_target = m_position;
1006 rot_translator.translate(dtime);
1007 v3f lastpos = pos_translator.val_current;
1011 aabb3f box = m_prop.collisionbox;
1014 collisionMoveResult moveresult;
1015 f32 pos_max_d = BS*0.125; // Distance per iteration
1016 v3f p_pos = m_position;
1017 v3f p_velocity = m_velocity;
1018 moveresult = collisionMoveSimple(env,env->getGameDef(),
1019 pos_max_d, box, m_prop.stepheight, dtime,
1020 &p_pos, &p_velocity, m_acceleration,
1021 this, m_prop.collideWithObjects);
1024 m_velocity = p_velocity;
1026 bool is_end_position = moveresult.collides;
1027 pos_translator.update(m_position, is_end_position, dtime);
1029 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1030 m_velocity += dtime * m_acceleration;
1031 pos_translator.update(m_position, pos_translator.aim_is_end,
1032 pos_translator.anim_time);
1034 pos_translator.translate(dtime);
1037 float moved = lastpos.getDistanceFrom(pos_translator.val_current);
1038 m_step_distance_counter += moved;
1039 if (m_step_distance_counter > 1.5f * BS) {
1040 m_step_distance_counter = 0.0f;
1041 if (!m_is_local_player && m_prop.makes_footstep_sound) {
1042 const NodeDefManager *ndef = m_client->ndef();
1043 v3s16 p = floatToInt(getPosition() +
1044 v3f(0.0f, (m_prop.collisionbox.MinEdge.Y - 0.5f) * BS, 0.0f), BS);
1045 MapNode n = m_env->getMap().getNode(p);
1046 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1047 // Reduce footstep gain, as non-local-player footsteps are
1050 m_client->sound()->playSoundAt(spec, false, getPosition());
1055 m_anim_timer += dtime;
1056 if(m_anim_timer >= m_anim_framelength)
1058 m_anim_timer -= m_anim_framelength;
1060 if(m_anim_frame >= m_anim_num_frames)
1066 if(m_reset_textures_timer >= 0)
1068 m_reset_textures_timer -= dtime;
1069 if(m_reset_textures_timer <= 0) {
1070 m_reset_textures_timer = -1;
1071 updateTextures(m_previous_texture_modifier);
1074 if (!getParent() && std::fabs(m_prop.automatic_rotate) > 0.001) {
1075 m_rotation.Y += dtime * m_prop.automatic_rotate * 180 / M_PI;
1076 rot_translator.val_current = m_rotation;
1080 if (!getParent() && m_prop.automatic_face_movement_dir &&
1081 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)) {
1082 float target_yaw = atan2(m_velocity.Z, m_velocity.X) * 180 / M_PI
1083 + m_prop.automatic_face_movement_dir_offset;
1084 float max_rotation_per_sec =
1085 m_prop.automatic_face_movement_max_rotation_per_sec;
1087 if (max_rotation_per_sec > 0) {
1088 wrappedApproachShortest(m_rotation.Y, target_yaw,
1089 dtime * max_rotation_per_sec, 360.f);
1091 // Negative values of max_rotation_per_sec mean disabled.
1092 m_rotation.Y = target_yaw;
1095 rot_translator.val_current = m_rotation;
1100 void GenericCAO::updateTexturePos()
1104 scene::ICameraSceneNode* camera =
1105 m_spritenode->getSceneManager()->getActiveCamera();
1108 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1109 - camera->getAbsolutePosition();
1110 cam_to_entity.normalize();
1112 int row = m_tx_basepos.Y;
1113 int col = m_tx_basepos.X;
1115 if (m_tx_select_horiz_by_yawpitch) {
1116 if (cam_to_entity.Y > 0.75)
1118 else if (cam_to_entity.Y < -0.75)
1122 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1123 float dir = mob_dir - m_rotation.Y;
1124 dir = wrapDegrees_180(dir);
1125 if (std::fabs(wrapDegrees_180(dir - 0)) <= 45.1f)
1127 else if(std::fabs(wrapDegrees_180(dir - 90)) <= 45.1f)
1129 else if(std::fabs(wrapDegrees_180(dir - 180)) <= 45.1f)
1131 else if(std::fabs(wrapDegrees_180(dir + 90)) <= 45.1f)
1138 // Animation goes downwards
1139 row += m_anim_frame;
1141 float txs = m_tx_size.X;
1142 float tys = m_tx_size.Y;
1143 setBillboardTextureMatrix(m_spritenode, txs, tys, col, row);
1147 // Do not pass by reference, see header.
1148 void GenericCAO::updateTextures(std::string mod)
1150 ITextureSource *tsrc = m_client->tsrc();
1152 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1153 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1154 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1156 m_previous_texture_modifier = m_current_texture_modifier;
1157 m_current_texture_modifier = mod;
1158 m_glow = m_prop.glow;
1161 if (m_prop.visual == "sprite") {
1162 std::string texturestring = "unknown_node.png";
1163 if (!m_prop.textures.empty())
1164 texturestring = m_prop.textures[0];
1165 texturestring += mod;
1166 m_spritenode->getMaterial(0).MaterialType = m_material_type;
1167 m_spritenode->getMaterial(0).MaterialTypeParam = 0.5f;
1168 m_spritenode->setMaterialTexture(0,
1169 tsrc->getTextureForMesh(texturestring));
1171 // This allows setting per-material colors. However, until a real lighting
1172 // system is added, the code below will have no effect. Once MineTest
1173 // has directional lighting, it should work automatically.
1174 if (!m_prop.colors.empty()) {
1175 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1176 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1177 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1180 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1181 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1182 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1186 if (m_animated_meshnode) {
1187 if (m_prop.visual == "mesh") {
1188 for (u32 i = 0; i < m_prop.textures.size() &&
1189 i < m_animated_meshnode->getMaterialCount(); ++i) {
1190 std::string texturestring = m_prop.textures[i];
1191 if (texturestring.empty())
1192 continue; // Empty texture string means don't modify that material
1193 texturestring += mod;
1194 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1196 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1200 // Set material flags and texture
1201 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1202 material.MaterialType = m_material_type;
1203 material.MaterialTypeParam = 0.5f;
1204 material.TextureLayer[0].Texture = texture;
1205 material.setFlag(video::EMF_LIGHTING, true);
1206 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1207 material.setFlag(video::EMF_BACK_FACE_CULLING, m_prop.backface_culling);
1209 // don't filter low-res textures, makes them look blurry
1210 // player models have a res of 64
1211 const core::dimension2d<u32> &size = texture->getOriginalSize();
1212 const u32 res = std::min(size.Height, size.Width);
1213 use_trilinear_filter &= res > 64;
1214 use_bilinear_filter &= res > 64;
1216 m_animated_meshnode->getMaterial(i)
1217 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1218 m_animated_meshnode->getMaterial(i)
1219 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1220 m_animated_meshnode->getMaterial(i)
1221 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1223 for (u32 i = 0; i < m_prop.colors.size() &&
1224 i < m_animated_meshnode->getMaterialCount(); ++i)
1226 // This allows setting per-material colors. However, until a real lighting
1227 // system is added, the code below will have no effect. Once MineTest
1228 // has directional lighting, it should work automatically.
1229 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1230 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1231 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1237 if(m_prop.visual == "cube")
1239 for (u32 i = 0; i < 6; ++i)
1241 std::string texturestring = "unknown_node.png";
1242 if(m_prop.textures.size() > i)
1243 texturestring = m_prop.textures[i];
1244 texturestring += mod;
1247 // Set material flags and texture
1248 video::SMaterial& material = m_meshnode->getMaterial(i);
1249 material.MaterialType = m_material_type;
1250 material.MaterialTypeParam = 0.5f;
1251 material.setFlag(video::EMF_LIGHTING, false);
1252 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1253 material.setTexture(0,
1254 tsrc->getTextureForMesh(texturestring));
1255 material.getTextureMatrix(0).makeIdentity();
1257 // This allows setting per-material colors. However, until a real lighting
1258 // system is added, the code below will have no effect. Once MineTest
1259 // has directional lighting, it should work automatically.
1260 if(m_prop.colors.size() > i)
1262 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1263 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1264 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1267 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1268 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1269 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1271 } else if (m_prop.visual == "upright_sprite") {
1272 scene::IMesh *mesh = m_meshnode->getMesh();
1274 std::string tname = "unknown_object.png";
1275 if (!m_prop.textures.empty())
1276 tname = m_prop.textures[0];
1278 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1279 buf->getMaterial().setTexture(0,
1280 tsrc->getTextureForMesh(tname));
1282 // This allows setting per-material colors. However, until a real lighting
1283 // system is added, the code below will have no effect. Once MineTest
1284 // has directional lighting, it should work automatically.
1285 if(!m_prop.colors.empty()) {
1286 buf->getMaterial().AmbientColor = m_prop.colors[0];
1287 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1288 buf->getMaterial().SpecularColor = m_prop.colors[0];
1291 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1292 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1293 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1296 std::string tname = "unknown_object.png";
1297 if (m_prop.textures.size() >= 2)
1298 tname = m_prop.textures[1];
1299 else if (!m_prop.textures.empty())
1300 tname = m_prop.textures[0];
1302 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1303 buf->getMaterial().setTexture(0,
1304 tsrc->getTextureForMesh(tname));
1306 // This allows setting per-material colors. However, until a real lighting
1307 // system is added, the code below will have no effect. Once MineTest
1308 // has directional lighting, it should work automatically.
1309 if (m_prop.colors.size() >= 2) {
1310 buf->getMaterial().AmbientColor = m_prop.colors[1];
1311 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1312 buf->getMaterial().SpecularColor = m_prop.colors[1];
1313 } else if (!m_prop.colors.empty()) {
1314 buf->getMaterial().AmbientColor = m_prop.colors[0];
1315 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1316 buf->getMaterial().SpecularColor = m_prop.colors[0];
1319 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1320 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1321 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1323 // Set mesh color (only if lighting is disabled)
1324 if (!m_prop.colors.empty() && m_glow < 0)
1325 setMeshColor(mesh, m_prop.colors[0]);
1330 void GenericCAO::updateAnimation()
1332 if (!m_animated_meshnode)
1335 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1336 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1337 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1338 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1339 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1340 m_animated_meshnode->setTransitionTime(m_animation_blend);
1341 // Requires Irrlicht 1.8 or greater
1342 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1343 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1344 m_animated_meshnode->setLoopMode(m_animation_loop);
1348 void GenericCAO::updateAnimationSpeed()
1350 if (!m_animated_meshnode)
1353 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1356 void GenericCAO::updateBonePosition()
1358 if (m_bone_position.empty() || !m_animated_meshnode)
1361 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1362 for(std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
1363 ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1364 std::string bone_name = (*ii).first;
1365 v3f bone_pos = (*ii).second.X;
1366 v3f bone_rot = (*ii).second.Y;
1367 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1370 bone->setPosition(bone_pos);
1371 bone->setRotation(bone_rot);
1376 void GenericCAO::updateAttachments()
1378 ClientActiveObject *parent = getParent();
1380 m_attached_to_local = parent && parent->isLocalPlayer();
1383 Following cases exist:
1384 m_attachment_parent_id == 0 && !parent
1385 This object is not attached
1386 m_attachment_parent_id != 0 && parent
1387 This object is attached
1388 m_attachment_parent_id != 0 && !parent
1389 This object will be attached as soon the parent is known
1390 m_attachment_parent_id == 0 && parent
1394 if (!parent) { // Detach or don't attach
1396 v3f old_pos = getPosition();
1398 m_matrixnode->setParent(m_smgr->getRootSceneNode());
1399 getPosRotMatrix().setTranslation(old_pos);
1400 m_matrixnode->updateAbsolutePosition();
1405 scene::ISceneNode *parent_node = parent->getSceneNode();
1406 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1407 parent->getAnimatedMeshSceneNode();
1408 if (parent_animated_mesh_node && !m_attachment_bone.empty()) {
1409 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1412 if (m_matrixnode && parent_node) {
1413 m_matrixnode->setParent(parent_node);
1414 getPosRotMatrix().setTranslation(m_attachment_position);
1415 //setPitchYawRoll(getPosRotMatrix(), m_attachment_rotation);
1416 // use Irrlicht eulers instead
1417 getPosRotMatrix().setRotationDegrees(m_attachment_rotation);
1418 m_matrixnode->updateAbsolutePosition();
1423 void GenericCAO::processMessage(const std::string &data)
1425 //infostream<<"GenericCAO: Got message"<<std::endl;
1426 std::istringstream is(data, std::ios::binary);
1428 u8 cmd = readU8(is);
1429 if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1430 m_prop = gob_read_set_properties(is);
1432 m_selection_box = m_prop.selectionbox;
1433 m_selection_box.MinEdge *= BS;
1434 m_selection_box.MaxEdge *= BS;
1436 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1437 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1439 if(!m_initial_tx_basepos_set){
1440 m_initial_tx_basepos_set = true;
1441 m_tx_basepos = m_prop.initial_sprite_basepos;
1443 if (m_is_local_player) {
1444 LocalPlayer *player = m_env->getLocalPlayer();
1445 player->makes_footstep_sound = m_prop.makes_footstep_sound;
1446 aabb3f collision_box = m_prop.collisionbox;
1447 collision_box.MinEdge *= BS;
1448 collision_box.MaxEdge *= BS;
1449 player->setCollisionbox(collision_box);
1450 player->setEyeHeight(m_prop.eye_height);
1451 player->setZoomFOV(m_prop.zoom_fov);
1454 if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
1455 m_prop.nametag = m_name;
1458 } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1459 // Not sent by the server if this object is an attachment.
1460 // We might however get here if the server notices the object being detached before the client.
1461 m_position = readV3F32(is);
1462 m_velocity = readV3F32(is);
1463 m_acceleration = readV3F32(is);
1465 if (std::fabs(m_prop.automatic_rotate) < 0.001f)
1466 m_rotation = readV3F32(is);
1470 m_rotation = wrapDegrees_0_360_v3f(m_rotation);
1471 bool do_interpolate = readU8(is);
1472 bool is_end_position = readU8(is);
1473 float update_interval = readF32(is);
1475 // Place us a bit higher if we're physical, to not sink into
1476 // the ground due to sucky collision detection...
1478 m_position += v3f(0,0.002,0);
1480 if(getParent() != NULL) // Just in case
1485 if(!m_prop.physical)
1486 pos_translator.update(m_position, is_end_position, update_interval);
1488 pos_translator.init(m_position);
1490 rot_translator.update(m_rotation, false, update_interval);
1492 } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1493 std::string mod = deSerializeString(is);
1495 // immediatly reset a engine issued texture modifier if a mod sends a different one
1496 if (m_reset_textures_timer > 0) {
1497 m_reset_textures_timer = -1;
1498 updateTextures(m_previous_texture_modifier);
1500 updateTextures(mod);
1501 } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1502 v2s16 p = readV2S16(is);
1503 int num_frames = readU16(is);
1504 float framelength = readF32(is);
1505 bool select_horiz_by_yawpitch = readU8(is);
1508 m_anim_num_frames = num_frames;
1509 m_anim_framelength = framelength;
1510 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1513 } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1514 float override_speed = readF32(is);
1515 float override_jump = readF32(is);
1516 float override_gravity = readF32(is);
1517 // these are sent inverted so we get true when the server sends nothing
1518 bool sneak = !readU8(is);
1519 bool sneak_glitch = !readU8(is);
1520 bool new_move = !readU8(is);
1523 if(m_is_local_player)
1525 LocalPlayer *player = m_env->getLocalPlayer();
1526 player->physics_override_speed = override_speed;
1527 player->physics_override_jump = override_jump;
1528 player->physics_override_gravity = override_gravity;
1529 player->physics_override_sneak = sneak;
1530 player->physics_override_sneak_glitch = sneak_glitch;
1531 player->physics_override_new_move = new_move;
1533 } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1534 // TODO: change frames send as v2s32 value
1535 v2f range = readV2F32(is);
1536 if (!m_is_local_player) {
1537 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1538 m_animation_speed = readF32(is);
1539 m_animation_blend = readF32(is);
1540 // these are sent inverted so we get true when the server sends nothing
1541 m_animation_loop = !readU8(is);
1544 LocalPlayer *player = m_env->getLocalPlayer();
1545 if(player->last_animation == NO_ANIM)
1547 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1548 m_animation_speed = readF32(is);
1549 m_animation_blend = readF32(is);
1550 // these are sent inverted so we get true when the server sends nothing
1551 m_animation_loop = !readU8(is);
1553 // update animation only if local animations present
1554 // and received animation is unknown (except idle animation)
1555 bool is_known = false;
1556 for (int i = 1;i<4;i++)
1558 if(m_animation_range.Y == player->local_animations[i].Y)
1562 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1567 } else if (cmd == GENERIC_CMD_SET_ANIMATION_SPEED) {
1568 m_animation_speed = readF32(is);
1569 updateAnimationSpeed();
1570 } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1571 std::string bone = deSerializeString(is);
1572 v3f position = readV3F32(is);
1573 v3f rotation = readV3F32(is);
1574 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1576 updateBonePosition();
1577 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1578 u16 parent_id = readS16(is);
1579 std::string bone = deSerializeString(is);
1580 v3f position = readV3F32(is);
1581 v3f rotation = readV3F32(is);
1583 setAttachment(parent_id, bone, position, rotation);
1585 // localplayer itself can't be attached to localplayer
1586 if (!m_is_local_player)
1587 m_is_visible = !m_attached_to_local;
1588 } else if (cmd == GENERIC_CMD_PUNCHED) {
1589 u16 result_hp = readU16(is);
1591 // Use this instead of the send damage to not interfere with prediction
1592 s32 damage = (s32)m_hp - (s32)result_hp;
1596 if (m_is_local_player)
1597 m_env->getLocalPlayer()->hp = m_hp;
1603 // TODO: Execute defined fast response
1604 // As there is no definition, make a smoke puff
1605 ClientSimpleObject *simple = createSmokePuff(
1606 m_smgr, m_env, m_position,
1607 v2f(m_prop.visual_size.X, m_prop.visual_size.Y) * BS);
1608 m_env->addSimpleObject(simple);
1609 } else if (m_reset_textures_timer < 0) {
1610 // TODO: Execute defined fast response
1611 // Flashing shall suffice as there is no definition
1612 m_reset_textures_timer = 0.05;
1614 m_reset_textures_timer += 0.05 * damage;
1615 updateTextures(m_current_texture_modifier + "^[brighten");
1620 // Same as 'Server::DiePlayer'
1621 clearParentAttachment();
1622 // Same as 'ObjectRef::l_remove'
1624 clearChildAttachments();
1626 } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1627 m_armor_groups.clear();
1628 int armor_groups_size = readU16(is);
1629 for(int i=0; i<armor_groups_size; i++)
1631 std::string name = deSerializeString(is);
1632 int rating = readS16(is);
1633 m_armor_groups[name] = rating;
1635 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1636 // Deprecated, for backwards compatibility only.
1637 readU8(is); // version
1638 m_prop.nametag_color = readARGB8(is);
1639 if (m_nametag != NULL) {
1640 m_nametag->nametag_color = m_prop.nametag_color;
1642 pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
1643 m_nametag->nametag_pos = pos;
1645 } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1646 u16 child_id = readU16(is);
1647 u8 type = readU8(is); // maybe this will be useful later
1650 addAttachmentChild(child_id);
1652 warningstream << FUNCTION_NAME
1653 << ": unknown command or outdated client \""
1654 << +cmd << "\"" << std::endl;
1658 /* \pre punchitem != NULL
1660 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1661 float time_from_last_punch)
1663 assert(punchitem); // pre-condition
1664 const ToolCapabilities *toolcap =
1665 &punchitem->getToolCapabilities(m_client->idef());
1666 PunchDamageResult result = getPunchDamage(
1670 time_from_last_punch);
1672 if(result.did_punch && result.damage != 0)
1674 if(result.damage < m_hp)
1676 m_hp -= result.damage;
1679 // TODO: Execute defined fast response
1680 // As there is no definition, make a smoke puff
1681 ClientSimpleObject *simple = createSmokePuff(
1682 m_smgr, m_env, m_position,
1683 v2f(m_prop.visual_size.X, m_prop.visual_size.Y) * BS);
1684 m_env->addSimpleObject(simple);
1686 // TODO: Execute defined fast response
1687 // Flashing shall suffice as there is no definition
1688 if (m_reset_textures_timer < 0) {
1689 m_reset_textures_timer = 0.05;
1690 if (result.damage >= 2)
1691 m_reset_textures_timer += 0.05 * result.damage;
1692 updateTextures(m_current_texture_modifier + "^[brighten");
1699 std::string GenericCAO::debugInfoText()
1701 std::ostringstream os(std::ios::binary);
1702 os<<"GenericCAO hp="<<m_hp<<"\n";
1704 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1705 i != m_armor_groups.end(); ++i)
1707 os<<i->first<<"="<<i->second<<", ";
1714 GenericCAO proto_GenericCAO(NULL, NULL);