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();
757 if(m_animated_meshnode)
759 m_animated_meshnode->remove();
760 m_animated_meshnode->drop();
761 m_animated_meshnode = NULL;
765 m_spritenode->remove();
766 m_spritenode->drop();
771 m_textnode->remove();
777 void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
783 if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
786 m_visuals_expired = false;
788 if(!m_prop.is_visible)
791 //video::IVideoDriver* driver = smgr->getVideoDriver();
793 if(m_prop.visual == "sprite")
795 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
796 m_spritenode = smgr->addBillboardSceneNode(
797 NULL, v2f(1, 1), v3f(0,0,0), -1);
798 m_spritenode->grab();
799 m_spritenode->setMaterialTexture(0,
800 tsrc->getTexture("unknown_node.png"));
801 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
802 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
803 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
804 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
805 u8 li = m_last_light;
806 m_spritenode->setColor(video::SColor(255,li,li,li));
807 m_spritenode->setSize(m_prop.visual_size*BS);
809 const float txs = 1.0 / 1;
810 const float tys = 1.0 / 1;
811 setBillboardTextureMatrix(m_spritenode,
815 else if(m_prop.visual == "upright_sprite") {
816 scene::SMesh *mesh = new scene::SMesh();
817 double dx = BS*m_prop.visual_size.X/2;
818 double dy = BS*m_prop.visual_size.Y/2;
820 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
821 u8 li = m_last_light;
822 video::SColor c(255,li,li,li);
823 video::S3DVertex vertices[4] =
825 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
826 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
827 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
828 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
830 u16 indices[] = {0,1,2,2,3,0};
831 buf->append(vertices, 4, indices, 6);
833 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
834 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
835 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
836 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
838 mesh->addMeshBuffer(buf);
842 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
843 u8 li = m_last_light;
844 video::SColor c(255,li,li,li);
845 video::S3DVertex vertices[4] =
847 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
848 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
849 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
850 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
852 u16 indices[] = {0,1,2,2,3,0};
853 buf->append(vertices, 4, indices, 6);
855 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
856 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
857 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
858 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
860 mesh->addMeshBuffer(buf);
863 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
866 // Set it to use the materials of the meshbuffers directly.
867 // This is needed for changing the texture in the future
868 m_meshnode->setReadOnlyMaterials(true);
870 else if(m_prop.visual == "cube") {
871 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
872 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
873 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
877 m_meshnode->setScale(v3f(m_prop.visual_size.X,
878 m_prop.visual_size.Y,
879 m_prop.visual_size.X));
880 u8 li = m_last_light;
881 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
883 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
884 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
885 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
886 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
888 else if(m_prop.visual == "mesh") {
889 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
890 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
893 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
894 m_animated_meshnode->grab();
895 mesh->drop(); // The scene node took hold of it
896 m_animated_meshnode->animateJoints(); // Needed for some animations
897 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
898 m_prop.visual_size.Y,
899 m_prop.visual_size.X));
900 u8 li = m_last_light;
901 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
903 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
904 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
905 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
906 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
909 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
911 else if(m_prop.visual == "wielditem") {
912 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
913 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
914 if(m_prop.textures.size() >= 1){
915 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
916 IItemDefManager *idef = m_gamedef->idef();
917 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
918 scene::IMesh *item_mesh = idef->getWieldMesh(item.getDefinition(idef).name, m_gamedef);
920 // Copy mesh to be able to set unique vertex colors
921 scene::IMeshManipulator *manip =
922 irr->getVideoDriver()->getMeshManipulator();
923 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
925 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
929 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
930 m_prop.visual_size.Y/2,
931 m_prop.visual_size.X/2));
932 u8 li = m_last_light;
933 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
936 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
937 <<"\" not supported"<<std::endl;
941 scene::ISceneNode *node = NULL;
944 else if(m_animated_meshnode)
945 node = m_animated_meshnode;
948 if(node && m_is_player && !m_is_local_player){
949 // Add a text node for showing the name
950 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
951 std::wstring wname = narrow_to_wide(m_name);
952 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
953 wname.c_str(), video::SColor(255,255,255,255), node);
955 m_textnode->setPosition(v3f(0, BS*1.1, 0));
960 updateBonePosition();
964 void GenericCAO::updateLight(u8 light_at_pos)
966 u8 li = decode_light(light_at_pos);
967 if(li != m_last_light)
970 video::SColor color(255,li,li,li);
972 setMeshColor(m_meshnode->getMesh(), color);
973 if(m_animated_meshnode)
974 setMeshColor(m_animated_meshnode->getMesh(), color);
976 m_spritenode->setColor(color);
980 v3s16 GenericCAO::getLightPosition()
982 return floatToInt(m_position, BS);
985 void GenericCAO::updateNodePos()
987 if(getParent() != NULL)
990 v3s16 camera_offset = m_env->getCameraOffset();
993 m_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
994 v3f rot = m_meshnode->getRotation();
996 m_meshnode->setRotation(rot);
998 if(m_animated_meshnode)
1000 m_animated_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
1001 v3f rot = m_animated_meshnode->getRotation();
1003 m_animated_meshnode->setRotation(rot);
1007 m_spritenode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
1011 void GenericCAO::step(float dtime, ClientEnvironment *env)
1013 // Handel model of local player instantly to prevent lags
1014 if(m_is_local_player)
1016 LocalPlayer *player = m_env->getLocalPlayer();
1020 int old_anim = player->last_animation;
1021 float old_anim_speed = player->last_animation_speed;
1022 m_position = player->getPosition() + v3f(0,BS,0);
1023 m_velocity = v3f(0,0,0);
1024 m_acceleration = v3f(0,0,0);
1025 pos_translator.vect_show = m_position;
1026 m_yaw = player->getYaw();
1027 PlayerControl controls = player->getPlayerControl();
1029 bool walking = false;
1030 if(controls.up || controls.down || controls.left || controls.right)
1033 f32 new_speed = player->local_animation_speed;
1034 v2s32 new_anim = v2s32(0,0);
1035 bool allow_update = false;
1037 // increase speed if using fast or flying fast
1038 if((g_settings->getBool("fast_move") &&
1039 m_gamedef->checkLocalPrivilege("fast")) &&
1041 (!player->touching_ground &&
1042 g_settings->getBool("free_move") &&
1043 m_gamedef->checkLocalPrivilege("fly"))))
1045 // slowdown speed if sneeking
1046 if(controls.sneak && walking)
1049 if(walking && (controls.LMB || controls.RMB))
1051 new_anim = player->local_animations[3];
1052 player->last_animation = WD_ANIM;
1053 } else if(walking) {
1054 new_anim = player->local_animations[1];
1055 player->last_animation = WALK_ANIM;
1056 } else if(controls.LMB || controls.RMB) {
1057 new_anim = player->local_animations[2];
1058 player->last_animation = DIG_ANIM;
1061 // Apply animations if input detected and not attached
1062 // or set idle animation
1063 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1065 allow_update = true;
1066 m_animation_range = new_anim;
1067 m_animation_speed = new_speed;
1068 player->last_animation_speed = m_animation_speed;
1070 player->last_animation = NO_ANIM;
1072 if (old_anim != NO_ANIM)
1074 m_animation_range = player->local_animations[0];
1079 // Update local player animations
1080 if ((player->last_animation != old_anim ||
1081 m_animation_speed != old_anim_speed) &&
1082 player->last_animation != NO_ANIM && allow_update)
1088 if(m_visuals_expired && m_smgr && m_irr){
1089 m_visuals_expired = false;
1091 // Attachments, part 1: All attached objects must be unparented first,
1092 // or Irrlicht causes a segmentation fault
1093 for(std::vector<u16>::iterator ci = m_children.begin();
1094 ci != m_children.end();)
1096 if (m_env->m_attachements[*ci] != getId()) {
1097 ci = m_children.erase(ci);
1100 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1103 scene::IMeshSceneNode *m_child_meshnode
1104 = obj->getMeshSceneNode();
1105 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode
1106 = obj->getAnimatedMeshSceneNode();
1107 scene::IBillboardSceneNode *m_child_spritenode
1108 = obj->getSpriteSceneNode();
1109 if(m_child_meshnode)
1110 m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1111 if(m_child_animated_meshnode)
1112 m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1113 if(m_child_spritenode)
1114 m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1119 removeFromScene(false);
1120 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1122 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1123 for(std::vector<u16>::iterator ci = m_children.begin();
1124 ci != m_children.end(); ci++)
1126 // Get the object of the child
1127 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1129 obj->setAttachments();
1133 // Make sure m_is_visible is always applied
1135 m_meshnode->setVisible(m_is_visible);
1136 if(m_animated_meshnode)
1137 m_animated_meshnode->setVisible(m_is_visible);
1139 m_spritenode->setVisible(m_is_visible);
1141 m_textnode->setVisible(m_is_visible);
1143 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1145 // Set these for later
1146 m_position = getPosition();
1147 m_velocity = v3f(0,0,0);
1148 m_acceleration = v3f(0,0,0);
1149 pos_translator.vect_show = m_position;
1151 if(m_is_local_player) // Update local player attachment position
1153 LocalPlayer *player = m_env->getLocalPlayer();
1154 player->overridePosition = getParent()->getPosition();
1155 m_env->getLocalPlayer()->parent = getParent();
1158 v3f lastpos = pos_translator.vect_show;
1162 core::aabbox3d<f32> box = m_prop.collisionbox;
1165 collisionMoveResult moveresult;
1166 f32 pos_max_d = BS*0.125; // Distance per iteration
1167 v3f p_pos = m_position;
1168 v3f p_velocity = m_velocity;
1169 v3f p_acceleration = m_acceleration;
1170 moveresult = collisionMoveSimple(env,env->getGameDef(),
1171 pos_max_d, box, m_prop.stepheight, dtime,
1172 p_pos, p_velocity, p_acceleration,
1173 this, m_prop.collideWithObjects);
1176 m_velocity = p_velocity;
1177 m_acceleration = p_acceleration;
1179 bool is_end_position = moveresult.collides;
1180 pos_translator.update(m_position, is_end_position, dtime);
1181 pos_translator.translate(dtime);
1184 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1185 m_velocity += dtime * m_acceleration;
1186 pos_translator.update(m_position, pos_translator.aim_is_end,
1187 pos_translator.anim_time);
1188 pos_translator.translate(dtime);
1192 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1193 m_step_distance_counter += moved;
1194 if(m_step_distance_counter > 1.5*BS)
1196 m_step_distance_counter = 0;
1197 if(!m_is_local_player && m_prop.makes_footstep_sound)
1199 INodeDefManager *ndef = m_gamedef->ndef();
1200 v3s16 p = floatToInt(getPosition() + v3f(0,
1201 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1202 MapNode n = m_env->getMap().getNodeNoEx(p);
1203 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1204 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1209 m_anim_timer += dtime;
1210 if(m_anim_timer >= m_anim_framelength)
1212 m_anim_timer -= m_anim_framelength;
1214 if(m_anim_frame >= m_anim_num_frames)
1220 if(m_reset_textures_timer >= 0)
1222 m_reset_textures_timer -= dtime;
1223 if(m_reset_textures_timer <= 0){
1224 m_reset_textures_timer = -1;
1228 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1230 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1234 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1235 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1237 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1238 + m_prop.automatic_face_movement_dir_offset;
1243 void GenericCAO::updateTexturePos()
1247 scene::ICameraSceneNode* camera =
1248 m_spritenode->getSceneManager()->getActiveCamera();
1251 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1252 - camera->getAbsolutePosition();
1253 cam_to_entity.normalize();
1255 int row = m_tx_basepos.Y;
1256 int col = m_tx_basepos.X;
1258 if(m_tx_select_horiz_by_yawpitch)
1260 if(cam_to_entity.Y > 0.75)
1262 else if(cam_to_entity.Y < -0.75)
1266 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1267 float dir = mob_dir - m_yaw;
1268 dir = wrapDegrees_180(dir);
1269 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1270 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1272 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1274 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1276 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1283 // Animation goes downwards
1284 row += m_anim_frame;
1286 float txs = m_tx_size.X;
1287 float tys = m_tx_size.Y;
1288 setBillboardTextureMatrix(m_spritenode,
1289 txs, tys, col, row);
1293 void GenericCAO::updateTextures(const std::string &mod)
1295 ITextureSource *tsrc = m_gamedef->tsrc();
1297 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1298 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1299 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1303 if(m_prop.visual == "sprite")
1305 std::string texturestring = "unknown_node.png";
1306 if(m_prop.textures.size() >= 1)
1307 texturestring = m_prop.textures[0];
1308 texturestring += mod;
1309 m_spritenode->setMaterialTexture(0,
1310 tsrc->getTexture(texturestring));
1312 // This allows setting per-material colors. However, until a real lighting
1313 // system is added, the code below will have no effect. Once MineTest
1314 // has directional lighting, it should work automatically.
1315 if(m_prop.colors.size() >= 1)
1317 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1318 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1319 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1322 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1323 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1324 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1327 if(m_animated_meshnode)
1329 if(m_prop.visual == "mesh")
1331 for (u32 i = 0; i < m_prop.textures.size() &&
1332 i < m_animated_meshnode->getMaterialCount(); ++i)
1334 std::string texturestring = m_prop.textures[i];
1335 if(texturestring == "")
1336 continue; // Empty texture string means don't modify that material
1337 texturestring += mod;
1338 video::ITexture* texture = tsrc->getTexture(texturestring);
1341 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1345 // Set material flags and texture
1346 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1347 material.TextureLayer[0].Texture = texture;
1348 material.setFlag(video::EMF_LIGHTING, false);
1349 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1351 m_animated_meshnode->getMaterial(i)
1352 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1353 m_animated_meshnode->getMaterial(i)
1354 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1355 m_animated_meshnode->getMaterial(i)
1356 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1358 for (u32 i = 0; i < m_prop.colors.size() &&
1359 i < m_animated_meshnode->getMaterialCount(); ++i)
1361 // This allows setting per-material colors. However, until a real lighting
1362 // system is added, the code below will have no effect. Once MineTest
1363 // has directional lighting, it should work automatically.
1364 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1365 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1366 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1372 if(m_prop.visual == "cube")
1374 for (u32 i = 0; i < 6; ++i)
1376 std::string texturestring = "unknown_node.png";
1377 if(m_prop.textures.size() > i)
1378 texturestring = m_prop.textures[i];
1379 texturestring += mod;
1382 // Set material flags and texture
1383 video::SMaterial& material = m_meshnode->getMaterial(i);
1384 material.setFlag(video::EMF_LIGHTING, false);
1385 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1386 material.setTexture(0,
1387 tsrc->getTexture(texturestring));
1388 material.getTextureMatrix(0).makeIdentity();
1390 // This allows setting per-material colors. However, until a real lighting
1391 // system is added, the code below will have no effect. Once MineTest
1392 // has directional lighting, it should work automatically.
1393 if(m_prop.colors.size() > i)
1395 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1396 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1397 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1400 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1401 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1402 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1405 else if(m_prop.visual == "upright_sprite")
1407 scene::IMesh *mesh = m_meshnode->getMesh();
1409 std::string tname = "unknown_object.png";
1410 if(m_prop.textures.size() >= 1)
1411 tname = m_prop.textures[0];
1413 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1414 buf->getMaterial().setTexture(0,
1415 tsrc->getTexture(tname));
1417 // This allows setting per-material colors. However, until a real lighting
1418 // system is added, the code below will have no effect. Once MineTest
1419 // has directional lighting, it should work automatically.
1420 if(m_prop.colors.size() >= 1)
1422 buf->getMaterial().AmbientColor = m_prop.colors[0];
1423 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1424 buf->getMaterial().SpecularColor = m_prop.colors[0];
1427 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1428 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1429 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1432 std::string tname = "unknown_object.png";
1433 if(m_prop.textures.size() >= 2)
1434 tname = m_prop.textures[1];
1435 else if(m_prop.textures.size() >= 1)
1436 tname = m_prop.textures[0];
1438 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1439 buf->getMaterial().setTexture(0,
1440 tsrc->getTexture(tname));
1442 // This allows setting per-material colors. However, until a real lighting
1443 // system is added, the code below will have no effect. Once MineTest
1444 // has directional lighting, it should work automatically.
1445 if(m_prop.colors.size() >= 2)
1447 buf->getMaterial().AmbientColor = m_prop.colors[1];
1448 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1449 buf->getMaterial().SpecularColor = m_prop.colors[1];
1451 else if(m_prop.colors.size() >= 1)
1453 buf->getMaterial().AmbientColor = m_prop.colors[0];
1454 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1455 buf->getMaterial().SpecularColor = m_prop.colors[0];
1458 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1459 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1460 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1466 void GenericCAO::updateAnimation()
1468 if(m_animated_meshnode == NULL)
1470 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1471 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1472 m_animated_meshnode->setTransitionTime(m_animation_blend);
1475 void GenericCAO::updateBonePosition()
1477 if(!m_bone_position.size() || m_animated_meshnode == NULL)
1480 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1481 for(std::map<std::string,
1482 core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1483 ii != m_bone_position.end(); ++ii)
1485 std::string bone_name = (*ii).first;
1486 v3f bone_pos = (*ii).second.X;
1487 v3f bone_rot = (*ii).second.Y;
1488 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1491 bone->setPosition(bone_pos);
1492 bone->setRotation(bone_rot);
1497 void GenericCAO::updateAttachments()
1500 // localplayer itself can't be attached to localplayer
1501 if (!m_is_local_player)
1503 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1504 // Objects attached to the local player should always be hidden
1505 m_is_visible = !m_attached_to_local;
1508 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1512 v3f old_position = m_meshnode->getAbsolutePosition();
1513 v3f old_rotation = m_meshnode->getRotation();
1514 m_meshnode->setParent(m_smgr->getRootSceneNode());
1515 m_meshnode->setPosition(old_position);
1516 m_meshnode->setRotation(old_rotation);
1517 m_meshnode->updateAbsolutePosition();
1519 if(m_animated_meshnode)
1521 v3f old_position = m_animated_meshnode->getAbsolutePosition();
1522 v3f old_rotation = m_animated_meshnode->getRotation();
1523 m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1524 m_animated_meshnode->setPosition(old_position);
1525 m_animated_meshnode->setRotation(old_rotation);
1526 m_animated_meshnode->updateAbsolutePosition();
1530 v3f old_position = m_spritenode->getAbsolutePosition();
1531 v3f old_rotation = m_spritenode->getRotation();
1532 m_spritenode->setParent(m_smgr->getRootSceneNode());
1533 m_spritenode->setPosition(old_position);
1534 m_spritenode->setRotation(old_rotation);
1535 m_spritenode->updateAbsolutePosition();
1537 if(m_is_local_player)
1539 LocalPlayer *player = m_env->getLocalPlayer();
1540 player->isAttached = false;
1545 scene::IMeshSceneNode *parent_mesh = NULL;
1546 if(getParent()->getMeshSceneNode())
1547 parent_mesh = getParent()->getMeshSceneNode();
1548 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1549 if(getParent()->getAnimatedMeshSceneNode())
1550 parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1551 scene::IBillboardSceneNode *parent_sprite = NULL;
1552 if(getParent()->getSpriteSceneNode())
1553 parent_sprite = getParent()->getSpriteSceneNode();
1555 scene::IBoneSceneNode *parent_bone = NULL;
1556 if(parent_animated_mesh && m_attachment_bone != "")
1559 parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1561 // The spaghetti code below makes sure attaching works if either the
1562 // parent or child is a spritenode, meshnode, or animatedmeshnode
1563 // TODO: Perhaps use polymorphism here to save code duplication
1568 m_meshnode->setParent(parent_bone);
1569 m_meshnode->setPosition(m_attachment_position);
1570 m_meshnode->setRotation(m_attachment_rotation);
1571 m_meshnode->updateAbsolutePosition();
1577 m_meshnode->setParent(parent_mesh);
1578 m_meshnode->setPosition(m_attachment_position);
1579 m_meshnode->setRotation(m_attachment_rotation);
1580 m_meshnode->updateAbsolutePosition();
1582 else if(parent_animated_mesh) {
1583 m_meshnode->setParent(parent_animated_mesh);
1584 m_meshnode->setPosition(m_attachment_position);
1585 m_meshnode->setRotation(m_attachment_rotation);
1586 m_meshnode->updateAbsolutePosition();
1588 else if(parent_sprite) {
1589 m_meshnode->setParent(parent_sprite);
1590 m_meshnode->setPosition(m_attachment_position);
1591 m_meshnode->setRotation(m_attachment_rotation);
1592 m_meshnode->updateAbsolutePosition();
1596 if(m_animated_meshnode)
1600 m_animated_meshnode->setParent(parent_bone);
1601 m_animated_meshnode->setPosition(m_attachment_position);
1602 m_animated_meshnode->setRotation(m_attachment_rotation);
1603 m_animated_meshnode->updateAbsolutePosition();
1609 m_animated_meshnode->setParent(parent_mesh);
1610 m_animated_meshnode->setPosition(m_attachment_position);
1611 m_animated_meshnode->setRotation(m_attachment_rotation);
1612 m_animated_meshnode->updateAbsolutePosition();
1613 } else if(parent_animated_mesh) {
1614 m_animated_meshnode->setParent(parent_animated_mesh);
1615 m_animated_meshnode->setPosition(m_attachment_position);
1616 m_animated_meshnode->setRotation(m_attachment_rotation);
1617 m_animated_meshnode->updateAbsolutePosition();
1618 } else if(parent_sprite) {
1619 m_animated_meshnode->setParent(parent_sprite);
1620 m_animated_meshnode->setPosition(m_attachment_position);
1621 m_animated_meshnode->setRotation(m_attachment_rotation);
1622 m_animated_meshnode->updateAbsolutePosition();
1630 m_spritenode->setParent(parent_bone);
1631 m_spritenode->setPosition(m_attachment_position);
1632 m_spritenode->setRotation(m_attachment_rotation);
1633 m_spritenode->updateAbsolutePosition();
1637 m_spritenode->setParent(parent_mesh);
1638 m_spritenode->setPosition(m_attachment_position);
1639 m_spritenode->setRotation(m_attachment_rotation);
1640 m_spritenode->updateAbsolutePosition();
1642 else if(parent_animated_mesh) {
1643 m_spritenode->setParent(parent_animated_mesh);
1644 m_spritenode->setPosition(m_attachment_position);
1645 m_spritenode->setRotation(m_attachment_rotation);
1646 m_spritenode->updateAbsolutePosition();
1648 else if(parent_sprite) {
1649 m_spritenode->setParent(parent_sprite);
1650 m_spritenode->setPosition(m_attachment_position);
1651 m_spritenode->setRotation(m_attachment_rotation);
1652 m_spritenode->updateAbsolutePosition();
1656 if(m_is_local_player)
1658 LocalPlayer *player = m_env->getLocalPlayer();
1659 player->isAttached = true;
1664 void GenericCAO::processMessage(const std::string &data)
1666 //infostream<<"GenericCAO: Got message"<<std::endl;
1667 std::istringstream is(data, std::ios::binary);
1669 u8 cmd = readU8(is);
1670 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1672 m_prop = gob_read_set_properties(is);
1674 m_selection_box = m_prop.collisionbox;
1675 m_selection_box.MinEdge *= BS;
1676 m_selection_box.MaxEdge *= BS;
1678 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1679 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1681 if(!m_initial_tx_basepos_set){
1682 m_initial_tx_basepos_set = true;
1683 m_tx_basepos = m_prop.initial_sprite_basepos;
1688 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1690 // Not sent by the server if this object is an attachment.
1691 // We might however get here if the server notices the object being detached before the client.
1692 m_position = readV3F1000(is);
1693 m_velocity = readV3F1000(is);
1694 m_acceleration = readV3F1000(is);
1695 if(fabs(m_prop.automatic_rotate) < 0.001)
1696 m_yaw = readF1000(is);
1699 bool do_interpolate = readU8(is);
1700 bool is_end_position = readU8(is);
1701 float update_interval = readF1000(is);
1703 // Place us a bit higher if we're physical, to not sink into
1704 // the ground due to sucky collision detection...
1706 m_position += v3f(0,0.002,0);
1708 if(getParent() != NULL) // Just in case
1713 if(!m_prop.physical)
1714 pos_translator.update(m_position, is_end_position, update_interval);
1716 pos_translator.init(m_position);
1720 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1721 std::string mod = deSerializeString(is);
1722 updateTextures(mod);
1724 else if(cmd == GENERIC_CMD_SET_SPRITE) {
1725 v2s16 p = readV2S16(is);
1726 int num_frames = readU16(is);
1727 float framelength = readF1000(is);
1728 bool select_horiz_by_yawpitch = readU8(is);
1731 m_anim_num_frames = num_frames;
1732 m_anim_framelength = framelength;
1733 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1737 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1738 float override_speed = readF1000(is);
1739 float override_jump = readF1000(is);
1740 float override_gravity = readF1000(is);
1741 // these are sent inverted so we get true when the server sends nothing
1742 bool sneak = !readU8(is);
1743 bool sneak_glitch = !readU8(is);
1746 if(m_is_local_player)
1748 LocalPlayer *player = m_env->getLocalPlayer();
1749 player->physics_override_speed = override_speed;
1750 player->physics_override_jump = override_jump;
1751 player->physics_override_gravity = override_gravity;
1752 player->physics_override_sneak = sneak;
1753 player->physics_override_sneak_glitch = sneak_glitch;
1756 else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1757 // TODO: change frames send as v2s32 value
1758 v2f range = readV2F1000(is);
1759 if (!m_is_local_player) {
1760 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1761 m_animation_speed = readF1000(is);
1762 m_animation_blend = readF1000(is);
1765 LocalPlayer *player = m_env->getLocalPlayer();
1766 if(player->last_animation == NO_ANIM)
1768 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1769 m_animation_speed = readF1000(is);
1770 m_animation_blend = readF1000(is);
1772 // update animation only if local animations present
1773 // and received animation is unknown (except idle animation)
1774 bool is_known = false;
1775 for (int i = 1;i<4;i++)
1777 if(m_animation_range.Y == player->local_animations[i].Y)
1781 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1787 else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1788 std::string bone = deSerializeString(is);
1789 v3f position = readV3F1000(is);
1790 v3f rotation = readV3F1000(is);
1791 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1793 updateBonePosition();
1795 else if(cmd == GENERIC_CMD_SET_ATTACHMENT) {
1796 m_env->m_attachements[getId()] = readS16(is);
1797 m_children.push_back(m_env->m_attachements[getId()]);
1798 m_attachment_bone = deSerializeString(is);
1799 m_attachment_position = readV3F1000(is);
1800 m_attachment_rotation = readV3F1000(is);
1802 updateAttachments();
1804 else if(cmd == GENERIC_CMD_PUNCHED) {
1805 /*s16 damage =*/ readS16(is);
1806 s16 result_hp = readS16(is);
1808 // Use this instead of the send damage to not interfere with prediction
1809 s16 damage = m_hp - result_hp;
1817 // TODO: Execute defined fast response
1818 // As there is no definition, make a smoke puff
1819 ClientSimpleObject *simple = createSmokePuff(
1820 m_smgr, m_env, m_position,
1821 m_prop.visual_size * BS);
1822 m_env->addSimpleObject(simple);
1824 // TODO: Execute defined fast response
1825 // Flashing shall suffice as there is no definition
1826 m_reset_textures_timer = 0.05;
1828 m_reset_textures_timer += 0.05 * damage;
1829 updateTextures("^[brighten");
1833 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1834 m_armor_groups.clear();
1835 int armor_groups_size = readU16(is);
1836 for(int i=0; i<armor_groups_size; i++)
1838 std::string name = deSerializeString(is);
1839 int rating = readS16(is);
1840 m_armor_groups[name] = rating;
1845 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1846 float time_from_last_punch)
1849 const ToolCapabilities *toolcap =
1850 &punchitem->getToolCapabilities(m_gamedef->idef());
1851 PunchDamageResult result = getPunchDamage(
1855 time_from_last_punch);
1857 if(result.did_punch && result.damage != 0)
1859 if(result.damage < m_hp)
1861 m_hp -= result.damage;
1864 // TODO: Execute defined fast response
1865 // As there is no definition, make a smoke puff
1866 ClientSimpleObject *simple = createSmokePuff(
1867 m_smgr, m_env, m_position,
1868 m_prop.visual_size * BS);
1869 m_env->addSimpleObject(simple);
1871 // TODO: Execute defined fast response
1872 // Flashing shall suffice as there is no definition
1873 m_reset_textures_timer = 0.05;
1874 if(result.damage >= 2)
1875 m_reset_textures_timer += 0.05 * result.damage;
1876 updateTextures("^[brighten");
1882 std::string GenericCAO::debugInfoText()
1884 std::ostringstream os(std::ios::binary);
1885 os<<"GenericCAO hp="<<m_hp<<"\n";
1887 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1888 i != m_armor_groups.end(); i++)
1890 os<<i->first<<"="<<i->second<<", ";
1897 GenericCAO proto_GenericCAO(NULL, NULL);