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 warningstream<<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),
555 m_position(v3f(0,10*BS,0)),
556 m_velocity(v3f(0,0,0)),
557 m_acceleration(v3f(0,0,0)),
562 m_initial_tx_basepos_set(false),
563 m_tx_select_horiz_by_yawpitch(false),
564 m_animation_range(v2s32(0,0)),
565 m_animation_speed(15),
566 m_animation_blend(0),
567 m_animation_loop(true),
568 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
569 m_attachment_bone(""),
570 m_attachment_position(v3f(0,0,0)),
571 m_attachment_rotation(v3f(0,0,0)),
572 m_attached_to_local(false),
574 m_anim_num_frames(1),
575 m_anim_framelength(0.2),
577 m_reset_textures_timer(-1),
578 m_visuals_expired(false),
579 m_step_distance_counter(0),
584 ClientActiveObject::registerType(getType(), create);
587 bool GenericCAO::getCollisionBox(aabb3f *toset)
591 //update collision box
592 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
593 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
595 toset->MinEdge += m_position;
596 toset->MaxEdge += m_position;
604 bool GenericCAO::collideWithObjects()
606 return m_prop.collideWithObjects;
609 void GenericCAO::initialize(const std::string &data)
611 infostream<<"GenericCAO: Got init data"<<std::endl;
612 std::istringstream is(data, std::ios::binary);
613 int num_messages = 0;
615 u8 version = readU8(is);
617 if(version == 1) // In PROTOCOL_VERSION 14
619 m_name = deSerializeString(is);
620 m_is_player = readU8(is);
622 m_position = readV3F1000(is);
623 m_yaw = readF1000(is);
625 num_messages = readU8(is);
627 else if(version == 0) // In PROTOCOL_VERSION 13
629 m_name = deSerializeString(is);
630 m_is_player = readU8(is);
631 m_position = readV3F1000(is);
632 m_yaw = readF1000(is);
634 num_messages = readU8(is);
638 errorstream<<"GenericCAO: Unsupported init data version"
643 for(int i=0; i<num_messages; i++)
645 std::string message = deSerializeLongString(is);
646 processMessage(message);
649 pos_translator.init(m_position);
654 Player *player = m_env->getPlayer(m_name.c_str());
655 if(player && player->isLocal())
657 m_is_local_player = true;
658 m_is_visible = false;
659 LocalPlayer* localplayer = dynamic_cast<LocalPlayer*>(player);
661 assert( localplayer != NULL );
662 localplayer->setCAO(this);
664 m_env->addPlayerName(m_name.c_str());
668 GenericCAO::~GenericCAO()
672 m_env->removePlayerName(m_name.c_str());
674 removeFromScene(true);
677 core::aabbox3d<f32>* GenericCAO::getSelectionBox()
679 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
681 return &m_selection_box;
684 v3f GenericCAO::getPosition()
686 if (getParent() != NULL) {
687 scene::ISceneNode *node = getSceneNode();
689 return node->getAbsolutePosition();
693 return pos_translator.vect_show;
696 scene::ISceneNode* GenericCAO::getSceneNode()
700 if (m_animated_meshnode)
701 return m_animated_meshnode;
702 if (m_wield_meshnode)
703 return m_wield_meshnode;
709 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
714 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
716 return m_animated_meshnode;
719 WieldMeshSceneNode* GenericCAO::getWieldMeshSceneNode()
721 return m_wield_meshnode;
724 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
729 void GenericCAO::setChildrenVisible(bool toset)
731 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
732 GenericCAO *obj = m_env->getGenericCAO(m_children[i]);
734 obj->setVisible(toset);
739 void GenericCAO::setAttachments()
744 ClientActiveObject* GenericCAO::getParent()
746 ClientActiveObject *obj = NULL;
748 u16 attached_id = m_env->attachement_parent_ids[getId()];
750 if ((attached_id != 0) &&
751 (attached_id != getId())) {
752 obj = m_env->getActiveObject(attached_id);
757 void GenericCAO::removeFromScene(bool permanent)
759 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
760 if((m_env != NULL) && (permanent))
762 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
763 u16 ci = m_children[i];
764 if (m_env->attachement_parent_ids[ci] == getId()) {
765 m_env->attachement_parent_ids[ci] = 0;
769 m_env->attachement_parent_ids[getId()] = 0;
771 LocalPlayer* player = m_env->getLocalPlayer();
772 if (this == player->parent) {
773 player->parent = NULL;
774 player->isAttached = false;
780 m_meshnode->remove();
784 if(m_animated_meshnode)
786 m_animated_meshnode->remove();
787 m_animated_meshnode->drop();
788 m_animated_meshnode = NULL;
792 m_wield_meshnode->remove();
793 m_wield_meshnode->drop();
794 m_wield_meshnode = NULL;
798 m_spritenode->remove();
799 m_spritenode->drop();
804 m_textnode->remove();
810 void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
816 if (getSceneNode() != NULL)
819 m_visuals_expired = false;
821 if(!m_prop.is_visible)
824 //video::IVideoDriver* driver = smgr->getVideoDriver();
826 if(m_prop.visual == "sprite")
828 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
829 m_spritenode = smgr->addBillboardSceneNode(
830 NULL, v2f(1, 1), v3f(0,0,0), -1);
831 m_spritenode->grab();
832 m_spritenode->setMaterialTexture(0,
833 tsrc->getTextureForMesh("unknown_node.png"));
834 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
835 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
836 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
837 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
838 u8 li = m_last_light;
839 m_spritenode->setColor(video::SColor(255,li,li,li));
840 m_spritenode->setSize(m_prop.visual_size*BS);
842 const float txs = 1.0 / 1;
843 const float tys = 1.0 / 1;
844 setBillboardTextureMatrix(m_spritenode,
848 else if(m_prop.visual == "upright_sprite") {
849 scene::SMesh *mesh = new scene::SMesh();
850 double dx = BS*m_prop.visual_size.X/2;
851 double dy = BS*m_prop.visual_size.Y/2;
853 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
854 u8 li = m_last_light;
855 video::SColor c(255,li,li,li);
856 video::S3DVertex vertices[4] =
858 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
859 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
860 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
861 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
863 u16 indices[] = {0,1,2,2,3,0};
864 buf->append(vertices, 4, indices, 6);
866 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
867 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
868 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
869 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
871 mesh->addMeshBuffer(buf);
875 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
876 u8 li = m_last_light;
877 video::SColor c(255,li,li,li);
878 video::S3DVertex vertices[4] =
880 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
881 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
882 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
883 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
885 u16 indices[] = {0,1,2,2,3,0};
886 buf->append(vertices, 4, indices, 6);
888 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
889 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
890 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
891 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
893 mesh->addMeshBuffer(buf);
896 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
899 // Set it to use the materials of the meshbuffers directly.
900 // This is needed for changing the texture in the future
901 m_meshnode->setReadOnlyMaterials(true);
903 else if(m_prop.visual == "cube") {
904 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
905 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
906 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
910 m_meshnode->setScale(v3f(m_prop.visual_size.X,
911 m_prop.visual_size.Y,
912 m_prop.visual_size.X));
913 u8 li = m_last_light;
914 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
916 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
917 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
918 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
919 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
921 else if(m_prop.visual == "mesh") {
922 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
923 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
926 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
927 m_animated_meshnode->grab();
928 mesh->drop(); // The scene node took hold of it
929 m_animated_meshnode->animateJoints(); // Needed for some animations
930 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
931 m_prop.visual_size.Y,
932 m_prop.visual_size.X));
933 u8 li = m_last_light;
934 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
936 bool backface_culling = m_prop.backface_culling;
938 backface_culling = false;
940 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
941 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
942 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
943 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
944 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
947 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
949 else if(m_prop.visual == "wielditem") {
950 infostream<<"GenericCAO::addToScene(): wielditem"<<std::endl;
951 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
952 if(m_prop.textures.size() >= 1){
953 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
954 IItemDefManager *idef = m_gamedef->idef();
955 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
957 m_wield_meshnode = new WieldMeshSceneNode(
958 smgr->getRootSceneNode(), smgr, -1);
959 m_wield_meshnode->setItem(item, m_gamedef);
961 m_wield_meshnode->setScale(v3f(m_prop.visual_size.X/2,
962 m_prop.visual_size.Y/2,
963 m_prop.visual_size.X/2));
964 u8 li = m_last_light;
965 m_wield_meshnode->setColor(video::SColor(255,li,li,li));
968 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
969 <<"\" not supported"<<std::endl;
973 scene::ISceneNode *node = getSceneNode();
974 if (node && m_prop.nametag != "" && !m_is_local_player) {
975 // Add a text node for showing the name
976 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
977 std::wstring nametag_text = utf8_to_wide(m_prop.nametag);
978 m_textnode = smgr->addTextSceneNode(gui->getSkin()->getFont(),
979 nametag_text.c_str(), m_prop.nametag_color, node);
981 m_textnode->setPosition(v3f(0, BS*1.1, 0));
983 // Enforce hiding nametag,
984 // because if freetype is enabled, a grey
985 // shadow can remain.
986 m_textnode->setVisible(m_prop.nametag_color.getAlpha() > 0);
991 updateBonePosition();
995 void GenericCAO::updateLight(u8 light_at_pos)
997 // Don't update light of attached one
998 if (getParent() != NULL) {
1002 updateLightNoCheck(light_at_pos);
1004 // Update light of all children
1005 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1006 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1008 obj->updateLightNoCheck(light_at_pos);
1013 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
1015 u8 li = decode_light(light_at_pos);
1016 if (li != m_last_light) {
1018 video::SColor color(255,li,li,li);
1020 setMeshColor(m_meshnode->getMesh(), color);
1021 } else if (m_animated_meshnode) {
1022 setMeshColor(m_animated_meshnode->getMesh(), color);
1023 } else if (m_wield_meshnode) {
1024 m_wield_meshnode->setColor(color);
1025 } else if (m_spritenode) {
1026 m_spritenode->setColor(color);
1031 v3s16 GenericCAO::getLightPosition()
1033 return floatToInt(m_position, BS);
1036 void GenericCAO::updateNodePos()
1038 if (getParent() != NULL)
1041 scene::ISceneNode *node = getSceneNode();
1044 v3s16 camera_offset = m_env->getCameraOffset();
1045 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
1046 if (node != m_spritenode) { // rotate if not a sprite
1047 v3f rot = node->getRotation();
1049 node->setRotation(rot);
1054 void GenericCAO::step(float dtime, ClientEnvironment *env)
1056 // Handel model of local player instantly to prevent lags
1057 if(m_is_local_player)
1059 LocalPlayer *player = m_env->getLocalPlayer();
1063 int old_anim = player->last_animation;
1064 float old_anim_speed = player->last_animation_speed;
1065 m_position = player->getPosition() + v3f(0,BS,0);
1066 m_velocity = v3f(0,0,0);
1067 m_acceleration = v3f(0,0,0);
1068 pos_translator.vect_show = m_position;
1069 m_yaw = player->getYaw();
1070 PlayerControl controls = player->getPlayerControl();
1072 bool walking = false;
1073 if(controls.up || controls.down || controls.left || controls.right)
1076 f32 new_speed = player->local_animation_speed;
1077 v2s32 new_anim = v2s32(0,0);
1078 bool allow_update = false;
1080 // increase speed if using fast or flying fast
1081 if((g_settings->getBool("fast_move") &&
1082 m_gamedef->checkLocalPrivilege("fast")) &&
1084 (!player->touching_ground &&
1085 g_settings->getBool("free_move") &&
1086 m_gamedef->checkLocalPrivilege("fly"))))
1088 // slowdown speed if sneeking
1089 if(controls.sneak && walking)
1092 if(walking && (controls.LMB || controls.RMB))
1094 new_anim = player->local_animations[3];
1095 player->last_animation = WD_ANIM;
1096 } else if(walking) {
1097 new_anim = player->local_animations[1];
1098 player->last_animation = WALK_ANIM;
1099 } else if(controls.LMB || controls.RMB) {
1100 new_anim = player->local_animations[2];
1101 player->last_animation = DIG_ANIM;
1104 // Apply animations if input detected and not attached
1105 // or set idle animation
1106 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1108 allow_update = true;
1109 m_animation_range = new_anim;
1110 m_animation_speed = new_speed;
1111 player->last_animation_speed = m_animation_speed;
1113 player->last_animation = NO_ANIM;
1115 if (old_anim != NO_ANIM)
1117 m_animation_range = player->local_animations[0];
1122 // Update local player animations
1123 if ((player->last_animation != old_anim ||
1124 m_animation_speed != old_anim_speed) &&
1125 player->last_animation != NO_ANIM && allow_update)
1131 if(m_visuals_expired && m_smgr && m_irr){
1132 m_visuals_expired = false;
1134 // Attachments, part 1: All attached objects must be unparented first,
1135 // or Irrlicht causes a segmentation fault
1136 for(std::vector<u16>::iterator ci = m_children.begin();
1137 ci != m_children.end();)
1139 if (m_env->attachement_parent_ids[*ci] != getId()) {
1140 ci = m_children.erase(ci);
1143 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1145 scene::ISceneNode *child_node = obj->getSceneNode();
1147 child_node->setParent(m_smgr->getRootSceneNode());
1152 removeFromScene(false);
1153 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1155 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1156 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1157 // Get the object of the child
1158 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1160 obj->setAttachments();
1164 // Make sure m_is_visible is always applied
1165 scene::ISceneNode *node = getSceneNode();
1167 node->setVisible(m_is_visible);
1169 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1171 // Set these for later
1172 m_position = getPosition();
1173 m_velocity = v3f(0,0,0);
1174 m_acceleration = v3f(0,0,0);
1175 pos_translator.vect_show = m_position;
1177 if(m_is_local_player) // Update local player attachment position
1179 LocalPlayer *player = m_env->getLocalPlayer();
1180 player->overridePosition = getParent()->getPosition();
1181 m_env->getLocalPlayer()->parent = getParent();
1184 v3f lastpos = pos_translator.vect_show;
1188 core::aabbox3d<f32> box = m_prop.collisionbox;
1191 collisionMoveResult moveresult;
1192 f32 pos_max_d = BS*0.125; // Distance per iteration
1193 v3f p_pos = m_position;
1194 v3f p_velocity = m_velocity;
1195 moveresult = collisionMoveSimple(env,env->getGameDef(),
1196 pos_max_d, box, m_prop.stepheight, dtime,
1197 &p_pos, &p_velocity, m_acceleration,
1198 this, m_prop.collideWithObjects);
1201 m_velocity = p_velocity;
1203 bool is_end_position = moveresult.collides;
1204 pos_translator.update(m_position, is_end_position, dtime);
1205 pos_translator.translate(dtime);
1208 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1209 m_velocity += dtime * m_acceleration;
1210 pos_translator.update(m_position, pos_translator.aim_is_end,
1211 pos_translator.anim_time);
1212 pos_translator.translate(dtime);
1216 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1217 m_step_distance_counter += moved;
1218 if(m_step_distance_counter > 1.5*BS)
1220 m_step_distance_counter = 0;
1221 if(!m_is_local_player && m_prop.makes_footstep_sound)
1223 INodeDefManager *ndef = m_gamedef->ndef();
1224 v3s16 p = floatToInt(getPosition() + v3f(0,
1225 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1226 MapNode n = m_env->getMap().getNodeNoEx(p);
1227 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1228 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1233 m_anim_timer += dtime;
1234 if(m_anim_timer >= m_anim_framelength)
1236 m_anim_timer -= m_anim_framelength;
1238 if(m_anim_frame >= m_anim_num_frames)
1244 if(m_reset_textures_timer >= 0)
1246 m_reset_textures_timer -= dtime;
1247 if(m_reset_textures_timer <= 0){
1248 m_reset_textures_timer = -1;
1252 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1254 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1258 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1259 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1261 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1262 + m_prop.automatic_face_movement_dir_offset;
1263 float max_rotation_delta =
1264 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1266 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
1267 (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
1269 m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
1271 m_yaw = optimal_yaw;
1277 void GenericCAO::updateTexturePos()
1281 scene::ICameraSceneNode* camera =
1282 m_spritenode->getSceneManager()->getActiveCamera();
1285 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1286 - camera->getAbsolutePosition();
1287 cam_to_entity.normalize();
1289 int row = m_tx_basepos.Y;
1290 int col = m_tx_basepos.X;
1292 if(m_tx_select_horiz_by_yawpitch)
1294 if(cam_to_entity.Y > 0.75)
1296 else if(cam_to_entity.Y < -0.75)
1300 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1301 float dir = mob_dir - m_yaw;
1302 dir = wrapDegrees_180(dir);
1303 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1304 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1306 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1308 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1310 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1317 // Animation goes downwards
1318 row += m_anim_frame;
1320 float txs = m_tx_size.X;
1321 float tys = m_tx_size.Y;
1322 setBillboardTextureMatrix(m_spritenode,
1323 txs, tys, col, row);
1327 void GenericCAO::updateTextures(const std::string &mod)
1329 ITextureSource *tsrc = m_gamedef->tsrc();
1331 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1332 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1333 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1337 if(m_prop.visual == "sprite")
1339 std::string texturestring = "unknown_node.png";
1340 if(m_prop.textures.size() >= 1)
1341 texturestring = m_prop.textures[0];
1342 texturestring += mod;
1343 m_spritenode->setMaterialTexture(0,
1344 tsrc->getTextureForMesh(texturestring));
1346 // This allows setting per-material colors. However, until a real lighting
1347 // system is added, the code below will have no effect. Once MineTest
1348 // has directional lighting, it should work automatically.
1349 if(m_prop.colors.size() >= 1)
1351 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1352 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1353 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1356 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1357 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1358 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1361 if(m_animated_meshnode)
1363 if(m_prop.visual == "mesh")
1365 for (u32 i = 0; i < m_prop.textures.size() &&
1366 i < m_animated_meshnode->getMaterialCount(); ++i)
1368 std::string texturestring = m_prop.textures[i];
1369 if(texturestring == "")
1370 continue; // Empty texture string means don't modify that material
1371 texturestring += mod;
1372 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1375 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1379 // Set material flags and texture
1380 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1381 material.TextureLayer[0].Texture = texture;
1382 material.setFlag(video::EMF_LIGHTING, false);
1383 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1385 m_animated_meshnode->getMaterial(i)
1386 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1387 m_animated_meshnode->getMaterial(i)
1388 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1389 m_animated_meshnode->getMaterial(i)
1390 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1392 for (u32 i = 0; i < m_prop.colors.size() &&
1393 i < m_animated_meshnode->getMaterialCount(); ++i)
1395 // This allows setting per-material colors. However, until a real lighting
1396 // system is added, the code below will have no effect. Once MineTest
1397 // has directional lighting, it should work automatically.
1398 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1399 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1400 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1406 if(m_prop.visual == "cube")
1408 for (u32 i = 0; i < 6; ++i)
1410 std::string texturestring = "unknown_node.png";
1411 if(m_prop.textures.size() > i)
1412 texturestring = m_prop.textures[i];
1413 texturestring += mod;
1416 // Set material flags and texture
1417 video::SMaterial& material = m_meshnode->getMaterial(i);
1418 material.setFlag(video::EMF_LIGHTING, false);
1419 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1420 material.setTexture(0,
1421 tsrc->getTextureForMesh(texturestring));
1422 material.getTextureMatrix(0).makeIdentity();
1424 // This allows setting per-material colors. However, until a real lighting
1425 // system is added, the code below will have no effect. Once MineTest
1426 // has directional lighting, it should work automatically.
1427 if(m_prop.colors.size() > i)
1429 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1430 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1431 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1434 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1435 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1436 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1439 else if(m_prop.visual == "upright_sprite")
1441 scene::IMesh *mesh = m_meshnode->getMesh();
1443 std::string tname = "unknown_object.png";
1444 if(m_prop.textures.size() >= 1)
1445 tname = m_prop.textures[0];
1447 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1448 buf->getMaterial().setTexture(0,
1449 tsrc->getTextureForMesh(tname));
1451 // This allows setting per-material colors. However, until a real lighting
1452 // system is added, the code below will have no effect. Once MineTest
1453 // has directional lighting, it should work automatically.
1454 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);
1466 std::string tname = "unknown_object.png";
1467 if(m_prop.textures.size() >= 2)
1468 tname = m_prop.textures[1];
1469 else if(m_prop.textures.size() >= 1)
1470 tname = m_prop.textures[0];
1472 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1473 buf->getMaterial().setTexture(0,
1474 tsrc->getTextureForMesh(tname));
1476 // This allows setting per-material colors. However, until a real lighting
1477 // system is added, the code below will have no effect. Once MineTest
1478 // has directional lighting, it should work automatically.
1479 if(m_prop.colors.size() >= 2)
1481 buf->getMaterial().AmbientColor = m_prop.colors[1];
1482 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1483 buf->getMaterial().SpecularColor = m_prop.colors[1];
1485 else if(m_prop.colors.size() >= 1)
1487 buf->getMaterial().AmbientColor = m_prop.colors[0];
1488 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1489 buf->getMaterial().SpecularColor = m_prop.colors[0];
1492 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1493 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1494 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1500 void GenericCAO::updateAnimation()
1502 if(m_animated_meshnode == NULL)
1505 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1506 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1507 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1508 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1509 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1510 m_animated_meshnode->setTransitionTime(m_animation_blend);
1511 // Requires Irrlicht 1.8 or greater
1512 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1513 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1514 m_animated_meshnode->setLoopMode(m_animation_loop);
1518 void GenericCAO::updateBonePosition()
1520 if(m_bone_position.empty() || m_animated_meshnode == NULL)
1523 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1524 for(std::map<std::string,
1525 core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1526 ii != m_bone_position.end(); ++ii)
1528 std::string bone_name = (*ii).first;
1529 v3f bone_pos = (*ii).second.X;
1530 v3f bone_rot = (*ii).second.Y;
1531 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1534 bone->setPosition(bone_pos);
1535 bone->setRotation(bone_rot);
1540 void GenericCAO::updateAttachments()
1543 if (getParent() == NULL) { // Detach or don't attach
1544 scene::ISceneNode *node = getSceneNode();
1546 v3f old_position = node->getAbsolutePosition();
1547 v3f old_rotation = node->getRotation();
1548 node->setParent(m_smgr->getRootSceneNode());
1549 node->setPosition(old_position);
1550 node->setRotation(old_rotation);
1551 node->updateAbsolutePosition();
1553 if (m_is_local_player) {
1554 LocalPlayer *player = m_env->getLocalPlayer();
1555 player->isAttached = false;
1560 scene::ISceneNode *my_node = getSceneNode();
1562 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1563 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1564 getParent()->getAnimatedMeshSceneNode();
1565 if (parent_animated_mesh_node && m_attachment_bone != "") {
1566 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1569 if (my_node && parent_node) {
1570 my_node->setParent(parent_node);
1571 my_node->setPosition(m_attachment_position);
1572 my_node->setRotation(m_attachment_rotation);
1573 my_node->updateAbsolutePosition();
1575 if (m_is_local_player) {
1576 LocalPlayer *player = m_env->getLocalPlayer();
1577 player->isAttached = true;
1582 void GenericCAO::processMessage(const std::string &data)
1584 //infostream<<"GenericCAO: Got message"<<std::endl;
1585 std::istringstream is(data, std::ios::binary);
1587 u8 cmd = readU8(is);
1588 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1590 m_prop = gob_read_set_properties(is);
1592 m_selection_box = m_prop.collisionbox;
1593 m_selection_box.MinEdge *= BS;
1594 m_selection_box.MaxEdge *= BS;
1596 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1597 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1599 if(!m_initial_tx_basepos_set){
1600 m_initial_tx_basepos_set = true;
1601 m_tx_basepos = m_prop.initial_sprite_basepos;
1604 if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
1605 m_prop.nametag = m_name;
1609 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1611 // Not sent by the server if this object is an attachment.
1612 // We might however get here if the server notices the object being detached before the client.
1613 m_position = readV3F1000(is);
1614 m_velocity = readV3F1000(is);
1615 m_acceleration = readV3F1000(is);
1616 if(fabs(m_prop.automatic_rotate) < 0.001)
1617 m_yaw = readF1000(is);
1620 bool do_interpolate = readU8(is);
1621 bool is_end_position = readU8(is);
1622 float update_interval = readF1000(is);
1624 // Place us a bit higher if we're physical, to not sink into
1625 // the ground due to sucky collision detection...
1627 m_position += v3f(0,0.002,0);
1629 if(getParent() != NULL) // Just in case
1634 if(!m_prop.physical)
1635 pos_translator.update(m_position, is_end_position, update_interval);
1637 pos_translator.init(m_position);
1641 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1642 std::string mod = deSerializeString(is);
1643 updateTextures(mod);
1645 else if(cmd == GENERIC_CMD_SET_SPRITE) {
1646 v2s16 p = readV2S16(is);
1647 int num_frames = readU16(is);
1648 float framelength = readF1000(is);
1649 bool select_horiz_by_yawpitch = readU8(is);
1652 m_anim_num_frames = num_frames;
1653 m_anim_framelength = framelength;
1654 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1658 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1659 float override_speed = readF1000(is);
1660 float override_jump = readF1000(is);
1661 float override_gravity = readF1000(is);
1662 // these are sent inverted so we get true when the server sends nothing
1663 bool sneak = !readU8(is);
1664 bool sneak_glitch = !readU8(is);
1667 if(m_is_local_player)
1669 LocalPlayer *player = m_env->getLocalPlayer();
1670 player->physics_override_speed = override_speed;
1671 player->physics_override_jump = override_jump;
1672 player->physics_override_gravity = override_gravity;
1673 player->physics_override_sneak = sneak;
1674 player->physics_override_sneak_glitch = sneak_glitch;
1677 else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1678 // TODO: change frames send as v2s32 value
1679 v2f range = readV2F1000(is);
1680 if (!m_is_local_player) {
1681 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1682 m_animation_speed = readF1000(is);
1683 m_animation_blend = readF1000(is);
1684 // these are sent inverted so we get true when the server sends nothing
1685 m_animation_loop = !readU8(is);
1688 LocalPlayer *player = m_env->getLocalPlayer();
1689 if(player->last_animation == NO_ANIM)
1691 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1692 m_animation_speed = readF1000(is);
1693 m_animation_blend = readF1000(is);
1694 // these are sent inverted so we get true when the server sends nothing
1695 m_animation_loop = !readU8(is);
1697 // update animation only if local animations present
1698 // and received animation is unknown (except idle animation)
1699 bool is_known = false;
1700 for (int i = 1;i<4;i++)
1702 if(m_animation_range.Y == player->local_animations[i].Y)
1706 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1712 else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1713 std::string bone = deSerializeString(is);
1714 v3f position = readV3F1000(is);
1715 v3f rotation = readV3F1000(is);
1716 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1718 updateBonePosition();
1719 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1720 u16 parentID = readS16(is);
1721 u16 oldparent = m_env->attachement_parent_ids[getId()];
1723 m_children.erase(std::remove(m_children.begin(), m_children.end(),
1724 getId()), m_children.end());
1726 m_env->attachement_parent_ids[getId()] = parentID;
1727 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1730 parentobj->m_children.push_back(getId());
1733 m_attachment_bone = deSerializeString(is);
1734 m_attachment_position = readV3F1000(is);
1735 m_attachment_rotation = readV3F1000(is);
1737 // localplayer itself can't be attached to localplayer
1738 if (!m_is_local_player) {
1739 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1740 // Objects attached to the local player should be hidden by default
1741 m_is_visible = !m_attached_to_local;
1744 updateAttachments();
1746 else if(cmd == GENERIC_CMD_PUNCHED) {
1747 /*s16 damage =*/ readS16(is);
1748 s16 result_hp = readS16(is);
1750 // Use this instead of the send damage to not interfere with prediction
1751 s16 damage = m_hp - result_hp;
1759 // TODO: Execute defined fast response
1760 // As there is no definition, make a smoke puff
1761 ClientSimpleObject *simple = createSmokePuff(
1762 m_smgr, m_env, m_position,
1763 m_prop.visual_size * BS);
1764 m_env->addSimpleObject(simple);
1766 // TODO: Execute defined fast response
1767 // Flashing shall suffice as there is no definition
1768 m_reset_textures_timer = 0.05;
1770 m_reset_textures_timer += 0.05 * damage;
1771 updateTextures("^[brighten");
1775 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1776 m_armor_groups.clear();
1777 int armor_groups_size = readU16(is);
1778 for(int i=0; i<armor_groups_size; i++)
1780 std::string name = deSerializeString(is);
1781 int rating = readS16(is);
1782 m_armor_groups[name] = rating;
1784 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1785 // Deprecated, for backwards compatibility only.
1786 readU8(is); // version
1787 m_prop.nametag_color = readARGB8(is);
1788 if (m_textnode != NULL) {
1789 m_textnode->setTextColor(m_prop.nametag_color);
1791 // Enforce hiding nametag,
1792 // because if freetype is enabled, a grey shadow can remain.
1793 m_textnode->setVisible(m_prop.nametag_color.getAlpha() > 0);
1798 /* \pre punchitem != NULL
1800 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1801 float time_from_last_punch)
1803 assert(punchitem); // pre-condition
1804 const ToolCapabilities *toolcap =
1805 &punchitem->getToolCapabilities(m_gamedef->idef());
1806 PunchDamageResult result = getPunchDamage(
1810 time_from_last_punch);
1812 if(result.did_punch && result.damage != 0)
1814 if(result.damage < m_hp)
1816 m_hp -= result.damage;
1819 // TODO: Execute defined fast response
1820 // As there is no definition, make a smoke puff
1821 ClientSimpleObject *simple = createSmokePuff(
1822 m_smgr, m_env, m_position,
1823 m_prop.visual_size * BS);
1824 m_env->addSimpleObject(simple);
1826 // TODO: Execute defined fast response
1827 // Flashing shall suffice as there is no definition
1828 m_reset_textures_timer = 0.05;
1829 if(result.damage >= 2)
1830 m_reset_textures_timer += 0.05 * result.damage;
1831 updateTextures("^[brighten");
1837 std::string GenericCAO::debugInfoText()
1839 std::ostringstream os(std::ios::binary);
1840 os<<"GenericCAO hp="<<m_hp<<"\n";
1842 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1843 i != m_armor_groups.end(); ++i)
1845 os<<i->first<<"="<<i->second<<", ";
1852 GenericCAO proto_GenericCAO(NULL, NULL);