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"
39 #include "localplayer.h"
41 struct ToolCapabilities;
43 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
45 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
51 struct SmoothTranslator
58 f32 anim_time_counter;
78 anim_time_counter = 0;
87 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
89 aim_is_end = is_end_position;
92 if(update_interval > 0){
93 anim_time = update_interval;
95 if(anim_time < 0.001 || anim_time > 1.0)
96 anim_time = anim_time_counter;
98 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
100 anim_time_counter = 0;
104 void translate(f32 dtime)
106 anim_time_counter = anim_time_counter + dtime;
107 anim_counter = anim_counter + dtime;
108 v3f vect_move = vect_aim - vect_old;
110 if(anim_time > 0.001)
111 moveratio = anim_time_counter / anim_time;
112 // Move a bit less than should, to avoid oscillation
113 moveratio = moveratio * 0.8;
114 float move_end = 1.5;
117 if(moveratio > move_end)
118 moveratio = move_end;
119 vect_show = vect_old + vect_move * moveratio;
124 return ((anim_time_counter / anim_time) < 1.4);
132 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
133 float txs, float tys, int col, int row)
135 video::SMaterial& material = bill->getMaterial(0);
136 core::matrix4& matrix = material.getTextureMatrix(0);
137 matrix.setTextureTranslate(txs*col, tys*row);
138 matrix.setTextureScale(txs, tys);
145 class TestCAO : public ClientActiveObject
148 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
153 return ACTIVEOBJECT_TYPE_TEST;
156 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
158 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
159 IrrlichtDevice *irr);
160 void removeFromScene();
161 void updateLight(u8 light_at_pos);
162 v3s16 getLightPosition();
163 void updateNodePos();
165 void step(float dtime, ClientEnvironment *env);
167 void processMessage(const std::string &data);
170 scene::IMeshSceneNode *m_node;
175 TestCAO proto_TestCAO(NULL, NULL);
177 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
178 ClientActiveObject(0, gamedef, env),
180 m_position(v3f(0,10*BS,0))
182 ClientActiveObject::registerType(getType(), create);
189 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
191 return new TestCAO(gamedef, env);
194 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
200 //video::IVideoDriver* driver = smgr->getVideoDriver();
202 scene::SMesh *mesh = new scene::SMesh();
203 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
204 video::SColor c(255,255,255,255);
205 video::S3DVertex vertices[4] =
207 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
208 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
209 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
210 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
212 u16 indices[] = {0,1,2,2,3,0};
213 buf->append(vertices, 4, indices, 6);
215 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
216 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
217 buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
218 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
219 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
220 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
222 mesh->addMeshBuffer(buf);
224 m_node = smgr->addMeshSceneNode(mesh, NULL);
229 void TestCAO::removeFromScene()
238 void TestCAO::updateLight(u8 light_at_pos)
242 v3s16 TestCAO::getLightPosition()
244 return floatToInt(m_position, BS);
247 void TestCAO::updateNodePos()
252 m_node->setPosition(m_position);
253 //m_node->setRotation(v3f(0, 45, 0));
256 void TestCAO::step(float dtime, ClientEnvironment *env)
260 v3f rot = m_node->getRotation();
261 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
262 rot.Y += dtime * 180;
263 m_node->setRotation(rot);
267 void TestCAO::processMessage(const std::string &data)
269 infostream<<"TestCAO: Got data: "<<data<<std::endl;
270 std::istringstream is(data, std::ios::binary);
288 class ItemCAO : public ClientActiveObject
291 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
296 return ACTIVEOBJECT_TYPE_ITEM;
299 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
301 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
302 IrrlichtDevice *irr);
303 void removeFromScene();
304 void updateLight(u8 light_at_pos);
305 v3s16 getLightPosition();
306 void updateNodePos();
307 void updateInfoText();
308 void updateTexture();
310 void step(float dtime, ClientEnvironment *env);
312 void processMessage(const std::string &data);
314 void initialize(const std::string &data);
316 core::aabbox3d<f32>* getSelectionBox()
317 {return &m_selection_box;}
321 std::string infoText()
325 core::aabbox3d<f32> m_selection_box;
326 scene::IMeshSceneNode *m_node;
328 std::string m_itemstring;
329 std::string m_infotext;
332 #include "inventory.h"
335 ItemCAO proto_ItemCAO(NULL, NULL);
337 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
338 ClientActiveObject(0, gamedef, env),
339 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
341 m_position(v3f(0,10*BS,0))
345 ClientActiveObject::registerType(getType(), create);
353 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
355 return new ItemCAO(gamedef, env);
358 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
364 //video::IVideoDriver* driver = smgr->getVideoDriver();
366 scene::SMesh *mesh = new scene::SMesh();
367 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
368 video::SColor c(255,255,255,255);
369 video::S3DVertex vertices[4] =
371 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
372 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
373 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
374 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
375 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
376 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
377 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
378 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
380 u16 indices[] = {0,1,2,2,3,0};
381 buf->append(vertices, 4, indices, 6);
383 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
384 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
385 // Initialize with a generated placeholder texture
386 buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
387 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
388 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
389 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
391 mesh->addMeshBuffer(buf);
393 m_node = smgr->addMeshSceneNode(mesh, NULL);
404 void ItemCAO::removeFromScene()
413 void ItemCAO::updateLight(u8 light_at_pos)
418 u8 li = decode_light(light_at_pos);
419 video::SColor color(255,li,li,li);
420 setMeshColor(m_node->getMesh(), color);
423 v3s16 ItemCAO::getLightPosition()
425 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
428 void ItemCAO::updateNodePos()
433 m_node->setPosition(m_position);
436 void ItemCAO::updateInfoText()
439 IItemDefManager *idef = m_gamedef->idef();
441 item.deSerialize(m_itemstring, idef);
442 if(item.isKnown(idef))
443 m_infotext = item.getDefinition(idef).description;
445 m_infotext = "Unknown item: '" + m_itemstring + "'";
447 m_infotext += " (" + itos(item.count) + ")";
449 catch(SerializationError &e)
451 m_infotext = "Unknown item: '" + m_itemstring + "'";
455 void ItemCAO::updateTexture()
460 // Create an inventory item to see what is its image
461 std::istringstream is(m_itemstring, std::ios_base::binary);
462 video::ITexture *texture = NULL;
464 IItemDefManager *idef = m_gamedef->idef();
466 item.deSerialize(is, idef);
467 texture = item.getDefinition(idef).inventory_texture;
469 catch(SerializationError &e)
471 infostream<<"WARNING: "<<__FUNCTION_NAME
472 <<": error deSerializing itemstring \""
473 <<m_itemstring<<std::endl;
476 // Set meshbuffer texture
477 m_node->getMaterial(0).setTexture(0, texture);
481 void ItemCAO::step(float dtime, ClientEnvironment *env)
485 /*v3f rot = m_node->getRotation();
486 rot.Y += dtime * 120;
487 m_node->setRotation(rot);*/
488 LocalPlayer *player = env->getLocalPlayer();
490 v3f rot = m_node->getRotation();
491 rot.Y = 180.0 - (player->getYaw());
492 m_node->setRotation(rot);
496 void ItemCAO::processMessage(const std::string &data)
498 //infostream<<"ItemCAO: Got message"<<std::endl;
499 std::istringstream is(data, std::ios::binary);
505 m_position = readV3F1000(is);
511 m_itemstring = deSerializeString(is);
517 void ItemCAO::initialize(const std::string &data)
519 infostream<<"ItemCAO: Got init data"<<std::endl;
522 std::istringstream is(data, std::ios::binary);
524 u8 version = readU8(is);
529 m_position = readV3F1000(is);
531 m_itemstring = deSerializeString(is);
542 #include "genericobject.h"
544 class GenericCAO : public ClientActiveObject
547 // Only set at initialization
550 bool m_is_local_player; // determined locally
551 // Property-ish things
552 ObjectProperties m_prop;
554 scene::ISceneManager *m_smgr;
555 IrrlichtDevice *m_irr;
556 core::aabbox3d<f32> m_selection_box;
557 scene::IMeshSceneNode *m_meshnode;
558 scene::IBillboardSceneNode *m_spritenode;
559 scene::ITextSceneNode* m_textnode;
565 SmoothTranslator pos_translator;
566 // Spritesheet/animation stuff
569 bool m_initial_tx_basepos_set;
570 bool m_tx_select_horiz_by_yawpitch;
572 int m_anim_num_frames;
573 float m_anim_framelength;
575 ItemGroupList m_armor_groups;
576 float m_reset_textures_timer;
577 bool m_visuals_expired;
578 float m_step_distance_counter;
582 GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
583 ClientActiveObject(0, gamedef, env),
586 m_is_local_player(false),
590 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
594 m_position(v3f(0,10*BS,0)),
595 m_velocity(v3f(0,0,0)),
596 m_acceleration(v3f(0,0,0)),
601 m_initial_tx_basepos_set(false),
602 m_tx_select_horiz_by_yawpitch(false),
604 m_anim_num_frames(1),
605 m_anim_framelength(0.2),
607 m_reset_textures_timer(-1),
608 m_visuals_expired(false),
609 m_step_distance_counter(0),
613 ClientActiveObject::registerType(getType(), create);
616 void initialize(const std::string &data)
618 infostream<<"GenericCAO: Got init data"<<std::endl;
619 std::istringstream is(data, std::ios::binary);
621 u8 version = readU8(is);
624 errorstream<<"GenericCAO: Unsupported init data version"
628 m_name = deSerializeString(is);
629 m_is_player = readU8(is);
630 m_position = readV3F1000(is);
631 m_yaw = readF1000(is);
634 int num_messages = readU8(is);
635 for(int i=0; i<num_messages; i++){
636 std::string message = deSerializeLongString(is);
637 processMessage(message);
640 pos_translator.init(m_position);
644 Player *player = m_env->getPlayer(m_name.c_str());
645 if(player && player->isLocal()){
646 m_is_local_player = true;
655 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
657 return new GenericCAO(gamedef, env);
662 return ACTIVEOBJECT_TYPE_GENERIC;
664 core::aabbox3d<f32>* getSelectionBox()
666 if(!m_prop.is_visible || m_is_local_player)
668 return &m_selection_box;
672 return pos_translator.vect_show;
675 void removeFromScene()
678 m_meshnode->remove();
682 m_spritenode->remove();
687 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
693 if(m_meshnode != NULL || m_spritenode != NULL)
696 m_visuals_expired = false;
698 if(!m_prop.is_visible || m_is_local_player)
701 //video::IVideoDriver* driver = smgr->getVideoDriver();
703 if(m_prop.visual == "sprite"){
704 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
705 m_spritenode = smgr->addBillboardSceneNode(
706 NULL, v2f(1, 1), v3f(0,0,0), -1);
707 m_spritenode->setMaterialTexture(0,
708 tsrc->getTextureRaw("unknown_block.png"));
709 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
710 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
711 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
712 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
713 u8 li = m_last_light;
714 m_spritenode->setColor(video::SColor(255,li,li,li));
715 m_spritenode->setSize(m_prop.visual_size*BS);
717 const float txs = 1.0 / 1;
718 const float tys = 1.0 / 1;
719 setBillboardTextureMatrix(m_spritenode,
723 else if(m_prop.visual == "upright_sprite")
725 scene::SMesh *mesh = new scene::SMesh();
726 double dx = BS*m_prop.visual_size.X/2;
727 double dy = BS*m_prop.visual_size.Y/2;
729 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
730 u8 li = m_last_light;
731 video::SColor c(255,li,li,li);
732 video::S3DVertex vertices[4] =
734 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
735 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
736 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
737 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
739 u16 indices[] = {0,1,2,2,3,0};
740 buf->append(vertices, 4, indices, 6);
742 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
743 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
744 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
745 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
747 mesh->addMeshBuffer(buf);
751 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
752 u8 li = m_last_light;
753 video::SColor c(255,li,li,li);
754 video::S3DVertex vertices[4] =
756 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
757 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
758 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
759 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
761 u16 indices[] = {0,1,2,2,3,0};
762 buf->append(vertices, 4, indices, 6);
764 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
765 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
766 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
767 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
769 mesh->addMeshBuffer(buf);
772 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
774 // Set it to use the materials of the meshbuffers directly.
775 // This is needed for changing the texture in the future
776 m_meshnode->setReadOnlyMaterials(true);
778 else if(m_prop.visual == "cube"){
779 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
780 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
781 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
784 m_meshnode->setScale(v3f(1));
785 u8 li = m_last_light;
786 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
788 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
789 <<"\" not supported"<<std::endl;
793 scene::ISceneNode *node = NULL;
798 if(node && m_is_player && !m_is_local_player){
799 // Add a text node for showing the name
800 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
801 std::wstring wname = narrow_to_wide(m_name);
802 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
803 wname.c_str(), video::SColor(255,255,255,255), node);
804 m_textnode->setPosition(v3f(0, BS*1.1, 0));
812 m_visuals_expired = true;
815 void updateLight(u8 light_at_pos)
817 bool is_visible = (m_hp != 0);
818 u8 li = decode_light(light_at_pos);
820 video::SColor color(255,li,li,li);
822 setMeshColor(m_meshnode->getMesh(), color);
823 m_meshnode->setVisible(is_visible);
826 m_spritenode->setColor(color);
827 m_spritenode->setVisible(is_visible);
831 v3s16 getLightPosition()
833 return floatToInt(m_position, BS);
839 m_meshnode->setPosition(pos_translator.vect_show);
840 v3f rot = m_meshnode->getRotation();
842 m_meshnode->setRotation(rot);
845 m_spritenode->setPosition(pos_translator.vect_show);
849 void step(float dtime, ClientEnvironment *env)
851 v3f lastpos = pos_translator.vect_show;
853 if(m_visuals_expired && m_smgr && m_irr){
854 m_visuals_expired = false;
856 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
860 core::aabbox3d<f32> box = m_prop.collisionbox;
863 collisionMoveResult moveresult;
864 f32 pos_max_d = BS*0.25; // Distance per iteration
865 v3f p_pos = m_position;
866 v3f p_velocity = m_velocity;
867 IGameDef *gamedef = env->getGameDef();
868 moveresult = collisionMovePrecise(&env->getMap(), gamedef,
869 pos_max_d, box, dtime, p_pos, p_velocity);
872 m_velocity = p_velocity;
874 bool is_end_position = moveresult.collides;
875 pos_translator.update(m_position, is_end_position, dtime);
876 pos_translator.translate(dtime);
879 m_velocity += dtime * m_acceleration;
881 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
882 m_velocity += dtime * m_acceleration;
883 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
884 pos_translator.translate(dtime);
888 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
889 m_step_distance_counter += moved;
890 if(m_step_distance_counter > 1.5*BS){
891 m_step_distance_counter = 0;
892 if(!m_is_local_player && m_prop.makes_footstep_sound){
893 INodeDefManager *ndef = m_gamedef->ndef();
894 v3s16 p = floatToInt(getPosition() + v3f(0,
895 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
896 MapNode n = m_env->getMap().getNodeNoEx(p);
897 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
898 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
902 m_anim_timer += dtime;
903 if(m_anim_timer >= m_anim_framelength){
904 m_anim_timer -= m_anim_framelength;
906 if(m_anim_frame >= m_anim_num_frames)
912 if(m_reset_textures_timer >= 0){
913 m_reset_textures_timer -= dtime;
914 if(m_reset_textures_timer <= 0){
915 m_reset_textures_timer = -1;
921 void updateTexturePos()
924 scene::ICameraSceneNode* camera =
925 m_spritenode->getSceneManager()->getActiveCamera();
928 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
929 - camera->getAbsolutePosition();
930 cam_to_entity.normalize();
932 int row = m_tx_basepos.Y;
933 int col = m_tx_basepos.X;
935 if(m_tx_select_horiz_by_yawpitch)
937 if(cam_to_entity.Y > 0.75)
939 else if(cam_to_entity.Y < -0.75)
942 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / PI * 180.;
943 float dir = mob_dir - m_yaw;
944 dir = wrapDegrees_180(dir);
945 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
946 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
948 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
950 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
952 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
959 // Animation goes downwards
962 float txs = m_tx_size.X;
963 float tys = m_tx_size.Y;
964 setBillboardTextureMatrix(m_spritenode,
969 void updateTextures(const std::string &mod)
971 ITextureSource *tsrc = m_gamedef->tsrc();
975 if(m_prop.visual == "sprite")
977 std::string texturestring = "unknown_block.png";
978 if(m_prop.textures.size() >= 1)
979 texturestring = m_prop.textures[0];
980 texturestring += mod;
981 m_spritenode->setMaterialTexture(0,
982 tsrc->getTextureRaw(texturestring));
987 if(m_prop.visual == "cube")
989 for (u32 i = 0; i < 6; ++i)
991 std::string texturestring = "unknown_block.png";
992 if(m_prop.textures.size() > i)
993 texturestring = m_prop.textures[i];
994 texturestring += mod;
995 AtlasPointer ap = tsrc->getTexture(texturestring);
997 // Get the tile texture and atlas transformation
998 video::ITexture* atlas = ap.atlas;
1002 // Set material flags and texture
1003 video::SMaterial& material = m_meshnode->getMaterial(i);
1004 material.setFlag(video::EMF_LIGHTING, false);
1005 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1006 material.setTexture(0, atlas);
1007 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1008 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1011 else if(m_prop.visual == "upright_sprite")
1013 scene::IMesh *mesh = m_meshnode->getMesh();
1015 std::string tname = "unknown_object.png";
1016 if(m_prop.textures.size() >= 1)
1017 tname = m_prop.textures[0];
1019 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1020 buf->getMaterial().setTexture(0,
1021 tsrc->getTextureRaw(tname));
1024 std::string tname = "unknown_object.png";
1025 if(m_prop.textures.size() >= 2)
1026 tname = m_prop.textures[1];
1027 else if(m_prop.textures.size() >= 1)
1028 tname = m_prop.textures[0];
1030 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1031 buf->getMaterial().setTexture(0,
1032 tsrc->getTextureRaw(tname));
1038 void processMessage(const std::string &data)
1040 //infostream<<"GenericCAO: Got message"<<std::endl;
1041 std::istringstream is(data, std::ios::binary);
1043 u8 cmd = readU8(is);
1044 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1046 m_prop = gob_read_set_properties(is);
1048 m_selection_box = m_prop.collisionbox;
1049 m_selection_box.MinEdge *= BS;
1050 m_selection_box.MaxEdge *= BS;
1052 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1053 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1055 if(!m_initial_tx_basepos_set){
1056 m_initial_tx_basepos_set = true;
1057 m_tx_basepos = m_prop.initial_sprite_basepos;
1062 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1064 m_position = readV3F1000(is);
1065 m_velocity = readV3F1000(is);
1066 m_acceleration = readV3F1000(is);
1067 m_yaw = readF1000(is);
1068 bool do_interpolate = readU8(is);
1069 bool is_end_position = readU8(is);
1070 float update_interval = readF1000(is);
1072 // Place us a bit higher if we're physical, to not sink into
1073 // the ground due to sucky collision detection...
1075 m_position += v3f(0,0.002,0);
1078 if(!m_prop.physical)
1079 pos_translator.update(m_position, is_end_position, update_interval);
1081 pos_translator.init(m_position);
1085 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1087 std::string mod = deSerializeString(is);
1088 updateTextures(mod);
1090 else if(cmd == GENERIC_CMD_SET_SPRITE)
1092 v2s16 p = readV2S16(is);
1093 int num_frames = readU16(is);
1094 float framelength = readF1000(is);
1095 bool select_horiz_by_yawpitch = readU8(is);
1098 m_anim_num_frames = num_frames;
1099 m_anim_framelength = framelength;
1100 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1104 else if(cmd == GENERIC_CMD_PUNCHED)
1106 /*s16 damage =*/ readS16(is);
1107 s16 result_hp = readS16(is);
1111 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1113 m_armor_groups.clear();
1114 int armor_groups_size = readU16(is);
1115 for(int i=0; i<armor_groups_size; i++){
1116 std::string name = deSerializeString(is);
1117 int rating = readS16(is);
1118 m_armor_groups[name] = rating;
1123 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1124 float time_from_last_punch=1000000)
1127 const ToolCapabilities *toolcap =
1128 &punchitem->getToolCapabilities(m_gamedef->idef());
1129 PunchDamageResult result = getPunchDamage(
1133 time_from_last_punch);
1135 if(result.did_punch && result.damage != 0)
1137 if(result.damage < m_hp){
1138 m_hp -= result.damage;
1141 // TODO: Execute defined fast response
1142 // As there is no definition, make a smoke puff
1143 ClientSimpleObject *simple = createSmokePuff(
1144 m_smgr, m_env, m_position,
1145 m_prop.visual_size * BS);
1146 m_env->addSimpleObject(simple);
1148 // TODO: Execute defined fast response
1149 // Flashing shall suffice as there is no definition
1150 m_reset_textures_timer = 0.05;
1151 if(result.damage >= 2)
1152 m_reset_textures_timer += 0.05 * result.damage;
1153 updateTextures("^[brighten");
1159 std::string debugInfoText()
1161 std::ostringstream os(std::ios::binary);
1162 os<<"GenericCAO hp="<<m_hp<<"\n";
1164 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1165 i != m_armor_groups.end(); i++){
1166 os<<i->first<<"="<<i->second<<", ";
1174 GenericCAO proto_GenericCAO(NULL, NULL);