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 <ICameraSceneNode.h>
21 #include <ITextSceneNode.h>
22 #include <IBillboardSceneNode.h>
23 #include <IMeshManipulator.h>
24 #include <IAnimatedMeshSceneNode.h>
25 #include <IBoneSceneNode.h>
26 #include "content_cao.h"
27 #include "util/numeric.h" // For IntervalLimiter
28 #include "util/serialize.h"
29 #include "util/mathconstants.h"
31 #include "environment.h"
32 #include "collision.h"
34 #include "serialization.h" // For decompressZlib
36 #include "clientobject.h"
37 #include "content_object.h"
41 #include "content_cso.h"
44 #include "localplayer.h"
46 #include "main.h" // g_settings
47 #include "camera.h" // CameraModes
51 struct ToolCapabilities;
53 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
55 std::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
57 SmoothTranslator::SmoothTranslator():
67 void SmoothTranslator::init(v3f vect)
74 anim_time_counter = 0;
78 void SmoothTranslator::sharpen()
83 void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
85 aim_is_end = is_end_position;
88 if(update_interval > 0)
90 anim_time = update_interval;
92 if(anim_time < 0.001 || anim_time > 1.0)
93 anim_time = anim_time_counter;
95 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
97 anim_time_counter = 0;
101 void SmoothTranslator::translate(f32 dtime)
103 anim_time_counter = anim_time_counter + dtime;
104 anim_counter = anim_counter + dtime;
105 v3f vect_move = vect_aim - vect_old;
107 if(anim_time > 0.001)
108 moveratio = anim_time_counter / anim_time;
109 // Move a bit less than should, to avoid oscillation
110 moveratio = moveratio * 0.8;
111 float move_end = 1.5;
114 if(moveratio > move_end)
115 moveratio = move_end;
116 vect_show = vect_old + vect_move * moveratio;
119 bool SmoothTranslator::is_moving()
121 return ((anim_time_counter / anim_time) < 1.4);
128 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
129 float txs, float tys, int col, int row)
131 video::SMaterial& material = bill->getMaterial(0);
132 core::matrix4& matrix = material.getTextureMatrix(0);
133 matrix.setTextureTranslate(txs*col, tys*row);
134 matrix.setTextureScale(txs, tys);
141 class TestCAO : public ClientActiveObject
144 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
149 return ACTIVEOBJECT_TYPE_TEST;
152 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
154 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
155 IrrlichtDevice *irr);
156 void removeFromScene(bool permanent);
157 void updateLight(u8 light_at_pos);
158 v3s16 getLightPosition();
159 void updateNodePos();
161 void step(float dtime, ClientEnvironment *env);
163 void processMessage(const std::string &data);
165 bool getCollisionBox(aabb3f *toset) { return false; }
167 scene::IMeshSceneNode *m_node;
172 TestCAO proto_TestCAO(NULL, NULL);
174 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
175 ClientActiveObject(0, gamedef, env),
177 m_position(v3f(0,10*BS,0))
179 ClientActiveObject::registerType(getType(), create);
186 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
188 return new TestCAO(gamedef, env);
191 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
197 //video::IVideoDriver* driver = smgr->getVideoDriver();
199 scene::SMesh *mesh = new scene::SMesh();
200 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
201 video::SColor c(255,255,255,255);
202 video::S3DVertex vertices[4] =
204 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
205 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
206 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
207 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
209 u16 indices[] = {0,1,2,2,3,0};
210 buf->append(vertices, 4, indices, 6);
212 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
213 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
214 buf->getMaterial().setTexture(0, tsrc->getTexture("rat.png"));
215 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
216 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
217 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
219 mesh->addMeshBuffer(buf);
221 m_node = smgr->addMeshSceneNode(mesh, NULL);
226 void TestCAO::removeFromScene(bool permanent)
235 void TestCAO::updateLight(u8 light_at_pos)
239 v3s16 TestCAO::getLightPosition()
241 return floatToInt(m_position, BS);
244 void TestCAO::updateNodePos()
249 m_node->setPosition(m_position);
250 //m_node->setRotation(v3f(0, 45, 0));
253 void TestCAO::step(float dtime, ClientEnvironment *env)
257 v3f rot = m_node->getRotation();
258 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
259 rot.Y += dtime * 180;
260 m_node->setRotation(rot);
264 void TestCAO::processMessage(const std::string &data)
266 infostream<<"TestCAO: Got data: "<<data<<std::endl;
267 std::istringstream is(data, std::ios::binary);
285 class ItemCAO : public ClientActiveObject
288 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
293 return ACTIVEOBJECT_TYPE_ITEM;
296 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
298 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
299 IrrlichtDevice *irr);
300 void removeFromScene(bool permanent);
301 void updateLight(u8 light_at_pos);
302 v3s16 getLightPosition();
303 void updateNodePos();
304 void updateInfoText();
305 void updateTexture();
307 void step(float dtime, ClientEnvironment *env);
309 void processMessage(const std::string &data);
311 void initialize(const std::string &data);
313 core::aabbox3d<f32>* getSelectionBox()
314 {return &m_selection_box;}
318 std::string infoText()
321 bool getCollisionBox(aabb3f *toset) { return false; }
323 core::aabbox3d<f32> m_selection_box;
324 scene::IMeshSceneNode *m_node;
326 std::string m_itemstring;
327 std::string m_infotext;
330 #include "inventory.h"
333 ItemCAO proto_ItemCAO(NULL, NULL);
335 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
336 ClientActiveObject(0, gamedef, env),
337 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
339 m_position(v3f(0,10*BS,0))
343 ClientActiveObject::registerType(getType(), create);
351 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
353 return new ItemCAO(gamedef, env);
356 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
362 //video::IVideoDriver* driver = smgr->getVideoDriver();
364 scene::SMesh *mesh = new scene::SMesh();
365 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
366 video::SColor c(255,255,255,255);
367 video::S3DVertex vertices[4] =
369 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
370 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
371 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
372 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
373 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
374 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
375 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
376 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
378 u16 indices[] = {0,1,2,2,3,0};
379 buf->append(vertices, 4, indices, 6);
381 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
382 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
383 // Initialize with a generated placeholder texture
384 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
385 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
386 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
387 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
389 mesh->addMeshBuffer(buf);
391 m_node = smgr->addMeshSceneNode(mesh, NULL);
402 void ItemCAO::removeFromScene(bool permanent)
411 void ItemCAO::updateLight(u8 light_at_pos)
416 u8 li = decode_light(light_at_pos);
417 video::SColor color(255,li,li,li);
418 setMeshColor(m_node->getMesh(), color);
421 v3s16 ItemCAO::getLightPosition()
423 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
426 void ItemCAO::updateNodePos()
431 m_node->setPosition(m_position);
434 void ItemCAO::updateInfoText()
437 IItemDefManager *idef = m_gamedef->idef();
439 item.deSerialize(m_itemstring, idef);
440 if(item.isKnown(idef))
441 m_infotext = item.getDefinition(idef).description;
443 m_infotext = "Unknown item: '" + m_itemstring + "'";
445 m_infotext += " (" + itos(item.count) + ")";
447 catch(SerializationError &e)
449 m_infotext = "Unknown item: '" + m_itemstring + "'";
453 void ItemCAO::updateTexture()
458 // Create an inventory item to see what is its image
459 std::istringstream is(m_itemstring, std::ios_base::binary);
460 video::ITexture *texture = NULL;
462 IItemDefManager *idef = m_gamedef->idef();
464 item.deSerialize(is, idef);
465 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
467 catch(SerializationError &e)
469 infostream<<"WARNING: "<<__FUNCTION_NAME
470 <<": error deSerializing itemstring \""
471 <<m_itemstring<<std::endl;
474 // Set meshbuffer texture
475 m_node->getMaterial(0).setTexture(0, texture);
479 void ItemCAO::step(float dtime, ClientEnvironment *env)
483 /*v3f rot = m_node->getRotation();
484 rot.Y += dtime * 120;
485 m_node->setRotation(rot);*/
486 LocalPlayer *player = env->getLocalPlayer();
488 v3f rot = m_node->getRotation();
489 rot.Y = 180.0 - (player->getYaw());
490 m_node->setRotation(rot);
494 void ItemCAO::processMessage(const std::string &data)
496 //infostream<<"ItemCAO: Got message"<<std::endl;
497 std::istringstream is(data, std::ios::binary);
503 m_position = readV3F1000(is);
509 m_itemstring = deSerializeString(is);
515 void ItemCAO::initialize(const std::string &data)
517 infostream<<"ItemCAO: Got init data"<<std::endl;
520 std::istringstream is(data, std::ios::binary);
522 u8 version = readU8(is);
527 m_position = readV3F1000(is);
529 m_itemstring = deSerializeString(is);
540 #include "genericobject.h"
542 GenericCAO::GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
543 ClientActiveObject(0, gamedef, env),
546 m_is_local_player(false),
551 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
553 m_animated_meshnode(NULL),
556 m_position(v3f(0,10*BS,0)),
557 m_velocity(v3f(0,0,0)),
558 m_acceleration(v3f(0,0,0)),
563 m_initial_tx_basepos_set(false),
564 m_tx_select_horiz_by_yawpitch(false),
565 m_animation_range(v2s32(0,0)),
566 m_animation_speed(15),
567 m_animation_blend(0),
568 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
569 m_attachment_bone(""),
570 m_attachment_position(v3f(0,0,0)),
571 m_attachment_rotation(v3f(0,0,0)),
572 m_attached_to_local(false),
574 m_anim_num_frames(1),
575 m_anim_framelength(0.2),
577 m_reset_textures_timer(-1),
578 m_visuals_expired(false),
579 m_step_distance_counter(0),
584 ClientActiveObject::registerType(getType(), create);
587 bool GenericCAO::getCollisionBox(aabb3f *toset)
591 //update collision box
592 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
593 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
595 toset->MinEdge += m_position;
596 toset->MaxEdge += m_position;
604 bool GenericCAO::collideWithObjects()
606 return m_prop.collideWithObjects;
609 void GenericCAO::initialize(const std::string &data)
611 infostream<<"GenericCAO: Got init data"<<std::endl;
612 std::istringstream is(data, std::ios::binary);
613 int num_messages = 0;
615 u8 version = readU8(is);
617 if(version == 1) // In PROTOCOL_VERSION 14
619 m_name = deSerializeString(is);
620 m_is_player = readU8(is);
622 m_position = readV3F1000(is);
623 m_yaw = readF1000(is);
625 num_messages = readU8(is);
627 else if(version == 0) // In PROTOCOL_VERSION 13
629 m_name = deSerializeString(is);
630 m_is_player = readU8(is);
631 m_position = readV3F1000(is);
632 m_yaw = readF1000(is);
634 num_messages = readU8(is);
638 errorstream<<"GenericCAO: Unsupported init data version"
643 for(int i=0; i<num_messages; i++)
645 std::string message = deSerializeLongString(is);
646 processMessage(message);
649 pos_translator.init(m_position);
654 Player *player = m_env->getPlayer(m_name.c_str());
655 if(player && player->isLocal())
657 m_is_local_player = true;
658 m_is_visible = false;
659 LocalPlayer* localplayer = dynamic_cast<LocalPlayer*>(player);
661 assert( localplayer != NULL );
662 localplayer->setCAO(this);
664 m_env->addPlayerName(m_name.c_str());
668 GenericCAO::~GenericCAO()
672 m_env->removePlayerName(m_name.c_str());
674 removeFromScene(true);
677 core::aabbox3d<f32>* GenericCAO::getSelectionBox()
679 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
681 return &m_selection_box;
684 v3f GenericCAO::getPosition()
686 if(getParent() != NULL)
689 return m_meshnode->getAbsolutePosition();
690 if(m_animated_meshnode)
691 return m_animated_meshnode->getAbsolutePosition();
693 return m_spritenode->getAbsolutePosition();
696 return pos_translator.vect_show;
699 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
706 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
708 if(m_animated_meshnode)
709 return m_animated_meshnode;
713 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
720 void GenericCAO::setAttachments()
725 ClientActiveObject* GenericCAO::getParent()
727 ClientActiveObject *obj = NULL;
729 u16 attached_id = m_env->m_attachements[getId()];
731 if ((attached_id != 0) &&
732 (attached_id != getId())) {
733 obj = m_env->getActiveObject(attached_id);
738 void GenericCAO::removeFromScene(bool permanent)
740 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
741 if((m_env != NULL) && (permanent))
743 for(std::vector<u16>::iterator ci = m_children.begin();
744 ci != m_children.end(); ci++)
746 if (m_env->m_attachements[*ci] == getId()) {
747 m_env->m_attachements[*ci] = 0;
751 m_env->m_attachements[getId()] = 0;
753 LocalPlayer* player = m_env->getLocalPlayer();
754 if (this == player->parent) {
755 player->parent = NULL;
756 player->isAttached = false;
762 m_meshnode->remove();
766 if(m_animated_meshnode)
768 m_animated_meshnode->remove();
769 m_animated_meshnode->drop();
770 m_animated_meshnode = NULL;
774 m_spritenode->remove();
775 m_spritenode->drop();
780 m_textnode->remove();
786 void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
792 if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
795 m_visuals_expired = false;
797 if(!m_prop.is_visible)
800 //video::IVideoDriver* driver = smgr->getVideoDriver();
802 if(m_prop.visual == "sprite")
804 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
805 m_spritenode = smgr->addBillboardSceneNode(
806 NULL, v2f(1, 1), v3f(0,0,0), -1);
807 m_spritenode->grab();
808 m_spritenode->setMaterialTexture(0,
809 tsrc->getTexture("unknown_node.png"));
810 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
811 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
812 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
813 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
814 u8 li = m_last_light;
815 m_spritenode->setColor(video::SColor(255,li,li,li));
816 m_spritenode->setSize(m_prop.visual_size*BS);
818 const float txs = 1.0 / 1;
819 const float tys = 1.0 / 1;
820 setBillboardTextureMatrix(m_spritenode,
824 else if(m_prop.visual == "upright_sprite") {
825 scene::SMesh *mesh = new scene::SMesh();
826 double dx = BS*m_prop.visual_size.X/2;
827 double dy = BS*m_prop.visual_size.Y/2;
829 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
830 u8 li = m_last_light;
831 video::SColor c(255,li,li,li);
832 video::S3DVertex vertices[4] =
834 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
835 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
836 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
837 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
839 u16 indices[] = {0,1,2,2,3,0};
840 buf->append(vertices, 4, indices, 6);
842 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
843 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
844 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
845 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
847 mesh->addMeshBuffer(buf);
851 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
852 u8 li = m_last_light;
853 video::SColor c(255,li,li,li);
854 video::S3DVertex vertices[4] =
856 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
857 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
858 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
859 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
861 u16 indices[] = {0,1,2,2,3,0};
862 buf->append(vertices, 4, indices, 6);
864 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
865 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
866 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
867 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
869 mesh->addMeshBuffer(buf);
872 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
875 // Set it to use the materials of the meshbuffers directly.
876 // This is needed for changing the texture in the future
877 m_meshnode->setReadOnlyMaterials(true);
879 else if(m_prop.visual == "cube") {
880 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
881 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
882 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
886 m_meshnode->setScale(v3f(m_prop.visual_size.X,
887 m_prop.visual_size.Y,
888 m_prop.visual_size.X));
889 u8 li = m_last_light;
890 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
892 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
893 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
894 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
895 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
897 else if(m_prop.visual == "mesh") {
898 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
899 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
902 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
903 m_animated_meshnode->grab();
904 mesh->drop(); // The scene node took hold of it
905 m_animated_meshnode->animateJoints(); // Needed for some animations
906 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
907 m_prop.visual_size.Y,
908 m_prop.visual_size.X));
909 u8 li = m_last_light;
910 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
912 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
913 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
914 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
915 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
918 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
920 else if(m_prop.visual == "wielditem") {
921 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
922 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
923 if(m_prop.textures.size() >= 1){
924 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
925 IItemDefManager *idef = m_gamedef->idef();
926 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
927 scene::IMesh *item_mesh = idef->getWieldMesh(item.getDefinition(idef).name, m_gamedef);
929 // Copy mesh to be able to set unique vertex colors
930 scene::IMeshManipulator *manip =
931 irr->getVideoDriver()->getMeshManipulator();
932 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
934 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
938 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
939 m_prop.visual_size.Y/2,
940 m_prop.visual_size.X/2));
941 u8 li = m_last_light;
942 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
945 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
946 <<"\" not supported"<<std::endl;
950 scene::ISceneNode *node = NULL;
953 else if(m_animated_meshnode)
954 node = m_animated_meshnode;
957 if(node && m_is_player && !m_is_local_player){
958 // Add a text node for showing the name
959 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
960 std::wstring wname = narrow_to_wide(m_name);
961 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
962 wname.c_str(), video::SColor(255,255,255,255), node);
964 m_textnode->setPosition(v3f(0, BS*1.1, 0));
969 updateBonePosition();
973 void GenericCAO::updateLight(u8 light_at_pos)
975 u8 li = decode_light(light_at_pos);
976 if(li != m_last_light)
979 video::SColor color(255,li,li,li);
981 setMeshColor(m_meshnode->getMesh(), color);
982 if(m_animated_meshnode)
983 setMeshColor(m_animated_meshnode->getMesh(), color);
985 m_spritenode->setColor(color);
989 v3s16 GenericCAO::getLightPosition()
991 return floatToInt(m_position, BS);
994 void GenericCAO::updateNodePos()
996 if(getParent() != NULL)
999 v3s16 camera_offset = m_env->getCameraOffset();
1002 m_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
1003 v3f rot = m_meshnode->getRotation();
1005 m_meshnode->setRotation(rot);
1007 if(m_animated_meshnode)
1009 m_animated_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
1010 v3f rot = m_animated_meshnode->getRotation();
1012 m_animated_meshnode->setRotation(rot);
1016 m_spritenode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
1020 void GenericCAO::step(float dtime, ClientEnvironment *env)
1022 // Handel model of local player instantly to prevent lags
1023 if(m_is_local_player)
1025 LocalPlayer *player = m_env->getLocalPlayer();
1029 int old_anim = player->last_animation;
1030 float old_anim_speed = player->last_animation_speed;
1031 m_position = player->getPosition() + v3f(0,BS,0);
1032 m_velocity = v3f(0,0,0);
1033 m_acceleration = v3f(0,0,0);
1034 pos_translator.vect_show = m_position;
1035 m_yaw = player->getYaw();
1036 PlayerControl controls = player->getPlayerControl();
1038 bool walking = false;
1039 if(controls.up || controls.down || controls.left || controls.right)
1042 f32 new_speed = player->local_animation_speed;
1043 v2s32 new_anim = v2s32(0,0);
1044 bool allow_update = false;
1046 // increase speed if using fast or flying fast
1047 if((g_settings->getBool("fast_move") &&
1048 m_gamedef->checkLocalPrivilege("fast")) &&
1050 (!player->touching_ground &&
1051 g_settings->getBool("free_move") &&
1052 m_gamedef->checkLocalPrivilege("fly"))))
1054 // slowdown speed if sneeking
1055 if(controls.sneak && walking)
1058 if(walking && (controls.LMB || controls.RMB))
1060 new_anim = player->local_animations[3];
1061 player->last_animation = WD_ANIM;
1062 } else if(walking) {
1063 new_anim = player->local_animations[1];
1064 player->last_animation = WALK_ANIM;
1065 } else if(controls.LMB || controls.RMB) {
1066 new_anim = player->local_animations[2];
1067 player->last_animation = DIG_ANIM;
1070 // Apply animations if input detected and not attached
1071 // or set idle animation
1072 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1074 allow_update = true;
1075 m_animation_range = new_anim;
1076 m_animation_speed = new_speed;
1077 player->last_animation_speed = m_animation_speed;
1079 player->last_animation = NO_ANIM;
1081 if (old_anim != NO_ANIM)
1083 m_animation_range = player->local_animations[0];
1088 // Update local player animations
1089 if ((player->last_animation != old_anim ||
1090 m_animation_speed != old_anim_speed) &&
1091 player->last_animation != NO_ANIM && allow_update)
1097 if(m_visuals_expired && m_smgr && m_irr){
1098 m_visuals_expired = false;
1100 // Attachments, part 1: All attached objects must be unparented first,
1101 // or Irrlicht causes a segmentation fault
1102 for(std::vector<u16>::iterator ci = m_children.begin();
1103 ci != m_children.end();)
1105 if (m_env->m_attachements[*ci] != getId()) {
1106 ci = m_children.erase(ci);
1109 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1112 scene::IMeshSceneNode *m_child_meshnode
1113 = obj->getMeshSceneNode();
1114 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode
1115 = obj->getAnimatedMeshSceneNode();
1116 scene::IBillboardSceneNode *m_child_spritenode
1117 = obj->getSpriteSceneNode();
1118 if(m_child_meshnode)
1119 m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1120 if(m_child_animated_meshnode)
1121 m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1122 if(m_child_spritenode)
1123 m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1128 removeFromScene(false);
1129 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1131 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1132 for(std::vector<u16>::iterator ci = m_children.begin();
1133 ci != m_children.end(); ci++)
1135 // Get the object of the child
1136 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1138 obj->setAttachments();
1142 // Make sure m_is_visible is always applied
1144 m_meshnode->setVisible(m_is_visible);
1145 if(m_animated_meshnode)
1146 m_animated_meshnode->setVisible(m_is_visible);
1148 m_spritenode->setVisible(m_is_visible);
1150 m_textnode->setVisible(m_is_visible);
1152 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1154 // Set these for later
1155 m_position = getPosition();
1156 m_velocity = v3f(0,0,0);
1157 m_acceleration = v3f(0,0,0);
1158 pos_translator.vect_show = m_position;
1160 if(m_is_local_player) // Update local player attachment position
1162 LocalPlayer *player = m_env->getLocalPlayer();
1163 player->overridePosition = getParent()->getPosition();
1164 m_env->getLocalPlayer()->parent = getParent();
1167 v3f lastpos = pos_translator.vect_show;
1171 core::aabbox3d<f32> box = m_prop.collisionbox;
1174 collisionMoveResult moveresult;
1175 f32 pos_max_d = BS*0.125; // Distance per iteration
1176 v3f p_pos = m_position;
1177 v3f p_velocity = m_velocity;
1178 v3f p_acceleration = m_acceleration;
1179 moveresult = collisionMoveSimple(env,env->getGameDef(),
1180 pos_max_d, box, m_prop.stepheight, dtime,
1181 p_pos, p_velocity, p_acceleration,
1182 this, m_prop.collideWithObjects);
1185 m_velocity = p_velocity;
1186 m_acceleration = p_acceleration;
1188 bool is_end_position = moveresult.collides;
1189 pos_translator.update(m_position, is_end_position, dtime);
1190 pos_translator.translate(dtime);
1193 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1194 m_velocity += dtime * m_acceleration;
1195 pos_translator.update(m_position, pos_translator.aim_is_end,
1196 pos_translator.anim_time);
1197 pos_translator.translate(dtime);
1201 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1202 m_step_distance_counter += moved;
1203 if(m_step_distance_counter > 1.5*BS)
1205 m_step_distance_counter = 0;
1206 if(!m_is_local_player && m_prop.makes_footstep_sound)
1208 INodeDefManager *ndef = m_gamedef->ndef();
1209 v3s16 p = floatToInt(getPosition() + v3f(0,
1210 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1211 MapNode n = m_env->getMap().getNodeNoEx(p);
1212 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1213 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1218 m_anim_timer += dtime;
1219 if(m_anim_timer >= m_anim_framelength)
1221 m_anim_timer -= m_anim_framelength;
1223 if(m_anim_frame >= m_anim_num_frames)
1229 if(m_reset_textures_timer >= 0)
1231 m_reset_textures_timer -= dtime;
1232 if(m_reset_textures_timer <= 0){
1233 m_reset_textures_timer = -1;
1237 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1239 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1243 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1244 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1246 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1247 + m_prop.automatic_face_movement_dir_offset;
1252 void GenericCAO::updateTexturePos()
1256 scene::ICameraSceneNode* camera =
1257 m_spritenode->getSceneManager()->getActiveCamera();
1260 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1261 - camera->getAbsolutePosition();
1262 cam_to_entity.normalize();
1264 int row = m_tx_basepos.Y;
1265 int col = m_tx_basepos.X;
1267 if(m_tx_select_horiz_by_yawpitch)
1269 if(cam_to_entity.Y > 0.75)
1271 else if(cam_to_entity.Y < -0.75)
1275 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1276 float dir = mob_dir - m_yaw;
1277 dir = wrapDegrees_180(dir);
1278 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1279 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1281 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1283 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1285 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1292 // Animation goes downwards
1293 row += m_anim_frame;
1295 float txs = m_tx_size.X;
1296 float tys = m_tx_size.Y;
1297 setBillboardTextureMatrix(m_spritenode,
1298 txs, tys, col, row);
1302 void GenericCAO::updateTextures(const std::string &mod)
1304 ITextureSource *tsrc = m_gamedef->tsrc();
1306 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1307 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1308 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1312 if(m_prop.visual == "sprite")
1314 std::string texturestring = "unknown_node.png";
1315 if(m_prop.textures.size() >= 1)
1316 texturestring = m_prop.textures[0];
1317 texturestring += mod;
1318 m_spritenode->setMaterialTexture(0,
1319 tsrc->getTexture(texturestring));
1321 // This allows setting per-material colors. However, until a real lighting
1322 // system is added, the code below will have no effect. Once MineTest
1323 // has directional lighting, it should work automatically.
1324 if(m_prop.colors.size() >= 1)
1326 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1327 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1328 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1331 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1332 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1333 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1336 if(m_animated_meshnode)
1338 if(m_prop.visual == "mesh")
1340 for (u32 i = 0; i < m_prop.textures.size() &&
1341 i < m_animated_meshnode->getMaterialCount(); ++i)
1343 std::string texturestring = m_prop.textures[i];
1344 if(texturestring == "")
1345 continue; // Empty texture string means don't modify that material
1346 texturestring += mod;
1347 video::ITexture* texture = tsrc->getTexture(texturestring);
1350 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1354 // Set material flags and texture
1355 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1356 material.TextureLayer[0].Texture = texture;
1357 material.setFlag(video::EMF_LIGHTING, false);
1358 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1360 m_animated_meshnode->getMaterial(i)
1361 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1362 m_animated_meshnode->getMaterial(i)
1363 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1364 m_animated_meshnode->getMaterial(i)
1365 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1367 for (u32 i = 0; i < m_prop.colors.size() &&
1368 i < m_animated_meshnode->getMaterialCount(); ++i)
1370 // This allows setting per-material colors. However, until a real lighting
1371 // system is added, the code below will have no effect. Once MineTest
1372 // has directional lighting, it should work automatically.
1373 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1374 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1375 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1381 if(m_prop.visual == "cube")
1383 for (u32 i = 0; i < 6; ++i)
1385 std::string texturestring = "unknown_node.png";
1386 if(m_prop.textures.size() > i)
1387 texturestring = m_prop.textures[i];
1388 texturestring += mod;
1391 // Set material flags and texture
1392 video::SMaterial& material = m_meshnode->getMaterial(i);
1393 material.setFlag(video::EMF_LIGHTING, false);
1394 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1395 material.setTexture(0,
1396 tsrc->getTexture(texturestring));
1397 material.getTextureMatrix(0).makeIdentity();
1399 // This allows setting per-material colors. However, until a real lighting
1400 // system is added, the code below will have no effect. Once MineTest
1401 // has directional lighting, it should work automatically.
1402 if(m_prop.colors.size() > i)
1404 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1405 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1406 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1409 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1410 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1411 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1414 else if(m_prop.visual == "upright_sprite")
1416 scene::IMesh *mesh = m_meshnode->getMesh();
1418 std::string tname = "unknown_object.png";
1419 if(m_prop.textures.size() >= 1)
1420 tname = m_prop.textures[0];
1422 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1423 buf->getMaterial().setTexture(0,
1424 tsrc->getTexture(tname));
1426 // This allows setting per-material colors. However, until a real lighting
1427 // system is added, the code below will have no effect. Once MineTest
1428 // has directional lighting, it should work automatically.
1429 if(m_prop.colors.size() >= 1)
1431 buf->getMaterial().AmbientColor = m_prop.colors[0];
1432 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1433 buf->getMaterial().SpecularColor = m_prop.colors[0];
1436 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1437 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1438 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1441 std::string tname = "unknown_object.png";
1442 if(m_prop.textures.size() >= 2)
1443 tname = m_prop.textures[1];
1444 else if(m_prop.textures.size() >= 1)
1445 tname = m_prop.textures[0];
1447 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1448 buf->getMaterial().setTexture(0,
1449 tsrc->getTexture(tname));
1451 // This allows setting per-material colors. However, until a real lighting
1452 // system is added, the code below will have no effect. Once MineTest
1453 // has directional lighting, it should work automatically.
1454 if(m_prop.colors.size() >= 2)
1456 buf->getMaterial().AmbientColor = m_prop.colors[1];
1457 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1458 buf->getMaterial().SpecularColor = m_prop.colors[1];
1460 else if(m_prop.colors.size() >= 1)
1462 buf->getMaterial().AmbientColor = m_prop.colors[0];
1463 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1464 buf->getMaterial().SpecularColor = m_prop.colors[0];
1467 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1468 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1469 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1475 void GenericCAO::updateAnimation()
1477 if(m_animated_meshnode == NULL)
1479 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1480 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1481 m_animated_meshnode->setTransitionTime(m_animation_blend);
1484 void GenericCAO::updateBonePosition()
1486 if(!m_bone_position.size() || m_animated_meshnode == NULL)
1489 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1490 for(std::map<std::string,
1491 core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1492 ii != m_bone_position.end(); ++ii)
1494 std::string bone_name = (*ii).first;
1495 v3f bone_pos = (*ii).second.X;
1496 v3f bone_rot = (*ii).second.Y;
1497 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1500 bone->setPosition(bone_pos);
1501 bone->setRotation(bone_rot);
1506 void GenericCAO::updateAttachments()
1509 // localplayer itself can't be attached to localplayer
1510 if (!m_is_local_player)
1512 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1513 // Objects attached to the local player should always be hidden
1514 m_is_visible = !m_attached_to_local;
1517 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1521 v3f old_position = m_meshnode->getAbsolutePosition();
1522 v3f old_rotation = m_meshnode->getRotation();
1523 m_meshnode->setParent(m_smgr->getRootSceneNode());
1524 m_meshnode->setPosition(old_position);
1525 m_meshnode->setRotation(old_rotation);
1526 m_meshnode->updateAbsolutePosition();
1528 if(m_animated_meshnode)
1530 v3f old_position = m_animated_meshnode->getAbsolutePosition();
1531 v3f old_rotation = m_animated_meshnode->getRotation();
1532 m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1533 m_animated_meshnode->setPosition(old_position);
1534 m_animated_meshnode->setRotation(old_rotation);
1535 m_animated_meshnode->updateAbsolutePosition();
1539 v3f old_position = m_spritenode->getAbsolutePosition();
1540 v3f old_rotation = m_spritenode->getRotation();
1541 m_spritenode->setParent(m_smgr->getRootSceneNode());
1542 m_spritenode->setPosition(old_position);
1543 m_spritenode->setRotation(old_rotation);
1544 m_spritenode->updateAbsolutePosition();
1546 if(m_is_local_player)
1548 LocalPlayer *player = m_env->getLocalPlayer();
1549 player->isAttached = false;
1554 scene::IMeshSceneNode *parent_mesh = NULL;
1555 if(getParent()->getMeshSceneNode())
1556 parent_mesh = getParent()->getMeshSceneNode();
1557 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1558 if(getParent()->getAnimatedMeshSceneNode())
1559 parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1560 scene::IBillboardSceneNode *parent_sprite = NULL;
1561 if(getParent()->getSpriteSceneNode())
1562 parent_sprite = getParent()->getSpriteSceneNode();
1564 scene::IBoneSceneNode *parent_bone = NULL;
1565 if(parent_animated_mesh && m_attachment_bone != "")
1568 parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1570 // The spaghetti code below makes sure attaching works if either the
1571 // parent or child is a spritenode, meshnode, or animatedmeshnode
1572 // TODO: Perhaps use polymorphism here to save code duplication
1577 m_meshnode->setParent(parent_bone);
1578 m_meshnode->setPosition(m_attachment_position);
1579 m_meshnode->setRotation(m_attachment_rotation);
1580 m_meshnode->updateAbsolutePosition();
1586 m_meshnode->setParent(parent_mesh);
1587 m_meshnode->setPosition(m_attachment_position);
1588 m_meshnode->setRotation(m_attachment_rotation);
1589 m_meshnode->updateAbsolutePosition();
1591 else if(parent_animated_mesh) {
1592 m_meshnode->setParent(parent_animated_mesh);
1593 m_meshnode->setPosition(m_attachment_position);
1594 m_meshnode->setRotation(m_attachment_rotation);
1595 m_meshnode->updateAbsolutePosition();
1597 else if(parent_sprite) {
1598 m_meshnode->setParent(parent_sprite);
1599 m_meshnode->setPosition(m_attachment_position);
1600 m_meshnode->setRotation(m_attachment_rotation);
1601 m_meshnode->updateAbsolutePosition();
1605 if(m_animated_meshnode)
1609 m_animated_meshnode->setParent(parent_bone);
1610 m_animated_meshnode->setPosition(m_attachment_position);
1611 m_animated_meshnode->setRotation(m_attachment_rotation);
1612 m_animated_meshnode->updateAbsolutePosition();
1618 m_animated_meshnode->setParent(parent_mesh);
1619 m_animated_meshnode->setPosition(m_attachment_position);
1620 m_animated_meshnode->setRotation(m_attachment_rotation);
1621 m_animated_meshnode->updateAbsolutePosition();
1622 } else if(parent_animated_mesh) {
1623 m_animated_meshnode->setParent(parent_animated_mesh);
1624 m_animated_meshnode->setPosition(m_attachment_position);
1625 m_animated_meshnode->setRotation(m_attachment_rotation);
1626 m_animated_meshnode->updateAbsolutePosition();
1627 } else if(parent_sprite) {
1628 m_animated_meshnode->setParent(parent_sprite);
1629 m_animated_meshnode->setPosition(m_attachment_position);
1630 m_animated_meshnode->setRotation(m_attachment_rotation);
1631 m_animated_meshnode->updateAbsolutePosition();
1639 m_spritenode->setParent(parent_bone);
1640 m_spritenode->setPosition(m_attachment_position);
1641 m_spritenode->setRotation(m_attachment_rotation);
1642 m_spritenode->updateAbsolutePosition();
1646 m_spritenode->setParent(parent_mesh);
1647 m_spritenode->setPosition(m_attachment_position);
1648 m_spritenode->setRotation(m_attachment_rotation);
1649 m_spritenode->updateAbsolutePosition();
1651 else if(parent_animated_mesh) {
1652 m_spritenode->setParent(parent_animated_mesh);
1653 m_spritenode->setPosition(m_attachment_position);
1654 m_spritenode->setRotation(m_attachment_rotation);
1655 m_spritenode->updateAbsolutePosition();
1657 else if(parent_sprite) {
1658 m_spritenode->setParent(parent_sprite);
1659 m_spritenode->setPosition(m_attachment_position);
1660 m_spritenode->setRotation(m_attachment_rotation);
1661 m_spritenode->updateAbsolutePosition();
1665 if(m_is_local_player)
1667 LocalPlayer *player = m_env->getLocalPlayer();
1668 player->isAttached = true;
1673 void GenericCAO::processMessage(const std::string &data)
1675 //infostream<<"GenericCAO: Got message"<<std::endl;
1676 std::istringstream is(data, std::ios::binary);
1678 u8 cmd = readU8(is);
1679 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1681 m_prop = gob_read_set_properties(is);
1683 m_selection_box = m_prop.collisionbox;
1684 m_selection_box.MinEdge *= BS;
1685 m_selection_box.MaxEdge *= BS;
1687 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1688 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1690 if(!m_initial_tx_basepos_set){
1691 m_initial_tx_basepos_set = true;
1692 m_tx_basepos = m_prop.initial_sprite_basepos;
1697 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1699 // Not sent by the server if this object is an attachment.
1700 // We might however get here if the server notices the object being detached before the client.
1701 m_position = readV3F1000(is);
1702 m_velocity = readV3F1000(is);
1703 m_acceleration = readV3F1000(is);
1704 if(fabs(m_prop.automatic_rotate) < 0.001)
1705 m_yaw = readF1000(is);
1708 bool do_interpolate = readU8(is);
1709 bool is_end_position = readU8(is);
1710 float update_interval = readF1000(is);
1712 // Place us a bit higher if we're physical, to not sink into
1713 // the ground due to sucky collision detection...
1715 m_position += v3f(0,0.002,0);
1717 if(getParent() != NULL) // Just in case
1722 if(!m_prop.physical)
1723 pos_translator.update(m_position, is_end_position, update_interval);
1725 pos_translator.init(m_position);
1729 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1730 std::string mod = deSerializeString(is);
1731 updateTextures(mod);
1733 else if(cmd == GENERIC_CMD_SET_SPRITE) {
1734 v2s16 p = readV2S16(is);
1735 int num_frames = readU16(is);
1736 float framelength = readF1000(is);
1737 bool select_horiz_by_yawpitch = readU8(is);
1740 m_anim_num_frames = num_frames;
1741 m_anim_framelength = framelength;
1742 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1746 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1747 float override_speed = readF1000(is);
1748 float override_jump = readF1000(is);
1749 float override_gravity = readF1000(is);
1750 // these are sent inverted so we get true when the server sends nothing
1751 bool sneak = !readU8(is);
1752 bool sneak_glitch = !readU8(is);
1755 if(m_is_local_player)
1757 LocalPlayer *player = m_env->getLocalPlayer();
1758 player->physics_override_speed = override_speed;
1759 player->physics_override_jump = override_jump;
1760 player->physics_override_gravity = override_gravity;
1761 player->physics_override_sneak = sneak;
1762 player->physics_override_sneak_glitch = sneak_glitch;
1765 else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1766 // TODO: change frames send as v2s32 value
1767 v2f range = readV2F1000(is);
1768 if (!m_is_local_player) {
1769 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1770 m_animation_speed = readF1000(is);
1771 m_animation_blend = readF1000(is);
1774 LocalPlayer *player = m_env->getLocalPlayer();
1775 if(player->last_animation == NO_ANIM)
1777 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1778 m_animation_speed = readF1000(is);
1779 m_animation_blend = readF1000(is);
1781 // update animation only if local animations present
1782 // and received animation is unknown (except idle animation)
1783 bool is_known = false;
1784 for (int i = 1;i<4;i++)
1786 if(m_animation_range.Y == player->local_animations[i].Y)
1790 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1796 else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1797 std::string bone = deSerializeString(is);
1798 v3f position = readV3F1000(is);
1799 v3f rotation = readV3F1000(is);
1800 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1802 updateBonePosition();
1804 else if(cmd == GENERIC_CMD_SET_ATTACHMENT) {
1805 m_env->m_attachements[getId()] = readS16(is);
1806 m_children.push_back(m_env->m_attachements[getId()]);
1807 m_attachment_bone = deSerializeString(is);
1808 m_attachment_position = readV3F1000(is);
1809 m_attachment_rotation = readV3F1000(is);
1811 updateAttachments();
1813 else if(cmd == GENERIC_CMD_PUNCHED) {
1814 /*s16 damage =*/ readS16(is);
1815 s16 result_hp = readS16(is);
1817 // Use this instead of the send damage to not interfere with prediction
1818 s16 damage = m_hp - result_hp;
1826 // TODO: Execute defined fast response
1827 // As there is no definition, make a smoke puff
1828 ClientSimpleObject *simple = createSmokePuff(
1829 m_smgr, m_env, m_position,
1830 m_prop.visual_size * BS);
1831 m_env->addSimpleObject(simple);
1833 // TODO: Execute defined fast response
1834 // Flashing shall suffice as there is no definition
1835 m_reset_textures_timer = 0.05;
1837 m_reset_textures_timer += 0.05 * damage;
1838 updateTextures("^[brighten");
1842 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1843 m_armor_groups.clear();
1844 int armor_groups_size = readU16(is);
1845 for(int i=0; i<armor_groups_size; i++)
1847 std::string name = deSerializeString(is);
1848 int rating = readS16(is);
1849 m_armor_groups[name] = rating;
1854 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1855 float time_from_last_punch)
1858 const ToolCapabilities *toolcap =
1859 &punchitem->getToolCapabilities(m_gamedef->idef());
1860 PunchDamageResult result = getPunchDamage(
1864 time_from_last_punch);
1866 if(result.did_punch && result.damage != 0)
1868 if(result.damage < m_hp)
1870 m_hp -= result.damage;
1873 // TODO: Execute defined fast response
1874 // As there is no definition, make a smoke puff
1875 ClientSimpleObject *simple = createSmokePuff(
1876 m_smgr, m_env, m_position,
1877 m_prop.visual_size * BS);
1878 m_env->addSimpleObject(simple);
1880 // TODO: Execute defined fast response
1881 // Flashing shall suffice as there is no definition
1882 m_reset_textures_timer = 0.05;
1883 if(result.damage >= 2)
1884 m_reset_textures_timer += 0.05 * result.damage;
1885 updateTextures("^[brighten");
1891 std::string GenericCAO::debugInfoText()
1893 std::ostringstream os(std::ios::binary);
1894 os<<"GenericCAO hp="<<m_hp<<"\n";
1896 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1897 i != m_armor_groups.end(); i++)
1899 os<<i->first<<"="<<i->second<<", ";
1906 GenericCAO proto_GenericCAO(NULL, NULL);