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 "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 std::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 = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
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);
650 int num_messages = 0;
652 u8 version = readU8(is);
654 if(version == 1) // In PROTOCOL_VERSION 14
656 m_name = deSerializeString(is);
657 m_is_player = readU8(is);
659 m_position = readV3F1000(is);
660 m_yaw = readF1000(is);
662 num_messages = readU8(is);
664 else if(version == 0) // In PROTOCOL_VERSION 13
666 m_name = deSerializeString(is);
667 m_is_player = readU8(is);
668 m_position = readV3F1000(is);
669 m_yaw = readF1000(is);
671 num_messages = readU8(is);
675 errorstream<<"GenericCAO: Unsupported init data version"
680 for(int i=0; i<num_messages; i++){
681 std::string message = deSerializeLongString(is);
682 processMessage(message);
685 pos_translator.init(m_position);
689 Player *player = m_env->getPlayer(m_name.c_str());
690 if(player && player->isLocal()){
691 m_is_local_player = true;
700 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
702 return new GenericCAO(gamedef, env);
707 return ACTIVEOBJECT_TYPE_GENERIC;
709 core::aabbox3d<f32>* getSelectionBox()
711 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
713 return &m_selection_box;
717 if(getParent() != NULL){
719 return m_meshnode->getAbsolutePosition();
720 if(m_animated_meshnode)
721 return m_animated_meshnode->getAbsolutePosition();
723 return m_spritenode->getAbsolutePosition();
726 return pos_translator.vect_show;
729 scene::IMeshSceneNode *getMeshSceneNode()
736 scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode()
738 if(m_animated_meshnode)
739 return m_animated_meshnode;
743 scene::IBillboardSceneNode *getSpriteSceneNode()
757 return m_is_local_player;
760 void setAttachments()
765 ClientActiveObject *getParent()
767 ClientActiveObject *obj = NULL;
768 for(std::vector<core::vector2d<int> >::const_iterator cii = m_env->attachment_list.begin(); cii != m_env->attachment_list.end(); cii++)
770 if(cii->X == getId()){ // This ID is our child
771 if(cii->Y > 0){ // A parent ID exists for our child
772 if(cii->X != cii->Y){ // The parent and child ID are not the same
773 obj = m_env->getActiveObject(cii->Y);
784 void removeFromScene(bool permanent)
786 if(permanent) // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
788 // Detach this object's children
789 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
791 if(ii->Y == getId()) // Is a child of our object
794 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
796 obj->setAttachments();
799 // Delete this object from the attachments list
800 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
802 if(ii->X == getId()) // Is our object
804 m_env->attachment_list.erase(ii);
811 m_meshnode->remove();
814 if(m_animated_meshnode){
815 m_animated_meshnode->remove();
816 m_animated_meshnode = NULL;
819 m_spritenode->remove();
824 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
830 if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
833 m_visuals_expired = false;
835 if(!m_prop.is_visible || m_is_local_player)
838 //video::IVideoDriver* driver = smgr->getVideoDriver();
840 if(m_prop.visual == "sprite"){
841 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
842 m_spritenode = smgr->addBillboardSceneNode(
843 NULL, v2f(1, 1), v3f(0,0,0), -1);
844 m_spritenode->setMaterialTexture(0,
845 tsrc->getTextureRaw("unknown_block.png"));
846 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
847 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
848 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
849 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
850 u8 li = m_last_light;
851 m_spritenode->setColor(video::SColor(255,li,li,li));
852 m_spritenode->setSize(m_prop.visual_size*BS);
854 const float txs = 1.0 / 1;
855 const float tys = 1.0 / 1;
856 setBillboardTextureMatrix(m_spritenode,
860 else if(m_prop.visual == "upright_sprite")
862 scene::SMesh *mesh = new scene::SMesh();
863 double dx = BS*m_prop.visual_size.X/2;
864 double dy = BS*m_prop.visual_size.Y/2;
866 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
867 u8 li = m_last_light;
868 video::SColor c(255,li,li,li);
869 video::S3DVertex vertices[4] =
871 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
872 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
873 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
874 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
876 u16 indices[] = {0,1,2,2,3,0};
877 buf->append(vertices, 4, indices, 6);
879 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
880 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
881 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
882 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
884 mesh->addMeshBuffer(buf);
888 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
889 u8 li = m_last_light;
890 video::SColor c(255,li,li,li);
891 video::S3DVertex vertices[4] =
893 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
894 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
895 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
896 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
898 u16 indices[] = {0,1,2,2,3,0};
899 buf->append(vertices, 4, indices, 6);
901 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
902 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
903 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
904 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
906 mesh->addMeshBuffer(buf);
909 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
911 // Set it to use the materials of the meshbuffers directly.
912 // This is needed for changing the texture in the future
913 m_meshnode->setReadOnlyMaterials(true);
915 else if(m_prop.visual == "cube"){
916 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
917 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
918 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
921 m_meshnode->setScale(v3f(m_prop.visual_size.X,
922 m_prop.visual_size.Y,
923 m_prop.visual_size.X));
924 u8 li = m_last_light;
925 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
927 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
928 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
929 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
930 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
932 else if(m_prop.visual == "mesh"){
933 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
934 scene::IAnimatedMesh *mesh = smgr->getMesh(m_prop.mesh.c_str());
937 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
938 m_animated_meshnode->animateJoints(); // Needed for some animations
939 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
940 m_prop.visual_size.Y,
941 m_prop.visual_size.X));
942 u8 li = m_last_light;
943 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
945 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
946 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
947 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
948 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
951 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
953 else if(m_prop.visual == "wielditem"){
954 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
955 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
956 if(m_prop.textures.size() >= 1){
957 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
958 IItemDefManager *idef = m_gamedef->idef();
959 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
960 scene::IMesh *item_mesh = idef->getWieldMesh(item.getDefinition(idef).name, m_gamedef);
962 // Copy mesh to be able to set unique vertex colors
963 scene::IMeshManipulator *manip =
964 irr->getVideoDriver()->getMeshManipulator();
965 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
967 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
970 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
971 m_prop.visual_size.Y/2,
972 m_prop.visual_size.X/2));
973 u8 li = m_last_light;
974 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
977 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
978 <<"\" not supported"<<std::endl;
982 scene::ISceneNode *node = NULL;
985 else if(m_animated_meshnode)
986 node = m_animated_meshnode;
989 if(node && m_is_player && !m_is_local_player){
990 // Add a text node for showing the name
991 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
992 std::wstring wname = narrow_to_wide(m_name);
993 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
994 wname.c_str(), video::SColor(255,255,255,255), node);
995 m_textnode->setPosition(v3f(0, BS*1.1, 0));
1000 updateBonePosition();
1001 updateAttachments();
1004 void expireVisuals()
1006 m_visuals_expired = true;
1009 void updateLight(u8 light_at_pos)
1011 u8 li = decode_light(light_at_pos);
1012 if(li != m_last_light){
1014 video::SColor color(255,li,li,li);
1016 setMeshColor(m_meshnode->getMesh(), color);
1017 if(m_animated_meshnode)
1018 setMeshColor(m_animated_meshnode->getMesh(), color);
1020 m_spritenode->setColor(color);
1024 v3s16 getLightPosition()
1026 return floatToInt(m_position, BS);
1029 void updateNodePos()
1031 if(getParent() != NULL)
1035 m_meshnode->setPosition(pos_translator.vect_show);
1036 v3f rot = m_meshnode->getRotation();
1038 m_meshnode->setRotation(rot);
1040 if(m_animated_meshnode){
1041 m_animated_meshnode->setPosition(pos_translator.vect_show);
1042 v3f rot = m_animated_meshnode->getRotation();
1044 m_animated_meshnode->setRotation(rot);
1047 m_spritenode->setPosition(pos_translator.vect_show);
1051 void step(float dtime, ClientEnvironment *env)
1053 if(m_visuals_expired && m_smgr && m_irr){
1054 m_visuals_expired = false;
1056 // Attachments, part 1: All attached objects must be unparented first, or Irrlicht causes a segmentation fault
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
1064 scene::IMeshSceneNode *m_child_meshnode = obj->getMeshSceneNode();
1065 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode = obj->getAnimatedMeshSceneNode();
1066 scene::IBillboardSceneNode *m_child_spritenode = obj->getSpriteSceneNode();
1067 if(m_child_meshnode)
1068 m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1069 if(m_child_animated_meshnode)
1070 m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1071 if(m_child_spritenode)
1072 m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1077 removeFromScene(false);
1078 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1080 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1081 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1083 if(ii->Y == getId()) // This is a child of our parent
1085 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1087 obj->setAttachments();
1092 // Make sure m_is_visible is always applied
1094 m_meshnode->setVisible(m_is_visible);
1095 if(m_animated_meshnode)
1096 m_animated_meshnode->setVisible(m_is_visible);
1098 m_spritenode->setVisible(m_is_visible);
1100 m_textnode->setVisible(m_is_visible);
1102 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1104 // Set these for later
1105 m_position = getPosition();
1106 m_velocity = v3f(0,0,0);
1107 m_acceleration = v3f(0,0,0);
1108 pos_translator.vect_show = m_position;
1110 if(m_is_local_player) // Update local player attachment position
1112 LocalPlayer *player = m_env->getLocalPlayer();
1113 player->overridePosition = getParent()->getPosition();
1118 v3f lastpos = pos_translator.vect_show;
1120 if(m_prop.physical){
1121 core::aabbox3d<f32> box = m_prop.collisionbox;
1124 collisionMoveResult moveresult;
1125 f32 pos_max_d = BS*0.125; // Distance per iteration
1127 v3f p_pos = m_position;
1128 v3f p_velocity = m_velocity;
1129 v3f p_acceleration = m_acceleration;
1130 IGameDef *gamedef = env->getGameDef();
1131 moveresult = collisionMoveSimple(&env->getMap(), gamedef,
1132 pos_max_d, box, stepheight, dtime,
1133 p_pos, p_velocity, p_acceleration);
1136 m_velocity = p_velocity;
1137 m_acceleration = p_acceleration;
1139 bool is_end_position = moveresult.collides;
1140 pos_translator.update(m_position, is_end_position, dtime);
1141 pos_translator.translate(dtime);
1144 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1145 m_velocity += dtime * m_acceleration;
1146 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
1147 pos_translator.translate(dtime);
1151 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1152 m_step_distance_counter += moved;
1153 if(m_step_distance_counter > 1.5*BS){
1154 m_step_distance_counter = 0;
1155 if(!m_is_local_player && m_prop.makes_footstep_sound){
1156 INodeDefManager *ndef = m_gamedef->ndef();
1157 v3s16 p = floatToInt(getPosition() + v3f(0,
1158 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1159 MapNode n = m_env->getMap().getNodeNoEx(p);
1160 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1161 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1166 m_anim_timer += dtime;
1167 if(m_anim_timer >= m_anim_framelength){
1168 m_anim_timer -= m_anim_framelength;
1170 if(m_anim_frame >= m_anim_num_frames)
1176 if(m_reset_textures_timer >= 0){
1177 m_reset_textures_timer -= dtime;
1178 if(m_reset_textures_timer <= 0){
1179 m_reset_textures_timer = -1;
1183 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001){
1184 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1189 void updateTexturePos()
1192 scene::ICameraSceneNode* camera =
1193 m_spritenode->getSceneManager()->getActiveCamera();
1196 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1197 - camera->getAbsolutePosition();
1198 cam_to_entity.normalize();
1200 int row = m_tx_basepos.Y;
1201 int col = m_tx_basepos.X;
1203 if(m_tx_select_horiz_by_yawpitch)
1205 if(cam_to_entity.Y > 0.75)
1207 else if(cam_to_entity.Y < -0.75)
1210 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1211 float dir = mob_dir - m_yaw;
1212 dir = wrapDegrees_180(dir);
1213 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1214 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1216 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1218 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1220 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1227 // Animation goes downwards
1228 row += m_anim_frame;
1230 float txs = m_tx_size.X;
1231 float tys = m_tx_size.Y;
1232 setBillboardTextureMatrix(m_spritenode,
1233 txs, tys, col, row);
1237 void updateTextures(const std::string &mod)
1239 ITextureSource *tsrc = m_gamedef->tsrc();
1241 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1242 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1243 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1247 if(m_prop.visual == "sprite")
1249 std::string texturestring = "unknown_block.png";
1250 if(m_prop.textures.size() >= 1)
1251 texturestring = m_prop.textures[0];
1252 texturestring += mod;
1253 m_spritenode->setMaterialTexture(0,
1254 tsrc->getTextureRaw(texturestring));
1256 // This allows setting per-material colors. However, until a real lighting
1257 // system is added, the code below will have no effect. Once MineTest
1258 // has directional lighting, it should work automatically.
1259 if(m_prop.colors.size() >= 1)
1261 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1262 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1263 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1266 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1267 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1268 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1271 if(m_animated_meshnode)
1273 if(m_prop.visual == "mesh")
1275 for (u32 i = 0; i < m_prop.textures.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1277 std::string texturestring = m_prop.textures[i];
1278 if(texturestring == "")
1279 continue; // Empty texture string means don't modify that material
1280 texturestring += mod;
1281 video::ITexture* texture = tsrc->getTextureRaw(texturestring);
1284 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1288 // Set material flags and texture
1289 m_animated_meshnode->setMaterialTexture(i, texture);
1290 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1291 material.setFlag(video::EMF_LIGHTING, false);
1292 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1294 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1295 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1296 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1298 for (u32 i = 0; i < m_prop.colors.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1300 // This allows setting per-material colors. However, until a real lighting
1301 // system is added, the code below will have no effect. Once MineTest
1302 // has directional lighting, it should work automatically.
1303 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1304 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1305 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1311 if(m_prop.visual == "cube")
1313 for (u32 i = 0; i < 6; ++i)
1315 std::string texturestring = "unknown_block.png";
1316 if(m_prop.textures.size() > i)
1317 texturestring = m_prop.textures[i];
1318 texturestring += mod;
1319 AtlasPointer ap = tsrc->getTexture(texturestring);
1321 // Get the tile texture and atlas transformation
1322 video::ITexture* atlas = ap.atlas;
1326 // Set material flags and texture
1327 video::SMaterial& material = m_meshnode->getMaterial(i);
1328 material.setFlag(video::EMF_LIGHTING, false);
1329 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1330 material.setTexture(0, atlas);
1331 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1332 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1334 // This allows setting per-material colors. However, until a real lighting
1335 // system is added, the code below will have no effect. Once MineTest
1336 // has directional lighting, it should work automatically.
1337 if(m_prop.colors.size() > i)
1339 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1340 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1341 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1344 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1345 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1346 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1349 else if(m_prop.visual == "upright_sprite")
1351 scene::IMesh *mesh = m_meshnode->getMesh();
1353 std::string tname = "unknown_object.png";
1354 if(m_prop.textures.size() >= 1)
1355 tname = m_prop.textures[0];
1357 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1358 buf->getMaterial().setTexture(0,
1359 tsrc->getTextureRaw(tname));
1361 // This allows setting per-material colors. However, until a real lighting
1362 // system is added, the code below will have no effect. Once MineTest
1363 // has directional lighting, it should work automatically.
1364 if(m_prop.colors.size() >= 1)
1366 buf->getMaterial().AmbientColor = m_prop.colors[0];
1367 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1368 buf->getMaterial().SpecularColor = m_prop.colors[0];
1371 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1372 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1373 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1376 std::string tname = "unknown_object.png";
1377 if(m_prop.textures.size() >= 2)
1378 tname = m_prop.textures[1];
1379 else if(m_prop.textures.size() >= 1)
1380 tname = m_prop.textures[0];
1382 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1383 buf->getMaterial().setTexture(0,
1384 tsrc->getTextureRaw(tname));
1386 // This allows setting per-material colors. However, until a real lighting
1387 // system is added, the code below will have no effect. Once MineTest
1388 // has directional lighting, it should work automatically.
1389 if(m_prop.colors.size() >= 2)
1391 buf->getMaterial().AmbientColor = m_prop.colors[1];
1392 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1393 buf->getMaterial().SpecularColor = m_prop.colors[1];
1395 else if(m_prop.colors.size() >= 1)
1397 buf->getMaterial().AmbientColor = m_prop.colors[0];
1398 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1399 buf->getMaterial().SpecularColor = m_prop.colors[0];
1402 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1403 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1404 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1410 void updateAnimation()
1412 if(m_animated_meshnode == NULL)
1415 m_animated_meshnode->setFrameLoop((int)m_animation_range.X, (int)m_animation_range.Y);
1416 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1417 m_animated_meshnode->setTransitionTime(m_animation_blend);
1420 void updateBonePosition()
1422 if(!m_bone_position.size() || m_animated_meshnode == NULL)
1425 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1426 for(std::map<std::string, core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){
1427 std::string bone_name = (*ii).first;
1428 v3f bone_pos = (*ii).second.X;
1429 v3f bone_rot = (*ii).second.Y;
1430 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1433 bone->setPosition(bone_pos);
1434 bone->setRotation(bone_rot);
1439 void updateAttachments()
1441 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1442 m_is_visible = !m_attached_to_local; // Objects attached to the local player should always be hidden
1444 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1448 v3f old_position = m_meshnode->getAbsolutePosition();
1449 v3f old_rotation = m_meshnode->getRotation();
1450 m_meshnode->setParent(m_smgr->getRootSceneNode());
1451 m_meshnode->setPosition(old_position);
1452 m_meshnode->setRotation(old_rotation);
1453 m_meshnode->updateAbsolutePosition();
1455 if(m_animated_meshnode)
1457 v3f old_position = m_animated_meshnode->getAbsolutePosition();
1458 v3f old_rotation = m_animated_meshnode->getRotation();
1459 m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1460 m_animated_meshnode->setPosition(old_position);
1461 m_animated_meshnode->setRotation(old_rotation);
1462 m_animated_meshnode->updateAbsolutePosition();
1466 v3f old_position = m_spritenode->getAbsolutePosition();
1467 v3f old_rotation = m_spritenode->getRotation();
1468 m_spritenode->setParent(m_smgr->getRootSceneNode());
1469 m_spritenode->setPosition(old_position);
1470 m_spritenode->setRotation(old_rotation);
1471 m_spritenode->updateAbsolutePosition();
1473 if(m_is_local_player)
1475 LocalPlayer *player = m_env->getLocalPlayer();
1476 player->isAttached = false;
1481 scene::IMeshSceneNode *parent_mesh = NULL;
1482 if(getParent()->getMeshSceneNode())
1483 parent_mesh = getParent()->getMeshSceneNode();
1484 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1485 if(getParent()->getAnimatedMeshSceneNode())
1486 parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1487 scene::IBillboardSceneNode *parent_sprite = NULL;
1488 if(getParent()->getSpriteSceneNode())
1489 parent_sprite = getParent()->getSpriteSceneNode();
1491 scene::IBoneSceneNode *parent_bone = NULL;
1492 if(parent_animated_mesh && m_attachment_bone != "")
1493 parent_bone = parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1495 // The spaghetti code below makes sure attaching works if either the parent or child is a spritenode, meshnode, or animatedmeshnode
1496 // TODO: Perhaps use polymorphism here to save code duplication
1499 m_meshnode->setParent(parent_bone);
1500 m_meshnode->setPosition(m_attachment_position);
1501 m_meshnode->setRotation(m_attachment_rotation);
1502 m_meshnode->updateAbsolutePosition();
1507 m_meshnode->setParent(parent_mesh);
1508 m_meshnode->setPosition(m_attachment_position);
1509 m_meshnode->setRotation(m_attachment_rotation);
1510 m_meshnode->updateAbsolutePosition();
1512 else if(parent_animated_mesh){
1513 m_meshnode->setParent(parent_animated_mesh);
1514 m_meshnode->setPosition(m_attachment_position);
1515 m_meshnode->setRotation(m_attachment_rotation);
1516 m_meshnode->updateAbsolutePosition();
1518 else if(parent_sprite){
1519 m_meshnode->setParent(parent_sprite);
1520 m_meshnode->setPosition(m_attachment_position);
1521 m_meshnode->setRotation(m_attachment_rotation);
1522 m_meshnode->updateAbsolutePosition();
1526 if(m_animated_meshnode){
1528 m_animated_meshnode->setParent(parent_bone);
1529 m_animated_meshnode->setPosition(m_attachment_position);
1530 m_animated_meshnode->setRotation(m_attachment_rotation);
1531 m_animated_meshnode->updateAbsolutePosition();
1536 m_animated_meshnode->setParent(parent_mesh);
1537 m_animated_meshnode->setPosition(m_attachment_position);
1538 m_animated_meshnode->setRotation(m_attachment_rotation);
1539 m_animated_meshnode->updateAbsolutePosition();
1541 else if(parent_animated_mesh){
1542 m_animated_meshnode->setParent(parent_animated_mesh);
1543 m_animated_meshnode->setPosition(m_attachment_position);
1544 m_animated_meshnode->setRotation(m_attachment_rotation);
1545 m_animated_meshnode->updateAbsolutePosition();
1547 else if(parent_sprite){
1548 m_animated_meshnode->setParent(parent_sprite);
1549 m_animated_meshnode->setPosition(m_attachment_position);
1550 m_animated_meshnode->setRotation(m_attachment_rotation);
1551 m_animated_meshnode->updateAbsolutePosition();
1557 m_spritenode->setParent(parent_bone);
1558 m_spritenode->setPosition(m_attachment_position);
1559 m_spritenode->setRotation(m_attachment_rotation);
1560 m_spritenode->updateAbsolutePosition();
1565 m_spritenode->setParent(parent_mesh);
1566 m_spritenode->setPosition(m_attachment_position);
1567 m_spritenode->setRotation(m_attachment_rotation);
1568 m_spritenode->updateAbsolutePosition();
1570 else if(parent_animated_mesh){
1571 m_spritenode->setParent(parent_animated_mesh);
1572 m_spritenode->setPosition(m_attachment_position);
1573 m_spritenode->setRotation(m_attachment_rotation);
1574 m_spritenode->updateAbsolutePosition();
1576 else if(parent_sprite){
1577 m_spritenode->setParent(parent_sprite);
1578 m_spritenode->setPosition(m_attachment_position);
1579 m_spritenode->setRotation(m_attachment_rotation);
1580 m_spritenode->updateAbsolutePosition();
1584 if(m_is_local_player)
1586 LocalPlayer *player = m_env->getLocalPlayer();
1587 player->isAttached = true;
1592 void processMessage(const std::string &data)
1594 //infostream<<"GenericCAO: Got message"<<std::endl;
1595 std::istringstream is(data, std::ios::binary);
1597 u8 cmd = readU8(is);
1598 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1600 m_prop = gob_read_set_properties(is);
1602 m_selection_box = m_prop.collisionbox;
1603 m_selection_box.MinEdge *= BS;
1604 m_selection_box.MaxEdge *= BS;
1606 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1607 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1609 if(!m_initial_tx_basepos_set){
1610 m_initial_tx_basepos_set = true;
1611 m_tx_basepos = m_prop.initial_sprite_basepos;
1616 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1618 // Not sent by the server if this object is an attachment.
1619 // We might however get here if the server notices the object being detached before the client.
1620 m_position = readV3F1000(is);
1621 m_velocity = readV3F1000(is);
1622 m_acceleration = readV3F1000(is);
1623 if(fabs(m_prop.automatic_rotate) < 0.001)
1624 m_yaw = readF1000(is);
1625 bool do_interpolate = readU8(is);
1626 bool is_end_position = readU8(is);
1627 float update_interval = readF1000(is);
1629 // Place us a bit higher if we're physical, to not sink into
1630 // the ground due to sucky collision detection...
1632 m_position += v3f(0,0.002,0);
1634 if(getParent() != NULL) // Just in case
1638 if(!m_prop.physical)
1639 pos_translator.update(m_position, is_end_position, update_interval);
1641 pos_translator.init(m_position);
1645 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1647 std::string mod = deSerializeString(is);
1648 updateTextures(mod);
1650 else if(cmd == GENERIC_CMD_SET_SPRITE)
1652 v2s16 p = readV2S16(is);
1653 int num_frames = readU16(is);
1654 float framelength = readF1000(is);
1655 bool select_horiz_by_yawpitch = readU8(is);
1658 m_anim_num_frames = num_frames;
1659 m_anim_framelength = framelength;
1660 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1664 else if(cmd == GENERIC_CMD_SET_ANIMATION)
1666 m_animation_range = readV2F1000(is);
1667 m_animation_speed = readF1000(is);
1668 m_animation_blend = readF1000(is);
1672 else if(cmd == GENERIC_CMD_SET_BONE_POSITION)
1674 std::string bone = deSerializeString(is);
1675 v3f position = readV3F1000(is);
1676 v3f rotation = readV3F1000(is);
1677 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1679 updateBonePosition();
1681 else if(cmd == GENERIC_CMD_SET_ATTACHMENT)
1683 // If an entry already exists for this object, delete it first to avoid duplicates
1684 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1686 if(ii->X == getId()) // This is the ID of our object
1688 m_env->attachment_list.erase(ii);
1692 m_env->attachment_list.push_back(core::vector2d<int>(getId(), readS16(is)));
1693 m_attachment_bone = deSerializeString(is);
1694 m_attachment_position = readV3F1000(is);
1695 m_attachment_rotation = readV3F1000(is);
1697 updateAttachments();
1699 else if(cmd == GENERIC_CMD_PUNCHED)
1701 /*s16 damage =*/ readS16(is);
1702 s16 result_hp = readS16(is);
1706 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1708 m_armor_groups.clear();
1709 int armor_groups_size = readU16(is);
1710 for(int i=0; i<armor_groups_size; i++){
1711 std::string name = deSerializeString(is);
1712 int rating = readS16(is);
1713 m_armor_groups[name] = rating;
1718 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1719 float time_from_last_punch=1000000)
1722 const ToolCapabilities *toolcap =
1723 &punchitem->getToolCapabilities(m_gamedef->idef());
1724 PunchDamageResult result = getPunchDamage(
1728 time_from_last_punch);
1730 if(result.did_punch && result.damage != 0)
1732 if(result.damage < m_hp){
1733 m_hp -= result.damage;
1736 // TODO: Execute defined fast response
1737 // As there is no definition, make a smoke puff
1738 ClientSimpleObject *simple = createSmokePuff(
1739 m_smgr, m_env, m_position,
1740 m_prop.visual_size * BS);
1741 m_env->addSimpleObject(simple);
1743 // TODO: Execute defined fast response
1744 // Flashing shall suffice as there is no definition
1745 m_reset_textures_timer = 0.05;
1746 if(result.damage >= 2)
1747 m_reset_textures_timer += 0.05 * result.damage;
1748 updateTextures("^[brighten");
1754 std::string debugInfoText()
1756 std::ostringstream os(std::ios::binary);
1757 os<<"GenericCAO hp="<<m_hp<<"\n";
1759 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1760 i != m_armor_groups.end(); i++){
1761 os<<i->first<<"="<<i->second<<", ";
1769 GenericCAO proto_GenericCAO(NULL, NULL);