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),
550 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
552 m_animated_meshnode(NULL),
553 m_wield_meshnode(NULL),
555 m_nametag_color(video::SColor(255, 255, 255, 255)),
557 m_position(v3f(0,10*BS,0)),
558 m_velocity(v3f(0,0,0)),
559 m_acceleration(v3f(0,0,0)),
564 m_initial_tx_basepos_set(false),
565 m_tx_select_horiz_by_yawpitch(false),
566 m_animation_range(v2s32(0,0)),
567 m_animation_speed(15),
568 m_animation_blend(0),
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>::iterator ci = m_children.begin();
733 ci != m_children.end(); ci++) {
734 GenericCAO *obj = m_env->getGenericCAO(*ci);
736 obj->setVisible(toset);
741 void GenericCAO::setAttachments()
746 ClientActiveObject* GenericCAO::getParent()
748 ClientActiveObject *obj = NULL;
750 u16 attached_id = m_env->m_attachements[getId()];
752 if ((attached_id != 0) &&
753 (attached_id != getId())) {
754 obj = m_env->getActiveObject(attached_id);
759 void GenericCAO::removeFromScene(bool permanent)
761 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
762 if((m_env != NULL) && (permanent))
764 for(std::vector<u16>::iterator ci = m_children.begin();
765 ci != m_children.end(); ci++)
767 if (m_env->m_attachements[*ci] == getId()) {
768 m_env->m_attachements[*ci] = 0;
772 m_env->m_attachements[getId()] = 0;
774 LocalPlayer* player = m_env->getLocalPlayer();
775 if (this == player->parent) {
776 player->parent = NULL;
777 player->isAttached = false;
783 m_meshnode->remove();
787 if(m_animated_meshnode)
789 m_animated_meshnode->remove();
790 m_animated_meshnode->drop();
791 m_animated_meshnode = NULL;
795 m_wield_meshnode->remove();
796 m_wield_meshnode->drop();
797 m_wield_meshnode = NULL;
801 m_spritenode->remove();
802 m_spritenode->drop();
807 m_textnode->remove();
813 void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
819 if (getSceneNode() != NULL)
822 m_visuals_expired = false;
824 if(!m_prop.is_visible)
827 //video::IVideoDriver* driver = smgr->getVideoDriver();
829 if(m_prop.visual == "sprite")
831 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
832 m_spritenode = smgr->addBillboardSceneNode(
833 NULL, v2f(1, 1), v3f(0,0,0), -1);
834 m_spritenode->grab();
835 m_spritenode->setMaterialTexture(0,
836 tsrc->getTextureForMesh("unknown_node.png"));
837 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
838 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
839 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
840 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
841 u8 li = m_last_light;
842 m_spritenode->setColor(video::SColor(255,li,li,li));
843 m_spritenode->setSize(m_prop.visual_size*BS);
845 const float txs = 1.0 / 1;
846 const float tys = 1.0 / 1;
847 setBillboardTextureMatrix(m_spritenode,
851 else if(m_prop.visual == "upright_sprite") {
852 scene::SMesh *mesh = new scene::SMesh();
853 double dx = BS*m_prop.visual_size.X/2;
854 double dy = BS*m_prop.visual_size.Y/2;
856 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
857 u8 li = m_last_light;
858 video::SColor c(255,li,li,li);
859 video::S3DVertex vertices[4] =
861 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
862 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
863 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
864 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
866 u16 indices[] = {0,1,2,2,3,0};
867 buf->append(vertices, 4, indices, 6);
869 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
870 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
871 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
872 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
874 mesh->addMeshBuffer(buf);
878 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
879 u8 li = m_last_light;
880 video::SColor c(255,li,li,li);
881 video::S3DVertex vertices[4] =
883 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
884 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
885 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
886 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
888 u16 indices[] = {0,1,2,2,3,0};
889 buf->append(vertices, 4, indices, 6);
891 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
892 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
893 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
894 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
896 mesh->addMeshBuffer(buf);
899 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
902 // Set it to use the materials of the meshbuffers directly.
903 // This is needed for changing the texture in the future
904 m_meshnode->setReadOnlyMaterials(true);
906 else if(m_prop.visual == "cube") {
907 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
908 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
909 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
913 m_meshnode->setScale(v3f(m_prop.visual_size.X,
914 m_prop.visual_size.Y,
915 m_prop.visual_size.X));
916 u8 li = m_last_light;
917 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
919 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
920 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
921 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
922 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
924 else if(m_prop.visual == "mesh") {
925 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
926 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
929 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
930 m_animated_meshnode->grab();
931 mesh->drop(); // The scene node took hold of it
932 m_animated_meshnode->animateJoints(); // Needed for some animations
933 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
934 m_prop.visual_size.Y,
935 m_prop.visual_size.X));
936 u8 li = m_last_light;
937 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
939 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
940 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
941 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
942 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
945 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
947 else if(m_prop.visual == "wielditem") {
948 infostream<<"GenericCAO::addToScene(): wielditem"<<std::endl;
949 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
950 if(m_prop.textures.size() >= 1){
951 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
952 IItemDefManager *idef = m_gamedef->idef();
953 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
955 m_wield_meshnode = new WieldMeshSceneNode(
956 smgr->getRootSceneNode(), smgr, -1);
957 m_wield_meshnode->setItem(item, m_gamedef);
959 m_wield_meshnode->setScale(v3f(m_prop.visual_size.X/2,
960 m_prop.visual_size.Y/2,
961 m_prop.visual_size.X/2));
962 u8 li = m_last_light;
963 m_wield_meshnode->setColor(video::SColor(255,li,li,li));
966 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
967 <<"\" not supported"<<std::endl;
971 scene::ISceneNode *node = getSceneNode();
972 if (node && m_is_player && !m_is_local_player) {
973 // Add a text node for showing the name
974 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
975 std::wstring wname = narrow_to_wide(m_name);
976 m_textnode = smgr->addTextSceneNode(gui->getSkin()->getFont(),
977 wname.c_str(), m_nametag_color, node);
979 m_textnode->setPosition(v3f(0, BS*1.1, 0));
984 updateBonePosition();
988 void GenericCAO::updateLight(u8 light_at_pos)
990 u8 li = decode_light(light_at_pos);
991 if(li != m_last_light)
994 video::SColor color(255,li,li,li);
996 setMeshColor(m_meshnode->getMesh(), color);
997 if(m_animated_meshnode)
998 setMeshColor(m_animated_meshnode->getMesh(), color);
1000 m_wield_meshnode->setColor(color);
1002 m_spritenode->setColor(color);
1006 v3s16 GenericCAO::getLightPosition()
1008 return floatToInt(m_position, BS);
1011 void GenericCAO::updateNodePos()
1013 if (getParent() != NULL)
1016 scene::ISceneNode *node = getSceneNode();
1019 v3s16 camera_offset = m_env->getCameraOffset();
1020 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
1021 if (node != m_spritenode) { // rotate if not a sprite
1022 v3f rot = node->getRotation();
1024 node->setRotation(rot);
1029 void GenericCAO::step(float dtime, ClientEnvironment *env)
1031 // Handel model of local player instantly to prevent lags
1032 if(m_is_local_player)
1034 LocalPlayer *player = m_env->getLocalPlayer();
1038 int old_anim = player->last_animation;
1039 float old_anim_speed = player->last_animation_speed;
1040 m_position = player->getPosition() + v3f(0,BS,0);
1041 m_velocity = v3f(0,0,0);
1042 m_acceleration = v3f(0,0,0);
1043 pos_translator.vect_show = m_position;
1044 m_yaw = player->getYaw();
1045 PlayerControl controls = player->getPlayerControl();
1047 bool walking = false;
1048 if(controls.up || controls.down || controls.left || controls.right)
1051 f32 new_speed = player->local_animation_speed;
1052 v2s32 new_anim = v2s32(0,0);
1053 bool allow_update = false;
1055 // increase speed if using fast or flying fast
1056 if((g_settings->getBool("fast_move") &&
1057 m_gamedef->checkLocalPrivilege("fast")) &&
1059 (!player->touching_ground &&
1060 g_settings->getBool("free_move") &&
1061 m_gamedef->checkLocalPrivilege("fly"))))
1063 // slowdown speed if sneeking
1064 if(controls.sneak && walking)
1067 if(walking && (controls.LMB || controls.RMB))
1069 new_anim = player->local_animations[3];
1070 player->last_animation = WD_ANIM;
1071 } else if(walking) {
1072 new_anim = player->local_animations[1];
1073 player->last_animation = WALK_ANIM;
1074 } else if(controls.LMB || controls.RMB) {
1075 new_anim = player->local_animations[2];
1076 player->last_animation = DIG_ANIM;
1079 // Apply animations if input detected and not attached
1080 // or set idle animation
1081 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1083 allow_update = true;
1084 m_animation_range = new_anim;
1085 m_animation_speed = new_speed;
1086 player->last_animation_speed = m_animation_speed;
1088 player->last_animation = NO_ANIM;
1090 if (old_anim != NO_ANIM)
1092 m_animation_range = player->local_animations[0];
1097 // Update local player animations
1098 if ((player->last_animation != old_anim ||
1099 m_animation_speed != old_anim_speed) &&
1100 player->last_animation != NO_ANIM && allow_update)
1106 if(m_visuals_expired && m_smgr && m_irr){
1107 m_visuals_expired = false;
1109 // Attachments, part 1: All attached objects must be unparented first,
1110 // or Irrlicht causes a segmentation fault
1111 for(std::vector<u16>::iterator ci = m_children.begin();
1112 ci != m_children.end();)
1114 if (m_env->m_attachements[*ci] != getId()) {
1115 ci = m_children.erase(ci);
1118 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1120 scene::ISceneNode *child_node = obj->getSceneNode();
1122 child_node->setParent(m_smgr->getRootSceneNode());
1127 removeFromScene(false);
1128 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1130 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1131 for(std::vector<u16>::iterator ci = m_children.begin();
1132 ci != m_children.end(); ci++)
1134 // Get the object of the child
1135 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1137 obj->setAttachments();
1141 // Make sure m_is_visible is always applied
1142 scene::ISceneNode *node = getSceneNode();
1144 node->setVisible(m_is_visible);
1146 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1148 // Set these for later
1149 m_position = getPosition();
1150 m_velocity = v3f(0,0,0);
1151 m_acceleration = v3f(0,0,0);
1152 pos_translator.vect_show = m_position;
1154 if(m_is_local_player) // Update local player attachment position
1156 LocalPlayer *player = m_env->getLocalPlayer();
1157 player->overridePosition = getParent()->getPosition();
1158 m_env->getLocalPlayer()->parent = getParent();
1161 v3f lastpos = pos_translator.vect_show;
1165 core::aabbox3d<f32> box = m_prop.collisionbox;
1168 collisionMoveResult moveresult;
1169 f32 pos_max_d = BS*0.125; // Distance per iteration
1170 v3f p_pos = m_position;
1171 v3f p_velocity = m_velocity;
1172 v3f p_acceleration = m_acceleration;
1173 moveresult = collisionMoveSimple(env,env->getGameDef(),
1174 pos_max_d, box, m_prop.stepheight, dtime,
1175 p_pos, p_velocity, p_acceleration,
1176 this, m_prop.collideWithObjects);
1179 m_velocity = p_velocity;
1180 m_acceleration = p_acceleration;
1182 bool is_end_position = moveresult.collides;
1183 pos_translator.update(m_position, is_end_position, dtime);
1184 pos_translator.translate(dtime);
1187 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1188 m_velocity += dtime * m_acceleration;
1189 pos_translator.update(m_position, pos_translator.aim_is_end,
1190 pos_translator.anim_time);
1191 pos_translator.translate(dtime);
1195 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1196 m_step_distance_counter += moved;
1197 if(m_step_distance_counter > 1.5*BS)
1199 m_step_distance_counter = 0;
1200 if(!m_is_local_player && m_prop.makes_footstep_sound)
1202 INodeDefManager *ndef = m_gamedef->ndef();
1203 v3s16 p = floatToInt(getPosition() + v3f(0,
1204 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1205 MapNode n = m_env->getMap().getNodeNoEx(p);
1206 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1207 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1212 m_anim_timer += dtime;
1213 if(m_anim_timer >= m_anim_framelength)
1215 m_anim_timer -= m_anim_framelength;
1217 if(m_anim_frame >= m_anim_num_frames)
1223 if(m_reset_textures_timer >= 0)
1225 m_reset_textures_timer -= dtime;
1226 if(m_reset_textures_timer <= 0){
1227 m_reset_textures_timer = -1;
1231 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1233 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1237 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1238 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1240 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1241 + m_prop.automatic_face_movement_dir_offset;
1246 void GenericCAO::updateTexturePos()
1250 scene::ICameraSceneNode* camera =
1251 m_spritenode->getSceneManager()->getActiveCamera();
1254 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1255 - camera->getAbsolutePosition();
1256 cam_to_entity.normalize();
1258 int row = m_tx_basepos.Y;
1259 int col = m_tx_basepos.X;
1261 if(m_tx_select_horiz_by_yawpitch)
1263 if(cam_to_entity.Y > 0.75)
1265 else if(cam_to_entity.Y < -0.75)
1269 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1270 float dir = mob_dir - m_yaw;
1271 dir = wrapDegrees_180(dir);
1272 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1273 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1275 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1277 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1279 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1286 // Animation goes downwards
1287 row += m_anim_frame;
1289 float txs = m_tx_size.X;
1290 float tys = m_tx_size.Y;
1291 setBillboardTextureMatrix(m_spritenode,
1292 txs, tys, col, row);
1296 void GenericCAO::updateTextures(const std::string &mod)
1298 ITextureSource *tsrc = m_gamedef->tsrc();
1300 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1301 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1302 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1306 if(m_prop.visual == "sprite")
1308 std::string texturestring = "unknown_node.png";
1309 if(m_prop.textures.size() >= 1)
1310 texturestring = m_prop.textures[0];
1311 texturestring += mod;
1312 m_spritenode->setMaterialTexture(0,
1313 tsrc->getTextureForMesh(texturestring));
1315 // This allows setting per-material colors. However, until a real lighting
1316 // system is added, the code below will have no effect. Once MineTest
1317 // has directional lighting, it should work automatically.
1318 if(m_prop.colors.size() >= 1)
1320 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1321 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1322 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1325 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1326 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1327 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1330 if(m_animated_meshnode)
1332 if(m_prop.visual == "mesh")
1334 for (u32 i = 0; i < m_prop.textures.size() &&
1335 i < m_animated_meshnode->getMaterialCount(); ++i)
1337 std::string texturestring = m_prop.textures[i];
1338 if(texturestring == "")
1339 continue; // Empty texture string means don't modify that material
1340 texturestring += mod;
1341 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1344 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1348 // Set material flags and texture
1349 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1350 material.TextureLayer[0].Texture = texture;
1351 material.setFlag(video::EMF_LIGHTING, false);
1352 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1354 m_animated_meshnode->getMaterial(i)
1355 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1356 m_animated_meshnode->getMaterial(i)
1357 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1358 m_animated_meshnode->getMaterial(i)
1359 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1361 for (u32 i = 0; i < m_prop.colors.size() &&
1362 i < m_animated_meshnode->getMaterialCount(); ++i)
1364 // This allows setting per-material colors. However, until a real lighting
1365 // system is added, the code below will have no effect. Once MineTest
1366 // has directional lighting, it should work automatically.
1367 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1368 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1369 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1375 if(m_prop.visual == "cube")
1377 for (u32 i = 0; i < 6; ++i)
1379 std::string texturestring = "unknown_node.png";
1380 if(m_prop.textures.size() > i)
1381 texturestring = m_prop.textures[i];
1382 texturestring += mod;
1385 // Set material flags and texture
1386 video::SMaterial& material = m_meshnode->getMaterial(i);
1387 material.setFlag(video::EMF_LIGHTING, false);
1388 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1389 material.setTexture(0,
1390 tsrc->getTextureForMesh(texturestring));
1391 material.getTextureMatrix(0).makeIdentity();
1393 // This allows setting per-material colors. However, until a real lighting
1394 // system is added, the code below will have no effect. Once MineTest
1395 // has directional lighting, it should work automatically.
1396 if(m_prop.colors.size() > i)
1398 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1399 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1400 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1403 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1404 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1405 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1408 else if(m_prop.visual == "upright_sprite")
1410 scene::IMesh *mesh = m_meshnode->getMesh();
1412 std::string tname = "unknown_object.png";
1413 if(m_prop.textures.size() >= 1)
1414 tname = m_prop.textures[0];
1416 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1417 buf->getMaterial().setTexture(0,
1418 tsrc->getTextureForMesh(tname));
1420 // This allows setting per-material colors. However, until a real lighting
1421 // system is added, the code below will have no effect. Once MineTest
1422 // has directional lighting, it should work automatically.
1423 if(m_prop.colors.size() >= 1)
1425 buf->getMaterial().AmbientColor = m_prop.colors[0];
1426 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1427 buf->getMaterial().SpecularColor = m_prop.colors[0];
1430 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1431 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1432 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1435 std::string tname = "unknown_object.png";
1436 if(m_prop.textures.size() >= 2)
1437 tname = m_prop.textures[1];
1438 else if(m_prop.textures.size() >= 1)
1439 tname = m_prop.textures[0];
1441 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1442 buf->getMaterial().setTexture(0,
1443 tsrc->getTextureForMesh(tname));
1445 // This allows setting per-material colors. However, until a real lighting
1446 // system is added, the code below will have no effect. Once MineTest
1447 // has directional lighting, it should work automatically.
1448 if(m_prop.colors.size() >= 2)
1450 buf->getMaterial().AmbientColor = m_prop.colors[1];
1451 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1452 buf->getMaterial().SpecularColor = m_prop.colors[1];
1454 else if(m_prop.colors.size() >= 1)
1456 buf->getMaterial().AmbientColor = m_prop.colors[0];
1457 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1458 buf->getMaterial().SpecularColor = m_prop.colors[0];
1461 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1462 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1463 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1469 void GenericCAO::updateAnimation()
1471 if(m_animated_meshnode == NULL)
1473 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1474 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1475 m_animated_meshnode->setTransitionTime(m_animation_blend);
1478 void GenericCAO::updateBonePosition()
1480 if(m_bone_position.empty() || m_animated_meshnode == NULL)
1483 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1484 for(std::map<std::string,
1485 core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1486 ii != m_bone_position.end(); ++ii)
1488 std::string bone_name = (*ii).first;
1489 v3f bone_pos = (*ii).second.X;
1490 v3f bone_rot = (*ii).second.Y;
1491 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1494 bone->setPosition(bone_pos);
1495 bone->setRotation(bone_rot);
1500 void GenericCAO::updateAttachments()
1503 if (getParent() == NULL) { // Detach or don't attach
1504 scene::ISceneNode *node = getSceneNode();
1506 v3f old_position = node->getAbsolutePosition();
1507 v3f old_rotation = node->getRotation();
1508 node->setParent(m_smgr->getRootSceneNode());
1509 node->setPosition(old_position);
1510 node->setRotation(old_rotation);
1511 node->updateAbsolutePosition();
1513 if (m_is_local_player) {
1514 LocalPlayer *player = m_env->getLocalPlayer();
1515 player->isAttached = false;
1520 scene::ISceneNode *my_node = getSceneNode();
1522 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1523 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1524 getParent()->getAnimatedMeshSceneNode();
1525 if (parent_animated_mesh_node && m_attachment_bone != "") {
1526 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1529 if (my_node && parent_node) {
1530 my_node->setParent(parent_node);
1531 my_node->setPosition(m_attachment_position);
1532 my_node->setRotation(m_attachment_rotation);
1533 my_node->updateAbsolutePosition();
1535 if (m_is_local_player) {
1536 LocalPlayer *player = m_env->getLocalPlayer();
1537 player->isAttached = true;
1542 void GenericCAO::processMessage(const std::string &data)
1544 //infostream<<"GenericCAO: Got message"<<std::endl;
1545 std::istringstream is(data, std::ios::binary);
1547 u8 cmd = readU8(is);
1548 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1550 m_prop = gob_read_set_properties(is);
1552 m_selection_box = m_prop.collisionbox;
1553 m_selection_box.MinEdge *= BS;
1554 m_selection_box.MaxEdge *= BS;
1556 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1557 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1559 if(!m_initial_tx_basepos_set){
1560 m_initial_tx_basepos_set = true;
1561 m_tx_basepos = m_prop.initial_sprite_basepos;
1566 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1568 // Not sent by the server if this object is an attachment.
1569 // We might however get here if the server notices the object being detached before the client.
1570 m_position = readV3F1000(is);
1571 m_velocity = readV3F1000(is);
1572 m_acceleration = readV3F1000(is);
1573 if(fabs(m_prop.automatic_rotate) < 0.001)
1574 m_yaw = readF1000(is);
1577 bool do_interpolate = readU8(is);
1578 bool is_end_position = readU8(is);
1579 float update_interval = readF1000(is);
1581 // Place us a bit higher if we're physical, to not sink into
1582 // the ground due to sucky collision detection...
1584 m_position += v3f(0,0.002,0);
1586 if(getParent() != NULL) // Just in case
1591 if(!m_prop.physical)
1592 pos_translator.update(m_position, is_end_position, update_interval);
1594 pos_translator.init(m_position);
1598 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1599 std::string mod = deSerializeString(is);
1600 updateTextures(mod);
1602 else if(cmd == GENERIC_CMD_SET_SPRITE) {
1603 v2s16 p = readV2S16(is);
1604 int num_frames = readU16(is);
1605 float framelength = readF1000(is);
1606 bool select_horiz_by_yawpitch = readU8(is);
1609 m_anim_num_frames = num_frames;
1610 m_anim_framelength = framelength;
1611 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1615 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1616 float override_speed = readF1000(is);
1617 float override_jump = readF1000(is);
1618 float override_gravity = readF1000(is);
1619 // these are sent inverted so we get true when the server sends nothing
1620 bool sneak = !readU8(is);
1621 bool sneak_glitch = !readU8(is);
1624 if(m_is_local_player)
1626 LocalPlayer *player = m_env->getLocalPlayer();
1627 player->physics_override_speed = override_speed;
1628 player->physics_override_jump = override_jump;
1629 player->physics_override_gravity = override_gravity;
1630 player->physics_override_sneak = sneak;
1631 player->physics_override_sneak_glitch = sneak_glitch;
1634 else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1635 // TODO: change frames send as v2s32 value
1636 v2f range = readV2F1000(is);
1637 if (!m_is_local_player) {
1638 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1639 m_animation_speed = readF1000(is);
1640 m_animation_blend = readF1000(is);
1643 LocalPlayer *player = m_env->getLocalPlayer();
1644 if(player->last_animation == NO_ANIM)
1646 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1647 m_animation_speed = readF1000(is);
1648 m_animation_blend = readF1000(is);
1650 // update animation only if local animations present
1651 // and received animation is unknown (except idle animation)
1652 bool is_known = false;
1653 for (int i = 1;i<4;i++)
1655 if(m_animation_range.Y == player->local_animations[i].Y)
1659 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1665 else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1666 std::string bone = deSerializeString(is);
1667 v3f position = readV3F1000(is);
1668 v3f rotation = readV3F1000(is);
1669 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1671 updateBonePosition();
1672 } else if (cmd == GENERIC_CMD_SET_ATTACHMENT) {
1673 u16 parentID = readS16(is);
1674 m_env->m_attachements[getId()] = parentID;
1675 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1678 parentobj->m_children.push_back(getId());
1681 m_attachment_bone = deSerializeString(is);
1682 m_attachment_position = readV3F1000(is);
1683 m_attachment_rotation = readV3F1000(is);
1685 // localplayer itself can't be attached to localplayer
1686 if (!m_is_local_player) {
1687 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1688 // Objects attached to the local player should be hidden by default
1689 m_is_visible = !m_attached_to_local;
1692 updateAttachments();
1694 else if(cmd == GENERIC_CMD_PUNCHED) {
1695 /*s16 damage =*/ readS16(is);
1696 s16 result_hp = readS16(is);
1698 // Use this instead of the send damage to not interfere with prediction
1699 s16 damage = m_hp - result_hp;
1707 // TODO: Execute defined fast response
1708 // As there is no definition, make a smoke puff
1709 ClientSimpleObject *simple = createSmokePuff(
1710 m_smgr, m_env, m_position,
1711 m_prop.visual_size * BS);
1712 m_env->addSimpleObject(simple);
1714 // TODO: Execute defined fast response
1715 // Flashing shall suffice as there is no definition
1716 m_reset_textures_timer = 0.05;
1718 m_reset_textures_timer += 0.05 * damage;
1719 updateTextures("^[brighten");
1723 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1724 m_armor_groups.clear();
1725 int armor_groups_size = readU16(is);
1726 for(int i=0; i<armor_groups_size; i++)
1728 std::string name = deSerializeString(is);
1729 int rating = readS16(is);
1730 m_armor_groups[name] = rating;
1732 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1733 readU8(is); // version
1734 m_nametag_color = readARGB8(is);
1735 if (m_textnode != NULL) {
1736 m_textnode->setTextColor(m_nametag_color);
1738 // Enforce hiding nametag,
1739 // because if freetype is enabled, a grey
1740 // shadow can remain.
1741 m_textnode->setVisible(m_nametag_color.getAlpha() > 0);
1746 /* \pre punchitem != NULL
1748 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1749 float time_from_last_punch)
1751 assert(punchitem); // pre-condition
1752 const ToolCapabilities *toolcap =
1753 &punchitem->getToolCapabilities(m_gamedef->idef());
1754 PunchDamageResult result = getPunchDamage(
1758 time_from_last_punch);
1760 if(result.did_punch && result.damage != 0)
1762 if(result.damage < m_hp)
1764 m_hp -= result.damage;
1767 // TODO: Execute defined fast response
1768 // As there is no definition, make a smoke puff
1769 ClientSimpleObject *simple = createSmokePuff(
1770 m_smgr, m_env, m_position,
1771 m_prop.visual_size * BS);
1772 m_env->addSimpleObject(simple);
1774 // TODO: Execute defined fast response
1775 // Flashing shall suffice as there is no definition
1776 m_reset_textures_timer = 0.05;
1777 if(result.damage >= 2)
1778 m_reset_textures_timer += 0.05 * result.damage;
1779 updateTextures("^[brighten");
1785 std::string GenericCAO::debugInfoText()
1787 std::ostringstream os(std::ios::binary);
1788 os<<"GenericCAO hp="<<m_hp<<"\n";
1790 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1791 i != m_armor_groups.end(); i++)
1793 os<<i->first<<"="<<i->second<<", ";
1800 GenericCAO proto_GenericCAO(NULL, NULL);