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"
38 struct ToolCapabilities;
40 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
46 struct SmoothTranslator
53 f32 anim_time_counter;
73 anim_time_counter = 0;
82 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
84 aim_is_end = is_end_position;
87 if(update_interval > 0){
88 anim_time = update_interval;
90 if(anim_time < 0.001 || anim_time > 1.0)
91 anim_time = anim_time_counter;
93 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
95 anim_time_counter = 0;
99 void translate(f32 dtime)
101 anim_time_counter = anim_time_counter + dtime;
102 anim_counter = anim_counter + dtime;
103 v3f vect_move = vect_aim - vect_old;
105 if(anim_time > 0.001)
106 moveratio = anim_time_counter / anim_time;
107 // Move a bit less than should, to avoid oscillation
108 moveratio = moveratio * 0.8;
109 float move_end = 1.5;
112 if(moveratio > move_end)
113 moveratio = move_end;
114 vect_show = vect_old + vect_move * moveratio;
119 return ((anim_time_counter / anim_time) < 1.4);
127 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
128 float txs, float tys, int col, int row)
130 video::SMaterial& material = bill->getMaterial(0);
131 core::matrix4& matrix = material.getTextureMatrix(0);
132 matrix.setTextureTranslate(txs*col, tys*row);
133 matrix.setTextureScale(txs, tys);
140 class TestCAO : public ClientActiveObject
143 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
148 return ACTIVEOBJECT_TYPE_TEST;
151 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
153 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
154 IrrlichtDevice *irr);
155 void removeFromScene();
156 void updateLight(u8 light_at_pos);
157 v3s16 getLightPosition();
158 void updateNodePos();
160 void step(float dtime, ClientEnvironment *env);
162 void processMessage(const std::string &data);
165 scene::IMeshSceneNode *m_node;
170 TestCAO proto_TestCAO(NULL, NULL);
172 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
173 ClientActiveObject(0, gamedef, env),
175 m_position(v3f(0,10*BS,0))
177 ClientActiveObject::registerType(getType(), create);
184 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
186 return new TestCAO(gamedef, env);
189 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
195 //video::IVideoDriver* driver = smgr->getVideoDriver();
197 scene::SMesh *mesh = new scene::SMesh();
198 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
199 video::SColor c(255,255,255,255);
200 video::S3DVertex vertices[4] =
202 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
203 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
204 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
205 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
207 u16 indices[] = {0,1,2,2,3,0};
208 buf->append(vertices, 4, indices, 6);
210 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
211 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
212 buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
213 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
214 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
215 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
217 mesh->addMeshBuffer(buf);
219 m_node = smgr->addMeshSceneNode(mesh, NULL);
224 void TestCAO::removeFromScene()
233 void TestCAO::updateLight(u8 light_at_pos)
237 v3s16 TestCAO::getLightPosition()
239 return floatToInt(m_position, BS);
242 void TestCAO::updateNodePos()
247 m_node->setPosition(m_position);
248 //m_node->setRotation(v3f(0, 45, 0));
251 void TestCAO::step(float dtime, ClientEnvironment *env)
255 v3f rot = m_node->getRotation();
256 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
257 rot.Y += dtime * 180;
258 m_node->setRotation(rot);
262 void TestCAO::processMessage(const std::string &data)
264 infostream<<"TestCAO: Got data: "<<data<<std::endl;
265 std::istringstream is(data, std::ios::binary);
283 class ItemCAO : public ClientActiveObject
286 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
291 return ACTIVEOBJECT_TYPE_ITEM;
294 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
296 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
297 IrrlichtDevice *irr);
298 void removeFromScene();
299 void updateLight(u8 light_at_pos);
300 v3s16 getLightPosition();
301 void updateNodePos();
302 void updateInfoText();
303 void updateTexture();
305 void step(float dtime, ClientEnvironment *env);
307 void processMessage(const std::string &data);
309 void initialize(const std::string &data);
311 core::aabbox3d<f32>* getSelectionBox()
312 {return &m_selection_box;}
316 std::string infoText()
320 core::aabbox3d<f32> m_selection_box;
321 scene::IMeshSceneNode *m_node;
323 std::string m_itemstring;
324 std::string m_infotext;
327 #include "inventory.h"
330 ItemCAO proto_ItemCAO(NULL, NULL);
332 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
333 ClientActiveObject(0, gamedef, env),
334 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
336 m_position(v3f(0,10*BS,0))
340 ClientActiveObject::registerType(getType(), create);
348 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
350 return new ItemCAO(gamedef, env);
353 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
359 //video::IVideoDriver* driver = smgr->getVideoDriver();
361 scene::SMesh *mesh = new scene::SMesh();
362 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
363 video::SColor c(255,255,255,255);
364 video::S3DVertex vertices[4] =
366 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
367 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
368 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
369 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
370 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
371 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
372 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
373 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
375 u16 indices[] = {0,1,2,2,3,0};
376 buf->append(vertices, 4, indices, 6);
378 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
379 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
380 // Initialize with a generated placeholder texture
381 buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
382 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
383 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
384 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
386 mesh->addMeshBuffer(buf);
388 m_node = smgr->addMeshSceneNode(mesh, NULL);
399 void ItemCAO::removeFromScene()
408 void ItemCAO::updateLight(u8 light_at_pos)
413 u8 li = decode_light(light_at_pos);
414 video::SColor color(255,li,li,li);
415 setMeshColor(m_node->getMesh(), color);
418 v3s16 ItemCAO::getLightPosition()
420 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
423 void ItemCAO::updateNodePos()
428 m_node->setPosition(m_position);
431 void ItemCAO::updateInfoText()
434 IItemDefManager *idef = m_gamedef->idef();
436 item.deSerialize(m_itemstring, idef);
437 if(item.isKnown(idef))
438 m_infotext = item.getDefinition(idef).description;
440 m_infotext = "Unknown item: '" + m_itemstring + "'";
442 m_infotext += " (" + itos(item.count) + ")";
444 catch(SerializationError &e)
446 m_infotext = "Unknown item: '" + m_itemstring + "'";
450 void ItemCAO::updateTexture()
455 // Create an inventory item to see what is its image
456 std::istringstream is(m_itemstring, std::ios_base::binary);
457 video::ITexture *texture = NULL;
459 IItemDefManager *idef = m_gamedef->idef();
461 item.deSerialize(is, idef);
462 texture = item.getDefinition(idef).inventory_texture;
464 catch(SerializationError &e)
466 infostream<<"WARNING: "<<__FUNCTION_NAME
467 <<": error deSerializing itemstring \""
468 <<m_itemstring<<std::endl;
471 // Set meshbuffer texture
472 m_node->getMaterial(0).setTexture(0, texture);
476 void ItemCAO::step(float dtime, ClientEnvironment *env)
480 /*v3f rot = m_node->getRotation();
481 rot.Y += dtime * 120;
482 m_node->setRotation(rot);*/
483 LocalPlayer *player = env->getLocalPlayer();
485 v3f rot = m_node->getRotation();
486 rot.Y = 180.0 - (player->getYaw());
487 m_node->setRotation(rot);
491 void ItemCAO::processMessage(const std::string &data)
493 //infostream<<"ItemCAO: Got message"<<std::endl;
494 std::istringstream is(data, std::ios::binary);
500 m_position = readV3F1000(is);
506 m_itemstring = deSerializeString(is);
512 void ItemCAO::initialize(const std::string &data)
514 infostream<<"ItemCAO: Got init data"<<std::endl;
517 std::istringstream is(data, std::ios::binary);
519 u8 version = readU8(is);
524 m_position = readV3F1000(is);
526 m_itemstring = deSerializeString(is);
537 #include "luaentity_common.h"
539 class LuaEntityCAO : public ClientActiveObject
542 scene::ISceneManager *m_smgr;
543 core::aabbox3d<f32> m_selection_box;
544 scene::IMeshSceneNode *m_meshnode;
545 scene::IBillboardSceneNode *m_spritenode;
551 struct LuaEntityProperties *m_prop;
552 SmoothTranslator pos_translator;
553 // Spritesheet/animation stuff
556 bool m_tx_select_horiz_by_yawpitch;
558 int m_anim_num_frames;
559 float m_anim_framelength;
561 ItemGroupList m_armor_groups;
562 float m_reset_textures_timer;
565 LuaEntityCAO(IGameDef *gamedef, ClientEnvironment *env):
566 ClientActiveObject(0, gamedef, env),
568 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
571 m_position(v3f(0,10*BS,0)),
572 m_velocity(v3f(0,0,0)),
573 m_acceleration(v3f(0,0,0)),
576 m_prop(new LuaEntityProperties),
579 m_tx_select_horiz_by_yawpitch(false),
581 m_anim_num_frames(1),
582 m_anim_framelength(0.2),
584 m_reset_textures_timer(-1)
587 ClientActiveObject::registerType(getType(), create);
590 void initialize(const std::string &data)
592 infostream<<"LuaEntityCAO: Got init data"<<std::endl;
594 std::istringstream is(data, std::ios::binary);
596 u8 version = readU8(is);
601 m_position = readV3F1000(is);
603 m_yaw = readF1000(is);
607 std::istringstream prop_is(deSerializeLongString(is), std::ios::binary);
608 m_prop->deSerialize(prop_is);
610 infostream<<"m_prop: "<<m_prop->dump()<<std::endl;
612 m_selection_box = m_prop->collisionbox;
613 m_selection_box.MinEdge *= BS;
614 m_selection_box.MaxEdge *= BS;
616 pos_translator.init(m_position);
618 m_tx_size.X = 1.0 / m_prop->spritediv.X;
619 m_tx_size.Y = 1.0 / m_prop->spritediv.Y;
620 m_tx_basepos.X = m_tx_size.X * m_prop->initial_sprite_basepos.X;
621 m_tx_basepos.Y = m_tx_size.Y * m_prop->initial_sprite_basepos.Y;
631 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
633 return new LuaEntityCAO(gamedef, env);
638 return ACTIVEOBJECT_TYPE_LUAENTITY;
640 core::aabbox3d<f32>* getSelectionBox()
642 return &m_selection_box;
646 return pos_translator.vect_show;
649 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
654 if(m_meshnode != NULL || m_spritenode != NULL)
657 //video::IVideoDriver* driver = smgr->getVideoDriver();
659 if(m_prop->visual == "sprite"){
660 infostream<<"LuaEntityCAO::addToScene(): single_sprite"<<std::endl;
661 m_spritenode = smgr->addBillboardSceneNode(
662 NULL, v2f(1, 1), v3f(0,0,0), -1);
663 m_spritenode->setMaterialTexture(0,
664 tsrc->getTextureRaw("unknown_block.png"));
665 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
666 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
667 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
668 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
669 m_spritenode->setColor(video::SColor(255,0,0,0));
670 m_spritenode->setVisible(false); /* Set visible when brightness is known */
671 m_spritenode->setSize(m_prop->visual_size*BS);
673 const float txs = 1.0 / 1;
674 const float tys = 1.0 / 1;
675 setBillboardTextureMatrix(m_spritenode,
678 } else if(m_prop->visual == "cube"){
679 infostream<<"LuaEntityCAO::addToScene(): cube"<<std::endl;
680 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
681 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
684 m_meshnode->setScale(v3f(1));
685 // Will be shown when we know the brightness
686 m_meshnode->setVisible(false);
688 infostream<<"LuaEntityCAO::addToScene(): \""<<m_prop->visual
689 <<"\" not supported"<<std::endl;
695 void removeFromScene()
698 m_meshnode->remove();
702 m_spritenode->remove();
707 void updateLight(u8 light_at_pos)
709 bool is_visible = (m_hp != 0);
710 u8 li = decode_light(light_at_pos);
711 video::SColor color(255,li,li,li);
713 setMeshColor(m_meshnode->getMesh(), color);
714 m_meshnode->setVisible(is_visible);
717 m_spritenode->setColor(color);
718 m_spritenode->setVisible(is_visible);
722 v3s16 getLightPosition()
724 return floatToInt(m_position, BS);
730 m_meshnode->setPosition(pos_translator.vect_show);
733 m_spritenode->setPosition(pos_translator.vect_show);
737 void step(float dtime, ClientEnvironment *env)
739 if(m_prop->physical){
740 core::aabbox3d<f32> box = m_prop->collisionbox;
743 collisionMoveResult moveresult;
744 f32 pos_max_d = BS*0.25; // Distance per iteration
745 v3f p_pos = m_position;
746 v3f p_velocity = m_velocity;
747 IGameDef *gamedef = env->getGameDef();
748 moveresult = collisionMovePrecise(&env->getMap(), gamedef,
749 pos_max_d, box, dtime, p_pos, p_velocity);
752 m_velocity = p_velocity;
754 bool is_end_position = moveresult.collides;
755 pos_translator.update(m_position, is_end_position, dtime);
756 pos_translator.translate(dtime);
759 m_velocity += dtime * m_acceleration;
761 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
762 m_velocity += dtime * m_acceleration;
763 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
764 pos_translator.translate(dtime);
768 m_anim_timer += dtime;
769 if(m_anim_timer >= m_anim_framelength){
770 m_anim_timer -= m_anim_framelength;
772 if(m_anim_frame >= m_anim_num_frames)
778 if(m_reset_textures_timer >= 0){
779 m_reset_textures_timer -= dtime;
780 if(m_reset_textures_timer <= 0){
781 m_reset_textures_timer = -1;
787 void updateTexturePos()
790 scene::ICameraSceneNode* camera =
791 m_spritenode->getSceneManager()->getActiveCamera();
794 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
795 - camera->getAbsolutePosition();
796 cam_to_entity.normalize();
798 int row = m_tx_basepos.Y;
799 int col = m_tx_basepos.X;
801 if(m_tx_select_horiz_by_yawpitch)
803 if(cam_to_entity.Y > 0.75)
805 else if(cam_to_entity.Y < -0.75)
808 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / PI * 180.;
809 float dir = mob_dir - m_yaw;
810 dir = wrapDegrees_180(dir);
811 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
812 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
814 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
816 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
818 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
825 // Animation goes downwards
828 float txs = m_tx_size.X;
829 float tys = m_tx_size.Y;
830 setBillboardTextureMatrix(m_spritenode,
835 void updateTextures(const std::string &mod)
837 ITextureSource *tsrc = m_gamedef->tsrc();
840 std::string texturestring = "unknown_block.png";
841 if(m_prop->textures.size() >= 1)
842 texturestring = m_prop->textures[0];
843 texturestring += mod;
844 m_spritenode->setMaterialTexture(0,
845 tsrc->getTextureRaw(texturestring));
848 for (u32 i = 0; i < 6; ++i)
850 std::string texturestring = "unknown_block.png";
851 if(m_prop->textures.size() > i)
852 texturestring = m_prop->textures[i];
853 texturestring += mod;
854 AtlasPointer ap = tsrc->getTexture(texturestring);
856 // Get the tile texture and atlas transformation
857 video::ITexture* atlas = ap.atlas;
861 // Set material flags and texture
862 video::SMaterial& material = m_meshnode->getMaterial(i);
863 material.setFlag(video::EMF_LIGHTING, false);
864 material.setFlag(video::EMF_BILINEAR_FILTER, false);
865 material.setTexture(0, atlas);
866 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
867 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
872 void processMessage(const std::string &data)
874 //infostream<<"LuaEntityCAO: Got message"<<std::endl;
875 std::istringstream is(data, std::ios::binary);
878 if(cmd == LUAENTITY_CMD_UPDATE_POSITION) // update position
881 bool do_interpolate = readU8(is);
883 m_position = readV3F1000(is);
885 m_velocity = readV3F1000(is);
887 m_acceleration = readV3F1000(is);
889 m_yaw = readF1000(is);
890 // is_end_position (for interpolation)
891 bool is_end_position = readU8(is);
893 float update_interval = readF1000(is);
896 if(!m_prop->physical)
897 pos_translator.update(m_position, is_end_position, update_interval);
899 pos_translator.init(m_position);
903 else if(cmd == LUAENTITY_CMD_SET_TEXTURE_MOD) // set texture modification
905 std::string mod = deSerializeString(is);
908 else if(cmd == LUAENTITY_CMD_SET_SPRITE) // set sprite
910 v2s16 p = readV2S16(is);
911 int num_frames = readU16(is);
912 float framelength = readF1000(is);
913 bool select_horiz_by_yawpitch = readU8(is);
916 m_anim_num_frames = num_frames;
917 m_anim_framelength = framelength;
918 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
922 else if(cmd == LUAENTITY_CMD_PUNCHED)
924 /*s16 damage =*/ readS16(is);
925 s16 result_hp = readS16(is);
928 // TODO: Execute defined fast response
930 else if(cmd == LUAENTITY_CMD_UPDATE_ARMOR_GROUPS)
932 m_armor_groups.clear();
933 int armor_groups_size = readU16(is);
934 for(int i=0; i<armor_groups_size; i++){
935 std::string name = deSerializeString(is);
936 int rating = readS16(is);
937 m_armor_groups[name] = rating;
942 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
943 float time_from_last_punch=1000000)
946 const ToolCapabilities *toolcap =
947 &punchitem->getToolCapabilities(m_gamedef->idef());
948 PunchDamageResult result = getPunchDamage(
952 time_from_last_punch);
954 if(result.did_punch && result.damage != 0)
956 if(result.damage < m_hp){
957 m_hp -= result.damage;
960 // TODO: Execute defined fast response
961 // As there is no definition, make a smoke puff
962 ClientSimpleObject *simple = createSmokePuff(
963 m_smgr, m_env, m_position,
964 m_prop->visual_size * BS);
965 m_env->addSimpleObject(simple);
967 // TODO: Execute defined fast response
968 // Flashing shall suffice as there is no definition
969 updateTextures("^[brighten");
970 m_reset_textures_timer = 0.1;
976 std::string debugInfoText()
978 std::ostringstream os(std::ios::binary);
979 os<<"LuaEntityCAO hp="<<m_hp<<"\n";
981 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
982 i != m_armor_groups.end(); i++){
983 os<<i->first<<"="<<i->second<<", ";
991 LuaEntityCAO proto_LuaEntityCAO(NULL, NULL);
997 class PlayerCAO : public ClientActiveObject
1000 core::aabbox3d<f32> m_selection_box;
1001 scene::IMeshSceneNode *m_node;
1002 scene::ITextSceneNode* m_text;
1006 SmoothTranslator pos_translator;
1007 bool m_is_local_player;
1008 LocalPlayer *m_local_player;
1009 float m_damage_visual_timer;
1013 PlayerCAO(IGameDef *gamedef, ClientEnvironment *env):
1014 ClientActiveObject(0, gamedef, env),
1015 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.0,BS/3.),
1018 m_position(v3f(0,10*BS,0)),
1020 m_is_local_player(false),
1021 m_local_player(NULL),
1022 m_damage_visual_timer(0),
1026 ClientActiveObject::registerType(getType(), create);
1029 void initialize(const std::string &data)
1031 infostream<<"PlayerCAO: Got init data"<<std::endl;
1033 std::istringstream is(data, std::ios::binary);
1035 u8 version = readU8(is);
1040 m_name = deSerializeString(is);
1042 m_position = readV3F1000(is);
1044 m_yaw = readF1000(is);
1046 m_dead = readU8(is);
1048 pos_translator.init(m_position);
1050 Player *player = m_env->getPlayer(m_name.c_str());
1051 if(player && player->isLocal()){
1052 m_is_local_player = true;
1053 m_local_player = (LocalPlayer*)player;
1063 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
1065 return new PlayerCAO(gamedef, env);
1070 return ACTIVEOBJECT_TYPE_PLAYER;
1072 core::aabbox3d<f32>* getSelectionBox()
1074 if(m_is_local_player)
1078 return &m_selection_box;
1082 return pos_translator.vect_show;
1085 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
1086 IrrlichtDevice *irr)
1090 if(m_is_local_player)
1093 //video::IVideoDriver* driver = smgr->getVideoDriver();
1094 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
1096 scene::SMesh *mesh = new scene::SMesh();
1098 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
1099 video::SColor c(255,255,255,255);
1100 video::S3DVertex vertices[4] =
1102 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
1103 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
1104 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
1105 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
1107 u16 indices[] = {0,1,2,2,3,0};
1108 buf->append(vertices, 4, indices, 6);
1110 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
1111 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
1112 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
1113 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
1115 mesh->addMeshBuffer(buf);
1119 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
1120 video::SColor c(255,255,255,255);
1121 video::S3DVertex vertices[4] =
1123 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
1124 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
1125 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
1126 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
1128 u16 indices[] = {0,1,2,2,3,0};
1129 buf->append(vertices, 4, indices, 6);
1131 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
1132 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
1133 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
1134 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1136 mesh->addMeshBuffer(buf);
1139 m_node = smgr->addMeshSceneNode(mesh, NULL);
1141 // Set it to use the materials of the meshbuffers directly.
1142 // This is needed for changing the texture in the future
1143 m_node->setReadOnlyMaterials(true);
1146 // Add a text node for showing the name
1147 std::wstring wname = narrow_to_wide(m_name);
1148 m_text = smgr->addTextSceneNode(gui->getBuiltInFont(),
1149 wname.c_str(), video::SColor(255,255,255,255), m_node);
1150 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
1157 void removeFromScene()
1166 void updateLight(u8 light_at_pos)
1171 u8 li = decode_light(light_at_pos);
1172 video::SColor color(255,li,li,li);
1173 setMeshColor(m_node->getMesh(), color);
1178 v3s16 getLightPosition()
1180 return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
1183 void updateVisibility()
1188 m_node->setVisible(!m_dead);
1191 void updateNodePos()
1196 m_node->setPosition(pos_translator.vect_show);
1198 v3f rot = m_node->getRotation();
1200 m_node->setRotation(rot);
1203 void step(float dtime, ClientEnvironment *env)
1205 pos_translator.translate(dtime);
1209 if(m_damage_visual_timer > 0){
1210 m_damage_visual_timer -= dtime;
1211 if(m_damage_visual_timer <= 0){
1217 void processMessage(const std::string &data)
1219 //infostream<<"PlayerCAO: Got message"<<std::endl;
1220 std::istringstream is(data, std::ios::binary);
1222 u8 cmd = readU8(is);
1223 if(cmd == 0) // update position
1226 m_position = readV3F1000(is);
1228 m_yaw = readF1000(is);
1230 pos_translator.update(m_position, false);
1234 else if(cmd == 1) // punched
1237 s16 damage = readS16(is);
1238 m_damage_visual_timer = 0.05;
1240 m_damage_visual_timer += 0.05 * damage;
1241 updateTextures("^[brighten");
1243 else if(cmd == 2) // died or respawned
1245 m_dead = readU8(is);
1250 void updateTextures(const std::string &mod)
1254 ITextureSource *tsrc = m_gamedef->tsrc();
1255 scene::IMesh *mesh = m_node->getMesh();
1258 std::string tname = "player.png";
1260 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1261 buf->getMaterial().setTexture(0,
1262 tsrc->getTextureRaw(tname));
1265 std::string tname = "player_back.png";
1267 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1268 buf->getMaterial().setTexture(0,
1269 tsrc->getTextureRaw(tname));
1276 PlayerCAO proto_PlayerCAO(NULL, NULL);