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") {
937 infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
938 if (m_prop.wield_item == "") {
939 // Old format, only textures are specified.
940 infostream << "textures: " << m_prop.textures.size() << std::endl;
941 if (m_prop.textures.size() >= 1) {
942 infostream << "textures[0]: " << m_prop.textures[0]
944 IItemDefManager *idef = m_client->idef();
945 item = ItemStack(m_prop.textures[0], 1, 0, idef);
948 infostream << "serialized form: " << m_prop.wield_item << std::endl;
949 item.deSerialize(m_prop.wield_item, m_client->idef());
951 m_wield_meshnode = new WieldMeshSceneNode(smgr->getRootSceneNode(),
953 m_wield_meshnode->setItem(item, m_client);
955 m_wield_meshnode->setScale(
956 v3f(m_prop.visual_size.X / 2, m_prop.visual_size.Y / 2,
957 m_prop.visual_size.X / 2));
958 u8 li = m_last_light;
959 m_wield_meshnode->setColor(video::SColor(255, li, li, li));
961 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
962 <<"\" not supported"<<std::endl;
965 /* don't update while punch texture modifier is active */
966 if (m_reset_textures_timer < 0)
967 updateTextures(m_current_texture_modifier);
969 scene::ISceneNode *node = getSceneNode();
970 if (node && m_prop.nametag != "" && !m_is_local_player) {
972 m_nametag = m_client->getCamera()->addNametag(node,
973 m_prop.nametag, m_prop.nametag_color);
978 updateBonePosition();
982 void GenericCAO::updateLight(u8 light_at_pos)
984 // Don't update light of attached one
985 if (getParent() != NULL) {
989 updateLightNoCheck(light_at_pos);
991 // Update light of all children
992 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
993 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
995 obj->updateLightNoCheck(light_at_pos);
1000 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
1002 u8 li = decode_light(light_at_pos);
1003 if (li != m_last_light) {
1005 video::SColor color(255,li,li,li);
1007 setMeshColor(m_meshnode->getMesh(), color);
1008 } else if (m_animated_meshnode) {
1009 setMeshColor(m_animated_meshnode->getMesh(), color);
1010 } else if (m_wield_meshnode) {
1011 m_wield_meshnode->setColor(color);
1012 } else if (m_spritenode) {
1013 m_spritenode->setColor(color);
1018 v3s16 GenericCAO::getLightPosition()
1020 return floatToInt(m_position, BS);
1023 void GenericCAO::updateNodePos()
1025 if (getParent() != NULL)
1028 scene::ISceneNode *node = getSceneNode();
1031 v3s16 camera_offset = m_env->getCameraOffset();
1032 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
1033 if (node != m_spritenode) { // rotate if not a sprite
1034 v3f rot = node->getRotation();
1036 node->setRotation(rot);
1041 void GenericCAO::step(float dtime, ClientEnvironment *env)
1043 // Handel model of local player instantly to prevent lags
1044 if(m_is_local_player)
1046 LocalPlayer *player = m_env->getLocalPlayer();
1050 int old_anim = player->last_animation;
1051 float old_anim_speed = player->last_animation_speed;
1052 m_position = player->getPosition() + v3f(0,BS,0);
1053 m_velocity = v3f(0,0,0);
1054 m_acceleration = v3f(0,0,0);
1055 pos_translator.vect_show = m_position;
1056 m_yaw = player->getYaw();
1057 PlayerControl controls = player->getPlayerControl();
1059 bool walking = false;
1060 if (controls.up || controls.down || controls.left || controls.right ||
1061 controls.forw_move_joystick_axis != 0.f ||
1062 controls.sidew_move_joystick_axis != 0.f)
1065 f32 new_speed = player->local_animation_speed;
1066 v2s32 new_anim = v2s32(0,0);
1067 bool allow_update = false;
1069 // increase speed if using fast or flying fast
1070 if((g_settings->getBool("fast_move") &&
1071 m_client->checkLocalPrivilege("fast")) &&
1073 (!player->touching_ground &&
1074 g_settings->getBool("free_move") &&
1075 m_client->checkLocalPrivilege("fly"))))
1077 // slowdown speed if sneeking
1078 if(controls.sneak && walking)
1081 if(walking && (controls.LMB || controls.RMB))
1083 new_anim = player->local_animations[3];
1084 player->last_animation = WD_ANIM;
1085 } else if(walking) {
1086 new_anim = player->local_animations[1];
1087 player->last_animation = WALK_ANIM;
1088 } else if(controls.LMB || controls.RMB) {
1089 new_anim = player->local_animations[2];
1090 player->last_animation = DIG_ANIM;
1093 // Apply animations if input detected and not attached
1094 // or set idle animation
1095 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1097 allow_update = true;
1098 m_animation_range = new_anim;
1099 m_animation_speed = new_speed;
1100 player->last_animation_speed = m_animation_speed;
1102 player->last_animation = NO_ANIM;
1104 if (old_anim != NO_ANIM)
1106 m_animation_range = player->local_animations[0];
1111 // Update local player animations
1112 if ((player->last_animation != old_anim ||
1113 m_animation_speed != old_anim_speed) &&
1114 player->last_animation != NO_ANIM && allow_update)
1120 if(m_visuals_expired && m_smgr && m_irr){
1121 m_visuals_expired = false;
1123 // Attachments, part 1: All attached objects must be unparented first,
1124 // or Irrlicht causes a segmentation fault
1125 for(std::vector<u16>::iterator ci = m_children.begin();
1126 ci != m_children.end();)
1128 if (m_env->attachement_parent_ids[*ci] != getId()) {
1129 ci = m_children.erase(ci);
1132 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1134 scene::ISceneNode *child_node = obj->getSceneNode();
1136 child_node->setParent(m_smgr->getRootSceneNode());
1141 removeFromScene(false);
1142 addToScene(m_smgr, m_client->tsrc(), m_irr);
1144 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1145 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1146 // Get the object of the child
1147 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1149 obj->setAttachments();
1153 // Make sure m_is_visible is always applied
1154 scene::ISceneNode *node = getSceneNode();
1156 node->setVisible(m_is_visible);
1158 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1160 // Set these for later
1161 m_position = getPosition();
1162 m_velocity = v3f(0,0,0);
1163 m_acceleration = v3f(0,0,0);
1164 pos_translator.vect_show = m_position;
1166 if(m_is_local_player) // Update local player attachment position
1168 LocalPlayer *player = m_env->getLocalPlayer();
1169 player->overridePosition = getParent()->getPosition();
1170 m_env->getLocalPlayer()->parent = getParent();
1173 v3f lastpos = pos_translator.vect_show;
1177 aabb3f box = m_prop.collisionbox;
1180 collisionMoveResult moveresult;
1181 f32 pos_max_d = BS*0.125; // Distance per iteration
1182 v3f p_pos = m_position;
1183 v3f p_velocity = m_velocity;
1184 moveresult = collisionMoveSimple(env,env->getGameDef(),
1185 pos_max_d, box, m_prop.stepheight, dtime,
1186 &p_pos, &p_velocity, m_acceleration,
1187 this, m_prop.collideWithObjects);
1190 m_velocity = p_velocity;
1192 bool is_end_position = moveresult.collides;
1193 pos_translator.update(m_position, is_end_position, dtime);
1194 pos_translator.translate(dtime);
1197 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1198 m_velocity += dtime * m_acceleration;
1199 pos_translator.update(m_position, pos_translator.aim_is_end,
1200 pos_translator.anim_time);
1201 pos_translator.translate(dtime);
1205 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1206 m_step_distance_counter += moved;
1207 if(m_step_distance_counter > 1.5*BS)
1209 m_step_distance_counter = 0;
1210 if(!m_is_local_player && m_prop.makes_footstep_sound)
1212 INodeDefManager *ndef = m_client->ndef();
1213 v3s16 p = floatToInt(getPosition() + v3f(0,
1214 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1215 MapNode n = m_env->getMap().getNodeNoEx(p);
1216 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1217 m_client->sound()->playSoundAt(spec, false, getPosition());
1222 m_anim_timer += dtime;
1223 if(m_anim_timer >= m_anim_framelength)
1225 m_anim_timer -= m_anim_framelength;
1227 if(m_anim_frame >= m_anim_num_frames)
1233 if(m_reset_textures_timer >= 0)
1235 m_reset_textures_timer -= dtime;
1236 if(m_reset_textures_timer <= 0) {
1237 m_reset_textures_timer = -1;
1238 updateTextures(m_previous_texture_modifier);
1241 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1243 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1247 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1248 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1250 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1251 + m_prop.automatic_face_movement_dir_offset;
1252 float max_rotation_delta =
1253 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1255 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
1256 (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
1258 m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
1260 m_yaw = optimal_yaw;
1266 void GenericCAO::updateTexturePos()
1270 scene::ICameraSceneNode* camera =
1271 m_spritenode->getSceneManager()->getActiveCamera();
1274 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1275 - camera->getAbsolutePosition();
1276 cam_to_entity.normalize();
1278 int row = m_tx_basepos.Y;
1279 int col = m_tx_basepos.X;
1281 if(m_tx_select_horiz_by_yawpitch)
1283 if(cam_to_entity.Y > 0.75)
1285 else if(cam_to_entity.Y < -0.75)
1289 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1290 float dir = mob_dir - m_yaw;
1291 dir = wrapDegrees_180(dir);
1292 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1293 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1295 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1297 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1299 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1306 // Animation goes downwards
1307 row += m_anim_frame;
1309 float txs = m_tx_size.X;
1310 float tys = m_tx_size.Y;
1311 setBillboardTextureMatrix(m_spritenode,
1312 txs, tys, col, row);
1316 void GenericCAO::updateTextures(const std::string &mod)
1318 ITextureSource *tsrc = m_client->tsrc();
1320 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1321 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1322 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1324 m_previous_texture_modifier = m_current_texture_modifier;
1325 m_current_texture_modifier = mod;
1329 if(m_prop.visual == "sprite")
1331 std::string texturestring = "unknown_node.png";
1332 if(m_prop.textures.size() >= 1)
1333 texturestring = m_prop.textures[0];
1334 texturestring += mod;
1335 m_spritenode->setMaterialTexture(0,
1336 tsrc->getTextureForMesh(texturestring));
1338 // This allows setting per-material colors. However, until a real lighting
1339 // system is added, the code below will have no effect. Once MineTest
1340 // has directional lighting, it should work automatically.
1341 if(m_prop.colors.size() >= 1)
1343 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1344 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1345 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1348 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1349 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1350 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1353 if(m_animated_meshnode)
1355 if(m_prop.visual == "mesh")
1357 for (u32 i = 0; i < m_prop.textures.size() &&
1358 i < m_animated_meshnode->getMaterialCount(); ++i)
1360 std::string texturestring = m_prop.textures[i];
1361 if(texturestring == "")
1362 continue; // Empty texture string means don't modify that material
1363 texturestring += mod;
1364 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1367 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1371 // Set material flags and texture
1372 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1373 material.TextureLayer[0].Texture = texture;
1374 material.setFlag(video::EMF_LIGHTING, false);
1375 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1377 m_animated_meshnode->getMaterial(i)
1378 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1379 m_animated_meshnode->getMaterial(i)
1380 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1381 m_animated_meshnode->getMaterial(i)
1382 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1384 for (u32 i = 0; i < m_prop.colors.size() &&
1385 i < m_animated_meshnode->getMaterialCount(); ++i)
1387 // This allows setting per-material colors. However, until a real lighting
1388 // system is added, the code below will have no effect. Once MineTest
1389 // has directional lighting, it should work automatically.
1390 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1391 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1392 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1398 if(m_prop.visual == "cube")
1400 for (u32 i = 0; i < 6; ++i)
1402 std::string texturestring = "unknown_node.png";
1403 if(m_prop.textures.size() > i)
1404 texturestring = m_prop.textures[i];
1405 texturestring += mod;
1408 // Set material flags and texture
1409 video::SMaterial& material = m_meshnode->getMaterial(i);
1410 material.setFlag(video::EMF_LIGHTING, false);
1411 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1412 material.setTexture(0,
1413 tsrc->getTextureForMesh(texturestring));
1414 material.getTextureMatrix(0).makeIdentity();
1416 // This allows setting per-material colors. However, until a real lighting
1417 // system is added, the code below will have no effect. Once MineTest
1418 // has directional lighting, it should work automatically.
1419 if(m_prop.colors.size() > i)
1421 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1422 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1423 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1426 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1427 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1428 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1431 else if(m_prop.visual == "upright_sprite")
1433 scene::IMesh *mesh = m_meshnode->getMesh();
1435 std::string tname = "unknown_object.png";
1436 if(m_prop.textures.size() >= 1)
1437 tname = m_prop.textures[0];
1439 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1440 buf->getMaterial().setTexture(0,
1441 tsrc->getTextureForMesh(tname));
1443 // This allows setting per-material colors. However, until a real lighting
1444 // system is added, the code below will have no effect. Once MineTest
1445 // has directional lighting, it should work automatically.
1446 if(m_prop.colors.size() >= 1)
1448 buf->getMaterial().AmbientColor = m_prop.colors[0];
1449 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1450 buf->getMaterial().SpecularColor = m_prop.colors[0];
1453 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1454 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1455 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1458 std::string tname = "unknown_object.png";
1459 if(m_prop.textures.size() >= 2)
1460 tname = m_prop.textures[1];
1461 else if(m_prop.textures.size() >= 1)
1462 tname = m_prop.textures[0];
1464 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1465 buf->getMaterial().setTexture(0,
1466 tsrc->getTextureForMesh(tname));
1468 // This allows setting per-material colors. However, until a real lighting
1469 // system is added, the code below will have no effect. Once MineTest
1470 // has directional lighting, it should work automatically.
1471 if(m_prop.colors.size() >= 2)
1473 buf->getMaterial().AmbientColor = m_prop.colors[1];
1474 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1475 buf->getMaterial().SpecularColor = m_prop.colors[1];
1477 else if(m_prop.colors.size() >= 1)
1479 buf->getMaterial().AmbientColor = m_prop.colors[0];
1480 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1481 buf->getMaterial().SpecularColor = m_prop.colors[0];
1484 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1485 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1486 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1492 void GenericCAO::updateAnimation()
1494 if(m_animated_meshnode == NULL)
1497 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1498 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1499 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1500 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1501 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1502 m_animated_meshnode->setTransitionTime(m_animation_blend);
1503 // Requires Irrlicht 1.8 or greater
1504 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1505 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1506 m_animated_meshnode->setLoopMode(m_animation_loop);
1510 void GenericCAO::updateBonePosition()
1512 if(m_bone_position.empty() || m_animated_meshnode == NULL)
1515 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1516 for(UNORDERED_MAP<std::string, core::vector2d<v3f> >::const_iterator
1517 ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1518 std::string bone_name = (*ii).first;
1519 v3f bone_pos = (*ii).second.X;
1520 v3f bone_rot = (*ii).second.Y;
1521 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1524 bone->setPosition(bone_pos);
1525 bone->setRotation(bone_rot);
1530 void GenericCAO::updateAttachments()
1533 if (getParent() == NULL) { // Detach or don't attach
1534 scene::ISceneNode *node = getSceneNode();
1536 v3f old_position = node->getAbsolutePosition();
1537 v3f old_rotation = node->getRotation();
1538 node->setParent(m_smgr->getRootSceneNode());
1539 node->setPosition(old_position);
1540 node->setRotation(old_rotation);
1541 node->updateAbsolutePosition();
1543 if (m_is_local_player) {
1544 LocalPlayer *player = m_env->getLocalPlayer();
1545 player->isAttached = false;
1550 scene::ISceneNode *my_node = getSceneNode();
1552 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1553 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1554 getParent()->getAnimatedMeshSceneNode();
1555 if (parent_animated_mesh_node && m_attachment_bone != "") {
1556 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1559 if (my_node && parent_node) {
1560 my_node->setParent(parent_node);
1561 my_node->setPosition(m_attachment_position);
1562 my_node->setRotation(m_attachment_rotation);
1563 my_node->updateAbsolutePosition();
1565 if (m_is_local_player) {
1566 LocalPlayer *player = m_env->getLocalPlayer();
1567 player->isAttached = true;
1572 void GenericCAO::processMessage(const std::string &data)
1574 //infostream<<"GenericCAO: Got message"<<std::endl;
1575 std::istringstream is(data, std::ios::binary);
1577 u8 cmd = readU8(is);
1578 if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1579 m_prop = gob_read_set_properties(is);
1581 m_selection_box = m_prop.collisionbox;
1582 m_selection_box.MinEdge *= BS;
1583 m_selection_box.MaxEdge *= BS;
1585 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1586 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1588 if(!m_initial_tx_basepos_set){
1589 m_initial_tx_basepos_set = true;
1590 m_tx_basepos = m_prop.initial_sprite_basepos;
1593 if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
1594 m_prop.nametag = m_name;
1597 } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1598 // Not sent by the server if this object is an attachment.
1599 // We might however get here if the server notices the object being detached before the client.
1600 m_position = readV3F1000(is);
1601 m_velocity = readV3F1000(is);
1602 m_acceleration = readV3F1000(is);
1603 if(fabs(m_prop.automatic_rotate) < 0.001)
1604 m_yaw = readF1000(is);
1607 bool do_interpolate = readU8(is);
1608 bool is_end_position = readU8(is);
1609 float update_interval = readF1000(is);
1611 // Place us a bit higher if we're physical, to not sink into
1612 // the ground due to sucky collision detection...
1614 m_position += v3f(0,0.002,0);
1616 if(getParent() != NULL) // Just in case
1621 if(!m_prop.physical)
1622 pos_translator.update(m_position, is_end_position, update_interval);
1624 pos_translator.init(m_position);
1627 } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1628 std::string mod = deSerializeString(is);
1630 // immediatly reset a engine issued texture modifier if a mod sends a different one
1631 if (m_reset_textures_timer > 0) {
1632 m_reset_textures_timer = -1;
1633 updateTextures(m_previous_texture_modifier);
1635 updateTextures(mod);
1636 } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1637 v2s16 p = readV2S16(is);
1638 int num_frames = readU16(is);
1639 float framelength = readF1000(is);
1640 bool select_horiz_by_yawpitch = readU8(is);
1643 m_anim_num_frames = num_frames;
1644 m_anim_framelength = framelength;
1645 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1648 } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1649 float override_speed = readF1000(is);
1650 float override_jump = readF1000(is);
1651 float override_gravity = readF1000(is);
1652 // these are sent inverted so we get true when the server sends nothing
1653 bool sneak = !readU8(is);
1654 bool sneak_glitch = !readU8(is);
1655 bool new_move = !readU8(is);
1658 if(m_is_local_player)
1660 LocalPlayer *player = m_env->getLocalPlayer();
1661 player->physics_override_speed = override_speed;
1662 player->physics_override_jump = override_jump;
1663 player->physics_override_gravity = override_gravity;
1664 player->physics_override_sneak = sneak;
1665 player->physics_override_sneak_glitch = sneak_glitch;
1666 player->physics_override_new_move = new_move;
1668 } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1669 // TODO: change frames send as v2s32 value
1670 v2f range = readV2F1000(is);
1671 if (!m_is_local_player) {
1672 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1673 m_animation_speed = readF1000(is);
1674 m_animation_blend = readF1000(is);
1675 // these are sent inverted so we get true when the server sends nothing
1676 m_animation_loop = !readU8(is);
1679 LocalPlayer *player = m_env->getLocalPlayer();
1680 if(player->last_animation == NO_ANIM)
1682 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1683 m_animation_speed = readF1000(is);
1684 m_animation_blend = readF1000(is);
1685 // these are sent inverted so we get true when the server sends nothing
1686 m_animation_loop = !readU8(is);
1688 // update animation only if local animations present
1689 // and received animation is unknown (except idle animation)
1690 bool is_known = false;
1691 for (int i = 1;i<4;i++)
1693 if(m_animation_range.Y == player->local_animations[i].Y)
1697 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1702 } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1703 std::string bone = deSerializeString(is);
1704 v3f position = readV3F1000(is);
1705 v3f rotation = readV3F1000(is);
1706 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1708 updateBonePosition();
1709 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1710 u16 parentID = readS16(is);
1711 u16 oldparent = m_env->attachement_parent_ids[getId()];
1713 m_children.erase(std::remove(m_children.begin(), m_children.end(),
1714 getId()), m_children.end());
1716 m_env->attachement_parent_ids[getId()] = parentID;
1717 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1720 parentobj->m_children.push_back(getId());
1723 m_attachment_bone = deSerializeString(is);
1724 m_attachment_position = readV3F1000(is);
1725 m_attachment_rotation = readV3F1000(is);
1727 // localplayer itself can't be attached to localplayer
1728 if (!m_is_local_player) {
1729 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1730 // Objects attached to the local player should be hidden by default
1731 m_is_visible = !m_attached_to_local;
1734 updateAttachments();
1735 } else if (cmd == GENERIC_CMD_PUNCHED) {
1736 /*s16 damage =*/ readS16(is);
1737 s16 result_hp = readS16(is);
1739 // Use this instead of the send damage to not interfere with prediction
1740 s16 damage = m_hp - result_hp;
1748 // TODO: Execute defined fast response
1749 // As there is no definition, make a smoke puff
1750 ClientSimpleObject *simple = createSmokePuff(
1751 m_smgr, m_env, m_position,
1752 m_prop.visual_size * BS);
1753 m_env->addSimpleObject(simple);
1755 // TODO: Execute defined fast response
1756 // Flashing shall suffice as there is no definition
1757 m_reset_textures_timer = 0.05;
1759 m_reset_textures_timer += 0.05 * damage;
1760 updateTextures(m_current_texture_modifier + "^[brighten");
1763 } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1764 m_armor_groups.clear();
1765 int armor_groups_size = readU16(is);
1766 for(int i=0; i<armor_groups_size; i++)
1768 std::string name = deSerializeString(is);
1769 int rating = readS16(is);
1770 m_armor_groups[name] = rating;
1772 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1773 // Deprecated, for backwards compatibility only.
1774 readU8(is); // version
1775 m_prop.nametag_color = readARGB8(is);
1776 if (m_nametag != NULL) {
1777 m_nametag->nametag_color = m_prop.nametag_color;
1779 } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1780 u16 child_id = readU16(is);
1781 u8 type = readU8(is);
1783 if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
1784 childobj->processInitData(deSerializeLongString(is));
1786 m_env->addActiveObject(child_id, type, deSerializeLongString(is));
1789 warningstream << FUNCTION_NAME
1790 << ": unknown command or outdated client \""
1791 << +cmd << "\"" << std::endl;
1795 /* \pre punchitem != NULL
1797 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1798 float time_from_last_punch)
1800 assert(punchitem); // pre-condition
1801 const ToolCapabilities *toolcap =
1802 &punchitem->getToolCapabilities(m_client->idef());
1803 PunchDamageResult result = getPunchDamage(
1807 time_from_last_punch);
1809 if(result.did_punch && result.damage != 0)
1811 if(result.damage < m_hp)
1813 m_hp -= result.damage;
1816 // TODO: Execute defined fast response
1817 // As there is no definition, make a smoke puff
1818 ClientSimpleObject *simple = createSmokePuff(
1819 m_smgr, m_env, m_position,
1820 m_prop.visual_size * BS);
1821 m_env->addSimpleObject(simple);
1823 // TODO: Execute defined fast response
1824 // Flashing shall suffice as there is no definition
1825 m_reset_textures_timer = 0.05;
1826 if(result.damage >= 2)
1827 m_reset_textures_timer += 0.05 * result.damage;
1828 updateTextures(m_current_texture_modifier + "^[brighten");
1834 std::string GenericCAO::debugInfoText()
1836 std::ostringstream os(std::ios::binary);
1837 os<<"GenericCAO hp="<<m_hp<<"\n";
1839 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1840 i != m_armor_groups.end(); ++i)
1842 os<<i->first<<"="<<i->second<<", ";
1849 GenericCAO proto_GenericCAO(NULL, NULL);