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"
30 #include "client/tile.h"
31 #include "environment.h"
32 #include "collision.h"
34 #include "serialization.h" // For decompressZlib
36 #include "clientobject.h"
40 #include "content_cso.h"
43 #include "localplayer.h"
45 #include "camera.h" // CameraModes
46 #include "wieldmesh.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);
146 ActiveObjectType getType() const
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->getTextureForMesh("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);
290 ActiveObjectType getType() const
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 aabb3f *getSelectionBox()
313 {return &m_selection_box;}
317 std::string infoText()
320 bool getCollisionBox(aabb3f *toset) { return false; }
322 aabb3f 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 warningstream<<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),
553 m_wield_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_animation_loop(true),
569 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
570 m_attachment_bone(""),
571 m_attachment_position(v3f(0,0,0)),
572 m_attachment_rotation(v3f(0,0,0)),
573 m_attached_to_local(false),
575 m_anim_num_frames(1),
576 m_anim_framelength(0.2),
578 m_reset_textures_timer(-1),
579 m_visuals_expired(false),
580 m_step_distance_counter(0),
584 if (gamedef == NULL) {
585 ClientActiveObject::registerType(getType(), create);
591 bool GenericCAO::getCollisionBox(aabb3f *toset)
595 //update collision box
596 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
597 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
599 toset->MinEdge += m_position;
600 toset->MaxEdge += m_position;
608 bool GenericCAO::collideWithObjects()
610 return m_prop.collideWithObjects;
613 void GenericCAO::initialize(const std::string &data)
615 infostream<<"GenericCAO: Got init data"<<std::endl;
616 std::istringstream is(data, std::ios::binary);
617 int num_messages = 0;
619 u8 version = readU8(is);
621 if(version == 1) // In PROTOCOL_VERSION 14
623 m_name = deSerializeString(is);
624 m_is_player = readU8(is);
626 m_position = readV3F1000(is);
627 m_yaw = readF1000(is);
629 num_messages = readU8(is);
631 else if(version == 0) // In PROTOCOL_VERSION 13
633 m_name = deSerializeString(is);
634 m_is_player = readU8(is);
635 m_position = readV3F1000(is);
636 m_yaw = readF1000(is);
638 num_messages = readU8(is);
642 errorstream<<"GenericCAO: Unsupported init data version"
647 for(int i=0; i<num_messages; i++)
649 std::string message = deSerializeLongString(is);
650 processMessage(message);
653 pos_translator.init(m_position);
658 Player *player = m_env->getPlayer(m_name.c_str());
659 if(player && player->isLocal())
661 m_is_local_player = true;
662 m_is_visible = false;
663 LocalPlayer* localplayer = dynamic_cast<LocalPlayer*>(player);
665 assert( localplayer != NULL );
666 localplayer->setCAO(this);
668 m_env->addPlayerName(m_name.c_str());
672 GenericCAO::~GenericCAO()
675 m_env->removePlayerName(m_name.c_str());
677 removeFromScene(true);
680 aabb3f *GenericCAO::getSelectionBox()
682 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
684 return &m_selection_box;
687 v3f GenericCAO::getPosition()
689 if (getParent() != NULL) {
690 scene::ISceneNode *node = getSceneNode();
692 return node->getAbsolutePosition();
696 return pos_translator.vect_show;
699 scene::ISceneNode* GenericCAO::getSceneNode()
703 } else if (m_animated_meshnode) {
704 return m_animated_meshnode;
705 } else if (m_wield_meshnode) {
706 return m_wield_meshnode;
707 } else if (m_spritenode) {
713 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
718 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
720 return m_animated_meshnode;
723 WieldMeshSceneNode* GenericCAO::getWieldMeshSceneNode()
725 return m_wield_meshnode;
728 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
733 void GenericCAO::setChildrenVisible(bool toset)
735 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
736 GenericCAO *obj = m_env->getGenericCAO(m_children[i]);
738 obj->setVisible(toset);
743 void GenericCAO::setAttachments()
748 ClientActiveObject* GenericCAO::getParent()
750 ClientActiveObject *obj = NULL;
752 u16 attached_id = m_env->attachement_parent_ids[getId()];
754 if ((attached_id != 0) &&
755 (attached_id != getId())) {
756 obj = m_env->getActiveObject(attached_id);
761 void GenericCAO::removeFromScene(bool permanent)
763 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
764 if((m_env != NULL) && (permanent))
766 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
767 u16 ci = m_children[i];
768 if (m_env->attachement_parent_ids[ci] == getId()) {
769 m_env->attachement_parent_ids[ci] = 0;
773 m_env->attachement_parent_ids[getId()] = 0;
775 LocalPlayer* player = m_env->getLocalPlayer();
776 if (this == player->parent) {
777 player->parent = NULL;
778 player->isAttached = false;
783 m_meshnode->remove();
786 } else if (m_animated_meshnode) {
787 m_animated_meshnode->remove();
788 m_animated_meshnode->drop();
789 m_animated_meshnode = NULL;
790 } else if (m_wield_meshnode) {
791 m_wield_meshnode->remove();
792 m_wield_meshnode->drop();
793 m_wield_meshnode = NULL;
794 } else if (m_spritenode) {
795 m_spritenode->remove();
796 m_spritenode->drop();
801 m_gamedef->getCamera()->removeNametag(m_nametag);
806 void GenericCAO::addToScene(scene::ISceneManager *smgr,
807 ITextureSource *tsrc, IrrlichtDevice *irr)
812 if (getSceneNode() != NULL) {
816 m_visuals_expired = false;
818 if (!m_prop.is_visible) {
822 if (m_prop.visual == "sprite") {
823 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
824 m_spritenode = smgr->addBillboardSceneNode(
825 NULL, v2f(1, 1), v3f(0,0,0), -1);
826 m_spritenode->grab();
827 m_spritenode->setMaterialTexture(0,
828 tsrc->getTextureForMesh("unknown_node.png"));
829 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
830 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
831 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
832 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
833 u8 li = m_last_light;
834 m_spritenode->setColor(video::SColor(255,li,li,li));
835 m_spritenode->setSize(m_prop.visual_size*BS);
837 const float txs = 1.0 / 1;
838 const float tys = 1.0 / 1;
839 setBillboardTextureMatrix(m_spritenode,
843 else if(m_prop.visual == "upright_sprite") {
844 scene::SMesh *mesh = new scene::SMesh();
845 double dx = BS*m_prop.visual_size.X/2;
846 double dy = BS*m_prop.visual_size.Y/2;
848 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
849 u8 li = m_last_light;
850 video::SColor c(255,li,li,li);
851 video::S3DVertex vertices[4] =
853 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
854 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
855 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
856 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
858 u16 indices[] = {0,1,2,2,3,0};
859 buf->append(vertices, 4, indices, 6);
861 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
862 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
863 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
864 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
866 mesh->addMeshBuffer(buf);
870 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
871 u8 li = m_last_light;
872 video::SColor c(255,li,li,li);
873 video::S3DVertex vertices[4] =
875 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
876 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
877 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
878 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
880 u16 indices[] = {0,1,2,2,3,0};
881 buf->append(vertices, 4, indices, 6);
883 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
884 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
885 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
886 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
888 mesh->addMeshBuffer(buf);
891 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
894 // Set it to use the materials of the meshbuffers directly.
895 // This is needed for changing the texture in the future
896 m_meshnode->setReadOnlyMaterials(true);
898 else if(m_prop.visual == "cube") {
899 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
900 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
901 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
905 m_meshnode->setScale(v3f(m_prop.visual_size.X,
906 m_prop.visual_size.Y,
907 m_prop.visual_size.X));
908 u8 li = m_last_light;
909 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
911 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
912 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
913 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
914 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
916 else if(m_prop.visual == "mesh") {
917 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
918 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
921 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
922 m_animated_meshnode->grab();
923 mesh->drop(); // The scene node took hold of it
924 m_animated_meshnode->animateJoints(); // Needed for some animations
925 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
926 m_prop.visual_size.Y,
927 m_prop.visual_size.X));
928 u8 li = m_last_light;
929 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
931 bool backface_culling = m_prop.backface_culling;
933 backface_culling = false;
935 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
936 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
937 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
938 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
939 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
942 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
944 else if(m_prop.visual == "wielditem") {
945 infostream<<"GenericCAO::addToScene(): wielditem"<<std::endl;
946 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
947 if(m_prop.textures.size() >= 1){
948 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
949 IItemDefManager *idef = m_gamedef->idef();
950 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
952 m_wield_meshnode = new WieldMeshSceneNode(
953 smgr->getRootSceneNode(), smgr, -1);
954 m_wield_meshnode->setItem(item, m_gamedef);
956 m_wield_meshnode->setScale(v3f(m_prop.visual_size.X/2,
957 m_prop.visual_size.Y/2,
958 m_prop.visual_size.X/2));
959 u8 li = m_last_light;
960 m_wield_meshnode->setColor(video::SColor(255,li,li,li));
963 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
964 <<"\" not supported"<<std::endl;
968 scene::ISceneNode *node = getSceneNode();
969 if (node && m_prop.nametag != "" && !m_is_local_player) {
971 m_nametag = m_gamedef->getCamera()->addNametag(node,
972 m_prop.nametag, m_prop.nametag_color);
977 updateBonePosition();
981 void GenericCAO::updateLight(u8 light_at_pos)
983 // Don't update light of attached one
984 if (getParent() != NULL) {
988 updateLightNoCheck(light_at_pos);
990 // Update light of all children
991 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
992 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
994 obj->updateLightNoCheck(light_at_pos);
999 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
1001 u8 li = decode_light(light_at_pos);
1002 if (li != m_last_light) {
1004 video::SColor color(255,li,li,li);
1006 setMeshColor(m_meshnode->getMesh(), color);
1007 } else if (m_animated_meshnode) {
1008 setMeshColor(m_animated_meshnode->getMesh(), color);
1009 } else if (m_wield_meshnode) {
1010 m_wield_meshnode->setColor(color);
1011 } else if (m_spritenode) {
1012 m_spritenode->setColor(color);
1017 v3s16 GenericCAO::getLightPosition()
1019 return floatToInt(m_position, BS);
1022 void GenericCAO::updateNodePos()
1024 if (getParent() != NULL)
1027 scene::ISceneNode *node = getSceneNode();
1030 v3s16 camera_offset = m_env->getCameraOffset();
1031 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
1032 if (node != m_spritenode) { // rotate if not a sprite
1033 v3f rot = node->getRotation();
1035 node->setRotation(rot);
1040 void GenericCAO::step(float dtime, ClientEnvironment *env)
1042 // Handel model of local player instantly to prevent lags
1043 if(m_is_local_player)
1045 LocalPlayer *player = m_env->getLocalPlayer();
1049 int old_anim = player->last_animation;
1050 float old_anim_speed = player->last_animation_speed;
1051 m_position = player->getPosition() + v3f(0,BS,0);
1052 m_velocity = v3f(0,0,0);
1053 m_acceleration = v3f(0,0,0);
1054 pos_translator.vect_show = m_position;
1055 m_yaw = player->getYaw();
1056 PlayerControl controls = player->getPlayerControl();
1058 bool walking = false;
1059 if (controls.up || controls.down || controls.left || controls.right ||
1060 controls.forw_move_joystick_axis != 0.f ||
1061 controls.sidew_move_joystick_axis != 0.f)
1064 f32 new_speed = player->local_animation_speed;
1065 v2s32 new_anim = v2s32(0,0);
1066 bool allow_update = false;
1068 // increase speed if using fast or flying fast
1069 if((g_settings->getBool("fast_move") &&
1070 m_gamedef->checkLocalPrivilege("fast")) &&
1072 (!player->touching_ground &&
1073 g_settings->getBool("free_move") &&
1074 m_gamedef->checkLocalPrivilege("fly"))))
1076 // slowdown speed if sneeking
1077 if(controls.sneak && walking)
1080 if(walking && (controls.LMB || controls.RMB))
1082 new_anim = player->local_animations[3];
1083 player->last_animation = WD_ANIM;
1084 } else if(walking) {
1085 new_anim = player->local_animations[1];
1086 player->last_animation = WALK_ANIM;
1087 } else if(controls.LMB || controls.RMB) {
1088 new_anim = player->local_animations[2];
1089 player->last_animation = DIG_ANIM;
1092 // Apply animations if input detected and not attached
1093 // or set idle animation
1094 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1096 allow_update = true;
1097 m_animation_range = new_anim;
1098 m_animation_speed = new_speed;
1099 player->last_animation_speed = m_animation_speed;
1101 player->last_animation = NO_ANIM;
1103 if (old_anim != NO_ANIM)
1105 m_animation_range = player->local_animations[0];
1110 // Update local player animations
1111 if ((player->last_animation != old_anim ||
1112 m_animation_speed != old_anim_speed) &&
1113 player->last_animation != NO_ANIM && allow_update)
1119 if(m_visuals_expired && m_smgr && m_irr){
1120 m_visuals_expired = false;
1122 // Attachments, part 1: All attached objects must be unparented first,
1123 // or Irrlicht causes a segmentation fault
1124 for(std::vector<u16>::iterator ci = m_children.begin();
1125 ci != m_children.end();)
1127 if (m_env->attachement_parent_ids[*ci] != getId()) {
1128 ci = m_children.erase(ci);
1131 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1133 scene::ISceneNode *child_node = obj->getSceneNode();
1135 child_node->setParent(m_smgr->getRootSceneNode());
1140 removeFromScene(false);
1141 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1143 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1144 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1145 // Get the object of the child
1146 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1148 obj->setAttachments();
1152 // Make sure m_is_visible is always applied
1153 scene::ISceneNode *node = getSceneNode();
1155 node->setVisible(m_is_visible);
1157 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1159 // Set these for later
1160 m_position = getPosition();
1161 m_velocity = v3f(0,0,0);
1162 m_acceleration = v3f(0,0,0);
1163 pos_translator.vect_show = m_position;
1165 if(m_is_local_player) // Update local player attachment position
1167 LocalPlayer *player = m_env->getLocalPlayer();
1168 player->overridePosition = getParent()->getPosition();
1169 m_env->getLocalPlayer()->parent = getParent();
1172 v3f lastpos = pos_translator.vect_show;
1176 aabb3f box = m_prop.collisionbox;
1179 collisionMoveResult moveresult;
1180 f32 pos_max_d = BS*0.125; // Distance per iteration
1181 v3f p_pos = m_position;
1182 v3f p_velocity = m_velocity;
1183 moveresult = collisionMoveSimple(env,env->getGameDef(),
1184 pos_max_d, box, m_prop.stepheight, dtime,
1185 &p_pos, &p_velocity, m_acceleration,
1186 this, m_prop.collideWithObjects);
1189 m_velocity = p_velocity;
1191 bool is_end_position = moveresult.collides;
1192 pos_translator.update(m_position, is_end_position, dtime);
1193 pos_translator.translate(dtime);
1196 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1197 m_velocity += dtime * m_acceleration;
1198 pos_translator.update(m_position, pos_translator.aim_is_end,
1199 pos_translator.anim_time);
1200 pos_translator.translate(dtime);
1204 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1205 m_step_distance_counter += moved;
1206 if(m_step_distance_counter > 1.5*BS)
1208 m_step_distance_counter = 0;
1209 if(!m_is_local_player && m_prop.makes_footstep_sound)
1211 INodeDefManager *ndef = m_gamedef->ndef();
1212 v3s16 p = floatToInt(getPosition() + v3f(0,
1213 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1214 MapNode n = m_env->getMap().getNodeNoEx(p);
1215 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1216 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1221 m_anim_timer += dtime;
1222 if(m_anim_timer >= m_anim_framelength)
1224 m_anim_timer -= m_anim_framelength;
1226 if(m_anim_frame >= m_anim_num_frames)
1232 if(m_reset_textures_timer >= 0)
1234 m_reset_textures_timer -= dtime;
1235 if(m_reset_textures_timer <= 0){
1236 m_reset_textures_timer = -1;
1240 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1242 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1246 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1247 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1249 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1250 + m_prop.automatic_face_movement_dir_offset;
1251 float max_rotation_delta =
1252 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1254 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
1255 (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
1257 m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
1259 m_yaw = optimal_yaw;
1265 void GenericCAO::updateTexturePos()
1269 scene::ICameraSceneNode* camera =
1270 m_spritenode->getSceneManager()->getActiveCamera();
1273 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1274 - camera->getAbsolutePosition();
1275 cam_to_entity.normalize();
1277 int row = m_tx_basepos.Y;
1278 int col = m_tx_basepos.X;
1280 if(m_tx_select_horiz_by_yawpitch)
1282 if(cam_to_entity.Y > 0.75)
1284 else if(cam_to_entity.Y < -0.75)
1288 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1289 float dir = mob_dir - m_yaw;
1290 dir = wrapDegrees_180(dir);
1291 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1292 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1294 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1296 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1298 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1305 // Animation goes downwards
1306 row += m_anim_frame;
1308 float txs = m_tx_size.X;
1309 float tys = m_tx_size.Y;
1310 setBillboardTextureMatrix(m_spritenode,
1311 txs, tys, col, row);
1315 void GenericCAO::updateTextures(const std::string &mod)
1317 ITextureSource *tsrc = m_gamedef->tsrc();
1319 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1320 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1321 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1325 if(m_prop.visual == "sprite")
1327 std::string texturestring = "unknown_node.png";
1328 if(m_prop.textures.size() >= 1)
1329 texturestring = m_prop.textures[0];
1330 texturestring += mod;
1331 m_spritenode->setMaterialTexture(0,
1332 tsrc->getTextureForMesh(texturestring));
1334 // This allows setting per-material colors. However, until a real lighting
1335 // system is added, the code below will have no effect. Once MineTest
1336 // has directional lighting, it should work automatically.
1337 if(m_prop.colors.size() >= 1)
1339 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1340 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1341 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1344 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1345 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1346 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1349 if(m_animated_meshnode)
1351 if(m_prop.visual == "mesh")
1353 for (u32 i = 0; i < m_prop.textures.size() &&
1354 i < m_animated_meshnode->getMaterialCount(); ++i)
1356 std::string texturestring = m_prop.textures[i];
1357 if(texturestring == "")
1358 continue; // Empty texture string means don't modify that material
1359 texturestring += mod;
1360 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1363 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1367 // Set material flags and texture
1368 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1369 material.TextureLayer[0].Texture = texture;
1370 material.setFlag(video::EMF_LIGHTING, false);
1371 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1373 m_animated_meshnode->getMaterial(i)
1374 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1375 m_animated_meshnode->getMaterial(i)
1376 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1377 m_animated_meshnode->getMaterial(i)
1378 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1380 for (u32 i = 0; i < m_prop.colors.size() &&
1381 i < m_animated_meshnode->getMaterialCount(); ++i)
1383 // This allows setting per-material colors. However, until a real lighting
1384 // system is added, the code below will have no effect. Once MineTest
1385 // has directional lighting, it should work automatically.
1386 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1387 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1388 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1394 if(m_prop.visual == "cube")
1396 for (u32 i = 0; i < 6; ++i)
1398 std::string texturestring = "unknown_node.png";
1399 if(m_prop.textures.size() > i)
1400 texturestring = m_prop.textures[i];
1401 texturestring += mod;
1404 // Set material flags and texture
1405 video::SMaterial& material = m_meshnode->getMaterial(i);
1406 material.setFlag(video::EMF_LIGHTING, false);
1407 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1408 material.setTexture(0,
1409 tsrc->getTextureForMesh(texturestring));
1410 material.getTextureMatrix(0).makeIdentity();
1412 // This allows setting per-material colors. However, until a real lighting
1413 // system is added, the code below will have no effect. Once MineTest
1414 // has directional lighting, it should work automatically.
1415 if(m_prop.colors.size() > i)
1417 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1418 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1419 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1422 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1423 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1424 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1427 else if(m_prop.visual == "upright_sprite")
1429 scene::IMesh *mesh = m_meshnode->getMesh();
1431 std::string tname = "unknown_object.png";
1432 if(m_prop.textures.size() >= 1)
1433 tname = m_prop.textures[0];
1435 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1436 buf->getMaterial().setTexture(0,
1437 tsrc->getTextureForMesh(tname));
1439 // This allows setting per-material colors. However, until a real lighting
1440 // system is added, the code below will have no effect. Once MineTest
1441 // has directional lighting, it should work automatically.
1442 if(m_prop.colors.size() >= 1)
1444 buf->getMaterial().AmbientColor = m_prop.colors[0];
1445 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1446 buf->getMaterial().SpecularColor = m_prop.colors[0];
1449 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1450 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1451 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1454 std::string tname = "unknown_object.png";
1455 if(m_prop.textures.size() >= 2)
1456 tname = m_prop.textures[1];
1457 else if(m_prop.textures.size() >= 1)
1458 tname = m_prop.textures[0];
1460 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1461 buf->getMaterial().setTexture(0,
1462 tsrc->getTextureForMesh(tname));
1464 // This allows setting per-material colors. However, until a real lighting
1465 // system is added, the code below will have no effect. Once MineTest
1466 // has directional lighting, it should work automatically.
1467 if(m_prop.colors.size() >= 2)
1469 buf->getMaterial().AmbientColor = m_prop.colors[1];
1470 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1471 buf->getMaterial().SpecularColor = m_prop.colors[1];
1473 else if(m_prop.colors.size() >= 1)
1475 buf->getMaterial().AmbientColor = m_prop.colors[0];
1476 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1477 buf->getMaterial().SpecularColor = m_prop.colors[0];
1480 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1481 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1482 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1488 void GenericCAO::updateAnimation()
1490 if(m_animated_meshnode == NULL)
1493 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1494 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1495 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1496 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1497 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1498 m_animated_meshnode->setTransitionTime(m_animation_blend);
1499 // Requires Irrlicht 1.8 or greater
1500 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1501 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1502 m_animated_meshnode->setLoopMode(m_animation_loop);
1506 void GenericCAO::updateBonePosition()
1508 if(m_bone_position.empty() || m_animated_meshnode == NULL)
1511 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1512 for(std::map<std::string,
1513 core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1514 ii != m_bone_position.end(); ++ii)
1516 std::string bone_name = (*ii).first;
1517 v3f bone_pos = (*ii).second.X;
1518 v3f bone_rot = (*ii).second.Y;
1519 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1522 bone->setPosition(bone_pos);
1523 bone->setRotation(bone_rot);
1528 void GenericCAO::updateAttachments()
1531 if (getParent() == NULL) { // Detach or don't attach
1532 scene::ISceneNode *node = getSceneNode();
1534 v3f old_position = node->getAbsolutePosition();
1535 v3f old_rotation = node->getRotation();
1536 node->setParent(m_smgr->getRootSceneNode());
1537 node->setPosition(old_position);
1538 node->setRotation(old_rotation);
1539 node->updateAbsolutePosition();
1541 if (m_is_local_player) {
1542 LocalPlayer *player = m_env->getLocalPlayer();
1543 player->isAttached = false;
1548 scene::ISceneNode *my_node = getSceneNode();
1550 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1551 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1552 getParent()->getAnimatedMeshSceneNode();
1553 if (parent_animated_mesh_node && m_attachment_bone != "") {
1554 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1557 if (my_node && parent_node) {
1558 my_node->setParent(parent_node);
1559 my_node->setPosition(m_attachment_position);
1560 my_node->setRotation(m_attachment_rotation);
1561 my_node->updateAbsolutePosition();
1563 if (m_is_local_player) {
1564 LocalPlayer *player = m_env->getLocalPlayer();
1565 player->isAttached = true;
1570 void GenericCAO::processMessage(const std::string &data)
1572 //infostream<<"GenericCAO: Got message"<<std::endl;
1573 std::istringstream is(data, std::ios::binary);
1575 u8 cmd = readU8(is);
1576 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1578 m_prop = gob_read_set_properties(is);
1580 m_selection_box = m_prop.collisionbox;
1581 m_selection_box.MinEdge *= BS;
1582 m_selection_box.MaxEdge *= BS;
1584 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1585 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1587 if(!m_initial_tx_basepos_set){
1588 m_initial_tx_basepos_set = true;
1589 m_tx_basepos = m_prop.initial_sprite_basepos;
1592 if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
1593 m_prop.nametag = m_name;
1597 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1599 // Not sent by the server if this object is an attachment.
1600 // We might however get here if the server notices the object being detached before the client.
1601 m_position = readV3F1000(is);
1602 m_velocity = readV3F1000(is);
1603 m_acceleration = readV3F1000(is);
1604 if(fabs(m_prop.automatic_rotate) < 0.001)
1605 m_yaw = readF1000(is);
1608 bool do_interpolate = readU8(is);
1609 bool is_end_position = readU8(is);
1610 float update_interval = readF1000(is);
1612 // Place us a bit higher if we're physical, to not sink into
1613 // the ground due to sucky collision detection...
1615 m_position += v3f(0,0.002,0);
1617 if(getParent() != NULL) // Just in case
1622 if(!m_prop.physical)
1623 pos_translator.update(m_position, is_end_position, update_interval);
1625 pos_translator.init(m_position);
1629 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1630 std::string mod = deSerializeString(is);
1631 updateTextures(mod);
1633 else if(cmd == GENERIC_CMD_SET_SPRITE) {
1634 v2s16 p = readV2S16(is);
1635 int num_frames = readU16(is);
1636 float framelength = readF1000(is);
1637 bool select_horiz_by_yawpitch = readU8(is);
1640 m_anim_num_frames = num_frames;
1641 m_anim_framelength = framelength;
1642 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1646 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1647 float override_speed = readF1000(is);
1648 float override_jump = readF1000(is);
1649 float override_gravity = readF1000(is);
1650 // these are sent inverted so we get true when the server sends nothing
1651 bool sneak = !readU8(is);
1652 bool sneak_glitch = !readU8(is);
1655 if(m_is_local_player)
1657 LocalPlayer *player = m_env->getLocalPlayer();
1658 player->physics_override_speed = override_speed;
1659 player->physics_override_jump = override_jump;
1660 player->physics_override_gravity = override_gravity;
1661 player->physics_override_sneak = sneak;
1662 player->physics_override_sneak_glitch = sneak_glitch;
1665 else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1666 // TODO: change frames send as v2s32 value
1667 v2f range = readV2F1000(is);
1668 if (!m_is_local_player) {
1669 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1670 m_animation_speed = readF1000(is);
1671 m_animation_blend = readF1000(is);
1672 // these are sent inverted so we get true when the server sends nothing
1673 m_animation_loop = !readU8(is);
1676 LocalPlayer *player = m_env->getLocalPlayer();
1677 if(player->last_animation == NO_ANIM)
1679 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1680 m_animation_speed = readF1000(is);
1681 m_animation_blend = readF1000(is);
1682 // these are sent inverted so we get true when the server sends nothing
1683 m_animation_loop = !readU8(is);
1685 // update animation only if local animations present
1686 // and received animation is unknown (except idle animation)
1687 bool is_known = false;
1688 for (int i = 1;i<4;i++)
1690 if(m_animation_range.Y == player->local_animations[i].Y)
1694 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1700 else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1701 std::string bone = deSerializeString(is);
1702 v3f position = readV3F1000(is);
1703 v3f rotation = readV3F1000(is);
1704 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1706 updateBonePosition();
1707 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1708 u16 parentID = readS16(is);
1709 u16 oldparent = m_env->attachement_parent_ids[getId()];
1711 m_children.erase(std::remove(m_children.begin(), m_children.end(),
1712 getId()), m_children.end());
1714 m_env->attachement_parent_ids[getId()] = parentID;
1715 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1718 parentobj->m_children.push_back(getId());
1721 m_attachment_bone = deSerializeString(is);
1722 m_attachment_position = readV3F1000(is);
1723 m_attachment_rotation = readV3F1000(is);
1725 // localplayer itself can't be attached to localplayer
1726 if (!m_is_local_player) {
1727 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1728 // Objects attached to the local player should be hidden by default
1729 m_is_visible = !m_attached_to_local;
1732 updateAttachments();
1734 else if(cmd == GENERIC_CMD_PUNCHED) {
1735 /*s16 damage =*/ readS16(is);
1736 s16 result_hp = readS16(is);
1738 // Use this instead of the send damage to not interfere with prediction
1739 s16 damage = m_hp - result_hp;
1747 // TODO: Execute defined fast response
1748 // As there is no definition, make a smoke puff
1749 ClientSimpleObject *simple = createSmokePuff(
1750 m_smgr, m_env, m_position,
1751 m_prop.visual_size * BS);
1752 m_env->addSimpleObject(simple);
1754 // TODO: Execute defined fast response
1755 // Flashing shall suffice as there is no definition
1756 m_reset_textures_timer = 0.05;
1758 m_reset_textures_timer += 0.05 * damage;
1759 updateTextures("^[brighten");
1763 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1764 m_armor_groups.clear();
1765 int armor_groups_size = readU16(is);
1766 for(int i=0; i<armor_groups_size; i++)
1768 std::string name = deSerializeString(is);
1769 int rating = readS16(is);
1770 m_armor_groups[name] = rating;
1772 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1773 // Deprecated, for backwards compatibility only.
1774 readU8(is); // version
1775 m_prop.nametag_color = readARGB8(is);
1776 if (m_nametag != NULL) {
1777 m_nametag->nametag_color = m_prop.nametag_color;
1782 /* \pre punchitem != NULL
1784 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1785 float time_from_last_punch)
1787 assert(punchitem); // pre-condition
1788 const ToolCapabilities *toolcap =
1789 &punchitem->getToolCapabilities(m_gamedef->idef());
1790 PunchDamageResult result = getPunchDamage(
1794 time_from_last_punch);
1796 if(result.did_punch && result.damage != 0)
1798 if(result.damage < m_hp)
1800 m_hp -= result.damage;
1803 // TODO: Execute defined fast response
1804 // As there is no definition, make a smoke puff
1805 ClientSimpleObject *simple = createSmokePuff(
1806 m_smgr, m_env, m_position,
1807 m_prop.visual_size * BS);
1808 m_env->addSimpleObject(simple);
1810 // TODO: Execute defined fast response
1811 // Flashing shall suffice as there is no definition
1812 m_reset_textures_timer = 0.05;
1813 if(result.damage >= 2)
1814 m_reset_textures_timer += 0.05 * result.damage;
1815 updateTextures("^[brighten");
1821 std::string GenericCAO::debugInfoText()
1823 std::ostringstream os(std::ios::binary);
1824 os<<"GenericCAO hp="<<m_hp<<"\n";
1826 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1827 i != m_armor_groups.end(); ++i)
1829 os<<i->first<<"="<<i->second<<", ";
1836 GenericCAO proto_GenericCAO(NULL, NULL);