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)
89 anim_time = update_interval;
91 if(anim_time < 0.001 || anim_time > 1.0)
92 anim_time = anim_time_counter;
94 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
96 anim_time_counter = 0;
100 void SmoothTranslator::translate(f32 dtime)
102 anim_time_counter = anim_time_counter + dtime;
103 anim_counter = anim_counter + dtime;
104 v3f vect_move = vect_aim - vect_old;
106 if(anim_time > 0.001)
107 moveratio = anim_time_counter / anim_time;
108 // Move a bit less than should, to avoid oscillation
109 moveratio = moveratio * 0.8;
110 float move_end = 1.5;
113 if(moveratio > move_end)
114 moveratio = move_end;
115 vect_show = vect_old + vect_move * moveratio;
118 bool SmoothTranslator::is_moving()
120 return ((anim_time_counter / anim_time) < 1.4);
127 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
128 float txs, float tys, int col, int row)
130 video::SMaterial& material = bill->getMaterial(0);
131 core::matrix4& matrix = material.getTextureMatrix(0);
132 matrix.setTextureTranslate(txs*col, tys*row);
133 matrix.setTextureScale(txs, tys);
140 class TestCAO : public ClientActiveObject
143 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
148 return ACTIVEOBJECT_TYPE_TEST;
151 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
153 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
154 IrrlichtDevice *irr);
155 void removeFromScene(bool permanent);
156 void updateLight(u8 light_at_pos);
157 v3s16 getLightPosition();
158 void updateNodePos();
160 void step(float dtime, ClientEnvironment *env);
162 void processMessage(const std::string &data);
164 bool getCollisionBox(aabb3f *toset) { return false; }
166 scene::IMeshSceneNode *m_node;
171 TestCAO proto_TestCAO(NULL, NULL);
173 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
174 ClientActiveObject(0, gamedef, env),
176 m_position(v3f(0,10*BS,0))
178 ClientActiveObject::registerType(getType(), create);
185 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
187 return new TestCAO(gamedef, env);
190 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
196 //video::IVideoDriver* driver = smgr->getVideoDriver();
198 scene::SMesh *mesh = new scene::SMesh();
199 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
200 video::SColor c(255,255,255,255);
201 video::S3DVertex vertices[4] =
203 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
204 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
205 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
206 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
208 u16 indices[] = {0,1,2,2,3,0};
209 buf->append(vertices, 4, indices, 6);
211 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
212 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
213 buf->getMaterial().setTexture(0, tsrc->getTexture("rat.png"));
214 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
215 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
216 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
218 mesh->addMeshBuffer(buf);
220 m_node = smgr->addMeshSceneNode(mesh, NULL);
225 void TestCAO::removeFromScene(bool permanent)
234 void TestCAO::updateLight(u8 light_at_pos)
238 v3s16 TestCAO::getLightPosition()
240 return floatToInt(m_position, BS);
243 void TestCAO::updateNodePos()
248 m_node->setPosition(m_position);
249 //m_node->setRotation(v3f(0, 45, 0));
252 void TestCAO::step(float dtime, ClientEnvironment *env)
256 v3f rot = m_node->getRotation();
257 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
258 rot.Y += dtime * 180;
259 m_node->setRotation(rot);
263 void TestCAO::processMessage(const std::string &data)
265 infostream<<"TestCAO: Got data: "<<data<<std::endl;
266 std::istringstream is(data, std::ios::binary);
284 class ItemCAO : public ClientActiveObject
287 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
292 return ACTIVEOBJECT_TYPE_ITEM;
295 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
297 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
298 IrrlichtDevice *irr);
299 void removeFromScene(bool permanent);
300 void updateLight(u8 light_at_pos);
301 v3s16 getLightPosition();
302 void updateNodePos();
303 void updateInfoText();
304 void updateTexture();
306 void step(float dtime, ClientEnvironment *env);
308 void processMessage(const std::string &data);
310 void initialize(const std::string &data);
312 core::aabbox3d<f32>* getSelectionBox()
313 {return &m_selection_box;}
317 std::string infoText()
320 bool getCollisionBox(aabb3f *toset) { return false; }
322 core::aabbox3d<f32> m_selection_box;
323 scene::IMeshSceneNode *m_node;
325 std::string m_itemstring;
326 std::string m_infotext;
329 #include "inventory.h"
332 ItemCAO proto_ItemCAO(NULL, NULL);
334 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
335 ClientActiveObject(0, gamedef, env),
336 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
338 m_position(v3f(0,10*BS,0))
342 ClientActiveObject::registerType(getType(), create);
350 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
352 return new ItemCAO(gamedef, env);
355 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
361 //video::IVideoDriver* driver = smgr->getVideoDriver();
363 scene::SMesh *mesh = new scene::SMesh();
364 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
365 video::SColor c(255,255,255,255);
366 video::S3DVertex vertices[4] =
368 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
369 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
370 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
371 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
372 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
373 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
374 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
375 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
377 u16 indices[] = {0,1,2,2,3,0};
378 buf->append(vertices, 4, indices, 6);
380 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
381 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
382 // Initialize with a generated placeholder texture
383 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
384 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
385 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
386 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
388 mesh->addMeshBuffer(buf);
390 m_node = smgr->addMeshSceneNode(mesh, NULL);
401 void ItemCAO::removeFromScene(bool permanent)
410 void ItemCAO::updateLight(u8 light_at_pos)
415 u8 li = decode_light(light_at_pos);
416 video::SColor color(255,li,li,li);
417 setMeshColor(m_node->getMesh(), color);
420 v3s16 ItemCAO::getLightPosition()
422 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
425 void ItemCAO::updateNodePos()
430 m_node->setPosition(m_position);
433 void ItemCAO::updateInfoText()
436 IItemDefManager *idef = m_gamedef->idef();
438 item.deSerialize(m_itemstring, idef);
439 if(item.isKnown(idef))
440 m_infotext = item.getDefinition(idef).description;
442 m_infotext = "Unknown item: '" + m_itemstring + "'";
444 m_infotext += " (" + itos(item.count) + ")";
446 catch(SerializationError &e)
448 m_infotext = "Unknown item: '" + m_itemstring + "'";
452 void ItemCAO::updateTexture()
457 // Create an inventory item to see what is its image
458 std::istringstream is(m_itemstring, std::ios_base::binary);
459 video::ITexture *texture = NULL;
461 IItemDefManager *idef = m_gamedef->idef();
463 item.deSerialize(is, idef);
464 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
466 catch(SerializationError &e)
468 infostream<<"WARNING: "<<__FUNCTION_NAME
469 <<": error deSerializing itemstring \""
470 <<m_itemstring<<std::endl;
473 // Set meshbuffer texture
474 m_node->getMaterial(0).setTexture(0, texture);
478 void ItemCAO::step(float dtime, ClientEnvironment *env)
482 /*v3f rot = m_node->getRotation();
483 rot.Y += dtime * 120;
484 m_node->setRotation(rot);*/
485 LocalPlayer *player = env->getLocalPlayer();
487 v3f rot = m_node->getRotation();
488 rot.Y = 180.0 - (player->getYaw());
489 m_node->setRotation(rot);
493 void ItemCAO::processMessage(const std::string &data)
495 //infostream<<"ItemCAO: Got message"<<std::endl;
496 std::istringstream is(data, std::ios::binary);
502 m_position = readV3F1000(is);
508 m_itemstring = deSerializeString(is);
514 void ItemCAO::initialize(const std::string &data)
516 infostream<<"ItemCAO: Got init data"<<std::endl;
519 std::istringstream is(data, std::ios::binary);
521 u8 version = readU8(is);
526 m_position = readV3F1000(is);
528 m_itemstring = deSerializeString(is);
539 #include "genericobject.h"
541 GenericCAO::GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
542 ClientActiveObject(0, gamedef, env),
545 m_is_local_player(false),
550 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
552 m_animated_meshnode(NULL),
555 m_position(v3f(0,10*BS,0)),
556 m_velocity(v3f(0,0,0)),
557 m_acceleration(v3f(0,0,0)),
562 m_initial_tx_basepos_set(false),
563 m_tx_select_horiz_by_yawpitch(false),
564 m_animation_range(v2s32(0,0)),
565 m_animation_speed(15),
566 m_animation_blend(0),
567 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
568 m_attachment_bone(""),
569 m_attachment_position(v3f(0,0,0)),
570 m_attachment_rotation(v3f(0,0,0)),
571 m_attached_to_local(false),
573 m_anim_num_frames(1),
574 m_anim_framelength(0.2),
576 m_reset_textures_timer(-1),
577 m_visuals_expired(false),
578 m_step_distance_counter(0),
583 ClientActiveObject::registerType(getType(), create);
586 bool GenericCAO::getCollisionBox(aabb3f *toset)
590 //update collision box
591 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
592 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
594 toset->MinEdge += m_position;
595 toset->MaxEdge += m_position;
603 bool GenericCAO::collideWithObjects()
605 return m_prop.collideWithObjects;
608 void GenericCAO::initialize(const std::string &data)
610 infostream<<"GenericCAO: Got init data"<<std::endl;
611 std::istringstream is(data, std::ios::binary);
612 int num_messages = 0;
614 u8 version = readU8(is);
616 if(version == 1) // In PROTOCOL_VERSION 14
618 m_name = deSerializeString(is);
619 m_is_player = readU8(is);
621 m_position = readV3F1000(is);
622 m_yaw = readF1000(is);
624 num_messages = readU8(is);
626 else if(version == 0) // In PROTOCOL_VERSION 13
628 m_name = deSerializeString(is);
629 m_is_player = readU8(is);
630 m_position = readV3F1000(is);
631 m_yaw = readF1000(is);
633 num_messages = readU8(is);
637 errorstream<<"GenericCAO: Unsupported init data version"
642 for(int i=0; i<num_messages; i++)
644 std::string message = deSerializeLongString(is);
645 processMessage(message);
648 pos_translator.init(m_position);
653 Player *player = m_env->getPlayer(m_name.c_str());
654 if(player && player->isLocal())
656 m_is_local_player = true;
657 m_is_visible = false;
658 LocalPlayer* localplayer = dynamic_cast<LocalPlayer*>(player);
660 assert( localplayer != NULL );
661 localplayer->setCAO(this);
663 m_env->addPlayerName(m_name.c_str());
667 GenericCAO::~GenericCAO()
671 m_env->removePlayerName(m_name.c_str());
675 core::aabbox3d<f32>* GenericCAO::getSelectionBox()
677 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
679 return &m_selection_box;
682 v3f GenericCAO::getPosition()
684 if(getParent() != NULL)
687 return m_meshnode->getAbsolutePosition();
688 if(m_animated_meshnode)
689 return m_animated_meshnode->getAbsolutePosition();
691 return m_spritenode->getAbsolutePosition();
694 return pos_translator.vect_show;
697 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
704 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
706 if(m_animated_meshnode)
707 return m_animated_meshnode;
711 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
718 void GenericCAO::setAttachments()
723 ClientActiveObject* GenericCAO::getParent()
725 ClientActiveObject *obj = NULL;
727 u16 attached_id = m_env->m_attachements[getId()];
729 if ((attached_id != 0) &&
730 (attached_id != getId())) {
731 obj = m_env->getActiveObject(attached_id);
736 void GenericCAO::removeFromScene(bool permanent)
738 if(permanent) // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
740 for(std::vector<u16>::iterator ci = m_children.begin();
741 ci != m_children.end(); ci++)
743 if (m_env->m_attachements[*ci] == getId()) {
744 m_env->m_attachements[*ci] = 0;
748 m_env->m_attachements[getId()] = 0;
753 m_meshnode->remove();
756 if(m_animated_meshnode)
758 m_animated_meshnode->remove();
759 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")
786 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
787 m_spritenode = smgr->addBillboardSceneNode(
788 NULL, v2f(1, 1), v3f(0,0,0), -1);
789 m_spritenode->setMaterialTexture(0,
790 tsrc->getTexture("unknown_node.png"));
791 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
792 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
793 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
794 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
795 u8 li = m_last_light;
796 m_spritenode->setColor(video::SColor(255,li,li,li));
797 m_spritenode->setSize(m_prop.visual_size*BS);
799 const float txs = 1.0 / 1;
800 const float tys = 1.0 / 1;
801 setBillboardTextureMatrix(m_spritenode,
805 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)
955 video::SColor color(255,li,li,li);
957 setMeshColor(m_meshnode->getMesh(), color);
958 if(m_animated_meshnode)
959 setMeshColor(m_animated_meshnode->getMesh(), color);
961 m_spritenode->setColor(color);
965 v3s16 GenericCAO::getLightPosition()
967 return floatToInt(m_position, BS);
970 void GenericCAO::updateNodePos()
972 if(getParent() != NULL)
975 v3s16 camera_offset = m_env->getCameraOffset();
978 m_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
979 v3f rot = m_meshnode->getRotation();
981 m_meshnode->setRotation(rot);
983 if(m_animated_meshnode)
985 m_animated_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
986 v3f rot = m_animated_meshnode->getRotation();
988 m_animated_meshnode->setRotation(rot);
992 m_spritenode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
996 void GenericCAO::step(float dtime, ClientEnvironment *env)
998 // Handel model of local player instantly to prevent lags
999 if(m_is_local_player)
1001 LocalPlayer *player = m_env->getLocalPlayer();
1005 int old_anim = player->last_animation;
1006 float old_anim_speed = player->last_animation_speed;
1007 m_position = player->getPosition() + v3f(0,BS,0);
1008 m_velocity = v3f(0,0,0);
1009 m_acceleration = v3f(0,0,0);
1010 pos_translator.vect_show = m_position;
1011 m_yaw = player->getYaw();
1012 PlayerControl controls = player->getPlayerControl();
1014 bool walking = false;
1015 if(controls.up || controls.down || controls.left || controls.right)
1018 f32 new_speed = player->local_animation_speed;
1019 v2s32 new_anim = v2s32(0,0);
1020 bool allow_update = false;
1022 // increase speed if using fast or flying fast
1023 if((g_settings->getBool("fast_move") &&
1024 m_gamedef->checkLocalPrivilege("fast")) &&
1026 (!player->touching_ground &&
1027 g_settings->getBool("free_move") &&
1028 m_gamedef->checkLocalPrivilege("fly"))))
1030 // slowdown speed if sneeking
1031 if(controls.sneak && walking)
1034 if(walking && (controls.LMB || controls.RMB))
1036 new_anim = player->local_animations[3];
1037 player->last_animation = WD_ANIM;
1038 } else if(walking) {
1039 new_anim = player->local_animations[1];
1040 player->last_animation = WALK_ANIM;
1041 } else if(controls.LMB || controls.RMB) {
1042 new_anim = player->local_animations[2];
1043 player->last_animation = DIG_ANIM;
1046 // Apply animations if input detected and not attached
1047 // or set idle animation
1048 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1050 allow_update = true;
1051 m_animation_range = new_anim;
1052 m_animation_speed = new_speed;
1053 player->last_animation_speed = m_animation_speed;
1055 player->last_animation = NO_ANIM;
1057 if (old_anim != NO_ANIM)
1059 m_animation_range = player->local_animations[0];
1064 // Update local player animations
1065 if ((player->last_animation != old_anim ||
1066 m_animation_speed != old_anim_speed) &&
1067 player->last_animation != NO_ANIM && allow_update)
1073 if(m_visuals_expired && m_smgr && m_irr){
1074 m_visuals_expired = false;
1076 // Attachments, part 1: All attached objects must be unparented first,
1077 // or Irrlicht causes a segmentation fault
1078 for(std::vector<u16>::iterator ci = m_children.begin();
1079 ci != m_children.end();)
1081 if (m_env->m_attachements[*ci] != getId()) {
1082 ci = m_children.erase(ci);
1085 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1088 scene::IMeshSceneNode *m_child_meshnode
1089 = obj->getMeshSceneNode();
1090 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode
1091 = obj->getAnimatedMeshSceneNode();
1092 scene::IBillboardSceneNode *m_child_spritenode
1093 = obj->getSpriteSceneNode();
1094 if(m_child_meshnode)
1095 m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1096 if(m_child_animated_meshnode)
1097 m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1098 if(m_child_spritenode)
1099 m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1104 removeFromScene(false);
1105 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1107 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1108 for(std::vector<u16>::iterator ci = m_children.begin();
1109 ci != m_children.end(); ci++)
1111 // Get the object of the child
1112 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1114 obj->setAttachments();
1118 // Make sure m_is_visible is always applied
1120 m_meshnode->setVisible(m_is_visible);
1121 if(m_animated_meshnode)
1122 m_animated_meshnode->setVisible(m_is_visible);
1124 m_spritenode->setVisible(m_is_visible);
1126 m_textnode->setVisible(m_is_visible);
1128 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1130 // Set these for later
1131 m_position = getPosition();
1132 m_velocity = v3f(0,0,0);
1133 m_acceleration = v3f(0,0,0);
1134 pos_translator.vect_show = m_position;
1136 if(m_is_local_player) // Update local player attachment position
1138 LocalPlayer *player = m_env->getLocalPlayer();
1139 player->overridePosition = getParent()->getPosition();
1140 m_env->getLocalPlayer()->parent = getParent();
1143 v3f lastpos = pos_translator.vect_show;
1147 core::aabbox3d<f32> box = m_prop.collisionbox;
1150 collisionMoveResult moveresult;
1151 f32 pos_max_d = BS*0.125; // Distance per iteration
1152 v3f p_pos = m_position;
1153 v3f p_velocity = m_velocity;
1154 v3f p_acceleration = m_acceleration;
1155 moveresult = collisionMoveSimple(env,env->getGameDef(),
1156 pos_max_d, box, m_prop.stepheight, dtime,
1157 p_pos, p_velocity, p_acceleration,
1158 this, m_prop.collideWithObjects);
1161 m_velocity = p_velocity;
1162 m_acceleration = p_acceleration;
1164 bool is_end_position = moveresult.collides;
1165 pos_translator.update(m_position, is_end_position, dtime);
1166 pos_translator.translate(dtime);
1169 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1170 m_velocity += dtime * m_acceleration;
1171 pos_translator.update(m_position, pos_translator.aim_is_end,
1172 pos_translator.anim_time);
1173 pos_translator.translate(dtime);
1177 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1178 m_step_distance_counter += moved;
1179 if(m_step_distance_counter > 1.5*BS)
1181 m_step_distance_counter = 0;
1182 if(!m_is_local_player && m_prop.makes_footstep_sound)
1184 INodeDefManager *ndef = m_gamedef->ndef();
1185 v3s16 p = floatToInt(getPosition() + v3f(0,
1186 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1187 MapNode n = m_env->getMap().getNodeNoEx(p);
1188 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1189 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1194 m_anim_timer += dtime;
1195 if(m_anim_timer >= m_anim_framelength)
1197 m_anim_timer -= m_anim_framelength;
1199 if(m_anim_frame >= m_anim_num_frames)
1205 if(m_reset_textures_timer >= 0)
1207 m_reset_textures_timer -= dtime;
1208 if(m_reset_textures_timer <= 0){
1209 m_reset_textures_timer = -1;
1213 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1215 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1219 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1220 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1222 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1223 + m_prop.automatic_face_movement_dir_offset;
1228 void GenericCAO::updateTexturePos()
1232 scene::ICameraSceneNode* camera =
1233 m_spritenode->getSceneManager()->getActiveCamera();
1236 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1237 - camera->getAbsolutePosition();
1238 cam_to_entity.normalize();
1240 int row = m_tx_basepos.Y;
1241 int col = m_tx_basepos.X;
1243 if(m_tx_select_horiz_by_yawpitch)
1245 if(cam_to_entity.Y > 0.75)
1247 else if(cam_to_entity.Y < -0.75)
1251 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1252 float dir = mob_dir - m_yaw;
1253 dir = wrapDegrees_180(dir);
1254 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1255 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1257 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1259 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1261 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1268 // Animation goes downwards
1269 row += m_anim_frame;
1271 float txs = m_tx_size.X;
1272 float tys = m_tx_size.Y;
1273 setBillboardTextureMatrix(m_spritenode,
1274 txs, tys, col, row);
1278 void GenericCAO::updateTextures(const std::string &mod)
1280 ITextureSource *tsrc = m_gamedef->tsrc();
1282 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1283 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1284 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1288 if(m_prop.visual == "sprite")
1290 std::string texturestring = "unknown_node.png";
1291 if(m_prop.textures.size() >= 1)
1292 texturestring = m_prop.textures[0];
1293 texturestring += mod;
1294 m_spritenode->setMaterialTexture(0,
1295 tsrc->getTexture(texturestring));
1297 // This allows setting per-material colors. However, until a real lighting
1298 // system is added, the code below will have no effect. Once MineTest
1299 // has directional lighting, it should work automatically.
1300 if(m_prop.colors.size() >= 1)
1302 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1303 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1304 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1307 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1308 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1309 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1312 if(m_animated_meshnode)
1314 if(m_prop.visual == "mesh")
1316 for (u32 i = 0; i < m_prop.textures.size() &&
1317 i < m_animated_meshnode->getMaterialCount(); ++i)
1319 std::string texturestring = m_prop.textures[i];
1320 if(texturestring == "")
1321 continue; // Empty texture string means don't modify that material
1322 texturestring += mod;
1323 video::ITexture* texture = tsrc->getTexture(texturestring);
1326 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1330 // Set material flags and texture
1331 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1332 material.TextureLayer[0].Texture = texture;
1333 material.setFlag(video::EMF_LIGHTING, false);
1334 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1336 m_animated_meshnode->getMaterial(i)
1337 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1338 m_animated_meshnode->getMaterial(i)
1339 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1340 m_animated_meshnode->getMaterial(i)
1341 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1343 for (u32 i = 0; i < m_prop.colors.size() &&
1344 i < m_animated_meshnode->getMaterialCount(); ++i)
1346 // This allows setting per-material colors. However, until a real lighting
1347 // system is added, the code below will have no effect. Once MineTest
1348 // has directional lighting, it should work automatically.
1349 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1350 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1351 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1357 if(m_prop.visual == "cube")
1359 for (u32 i = 0; i < 6; ++i)
1361 std::string texturestring = "unknown_node.png";
1362 if(m_prop.textures.size() > i)
1363 texturestring = m_prop.textures[i];
1364 texturestring += mod;
1367 // Set material flags and texture
1368 video::SMaterial& material = m_meshnode->getMaterial(i);
1369 material.setFlag(video::EMF_LIGHTING, false);
1370 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1371 material.setTexture(0,
1372 tsrc->getTexture(texturestring));
1373 material.getTextureMatrix(0).makeIdentity();
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() > i)
1380 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1381 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1382 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1385 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1386 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1387 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1390 else if(m_prop.visual == "upright_sprite")
1392 scene::IMesh *mesh = m_meshnode->getMesh();
1394 std::string tname = "unknown_object.png";
1395 if(m_prop.textures.size() >= 1)
1396 tname = m_prop.textures[0];
1398 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1399 buf->getMaterial().setTexture(0,
1400 tsrc->getTexture(tname));
1402 // This allows setting per-material colors. However, until a real lighting
1403 // system is added, the code below will have no effect. Once MineTest
1404 // has directional lighting, it should work automatically.
1405 if(m_prop.colors.size() >= 1)
1407 buf->getMaterial().AmbientColor = m_prop.colors[0];
1408 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1409 buf->getMaterial().SpecularColor = m_prop.colors[0];
1412 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1413 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1414 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1417 std::string tname = "unknown_object.png";
1418 if(m_prop.textures.size() >= 2)
1419 tname = m_prop.textures[1];
1420 else if(m_prop.textures.size() >= 1)
1421 tname = m_prop.textures[0];
1423 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1424 buf->getMaterial().setTexture(0,
1425 tsrc->getTexture(tname));
1427 // This allows setting per-material colors. However, until a real lighting
1428 // system is added, the code below will have no effect. Once MineTest
1429 // has directional lighting, it should work automatically.
1430 if(m_prop.colors.size() >= 2)
1432 buf->getMaterial().AmbientColor = m_prop.colors[1];
1433 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1434 buf->getMaterial().SpecularColor = m_prop.colors[1];
1436 else if(m_prop.colors.size() >= 1)
1438 buf->getMaterial().AmbientColor = m_prop.colors[0];
1439 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1440 buf->getMaterial().SpecularColor = m_prop.colors[0];
1443 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1444 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1445 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1451 void GenericCAO::updateAnimation()
1453 if(m_animated_meshnode == NULL)
1455 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1456 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1457 m_animated_meshnode->setTransitionTime(m_animation_blend);
1460 void GenericCAO::updateBonePosition()
1462 if(!m_bone_position.size() || m_animated_meshnode == NULL)
1465 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1466 for(std::map<std::string,
1467 core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1468 ii != m_bone_position.end(); ++ii)
1470 std::string bone_name = (*ii).first;
1471 v3f bone_pos = (*ii).second.X;
1472 v3f bone_rot = (*ii).second.Y;
1473 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1476 bone->setPosition(bone_pos);
1477 bone->setRotation(bone_rot);
1482 void GenericCAO::updateAttachments()
1485 // localplayer itself can't be attached to localplayer
1486 if (!m_is_local_player)
1488 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1489 // Objects attached to the local player should always be hidden
1490 m_is_visible = !m_attached_to_local;
1493 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1497 v3f old_position = m_meshnode->getAbsolutePosition();
1498 v3f old_rotation = m_meshnode->getRotation();
1499 m_meshnode->setParent(m_smgr->getRootSceneNode());
1500 m_meshnode->setPosition(old_position);
1501 m_meshnode->setRotation(old_rotation);
1502 m_meshnode->updateAbsolutePosition();
1504 if(m_animated_meshnode)
1506 v3f old_position = m_animated_meshnode->getAbsolutePosition();
1507 v3f old_rotation = m_animated_meshnode->getRotation();
1508 m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1509 m_animated_meshnode->setPosition(old_position);
1510 m_animated_meshnode->setRotation(old_rotation);
1511 m_animated_meshnode->updateAbsolutePosition();
1515 v3f old_position = m_spritenode->getAbsolutePosition();
1516 v3f old_rotation = m_spritenode->getRotation();
1517 m_spritenode->setParent(m_smgr->getRootSceneNode());
1518 m_spritenode->setPosition(old_position);
1519 m_spritenode->setRotation(old_rotation);
1520 m_spritenode->updateAbsolutePosition();
1522 if(m_is_local_player)
1524 LocalPlayer *player = m_env->getLocalPlayer();
1525 player->isAttached = false;
1530 scene::IMeshSceneNode *parent_mesh = NULL;
1531 if(getParent()->getMeshSceneNode())
1532 parent_mesh = getParent()->getMeshSceneNode();
1533 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1534 if(getParent()->getAnimatedMeshSceneNode())
1535 parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1536 scene::IBillboardSceneNode *parent_sprite = NULL;
1537 if(getParent()->getSpriteSceneNode())
1538 parent_sprite = getParent()->getSpriteSceneNode();
1540 scene::IBoneSceneNode *parent_bone = NULL;
1541 if(parent_animated_mesh && m_attachment_bone != "")
1544 parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1546 // The spaghetti code below makes sure attaching works if either the
1547 // parent or child is a spritenode, meshnode, or animatedmeshnode
1548 // TODO: Perhaps use polymorphism here to save code duplication
1553 m_meshnode->setParent(parent_bone);
1554 m_meshnode->setPosition(m_attachment_position);
1555 m_meshnode->setRotation(m_attachment_rotation);
1556 m_meshnode->updateAbsolutePosition();
1562 m_meshnode->setParent(parent_mesh);
1563 m_meshnode->setPosition(m_attachment_position);
1564 m_meshnode->setRotation(m_attachment_rotation);
1565 m_meshnode->updateAbsolutePosition();
1567 else if(parent_animated_mesh) {
1568 m_meshnode->setParent(parent_animated_mesh);
1569 m_meshnode->setPosition(m_attachment_position);
1570 m_meshnode->setRotation(m_attachment_rotation);
1571 m_meshnode->updateAbsolutePosition();
1573 else if(parent_sprite) {
1574 m_meshnode->setParent(parent_sprite);
1575 m_meshnode->setPosition(m_attachment_position);
1576 m_meshnode->setRotation(m_attachment_rotation);
1577 m_meshnode->updateAbsolutePosition();
1581 if(m_animated_meshnode)
1585 m_animated_meshnode->setParent(parent_bone);
1586 m_animated_meshnode->setPosition(m_attachment_position);
1587 m_animated_meshnode->setRotation(m_attachment_rotation);
1588 m_animated_meshnode->updateAbsolutePosition();
1594 m_animated_meshnode->setParent(parent_mesh);
1595 m_animated_meshnode->setPosition(m_attachment_position);
1596 m_animated_meshnode->setRotation(m_attachment_rotation);
1597 m_animated_meshnode->updateAbsolutePosition();
1598 } else if(parent_animated_mesh) {
1599 m_animated_meshnode->setParent(parent_animated_mesh);
1600 m_animated_meshnode->setPosition(m_attachment_position);
1601 m_animated_meshnode->setRotation(m_attachment_rotation);
1602 m_animated_meshnode->updateAbsolutePosition();
1603 } else if(parent_sprite) {
1604 m_animated_meshnode->setParent(parent_sprite);
1605 m_animated_meshnode->setPosition(m_attachment_position);
1606 m_animated_meshnode->setRotation(m_attachment_rotation);
1607 m_animated_meshnode->updateAbsolutePosition();
1615 m_spritenode->setParent(parent_bone);
1616 m_spritenode->setPosition(m_attachment_position);
1617 m_spritenode->setRotation(m_attachment_rotation);
1618 m_spritenode->updateAbsolutePosition();
1622 m_spritenode->setParent(parent_mesh);
1623 m_spritenode->setPosition(m_attachment_position);
1624 m_spritenode->setRotation(m_attachment_rotation);
1625 m_spritenode->updateAbsolutePosition();
1627 else if(parent_animated_mesh) {
1628 m_spritenode->setParent(parent_animated_mesh);
1629 m_spritenode->setPosition(m_attachment_position);
1630 m_spritenode->setRotation(m_attachment_rotation);
1631 m_spritenode->updateAbsolutePosition();
1633 else if(parent_sprite) {
1634 m_spritenode->setParent(parent_sprite);
1635 m_spritenode->setPosition(m_attachment_position);
1636 m_spritenode->setRotation(m_attachment_rotation);
1637 m_spritenode->updateAbsolutePosition();
1641 if(m_is_local_player)
1643 LocalPlayer *player = m_env->getLocalPlayer();
1644 player->isAttached = true;
1649 void GenericCAO::processMessage(const std::string &data)
1651 //infostream<<"GenericCAO: Got message"<<std::endl;
1652 std::istringstream is(data, std::ios::binary);
1654 u8 cmd = readU8(is);
1655 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1657 m_prop = gob_read_set_properties(is);
1659 m_selection_box = m_prop.collisionbox;
1660 m_selection_box.MinEdge *= BS;
1661 m_selection_box.MaxEdge *= BS;
1663 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1664 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1666 if(!m_initial_tx_basepos_set){
1667 m_initial_tx_basepos_set = true;
1668 m_tx_basepos = m_prop.initial_sprite_basepos;
1673 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1675 // Not sent by the server if this object is an attachment.
1676 // We might however get here if the server notices the object being detached before the client.
1677 m_position = readV3F1000(is);
1678 m_velocity = readV3F1000(is);
1679 m_acceleration = readV3F1000(is);
1680 if(fabs(m_prop.automatic_rotate) < 0.001)
1681 m_yaw = readF1000(is);
1684 bool do_interpolate = readU8(is);
1685 bool is_end_position = readU8(is);
1686 float update_interval = readF1000(is);
1688 // Place us a bit higher if we're physical, to not sink into
1689 // the ground due to sucky collision detection...
1691 m_position += v3f(0,0.002,0);
1693 if(getParent() != NULL) // Just in case
1698 if(!m_prop.physical)
1699 pos_translator.update(m_position, is_end_position, update_interval);
1701 pos_translator.init(m_position);
1705 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1706 std::string mod = deSerializeString(is);
1707 updateTextures(mod);
1709 else if(cmd == GENERIC_CMD_SET_SPRITE) {
1710 v2s16 p = readV2S16(is);
1711 int num_frames = readU16(is);
1712 float framelength = readF1000(is);
1713 bool select_horiz_by_yawpitch = readU8(is);
1716 m_anim_num_frames = num_frames;
1717 m_anim_framelength = framelength;
1718 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1722 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1723 float override_speed = readF1000(is);
1724 float override_jump = readF1000(is);
1725 float override_gravity = readF1000(is);
1726 // these are sent inverted so we get true when the server sends nothing
1727 bool sneak = !readU8(is);
1728 bool sneak_glitch = !readU8(is);
1731 if(m_is_local_player)
1733 LocalPlayer *player = m_env->getLocalPlayer();
1734 player->physics_override_speed = override_speed;
1735 player->physics_override_jump = override_jump;
1736 player->physics_override_gravity = override_gravity;
1737 player->physics_override_sneak = sneak;
1738 player->physics_override_sneak_glitch = sneak_glitch;
1741 else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1742 // TODO: change frames send as v2s32 value
1743 v2f range = readV2F1000(is);
1744 if (!m_is_local_player) {
1745 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1746 m_animation_speed = readF1000(is);
1747 m_animation_blend = readF1000(is);
1750 LocalPlayer *player = m_env->getLocalPlayer();
1751 if(player->last_animation == NO_ANIM)
1753 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1754 m_animation_speed = readF1000(is);
1755 m_animation_blend = readF1000(is);
1757 // update animation only if local animations present
1758 // and received animation is unknown (except idle animation)
1759 bool is_known = false;
1760 for (int i = 1;i<4;i++)
1762 if(m_animation_range.Y == player->local_animations[i].Y)
1766 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1772 else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1773 std::string bone = deSerializeString(is);
1774 v3f position = readV3F1000(is);
1775 v3f rotation = readV3F1000(is);
1776 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1778 updateBonePosition();
1780 else if(cmd == GENERIC_CMD_SET_ATTACHMENT) {
1781 m_env->m_attachements[getId()] = readS16(is);
1782 m_children.push_back(m_env->m_attachements[getId()]);
1783 m_attachment_bone = deSerializeString(is);
1784 m_attachment_position = readV3F1000(is);
1785 m_attachment_rotation = readV3F1000(is);
1787 updateAttachments();
1789 else if(cmd == GENERIC_CMD_PUNCHED) {
1790 /*s16 damage =*/ readS16(is);
1791 s16 result_hp = readS16(is);
1793 // Use this instead of the send damage to not interfere with prediction
1794 s16 damage = m_hp - result_hp;
1802 // TODO: Execute defined fast response
1803 // As there is no definition, make a smoke puff
1804 ClientSimpleObject *simple = createSmokePuff(
1805 m_smgr, m_env, m_position,
1806 m_prop.visual_size * BS);
1807 m_env->addSimpleObject(simple);
1809 // TODO: Execute defined fast response
1810 // Flashing shall suffice as there is no definition
1811 m_reset_textures_timer = 0.05;
1813 m_reset_textures_timer += 0.05 * damage;
1814 updateTextures("^[brighten");
1818 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1819 m_armor_groups.clear();
1820 int armor_groups_size = readU16(is);
1821 for(int i=0; i<armor_groups_size; i++)
1823 std::string name = deSerializeString(is);
1824 int rating = readS16(is);
1825 m_armor_groups[name] = rating;
1830 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1831 float time_from_last_punch)
1834 const ToolCapabilities *toolcap =
1835 &punchitem->getToolCapabilities(m_gamedef->idef());
1836 PunchDamageResult result = getPunchDamage(
1840 time_from_last_punch);
1842 if(result.did_punch && result.damage != 0)
1844 if(result.damage < m_hp)
1846 m_hp -= result.damage;
1849 // TODO: Execute defined fast response
1850 // As there is no definition, make a smoke puff
1851 ClientSimpleObject *simple = createSmokePuff(
1852 m_smgr, m_env, m_position,
1853 m_prop.visual_size * BS);
1854 m_env->addSimpleObject(simple);
1856 // TODO: Execute defined fast response
1857 // Flashing shall suffice as there is no definition
1858 m_reset_textures_timer = 0.05;
1859 if(result.damage >= 2)
1860 m_reset_textures_timer += 0.05 * result.damage;
1861 updateTextures("^[brighten");
1867 std::string GenericCAO::debugInfoText()
1869 std::ostringstream os(std::ios::binary);
1870 os<<"GenericCAO hp="<<m_hp<<"\n";
1872 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1873 i != m_armor_groups.end(); i++)
1875 os<<i->first<<"="<<i->second<<", ";
1882 GenericCAO proto_GenericCAO(NULL, NULL);