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"
49 struct ToolCapabilities;
51 UNORDERED_MAP<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
53 SmoothTranslator::SmoothTranslator():
63 void SmoothTranslator::init(v3f vect)
70 anim_time_counter = 0;
74 void SmoothTranslator::sharpen()
79 void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
81 aim_is_end = is_end_position;
84 if(update_interval > 0)
86 anim_time = update_interval;
88 if(anim_time < 0.001 || anim_time > 1.0)
89 anim_time = anim_time_counter;
91 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
93 anim_time_counter = 0;
97 void SmoothTranslator::translate(f32 dtime)
99 anim_time_counter = anim_time_counter + dtime;
100 anim_counter = anim_counter + dtime;
101 v3f vect_move = vect_aim - vect_old;
103 if(anim_time > 0.001)
104 moveratio = anim_time_counter / anim_time;
105 // Move a bit less than should, to avoid oscillation
106 moveratio = moveratio * 0.8;
107 float move_end = 1.5;
110 if(moveratio > move_end)
111 moveratio = move_end;
112 vect_show = vect_old + vect_move * moveratio;
115 bool SmoothTranslator::is_moving()
117 return ((anim_time_counter / anim_time) < 1.4);
124 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
125 float txs, float tys, int col, int row)
127 video::SMaterial& material = bill->getMaterial(0);
128 core::matrix4& matrix = material.getTextureMatrix(0);
129 matrix.setTextureTranslate(txs*col, tys*row);
130 matrix.setTextureScale(txs, tys);
137 class TestCAO : public ClientActiveObject
140 TestCAO(Client *client, ClientEnvironment *env);
143 ActiveObjectType getType() const
145 return ACTIVEOBJECT_TYPE_TEST;
148 static ClientActiveObject* create(Client *client, ClientEnvironment *env);
150 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
151 IrrlichtDevice *irr);
152 void removeFromScene(bool permanent);
153 void updateLight(u8 light_at_pos);
154 v3s16 getLightPosition();
155 void updateNodePos();
157 void step(float dtime, ClientEnvironment *env);
159 void processMessage(const std::string &data);
161 bool getCollisionBox(aabb3f *toset) const { return false; }
163 scene::IMeshSceneNode *m_node;
168 TestCAO proto_TestCAO(NULL, NULL);
170 TestCAO::TestCAO(Client *client, ClientEnvironment *env):
171 ClientActiveObject(0, client, env),
173 m_position(v3f(0,10*BS,0))
175 ClientActiveObject::registerType(getType(), create);
182 ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
184 return new TestCAO(client, env);
187 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
193 //video::IVideoDriver* driver = smgr->getVideoDriver();
195 scene::SMesh *mesh = new scene::SMesh();
196 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
197 video::SColor c(255,255,255,255);
198 video::S3DVertex vertices[4] =
200 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
201 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
202 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
203 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
205 u16 indices[] = {0,1,2,2,3,0};
206 buf->append(vertices, 4, indices, 6);
208 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
209 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
210 buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
211 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
212 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
213 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
215 mesh->addMeshBuffer(buf);
217 m_node = smgr->addMeshSceneNode(mesh, NULL);
222 void TestCAO::removeFromScene(bool permanent)
231 void TestCAO::updateLight(u8 light_at_pos)
235 v3s16 TestCAO::getLightPosition()
237 return floatToInt(m_position, BS);
240 void TestCAO::updateNodePos()
245 m_node->setPosition(m_position);
246 //m_node->setRotation(v3f(0, 45, 0));
249 void TestCAO::step(float dtime, ClientEnvironment *env)
253 v3f rot = m_node->getRotation();
254 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
255 rot.Y += dtime * 180;
256 m_node->setRotation(rot);
260 void TestCAO::processMessage(const std::string &data)
262 infostream<<"TestCAO: Got data: "<<data<<std::endl;
263 std::istringstream is(data, std::ios::binary);
281 class ItemCAO : public ClientActiveObject
284 ItemCAO(Client *client, ClientEnvironment *env);
287 ActiveObjectType getType() const
289 return ACTIVEOBJECT_TYPE_ITEM;
292 static ClientActiveObject* create(Client *client, ClientEnvironment *env);
294 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
295 IrrlichtDevice *irr);
296 void removeFromScene(bool permanent);
297 void updateLight(u8 light_at_pos);
298 v3s16 getLightPosition();
299 void updateNodePos();
300 void updateInfoText();
301 void updateTexture();
303 void step(float dtime, ClientEnvironment *env);
305 void processMessage(const std::string &data);
307 void initialize(const std::string &data);
309 aabb3f *getSelectionBox()
310 {return &m_selection_box;}
313 inline float getYaw() const
315 std::string infoText()
318 bool getCollisionBox(aabb3f *toset) const { return false; }
320 aabb3f m_selection_box;
321 scene::IMeshSceneNode *m_node;
323 std::string m_itemstring;
324 std::string m_infotext;
327 #include "inventory.h"
330 ItemCAO proto_ItemCAO(NULL, NULL);
332 ItemCAO::ItemCAO(Client *client, ClientEnvironment *env):
333 ClientActiveObject(0, client, env),
334 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
336 m_position(v3f(0,10*BS,0))
340 ClientActiveObject::registerType(getType(), create);
348 ClientActiveObject* ItemCAO::create(Client *client, ClientEnvironment *env)
350 return new ItemCAO(client, env);
353 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
359 //video::IVideoDriver* driver = smgr->getVideoDriver();
361 scene::SMesh *mesh = new scene::SMesh();
362 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
363 video::SColor c(255,255,255,255);
364 video::S3DVertex vertices[4] =
366 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
367 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
368 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
369 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
370 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
371 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
372 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
373 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
375 u16 indices[] = {0,1,2,2,3,0};
376 buf->append(vertices, 4, indices, 6);
378 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
379 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
380 // Initialize with a generated placeholder texture
381 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
382 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
383 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
384 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
386 mesh->addMeshBuffer(buf);
388 m_node = smgr->addMeshSceneNode(mesh, NULL);
399 void ItemCAO::removeFromScene(bool permanent)
408 void ItemCAO::updateLight(u8 light_at_pos)
413 u8 li = decode_light(light_at_pos);
414 video::SColor color(255,li,li,li);
415 setMeshColor(m_node->getMesh(), color);
418 v3s16 ItemCAO::getLightPosition()
420 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
423 void ItemCAO::updateNodePos()
428 m_node->setPosition(m_position);
431 void ItemCAO::updateInfoText()
434 IItemDefManager *idef = m_client->idef();
436 item.deSerialize(m_itemstring, idef);
437 if(item.isKnown(idef))
438 m_infotext = item.getDefinition(idef).description;
440 m_infotext = "Unknown item: '" + m_itemstring + "'";
442 m_infotext += " (" + itos(item.count) + ")";
444 catch(SerializationError &e)
446 m_infotext = "Unknown item: '" + m_itemstring + "'";
450 void ItemCAO::updateTexture()
455 // Create an inventory item to see what is its image
456 std::istringstream is(m_itemstring, std::ios_base::binary);
457 video::ITexture *texture = NULL;
459 IItemDefManager *idef = m_client->idef();
461 item.deSerialize(is, idef);
462 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_client);
464 catch(SerializationError &e)
466 warningstream<<FUNCTION_NAME
467 <<": error deSerializing itemstring \""
468 <<m_itemstring<<std::endl;
471 // Set meshbuffer texture
472 m_node->getMaterial(0).setTexture(0, texture);
476 void ItemCAO::step(float dtime, ClientEnvironment *env)
480 /*v3f rot = m_node->getRotation();
481 rot.Y += dtime * 120;
482 m_node->setRotation(rot);*/
483 LocalPlayer *player = env->getLocalPlayer();
485 v3f rot = m_node->getRotation();
486 rot.Y = 180.0 - (player->getYaw());
487 m_node->setRotation(rot);
491 void ItemCAO::processMessage(const std::string &data)
493 //infostream<<"ItemCAO: Got message"<<std::endl;
494 std::istringstream is(data, std::ios::binary);
500 m_position = readV3F1000(is);
506 m_itemstring = deSerializeString(is);
512 void ItemCAO::initialize(const std::string &data)
514 infostream<<"ItemCAO: Got init data"<<std::endl;
517 std::istringstream is(data, std::ios::binary);
519 u8 version = readU8(is);
524 m_position = readV3F1000(is);
526 m_itemstring = deSerializeString(is);
537 #include "genericobject.h"
539 GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
540 ClientActiveObject(0, client, env),
543 m_is_local_player(false),
548 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
550 m_animated_meshnode(NULL),
551 m_wield_meshnode(NULL),
554 m_position(v3f(0,10*BS,0)),
555 m_velocity(v3f(0,0,0)),
556 m_acceleration(v3f(0,0,0)),
561 m_initial_tx_basepos_set(false),
562 m_tx_select_horiz_by_yawpitch(false),
563 m_animation_range(v2s32(0,0)),
564 m_animation_speed(15),
565 m_animation_blend(0),
566 m_animation_loop(true),
567 m_bone_position(UNORDERED_MAP<std::string, core::vector2d<v3f> >()),
568 m_attachment_bone(""),
569 m_attachment_position(v3f(0,0,0)),
570 m_attachment_rotation(v3f(0,0,0)),
571 m_attached_to_local(false),
573 m_anim_num_frames(1),
574 m_anim_framelength(0.2),
576 m_reset_textures_timer(-1),
577 m_previous_texture_modifier(""),
578 m_current_texture_modifier(""),
579 m_visuals_expired(false),
580 m_step_distance_counter(0),
584 if (client == NULL) {
585 ClientActiveObject::registerType(getType(), create);
591 bool GenericCAO::getCollisionBox(aabb3f *toset) const
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() const
610 return m_prop.collideWithObjects;
613 void GenericCAO::initialize(const std::string &data)
615 infostream<<"GenericCAO: Got init data"<<std::endl;
616 processInitData(data);
619 // Check if it's the current player
620 LocalPlayer *player = m_env->getLocalPlayer();
621 if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
622 m_is_local_player = true;
623 m_is_visible = false;
624 player->setCAO(this);
626 m_env->addPlayerName(m_name.c_str());
630 void GenericCAO::processInitData(const std::string &data)
632 std::istringstream is(data, std::ios::binary);
633 int num_messages = 0;
635 u8 version = readU8(is);
637 if (version == 1) { // In PROTOCOL_VERSION 14
638 m_name = deSerializeString(is);
639 m_is_player = readU8(is);
641 m_position = readV3F1000(is);
642 m_yaw = readF1000(is);
644 num_messages = readU8(is);
645 } else if (version == 0) { // In PROTOCOL_VERSION 13
646 m_name = deSerializeString(is);
647 m_is_player = readU8(is);
648 m_position = readV3F1000(is);
649 m_yaw = readF1000(is);
651 num_messages = readU8(is);
653 errorstream<<"GenericCAO: Unsupported init data version"
658 for (int i = 0; i < num_messages; i++) {
659 std::string message = deSerializeLongString(is);
660 processMessage(message);
663 pos_translator.init(m_position);
667 GenericCAO::~GenericCAO()
670 m_env->removePlayerName(m_name.c_str());
672 removeFromScene(true);
675 aabb3f *GenericCAO::getSelectionBox()
677 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
679 return &m_selection_box;
682 v3f GenericCAO::getPosition()
684 if (getParent() != NULL) {
685 scene::ISceneNode *node = getSceneNode();
687 return node->getAbsolutePosition();
691 return pos_translator.vect_show;
694 scene::ISceneNode* GenericCAO::getSceneNode()
698 } else if (m_animated_meshnode) {
699 return m_animated_meshnode;
700 } else if (m_wield_meshnode) {
701 return m_wield_meshnode;
702 } else if (m_spritenode) {
708 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
713 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
715 return m_animated_meshnode;
718 WieldMeshSceneNode* GenericCAO::getWieldMeshSceneNode()
720 return m_wield_meshnode;
723 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
728 void GenericCAO::setChildrenVisible(bool toset)
730 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
731 GenericCAO *obj = m_env->getGenericCAO(m_children[i]);
733 obj->setVisible(toset);
738 void GenericCAO::setAttachments()
743 ClientActiveObject* GenericCAO::getParent()
745 ClientActiveObject *obj = NULL;
747 u16 attached_id = m_env->attachement_parent_ids[getId()];
749 if ((attached_id != 0) &&
750 (attached_id != getId())) {
751 obj = m_env->getActiveObject(attached_id);
756 void GenericCAO::removeFromScene(bool permanent)
758 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
759 if((m_env != NULL) && (permanent))
761 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
762 u16 ci = m_children[i];
763 if (m_env->attachement_parent_ids[ci] == getId()) {
764 m_env->attachement_parent_ids[ci] = 0;
768 m_env->attachement_parent_ids[getId()] = 0;
770 LocalPlayer* player = m_env->getLocalPlayer();
771 if (this == player->parent) {
772 player->parent = NULL;
773 player->isAttached = false;
778 m_meshnode->remove();
781 } else if (m_animated_meshnode) {
782 m_animated_meshnode->remove();
783 m_animated_meshnode->drop();
784 m_animated_meshnode = NULL;
785 } else if (m_wield_meshnode) {
786 m_wield_meshnode->remove();
787 m_wield_meshnode->drop();
788 m_wield_meshnode = NULL;
789 } else if (m_spritenode) {
790 m_spritenode->remove();
791 m_spritenode->drop();
796 m_client->getCamera()->removeNametag(m_nametag);
801 void GenericCAO::addToScene(scene::ISceneManager *smgr,
802 ITextureSource *tsrc, IrrlichtDevice *irr)
807 if (getSceneNode() != NULL) {
811 m_visuals_expired = false;
813 if (!m_prop.is_visible) {
817 if (m_prop.visual == "sprite") {
818 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
819 m_spritenode = smgr->addBillboardSceneNode(
820 NULL, v2f(1, 1), v3f(0,0,0), -1);
821 m_spritenode->grab();
822 m_spritenode->setMaterialTexture(0,
823 tsrc->getTextureForMesh("unknown_node.png"));
824 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
825 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
826 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
827 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
828 u8 li = m_last_light;
829 m_spritenode->setColor(video::SColor(255,li,li,li));
830 m_spritenode->setSize(m_prop.visual_size*BS);
832 const float txs = 1.0 / 1;
833 const float tys = 1.0 / 1;
834 setBillboardTextureMatrix(m_spritenode,
837 } else if (m_prop.visual == "upright_sprite") {
838 scene::SMesh *mesh = new scene::SMesh();
839 double dx = BS * m_prop.visual_size.X / 2;
840 double dy = BS * m_prop.visual_size.Y / 2;
841 u8 li = m_last_light;
842 video::SColor c(255, li, li, li);
845 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
846 video::S3DVertex vertices[4] = {
847 video::S3DVertex(-dx, -dy, 0, 0,0,0, c, 1,1),
848 video::S3DVertex( dx, -dy, 0, 0,0,0, c, 0,1),
849 video::S3DVertex( dx, dy, 0, 0,0,0, c, 0,0),
850 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 1,0),
852 u16 indices[] = {0,1,2,2,3,0};
853 buf->append(vertices, 4, indices, 6);
855 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
856 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
857 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
858 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
860 mesh->addMeshBuffer(buf);
864 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
865 video::S3DVertex vertices[4] = {
866 video::S3DVertex( dx,-dy, 0, 0,0,0, c, 1,1),
867 video::S3DVertex(-dx,-dy, 0, 0,0,0, c, 0,1),
868 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 0,0),
869 video::S3DVertex( dx, dy, 0, 0,0,0, c, 1,0),
871 u16 indices[] = {0,1,2,2,3,0};
872 buf->append(vertices, 4, indices, 6);
874 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
875 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
876 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
877 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
879 mesh->addMeshBuffer(buf);
882 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
885 // Set it to use the materials of the meshbuffers directly.
886 // This is needed for changing the texture in the future
887 m_meshnode->setReadOnlyMaterials(true);
889 else if(m_prop.visual == "cube") {
890 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
891 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
892 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
896 m_meshnode->setScale(v3f(m_prop.visual_size.X,
897 m_prop.visual_size.Y,
898 m_prop.visual_size.X));
899 u8 li = m_last_light;
900 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
902 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
903 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
904 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
905 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
907 else if(m_prop.visual == "mesh") {
908 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
909 scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh);
912 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
913 m_animated_meshnode->grab();
914 mesh->drop(); // The scene node took hold of it
915 m_animated_meshnode->animateJoints(); // Needed for some animations
916 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
917 m_prop.visual_size.Y,
918 m_prop.visual_size.X));
919 u8 li = m_last_light;
920 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
922 bool backface_culling = m_prop.backface_culling;
924 backface_culling = false;
926 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
927 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
928 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
929 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
930 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
933 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
935 else if(m_prop.visual == "wielditem") {
936 infostream<<"GenericCAO::addToScene(): wielditem"<<std::endl;
937 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
938 if(m_prop.textures.size() >= 1){
939 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
940 IItemDefManager *idef = m_client->idef();
941 ItemStack item(m_prop.textures[0], 1, 0, idef);
943 m_wield_meshnode = new WieldMeshSceneNode(
944 smgr->getRootSceneNode(), smgr, -1);
945 m_wield_meshnode->setItem(item, m_client);
947 m_wield_meshnode->setScale(v3f(m_prop.visual_size.X/2,
948 m_prop.visual_size.Y/2,
949 m_prop.visual_size.X/2));
950 u8 li = m_last_light;
951 m_wield_meshnode->setColor(video::SColor(255,li,li,li));
954 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
955 <<"\" not supported"<<std::endl;
958 /* don't update while punch texture modifier is active */
959 if (m_reset_textures_timer < 0)
960 updateTextures(m_current_texture_modifier);
962 scene::ISceneNode *node = getSceneNode();
963 if (node && m_prop.nametag != "" && !m_is_local_player) {
965 m_nametag = m_client->getCamera()->addNametag(node,
966 m_prop.nametag, m_prop.nametag_color);
971 updateBonePosition();
975 void GenericCAO::updateLight(u8 light_at_pos)
977 // Don't update light of attached one
978 if (getParent() != NULL) {
982 updateLightNoCheck(light_at_pos);
984 // Update light of all children
985 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
986 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
988 obj->updateLightNoCheck(light_at_pos);
993 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
995 u8 li = decode_light(light_at_pos);
996 if (li != m_last_light) {
998 video::SColor color(255,li,li,li);
1000 setMeshColor(m_meshnode->getMesh(), color);
1001 } else if (m_animated_meshnode) {
1002 setMeshColor(m_animated_meshnode->getMesh(), color);
1003 } else if (m_wield_meshnode) {
1004 m_wield_meshnode->setColor(color);
1005 } else if (m_spritenode) {
1006 m_spritenode->setColor(color);
1011 v3s16 GenericCAO::getLightPosition()
1013 return floatToInt(m_position, BS);
1016 void GenericCAO::updateNodePos()
1018 if (getParent() != NULL)
1021 scene::ISceneNode *node = getSceneNode();
1024 v3s16 camera_offset = m_env->getCameraOffset();
1025 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
1026 if (node != m_spritenode) { // rotate if not a sprite
1027 v3f rot = node->getRotation();
1029 node->setRotation(rot);
1034 void GenericCAO::step(float dtime, ClientEnvironment *env)
1036 // Handel model of local player instantly to prevent lags
1037 if(m_is_local_player)
1039 LocalPlayer *player = m_env->getLocalPlayer();
1043 int old_anim = player->last_animation;
1044 float old_anim_speed = player->last_animation_speed;
1045 m_position = player->getPosition() + v3f(0,BS,0);
1046 m_velocity = v3f(0,0,0);
1047 m_acceleration = v3f(0,0,0);
1048 pos_translator.vect_show = m_position;
1049 m_yaw = player->getYaw();
1050 PlayerControl controls = player->getPlayerControl();
1052 bool walking = false;
1053 if (controls.up || controls.down || controls.left || controls.right ||
1054 controls.forw_move_joystick_axis != 0.f ||
1055 controls.sidew_move_joystick_axis != 0.f)
1058 f32 new_speed = player->local_animation_speed;
1059 v2s32 new_anim = v2s32(0,0);
1060 bool allow_update = false;
1062 // increase speed if using fast or flying fast
1063 if((g_settings->getBool("fast_move") &&
1064 m_client->checkLocalPrivilege("fast")) &&
1066 (!player->touching_ground &&
1067 g_settings->getBool("free_move") &&
1068 m_client->checkLocalPrivilege("fly"))))
1070 // slowdown speed if sneeking
1071 if(controls.sneak && walking)
1074 if(walking && (controls.LMB || controls.RMB))
1076 new_anim = player->local_animations[3];
1077 player->last_animation = WD_ANIM;
1078 } else if(walking) {
1079 new_anim = player->local_animations[1];
1080 player->last_animation = WALK_ANIM;
1081 } else if(controls.LMB || controls.RMB) {
1082 new_anim = player->local_animations[2];
1083 player->last_animation = DIG_ANIM;
1086 // Apply animations if input detected and not attached
1087 // or set idle animation
1088 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1090 allow_update = true;
1091 m_animation_range = new_anim;
1092 m_animation_speed = new_speed;
1093 player->last_animation_speed = m_animation_speed;
1095 player->last_animation = NO_ANIM;
1097 if (old_anim != NO_ANIM)
1099 m_animation_range = player->local_animations[0];
1104 // Update local player animations
1105 if ((player->last_animation != old_anim ||
1106 m_animation_speed != old_anim_speed) &&
1107 player->last_animation != NO_ANIM && allow_update)
1113 if(m_visuals_expired && m_smgr && m_irr){
1114 m_visuals_expired = false;
1116 // Attachments, part 1: All attached objects must be unparented first,
1117 // or Irrlicht causes a segmentation fault
1118 for(std::vector<u16>::iterator ci = m_children.begin();
1119 ci != m_children.end();)
1121 if (m_env->attachement_parent_ids[*ci] != getId()) {
1122 ci = m_children.erase(ci);
1125 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1127 scene::ISceneNode *child_node = obj->getSceneNode();
1129 child_node->setParent(m_smgr->getRootSceneNode());
1134 removeFromScene(false);
1135 addToScene(m_smgr, m_client->tsrc(), m_irr);
1137 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1138 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1139 // Get the object of the child
1140 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1142 obj->setAttachments();
1146 // Make sure m_is_visible is always applied
1147 scene::ISceneNode *node = getSceneNode();
1149 node->setVisible(m_is_visible);
1151 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1153 // Set these for later
1154 m_position = getPosition();
1155 m_velocity = v3f(0,0,0);
1156 m_acceleration = v3f(0,0,0);
1157 pos_translator.vect_show = m_position;
1159 if(m_is_local_player) // Update local player attachment position
1161 LocalPlayer *player = m_env->getLocalPlayer();
1162 player->overridePosition = getParent()->getPosition();
1163 m_env->getLocalPlayer()->parent = getParent();
1166 v3f lastpos = pos_translator.vect_show;
1170 aabb3f box = m_prop.collisionbox;
1173 collisionMoveResult moveresult;
1174 f32 pos_max_d = BS*0.125; // Distance per iteration
1175 v3f p_pos = m_position;
1176 v3f p_velocity = m_velocity;
1177 moveresult = collisionMoveSimple(env,env->getGameDef(),
1178 pos_max_d, box, m_prop.stepheight, dtime,
1179 &p_pos, &p_velocity, m_acceleration,
1180 this, m_prop.collideWithObjects);
1183 m_velocity = p_velocity;
1185 bool is_end_position = moveresult.collides;
1186 pos_translator.update(m_position, is_end_position, dtime);
1187 pos_translator.translate(dtime);
1190 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1191 m_velocity += dtime * m_acceleration;
1192 pos_translator.update(m_position, pos_translator.aim_is_end,
1193 pos_translator.anim_time);
1194 pos_translator.translate(dtime);
1198 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1199 m_step_distance_counter += moved;
1200 if(m_step_distance_counter > 1.5*BS)
1202 m_step_distance_counter = 0;
1203 if(!m_is_local_player && m_prop.makes_footstep_sound)
1205 INodeDefManager *ndef = m_client->ndef();
1206 v3s16 p = floatToInt(getPosition() + v3f(0,
1207 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1208 MapNode n = m_env->getMap().getNodeNoEx(p);
1209 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1210 m_client->sound()->playSoundAt(spec, false, getPosition());
1215 m_anim_timer += dtime;
1216 if(m_anim_timer >= m_anim_framelength)
1218 m_anim_timer -= m_anim_framelength;
1220 if(m_anim_frame >= m_anim_num_frames)
1226 if(m_reset_textures_timer >= 0)
1228 m_reset_textures_timer -= dtime;
1229 if(m_reset_textures_timer <= 0) {
1230 m_reset_textures_timer = -1;
1231 updateTextures(m_previous_texture_modifier);
1234 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1236 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1240 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1241 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1243 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1244 + m_prop.automatic_face_movement_dir_offset;
1245 float max_rotation_delta =
1246 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1248 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
1249 (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
1251 m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
1253 m_yaw = optimal_yaw;
1259 void GenericCAO::updateTexturePos()
1263 scene::ICameraSceneNode* camera =
1264 m_spritenode->getSceneManager()->getActiveCamera();
1267 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1268 - camera->getAbsolutePosition();
1269 cam_to_entity.normalize();
1271 int row = m_tx_basepos.Y;
1272 int col = m_tx_basepos.X;
1274 if(m_tx_select_horiz_by_yawpitch)
1276 if(cam_to_entity.Y > 0.75)
1278 else if(cam_to_entity.Y < -0.75)
1282 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1283 float dir = mob_dir - m_yaw;
1284 dir = wrapDegrees_180(dir);
1285 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1286 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1288 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1290 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1292 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1299 // Animation goes downwards
1300 row += m_anim_frame;
1302 float txs = m_tx_size.X;
1303 float tys = m_tx_size.Y;
1304 setBillboardTextureMatrix(m_spritenode,
1305 txs, tys, col, row);
1309 void GenericCAO::updateTextures(const std::string mod)
1311 ITextureSource *tsrc = m_client->tsrc();
1313 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1314 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1315 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1317 m_previous_texture_modifier = m_current_texture_modifier;
1318 m_current_texture_modifier = mod;
1322 if(m_prop.visual == "sprite")
1324 std::string texturestring = "unknown_node.png";
1325 if(m_prop.textures.size() >= 1)
1326 texturestring = m_prop.textures[0];
1327 texturestring += mod;
1328 m_spritenode->setMaterialTexture(0,
1329 tsrc->getTextureForMesh(texturestring));
1331 // This allows setting per-material colors. However, until a real lighting
1332 // system is added, the code below will have no effect. Once MineTest
1333 // has directional lighting, it should work automatically.
1334 if(m_prop.colors.size() >= 1)
1336 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1337 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1338 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1341 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1342 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1343 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1346 if(m_animated_meshnode)
1348 if(m_prop.visual == "mesh")
1350 for (u32 i = 0; i < m_prop.textures.size() &&
1351 i < m_animated_meshnode->getMaterialCount(); ++i)
1353 std::string texturestring = m_prop.textures[i];
1354 if(texturestring == "")
1355 continue; // Empty texture string means don't modify that material
1356 texturestring += mod;
1357 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1360 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1364 // Set material flags and texture
1365 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1366 material.TextureLayer[0].Texture = texture;
1367 material.setFlag(video::EMF_LIGHTING, false);
1368 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1370 m_animated_meshnode->getMaterial(i)
1371 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1372 m_animated_meshnode->getMaterial(i)
1373 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1374 m_animated_meshnode->getMaterial(i)
1375 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1377 for (u32 i = 0; i < m_prop.colors.size() &&
1378 i < m_animated_meshnode->getMaterialCount(); ++i)
1380 // This allows setting per-material colors. However, until a real lighting
1381 // system is added, the code below will have no effect. Once MineTest
1382 // has directional lighting, it should work automatically.
1383 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1384 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1385 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1391 if(m_prop.visual == "cube")
1393 for (u32 i = 0; i < 6; ++i)
1395 std::string texturestring = "unknown_node.png";
1396 if(m_prop.textures.size() > i)
1397 texturestring = m_prop.textures[i];
1398 texturestring += mod;
1401 // Set material flags and texture
1402 video::SMaterial& material = m_meshnode->getMaterial(i);
1403 material.setFlag(video::EMF_LIGHTING, false);
1404 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1405 material.setTexture(0,
1406 tsrc->getTextureForMesh(texturestring));
1407 material.getTextureMatrix(0).makeIdentity();
1409 // This allows setting per-material colors. However, until a real lighting
1410 // system is added, the code below will have no effect. Once MineTest
1411 // has directional lighting, it should work automatically.
1412 if(m_prop.colors.size() > i)
1414 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1415 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1416 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1419 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1420 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1421 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1424 else if(m_prop.visual == "upright_sprite")
1426 scene::IMesh *mesh = m_meshnode->getMesh();
1428 std::string tname = "unknown_object.png";
1429 if(m_prop.textures.size() >= 1)
1430 tname = m_prop.textures[0];
1432 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1433 buf->getMaterial().setTexture(0,
1434 tsrc->getTextureForMesh(tname));
1436 // This allows setting per-material colors. However, until a real lighting
1437 // system is added, the code below will have no effect. Once MineTest
1438 // has directional lighting, it should work automatically.
1439 if(m_prop.colors.size() >= 1)
1441 buf->getMaterial().AmbientColor = m_prop.colors[0];
1442 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1443 buf->getMaterial().SpecularColor = m_prop.colors[0];
1446 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1447 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1448 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1451 std::string tname = "unknown_object.png";
1452 if(m_prop.textures.size() >= 2)
1453 tname = m_prop.textures[1];
1454 else if(m_prop.textures.size() >= 1)
1455 tname = m_prop.textures[0];
1457 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1458 buf->getMaterial().setTexture(0,
1459 tsrc->getTextureForMesh(tname));
1461 // This allows setting per-material colors. However, until a real lighting
1462 // system is added, the code below will have no effect. Once MineTest
1463 // has directional lighting, it should work automatically.
1464 if(m_prop.colors.size() >= 2)
1466 buf->getMaterial().AmbientColor = m_prop.colors[1];
1467 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1468 buf->getMaterial().SpecularColor = m_prop.colors[1];
1470 else if(m_prop.colors.size() >= 1)
1472 buf->getMaterial().AmbientColor = m_prop.colors[0];
1473 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1474 buf->getMaterial().SpecularColor = m_prop.colors[0];
1477 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1478 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1479 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1485 void GenericCAO::updateAnimation()
1487 if(m_animated_meshnode == NULL)
1490 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1491 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1492 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1493 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1494 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1495 m_animated_meshnode->setTransitionTime(m_animation_blend);
1496 // Requires Irrlicht 1.8 or greater
1497 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1498 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1499 m_animated_meshnode->setLoopMode(m_animation_loop);
1503 void GenericCAO::updateBonePosition()
1505 if(m_bone_position.empty() || m_animated_meshnode == NULL)
1508 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1509 for(UNORDERED_MAP<std::string, core::vector2d<v3f> >::const_iterator
1510 ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1511 std::string bone_name = (*ii).first;
1512 v3f bone_pos = (*ii).second.X;
1513 v3f bone_rot = (*ii).second.Y;
1514 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1517 bone->setPosition(bone_pos);
1518 bone->setRotation(bone_rot);
1523 void GenericCAO::updateAttachments()
1526 if (getParent() == NULL) { // Detach or don't attach
1527 scene::ISceneNode *node = getSceneNode();
1529 v3f old_position = node->getAbsolutePosition();
1530 v3f old_rotation = node->getRotation();
1531 node->setParent(m_smgr->getRootSceneNode());
1532 node->setPosition(old_position);
1533 node->setRotation(old_rotation);
1534 node->updateAbsolutePosition();
1536 if (m_is_local_player) {
1537 LocalPlayer *player = m_env->getLocalPlayer();
1538 player->isAttached = false;
1543 scene::ISceneNode *my_node = getSceneNode();
1545 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1546 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1547 getParent()->getAnimatedMeshSceneNode();
1548 if (parent_animated_mesh_node && m_attachment_bone != "") {
1549 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1552 if (my_node && parent_node) {
1553 my_node->setParent(parent_node);
1554 my_node->setPosition(m_attachment_position);
1555 my_node->setRotation(m_attachment_rotation);
1556 my_node->updateAbsolutePosition();
1558 if (m_is_local_player) {
1559 LocalPlayer *player = m_env->getLocalPlayer();
1560 player->isAttached = true;
1565 void GenericCAO::processMessage(const std::string &data)
1567 //infostream<<"GenericCAO: Got message"<<std::endl;
1568 std::istringstream is(data, std::ios::binary);
1570 u8 cmd = readU8(is);
1571 if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1572 m_prop = gob_read_set_properties(is);
1574 m_selection_box = m_prop.collisionbox;
1575 m_selection_box.MinEdge *= BS;
1576 m_selection_box.MaxEdge *= BS;
1578 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1579 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1581 if(!m_initial_tx_basepos_set){
1582 m_initial_tx_basepos_set = true;
1583 m_tx_basepos = m_prop.initial_sprite_basepos;
1586 if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
1587 m_prop.nametag = m_name;
1590 } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1591 // Not sent by the server if this object is an attachment.
1592 // We might however get here if the server notices the object being detached before the client.
1593 m_position = readV3F1000(is);
1594 m_velocity = readV3F1000(is);
1595 m_acceleration = readV3F1000(is);
1596 if(fabs(m_prop.automatic_rotate) < 0.001)
1597 m_yaw = readF1000(is);
1600 bool do_interpolate = readU8(is);
1601 bool is_end_position = readU8(is);
1602 float update_interval = readF1000(is);
1604 // Place us a bit higher if we're physical, to not sink into
1605 // the ground due to sucky collision detection...
1607 m_position += v3f(0,0.002,0);
1609 if(getParent() != NULL) // Just in case
1614 if(!m_prop.physical)
1615 pos_translator.update(m_position, is_end_position, update_interval);
1617 pos_translator.init(m_position);
1620 } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1621 std::string mod = deSerializeString(is);
1623 // immediatly reset a engine issued texture modifier if a mod sends a different one
1624 if (m_reset_textures_timer > 0) {
1625 m_reset_textures_timer = -1;
1626 updateTextures(m_previous_texture_modifier);
1628 updateTextures(mod);
1629 } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1630 v2s16 p = readV2S16(is);
1631 int num_frames = readU16(is);
1632 float framelength = readF1000(is);
1633 bool select_horiz_by_yawpitch = readU8(is);
1636 m_anim_num_frames = num_frames;
1637 m_anim_framelength = framelength;
1638 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1641 } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1642 float override_speed = readF1000(is);
1643 float override_jump = readF1000(is);
1644 float override_gravity = readF1000(is);
1645 // these are sent inverted so we get true when the server sends nothing
1646 bool sneak = !readU8(is);
1647 bool sneak_glitch = !readU8(is);
1650 if(m_is_local_player)
1652 LocalPlayer *player = m_env->getLocalPlayer();
1653 player->physics_override_speed = override_speed;
1654 player->physics_override_jump = override_jump;
1655 player->physics_override_gravity = override_gravity;
1656 player->physics_override_sneak = sneak;
1657 player->physics_override_sneak_glitch = sneak_glitch;
1659 } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1660 // TODO: change frames send as v2s32 value
1661 v2f range = readV2F1000(is);
1662 if (!m_is_local_player) {
1663 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1664 m_animation_speed = readF1000(is);
1665 m_animation_blend = readF1000(is);
1666 // these are sent inverted so we get true when the server sends nothing
1667 m_animation_loop = !readU8(is);
1670 LocalPlayer *player = m_env->getLocalPlayer();
1671 if(player->last_animation == NO_ANIM)
1673 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1674 m_animation_speed = readF1000(is);
1675 m_animation_blend = readF1000(is);
1676 // these are sent inverted so we get true when the server sends nothing
1677 m_animation_loop = !readU8(is);
1679 // update animation only if local animations present
1680 // and received animation is unknown (except idle animation)
1681 bool is_known = false;
1682 for (int i = 1;i<4;i++)
1684 if(m_animation_range.Y == player->local_animations[i].Y)
1688 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1693 } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1694 std::string bone = deSerializeString(is);
1695 v3f position = readV3F1000(is);
1696 v3f rotation = readV3F1000(is);
1697 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1699 updateBonePosition();
1700 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1701 u16 parentID = readS16(is);
1702 u16 oldparent = m_env->attachement_parent_ids[getId()];
1704 m_children.erase(std::remove(m_children.begin(), m_children.end(),
1705 getId()), m_children.end());
1707 m_env->attachement_parent_ids[getId()] = parentID;
1708 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1711 parentobj->m_children.push_back(getId());
1714 m_attachment_bone = deSerializeString(is);
1715 m_attachment_position = readV3F1000(is);
1716 m_attachment_rotation = readV3F1000(is);
1718 // localplayer itself can't be attached to localplayer
1719 if (!m_is_local_player) {
1720 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1721 // Objects attached to the local player should be hidden by default
1722 m_is_visible = !m_attached_to_local;
1725 updateAttachments();
1726 } else if (cmd == GENERIC_CMD_PUNCHED) {
1727 /*s16 damage =*/ readS16(is);
1728 s16 result_hp = readS16(is);
1730 // Use this instead of the send damage to not interfere with prediction
1731 s16 damage = m_hp - result_hp;
1739 // TODO: Execute defined fast response
1740 // As there is no definition, make a smoke puff
1741 ClientSimpleObject *simple = createSmokePuff(
1742 m_smgr, m_env, m_position,
1743 m_prop.visual_size * BS);
1744 m_env->addSimpleObject(simple);
1746 // TODO: Execute defined fast response
1747 // Flashing shall suffice as there is no definition
1748 m_reset_textures_timer = 0.05;
1750 m_reset_textures_timer += 0.05 * damage;
1751 updateTextures(m_current_texture_modifier + "^[brighten");
1754 } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1755 m_armor_groups.clear();
1756 int armor_groups_size = readU16(is);
1757 for(int i=0; i<armor_groups_size; i++)
1759 std::string name = deSerializeString(is);
1760 int rating = readS16(is);
1761 m_armor_groups[name] = rating;
1763 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1764 // Deprecated, for backwards compatibility only.
1765 readU8(is); // version
1766 m_prop.nametag_color = readARGB8(is);
1767 if (m_nametag != NULL) {
1768 m_nametag->nametag_color = m_prop.nametag_color;
1770 } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1771 u16 child_id = readU16(is);
1772 u8 type = readU8(is);
1774 if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
1775 childobj->processInitData(deSerializeLongString(is));
1777 m_env->addActiveObject(child_id, type, deSerializeLongString(is));
1780 warningstream << FUNCTION_NAME
1781 << ": unknown command or outdated client \""
1782 << +cmd << "\"" << std::endl;
1786 /* \pre punchitem != NULL
1788 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1789 float time_from_last_punch)
1791 assert(punchitem); // pre-condition
1792 const ToolCapabilities *toolcap =
1793 &punchitem->getToolCapabilities(m_client->idef());
1794 PunchDamageResult result = getPunchDamage(
1798 time_from_last_punch);
1800 if(result.did_punch && result.damage != 0)
1802 if(result.damage < m_hp)
1804 m_hp -= result.damage;
1807 // TODO: Execute defined fast response
1808 // As there is no definition, make a smoke puff
1809 ClientSimpleObject *simple = createSmokePuff(
1810 m_smgr, m_env, m_position,
1811 m_prop.visual_size * BS);
1812 m_env->addSimpleObject(simple);
1814 // TODO: Execute defined fast response
1815 // Flashing shall suffice as there is no definition
1816 m_reset_textures_timer = 0.05;
1817 if(result.damage >= 2)
1818 m_reset_textures_timer += 0.05 * result.damage;
1819 updateTextures(m_current_texture_modifier + "^[brighten");
1825 std::string GenericCAO::debugInfoText()
1827 std::ostringstream os(std::ios::binary);
1828 os<<"GenericCAO hp="<<m_hp<<"\n";
1830 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1831 i != m_armor_groups.end(); ++i)
1833 os<<i->first<<"="<<i->second<<", ";
1840 GenericCAO proto_GenericCAO(NULL, NULL);