3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "content_cao.h"
22 #include "environment.h"
23 #include "collision.h"
25 #include <ICameraSceneNode.h>
26 #include <ITextSceneNode.h>
27 #include <IBillboardSceneNode.h>
28 #include "serialization.h" // For decompressZlib
30 #include "clientobject.h"
31 #include "content_object.h"
35 #include "content_cso.h"
38 #include "localplayer.h"
39 #include "util/numeric.h" // For IntervalLimiter
40 #include "util/serialize.h"
41 #include "util/mathconstants.h"
43 #include "main.h" // g_settings
44 #include <IMeshManipulator.h>
45 #include <IAnimatedMeshSceneNode.h>
46 #include <IBoneSceneNode.h>
49 struct ToolCapabilities;
51 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
53 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
59 struct SmoothTranslator
66 f32 anim_time_counter;
86 anim_time_counter = 0;
95 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
97 aim_is_end = is_end_position;
100 if(update_interval > 0){
101 anim_time = update_interval;
103 if(anim_time < 0.001 || anim_time > 1.0)
104 anim_time = anim_time_counter;
106 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
108 anim_time_counter = 0;
112 void translate(f32 dtime)
114 anim_time_counter = anim_time_counter + dtime;
115 anim_counter = anim_counter + dtime;
116 v3f vect_move = vect_aim - vect_old;
118 if(anim_time > 0.001)
119 moveratio = anim_time_counter / anim_time;
120 // Move a bit less than should, to avoid oscillation
121 moveratio = moveratio * 0.8;
122 float move_end = 1.5;
125 if(moveratio > move_end)
126 moveratio = move_end;
127 vect_show = vect_old + vect_move * moveratio;
132 return ((anim_time_counter / anim_time) < 1.4);
140 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
141 float txs, float tys, int col, int row)
143 video::SMaterial& material = bill->getMaterial(0);
144 core::matrix4& matrix = material.getTextureMatrix(0);
145 matrix.setTextureTranslate(txs*col, tys*row);
146 matrix.setTextureScale(txs, tys);
153 class TestCAO : public ClientActiveObject
156 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
161 return ACTIVEOBJECT_TYPE_TEST;
164 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
166 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
167 IrrlichtDevice *irr);
168 void removeFromScene();
169 void updateLight(u8 light_at_pos);
170 v3s16 getLightPosition();
171 void updateNodePos();
173 void step(float dtime, ClientEnvironment *env);
175 void processMessage(const std::string &data);
178 scene::IMeshSceneNode *m_node;
183 TestCAO proto_TestCAO(NULL, NULL);
185 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
186 ClientActiveObject(0, gamedef, env),
188 m_position(v3f(0,10*BS,0))
190 ClientActiveObject::registerType(getType(), create);
197 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
199 return new TestCAO(gamedef, env);
202 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
208 //video::IVideoDriver* driver = smgr->getVideoDriver();
210 scene::SMesh *mesh = new scene::SMesh();
211 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
212 video::SColor c(255,255,255,255);
213 video::S3DVertex vertices[4] =
215 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
216 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
217 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
218 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
220 u16 indices[] = {0,1,2,2,3,0};
221 buf->append(vertices, 4, indices, 6);
223 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
224 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
225 buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
226 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
227 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
228 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
230 mesh->addMeshBuffer(buf);
232 m_node = smgr->addMeshSceneNode(mesh, NULL);
237 void TestCAO::removeFromScene()
246 void TestCAO::updateLight(u8 light_at_pos)
250 v3s16 TestCAO::getLightPosition()
252 return floatToInt(m_position, BS);
255 void TestCAO::updateNodePos()
260 m_node->setPosition(m_position);
261 //m_node->setRotation(v3f(0, 45, 0));
264 void TestCAO::step(float dtime, ClientEnvironment *env)
268 v3f rot = m_node->getRotation();
269 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
270 rot.Y += dtime * 180;
271 m_node->setRotation(rot);
275 void TestCAO::processMessage(const std::string &data)
277 infostream<<"TestCAO: Got data: "<<data<<std::endl;
278 std::istringstream is(data, std::ios::binary);
296 class ItemCAO : public ClientActiveObject
299 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
304 return ACTIVEOBJECT_TYPE_ITEM;
307 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
309 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
310 IrrlichtDevice *irr);
311 void removeFromScene();
312 void updateLight(u8 light_at_pos);
313 v3s16 getLightPosition();
314 void updateNodePos();
315 void updateInfoText();
316 void updateTexture();
318 void step(float dtime, ClientEnvironment *env);
320 void processMessage(const std::string &data);
322 void initialize(const std::string &data);
324 core::aabbox3d<f32>* getSelectionBox()
325 {return &m_selection_box;}
329 std::string infoText()
333 core::aabbox3d<f32> m_selection_box;
334 scene::IMeshSceneNode *m_node;
336 std::string m_itemstring;
337 std::string m_infotext;
340 #include "inventory.h"
343 ItemCAO proto_ItemCAO(NULL, NULL);
345 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
346 ClientActiveObject(0, gamedef, env),
347 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
349 m_position(v3f(0,10*BS,0))
353 ClientActiveObject::registerType(getType(), create);
361 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
363 return new ItemCAO(gamedef, env);
366 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
372 //video::IVideoDriver* driver = smgr->getVideoDriver();
374 scene::SMesh *mesh = new scene::SMesh();
375 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
376 video::SColor c(255,255,255,255);
377 video::S3DVertex vertices[4] =
379 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
380 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
381 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
382 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
383 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
384 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
385 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
386 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
388 u16 indices[] = {0,1,2,2,3,0};
389 buf->append(vertices, 4, indices, 6);
391 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
392 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
393 // Initialize with a generated placeholder texture
394 buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
395 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
396 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
397 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
399 mesh->addMeshBuffer(buf);
401 m_node = smgr->addMeshSceneNode(mesh, NULL);
412 void ItemCAO::removeFromScene()
421 void ItemCAO::updateLight(u8 light_at_pos)
426 u8 li = decode_light(light_at_pos);
427 video::SColor color(255,li,li,li);
428 setMeshColor(m_node->getMesh(), color);
431 v3s16 ItemCAO::getLightPosition()
433 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
436 void ItemCAO::updateNodePos()
441 m_node->setPosition(m_position);
444 void ItemCAO::updateInfoText()
447 IItemDefManager *idef = m_gamedef->idef();
449 item.deSerialize(m_itemstring, idef);
450 if(item.isKnown(idef))
451 m_infotext = item.getDefinition(idef).description;
453 m_infotext = "Unknown item: '" + m_itemstring + "'";
455 m_infotext += " (" + itos(item.count) + ")";
457 catch(SerializationError &e)
459 m_infotext = "Unknown item: '" + m_itemstring + "'";
463 void ItemCAO::updateTexture()
468 // Create an inventory item to see what is its image
469 std::istringstream is(m_itemstring, std::ios_base::binary);
470 video::ITexture *texture = NULL;
472 IItemDefManager *idef = m_gamedef->idef();
474 item.deSerialize(is, idef);
475 texture = item.getDefinition(idef).inventory_texture;
477 catch(SerializationError &e)
479 infostream<<"WARNING: "<<__FUNCTION_NAME
480 <<": error deSerializing itemstring \""
481 <<m_itemstring<<std::endl;
484 // Set meshbuffer texture
485 m_node->getMaterial(0).setTexture(0, texture);
489 void ItemCAO::step(float dtime, ClientEnvironment *env)
493 /*v3f rot = m_node->getRotation();
494 rot.Y += dtime * 120;
495 m_node->setRotation(rot);*/
496 LocalPlayer *player = env->getLocalPlayer();
498 v3f rot = m_node->getRotation();
499 rot.Y = 180.0 - (player->getYaw());
500 m_node->setRotation(rot);
504 void ItemCAO::processMessage(const std::string &data)
506 //infostream<<"ItemCAO: Got message"<<std::endl;
507 std::istringstream is(data, std::ios::binary);
513 m_position = readV3F1000(is);
519 m_itemstring = deSerializeString(is);
525 void ItemCAO::initialize(const std::string &data)
527 infostream<<"ItemCAO: Got init data"<<std::endl;
530 std::istringstream is(data, std::ios::binary);
532 u8 version = readU8(is);
537 m_position = readV3F1000(is);
539 m_itemstring = deSerializeString(is);
550 #include "genericobject.h"
552 class GenericCAO : public ClientActiveObject
555 // Only set at initialization
558 bool m_is_local_player;
560 // Property-ish things
561 ObjectProperties m_prop;
563 scene::ISceneManager *m_smgr;
564 IrrlichtDevice *m_irr;
565 core::aabbox3d<f32> m_selection_box;
566 scene::IMeshSceneNode *m_meshnode;
567 scene::IAnimatedMeshSceneNode *m_animated_meshnode;
568 scene::IBillboardSceneNode *m_spritenode;
569 scene::ITextSceneNode* m_textnode;
575 SmoothTranslator pos_translator;
576 // Spritesheet/animation stuff
579 bool m_initial_tx_basepos_set;
580 bool m_tx_select_horiz_by_yawpitch;
581 v2f m_animation_range;
582 int m_animation_speed;
583 int m_animation_blend;
584 std::map<std::string, core::vector2d<v3f> > m_bone_position; // stores position and rotation for each bone name
585 std::string m_attachment_bone;
586 v3f m_attachment_position;
587 v3f m_attachment_rotation;
588 bool m_attached_to_local;
590 int m_anim_num_frames;
591 float m_anim_framelength;
593 ItemGroupList m_armor_groups;
594 float m_reset_textures_timer;
595 bool m_visuals_expired;
596 float m_step_distance_counter;
601 GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
602 ClientActiveObject(0, gamedef, env),
605 m_is_local_player(false),
610 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
612 m_animated_meshnode(NULL),
615 m_position(v3f(0,10*BS,0)),
616 m_velocity(v3f(0,0,0)),
617 m_acceleration(v3f(0,0,0)),
622 m_initial_tx_basepos_set(false),
623 m_tx_select_horiz_by_yawpitch(false),
624 m_animation_range(v2f(0,0)),
625 m_animation_speed(15),
626 m_animation_blend(0),
627 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
628 m_attachment_bone(""),
629 m_attachment_position(v3f(0,0,0)),
630 m_attachment_rotation(v3f(0,0,0)),
631 m_attached_to_local(false),
633 m_anim_num_frames(1),
634 m_anim_framelength(0.2),
636 m_reset_textures_timer(-1),
637 m_visuals_expired(false),
638 m_step_distance_counter(0),
643 ClientActiveObject::registerType(getType(), create);
646 void initialize(const std::string &data)
648 infostream<<"GenericCAO: Got init data"<<std::endl;
649 std::istringstream is(data, std::ios::binary);
651 u8 version = readU8(is);
654 errorstream<<"GenericCAO: Unsupported init data version"
658 m_name = deSerializeString(is);
659 m_is_player = readU8(is);
661 m_position = readV3F1000(is);
662 m_yaw = readF1000(is);
665 int num_messages = readU8(is);
666 for(int i=0; i<num_messages; i++){
667 std::string message = deSerializeLongString(is);
668 processMessage(message);
671 pos_translator.init(m_position);
675 Player *player = m_env->getPlayer(m_name.c_str());
676 if(player && player->isLocal()){
677 m_is_local_player = true;
686 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
688 return new GenericCAO(gamedef, env);
693 return ACTIVEOBJECT_TYPE_GENERIC;
695 core::aabbox3d<f32>* getSelectionBox()
697 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
699 return &m_selection_box;
703 if(getParent() != NULL){
705 return m_meshnode->getAbsolutePosition();
706 if(m_animated_meshnode)
707 return m_animated_meshnode->getAbsolutePosition();
709 return m_spritenode->getAbsolutePosition();
712 return pos_translator.vect_show;
715 scene::IMeshSceneNode *getMeshSceneNode()
722 scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode()
724 if(m_animated_meshnode)
725 return m_animated_meshnode;
729 scene::IBillboardSceneNode *getSpriteSceneNode()
743 return m_is_local_player;
746 void setAttachments()
751 ClientActiveObject *getParent()
753 ClientActiveObject *obj = NULL;
754 for(std::vector<core::vector2d<int> >::const_iterator cii = m_env->attachment_list.begin(); cii != m_env->attachment_list.end(); cii++)
756 if(cii->X == getId()){ // This ID is our child
757 if(cii->Y > 0){ // A parent ID exists for our child
758 if(cii->X != cii->Y){ // The parent and child ID are not the same
759 obj = m_env->getActiveObject(cii->Y);
770 void removeFromScene(bool permanent)
772 if(permanent) // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
774 // Detach this object's children
775 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
777 if(ii->Y == getId()) // Is a child of our object
780 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
782 obj->setAttachments();
785 // Delete this object from the attachments list
786 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
788 if(ii->X == getId()) // Is our object
790 m_env->attachment_list.erase(ii);
797 m_meshnode->remove();
800 if(m_animated_meshnode){
801 m_animated_meshnode->remove();
802 m_animated_meshnode = NULL;
805 m_spritenode->remove();
810 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
816 if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
819 m_visuals_expired = false;
821 if(!m_prop.is_visible || m_is_local_player)
824 //video::IVideoDriver* driver = smgr->getVideoDriver();
826 if(m_prop.visual == "sprite"){
827 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
828 m_spritenode = smgr->addBillboardSceneNode(
829 NULL, v2f(1, 1), v3f(0,0,0), -1);
830 m_spritenode->setMaterialTexture(0,
831 tsrc->getTextureRaw("unknown_block.png"));
832 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
833 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
834 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
835 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
836 u8 li = m_last_light;
837 m_spritenode->setColor(video::SColor(255,li,li,li));
838 m_spritenode->setSize(m_prop.visual_size*BS);
840 const float txs = 1.0 / 1;
841 const float tys = 1.0 / 1;
842 setBillboardTextureMatrix(m_spritenode,
846 else if(m_prop.visual == "upright_sprite")
848 scene::SMesh *mesh = new scene::SMesh();
849 double dx = BS*m_prop.visual_size.X/2;
850 double dy = BS*m_prop.visual_size.Y/2;
852 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
853 u8 li = m_last_light;
854 video::SColor c(255,li,li,li);
855 video::S3DVertex vertices[4] =
857 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
858 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
859 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
860 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
862 u16 indices[] = {0,1,2,2,3,0};
863 buf->append(vertices, 4, indices, 6);
865 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
866 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
867 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
868 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
870 mesh->addMeshBuffer(buf);
874 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
875 u8 li = m_last_light;
876 video::SColor c(255,li,li,li);
877 video::S3DVertex vertices[4] =
879 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
880 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
881 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
882 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
884 u16 indices[] = {0,1,2,2,3,0};
885 buf->append(vertices, 4, indices, 6);
887 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
888 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
889 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
890 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
892 mesh->addMeshBuffer(buf);
895 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
897 // Set it to use the materials of the meshbuffers directly.
898 // This is needed for changing the texture in the future
899 m_meshnode->setReadOnlyMaterials(true);
901 else if(m_prop.visual == "cube"){
902 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
903 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
904 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
907 m_meshnode->setScale(v3f(m_prop.visual_size.X,
908 m_prop.visual_size.Y,
909 m_prop.visual_size.X));
910 u8 li = m_last_light;
911 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
913 else if(m_prop.visual == "mesh"){
914 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
915 scene::IAnimatedMesh *mesh = smgr->getMesh(m_prop.mesh.c_str());
918 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
919 m_animated_meshnode->animateJoints(); // Needed for some animations
920 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
921 m_prop.visual_size.Y,
922 m_prop.visual_size.X));
923 u8 li = m_last_light;
924 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
927 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
929 else if(m_prop.visual == "wielditem"){
930 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
931 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
932 if(m_prop.textures.size() >= 1){
933 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
934 IItemDefManager *idef = m_gamedef->idef();
935 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
936 scene::IMesh *item_mesh = item.getDefinition(idef).wield_mesh;
938 // Copy mesh to be able to set unique vertex colors
939 scene::IMeshManipulator *manip =
940 irr->getVideoDriver()->getMeshManipulator();
941 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
943 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
946 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
947 m_prop.visual_size.Y/2,
948 m_prop.visual_size.X/2));
949 u8 li = m_last_light;
950 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
953 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
954 <<"\" not supported"<<std::endl;
958 scene::ISceneNode *node = NULL;
961 else if(m_animated_meshnode)
962 node = m_animated_meshnode;
965 if(node && m_is_player && !m_is_local_player){
966 // Add a text node for showing the name
967 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
968 std::wstring wname = narrow_to_wide(m_name);
969 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
970 wname.c_str(), video::SColor(255,255,255,255), node);
971 m_textnode->setPosition(v3f(0, BS*1.1, 0));
976 updateBonePosition();
982 m_visuals_expired = true;
985 void updateLight(u8 light_at_pos)
987 u8 li = decode_light(light_at_pos);
988 if(li != m_last_light){
990 video::SColor color(255,li,li,li);
992 setMeshColor(m_meshnode->getMesh(), color);
993 if(m_animated_meshnode)
994 setMeshColor(m_animated_meshnode->getMesh(), color);
996 m_spritenode->setColor(color);
1000 v3s16 getLightPosition()
1002 return floatToInt(m_position, BS);
1005 void updateNodePos()
1007 if(getParent() != NULL)
1011 m_meshnode->setPosition(pos_translator.vect_show);
1012 v3f rot = m_meshnode->getRotation();
1014 m_meshnode->setRotation(rot);
1016 if(m_animated_meshnode){
1017 m_animated_meshnode->setPosition(pos_translator.vect_show);
1018 v3f rot = m_animated_meshnode->getRotation();
1020 m_animated_meshnode->setRotation(rot);
1023 m_spritenode->setPosition(pos_translator.vect_show);
1027 void step(float dtime, ClientEnvironment *env)
1029 if(m_visuals_expired && m_smgr && m_irr){
1030 m_visuals_expired = false;
1032 // Attachments, part 1: All attached objects must be unparented first, or Irrlicht causes a segmentation fault
1033 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1035 if(ii->Y == getId()) // This is a child of our parent
1037 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1040 scene::IMeshSceneNode *m_child_meshnode = obj->getMeshSceneNode();
1041 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode = obj->getAnimatedMeshSceneNode();
1042 scene::IBillboardSceneNode *m_child_spritenode = obj->getSpriteSceneNode();
1043 if(m_child_meshnode)
1044 m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1045 if(m_child_animated_meshnode)
1046 m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1047 if(m_child_spritenode)
1048 m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1053 removeFromScene(false);
1054 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1056 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1057 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1059 if(ii->Y == getId()) // This is a child of our parent
1061 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1063 obj->setAttachments();
1068 // Make sure m_is_visible is always applied
1070 m_meshnode->setVisible(m_is_visible);
1071 if(m_animated_meshnode)
1072 m_animated_meshnode->setVisible(m_is_visible);
1074 m_spritenode->setVisible(m_is_visible);
1076 m_textnode->setVisible(m_is_visible);
1078 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1080 // Set these for later
1081 m_position = getPosition();
1082 m_velocity = v3f(0,0,0);
1083 m_acceleration = v3f(0,0,0);
1084 pos_translator.vect_show = m_position;
1086 if(m_is_local_player) // Update local player attachment position
1088 LocalPlayer *player = m_env->getLocalPlayer();
1089 player->overridePosition = getParent()->getPosition();
1094 v3f lastpos = pos_translator.vect_show;
1096 if(m_prop.physical){
1097 core::aabbox3d<f32> box = m_prop.collisionbox;
1100 collisionMoveResult moveresult;
1101 f32 pos_max_d = BS*0.125; // Distance per iteration
1103 v3f p_pos = m_position;
1104 v3f p_velocity = m_velocity;
1105 v3f p_acceleration = m_acceleration;
1106 IGameDef *gamedef = env->getGameDef();
1107 moveresult = collisionMoveSimple(&env->getMap(), gamedef,
1108 pos_max_d, box, stepheight, dtime,
1109 p_pos, p_velocity, p_acceleration);
1112 m_velocity = p_velocity;
1113 m_acceleration = p_acceleration;
1115 bool is_end_position = moveresult.collides;
1116 pos_translator.update(m_position, is_end_position, dtime);
1117 pos_translator.translate(dtime);
1120 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1121 m_velocity += dtime * m_acceleration;
1122 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
1123 pos_translator.translate(dtime);
1127 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1128 m_step_distance_counter += moved;
1129 if(m_step_distance_counter > 1.5*BS){
1130 m_step_distance_counter = 0;
1131 if(!m_is_local_player && m_prop.makes_footstep_sound){
1132 INodeDefManager *ndef = m_gamedef->ndef();
1133 v3s16 p = floatToInt(getPosition() + v3f(0,
1134 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1135 MapNode n = m_env->getMap().getNodeNoEx(p);
1136 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1137 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1142 m_anim_timer += dtime;
1143 if(m_anim_timer >= m_anim_framelength){
1144 m_anim_timer -= m_anim_framelength;
1146 if(m_anim_frame >= m_anim_num_frames)
1152 if(m_reset_textures_timer >= 0){
1153 m_reset_textures_timer -= dtime;
1154 if(m_reset_textures_timer <= 0){
1155 m_reset_textures_timer = -1;
1159 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001){
1160 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1165 void updateTexturePos()
1168 scene::ICameraSceneNode* camera =
1169 m_spritenode->getSceneManager()->getActiveCamera();
1172 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1173 - camera->getAbsolutePosition();
1174 cam_to_entity.normalize();
1176 int row = m_tx_basepos.Y;
1177 int col = m_tx_basepos.X;
1179 if(m_tx_select_horiz_by_yawpitch)
1181 if(cam_to_entity.Y > 0.75)
1183 else if(cam_to_entity.Y < -0.75)
1186 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1187 float dir = mob_dir - m_yaw;
1188 dir = wrapDegrees_180(dir);
1189 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1190 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1192 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1194 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1196 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1203 // Animation goes downwards
1204 row += m_anim_frame;
1206 float txs = m_tx_size.X;
1207 float tys = m_tx_size.Y;
1208 setBillboardTextureMatrix(m_spritenode,
1209 txs, tys, col, row);
1213 void updateTextures(const std::string &mod)
1215 ITextureSource *tsrc = m_gamedef->tsrc();
1217 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1218 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1219 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1223 if(m_prop.visual == "sprite")
1225 std::string texturestring = "unknown_block.png";
1226 if(m_prop.textures.size() >= 1)
1227 texturestring = m_prop.textures[0];
1228 texturestring += mod;
1229 m_spritenode->setMaterialTexture(0,
1230 tsrc->getTextureRaw(texturestring));
1232 // This allows setting per-material colors. However, until a real lighting
1233 // system is added, the code below will have no effect. Once MineTest
1234 // has directional lighting, it should work automatically.
1235 if(m_prop.colors.size() >= 1)
1237 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1238 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1239 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1242 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1243 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1244 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1247 if(m_animated_meshnode)
1249 if(m_prop.visual == "mesh")
1251 for (u32 i = 0; i < m_prop.textures.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1253 std::string texturestring = m_prop.textures[i];
1254 if(texturestring == "")
1255 continue; // Empty texture string means don't modify that material
1256 texturestring += mod;
1257 video::ITexture* texture = tsrc->getTextureRaw(texturestring);
1260 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1264 // Set material flags and texture
1265 m_animated_meshnode->setMaterialTexture(i, texture);
1266 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1267 material.setFlag(video::EMF_LIGHTING, false);
1268 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1270 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1271 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1272 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1274 for (u32 i = 0; i < m_prop.colors.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1276 // This allows setting per-material colors. However, until a real lighting
1277 // system is added, the code below will have no effect. Once MineTest
1278 // has directional lighting, it should work automatically.
1279 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1280 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1281 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1287 if(m_prop.visual == "cube")
1289 for (u32 i = 0; i < 6; ++i)
1291 std::string texturestring = "unknown_block.png";
1292 if(m_prop.textures.size() > i)
1293 texturestring = m_prop.textures[i];
1294 texturestring += mod;
1295 AtlasPointer ap = tsrc->getTexture(texturestring);
1297 // Get the tile texture and atlas transformation
1298 video::ITexture* atlas = ap.atlas;
1302 // Set material flags and texture
1303 video::SMaterial& material = m_meshnode->getMaterial(i);
1304 material.setFlag(video::EMF_LIGHTING, false);
1305 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1306 material.setTexture(0, atlas);
1307 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1308 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1310 // This allows setting per-material colors. However, until a real lighting
1311 // system is added, the code below will have no effect. Once MineTest
1312 // has directional lighting, it should work automatically.
1313 if(m_prop.colors.size() > i)
1315 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1316 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1317 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1320 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1321 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1322 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1325 else if(m_prop.visual == "upright_sprite")
1327 scene::IMesh *mesh = m_meshnode->getMesh();
1329 std::string tname = "unknown_object.png";
1330 if(m_prop.textures.size() >= 1)
1331 tname = m_prop.textures[0];
1333 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1334 buf->getMaterial().setTexture(0,
1335 tsrc->getTextureRaw(tname));
1337 // This allows setting per-material colors. However, until a real lighting
1338 // system is added, the code below will have no effect. Once MineTest
1339 // has directional lighting, it should work automatically.
1340 if(m_prop.colors.size() >= 1)
1342 buf->getMaterial().AmbientColor = m_prop.colors[0];
1343 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1344 buf->getMaterial().SpecularColor = m_prop.colors[0];
1347 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1348 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1349 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1352 std::string tname = "unknown_object.png";
1353 if(m_prop.textures.size() >= 2)
1354 tname = m_prop.textures[1];
1355 else if(m_prop.textures.size() >= 1)
1356 tname = m_prop.textures[0];
1358 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1359 buf->getMaterial().setTexture(0,
1360 tsrc->getTextureRaw(tname));
1362 // This allows setting per-material colors. However, until a real lighting
1363 // system is added, the code below will have no effect. Once MineTest
1364 // has directional lighting, it should work automatically.
1365 if(m_prop.colors.size() >= 2)
1367 buf->getMaterial().AmbientColor = m_prop.colors[1];
1368 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1369 buf->getMaterial().SpecularColor = m_prop.colors[1];
1371 else if(m_prop.colors.size() >= 1)
1373 buf->getMaterial().AmbientColor = m_prop.colors[0];
1374 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1375 buf->getMaterial().SpecularColor = m_prop.colors[0];
1378 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1379 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1380 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1386 void updateAnimation()
1388 if(m_animated_meshnode == NULL)
1391 m_animated_meshnode->setFrameLoop((int)m_animation_range.X, (int)m_animation_range.Y);
1392 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1393 m_animated_meshnode->setTransitionTime(m_animation_blend);
1396 void updateBonePosition()
1398 if(!m_bone_position.size() || m_animated_meshnode == NULL)
1401 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1402 for(std::map<std::string, core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){
1403 std::string bone_name = (*ii).first;
1404 v3f bone_pos = (*ii).second.X;
1405 v3f bone_rot = (*ii).second.Y;
1406 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1409 bone->setPosition(bone_pos);
1410 bone->setRotation(bone_rot);
1415 void updateAttachments()
1417 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1418 m_is_visible = !m_attached_to_local; // Objects attached to the local player should always be hidden
1420 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1424 v3f old_position = m_meshnode->getAbsolutePosition();
1425 v3f old_rotation = m_meshnode->getRotation();
1426 m_meshnode->setParent(m_smgr->getRootSceneNode());
1427 m_meshnode->setPosition(old_position);
1428 m_meshnode->setRotation(old_rotation);
1429 m_meshnode->updateAbsolutePosition();
1431 if(m_animated_meshnode)
1433 v3f old_position = m_animated_meshnode->getAbsolutePosition();
1434 v3f old_rotation = m_animated_meshnode->getRotation();
1435 m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1436 m_animated_meshnode->setPosition(old_position);
1437 m_animated_meshnode->setRotation(old_rotation);
1438 m_animated_meshnode->updateAbsolutePosition();
1442 v3f old_position = m_spritenode->getAbsolutePosition();
1443 v3f old_rotation = m_spritenode->getRotation();
1444 m_spritenode->setParent(m_smgr->getRootSceneNode());
1445 m_spritenode->setPosition(old_position);
1446 m_spritenode->setRotation(old_rotation);
1447 m_spritenode->updateAbsolutePosition();
1449 if(m_is_local_player)
1451 LocalPlayer *player = m_env->getLocalPlayer();
1452 player->isAttached = false;
1457 scene::IMeshSceneNode *parent_mesh = NULL;
1458 if(getParent()->getMeshSceneNode())
1459 parent_mesh = getParent()->getMeshSceneNode();
1460 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1461 if(getParent()->getAnimatedMeshSceneNode())
1462 parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1463 scene::IBillboardSceneNode *parent_sprite = NULL;
1464 if(getParent()->getSpriteSceneNode())
1465 parent_sprite = getParent()->getSpriteSceneNode();
1467 scene::IBoneSceneNode *parent_bone = NULL;
1468 if(parent_animated_mesh && m_attachment_bone != "")
1469 parent_bone = parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1471 // The spaghetti code below makes sure attaching works if either the parent or child is a spritenode, meshnode, or animatedmeshnode
1472 // TODO: Perhaps use polymorphism here to save code duplication
1475 m_meshnode->setParent(parent_bone);
1476 m_meshnode->setPosition(m_attachment_position);
1477 m_meshnode->setRotation(m_attachment_rotation);
1478 m_meshnode->updateAbsolutePosition();
1483 m_meshnode->setParent(parent_mesh);
1484 m_meshnode->setPosition(m_attachment_position);
1485 m_meshnode->setRotation(m_attachment_rotation);
1486 m_meshnode->updateAbsolutePosition();
1488 else if(parent_animated_mesh){
1489 m_meshnode->setParent(parent_animated_mesh);
1490 m_meshnode->setPosition(m_attachment_position);
1491 m_meshnode->setRotation(m_attachment_rotation);
1492 m_meshnode->updateAbsolutePosition();
1494 else if(parent_sprite){
1495 m_meshnode->setParent(parent_sprite);
1496 m_meshnode->setPosition(m_attachment_position);
1497 m_meshnode->setRotation(m_attachment_rotation);
1498 m_meshnode->updateAbsolutePosition();
1502 if(m_animated_meshnode){
1504 m_animated_meshnode->setParent(parent_bone);
1505 m_animated_meshnode->setPosition(m_attachment_position);
1506 m_animated_meshnode->setRotation(m_attachment_rotation);
1507 m_animated_meshnode->updateAbsolutePosition();
1512 m_animated_meshnode->setParent(parent_mesh);
1513 m_animated_meshnode->setPosition(m_attachment_position);
1514 m_animated_meshnode->setRotation(m_attachment_rotation);
1515 m_animated_meshnode->updateAbsolutePosition();
1517 else if(parent_animated_mesh){
1518 m_animated_meshnode->setParent(parent_animated_mesh);
1519 m_animated_meshnode->setPosition(m_attachment_position);
1520 m_animated_meshnode->setRotation(m_attachment_rotation);
1521 m_animated_meshnode->updateAbsolutePosition();
1523 else if(parent_sprite){
1524 m_animated_meshnode->setParent(parent_sprite);
1525 m_animated_meshnode->setPosition(m_attachment_position);
1526 m_animated_meshnode->setRotation(m_attachment_rotation);
1527 m_animated_meshnode->updateAbsolutePosition();
1533 m_spritenode->setParent(parent_bone);
1534 m_spritenode->setPosition(m_attachment_position);
1535 m_spritenode->setRotation(m_attachment_rotation);
1536 m_spritenode->updateAbsolutePosition();
1541 m_spritenode->setParent(parent_mesh);
1542 m_spritenode->setPosition(m_attachment_position);
1543 m_spritenode->setRotation(m_attachment_rotation);
1544 m_spritenode->updateAbsolutePosition();
1546 else if(parent_animated_mesh){
1547 m_spritenode->setParent(parent_animated_mesh);
1548 m_spritenode->setPosition(m_attachment_position);
1549 m_spritenode->setRotation(m_attachment_rotation);
1550 m_spritenode->updateAbsolutePosition();
1552 else if(parent_sprite){
1553 m_spritenode->setParent(parent_sprite);
1554 m_spritenode->setPosition(m_attachment_position);
1555 m_spritenode->setRotation(m_attachment_rotation);
1556 m_spritenode->updateAbsolutePosition();
1560 if(m_is_local_player)
1562 LocalPlayer *player = m_env->getLocalPlayer();
1563 player->isAttached = true;
1568 void processMessage(const std::string &data)
1570 //infostream<<"GenericCAO: Got message"<<std::endl;
1571 std::istringstream is(data, std::ios::binary);
1573 u8 cmd = readU8(is);
1574 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1576 m_prop = gob_read_set_properties(is);
1578 m_selection_box = m_prop.collisionbox;
1579 m_selection_box.MinEdge *= BS;
1580 m_selection_box.MaxEdge *= BS;
1582 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1583 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1585 if(!m_initial_tx_basepos_set){
1586 m_initial_tx_basepos_set = true;
1587 m_tx_basepos = m_prop.initial_sprite_basepos;
1592 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1594 // Not sent by the server if this object is an attachment.
1595 // We might however get here if the server notices the object being detached before the client.
1596 m_position = readV3F1000(is);
1597 m_velocity = readV3F1000(is);
1598 m_acceleration = readV3F1000(is);
1599 if(fabs(m_prop.automatic_rotate) < 0.001)
1600 m_yaw = readF1000(is);
1601 bool do_interpolate = readU8(is);
1602 bool is_end_position = readU8(is);
1603 float update_interval = readF1000(is);
1605 // Place us a bit higher if we're physical, to not sink into
1606 // the ground due to sucky collision detection...
1608 m_position += v3f(0,0.002,0);
1610 if(getParent() != NULL) // Just in case
1614 if(!m_prop.physical)
1615 pos_translator.update(m_position, is_end_position, update_interval);
1617 pos_translator.init(m_position);
1621 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1623 std::string mod = deSerializeString(is);
1624 updateTextures(mod);
1626 else if(cmd == GENERIC_CMD_SET_SPRITE)
1628 v2s16 p = readV2S16(is);
1629 int num_frames = readU16(is);
1630 float framelength = readF1000(is);
1631 bool select_horiz_by_yawpitch = readU8(is);
1634 m_anim_num_frames = num_frames;
1635 m_anim_framelength = framelength;
1636 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1640 else if(cmd == GENERIC_CMD_SET_ANIMATION)
1642 m_animation_range = readV2F1000(is);
1643 m_animation_speed = readF1000(is);
1644 m_animation_blend = readF1000(is);
1648 else if(cmd == GENERIC_CMD_SET_BONE_POSITION)
1650 std::string bone = deSerializeString(is);
1651 v3f position = readV3F1000(is);
1652 v3f rotation = readV3F1000(is);
1653 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1655 updateBonePosition();
1657 else if(cmd == GENERIC_CMD_SET_ATTACHMENT)
1659 // If an entry already exists for this object, delete it first to avoid duplicates
1660 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1662 if(ii->X == getId()) // This is the ID of our object
1664 m_env->attachment_list.erase(ii);
1668 m_env->attachment_list.push_back(core::vector2d<int>(getId(), readS16(is)));
1669 m_attachment_bone = deSerializeString(is);
1670 m_attachment_position = readV3F1000(is);
1671 m_attachment_rotation = readV3F1000(is);
1673 updateAttachments();
1675 else if(cmd == GENERIC_CMD_PUNCHED)
1677 /*s16 damage =*/ readS16(is);
1678 s16 result_hp = readS16(is);
1682 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1684 m_armor_groups.clear();
1685 int armor_groups_size = readU16(is);
1686 for(int i=0; i<armor_groups_size; i++){
1687 std::string name = deSerializeString(is);
1688 int rating = readS16(is);
1689 m_armor_groups[name] = rating;
1694 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1695 float time_from_last_punch=1000000)
1698 const ToolCapabilities *toolcap =
1699 &punchitem->getToolCapabilities(m_gamedef->idef());
1700 PunchDamageResult result = getPunchDamage(
1704 time_from_last_punch);
1706 if(result.did_punch && result.damage != 0)
1708 if(result.damage < m_hp){
1709 m_hp -= result.damage;
1712 // TODO: Execute defined fast response
1713 // As there is no definition, make a smoke puff
1714 ClientSimpleObject *simple = createSmokePuff(
1715 m_smgr, m_env, m_position,
1716 m_prop.visual_size * BS);
1717 m_env->addSimpleObject(simple);
1719 // TODO: Execute defined fast response
1720 // Flashing shall suffice as there is no definition
1721 m_reset_textures_timer = 0.05;
1722 if(result.damage >= 2)
1723 m_reset_textures_timer += 0.05 * result.damage;
1724 updateTextures("^[brighten");
1730 std::string debugInfoText()
1732 std::ostringstream os(std::ios::binary);
1733 os<<"GenericCAO hp="<<m_hp<<"\n";
1735 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1736 i != m_armor_groups.end(); i++){
1737 os<<i->first<<"="<<i->second<<", ";
1745 GenericCAO proto_GenericCAO(NULL, NULL);