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);
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 else if(m_prop.visual == "mesh"){
928 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
929 scene::IAnimatedMesh *mesh = smgr->getMesh(m_prop.mesh.c_str());
932 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
933 m_animated_meshnode->animateJoints(); // Needed for some animations
934 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
935 m_prop.visual_size.Y,
936 m_prop.visual_size.X));
937 u8 li = m_last_light;
938 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
941 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
943 else if(m_prop.visual == "wielditem"){
944 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
945 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
946 if(m_prop.textures.size() >= 1){
947 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
948 IItemDefManager *idef = m_gamedef->idef();
949 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
950 scene::IMesh *item_mesh = item.getDefinition(idef).wield_mesh;
952 // Copy mesh to be able to set unique vertex colors
953 scene::IMeshManipulator *manip =
954 irr->getVideoDriver()->getMeshManipulator();
955 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
957 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
960 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
961 m_prop.visual_size.Y/2,
962 m_prop.visual_size.X/2));
963 u8 li = m_last_light;
964 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
967 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
968 <<"\" not supported"<<std::endl;
972 scene::ISceneNode *node = NULL;
975 else if(m_animated_meshnode)
976 node = m_animated_meshnode;
979 if(node && m_is_player && !m_is_local_player){
980 // Add a text node for showing the name
981 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
982 std::wstring wname = narrow_to_wide(m_name);
983 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
984 wname.c_str(), video::SColor(255,255,255,255), node);
985 m_textnode->setPosition(v3f(0, BS*1.1, 0));
990 updateBonePosition();
996 m_visuals_expired = true;
999 void updateLight(u8 light_at_pos)
1001 u8 li = decode_light(light_at_pos);
1002 if(li != m_last_light){
1004 video::SColor color(255,li,li,li);
1006 setMeshColor(m_meshnode->getMesh(), color);
1007 if(m_animated_meshnode)
1008 setMeshColor(m_animated_meshnode->getMesh(), color);
1010 m_spritenode->setColor(color);
1014 v3s16 getLightPosition()
1016 return floatToInt(m_position, BS);
1019 void updateNodePos()
1021 if(getParent() != NULL)
1025 m_meshnode->setPosition(pos_translator.vect_show);
1026 v3f rot = m_meshnode->getRotation();
1028 m_meshnode->setRotation(rot);
1030 if(m_animated_meshnode){
1031 m_animated_meshnode->setPosition(pos_translator.vect_show);
1032 v3f rot = m_animated_meshnode->getRotation();
1034 m_animated_meshnode->setRotation(rot);
1037 m_spritenode->setPosition(pos_translator.vect_show);
1041 void step(float dtime, ClientEnvironment *env)
1043 if(m_visuals_expired && m_smgr && m_irr){
1044 m_visuals_expired = false;
1046 // Attachments, part 1: All attached objects must be unparented first, or Irrlicht causes a segmentation fault
1047 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1049 if(ii->Y == getId()) // This is a child of our parent
1051 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1054 scene::IMeshSceneNode *m_child_meshnode = obj->getMeshSceneNode();
1055 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode = obj->getAnimatedMeshSceneNode();
1056 scene::IBillboardSceneNode *m_child_spritenode = obj->getSpriteSceneNode();
1057 if(m_child_meshnode)
1058 m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1059 if(m_child_animated_meshnode)
1060 m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1061 if(m_child_spritenode)
1062 m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1067 removeFromScene(false);
1068 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1070 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1071 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1073 if(ii->Y == getId()) // This is a child of our parent
1075 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1077 obj->setAttachments();
1082 // Make sure m_is_visible is always applied
1084 m_meshnode->setVisible(m_is_visible);
1085 if(m_animated_meshnode)
1086 m_animated_meshnode->setVisible(m_is_visible);
1088 m_spritenode->setVisible(m_is_visible);
1090 m_textnode->setVisible(m_is_visible);
1092 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1094 // Set these for later
1095 m_position = getPosition();
1096 m_velocity = v3f(0,0,0);
1097 m_acceleration = v3f(0,0,0);
1098 pos_translator.vect_show = m_position;
1100 if(m_is_local_player) // Update local player attachment position
1102 LocalPlayer *player = m_env->getLocalPlayer();
1103 player->overridePosition = getParent()->getPosition();
1108 v3f lastpos = pos_translator.vect_show;
1110 if(m_prop.physical){
1111 core::aabbox3d<f32> box = m_prop.collisionbox;
1114 collisionMoveResult moveresult;
1115 f32 pos_max_d = BS*0.125; // Distance per iteration
1117 v3f p_pos = m_position;
1118 v3f p_velocity = m_velocity;
1119 v3f p_acceleration = m_acceleration;
1120 IGameDef *gamedef = env->getGameDef();
1121 moveresult = collisionMoveSimple(&env->getMap(), gamedef,
1122 pos_max_d, box, stepheight, dtime,
1123 p_pos, p_velocity, p_acceleration);
1126 m_velocity = p_velocity;
1127 m_acceleration = p_acceleration;
1129 bool is_end_position = moveresult.collides;
1130 pos_translator.update(m_position, is_end_position, dtime);
1131 pos_translator.translate(dtime);
1134 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1135 m_velocity += dtime * m_acceleration;
1136 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
1137 pos_translator.translate(dtime);
1141 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1142 m_step_distance_counter += moved;
1143 if(m_step_distance_counter > 1.5*BS){
1144 m_step_distance_counter = 0;
1145 if(!m_is_local_player && m_prop.makes_footstep_sound){
1146 INodeDefManager *ndef = m_gamedef->ndef();
1147 v3s16 p = floatToInt(getPosition() + v3f(0,
1148 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1149 MapNode n = m_env->getMap().getNodeNoEx(p);
1150 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1151 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1156 m_anim_timer += dtime;
1157 if(m_anim_timer >= m_anim_framelength){
1158 m_anim_timer -= m_anim_framelength;
1160 if(m_anim_frame >= m_anim_num_frames)
1166 if(m_reset_textures_timer >= 0){
1167 m_reset_textures_timer -= dtime;
1168 if(m_reset_textures_timer <= 0){
1169 m_reset_textures_timer = -1;
1173 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001){
1174 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1179 void updateTexturePos()
1182 scene::ICameraSceneNode* camera =
1183 m_spritenode->getSceneManager()->getActiveCamera();
1186 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1187 - camera->getAbsolutePosition();
1188 cam_to_entity.normalize();
1190 int row = m_tx_basepos.Y;
1191 int col = m_tx_basepos.X;
1193 if(m_tx_select_horiz_by_yawpitch)
1195 if(cam_to_entity.Y > 0.75)
1197 else if(cam_to_entity.Y < -0.75)
1200 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1201 float dir = mob_dir - m_yaw;
1202 dir = wrapDegrees_180(dir);
1203 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1204 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1206 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1208 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1210 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1217 // Animation goes downwards
1218 row += m_anim_frame;
1220 float txs = m_tx_size.X;
1221 float tys = m_tx_size.Y;
1222 setBillboardTextureMatrix(m_spritenode,
1223 txs, tys, col, row);
1227 void updateTextures(const std::string &mod)
1229 ITextureSource *tsrc = m_gamedef->tsrc();
1231 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1232 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1233 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1237 if(m_prop.visual == "sprite")
1239 std::string texturestring = "unknown_block.png";
1240 if(m_prop.textures.size() >= 1)
1241 texturestring = m_prop.textures[0];
1242 texturestring += mod;
1243 m_spritenode->setMaterialTexture(0,
1244 tsrc->getTextureRaw(texturestring));
1246 // This allows setting per-material colors. However, until a real lighting
1247 // system is added, the code below will have no effect. Once MineTest
1248 // has directional lighting, it should work automatically.
1249 if(m_prop.colors.size() >= 1)
1251 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1252 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1253 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1256 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1257 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1258 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1261 if(m_animated_meshnode)
1263 if(m_prop.visual == "mesh")
1265 for (u32 i = 0; i < m_prop.textures.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1267 std::string texturestring = m_prop.textures[i];
1268 if(texturestring == "")
1269 continue; // Empty texture string means don't modify that material
1270 texturestring += mod;
1271 video::ITexture* texture = tsrc->getTextureRaw(texturestring);
1274 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1278 // Set material flags and texture
1279 m_animated_meshnode->setMaterialTexture(i, texture);
1280 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1281 material.setFlag(video::EMF_LIGHTING, false);
1282 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1284 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1285 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1286 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1288 for (u32 i = 0; i < m_prop.colors.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1290 // This allows setting per-material colors. However, until a real lighting
1291 // system is added, the code below will have no effect. Once MineTest
1292 // has directional lighting, it should work automatically.
1293 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1294 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1295 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1301 if(m_prop.visual == "cube")
1303 for (u32 i = 0; i < 6; ++i)
1305 std::string texturestring = "unknown_block.png";
1306 if(m_prop.textures.size() > i)
1307 texturestring = m_prop.textures[i];
1308 texturestring += mod;
1309 AtlasPointer ap = tsrc->getTexture(texturestring);
1311 // Get the tile texture and atlas transformation
1312 video::ITexture* atlas = ap.atlas;
1316 // Set material flags and texture
1317 video::SMaterial& material = m_meshnode->getMaterial(i);
1318 material.setFlag(video::EMF_LIGHTING, false);
1319 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1320 material.setTexture(0, atlas);
1321 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1322 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1324 // This allows setting per-material colors. However, until a real lighting
1325 // system is added, the code below will have no effect. Once MineTest
1326 // has directional lighting, it should work automatically.
1327 if(m_prop.colors.size() > i)
1329 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1330 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1331 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1334 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1335 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1336 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1339 else if(m_prop.visual == "upright_sprite")
1341 scene::IMesh *mesh = m_meshnode->getMesh();
1343 std::string tname = "unknown_object.png";
1344 if(m_prop.textures.size() >= 1)
1345 tname = m_prop.textures[0];
1347 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1348 buf->getMaterial().setTexture(0,
1349 tsrc->getTextureRaw(tname));
1351 // This allows setting per-material colors. However, until a real lighting
1352 // system is added, the code below will have no effect. Once MineTest
1353 // has directional lighting, it should work automatically.
1354 if(m_prop.colors.size() >= 1)
1356 buf->getMaterial().AmbientColor = m_prop.colors[0];
1357 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1358 buf->getMaterial().SpecularColor = m_prop.colors[0];
1361 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1362 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1363 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1366 std::string tname = "unknown_object.png";
1367 if(m_prop.textures.size() >= 2)
1368 tname = m_prop.textures[1];
1369 else if(m_prop.textures.size() >= 1)
1370 tname = m_prop.textures[0];
1372 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1373 buf->getMaterial().setTexture(0,
1374 tsrc->getTextureRaw(tname));
1376 // This allows setting per-material colors. However, until a real lighting
1377 // system is added, the code below will have no effect. Once MineTest
1378 // has directional lighting, it should work automatically.
1379 if(m_prop.colors.size() >= 2)
1381 buf->getMaterial().AmbientColor = m_prop.colors[1];
1382 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1383 buf->getMaterial().SpecularColor = m_prop.colors[1];
1385 else if(m_prop.colors.size() >= 1)
1387 buf->getMaterial().AmbientColor = m_prop.colors[0];
1388 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1389 buf->getMaterial().SpecularColor = m_prop.colors[0];
1392 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1393 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1394 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1400 void updateAnimation()
1402 if(m_animated_meshnode == NULL)
1405 m_animated_meshnode->setFrameLoop((int)m_animation_range.X, (int)m_animation_range.Y);
1406 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1407 m_animated_meshnode->setTransitionTime(m_animation_blend);
1410 void updateBonePosition()
1412 if(!m_bone_position.size() || m_animated_meshnode == NULL)
1415 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1416 for(std::map<std::string, core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){
1417 std::string bone_name = (*ii).first;
1418 v3f bone_pos = (*ii).second.X;
1419 v3f bone_rot = (*ii).second.Y;
1420 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1423 bone->setPosition(bone_pos);
1424 bone->setRotation(bone_rot);
1429 void updateAttachments()
1431 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1432 m_is_visible = !m_attached_to_local; // Objects attached to the local player should always be hidden
1434 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1438 v3f old_position = m_meshnode->getAbsolutePosition();
1439 v3f old_rotation = m_meshnode->getRotation();
1440 m_meshnode->setParent(m_smgr->getRootSceneNode());
1441 m_meshnode->setPosition(old_position);
1442 m_meshnode->setRotation(old_rotation);
1443 m_meshnode->updateAbsolutePosition();
1445 if(m_animated_meshnode)
1447 v3f old_position = m_animated_meshnode->getAbsolutePosition();
1448 v3f old_rotation = m_animated_meshnode->getRotation();
1449 m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1450 m_animated_meshnode->setPosition(old_position);
1451 m_animated_meshnode->setRotation(old_rotation);
1452 m_animated_meshnode->updateAbsolutePosition();
1456 v3f old_position = m_spritenode->getAbsolutePosition();
1457 v3f old_rotation = m_spritenode->getRotation();
1458 m_spritenode->setParent(m_smgr->getRootSceneNode());
1459 m_spritenode->setPosition(old_position);
1460 m_spritenode->setRotation(old_rotation);
1461 m_spritenode->updateAbsolutePosition();
1463 if(m_is_local_player)
1465 LocalPlayer *player = m_env->getLocalPlayer();
1466 player->isAttached = false;
1471 scene::IMeshSceneNode *parent_mesh = NULL;
1472 if(getParent()->getMeshSceneNode())
1473 parent_mesh = getParent()->getMeshSceneNode();
1474 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1475 if(getParent()->getAnimatedMeshSceneNode())
1476 parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1477 scene::IBillboardSceneNode *parent_sprite = NULL;
1478 if(getParent()->getSpriteSceneNode())
1479 parent_sprite = getParent()->getSpriteSceneNode();
1481 scene::IBoneSceneNode *parent_bone = NULL;
1482 if(parent_animated_mesh && m_attachment_bone != "")
1483 parent_bone = parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1485 // The spaghetti code below makes sure attaching works if either the parent or child is a spritenode, meshnode, or animatedmeshnode
1486 // TODO: Perhaps use polymorphism here to save code duplication
1489 m_meshnode->setParent(parent_bone);
1490 m_meshnode->setPosition(m_attachment_position);
1491 m_meshnode->setRotation(m_attachment_rotation);
1492 m_meshnode->updateAbsolutePosition();
1497 m_meshnode->setParent(parent_mesh);
1498 m_meshnode->setPosition(m_attachment_position);
1499 m_meshnode->setRotation(m_attachment_rotation);
1500 m_meshnode->updateAbsolutePosition();
1502 else if(parent_animated_mesh){
1503 m_meshnode->setParent(parent_animated_mesh);
1504 m_meshnode->setPosition(m_attachment_position);
1505 m_meshnode->setRotation(m_attachment_rotation);
1506 m_meshnode->updateAbsolutePosition();
1508 else if(parent_sprite){
1509 m_meshnode->setParent(parent_sprite);
1510 m_meshnode->setPosition(m_attachment_position);
1511 m_meshnode->setRotation(m_attachment_rotation);
1512 m_meshnode->updateAbsolutePosition();
1516 if(m_animated_meshnode){
1518 m_animated_meshnode->setParent(parent_bone);
1519 m_animated_meshnode->setPosition(m_attachment_position);
1520 m_animated_meshnode->setRotation(m_attachment_rotation);
1521 m_animated_meshnode->updateAbsolutePosition();
1526 m_animated_meshnode->setParent(parent_mesh);
1527 m_animated_meshnode->setPosition(m_attachment_position);
1528 m_animated_meshnode->setRotation(m_attachment_rotation);
1529 m_animated_meshnode->updateAbsolutePosition();
1531 else if(parent_animated_mesh){
1532 m_animated_meshnode->setParent(parent_animated_mesh);
1533 m_animated_meshnode->setPosition(m_attachment_position);
1534 m_animated_meshnode->setRotation(m_attachment_rotation);
1535 m_animated_meshnode->updateAbsolutePosition();
1537 else if(parent_sprite){
1538 m_animated_meshnode->setParent(parent_sprite);
1539 m_animated_meshnode->setPosition(m_attachment_position);
1540 m_animated_meshnode->setRotation(m_attachment_rotation);
1541 m_animated_meshnode->updateAbsolutePosition();
1547 m_spritenode->setParent(parent_bone);
1548 m_spritenode->setPosition(m_attachment_position);
1549 m_spritenode->setRotation(m_attachment_rotation);
1550 m_spritenode->updateAbsolutePosition();
1555 m_spritenode->setParent(parent_mesh);
1556 m_spritenode->setPosition(m_attachment_position);
1557 m_spritenode->setRotation(m_attachment_rotation);
1558 m_spritenode->updateAbsolutePosition();
1560 else if(parent_animated_mesh){
1561 m_spritenode->setParent(parent_animated_mesh);
1562 m_spritenode->setPosition(m_attachment_position);
1563 m_spritenode->setRotation(m_attachment_rotation);
1564 m_spritenode->updateAbsolutePosition();
1566 else if(parent_sprite){
1567 m_spritenode->setParent(parent_sprite);
1568 m_spritenode->setPosition(m_attachment_position);
1569 m_spritenode->setRotation(m_attachment_rotation);
1570 m_spritenode->updateAbsolutePosition();
1574 if(m_is_local_player)
1576 LocalPlayer *player = m_env->getLocalPlayer();
1577 player->isAttached = true;
1582 void processMessage(const std::string &data)
1584 //infostream<<"GenericCAO: Got message"<<std::endl;
1585 std::istringstream is(data, std::ios::binary);
1587 u8 cmd = readU8(is);
1588 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1590 m_prop = gob_read_set_properties(is);
1592 m_selection_box = m_prop.collisionbox;
1593 m_selection_box.MinEdge *= BS;
1594 m_selection_box.MaxEdge *= BS;
1596 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1597 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1599 if(!m_initial_tx_basepos_set){
1600 m_initial_tx_basepos_set = true;
1601 m_tx_basepos = m_prop.initial_sprite_basepos;
1606 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1608 // Not sent by the server if this object is an attachment.
1609 // We might however get here if the server notices the object being detached before the client.
1610 m_position = readV3F1000(is);
1611 m_velocity = readV3F1000(is);
1612 m_acceleration = readV3F1000(is);
1613 if(fabs(m_prop.automatic_rotate) < 0.001)
1614 m_yaw = readF1000(is);
1615 bool do_interpolate = readU8(is);
1616 bool is_end_position = readU8(is);
1617 float update_interval = readF1000(is);
1619 // Place us a bit higher if we're physical, to not sink into
1620 // the ground due to sucky collision detection...
1622 m_position += v3f(0,0.002,0);
1624 if(getParent() != NULL) // Just in case
1628 if(!m_prop.physical)
1629 pos_translator.update(m_position, is_end_position, update_interval);
1631 pos_translator.init(m_position);
1635 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1637 std::string mod = deSerializeString(is);
1638 updateTextures(mod);
1640 else if(cmd == GENERIC_CMD_SET_SPRITE)
1642 v2s16 p = readV2S16(is);
1643 int num_frames = readU16(is);
1644 float framelength = readF1000(is);
1645 bool select_horiz_by_yawpitch = readU8(is);
1648 m_anim_num_frames = num_frames;
1649 m_anim_framelength = framelength;
1650 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1654 else if(cmd == GENERIC_CMD_SET_ANIMATION)
1656 m_animation_range = readV2F1000(is);
1657 m_animation_speed = readF1000(is);
1658 m_animation_blend = readF1000(is);
1662 else if(cmd == GENERIC_CMD_SET_BONE_POSITION)
1664 std::string bone = deSerializeString(is);
1665 v3f position = readV3F1000(is);
1666 v3f rotation = readV3F1000(is);
1667 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1669 updateBonePosition();
1671 else if(cmd == GENERIC_CMD_SET_ATTACHMENT)
1673 // If an entry already exists for this object, delete it first to avoid duplicates
1674 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1676 if(ii->X == getId()) // This is the ID of our object
1678 m_env->attachment_list.erase(ii);
1682 m_env->attachment_list.push_back(core::vector2d<int>(getId(), readS16(is)));
1683 m_attachment_bone = deSerializeString(is);
1684 m_attachment_position = readV3F1000(is);
1685 m_attachment_rotation = readV3F1000(is);
1687 updateAttachments();
1689 else if(cmd == GENERIC_CMD_PUNCHED)
1691 /*s16 damage =*/ readS16(is);
1692 s16 result_hp = readS16(is);
1696 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1698 m_armor_groups.clear();
1699 int armor_groups_size = readU16(is);
1700 for(int i=0; i<armor_groups_size; i++){
1701 std::string name = deSerializeString(is);
1702 int rating = readS16(is);
1703 m_armor_groups[name] = rating;
1708 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1709 float time_from_last_punch=1000000)
1712 const ToolCapabilities *toolcap =
1713 &punchitem->getToolCapabilities(m_gamedef->idef());
1714 PunchDamageResult result = getPunchDamage(
1718 time_from_last_punch);
1720 if(result.did_punch && result.damage != 0)
1722 if(result.damage < m_hp){
1723 m_hp -= result.damage;
1726 // TODO: Execute defined fast response
1727 // As there is no definition, make a smoke puff
1728 ClientSimpleObject *simple = createSmokePuff(
1729 m_smgr, m_env, m_position,
1730 m_prop.visual_size * BS);
1731 m_env->addSimpleObject(simple);
1733 // TODO: Execute defined fast response
1734 // Flashing shall suffice as there is no definition
1735 m_reset_textures_timer = 0.05;
1736 if(result.damage >= 2)
1737 m_reset_textures_timer += 0.05 * result.damage;
1738 updateTextures("^[brighten");
1744 std::string debugInfoText()
1746 std::ostringstream os(std::ios::binary);
1747 os<<"GenericCAO hp="<<m_hp<<"\n";
1749 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1750 i != m_armor_groups.end(); i++){
1751 os<<i->first<<"="<<i->second<<", ";
1759 GenericCAO proto_GenericCAO(NULL, NULL);