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"
31 #include "environment.h"
32 #include "collision.h"
34 #include "serialization.h" // For decompressZlib
36 #include "clientobject.h"
37 #include "content_object.h"
41 #include "content_cso.h"
44 #include "localplayer.h"
46 #include "main.h" // g_settings
47 #include "camera.h" // CameraModes
48 #include "wieldmesh.h"
52 struct ToolCapabilities;
54 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
56 std::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
58 SmoothTranslator::SmoothTranslator():
68 void SmoothTranslator::init(v3f vect)
75 anim_time_counter = 0;
79 void SmoothTranslator::sharpen()
84 void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
86 aim_is_end = is_end_position;
89 if(update_interval > 0)
91 anim_time = update_interval;
93 if(anim_time < 0.001 || anim_time > 1.0)
94 anim_time = anim_time_counter;
96 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
98 anim_time_counter = 0;
102 void SmoothTranslator::translate(f32 dtime)
104 anim_time_counter = anim_time_counter + dtime;
105 anim_counter = anim_counter + dtime;
106 v3f vect_move = vect_aim - vect_old;
108 if(anim_time > 0.001)
109 moveratio = anim_time_counter / anim_time;
110 // Move a bit less than should, to avoid oscillation
111 moveratio = moveratio * 0.8;
112 float move_end = 1.5;
115 if(moveratio > move_end)
116 moveratio = move_end;
117 vect_show = vect_old + vect_move * moveratio;
120 bool SmoothTranslator::is_moving()
122 return ((anim_time_counter / anim_time) < 1.4);
129 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
130 float txs, float tys, int col, int row)
132 video::SMaterial& material = bill->getMaterial(0);
133 core::matrix4& matrix = material.getTextureMatrix(0);
134 matrix.setTextureTranslate(txs*col, tys*row);
135 matrix.setTextureScale(txs, tys);
142 class TestCAO : public ClientActiveObject
145 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
150 return ACTIVEOBJECT_TYPE_TEST;
153 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
155 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
156 IrrlichtDevice *irr);
157 void removeFromScene(bool permanent);
158 void updateLight(u8 light_at_pos);
159 v3s16 getLightPosition();
160 void updateNodePos();
162 void step(float dtime, ClientEnvironment *env);
164 void processMessage(const std::string &data);
166 bool getCollisionBox(aabb3f *toset) { return false; }
168 scene::IMeshSceneNode *m_node;
173 TestCAO proto_TestCAO(NULL, NULL);
175 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
176 ClientActiveObject(0, gamedef, env),
178 m_position(v3f(0,10*BS,0))
180 ClientActiveObject::registerType(getType(), create);
187 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
189 return new TestCAO(gamedef, env);
192 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
198 //video::IVideoDriver* driver = smgr->getVideoDriver();
200 scene::SMesh *mesh = new scene::SMesh();
201 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
202 video::SColor c(255,255,255,255);
203 video::S3DVertex vertices[4] =
205 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
206 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
207 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
208 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
210 u16 indices[] = {0,1,2,2,3,0};
211 buf->append(vertices, 4, indices, 6);
213 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
214 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
215 buf->getMaterial().setTexture(0, tsrc->getTexture("rat.png"));
216 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
217 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
218 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
220 mesh->addMeshBuffer(buf);
222 m_node = smgr->addMeshSceneNode(mesh, NULL);
227 void TestCAO::removeFromScene(bool permanent)
236 void TestCAO::updateLight(u8 light_at_pos)
240 v3s16 TestCAO::getLightPosition()
242 return floatToInt(m_position, BS);
245 void TestCAO::updateNodePos()
250 m_node->setPosition(m_position);
251 //m_node->setRotation(v3f(0, 45, 0));
254 void TestCAO::step(float dtime, ClientEnvironment *env)
258 v3f rot = m_node->getRotation();
259 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
260 rot.Y += dtime * 180;
261 m_node->setRotation(rot);
265 void TestCAO::processMessage(const std::string &data)
267 infostream<<"TestCAO: Got data: "<<data<<std::endl;
268 std::istringstream is(data, std::ios::binary);
286 class ItemCAO : public ClientActiveObject
289 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
294 return ACTIVEOBJECT_TYPE_ITEM;
297 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
299 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
300 IrrlichtDevice *irr);
301 void removeFromScene(bool permanent);
302 void updateLight(u8 light_at_pos);
303 v3s16 getLightPosition();
304 void updateNodePos();
305 void updateInfoText();
306 void updateTexture();
308 void step(float dtime, ClientEnvironment *env);
310 void processMessage(const std::string &data);
312 void initialize(const std::string &data);
314 core::aabbox3d<f32>* getSelectionBox()
315 {return &m_selection_box;}
319 std::string infoText()
322 bool getCollisionBox(aabb3f *toset) { return false; }
324 core::aabbox3d<f32> m_selection_box;
325 scene::IMeshSceneNode *m_node;
327 std::string m_itemstring;
328 std::string m_infotext;
331 #include "inventory.h"
334 ItemCAO proto_ItemCAO(NULL, NULL);
336 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
337 ClientActiveObject(0, gamedef, env),
338 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
340 m_position(v3f(0,10*BS,0))
344 ClientActiveObject::registerType(getType(), create);
352 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
354 return new ItemCAO(gamedef, env);
357 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
363 //video::IVideoDriver* driver = smgr->getVideoDriver();
365 scene::SMesh *mesh = new scene::SMesh();
366 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
367 video::SColor c(255,255,255,255);
368 video::S3DVertex vertices[4] =
370 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
371 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
372 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
373 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
374 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
375 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
376 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
377 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
379 u16 indices[] = {0,1,2,2,3,0};
380 buf->append(vertices, 4, indices, 6);
382 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
383 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
384 // Initialize with a generated placeholder texture
385 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
386 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
387 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
388 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
390 mesh->addMeshBuffer(buf);
392 m_node = smgr->addMeshSceneNode(mesh, NULL);
403 void ItemCAO::removeFromScene(bool permanent)
412 void ItemCAO::updateLight(u8 light_at_pos)
417 u8 li = decode_light(light_at_pos);
418 video::SColor color(255,li,li,li);
419 setMeshColor(m_node->getMesh(), color);
422 v3s16 ItemCAO::getLightPosition()
424 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
427 void ItemCAO::updateNodePos()
432 m_node->setPosition(m_position);
435 void ItemCAO::updateInfoText()
438 IItemDefManager *idef = m_gamedef->idef();
440 item.deSerialize(m_itemstring, idef);
441 if(item.isKnown(idef))
442 m_infotext = item.getDefinition(idef).description;
444 m_infotext = "Unknown item: '" + m_itemstring + "'";
446 m_infotext += " (" + itos(item.count) + ")";
448 catch(SerializationError &e)
450 m_infotext = "Unknown item: '" + m_itemstring + "'";
454 void ItemCAO::updateTexture()
459 // Create an inventory item to see what is its image
460 std::istringstream is(m_itemstring, std::ios_base::binary);
461 video::ITexture *texture = NULL;
463 IItemDefManager *idef = m_gamedef->idef();
465 item.deSerialize(is, idef);
466 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
468 catch(SerializationError &e)
470 infostream<<"WARNING: "<<__FUNCTION_NAME
471 <<": error deSerializing itemstring \""
472 <<m_itemstring<<std::endl;
475 // Set meshbuffer texture
476 m_node->getMaterial(0).setTexture(0, texture);
480 void ItemCAO::step(float dtime, ClientEnvironment *env)
484 /*v3f rot = m_node->getRotation();
485 rot.Y += dtime * 120;
486 m_node->setRotation(rot);*/
487 LocalPlayer *player = env->getLocalPlayer();
489 v3f rot = m_node->getRotation();
490 rot.Y = 180.0 - (player->getYaw());
491 m_node->setRotation(rot);
495 void ItemCAO::processMessage(const std::string &data)
497 //infostream<<"ItemCAO: Got message"<<std::endl;
498 std::istringstream is(data, std::ios::binary);
504 m_position = readV3F1000(is);
510 m_itemstring = deSerializeString(is);
516 void ItemCAO::initialize(const std::string &data)
518 infostream<<"ItemCAO: Got init data"<<std::endl;
521 std::istringstream is(data, std::ios::binary);
523 u8 version = readU8(is);
528 m_position = readV3F1000(is);
530 m_itemstring = deSerializeString(is);
541 #include "genericobject.h"
543 GenericCAO::GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
544 ClientActiveObject(0, gamedef, env),
547 m_is_local_player(false),
552 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
554 m_animated_meshnode(NULL),
555 m_wield_meshnode(NULL),
558 m_position(v3f(0,10*BS,0)),
559 m_velocity(v3f(0,0,0)),
560 m_acceleration(v3f(0,0,0)),
565 m_initial_tx_basepos_set(false),
566 m_tx_select_horiz_by_yawpitch(false),
567 m_animation_range(v2s32(0,0)),
568 m_animation_speed(15),
569 m_animation_blend(0),
570 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
571 m_attachment_bone(""),
572 m_attachment_position(v3f(0,0,0)),
573 m_attachment_rotation(v3f(0,0,0)),
574 m_attached_to_local(false),
576 m_anim_num_frames(1),
577 m_anim_framelength(0.2),
579 m_reset_textures_timer(-1),
580 m_visuals_expired(false),
581 m_step_distance_counter(0),
586 ClientActiveObject::registerType(getType(), create);
589 bool GenericCAO::getCollisionBox(aabb3f *toset)
593 //update collision box
594 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
595 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
597 toset->MinEdge += m_position;
598 toset->MaxEdge += m_position;
606 bool GenericCAO::collideWithObjects()
608 return m_prop.collideWithObjects;
611 void GenericCAO::initialize(const std::string &data)
613 infostream<<"GenericCAO: Got init data"<<std::endl;
614 std::istringstream is(data, std::ios::binary);
615 int num_messages = 0;
617 u8 version = readU8(is);
619 if(version == 1) // In PROTOCOL_VERSION 14
621 m_name = deSerializeString(is);
622 m_is_player = readU8(is);
624 m_position = readV3F1000(is);
625 m_yaw = readF1000(is);
627 num_messages = readU8(is);
629 else if(version == 0) // In PROTOCOL_VERSION 13
631 m_name = deSerializeString(is);
632 m_is_player = readU8(is);
633 m_position = readV3F1000(is);
634 m_yaw = readF1000(is);
636 num_messages = readU8(is);
640 errorstream<<"GenericCAO: Unsupported init data version"
645 for(int i=0; i<num_messages; i++)
647 std::string message = deSerializeLongString(is);
648 processMessage(message);
651 pos_translator.init(m_position);
656 Player *player = m_env->getPlayer(m_name.c_str());
657 if(player && player->isLocal())
659 m_is_local_player = true;
660 m_is_visible = false;
661 LocalPlayer* localplayer = dynamic_cast<LocalPlayer*>(player);
663 assert( localplayer != NULL );
664 localplayer->setCAO(this);
666 m_env->addPlayerName(m_name.c_str());
670 GenericCAO::~GenericCAO()
674 m_env->removePlayerName(m_name.c_str());
676 removeFromScene(true);
679 core::aabbox3d<f32>* GenericCAO::getSelectionBox()
681 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
683 return &m_selection_box;
686 v3f GenericCAO::getPosition()
688 if (getParent() != NULL) {
689 scene::ISceneNode *node = getSceneNode();
691 return node->getAbsolutePosition();
695 return pos_translator.vect_show;
698 scene::ISceneNode* GenericCAO::getSceneNode()
702 if (m_animated_meshnode)
703 return m_animated_meshnode;
704 if (m_wield_meshnode)
705 return m_wield_meshnode;
711 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
716 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
718 return m_animated_meshnode;
721 WieldMeshSceneNode* GenericCAO::getWieldMeshSceneNode()
723 return m_wield_meshnode;
726 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
731 void GenericCAO::setAttachments()
736 ClientActiveObject* GenericCAO::getParent()
738 ClientActiveObject *obj = NULL;
740 u16 attached_id = m_env->m_attachements[getId()];
742 if ((attached_id != 0) &&
743 (attached_id != getId())) {
744 obj = m_env->getActiveObject(attached_id);
749 void GenericCAO::removeFromScene(bool permanent)
751 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
752 if((m_env != NULL) && (permanent))
754 for(std::vector<u16>::iterator ci = m_children.begin();
755 ci != m_children.end(); ci++)
757 if (m_env->m_attachements[*ci] == getId()) {
758 m_env->m_attachements[*ci] = 0;
762 m_env->m_attachements[getId()] = 0;
764 LocalPlayer* player = m_env->getLocalPlayer();
765 if (this == player->parent) {
766 player->parent = NULL;
767 player->isAttached = false;
773 m_meshnode->remove();
777 if(m_animated_meshnode)
779 m_animated_meshnode->remove();
780 m_animated_meshnode->drop();
781 m_animated_meshnode = NULL;
785 m_wield_meshnode->remove();
786 m_wield_meshnode->drop();
787 m_wield_meshnode = NULL;
791 m_spritenode->remove();
792 m_spritenode->drop();
797 m_textnode->remove();
803 void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
809 if (getSceneNode() != NULL)
812 m_visuals_expired = false;
814 if(!m_prop.is_visible)
817 //video::IVideoDriver* driver = smgr->getVideoDriver();
819 if(m_prop.visual == "sprite")
821 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
822 m_spritenode = smgr->addBillboardSceneNode(
823 NULL, v2f(1, 1), v3f(0,0,0), -1);
824 m_spritenode->grab();
825 m_spritenode->setMaterialTexture(0,
826 tsrc->getTexture("unknown_node.png"));
827 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
828 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
829 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
830 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
831 u8 li = m_last_light;
832 m_spritenode->setColor(video::SColor(255,li,li,li));
833 m_spritenode->setSize(m_prop.visual_size*BS);
835 const float txs = 1.0 / 1;
836 const float tys = 1.0 / 1;
837 setBillboardTextureMatrix(m_spritenode,
841 else if(m_prop.visual == "upright_sprite") {
842 scene::SMesh *mesh = new scene::SMesh();
843 double dx = BS*m_prop.visual_size.X/2;
844 double dy = BS*m_prop.visual_size.Y/2;
846 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
847 u8 li = m_last_light;
848 video::SColor c(255,li,li,li);
849 video::S3DVertex vertices[4] =
851 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
852 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
853 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
854 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
856 u16 indices[] = {0,1,2,2,3,0};
857 buf->append(vertices, 4, indices, 6);
859 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
860 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
861 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
862 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
864 mesh->addMeshBuffer(buf);
868 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
869 u8 li = m_last_light;
870 video::SColor c(255,li,li,li);
871 video::S3DVertex vertices[4] =
873 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
874 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
875 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
876 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
878 u16 indices[] = {0,1,2,2,3,0};
879 buf->append(vertices, 4, indices, 6);
881 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
882 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
883 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
884 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
886 mesh->addMeshBuffer(buf);
889 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
892 // Set it to use the materials of the meshbuffers directly.
893 // This is needed for changing the texture in the future
894 m_meshnode->setReadOnlyMaterials(true);
896 else if(m_prop.visual == "cube") {
897 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
898 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
899 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
903 m_meshnode->setScale(v3f(m_prop.visual_size.X,
904 m_prop.visual_size.Y,
905 m_prop.visual_size.X));
906 u8 li = m_last_light;
907 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
909 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
910 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
911 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
912 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
914 else if(m_prop.visual == "mesh") {
915 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
916 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
919 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
920 m_animated_meshnode->grab();
921 mesh->drop(); // The scene node took hold of it
922 m_animated_meshnode->animateJoints(); // Needed for some animations
923 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
924 m_prop.visual_size.Y,
925 m_prop.visual_size.X));
926 u8 li = m_last_light;
927 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
929 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
930 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
931 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
932 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
935 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
937 else if(m_prop.visual == "wielditem") {
938 infostream<<"GenericCAO::addToScene(): wielditem"<<std::endl;
939 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
940 if(m_prop.textures.size() >= 1){
941 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
942 IItemDefManager *idef = m_gamedef->idef();
943 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
945 m_wield_meshnode = new WieldMeshSceneNode(
946 smgr->getRootSceneNode(), smgr, -1);
947 m_wield_meshnode->setItem(item, m_gamedef);
949 m_wield_meshnode->setScale(v3f(m_prop.visual_size.X/2,
950 m_prop.visual_size.Y/2,
951 m_prop.visual_size.X/2));
952 u8 li = m_last_light;
953 m_wield_meshnode->setColor(video::SColor(255,li,li,li));
956 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
957 <<"\" not supported"<<std::endl;
961 scene::ISceneNode *node = getSceneNode();
962 if (node && m_is_player && !m_is_local_player) {
963 // Add a text node for showing the name
964 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
965 std::wstring wname = narrow_to_wide(m_name);
966 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
967 wname.c_str(), video::SColor(255,255,255,255), node);
969 m_textnode->setPosition(v3f(0, BS*1.1, 0));
974 updateBonePosition();
978 void GenericCAO::updateLight(u8 light_at_pos)
980 u8 li = decode_light(light_at_pos);
981 if(li != m_last_light)
984 video::SColor color(255,li,li,li);
986 setMeshColor(m_meshnode->getMesh(), color);
987 if(m_animated_meshnode)
988 setMeshColor(m_animated_meshnode->getMesh(), color);
990 m_wield_meshnode->setColor(color);
992 m_spritenode->setColor(color);
996 v3s16 GenericCAO::getLightPosition()
998 return floatToInt(m_position, BS);
1001 void GenericCAO::updateNodePos()
1003 if (getParent() != NULL)
1006 scene::ISceneNode *node = getSceneNode();
1009 v3s16 camera_offset = m_env->getCameraOffset();
1010 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
1011 if (node != m_spritenode) { // rotate if not a sprite
1012 v3f rot = node->getRotation();
1014 node->setRotation(rot);
1019 void GenericCAO::step(float dtime, ClientEnvironment *env)
1021 // Handel model of local player instantly to prevent lags
1022 if(m_is_local_player)
1024 LocalPlayer *player = m_env->getLocalPlayer();
1028 int old_anim = player->last_animation;
1029 float old_anim_speed = player->last_animation_speed;
1030 m_position = player->getPosition() + v3f(0,BS,0);
1031 m_velocity = v3f(0,0,0);
1032 m_acceleration = v3f(0,0,0);
1033 pos_translator.vect_show = m_position;
1034 m_yaw = player->getYaw();
1035 PlayerControl controls = player->getPlayerControl();
1037 bool walking = false;
1038 if(controls.up || controls.down || controls.left || controls.right)
1041 f32 new_speed = player->local_animation_speed;
1042 v2s32 new_anim = v2s32(0,0);
1043 bool allow_update = false;
1045 // increase speed if using fast or flying fast
1046 if((g_settings->getBool("fast_move") &&
1047 m_gamedef->checkLocalPrivilege("fast")) &&
1049 (!player->touching_ground &&
1050 g_settings->getBool("free_move") &&
1051 m_gamedef->checkLocalPrivilege("fly"))))
1053 // slowdown speed if sneeking
1054 if(controls.sneak && walking)
1057 if(walking && (controls.LMB || controls.RMB))
1059 new_anim = player->local_animations[3];
1060 player->last_animation = WD_ANIM;
1061 } else if(walking) {
1062 new_anim = player->local_animations[1];
1063 player->last_animation = WALK_ANIM;
1064 } else if(controls.LMB || controls.RMB) {
1065 new_anim = player->local_animations[2];
1066 player->last_animation = DIG_ANIM;
1069 // Apply animations if input detected and not attached
1070 // or set idle animation
1071 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1073 allow_update = true;
1074 m_animation_range = new_anim;
1075 m_animation_speed = new_speed;
1076 player->last_animation_speed = m_animation_speed;
1078 player->last_animation = NO_ANIM;
1080 if (old_anim != NO_ANIM)
1082 m_animation_range = player->local_animations[0];
1087 // Update local player animations
1088 if ((player->last_animation != old_anim ||
1089 m_animation_speed != old_anim_speed) &&
1090 player->last_animation != NO_ANIM && allow_update)
1096 if(m_visuals_expired && m_smgr && m_irr){
1097 m_visuals_expired = false;
1099 // Attachments, part 1: All attached objects must be unparented first,
1100 // or Irrlicht causes a segmentation fault
1101 for(std::vector<u16>::iterator ci = m_children.begin();
1102 ci != m_children.end();)
1104 if (m_env->m_attachements[*ci] != getId()) {
1105 ci = m_children.erase(ci);
1108 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1110 scene::ISceneNode *child_node = obj->getSceneNode();
1112 child_node->setParent(m_smgr->getRootSceneNode());
1117 removeFromScene(false);
1118 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1120 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1121 for(std::vector<u16>::iterator ci = m_children.begin();
1122 ci != m_children.end(); ci++)
1124 // Get the object of the child
1125 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1127 obj->setAttachments();
1131 // Make sure m_is_visible is always applied
1132 scene::ISceneNode *node = getSceneNode();
1134 node->setVisible(m_is_visible);
1136 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1138 // Set these for later
1139 m_position = getPosition();
1140 m_velocity = v3f(0,0,0);
1141 m_acceleration = v3f(0,0,0);
1142 pos_translator.vect_show = m_position;
1144 if(m_is_local_player) // Update local player attachment position
1146 LocalPlayer *player = m_env->getLocalPlayer();
1147 player->overridePosition = getParent()->getPosition();
1148 m_env->getLocalPlayer()->parent = getParent();
1151 v3f lastpos = pos_translator.vect_show;
1155 core::aabbox3d<f32> box = m_prop.collisionbox;
1158 collisionMoveResult moveresult;
1159 f32 pos_max_d = BS*0.125; // Distance per iteration
1160 v3f p_pos = m_position;
1161 v3f p_velocity = m_velocity;
1162 v3f p_acceleration = m_acceleration;
1163 moveresult = collisionMoveSimple(env,env->getGameDef(),
1164 pos_max_d, box, m_prop.stepheight, dtime,
1165 p_pos, p_velocity, p_acceleration,
1166 this, m_prop.collideWithObjects);
1169 m_velocity = p_velocity;
1170 m_acceleration = p_acceleration;
1172 bool is_end_position = moveresult.collides;
1173 pos_translator.update(m_position, is_end_position, dtime);
1174 pos_translator.translate(dtime);
1177 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1178 m_velocity += dtime * m_acceleration;
1179 pos_translator.update(m_position, pos_translator.aim_is_end,
1180 pos_translator.anim_time);
1181 pos_translator.translate(dtime);
1185 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1186 m_step_distance_counter += moved;
1187 if(m_step_distance_counter > 1.5*BS)
1189 m_step_distance_counter = 0;
1190 if(!m_is_local_player && m_prop.makes_footstep_sound)
1192 INodeDefManager *ndef = m_gamedef->ndef();
1193 v3s16 p = floatToInt(getPosition() + v3f(0,
1194 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1195 MapNode n = m_env->getMap().getNodeNoEx(p);
1196 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1197 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1202 m_anim_timer += dtime;
1203 if(m_anim_timer >= m_anim_framelength)
1205 m_anim_timer -= m_anim_framelength;
1207 if(m_anim_frame >= m_anim_num_frames)
1213 if(m_reset_textures_timer >= 0)
1215 m_reset_textures_timer -= dtime;
1216 if(m_reset_textures_timer <= 0){
1217 m_reset_textures_timer = -1;
1221 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1223 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1227 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1228 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1230 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1231 + m_prop.automatic_face_movement_dir_offset;
1236 void GenericCAO::updateTexturePos()
1240 scene::ICameraSceneNode* camera =
1241 m_spritenode->getSceneManager()->getActiveCamera();
1244 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1245 - camera->getAbsolutePosition();
1246 cam_to_entity.normalize();
1248 int row = m_tx_basepos.Y;
1249 int col = m_tx_basepos.X;
1251 if(m_tx_select_horiz_by_yawpitch)
1253 if(cam_to_entity.Y > 0.75)
1255 else if(cam_to_entity.Y < -0.75)
1259 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1260 float dir = mob_dir - m_yaw;
1261 dir = wrapDegrees_180(dir);
1262 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1263 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1265 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1267 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1269 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1276 // Animation goes downwards
1277 row += m_anim_frame;
1279 float txs = m_tx_size.X;
1280 float tys = m_tx_size.Y;
1281 setBillboardTextureMatrix(m_spritenode,
1282 txs, tys, col, row);
1286 void GenericCAO::updateTextures(const std::string &mod)
1288 ITextureSource *tsrc = m_gamedef->tsrc();
1290 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1291 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1292 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1296 if(m_prop.visual == "sprite")
1298 std::string texturestring = "unknown_node.png";
1299 if(m_prop.textures.size() >= 1)
1300 texturestring = m_prop.textures[0];
1301 texturestring += mod;
1302 m_spritenode->setMaterialTexture(0,
1303 tsrc->getTexture(texturestring));
1305 // This allows setting per-material colors. However, until a real lighting
1306 // system is added, the code below will have no effect. Once MineTest
1307 // has directional lighting, it should work automatically.
1308 if(m_prop.colors.size() >= 1)
1310 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1311 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1312 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1315 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1316 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1317 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1320 if(m_animated_meshnode)
1322 if(m_prop.visual == "mesh")
1324 for (u32 i = 0; i < m_prop.textures.size() &&
1325 i < m_animated_meshnode->getMaterialCount(); ++i)
1327 std::string texturestring = m_prop.textures[i];
1328 if(texturestring == "")
1329 continue; // Empty texture string means don't modify that material
1330 texturestring += mod;
1331 video::ITexture* texture = tsrc->getTexture(texturestring);
1334 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1338 // Set material flags and texture
1339 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1340 material.TextureLayer[0].Texture = texture;
1341 material.setFlag(video::EMF_LIGHTING, false);
1342 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1344 m_animated_meshnode->getMaterial(i)
1345 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1346 m_animated_meshnode->getMaterial(i)
1347 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1348 m_animated_meshnode->getMaterial(i)
1349 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1351 for (u32 i = 0; i < m_prop.colors.size() &&
1352 i < m_animated_meshnode->getMaterialCount(); ++i)
1354 // This allows setting per-material colors. However, until a real lighting
1355 // system is added, the code below will have no effect. Once MineTest
1356 // has directional lighting, it should work automatically.
1357 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1358 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1359 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1365 if(m_prop.visual == "cube")
1367 for (u32 i = 0; i < 6; ++i)
1369 std::string texturestring = "unknown_node.png";
1370 if(m_prop.textures.size() > i)
1371 texturestring = m_prop.textures[i];
1372 texturestring += mod;
1375 // Set material flags and texture
1376 video::SMaterial& material = m_meshnode->getMaterial(i);
1377 material.setFlag(video::EMF_LIGHTING, false);
1378 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1379 material.setTexture(0,
1380 tsrc->getTexture(texturestring));
1381 material.getTextureMatrix(0).makeIdentity();
1383 // This allows setting per-material colors. However, until a real lighting
1384 // system is added, the code below will have no effect. Once MineTest
1385 // has directional lighting, it should work automatically.
1386 if(m_prop.colors.size() > i)
1388 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1389 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1390 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1393 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1394 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1395 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1398 else if(m_prop.visual == "upright_sprite")
1400 scene::IMesh *mesh = m_meshnode->getMesh();
1402 std::string tname = "unknown_object.png";
1403 if(m_prop.textures.size() >= 1)
1404 tname = m_prop.textures[0];
1406 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1407 buf->getMaterial().setTexture(0,
1408 tsrc->getTexture(tname));
1410 // This allows setting per-material colors. However, until a real lighting
1411 // system is added, the code below will have no effect. Once MineTest
1412 // has directional lighting, it should work automatically.
1413 if(m_prop.colors.size() >= 1)
1415 buf->getMaterial().AmbientColor = m_prop.colors[0];
1416 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1417 buf->getMaterial().SpecularColor = m_prop.colors[0];
1420 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1421 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1422 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1425 std::string tname = "unknown_object.png";
1426 if(m_prop.textures.size() >= 2)
1427 tname = m_prop.textures[1];
1428 else if(m_prop.textures.size() >= 1)
1429 tname = m_prop.textures[0];
1431 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1432 buf->getMaterial().setTexture(0,
1433 tsrc->getTexture(tname));
1435 // This allows setting per-material colors. However, until a real lighting
1436 // system is added, the code below will have no effect. Once MineTest
1437 // has directional lighting, it should work automatically.
1438 if(m_prop.colors.size() >= 2)
1440 buf->getMaterial().AmbientColor = m_prop.colors[1];
1441 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1442 buf->getMaterial().SpecularColor = m_prop.colors[1];
1444 else if(m_prop.colors.size() >= 1)
1446 buf->getMaterial().AmbientColor = m_prop.colors[0];
1447 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1448 buf->getMaterial().SpecularColor = m_prop.colors[0];
1451 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1452 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1453 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1459 void GenericCAO::updateAnimation()
1461 if(m_animated_meshnode == NULL)
1463 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1464 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1465 m_animated_meshnode->setTransitionTime(m_animation_blend);
1468 void GenericCAO::updateBonePosition()
1470 if(m_bone_position.empty() || m_animated_meshnode == NULL)
1473 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1474 for(std::map<std::string,
1475 core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1476 ii != m_bone_position.end(); ++ii)
1478 std::string bone_name = (*ii).first;
1479 v3f bone_pos = (*ii).second.X;
1480 v3f bone_rot = (*ii).second.Y;
1481 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1484 bone->setPosition(bone_pos);
1485 bone->setRotation(bone_rot);
1490 void GenericCAO::updateAttachments()
1493 // localplayer itself can't be attached to localplayer
1494 if (!m_is_local_player)
1496 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1497 // Objects attached to the local player should always be hidden
1498 m_is_visible = !m_attached_to_local;
1501 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1503 scene::ISceneNode *node = getSceneNode();
1505 v3f old_position = node->getAbsolutePosition();
1506 v3f old_rotation = node->getRotation();
1507 node->setParent(m_smgr->getRootSceneNode());
1508 node->setPosition(old_position);
1509 node->setRotation(old_rotation);
1510 node->updateAbsolutePosition();
1512 if (m_is_local_player) {
1513 LocalPlayer *player = m_env->getLocalPlayer();
1514 player->isAttached = false;
1519 scene::ISceneNode *my_node = getSceneNode();
1521 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1522 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1523 getParent()->getAnimatedMeshSceneNode();
1524 if (parent_animated_mesh_node && m_attachment_bone != "") {
1525 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1528 if (my_node && parent_node) {
1529 my_node->setParent(parent_node);
1530 my_node->setPosition(m_attachment_position);
1531 my_node->setRotation(m_attachment_rotation);
1532 my_node->updateAbsolutePosition();
1534 if (m_is_local_player) {
1535 LocalPlayer *player = m_env->getLocalPlayer();
1536 player->isAttached = true;
1541 void GenericCAO::processMessage(const std::string &data)
1543 //infostream<<"GenericCAO: Got message"<<std::endl;
1544 std::istringstream is(data, std::ios::binary);
1546 u8 cmd = readU8(is);
1547 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1549 m_prop = gob_read_set_properties(is);
1551 m_selection_box = m_prop.collisionbox;
1552 m_selection_box.MinEdge *= BS;
1553 m_selection_box.MaxEdge *= BS;
1555 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1556 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1558 if(!m_initial_tx_basepos_set){
1559 m_initial_tx_basepos_set = true;
1560 m_tx_basepos = m_prop.initial_sprite_basepos;
1565 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1567 // Not sent by the server if this object is an attachment.
1568 // We might however get here if the server notices the object being detached before the client.
1569 m_position = readV3F1000(is);
1570 m_velocity = readV3F1000(is);
1571 m_acceleration = readV3F1000(is);
1572 if(fabs(m_prop.automatic_rotate) < 0.001)
1573 m_yaw = readF1000(is);
1576 bool do_interpolate = readU8(is);
1577 bool is_end_position = readU8(is);
1578 float update_interval = readF1000(is);
1580 // Place us a bit higher if we're physical, to not sink into
1581 // the ground due to sucky collision detection...
1583 m_position += v3f(0,0.002,0);
1585 if(getParent() != NULL) // Just in case
1590 if(!m_prop.physical)
1591 pos_translator.update(m_position, is_end_position, update_interval);
1593 pos_translator.init(m_position);
1597 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1598 std::string mod = deSerializeString(is);
1599 updateTextures(mod);
1601 else if(cmd == GENERIC_CMD_SET_SPRITE) {
1602 v2s16 p = readV2S16(is);
1603 int num_frames = readU16(is);
1604 float framelength = readF1000(is);
1605 bool select_horiz_by_yawpitch = readU8(is);
1608 m_anim_num_frames = num_frames;
1609 m_anim_framelength = framelength;
1610 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1614 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1615 float override_speed = readF1000(is);
1616 float override_jump = readF1000(is);
1617 float override_gravity = readF1000(is);
1618 // these are sent inverted so we get true when the server sends nothing
1619 bool sneak = !readU8(is);
1620 bool sneak_glitch = !readU8(is);
1623 if(m_is_local_player)
1625 LocalPlayer *player = m_env->getLocalPlayer();
1626 player->physics_override_speed = override_speed;
1627 player->physics_override_jump = override_jump;
1628 player->physics_override_gravity = override_gravity;
1629 player->physics_override_sneak = sneak;
1630 player->physics_override_sneak_glitch = sneak_glitch;
1633 else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1634 // TODO: change frames send as v2s32 value
1635 v2f range = readV2F1000(is);
1636 if (!m_is_local_player) {
1637 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1638 m_animation_speed = readF1000(is);
1639 m_animation_blend = readF1000(is);
1642 LocalPlayer *player = m_env->getLocalPlayer();
1643 if(player->last_animation == NO_ANIM)
1645 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1646 m_animation_speed = readF1000(is);
1647 m_animation_blend = readF1000(is);
1649 // update animation only if local animations present
1650 // and received animation is unknown (except idle animation)
1651 bool is_known = false;
1652 for (int i = 1;i<4;i++)
1654 if(m_animation_range.Y == player->local_animations[i].Y)
1658 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1664 else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1665 std::string bone = deSerializeString(is);
1666 v3f position = readV3F1000(is);
1667 v3f rotation = readV3F1000(is);
1668 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1670 updateBonePosition();
1672 else if(cmd == GENERIC_CMD_SET_ATTACHMENT) {
1673 m_env->m_attachements[getId()] = readS16(is);
1674 m_children.push_back(m_env->m_attachements[getId()]);
1675 m_attachment_bone = deSerializeString(is);
1676 m_attachment_position = readV3F1000(is);
1677 m_attachment_rotation = readV3F1000(is);
1679 updateAttachments();
1681 else if(cmd == GENERIC_CMD_PUNCHED) {
1682 /*s16 damage =*/ readS16(is);
1683 s16 result_hp = readS16(is);
1685 // Use this instead of the send damage to not interfere with prediction
1686 s16 damage = m_hp - result_hp;
1694 // TODO: Execute defined fast response
1695 // As there is no definition, make a smoke puff
1696 ClientSimpleObject *simple = createSmokePuff(
1697 m_smgr, m_env, m_position,
1698 m_prop.visual_size * BS);
1699 m_env->addSimpleObject(simple);
1701 // TODO: Execute defined fast response
1702 // Flashing shall suffice as there is no definition
1703 m_reset_textures_timer = 0.05;
1705 m_reset_textures_timer += 0.05 * damage;
1706 updateTextures("^[brighten");
1710 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1711 m_armor_groups.clear();
1712 int armor_groups_size = readU16(is);
1713 for(int i=0; i<armor_groups_size; i++)
1715 std::string name = deSerializeString(is);
1716 int rating = readS16(is);
1717 m_armor_groups[name] = rating;
1722 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1723 float time_from_last_punch)
1726 const ToolCapabilities *toolcap =
1727 &punchitem->getToolCapabilities(m_gamedef->idef());
1728 PunchDamageResult result = getPunchDamage(
1732 time_from_last_punch);
1734 if(result.did_punch && result.damage != 0)
1736 if(result.damage < m_hp)
1738 m_hp -= result.damage;
1741 // TODO: Execute defined fast response
1742 // As there is no definition, make a smoke puff
1743 ClientSimpleObject *simple = createSmokePuff(
1744 m_smgr, m_env, m_position,
1745 m_prop.visual_size * BS);
1746 m_env->addSimpleObject(simple);
1748 // TODO: Execute defined fast response
1749 // Flashing shall suffice as there is no definition
1750 m_reset_textures_timer = 0.05;
1751 if(result.damage >= 2)
1752 m_reset_textures_timer += 0.05 * result.damage;
1753 updateTextures("^[brighten");
1759 std::string GenericCAO::debugInfoText()
1761 std::ostringstream os(std::ios::binary);
1762 os<<"GenericCAO hp="<<m_hp<<"\n";
1764 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1765 i != m_armor_groups.end(); i++)
1767 os<<i->first<<"="<<i->second<<", ";
1774 GenericCAO proto_GenericCAO(NULL, NULL);