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 "content_cao.h"
22 #include "environment.h"
23 #include "collision.h"
25 #include <ICameraSceneNode.h>
26 #include <ITextSceneNode.h>
27 #include <IBillboardSceneNode.h>
28 #include "serialization.h" // For decompressZlib
30 #include "clientobject.h"
31 #include "content_object.h"
35 #include "content_cso.h"
38 #include "localplayer.h"
39 #include "util/numeric.h" // For IntervalLimiter
40 #include "util/serialize.h"
41 #include "util/mathconstants.h"
43 #include "main.h" // g_settings
44 #include <IMeshManipulator.h>
45 #include <IAnimatedMeshSceneNode.h>
46 #include <IBoneSceneNode.h>
49 struct ToolCapabilities;
51 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
53 std::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
59 struct SmoothTranslator
66 f32 anim_time_counter;
86 anim_time_counter = 0;
95 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
97 aim_is_end = is_end_position;
100 if(update_interval > 0){
101 anim_time = update_interval;
103 if(anim_time < 0.001 || anim_time > 1.0)
104 anim_time = anim_time_counter;
106 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
108 anim_time_counter = 0;
112 void translate(f32 dtime)
114 anim_time_counter = anim_time_counter + dtime;
115 anim_counter = anim_counter + dtime;
116 v3f vect_move = vect_aim - vect_old;
118 if(anim_time > 0.001)
119 moveratio = anim_time_counter / anim_time;
120 // Move a bit less than should, to avoid oscillation
121 moveratio = moveratio * 0.8;
122 float move_end = 1.5;
125 if(moveratio > move_end)
126 moveratio = move_end;
127 vect_show = vect_old + vect_move * moveratio;
132 return ((anim_time_counter / anim_time) < 1.4);
140 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
141 float txs, float tys, int col, int row)
143 video::SMaterial& material = bill->getMaterial(0);
144 core::matrix4& matrix = material.getTextureMatrix(0);
145 matrix.setTextureTranslate(txs*col, tys*row);
146 matrix.setTextureScale(txs, tys);
153 class TestCAO : public ClientActiveObject
156 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
161 return ACTIVEOBJECT_TYPE_TEST;
164 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
166 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
167 IrrlichtDevice *irr);
168 void removeFromScene();
169 void updateLight(u8 light_at_pos);
170 v3s16 getLightPosition();
171 void updateNodePos();
173 void step(float dtime, ClientEnvironment *env);
175 void processMessage(const std::string &data);
177 bool getCollisionBox(aabb3f *toset) { return false; }
179 scene::IMeshSceneNode *m_node;
184 TestCAO proto_TestCAO(NULL, NULL);
186 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
187 ClientActiveObject(0, gamedef, env),
189 m_position(v3f(0,10*BS,0))
191 ClientActiveObject::registerType(getType(), create);
198 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
200 return new TestCAO(gamedef, env);
203 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
209 //video::IVideoDriver* driver = smgr->getVideoDriver();
211 scene::SMesh *mesh = new scene::SMesh();
212 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
213 video::SColor c(255,255,255,255);
214 video::S3DVertex vertices[4] =
216 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
217 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
218 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
219 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
221 u16 indices[] = {0,1,2,2,3,0};
222 buf->append(vertices, 4, indices, 6);
224 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
225 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
226 buf->getMaterial().setTexture(0, tsrc->getTexture("rat.png"));
227 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
228 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
229 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
231 mesh->addMeshBuffer(buf);
233 m_node = smgr->addMeshSceneNode(mesh, NULL);
238 void TestCAO::removeFromScene()
247 void TestCAO::updateLight(u8 light_at_pos)
251 v3s16 TestCAO::getLightPosition()
253 return floatToInt(m_position, BS);
256 void TestCAO::updateNodePos()
261 m_node->setPosition(m_position);
262 //m_node->setRotation(v3f(0, 45, 0));
265 void TestCAO::step(float dtime, ClientEnvironment *env)
269 v3f rot = m_node->getRotation();
270 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
271 rot.Y += dtime * 180;
272 m_node->setRotation(rot);
276 void TestCAO::processMessage(const std::string &data)
278 infostream<<"TestCAO: Got data: "<<data<<std::endl;
279 std::istringstream is(data, std::ios::binary);
297 class ItemCAO : public ClientActiveObject
300 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
305 return ACTIVEOBJECT_TYPE_ITEM;
308 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
310 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
311 IrrlichtDevice *irr);
312 void removeFromScene();
313 void updateLight(u8 light_at_pos);
314 v3s16 getLightPosition();
315 void updateNodePos();
316 void updateInfoText();
317 void updateTexture();
319 void step(float dtime, ClientEnvironment *env);
321 void processMessage(const std::string &data);
323 void initialize(const std::string &data);
325 core::aabbox3d<f32>* getSelectionBox()
326 {return &m_selection_box;}
330 std::string infoText()
333 bool getCollisionBox(aabb3f *toset) { return false; }
335 core::aabbox3d<f32> m_selection_box;
336 scene::IMeshSceneNode *m_node;
338 std::string m_itemstring;
339 std::string m_infotext;
342 #include "inventory.h"
345 ItemCAO proto_ItemCAO(NULL, NULL);
347 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
348 ClientActiveObject(0, gamedef, env),
349 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
351 m_position(v3f(0,10*BS,0))
355 ClientActiveObject::registerType(getType(), create);
363 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
365 return new ItemCAO(gamedef, env);
368 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
374 //video::IVideoDriver* driver = smgr->getVideoDriver();
376 scene::SMesh *mesh = new scene::SMesh();
377 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
378 video::SColor c(255,255,255,255);
379 video::S3DVertex vertices[4] =
381 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
382 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
383 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
384 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
385 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
386 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
387 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
388 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
390 u16 indices[] = {0,1,2,2,3,0};
391 buf->append(vertices, 4, indices, 6);
393 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
394 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
395 // Initialize with a generated placeholder texture
396 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
397 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
398 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
399 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
401 mesh->addMeshBuffer(buf);
403 m_node = smgr->addMeshSceneNode(mesh, NULL);
414 void ItemCAO::removeFromScene()
423 void ItemCAO::updateLight(u8 light_at_pos)
428 u8 li = decode_light(light_at_pos);
429 video::SColor color(255,li,li,li);
430 setMeshColor(m_node->getMesh(), color);
433 v3s16 ItemCAO::getLightPosition()
435 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
438 void ItemCAO::updateNodePos()
443 m_node->setPosition(m_position);
446 void ItemCAO::updateInfoText()
449 IItemDefManager *idef = m_gamedef->idef();
451 item.deSerialize(m_itemstring, idef);
452 if(item.isKnown(idef))
453 m_infotext = item.getDefinition(idef).description;
455 m_infotext = "Unknown item: '" + m_itemstring + "'";
457 m_infotext += " (" + itos(item.count) + ")";
459 catch(SerializationError &e)
461 m_infotext = "Unknown item: '" + m_itemstring + "'";
465 void ItemCAO::updateTexture()
470 // Create an inventory item to see what is its image
471 std::istringstream is(m_itemstring, std::ios_base::binary);
472 video::ITexture *texture = NULL;
474 IItemDefManager *idef = m_gamedef->idef();
476 item.deSerialize(is, idef);
477 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
479 catch(SerializationError &e)
481 infostream<<"WARNING: "<<__FUNCTION_NAME
482 <<": error deSerializing itemstring \""
483 <<m_itemstring<<std::endl;
486 // Set meshbuffer texture
487 m_node->getMaterial(0).setTexture(0, texture);
491 void ItemCAO::step(float dtime, ClientEnvironment *env)
495 /*v3f rot = m_node->getRotation();
496 rot.Y += dtime * 120;
497 m_node->setRotation(rot);*/
498 LocalPlayer *player = env->getLocalPlayer();
500 v3f rot = m_node->getRotation();
501 rot.Y = 180.0 - (player->getYaw());
502 m_node->setRotation(rot);
506 void ItemCAO::processMessage(const std::string &data)
508 //infostream<<"ItemCAO: Got message"<<std::endl;
509 std::istringstream is(data, std::ios::binary);
515 m_position = readV3F1000(is);
521 m_itemstring = deSerializeString(is);
527 void ItemCAO::initialize(const std::string &data)
529 infostream<<"ItemCAO: Got init data"<<std::endl;
532 std::istringstream is(data, std::ios::binary);
534 u8 version = readU8(is);
539 m_position = readV3F1000(is);
541 m_itemstring = deSerializeString(is);
552 #include "genericobject.h"
554 class GenericCAO : public ClientActiveObject
557 // Only set at initialization
560 bool m_is_local_player;
562 // Property-ish things
563 ObjectProperties m_prop;
565 scene::ISceneManager *m_smgr;
566 IrrlichtDevice *m_irr;
567 core::aabbox3d<f32> m_selection_box;
568 scene::IMeshSceneNode *m_meshnode;
569 scene::IAnimatedMeshSceneNode *m_animated_meshnode;
570 scene::IBillboardSceneNode *m_spritenode;
571 scene::ITextSceneNode* m_textnode;
577 SmoothTranslator pos_translator;
578 // Spritesheet/animation stuff
581 bool m_initial_tx_basepos_set;
582 bool m_tx_select_horiz_by_yawpitch;
583 v2f m_animation_range;
584 int m_animation_speed;
585 int m_animation_blend;
586 std::map<std::string, core::vector2d<v3f> > m_bone_position; // stores position and rotation for each bone name
587 std::string m_attachment_bone;
588 v3f m_attachment_position;
589 v3f m_attachment_rotation;
590 bool m_attached_to_local;
592 int m_anim_num_frames;
593 float m_anim_framelength;
595 ItemGroupList m_armor_groups;
596 float m_reset_textures_timer;
597 bool m_visuals_expired;
598 float m_step_distance_counter;
603 GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
604 ClientActiveObject(0, gamedef, env),
607 m_is_local_player(false),
612 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
614 m_animated_meshnode(NULL),
617 m_position(v3f(0,10*BS,0)),
618 m_velocity(v3f(0,0,0)),
619 m_acceleration(v3f(0,0,0)),
624 m_initial_tx_basepos_set(false),
625 m_tx_select_horiz_by_yawpitch(false),
626 m_animation_range(v2f(0,0)),
627 m_animation_speed(15),
628 m_animation_blend(0),
629 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
630 m_attachment_bone(""),
631 m_attachment_position(v3f(0,0,0)),
632 m_attachment_rotation(v3f(0,0,0)),
633 m_attached_to_local(false),
635 m_anim_num_frames(1),
636 m_anim_framelength(0.2),
638 m_reset_textures_timer(-1),
639 m_visuals_expired(false),
640 m_step_distance_counter(0),
645 ClientActiveObject::registerType(getType(), create);
648 bool getCollisionBox(aabb3f *toset) {
649 if (m_prop.physical) {
651 //update collision box
652 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
653 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
655 toset->MinEdge += m_position;
656 toset->MaxEdge += m_position;
664 bool collideWithObjects() {
665 return m_prop.collideWithObjects;
668 void initialize(const std::string &data)
670 infostream<<"GenericCAO: Got init data"<<std::endl;
671 std::istringstream is(data, std::ios::binary);
672 int num_messages = 0;
674 u8 version = readU8(is);
676 if(version == 1) // In PROTOCOL_VERSION 14
678 m_name = deSerializeString(is);
679 m_is_player = readU8(is);
681 m_position = readV3F1000(is);
682 m_yaw = readF1000(is);
684 num_messages = readU8(is);
686 else if(version == 0) // In PROTOCOL_VERSION 13
688 m_name = deSerializeString(is);
689 m_is_player = readU8(is);
690 m_position = readV3F1000(is);
691 m_yaw = readF1000(is);
693 num_messages = readU8(is);
697 errorstream<<"GenericCAO: Unsupported init data version"
702 for(int i=0; i<num_messages; i++){
703 std::string message = deSerializeLongString(is);
704 processMessage(message);
707 pos_translator.init(m_position);
711 Player *player = m_env->getPlayer(m_name.c_str());
712 if(player && player->isLocal()){
713 m_is_local_player = true;
715 m_env->addPlayerName(m_name.c_str());
722 m_env->removePlayerName(m_name.c_str());
726 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
728 return new GenericCAO(gamedef, env);
733 return ACTIVEOBJECT_TYPE_GENERIC;
735 core::aabbox3d<f32>* getSelectionBox()
737 if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
739 return &m_selection_box;
743 if(getParent() != NULL){
745 return m_meshnode->getAbsolutePosition();
746 if(m_animated_meshnode)
747 return m_animated_meshnode->getAbsolutePosition();
749 return m_spritenode->getAbsolutePosition();
752 return pos_translator.vect_show;
755 scene::IMeshSceneNode *getMeshSceneNode()
762 scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode()
764 if(m_animated_meshnode)
765 return m_animated_meshnode;
769 scene::IBillboardSceneNode *getSpriteSceneNode()
783 return m_is_local_player;
786 void setAttachments()
791 ClientActiveObject *getParent()
793 ClientActiveObject *obj = NULL;
794 for(std::vector<core::vector2d<int> >::const_iterator cii = m_env->attachment_list.begin(); cii != m_env->attachment_list.end(); cii++)
796 if(cii->X == getId()){ // This ID is our child
797 if(cii->Y > 0){ // A parent ID exists for our child
798 if(cii->X != cii->Y){ // The parent and child ID are not the same
799 obj = m_env->getActiveObject(cii->Y);
810 void removeFromScene(bool permanent)
812 if(permanent) // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
814 // Detach this object's children
815 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
817 if(ii->Y == getId()) // Is a child of our object
820 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
822 obj->setAttachments();
825 // Delete this object from the attachments list
826 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
828 if(ii->X == getId()) // Is our object
830 m_env->attachment_list.erase(ii);
837 m_meshnode->remove();
840 if(m_animated_meshnode){
841 m_animated_meshnode->remove();
842 m_animated_meshnode = NULL;
845 m_spritenode->remove();
850 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
856 if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
859 m_visuals_expired = false;
861 if(!m_prop.is_visible || m_is_local_player)
864 //video::IVideoDriver* driver = smgr->getVideoDriver();
866 if(m_prop.visual == "sprite"){
867 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
868 m_spritenode = smgr->addBillboardSceneNode(
869 NULL, v2f(1, 1), v3f(0,0,0), -1);
870 m_spritenode->setMaterialTexture(0,
871 tsrc->getTexture("unknown_node.png"));
872 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
873 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
874 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
875 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
876 u8 li = m_last_light;
877 m_spritenode->setColor(video::SColor(255,li,li,li));
878 m_spritenode->setSize(m_prop.visual_size*BS);
880 const float txs = 1.0 / 1;
881 const float tys = 1.0 / 1;
882 setBillboardTextureMatrix(m_spritenode,
886 else if(m_prop.visual == "upright_sprite")
888 scene::SMesh *mesh = new scene::SMesh();
889 double dx = BS*m_prop.visual_size.X/2;
890 double dy = BS*m_prop.visual_size.Y/2;
892 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
893 u8 li = m_last_light;
894 video::SColor c(255,li,li,li);
895 video::S3DVertex vertices[4] =
897 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
898 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
899 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
900 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
902 u16 indices[] = {0,1,2,2,3,0};
903 buf->append(vertices, 4, indices, 6);
905 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
906 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
907 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
908 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
910 mesh->addMeshBuffer(buf);
914 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
915 u8 li = m_last_light;
916 video::SColor c(255,li,li,li);
917 video::S3DVertex vertices[4] =
919 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
920 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
921 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
922 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
924 u16 indices[] = {0,1,2,2,3,0};
925 buf->append(vertices, 4, indices, 6);
927 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
928 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
929 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
930 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
932 mesh->addMeshBuffer(buf);
935 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
937 // Set it to use the materials of the meshbuffers directly.
938 // This is needed for changing the texture in the future
939 m_meshnode->setReadOnlyMaterials(true);
941 else if(m_prop.visual == "cube"){
942 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
943 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
944 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
947 m_meshnode->setScale(v3f(m_prop.visual_size.X,
948 m_prop.visual_size.Y,
949 m_prop.visual_size.X));
950 u8 li = m_last_light;
951 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
953 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
954 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
955 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
956 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
958 else if(m_prop.visual == "mesh"){
959 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
960 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
963 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
964 mesh->drop(); // The scene node took hold of it
965 m_animated_meshnode->animateJoints(); // Needed for some animations
966 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
967 m_prop.visual_size.Y,
968 m_prop.visual_size.X));
969 u8 li = m_last_light;
970 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
972 m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
973 m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
974 m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
975 m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
978 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
980 else if(m_prop.visual == "wielditem"){
981 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
982 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
983 if(m_prop.textures.size() >= 1){
984 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
985 IItemDefManager *idef = m_gamedef->idef();
986 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
987 scene::IMesh *item_mesh = idef->getWieldMesh(item.getDefinition(idef).name, m_gamedef);
989 // Copy mesh to be able to set unique vertex colors
990 scene::IMeshManipulator *manip =
991 irr->getVideoDriver()->getMeshManipulator();
992 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
994 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
997 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
998 m_prop.visual_size.Y/2,
999 m_prop.visual_size.X/2));
1000 u8 li = m_last_light;
1001 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
1004 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
1005 <<"\" not supported"<<std::endl;
1009 scene::ISceneNode *node = NULL;
1011 node = m_spritenode;
1012 else if(m_animated_meshnode)
1013 node = m_animated_meshnode;
1016 if(node && m_is_player && !m_is_local_player){
1017 // Add a text node for showing the name
1018 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
1019 std::wstring wname = narrow_to_wide(m_name);
1020 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
1021 wname.c_str(), video::SColor(255,255,255,255), node);
1022 m_textnode->setPosition(v3f(0, BS*1.1, 0));
1027 updateBonePosition();
1028 updateAttachments();
1031 void expireVisuals()
1033 m_visuals_expired = true;
1036 void updateLight(u8 light_at_pos)
1038 u8 li = decode_light(light_at_pos);
1039 if(li != m_last_light){
1041 video::SColor color(255,li,li,li);
1043 setMeshColor(m_meshnode->getMesh(), color);
1044 if(m_animated_meshnode)
1045 setMeshColor(m_animated_meshnode->getMesh(), color);
1047 m_spritenode->setColor(color);
1051 v3s16 getLightPosition()
1053 return floatToInt(m_position, BS);
1056 void updateNodePos()
1058 if(getParent() != NULL)
1061 v3s16 camera_offset = m_env->getCameraOffset();
1063 m_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
1064 v3f rot = m_meshnode->getRotation();
1066 m_meshnode->setRotation(rot);
1068 if(m_animated_meshnode){
1069 m_animated_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
1070 v3f rot = m_animated_meshnode->getRotation();
1072 m_animated_meshnode->setRotation(rot);
1075 m_spritenode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
1079 void step(float dtime, ClientEnvironment *env)
1081 if(m_visuals_expired && m_smgr && m_irr){
1082 m_visuals_expired = false;
1084 // Attachments, part 1: All attached objects must be unparented first, or Irrlicht causes a segmentation fault
1085 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1087 if(ii->Y == getId()) // This is a child of our parent
1089 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1092 scene::IMeshSceneNode *m_child_meshnode = obj->getMeshSceneNode();
1093 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode = obj->getAnimatedMeshSceneNode();
1094 scene::IBillboardSceneNode *m_child_spritenode = obj->getSpriteSceneNode();
1095 if(m_child_meshnode)
1096 m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1097 if(m_child_animated_meshnode)
1098 m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1099 if(m_child_spritenode)
1100 m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1105 removeFromScene(false);
1106 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1108 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1109 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1111 if(ii->Y == getId()) // This is a child of our parent
1113 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1115 obj->setAttachments();
1120 // Make sure m_is_visible is always applied
1122 m_meshnode->setVisible(m_is_visible);
1123 if(m_animated_meshnode)
1124 m_animated_meshnode->setVisible(m_is_visible);
1126 m_spritenode->setVisible(m_is_visible);
1128 m_textnode->setVisible(m_is_visible);
1130 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1132 // Set these for later
1133 m_position = getPosition();
1134 m_velocity = v3f(0,0,0);
1135 m_acceleration = v3f(0,0,0);
1136 pos_translator.vect_show = m_position;
1138 if(m_is_local_player) // Update local player attachment position
1140 LocalPlayer *player = m_env->getLocalPlayer();
1141 player->overridePosition = getParent()->getPosition();
1142 m_env->getLocalPlayer()->parent = getParent();
1147 v3f lastpos = pos_translator.vect_show;
1149 if(m_prop.physical){
1150 core::aabbox3d<f32> box = m_prop.collisionbox;
1153 collisionMoveResult moveresult;
1154 f32 pos_max_d = BS*0.125; // Distance per iteration
1155 v3f p_pos = m_position;
1156 v3f p_velocity = m_velocity;
1157 v3f p_acceleration = m_acceleration;
1158 moveresult = collisionMoveSimple(env,env->getGameDef(),
1159 pos_max_d, box, m_prop.stepheight, dtime,
1160 p_pos, p_velocity, p_acceleration,
1161 this, m_prop.collideWithObjects);
1164 m_velocity = p_velocity;
1165 m_acceleration = p_acceleration;
1167 bool is_end_position = moveresult.collides;
1168 pos_translator.update(m_position, is_end_position, dtime);
1169 pos_translator.translate(dtime);
1172 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1173 m_velocity += dtime * m_acceleration;
1174 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
1175 pos_translator.translate(dtime);
1179 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1180 m_step_distance_counter += moved;
1181 if(m_step_distance_counter > 1.5*BS){
1182 m_step_distance_counter = 0;
1183 if(!m_is_local_player && m_prop.makes_footstep_sound){
1184 INodeDefManager *ndef = m_gamedef->ndef();
1185 v3s16 p = floatToInt(getPosition() + v3f(0,
1186 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1187 MapNode n = m_env->getMap().getNodeNoEx(p);
1188 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1189 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1194 m_anim_timer += dtime;
1195 if(m_anim_timer >= m_anim_framelength){
1196 m_anim_timer -= m_anim_framelength;
1198 if(m_anim_frame >= m_anim_num_frames)
1204 if(m_reset_textures_timer >= 0){
1205 m_reset_textures_timer -= dtime;
1206 if(m_reset_textures_timer <= 0){
1207 m_reset_textures_timer = -1;
1211 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001){
1212 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1216 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1217 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)){
1218 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI + m_prop.automatic_face_movement_dir_offset;
1223 void updateTexturePos()
1226 scene::ICameraSceneNode* camera =
1227 m_spritenode->getSceneManager()->getActiveCamera();
1230 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1231 - camera->getAbsolutePosition();
1232 cam_to_entity.normalize();
1234 int row = m_tx_basepos.Y;
1235 int col = m_tx_basepos.X;
1237 if(m_tx_select_horiz_by_yawpitch)
1239 if(cam_to_entity.Y > 0.75)
1241 else if(cam_to_entity.Y < -0.75)
1244 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1245 float dir = mob_dir - m_yaw;
1246 dir = wrapDegrees_180(dir);
1247 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1248 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1250 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1252 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1254 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1261 // Animation goes downwards
1262 row += m_anim_frame;
1264 float txs = m_tx_size.X;
1265 float tys = m_tx_size.Y;
1266 setBillboardTextureMatrix(m_spritenode,
1267 txs, tys, col, row);
1271 void updateTextures(const std::string &mod)
1273 ITextureSource *tsrc = m_gamedef->tsrc();
1275 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1276 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1277 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1281 if(m_prop.visual == "sprite")
1283 std::string texturestring = "unknown_node.png";
1284 if(m_prop.textures.size() >= 1)
1285 texturestring = m_prop.textures[0];
1286 texturestring += mod;
1287 m_spritenode->setMaterialTexture(0,
1288 tsrc->getTexture(texturestring));
1290 // This allows setting per-material colors. However, until a real lighting
1291 // system is added, the code below will have no effect. Once MineTest
1292 // has directional lighting, it should work automatically.
1293 if(m_prop.colors.size() >= 1)
1295 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1296 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1297 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1300 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1301 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1302 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1305 if(m_animated_meshnode)
1307 if(m_prop.visual == "mesh")
1309 for (u32 i = 0; i < m_prop.textures.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1311 std::string texturestring = m_prop.textures[i];
1312 if(texturestring == "")
1313 continue; // Empty texture string means don't modify that material
1314 texturestring += mod;
1315 video::ITexture* texture = tsrc->getTexture(texturestring);
1318 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1322 // Set material flags and texture
1323 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1324 material.TextureLayer[0].Texture = texture;
1325 material.setFlag(video::EMF_LIGHTING, false);
1326 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1328 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1329 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1330 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1332 for (u32 i = 0; i < m_prop.colors.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1334 // This allows setting per-material colors. However, until a real lighting
1335 // system is added, the code below will have no effect. Once MineTest
1336 // has directional lighting, it should work automatically.
1337 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1338 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1339 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1345 if(m_prop.visual == "cube")
1347 for (u32 i = 0; i < 6; ++i)
1349 std::string texturestring = "unknown_node.png";
1350 if(m_prop.textures.size() > i)
1351 texturestring = m_prop.textures[i];
1352 texturestring += mod;
1355 // Set material flags and texture
1356 video::SMaterial& material = m_meshnode->getMaterial(i);
1357 material.setFlag(video::EMF_LIGHTING, false);
1358 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1359 material.setTexture(0,
1360 tsrc->getTexture(texturestring));
1361 material.getTextureMatrix(0).makeIdentity();
1363 // This allows setting per-material colors. However, until a real lighting
1364 // system is added, the code below will have no effect. Once MineTest
1365 // has directional lighting, it should work automatically.
1366 if(m_prop.colors.size() > i)
1368 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1369 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1370 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1373 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1374 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1375 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1378 else if(m_prop.visual == "upright_sprite")
1380 scene::IMesh *mesh = m_meshnode->getMesh();
1382 std::string tname = "unknown_object.png";
1383 if(m_prop.textures.size() >= 1)
1384 tname = m_prop.textures[0];
1386 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1387 buf->getMaterial().setTexture(0,
1388 tsrc->getTexture(tname));
1390 // This allows setting per-material colors. However, until a real lighting
1391 // system is added, the code below will have no effect. Once MineTest
1392 // has directional lighting, it should work automatically.
1393 if(m_prop.colors.size() >= 1)
1395 buf->getMaterial().AmbientColor = m_prop.colors[0];
1396 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1397 buf->getMaterial().SpecularColor = m_prop.colors[0];
1400 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1401 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1402 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1405 std::string tname = "unknown_object.png";
1406 if(m_prop.textures.size() >= 2)
1407 tname = m_prop.textures[1];
1408 else if(m_prop.textures.size() >= 1)
1409 tname = m_prop.textures[0];
1411 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1412 buf->getMaterial().setTexture(0,
1413 tsrc->getTexture(tname));
1415 // This allows setting per-material colors. However, until a real lighting
1416 // system is added, the code below will have no effect. Once MineTest
1417 // has directional lighting, it should work automatically.
1418 if(m_prop.colors.size() >= 2)
1420 buf->getMaterial().AmbientColor = m_prop.colors[1];
1421 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1422 buf->getMaterial().SpecularColor = m_prop.colors[1];
1424 else if(m_prop.colors.size() >= 1)
1426 buf->getMaterial().AmbientColor = m_prop.colors[0];
1427 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1428 buf->getMaterial().SpecularColor = m_prop.colors[0];
1431 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1432 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1433 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1439 void updateAnimation()
1441 if(m_animated_meshnode == NULL)
1444 m_animated_meshnode->setFrameLoop((int)m_animation_range.X, (int)m_animation_range.Y);
1445 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1446 m_animated_meshnode->setTransitionTime(m_animation_blend);
1449 void updateBonePosition()
1451 if(!m_bone_position.size() || m_animated_meshnode == NULL)
1454 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1455 for(std::map<std::string, core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){
1456 std::string bone_name = (*ii).first;
1457 v3f bone_pos = (*ii).second.X;
1458 v3f bone_rot = (*ii).second.Y;
1459 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1462 bone->setPosition(bone_pos);
1463 bone->setRotation(bone_rot);
1468 void updateAttachments()
1470 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1471 m_is_visible = !m_attached_to_local; // Objects attached to the local player should always be hidden
1473 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1477 v3f old_position = m_meshnode->getAbsolutePosition();
1478 v3f old_rotation = m_meshnode->getRotation();
1479 m_meshnode->setParent(m_smgr->getRootSceneNode());
1480 m_meshnode->setPosition(old_position);
1481 m_meshnode->setRotation(old_rotation);
1482 m_meshnode->updateAbsolutePosition();
1484 if(m_animated_meshnode)
1486 v3f old_position = m_animated_meshnode->getAbsolutePosition();
1487 v3f old_rotation = m_animated_meshnode->getRotation();
1488 m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1489 m_animated_meshnode->setPosition(old_position);
1490 m_animated_meshnode->setRotation(old_rotation);
1491 m_animated_meshnode->updateAbsolutePosition();
1495 v3f old_position = m_spritenode->getAbsolutePosition();
1496 v3f old_rotation = m_spritenode->getRotation();
1497 m_spritenode->setParent(m_smgr->getRootSceneNode());
1498 m_spritenode->setPosition(old_position);
1499 m_spritenode->setRotation(old_rotation);
1500 m_spritenode->updateAbsolutePosition();
1502 if(m_is_local_player)
1504 LocalPlayer *player = m_env->getLocalPlayer();
1505 player->isAttached = false;
1510 scene::IMeshSceneNode *parent_mesh = NULL;
1511 if(getParent()->getMeshSceneNode())
1512 parent_mesh = getParent()->getMeshSceneNode();
1513 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1514 if(getParent()->getAnimatedMeshSceneNode())
1515 parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1516 scene::IBillboardSceneNode *parent_sprite = NULL;
1517 if(getParent()->getSpriteSceneNode())
1518 parent_sprite = getParent()->getSpriteSceneNode();
1520 scene::IBoneSceneNode *parent_bone = NULL;
1521 if(parent_animated_mesh && m_attachment_bone != "")
1522 parent_bone = parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1524 // The spaghetti code below makes sure attaching works if either the parent or child is a spritenode, meshnode, or animatedmeshnode
1525 // TODO: Perhaps use polymorphism here to save code duplication
1528 m_meshnode->setParent(parent_bone);
1529 m_meshnode->setPosition(m_attachment_position);
1530 m_meshnode->setRotation(m_attachment_rotation);
1531 m_meshnode->updateAbsolutePosition();
1536 m_meshnode->setParent(parent_mesh);
1537 m_meshnode->setPosition(m_attachment_position);
1538 m_meshnode->setRotation(m_attachment_rotation);
1539 m_meshnode->updateAbsolutePosition();
1541 else if(parent_animated_mesh){
1542 m_meshnode->setParent(parent_animated_mesh);
1543 m_meshnode->setPosition(m_attachment_position);
1544 m_meshnode->setRotation(m_attachment_rotation);
1545 m_meshnode->updateAbsolutePosition();
1547 else if(parent_sprite){
1548 m_meshnode->setParent(parent_sprite);
1549 m_meshnode->setPosition(m_attachment_position);
1550 m_meshnode->setRotation(m_attachment_rotation);
1551 m_meshnode->updateAbsolutePosition();
1555 if(m_animated_meshnode){
1557 m_animated_meshnode->setParent(parent_bone);
1558 m_animated_meshnode->setPosition(m_attachment_position);
1559 m_animated_meshnode->setRotation(m_attachment_rotation);
1560 m_animated_meshnode->updateAbsolutePosition();
1565 m_animated_meshnode->setParent(parent_mesh);
1566 m_animated_meshnode->setPosition(m_attachment_position);
1567 m_animated_meshnode->setRotation(m_attachment_rotation);
1568 m_animated_meshnode->updateAbsolutePosition();
1570 else if(parent_animated_mesh){
1571 m_animated_meshnode->setParent(parent_animated_mesh);
1572 m_animated_meshnode->setPosition(m_attachment_position);
1573 m_animated_meshnode->setRotation(m_attachment_rotation);
1574 m_animated_meshnode->updateAbsolutePosition();
1576 else if(parent_sprite){
1577 m_animated_meshnode->setParent(parent_sprite);
1578 m_animated_meshnode->setPosition(m_attachment_position);
1579 m_animated_meshnode->setRotation(m_attachment_rotation);
1580 m_animated_meshnode->updateAbsolutePosition();
1586 m_spritenode->setParent(parent_bone);
1587 m_spritenode->setPosition(m_attachment_position);
1588 m_spritenode->setRotation(m_attachment_rotation);
1589 m_spritenode->updateAbsolutePosition();
1594 m_spritenode->setParent(parent_mesh);
1595 m_spritenode->setPosition(m_attachment_position);
1596 m_spritenode->setRotation(m_attachment_rotation);
1597 m_spritenode->updateAbsolutePosition();
1599 else if(parent_animated_mesh){
1600 m_spritenode->setParent(parent_animated_mesh);
1601 m_spritenode->setPosition(m_attachment_position);
1602 m_spritenode->setRotation(m_attachment_rotation);
1603 m_spritenode->updateAbsolutePosition();
1605 else if(parent_sprite){
1606 m_spritenode->setParent(parent_sprite);
1607 m_spritenode->setPosition(m_attachment_position);
1608 m_spritenode->setRotation(m_attachment_rotation);
1609 m_spritenode->updateAbsolutePosition();
1613 if(m_is_local_player)
1615 LocalPlayer *player = m_env->getLocalPlayer();
1616 player->isAttached = true;
1621 void processMessage(const std::string &data)
1623 //infostream<<"GenericCAO: Got message"<<std::endl;
1624 std::istringstream is(data, std::ios::binary);
1626 u8 cmd = readU8(is);
1627 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1629 m_prop = gob_read_set_properties(is);
1631 m_selection_box = m_prop.collisionbox;
1632 m_selection_box.MinEdge *= BS;
1633 m_selection_box.MaxEdge *= BS;
1635 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1636 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1638 if(!m_initial_tx_basepos_set){
1639 m_initial_tx_basepos_set = true;
1640 m_tx_basepos = m_prop.initial_sprite_basepos;
1645 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1647 // Not sent by the server if this object is an attachment.
1648 // We might however get here if the server notices the object being detached before the client.
1649 m_position = readV3F1000(is);
1650 m_velocity = readV3F1000(is);
1651 m_acceleration = readV3F1000(is);
1652 if(fabs(m_prop.automatic_rotate) < 0.001)
1653 m_yaw = readF1000(is);
1656 bool do_interpolate = readU8(is);
1657 bool is_end_position = readU8(is);
1658 float update_interval = readF1000(is);
1660 // Place us a bit higher if we're physical, to not sink into
1661 // the ground due to sucky collision detection...
1663 m_position += v3f(0,0.002,0);
1665 if(getParent() != NULL) // Just in case
1669 if(!m_prop.physical)
1670 pos_translator.update(m_position, is_end_position, update_interval);
1672 pos_translator.init(m_position);
1676 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1678 std::string mod = deSerializeString(is);
1679 updateTextures(mod);
1681 else if(cmd == GENERIC_CMD_SET_SPRITE)
1683 v2s16 p = readV2S16(is);
1684 int num_frames = readU16(is);
1685 float framelength = readF1000(is);
1686 bool select_horiz_by_yawpitch = readU8(is);
1689 m_anim_num_frames = num_frames;
1690 m_anim_framelength = framelength;
1691 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1695 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE)
1697 float override_speed = readF1000(is);
1698 float override_jump = readF1000(is);
1699 float override_gravity = readF1000(is);
1700 // these are sent inverted so we get true when the server sends nothing
1701 bool sneak = !readU8(is);
1702 bool sneak_glitch = !readU8(is);
1704 if(m_is_local_player)
1706 LocalPlayer *player = m_env->getLocalPlayer();
1707 player->physics_override_speed = override_speed;
1708 player->physics_override_jump = override_jump;
1709 player->physics_override_gravity = override_gravity;
1710 player->physics_override_sneak = sneak;
1711 player->physics_override_sneak_glitch = sneak_glitch;
1714 else if(cmd == GENERIC_CMD_SET_ANIMATION)
1716 m_animation_range = readV2F1000(is);
1717 m_animation_speed = readF1000(is);
1718 m_animation_blend = readF1000(is);
1722 else if(cmd == GENERIC_CMD_SET_BONE_POSITION)
1724 std::string bone = deSerializeString(is);
1725 v3f position = readV3F1000(is);
1726 v3f rotation = readV3F1000(is);
1727 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1729 updateBonePosition();
1731 else if(cmd == GENERIC_CMD_SET_ATTACHMENT)
1733 // If an entry already exists for this object, delete it first to avoid duplicates
1734 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1736 if(ii->X == getId()) // This is the ID of our object
1738 m_env->attachment_list.erase(ii);
1742 m_env->attachment_list.push_back(core::vector2d<int>(getId(), readS16(is)));
1743 m_attachment_bone = deSerializeString(is);
1744 m_attachment_position = readV3F1000(is);
1745 m_attachment_rotation = readV3F1000(is);
1747 updateAttachments();
1749 else if(cmd == GENERIC_CMD_PUNCHED)
1751 /*s16 damage =*/ readS16(is);
1752 s16 result_hp = readS16(is);
1754 // Use this instead of the send damage to not interfere with prediction
1755 s16 damage = m_hp - result_hp;
1761 // TODO: Execute defined fast response
1762 // As there is no definition, make a smoke puff
1763 ClientSimpleObject *simple = createSmokePuff(
1764 m_smgr, m_env, m_position,
1765 m_prop.visual_size * BS);
1766 m_env->addSimpleObject(simple);
1768 // TODO: Execute defined fast response
1769 // Flashing shall suffice as there is no definition
1770 m_reset_textures_timer = 0.05;
1772 m_reset_textures_timer += 0.05 * damage;
1773 updateTextures("^[brighten");
1777 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1779 m_armor_groups.clear();
1780 int armor_groups_size = readU16(is);
1781 for(int i=0; i<armor_groups_size; i++){
1782 std::string name = deSerializeString(is);
1783 int rating = readS16(is);
1784 m_armor_groups[name] = rating;
1789 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1790 float time_from_last_punch=1000000)
1793 const ToolCapabilities *toolcap =
1794 &punchitem->getToolCapabilities(m_gamedef->idef());
1795 PunchDamageResult result = getPunchDamage(
1799 time_from_last_punch);
1801 if(result.did_punch && result.damage != 0)
1803 if(result.damage < m_hp){
1804 m_hp -= result.damage;
1807 // TODO: Execute defined fast response
1808 // As there is no definition, make a smoke puff
1809 ClientSimpleObject *simple = createSmokePuff(
1810 m_smgr, m_env, m_position,
1811 m_prop.visual_size * BS);
1812 m_env->addSimpleObject(simple);
1814 // TODO: Execute defined fast response
1815 // Flashing shall suffice as there is no definition
1816 m_reset_textures_timer = 0.05;
1817 if(result.damage >= 2)
1818 m_reset_textures_timer += 0.05 * result.damage;
1819 updateTextures("^[brighten");
1825 std::string debugInfoText()
1827 std::ostringstream os(std::ios::binary);
1828 os<<"GenericCAO hp="<<m_hp<<"\n";
1830 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1831 i != m_armor_groups.end(); i++){
1832 os<<i->first<<"="<<i->second<<", ";
1840 GenericCAO proto_GenericCAO(NULL, NULL);