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"
43 struct ToolCapabilities;
45 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
47 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
53 struct SmoothTranslator
60 f32 anim_time_counter;
80 anim_time_counter = 0;
89 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
91 aim_is_end = is_end_position;
94 if(update_interval > 0){
95 anim_time = update_interval;
97 if(anim_time < 0.001 || anim_time > 1.0)
98 anim_time = anim_time_counter;
100 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
102 anim_time_counter = 0;
106 void translate(f32 dtime)
108 anim_time_counter = anim_time_counter + dtime;
109 anim_counter = anim_counter + dtime;
110 v3f vect_move = vect_aim - vect_old;
112 if(anim_time > 0.001)
113 moveratio = anim_time_counter / anim_time;
114 // Move a bit less than should, to avoid oscillation
115 moveratio = moveratio * 0.8;
116 float move_end = 1.5;
119 if(moveratio > move_end)
120 moveratio = move_end;
121 vect_show = vect_old + vect_move * moveratio;
126 return ((anim_time_counter / anim_time) < 1.4);
134 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
135 float txs, float tys, int col, int row)
137 video::SMaterial& material = bill->getMaterial(0);
138 core::matrix4& matrix = material.getTextureMatrix(0);
139 matrix.setTextureTranslate(txs*col, tys*row);
140 matrix.setTextureScale(txs, tys);
147 class TestCAO : public ClientActiveObject
150 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
155 return ACTIVEOBJECT_TYPE_TEST;
158 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
160 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
161 IrrlichtDevice *irr);
162 void removeFromScene();
163 void updateLight(u8 light_at_pos);
164 v3s16 getLightPosition();
165 void updateNodePos();
167 void step(float dtime, ClientEnvironment *env);
169 void processMessage(const std::string &data);
172 scene::IMeshSceneNode *m_node;
177 TestCAO proto_TestCAO(NULL, NULL);
179 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
180 ClientActiveObject(0, gamedef, env),
182 m_position(v3f(0,10*BS,0))
184 ClientActiveObject::registerType(getType(), create);
191 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
193 return new TestCAO(gamedef, env);
196 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
202 //video::IVideoDriver* driver = smgr->getVideoDriver();
204 scene::SMesh *mesh = new scene::SMesh();
205 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
206 video::SColor c(255,255,255,255);
207 video::S3DVertex vertices[4] =
209 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
210 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
211 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
212 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
214 u16 indices[] = {0,1,2,2,3,0};
215 buf->append(vertices, 4, indices, 6);
217 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
218 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
219 buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
220 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
221 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
222 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
224 mesh->addMeshBuffer(buf);
226 m_node = smgr->addMeshSceneNode(mesh, NULL);
231 void TestCAO::removeFromScene()
240 void TestCAO::updateLight(u8 light_at_pos)
244 v3s16 TestCAO::getLightPosition()
246 return floatToInt(m_position, BS);
249 void TestCAO::updateNodePos()
254 m_node->setPosition(m_position);
255 //m_node->setRotation(v3f(0, 45, 0));
258 void TestCAO::step(float dtime, ClientEnvironment *env)
262 v3f rot = m_node->getRotation();
263 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
264 rot.Y += dtime * 180;
265 m_node->setRotation(rot);
269 void TestCAO::processMessage(const std::string &data)
271 infostream<<"TestCAO: Got data: "<<data<<std::endl;
272 std::istringstream is(data, std::ios::binary);
290 class ItemCAO : public ClientActiveObject
293 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
298 return ACTIVEOBJECT_TYPE_ITEM;
301 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
303 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
304 IrrlichtDevice *irr);
305 void removeFromScene();
306 void updateLight(u8 light_at_pos);
307 v3s16 getLightPosition();
308 void updateNodePos();
309 void updateInfoText();
310 void updateTexture();
312 void step(float dtime, ClientEnvironment *env);
314 void processMessage(const std::string &data);
316 void initialize(const std::string &data);
318 core::aabbox3d<f32>* getSelectionBox()
319 {return &m_selection_box;}
323 std::string infoText()
327 core::aabbox3d<f32> m_selection_box;
328 scene::IMeshSceneNode *m_node;
330 std::string m_itemstring;
331 std::string m_infotext;
334 #include "inventory.h"
337 ItemCAO proto_ItemCAO(NULL, NULL);
339 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
340 ClientActiveObject(0, gamedef, env),
341 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
343 m_position(v3f(0,10*BS,0))
347 ClientActiveObject::registerType(getType(), create);
355 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
357 return new ItemCAO(gamedef, env);
360 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
366 //video::IVideoDriver* driver = smgr->getVideoDriver();
368 scene::SMesh *mesh = new scene::SMesh();
369 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
370 video::SColor c(255,255,255,255);
371 video::S3DVertex vertices[4] =
373 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
374 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
375 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
376 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
377 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
378 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
379 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
380 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
382 u16 indices[] = {0,1,2,2,3,0};
383 buf->append(vertices, 4, indices, 6);
385 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
386 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
387 // Initialize with a generated placeholder texture
388 buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
389 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
390 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
391 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
393 mesh->addMeshBuffer(buf);
395 m_node = smgr->addMeshSceneNode(mesh, NULL);
406 void ItemCAO::removeFromScene()
415 void ItemCAO::updateLight(u8 light_at_pos)
420 u8 li = decode_light(light_at_pos);
421 video::SColor color(255,li,li,li);
422 setMeshColor(m_node->getMesh(), color);
425 v3s16 ItemCAO::getLightPosition()
427 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
430 void ItemCAO::updateNodePos()
435 m_node->setPosition(m_position);
438 void ItemCAO::updateInfoText()
441 IItemDefManager *idef = m_gamedef->idef();
443 item.deSerialize(m_itemstring, idef);
444 if(item.isKnown(idef))
445 m_infotext = item.getDefinition(idef).description;
447 m_infotext = "Unknown item: '" + m_itemstring + "'";
449 m_infotext += " (" + itos(item.count) + ")";
451 catch(SerializationError &e)
453 m_infotext = "Unknown item: '" + m_itemstring + "'";
457 void ItemCAO::updateTexture()
462 // Create an inventory item to see what is its image
463 std::istringstream is(m_itemstring, std::ios_base::binary);
464 video::ITexture *texture = NULL;
466 IItemDefManager *idef = m_gamedef->idef();
468 item.deSerialize(is, idef);
469 texture = item.getDefinition(idef).inventory_texture;
471 catch(SerializationError &e)
473 infostream<<"WARNING: "<<__FUNCTION_NAME
474 <<": error deSerializing itemstring \""
475 <<m_itemstring<<std::endl;
478 // Set meshbuffer texture
479 m_node->getMaterial(0).setTexture(0, texture);
483 void ItemCAO::step(float dtime, ClientEnvironment *env)
487 /*v3f rot = m_node->getRotation();
488 rot.Y += dtime * 120;
489 m_node->setRotation(rot);*/
490 LocalPlayer *player = env->getLocalPlayer();
492 v3f rot = m_node->getRotation();
493 rot.Y = 180.0 - (player->getYaw());
494 m_node->setRotation(rot);
498 void ItemCAO::processMessage(const std::string &data)
500 //infostream<<"ItemCAO: Got message"<<std::endl;
501 std::istringstream is(data, std::ios::binary);
507 m_position = readV3F1000(is);
513 m_itemstring = deSerializeString(is);
519 void ItemCAO::initialize(const std::string &data)
521 infostream<<"ItemCAO: Got init data"<<std::endl;
524 std::istringstream is(data, std::ios::binary);
526 u8 version = readU8(is);
531 m_position = readV3F1000(is);
533 m_itemstring = deSerializeString(is);
544 #include "genericobject.h"
546 class GenericCAO : public ClientActiveObject
549 // Only set at initialization
552 bool m_is_local_player; // determined locally
553 // Property-ish things
554 ObjectProperties m_prop;
556 scene::ISceneManager *m_smgr;
557 IrrlichtDevice *m_irr;
558 core::aabbox3d<f32> m_selection_box;
559 scene::IMeshSceneNode *m_meshnode;
560 scene::IBillboardSceneNode *m_spritenode;
561 scene::ITextSceneNode* m_textnode;
567 SmoothTranslator pos_translator;
568 // Spritesheet/animation stuff
571 bool m_initial_tx_basepos_set;
572 bool m_tx_select_horiz_by_yawpitch;
574 int m_anim_num_frames;
575 float m_anim_framelength;
577 ItemGroupList m_armor_groups;
578 float m_reset_textures_timer;
579 bool m_visuals_expired;
580 float m_step_distance_counter;
584 GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
585 ClientActiveObject(0, gamedef, env),
588 m_is_local_player(false),
592 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
596 m_position(v3f(0,10*BS,0)),
597 m_velocity(v3f(0,0,0)),
598 m_acceleration(v3f(0,0,0)),
603 m_initial_tx_basepos_set(false),
604 m_tx_select_horiz_by_yawpitch(false),
606 m_anim_num_frames(1),
607 m_anim_framelength(0.2),
609 m_reset_textures_timer(-1),
610 m_visuals_expired(false),
611 m_step_distance_counter(0),
615 ClientActiveObject::registerType(getType(), create);
618 void initialize(const std::string &data)
620 infostream<<"GenericCAO: Got init data"<<std::endl;
621 std::istringstream is(data, std::ios::binary);
623 u8 version = readU8(is);
626 errorstream<<"GenericCAO: Unsupported init data version"
630 m_name = deSerializeString(is);
631 m_is_player = readU8(is);
632 m_position = readV3F1000(is);
633 m_yaw = readF1000(is);
636 int num_messages = readU8(is);
637 for(int i=0; i<num_messages; i++){
638 std::string message = deSerializeLongString(is);
639 processMessage(message);
642 pos_translator.init(m_position);
646 Player *player = m_env->getPlayer(m_name.c_str());
647 if(player && player->isLocal()){
648 m_is_local_player = true;
657 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
659 return new GenericCAO(gamedef, env);
664 return ACTIVEOBJECT_TYPE_GENERIC;
666 core::aabbox3d<f32>* getSelectionBox()
668 if(!m_prop.is_visible || m_is_local_player)
670 return &m_selection_box;
674 return pos_translator.vect_show;
677 void removeFromScene()
680 m_meshnode->remove();
684 m_spritenode->remove();
689 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
695 if(m_meshnode != NULL || m_spritenode != NULL)
698 m_visuals_expired = false;
700 if(!m_prop.is_visible || m_is_local_player)
703 //video::IVideoDriver* driver = smgr->getVideoDriver();
705 if(m_prop.visual == "sprite"){
706 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
707 m_spritenode = smgr->addBillboardSceneNode(
708 NULL, v2f(1, 1), v3f(0,0,0), -1);
709 m_spritenode->setMaterialTexture(0,
710 tsrc->getTextureRaw("unknown_block.png"));
711 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
712 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
713 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
714 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
715 u8 li = m_last_light;
716 m_spritenode->setColor(video::SColor(255,li,li,li));
717 m_spritenode->setSize(m_prop.visual_size*BS);
719 const float txs = 1.0 / 1;
720 const float tys = 1.0 / 1;
721 setBillboardTextureMatrix(m_spritenode,
725 else if(m_prop.visual == "upright_sprite")
727 scene::SMesh *mesh = new scene::SMesh();
728 double dx = BS*m_prop.visual_size.X/2;
729 double dy = BS*m_prop.visual_size.Y/2;
731 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
732 u8 li = m_last_light;
733 video::SColor c(255,li,li,li);
734 video::S3DVertex vertices[4] =
736 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
737 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
738 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
739 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
741 u16 indices[] = {0,1,2,2,3,0};
742 buf->append(vertices, 4, indices, 6);
744 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
745 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
746 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
747 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
749 mesh->addMeshBuffer(buf);
753 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
754 u8 li = m_last_light;
755 video::SColor c(255,li,li,li);
756 video::S3DVertex vertices[4] =
758 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
759 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
760 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
761 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
763 u16 indices[] = {0,1,2,2,3,0};
764 buf->append(vertices, 4, indices, 6);
766 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
767 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
768 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
769 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
771 mesh->addMeshBuffer(buf);
774 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
776 // Set it to use the materials of the meshbuffers directly.
777 // This is needed for changing the texture in the future
778 m_meshnode->setReadOnlyMaterials(true);
780 else if(m_prop.visual == "cube"){
781 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
782 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
783 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
786 m_meshnode->setScale(v3f(m_prop.visual_size.X,
787 m_prop.visual_size.Y,
788 m_prop.visual_size.X));
789 u8 li = m_last_light;
790 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
791 } else if(m_prop.visual == "wielditem"){
792 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
793 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
794 if(m_prop.textures.size() >= 1){
795 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
796 IItemDefManager *idef = m_gamedef->idef();
797 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
798 scene::IMesh *mesh = item.getDefinition(idef).wield_mesh;
799 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
801 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
802 m_prop.visual_size.Y/2,
803 m_prop.visual_size.X/2));
804 u8 li = m_last_light;
805 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
808 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
809 <<"\" not supported"<<std::endl;
813 scene::ISceneNode *node = NULL;
818 if(node && m_is_player && !m_is_local_player){
819 // Add a text node for showing the name
820 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
821 std::wstring wname = narrow_to_wide(m_name);
822 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
823 wname.c_str(), video::SColor(255,255,255,255), node);
824 m_textnode->setPosition(v3f(0, BS*1.1, 0));
832 m_visuals_expired = true;
835 void updateLight(u8 light_at_pos)
837 bool is_visible = (m_hp != 0);
838 u8 li = decode_light(light_at_pos);
840 video::SColor color(255,li,li,li);
842 setMeshColor(m_meshnode->getMesh(), color);
843 m_meshnode->setVisible(is_visible);
846 m_spritenode->setColor(color);
847 m_spritenode->setVisible(is_visible);
851 v3s16 getLightPosition()
853 return floatToInt(m_position, BS);
859 m_meshnode->setPosition(pos_translator.vect_show);
860 v3f rot = m_meshnode->getRotation();
862 m_meshnode->setRotation(rot);
865 m_spritenode->setPosition(pos_translator.vect_show);
869 void step(float dtime, ClientEnvironment *env)
871 v3f lastpos = pos_translator.vect_show;
873 if(m_visuals_expired && m_smgr && m_irr){
874 m_visuals_expired = false;
876 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
880 core::aabbox3d<f32> box = m_prop.collisionbox;
883 collisionMoveResult moveresult;
884 f32 pos_max_d = BS*0.125; // Distance per iteration
886 v3f p_pos = m_position;
887 v3f p_velocity = m_velocity;
888 v3f p_acceleration = m_acceleration;
889 IGameDef *gamedef = env->getGameDef();
890 moveresult = collisionMoveSimple(&env->getMap(), gamedef,
891 pos_max_d, box, stepheight, dtime,
892 p_pos, p_velocity, p_acceleration);
895 m_velocity = p_velocity;
896 m_acceleration = p_acceleration;
898 bool is_end_position = moveresult.collides;
899 pos_translator.update(m_position, is_end_position, dtime);
900 pos_translator.translate(dtime);
903 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
904 m_velocity += dtime * m_acceleration;
905 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
906 pos_translator.translate(dtime);
910 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
911 m_step_distance_counter += moved;
912 if(m_step_distance_counter > 1.5*BS){
913 m_step_distance_counter = 0;
914 if(!m_is_local_player && m_prop.makes_footstep_sound){
915 INodeDefManager *ndef = m_gamedef->ndef();
916 v3s16 p = floatToInt(getPosition() + v3f(0,
917 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
918 MapNode n = m_env->getMap().getNodeNoEx(p);
919 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
920 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
924 m_anim_timer += dtime;
925 if(m_anim_timer >= m_anim_framelength){
926 m_anim_timer -= m_anim_framelength;
928 if(m_anim_frame >= m_anim_num_frames)
934 if(m_reset_textures_timer >= 0){
935 m_reset_textures_timer -= dtime;
936 if(m_reset_textures_timer <= 0){
937 m_reset_textures_timer = -1;
941 if(fabs(m_prop.automatic_rotate) > 0.001){
942 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
947 void updateTexturePos()
950 scene::ICameraSceneNode* camera =
951 m_spritenode->getSceneManager()->getActiveCamera();
954 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
955 - camera->getAbsolutePosition();
956 cam_to_entity.normalize();
958 int row = m_tx_basepos.Y;
959 int col = m_tx_basepos.X;
961 if(m_tx_select_horiz_by_yawpitch)
963 if(cam_to_entity.Y > 0.75)
965 else if(cam_to_entity.Y < -0.75)
968 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
969 float dir = mob_dir - m_yaw;
970 dir = wrapDegrees_180(dir);
971 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
972 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
974 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
976 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
978 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
985 // Animation goes downwards
988 float txs = m_tx_size.X;
989 float tys = m_tx_size.Y;
990 setBillboardTextureMatrix(m_spritenode,
995 void updateTextures(const std::string &mod)
997 ITextureSource *tsrc = m_gamedef->tsrc();
1001 if(m_prop.visual == "sprite")
1003 std::string texturestring = "unknown_block.png";
1004 if(m_prop.textures.size() >= 1)
1005 texturestring = m_prop.textures[0];
1006 texturestring += mod;
1007 m_spritenode->setMaterialTexture(0,
1008 tsrc->getTextureRaw(texturestring));
1013 if(m_prop.visual == "cube")
1015 for (u32 i = 0; i < 6; ++i)
1017 std::string texturestring = "unknown_block.png";
1018 if(m_prop.textures.size() > i)
1019 texturestring = m_prop.textures[i];
1020 texturestring += mod;
1021 AtlasPointer ap = tsrc->getTexture(texturestring);
1023 // Get the tile texture and atlas transformation
1024 video::ITexture* atlas = ap.atlas;
1028 // Set material flags and texture
1029 video::SMaterial& material = m_meshnode->getMaterial(i);
1030 material.setFlag(video::EMF_LIGHTING, false);
1031 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1032 material.setTexture(0, atlas);
1033 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1034 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1037 else if(m_prop.visual == "upright_sprite")
1039 scene::IMesh *mesh = m_meshnode->getMesh();
1041 std::string tname = "unknown_object.png";
1042 if(m_prop.textures.size() >= 1)
1043 tname = m_prop.textures[0];
1045 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1046 buf->getMaterial().setTexture(0,
1047 tsrc->getTextureRaw(tname));
1050 std::string tname = "unknown_object.png";
1051 if(m_prop.textures.size() >= 2)
1052 tname = m_prop.textures[1];
1053 else if(m_prop.textures.size() >= 1)
1054 tname = m_prop.textures[0];
1056 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1057 buf->getMaterial().setTexture(0,
1058 tsrc->getTextureRaw(tname));
1064 void processMessage(const std::string &data)
1066 //infostream<<"GenericCAO: Got message"<<std::endl;
1067 std::istringstream is(data, std::ios::binary);
1069 u8 cmd = readU8(is);
1070 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1072 m_prop = gob_read_set_properties(is);
1074 m_selection_box = m_prop.collisionbox;
1075 m_selection_box.MinEdge *= BS;
1076 m_selection_box.MaxEdge *= BS;
1078 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1079 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1081 if(!m_initial_tx_basepos_set){
1082 m_initial_tx_basepos_set = true;
1083 m_tx_basepos = m_prop.initial_sprite_basepos;
1088 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1090 m_position = readV3F1000(is);
1091 m_velocity = readV3F1000(is);
1092 m_acceleration = readV3F1000(is);
1093 if(fabs(m_prop.automatic_rotate) < 0.001)
1094 m_yaw = readF1000(is);
1095 bool do_interpolate = readU8(is);
1096 bool is_end_position = readU8(is);
1097 float update_interval = readF1000(is);
1099 // Place us a bit higher if we're physical, to not sink into
1100 // the ground due to sucky collision detection...
1102 m_position += v3f(0,0.002,0);
1105 if(!m_prop.physical)
1106 pos_translator.update(m_position, is_end_position, update_interval);
1108 pos_translator.init(m_position);
1112 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1114 std::string mod = deSerializeString(is);
1115 updateTextures(mod);
1117 else if(cmd == GENERIC_CMD_SET_SPRITE)
1119 v2s16 p = readV2S16(is);
1120 int num_frames = readU16(is);
1121 float framelength = readF1000(is);
1122 bool select_horiz_by_yawpitch = readU8(is);
1125 m_anim_num_frames = num_frames;
1126 m_anim_framelength = framelength;
1127 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1131 else if(cmd == GENERIC_CMD_PUNCHED)
1133 /*s16 damage =*/ readS16(is);
1134 s16 result_hp = readS16(is);
1138 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1140 m_armor_groups.clear();
1141 int armor_groups_size = readU16(is);
1142 for(int i=0; i<armor_groups_size; i++){
1143 std::string name = deSerializeString(is);
1144 int rating = readS16(is);
1145 m_armor_groups[name] = rating;
1150 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1151 float time_from_last_punch=1000000)
1154 const ToolCapabilities *toolcap =
1155 &punchitem->getToolCapabilities(m_gamedef->idef());
1156 PunchDamageResult result = getPunchDamage(
1160 time_from_last_punch);
1162 if(result.did_punch && result.damage != 0)
1164 if(result.damage < m_hp){
1165 m_hp -= result.damage;
1168 // TODO: Execute defined fast response
1169 // As there is no definition, make a smoke puff
1170 ClientSimpleObject *simple = createSmokePuff(
1171 m_smgr, m_env, m_position,
1172 m_prop.visual_size * BS);
1173 m_env->addSimpleObject(simple);
1175 // TODO: Execute defined fast response
1176 // Flashing shall suffice as there is no definition
1177 m_reset_textures_timer = 0.05;
1178 if(result.damage >= 2)
1179 m_reset_textures_timer += 0.05 * result.damage;
1180 updateTextures("^[brighten");
1186 std::string debugInfoText()
1188 std::ostringstream os(std::ios::binary);
1189 os<<"GenericCAO hp="<<m_hp<<"\n";
1191 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1192 i != m_armor_groups.end(); i++){
1193 os<<i->first<<"="<<i->second<<", ";
1201 GenericCAO proto_GenericCAO(NULL, NULL);