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>
46 struct ToolCapabilities;
48 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
50 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
56 struct SmoothTranslator
63 f32 anim_time_counter;
83 anim_time_counter = 0;
92 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
94 aim_is_end = is_end_position;
97 if(update_interval > 0){
98 anim_time = update_interval;
100 if(anim_time < 0.001 || anim_time > 1.0)
101 anim_time = anim_time_counter;
103 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
105 anim_time_counter = 0;
109 void translate(f32 dtime)
111 anim_time_counter = anim_time_counter + dtime;
112 anim_counter = anim_counter + dtime;
113 v3f vect_move = vect_aim - vect_old;
115 if(anim_time > 0.001)
116 moveratio = anim_time_counter / anim_time;
117 // Move a bit less than should, to avoid oscillation
118 moveratio = moveratio * 0.8;
119 float move_end = 1.5;
122 if(moveratio > move_end)
123 moveratio = move_end;
124 vect_show = vect_old + vect_move * moveratio;
129 return ((anim_time_counter / anim_time) < 1.4);
137 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
138 float txs, float tys, int col, int row)
140 video::SMaterial& material = bill->getMaterial(0);
141 core::matrix4& matrix = material.getTextureMatrix(0);
142 matrix.setTextureTranslate(txs*col, tys*row);
143 matrix.setTextureScale(txs, tys);
150 class TestCAO : public ClientActiveObject
153 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
158 return ACTIVEOBJECT_TYPE_TEST;
161 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
163 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
164 IrrlichtDevice *irr);
165 void removeFromScene();
166 void updateLight(u8 light_at_pos);
167 v3s16 getLightPosition();
168 void updateNodePos();
170 void step(float dtime, ClientEnvironment *env);
172 void processMessage(const std::string &data);
175 scene::IMeshSceneNode *m_node;
180 TestCAO proto_TestCAO(NULL, NULL);
182 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
183 ClientActiveObject(0, gamedef, env),
185 m_position(v3f(0,10*BS,0))
187 ClientActiveObject::registerType(getType(), create);
194 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
196 return new TestCAO(gamedef, env);
199 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
205 //video::IVideoDriver* driver = smgr->getVideoDriver();
207 scene::SMesh *mesh = new scene::SMesh();
208 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
209 video::SColor c(255,255,255,255);
210 video::S3DVertex vertices[4] =
212 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
213 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
214 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
215 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
217 u16 indices[] = {0,1,2,2,3,0};
218 buf->append(vertices, 4, indices, 6);
220 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
221 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
222 buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
223 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
224 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
225 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
227 mesh->addMeshBuffer(buf);
229 m_node = smgr->addMeshSceneNode(mesh, NULL);
234 void TestCAO::removeFromScene()
243 void TestCAO::updateLight(u8 light_at_pos)
247 v3s16 TestCAO::getLightPosition()
249 return floatToInt(m_position, BS);
252 void TestCAO::updateNodePos()
257 m_node->setPosition(m_position);
258 //m_node->setRotation(v3f(0, 45, 0));
261 void TestCAO::step(float dtime, ClientEnvironment *env)
265 v3f rot = m_node->getRotation();
266 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
267 rot.Y += dtime * 180;
268 m_node->setRotation(rot);
272 void TestCAO::processMessage(const std::string &data)
274 infostream<<"TestCAO: Got data: "<<data<<std::endl;
275 std::istringstream is(data, std::ios::binary);
293 class ItemCAO : public ClientActiveObject
296 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
301 return ACTIVEOBJECT_TYPE_ITEM;
304 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
306 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
307 IrrlichtDevice *irr);
308 void removeFromScene();
309 void updateLight(u8 light_at_pos);
310 v3s16 getLightPosition();
311 void updateNodePos();
312 void updateInfoText();
313 void updateTexture();
315 void step(float dtime, ClientEnvironment *env);
317 void processMessage(const std::string &data);
319 void initialize(const std::string &data);
321 core::aabbox3d<f32>* getSelectionBox()
322 {return &m_selection_box;}
326 std::string infoText()
330 core::aabbox3d<f32> m_selection_box;
331 scene::IMeshSceneNode *m_node;
333 std::string m_itemstring;
334 std::string m_infotext;
337 #include "inventory.h"
340 ItemCAO proto_ItemCAO(NULL, NULL);
342 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
343 ClientActiveObject(0, gamedef, env),
344 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
346 m_position(v3f(0,10*BS,0))
350 ClientActiveObject::registerType(getType(), create);
358 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
360 return new ItemCAO(gamedef, env);
363 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
369 //video::IVideoDriver* driver = smgr->getVideoDriver();
371 scene::SMesh *mesh = new scene::SMesh();
372 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
373 video::SColor c(255,255,255,255);
374 video::S3DVertex vertices[4] =
376 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
377 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
378 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
379 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
380 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
381 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
382 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
383 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
385 u16 indices[] = {0,1,2,2,3,0};
386 buf->append(vertices, 4, indices, 6);
388 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
389 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
390 // Initialize with a generated placeholder texture
391 buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
392 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
393 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
394 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
396 mesh->addMeshBuffer(buf);
398 m_node = smgr->addMeshSceneNode(mesh, NULL);
409 void ItemCAO::removeFromScene()
418 void ItemCAO::updateLight(u8 light_at_pos)
423 u8 li = decode_light(light_at_pos);
424 video::SColor color(255,li,li,li);
425 setMeshColor(m_node->getMesh(), color);
428 v3s16 ItemCAO::getLightPosition()
430 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
433 void ItemCAO::updateNodePos()
438 m_node->setPosition(m_position);
441 void ItemCAO::updateInfoText()
444 IItemDefManager *idef = m_gamedef->idef();
446 item.deSerialize(m_itemstring, idef);
447 if(item.isKnown(idef))
448 m_infotext = item.getDefinition(idef).description;
450 m_infotext = "Unknown item: '" + m_itemstring + "'";
452 m_infotext += " (" + itos(item.count) + ")";
454 catch(SerializationError &e)
456 m_infotext = "Unknown item: '" + m_itemstring + "'";
460 void ItemCAO::updateTexture()
465 // Create an inventory item to see what is its image
466 std::istringstream is(m_itemstring, std::ios_base::binary);
467 video::ITexture *texture = NULL;
469 IItemDefManager *idef = m_gamedef->idef();
471 item.deSerialize(is, idef);
472 texture = item.getDefinition(idef).inventory_texture;
474 catch(SerializationError &e)
476 infostream<<"WARNING: "<<__FUNCTION_NAME
477 <<": error deSerializing itemstring \""
478 <<m_itemstring<<std::endl;
481 // Set meshbuffer texture
482 m_node->getMaterial(0).setTexture(0, texture);
486 void ItemCAO::step(float dtime, ClientEnvironment *env)
490 /*v3f rot = m_node->getRotation();
491 rot.Y += dtime * 120;
492 m_node->setRotation(rot);*/
493 LocalPlayer *player = env->getLocalPlayer();
495 v3f rot = m_node->getRotation();
496 rot.Y = 180.0 - (player->getYaw());
497 m_node->setRotation(rot);
501 void ItemCAO::processMessage(const std::string &data)
503 //infostream<<"ItemCAO: Got message"<<std::endl;
504 std::istringstream is(data, std::ios::binary);
510 m_position = readV3F1000(is);
516 m_itemstring = deSerializeString(is);
522 void ItemCAO::initialize(const std::string &data)
524 infostream<<"ItemCAO: Got init data"<<std::endl;
527 std::istringstream is(data, std::ios::binary);
529 u8 version = readU8(is);
534 m_position = readV3F1000(is);
536 m_itemstring = deSerializeString(is);
547 #include "genericobject.h"
549 class GenericCAO : public ClientActiveObject
552 // Only set at initialization
555 bool m_is_local_player; // determined locally
556 // Property-ish things
557 ObjectProperties m_prop;
559 scene::ISceneManager *m_smgr;
560 IrrlichtDevice *m_irr;
561 core::aabbox3d<f32> m_selection_box;
562 scene::IMeshSceneNode *m_meshnode;
563 scene::IBillboardSceneNode *m_spritenode;
564 scene::ITextSceneNode* m_textnode;
570 SmoothTranslator pos_translator;
571 // Spritesheet/animation stuff
574 bool m_initial_tx_basepos_set;
575 bool m_tx_select_horiz_by_yawpitch;
577 int m_anim_num_frames;
578 float m_anim_framelength;
580 ItemGroupList m_armor_groups;
581 float m_reset_textures_timer;
582 bool m_visuals_expired;
583 float m_step_distance_counter;
587 GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
588 ClientActiveObject(0, gamedef, env),
591 m_is_local_player(false),
595 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
599 m_position(v3f(0,10*BS,0)),
600 m_velocity(v3f(0,0,0)),
601 m_acceleration(v3f(0,0,0)),
606 m_initial_tx_basepos_set(false),
607 m_tx_select_horiz_by_yawpitch(false),
609 m_anim_num_frames(1),
610 m_anim_framelength(0.2),
612 m_reset_textures_timer(-1),
613 m_visuals_expired(false),
614 m_step_distance_counter(0),
618 ClientActiveObject::registerType(getType(), create);
621 void initialize(const std::string &data)
623 infostream<<"GenericCAO: Got init data"<<std::endl;
624 std::istringstream is(data, std::ios::binary);
626 u8 version = readU8(is);
629 errorstream<<"GenericCAO: Unsupported init data version"
633 m_name = deSerializeString(is);
634 m_is_player = readU8(is);
635 m_position = readV3F1000(is);
636 m_yaw = readF1000(is);
639 int num_messages = readU8(is);
640 for(int i=0; i<num_messages; i++){
641 std::string message = deSerializeLongString(is);
642 processMessage(message);
645 pos_translator.init(m_position);
649 Player *player = m_env->getPlayer(m_name.c_str());
650 if(player && player->isLocal()){
651 m_is_local_player = true;
660 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
662 return new GenericCAO(gamedef, env);
667 return ACTIVEOBJECT_TYPE_GENERIC;
669 core::aabbox3d<f32>* getSelectionBox()
671 if(!m_prop.is_visible || m_is_local_player)
673 return &m_selection_box;
677 return pos_translator.vect_show;
680 void removeFromScene()
683 m_meshnode->remove();
687 m_spritenode->remove();
692 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
698 if(m_meshnode != NULL || m_spritenode != NULL)
701 m_visuals_expired = false;
703 if(!m_prop.is_visible || m_is_local_player)
706 //video::IVideoDriver* driver = smgr->getVideoDriver();
708 if(m_prop.visual == "sprite"){
709 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
710 m_spritenode = smgr->addBillboardSceneNode(
711 NULL, v2f(1, 1), v3f(0,0,0), -1);
712 m_spritenode->setMaterialTexture(0,
713 tsrc->getTextureRaw("unknown_block.png"));
714 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
715 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
716 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
717 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
718 u8 li = m_last_light;
719 m_spritenode->setColor(video::SColor(255,li,li,li));
720 m_spritenode->setSize(m_prop.visual_size*BS);
722 const float txs = 1.0 / 1;
723 const float tys = 1.0 / 1;
724 setBillboardTextureMatrix(m_spritenode,
728 else if(m_prop.visual == "upright_sprite")
730 scene::SMesh *mesh = new scene::SMesh();
731 double dx = BS*m_prop.visual_size.X/2;
732 double dy = BS*m_prop.visual_size.Y/2;
734 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
735 u8 li = m_last_light;
736 video::SColor c(255,li,li,li);
737 video::S3DVertex vertices[4] =
739 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
740 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
741 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
742 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
744 u16 indices[] = {0,1,2,2,3,0};
745 buf->append(vertices, 4, indices, 6);
747 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
748 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
749 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
750 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
752 mesh->addMeshBuffer(buf);
756 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
757 u8 li = m_last_light;
758 video::SColor c(255,li,li,li);
759 video::S3DVertex vertices[4] =
761 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
762 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
763 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
764 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
766 u16 indices[] = {0,1,2,2,3,0};
767 buf->append(vertices, 4, indices, 6);
769 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
770 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
771 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
772 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
774 mesh->addMeshBuffer(buf);
777 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
779 // Set it to use the materials of the meshbuffers directly.
780 // This is needed for changing the texture in the future
781 m_meshnode->setReadOnlyMaterials(true);
783 else if(m_prop.visual == "cube"){
784 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
785 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
786 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
789 m_meshnode->setScale(v3f(m_prop.visual_size.X,
790 m_prop.visual_size.Y,
791 m_prop.visual_size.X));
792 u8 li = m_last_light;
793 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
794 } else if(m_prop.visual == "wielditem"){
795 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
796 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
797 if(m_prop.textures.size() >= 1){
798 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
799 IItemDefManager *idef = m_gamedef->idef();
800 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
801 scene::IMesh *item_mesh = item.getDefinition(idef).wield_mesh;
803 // Copy mesh to be able to set unique vertex colors
804 scene::IMeshManipulator *manip =
805 irr->getVideoDriver()->getMeshManipulator();
806 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
808 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
811 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
812 m_prop.visual_size.Y/2,
813 m_prop.visual_size.X/2));
814 u8 li = m_last_light;
815 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
818 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
819 <<"\" not supported"<<std::endl;
823 scene::ISceneNode *node = NULL;
828 if(node && m_is_player && !m_is_local_player){
829 // Add a text node for showing the name
830 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
831 std::wstring wname = narrow_to_wide(m_name);
832 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
833 wname.c_str(), video::SColor(255,255,255,255), node);
834 m_textnode->setPosition(v3f(0, BS*1.1, 0));
842 m_visuals_expired = true;
845 void updateLight(u8 light_at_pos)
847 bool is_visible = (m_hp != 0);
848 u8 li = decode_light(light_at_pos);
849 if(li != m_last_light){
851 video::SColor color(255,li,li,li);
853 setMeshColor(m_meshnode->getMesh(), color);
854 m_meshnode->setVisible(is_visible);
857 m_spritenode->setColor(color);
858 m_spritenode->setVisible(is_visible);
863 v3s16 getLightPosition()
865 return floatToInt(m_position, BS);
871 m_meshnode->setPosition(pos_translator.vect_show);
872 v3f rot = m_meshnode->getRotation();
874 m_meshnode->setRotation(rot);
877 m_spritenode->setPosition(pos_translator.vect_show);
881 void step(float dtime, ClientEnvironment *env)
883 v3f lastpos = pos_translator.vect_show;
885 if(m_visuals_expired && m_smgr && m_irr){
886 m_visuals_expired = false;
888 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
892 core::aabbox3d<f32> box = m_prop.collisionbox;
895 collisionMoveResult moveresult;
896 f32 pos_max_d = BS*0.125; // Distance per iteration
898 v3f p_pos = m_position;
899 v3f p_velocity = m_velocity;
900 v3f p_acceleration = m_acceleration;
901 IGameDef *gamedef = env->getGameDef();
902 moveresult = collisionMoveSimple(&env->getMap(), gamedef,
903 pos_max_d, box, stepheight, dtime,
904 p_pos, p_velocity, p_acceleration);
907 m_velocity = p_velocity;
908 m_acceleration = p_acceleration;
910 bool is_end_position = moveresult.collides;
911 pos_translator.update(m_position, is_end_position, dtime);
912 pos_translator.translate(dtime);
915 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
916 m_velocity += dtime * m_acceleration;
917 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
918 pos_translator.translate(dtime);
922 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
923 m_step_distance_counter += moved;
924 if(m_step_distance_counter > 1.5*BS){
925 m_step_distance_counter = 0;
926 if(!m_is_local_player && m_prop.makes_footstep_sound){
927 INodeDefManager *ndef = m_gamedef->ndef();
928 v3s16 p = floatToInt(getPosition() + v3f(0,
929 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
930 MapNode n = m_env->getMap().getNodeNoEx(p);
931 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
932 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
936 m_anim_timer += dtime;
937 if(m_anim_timer >= m_anim_framelength){
938 m_anim_timer -= m_anim_framelength;
940 if(m_anim_frame >= m_anim_num_frames)
946 if(m_reset_textures_timer >= 0){
947 m_reset_textures_timer -= dtime;
948 if(m_reset_textures_timer <= 0){
949 m_reset_textures_timer = -1;
953 if(fabs(m_prop.automatic_rotate) > 0.001){
954 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
959 void updateTexturePos()
962 scene::ICameraSceneNode* camera =
963 m_spritenode->getSceneManager()->getActiveCamera();
966 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
967 - camera->getAbsolutePosition();
968 cam_to_entity.normalize();
970 int row = m_tx_basepos.Y;
971 int col = m_tx_basepos.X;
973 if(m_tx_select_horiz_by_yawpitch)
975 if(cam_to_entity.Y > 0.75)
977 else if(cam_to_entity.Y < -0.75)
980 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
981 float dir = mob_dir - m_yaw;
982 dir = wrapDegrees_180(dir);
983 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
984 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
986 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
988 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
990 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
997 // Animation goes downwards
1000 float txs = m_tx_size.X;
1001 float tys = m_tx_size.Y;
1002 setBillboardTextureMatrix(m_spritenode,
1003 txs, tys, col, row);
1007 void updateTextures(const std::string &mod)
1009 ITextureSource *tsrc = m_gamedef->tsrc();
1013 if(m_prop.visual == "sprite")
1015 std::string texturestring = "unknown_block.png";
1016 if(m_prop.textures.size() >= 1)
1017 texturestring = m_prop.textures[0];
1018 texturestring += mod;
1019 m_spritenode->setMaterialTexture(0,
1020 tsrc->getTextureRaw(texturestring));
1025 if(m_prop.visual == "cube")
1027 for (u32 i = 0; i < 6; ++i)
1029 std::string texturestring = "unknown_block.png";
1030 if(m_prop.textures.size() > i)
1031 texturestring = m_prop.textures[i];
1032 texturestring += mod;
1033 AtlasPointer ap = tsrc->getTexture(texturestring);
1035 // Get the tile texture and atlas transformation
1036 video::ITexture* atlas = ap.atlas;
1040 // Set material flags and texture
1041 video::SMaterial& material = m_meshnode->getMaterial(i);
1042 material.setFlag(video::EMF_LIGHTING, false);
1043 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1044 material.setTexture(0, atlas);
1045 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1046 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1049 else if(m_prop.visual == "upright_sprite")
1051 scene::IMesh *mesh = m_meshnode->getMesh();
1053 std::string tname = "unknown_object.png";
1054 if(m_prop.textures.size() >= 1)
1055 tname = m_prop.textures[0];
1057 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1058 buf->getMaterial().setTexture(0,
1059 tsrc->getTextureRaw(tname));
1062 std::string tname = "unknown_object.png";
1063 if(m_prop.textures.size() >= 2)
1064 tname = m_prop.textures[1];
1065 else if(m_prop.textures.size() >= 1)
1066 tname = m_prop.textures[0];
1068 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1069 buf->getMaterial().setTexture(0,
1070 tsrc->getTextureRaw(tname));
1076 void processMessage(const std::string &data)
1078 //infostream<<"GenericCAO: Got message"<<std::endl;
1079 std::istringstream is(data, std::ios::binary);
1081 u8 cmd = readU8(is);
1082 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1084 m_prop = gob_read_set_properties(is);
1086 m_selection_box = m_prop.collisionbox;
1087 m_selection_box.MinEdge *= BS;
1088 m_selection_box.MaxEdge *= BS;
1090 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1091 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1093 if(!m_initial_tx_basepos_set){
1094 m_initial_tx_basepos_set = true;
1095 m_tx_basepos = m_prop.initial_sprite_basepos;
1100 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1102 m_position = readV3F1000(is);
1103 m_velocity = readV3F1000(is);
1104 m_acceleration = readV3F1000(is);
1105 if(fabs(m_prop.automatic_rotate) < 0.001)
1106 m_yaw = readF1000(is);
1107 bool do_interpolate = readU8(is);
1108 bool is_end_position = readU8(is);
1109 float update_interval = readF1000(is);
1111 // Place us a bit higher if we're physical, to not sink into
1112 // the ground due to sucky collision detection...
1114 m_position += v3f(0,0.002,0);
1117 if(!m_prop.physical)
1118 pos_translator.update(m_position, is_end_position, update_interval);
1120 pos_translator.init(m_position);
1124 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1126 std::string mod = deSerializeString(is);
1127 updateTextures(mod);
1129 else if(cmd == GENERIC_CMD_SET_SPRITE)
1131 v2s16 p = readV2S16(is);
1132 int num_frames = readU16(is);
1133 float framelength = readF1000(is);
1134 bool select_horiz_by_yawpitch = readU8(is);
1137 m_anim_num_frames = num_frames;
1138 m_anim_framelength = framelength;
1139 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1143 else if(cmd == GENERIC_CMD_PUNCHED)
1145 /*s16 damage =*/ readS16(is);
1146 s16 result_hp = readS16(is);
1150 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1152 m_armor_groups.clear();
1153 int armor_groups_size = readU16(is);
1154 for(int i=0; i<armor_groups_size; i++){
1155 std::string name = deSerializeString(is);
1156 int rating = readS16(is);
1157 m_armor_groups[name] = rating;
1162 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1163 float time_from_last_punch=1000000)
1166 const ToolCapabilities *toolcap =
1167 &punchitem->getToolCapabilities(m_gamedef->idef());
1168 PunchDamageResult result = getPunchDamage(
1172 time_from_last_punch);
1174 if(result.did_punch && result.damage != 0)
1176 if(result.damage < m_hp){
1177 m_hp -= result.damage;
1180 // TODO: Execute defined fast response
1181 // As there is no definition, make a smoke puff
1182 ClientSimpleObject *simple = createSmokePuff(
1183 m_smgr, m_env, m_position,
1184 m_prop.visual_size * BS);
1185 m_env->addSimpleObject(simple);
1187 // TODO: Execute defined fast response
1188 // Flashing shall suffice as there is no definition
1189 m_reset_textures_timer = 0.05;
1190 if(result.damage >= 2)
1191 m_reset_textures_timer += 0.05 * result.damage;
1192 updateTextures("^[brighten");
1198 std::string debugInfoText()
1200 std::ostringstream os(std::ios::binary);
1201 os<<"GenericCAO hp="<<m_hp<<"\n";
1203 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1204 i != m_armor_groups.end(); i++){
1205 os<<i->first<<"="<<i->second<<", ";
1213 GenericCAO proto_GenericCAO(NULL, NULL);