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 "camera.h" // CameraModes
45 #include <IMeshManipulator.h>
46 #include <IAnimatedMeshSceneNode.h>
47 #include <IBoneSceneNode.h>
50 struct ToolCapabilities;
52 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
54 std::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
60 struct SmoothTranslator
67 f32 anim_time_counter;
87 anim_time_counter = 0;
96 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
98 aim_is_end = is_end_position;
101 if(update_interval > 0){
102 anim_time = update_interval;
104 if(anim_time < 0.001 || anim_time > 1.0)
105 anim_time = anim_time_counter;
107 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
109 anim_time_counter = 0;
113 void translate(f32 dtime)
115 anim_time_counter = anim_time_counter + dtime;
116 anim_counter = anim_counter + dtime;
117 v3f vect_move = vect_aim - vect_old;
119 if(anim_time > 0.001)
120 moveratio = anim_time_counter / anim_time;
121 // Move a bit less than should, to avoid oscillation
122 moveratio = moveratio * 0.8;
123 float move_end = 1.5;
126 if(moveratio > move_end)
127 moveratio = move_end;
128 vect_show = vect_old + vect_move * moveratio;
133 return ((anim_time_counter / anim_time) < 1.4);
141 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
142 float txs, float tys, int col, int row)
144 video::SMaterial& material = bill->getMaterial(0);
145 core::matrix4& matrix = material.getTextureMatrix(0);
146 matrix.setTextureTranslate(txs*col, tys*row);
147 matrix.setTextureScale(txs, tys);
154 class TestCAO : public ClientActiveObject
157 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
162 return ACTIVEOBJECT_TYPE_TEST;
165 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
167 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
168 IrrlichtDevice *irr);
169 void removeFromScene(bool permanent);
170 void updateLight(u8 light_at_pos);
171 v3s16 getLightPosition();
172 void updateNodePos();
174 void step(float dtime, ClientEnvironment *env);
176 void processMessage(const std::string &data);
178 bool getCollisionBox(aabb3f *toset) { return false; }
180 scene::IMeshSceneNode *m_node;
185 TestCAO proto_TestCAO(NULL, NULL);
187 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
188 ClientActiveObject(0, gamedef, env),
190 m_position(v3f(0,10*BS,0))
192 ClientActiveObject::registerType(getType(), create);
199 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
201 return new TestCAO(gamedef, env);
204 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
210 //video::IVideoDriver* driver = smgr->getVideoDriver();
212 scene::SMesh *mesh = new scene::SMesh();
213 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
214 video::SColor c(255,255,255,255);
215 video::S3DVertex vertices[4] =
217 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
218 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
219 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
220 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
222 u16 indices[] = {0,1,2,2,3,0};
223 buf->append(vertices, 4, indices, 6);
225 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
226 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
227 buf->getMaterial().setTexture(0, tsrc->getTexture("rat.png"));
228 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
229 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
230 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
232 mesh->addMeshBuffer(buf);
234 m_node = smgr->addMeshSceneNode(mesh, NULL);
239 void TestCAO::removeFromScene(bool permanent)
248 void TestCAO::updateLight(u8 light_at_pos)
252 v3s16 TestCAO::getLightPosition()
254 return floatToInt(m_position, BS);
257 void TestCAO::updateNodePos()
262 m_node->setPosition(m_position);
263 //m_node->setRotation(v3f(0, 45, 0));
266 void TestCAO::step(float dtime, ClientEnvironment *env)
270 v3f rot = m_node->getRotation();
271 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
272 rot.Y += dtime * 180;
273 m_node->setRotation(rot);
277 void TestCAO::processMessage(const std::string &data)
279 infostream<<"TestCAO: Got data: "<<data<<std::endl;
280 std::istringstream is(data, std::ios::binary);
298 class ItemCAO : public ClientActiveObject
301 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
306 return ACTIVEOBJECT_TYPE_ITEM;
309 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
311 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
312 IrrlichtDevice *irr);
313 void removeFromScene(bool permanent);
314 void updateLight(u8 light_at_pos);
315 v3s16 getLightPosition();
316 void updateNodePos();
317 void updateInfoText();
318 void updateTexture();
320 void step(float dtime, ClientEnvironment *env);
322 void processMessage(const std::string &data);
324 void initialize(const std::string &data);
326 core::aabbox3d<f32>* getSelectionBox()
327 {return &m_selection_box;}
331 std::string infoText()
334 bool getCollisionBox(aabb3f *toset) { return false; }
336 core::aabbox3d<f32> m_selection_box;
337 scene::IMeshSceneNode *m_node;
339 std::string m_itemstring;
340 std::string m_infotext;
343 #include "inventory.h"
346 ItemCAO proto_ItemCAO(NULL, NULL);
348 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
349 ClientActiveObject(0, gamedef, env),
350 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
352 m_position(v3f(0,10*BS,0))
356 ClientActiveObject::registerType(getType(), create);
364 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
366 return new ItemCAO(gamedef, env);
369 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
375 //video::IVideoDriver* driver = smgr->getVideoDriver();
377 scene::SMesh *mesh = new scene::SMesh();
378 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
379 video::SColor c(255,255,255,255);
380 video::S3DVertex vertices[4] =
382 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
383 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
384 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
385 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
386 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
387 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
388 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
389 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
391 u16 indices[] = {0,1,2,2,3,0};
392 buf->append(vertices, 4, indices, 6);
394 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
395 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
396 // Initialize with a generated placeholder texture
397 buf->getMaterial().setTexture(0, tsrc->getTexture(""));
398 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
399 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
400 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
402 mesh->addMeshBuffer(buf);
404 m_node = smgr->addMeshSceneNode(mesh, NULL);
415 void ItemCAO::removeFromScene(bool permanent)
424 void ItemCAO::updateLight(u8 light_at_pos)
429 u8 li = decode_light(light_at_pos);
430 video::SColor color(255,li,li,li);
431 setMeshColor(m_node->getMesh(), color);
434 v3s16 ItemCAO::getLightPosition()
436 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
439 void ItemCAO::updateNodePos()
444 m_node->setPosition(m_position);
447 void ItemCAO::updateInfoText()
450 IItemDefManager *idef = m_gamedef->idef();
452 item.deSerialize(m_itemstring, idef);
453 if(item.isKnown(idef))
454 m_infotext = item.getDefinition(idef).description;
456 m_infotext = "Unknown item: '" + m_itemstring + "'";
458 m_infotext += " (" + itos(item.count) + ")";
460 catch(SerializationError &e)
462 m_infotext = "Unknown item: '" + m_itemstring + "'";
466 void ItemCAO::updateTexture()
471 // Create an inventory item to see what is its image
472 std::istringstream is(m_itemstring, std::ios_base::binary);
473 video::ITexture *texture = NULL;
475 IItemDefManager *idef = m_gamedef->idef();
477 item.deSerialize(is, idef);
478 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
480 catch(SerializationError &e)
482 infostream<<"WARNING: "<<__FUNCTION_NAME
483 <<": error deSerializing itemstring \""
484 <<m_itemstring<<std::endl;
487 // Set meshbuffer texture
488 m_node->getMaterial(0).setTexture(0, texture);
492 void ItemCAO::step(float dtime, ClientEnvironment *env)
496 /*v3f rot = m_node->getRotation();
497 rot.Y += dtime * 120;
498 m_node->setRotation(rot);*/
499 LocalPlayer *player = env->getLocalPlayer();
501 v3f rot = m_node->getRotation();
502 rot.Y = 180.0 - (player->getYaw());
503 m_node->setRotation(rot);
507 void ItemCAO::processMessage(const std::string &data)
509 //infostream<<"ItemCAO: Got message"<<std::endl;
510 std::istringstream is(data, std::ios::binary);
516 m_position = readV3F1000(is);
522 m_itemstring = deSerializeString(is);
528 void ItemCAO::initialize(const std::string &data)
530 infostream<<"ItemCAO: Got init data"<<std::endl;
533 std::istringstream is(data, std::ios::binary);
535 u8 version = readU8(is);
540 m_position = readV3F1000(is);
542 m_itemstring = deSerializeString(is);
553 #include "genericobject.h"
555 class GenericCAO : public ClientActiveObject
558 // Only set at initialization
561 bool m_is_local_player;
563 // Property-ish things
564 ObjectProperties m_prop;
566 scene::ISceneManager *m_smgr;
567 IrrlichtDevice *m_irr;
568 core::aabbox3d<f32> m_selection_box;
569 scene::IMeshSceneNode *m_meshnode;
570 scene::IAnimatedMeshSceneNode *m_animated_meshnode;
571 scene::IBillboardSceneNode *m_spritenode;
572 scene::ITextSceneNode* m_textnode;
578 SmoothTranslator pos_translator;
579 // Spritesheet/animation stuff
582 bool m_initial_tx_basepos_set;
583 bool m_tx_select_horiz_by_yawpitch;
584 v2s32 m_animation_range;
585 int m_animation_speed;
586 int m_animation_blend;
587 std::map<std::string, core::vector2d<v3f> > m_bone_position; // stores position and rotation for each bone name
588 std::string m_attachment_bone;
589 v3f m_attachment_position;
590 v3f m_attachment_rotation;
591 bool m_attached_to_local;
593 int m_anim_num_frames;
594 float m_anim_framelength;
596 ItemGroupList m_armor_groups;
597 float m_reset_textures_timer;
598 bool m_visuals_expired;
599 float m_step_distance_counter;
604 GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
605 ClientActiveObject(0, gamedef, env),
608 m_is_local_player(false),
613 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
615 m_animated_meshnode(NULL),
618 m_position(v3f(0,10*BS,0)),
619 m_velocity(v3f(0,0,0)),
620 m_acceleration(v3f(0,0,0)),
625 m_initial_tx_basepos_set(false),
626 m_tx_select_horiz_by_yawpitch(false),
627 m_animation_range(v2s32(0,0)),
628 m_animation_speed(15),
629 m_animation_blend(0),
630 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
631 m_attachment_bone(""),
632 m_attachment_position(v3f(0,0,0)),
633 m_attachment_rotation(v3f(0,0,0)),
634 m_attached_to_local(false),
636 m_anim_num_frames(1),
637 m_anim_framelength(0.2),
639 m_reset_textures_timer(-1),
640 m_visuals_expired(false),
641 m_step_distance_counter(0),
646 ClientActiveObject::registerType(getType(), create);
649 bool getCollisionBox(aabb3f *toset) {
650 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)
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 // Handel model of local player instantly to prevent lags
1082 if(m_is_local_player) {
1083 LocalPlayer *player = m_env->getLocalPlayer();
1085 if (player->camera_mode > CAMERA_MODE_FIRST) {
1086 int old_anim = player->last_animation;
1087 float old_anim_speed = player->last_animation_speed;
1088 m_is_visible = true;
1089 m_position = player->getPosition() + v3f(0,BS,0);
1090 m_velocity = v3f(0,0,0);
1091 m_acceleration = v3f(0,0,0);
1092 pos_translator.vect_show = m_position;
1093 m_yaw = player->getYaw();
1094 PlayerControl controls = player->getPlayerControl();
1096 bool walking = false;
1097 if(controls.up || controls.down || controls.left || controls.right)
1100 f32 new_speed = player->local_animation_speed;
1101 v2s32 new_anim = v2s32(0,0);
1102 bool allow_update = false;
1104 if(!player->touching_ground &&
1105 g_settings->getBool("free_move") &&
1106 m_gamedef->checkLocalPrivilege("fly") &&
1107 g_settings->getBool("fast_move") &&
1108 m_gamedef->checkLocalPrivilege("fast"))
1110 if(controls.sneak && walking)
1113 if(walking && (controls.LMB || controls.RMB)) {
1114 new_anim = player->local_animations[3];
1115 player->last_animation = WD_ANIM;
1116 } else if(walking) {
1117 new_anim = player->local_animations[1];
1118 player->last_animation = WALK_ANIM;
1119 } else if(controls.LMB || controls.RMB) {
1120 new_anim = player->local_animations[2];
1121 player->last_animation = DIG_ANIM;
1124 if ((new_anim.X + new_anim.Y) > 0) {
1125 allow_update = true;
1126 m_animation_range = new_anim;
1127 m_animation_speed = new_speed;
1128 player->last_animation_speed = m_animation_speed;
1130 player->last_animation = NO_ANIM;
1132 // reset animation when no input detected
1133 if (!walking && !controls.LMB && !controls.RMB) {
1134 player->last_animation = NO_ANIM;
1135 if (old_anim != NO_ANIM) {
1136 m_animation_range = player->local_animations[0];
1141 // Update local player animations
1142 if ((player->last_animation != old_anim || m_animation_speed != old_anim_speed) &&
1143 player->last_animation != NO_ANIM && allow_update)
1147 m_is_visible = false;
1151 if(m_visuals_expired && m_smgr && m_irr){
1152 m_visuals_expired = false;
1154 // Attachments, part 1: All attached objects must be unparented first, or Irrlicht causes a segmentation fault
1155 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1157 if(ii->Y == getId()) // This is a child of our parent
1159 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1162 scene::IMeshSceneNode *m_child_meshnode = obj->getMeshSceneNode();
1163 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode = obj->getAnimatedMeshSceneNode();
1164 scene::IBillboardSceneNode *m_child_spritenode = obj->getSpriteSceneNode();
1165 if(m_child_meshnode)
1166 m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1167 if(m_child_animated_meshnode)
1168 m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1169 if(m_child_spritenode)
1170 m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1175 removeFromScene(false);
1176 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1178 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1179 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1181 if(ii->Y == getId()) // This is a child of our parent
1183 ClientActiveObject *obj = m_env->getActiveObject(ii->X); // Get the object of the child
1185 obj->setAttachments();
1190 // Make sure m_is_visible is always applied
1192 m_meshnode->setVisible(m_is_visible);
1193 if(m_animated_meshnode)
1194 m_animated_meshnode->setVisible(m_is_visible);
1196 m_spritenode->setVisible(m_is_visible);
1198 m_textnode->setVisible(m_is_visible);
1200 if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1202 // Set these for later
1203 m_position = getPosition();
1204 m_velocity = v3f(0,0,0);
1205 m_acceleration = v3f(0,0,0);
1206 pos_translator.vect_show = m_position;
1208 if(m_is_local_player) // Update local player attachment position
1210 LocalPlayer *player = m_env->getLocalPlayer();
1211 player->overridePosition = getParent()->getPosition();
1212 m_env->getLocalPlayer()->parent = getParent();
1217 v3f lastpos = pos_translator.vect_show;
1219 if(m_prop.physical){
1220 core::aabbox3d<f32> box = m_prop.collisionbox;
1223 collisionMoveResult moveresult;
1224 f32 pos_max_d = BS*0.125; // Distance per iteration
1225 v3f p_pos = m_position;
1226 v3f p_velocity = m_velocity;
1227 v3f p_acceleration = m_acceleration;
1228 moveresult = collisionMoveSimple(env,env->getGameDef(),
1229 pos_max_d, box, m_prop.stepheight, dtime,
1230 p_pos, p_velocity, p_acceleration,
1231 this, m_prop.collideWithObjects);
1234 m_velocity = p_velocity;
1235 m_acceleration = p_acceleration;
1237 bool is_end_position = moveresult.collides;
1238 pos_translator.update(m_position, is_end_position, dtime);
1239 pos_translator.translate(dtime);
1242 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1243 m_velocity += dtime * m_acceleration;
1244 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
1245 pos_translator.translate(dtime);
1249 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1250 m_step_distance_counter += moved;
1251 if(m_step_distance_counter > 1.5*BS){
1252 m_step_distance_counter = 0;
1253 if(!m_is_local_player && m_prop.makes_footstep_sound){
1254 INodeDefManager *ndef = m_gamedef->ndef();
1255 v3s16 p = floatToInt(getPosition() + v3f(0,
1256 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1257 MapNode n = m_env->getMap().getNodeNoEx(p);
1258 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1259 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1264 m_anim_timer += dtime;
1265 if(m_anim_timer >= m_anim_framelength){
1266 m_anim_timer -= m_anim_framelength;
1268 if(m_anim_frame >= m_anim_num_frames)
1274 if(m_reset_textures_timer >= 0){
1275 m_reset_textures_timer -= dtime;
1276 if(m_reset_textures_timer <= 0){
1277 m_reset_textures_timer = -1;
1281 if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001){
1282 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1286 if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1287 (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)){
1288 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI + m_prop.automatic_face_movement_dir_offset;
1293 void updateTexturePos()
1296 scene::ICameraSceneNode* camera =
1297 m_spritenode->getSceneManager()->getActiveCamera();
1300 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1301 - camera->getAbsolutePosition();
1302 cam_to_entity.normalize();
1304 int row = m_tx_basepos.Y;
1305 int col = m_tx_basepos.X;
1307 if(m_tx_select_horiz_by_yawpitch)
1309 if(cam_to_entity.Y > 0.75)
1311 else if(cam_to_entity.Y < -0.75)
1314 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1315 float dir = mob_dir - m_yaw;
1316 dir = wrapDegrees_180(dir);
1317 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1318 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1320 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1322 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1324 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1331 // Animation goes downwards
1332 row += m_anim_frame;
1334 float txs = m_tx_size.X;
1335 float tys = m_tx_size.Y;
1336 setBillboardTextureMatrix(m_spritenode,
1337 txs, tys, col, row);
1341 void updateTextures(const std::string &mod)
1343 ITextureSource *tsrc = m_gamedef->tsrc();
1345 bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1346 bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1347 bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1351 if(m_prop.visual == "sprite")
1353 std::string texturestring = "unknown_node.png";
1354 if(m_prop.textures.size() >= 1)
1355 texturestring = m_prop.textures[0];
1356 texturestring += mod;
1357 m_spritenode->setMaterialTexture(0,
1358 tsrc->getTexture(texturestring));
1360 // This allows setting per-material colors. However, until a real lighting
1361 // system is added, the code below will have no effect. Once MineTest
1362 // has directional lighting, it should work automatically.
1363 if(m_prop.colors.size() >= 1)
1365 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1366 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1367 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1370 m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1371 m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1372 m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1375 if(m_animated_meshnode)
1377 if(m_prop.visual == "mesh")
1379 for (u32 i = 0; i < m_prop.textures.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1381 std::string texturestring = m_prop.textures[i];
1382 if(texturestring == "")
1383 continue; // Empty texture string means don't modify that material
1384 texturestring += mod;
1385 video::ITexture* texture = tsrc->getTexture(texturestring);
1388 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1392 // Set material flags and texture
1393 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1394 material.TextureLayer[0].Texture = texture;
1395 material.setFlag(video::EMF_LIGHTING, false);
1396 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1398 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1399 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1400 m_animated_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1402 for (u32 i = 0; i < m_prop.colors.size() && i < m_animated_meshnode->getMaterialCount(); ++i)
1404 // This allows setting per-material colors. However, until a real lighting
1405 // system is added, the code below will have no effect. Once MineTest
1406 // has directional lighting, it should work automatically.
1407 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1408 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1409 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1415 if(m_prop.visual == "cube")
1417 for (u32 i = 0; i < 6; ++i)
1419 std::string texturestring = "unknown_node.png";
1420 if(m_prop.textures.size() > i)
1421 texturestring = m_prop.textures[i];
1422 texturestring += mod;
1425 // Set material flags and texture
1426 video::SMaterial& material = m_meshnode->getMaterial(i);
1427 material.setFlag(video::EMF_LIGHTING, false);
1428 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1429 material.setTexture(0,
1430 tsrc->getTexture(texturestring));
1431 material.getTextureMatrix(0).makeIdentity();
1433 // This allows setting per-material colors. However, until a real lighting
1434 // system is added, the code below will have no effect. Once MineTest
1435 // has directional lighting, it should work automatically.
1436 if(m_prop.colors.size() > i)
1438 m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1439 m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1440 m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1443 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1444 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1445 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1448 else if(m_prop.visual == "upright_sprite")
1450 scene::IMesh *mesh = m_meshnode->getMesh();
1452 std::string tname = "unknown_object.png";
1453 if(m_prop.textures.size() >= 1)
1454 tname = m_prop.textures[0];
1456 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1457 buf->getMaterial().setTexture(0,
1458 tsrc->getTexture(tname));
1460 // This allows setting per-material colors. However, until a real lighting
1461 // system is added, the code below will have no effect. Once MineTest
1462 // has directional lighting, it should work automatically.
1463 if(m_prop.colors.size() >= 1)
1465 buf->getMaterial().AmbientColor = m_prop.colors[0];
1466 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1467 buf->getMaterial().SpecularColor = m_prop.colors[0];
1470 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1471 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1472 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1475 std::string tname = "unknown_object.png";
1476 if(m_prop.textures.size() >= 2)
1477 tname = m_prop.textures[1];
1478 else if(m_prop.textures.size() >= 1)
1479 tname = m_prop.textures[0];
1481 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1482 buf->getMaterial().setTexture(0,
1483 tsrc->getTexture(tname));
1485 // This allows setting per-material colors. However, until a real lighting
1486 // system is added, the code below will have no effect. Once MineTest
1487 // has directional lighting, it should work automatically.
1488 if(m_prop.colors.size() >= 2)
1490 buf->getMaterial().AmbientColor = m_prop.colors[1];
1491 buf->getMaterial().DiffuseColor = m_prop.colors[1];
1492 buf->getMaterial().SpecularColor = m_prop.colors[1];
1494 else if(m_prop.colors.size() >= 1)
1496 buf->getMaterial().AmbientColor = m_prop.colors[0];
1497 buf->getMaterial().DiffuseColor = m_prop.colors[0];
1498 buf->getMaterial().SpecularColor = m_prop.colors[0];
1501 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1502 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1503 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1509 void updateAnimation()
1511 if(m_animated_meshnode == NULL)
1513 m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1514 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1515 m_animated_meshnode->setTransitionTime(m_animation_blend);
1518 void updateBonePosition()
1520 if(!m_bone_position.size() || m_animated_meshnode == NULL)
1523 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1524 for(std::map<std::string, core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){
1525 std::string bone_name = (*ii).first;
1526 v3f bone_pos = (*ii).second.X;
1527 v3f bone_rot = (*ii).second.Y;
1528 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1531 bone->setPosition(bone_pos);
1532 bone->setRotation(bone_rot);
1537 void updateAttachments()
1539 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1540 m_is_visible = !m_attached_to_local; // Objects attached to the local player should always be hidden
1542 if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1546 v3f old_position = m_meshnode->getAbsolutePosition();
1547 v3f old_rotation = m_meshnode->getRotation();
1548 m_meshnode->setParent(m_smgr->getRootSceneNode());
1549 m_meshnode->setPosition(old_position);
1550 m_meshnode->setRotation(old_rotation);
1551 m_meshnode->updateAbsolutePosition();
1553 if(m_animated_meshnode)
1555 v3f old_position = m_animated_meshnode->getAbsolutePosition();
1556 v3f old_rotation = m_animated_meshnode->getRotation();
1557 m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1558 m_animated_meshnode->setPosition(old_position);
1559 m_animated_meshnode->setRotation(old_rotation);
1560 m_animated_meshnode->updateAbsolutePosition();
1564 v3f old_position = m_spritenode->getAbsolutePosition();
1565 v3f old_rotation = m_spritenode->getRotation();
1566 m_spritenode->setParent(m_smgr->getRootSceneNode());
1567 m_spritenode->setPosition(old_position);
1568 m_spritenode->setRotation(old_rotation);
1569 m_spritenode->updateAbsolutePosition();
1571 if(m_is_local_player)
1573 LocalPlayer *player = m_env->getLocalPlayer();
1574 player->isAttached = false;
1579 scene::IMeshSceneNode *parent_mesh = NULL;
1580 if(getParent()->getMeshSceneNode())
1581 parent_mesh = getParent()->getMeshSceneNode();
1582 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1583 if(getParent()->getAnimatedMeshSceneNode())
1584 parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1585 scene::IBillboardSceneNode *parent_sprite = NULL;
1586 if(getParent()->getSpriteSceneNode())
1587 parent_sprite = getParent()->getSpriteSceneNode();
1589 scene::IBoneSceneNode *parent_bone = NULL;
1590 if(parent_animated_mesh && m_attachment_bone != "")
1591 parent_bone = parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1593 // The spaghetti code below makes sure attaching works if either the parent or child is a spritenode, meshnode, or animatedmeshnode
1594 // TODO: Perhaps use polymorphism here to save code duplication
1597 m_meshnode->setParent(parent_bone);
1598 m_meshnode->setPosition(m_attachment_position);
1599 m_meshnode->setRotation(m_attachment_rotation);
1600 m_meshnode->updateAbsolutePosition();
1605 m_meshnode->setParent(parent_mesh);
1606 m_meshnode->setPosition(m_attachment_position);
1607 m_meshnode->setRotation(m_attachment_rotation);
1608 m_meshnode->updateAbsolutePosition();
1610 else if(parent_animated_mesh){
1611 m_meshnode->setParent(parent_animated_mesh);
1612 m_meshnode->setPosition(m_attachment_position);
1613 m_meshnode->setRotation(m_attachment_rotation);
1614 m_meshnode->updateAbsolutePosition();
1616 else if(parent_sprite){
1617 m_meshnode->setParent(parent_sprite);
1618 m_meshnode->setPosition(m_attachment_position);
1619 m_meshnode->setRotation(m_attachment_rotation);
1620 m_meshnode->updateAbsolutePosition();
1624 if(m_animated_meshnode){
1626 m_animated_meshnode->setParent(parent_bone);
1627 m_animated_meshnode->setPosition(m_attachment_position);
1628 m_animated_meshnode->setRotation(m_attachment_rotation);
1629 m_animated_meshnode->updateAbsolutePosition();
1634 m_animated_meshnode->setParent(parent_mesh);
1635 m_animated_meshnode->setPosition(m_attachment_position);
1636 m_animated_meshnode->setRotation(m_attachment_rotation);
1637 m_animated_meshnode->updateAbsolutePosition();
1639 else if(parent_animated_mesh){
1640 m_animated_meshnode->setParent(parent_animated_mesh);
1641 m_animated_meshnode->setPosition(m_attachment_position);
1642 m_animated_meshnode->setRotation(m_attachment_rotation);
1643 m_animated_meshnode->updateAbsolutePosition();
1645 else if(parent_sprite){
1646 m_animated_meshnode->setParent(parent_sprite);
1647 m_animated_meshnode->setPosition(m_attachment_position);
1648 m_animated_meshnode->setRotation(m_attachment_rotation);
1649 m_animated_meshnode->updateAbsolutePosition();
1655 m_spritenode->setParent(parent_bone);
1656 m_spritenode->setPosition(m_attachment_position);
1657 m_spritenode->setRotation(m_attachment_rotation);
1658 m_spritenode->updateAbsolutePosition();
1663 m_spritenode->setParent(parent_mesh);
1664 m_spritenode->setPosition(m_attachment_position);
1665 m_spritenode->setRotation(m_attachment_rotation);
1666 m_spritenode->updateAbsolutePosition();
1668 else if(parent_animated_mesh){
1669 m_spritenode->setParent(parent_animated_mesh);
1670 m_spritenode->setPosition(m_attachment_position);
1671 m_spritenode->setRotation(m_attachment_rotation);
1672 m_spritenode->updateAbsolutePosition();
1674 else if(parent_sprite){
1675 m_spritenode->setParent(parent_sprite);
1676 m_spritenode->setPosition(m_attachment_position);
1677 m_spritenode->setRotation(m_attachment_rotation);
1678 m_spritenode->updateAbsolutePosition();
1682 if(m_is_local_player)
1684 LocalPlayer *player = m_env->getLocalPlayer();
1685 player->isAttached = true;
1690 void processMessage(const std::string &data)
1692 //infostream<<"GenericCAO: Got message"<<std::endl;
1693 std::istringstream is(data, std::ios::binary);
1695 u8 cmd = readU8(is);
1696 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1698 m_prop = gob_read_set_properties(is);
1700 m_selection_box = m_prop.collisionbox;
1701 m_selection_box.MinEdge *= BS;
1702 m_selection_box.MaxEdge *= BS;
1704 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1705 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1707 if(!m_initial_tx_basepos_set){
1708 m_initial_tx_basepos_set = true;
1709 m_tx_basepos = m_prop.initial_sprite_basepos;
1714 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1716 // Not sent by the server if this object is an attachment.
1717 // We might however get here if the server notices the object being detached before the client.
1718 m_position = readV3F1000(is);
1719 m_velocity = readV3F1000(is);
1720 m_acceleration = readV3F1000(is);
1721 if(fabs(m_prop.automatic_rotate) < 0.001)
1722 m_yaw = readF1000(is);
1725 bool do_interpolate = readU8(is);
1726 bool is_end_position = readU8(is);
1727 float update_interval = readF1000(is);
1729 // Place us a bit higher if we're physical, to not sink into
1730 // the ground due to sucky collision detection...
1732 m_position += v3f(0,0.002,0);
1734 if(getParent() != NULL) // Just in case
1738 if(!m_prop.physical)
1739 pos_translator.update(m_position, is_end_position, update_interval);
1741 pos_translator.init(m_position);
1745 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1747 std::string mod = deSerializeString(is);
1748 updateTextures(mod);
1750 else if(cmd == GENERIC_CMD_SET_SPRITE)
1752 v2s16 p = readV2S16(is);
1753 int num_frames = readU16(is);
1754 float framelength = readF1000(is);
1755 bool select_horiz_by_yawpitch = readU8(is);
1758 m_anim_num_frames = num_frames;
1759 m_anim_framelength = framelength;
1760 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1764 else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE)
1766 float override_speed = readF1000(is);
1767 float override_jump = readF1000(is);
1768 float override_gravity = readF1000(is);
1769 // these are sent inverted so we get true when the server sends nothing
1770 bool sneak = !readU8(is);
1771 bool sneak_glitch = !readU8(is);
1774 if(m_is_local_player)
1776 LocalPlayer *player = m_env->getLocalPlayer();
1777 player->physics_override_speed = override_speed;
1778 player->physics_override_jump = override_jump;
1779 player->physics_override_gravity = override_gravity;
1780 player->physics_override_sneak = sneak;
1781 player->physics_override_sneak_glitch = sneak_glitch;
1784 else if(cmd == GENERIC_CMD_SET_ANIMATION)
1786 // TODO: change frames send as v2s32 value
1787 v2f range = readV2F1000(is);
1788 if (!m_is_local_player) {
1789 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1790 m_animation_speed = readF1000(is);
1791 m_animation_blend = readF1000(is);
1794 LocalPlayer *player = m_env->getLocalPlayer();
1795 if(player->last_animation == NO_ANIM) {
1796 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1797 m_animation_speed = readF1000(is);
1798 m_animation_blend = readF1000(is);
1800 // update animation only if local animations present
1801 // and received animation is not unknown
1803 for (int i = 0;i<4;i++) {
1804 frames += (int)player->local_animations[i].Y;
1807 player->last_animation = NO_ANIM;
1812 else if(cmd == GENERIC_CMD_SET_BONE_POSITION)
1814 std::string bone = deSerializeString(is);
1815 v3f position = readV3F1000(is);
1816 v3f rotation = readV3F1000(is);
1817 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1819 updateBonePosition();
1821 else if(cmd == GENERIC_CMD_SET_ATTACHMENT)
1823 // If an entry already exists for this object, delete it first to avoid duplicates
1824 for(std::vector<core::vector2d<int> >::iterator ii = m_env->attachment_list.begin(); ii != m_env->attachment_list.end(); ii++)
1826 if(ii->X == getId()) // This is the ID of our object
1828 m_env->attachment_list.erase(ii);
1832 m_env->attachment_list.push_back(core::vector2d<int>(getId(), readS16(is)));
1833 m_attachment_bone = deSerializeString(is);
1834 m_attachment_position = readV3F1000(is);
1835 m_attachment_rotation = readV3F1000(is);
1837 updateAttachments();
1839 else if(cmd == GENERIC_CMD_PUNCHED)
1841 /*s16 damage =*/ readS16(is);
1842 s16 result_hp = readS16(is);
1844 // Use this instead of the send damage to not interfere with prediction
1845 s16 damage = m_hp - result_hp;
1851 // TODO: Execute defined fast response
1852 // As there is no definition, make a smoke puff
1853 ClientSimpleObject *simple = createSmokePuff(
1854 m_smgr, m_env, m_position,
1855 m_prop.visual_size * BS);
1856 m_env->addSimpleObject(simple);
1858 // TODO: Execute defined fast response
1859 // Flashing shall suffice as there is no definition
1860 m_reset_textures_timer = 0.05;
1862 m_reset_textures_timer += 0.05 * damage;
1863 updateTextures("^[brighten");
1867 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1869 m_armor_groups.clear();
1870 int armor_groups_size = readU16(is);
1871 for(int i=0; i<armor_groups_size; i++){
1872 std::string name = deSerializeString(is);
1873 int rating = readS16(is);
1874 m_armor_groups[name] = rating;
1879 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1880 float time_from_last_punch=1000000)
1883 const ToolCapabilities *toolcap =
1884 &punchitem->getToolCapabilities(m_gamedef->idef());
1885 PunchDamageResult result = getPunchDamage(
1889 time_from_last_punch);
1891 if(result.did_punch && result.damage != 0)
1893 if(result.damage < m_hp){
1894 m_hp -= result.damage;
1897 // TODO: Execute defined fast response
1898 // As there is no definition, make a smoke puff
1899 ClientSimpleObject *simple = createSmokePuff(
1900 m_smgr, m_env, m_position,
1901 m_prop.visual_size * BS);
1902 m_env->addSimpleObject(simple);
1904 // TODO: Execute defined fast response
1905 // Flashing shall suffice as there is no definition
1906 m_reset_textures_timer = 0.05;
1907 if(result.damage >= 2)
1908 m_reset_textures_timer += 0.05 * result.damage;
1909 updateTextures("^[brighten");
1915 std::string debugInfoText()
1917 std::ostringstream os(std::ios::binary);
1918 os<<"GenericCAO hp="<<m_hp<<"\n";
1920 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1921 i != m_armor_groups.end(); i++){
1922 os<<i->first<<"="<<i->second<<", ";
1930 GenericCAO proto_GenericCAO(NULL, NULL);