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) {
899 pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
900 m_nametag = m_client->getCamera()->addNametag(node,
901 m_prop.nametag, m_prop.nametag_color,
907 updateBonePosition();
911 void GenericCAO::updateLight(u8 light_at_pos)
913 // Don't update light of attached one
914 if (getParent() != NULL) {
918 updateLightNoCheck(light_at_pos);
920 // Update light of all children
921 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
922 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
924 obj->updateLightNoCheck(light_at_pos);
929 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
931 u8 li = decode_light(light_at_pos);
932 if (li != m_last_light) {
934 video::SColor color(255,li,li,li);
936 setMeshColor(m_meshnode->getMesh(), color);
937 } else if (m_animated_meshnode) {
938 setMeshColor(m_animated_meshnode->getMesh(), color);
939 } else if (m_wield_meshnode) {
940 m_wield_meshnode->setColor(color);
941 } else if (m_spritenode) {
942 m_spritenode->setColor(color);
947 v3s16 GenericCAO::getLightPosition()
949 return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
952 void GenericCAO::updateNodePos()
954 if (getParent() != NULL)
957 scene::ISceneNode *node = getSceneNode();
960 v3s16 camera_offset = m_env->getCameraOffset();
961 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
962 if (node != m_spritenode) { // rotate if not a sprite
963 v3f rot = node->getRotation();
965 node->setRotation(rot);
970 void GenericCAO::step(float dtime, ClientEnvironment *env)
972 // Handel model of local player instantly to prevent lags
973 if (m_is_local_player) {
974 LocalPlayer *player = m_env->getLocalPlayer();
976 int old_anim = player->last_animation;
977 float old_anim_speed = player->last_animation_speed;
978 m_position = player->getPosition();
979 m_velocity = v3f(0,0,0);
980 m_acceleration = v3f(0,0,0);
981 pos_translator.vect_show = m_position;
982 m_yaw = player->getYaw();
983 const PlayerControl &controls = player->getPlayerControl();
985 bool walking = false;
986 if (controls.up || controls.down || controls.left || controls.right ||
987 controls.forw_move_joystick_axis != 0.f ||
988 controls.sidew_move_joystick_axis != 0.f)
991 f32 new_speed = player->local_animation_speed;
992 v2s32 new_anim = v2s32(0,0);
993 bool allow_update = false;
995 // increase speed if using fast or flying fast
996 if((g_settings->getBool("fast_move") &&
997 m_client->checkLocalPrivilege("fast")) &&
999 (!player->touching_ground &&
1000 g_settings->getBool("free_move") &&
1001 m_client->checkLocalPrivilege("fly"))))
1003 // slowdown speed if sneeking
1004 if (controls.sneak && walking)
1007 if (walking && (controls.LMB || controls.RMB)) {
1008 new_anim = player->local_animations[3];
1009 player->last_animation = WD_ANIM;
1010 } else if(walking) {
1011 new_anim = player->local_animations[1];
1012 player->last_animation = WALK_ANIM;
1013 } else if(controls.LMB || controls.RMB) {
1014 new_anim = player->local_animations[2];
1015 player->last_animation = DIG_ANIM;
1018 // Apply animations if input detected and not attached
1019 // or set idle animation
1020 if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
1021 allow_update = true;
1022 m_animation_range = new_anim;
1023 m_animation_speed = new_speed;
1024 player->last_animation_speed = m_animation_speed;
1026 player->last_animation = NO_ANIM;
1028 if (old_anim != NO_ANIM) {
1029 m_animation_range = player->local_animations[0];
1034 // Update local player animations
1035 if ((player->last_animation != old_anim ||
1036 m_animation_speed != old_anim_speed) &&
1037 player->last_animation != NO_ANIM && allow_update)
1043 if (m_visuals_expired && m_smgr) {
1044 m_visuals_expired = false;
1046 // Attachments, part 1: All attached objects must be unparented first,
1047 // or Irrlicht causes a segmentation fault
1048 for(std::vector<u16>::iterator ci = m_children.begin();
1049 ci != m_children.end();)
1051 if (m_env->attachement_parent_ids[*ci] != getId()) {
1052 ci = m_children.erase(ci);
1055 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1057 scene::ISceneNode *child_node = obj->getSceneNode();
1059 child_node->setParent(m_smgr->getRootSceneNode());
1064 removeFromScene(false);
1065 addToScene(m_client->tsrc());
1067 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1068 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1069 // Get the object of the child
1070 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1072 obj->setAttachments();
1076 // Make sure m_is_visible is always applied
1077 scene::ISceneNode *node = getSceneNode();
1079 node->setVisible(m_is_visible);
1081 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1083 // Set these for later
1084 m_position = getPosition();
1085 m_velocity = v3f(0,0,0);
1086 m_acceleration = v3f(0,0,0);
1087 pos_translator.vect_show = m_position;
1089 if(m_is_local_player) // Update local player attachment position
1091 LocalPlayer *player = m_env->getLocalPlayer();
1092 player->overridePosition = getParent()->getPosition();
1093 m_env->getLocalPlayer()->parent = getParent();
1096 v3f lastpos = pos_translator.vect_show;
1100 aabb3f box = m_prop.collisionbox;
1103 collisionMoveResult moveresult;
1104 f32 pos_max_d = BS*0.125; // Distance per iteration
1105 v3f p_pos = m_position;
1106 v3f p_velocity = m_velocity;
1107 moveresult = collisionMoveSimple(env,env->getGameDef(),
1108 pos_max_d, box, m_prop.stepheight, dtime,
1109 &p_pos, &p_velocity, m_acceleration,
1110 this, m_prop.collideWithObjects);
1113 m_velocity = p_velocity;
1115 bool is_end_position = moveresult.collides;
1116 pos_translator.update(m_position, is_end_position, dtime);
1117 pos_translator.translate(dtime);
1120 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1121 m_velocity += dtime * m_acceleration;
1122 pos_translator.update(m_position, pos_translator.aim_is_end,
1123 pos_translator.anim_time);
1124 pos_translator.translate(dtime);
1128 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1129 m_step_distance_counter += moved;
1130 if(m_step_distance_counter > 1.5*BS)
1132 m_step_distance_counter = 0;
1133 if(!m_is_local_player && m_prop.makes_footstep_sound)
1135 INodeDefManager *ndef = m_client->ndef();
1136 v3s16 p = floatToInt(getPosition() + v3f(0,
1137 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1138 MapNode n = m_env->getMap().getNodeNoEx(p);
1139 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1140 m_client->sound()->playSoundAt(spec, false, getPosition());
1145 m_anim_timer += dtime;
1146 if(m_anim_timer >= m_anim_framelength)
1148 m_anim_timer -= m_anim_framelength;
1150 if(m_anim_frame >= m_anim_num_frames)
1156 if(m_reset_textures_timer >= 0)
1158 m_reset_textures_timer -= dtime;
1159 if(m_reset_textures_timer <= 0) {
1160 m_reset_textures_timer = -1;
1161 updateTextures(m_previous_texture_modifier);
1164 if(!getParent() && fabs(m_prop.automatic_rotate) > 0.001)
1166 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1170 if (!getParent() && m_prop.automatic_face_movement_dir &&
1171 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1173 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1174 + m_prop.automatic_face_movement_dir_offset;
1175 float max_rotation_delta =
1176 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1178 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
1179 (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
1181 m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
1183 m_yaw = optimal_yaw;
1189 void GenericCAO::updateTexturePos()
1193 scene::ICameraSceneNode* camera =
1194 m_spritenode->getSceneManager()->getActiveCamera();
1197 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1198 - camera->getAbsolutePosition();
1199 cam_to_entity.normalize();
1201 int row = m_tx_basepos.Y;
1202 int col = m_tx_basepos.X;
1204 if(m_tx_select_horiz_by_yawpitch)
1206 if(cam_to_entity.Y > 0.75)
1208 else if(cam_to_entity.Y < -0.75)
1212 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1213 float dir = mob_dir - m_yaw;
1214 dir = wrapDegrees_180(dir);
1215 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1216 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1218 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1220 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1222 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1229 // Animation goes downwards
1230 row += m_anim_frame;
1232 float txs = m_tx_size.X;
1233 float tys = m_tx_size.Y;
1234 setBillboardTextureMatrix(m_spritenode,
1235 txs, tys, col, row);
1239 void GenericCAO::updateTextures(std::string mod)
1241 ITextureSource *tsrc = m_client->tsrc();
1243 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1244 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1245 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1247 m_previous_texture_modifier = m_current_texture_modifier;
1248 m_current_texture_modifier = mod;
1252 if(m_prop.visual == "sprite")
1254 std::string texturestring = "unknown_node.png";
1255 if(m_prop.textures.size() >= 1)
1256 texturestring = m_prop.textures[0];
1257 texturestring += mod;
1258 m_spritenode->setMaterialTexture(0,
1259 tsrc->getTextureForMesh(texturestring));
1261 // This allows setting per-material colors. However, until a real lighting
1262 // system is added, the code below will have no effect. Once MineTest
1263 // has directional lighting, it should work automatically.
1264 if(m_prop.colors.size() >= 1)
1266 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1267 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1268 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1271 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1272 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1273 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1276 if(m_animated_meshnode)
1278 if(m_prop.visual == "mesh")
1280 for (u32 i = 0; i < m_prop.textures.size() &&
1281 i < m_animated_meshnode->getMaterialCount(); ++i)
1283 std::string texturestring = m_prop.textures[i];
1284 if(texturestring == "")
1285 continue; // Empty texture string means don't modify that material
1286 texturestring += mod;
1287 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1290 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1294 // Set material flags and texture
1295 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1296 material.TextureLayer[0].Texture = texture;
1297 material.setFlag(video::EMF_LIGHTING, false);
1298 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1300 m_animated_meshnode->getMaterial(i)
1301 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1302 m_animated_meshnode->getMaterial(i)
1303 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1304 m_animated_meshnode->getMaterial(i)
1305 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1307 for (u32 i = 0; i < m_prop.colors.size() &&
1308 i < m_animated_meshnode->getMaterialCount(); ++i)
1310 // This allows setting per-material colors. However, until a real lighting
1311 // system is added, the code below will have no effect. Once MineTest
1312 // has directional lighting, it should work automatically.
1313 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1314 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1315 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1321 if(m_prop.visual == "cube")
1323 for (u32 i = 0; i < 6; ++i)
1325 std::string texturestring = "unknown_node.png";
1326 if(m_prop.textures.size() > i)
1327 texturestring = m_prop.textures[i];
1328 texturestring += mod;
1331 // Set material flags and texture
1332 video::SMaterial& material = m_meshnode->getMaterial(i);
1333 material.setFlag(video::EMF_LIGHTING, false);
1334 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1335 material.setTexture(0,
1336 tsrc->getTextureForMesh(texturestring));
1337 material.getTextureMatrix(0).makeIdentity();
1339 // This allows setting per-material colors. However, until a real lighting
1340 // system is added, the code below will have no effect. Once MineTest
1341 // has directional lighting, it should work automatically.
1342 if(m_prop.colors.size() > i)
1344 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1345 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1346 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1349 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1350 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1351 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1354 else if(m_prop.visual == "upright_sprite")
1356 scene::IMesh *mesh = m_meshnode->getMesh();
1358 std::string tname = "unknown_object.png";
1359 if(m_prop.textures.size() >= 1)
1360 tname = m_prop.textures[0];
1362 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1363 buf->getMaterial().setTexture(0,
1364 tsrc->getTextureForMesh(tname));
1366 // This allows setting per-material colors. However, until a real lighting
1367 // system is added, the code below will have no effect. Once MineTest
1368 // has directional lighting, it should work automatically.
1369 if(m_prop.colors.size() >= 1)
1371 buf->getMaterial().AmbientColor = m_prop.colors[0];
1372 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1373 buf->getMaterial().SpecularColor = m_prop.colors[0];
1376 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1377 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1378 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1381 std::string tname = "unknown_object.png";
1382 if(m_prop.textures.size() >= 2)
1383 tname = m_prop.textures[1];
1384 else if(m_prop.textures.size() >= 1)
1385 tname = m_prop.textures[0];
1387 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1388 buf->getMaterial().setTexture(0,
1389 tsrc->getTextureForMesh(tname));
1391 // This allows setting per-material colors. However, until a real lighting
1392 // system is added, the code below will have no effect. Once MineTest
1393 // has directional lighting, it should work automatically.
1394 if(m_prop.colors.size() >= 2)
1396 buf->getMaterial().AmbientColor = m_prop.colors[1];
1397 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1398 buf->getMaterial().SpecularColor = m_prop.colors[1];
1400 else if(m_prop.colors.size() >= 1)
1402 buf->getMaterial().AmbientColor = m_prop.colors[0];
1403 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1404 buf->getMaterial().SpecularColor = m_prop.colors[0];
1407 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1408 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1409 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1415 void GenericCAO::updateAnimation()
1417 if (!m_animated_meshnode)
1420 if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1421 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1422 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1423 if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1424 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1425 m_animated_meshnode->setTransitionTime(m_animation_blend);
1426 // Requires Irrlicht 1.8 or greater
1427 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1428 if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1429 m_animated_meshnode->setLoopMode(m_animation_loop);
1433 void GenericCAO::updateBonePosition()
1435 if(m_bone_position.empty() || !m_animated_meshnode)
1438 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1439 for(std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
1440 ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1441 std::string bone_name = (*ii).first;
1442 v3f bone_pos = (*ii).second.X;
1443 v3f bone_rot = (*ii).second.Y;
1444 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1447 bone->setPosition(bone_pos);
1448 bone->setRotation(bone_rot);
1453 void GenericCAO::updateAttachments()
1456 if (!getParent()) { // Detach or don't attach
1457 scene::ISceneNode *node = getSceneNode();
1459 v3f old_position = node->getAbsolutePosition();
1460 v3f old_rotation = node->getRotation();
1461 node->setParent(m_smgr->getRootSceneNode());
1462 node->setPosition(old_position);
1463 node->setRotation(old_rotation);
1464 node->updateAbsolutePosition();
1466 if (m_is_local_player) {
1467 LocalPlayer *player = m_env->getLocalPlayer();
1468 player->isAttached = false;
1473 scene::ISceneNode *my_node = getSceneNode();
1475 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1476 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1477 getParent()->getAnimatedMeshSceneNode();
1478 if (parent_animated_mesh_node && m_attachment_bone != "") {
1479 parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1482 if (my_node && parent_node) {
1483 my_node->setParent(parent_node);
1484 my_node->setPosition(m_attachment_position);
1485 my_node->setRotation(m_attachment_rotation);
1486 my_node->updateAbsolutePosition();
1488 if (m_is_local_player) {
1489 LocalPlayer *player = m_env->getLocalPlayer();
1490 player->isAttached = true;
1495 void GenericCAO::processMessage(const std::string &data)
1497 //infostream<<"GenericCAO: Got message"<<std::endl;
1498 std::istringstream is(data, std::ios::binary);
1500 u8 cmd = readU8(is);
1501 if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1502 m_prop = gob_read_set_properties(is);
1504 m_selection_box = m_prop.collisionbox;
1505 m_selection_box.MinEdge *= BS;
1506 m_selection_box.MaxEdge *= BS;
1508 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1509 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1511 if(!m_initial_tx_basepos_set){
1512 m_initial_tx_basepos_set = true;
1513 m_tx_basepos = m_prop.initial_sprite_basepos;
1515 if (m_is_local_player) {
1516 LocalPlayer *player = m_env->getLocalPlayer();
1517 player->makes_footstep_sound = m_prop.makes_footstep_sound;
1518 player->setCollisionbox(m_selection_box);
1521 if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
1522 m_prop.nametag = m_name;
1525 } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1526 // Not sent by the server if this object is an attachment.
1527 // We might however get here if the server notices the object being detached before the client.
1528 m_position = readV3F1000(is);
1529 m_velocity = readV3F1000(is);
1530 m_acceleration = readV3F1000(is);
1531 if(fabs(m_prop.automatic_rotate) < 0.001)
1532 m_yaw = readF1000(is);
1535 bool do_interpolate = readU8(is);
1536 bool is_end_position = readU8(is);
1537 float update_interval = readF1000(is);
1539 // Place us a bit higher if we're physical, to not sink into
1540 // the ground due to sucky collision detection...
1542 m_position += v3f(0,0.002,0);
1544 if(getParent() != NULL) // Just in case
1549 if(!m_prop.physical)
1550 pos_translator.update(m_position, is_end_position, update_interval);
1552 pos_translator.init(m_position);
1555 } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1556 std::string mod = deSerializeString(is);
1558 // immediatly reset a engine issued texture modifier if a mod sends a different one
1559 if (m_reset_textures_timer > 0) {
1560 m_reset_textures_timer = -1;
1561 updateTextures(m_previous_texture_modifier);
1563 updateTextures(mod);
1564 } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1565 v2s16 p = readV2S16(is);
1566 int num_frames = readU16(is);
1567 float framelength = readF1000(is);
1568 bool select_horiz_by_yawpitch = readU8(is);
1571 m_anim_num_frames = num_frames;
1572 m_anim_framelength = framelength;
1573 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1576 } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1577 float override_speed = readF1000(is);
1578 float override_jump = readF1000(is);
1579 float override_gravity = readF1000(is);
1580 // these are sent inverted so we get true when the server sends nothing
1581 bool sneak = !readU8(is);
1582 bool sneak_glitch = !readU8(is);
1583 bool new_move = !readU8(is);
1586 if(m_is_local_player)
1588 LocalPlayer *player = m_env->getLocalPlayer();
1589 player->physics_override_speed = override_speed;
1590 player->physics_override_jump = override_jump;
1591 player->physics_override_gravity = override_gravity;
1592 player->physics_override_sneak = sneak;
1593 player->physics_override_sneak_glitch = sneak_glitch;
1594 player->physics_override_new_move = new_move;
1596 } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1597 // TODO: change frames send as v2s32 value
1598 v2f range = readV2F1000(is);
1599 if (!m_is_local_player) {
1600 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1601 m_animation_speed = readF1000(is);
1602 m_animation_blend = readF1000(is);
1603 // these are sent inverted so we get true when the server sends nothing
1604 m_animation_loop = !readU8(is);
1607 LocalPlayer *player = m_env->getLocalPlayer();
1608 if(player->last_animation == NO_ANIM)
1610 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1611 m_animation_speed = readF1000(is);
1612 m_animation_blend = readF1000(is);
1613 // these are sent inverted so we get true when the server sends nothing
1614 m_animation_loop = !readU8(is);
1616 // update animation only if local animations present
1617 // and received animation is unknown (except idle animation)
1618 bool is_known = false;
1619 for (int i = 1;i<4;i++)
1621 if(m_animation_range.Y == player->local_animations[i].Y)
1625 (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1630 } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1631 std::string bone = deSerializeString(is);
1632 v3f position = readV3F1000(is);
1633 v3f rotation = readV3F1000(is);
1634 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1636 updateBonePosition();
1637 } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1638 u16 parentID = readS16(is);
1639 u16 oldparent = m_env->attachement_parent_ids[getId()];
1641 m_children.erase(std::remove(m_children.begin(), m_children.end(),
1642 getId()), m_children.end());
1644 m_env->attachement_parent_ids[getId()] = parentID;
1645 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1648 parentobj->m_children.push_back(getId());
1651 m_attachment_bone = deSerializeString(is);
1652 m_attachment_position = readV3F1000(is);
1653 m_attachment_rotation = readV3F1000(is);
1655 // localplayer itself can't be attached to localplayer
1656 if (!m_is_local_player) {
1657 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1658 // Objects attached to the local player should be hidden by default
1659 m_is_visible = !m_attached_to_local;
1662 updateAttachments();
1663 } else if (cmd == GENERIC_CMD_PUNCHED) {
1664 /*s16 damage =*/ readS16(is);
1665 s16 result_hp = readS16(is);
1667 // Use this instead of the send damage to not interfere with prediction
1668 s16 damage = m_hp - result_hp;
1676 // TODO: Execute defined fast response
1677 // As there is no definition, make a smoke puff
1678 ClientSimpleObject *simple = createSmokePuff(
1679 m_smgr, m_env, m_position,
1680 m_prop.visual_size * BS);
1681 m_env->addSimpleObject(simple);
1682 } else if (m_reset_textures_timer < 0) {
1683 // TODO: Execute defined fast response
1684 // Flashing shall suffice as there is no definition
1685 m_reset_textures_timer = 0.05;
1687 m_reset_textures_timer += 0.05 * damage;
1688 updateTextures(m_current_texture_modifier + "^[brighten");
1691 } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1692 m_armor_groups.clear();
1693 int armor_groups_size = readU16(is);
1694 for(int i=0; i<armor_groups_size; i++)
1696 std::string name = deSerializeString(is);
1697 int rating = readS16(is);
1698 m_armor_groups[name] = rating;
1700 } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1701 // Deprecated, for backwards compatibility only.
1702 readU8(is); // version
1703 m_prop.nametag_color = readARGB8(is);
1704 if (m_nametag != NULL) {
1705 m_nametag->nametag_color = m_prop.nametag_color;
1707 pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
1708 m_nametag->nametag_pos = pos;
1710 } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1711 u16 child_id = readU16(is);
1712 u8 type = readU8(is);
1714 if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
1715 childobj->processInitData(deSerializeLongString(is));
1717 m_env->addActiveObject(child_id, type, deSerializeLongString(is));
1720 warningstream << FUNCTION_NAME
1721 << ": unknown command or outdated client \""
1722 << +cmd << "\"" << std::endl;
1726 /* \pre punchitem != NULL
1728 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1729 float time_from_last_punch)
1731 assert(punchitem); // pre-condition
1732 const ToolCapabilities *toolcap =
1733 &punchitem->getToolCapabilities(m_client->idef());
1734 PunchDamageResult result = getPunchDamage(
1738 time_from_last_punch);
1740 if(result.did_punch && result.damage != 0)
1742 if(result.damage < m_hp)
1744 m_hp -= result.damage;
1747 // TODO: Execute defined fast response
1748 // As there is no definition, make a smoke puff
1749 ClientSimpleObject *simple = createSmokePuff(
1750 m_smgr, m_env, m_position,
1751 m_prop.visual_size * BS);
1752 m_env->addSimpleObject(simple);
1754 // TODO: Execute defined fast response
1755 // Flashing shall suffice as there is no definition
1756 if (m_reset_textures_timer < 0) {
1757 m_reset_textures_timer = 0.05;
1758 if (result.damage >= 2)
1759 m_reset_textures_timer += 0.05 * result.damage;
1760 updateTextures(m_current_texture_modifier + "^[brighten");
1767 std::string GenericCAO::debugInfoText()
1769 std::ostringstream os(std::ios::binary);
1770 os<<"GenericCAO hp="<<m_hp<<"\n";
1772 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1773 i != m_armor_groups.end(); ++i)
1775 os<<i->first<<"="<<i->second<<", ";
1782 GenericCAO proto_GenericCAO(NULL, NULL);