3 Copyright (C) 2010-2011 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 <IMeshManipulator.h>
44 #include <IAnimatedMeshSceneNode.h>
45 #include <IBoneSceneNode.h>
48 struct ToolCapabilities;
50 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
52 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
58 struct SmoothTranslator
65 f32 anim_time_counter;
85 anim_time_counter = 0;
94 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
96 aim_is_end = is_end_position;
99 if(update_interval > 0){
100 anim_time = update_interval;
102 if(anim_time < 0.001 || anim_time > 1.0)
103 anim_time = anim_time_counter;
105 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
107 anim_time_counter = 0;
111 void translate(f32 dtime)
113 anim_time_counter = anim_time_counter + dtime;
114 anim_counter = anim_counter + dtime;
115 v3f vect_move = vect_aim - vect_old;
117 if(anim_time > 0.001)
118 moveratio = anim_time_counter / anim_time;
119 // Move a bit less than should, to avoid oscillation
120 moveratio = moveratio * 0.8;
121 float move_end = 1.5;
124 if(moveratio > move_end)
125 moveratio = move_end;
126 vect_show = vect_old + vect_move * moveratio;
131 return ((anim_time_counter / anim_time) < 1.4);
139 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
140 float txs, float tys, int col, int row)
142 video::SMaterial& material = bill->getMaterial(0);
143 core::matrix4& matrix = material.getTextureMatrix(0);
144 matrix.setTextureTranslate(txs*col, tys*row);
145 matrix.setTextureScale(txs, tys);
152 class TestCAO : public ClientActiveObject
155 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
160 return ACTIVEOBJECT_TYPE_TEST;
163 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
165 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
166 IrrlichtDevice *irr);
167 void removeFromScene();
168 void updateLight(u8 light_at_pos);
169 v3s16 getLightPosition();
170 void updateNodePos();
172 void step(float dtime, ClientEnvironment *env);
174 void processMessage(const std::string &data);
177 scene::IMeshSceneNode *m_node;
182 TestCAO proto_TestCAO(NULL, NULL);
184 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
185 ClientActiveObject(0, gamedef, env),
187 m_position(v3f(0,10*BS,0))
189 ClientActiveObject::registerType(getType(), create);
196 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
198 return new TestCAO(gamedef, env);
201 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
207 //video::IVideoDriver* driver = smgr->getVideoDriver();
209 scene::SMesh *mesh = new scene::SMesh();
210 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
211 video::SColor c(255,255,255,255);
212 video::S3DVertex vertices[4] =
214 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
215 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
216 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
217 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
219 u16 indices[] = {0,1,2,2,3,0};
220 buf->append(vertices, 4, indices, 6);
222 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
223 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
224 buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
225 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
226 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
227 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
229 mesh->addMeshBuffer(buf);
231 m_node = smgr->addMeshSceneNode(mesh, NULL);
236 void TestCAO::removeFromScene()
245 void TestCAO::updateLight(u8 light_at_pos)
249 v3s16 TestCAO::getLightPosition()
251 return floatToInt(m_position, BS);
254 void TestCAO::updateNodePos()
259 m_node->setPosition(m_position);
260 //m_node->setRotation(v3f(0, 45, 0));
263 void TestCAO::step(float dtime, ClientEnvironment *env)
267 v3f rot = m_node->getRotation();
268 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
269 rot.Y += dtime * 180;
270 m_node->setRotation(rot);
274 void TestCAO::processMessage(const std::string &data)
276 infostream<<"TestCAO: Got data: "<<data<<std::endl;
277 std::istringstream is(data, std::ios::binary);
295 class ItemCAO : public ClientActiveObject
298 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
303 return ACTIVEOBJECT_TYPE_ITEM;
306 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
308 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
309 IrrlichtDevice *irr);
310 void removeFromScene();
311 void updateLight(u8 light_at_pos);
312 v3s16 getLightPosition();
313 void updateNodePos();
314 void updateInfoText();
315 void updateTexture();
317 void step(float dtime, ClientEnvironment *env);
319 void processMessage(const std::string &data);
321 void initialize(const std::string &data);
323 core::aabbox3d<f32>* getSelectionBox()
324 {return &m_selection_box;}
328 std::string infoText()
332 core::aabbox3d<f32> m_selection_box;
333 scene::IMeshSceneNode *m_node;
335 std::string m_itemstring;
336 std::string m_infotext;
339 #include "inventory.h"
342 ItemCAO proto_ItemCAO(NULL, NULL);
344 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
345 ClientActiveObject(0, gamedef, env),
346 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
348 m_position(v3f(0,10*BS,0))
352 ClientActiveObject::registerType(getType(), create);
360 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
362 return new ItemCAO(gamedef, env);
365 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
371 //video::IVideoDriver* driver = smgr->getVideoDriver();
373 scene::SMesh *mesh = new scene::SMesh();
374 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
375 video::SColor c(255,255,255,255);
376 video::S3DVertex vertices[4] =
378 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
379 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
380 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
381 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
382 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
383 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
384 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
385 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
387 u16 indices[] = {0,1,2,2,3,0};
388 buf->append(vertices, 4, indices, 6);
390 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
391 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
392 // Initialize with a generated placeholder texture
393 buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
394 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
395 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
396 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
398 mesh->addMeshBuffer(buf);
400 m_node = smgr->addMeshSceneNode(mesh, NULL);
411 void ItemCAO::removeFromScene()
420 void ItemCAO::updateLight(u8 light_at_pos)
425 u8 li = decode_light(light_at_pos);
426 video::SColor color(255,li,li,li);
427 setMeshColor(m_node->getMesh(), color);
430 v3s16 ItemCAO::getLightPosition()
432 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
435 void ItemCAO::updateNodePos()
440 m_node->setPosition(m_position);
443 void ItemCAO::updateInfoText()
446 IItemDefManager *idef = m_gamedef->idef();
448 item.deSerialize(m_itemstring, idef);
449 if(item.isKnown(idef))
450 m_infotext = item.getDefinition(idef).description;
452 m_infotext = "Unknown item: '" + m_itemstring + "'";
454 m_infotext += " (" + itos(item.count) + ")";
456 catch(SerializationError &e)
458 m_infotext = "Unknown item: '" + m_itemstring + "'";
462 void ItemCAO::updateTexture()
467 // Create an inventory item to see what is its image
468 std::istringstream is(m_itemstring, std::ios_base::binary);
469 video::ITexture *texture = NULL;
471 IItemDefManager *idef = m_gamedef->idef();
473 item.deSerialize(is, idef);
474 texture = item.getDefinition(idef).inventory_texture;
476 catch(SerializationError &e)
478 infostream<<"WARNING: "<<__FUNCTION_NAME
479 <<": error deSerializing itemstring \""
480 <<m_itemstring<<std::endl;
483 // Set meshbuffer texture
484 m_node->getMaterial(0).setTexture(0, texture);
488 void ItemCAO::step(float dtime, ClientEnvironment *env)
492 /*v3f rot = m_node->getRotation();
493 rot.Y += dtime * 120;
494 m_node->setRotation(rot);*/
495 LocalPlayer *player = env->getLocalPlayer();
497 v3f rot = m_node->getRotation();
498 rot.Y = 180.0 - (player->getYaw());
499 m_node->setRotation(rot);
503 void ItemCAO::processMessage(const std::string &data)
505 //infostream<<"ItemCAO: Got message"<<std::endl;
506 std::istringstream is(data, std::ios::binary);
512 m_position = readV3F1000(is);
518 m_itemstring = deSerializeString(is);
524 void ItemCAO::initialize(const std::string &data)
526 infostream<<"ItemCAO: Got init data"<<std::endl;
529 std::istringstream is(data, std::ios::binary);
531 u8 version = readU8(is);
536 m_position = readV3F1000(is);
538 m_itemstring = deSerializeString(is);
549 #include "genericobject.h"
551 class GenericCAO : public ClientActiveObject
554 // Only set at initialization
557 bool m_is_local_player; // determined locally
558 // Property-ish things
559 ObjectProperties m_prop;
561 scene::ISceneManager *m_smgr;
562 IrrlichtDevice *m_irr;
563 core::aabbox3d<f32> m_selection_box;
564 scene::IMeshSceneNode *m_meshnode;
565 scene::IAnimatedMeshSceneNode *m_animated_meshnode;
566 scene::IBillboardSceneNode *m_spritenode;
567 scene::ITextSceneNode* m_textnode;
573 SmoothTranslator pos_translator;
574 // Spritesheet/animation stuff
577 bool m_initial_tx_basepos_set;
578 bool m_tx_select_horiz_by_yawpitch;
582 std::map<std::string, core::vector2d<v3f> > m_bone_posrot;
584 int m_anim_num_frames;
585 float m_anim_framelength;
587 ItemGroupList m_armor_groups;
588 float m_reset_textures_timer;
589 bool m_visuals_expired;
590 float m_step_distance_counter;
594 GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
595 ClientActiveObject(0, gamedef, env),
598 m_is_local_player(false),
602 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
604 m_animated_meshnode(NULL),
607 m_position(v3f(0,10*BS,0)),
608 m_velocity(v3f(0,0,0)),
609 m_acceleration(v3f(0,0,0)),
614 m_initial_tx_basepos_set(false),
615 m_tx_select_horiz_by_yawpitch(false),
617 m_anim_num_frames(1),
618 m_anim_framelength(0.2),
620 m_reset_textures_timer(-1),
621 m_visuals_expired(false),
622 m_step_distance_counter(0),
626 ClientActiveObject::registerType(getType(), create);
629 void initialize(const std::string &data)
631 infostream<<"GenericCAO: Got init data"<<std::endl;
632 std::istringstream is(data, std::ios::binary);
634 u8 version = readU8(is);
637 errorstream<<"GenericCAO: Unsupported init data version"
641 m_name = deSerializeString(is);
642 m_is_player = readU8(is);
643 m_position = readV3F1000(is);
644 m_yaw = readF1000(is);
647 int num_messages = readU8(is);
648 for(int i=0; i<num_messages; i++){
649 std::string message = deSerializeLongString(is);
650 processMessage(message);
653 pos_translator.init(m_position);
657 Player *player = m_env->getPlayer(m_name.c_str());
658 if(player && player->isLocal()){
659 m_is_local_player = true;
668 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
670 return new GenericCAO(gamedef, env);
675 return ACTIVEOBJECT_TYPE_GENERIC;
677 core::aabbox3d<f32>* getSelectionBox()
679 if(!m_prop.is_visible || m_is_local_player)
681 return &m_selection_box;
685 return pos_translator.vect_show;
688 void removeFromScene()
691 m_meshnode->remove();
694 if(m_animated_meshnode){
695 m_animated_meshnode->remove();
696 m_animated_meshnode = NULL;
699 m_spritenode->remove();
704 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
710 if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
713 m_visuals_expired = false;
715 if(!m_prop.is_visible || m_is_local_player)
718 //video::IVideoDriver* driver = smgr->getVideoDriver();
720 if(m_prop.visual == "sprite"){
721 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
722 m_spritenode = smgr->addBillboardSceneNode(
723 NULL, v2f(1, 1), v3f(0,0,0), -1);
724 m_spritenode->setMaterialTexture(0,
725 tsrc->getTextureRaw("unknown_block.png"));
726 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
727 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
728 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
729 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
730 u8 li = m_last_light;
731 m_spritenode->setColor(video::SColor(255,li,li,li));
732 m_spritenode->setSize(m_prop.visual_size*BS);
734 const float txs = 1.0 / 1;
735 const float tys = 1.0 / 1;
736 setBillboardTextureMatrix(m_spritenode,
740 else if(m_prop.visual == "upright_sprite")
742 scene::SMesh *mesh = new scene::SMesh();
743 double dx = BS*m_prop.visual_size.X/2;
744 double dy = BS*m_prop.visual_size.Y/2;
746 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
747 u8 li = m_last_light;
748 video::SColor c(255,li,li,li);
749 video::S3DVertex vertices[4] =
751 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
752 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
753 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
754 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
756 u16 indices[] = {0,1,2,2,3,0};
757 buf->append(vertices, 4, indices, 6);
759 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
760 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
761 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
762 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
764 mesh->addMeshBuffer(buf);
768 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
769 u8 li = m_last_light;
770 video::SColor c(255,li,li,li);
771 video::S3DVertex vertices[4] =
773 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
774 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
775 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
776 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
778 u16 indices[] = {0,1,2,2,3,0};
779 buf->append(vertices, 4, indices, 6);
781 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
782 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
783 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
784 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
786 mesh->addMeshBuffer(buf);
789 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
791 // Set it to use the materials of the meshbuffers directly.
792 // This is needed for changing the texture in the future
793 m_meshnode->setReadOnlyMaterials(true);
795 else if(m_prop.visual == "cube"){
796 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
797 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
798 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
801 m_meshnode->setScale(v3f(m_prop.visual_size.X,
802 m_prop.visual_size.Y,
803 m_prop.visual_size.X));
804 u8 li = m_last_light;
805 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
807 else if(m_prop.visual == "mesh"){
808 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
809 scene::IAnimatedMesh *mesh = smgr->getMesh(m_prop.mesh.c_str());
812 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
813 m_animated_meshnode->setMD2Animation(scene::EMAT_STAND);
814 m_animated_meshnode->animateJoints(); // Needed for some animations
815 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
816 m_prop.visual_size.Y,
817 m_prop.visual_size.X));
818 u8 li = m_last_light;
819 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
822 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
824 else if(m_prop.visual == "wielditem"){
825 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
826 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
827 if(m_prop.textures.size() >= 1){
828 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
829 IItemDefManager *idef = m_gamedef->idef();
830 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
831 scene::IMesh *item_mesh = item.getDefinition(idef).wield_mesh;
833 // Copy mesh to be able to set unique vertex colors
834 scene::IMeshManipulator *manip =
835 irr->getVideoDriver()->getMeshManipulator();
836 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
838 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
841 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
842 m_prop.visual_size.Y/2,
843 m_prop.visual_size.X/2));
844 u8 li = m_last_light;
845 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
848 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
849 <<"\" not supported"<<std::endl;
853 scene::ISceneNode *node = NULL;
856 else if(m_animated_meshnode)
857 node = m_animated_meshnode;
860 if(node && m_is_player && !m_is_local_player){
861 // Add a text node for showing the name
862 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
863 std::wstring wname = narrow_to_wide(m_name);
864 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
865 wname.c_str(), video::SColor(255,255,255,255), node);
866 m_textnode->setPosition(v3f(0, BS*1.1, 0));
874 m_visuals_expired = true;
877 void updateLight(u8 light_at_pos)
879 bool is_visible = (m_hp != 0);
880 u8 li = decode_light(light_at_pos);
881 if(li != m_last_light){
883 video::SColor color(255,li,li,li);
885 setMeshColor(m_meshnode->getMesh(), color);
886 m_meshnode->setVisible(is_visible);
888 if(m_animated_meshnode){
889 setMeshColor(m_animated_meshnode->getMesh(), color);
890 m_animated_meshnode->setVisible(is_visible);
893 m_spritenode->setColor(color);
894 m_spritenode->setVisible(is_visible);
899 v3s16 getLightPosition()
901 return floatToInt(m_position, BS);
907 m_meshnode->setPosition(pos_translator.vect_show);
908 v3f rot = m_meshnode->getRotation();
910 m_meshnode->setRotation(rot);
912 if(m_animated_meshnode){
913 m_animated_meshnode->setPosition(pos_translator.vect_show);
914 v3f rot = m_animated_meshnode->getRotation();
916 m_animated_meshnode->setRotation(rot);
919 m_spritenode->setPosition(pos_translator.vect_show);
923 void step(float dtime, ClientEnvironment *env)
925 v3f lastpos = pos_translator.vect_show;
927 if(m_visuals_expired && m_smgr && m_irr){
928 m_visuals_expired = false;
930 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
936 core::aabbox3d<f32> box = m_prop.collisionbox;
939 collisionMoveResult moveresult;
940 f32 pos_max_d = BS*0.125; // Distance per iteration
942 v3f p_pos = m_position;
943 v3f p_velocity = m_velocity;
944 v3f p_acceleration = m_acceleration;
945 IGameDef *gamedef = env->getGameDef();
946 moveresult = collisionMoveSimple(&env->getMap(), gamedef,
947 pos_max_d, box, stepheight, dtime,
948 p_pos, p_velocity, p_acceleration);
951 m_velocity = p_velocity;
952 m_acceleration = p_acceleration;
954 bool is_end_position = moveresult.collides;
955 pos_translator.update(m_position, is_end_position, dtime);
956 pos_translator.translate(dtime);
959 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
960 m_velocity += dtime * m_acceleration;
961 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
962 pos_translator.translate(dtime);
966 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
967 m_step_distance_counter += moved;
968 if(m_step_distance_counter > 1.5*BS){
969 m_step_distance_counter = 0;
970 if(!m_is_local_player && m_prop.makes_footstep_sound){
971 INodeDefManager *ndef = m_gamedef->ndef();
972 v3s16 p = floatToInt(getPosition() + v3f(0,
973 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
974 MapNode n = m_env->getMap().getNodeNoEx(p);
975 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
976 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
980 m_anim_timer += dtime;
981 if(m_anim_timer >= m_anim_framelength){
982 m_anim_timer -= m_anim_framelength;
984 if(m_anim_frame >= m_anim_num_frames)
990 if(m_reset_textures_timer >= 0){
991 m_reset_textures_timer -= dtime;
992 if(m_reset_textures_timer <= 0){
993 m_reset_textures_timer = -1;
997 if(fabs(m_prop.automatic_rotate) > 0.001){
998 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1003 void updateTexturePos()
1006 scene::ICameraSceneNode* camera =
1007 m_spritenode->getSceneManager()->getActiveCamera();
1010 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1011 - camera->getAbsolutePosition();
1012 cam_to_entity.normalize();
1014 int row = m_tx_basepos.Y;
1015 int col = m_tx_basepos.X;
1017 if(m_tx_select_horiz_by_yawpitch)
1019 if(cam_to_entity.Y > 0.75)
1021 else if(cam_to_entity.Y < -0.75)
1024 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1025 float dir = mob_dir - m_yaw;
1026 dir = wrapDegrees_180(dir);
1027 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1028 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1030 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1032 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1034 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1041 // Animation goes downwards
1042 row += m_anim_frame;
1044 float txs = m_tx_size.X;
1045 float tys = m_tx_size.Y;
1046 setBillboardTextureMatrix(m_spritenode,
1047 txs, tys, col, row);
1051 void updateTextures(const std::string &mod)
1053 ITextureSource *tsrc = m_gamedef->tsrc();
1057 if(m_prop.visual == "sprite")
1059 std::string texturestring = "unknown_block.png";
1060 if(m_prop.textures.size() >= 1)
1061 texturestring = m_prop.textures[0];
1062 texturestring += mod;
1063 m_spritenode->setMaterialTexture(0,
1064 tsrc->getTextureRaw(texturestring));
1067 if(m_animated_meshnode)
1069 if(m_prop.visual == "mesh")
1071 for (u32 i = 0; i < m_prop.textures.size(); ++i)
1073 std::string texturestring = m_prop.textures[i];
1074 if(texturestring == "")
1075 continue; // Empty texture string means don't modify that material
1076 texturestring += mod;
1077 video::ITexture* texture = tsrc->getTextureRaw(texturestring);
1080 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1084 // Set material flags and texture
1085 m_animated_meshnode->setMaterialTexture(i, texture);
1086 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1087 material.setFlag(video::EMF_LIGHTING, false);
1088 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1094 if(m_prop.visual == "cube")
1096 for (u32 i = 0; i < 6; ++i)
1098 std::string texturestring = "unknown_block.png";
1099 if(m_prop.textures.size() > i)
1100 texturestring = m_prop.textures[i];
1101 texturestring += mod;
1102 AtlasPointer ap = tsrc->getTexture(texturestring);
1104 // Get the tile texture and atlas transformation
1105 video::ITexture* atlas = ap.atlas;
1109 // Set material flags and texture
1110 video::SMaterial& material = m_meshnode->getMaterial(i);
1111 material.setFlag(video::EMF_LIGHTING, false);
1112 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1113 material.setTexture(0, atlas);
1114 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1115 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1118 else if(m_prop.visual == "upright_sprite")
1120 scene::IMesh *mesh = m_meshnode->getMesh();
1122 std::string tname = "unknown_object.png";
1123 if(m_prop.textures.size() >= 1)
1124 tname = m_prop.textures[0];
1126 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1127 buf->getMaterial().setTexture(0,
1128 tsrc->getTextureRaw(tname));
1131 std::string tname = "unknown_object.png";
1132 if(m_prop.textures.size() >= 2)
1133 tname = m_prop.textures[1];
1134 else if(m_prop.textures.size() >= 1)
1135 tname = m_prop.textures[0];
1137 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1138 buf->getMaterial().setTexture(0,
1139 tsrc->getTextureRaw(tname));
1145 void updateAnimations()
1147 if(!m_animated_meshnode)
1150 m_animated_meshnode->setFrameLoop((int)m_frames.X, (int)m_frames.Y);
1151 m_animated_meshnode->setAnimationSpeed(m_frame_speed);
1152 m_animated_meshnode->setTransitionTime(m_frame_blend);
1155 void updateBonePosRot()
1157 if(m_bone_posrot.size() > 0)
1159 m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1160 for(std::map<std::string, core::vector2d<v3f> >::const_iterator ii = m_bone_posrot.begin(); ii != m_bone_posrot.end(); ++ii){
1161 std::string bone_name = (*ii).first;
1162 v3f bone_pos = (*ii).second.X;
1163 v3f bone_rot = (*ii).second.Y;
1164 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1165 bone->setPosition(bone_pos);
1166 bone->setRotation(bone_rot);
1171 void processMessage(const std::string &data)
1173 //infostream<<"GenericCAO: Got message"<<std::endl;
1174 std::istringstream is(data, std::ios::binary);
1176 u8 cmd = readU8(is);
1177 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1179 m_prop = gob_read_set_properties(is);
1181 m_selection_box = m_prop.collisionbox;
1182 m_selection_box.MinEdge *= BS;
1183 m_selection_box.MaxEdge *= BS;
1185 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1186 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1188 if(!m_initial_tx_basepos_set){
1189 m_initial_tx_basepos_set = true;
1190 m_tx_basepos = m_prop.initial_sprite_basepos;
1195 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1197 m_position = readV3F1000(is);
1198 m_velocity = readV3F1000(is);
1199 m_acceleration = readV3F1000(is);
1200 if(fabs(m_prop.automatic_rotate) < 0.001)
1201 m_yaw = readF1000(is);
1202 bool do_interpolate = readU8(is);
1203 bool is_end_position = readU8(is);
1204 float update_interval = readF1000(is);
1206 // Place us a bit higher if we're physical, to not sink into
1207 // the ground due to sucky collision detection...
1209 m_position += v3f(0,0.002,0);
1212 if(!m_prop.physical)
1213 pos_translator.update(m_position, is_end_position, update_interval);
1215 pos_translator.init(m_position);
1219 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1221 std::string mod = deSerializeString(is);
1222 updateTextures(mod);
1224 else if(cmd == GENERIC_CMD_SET_SPRITE)
1226 v2s16 p = readV2S16(is);
1227 int num_frames = readU16(is);
1228 float framelength = readF1000(is);
1229 bool select_horiz_by_yawpitch = readU8(is);
1232 m_anim_num_frames = num_frames;
1233 m_anim_framelength = framelength;
1234 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1238 else if(cmd == GENERIC_CMD_SET_ANIMATIONS)
1240 m_frames = readV2F1000(is);
1241 m_frame_speed = readF1000(is);
1242 m_frame_blend = readF1000(is);
1247 else if(cmd == GENERIC_CMD_SET_BONE_POSROT)
1249 std::string bone = deSerializeString(is);
1250 v3f position = readV3F1000(is);
1251 v3f rotation = readV3F1000(is);
1252 m_bone_posrot[bone] = core::vector2d<v3f>(position, rotation);
1257 else if(cmd == GENERIC_CMD_PUNCHED)
1259 /*s16 damage =*/ readS16(is);
1260 s16 result_hp = readS16(is);
1264 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1266 m_armor_groups.clear();
1267 int armor_groups_size = readU16(is);
1268 for(int i=0; i<armor_groups_size; i++){
1269 std::string name = deSerializeString(is);
1270 int rating = readS16(is);
1271 m_armor_groups[name] = rating;
1276 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1277 float time_from_last_punch=1000000)
1280 const ToolCapabilities *toolcap =
1281 &punchitem->getToolCapabilities(m_gamedef->idef());
1282 PunchDamageResult result = getPunchDamage(
1286 time_from_last_punch);
1288 if(result.did_punch && result.damage != 0)
1290 if(result.damage < m_hp){
1291 m_hp -= result.damage;
1294 // TODO: Execute defined fast response
1295 // As there is no definition, make a smoke puff
1296 ClientSimpleObject *simple = createSmokePuff(
1297 m_smgr, m_env, m_position,
1298 m_prop.visual_size * BS);
1299 m_env->addSimpleObject(simple);
1301 // TODO: Execute defined fast response
1302 // Flashing shall suffice as there is no definition
1303 m_reset_textures_timer = 0.05;
1304 if(result.damage >= 2)
1305 m_reset_textures_timer += 0.05 * result.damage;
1306 updateTextures("^[brighten");
1312 std::string debugInfoText()
1314 std::ostringstream os(std::ios::binary);
1315 os<<"GenericCAO hp="<<m_hp<<"\n";
1317 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1318 i != m_armor_groups.end(); i++){
1319 os<<i->first<<"="<<i->second<<", ";
1327 GenericCAO proto_GenericCAO(NULL, NULL);