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"
44 struct ToolCapabilities;
46 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
48 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
54 struct SmoothTranslator
61 f32 anim_time_counter;
81 anim_time_counter = 0;
90 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
92 aim_is_end = is_end_position;
95 if(update_interval > 0){
96 anim_time = update_interval;
98 if(anim_time < 0.001 || anim_time > 1.0)
99 anim_time = anim_time_counter;
101 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
103 anim_time_counter = 0;
107 void translate(f32 dtime)
109 anim_time_counter = anim_time_counter + dtime;
110 anim_counter = anim_counter + dtime;
111 v3f vect_move = vect_aim - vect_old;
113 if(anim_time > 0.001)
114 moveratio = anim_time_counter / anim_time;
115 // Move a bit less than should, to avoid oscillation
116 moveratio = moveratio * 0.8;
117 float move_end = 1.5;
120 if(moveratio > move_end)
121 moveratio = move_end;
122 vect_show = vect_old + vect_move * moveratio;
127 return ((anim_time_counter / anim_time) < 1.4);
135 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
136 float txs, float tys, int col, int row)
138 video::SMaterial& material = bill->getMaterial(0);
139 core::matrix4& matrix = material.getTextureMatrix(0);
140 matrix.setTextureTranslate(txs*col, tys*row);
141 matrix.setTextureScale(txs, tys);
148 class TestCAO : public ClientActiveObject
151 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
156 return ACTIVEOBJECT_TYPE_TEST;
159 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
161 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
162 IrrlichtDevice *irr);
163 void removeFromScene();
164 void updateLight(u8 light_at_pos);
165 v3s16 getLightPosition();
166 void updateNodePos();
168 void step(float dtime, ClientEnvironment *env);
170 void processMessage(const std::string &data);
173 scene::IMeshSceneNode *m_node;
178 TestCAO proto_TestCAO(NULL, NULL);
180 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
181 ClientActiveObject(0, gamedef, env),
183 m_position(v3f(0,10*BS,0))
185 ClientActiveObject::registerType(getType(), create);
192 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
194 return new TestCAO(gamedef, env);
197 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
203 //video::IVideoDriver* driver = smgr->getVideoDriver();
205 scene::SMesh *mesh = new scene::SMesh();
206 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
207 video::SColor c(255,255,255,255);
208 video::S3DVertex vertices[4] =
210 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
211 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
212 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
213 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
215 u16 indices[] = {0,1,2,2,3,0};
216 buf->append(vertices, 4, indices, 6);
218 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
219 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
220 buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
221 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
222 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
223 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
225 mesh->addMeshBuffer(buf);
227 m_node = smgr->addMeshSceneNode(mesh, NULL);
232 void TestCAO::removeFromScene()
241 void TestCAO::updateLight(u8 light_at_pos)
245 v3s16 TestCAO::getLightPosition()
247 return floatToInt(m_position, BS);
250 void TestCAO::updateNodePos()
255 m_node->setPosition(m_position);
256 //m_node->setRotation(v3f(0, 45, 0));
259 void TestCAO::step(float dtime, ClientEnvironment *env)
263 v3f rot = m_node->getRotation();
264 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
265 rot.Y += dtime * 180;
266 m_node->setRotation(rot);
270 void TestCAO::processMessage(const std::string &data)
272 infostream<<"TestCAO: Got data: "<<data<<std::endl;
273 std::istringstream is(data, std::ios::binary);
291 class ItemCAO : public ClientActiveObject
294 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
299 return ACTIVEOBJECT_TYPE_ITEM;
302 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
304 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
305 IrrlichtDevice *irr);
306 void removeFromScene();
307 void updateLight(u8 light_at_pos);
308 v3s16 getLightPosition();
309 void updateNodePos();
310 void updateInfoText();
311 void updateTexture();
313 void step(float dtime, ClientEnvironment *env);
315 void processMessage(const std::string &data);
317 void initialize(const std::string &data);
319 core::aabbox3d<f32>* getSelectionBox()
320 {return &m_selection_box;}
324 std::string infoText()
328 core::aabbox3d<f32> m_selection_box;
329 scene::IMeshSceneNode *m_node;
331 std::string m_itemstring;
332 std::string m_infotext;
335 #include "inventory.h"
338 ItemCAO proto_ItemCAO(NULL, NULL);
340 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
341 ClientActiveObject(0, gamedef, env),
342 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
344 m_position(v3f(0,10*BS,0))
348 ClientActiveObject::registerType(getType(), create);
356 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
358 return new ItemCAO(gamedef, env);
361 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
367 //video::IVideoDriver* driver = smgr->getVideoDriver();
369 scene::SMesh *mesh = new scene::SMesh();
370 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
371 video::SColor c(255,255,255,255);
372 video::S3DVertex vertices[4] =
374 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
375 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
376 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
377 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
378 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
379 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
380 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
381 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
383 u16 indices[] = {0,1,2,2,3,0};
384 buf->append(vertices, 4, indices, 6);
386 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
387 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
388 // Initialize with a generated placeholder texture
389 buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
390 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
391 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
392 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
394 mesh->addMeshBuffer(buf);
396 m_node = smgr->addMeshSceneNode(mesh, NULL);
407 void ItemCAO::removeFromScene()
416 void ItemCAO::updateLight(u8 light_at_pos)
421 u8 li = decode_light(light_at_pos);
422 video::SColor color(255,li,li,li);
423 setMeshColor(m_node->getMesh(), color);
426 v3s16 ItemCAO::getLightPosition()
428 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
431 void ItemCAO::updateNodePos()
436 m_node->setPosition(m_position);
439 void ItemCAO::updateInfoText()
442 IItemDefManager *idef = m_gamedef->idef();
444 item.deSerialize(m_itemstring, idef);
445 if(item.isKnown(idef))
446 m_infotext = item.getDefinition(idef).description;
448 m_infotext = "Unknown item: '" + m_itemstring + "'";
450 m_infotext += " (" + itos(item.count) + ")";
452 catch(SerializationError &e)
454 m_infotext = "Unknown item: '" + m_itemstring + "'";
458 void ItemCAO::updateTexture()
463 // Create an inventory item to see what is its image
464 std::istringstream is(m_itemstring, std::ios_base::binary);
465 video::ITexture *texture = NULL;
467 IItemDefManager *idef = m_gamedef->idef();
469 item.deSerialize(is, idef);
470 texture = item.getDefinition(idef).inventory_texture;
472 catch(SerializationError &e)
474 infostream<<"WARNING: "<<__FUNCTION_NAME
475 <<": error deSerializing itemstring \""
476 <<m_itemstring<<std::endl;
479 // Set meshbuffer texture
480 m_node->getMaterial(0).setTexture(0, texture);
484 void ItemCAO::step(float dtime, ClientEnvironment *env)
488 /*v3f rot = m_node->getRotation();
489 rot.Y += dtime * 120;
490 m_node->setRotation(rot);*/
491 LocalPlayer *player = env->getLocalPlayer();
493 v3f rot = m_node->getRotation();
494 rot.Y = 180.0 - (player->getYaw());
495 m_node->setRotation(rot);
499 void ItemCAO::processMessage(const std::string &data)
501 //infostream<<"ItemCAO: Got message"<<std::endl;
502 std::istringstream is(data, std::ios::binary);
508 m_position = readV3F1000(is);
514 m_itemstring = deSerializeString(is);
520 void ItemCAO::initialize(const std::string &data)
522 infostream<<"ItemCAO: Got init data"<<std::endl;
525 std::istringstream is(data, std::ios::binary);
527 u8 version = readU8(is);
532 m_position = readV3F1000(is);
534 m_itemstring = deSerializeString(is);
545 #include "genericobject.h"
547 class GenericCAO : public ClientActiveObject
550 // Only set at initialization
553 bool m_is_local_player; // determined locally
554 // Property-ish things
555 ObjectProperties m_prop;
557 scene::ISceneManager *m_smgr;
558 IrrlichtDevice *m_irr;
559 core::aabbox3d<f32> m_selection_box;
560 scene::IMeshSceneNode *m_meshnode;
561 scene::IBillboardSceneNode *m_spritenode;
562 scene::ITextSceneNode* m_textnode;
568 SmoothTranslator pos_translator;
569 // Spritesheet/animation stuff
572 bool m_initial_tx_basepos_set;
573 bool m_tx_select_horiz_by_yawpitch;
575 int m_anim_num_frames;
576 float m_anim_framelength;
578 ItemGroupList m_armor_groups;
579 float m_reset_textures_timer;
580 bool m_visuals_expired;
581 float m_step_distance_counter;
585 GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
586 ClientActiveObject(0, gamedef, env),
589 m_is_local_player(false),
593 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
597 m_position(v3f(0,10*BS,0)),
598 m_velocity(v3f(0,0,0)),
599 m_acceleration(v3f(0,0,0)),
604 m_initial_tx_basepos_set(false),
605 m_tx_select_horiz_by_yawpitch(false),
607 m_anim_num_frames(1),
608 m_anim_framelength(0.2),
610 m_reset_textures_timer(-1),
611 m_visuals_expired(false),
612 m_step_distance_counter(0),
616 ClientActiveObject::registerType(getType(), create);
619 void initialize(const std::string &data)
621 infostream<<"GenericCAO: Got init data"<<std::endl;
622 std::istringstream is(data, std::ios::binary);
624 u8 version = readU8(is);
627 errorstream<<"GenericCAO: Unsupported init data version"
631 m_name = deSerializeString(is);
632 m_is_player = readU8(is);
633 m_position = readV3F1000(is);
634 m_yaw = readF1000(is);
637 int num_messages = readU8(is);
638 for(int i=0; i<num_messages; i++){
639 std::string message = deSerializeLongString(is);
640 processMessage(message);
643 pos_translator.init(m_position);
647 Player *player = m_env->getPlayer(m_name.c_str());
648 if(player && player->isLocal()){
649 m_is_local_player = true;
658 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
660 return new GenericCAO(gamedef, env);
665 return ACTIVEOBJECT_TYPE_GENERIC;
667 core::aabbox3d<f32>* getSelectionBox()
669 if(!m_prop.is_visible || m_is_local_player)
671 return &m_selection_box;
675 return pos_translator.vect_show;
678 void removeFromScene()
681 m_meshnode->remove();
685 m_spritenode->remove();
690 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
696 if(m_meshnode != NULL || m_spritenode != NULL)
699 m_visuals_expired = false;
701 if(!m_prop.is_visible || m_is_local_player)
704 //video::IVideoDriver* driver = smgr->getVideoDriver();
706 if(m_prop.visual == "sprite"){
707 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
708 m_spritenode = smgr->addBillboardSceneNode(
709 NULL, v2f(1, 1), v3f(0,0,0), -1);
710 m_spritenode->setMaterialTexture(0,
711 tsrc->getTextureRaw("unknown_block.png"));
712 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
713 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
714 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
715 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
716 u8 li = m_last_light;
717 m_spritenode->setColor(video::SColor(255,li,li,li));
718 m_spritenode->setSize(m_prop.visual_size*BS);
720 const float txs = 1.0 / 1;
721 const float tys = 1.0 / 1;
722 setBillboardTextureMatrix(m_spritenode,
726 else if(m_prop.visual == "upright_sprite")
728 scene::SMesh *mesh = new scene::SMesh();
729 double dx = BS*m_prop.visual_size.X/2;
730 double dy = BS*m_prop.visual_size.Y/2;
732 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
733 u8 li = m_last_light;
734 video::SColor c(255,li,li,li);
735 video::S3DVertex vertices[4] =
737 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
738 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
739 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
740 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
742 u16 indices[] = {0,1,2,2,3,0};
743 buf->append(vertices, 4, indices, 6);
745 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
746 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
747 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
748 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
750 mesh->addMeshBuffer(buf);
754 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
755 u8 li = m_last_light;
756 video::SColor c(255,li,li,li);
757 video::S3DVertex vertices[4] =
759 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
760 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
761 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
762 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
764 u16 indices[] = {0,1,2,2,3,0};
765 buf->append(vertices, 4, indices, 6);
767 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
768 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
769 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
770 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
772 mesh->addMeshBuffer(buf);
775 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
777 // Set it to use the materials of the meshbuffers directly.
778 // This is needed for changing the texture in the future
779 m_meshnode->setReadOnlyMaterials(true);
781 else if(m_prop.visual == "cube"){
782 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
783 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
784 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
787 m_meshnode->setScale(v3f(m_prop.visual_size.X,
788 m_prop.visual_size.Y,
789 m_prop.visual_size.X));
790 u8 li = m_last_light;
791 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
792 } else if(m_prop.visual == "wielditem"){
793 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
794 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
795 if(m_prop.textures.size() >= 1){
796 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
797 IItemDefManager *idef = m_gamedef->idef();
798 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
799 scene::IMesh *mesh = item.getDefinition(idef).wield_mesh;
800 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
802 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
803 m_prop.visual_size.Y/2,
804 m_prop.visual_size.X/2));
805 u8 li = m_last_light;
806 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
809 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
810 <<"\" not supported"<<std::endl;
814 scene::ISceneNode *node = NULL;
819 if(node && m_is_player && !m_is_local_player){
820 // Add a text node for showing the name
821 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
822 std::wstring wname = narrow_to_wide(m_name);
823 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
824 wname.c_str(), video::SColor(255,255,255,255), node);
825 m_textnode->setPosition(v3f(0, BS*1.1, 0));
833 m_visuals_expired = true;
836 void updateLight(u8 light_at_pos)
838 bool is_visible = (m_hp != 0);
839 u8 li = decode_light(light_at_pos);
841 video::SColor color(255,li,li,li);
843 setMeshColor(m_meshnode->getMesh(), color);
844 m_meshnode->setVisible(is_visible);
847 m_spritenode->setColor(color);
848 m_spritenode->setVisible(is_visible);
852 v3s16 getLightPosition()
854 return floatToInt(m_position, BS);
860 m_meshnode->setPosition(pos_translator.vect_show);
861 v3f rot = m_meshnode->getRotation();
863 m_meshnode->setRotation(rot);
866 m_spritenode->setPosition(pos_translator.vect_show);
870 void step(float dtime, ClientEnvironment *env)
872 v3f lastpos = pos_translator.vect_show;
874 if(m_visuals_expired && m_smgr && m_irr){
875 m_visuals_expired = false;
877 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
881 core::aabbox3d<f32> box = m_prop.collisionbox;
884 collisionMoveResult moveresult;
885 f32 pos_max_d = BS*0.125; // Distance per iteration
887 v3f p_pos = m_position;
888 v3f p_velocity = m_velocity;
889 v3f p_acceleration = m_acceleration;
890 IGameDef *gamedef = env->getGameDef();
891 moveresult = collisionMoveSimple(&env->getMap(), gamedef,
892 pos_max_d, box, stepheight, dtime,
893 p_pos, p_velocity, p_acceleration);
896 m_velocity = p_velocity;
897 m_acceleration = p_acceleration;
899 bool is_end_position = moveresult.collides;
900 pos_translator.update(m_position, is_end_position, dtime);
901 pos_translator.translate(dtime);
904 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
905 m_velocity += dtime * m_acceleration;
906 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
907 pos_translator.translate(dtime);
911 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
912 m_step_distance_counter += moved;
913 if(m_step_distance_counter > 1.5*BS){
914 m_step_distance_counter = 0;
915 if(!m_is_local_player && m_prop.makes_footstep_sound){
916 INodeDefManager *ndef = m_gamedef->ndef();
917 v3s16 p = floatToInt(getPosition() + v3f(0,
918 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
919 MapNode n = m_env->getMap().getNodeNoEx(p);
920 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
921 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
925 m_anim_timer += dtime;
926 if(m_anim_timer >= m_anim_framelength){
927 m_anim_timer -= m_anim_framelength;
929 if(m_anim_frame >= m_anim_num_frames)
935 if(m_reset_textures_timer >= 0){
936 m_reset_textures_timer -= dtime;
937 if(m_reset_textures_timer <= 0){
938 m_reset_textures_timer = -1;
942 if(fabs(m_prop.automatic_rotate) > 0.001){
943 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
948 void updateTexturePos()
951 scene::ICameraSceneNode* camera =
952 m_spritenode->getSceneManager()->getActiveCamera();
955 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
956 - camera->getAbsolutePosition();
957 cam_to_entity.normalize();
959 int row = m_tx_basepos.Y;
960 int col = m_tx_basepos.X;
962 if(m_tx_select_horiz_by_yawpitch)
964 if(cam_to_entity.Y > 0.75)
966 else if(cam_to_entity.Y < -0.75)
969 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
970 float dir = mob_dir - m_yaw;
971 dir = wrapDegrees_180(dir);
972 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
973 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
975 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
977 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
979 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
986 // Animation goes downwards
989 float txs = m_tx_size.X;
990 float tys = m_tx_size.Y;
991 setBillboardTextureMatrix(m_spritenode,
996 void updateTextures(const std::string &mod)
998 ITextureSource *tsrc = m_gamedef->tsrc();
1002 if(m_prop.visual == "sprite")
1004 std::string texturestring = "unknown_block.png";
1005 if(m_prop.textures.size() >= 1)
1006 texturestring = m_prop.textures[0];
1007 texturestring += mod;
1008 m_spritenode->setMaterialTexture(0,
1009 tsrc->getTextureRaw(texturestring));
1014 if(m_prop.visual == "cube")
1016 for (u32 i = 0; i < 6; ++i)
1018 std::string texturestring = "unknown_block.png";
1019 if(m_prop.textures.size() > i)
1020 texturestring = m_prop.textures[i];
1021 texturestring += mod;
1022 AtlasPointer ap = tsrc->getTexture(texturestring);
1024 // Get the tile texture and atlas transformation
1025 video::ITexture* atlas = ap.atlas;
1029 // Set material flags and texture
1030 video::SMaterial& material = m_meshnode->getMaterial(i);
1031 material.setFlag(video::EMF_LIGHTING, false);
1032 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1033 material.setTexture(0, atlas);
1034 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1035 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1038 else if(m_prop.visual == "upright_sprite")
1040 scene::IMesh *mesh = m_meshnode->getMesh();
1042 std::string tname = "unknown_object.png";
1043 if(m_prop.textures.size() >= 1)
1044 tname = m_prop.textures[0];
1046 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1047 buf->getMaterial().setTexture(0,
1048 tsrc->getTextureRaw(tname));
1051 std::string tname = "unknown_object.png";
1052 if(m_prop.textures.size() >= 2)
1053 tname = m_prop.textures[1];
1054 else if(m_prop.textures.size() >= 1)
1055 tname = m_prop.textures[0];
1057 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1058 buf->getMaterial().setTexture(0,
1059 tsrc->getTextureRaw(tname));
1065 void processMessage(const std::string &data)
1067 //infostream<<"GenericCAO: Got message"<<std::endl;
1068 std::istringstream is(data, std::ios::binary);
1070 u8 cmd = readU8(is);
1071 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1073 m_prop = gob_read_set_properties(is);
1075 m_selection_box = m_prop.collisionbox;
1076 m_selection_box.MinEdge *= BS;
1077 m_selection_box.MaxEdge *= BS;
1079 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1080 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1082 if(!m_initial_tx_basepos_set){
1083 m_initial_tx_basepos_set = true;
1084 m_tx_basepos = m_prop.initial_sprite_basepos;
1089 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1091 m_position = readV3F1000(is);
1092 m_velocity = readV3F1000(is);
1093 m_acceleration = readV3F1000(is);
1094 if(fabs(m_prop.automatic_rotate) < 0.001)
1095 m_yaw = readF1000(is);
1096 bool do_interpolate = readU8(is);
1097 bool is_end_position = readU8(is);
1098 float update_interval = readF1000(is);
1100 // Place us a bit higher if we're physical, to not sink into
1101 // the ground due to sucky collision detection...
1103 m_position += v3f(0,0.002,0);
1106 if(!m_prop.physical)
1107 pos_translator.update(m_position, is_end_position, update_interval);
1109 pos_translator.init(m_position);
1113 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1115 std::string mod = deSerializeString(is);
1116 updateTextures(mod);
1118 else if(cmd == GENERIC_CMD_SET_SPRITE)
1120 v2s16 p = readV2S16(is);
1121 int num_frames = readU16(is);
1122 float framelength = readF1000(is);
1123 bool select_horiz_by_yawpitch = readU8(is);
1126 m_anim_num_frames = num_frames;
1127 m_anim_framelength = framelength;
1128 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1132 else if(cmd == GENERIC_CMD_PUNCHED)
1134 /*s16 damage =*/ readS16(is);
1135 s16 result_hp = readS16(is);
1139 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1141 m_armor_groups.clear();
1142 int armor_groups_size = readU16(is);
1143 for(int i=0; i<armor_groups_size; i++){
1144 std::string name = deSerializeString(is);
1145 int rating = readS16(is);
1146 m_armor_groups[name] = rating;
1151 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1152 float time_from_last_punch=1000000)
1155 const ToolCapabilities *toolcap =
1156 &punchitem->getToolCapabilities(m_gamedef->idef());
1157 PunchDamageResult result = getPunchDamage(
1161 time_from_last_punch);
1163 if(result.did_punch && result.damage != 0)
1165 if(result.damage < m_hp){
1166 m_hp -= result.damage;
1169 // TODO: Execute defined fast response
1170 // As there is no definition, make a smoke puff
1171 ClientSimpleObject *simple = createSmokePuff(
1172 m_smgr, m_env, m_position,
1173 m_prop.visual_size * BS);
1174 m_env->addSimpleObject(simple);
1176 // TODO: Execute defined fast response
1177 // Flashing shall suffice as there is no definition
1178 m_reset_textures_timer = 0.05;
1179 if(result.damage >= 2)
1180 m_reset_textures_timer += 0.05 * result.damage;
1181 updateTextures("^[brighten");
1187 std::string debugInfoText()
1189 std::ostringstream os(std::ios::binary);
1190 os<<"GenericCAO hp="<<m_hp<<"\n";
1192 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1193 i != m_armor_groups.end(); i++){
1194 os<<i->first<<"="<<i->second<<", ";
1202 GenericCAO proto_GenericCAO(NULL, NULL);