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 "content_cao.h"
26 #include "util/numeric.h" // For IntervalLimiter
27 #include "util/serialize.h"
28 #include "util/basic_macros.h"
29 #include "client/tile.h"
30 #include "environment.h"
31 #include "collision.h"
33 #include "serialization.h" // For decompressZlib
34 #include "clientobject.h"
38 #include "content_cso.h"
41 #include "localplayer.h"
43 #include "camera.h" // CameraModes
44 #include "wieldmesh.h"
46 #include "client/renderingengine.h"
49 struct ToolCapabilities;
51 std::unordered_map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
53 void SmoothTranslator::init(v3f vect)
60 anim_time_counter = 0;
64 void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
66 aim_is_end = is_end_position;
69 if(update_interval > 0)
71 anim_time = update_interval;
73 if(anim_time < 0.001 || anim_time > 1.0)
74 anim_time = anim_time_counter;
76 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
78 anim_time_counter = 0;
82 void SmoothTranslator::translate(f32 dtime)
84 anim_time_counter = anim_time_counter + dtime;
85 anim_counter = anim_counter + dtime;
86 v3f vect_move = vect_aim - vect_old;
89 moveratio = anim_time_counter / anim_time;
90 // Move a bit less than should, to avoid oscillation
91 moveratio = moveratio * 0.8;
95 if(moveratio > move_end)
97 vect_show = vect_old + vect_move * moveratio;
104 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
105 float txs, float tys, int col, int row)
107 video::SMaterial& material = bill->getMaterial(0);
108 core::matrix4& matrix = material.getTextureMatrix(0);
109 matrix.setTextureTranslate(txs*col, tys*row);
110 matrix.setTextureScale(txs, tys);
117 class TestCAO : public ClientActiveObject
120 TestCAO(Client *client, ClientEnvironment *env);
123 ActiveObjectType getType() const
125 return ACTIVEOBJECT_TYPE_TEST;
128 static ClientActiveObject* create(Client *client, ClientEnvironment *env);
130 void addToScene(ITextureSource *tsrc);
131 void removeFromScene(bool permanent);
132 void updateLight(u8 light_at_pos);
133 v3s16 getLightPosition();
134 void updateNodePos();
136 void step(float dtime, ClientEnvironment *env);
138 void processMessage(const std::string &data);
140 bool getCollisionBox(aabb3f *toset) const { return false; }
142 scene::IMeshSceneNode *m_node;
147 TestCAO proto_TestCAO(NULL, NULL);
149 TestCAO::TestCAO(Client *client, ClientEnvironment *env):
150 ClientActiveObject(0, client, env),
152 m_position(v3f(0,10*BS,0))
154 ClientActiveObject::registerType(getType(), create);
161 ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
163 return new TestCAO(client, env);
166 void TestCAO::addToScene(ITextureSource *tsrc)
171 //video::IVideoDriver* driver = smgr->getVideoDriver();
173 scene::SMesh *mesh = new scene::SMesh();
174 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
175 video::SColor c(255,255,255,255);
176 video::S3DVertex vertices[4] =
178 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
179 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
180 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
181 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
183 u16 indices[] = {0,1,2,2,3,0};
184 buf->append(vertices, 4, indices, 6);
186 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
187 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
188 buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
189 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
190 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
191 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
193 mesh->addMeshBuffer(buf);
195 m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
200 void TestCAO::removeFromScene(bool permanent)
209 void TestCAO::updateLight(u8 light_at_pos)
213 v3s16 TestCAO::getLightPosition()
215 return floatToInt(m_position, BS);
218 void TestCAO::updateNodePos()
223 m_node->setPosition(m_position);
224 //m_node->setRotation(v3f(0, 45, 0));
227 void TestCAO::step(float dtime, ClientEnvironment *env)
231 v3f rot = m_node->getRotation();
232 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
233 rot.Y += dtime * 180;
234 m_node->setRotation(rot);
238 void TestCAO::processMessage(const std::string &data)
240 infostream<<"TestCAO: Got data: "<<data<<std::endl;
241 std::istringstream is(data, std::ios::binary);
259 class ItemCAO : public ClientActiveObject
262 ItemCAO(Client *client, ClientEnvironment *env);
265 ActiveObjectType getType() const
267 return ACTIVEOBJECT_TYPE_ITEM;
270 static ClientActiveObject* create(Client *client, ClientEnvironment *env);
272 void addToScene(ITextureSource *tsrc);
273 void removeFromScene(bool permanent);
274 void updateLight(u8 light_at_pos);
275 v3s16 getLightPosition();
276 void updateNodePos();
277 void updateInfoText();
278 void updateTexture();
280 void step(float dtime, ClientEnvironment *env);
282 void processMessage(const std::string &data);
284 void initialize(const std::string &data);
287 virtual bool getSelectionBox(aabb3f *toset) const
289 *toset = m_selection_box;
296 inline float getYaw() const
298 std::string infoText()
301 bool getCollisionBox(aabb3f *toset) const { return false; }
303 aabb3f m_selection_box;
304 scene::IMeshSceneNode *m_node;
306 std::string m_itemstring;
307 std::string m_infotext;
311 ItemCAO proto_ItemCAO(NULL, NULL);
313 ItemCAO::ItemCAO(Client *client, ClientEnvironment *env):
314 ClientActiveObject(0, client, env),
315 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
317 m_position(v3f(0,10*BS,0))
321 ClientActiveObject::registerType(getType(), create);
329 ClientActiveObject* ItemCAO::create(Client *client, ClientEnvironment *env)
331 return new ItemCAO(client, env);
334 void ItemCAO::addToScene(ITextureSource *tsrc)
339 //video::IVideoDriver* driver = smgr->getVideoDriver();
341 scene::SMesh *mesh = new scene::SMesh();
342 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
343 video::SColor c(255,255,255,255);
344 video::S3DVertex vertices[4] =
346 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
347 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
348 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
349 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
350 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
351 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
352 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
353 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
355 u16 indices[] = {0,1,2,2,3,0};
356 buf->append(vertices, 4, indices, 6);
358 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
359 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
360 // Initialize with a generated placeholder texture
361 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
362 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
363 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
364 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
366 mesh->addMeshBuffer(buf);
368 m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
379 void ItemCAO::removeFromScene(bool permanent)
388 void ItemCAO::updateLight(u8 light_at_pos)
393 u8 li = decode_light(light_at_pos);
394 video::SColor color(255,li,li,li);
395 setMeshColor(m_node->getMesh(), color);
398 v3s16 ItemCAO::getLightPosition()
400 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
403 void ItemCAO::updateNodePos()
408 m_node->setPosition(m_position);
411 void ItemCAO::updateInfoText()
414 IItemDefManager *idef = m_client->idef();
416 item.deSerialize(m_itemstring, idef);
417 if(item.isKnown(idef))
418 m_infotext = item.getDefinition(idef).description;
420 m_infotext = "Unknown item: '" + m_itemstring + "'";
422 m_infotext += " (" + itos(item.count) + ")";
424 catch(SerializationError &e)
426 m_infotext = "Unknown item: '" + m_itemstring + "'";
430 void ItemCAO::updateTexture()
435 // Create an inventory item to see what is its image
436 std::istringstream is(m_itemstring, std::ios_base::binary);
437 video::ITexture *texture = NULL;
439 IItemDefManager *idef = m_client->idef();
441 item.deSerialize(is, idef);
442 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_client);
444 catch(SerializationError &e)
446 warningstream<<FUNCTION_NAME
447 <<": error deSerializing itemstring \""
448 <<m_itemstring<<std::endl;
451 // Set meshbuffer texture
452 m_node->getMaterial(0).setTexture(0, texture);
456 void ItemCAO::step(float dtime, ClientEnvironment *env)
460 /*v3f rot = m_node->getRotation();
461 rot.Y += dtime * 120;
462 m_node->setRotation(rot);*/
463 LocalPlayer *player = env->getLocalPlayer();
465 v3f rot = m_node->getRotation();
466 rot.Y = 180.0 - (player->getYaw());
467 m_node->setRotation(rot);
471 void ItemCAO::processMessage(const std::string &data)
473 //infostream<<"ItemCAO: Got message"<<std::endl;
474 std::istringstream is(data, std::ios::binary);
480 m_position = readV3F1000(is);
486 m_itemstring = deSerializeString(is);
492 void ItemCAO::initialize(const std::string &data)
494 infostream<<"ItemCAO: Got init data"<<std::endl;
497 std::istringstream is(data, std::ios::binary);
499 u8 version = readU8(is);
504 m_position = readV3F1000(is);
506 m_itemstring = deSerializeString(is);
517 #include "genericobject.h"
519 GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
520 ClientActiveObject(0, client, env)
522 if (client == NULL) {
523 ClientActiveObject::registerType(getType(), create);
529 bool GenericCAO::getCollisionBox(aabb3f *toset) const
533 //update collision box
534 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
535 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
537 toset->MinEdge += m_position;
538 toset->MaxEdge += m_position;
546 bool GenericCAO::collideWithObjects() const
548 return m_prop.collideWithObjects;
551 void GenericCAO::initialize(const std::string &data)
553 infostream<<"GenericCAO: Got init data"<<std::endl;
554 processInitData(data);
557 // Check if it's the current player
558 LocalPlayer *player = m_env->getLocalPlayer();
559 if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
560 m_is_local_player = true;
561 m_is_visible = false;
562 player->setCAO(this);
564 if (m_client->getProtoVersion() < 33)
565 m_env->addPlayerName(m_name.c_str());
569 void GenericCAO::processInitData(const std::string &data)
571 std::istringstream is(data, std::ios::binary);
572 int num_messages = 0;
574 u8 version = readU8(is);
576 if (version == 1) { // In PROTOCOL_VERSION 14
577 m_name = deSerializeString(is);
578 m_is_player = readU8(is);
580 m_position = readV3F1000(is);
581 m_yaw = readF1000(is);
583 num_messages = readU8(is);
584 } else if (version == 0) { // In PROTOCOL_VERSION 13
585 m_name = deSerializeString(is);
586 m_is_player = readU8(is);
587 m_position = readV3F1000(is);
588 m_yaw = readF1000(is);
590 num_messages = readU8(is);
592 errorstream<<"GenericCAO: Unsupported init data version"
597 for (int i = 0; i < num_messages; i++) {
598 std::string message = deSerializeLongString(is);
599 processMessage(message);
602 pos_translator.init(m_position);
606 GenericCAO::~GenericCAO()
608 if (m_is_player && m_client->getProtoVersion() < 33) {
609 m_env->removePlayerName(m_name.c_str());
611 removeFromScene(true);
614 bool GenericCAO::getSelectionBox(aabb3f *toset) const
616 if (!m_prop.is_visible || !m_is_visible || m_is_local_player
617 || getParent() != NULL){
620 *toset = m_selection_box;
624 v3f GenericCAO::getPosition()
626 if (getParent() != NULL) {
627 scene::ISceneNode *node = getSceneNode();
629 return node->getAbsolutePosition();
633 return pos_translator.vect_show;
636 scene::ISceneNode* GenericCAO::getSceneNode()
640 } else if (m_animated_meshnode) {
641 return m_animated_meshnode;
642 } else if (m_wield_meshnode) {
643 return m_wield_meshnode;
644 } else if (m_spritenode) {
650 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
652 return m_animated_meshnode;
655 void GenericCAO::setChildrenVisible(bool toset)
657 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
658 GenericCAO *obj = m_env->getGenericCAO(m_children[i]);
660 obj->setVisible(toset);
665 void GenericCAO::setAttachments()
670 ClientActiveObject* GenericCAO::getParent() const
672 ClientActiveObject *obj = NULL;
674 u16 attached_id = m_env->attachement_parent_ids[getId()];
676 if ((attached_id != 0) &&
677 (attached_id != getId())) {
678 obj = m_env->getActiveObject(attached_id);
683 void GenericCAO::removeFromScene(bool permanent)
685 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
686 if((m_env != NULL) && (permanent))
688 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
689 u16 ci = m_children[i];
690 if (m_env->attachement_parent_ids[ci] == getId()) {
691 m_env->attachement_parent_ids[ci] = 0;
695 m_env->attachement_parent_ids[getId()] = 0;
697 LocalPlayer* player = m_env->getLocalPlayer();
698 if (this == player->parent) {
699 player->parent = NULL;
700 player->isAttached = false;
705 m_meshnode->remove();
708 } else if (m_animated_meshnode) {
709 m_animated_meshnode->remove();
710 m_animated_meshnode->drop();
711 m_animated_meshnode = NULL;
712 } else if (m_wield_meshnode) {
713 m_wield_meshnode->remove();
714 m_wield_meshnode->drop();
715 m_wield_meshnode = NULL;
716 } else if (m_spritenode) {
717 m_spritenode->remove();
718 m_spritenode->drop();
723 m_client->getCamera()->removeNametag(m_nametag);
728 void GenericCAO::addToScene(ITextureSource *tsrc)
730 m_smgr = RenderingEngine::get_scene_manager();
732 if (getSceneNode() != NULL) {
736 m_visuals_expired = false;
738 if (!m_prop.is_visible) {
742 if (m_prop.visual == "sprite") {
743 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
744 m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
745 NULL, v2f(1, 1), v3f(0,0,0), -1);
746 m_spritenode->grab();
747 m_spritenode->setMaterialTexture(0,
748 tsrc->getTextureForMesh("unknown_node.png"));
749 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
750 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
751 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
752 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
753 u8 li = m_last_light;
754 m_spritenode->setColor(video::SColor(255,li,li,li));
755 m_spritenode->setSize(m_prop.visual_size*BS);
757 const float txs = 1.0 / 1;
758 const float tys = 1.0 / 1;
759 setBillboardTextureMatrix(m_spritenode,
762 } else if (m_prop.visual == "upright_sprite") {
763 scene::SMesh *mesh = new scene::SMesh();
764 double dx = BS * m_prop.visual_size.X / 2;
765 double dy = BS * m_prop.visual_size.Y / 2;
766 u8 li = m_last_light;
767 video::SColor c(255, li, li, li);
770 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
771 video::S3DVertex vertices[4] = {
772 video::S3DVertex(-dx, -dy, 0, 0,0,0, c, 1,1),
773 video::S3DVertex( dx, -dy, 0, 0,0,0, c, 0,1),
774 video::S3DVertex( dx, dy, 0, 0,0,0, c, 0,0),
775 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 1,0),
777 u16 indices[] = {0,1,2,2,3,0};
778 buf->append(vertices, 4, indices, 6);
780 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
781 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
782 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
783 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
785 mesh->addMeshBuffer(buf);
789 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
790 video::S3DVertex vertices[4] = {
791 video::S3DVertex( dx,-dy, 0, 0,0,0, c, 1,1),
792 video::S3DVertex(-dx,-dy, 0, 0,0,0, c, 0,1),
793 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 0,0),
794 video::S3DVertex( dx, dy, 0, 0,0,0, c, 1,0),
796 u16 indices[] = {0,1,2,2,3,0};
797 buf->append(vertices, 4, indices, 6);
799 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
800 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
801 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
802 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
804 mesh->addMeshBuffer(buf);
807 m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
810 // Set it to use the materials of the meshbuffers directly.
811 // This is needed for changing the texture in the future
812 m_meshnode->setReadOnlyMaterials(true);
814 else if(m_prop.visual == "cube") {
815 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
816 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
817 m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
821 m_meshnode->setScale(v3f(m_prop.visual_size.X,
822 m_prop.visual_size.Y,
823 m_prop.visual_size.X));
824 u8 li = m_last_light;
825 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
827 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
828 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
829 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
830 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
832 else if(m_prop.visual == "mesh") {
833 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
834 scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh);
837 m_animated_meshnode = RenderingEngine::get_scene_manager()->
838 addAnimatedMeshSceneNode(mesh, NULL);
839 m_animated_meshnode->grab();
840 mesh->drop(); // The scene node took hold of it
841 m_animated_meshnode->animateJoints(); // Needed for some animations
842 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
843 m_prop.visual_size.Y,
844 m_prop.visual_size.X));
845 u8 li = m_last_light;
846 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
848 bool backface_culling = m_prop.backface_culling;
850 backface_culling = false;
852 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
853 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
854 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
855 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
856 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
859 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
861 else if(m_prop.visual == "wielditem") {
863 infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
864 if (m_prop.wield_item == "") {
865 // Old format, only textures are specified.
866 infostream << "textures: " << m_prop.textures.size() << std::endl;
867 if (m_prop.textures.size() >= 1) {
868 infostream << "textures[0]: " << m_prop.textures[0]
870 IItemDefManager *idef = m_client->idef();
871 item = ItemStack(m_prop.textures[0], 1, 0, idef);
874 infostream << "serialized form: " << m_prop.wield_item << std::endl;
875 item.deSerialize(m_prop.wield_item, m_client->idef());
877 m_wield_meshnode = new WieldMeshSceneNode(
878 RenderingEngine::get_scene_manager(), -1);
879 m_wield_meshnode->setItem(item, m_client);
881 m_wield_meshnode->setScale(
882 v3f(m_prop.visual_size.X / 2, m_prop.visual_size.Y / 2,
883 m_prop.visual_size.X / 2));
884 u8 li = m_last_light;
885 m_wield_meshnode->setColor(video::SColor(255, li, li, li));
887 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
888 <<"\" not supported"<<std::endl;
891 /* don't update while punch texture modifier is active */
892 if (m_reset_textures_timer < 0)
893 updateTextures(m_current_texture_modifier);
895 scene::ISceneNode *node = getSceneNode();
896 if (node && m_prop.nametag != "" && !m_is_local_player) {
898 m_nametag = m_client->getCamera()->addNametag(node,
899 m_prop.nametag, m_prop.nametag_color);
904 updateBonePosition();
908 void GenericCAO::updateLight(u8 light_at_pos)
910 // Don't update light of attached one
911 if (getParent() != NULL) {
915 updateLightNoCheck(light_at_pos);
917 // Update light of all children
918 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
919 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
921 obj->updateLightNoCheck(light_at_pos);
926 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
928 u8 li = decode_light(light_at_pos);
929 if (li != m_last_light) {
931 video::SColor color(255,li,li,li);
933 setMeshColor(m_meshnode->getMesh(), color);
934 } else if (m_animated_meshnode) {
935 setMeshColor(m_animated_meshnode->getMesh(), color);
936 } else if (m_wield_meshnode) {
937 m_wield_meshnode->setColor(color);
938 } else if (m_spritenode) {
939 m_spritenode->setColor(color);
944 v3s16 GenericCAO::getLightPosition()
946 return floatToInt(m_position, BS);
949 void GenericCAO::updateNodePos()
951 if (getParent() != NULL)
954 scene::ISceneNode *node = getSceneNode();
957 v3s16 camera_offset = m_env->getCameraOffset();
958 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
959 if (node != m_spritenode) { // rotate if not a sprite
960 v3f rot = node->getRotation();
962 node->setRotation(rot);
967 void GenericCAO::step(float dtime, ClientEnvironment *env)
969 // Handel model of local player instantly to prevent lags
970 if (m_is_local_player) {
971 LocalPlayer *player = m_env->getLocalPlayer();
973 int old_anim = player->last_animation;
974 float old_anim_speed = player->last_animation_speed;
975 m_position = player->getPosition();
976 m_velocity = v3f(0,0,0);
977 m_acceleration = v3f(0,0,0);
978 pos_translator.vect_show = m_position;
979 m_yaw = player->getYaw();
980 const PlayerControl &controls = player->getPlayerControl();
982 bool walking = false;
983 if (controls.up || controls.down || controls.left || controls.right ||
984 controls.forw_move_joystick_axis != 0.f ||
985 controls.sidew_move_joystick_axis != 0.f)
988 f32 new_speed = player->local_animation_speed;
989 v2s32 new_anim = v2s32(0,0);
990 bool allow_update = false;
992 // increase speed if using fast or flying fast
993 if((g_settings->getBool("fast_move") &&
994 m_client->checkLocalPrivilege("fast")) &&
996 (!player->touching_ground &&
997 g_settings->getBool("free_move") &&
998 m_client->checkLocalPrivilege("fly"))))
1000 // slowdown speed if sneeking
1001 if (controls.sneak && walking)
1004 if (walking && (controls.LMB || controls.RMB)) {
1005 new_anim = player->local_animations[3];
1006 player->last_animation = WD_ANIM;
1007 } else if(walking) {
1008 new_anim = player->local_animations[1];
1009 player->last_animation = WALK_ANIM;
1010 } else if(controls.LMB || controls.RMB) {
1011 new_anim = player->local_animations[2];
1012 player->last_animation = DIG_ANIM;
1015 // Apply animations if input detected and not attached
1016 // or set idle animation
1017 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
1018 allow_update = true;
1019 m_animation_range = new_anim;
1020 m_animation_speed = new_speed;
1021 player->last_animation_speed = m_animation_speed;
1023 player->last_animation = NO_ANIM;
1025 if (old_anim != NO_ANIM) {
1026 m_animation_range = player->local_animations[0];
1031 // Update local player animations
1032 if ((player->last_animation != old_anim ||
1033 m_animation_speed != old_anim_speed) &&
1034 player->last_animation != NO_ANIM && allow_update)
1040 if (m_visuals_expired && m_smgr) {
1041 m_visuals_expired = false;
1043 // Attachments, part 1: All attached objects must be unparented first,
1044 // or Irrlicht causes a segmentation fault
1045 for(std::vector<u16>::iterator ci = m_children.begin();
1046 ci != m_children.end();)
1048 if (m_env->attachement_parent_ids[*ci] != getId()) {
1049 ci = m_children.erase(ci);
1052 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1054 scene::ISceneNode *child_node = obj->getSceneNode();
1056 child_node->setParent(m_smgr->getRootSceneNode());
1061 removeFromScene(false);
1062 addToScene(m_client->tsrc());
1064 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1065 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1066 // Get the object of the child
1067 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1069 obj->setAttachments();
1073 // Make sure m_is_visible is always applied
1074 scene::ISceneNode *node = getSceneNode();
1076 node->setVisible(m_is_visible);
1078 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1080 // Set these for later
1081 m_position = getPosition();
1082 m_velocity = v3f(0,0,0);
1083 m_acceleration = v3f(0,0,0);
1084 pos_translator.vect_show = m_position;
1086 if(m_is_local_player) // Update local player attachment position
1088 LocalPlayer *player = m_env->getLocalPlayer();
1089 player->overridePosition = getParent()->getPosition();
1090 m_env->getLocalPlayer()->parent = getParent();
1093 v3f lastpos = pos_translator.vect_show;
1097 aabb3f box = m_prop.collisionbox;
1100 collisionMoveResult moveresult;
1101 f32 pos_max_d = BS*0.125; // Distance per iteration
1102 v3f p_pos = m_position;
1103 v3f p_velocity = m_velocity;
1104 moveresult = collisionMoveSimple(env,env->getGameDef(),
1105 pos_max_d, box, m_prop.stepheight, dtime,
1106 &p_pos, &p_velocity, m_acceleration,
1107 this, m_prop.collideWithObjects);
1110 m_velocity = p_velocity;
1112 bool is_end_position = moveresult.collides;
1113 pos_translator.update(m_position, is_end_position, dtime);
1114 pos_translator.translate(dtime);
1117 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1118 m_velocity += dtime * m_acceleration;
1119 pos_translator.update(m_position, pos_translator.aim_is_end,
1120 pos_translator.anim_time);
1121 pos_translator.translate(dtime);
1125 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1126 m_step_distance_counter += moved;
1127 if(m_step_distance_counter > 1.5*BS)
1129 m_step_distance_counter = 0;
1130 if(!m_is_local_player && m_prop.makes_footstep_sound)
1132 INodeDefManager *ndef = m_client->ndef();
1133 v3s16 p = floatToInt(getPosition() + v3f(0,
1134 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1135 MapNode n = m_env->getMap().getNodeNoEx(p);
1136 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1137 m_client->sound()->playSoundAt(spec, false, getPosition());
1142 m_anim_timer += dtime;
1143 if(m_anim_timer >= m_anim_framelength)
1145 m_anim_timer -= m_anim_framelength;
1147 if(m_anim_frame >= m_anim_num_frames)
1153 if(m_reset_textures_timer >= 0)
1155 m_reset_textures_timer -= dtime;
1156 if(m_reset_textures_timer <= 0) {
1157 m_reset_textures_timer = -1;
1158 updateTextures(m_previous_texture_modifier);
1161 if(!getParent() && fabs(m_prop.automatic_rotate) > 0.001)
1163 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1167 if (!getParent() && m_prop.automatic_face_movement_dir &&
1168 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1170 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1171 + m_prop.automatic_face_movement_dir_offset;
1172 float max_rotation_delta =
1173 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1175 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
1176 (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
1178 m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
1180 m_yaw = optimal_yaw;
1186 void GenericCAO::updateTexturePos()
1190 scene::ICameraSceneNode* camera =
1191 m_spritenode->getSceneManager()->getActiveCamera();
1194 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1195 - camera->getAbsolutePosition();
1196 cam_to_entity.normalize();
1198 int row = m_tx_basepos.Y;
1199 int col = m_tx_basepos.X;
1201 if(m_tx_select_horiz_by_yawpitch)
1203 if(cam_to_entity.Y > 0.75)
1205 else if(cam_to_entity.Y < -0.75)
1209 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1210 float dir = mob_dir - m_yaw;
1211 dir = wrapDegrees_180(dir);
1212 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1213 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1215 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1217 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1219 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1226 // Animation goes downwards
1227 row += m_anim_frame;
1229 float txs = m_tx_size.X;
1230 float tys = m_tx_size.Y;
1231 setBillboardTextureMatrix(m_spritenode,
1232 txs, tys, col, row);
1236 void GenericCAO::updateTextures(std::string mod)
1238 ITextureSource *tsrc = m_client->tsrc();
1240 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1241 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1242 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1244 m_previous_texture_modifier = m_current_texture_modifier;
1245 m_current_texture_modifier = mod;
1249 if(m_prop.visual == "sprite")
1251 std::string texturestring = "unknown_node.png";
1252 if(m_prop.textures.size() >= 1)
1253 texturestring = m_prop.textures[0];
1254 texturestring += mod;
1255 m_spritenode->setMaterialTexture(0,
1256 tsrc->getTextureForMesh(texturestring));
1258 // This allows setting per-material colors. However, until a real lighting
1259 // system is added, the code below will have no effect. Once MineTest
1260 // has directional lighting, it should work automatically.
1261 if(m_prop.colors.size() >= 1)
1263 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1264 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1265 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1268 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1269 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1270 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1273 if(m_animated_meshnode)
1275 if(m_prop.visual == "mesh")
1277 for (u32 i = 0; i < m_prop.textures.size() &&
1278 i < m_animated_meshnode->getMaterialCount(); ++i)
1280 std::string texturestring = m_prop.textures[i];
1281 if(texturestring == "")
1282 continue; // Empty texture string means don't modify that material
1283 texturestring += mod;
1284 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1287 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1291 // Set material flags and texture
1292 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1293 material.TextureLayer[0].Texture = texture;
1294 material.setFlag(video::EMF_LIGHTING, false);
1295 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1297 m_animated_meshnode->getMaterial(i)
1298 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1299 m_animated_meshnode->getMaterial(i)
1300 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1301 m_animated_meshnode->getMaterial(i)
1302 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1304 for (u32 i = 0; i < m_prop.colors.size() &&
1305 i < m_animated_meshnode->getMaterialCount(); ++i)
1307 // This allows setting per-material colors. However, until a real lighting
1308 // system is added, the code below will have no effect. Once MineTest
1309 // has directional lighting, it should work automatically.
1310 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1311 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1312 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1318 if(m_prop.visual == "cube")
1320 for (u32 i = 0; i < 6; ++i)
1322 std::string texturestring = "unknown_node.png";
1323 if(m_prop.textures.size() > i)
1324 texturestring = m_prop.textures[i];
1325 texturestring += mod;
1328 // Set material flags and texture
1329 video::SMaterial& material = m_meshnode->getMaterial(i);
1330 material.setFlag(video::EMF_LIGHTING, false);
1331 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1332 material.setTexture(0,
1333 tsrc->getTextureForMesh(texturestring));
1334 material.getTextureMatrix(0).makeIdentity();
1336 // This allows setting per-material colors. However, until a real lighting
1337 // system is added, the code below will have no effect. Once MineTest
1338 // has directional lighting, it should work automatically.
1339 if(m_prop.colors.size() > i)
1341 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1342 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1343 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1346 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1347 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1348 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1351 else if(m_prop.visual == "upright_sprite")
1353 scene::IMesh *mesh = m_meshnode->getMesh();
1355 std::string tname = "unknown_object.png";
1356 if(m_prop.textures.size() >= 1)
1357 tname = m_prop.textures[0];
1359 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1360 buf->getMaterial().setTexture(0,
1361 tsrc->getTextureForMesh(tname));
1363 // This allows setting per-material colors. However, until a real lighting
1364 // system is added, the code below will have no effect. Once MineTest
1365 // has directional lighting, it should work automatically.
1366 if(m_prop.colors.size() >= 1)
1368 buf->getMaterial().AmbientColor = m_prop.colors[0];
1369 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1370 buf->getMaterial().SpecularColor = m_prop.colors[0];
1373 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1374 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1375 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1378 std::string tname = "unknown_object.png";
1379 if(m_prop.textures.size() >= 2)
1380 tname = m_prop.textures[1];
1381 else if(m_prop.textures.size() >= 1)
1382 tname = m_prop.textures[0];
1384 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1385 buf->getMaterial().setTexture(0,
1386 tsrc->getTextureForMesh(tname));
1388 // This allows setting per-material colors. However, until a real lighting
1389 // system is added, the code below will have no effect. Once MineTest
1390 // has directional lighting, it should work automatically.
1391 if(m_prop.colors.size() >= 2)
1393 buf->getMaterial().AmbientColor = m_prop.colors[1];
1394 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1395 buf->getMaterial().SpecularColor = m_prop.colors[1];
1397 else if(m_prop.colors.size() >= 1)
1399 buf->getMaterial().AmbientColor = m_prop.colors[0];
1400 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1401 buf->getMaterial().SpecularColor = m_prop.colors[0];
1404 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1405 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1406 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1412 void GenericCAO::updateAnimation()
1414 if (!m_animated_meshnode)
1417 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1418 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1419 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1420 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1421 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1422 m_animated_meshnode->setTransitionTime(m_animation_blend);
1423 // Requires Irrlicht 1.8 or greater
1424 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1425 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1426 m_animated_meshnode->setLoopMode(m_animation_loop);
1430 void GenericCAO::updateBonePosition()
1432 if(m_bone_position.empty() || !m_animated_meshnode)
1435 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1436 for(std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
1437 ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1438 std::string bone_name = (*ii).first;
1439 v3f bone_pos = (*ii).second.X;
1440 v3f bone_rot = (*ii).second.Y;
1441 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1444 bone->setPosition(bone_pos);
1445 bone->setRotation(bone_rot);
1450 void GenericCAO::updateAttachments()
1453 if (!getParent()) { // Detach or don't attach
1454 scene::ISceneNode *node = getSceneNode();
1456 v3f old_position = node->getAbsolutePosition();
1457 v3f old_rotation = node->getRotation();
1458 node->setParent(m_smgr->getRootSceneNode());
1459 node->setPosition(old_position);
1460 node->setRotation(old_rotation);
1461 node->updateAbsolutePosition();
1463 if (m_is_local_player) {
1464 LocalPlayer *player = m_env->getLocalPlayer();
1465 player->isAttached = false;
1470 scene::ISceneNode *my_node = getSceneNode();
1472 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1473 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1474 getParent()->getAnimatedMeshSceneNode();
1475 if (parent_animated_mesh_node && m_attachment_bone != "") {
1476 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1479 if (my_node && parent_node) {
1480 my_node->setParent(parent_node);
1481 my_node->setPosition(m_attachment_position);
1482 my_node->setRotation(m_attachment_rotation);
1483 my_node->updateAbsolutePosition();
1485 if (m_is_local_player) {
1486 LocalPlayer *player = m_env->getLocalPlayer();
1487 player->isAttached = true;
1492 void GenericCAO::processMessage(const std::string &data)
1494 //infostream<<"GenericCAO: Got message"<<std::endl;
1495 std::istringstream is(data, std::ios::binary);
1497 u8 cmd = readU8(is);
1498 if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1499 m_prop = gob_read_set_properties(is);
1501 m_selection_box = m_prop.collisionbox;
1502 m_selection_box.MinEdge *= BS;
1503 m_selection_box.MaxEdge *= BS;
1505 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1506 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1508 if(!m_initial_tx_basepos_set){
1509 m_initial_tx_basepos_set = true;
1510 m_tx_basepos = m_prop.initial_sprite_basepos;
1512 if (m_is_local_player) {
1513 LocalPlayer *player = m_env->getLocalPlayer();
1514 player->makes_footstep_sound = m_prop.makes_footstep_sound;
1515 player->setCollisionbox(m_selection_box);
1518 if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
1519 m_prop.nametag = m_name;
1522 } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1523 // Not sent by the server if this object is an attachment.
1524 // We might however get here if the server notices the object being detached before the client.
1525 m_position = readV3F1000(is);
1526 m_velocity = readV3F1000(is);
1527 m_acceleration = readV3F1000(is);
1528 if(fabs(m_prop.automatic_rotate) < 0.001)
1529 m_yaw = readF1000(is);
1532 bool do_interpolate = readU8(is);
1533 bool is_end_position = readU8(is);
1534 float update_interval = readF1000(is);
1536 // Place us a bit higher if we're physical, to not sink into
1537 // the ground due to sucky collision detection...
1539 m_position += v3f(0,0.002,0);
1541 if(getParent() != NULL) // Just in case
1546 if(!m_prop.physical)
1547 pos_translator.update(m_position, is_end_position, update_interval);
1549 pos_translator.init(m_position);
1552 } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1553 std::string mod = deSerializeString(is);
1555 // immediatly reset a engine issued texture modifier if a mod sends a different one
1556 if (m_reset_textures_timer > 0) {
1557 m_reset_textures_timer = -1;
1558 updateTextures(m_previous_texture_modifier);
1560 updateTextures(mod);
1561 } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1562 v2s16 p = readV2S16(is);
1563 int num_frames = readU16(is);
1564 float framelength = readF1000(is);
1565 bool select_horiz_by_yawpitch = readU8(is);
1568 m_anim_num_frames = num_frames;
1569 m_anim_framelength = framelength;
1570 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1573 } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1574 float override_speed = readF1000(is);
1575 float override_jump = readF1000(is);
1576 float override_gravity = readF1000(is);
1577 // these are sent inverted so we get true when the server sends nothing
1578 bool sneak = !readU8(is);
1579 bool sneak_glitch = !readU8(is);
1580 bool new_move = !readU8(is);
1583 if(m_is_local_player)
1585 LocalPlayer *player = m_env->getLocalPlayer();
1586 player->physics_override_speed = override_speed;
1587 player->physics_override_jump = override_jump;
1588 player->physics_override_gravity = override_gravity;
1589 player->physics_override_sneak = sneak;
1590 player->physics_override_sneak_glitch = sneak_glitch;
1591 player->physics_override_new_move = new_move;
1593 } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1594 // TODO: change frames send as v2s32 value
1595 v2f range = readV2F1000(is);
1596 if (!m_is_local_player) {
1597 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1598 m_animation_speed = readF1000(is);
1599 m_animation_blend = readF1000(is);
1600 // these are sent inverted so we get true when the server sends nothing
1601 m_animation_loop = !readU8(is);
1604 LocalPlayer *player = m_env->getLocalPlayer();
1605 if(player->last_animation == NO_ANIM)
1607 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1608 m_animation_speed = readF1000(is);
1609 m_animation_blend = readF1000(is);
1610 // these are sent inverted so we get true when the server sends nothing
1611 m_animation_loop = !readU8(is);
1613 // update animation only if local animations present
1614 // and received animation is unknown (except idle animation)
1615 bool is_known = false;
1616 for (int i = 1;i<4;i++)
1618 if(m_animation_range.Y == player->local_animations[i].Y)
1622 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1627 } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1628 std::string bone = deSerializeString(is);
1629 v3f position = readV3F1000(is);
1630 v3f rotation = readV3F1000(is);
1631 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1633 updateBonePosition();
1634 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1635 u16 parentID = readS16(is);
1636 u16 oldparent = m_env->attachement_parent_ids[getId()];
1638 m_children.erase(std::remove(m_children.begin(), m_children.end(),
1639 getId()), m_children.end());
1641 m_env->attachement_parent_ids[getId()] = parentID;
1642 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1645 parentobj->m_children.push_back(getId());
1648 m_attachment_bone = deSerializeString(is);
1649 m_attachment_position = readV3F1000(is);
1650 m_attachment_rotation = readV3F1000(is);
1652 // localplayer itself can't be attached to localplayer
1653 if (!m_is_local_player) {
1654 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1655 // Objects attached to the local player should be hidden by default
1656 m_is_visible = !m_attached_to_local;
1659 updateAttachments();
1660 } else if (cmd == GENERIC_CMD_PUNCHED) {
1661 /*s16 damage =*/ readS16(is);
1662 s16 result_hp = readS16(is);
1664 // Use this instead of the send damage to not interfere with prediction
1665 s16 damage = m_hp - result_hp;
1673 // TODO: Execute defined fast response
1674 // As there is no definition, make a smoke puff
1675 ClientSimpleObject *simple = createSmokePuff(
1676 m_smgr, m_env, m_position,
1677 m_prop.visual_size * BS);
1678 m_env->addSimpleObject(simple);
1679 } else if (m_reset_textures_timer < 0) {
1680 // TODO: Execute defined fast response
1681 // Flashing shall suffice as there is no definition
1682 m_reset_textures_timer = 0.05;
1684 m_reset_textures_timer += 0.05 * damage;
1685 updateTextures(m_current_texture_modifier + "^[brighten");
1688 } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1689 m_armor_groups.clear();
1690 int armor_groups_size = readU16(is);
1691 for(int i=0; i<armor_groups_size; i++)
1693 std::string name = deSerializeString(is);
1694 int rating = readS16(is);
1695 m_armor_groups[name] = rating;
1697 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1698 // Deprecated, for backwards compatibility only.
1699 readU8(is); // version
1700 m_prop.nametag_color = readARGB8(is);
1701 if (m_nametag != NULL) {
1702 m_nametag->nametag_color = m_prop.nametag_color;
1704 } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1705 u16 child_id = readU16(is);
1706 u8 type = readU8(is);
1708 if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
1709 childobj->processInitData(deSerializeLongString(is));
1711 m_env->addActiveObject(child_id, type, deSerializeLongString(is));
1714 warningstream << FUNCTION_NAME
1715 << ": unknown command or outdated client \""
1716 << +cmd << "\"" << std::endl;
1720 /* \pre punchitem != NULL
1722 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1723 float time_from_last_punch)
1725 assert(punchitem); // pre-condition
1726 const ToolCapabilities *toolcap =
1727 &punchitem->getToolCapabilities(m_client->idef());
1728 PunchDamageResult result = getPunchDamage(
1732 time_from_last_punch);
1734 if(result.did_punch && result.damage != 0)
1736 if(result.damage < m_hp)
1738 m_hp -= result.damage;
1741 // TODO: Execute defined fast response
1742 // As there is no definition, make a smoke puff
1743 ClientSimpleObject *simple = createSmokePuff(
1744 m_smgr, m_env, m_position,
1745 m_prop.visual_size * BS);
1746 m_env->addSimpleObject(simple);
1748 // TODO: Execute defined fast response
1749 // Flashing shall suffice as there is no definition
1750 if (m_reset_textures_timer < 0) {
1751 m_reset_textures_timer = 0.05;
1752 if (result.damage >= 2)
1753 m_reset_textures_timer += 0.05 * result.damage;
1754 updateTextures(m_current_texture_modifier + "^[brighten");
1761 std::string GenericCAO::debugInfoText()
1763 std::ostringstream os(std::ios::binary);
1764 os<<"GenericCAO hp="<<m_hp<<"\n";
1766 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1767 i != m_armor_groups.end(); ++i)
1769 os<<i->first<<"="<<i->second<<", ";
1776 GenericCAO proto_GenericCAO(NULL, NULL);