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 "camera.h" // CameraModes
45 #include <IMeshManipulator.h>
46 #include <IAnimatedMeshSceneNode.h>
47 #include <IBoneSceneNode.h>
50 struct ToolCapabilities;
52 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
54 std::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
56 SmoothTranslator::SmoothTranslator():
66 void SmoothTranslator::init(v3f vect)
73 anim_time_counter = 0;
77 void SmoothTranslator::sharpen()
82 void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
84 aim_is_end = is_end_position;
87 if(update_interval > 0){
88 anim_time = update_interval;
90 if(anim_time < 0.001 || anim_time > 1.0)
91 anim_time = anim_time_counter;
93 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
95 anim_time_counter = 0;
99 void SmoothTranslator::translate(f32 dtime)
101 anim_time_counter = anim_time_counter + dtime;
102 anim_counter = anim_counter + dtime;
103 v3f vect_move = vect_aim - vect_old;
105 if(anim_time > 0.001)
106 moveratio = anim_time_counter / anim_time;
107 // Move a bit less than should, to avoid oscillation
108 moveratio = moveratio * 0.8;
109 float move_end = 1.5;
112 if(moveratio > move_end)
113 moveratio = move_end;
114 vect_show = vect_old + vect_move * moveratio;
117 bool SmoothTranslator::is_moving()
119 return ((anim_time_counter / anim_time) < 1.4);
126 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
127 float txs, float tys, int col, int row)
129 video::SMaterial& material = bill->getMaterial(0);
130 core::matrix4& matrix = material.getTextureMatrix(0);
131 matrix.setTextureTranslate(txs*col, tys*row);
132 matrix.setTextureScale(txs, tys);
139 class TestCAO : public ClientActiveObject
142 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
147 return ACTIVEOBJECT_TYPE_TEST;
150 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
152 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
153 IrrlichtDevice *irr);
154 void removeFromScene(bool permanent);
155 void updateLight(u8 light_at_pos);
156 v3s16 getLightPosition();
157 void updateNodePos();
159 void step(float dtime, ClientEnvironment *env);
161 void processMessage(const std::string &data);
163 bool getCollisionBox(aabb3f *toset) { return false; }
165 scene::IMeshSceneNode *m_node;
170 TestCAO proto_TestCAO(NULL, NULL);
172 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
173 ClientActiveObject(0, gamedef, env),
175 m_position(v3f(0,10*BS,0))
177 ClientActiveObject::registerType(getType(), create);
184 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
186 return new TestCAO(gamedef, env);
189 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
195 //video::IVideoDriver* driver = smgr->getVideoDriver();
197 scene::SMesh *mesh = new scene::SMesh();
198 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
199 video::SColor c(255,255,255,255);
200 video::S3DVertex vertices[4] =
202 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
203 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
204 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
205 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
207 u16 indices[] = {0,1,2,2,3,0};
208 buf->append(vertices, 4, indices, 6);
210 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
211 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
212 buf->getMaterial().setTexture(0, tsrc->getTexture("rat.png"));
213 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
214 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
215 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
217 mesh->addMeshBuffer(buf);
219 m_node = smgr->addMeshSceneNode(mesh, NULL);
224 void TestCAO::removeFromScene(bool permanent)
233 void TestCAO::updateLight(u8 light_at_pos)
237 v3s16 TestCAO::getLightPosition()
239 return floatToInt(m_position, BS);
242 void TestCAO::updateNodePos()
247 m_node->setPosition(m_position);
248 //m_node->setRotation(v3f(0, 45, 0));
251 void TestCAO::step(float dtime, ClientEnvironment *env)
255 v3f rot = m_node->getRotation();
256 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
257 rot.Y += dtime * 180;
258 m_node->setRotation(rot);
262 void TestCAO::processMessage(const std::string &data)
264 infostream<<"TestCAO: Got data: "<<data<<std::endl;
265 std::istringstream is(data, std::ios::binary);
283 class ItemCAO : public ClientActiveObject
286 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
291 return ACTIVEOBJECT_TYPE_ITEM;
294 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
296 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
297 IrrlichtDevice *irr);
298 void removeFromScene(bool permanent);
299 void updateLight(u8 light_at_pos);
300 v3s16 getLightPosition();
301 void updateNodePos();
302 void updateInfoText();
303 void updateTexture();
305 void step(float dtime, ClientEnvironment *env);
307 void processMessage(const std::string &data);
309 void initialize(const std::string &data);
311 core::aabbox3d<f32>* getSelectionBox()
312 {return &m_selection_box;}
316 std::string infoText()
319 bool getCollisionBox(aabb3f *toset) { return false; }
321 core::aabbox3d<f32> m_selection_box;
322 scene::IMeshSceneNode *m_node;
324 std::string m_itemstring;
325 std::string m_infotext;
328 #include "inventory.h"
331 ItemCAO proto_ItemCAO(NULL, NULL);
333 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
334 ClientActiveObject(0, gamedef, env),
335 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
337 m_position(v3f(0,10*BS,0))
341 ClientActiveObject::registerType(getType(), create);
349 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
351 return new ItemCAO(gamedef, env);
354 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
360 //video::IVideoDriver* driver = smgr->getVideoDriver();
362 scene::SMesh *mesh = new scene::SMesh();
363 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
364 video::SColor c(255,255,255,255);
365 video::S3DVertex vertices[4] =
367 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
368 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
369 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
370 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
371 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
372 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
373 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
374 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
376 u16 indices[] = {0,1,2,2,3,0};
377 buf->append(vertices, 4, indices, 6);
379 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
380 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
381 // Initialize with a generated placeholder texture
382 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
383 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
384 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
385 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
387 mesh->addMeshBuffer(buf);
389 m_node = smgr->addMeshSceneNode(mesh, NULL);
400 void ItemCAO::removeFromScene(bool permanent)
409 void ItemCAO::updateLight(u8 light_at_pos)
414 u8 li = decode_light(light_at_pos);
415 video::SColor color(255,li,li,li);
416 setMeshColor(m_node->getMesh(), color);
419 v3s16 ItemCAO::getLightPosition()
421 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
424 void ItemCAO::updateNodePos()
429 m_node->setPosition(m_position);
432 void ItemCAO::updateInfoText()
435 IItemDefManager *idef = m_gamedef->idef();
437 item.deSerialize(m_itemstring, idef);
438 if(item.isKnown(idef))
439 m_infotext = item.getDefinition(idef).description;
441 m_infotext = "Unknown item: '" + m_itemstring + "'";
443 m_infotext += " (" + itos(item.count) + ")";
445 catch(SerializationError &e)
447 m_infotext = "Unknown item: '" + m_itemstring + "'";
451 void ItemCAO::updateTexture()
456 // Create an inventory item to see what is its image
457 std::istringstream is(m_itemstring, std::ios_base::binary);
458 video::ITexture *texture = NULL;
460 IItemDefManager *idef = m_gamedef->idef();
462 item.deSerialize(is, idef);
463 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
465 catch(SerializationError &e)
467 infostream<<"WARNING: "<<__FUNCTION_NAME
468 <<": error deSerializing itemstring \""
469 <<m_itemstring<<std::endl;
472 // Set meshbuffer texture
473 m_node->getMaterial(0).setTexture(0, texture);
477 void ItemCAO::step(float dtime, ClientEnvironment *env)
481 /*v3f rot = m_node->getRotation();
482 rot.Y += dtime * 120;
483 m_node->setRotation(rot);*/
484 LocalPlayer *player = env->getLocalPlayer();
486 v3f rot = m_node->getRotation();
487 rot.Y = 180.0 - (player->getYaw());
488 m_node->setRotation(rot);
492 void ItemCAO::processMessage(const std::string &data)
494 //infostream<<"ItemCAO: Got message"<<std::endl;
495 std::istringstream is(data, std::ios::binary);
501 m_position = readV3F1000(is);
507 m_itemstring = deSerializeString(is);
513 void ItemCAO::initialize(const std::string &data)
515 infostream<<"ItemCAO: Got init data"<<std::endl;
518 std::istringstream is(data, std::ios::binary);
520 u8 version = readU8(is);
525 m_position = readV3F1000(is);
527 m_itemstring = deSerializeString(is);
538 #include "genericobject.h"
540 GenericCAO::GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
541 ClientActiveObject(0, gamedef, env),
544 m_is_local_player(false),
549 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
551 m_animated_meshnode(NULL),
554 m_position(v3f(0,10*BS,0)),
555 m_velocity(v3f(0,0,0)),
556 m_acceleration(v3f(0,0,0)),
561 m_initial_tx_basepos_set(false),
562 m_tx_select_horiz_by_yawpitch(false),
563 m_animation_range(v2s32(0,0)),
564 m_animation_speed(15),
565 m_animation_blend(0),
566 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
567 m_attachment_bone(""),
568 m_attachment_position(v3f(0,0,0)),
569 m_attachment_rotation(v3f(0,0,0)),
570 m_attached_to_local(false),
572 m_anim_num_frames(1),
573 m_anim_framelength(0.2),
575 m_reset_textures_timer(-1),
576 m_visuals_expired(false),
577 m_step_distance_counter(0),
582 ClientActiveObject::registerType(getType(), create);
585 bool GenericCAO::getCollisionBox(aabb3f *toset) {
586 if (m_prop.physical) {
587 //update collision box
588 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
589 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
591 toset->MinEdge += m_position;
592 toset->MaxEdge += m_position;
600 bool GenericCAO::collideWithObjects() {
601 return m_prop.collideWithObjects;
604 void GenericCAO::initialize(const std::string &data)
606 infostream<<"GenericCAO: Got init data"<<std::endl;
607 std::istringstream is(data, std::ios::binary);
608 int num_messages = 0;
610 u8 version = readU8(is);
612 if(version == 1) // In PROTOCOL_VERSION 14
614 m_name = deSerializeString(is);
615 m_is_player = readU8(is);
617 m_position = readV3F1000(is);
618 m_yaw = readF1000(is);
620 num_messages = readU8(is);
622 else if(version == 0) // In PROTOCOL_VERSION 13
624 m_name = deSerializeString(is);
625 m_is_player = readU8(is);
626 m_position = readV3F1000(is);
627 m_yaw = readF1000(is);
629 num_messages = readU8(is);
633 errorstream<<"GenericCAO: Unsupported init data version"
638 for(int i=0; i<num_messages; i++){
639 std::string message = deSerializeLongString(is);
640 processMessage(message);
643 pos_translator.init(m_position);
647 Player *player = m_env->getPlayer(m_name.c_str());
648 if(player && player->isLocal()){
649 m_is_local_player = true;
651 m_env->addPlayerName(m_name.c_str());
655 GenericCAO::~GenericCAO()
658 m_env->removePlayerName(m_name.c_str());
662 core::aabbox3d<f32>* GenericCAO::getSelectionBox()
664 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
666 return &m_selection_box;
669 v3f GenericCAO::getPosition()
671 if(getParent() != NULL){
673 return m_meshnode->getAbsolutePosition();
674 if(m_animated_meshnode)
675 return m_animated_meshnode->getAbsolutePosition();
677 return m_spritenode->getAbsolutePosition();
680 return pos_translator.vect_show;
683 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
690 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
692 if(m_animated_meshnode)
693 return m_animated_meshnode;
697 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
704 void GenericCAO::setAttachments()
709 ClientActiveObject* GenericCAO::getParent()
711 ClientActiveObject *obj = NULL;
712 for(std::vector<core::vector2d<int> >::const_iterator cii = m_env->attachment_list.begin(); cii != m_env->attachment_list.end(); cii++)
714 if(cii->X == getId()){ // This ID is our child
715 if(cii->Y > 0){ // A parent ID exists for our child
716 if(cii->X != cii->Y){ // The parent and child ID are not the same
717 obj = m_env->getActiveObject(cii->Y);
728 void GenericCAO::removeFromScene(bool permanent)
730 if(permanent) // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
732 // Detach this object's children
733 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
735 if(ii->Y == getId()) // Is a child of our object
738 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
740 obj->setAttachments();
743 // Delete this object from the attachments list
744 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
746 if(ii->X == getId()) // Is our object
748 m_env->attachment_list.erase(ii);
755 m_meshnode->remove();
758 if(m_animated_meshnode){
759 m_animated_meshnode->remove();
760 m_animated_meshnode = NULL;
763 m_spritenode->remove();
768 void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
774 if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
777 m_visuals_expired = false;
779 if(!m_prop.is_visible)
782 //video::IVideoDriver* driver = smgr->getVideoDriver();
784 if(m_prop.visual == "sprite"){
785 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
786 m_spritenode = smgr->addBillboardSceneNode(
787 NULL, v2f(1, 1), v3f(0,0,0), -1);
788 m_spritenode->setMaterialTexture(0,
789 tsrc->getTexture("unknown_node.png"));
790 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
791 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
792 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
793 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
794 u8 li = m_last_light;
795 m_spritenode->setColor(video::SColor(255,li,li,li));
796 m_spritenode->setSize(m_prop.visual_size*BS);
798 const float txs = 1.0 / 1;
799 const float tys = 1.0 / 1;
800 setBillboardTextureMatrix(m_spritenode,
804 else if(m_prop.visual == "upright_sprite")
806 scene::SMesh *mesh = new scene::SMesh();
807 double dx = BS*m_prop.visual_size.X/2;
808 double dy = BS*m_prop.visual_size.Y/2;
810 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
811 u8 li = m_last_light;
812 video::SColor c(255,li,li,li);
813 video::S3DVertex vertices[4] =
815 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
816 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
817 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
818 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
820 u16 indices[] = {0,1,2,2,3,0};
821 buf->append(vertices, 4, indices, 6);
823 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
824 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
825 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
826 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
828 mesh->addMeshBuffer(buf);
832 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
833 u8 li = m_last_light;
834 video::SColor c(255,li,li,li);
835 video::S3DVertex vertices[4] =
837 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
838 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
839 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
840 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
842 u16 indices[] = {0,1,2,2,3,0};
843 buf->append(vertices, 4, indices, 6);
845 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
846 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
847 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
848 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
850 mesh->addMeshBuffer(buf);
853 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
855 // Set it to use the materials of the meshbuffers directly.
856 // This is needed for changing the texture in the future
857 m_meshnode->setReadOnlyMaterials(true);
859 else if(m_prop.visual == "cube"){
860 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
861 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
862 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
865 m_meshnode->setScale(v3f(m_prop.visual_size.X,
866 m_prop.visual_size.Y,
867 m_prop.visual_size.X));
868 u8 li = m_last_light;
869 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
871 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
872 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
873 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
874 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
876 else if(m_prop.visual == "mesh"){
877 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
878 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
881 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
882 mesh->drop(); // The scene node took hold of it
883 m_animated_meshnode->animateJoints(); // Needed for some animations
884 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
885 m_prop.visual_size.Y,
886 m_prop.visual_size.X));
887 u8 li = m_last_light;
888 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
890 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
891 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
892 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
893 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
896 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
898 else if(m_prop.visual == "wielditem"){
899 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
900 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
901 if(m_prop.textures.size() >= 1){
902 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
903 IItemDefManager *idef = m_gamedef->idef();
904 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
905 scene::IMesh *item_mesh = idef->getWieldMesh(item.getDefinition(idef).name, m_gamedef);
907 // Copy mesh to be able to set unique vertex colors
908 scene::IMeshManipulator *manip =
909 irr->getVideoDriver()->getMeshManipulator();
910 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
912 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
915 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
916 m_prop.visual_size.Y/2,
917 m_prop.visual_size.X/2));
918 u8 li = m_last_light;
919 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
922 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
923 <<"\" not supported"<<std::endl;
927 scene::ISceneNode *node = NULL;
930 else if(m_animated_meshnode)
931 node = m_animated_meshnode;
934 if(node && m_is_player && !m_is_local_player){
935 // Add a text node for showing the name
936 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
937 std::wstring wname = narrow_to_wide(m_name);
938 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
939 wname.c_str(), video::SColor(255,255,255,255), node);
940 m_textnode->setPosition(v3f(0, BS*1.1, 0));
945 updateBonePosition();
949 void GenericCAO::updateLight(u8 light_at_pos)
951 u8 li = decode_light(light_at_pos);
952 if(li != m_last_light){
954 video::SColor color(255,li,li,li);
956 setMeshColor(m_meshnode->getMesh(), color);
957 if(m_animated_meshnode)
958 setMeshColor(m_animated_meshnode->getMesh(), color);
960 m_spritenode->setColor(color);
964 v3s16 GenericCAO::getLightPosition()
966 return floatToInt(m_position, BS);
969 void GenericCAO::updateNodePos()
971 if(getParent() != NULL)
974 v3s16 camera_offset = m_env->getCameraOffset();
976 m_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
977 v3f rot = m_meshnode->getRotation();
979 m_meshnode->setRotation(rot);
981 if(m_animated_meshnode){
982 m_animated_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
983 v3f rot = m_animated_meshnode->getRotation();
985 m_animated_meshnode->setRotation(rot);
988 m_spritenode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
992 void GenericCAO::step(float dtime, ClientEnvironment *env)
994 // Handel model of local player instantly to prevent lags
995 if(m_is_local_player) {
996 LocalPlayer *player = m_env->getLocalPlayer();
998 if (player->camera_mode > CAMERA_MODE_FIRST) {
999 int old_anim = player->last_animation;
1000 float old_anim_speed = player->last_animation_speed;
1001 m_is_visible = true;
1002 m_position = player->getPosition() + v3f(0,BS,0);
1003 m_velocity = v3f(0,0,0);
1004 m_acceleration = v3f(0,0,0);
1005 pos_translator.vect_show = m_position;
1006 m_yaw = player->getYaw();
1007 PlayerControl controls = player->getPlayerControl();
1009 bool walking = false;
1010 if(controls.up || controls.down || controls.left || controls.right)
1013 f32 new_speed = player->local_animation_speed;
1014 v2s32 new_anim = v2s32(0,0);
1015 bool allow_update = false;
1017 // increase speed if using fast or flying fast
1018 if((g_settings->getBool("fast_move") &&
1019 m_gamedef->checkLocalPrivilege("fast")) &&
1021 (!player->touching_ground &&
1022 g_settings->getBool("free_move") &&
1023 m_gamedef->checkLocalPrivilege("fly"))))
1025 // slowdown speed if sneeking
1026 if(controls.sneak && walking)
1029 if(walking && (controls.LMB || controls.RMB)) {
1030 new_anim = player->local_animations[3];
1031 player->last_animation = WD_ANIM;
1032 } else if(walking) {
1033 new_anim = player->local_animations[1];
1034 player->last_animation = WALK_ANIM;
1035 } else if(controls.LMB || controls.RMB) {
1036 new_anim = player->local_animations[2];
1037 player->last_animation = DIG_ANIM;
1040 // Apply animations if input detected and not attached
1041 // or set idle animation
1042 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
1043 allow_update = true;
1044 m_animation_range = new_anim;
1045 m_animation_speed = new_speed;
1046 player->last_animation_speed = m_animation_speed;
1048 player->last_animation = NO_ANIM;
1050 if (old_anim != NO_ANIM) {
1051 m_animation_range = player->local_animations[0];
1056 // Update local player animations
1057 if ((player->last_animation != old_anim || m_animation_speed != old_anim_speed) &&
1058 player->last_animation != NO_ANIM && allow_update)
1062 m_is_visible = false;
1066 if(m_visuals_expired && m_smgr && m_irr){
1067 m_visuals_expired = false;
1069 // Attachments, part 1: All attached objects must be unparented first, or Irrlicht causes a segmentation fault
1070 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1072 if(ii->Y == getId()) // This is a child of our parent
1074 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1077 scene::IMeshSceneNode *m_child_meshnode = obj->getMeshSceneNode();
1078 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode = obj->getAnimatedMeshSceneNode();
1079 scene::IBillboardSceneNode *m_child_spritenode = obj->getSpriteSceneNode();
1080 if(m_child_meshnode)
1081 m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1082 if(m_child_animated_meshnode)
1083 m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1084 if(m_child_spritenode)
1085 m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1090 removeFromScene(false);
1091 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1093 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1094 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1096 if(ii->Y == getId()) // This is a child of our parent
1098 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1100 obj->setAttachments();
1105 // Make sure m_is_visible is always applied
1107 m_meshnode->setVisible(m_is_visible);
1108 if(m_animated_meshnode)
1109 m_animated_meshnode->setVisible(m_is_visible);
1111 m_spritenode->setVisible(m_is_visible);
1113 m_textnode->setVisible(m_is_visible);
1115 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1117 // Set these for later
1118 m_position = getPosition();
1119 m_velocity = v3f(0,0,0);
1120 m_acceleration = v3f(0,0,0);
1121 pos_translator.vect_show = m_position;
1123 if(m_is_local_player) // Update local player attachment position
1125 LocalPlayer *player = m_env->getLocalPlayer();
1126 player->overridePosition = getParent()->getPosition();
1127 m_env->getLocalPlayer()->parent = getParent();
1132 v3f lastpos = pos_translator.vect_show;
1134 if(m_prop.physical){
1135 core::aabbox3d<f32> box = m_prop.collisionbox;
1138 collisionMoveResult moveresult;
1139 f32 pos_max_d = BS*0.125; // Distance per iteration
1140 v3f p_pos = m_position;
1141 v3f p_velocity = m_velocity;
1142 v3f p_acceleration = m_acceleration;
1143 moveresult = collisionMoveSimple(env,env->getGameDef(),
1144 pos_max_d, box, m_prop.stepheight, dtime,
1145 p_pos, p_velocity, p_acceleration,
1146 this, m_prop.collideWithObjects);
1149 m_velocity = p_velocity;
1150 m_acceleration = p_acceleration;
1152 bool is_end_position = moveresult.collides;
1153 pos_translator.update(m_position, is_end_position, dtime);
1154 pos_translator.translate(dtime);
1157 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1158 m_velocity += dtime * m_acceleration;
1159 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
1160 pos_translator.translate(dtime);
1164 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1165 m_step_distance_counter += moved;
1166 if(m_step_distance_counter > 1.5*BS){
1167 m_step_distance_counter = 0;
1168 if(!m_is_local_player && m_prop.makes_footstep_sound){
1169 INodeDefManager *ndef = m_gamedef->ndef();
1170 v3s16 p = floatToInt(getPosition() + v3f(0,
1171 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1172 MapNode n = m_env->getMap().getNodeNoEx(p);
1173 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1174 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1179 m_anim_timer += dtime;
1180 if(m_anim_timer >= m_anim_framelength){
1181 m_anim_timer -= m_anim_framelength;
1183 if(m_anim_frame >= m_anim_num_frames)
1189 if(m_reset_textures_timer >= 0){
1190 m_reset_textures_timer -= dtime;
1191 if(m_reset_textures_timer <= 0){
1192 m_reset_textures_timer = -1;
1196 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001){
1197 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1201 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1202 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)){
1203 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI + m_prop.automatic_face_movement_dir_offset;
1208 void GenericCAO::updateTexturePos()
1211 scene::ICameraSceneNode* camera =
1212 m_spritenode->getSceneManager()->getActiveCamera();
1215 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1216 - camera->getAbsolutePosition();
1217 cam_to_entity.normalize();
1219 int row = m_tx_basepos.Y;
1220 int col = m_tx_basepos.X;
1222 if(m_tx_select_horiz_by_yawpitch)
1224 if(cam_to_entity.Y > 0.75)
1226 else if(cam_to_entity.Y < -0.75)
1229 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1230 float dir = mob_dir - m_yaw;
1231 dir = wrapDegrees_180(dir);
1232 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1233 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1235 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1237 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1239 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1246 // Animation goes downwards
1247 row += m_anim_frame;
1249 float txs = m_tx_size.X;
1250 float tys = m_tx_size.Y;
1251 setBillboardTextureMatrix(m_spritenode,
1252 txs, tys, col, row);
1256 void GenericCAO::updateTextures(const std::string &mod)
1258 ITextureSource *tsrc = m_gamedef->tsrc();
1260 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1261 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1262 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1266 if(m_prop.visual == "sprite")
1268 std::string texturestring = "unknown_node.png";
1269 if(m_prop.textures.size() >= 1)
1270 texturestring = m_prop.textures[0];
1271 texturestring += mod;
1272 m_spritenode->setMaterialTexture(0,
1273 tsrc->getTexture(texturestring));
1275 // This allows setting per-material colors. However, until a real lighting
1276 // system is added, the code below will have no effect. Once MineTest
1277 // has directional lighting, it should work automatically.
1278 if(m_prop.colors.size() >= 1)
1280 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1281 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1282 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1285 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1286 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1287 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1290 if(m_animated_meshnode)
1292 if(m_prop.visual == "mesh")
1294 for (u32 i = 0; i < m_prop.textures.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1296 std::string texturestring = m_prop.textures[i];
1297 if(texturestring == "")
1298 continue; // Empty texture string means don't modify that material
1299 texturestring += mod;
1300 video::ITexture* texture = tsrc->getTexture(texturestring);
1303 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1307 // Set material flags and texture
1308 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1309 material.TextureLayer[0].Texture = texture;
1310 material.setFlag(video::EMF_LIGHTING, false);
1311 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1313 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1314 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1315 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1317 for (u32 i = 0; i < m_prop.colors.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1319 // This allows setting per-material colors. However, until a real lighting
1320 // system is added, the code below will have no effect. Once MineTest
1321 // has directional lighting, it should work automatically.
1322 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1323 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1324 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1330 if(m_prop.visual == "cube")
1332 for (u32 i = 0; i < 6; ++i)
1334 std::string texturestring = "unknown_node.png";
1335 if(m_prop.textures.size() > i)
1336 texturestring = m_prop.textures[i];
1337 texturestring += mod;
1340 // Set material flags and texture
1341 video::SMaterial& material = m_meshnode->getMaterial(i);
1342 material.setFlag(video::EMF_LIGHTING, false);
1343 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1344 material.setTexture(0,
1345 tsrc->getTexture(texturestring));
1346 material.getTextureMatrix(0).makeIdentity();
1348 // This allows setting per-material colors. However, until a real lighting
1349 // system is added, the code below will have no effect. Once MineTest
1350 // has directional lighting, it should work automatically.
1351 if(m_prop.colors.size() > i)
1353 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1354 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1355 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1358 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1359 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1360 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1363 else if(m_prop.visual == "upright_sprite")
1365 scene::IMesh *mesh = m_meshnode->getMesh();
1367 std::string tname = "unknown_object.png";
1368 if(m_prop.textures.size() >= 1)
1369 tname = m_prop.textures[0];
1371 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1372 buf->getMaterial().setTexture(0,
1373 tsrc->getTexture(tname));
1375 // This allows setting per-material colors. However, until a real lighting
1376 // system is added, the code below will have no effect. Once MineTest
1377 // has directional lighting, it should work automatically.
1378 if(m_prop.colors.size() >= 1)
1380 buf->getMaterial().AmbientColor = m_prop.colors[0];
1381 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1382 buf->getMaterial().SpecularColor = m_prop.colors[0];
1385 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1386 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1387 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1390 std::string tname = "unknown_object.png";
1391 if(m_prop.textures.size() >= 2)
1392 tname = m_prop.textures[1];
1393 else if(m_prop.textures.size() >= 1)
1394 tname = m_prop.textures[0];
1396 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1397 buf->getMaterial().setTexture(0,
1398 tsrc->getTexture(tname));
1400 // This allows setting per-material colors. However, until a real lighting
1401 // system is added, the code below will have no effect. Once MineTest
1402 // has directional lighting, it should work automatically.
1403 if(m_prop.colors.size() >= 2)
1405 buf->getMaterial().AmbientColor = m_prop.colors[1];
1406 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1407 buf->getMaterial().SpecularColor = m_prop.colors[1];
1409 else if(m_prop.colors.size() >= 1)
1411 buf->getMaterial().AmbientColor = m_prop.colors[0];
1412 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1413 buf->getMaterial().SpecularColor = m_prop.colors[0];
1416 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1417 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1418 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1424 void GenericCAO::updateAnimation()
1426 if(m_animated_meshnode == NULL)
1428 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1429 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1430 m_animated_meshnode->setTransitionTime(m_animation_blend);
1433 void GenericCAO::updateBonePosition()
1435 if(!m_bone_position.size() || m_animated_meshnode == NULL)
1438 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1439 for(std::map<std::string, core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){
1440 std::string bone_name = (*ii).first;
1441 v3f bone_pos = (*ii).second.X;
1442 v3f bone_rot = (*ii).second.Y;
1443 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1446 bone->setPosition(bone_pos);
1447 bone->setRotation(bone_rot);
1452 void GenericCAO::updateAttachments()
1454 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1455 m_is_visible = !m_attached_to_local; // Objects attached to the local player should always be hidden
1457 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1461 v3f old_position = m_meshnode->getAbsolutePosition();
1462 v3f old_rotation = m_meshnode->getRotation();
1463 m_meshnode->setParent(m_smgr->getRootSceneNode());
1464 m_meshnode->setPosition(old_position);
1465 m_meshnode->setRotation(old_rotation);
1466 m_meshnode->updateAbsolutePosition();
1468 if(m_animated_meshnode)
1470 v3f old_position = m_animated_meshnode->getAbsolutePosition();
1471 v3f old_rotation = m_animated_meshnode->getRotation();
1472 m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1473 m_animated_meshnode->setPosition(old_position);
1474 m_animated_meshnode->setRotation(old_rotation);
1475 m_animated_meshnode->updateAbsolutePosition();
1479 v3f old_position = m_spritenode->getAbsolutePosition();
1480 v3f old_rotation = m_spritenode->getRotation();
1481 m_spritenode->setParent(m_smgr->getRootSceneNode());
1482 m_spritenode->setPosition(old_position);
1483 m_spritenode->setRotation(old_rotation);
1484 m_spritenode->updateAbsolutePosition();
1486 if(m_is_local_player)
1488 LocalPlayer *player = m_env->getLocalPlayer();
1489 player->isAttached = false;
1494 scene::IMeshSceneNode *parent_mesh = NULL;
1495 if(getParent()->getMeshSceneNode())
1496 parent_mesh = getParent()->getMeshSceneNode();
1497 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1498 if(getParent()->getAnimatedMeshSceneNode())
1499 parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1500 scene::IBillboardSceneNode *parent_sprite = NULL;
1501 if(getParent()->getSpriteSceneNode())
1502 parent_sprite = getParent()->getSpriteSceneNode();
1504 scene::IBoneSceneNode *parent_bone = NULL;
1505 if(parent_animated_mesh && m_attachment_bone != "")
1506 parent_bone = parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1508 // The spaghetti code below makes sure attaching works if either the parent or child is a spritenode, meshnode, or animatedmeshnode
1509 // TODO: Perhaps use polymorphism here to save code duplication
1512 m_meshnode->setParent(parent_bone);
1513 m_meshnode->setPosition(m_attachment_position);
1514 m_meshnode->setRotation(m_attachment_rotation);
1515 m_meshnode->updateAbsolutePosition();
1520 m_meshnode->setParent(parent_mesh);
1521 m_meshnode->setPosition(m_attachment_position);
1522 m_meshnode->setRotation(m_attachment_rotation);
1523 m_meshnode->updateAbsolutePosition();
1525 else if(parent_animated_mesh){
1526 m_meshnode->setParent(parent_animated_mesh);
1527 m_meshnode->setPosition(m_attachment_position);
1528 m_meshnode->setRotation(m_attachment_rotation);
1529 m_meshnode->updateAbsolutePosition();
1531 else if(parent_sprite){
1532 m_meshnode->setParent(parent_sprite);
1533 m_meshnode->setPosition(m_attachment_position);
1534 m_meshnode->setRotation(m_attachment_rotation);
1535 m_meshnode->updateAbsolutePosition();
1539 if(m_animated_meshnode){
1541 m_animated_meshnode->setParent(parent_bone);
1542 m_animated_meshnode->setPosition(m_attachment_position);
1543 m_animated_meshnode->setRotation(m_attachment_rotation);
1544 m_animated_meshnode->updateAbsolutePosition();
1549 m_animated_meshnode->setParent(parent_mesh);
1550 m_animated_meshnode->setPosition(m_attachment_position);
1551 m_animated_meshnode->setRotation(m_attachment_rotation);
1552 m_animated_meshnode->updateAbsolutePosition();
1554 else if(parent_animated_mesh){
1555 m_animated_meshnode->setParent(parent_animated_mesh);
1556 m_animated_meshnode->setPosition(m_attachment_position);
1557 m_animated_meshnode->setRotation(m_attachment_rotation);
1558 m_animated_meshnode->updateAbsolutePosition();
1560 else if(parent_sprite){
1561 m_animated_meshnode->setParent(parent_sprite);
1562 m_animated_meshnode->setPosition(m_attachment_position);
1563 m_animated_meshnode->setRotation(m_attachment_rotation);
1564 m_animated_meshnode->updateAbsolutePosition();
1570 m_spritenode->setParent(parent_bone);
1571 m_spritenode->setPosition(m_attachment_position);
1572 m_spritenode->setRotation(m_attachment_rotation);
1573 m_spritenode->updateAbsolutePosition();
1578 m_spritenode->setParent(parent_mesh);
1579 m_spritenode->setPosition(m_attachment_position);
1580 m_spritenode->setRotation(m_attachment_rotation);
1581 m_spritenode->updateAbsolutePosition();
1583 else if(parent_animated_mesh){
1584 m_spritenode->setParent(parent_animated_mesh);
1585 m_spritenode->setPosition(m_attachment_position);
1586 m_spritenode->setRotation(m_attachment_rotation);
1587 m_spritenode->updateAbsolutePosition();
1589 else if(parent_sprite){
1590 m_spritenode->setParent(parent_sprite);
1591 m_spritenode->setPosition(m_attachment_position);
1592 m_spritenode->setRotation(m_attachment_rotation);
1593 m_spritenode->updateAbsolutePosition();
1597 if(m_is_local_player)
1599 LocalPlayer *player = m_env->getLocalPlayer();
1600 player->isAttached = true;
1605 void GenericCAO::processMessage(const std::string &data)
1607 //infostream<<"GenericCAO: Got message"<<std::endl;
1608 std::istringstream is(data, std::ios::binary);
1610 u8 cmd = readU8(is);
1611 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1613 m_prop = gob_read_set_properties(is);
1615 m_selection_box = m_prop.collisionbox;
1616 m_selection_box.MinEdge *= BS;
1617 m_selection_box.MaxEdge *= BS;
1619 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1620 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1622 if(!m_initial_tx_basepos_set){
1623 m_initial_tx_basepos_set = true;
1624 m_tx_basepos = m_prop.initial_sprite_basepos;
1629 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1631 // Not sent by the server if this object is an attachment.
1632 // We might however get here if the server notices the object being detached before the client.
1633 m_position = readV3F1000(is);
1634 m_velocity = readV3F1000(is);
1635 m_acceleration = readV3F1000(is);
1636 if(fabs(m_prop.automatic_rotate) < 0.001)
1637 m_yaw = readF1000(is);
1640 bool do_interpolate = readU8(is);
1641 bool is_end_position = readU8(is);
1642 float update_interval = readF1000(is);
1644 // Place us a bit higher if we're physical, to not sink into
1645 // the ground due to sucky collision detection...
1647 m_position += v3f(0,0.002,0);
1649 if(getParent() != NULL) // Just in case
1653 if(!m_prop.physical)
1654 pos_translator.update(m_position, is_end_position, update_interval);
1656 pos_translator.init(m_position);
1660 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1662 std::string mod = deSerializeString(is);
1663 updateTextures(mod);
1665 else if(cmd == GENERIC_CMD_SET_SPRITE)
1667 v2s16 p = readV2S16(is);
1668 int num_frames = readU16(is);
1669 float framelength = readF1000(is);
1670 bool select_horiz_by_yawpitch = readU8(is);
1673 m_anim_num_frames = num_frames;
1674 m_anim_framelength = framelength;
1675 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1679 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE)
1681 float override_speed = readF1000(is);
1682 float override_jump = readF1000(is);
1683 float override_gravity = readF1000(is);
1684 // these are sent inverted so we get true when the server sends nothing
1685 bool sneak = !readU8(is);
1686 bool sneak_glitch = !readU8(is);
1689 if(m_is_local_player)
1691 LocalPlayer *player = m_env->getLocalPlayer();
1692 player->physics_override_speed = override_speed;
1693 player->physics_override_jump = override_jump;
1694 player->physics_override_gravity = override_gravity;
1695 player->physics_override_sneak = sneak;
1696 player->physics_override_sneak_glitch = sneak_glitch;
1699 else if(cmd == GENERIC_CMD_SET_ANIMATION)
1701 // TODO: change frames send as v2s32 value
1702 v2f range = readV2F1000(is);
1703 if (!m_is_local_player) {
1704 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1705 m_animation_speed = readF1000(is);
1706 m_animation_blend = readF1000(is);
1709 LocalPlayer *player = m_env->getLocalPlayer();
1710 if(player->last_animation == NO_ANIM) {
1711 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1712 m_animation_speed = readF1000(is);
1713 m_animation_blend = readF1000(is);
1715 // update animation only if local animations present
1716 // and received animation is unknown (except idle animation)
1717 bool is_known = false;
1718 for (int i = 1;i<4;i++) {
1719 if(m_animation_range.Y == player->local_animations[i].Y)
1723 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1729 else if(cmd == GENERIC_CMD_SET_BONE_POSITION)
1731 std::string bone = deSerializeString(is);
1732 v3f position = readV3F1000(is);
1733 v3f rotation = readV3F1000(is);
1734 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1736 updateBonePosition();
1738 else if(cmd == GENERIC_CMD_SET_ATTACHMENT)
1740 // If an entry already exists for this object, delete it first to avoid duplicates
1741 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1743 if(ii->X == getId()) // This is the ID of our object
1745 m_env->attachment_list.erase(ii);
1749 m_env->attachment_list.push_back(core::vector2d<int>(getId(), readS16(is)));
1750 m_attachment_bone = deSerializeString(is);
1751 m_attachment_position = readV3F1000(is);
1752 m_attachment_rotation = readV3F1000(is);
1754 updateAttachments();
1756 else if(cmd == GENERIC_CMD_PUNCHED)
1758 /*s16 damage =*/ readS16(is);
1759 s16 result_hp = readS16(is);
1761 // Use this instead of the send damage to not interfere with prediction
1762 s16 damage = m_hp - result_hp;
1768 // TODO: Execute defined fast response
1769 // As there is no definition, make a smoke puff
1770 ClientSimpleObject *simple = createSmokePuff(
1771 m_smgr, m_env, m_position,
1772 m_prop.visual_size * BS);
1773 m_env->addSimpleObject(simple);
1775 // TODO: Execute defined fast response
1776 // Flashing shall suffice as there is no definition
1777 m_reset_textures_timer = 0.05;
1779 m_reset_textures_timer += 0.05 * damage;
1780 updateTextures("^[brighten");
1784 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1786 m_armor_groups.clear();
1787 int armor_groups_size = readU16(is);
1788 for(int i=0; i<armor_groups_size; i++){
1789 std::string name = deSerializeString(is);
1790 int rating = readS16(is);
1791 m_armor_groups[name] = rating;
1796 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1797 float time_from_last_punch)
1800 const ToolCapabilities *toolcap =
1801 &punchitem->getToolCapabilities(m_gamedef->idef());
1802 PunchDamageResult result = getPunchDamage(
1806 time_from_last_punch);
1808 if(result.did_punch && result.damage != 0)
1810 if(result.damage < m_hp){
1811 m_hp -= result.damage;
1814 // TODO: Execute defined fast response
1815 // As there is no definition, make a smoke puff
1816 ClientSimpleObject *simple = createSmokePuff(
1817 m_smgr, m_env, m_position,
1818 m_prop.visual_size * BS);
1819 m_env->addSimpleObject(simple);
1821 // TODO: Execute defined fast response
1822 // Flashing shall suffice as there is no definition
1823 m_reset_textures_timer = 0.05;
1824 if(result.damage >= 2)
1825 m_reset_textures_timer += 0.05 * result.damage;
1826 updateTextures("^[brighten");
1832 std::string GenericCAO::debugInfoText()
1834 std::ostringstream os(std::ios::binary);
1835 os<<"GenericCAO hp="<<m_hp<<"\n";
1837 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1838 i != m_armor_groups.end(); i++){
1839 os<<i->first<<"="<<i->second<<", ";
1846 GenericCAO proto_GenericCAO(NULL, NULL);