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 <IBoneSceneNode.h>
26 #include "content_cao.h"
27 #include "util/numeric.h" // For IntervalLimiter
28 #include "util/serialize.h"
29 #include "util/mathconstants.h"
30 #include "client/tile.h"
31 #include "environment.h"
32 #include "collision.h"
34 #include "serialization.h" // For decompressZlib
36 #include "clientobject.h"
40 #include "content_cso.h"
43 #include "localplayer.h"
45 #include "camera.h" // CameraModes
46 #include "wieldmesh.h"
50 struct ToolCapabilities;
52 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
54 std::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
56 SmoothTranslator::SmoothTranslator():
66 void SmoothTranslator::init(v3f vect)
73 anim_time_counter = 0;
77 void SmoothTranslator::sharpen()
82 void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
84 aim_is_end = is_end_position;
87 if(update_interval > 0)
89 anim_time = update_interval;
91 if(anim_time < 0.001 || anim_time > 1.0)
92 anim_time = anim_time_counter;
94 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
96 anim_time_counter = 0;
100 void SmoothTranslator::translate(f32 dtime)
102 anim_time_counter = anim_time_counter + dtime;
103 anim_counter = anim_counter + dtime;
104 v3f vect_move = vect_aim - vect_old;
106 if(anim_time > 0.001)
107 moveratio = anim_time_counter / anim_time;
108 // Move a bit less than should, to avoid oscillation
109 moveratio = moveratio * 0.8;
110 float move_end = 1.5;
113 if(moveratio > move_end)
114 moveratio = move_end;
115 vect_show = vect_old + vect_move * moveratio;
118 bool SmoothTranslator::is_moving()
120 return ((anim_time_counter / anim_time) < 1.4);
127 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
128 float txs, float tys, int col, int row)
130 video::SMaterial& material = bill->getMaterial(0);
131 core::matrix4& matrix = material.getTextureMatrix(0);
132 matrix.setTextureTranslate(txs*col, tys*row);
133 matrix.setTextureScale(txs, tys);
140 class TestCAO : public ClientActiveObject
143 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
146 ActiveObjectType getType() const
148 return ACTIVEOBJECT_TYPE_TEST;
151 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
153 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
154 IrrlichtDevice *irr);
155 void removeFromScene(bool permanent);
156 void updateLight(u8 light_at_pos);
157 v3s16 getLightPosition();
158 void updateNodePos();
160 void step(float dtime, ClientEnvironment *env);
162 void processMessage(const std::string &data);
164 bool getCollisionBox(aabb3f *toset) { return false; }
166 scene::IMeshSceneNode *m_node;
171 TestCAO proto_TestCAO(NULL, NULL);
173 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
174 ClientActiveObject(0, gamedef, env),
176 m_position(v3f(0,10*BS,0))
178 ClientActiveObject::registerType(getType(), create);
185 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
187 return new TestCAO(gamedef, env);
190 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
196 //video::IVideoDriver* driver = smgr->getVideoDriver();
198 scene::SMesh *mesh = new scene::SMesh();
199 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
200 video::SColor c(255,255,255,255);
201 video::S3DVertex vertices[4] =
203 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
204 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
205 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
206 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
208 u16 indices[] = {0,1,2,2,3,0};
209 buf->append(vertices, 4, indices, 6);
211 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
212 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
213 buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
214 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
215 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
216 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
218 mesh->addMeshBuffer(buf);
220 m_node = smgr->addMeshSceneNode(mesh, NULL);
225 void TestCAO::removeFromScene(bool permanent)
234 void TestCAO::updateLight(u8 light_at_pos)
238 v3s16 TestCAO::getLightPosition()
240 return floatToInt(m_position, BS);
243 void TestCAO::updateNodePos()
248 m_node->setPosition(m_position);
249 //m_node->setRotation(v3f(0, 45, 0));
252 void TestCAO::step(float dtime, ClientEnvironment *env)
256 v3f rot = m_node->getRotation();
257 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
258 rot.Y += dtime * 180;
259 m_node->setRotation(rot);
263 void TestCAO::processMessage(const std::string &data)
265 infostream<<"TestCAO: Got data: "<<data<<std::endl;
266 std::istringstream is(data, std::ios::binary);
284 class ItemCAO : public ClientActiveObject
287 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
290 ActiveObjectType getType() const
292 return ACTIVEOBJECT_TYPE_ITEM;
295 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
297 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
298 IrrlichtDevice *irr);
299 void removeFromScene(bool permanent);
300 void updateLight(u8 light_at_pos);
301 v3s16 getLightPosition();
302 void updateNodePos();
303 void updateInfoText();
304 void updateTexture();
306 void step(float dtime, ClientEnvironment *env);
308 void processMessage(const std::string &data);
310 void initialize(const std::string &data);
312 core::aabbox3d<f32>* getSelectionBox()
313 {return &m_selection_box;}
317 std::string infoText()
320 bool getCollisionBox(aabb3f *toset) { return false; }
322 core::aabbox3d<f32> m_selection_box;
323 scene::IMeshSceneNode *m_node;
325 std::string m_itemstring;
326 std::string m_infotext;
329 #include "inventory.h"
332 ItemCAO proto_ItemCAO(NULL, NULL);
334 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
335 ClientActiveObject(0, gamedef, env),
336 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
338 m_position(v3f(0,10*BS,0))
342 ClientActiveObject::registerType(getType(), create);
350 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
352 return new ItemCAO(gamedef, env);
355 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
361 //video::IVideoDriver* driver = smgr->getVideoDriver();
363 scene::SMesh *mesh = new scene::SMesh();
364 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
365 video::SColor c(255,255,255,255);
366 video::S3DVertex vertices[4] =
368 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
369 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
370 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
371 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
372 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
373 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
374 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
375 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
377 u16 indices[] = {0,1,2,2,3,0};
378 buf->append(vertices, 4, indices, 6);
380 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
381 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
382 // Initialize with a generated placeholder texture
383 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
384 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
385 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
386 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
388 mesh->addMeshBuffer(buf);
390 m_node = smgr->addMeshSceneNode(mesh, NULL);
401 void ItemCAO::removeFromScene(bool permanent)
410 void ItemCAO::updateLight(u8 light_at_pos)
415 u8 li = decode_light(light_at_pos);
416 video::SColor color(255,li,li,li);
417 setMeshColor(m_node->getMesh(), color);
420 v3s16 ItemCAO::getLightPosition()
422 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
425 void ItemCAO::updateNodePos()
430 m_node->setPosition(m_position);
433 void ItemCAO::updateInfoText()
436 IItemDefManager *idef = m_gamedef->idef();
438 item.deSerialize(m_itemstring, idef);
439 if(item.isKnown(idef))
440 m_infotext = item.getDefinition(idef).description;
442 m_infotext = "Unknown item: '" + m_itemstring + "'";
444 m_infotext += " (" + itos(item.count) + ")";
446 catch(SerializationError &e)
448 m_infotext = "Unknown item: '" + m_itemstring + "'";
452 void ItemCAO::updateTexture()
457 // Create an inventory item to see what is its image
458 std::istringstream is(m_itemstring, std::ios_base::binary);
459 video::ITexture *texture = NULL;
461 IItemDefManager *idef = m_gamedef->idef();
463 item.deSerialize(is, idef);
464 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
466 catch(SerializationError &e)
468 infostream<<"WARNING: "<<__FUNCTION_NAME
469 <<": error deSerializing itemstring \""
470 <<m_itemstring<<std::endl;
473 // Set meshbuffer texture
474 m_node->getMaterial(0).setTexture(0, texture);
478 void ItemCAO::step(float dtime, ClientEnvironment *env)
482 /*v3f rot = m_node->getRotation();
483 rot.Y += dtime * 120;
484 m_node->setRotation(rot);*/
485 LocalPlayer *player = env->getLocalPlayer();
487 v3f rot = m_node->getRotation();
488 rot.Y = 180.0 - (player->getYaw());
489 m_node->setRotation(rot);
493 void ItemCAO::processMessage(const std::string &data)
495 //infostream<<"ItemCAO: Got message"<<std::endl;
496 std::istringstream is(data, std::ios::binary);
502 m_position = readV3F1000(is);
508 m_itemstring = deSerializeString(is);
514 void ItemCAO::initialize(const std::string &data)
516 infostream<<"ItemCAO: Got init data"<<std::endl;
519 std::istringstream is(data, std::ios::binary);
521 u8 version = readU8(is);
526 m_position = readV3F1000(is);
528 m_itemstring = deSerializeString(is);
539 #include "genericobject.h"
541 GenericCAO::GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
542 ClientActiveObject(0, gamedef, env),
545 m_is_local_player(false),
549 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
551 m_animated_meshnode(NULL),
552 m_wield_meshnode(NULL),
554 m_nametag_color(video::SColor(255, 255, 255, 255)),
556 m_position(v3f(0,10*BS,0)),
557 m_velocity(v3f(0,0,0)),
558 m_acceleration(v3f(0,0,0)),
563 m_initial_tx_basepos_set(false),
564 m_tx_select_horiz_by_yawpitch(false),
565 m_animation_range(v2s32(0,0)),
566 m_animation_speed(15),
567 m_animation_blend(0),
568 m_animation_loop(true),
569 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
570 m_attachment_bone(""),
571 m_attachment_position(v3f(0,0,0)),
572 m_attachment_rotation(v3f(0,0,0)),
573 m_attached_to_local(false),
575 m_anim_num_frames(1),
576 m_anim_framelength(0.2),
578 m_reset_textures_timer(-1),
579 m_visuals_expired(false),
580 m_step_distance_counter(0),
585 ClientActiveObject::registerType(getType(), create);
588 bool GenericCAO::getCollisionBox(aabb3f *toset)
592 //update collision box
593 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
594 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
596 toset->MinEdge += m_position;
597 toset->MaxEdge += m_position;
605 bool GenericCAO::collideWithObjects()
607 return m_prop.collideWithObjects;
610 void GenericCAO::initialize(const std::string &data)
612 infostream<<"GenericCAO: Got init data"<<std::endl;
613 std::istringstream is(data, std::ios::binary);
614 int num_messages = 0;
616 u8 version = readU8(is);
618 if(version == 1) // In PROTOCOL_VERSION 14
620 m_name = deSerializeString(is);
621 m_is_player = readU8(is);
623 m_position = readV3F1000(is);
624 m_yaw = readF1000(is);
626 num_messages = readU8(is);
628 else if(version == 0) // In PROTOCOL_VERSION 13
630 m_name = deSerializeString(is);
631 m_is_player = readU8(is);
632 m_position = readV3F1000(is);
633 m_yaw = readF1000(is);
635 num_messages = readU8(is);
639 errorstream<<"GenericCAO: Unsupported init data version"
644 for(int i=0; i<num_messages; i++)
646 std::string message = deSerializeLongString(is);
647 processMessage(message);
650 pos_translator.init(m_position);
655 Player *player = m_env->getPlayer(m_name.c_str());
656 if(player && player->isLocal())
658 m_is_local_player = true;
659 m_is_visible = false;
660 LocalPlayer* localplayer = dynamic_cast<LocalPlayer*>(player);
662 assert( localplayer != NULL );
663 localplayer->setCAO(this);
665 m_env->addPlayerName(m_name.c_str());
669 GenericCAO::~GenericCAO()
673 m_env->removePlayerName(m_name.c_str());
675 removeFromScene(true);
678 core::aabbox3d<f32>* GenericCAO::getSelectionBox()
680 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
682 return &m_selection_box;
685 v3f GenericCAO::getPosition()
687 if (getParent() != NULL) {
688 scene::ISceneNode *node = getSceneNode();
690 return node->getAbsolutePosition();
694 return pos_translator.vect_show;
697 scene::ISceneNode* GenericCAO::getSceneNode()
701 if (m_animated_meshnode)
702 return m_animated_meshnode;
703 if (m_wield_meshnode)
704 return m_wield_meshnode;
710 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
715 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
717 return m_animated_meshnode;
720 WieldMeshSceneNode* GenericCAO::getWieldMeshSceneNode()
722 return m_wield_meshnode;
725 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
730 void GenericCAO::setChildrenVisible(bool toset)
732 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
733 GenericCAO *obj = m_env->getGenericCAO(m_children[i]);
735 obj->setVisible(toset);
740 void GenericCAO::setAttachments()
745 ClientActiveObject* GenericCAO::getParent()
747 ClientActiveObject *obj = NULL;
749 u16 attached_id = m_env->attachement_parent_ids[getId()];
751 if ((attached_id != 0) &&
752 (attached_id != getId())) {
753 obj = m_env->getActiveObject(attached_id);
758 void GenericCAO::removeFromScene(bool permanent)
760 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
761 if((m_env != NULL) && (permanent))
763 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
764 u16 ci = m_children[i];
765 if (m_env->attachement_parent_ids[ci] == getId()) {
766 m_env->attachement_parent_ids[ci] = 0;
770 m_env->attachement_parent_ids[getId()] = 0;
772 LocalPlayer* player = m_env->getLocalPlayer();
773 if (this == player->parent) {
774 player->parent = NULL;
775 player->isAttached = false;
781 m_meshnode->remove();
785 if(m_animated_meshnode)
787 m_animated_meshnode->remove();
788 m_animated_meshnode->drop();
789 m_animated_meshnode = NULL;
793 m_wield_meshnode->remove();
794 m_wield_meshnode->drop();
795 m_wield_meshnode = NULL;
799 m_spritenode->remove();
800 m_spritenode->drop();
805 m_textnode->remove();
811 void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
817 if (getSceneNode() != NULL)
820 m_visuals_expired = false;
822 if(!m_prop.is_visible)
825 //video::IVideoDriver* driver = smgr->getVideoDriver();
827 if(m_prop.visual == "sprite")
829 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
830 m_spritenode = smgr->addBillboardSceneNode(
831 NULL, v2f(1, 1), v3f(0,0,0), -1);
832 m_spritenode->grab();
833 m_spritenode->setMaterialTexture(0,
834 tsrc->getTextureForMesh("unknown_node.png"));
835 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
836 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
837 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
838 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
839 u8 li = m_last_light;
840 m_spritenode->setColor(video::SColor(255,li,li,li));
841 m_spritenode->setSize(m_prop.visual_size*BS);
843 const float txs = 1.0 / 1;
844 const float tys = 1.0 / 1;
845 setBillboardTextureMatrix(m_spritenode,
849 else if(m_prop.visual == "upright_sprite") {
850 scene::SMesh *mesh = new scene::SMesh();
851 double dx = BS*m_prop.visual_size.X/2;
852 double dy = BS*m_prop.visual_size.Y/2;
854 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
855 u8 li = m_last_light;
856 video::SColor c(255,li,li,li);
857 video::S3DVertex vertices[4] =
859 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
860 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
861 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
862 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
864 u16 indices[] = {0,1,2,2,3,0};
865 buf->append(vertices, 4, indices, 6);
867 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
868 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
869 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
870 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
872 mesh->addMeshBuffer(buf);
876 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
877 u8 li = m_last_light;
878 video::SColor c(255,li,li,li);
879 video::S3DVertex vertices[4] =
881 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
882 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
883 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
884 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
886 u16 indices[] = {0,1,2,2,3,0};
887 buf->append(vertices, 4, indices, 6);
889 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
890 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
891 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
892 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
894 mesh->addMeshBuffer(buf);
897 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
900 // Set it to use the materials of the meshbuffers directly.
901 // This is needed for changing the texture in the future
902 m_meshnode->setReadOnlyMaterials(true);
904 else if(m_prop.visual == "cube") {
905 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
906 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
907 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
911 m_meshnode->setScale(v3f(m_prop.visual_size.X,
912 m_prop.visual_size.Y,
913 m_prop.visual_size.X));
914 u8 li = m_last_light;
915 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
917 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
918 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
919 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
920 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
922 else if(m_prop.visual == "mesh") {
923 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
924 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
927 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
928 m_animated_meshnode->grab();
929 mesh->drop(); // The scene node took hold of it
930 m_animated_meshnode->animateJoints(); // Needed for some animations
931 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
932 m_prop.visual_size.Y,
933 m_prop.visual_size.X));
934 u8 li = m_last_light;
935 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
937 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
938 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
939 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
940 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
943 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
945 else if(m_prop.visual == "wielditem") {
946 infostream<<"GenericCAO::addToScene(): wielditem"<<std::endl;
947 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
948 if(m_prop.textures.size() >= 1){
949 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
950 IItemDefManager *idef = m_gamedef->idef();
951 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
953 m_wield_meshnode = new WieldMeshSceneNode(
954 smgr->getRootSceneNode(), smgr, -1);
955 m_wield_meshnode->setItem(item, m_gamedef);
957 m_wield_meshnode->setScale(v3f(m_prop.visual_size.X/2,
958 m_prop.visual_size.Y/2,
959 m_prop.visual_size.X/2));
960 u8 li = m_last_light;
961 m_wield_meshnode->setColor(video::SColor(255,li,li,li));
964 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
965 <<"\" not supported"<<std::endl;
969 scene::ISceneNode *node = getSceneNode();
970 if (node && m_is_player && !m_is_local_player) {
971 // Add a text node for showing the name
972 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
973 std::wstring wname = narrow_to_wide(m_name);
974 m_textnode = smgr->addTextSceneNode(gui->getSkin()->getFont(),
975 wname.c_str(), m_nametag_color, node);
977 m_textnode->setPosition(v3f(0, BS*1.1, 0));
982 updateBonePosition();
986 void GenericCAO::updateLight(u8 light_at_pos)
988 u8 li = decode_light(light_at_pos);
989 if(li != m_last_light)
992 video::SColor color(255,li,li,li);
994 setMeshColor(m_meshnode->getMesh(), color);
995 if(m_animated_meshnode)
996 setMeshColor(m_animated_meshnode->getMesh(), color);
998 m_wield_meshnode->setColor(color);
1000 m_spritenode->setColor(color);
1004 v3s16 GenericCAO::getLightPosition()
1006 return floatToInt(m_position, BS);
1009 void GenericCAO::updateNodePos()
1011 if (getParent() != NULL)
1014 scene::ISceneNode *node = getSceneNode();
1017 v3s16 camera_offset = m_env->getCameraOffset();
1018 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
1019 if (node != m_spritenode) { // rotate if not a sprite
1020 v3f rot = node->getRotation();
1022 node->setRotation(rot);
1027 void GenericCAO::step(float dtime, ClientEnvironment *env)
1029 // Handel model of local player instantly to prevent lags
1030 if(m_is_local_player)
1032 LocalPlayer *player = m_env->getLocalPlayer();
1036 int old_anim = player->last_animation;
1037 float old_anim_speed = player->last_animation_speed;
1038 m_position = player->getPosition() + v3f(0,BS,0);
1039 m_velocity = v3f(0,0,0);
1040 m_acceleration = v3f(0,0,0);
1041 pos_translator.vect_show = m_position;
1042 m_yaw = player->getYaw();
1043 PlayerControl controls = player->getPlayerControl();
1045 bool walking = false;
1046 if(controls.up || controls.down || controls.left || controls.right)
1049 f32 new_speed = player->local_animation_speed;
1050 v2s32 new_anim = v2s32(0,0);
1051 bool allow_update = false;
1053 // increase speed if using fast or flying fast
1054 if((g_settings->getBool("fast_move") &&
1055 m_gamedef->checkLocalPrivilege("fast")) &&
1057 (!player->touching_ground &&
1058 g_settings->getBool("free_move") &&
1059 m_gamedef->checkLocalPrivilege("fly"))))
1061 // slowdown speed if sneeking
1062 if(controls.sneak && walking)
1065 if(walking && (controls.LMB || controls.RMB))
1067 new_anim = player->local_animations[3];
1068 player->last_animation = WD_ANIM;
1069 } else if(walking) {
1070 new_anim = player->local_animations[1];
1071 player->last_animation = WALK_ANIM;
1072 } else if(controls.LMB || controls.RMB) {
1073 new_anim = player->local_animations[2];
1074 player->last_animation = DIG_ANIM;
1077 // Apply animations if input detected and not attached
1078 // or set idle animation
1079 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1081 allow_update = true;
1082 m_animation_range = new_anim;
1083 m_animation_speed = new_speed;
1084 player->last_animation_speed = m_animation_speed;
1086 player->last_animation = NO_ANIM;
1088 if (old_anim != NO_ANIM)
1090 m_animation_range = player->local_animations[0];
1095 // Update local player animations
1096 if ((player->last_animation != old_anim ||
1097 m_animation_speed != old_anim_speed) &&
1098 player->last_animation != NO_ANIM && allow_update)
1104 if(m_visuals_expired && m_smgr && m_irr){
1105 m_visuals_expired = false;
1107 // Attachments, part 1: All attached objects must be unparented first,
1108 // or Irrlicht causes a segmentation fault
1109 for(std::vector<u16>::iterator ci = m_children.begin();
1110 ci != m_children.end();)
1112 if (m_env->attachement_parent_ids[*ci] != getId()) {
1113 ci = m_children.erase(ci);
1116 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1118 scene::ISceneNode *child_node = obj->getSceneNode();
1120 child_node->setParent(m_smgr->getRootSceneNode());
1125 removeFromScene(false);
1126 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1128 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1129 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1130 // Get the object of the child
1131 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1133 obj->setAttachments();
1137 // Make sure m_is_visible is always applied
1138 scene::ISceneNode *node = getSceneNode();
1140 node->setVisible(m_is_visible);
1142 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1144 // Set these for later
1145 m_position = getPosition();
1146 m_velocity = v3f(0,0,0);
1147 m_acceleration = v3f(0,0,0);
1148 pos_translator.vect_show = m_position;
1150 if(m_is_local_player) // Update local player attachment position
1152 LocalPlayer *player = m_env->getLocalPlayer();
1153 player->overridePosition = getParent()->getPosition();
1154 m_env->getLocalPlayer()->parent = getParent();
1157 v3f lastpos = pos_translator.vect_show;
1161 core::aabbox3d<f32> box = m_prop.collisionbox;
1164 collisionMoveResult moveresult;
1165 f32 pos_max_d = BS*0.125; // Distance per iteration
1166 v3f p_pos = m_position;
1167 v3f p_velocity = m_velocity;
1168 v3f p_acceleration = m_acceleration;
1169 moveresult = collisionMoveSimple(env,env->getGameDef(),
1170 pos_max_d, box, m_prop.stepheight, dtime,
1171 p_pos, p_velocity, p_acceleration,
1172 this, m_prop.collideWithObjects);
1175 m_velocity = p_velocity;
1176 m_acceleration = p_acceleration;
1178 bool is_end_position = moveresult.collides;
1179 pos_translator.update(m_position, is_end_position, dtime);
1180 pos_translator.translate(dtime);
1183 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1184 m_velocity += dtime * m_acceleration;
1185 pos_translator.update(m_position, pos_translator.aim_is_end,
1186 pos_translator.anim_time);
1187 pos_translator.translate(dtime);
1191 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1192 m_step_distance_counter += moved;
1193 if(m_step_distance_counter > 1.5*BS)
1195 m_step_distance_counter = 0;
1196 if(!m_is_local_player && m_prop.makes_footstep_sound)
1198 INodeDefManager *ndef = m_gamedef->ndef();
1199 v3s16 p = floatToInt(getPosition() + v3f(0,
1200 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1201 MapNode n = m_env->getMap().getNodeNoEx(p);
1202 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1203 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1208 m_anim_timer += dtime;
1209 if(m_anim_timer >= m_anim_framelength)
1211 m_anim_timer -= m_anim_framelength;
1213 if(m_anim_frame >= m_anim_num_frames)
1219 if(m_reset_textures_timer >= 0)
1221 m_reset_textures_timer -= dtime;
1222 if(m_reset_textures_timer <= 0){
1223 m_reset_textures_timer = -1;
1227 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1229 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1233 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1234 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1236 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1237 + m_prop.automatic_face_movement_dir_offset;
1242 void GenericCAO::updateTexturePos()
1246 scene::ICameraSceneNode* camera =
1247 m_spritenode->getSceneManager()->getActiveCamera();
1250 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1251 - camera->getAbsolutePosition();
1252 cam_to_entity.normalize();
1254 int row = m_tx_basepos.Y;
1255 int col = m_tx_basepos.X;
1257 if(m_tx_select_horiz_by_yawpitch)
1259 if(cam_to_entity.Y > 0.75)
1261 else if(cam_to_entity.Y < -0.75)
1265 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1266 float dir = mob_dir - m_yaw;
1267 dir = wrapDegrees_180(dir);
1268 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1269 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1271 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1273 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1275 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1282 // Animation goes downwards
1283 row += m_anim_frame;
1285 float txs = m_tx_size.X;
1286 float tys = m_tx_size.Y;
1287 setBillboardTextureMatrix(m_spritenode,
1288 txs, tys, col, row);
1292 void GenericCAO::updateTextures(const std::string &mod)
1294 ITextureSource *tsrc = m_gamedef->tsrc();
1296 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1297 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1298 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1302 if(m_prop.visual == "sprite")
1304 std::string texturestring = "unknown_node.png";
1305 if(m_prop.textures.size() >= 1)
1306 texturestring = m_prop.textures[0];
1307 texturestring += mod;
1308 m_spritenode->setMaterialTexture(0,
1309 tsrc->getTextureForMesh(texturestring));
1311 // This allows setting per-material colors. However, until a real lighting
1312 // system is added, the code below will have no effect. Once MineTest
1313 // has directional lighting, it should work automatically.
1314 if(m_prop.colors.size() >= 1)
1316 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1317 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1318 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1321 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1322 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1323 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1326 if(m_animated_meshnode)
1328 if(m_prop.visual == "mesh")
1330 for (u32 i = 0; i < m_prop.textures.size() &&
1331 i < m_animated_meshnode->getMaterialCount(); ++i)
1333 std::string texturestring = m_prop.textures[i];
1334 if(texturestring == "")
1335 continue; // Empty texture string means don't modify that material
1336 texturestring += mod;
1337 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1340 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1344 // Set material flags and texture
1345 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1346 material.TextureLayer[0].Texture = texture;
1347 material.setFlag(video::EMF_LIGHTING, false);
1348 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1350 m_animated_meshnode->getMaterial(i)
1351 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1352 m_animated_meshnode->getMaterial(i)
1353 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1354 m_animated_meshnode->getMaterial(i)
1355 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1357 for (u32 i = 0; i < m_prop.colors.size() &&
1358 i < m_animated_meshnode->getMaterialCount(); ++i)
1360 // This allows setting per-material colors. However, until a real lighting
1361 // system is added, the code below will have no effect. Once MineTest
1362 // has directional lighting, it should work automatically.
1363 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1364 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1365 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1371 if(m_prop.visual == "cube")
1373 for (u32 i = 0; i < 6; ++i)
1375 std::string texturestring = "unknown_node.png";
1376 if(m_prop.textures.size() > i)
1377 texturestring = m_prop.textures[i];
1378 texturestring += mod;
1381 // Set material flags and texture
1382 video::SMaterial& material = m_meshnode->getMaterial(i);
1383 material.setFlag(video::EMF_LIGHTING, false);
1384 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1385 material.setTexture(0,
1386 tsrc->getTextureForMesh(texturestring));
1387 material.getTextureMatrix(0).makeIdentity();
1389 // This allows setting per-material colors. However, until a real lighting
1390 // system is added, the code below will have no effect. Once MineTest
1391 // has directional lighting, it should work automatically.
1392 if(m_prop.colors.size() > i)
1394 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1395 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1396 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1399 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1400 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1401 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1404 else if(m_prop.visual == "upright_sprite")
1406 scene::IMesh *mesh = m_meshnode->getMesh();
1408 std::string tname = "unknown_object.png";
1409 if(m_prop.textures.size() >= 1)
1410 tname = m_prop.textures[0];
1412 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1413 buf->getMaterial().setTexture(0,
1414 tsrc->getTextureForMesh(tname));
1416 // This allows setting per-material colors. However, until a real lighting
1417 // system is added, the code below will have no effect. Once MineTest
1418 // has directional lighting, it should work automatically.
1419 if(m_prop.colors.size() >= 1)
1421 buf->getMaterial().AmbientColor = m_prop.colors[0];
1422 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1423 buf->getMaterial().SpecularColor = m_prop.colors[0];
1426 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1427 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1428 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1431 std::string tname = "unknown_object.png";
1432 if(m_prop.textures.size() >= 2)
1433 tname = m_prop.textures[1];
1434 else if(m_prop.textures.size() >= 1)
1435 tname = m_prop.textures[0];
1437 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1438 buf->getMaterial().setTexture(0,
1439 tsrc->getTextureForMesh(tname));
1441 // This allows setting per-material colors. However, until a real lighting
1442 // system is added, the code below will have no effect. Once MineTest
1443 // has directional lighting, it should work automatically.
1444 if(m_prop.colors.size() >= 2)
1446 buf->getMaterial().AmbientColor = m_prop.colors[1];
1447 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1448 buf->getMaterial().SpecularColor = m_prop.colors[1];
1450 else if(m_prop.colors.size() >= 1)
1452 buf->getMaterial().AmbientColor = m_prop.colors[0];
1453 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1454 buf->getMaterial().SpecularColor = m_prop.colors[0];
1457 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1458 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1459 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1465 void GenericCAO::updateAnimation()
1467 if(m_animated_meshnode == NULL)
1470 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1471 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1472 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1473 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1474 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1475 m_animated_meshnode->setTransitionTime(m_animation_blend);
1476 // Requires Irrlicht 1.8 or greater
1477 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1478 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1479 m_animated_meshnode->setLoopMode(m_animation_loop);
1483 void GenericCAO::updateBonePosition()
1485 if(m_bone_position.empty() || m_animated_meshnode == NULL)
1488 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1489 for(std::map<std::string,
1490 core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1491 ii != m_bone_position.end(); ++ii)
1493 std::string bone_name = (*ii).first;
1494 v3f bone_pos = (*ii).second.X;
1495 v3f bone_rot = (*ii).second.Y;
1496 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1499 bone->setPosition(bone_pos);
1500 bone->setRotation(bone_rot);
1505 void GenericCAO::updateAttachments()
1508 if (getParent() == NULL) { // Detach or don't attach
1509 scene::ISceneNode *node = getSceneNode();
1511 v3f old_position = node->getAbsolutePosition();
1512 v3f old_rotation = node->getRotation();
1513 node->setParent(m_smgr->getRootSceneNode());
1514 node->setPosition(old_position);
1515 node->setRotation(old_rotation);
1516 node->updateAbsolutePosition();
1518 if (m_is_local_player) {
1519 LocalPlayer *player = m_env->getLocalPlayer();
1520 player->isAttached = false;
1525 scene::ISceneNode *my_node = getSceneNode();
1527 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1528 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1529 getParent()->getAnimatedMeshSceneNode();
1530 if (parent_animated_mesh_node && m_attachment_bone != "") {
1531 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1534 if (my_node && parent_node) {
1535 my_node->setParent(parent_node);
1536 my_node->setPosition(m_attachment_position);
1537 my_node->setRotation(m_attachment_rotation);
1538 my_node->updateAbsolutePosition();
1540 if (m_is_local_player) {
1541 LocalPlayer *player = m_env->getLocalPlayer();
1542 player->isAttached = true;
1547 void GenericCAO::processMessage(const std::string &data)
1549 //infostream<<"GenericCAO: Got message"<<std::endl;
1550 std::istringstream is(data, std::ios::binary);
1552 u8 cmd = readU8(is);
1553 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1555 m_prop = gob_read_set_properties(is);
1557 m_selection_box = m_prop.collisionbox;
1558 m_selection_box.MinEdge *= BS;
1559 m_selection_box.MaxEdge *= BS;
1561 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1562 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1564 if(!m_initial_tx_basepos_set){
1565 m_initial_tx_basepos_set = true;
1566 m_tx_basepos = m_prop.initial_sprite_basepos;
1571 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1573 // Not sent by the server if this object is an attachment.
1574 // We might however get here if the server notices the object being detached before the client.
1575 m_position = readV3F1000(is);
1576 m_velocity = readV3F1000(is);
1577 m_acceleration = readV3F1000(is);
1578 if(fabs(m_prop.automatic_rotate) < 0.001)
1579 m_yaw = readF1000(is);
1582 bool do_interpolate = readU8(is);
1583 bool is_end_position = readU8(is);
1584 float update_interval = readF1000(is);
1586 // Place us a bit higher if we're physical, to not sink into
1587 // the ground due to sucky collision detection...
1589 m_position += v3f(0,0.002,0);
1591 if(getParent() != NULL) // Just in case
1596 if(!m_prop.physical)
1597 pos_translator.update(m_position, is_end_position, update_interval);
1599 pos_translator.init(m_position);
1603 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1604 std::string mod = deSerializeString(is);
1605 updateTextures(mod);
1607 else if(cmd == GENERIC_CMD_SET_SPRITE) {
1608 v2s16 p = readV2S16(is);
1609 int num_frames = readU16(is);
1610 float framelength = readF1000(is);
1611 bool select_horiz_by_yawpitch = readU8(is);
1614 m_anim_num_frames = num_frames;
1615 m_anim_framelength = framelength;
1616 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1620 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1621 float override_speed = readF1000(is);
1622 float override_jump = readF1000(is);
1623 float override_gravity = readF1000(is);
1624 // these are sent inverted so we get true when the server sends nothing
1625 bool sneak = !readU8(is);
1626 bool sneak_glitch = !readU8(is);
1629 if(m_is_local_player)
1631 LocalPlayer *player = m_env->getLocalPlayer();
1632 player->physics_override_speed = override_speed;
1633 player->physics_override_jump = override_jump;
1634 player->physics_override_gravity = override_gravity;
1635 player->physics_override_sneak = sneak;
1636 player->physics_override_sneak_glitch = sneak_glitch;
1639 else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1640 // TODO: change frames send as v2s32 value
1641 v2f range = readV2F1000(is);
1642 if (!m_is_local_player) {
1643 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1644 m_animation_speed = readF1000(is);
1645 m_animation_blend = readF1000(is);
1646 // these are sent inverted so we get true when the server sends nothing
1647 m_animation_loop = !readU8(is);
1650 LocalPlayer *player = m_env->getLocalPlayer();
1651 if(player->last_animation == NO_ANIM)
1653 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1654 m_animation_speed = readF1000(is);
1655 m_animation_blend = readF1000(is);
1656 // these are sent inverted so we get true when the server sends nothing
1657 m_animation_loop = !readU8(is);
1659 // update animation only if local animations present
1660 // and received animation is unknown (except idle animation)
1661 bool is_known = false;
1662 for (int i = 1;i<4;i++)
1664 if(m_animation_range.Y == player->local_animations[i].Y)
1668 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1674 else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1675 std::string bone = deSerializeString(is);
1676 v3f position = readV3F1000(is);
1677 v3f rotation = readV3F1000(is);
1678 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1680 updateBonePosition();
1681 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1682 u16 parentID = readS16(is);
1683 u16 oldparent = m_env->attachement_parent_ids[getId()];
1685 m_children.erase(std::remove(m_children.begin(), m_children.end(),
1686 getId()), m_children.end());
1688 m_env->attachement_parent_ids[getId()] = parentID;
1689 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1692 parentobj->m_children.push_back(getId());
1695 m_attachment_bone = deSerializeString(is);
1696 m_attachment_position = readV3F1000(is);
1697 m_attachment_rotation = readV3F1000(is);
1699 // localplayer itself can't be attached to localplayer
1700 if (!m_is_local_player) {
1701 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1702 // Objects attached to the local player should be hidden by default
1703 m_is_visible = !m_attached_to_local;
1706 updateAttachments();
1708 else if(cmd == GENERIC_CMD_PUNCHED) {
1709 /*s16 damage =*/ readS16(is);
1710 s16 result_hp = readS16(is);
1712 // Use this instead of the send damage to not interfere with prediction
1713 s16 damage = m_hp - result_hp;
1721 // TODO: Execute defined fast response
1722 // As there is no definition, make a smoke puff
1723 ClientSimpleObject *simple = createSmokePuff(
1724 m_smgr, m_env, m_position,
1725 m_prop.visual_size * BS);
1726 m_env->addSimpleObject(simple);
1728 // TODO: Execute defined fast response
1729 // Flashing shall suffice as there is no definition
1730 m_reset_textures_timer = 0.05;
1732 m_reset_textures_timer += 0.05 * damage;
1733 updateTextures("^[brighten");
1737 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1738 m_armor_groups.clear();
1739 int armor_groups_size = readU16(is);
1740 for(int i=0; i<armor_groups_size; i++)
1742 std::string name = deSerializeString(is);
1743 int rating = readS16(is);
1744 m_armor_groups[name] = rating;
1746 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1747 readU8(is); // version
1748 m_nametag_color = readARGB8(is);
1749 if (m_textnode != NULL) {
1750 m_textnode->setTextColor(m_nametag_color);
1752 // Enforce hiding nametag,
1753 // because if freetype is enabled, a grey
1754 // shadow can remain.
1755 m_textnode->setVisible(m_nametag_color.getAlpha() > 0);
1760 /* \pre punchitem != NULL
1762 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1763 float time_from_last_punch)
1765 assert(punchitem); // pre-condition
1766 const ToolCapabilities *toolcap =
1767 &punchitem->getToolCapabilities(m_gamedef->idef());
1768 PunchDamageResult result = getPunchDamage(
1772 time_from_last_punch);
1774 if(result.did_punch && result.damage != 0)
1776 if(result.damage < m_hp)
1778 m_hp -= result.damage;
1781 // TODO: Execute defined fast response
1782 // As there is no definition, make a smoke puff
1783 ClientSimpleObject *simple = createSmokePuff(
1784 m_smgr, m_env, m_position,
1785 m_prop.visual_size * BS);
1786 m_env->addSimpleObject(simple);
1788 // TODO: Execute defined fast response
1789 // Flashing shall suffice as there is no definition
1790 m_reset_textures_timer = 0.05;
1791 if(result.damage >= 2)
1792 m_reset_textures_timer += 0.05 * result.damage;
1793 updateTextures("^[brighten");
1799 std::string GenericCAO::debugInfoText()
1801 std::ostringstream os(std::ios::binary);
1802 os<<"GenericCAO hp="<<m_hp<<"\n";
1804 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1805 i != m_armor_groups.end(); i++)
1807 os<<i->first<<"="<<i->second<<", ";
1814 GenericCAO proto_GenericCAO(NULL, NULL);