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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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"
33 #include "utility.h" // For IntervalLimiter
36 #include "content_cso.h"
40 struct ToolCapabilities;
42 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
44 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
50 struct SmoothTranslator
57 f32 anim_time_counter;
77 anim_time_counter = 0;
86 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
88 aim_is_end = is_end_position;
91 if(update_interval > 0){
92 anim_time = update_interval;
94 if(anim_time < 0.001 || anim_time > 1.0)
95 anim_time = anim_time_counter;
97 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
99 anim_time_counter = 0;
103 void translate(f32 dtime)
105 anim_time_counter = anim_time_counter + dtime;
106 anim_counter = anim_counter + dtime;
107 v3f vect_move = vect_aim - vect_old;
109 if(anim_time > 0.001)
110 moveratio = anim_time_counter / anim_time;
111 // Move a bit less than should, to avoid oscillation
112 moveratio = moveratio * 0.8;
113 float move_end = 1.5;
116 if(moveratio > move_end)
117 moveratio = move_end;
118 vect_show = vect_old + vect_move * moveratio;
123 return ((anim_time_counter / anim_time) < 1.4);
131 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
132 float txs, float tys, int col, int row)
134 video::SMaterial& material = bill->getMaterial(0);
135 core::matrix4& matrix = material.getTextureMatrix(0);
136 matrix.setTextureTranslate(txs*col, tys*row);
137 matrix.setTextureScale(txs, tys);
144 class TestCAO : public ClientActiveObject
147 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
152 return ACTIVEOBJECT_TYPE_TEST;
155 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
157 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
158 IrrlichtDevice *irr);
159 void removeFromScene();
160 void updateLight(u8 light_at_pos);
161 v3s16 getLightPosition();
162 void updateNodePos();
164 void step(float dtime, ClientEnvironment *env);
166 void processMessage(const std::string &data);
169 scene::IMeshSceneNode *m_node;
174 TestCAO proto_TestCAO(NULL, NULL);
176 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
177 ClientActiveObject(0, gamedef, env),
179 m_position(v3f(0,10*BS,0))
181 ClientActiveObject::registerType(getType(), create);
188 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
190 return new TestCAO(gamedef, env);
193 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
199 //video::IVideoDriver* driver = smgr->getVideoDriver();
201 scene::SMesh *mesh = new scene::SMesh();
202 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
203 video::SColor c(255,255,255,255);
204 video::S3DVertex vertices[4] =
206 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
207 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
208 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
209 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
211 u16 indices[] = {0,1,2,2,3,0};
212 buf->append(vertices, 4, indices, 6);
214 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
215 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
216 buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
217 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
218 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
219 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
221 mesh->addMeshBuffer(buf);
223 m_node = smgr->addMeshSceneNode(mesh, NULL);
228 void TestCAO::removeFromScene()
237 void TestCAO::updateLight(u8 light_at_pos)
241 v3s16 TestCAO::getLightPosition()
243 return floatToInt(m_position, BS);
246 void TestCAO::updateNodePos()
251 m_node->setPosition(m_position);
252 //m_node->setRotation(v3f(0, 45, 0));
255 void TestCAO::step(float dtime, ClientEnvironment *env)
259 v3f rot = m_node->getRotation();
260 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
261 rot.Y += dtime * 180;
262 m_node->setRotation(rot);
266 void TestCAO::processMessage(const std::string &data)
268 infostream<<"TestCAO: Got data: "<<data<<std::endl;
269 std::istringstream is(data, std::ios::binary);
287 class ItemCAO : public ClientActiveObject
290 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
295 return ACTIVEOBJECT_TYPE_ITEM;
298 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
300 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
301 IrrlichtDevice *irr);
302 void removeFromScene();
303 void updateLight(u8 light_at_pos);
304 v3s16 getLightPosition();
305 void updateNodePos();
306 void updateInfoText();
307 void updateTexture();
309 void step(float dtime, ClientEnvironment *env);
311 void processMessage(const std::string &data);
313 void initialize(const std::string &data);
315 core::aabbox3d<f32>* getSelectionBox()
316 {return &m_selection_box;}
320 std::string infoText()
324 core::aabbox3d<f32> m_selection_box;
325 scene::IMeshSceneNode *m_node;
327 std::string m_itemstring;
328 std::string m_infotext;
331 #include "inventory.h"
334 ItemCAO proto_ItemCAO(NULL, NULL);
336 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
337 ClientActiveObject(0, gamedef, env),
338 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
340 m_position(v3f(0,10*BS,0))
344 ClientActiveObject::registerType(getType(), create);
352 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
354 return new ItemCAO(gamedef, env);
357 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
363 //video::IVideoDriver* driver = smgr->getVideoDriver();
365 scene::SMesh *mesh = new scene::SMesh();
366 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
367 video::SColor c(255,255,255,255);
368 video::S3DVertex vertices[4] =
370 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
371 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
372 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
373 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
374 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
375 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
376 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
377 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
379 u16 indices[] = {0,1,2,2,3,0};
380 buf->append(vertices, 4, indices, 6);
382 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
383 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
384 // Initialize with a generated placeholder texture
385 buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
386 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
387 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
388 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
390 mesh->addMeshBuffer(buf);
392 m_node = smgr->addMeshSceneNode(mesh, NULL);
403 void ItemCAO::removeFromScene()
412 void ItemCAO::updateLight(u8 light_at_pos)
417 u8 li = decode_light(light_at_pos);
418 video::SColor color(255,li,li,li);
419 setMeshColor(m_node->getMesh(), color);
422 v3s16 ItemCAO::getLightPosition()
424 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
427 void ItemCAO::updateNodePos()
432 m_node->setPosition(m_position);
435 void ItemCAO::updateInfoText()
438 IItemDefManager *idef = m_gamedef->idef();
440 item.deSerialize(m_itemstring, idef);
441 if(item.isKnown(idef))
442 m_infotext = item.getDefinition(idef).description;
444 m_infotext = "Unknown item: '" + m_itemstring + "'";
446 m_infotext += " (" + itos(item.count) + ")";
448 catch(SerializationError &e)
450 m_infotext = "Unknown item: '" + m_itemstring + "'";
454 void ItemCAO::updateTexture()
459 // Create an inventory item to see what is its image
460 std::istringstream is(m_itemstring, std::ios_base::binary);
461 video::ITexture *texture = NULL;
463 IItemDefManager *idef = m_gamedef->idef();
465 item.deSerialize(is, idef);
466 texture = item.getDefinition(idef).inventory_texture;
468 catch(SerializationError &e)
470 infostream<<"WARNING: "<<__FUNCTION_NAME
471 <<": error deSerializing itemstring \""
472 <<m_itemstring<<std::endl;
475 // Set meshbuffer texture
476 m_node->getMaterial(0).setTexture(0, texture);
480 void ItemCAO::step(float dtime, ClientEnvironment *env)
484 /*v3f rot = m_node->getRotation();
485 rot.Y += dtime * 120;
486 m_node->setRotation(rot);*/
487 LocalPlayer *player = env->getLocalPlayer();
489 v3f rot = m_node->getRotation();
490 rot.Y = 180.0 - (player->getYaw());
491 m_node->setRotation(rot);
495 void ItemCAO::processMessage(const std::string &data)
497 //infostream<<"ItemCAO: Got message"<<std::endl;
498 std::istringstream is(data, std::ios::binary);
504 m_position = readV3F1000(is);
510 m_itemstring = deSerializeString(is);
516 void ItemCAO::initialize(const std::string &data)
518 infostream<<"ItemCAO: Got init data"<<std::endl;
521 std::istringstream is(data, std::ios::binary);
523 u8 version = readU8(is);
528 m_position = readV3F1000(is);
530 m_itemstring = deSerializeString(is);
541 #include "genericobject.h"
543 class GenericCAO : public ClientActiveObject
546 // Only set at initialization
549 bool m_is_local_player; // determined locally
550 // Property-ish things
554 core::aabbox3d<f32> m_collisionbox;
555 std::string m_visual;
557 core::array<std::string> m_textures;
560 bool m_makes_footstep_sound;
562 scene::ISceneManager *m_smgr;
563 IrrlichtDevice *m_irr;
564 core::aabbox3d<f32> m_selection_box;
565 scene::IMeshSceneNode *m_meshnode;
566 scene::IBillboardSceneNode *m_spritenode;
567 scene::ITextSceneNode* m_textnode;
573 SmoothTranslator pos_translator;
574 // Spritesheet/animation stuff
577 bool m_tx_select_horiz_by_yawpitch;
579 int m_anim_num_frames;
580 float m_anim_framelength;
582 ItemGroupList m_armor_groups;
583 float m_reset_textures_timer;
584 bool m_visuals_expired;
585 float m_step_distance_counter;
588 GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
589 ClientActiveObject(0, gamedef, env),
592 m_is_local_player(false),
597 m_collisionbox(-0.5,-0.5,-0.5, 0.5,0.5,0.5),
602 m_makes_footstep_sound(false),
606 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
610 m_position(v3f(0,10*BS,0)),
611 m_velocity(v3f(0,0,0)),
612 m_acceleration(v3f(0,0,0)),
617 m_tx_select_horiz_by_yawpitch(false),
619 m_anim_num_frames(1),
620 m_anim_framelength(0.2),
622 m_reset_textures_timer(-1),
623 m_visuals_expired(false),
624 m_step_distance_counter(0)
626 m_textures.push_back("unknown_object.png");
628 ClientActiveObject::registerType(getType(), create);
631 void initialize(const std::string &data)
633 infostream<<"GenericCAO: Got init data"<<std::endl;
634 std::istringstream is(data, std::ios::binary);
636 u8 version = readU8(is);
639 errorstream<<"GenericCAO: Unsupported init data version"
643 m_name = deSerializeString(is);
644 m_is_player = readU8(is);
645 m_position = readV3F1000(is);
646 m_yaw = readF1000(is);
649 int num_messages = readU8(is);
650 for(int i=0; i<num_messages; i++){
651 std::string message = deSerializeLongString(is);
652 processMessage(message);
655 pos_translator.init(m_position);
659 Player *player = m_env->getPlayer(m_name.c_str());
660 if(player && player->isLocal()){
661 m_is_local_player = true;
670 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
672 return new GenericCAO(gamedef, env);
677 return ACTIVEOBJECT_TYPE_GENERIC;
679 core::aabbox3d<f32>* getSelectionBox()
681 if(!m_is_visible || m_is_local_player)
683 return &m_selection_box;
687 return pos_translator.vect_show;
690 void removeFromScene()
693 m_meshnode->remove();
697 m_spritenode->remove();
702 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
708 if(m_meshnode != NULL || m_spritenode != NULL)
711 m_visuals_expired = false;
713 if(!m_is_visible || m_is_local_player)
716 //video::IVideoDriver* driver = smgr->getVideoDriver();
718 if(m_visual == "sprite"){
719 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
720 m_spritenode = smgr->addBillboardSceneNode(
721 NULL, v2f(1, 1), v3f(0,0,0), -1);
722 m_spritenode->setMaterialTexture(0,
723 tsrc->getTextureRaw("unknown_block.png"));
724 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
725 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
726 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
727 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
728 m_spritenode->setColor(video::SColor(255,0,0,0));
729 m_spritenode->setVisible(false); /* Set visible when brightness is known */
730 m_spritenode->setSize(m_visual_size*BS);
732 const float txs = 1.0 / 1;
733 const float tys = 1.0 / 1;
734 setBillboardTextureMatrix(m_spritenode,
738 else if(m_visual == "upright_sprite")
740 scene::SMesh *mesh = new scene::SMesh();
741 double dx = BS*m_visual_size.X/2;
742 double dy = BS*m_visual_size.Y/2;
744 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
745 video::SColor c(255,255,255,255);
746 video::S3DVertex vertices[4] =
748 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
749 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
750 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
751 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
753 u16 indices[] = {0,1,2,2,3,0};
754 buf->append(vertices, 4, indices, 6);
756 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
757 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
758 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
759 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
761 mesh->addMeshBuffer(buf);
765 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
766 video::SColor c(255,255,255,255);
767 video::S3DVertex vertices[4] =
769 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
770 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
771 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
772 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
774 u16 indices[] = {0,1,2,2,3,0};
775 buf->append(vertices, 4, indices, 6);
777 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
778 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
779 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
780 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
782 mesh->addMeshBuffer(buf);
785 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
787 // Set it to use the materials of the meshbuffers directly.
788 // This is needed for changing the texture in the future
789 m_meshnode->setReadOnlyMaterials(true);
791 else if(m_visual == "cube"){
792 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
793 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
794 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
797 m_meshnode->setScale(v3f(1));
798 // Will be shown when we know the brightness
799 m_meshnode->setVisible(false);
801 infostream<<"GenericCAO::addToScene(): \""<<m_visual
802 <<"\" not supported"<<std::endl;
806 scene::ISceneNode *node = NULL;
811 if(node && m_is_player && !m_is_local_player){
812 // Add a text node for showing the name
813 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
814 std::wstring wname = narrow_to_wide(m_name);
815 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
816 wname.c_str(), video::SColor(255,255,255,255), node);
817 m_textnode->setPosition(v3f(0, BS*1.1, 0));
825 m_visuals_expired = true;
828 void updateLight(u8 light_at_pos)
830 bool is_visible = (m_hp != 0);
831 u8 li = decode_light(light_at_pos);
832 video::SColor color(255,li,li,li);
834 setMeshColor(m_meshnode->getMesh(), color);
835 m_meshnode->setVisible(is_visible);
838 m_spritenode->setColor(color);
839 m_spritenode->setVisible(is_visible);
843 v3s16 getLightPosition()
845 return floatToInt(m_position, BS);
851 m_meshnode->setPosition(pos_translator.vect_show);
852 v3f rot = m_meshnode->getRotation();
854 m_meshnode->setRotation(rot);
857 m_spritenode->setPosition(pos_translator.vect_show);
861 void step(float dtime, ClientEnvironment *env)
863 v3f lastpos = pos_translator.vect_show;
865 if(m_visuals_expired && m_smgr && m_irr){
866 m_visuals_expired = false;
868 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
872 core::aabbox3d<f32> box = m_collisionbox;
875 collisionMoveResult moveresult;
876 f32 pos_max_d = BS*0.25; // Distance per iteration
877 v3f p_pos = m_position;
878 v3f p_velocity = m_velocity;
879 IGameDef *gamedef = env->getGameDef();
880 moveresult = collisionMovePrecise(&env->getMap(), gamedef,
881 pos_max_d, box, dtime, p_pos, p_velocity);
884 m_velocity = p_velocity;
886 bool is_end_position = moveresult.collides;
887 pos_translator.update(m_position, is_end_position, dtime);
888 pos_translator.translate(dtime);
891 m_velocity += dtime * m_acceleration;
893 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
894 m_velocity += dtime * m_acceleration;
895 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
896 pos_translator.translate(dtime);
900 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
901 m_step_distance_counter += moved;
902 if(m_step_distance_counter > 1.5*BS){
903 m_step_distance_counter = 0;
904 if(!m_is_local_player && m_makes_footstep_sound){
905 INodeDefManager *ndef = m_gamedef->ndef();
906 v3s16 p = floatToInt(getPosition()+v3f(0,-0.5*BS, 0), BS);
907 MapNode n = m_env->getMap().getNodeNoEx(p);
908 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
909 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
913 m_anim_timer += dtime;
914 if(m_anim_timer >= m_anim_framelength){
915 m_anim_timer -= m_anim_framelength;
917 if(m_anim_frame >= m_anim_num_frames)
923 if(m_reset_textures_timer >= 0){
924 m_reset_textures_timer -= dtime;
925 if(m_reset_textures_timer <= 0){
926 m_reset_textures_timer = -1;
932 void updateTexturePos()
935 scene::ICameraSceneNode* camera =
936 m_spritenode->getSceneManager()->getActiveCamera();
939 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
940 - camera->getAbsolutePosition();
941 cam_to_entity.normalize();
943 int row = m_tx_basepos.Y;
944 int col = m_tx_basepos.X;
946 if(m_tx_select_horiz_by_yawpitch)
948 if(cam_to_entity.Y > 0.75)
950 else if(cam_to_entity.Y < -0.75)
953 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / PI * 180.;
954 float dir = mob_dir - m_yaw;
955 dir = wrapDegrees_180(dir);
956 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
957 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
959 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
961 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
963 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
970 // Animation goes downwards
973 float txs = m_tx_size.X;
974 float tys = m_tx_size.Y;
975 setBillboardTextureMatrix(m_spritenode,
980 void updateTextures(const std::string &mod)
982 ITextureSource *tsrc = m_gamedef->tsrc();
986 if(m_visual == "sprite")
988 std::string texturestring = "unknown_block.png";
989 if(m_textures.size() >= 1)
990 texturestring = m_textures[0];
991 texturestring += mod;
992 m_spritenode->setMaterialTexture(0,
993 tsrc->getTextureRaw(texturestring));
998 if(m_visual == "cube")
1000 for (u32 i = 0; i < 6; ++i)
1002 std::string texturestring = "unknown_block.png";
1003 if(m_textures.size() > i)
1004 texturestring = m_textures[i];
1005 texturestring += mod;
1006 AtlasPointer ap = tsrc->getTexture(texturestring);
1008 // Get the tile texture and atlas transformation
1009 video::ITexture* atlas = ap.atlas;
1013 // Set material flags and texture
1014 video::SMaterial& material = m_meshnode->getMaterial(i);
1015 material.setFlag(video::EMF_LIGHTING, false);
1016 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1017 material.setTexture(0, atlas);
1018 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1019 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1022 else if(m_visual == "upright_sprite")
1024 scene::IMesh *mesh = m_meshnode->getMesh();
1026 std::string tname = "unknown_object.png";
1027 if(m_textures.size() >= 1)
1028 tname = m_textures[0];
1030 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1031 buf->getMaterial().setTexture(0,
1032 tsrc->getTextureRaw(tname));
1035 std::string tname = "unknown_object.png";
1036 if(m_textures.size() >= 2)
1037 tname = m_textures[1];
1038 else if(m_textures.size() >= 1)
1039 tname = m_textures[0];
1041 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1042 buf->getMaterial().setTexture(0,
1043 tsrc->getTextureRaw(tname));
1049 void processMessage(const std::string &data)
1051 //infostream<<"GenericCAO: Got message"<<std::endl;
1052 std::istringstream is(data, std::ios::binary);
1054 u8 cmd = readU8(is);
1055 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1057 m_hp_max = readS16(is);
1058 m_physical = readU8(is);
1059 m_weight = readF1000(is);
1060 m_collisionbox.MinEdge = readV3F1000(is);
1061 m_collisionbox.MaxEdge = readV3F1000(is);
1062 m_visual = deSerializeString(is);
1063 m_visual_size = readV2F1000(is);
1065 u32 texture_count = readU16(is);
1066 for(u32 i=0; i<texture_count; i++){
1067 m_textures.push_back(deSerializeString(is));
1069 m_spritediv = readV2S16(is);
1070 m_is_visible = readU8(is);
1071 m_makes_footstep_sound = readU8(is);
1073 m_selection_box = m_collisionbox;
1074 m_selection_box.MinEdge *= BS;
1075 m_selection_box.MaxEdge *= BS;
1077 m_tx_size.X = 1.0 / m_spritediv.X;
1078 m_tx_size.Y = 1.0 / m_spritediv.Y;
1082 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1084 m_position = readV3F1000(is);
1085 m_velocity = readV3F1000(is);
1086 m_acceleration = readV3F1000(is);
1087 m_yaw = readF1000(is);
1088 bool do_interpolate = readU8(is);
1089 bool is_end_position = readU8(is);
1090 float update_interval = readF1000(is);
1094 pos_translator.update(m_position, is_end_position, update_interval);
1096 pos_translator.init(m_position);
1100 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1102 std::string mod = deSerializeString(is);
1103 updateTextures(mod);
1105 else if(cmd == GENERIC_CMD_SET_SPRITE)
1107 v2s16 p = readV2S16(is);
1108 int num_frames = readU16(is);
1109 float framelength = readF1000(is);
1110 bool select_horiz_by_yawpitch = readU8(is);
1113 m_anim_num_frames = num_frames;
1114 m_anim_framelength = framelength;
1115 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1119 else if(cmd == GENERIC_CMD_PUNCHED)
1121 /*s16 damage =*/ readS16(is);
1122 s16 result_hp = readS16(is);
1126 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1128 m_armor_groups.clear();
1129 int armor_groups_size = readU16(is);
1130 for(int i=0; i<armor_groups_size; i++){
1131 std::string name = deSerializeString(is);
1132 int rating = readS16(is);
1133 m_armor_groups[name] = rating;
1138 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1139 float time_from_last_punch=1000000)
1142 const ToolCapabilities *toolcap =
1143 &punchitem->getToolCapabilities(m_gamedef->idef());
1144 PunchDamageResult result = getPunchDamage(
1148 time_from_last_punch);
1150 dstream<<"Directly did_punch="<<result.did_punch<<" result.damage="<<result.damage<<std::endl;
1152 if(result.did_punch && result.damage != 0)
1154 if(result.damage < m_hp){
1155 m_hp -= result.damage;
1158 // TODO: Execute defined fast response
1159 // As there is no definition, make a smoke puff
1160 ClientSimpleObject *simple = createSmokePuff(
1161 m_smgr, m_env, m_position,
1162 m_visual_size * BS);
1163 m_env->addSimpleObject(simple);
1165 // TODO: Execute defined fast response
1166 // Flashing shall suffice as there is no definition
1167 m_reset_textures_timer = 0.05;
1168 if(result.damage >= 2)
1169 m_reset_textures_timer += 0.05 * result.damage;
1170 updateTextures("^[brighten");
1176 std::string debugInfoText()
1178 std::ostringstream os(std::ios::binary);
1179 os<<"GenericCAO hp="<<m_hp<<"\n";
1181 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1182 i != m_armor_groups.end(); i++){
1183 os<<i->first<<"="<<i->second<<", ";
1191 GenericCAO proto_GenericCAO(NULL, NULL);