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/basic_macros.h"
30 #include "client/tile.h"
31 #include "environment.h"
32 #include "collision.h"
34 #include "serialization.h" // For decompressZlib
35 #include "clientobject.h"
39 #include "content_cso.h"
42 #include "localplayer.h"
44 #include "camera.h" // CameraModes
45 #include "wieldmesh.h"
50 struct ToolCapabilities;
52 UNORDERED_MAP<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
54 SmoothTranslator::SmoothTranslator():
64 void SmoothTranslator::init(v3f vect)
71 anim_time_counter = 0;
75 void SmoothTranslator::sharpen()
80 void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
82 aim_is_end = is_end_position;
85 if(update_interval > 0)
87 anim_time = update_interval;
89 if(anim_time < 0.001 || anim_time > 1.0)
90 anim_time = anim_time_counter;
92 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
94 anim_time_counter = 0;
98 void SmoothTranslator::translate(f32 dtime)
100 anim_time_counter = anim_time_counter + dtime;
101 anim_counter = anim_counter + dtime;
102 v3f vect_move = vect_aim - vect_old;
104 if(anim_time > 0.001)
105 moveratio = anim_time_counter / anim_time;
106 // Move a bit less than should, to avoid oscillation
107 moveratio = moveratio * 0.8;
108 float move_end = 1.5;
111 if(moveratio > move_end)
112 moveratio = move_end;
113 vect_show = vect_old + vect_move * moveratio;
116 bool SmoothTranslator::is_moving()
118 return ((anim_time_counter / anim_time) < 1.4);
125 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
126 float txs, float tys, int col, int row)
128 video::SMaterial& material = bill->getMaterial(0);
129 core::matrix4& matrix = material.getTextureMatrix(0);
130 matrix.setTextureTranslate(txs*col, tys*row);
131 matrix.setTextureScale(txs, tys);
138 class TestCAO : public ClientActiveObject
141 TestCAO(Client *client, ClientEnvironment *env);
144 ActiveObjectType getType() const
146 return ACTIVEOBJECT_TYPE_TEST;
149 static ClientActiveObject* create(Client *client, ClientEnvironment *env);
151 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
152 IrrlichtDevice *irr);
153 void removeFromScene(bool permanent);
154 void updateLight(u8 light_at_pos);
155 v3s16 getLightPosition();
156 void updateNodePos();
158 void step(float dtime, ClientEnvironment *env);
160 void processMessage(const std::string &data);
162 bool getCollisionBox(aabb3f *toset) const { return false; }
164 scene::IMeshSceneNode *m_node;
169 TestCAO proto_TestCAO(NULL, NULL);
171 TestCAO::TestCAO(Client *client, ClientEnvironment *env):
172 ClientActiveObject(0, client, env),
174 m_position(v3f(0,10*BS,0))
176 ClientActiveObject::registerType(getType(), create);
183 ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
185 return new TestCAO(client, env);
188 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
194 //video::IVideoDriver* driver = smgr->getVideoDriver();
196 scene::SMesh *mesh = new scene::SMesh();
197 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
198 video::SColor c(255,255,255,255);
199 video::S3DVertex vertices[4] =
201 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
202 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
203 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
204 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
206 u16 indices[] = {0,1,2,2,3,0};
207 buf->append(vertices, 4, indices, 6);
209 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
210 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
211 buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
212 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
213 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
214 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
216 mesh->addMeshBuffer(buf);
218 m_node = smgr->addMeshSceneNode(mesh, NULL);
223 void TestCAO::removeFromScene(bool permanent)
232 void TestCAO::updateLight(u8 light_at_pos)
236 v3s16 TestCAO::getLightPosition()
238 return floatToInt(m_position, BS);
241 void TestCAO::updateNodePos()
246 m_node->setPosition(m_position);
247 //m_node->setRotation(v3f(0, 45, 0));
250 void TestCAO::step(float dtime, ClientEnvironment *env)
254 v3f rot = m_node->getRotation();
255 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
256 rot.Y += dtime * 180;
257 m_node->setRotation(rot);
261 void TestCAO::processMessage(const std::string &data)
263 infostream<<"TestCAO: Got data: "<<data<<std::endl;
264 std::istringstream is(data, std::ios::binary);
282 class ItemCAO : public ClientActiveObject
285 ItemCAO(Client *client, ClientEnvironment *env);
288 ActiveObjectType getType() const
290 return ACTIVEOBJECT_TYPE_ITEM;
293 static ClientActiveObject* create(Client *client, ClientEnvironment *env);
295 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
296 IrrlichtDevice *irr);
297 void removeFromScene(bool permanent);
298 void updateLight(u8 light_at_pos);
299 v3s16 getLightPosition();
300 void updateNodePos();
301 void updateInfoText();
302 void updateTexture();
304 void step(float dtime, ClientEnvironment *env);
306 void processMessage(const std::string &data);
308 void initialize(const std::string &data);
310 aabb3f *getSelectionBox()
311 {return &m_selection_box;}
314 inline float getYaw() const
316 std::string infoText()
319 bool getCollisionBox(aabb3f *toset) const { return false; }
321 aabb3f m_selection_box;
322 scene::IMeshSceneNode *m_node;
324 std::string m_itemstring;
325 std::string m_infotext;
328 #include "inventory.h"
331 ItemCAO proto_ItemCAO(NULL, NULL);
333 ItemCAO::ItemCAO(Client *client, ClientEnvironment *env):
334 ClientActiveObject(0, client, env),
335 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
337 m_position(v3f(0,10*BS,0))
341 ClientActiveObject::registerType(getType(), create);
349 ClientActiveObject* ItemCAO::create(Client *client, ClientEnvironment *env)
351 return new ItemCAO(client, env);
354 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
360 //video::IVideoDriver* driver = smgr->getVideoDriver();
362 scene::SMesh *mesh = new scene::SMesh();
363 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
364 video::SColor c(255,255,255,255);
365 video::S3DVertex vertices[4] =
367 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
368 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
369 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
370 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
371 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
372 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
373 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
374 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
376 u16 indices[] = {0,1,2,2,3,0};
377 buf->append(vertices, 4, indices, 6);
379 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
380 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
381 // Initialize with a generated placeholder texture
382 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
383 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
384 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
385 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
387 mesh->addMeshBuffer(buf);
389 m_node = smgr->addMeshSceneNode(mesh, NULL);
400 void ItemCAO::removeFromScene(bool permanent)
409 void ItemCAO::updateLight(u8 light_at_pos)
414 u8 li = decode_light(light_at_pos);
415 video::SColor color(255,li,li,li);
416 setMeshColor(m_node->getMesh(), color);
419 v3s16 ItemCAO::getLightPosition()
421 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
424 void ItemCAO::updateNodePos()
429 m_node->setPosition(m_position);
432 void ItemCAO::updateInfoText()
435 IItemDefManager *idef = m_client->idef();
437 item.deSerialize(m_itemstring, idef);
438 if(item.isKnown(idef))
439 m_infotext = item.getDefinition(idef).description;
441 m_infotext = "Unknown item: '" + m_itemstring + "'";
443 m_infotext += " (" + itos(item.count) + ")";
445 catch(SerializationError &e)
447 m_infotext = "Unknown item: '" + m_itemstring + "'";
451 void ItemCAO::updateTexture()
456 // Create an inventory item to see what is its image
457 std::istringstream is(m_itemstring, std::ios_base::binary);
458 video::ITexture *texture = NULL;
460 IItemDefManager *idef = m_client->idef();
462 item.deSerialize(is, idef);
463 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_client);
465 catch(SerializationError &e)
467 warningstream<<FUNCTION_NAME
468 <<": error deSerializing itemstring \""
469 <<m_itemstring<<std::endl;
472 // Set meshbuffer texture
473 m_node->getMaterial(0).setTexture(0, texture);
477 void ItemCAO::step(float dtime, ClientEnvironment *env)
481 /*v3f rot = m_node->getRotation();
482 rot.Y += dtime * 120;
483 m_node->setRotation(rot);*/
484 LocalPlayer *player = env->getLocalPlayer();
486 v3f rot = m_node->getRotation();
487 rot.Y = 180.0 - (player->getYaw());
488 m_node->setRotation(rot);
492 void ItemCAO::processMessage(const std::string &data)
494 //infostream<<"ItemCAO: Got message"<<std::endl;
495 std::istringstream is(data, std::ios::binary);
501 m_position = readV3F1000(is);
507 m_itemstring = deSerializeString(is);
513 void ItemCAO::initialize(const std::string &data)
515 infostream<<"ItemCAO: Got init data"<<std::endl;
518 std::istringstream is(data, std::ios::binary);
520 u8 version = readU8(is);
525 m_position = readV3F1000(is);
527 m_itemstring = deSerializeString(is);
538 #include "genericobject.h"
540 GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
541 ClientActiveObject(0, client, env),
544 m_is_local_player(false),
549 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
551 m_animated_meshnode(NULL),
552 m_wield_meshnode(NULL),
555 m_position(v3f(0,10*BS,0)),
556 m_velocity(v3f(0,0,0)),
557 m_acceleration(v3f(0,0,0)),
562 m_initial_tx_basepos_set(false),
563 m_tx_select_horiz_by_yawpitch(false),
564 m_animation_range(v2s32(0,0)),
565 m_animation_speed(15),
566 m_animation_blend(0),
567 m_animation_loop(true),
568 m_bone_position(UNORDERED_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_previous_texture_modifier(""),
579 m_current_texture_modifier(""),
580 m_visuals_expired(false),
581 m_step_distance_counter(0),
585 if (client == NULL) {
586 ClientActiveObject::registerType(getType(), create);
592 bool GenericCAO::getCollisionBox(aabb3f *toset) const
596 //update collision box
597 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
598 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
600 toset->MinEdge += m_position;
601 toset->MaxEdge += m_position;
609 bool GenericCAO::collideWithObjects() const
611 return m_prop.collideWithObjects;
614 void GenericCAO::initialize(const std::string &data)
616 infostream<<"GenericCAO: Got init data"<<std::endl;
617 processInitData(data);
620 // Check if it's the current player
621 LocalPlayer *player = m_env->getLocalPlayer();
622 if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
623 m_is_local_player = true;
624 m_is_visible = false;
625 player->setCAO(this);
627 m_env->addPlayerName(m_name.c_str());
631 void GenericCAO::processInitData(const std::string &data)
633 std::istringstream is(data, std::ios::binary);
634 int num_messages = 0;
636 u8 version = readU8(is);
638 if (version == 1) { // In PROTOCOL_VERSION 14
639 m_name = deSerializeString(is);
640 m_is_player = readU8(is);
642 m_position = readV3F1000(is);
643 m_yaw = readF1000(is);
645 num_messages = readU8(is);
646 } else if (version == 0) { // In PROTOCOL_VERSION 13
647 m_name = deSerializeString(is);
648 m_is_player = readU8(is);
649 m_position = readV3F1000(is);
650 m_yaw = readF1000(is);
652 num_messages = readU8(is);
654 errorstream<<"GenericCAO: Unsupported init data version"
659 for (int i = 0; i < num_messages; i++) {
660 std::string message = deSerializeLongString(is);
661 processMessage(message);
664 pos_translator.init(m_position);
668 GenericCAO::~GenericCAO()
671 m_env->removePlayerName(m_name.c_str());
673 removeFromScene(true);
676 aabb3f *GenericCAO::getSelectionBox()
678 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
680 return &m_selection_box;
683 v3f GenericCAO::getPosition()
685 if (getParent() != NULL) {
686 scene::ISceneNode *node = getSceneNode();
688 return node->getAbsolutePosition();
692 return pos_translator.vect_show;
695 scene::ISceneNode* GenericCAO::getSceneNode()
699 } else if (m_animated_meshnode) {
700 return m_animated_meshnode;
701 } else if (m_wield_meshnode) {
702 return m_wield_meshnode;
703 } else if (m_spritenode) {
709 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
714 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
716 return m_animated_meshnode;
719 WieldMeshSceneNode* GenericCAO::getWieldMeshSceneNode()
721 return m_wield_meshnode;
724 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
729 void GenericCAO::setChildrenVisible(bool toset)
731 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
732 GenericCAO *obj = m_env->getGenericCAO(m_children[i]);
734 obj->setVisible(toset);
739 void GenericCAO::setAttachments()
744 ClientActiveObject* GenericCAO::getParent()
746 ClientActiveObject *obj = NULL;
748 u16 attached_id = m_env->attachement_parent_ids[getId()];
750 if ((attached_id != 0) &&
751 (attached_id != getId())) {
752 obj = m_env->getActiveObject(attached_id);
757 void GenericCAO::removeFromScene(bool permanent)
759 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
760 if((m_env != NULL) && (permanent))
762 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
763 u16 ci = m_children[i];
764 if (m_env->attachement_parent_ids[ci] == getId()) {
765 m_env->attachement_parent_ids[ci] = 0;
769 m_env->attachement_parent_ids[getId()] = 0;
771 LocalPlayer* player = m_env->getLocalPlayer();
772 if (this == player->parent) {
773 player->parent = NULL;
774 player->isAttached = false;
779 m_meshnode->remove();
782 } else if (m_animated_meshnode) {
783 m_animated_meshnode->remove();
784 m_animated_meshnode->drop();
785 m_animated_meshnode = NULL;
786 } else if (m_wield_meshnode) {
787 m_wield_meshnode->remove();
788 m_wield_meshnode->drop();
789 m_wield_meshnode = NULL;
790 } else if (m_spritenode) {
791 m_spritenode->remove();
792 m_spritenode->drop();
797 m_client->getCamera()->removeNametag(m_nametag);
802 void GenericCAO::addToScene(scene::ISceneManager *smgr,
803 ITextureSource *tsrc, IrrlichtDevice *irr)
808 if (getSceneNode() != NULL) {
812 m_visuals_expired = false;
814 if (!m_prop.is_visible) {
818 if (m_prop.visual == "sprite") {
819 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
820 m_spritenode = smgr->addBillboardSceneNode(
821 NULL, v2f(1, 1), v3f(0,0,0), -1);
822 m_spritenode->grab();
823 m_spritenode->setMaterialTexture(0,
824 tsrc->getTextureForMesh("unknown_node.png"));
825 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
826 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
827 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
828 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
829 u8 li = m_last_light;
830 m_spritenode->setColor(video::SColor(255,li,li,li));
831 m_spritenode->setSize(m_prop.visual_size*BS);
833 const float txs = 1.0 / 1;
834 const float tys = 1.0 / 1;
835 setBillboardTextureMatrix(m_spritenode,
838 } else if (m_prop.visual == "upright_sprite") {
839 scene::SMesh *mesh = new scene::SMesh();
840 double dx = BS * m_prop.visual_size.X / 2;
841 double dy = BS * m_prop.visual_size.Y / 2;
842 u8 li = m_last_light;
843 video::SColor c(255, li, li, li);
846 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
847 video::S3DVertex vertices[4] = {
848 video::S3DVertex(-dx, -dy, 0, 0,0,0, c, 1,1),
849 video::S3DVertex( dx, -dy, 0, 0,0,0, c, 0,1),
850 video::S3DVertex( dx, dy, 0, 0,0,0, c, 0,0),
851 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 1,0),
853 u16 indices[] = {0,1,2,2,3,0};
854 buf->append(vertices, 4, indices, 6);
856 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
857 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
858 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
859 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
861 mesh->addMeshBuffer(buf);
865 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
866 video::S3DVertex vertices[4] = {
867 video::S3DVertex( dx,-dy, 0, 0,0,0, c, 1,1),
868 video::S3DVertex(-dx,-dy, 0, 0,0,0, c, 0,1),
869 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 0,0),
870 video::S3DVertex( dx, dy, 0, 0,0,0, c, 1,0),
872 u16 indices[] = {0,1,2,2,3,0};
873 buf->append(vertices, 4, indices, 6);
875 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
876 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
877 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
878 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
880 mesh->addMeshBuffer(buf);
883 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
886 // Set it to use the materials of the meshbuffers directly.
887 // This is needed for changing the texture in the future
888 m_meshnode->setReadOnlyMaterials(true);
890 else if(m_prop.visual == "cube") {
891 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
892 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
893 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
897 m_meshnode->setScale(v3f(m_prop.visual_size.X,
898 m_prop.visual_size.Y,
899 m_prop.visual_size.X));
900 u8 li = m_last_light;
901 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
903 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
904 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
905 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
906 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
908 else if(m_prop.visual == "mesh") {
909 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
910 scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh);
913 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
914 m_animated_meshnode->grab();
915 mesh->drop(); // The scene node took hold of it
916 m_animated_meshnode->animateJoints(); // Needed for some animations
917 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
918 m_prop.visual_size.Y,
919 m_prop.visual_size.X));
920 u8 li = m_last_light;
921 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
923 bool backface_culling = m_prop.backface_culling;
925 backface_culling = false;
927 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
928 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
929 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
930 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
931 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
934 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
936 else if(m_prop.visual == "wielditem") {
938 infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
939 if (m_prop.wield_item == "") {
940 // Old format, only textures are specified.
941 infostream << "textures: " << m_prop.textures.size() << std::endl;
942 if (m_prop.textures.size() >= 1) {
943 infostream << "textures[0]: " << m_prop.textures[0]
945 IItemDefManager *idef = m_client->idef();
946 item = ItemStack(m_prop.textures[0], 1, 0, idef);
949 infostream << "serialized form: " << m_prop.wield_item << std::endl;
950 item.deSerialize(m_prop.wield_item, m_client->idef());
952 m_wield_meshnode = new WieldMeshSceneNode(smgr->getRootSceneNode(),
954 m_wield_meshnode->setItem(item, m_client);
956 m_wield_meshnode->setScale(
957 v3f(m_prop.visual_size.X / 2, 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));
962 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
963 <<"\" not supported"<<std::endl;
966 /* don't update while punch texture modifier is active */
967 if (m_reset_textures_timer < 0)
968 updateTextures(m_current_texture_modifier);
970 scene::ISceneNode *node = getSceneNode();
971 if (node && m_prop.nametag != "" && !m_is_local_player) {
973 m_nametag = m_client->getCamera()->addNametag(node,
974 m_prop.nametag, m_prop.nametag_color);
979 updateBonePosition();
983 void GenericCAO::updateLight(u8 light_at_pos)
985 // Don't update light of attached one
986 if (getParent() != NULL) {
990 updateLightNoCheck(light_at_pos);
992 // Update light of all children
993 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
994 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
996 obj->updateLightNoCheck(light_at_pos);
1001 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
1003 u8 li = decode_light(light_at_pos);
1004 if (li != m_last_light) {
1006 video::SColor color(255,li,li,li);
1008 setMeshColor(m_meshnode->getMesh(), color);
1009 } else if (m_animated_meshnode) {
1010 setMeshColor(m_animated_meshnode->getMesh(), color);
1011 } else if (m_wield_meshnode) {
1012 m_wield_meshnode->setColor(color);
1013 } else if (m_spritenode) {
1014 m_spritenode->setColor(color);
1019 v3s16 GenericCAO::getLightPosition()
1021 return floatToInt(m_position, BS);
1024 void GenericCAO::updateNodePos()
1026 if (getParent() != NULL)
1029 scene::ISceneNode *node = getSceneNode();
1032 v3s16 camera_offset = m_env->getCameraOffset();
1033 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
1034 if (node != m_spritenode) { // rotate if not a sprite
1035 v3f rot = node->getRotation();
1037 node->setRotation(rot);
1042 void GenericCAO::step(float dtime, ClientEnvironment *env)
1044 // Handel model of local player instantly to prevent lags
1045 if (m_is_local_player) {
1046 LocalPlayer *player = m_env->getLocalPlayer();
1048 int old_anim = player->last_animation;
1049 float old_anim_speed = player->last_animation_speed;
1050 m_position = player->getPosition() + v3f(0,BS,0);
1051 m_velocity = v3f(0,0,0);
1052 m_acceleration = v3f(0,0,0);
1053 pos_translator.vect_show = m_position;
1054 m_yaw = player->getYaw();
1055 const PlayerControl &controls = player->getPlayerControl();
1057 bool walking = false;
1058 if (controls.up || controls.down || controls.left || controls.right ||
1059 controls.forw_move_joystick_axis != 0.f ||
1060 controls.sidew_move_joystick_axis != 0.f)
1063 f32 new_speed = player->local_animation_speed;
1064 v2s32 new_anim = v2s32(0,0);
1065 bool allow_update = false;
1067 // increase speed if using fast or flying fast
1068 if((g_settings->getBool("fast_move") &&
1069 m_client->checkLocalPrivilege("fast")) &&
1071 (!player->touching_ground &&
1072 g_settings->getBool("free_move") &&
1073 m_client->checkLocalPrivilege("fly"))))
1075 // slowdown speed if sneeking
1076 if (controls.sneak && walking)
1079 if (walking && (controls.LMB || controls.RMB)) {
1080 new_anim = player->local_animations[3];
1081 player->last_animation = WD_ANIM;
1082 } else if(walking) {
1083 new_anim = player->local_animations[1];
1084 player->last_animation = WALK_ANIM;
1085 } else if(controls.LMB || controls.RMB) {
1086 new_anim = player->local_animations[2];
1087 player->last_animation = DIG_ANIM;
1090 // Apply animations if input detected and not attached
1091 // or set idle animation
1092 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
1093 allow_update = true;
1094 m_animation_range = new_anim;
1095 m_animation_speed = new_speed;
1096 player->last_animation_speed = m_animation_speed;
1098 player->last_animation = NO_ANIM;
1100 if (old_anim != NO_ANIM) {
1101 m_animation_range = player->local_animations[0];
1106 // Update local player animations
1107 if ((player->last_animation != old_anim ||
1108 m_animation_speed != old_anim_speed) &&
1109 player->last_animation != NO_ANIM && allow_update)
1115 if(m_visuals_expired && m_smgr && m_irr){
1116 m_visuals_expired = false;
1118 // Attachments, part 1: All attached objects must be unparented first,
1119 // or Irrlicht causes a segmentation fault
1120 for(std::vector<u16>::iterator ci = m_children.begin();
1121 ci != m_children.end();)
1123 if (m_env->attachement_parent_ids[*ci] != getId()) {
1124 ci = m_children.erase(ci);
1127 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1129 scene::ISceneNode *child_node = obj->getSceneNode();
1131 child_node->setParent(m_smgr->getRootSceneNode());
1136 removeFromScene(false);
1137 addToScene(m_smgr, m_client->tsrc(), m_irr);
1139 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1140 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1141 // Get the object of the child
1142 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1144 obj->setAttachments();
1148 // Make sure m_is_visible is always applied
1149 scene::ISceneNode *node = getSceneNode();
1151 node->setVisible(m_is_visible);
1153 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1155 // Set these for later
1156 m_position = getPosition();
1157 m_velocity = v3f(0,0,0);
1158 m_acceleration = v3f(0,0,0);
1159 pos_translator.vect_show = m_position;
1161 if(m_is_local_player) // Update local player attachment position
1163 LocalPlayer *player = m_env->getLocalPlayer();
1164 player->overridePosition = getParent()->getPosition();
1165 m_env->getLocalPlayer()->parent = getParent();
1168 v3f lastpos = pos_translator.vect_show;
1172 aabb3f box = m_prop.collisionbox;
1175 collisionMoveResult moveresult;
1176 f32 pos_max_d = BS*0.125; // Distance per iteration
1177 v3f p_pos = m_position;
1178 v3f p_velocity = m_velocity;
1179 moveresult = collisionMoveSimple(env,env->getGameDef(),
1180 pos_max_d, box, m_prop.stepheight, dtime,
1181 &p_pos, &p_velocity, m_acceleration,
1182 this, m_prop.collideWithObjects);
1185 m_velocity = p_velocity;
1187 bool is_end_position = moveresult.collides;
1188 pos_translator.update(m_position, is_end_position, dtime);
1189 pos_translator.translate(dtime);
1192 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1193 m_velocity += dtime * m_acceleration;
1194 pos_translator.update(m_position, pos_translator.aim_is_end,
1195 pos_translator.anim_time);
1196 pos_translator.translate(dtime);
1200 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1201 m_step_distance_counter += moved;
1202 if(m_step_distance_counter > 1.5*BS)
1204 m_step_distance_counter = 0;
1205 if(!m_is_local_player && m_prop.makes_footstep_sound)
1207 INodeDefManager *ndef = m_client->ndef();
1208 v3s16 p = floatToInt(getPosition() + v3f(0,
1209 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1210 MapNode n = m_env->getMap().getNodeNoEx(p);
1211 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1212 m_client->sound()->playSoundAt(spec, false, getPosition());
1217 m_anim_timer += dtime;
1218 if(m_anim_timer >= m_anim_framelength)
1220 m_anim_timer -= m_anim_framelength;
1222 if(m_anim_frame >= m_anim_num_frames)
1228 if(m_reset_textures_timer >= 0)
1230 m_reset_textures_timer -= dtime;
1231 if(m_reset_textures_timer <= 0) {
1232 m_reset_textures_timer = -1;
1233 updateTextures(m_previous_texture_modifier);
1236 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1238 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1242 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1243 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1245 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1246 + m_prop.automatic_face_movement_dir_offset;
1247 float max_rotation_delta =
1248 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1250 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
1251 (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
1253 m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
1255 m_yaw = optimal_yaw;
1261 void GenericCAO::updateTexturePos()
1265 scene::ICameraSceneNode* camera =
1266 m_spritenode->getSceneManager()->getActiveCamera();
1269 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1270 - camera->getAbsolutePosition();
1271 cam_to_entity.normalize();
1273 int row = m_tx_basepos.Y;
1274 int col = m_tx_basepos.X;
1276 if(m_tx_select_horiz_by_yawpitch)
1278 if(cam_to_entity.Y > 0.75)
1280 else if(cam_to_entity.Y < -0.75)
1284 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1285 float dir = mob_dir - m_yaw;
1286 dir = wrapDegrees_180(dir);
1287 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1288 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1290 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1292 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1294 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1301 // Animation goes downwards
1302 row += m_anim_frame;
1304 float txs = m_tx_size.X;
1305 float tys = m_tx_size.Y;
1306 setBillboardTextureMatrix(m_spritenode,
1307 txs, tys, col, row);
1311 void GenericCAO::updateTextures(std::string mod)
1313 ITextureSource *tsrc = m_client->tsrc();
1315 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1316 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1317 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1319 m_previous_texture_modifier = m_current_texture_modifier;
1320 m_current_texture_modifier = mod;
1324 if(m_prop.visual == "sprite")
1326 std::string texturestring = "unknown_node.png";
1327 if(m_prop.textures.size() >= 1)
1328 texturestring = m_prop.textures[0];
1329 texturestring += mod;
1330 m_spritenode->setMaterialTexture(0,
1331 tsrc->getTextureForMesh(texturestring));
1333 // This allows setting per-material colors. However, until a real lighting
1334 // system is added, the code below will have no effect. Once MineTest
1335 // has directional lighting, it should work automatically.
1336 if(m_prop.colors.size() >= 1)
1338 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1339 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1340 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1343 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1344 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1345 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1348 if(m_animated_meshnode)
1350 if(m_prop.visual == "mesh")
1352 for (u32 i = 0; i < m_prop.textures.size() &&
1353 i < m_animated_meshnode->getMaterialCount(); ++i)
1355 std::string texturestring = m_prop.textures[i];
1356 if(texturestring == "")
1357 continue; // Empty texture string means don't modify that material
1358 texturestring += mod;
1359 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1362 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1366 // Set material flags and texture
1367 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1368 material.TextureLayer[0].Texture = texture;
1369 material.setFlag(video::EMF_LIGHTING, false);
1370 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1372 m_animated_meshnode->getMaterial(i)
1373 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1374 m_animated_meshnode->getMaterial(i)
1375 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1376 m_animated_meshnode->getMaterial(i)
1377 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1379 for (u32 i = 0; i < m_prop.colors.size() &&
1380 i < m_animated_meshnode->getMaterialCount(); ++i)
1382 // This allows setting per-material colors. However, until a real lighting
1383 // system is added, the code below will have no effect. Once MineTest
1384 // has directional lighting, it should work automatically.
1385 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1386 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1387 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1393 if(m_prop.visual == "cube")
1395 for (u32 i = 0; i < 6; ++i)
1397 std::string texturestring = "unknown_node.png";
1398 if(m_prop.textures.size() > i)
1399 texturestring = m_prop.textures[i];
1400 texturestring += mod;
1403 // Set material flags and texture
1404 video::SMaterial& material = m_meshnode->getMaterial(i);
1405 material.setFlag(video::EMF_LIGHTING, false);
1406 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1407 material.setTexture(0,
1408 tsrc->getTextureForMesh(texturestring));
1409 material.getTextureMatrix(0).makeIdentity();
1411 // This allows setting per-material colors. However, until a real lighting
1412 // system is added, the code below will have no effect. Once MineTest
1413 // has directional lighting, it should work automatically.
1414 if(m_prop.colors.size() > i)
1416 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1417 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1418 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1421 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1422 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1423 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1426 else if(m_prop.visual == "upright_sprite")
1428 scene::IMesh *mesh = m_meshnode->getMesh();
1430 std::string tname = "unknown_object.png";
1431 if(m_prop.textures.size() >= 1)
1432 tname = m_prop.textures[0];
1434 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1435 buf->getMaterial().setTexture(0,
1436 tsrc->getTextureForMesh(tname));
1438 // This allows setting per-material colors. However, until a real lighting
1439 // system is added, the code below will have no effect. Once MineTest
1440 // has directional lighting, it should work automatically.
1441 if(m_prop.colors.size() >= 1)
1443 buf->getMaterial().AmbientColor = m_prop.colors[0];
1444 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1445 buf->getMaterial().SpecularColor = m_prop.colors[0];
1448 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1449 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1450 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1453 std::string tname = "unknown_object.png";
1454 if(m_prop.textures.size() >= 2)
1455 tname = m_prop.textures[1];
1456 else if(m_prop.textures.size() >= 1)
1457 tname = m_prop.textures[0];
1459 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1460 buf->getMaterial().setTexture(0,
1461 tsrc->getTextureForMesh(tname));
1463 // This allows setting per-material colors. However, until a real lighting
1464 // system is added, the code below will have no effect. Once MineTest
1465 // has directional lighting, it should work automatically.
1466 if(m_prop.colors.size() >= 2)
1468 buf->getMaterial().AmbientColor = m_prop.colors[1];
1469 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1470 buf->getMaterial().SpecularColor = m_prop.colors[1];
1472 else if(m_prop.colors.size() >= 1)
1474 buf->getMaterial().AmbientColor = m_prop.colors[0];
1475 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1476 buf->getMaterial().SpecularColor = m_prop.colors[0];
1479 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1480 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1481 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1487 void GenericCAO::updateAnimation()
1489 if(m_animated_meshnode == NULL)
1492 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1493 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1494 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1495 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1496 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1497 m_animated_meshnode->setTransitionTime(m_animation_blend);
1498 // Requires Irrlicht 1.8 or greater
1499 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1500 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1501 m_animated_meshnode->setLoopMode(m_animation_loop);
1505 void GenericCAO::updateBonePosition()
1507 if(m_bone_position.empty() || m_animated_meshnode == NULL)
1510 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1511 for(UNORDERED_MAP<std::string, core::vector2d<v3f> >::const_iterator
1512 ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1513 std::string bone_name = (*ii).first;
1514 v3f bone_pos = (*ii).second.X;
1515 v3f bone_rot = (*ii).second.Y;
1516 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1519 bone->setPosition(bone_pos);
1520 bone->setRotation(bone_rot);
1525 void GenericCAO::updateAttachments()
1528 if (getParent() == NULL) { // Detach or don't attach
1529 scene::ISceneNode *node = getSceneNode();
1531 v3f old_position = node->getAbsolutePosition();
1532 v3f old_rotation = node->getRotation();
1533 node->setParent(m_smgr->getRootSceneNode());
1534 node->setPosition(old_position);
1535 node->setRotation(old_rotation);
1536 node->updateAbsolutePosition();
1538 if (m_is_local_player) {
1539 LocalPlayer *player = m_env->getLocalPlayer();
1540 player->isAttached = false;
1545 scene::ISceneNode *my_node = getSceneNode();
1547 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1548 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1549 getParent()->getAnimatedMeshSceneNode();
1550 if (parent_animated_mesh_node && m_attachment_bone != "") {
1551 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1554 if (my_node && parent_node) {
1555 my_node->setParent(parent_node);
1556 my_node->setPosition(m_attachment_position);
1557 my_node->setRotation(m_attachment_rotation);
1558 my_node->updateAbsolutePosition();
1560 if (m_is_local_player) {
1561 LocalPlayer *player = m_env->getLocalPlayer();
1562 player->isAttached = true;
1567 void GenericCAO::processMessage(const std::string &data)
1569 //infostream<<"GenericCAO: Got message"<<std::endl;
1570 std::istringstream is(data, std::ios::binary);
1572 u8 cmd = readU8(is);
1573 if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1574 m_prop = gob_read_set_properties(is);
1576 m_selection_box = m_prop.collisionbox;
1577 m_selection_box.MinEdge *= BS;
1578 m_selection_box.MaxEdge *= BS;
1580 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1581 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1583 if(!m_initial_tx_basepos_set){
1584 m_initial_tx_basepos_set = true;
1585 m_tx_basepos = m_prop.initial_sprite_basepos;
1587 if (m_is_local_player) {
1588 LocalPlayer *player = m_env->getLocalPlayer();
1589 player->makes_footstep_sound = m_prop.makes_footstep_sound;
1592 if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
1593 m_prop.nametag = m_name;
1596 } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1597 // Not sent by the server if this object is an attachment.
1598 // We might however get here if the server notices the object being detached before the client.
1599 m_position = readV3F1000(is);
1600 m_velocity = readV3F1000(is);
1601 m_acceleration = readV3F1000(is);
1602 if(fabs(m_prop.automatic_rotate) < 0.001)
1603 m_yaw = readF1000(is);
1606 bool do_interpolate = readU8(is);
1607 bool is_end_position = readU8(is);
1608 float update_interval = readF1000(is);
1610 // Place us a bit higher if we're physical, to not sink into
1611 // the ground due to sucky collision detection...
1613 m_position += v3f(0,0.002,0);
1615 if(getParent() != NULL) // Just in case
1620 if(!m_prop.physical)
1621 pos_translator.update(m_position, is_end_position, update_interval);
1623 pos_translator.init(m_position);
1626 } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1627 std::string mod = deSerializeString(is);
1629 // immediatly reset a engine issued texture modifier if a mod sends a different one
1630 if (m_reset_textures_timer > 0) {
1631 m_reset_textures_timer = -1;
1632 updateTextures(m_previous_texture_modifier);
1634 updateTextures(mod);
1635 } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1636 v2s16 p = readV2S16(is);
1637 int num_frames = readU16(is);
1638 float framelength = readF1000(is);
1639 bool select_horiz_by_yawpitch = readU8(is);
1642 m_anim_num_frames = num_frames;
1643 m_anim_framelength = framelength;
1644 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1647 } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1648 float override_speed = readF1000(is);
1649 float override_jump = readF1000(is);
1650 float override_gravity = readF1000(is);
1651 // these are sent inverted so we get true when the server sends nothing
1652 bool sneak = !readU8(is);
1653 bool sneak_glitch = !readU8(is);
1654 bool new_move = !readU8(is);
1657 if(m_is_local_player)
1659 LocalPlayer *player = m_env->getLocalPlayer();
1660 player->physics_override_speed = override_speed;
1661 player->physics_override_jump = override_jump;
1662 player->physics_override_gravity = override_gravity;
1663 player->physics_override_sneak = sneak;
1664 player->physics_override_sneak_glitch = sneak_glitch;
1665 player->physics_override_new_move = new_move;
1667 } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1668 // TODO: change frames send as v2s32 value
1669 v2f range = readV2F1000(is);
1670 if (!m_is_local_player) {
1671 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1672 m_animation_speed = readF1000(is);
1673 m_animation_blend = readF1000(is);
1674 // these are sent inverted so we get true when the server sends nothing
1675 m_animation_loop = !readU8(is);
1678 LocalPlayer *player = m_env->getLocalPlayer();
1679 if(player->last_animation == NO_ANIM)
1681 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1682 m_animation_speed = readF1000(is);
1683 m_animation_blend = readF1000(is);
1684 // these are sent inverted so we get true when the server sends nothing
1685 m_animation_loop = !readU8(is);
1687 // update animation only if local animations present
1688 // and received animation is unknown (except idle animation)
1689 bool is_known = false;
1690 for (int i = 1;i<4;i++)
1692 if(m_animation_range.Y == player->local_animations[i].Y)
1696 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1701 } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1702 std::string bone = deSerializeString(is);
1703 v3f position = readV3F1000(is);
1704 v3f rotation = readV3F1000(is);
1705 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1707 updateBonePosition();
1708 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1709 u16 parentID = readS16(is);
1710 u16 oldparent = m_env->attachement_parent_ids[getId()];
1712 m_children.erase(std::remove(m_children.begin(), m_children.end(),
1713 getId()), m_children.end());
1715 m_env->attachement_parent_ids[getId()] = parentID;
1716 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1719 parentobj->m_children.push_back(getId());
1722 m_attachment_bone = deSerializeString(is);
1723 m_attachment_position = readV3F1000(is);
1724 m_attachment_rotation = readV3F1000(is);
1726 // localplayer itself can't be attached to localplayer
1727 if (!m_is_local_player) {
1728 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1729 // Objects attached to the local player should be hidden by default
1730 m_is_visible = !m_attached_to_local;
1733 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(m_current_texture_modifier + "^[brighten");
1762 } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1763 m_armor_groups.clear();
1764 int armor_groups_size = readU16(is);
1765 for(int i=0; i<armor_groups_size; i++)
1767 std::string name = deSerializeString(is);
1768 int rating = readS16(is);
1769 m_armor_groups[name] = rating;
1771 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1772 // Deprecated, for backwards compatibility only.
1773 readU8(is); // version
1774 m_prop.nametag_color = readARGB8(is);
1775 if (m_nametag != NULL) {
1776 m_nametag->nametag_color = m_prop.nametag_color;
1778 } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1779 u16 child_id = readU16(is);
1780 u8 type = readU8(is);
1782 if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
1783 childobj->processInitData(deSerializeLongString(is));
1785 m_env->addActiveObject(child_id, type, deSerializeLongString(is));
1788 warningstream << FUNCTION_NAME
1789 << ": unknown command or outdated client \""
1790 << +cmd << "\"" << std::endl;
1794 /* \pre punchitem != NULL
1796 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1797 float time_from_last_punch)
1799 assert(punchitem); // pre-condition
1800 const ToolCapabilities *toolcap =
1801 &punchitem->getToolCapabilities(m_client->idef());
1802 PunchDamageResult result = getPunchDamage(
1806 time_from_last_punch);
1808 if(result.did_punch && result.damage != 0)
1810 if(result.damage < m_hp)
1812 m_hp -= result.damage;
1815 // TODO: Execute defined fast response
1816 // As there is no definition, make a smoke puff
1817 ClientSimpleObject *simple = createSmokePuff(
1818 m_smgr, m_env, m_position,
1819 m_prop.visual_size * BS);
1820 m_env->addSimpleObject(simple);
1822 // TODO: Execute defined fast response
1823 // Flashing shall suffice as there is no definition
1824 m_reset_textures_timer = 0.05;
1825 if(result.damage >= 2)
1826 m_reset_textures_timer += 0.05 * result.damage;
1827 updateTextures(m_current_texture_modifier + "^[brighten");
1833 std::string GenericCAO::debugInfoText()
1835 std::ostringstream os(std::ios::binary);
1836 os<<"GenericCAO hp="<<m_hp<<"\n";
1838 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1839 i != m_armor_groups.end(); ++i)
1841 os<<i->first<<"="<<i->second<<", ";
1848 GenericCAO proto_GenericCAO(NULL, NULL);