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);
286 aabb3f *getSelectionBox()
287 {return &m_selection_box;}
290 inline float getYaw() const
292 std::string infoText()
295 bool getCollisionBox(aabb3f *toset) const { return false; }
297 aabb3f m_selection_box;
298 scene::IMeshSceneNode *m_node;
300 std::string m_itemstring;
301 std::string m_infotext;
305 ItemCAO proto_ItemCAO(NULL, NULL);
307 ItemCAO::ItemCAO(Client *client, ClientEnvironment *env):
308 ClientActiveObject(0, client, env),
309 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
311 m_position(v3f(0,10*BS,0))
315 ClientActiveObject::registerType(getType(), create);
323 ClientActiveObject* ItemCAO::create(Client *client, ClientEnvironment *env)
325 return new ItemCAO(client, env);
328 void ItemCAO::addToScene(ITextureSource *tsrc)
333 //video::IVideoDriver* driver = smgr->getVideoDriver();
335 scene::SMesh *mesh = new scene::SMesh();
336 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
337 video::SColor c(255,255,255,255);
338 video::S3DVertex vertices[4] =
340 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
341 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
342 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
343 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
344 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
345 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
346 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
347 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
349 u16 indices[] = {0,1,2,2,3,0};
350 buf->append(vertices, 4, indices, 6);
352 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
353 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
354 // Initialize with a generated placeholder texture
355 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
356 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
357 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
358 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
360 mesh->addMeshBuffer(buf);
362 m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
373 void ItemCAO::removeFromScene(bool permanent)
382 void ItemCAO::updateLight(u8 light_at_pos)
387 u8 li = decode_light(light_at_pos);
388 video::SColor color(255,li,li,li);
389 setMeshColor(m_node->getMesh(), color);
392 v3s16 ItemCAO::getLightPosition()
394 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
397 void ItemCAO::updateNodePos()
402 m_node->setPosition(m_position);
405 void ItemCAO::updateInfoText()
408 IItemDefManager *idef = m_client->idef();
410 item.deSerialize(m_itemstring, idef);
411 if(item.isKnown(idef))
412 m_infotext = item.getDefinition(idef).description;
414 m_infotext = "Unknown item: '" + m_itemstring + "'";
416 m_infotext += " (" + itos(item.count) + ")";
418 catch(SerializationError &e)
420 m_infotext = "Unknown item: '" + m_itemstring + "'";
424 void ItemCAO::updateTexture()
429 // Create an inventory item to see what is its image
430 std::istringstream is(m_itemstring, std::ios_base::binary);
431 video::ITexture *texture = NULL;
433 IItemDefManager *idef = m_client->idef();
435 item.deSerialize(is, idef);
436 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_client);
438 catch(SerializationError &e)
440 warningstream<<FUNCTION_NAME
441 <<": error deSerializing itemstring \""
442 <<m_itemstring<<std::endl;
445 // Set meshbuffer texture
446 m_node->getMaterial(0).setTexture(0, texture);
450 void ItemCAO::step(float dtime, ClientEnvironment *env)
454 /*v3f rot = m_node->getRotation();
455 rot.Y += dtime * 120;
456 m_node->setRotation(rot);*/
457 LocalPlayer *player = env->getLocalPlayer();
459 v3f rot = m_node->getRotation();
460 rot.Y = 180.0 - (player->getYaw());
461 m_node->setRotation(rot);
465 void ItemCAO::processMessage(const std::string &data)
467 //infostream<<"ItemCAO: Got message"<<std::endl;
468 std::istringstream is(data, std::ios::binary);
474 m_position = readV3F1000(is);
480 m_itemstring = deSerializeString(is);
486 void ItemCAO::initialize(const std::string &data)
488 infostream<<"ItemCAO: Got init data"<<std::endl;
491 std::istringstream is(data, std::ios::binary);
493 u8 version = readU8(is);
498 m_position = readV3F1000(is);
500 m_itemstring = deSerializeString(is);
511 #include "genericobject.h"
513 GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
514 ClientActiveObject(0, client, env)
516 if (client == NULL) {
517 ClientActiveObject::registerType(getType(), create);
523 bool GenericCAO::getCollisionBox(aabb3f *toset) const
527 //update collision box
528 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
529 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
531 toset->MinEdge += m_position;
532 toset->MaxEdge += m_position;
540 bool GenericCAO::collideWithObjects() const
542 return m_prop.collideWithObjects;
545 void GenericCAO::initialize(const std::string &data)
547 infostream<<"GenericCAO: Got init data"<<std::endl;
548 processInitData(data);
551 // Check if it's the current player
552 LocalPlayer *player = m_env->getLocalPlayer();
553 if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
554 m_is_local_player = true;
555 m_is_visible = false;
556 player->setCAO(this);
558 if (m_client->getProtoVersion() < 33)
559 m_env->addPlayerName(m_name.c_str());
563 void GenericCAO::processInitData(const std::string &data)
565 std::istringstream is(data, std::ios::binary);
566 int num_messages = 0;
568 u8 version = readU8(is);
570 if (version == 1) { // In PROTOCOL_VERSION 14
571 m_name = deSerializeString(is);
572 m_is_player = readU8(is);
574 m_position = readV3F1000(is);
575 m_yaw = readF1000(is);
577 num_messages = readU8(is);
578 } else if (version == 0) { // In PROTOCOL_VERSION 13
579 m_name = deSerializeString(is);
580 m_is_player = readU8(is);
581 m_position = readV3F1000(is);
582 m_yaw = readF1000(is);
584 num_messages = readU8(is);
586 errorstream<<"GenericCAO: Unsupported init data version"
591 for (int i = 0; i < num_messages; i++) {
592 std::string message = deSerializeLongString(is);
593 processMessage(message);
596 pos_translator.init(m_position);
600 GenericCAO::~GenericCAO()
602 if (m_is_player && m_client->getProtoVersion() < 33) {
603 m_env->removePlayerName(m_name.c_str());
605 removeFromScene(true);
608 aabb3f *GenericCAO::getSelectionBox()
610 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
612 return &m_selection_box;
615 v3f GenericCAO::getPosition()
617 if (getParent() != NULL) {
618 scene::ISceneNode *node = getSceneNode();
620 return node->getAbsolutePosition();
624 return pos_translator.vect_show;
627 scene::ISceneNode* GenericCAO::getSceneNode()
631 } else if (m_animated_meshnode) {
632 return m_animated_meshnode;
633 } else if (m_wield_meshnode) {
634 return m_wield_meshnode;
635 } else if (m_spritenode) {
641 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
643 return m_animated_meshnode;
646 void GenericCAO::setChildrenVisible(bool toset)
648 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
649 GenericCAO *obj = m_env->getGenericCAO(m_children[i]);
651 obj->setVisible(toset);
656 void GenericCAO::setAttachments()
661 ClientActiveObject* GenericCAO::getParent()
663 ClientActiveObject *obj = NULL;
665 u16 attached_id = m_env->attachement_parent_ids[getId()];
667 if ((attached_id != 0) &&
668 (attached_id != getId())) {
669 obj = m_env->getActiveObject(attached_id);
674 void GenericCAO::removeFromScene(bool permanent)
676 // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
677 if((m_env != NULL) && (permanent))
679 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
680 u16 ci = m_children[i];
681 if (m_env->attachement_parent_ids[ci] == getId()) {
682 m_env->attachement_parent_ids[ci] = 0;
686 m_env->attachement_parent_ids[getId()] = 0;
688 LocalPlayer* player = m_env->getLocalPlayer();
689 if (this == player->parent) {
690 player->parent = NULL;
691 player->isAttached = false;
696 m_meshnode->remove();
699 } else if (m_animated_meshnode) {
700 m_animated_meshnode->remove();
701 m_animated_meshnode->drop();
702 m_animated_meshnode = NULL;
703 } else if (m_wield_meshnode) {
704 m_wield_meshnode->remove();
705 m_wield_meshnode->drop();
706 m_wield_meshnode = NULL;
707 } else if (m_spritenode) {
708 m_spritenode->remove();
709 m_spritenode->drop();
714 m_client->getCamera()->removeNametag(m_nametag);
719 void GenericCAO::addToScene(ITextureSource *tsrc)
721 m_smgr = RenderingEngine::get_scene_manager();
723 if (getSceneNode() != NULL) {
727 m_visuals_expired = false;
729 if (!m_prop.is_visible) {
733 if (m_prop.visual == "sprite") {
734 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
735 m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
736 NULL, v2f(1, 1), v3f(0,0,0), -1);
737 m_spritenode->grab();
738 m_spritenode->setMaterialTexture(0,
739 tsrc->getTextureForMesh("unknown_node.png"));
740 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
741 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
742 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
743 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
744 u8 li = m_last_light;
745 m_spritenode->setColor(video::SColor(255,li,li,li));
746 m_spritenode->setSize(m_prop.visual_size*BS);
748 const float txs = 1.0 / 1;
749 const float tys = 1.0 / 1;
750 setBillboardTextureMatrix(m_spritenode,
753 } else if (m_prop.visual == "upright_sprite") {
754 scene::SMesh *mesh = new scene::SMesh();
755 double dx = BS * m_prop.visual_size.X / 2;
756 double dy = BS * m_prop.visual_size.Y / 2;
757 u8 li = m_last_light;
758 video::SColor c(255, li, li, li);
761 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
762 video::S3DVertex vertices[4] = {
763 video::S3DVertex(-dx, -dy, 0, 0,0,0, c, 1,1),
764 video::S3DVertex( dx, -dy, 0, 0,0,0, c, 0,1),
765 video::S3DVertex( dx, dy, 0, 0,0,0, c, 0,0),
766 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 1,0),
768 u16 indices[] = {0,1,2,2,3,0};
769 buf->append(vertices, 4, indices, 6);
771 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
772 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
773 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
774 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
776 mesh->addMeshBuffer(buf);
780 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
781 video::S3DVertex vertices[4] = {
782 video::S3DVertex( dx,-dy, 0, 0,0,0, c, 1,1),
783 video::S3DVertex(-dx,-dy, 0, 0,0,0, c, 0,1),
784 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 0,0),
785 video::S3DVertex( dx, dy, 0, 0,0,0, c, 1,0),
787 u16 indices[] = {0,1,2,2,3,0};
788 buf->append(vertices, 4, indices, 6);
790 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
791 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
792 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
793 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
795 mesh->addMeshBuffer(buf);
798 m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
801 // Set it to use the materials of the meshbuffers directly.
802 // This is needed for changing the texture in the future
803 m_meshnode->setReadOnlyMaterials(true);
805 else if(m_prop.visual == "cube") {
806 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
807 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
808 m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
812 m_meshnode->setScale(v3f(m_prop.visual_size.X,
813 m_prop.visual_size.Y,
814 m_prop.visual_size.X));
815 u8 li = m_last_light;
816 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
818 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
819 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
820 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
821 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
823 else if(m_prop.visual == "mesh") {
824 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
825 scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh);
828 m_animated_meshnode = RenderingEngine::get_scene_manager()->
829 addAnimatedMeshSceneNode(mesh, NULL);
830 m_animated_meshnode->grab();
831 mesh->drop(); // The scene node took hold of it
832 m_animated_meshnode->animateJoints(); // Needed for some animations
833 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
834 m_prop.visual_size.Y,
835 m_prop.visual_size.X));
836 u8 li = m_last_light;
837 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
839 bool backface_culling = m_prop.backface_culling;
841 backface_culling = false;
843 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
844 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
845 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
846 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
847 m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
850 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
852 else if(m_prop.visual == "wielditem") {
854 infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
855 if (m_prop.wield_item == "") {
856 // Old format, only textures are specified.
857 infostream << "textures: " << m_prop.textures.size() << std::endl;
858 if (m_prop.textures.size() >= 1) {
859 infostream << "textures[0]: " << m_prop.textures[0]
861 IItemDefManager *idef = m_client->idef();
862 item = ItemStack(m_prop.textures[0], 1, 0, idef);
865 infostream << "serialized form: " << m_prop.wield_item << std::endl;
866 item.deSerialize(m_prop.wield_item, m_client->idef());
868 m_wield_meshnode = new WieldMeshSceneNode(
869 RenderingEngine::get_scene_manager(), -1);
870 m_wield_meshnode->setItem(item, m_client);
872 m_wield_meshnode->setScale(
873 v3f(m_prop.visual_size.X / 2, m_prop.visual_size.Y / 2,
874 m_prop.visual_size.X / 2));
875 u8 li = m_last_light;
876 m_wield_meshnode->setColor(video::SColor(255, li, li, li));
878 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
879 <<"\" not supported"<<std::endl;
882 /* don't update while punch texture modifier is active */
883 if (m_reset_textures_timer < 0)
884 updateTextures(m_current_texture_modifier);
886 scene::ISceneNode *node = getSceneNode();
887 if (node && m_prop.nametag != "" && !m_is_local_player) {
889 m_nametag = m_client->getCamera()->addNametag(node,
890 m_prop.nametag, m_prop.nametag_color);
895 updateBonePosition();
899 void GenericCAO::updateLight(u8 light_at_pos)
901 // Don't update light of attached one
902 if (getParent() != NULL) {
906 updateLightNoCheck(light_at_pos);
908 // Update light of all children
909 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
910 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
912 obj->updateLightNoCheck(light_at_pos);
917 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
919 u8 li = decode_light(light_at_pos);
920 if (li != m_last_light) {
922 video::SColor color(255,li,li,li);
924 setMeshColor(m_meshnode->getMesh(), color);
925 } else if (m_animated_meshnode) {
926 setMeshColor(m_animated_meshnode->getMesh(), color);
927 } else if (m_wield_meshnode) {
928 m_wield_meshnode->setColor(color);
929 } else if (m_spritenode) {
930 m_spritenode->setColor(color);
935 v3s16 GenericCAO::getLightPosition()
937 return floatToInt(m_position, BS);
940 void GenericCAO::updateNodePos()
942 if (getParent() != NULL)
945 scene::ISceneNode *node = getSceneNode();
948 v3s16 camera_offset = m_env->getCameraOffset();
949 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
950 if (node != m_spritenode) { // rotate if not a sprite
951 v3f rot = node->getRotation();
953 node->setRotation(rot);
958 void GenericCAO::step(float dtime, ClientEnvironment *env)
960 // Handel model of local player instantly to prevent lags
961 if (m_is_local_player) {
962 LocalPlayer *player = m_env->getLocalPlayer();
964 int old_anim = player->last_animation;
965 float old_anim_speed = player->last_animation_speed;
966 m_position = player->getPosition() + v3f(0,BS,0);
967 m_velocity = v3f(0,0,0);
968 m_acceleration = v3f(0,0,0);
969 pos_translator.vect_show = m_position;
970 m_yaw = player->getYaw();
971 const PlayerControl &controls = player->getPlayerControl();
973 bool walking = false;
974 if (controls.up || controls.down || controls.left || controls.right ||
975 controls.forw_move_joystick_axis != 0.f ||
976 controls.sidew_move_joystick_axis != 0.f)
979 f32 new_speed = player->local_animation_speed;
980 v2s32 new_anim = v2s32(0,0);
981 bool allow_update = false;
983 // increase speed if using fast or flying fast
984 if((g_settings->getBool("fast_move") &&
985 m_client->checkLocalPrivilege("fast")) &&
987 (!player->touching_ground &&
988 g_settings->getBool("free_move") &&
989 m_client->checkLocalPrivilege("fly"))))
991 // slowdown speed if sneeking
992 if (controls.sneak && walking)
995 if (walking && (controls.LMB || controls.RMB)) {
996 new_anim = player->local_animations[3];
997 player->last_animation = WD_ANIM;
999 new_anim = player->local_animations[1];
1000 player->last_animation = WALK_ANIM;
1001 } else if(controls.LMB || controls.RMB) {
1002 new_anim = player->local_animations[2];
1003 player->last_animation = DIG_ANIM;
1006 // Apply animations if input detected and not attached
1007 // or set idle animation
1008 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
1009 allow_update = true;
1010 m_animation_range = new_anim;
1011 m_animation_speed = new_speed;
1012 player->last_animation_speed = m_animation_speed;
1014 player->last_animation = NO_ANIM;
1016 if (old_anim != NO_ANIM) {
1017 m_animation_range = player->local_animations[0];
1022 // Update local player animations
1023 if ((player->last_animation != old_anim ||
1024 m_animation_speed != old_anim_speed) &&
1025 player->last_animation != NO_ANIM && allow_update)
1031 if (m_visuals_expired && m_smgr) {
1032 m_visuals_expired = false;
1034 // Attachments, part 1: All attached objects must be unparented first,
1035 // or Irrlicht causes a segmentation fault
1036 for(std::vector<u16>::iterator ci = m_children.begin();
1037 ci != m_children.end();)
1039 if (m_env->attachement_parent_ids[*ci] != getId()) {
1040 ci = m_children.erase(ci);
1043 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1045 scene::ISceneNode *child_node = obj->getSceneNode();
1047 child_node->setParent(m_smgr->getRootSceneNode());
1052 removeFromScene(false);
1053 addToScene(m_client->tsrc());
1055 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1056 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1057 // Get the object of the child
1058 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1060 obj->setAttachments();
1064 // Make sure m_is_visible is always applied
1065 scene::ISceneNode *node = getSceneNode();
1067 node->setVisible(m_is_visible);
1069 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1071 // Set these for later
1072 m_position = getPosition();
1073 m_velocity = v3f(0,0,0);
1074 m_acceleration = v3f(0,0,0);
1075 pos_translator.vect_show = m_position;
1077 if(m_is_local_player) // Update local player attachment position
1079 LocalPlayer *player = m_env->getLocalPlayer();
1080 player->overridePosition = getParent()->getPosition();
1081 m_env->getLocalPlayer()->parent = getParent();
1084 v3f lastpos = pos_translator.vect_show;
1088 aabb3f box = m_prop.collisionbox;
1091 collisionMoveResult moveresult;
1092 f32 pos_max_d = BS*0.125; // Distance per iteration
1093 v3f p_pos = m_position;
1094 v3f p_velocity = m_velocity;
1095 moveresult = collisionMoveSimple(env,env->getGameDef(),
1096 pos_max_d, box, m_prop.stepheight, dtime,
1097 &p_pos, &p_velocity, m_acceleration,
1098 this, m_prop.collideWithObjects);
1101 m_velocity = p_velocity;
1103 bool is_end_position = moveresult.collides;
1104 pos_translator.update(m_position, is_end_position, dtime);
1105 pos_translator.translate(dtime);
1108 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1109 m_velocity += dtime * m_acceleration;
1110 pos_translator.update(m_position, pos_translator.aim_is_end,
1111 pos_translator.anim_time);
1112 pos_translator.translate(dtime);
1116 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1117 m_step_distance_counter += moved;
1118 if(m_step_distance_counter > 1.5*BS)
1120 m_step_distance_counter = 0;
1121 if(!m_is_local_player && m_prop.makes_footstep_sound)
1123 INodeDefManager *ndef = m_client->ndef();
1124 v3s16 p = floatToInt(getPosition() + v3f(0,
1125 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1126 MapNode n = m_env->getMap().getNodeNoEx(p);
1127 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1128 m_client->sound()->playSoundAt(spec, false, getPosition());
1133 m_anim_timer += dtime;
1134 if(m_anim_timer >= m_anim_framelength)
1136 m_anim_timer -= m_anim_framelength;
1138 if(m_anim_frame >= m_anim_num_frames)
1144 if(m_reset_textures_timer >= 0)
1146 m_reset_textures_timer -= dtime;
1147 if(m_reset_textures_timer <= 0) {
1148 m_reset_textures_timer = -1;
1149 updateTextures(m_previous_texture_modifier);
1152 if(!getParent() && fabs(m_prop.automatic_rotate) > 0.001)
1154 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1158 if (!getParent() && m_prop.automatic_face_movement_dir &&
1159 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1161 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1162 + m_prop.automatic_face_movement_dir_offset;
1163 float max_rotation_delta =
1164 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1166 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
1167 (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
1169 m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
1171 m_yaw = optimal_yaw;
1177 void GenericCAO::updateTexturePos()
1181 scene::ICameraSceneNode* camera =
1182 m_spritenode->getSceneManager()->getActiveCamera();
1185 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1186 - camera->getAbsolutePosition();
1187 cam_to_entity.normalize();
1189 int row = m_tx_basepos.Y;
1190 int col = m_tx_basepos.X;
1192 if(m_tx_select_horiz_by_yawpitch)
1194 if(cam_to_entity.Y > 0.75)
1196 else if(cam_to_entity.Y < -0.75)
1200 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1201 float dir = mob_dir - m_yaw;
1202 dir = wrapDegrees_180(dir);
1203 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1204 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1206 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1208 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1210 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1217 // Animation goes downwards
1218 row += m_anim_frame;
1220 float txs = m_tx_size.X;
1221 float tys = m_tx_size.Y;
1222 setBillboardTextureMatrix(m_spritenode,
1223 txs, tys, col, row);
1227 void GenericCAO::updateTextures(std::string mod)
1229 ITextureSource *tsrc = m_client->tsrc();
1231 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1232 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1233 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1235 m_previous_texture_modifier = m_current_texture_modifier;
1236 m_current_texture_modifier = mod;
1240 if(m_prop.visual == "sprite")
1242 std::string texturestring = "unknown_node.png";
1243 if(m_prop.textures.size() >= 1)
1244 texturestring = m_prop.textures[0];
1245 texturestring += mod;
1246 m_spritenode->setMaterialTexture(0,
1247 tsrc->getTextureForMesh(texturestring));
1249 // This allows setting per-material colors. However, until a real lighting
1250 // system is added, the code below will have no effect. Once MineTest
1251 // has directional lighting, it should work automatically.
1252 if(m_prop.colors.size() >= 1)
1254 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1255 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1256 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1259 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1260 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1261 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1264 if(m_animated_meshnode)
1266 if(m_prop.visual == "mesh")
1268 for (u32 i = 0; i < m_prop.textures.size() &&
1269 i < m_animated_meshnode->getMaterialCount(); ++i)
1271 std::string texturestring = m_prop.textures[i];
1272 if(texturestring == "")
1273 continue; // Empty texture string means don't modify that material
1274 texturestring += mod;
1275 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1278 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1282 // Set material flags and texture
1283 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1284 material.TextureLayer[0].Texture = texture;
1285 material.setFlag(video::EMF_LIGHTING, false);
1286 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1288 m_animated_meshnode->getMaterial(i)
1289 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1290 m_animated_meshnode->getMaterial(i)
1291 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1292 m_animated_meshnode->getMaterial(i)
1293 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1295 for (u32 i = 0; i < m_prop.colors.size() &&
1296 i < m_animated_meshnode->getMaterialCount(); ++i)
1298 // This allows setting per-material colors. However, until a real lighting
1299 // system is added, the code below will have no effect. Once MineTest
1300 // has directional lighting, it should work automatically.
1301 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1302 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1303 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1309 if(m_prop.visual == "cube")
1311 for (u32 i = 0; i < 6; ++i)
1313 std::string texturestring = "unknown_node.png";
1314 if(m_prop.textures.size() > i)
1315 texturestring = m_prop.textures[i];
1316 texturestring += mod;
1319 // Set material flags and texture
1320 video::SMaterial& material = m_meshnode->getMaterial(i);
1321 material.setFlag(video::EMF_LIGHTING, false);
1322 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1323 material.setTexture(0,
1324 tsrc->getTextureForMesh(texturestring));
1325 material.getTextureMatrix(0).makeIdentity();
1327 // This allows setting per-material colors. However, until a real lighting
1328 // system is added, the code below will have no effect. Once MineTest
1329 // has directional lighting, it should work automatically.
1330 if(m_prop.colors.size() > i)
1332 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1333 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1334 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1337 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1338 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1339 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1342 else if(m_prop.visual == "upright_sprite")
1344 scene::IMesh *mesh = m_meshnode->getMesh();
1346 std::string tname = "unknown_object.png";
1347 if(m_prop.textures.size() >= 1)
1348 tname = m_prop.textures[0];
1350 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1351 buf->getMaterial().setTexture(0,
1352 tsrc->getTextureForMesh(tname));
1354 // This allows setting per-material colors. However, until a real lighting
1355 // system is added, the code below will have no effect. Once MineTest
1356 // has directional lighting, it should work automatically.
1357 if(m_prop.colors.size() >= 1)
1359 buf->getMaterial().AmbientColor = m_prop.colors[0];
1360 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1361 buf->getMaterial().SpecularColor = m_prop.colors[0];
1364 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1365 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1366 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1369 std::string tname = "unknown_object.png";
1370 if(m_prop.textures.size() >= 2)
1371 tname = m_prop.textures[1];
1372 else if(m_prop.textures.size() >= 1)
1373 tname = m_prop.textures[0];
1375 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1376 buf->getMaterial().setTexture(0,
1377 tsrc->getTextureForMesh(tname));
1379 // This allows setting per-material colors. However, until a real lighting
1380 // system is added, the code below will have no effect. Once MineTest
1381 // has directional lighting, it should work automatically.
1382 if(m_prop.colors.size() >= 2)
1384 buf->getMaterial().AmbientColor = m_prop.colors[1];
1385 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1386 buf->getMaterial().SpecularColor = m_prop.colors[1];
1388 else if(m_prop.colors.size() >= 1)
1390 buf->getMaterial().AmbientColor = m_prop.colors[0];
1391 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1392 buf->getMaterial().SpecularColor = m_prop.colors[0];
1395 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1396 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1397 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1403 void GenericCAO::updateAnimation()
1405 if (!m_animated_meshnode)
1408 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1409 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1410 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1411 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1412 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1413 m_animated_meshnode->setTransitionTime(m_animation_blend);
1414 // Requires Irrlicht 1.8 or greater
1415 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1416 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1417 m_animated_meshnode->setLoopMode(m_animation_loop);
1421 void GenericCAO::updateBonePosition()
1423 if(m_bone_position.empty() || !m_animated_meshnode)
1426 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1427 for(std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
1428 ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1429 std::string bone_name = (*ii).first;
1430 v3f bone_pos = (*ii).second.X;
1431 v3f bone_rot = (*ii).second.Y;
1432 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1435 bone->setPosition(bone_pos);
1436 bone->setRotation(bone_rot);
1441 void GenericCAO::updateAttachments()
1444 if (!getParent()) { // Detach or don't attach
1445 scene::ISceneNode *node = getSceneNode();
1447 v3f old_position = node->getAbsolutePosition();
1448 v3f old_rotation = node->getRotation();
1449 node->setParent(m_smgr->getRootSceneNode());
1450 node->setPosition(old_position);
1451 node->setRotation(old_rotation);
1452 node->updateAbsolutePosition();
1454 if (m_is_local_player) {
1455 LocalPlayer *player = m_env->getLocalPlayer();
1456 player->isAttached = false;
1461 scene::ISceneNode *my_node = getSceneNode();
1463 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1464 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1465 getParent()->getAnimatedMeshSceneNode();
1466 if (parent_animated_mesh_node && m_attachment_bone != "") {
1467 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1470 if (my_node && parent_node) {
1471 my_node->setParent(parent_node);
1472 my_node->setPosition(m_attachment_position);
1473 my_node->setRotation(m_attachment_rotation);
1474 my_node->updateAbsolutePosition();
1476 if (m_is_local_player) {
1477 LocalPlayer *player = m_env->getLocalPlayer();
1478 player->isAttached = true;
1483 void GenericCAO::processMessage(const std::string &data)
1485 //infostream<<"GenericCAO: Got message"<<std::endl;
1486 std::istringstream is(data, std::ios::binary);
1488 u8 cmd = readU8(is);
1489 if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1490 m_prop = gob_read_set_properties(is);
1492 m_selection_box = m_prop.collisionbox;
1493 m_selection_box.MinEdge *= BS;
1494 m_selection_box.MaxEdge *= BS;
1496 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1497 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1499 if(!m_initial_tx_basepos_set){
1500 m_initial_tx_basepos_set = true;
1501 m_tx_basepos = m_prop.initial_sprite_basepos;
1503 if (m_is_local_player) {
1504 LocalPlayer *player = m_env->getLocalPlayer();
1505 player->makes_footstep_sound = m_prop.makes_footstep_sound;
1508 if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
1509 m_prop.nametag = m_name;
1512 } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1513 // Not sent by the server if this object is an attachment.
1514 // We might however get here if the server notices the object being detached before the client.
1515 m_position = readV3F1000(is);
1516 m_velocity = readV3F1000(is);
1517 m_acceleration = readV3F1000(is);
1518 if(fabs(m_prop.automatic_rotate) < 0.001)
1519 m_yaw = readF1000(is);
1522 bool do_interpolate = readU8(is);
1523 bool is_end_position = readU8(is);
1524 float update_interval = readF1000(is);
1526 // Place us a bit higher if we're physical, to not sink into
1527 // the ground due to sucky collision detection...
1529 m_position += v3f(0,0.002,0);
1531 if(getParent() != NULL) // Just in case
1536 if(!m_prop.physical)
1537 pos_translator.update(m_position, is_end_position, update_interval);
1539 pos_translator.init(m_position);
1542 } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1543 std::string mod = deSerializeString(is);
1545 // immediatly reset a engine issued texture modifier if a mod sends a different one
1546 if (m_reset_textures_timer > 0) {
1547 m_reset_textures_timer = -1;
1548 updateTextures(m_previous_texture_modifier);
1550 updateTextures(mod);
1551 } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1552 v2s16 p = readV2S16(is);
1553 int num_frames = readU16(is);
1554 float framelength = readF1000(is);
1555 bool select_horiz_by_yawpitch = readU8(is);
1558 m_anim_num_frames = num_frames;
1559 m_anim_framelength = framelength;
1560 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1563 } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1564 float override_speed = readF1000(is);
1565 float override_jump = readF1000(is);
1566 float override_gravity = readF1000(is);
1567 // these are sent inverted so we get true when the server sends nothing
1568 bool sneak = !readU8(is);
1569 bool sneak_glitch = !readU8(is);
1570 bool new_move = !readU8(is);
1573 if(m_is_local_player)
1575 LocalPlayer *player = m_env->getLocalPlayer();
1576 player->physics_override_speed = override_speed;
1577 player->physics_override_jump = override_jump;
1578 player->physics_override_gravity = override_gravity;
1579 player->physics_override_sneak = sneak;
1580 player->physics_override_sneak_glitch = sneak_glitch;
1581 player->physics_override_new_move = new_move;
1583 } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1584 // TODO: change frames send as v2s32 value
1585 v2f range = readV2F1000(is);
1586 if (!m_is_local_player) {
1587 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1588 m_animation_speed = readF1000(is);
1589 m_animation_blend = readF1000(is);
1590 // these are sent inverted so we get true when the server sends nothing
1591 m_animation_loop = !readU8(is);
1594 LocalPlayer *player = m_env->getLocalPlayer();
1595 if(player->last_animation == NO_ANIM)
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);
1603 // update animation only if local animations present
1604 // and received animation is unknown (except idle animation)
1605 bool is_known = false;
1606 for (int i = 1;i<4;i++)
1608 if(m_animation_range.Y == player->local_animations[i].Y)
1612 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1617 } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1618 std::string bone = deSerializeString(is);
1619 v3f position = readV3F1000(is);
1620 v3f rotation = readV3F1000(is);
1621 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1623 updateBonePosition();
1624 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1625 u16 parentID = readS16(is);
1626 u16 oldparent = m_env->attachement_parent_ids[getId()];
1628 m_children.erase(std::remove(m_children.begin(), m_children.end(),
1629 getId()), m_children.end());
1631 m_env->attachement_parent_ids[getId()] = parentID;
1632 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1635 parentobj->m_children.push_back(getId());
1638 m_attachment_bone = deSerializeString(is);
1639 m_attachment_position = readV3F1000(is);
1640 m_attachment_rotation = readV3F1000(is);
1642 // localplayer itself can't be attached to localplayer
1643 if (!m_is_local_player) {
1644 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1645 // Objects attached to the local player should be hidden by default
1646 m_is_visible = !m_attached_to_local;
1649 updateAttachments();
1650 } else if (cmd == GENERIC_CMD_PUNCHED) {
1651 /*s16 damage =*/ readS16(is);
1652 s16 result_hp = readS16(is);
1654 // Use this instead of the send damage to not interfere with prediction
1655 s16 damage = m_hp - result_hp;
1663 // TODO: Execute defined fast response
1664 // As there is no definition, make a smoke puff
1665 ClientSimpleObject *simple = createSmokePuff(
1666 m_smgr, m_env, m_position,
1667 m_prop.visual_size * BS);
1668 m_env->addSimpleObject(simple);
1669 } else if (m_reset_textures_timer < 0) {
1670 // TODO: Execute defined fast response
1671 // Flashing shall suffice as there is no definition
1672 m_reset_textures_timer = 0.05;
1674 m_reset_textures_timer += 0.05 * damage;
1675 updateTextures(m_current_texture_modifier + "^[brighten");
1678 } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1679 m_armor_groups.clear();
1680 int armor_groups_size = readU16(is);
1681 for(int i=0; i<armor_groups_size; i++)
1683 std::string name = deSerializeString(is);
1684 int rating = readS16(is);
1685 m_armor_groups[name] = rating;
1687 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1688 // Deprecated, for backwards compatibility only.
1689 readU8(is); // version
1690 m_prop.nametag_color = readARGB8(is);
1691 if (m_nametag != NULL) {
1692 m_nametag->nametag_color = m_prop.nametag_color;
1694 } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1695 u16 child_id = readU16(is);
1696 u8 type = readU8(is);
1698 if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
1699 childobj->processInitData(deSerializeLongString(is));
1701 m_env->addActiveObject(child_id, type, deSerializeLongString(is));
1704 warningstream << FUNCTION_NAME
1705 << ": unknown command or outdated client \""
1706 << +cmd << "\"" << std::endl;
1710 /* \pre punchitem != NULL
1712 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1713 float time_from_last_punch)
1715 assert(punchitem); // pre-condition
1716 const ToolCapabilities *toolcap =
1717 &punchitem->getToolCapabilities(m_client->idef());
1718 PunchDamageResult result = getPunchDamage(
1722 time_from_last_punch);
1724 if(result.did_punch && result.damage != 0)
1726 if(result.damage < m_hp)
1728 m_hp -= result.damage;
1731 // TODO: Execute defined fast response
1732 // As there is no definition, make a smoke puff
1733 ClientSimpleObject *simple = createSmokePuff(
1734 m_smgr, m_env, m_position,
1735 m_prop.visual_size * BS);
1736 m_env->addSimpleObject(simple);
1738 // TODO: Execute defined fast response
1739 // Flashing shall suffice as there is no definition
1740 if (m_reset_textures_timer < 0) {
1741 m_reset_textures_timer = 0.05;
1742 if (result.damage >= 2)
1743 m_reset_textures_timer += 0.05 * result.damage;
1744 updateTextures(m_current_texture_modifier + "^[brighten");
1751 std::string GenericCAO::debugInfoText()
1753 std::ostringstream os(std::ios::binary);
1754 os<<"GenericCAO hp="<<m_hp<<"\n";
1756 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1757 i != m_armor_groups.end(); ++i)
1759 os<<i->first<<"="<<i->second<<", ";
1766 GenericCAO proto_GenericCAO(NULL, NULL);