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 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
48 struct SmoothTranslator
55 f32 anim_time_counter;
75 anim_time_counter = 0;
84 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
86 aim_is_end = is_end_position;
89 if(update_interval > 0){
90 anim_time = update_interval;
92 if(anim_time < 0.001 || anim_time > 1.0)
93 anim_time = anim_time_counter;
95 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
97 anim_time_counter = 0;
101 void translate(f32 dtime)
103 anim_time_counter = anim_time_counter + dtime;
104 anim_counter = anim_counter + dtime;
105 v3f vect_move = vect_aim - vect_old;
107 if(anim_time > 0.001)
108 moveratio = anim_time_counter / anim_time;
109 // Move a bit less than should, to avoid oscillation
110 moveratio = moveratio * 0.8;
111 float move_end = 1.5;
114 if(moveratio > move_end)
115 moveratio = move_end;
116 vect_show = vect_old + vect_move * moveratio;
121 return ((anim_time_counter / anim_time) < 1.4);
129 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
130 float txs, float tys, int col, int row)
132 video::SMaterial& material = bill->getMaterial(0);
133 core::matrix4& matrix = material.getTextureMatrix(0);
134 matrix.setTextureTranslate(txs*col, tys*row);
135 matrix.setTextureScale(txs, tys);
142 class TestCAO : public ClientActiveObject
145 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
150 return ACTIVEOBJECT_TYPE_TEST;
153 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
155 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
156 IrrlichtDevice *irr);
157 void removeFromScene();
158 void updateLight(u8 light_at_pos);
159 v3s16 getLightPosition();
160 void updateNodePos();
162 void step(float dtime, ClientEnvironment *env);
164 void processMessage(const std::string &data);
167 scene::IMeshSceneNode *m_node;
172 TestCAO proto_TestCAO(NULL, NULL);
174 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
175 ClientActiveObject(0, gamedef, env),
177 m_position(v3f(0,10*BS,0))
179 ClientActiveObject::registerType(getType(), create);
186 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
188 return new TestCAO(gamedef, env);
191 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
197 //video::IVideoDriver* driver = smgr->getVideoDriver();
199 scene::SMesh *mesh = new scene::SMesh();
200 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
201 video::SColor c(255,255,255,255);
202 video::S3DVertex vertices[4] =
204 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
205 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
206 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
207 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
209 u16 indices[] = {0,1,2,2,3,0};
210 buf->append(vertices, 4, indices, 6);
212 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
213 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
214 buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
215 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
216 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
217 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
219 mesh->addMeshBuffer(buf);
221 m_node = smgr->addMeshSceneNode(mesh, NULL);
226 void TestCAO::removeFromScene()
235 void TestCAO::updateLight(u8 light_at_pos)
239 v3s16 TestCAO::getLightPosition()
241 return floatToInt(m_position, BS);
244 void TestCAO::updateNodePos()
249 m_node->setPosition(m_position);
250 //m_node->setRotation(v3f(0, 45, 0));
253 void TestCAO::step(float dtime, ClientEnvironment *env)
257 v3f rot = m_node->getRotation();
258 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
259 rot.Y += dtime * 180;
260 m_node->setRotation(rot);
264 void TestCAO::processMessage(const std::string &data)
266 infostream<<"TestCAO: Got data: "<<data<<std::endl;
267 std::istringstream is(data, std::ios::binary);
285 class ItemCAO : public ClientActiveObject
288 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
293 return ACTIVEOBJECT_TYPE_ITEM;
296 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
298 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
299 IrrlichtDevice *irr);
300 void removeFromScene();
301 void updateLight(u8 light_at_pos);
302 v3s16 getLightPosition();
303 void updateNodePos();
304 void updateInfoText();
305 void updateTexture();
307 void step(float dtime, ClientEnvironment *env);
309 void processMessage(const std::string &data);
311 void initialize(const std::string &data);
313 core::aabbox3d<f32>* getSelectionBox()
314 {return &m_selection_box;}
318 std::string infoText()
322 core::aabbox3d<f32> m_selection_box;
323 scene::IMeshSceneNode *m_node;
325 std::string m_itemstring;
326 std::string m_infotext;
329 #include "inventory.h"
332 ItemCAO proto_ItemCAO(NULL, NULL);
334 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
335 ClientActiveObject(0, gamedef, env),
336 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
338 m_position(v3f(0,10*BS,0))
342 ClientActiveObject::registerType(getType(), create);
350 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
352 return new ItemCAO(gamedef, env);
355 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
361 //video::IVideoDriver* driver = smgr->getVideoDriver();
363 scene::SMesh *mesh = new scene::SMesh();
364 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
365 video::SColor c(255,255,255,255);
366 video::S3DVertex vertices[4] =
368 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
369 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
370 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
371 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
372 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
373 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
374 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
375 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
377 u16 indices[] = {0,1,2,2,3,0};
378 buf->append(vertices, 4, indices, 6);
380 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
381 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
382 // Initialize with a generated placeholder texture
383 buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
384 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
385 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
386 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
388 mesh->addMeshBuffer(buf);
390 m_node = smgr->addMeshSceneNode(mesh, NULL);
401 void ItemCAO::removeFromScene()
410 void ItemCAO::updateLight(u8 light_at_pos)
415 u8 li = decode_light(light_at_pos);
416 video::SColor color(255,li,li,li);
417 setMeshColor(m_node->getMesh(), color);
420 v3s16 ItemCAO::getLightPosition()
422 return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
425 void ItemCAO::updateNodePos()
430 m_node->setPosition(m_position);
433 void ItemCAO::updateInfoText()
436 IItemDefManager *idef = m_gamedef->idef();
438 item.deSerialize(m_itemstring, idef);
439 if(item.isKnown(idef))
440 m_infotext = item.getDefinition(idef).description;
442 m_infotext = "Unknown item: '" + m_itemstring + "'";
444 m_infotext += " (" + itos(item.count) + ")";
446 catch(SerializationError &e)
448 m_infotext = "Unknown item: '" + m_itemstring + "'";
452 void ItemCAO::updateTexture()
457 // Create an inventory item to see what is its image
458 std::istringstream is(m_itemstring, std::ios_base::binary);
459 video::ITexture *texture = NULL;
461 IItemDefManager *idef = m_gamedef->idef();
463 item.deSerialize(is, idef);
464 texture = item.getDefinition(idef).inventory_texture;
466 catch(SerializationError &e)
468 infostream<<"WARNING: "<<__FUNCTION_NAME
469 <<": error deSerializing itemstring \""
470 <<m_itemstring<<std::endl;
473 // Set meshbuffer texture
474 m_node->getMaterial(0).setTexture(0, texture);
478 void ItemCAO::step(float dtime, ClientEnvironment *env)
482 /*v3f rot = m_node->getRotation();
483 rot.Y += dtime * 120;
484 m_node->setRotation(rot);*/
485 LocalPlayer *player = env->getLocalPlayer();
487 v3f rot = m_node->getRotation();
488 rot.Y = 180.0 - (player->getYaw());
489 m_node->setRotation(rot);
493 void ItemCAO::processMessage(const std::string &data)
495 //infostream<<"ItemCAO: Got message"<<std::endl;
496 std::istringstream is(data, std::ios::binary);
502 m_position = readV3F1000(is);
508 m_itemstring = deSerializeString(is);
514 void ItemCAO::initialize(const std::string &data)
516 infostream<<"ItemCAO: Got init data"<<std::endl;
519 std::istringstream is(data, std::ios::binary);
521 u8 version = readU8(is);
526 m_position = readV3F1000(is);
528 m_itemstring = deSerializeString(is);
539 #include "luaentity_common.h"
541 class LuaEntityCAO : public ClientActiveObject
544 scene::ISceneManager *m_smgr;
545 core::aabbox3d<f32> m_selection_box;
546 scene::IMeshSceneNode *m_meshnode;
547 scene::IBillboardSceneNode *m_spritenode;
553 struct LuaEntityProperties *m_prop;
554 SmoothTranslator pos_translator;
555 // Spritesheet/animation stuff
558 bool m_tx_select_horiz_by_yawpitch;
560 int m_anim_num_frames;
561 float m_anim_framelength;
563 ItemGroupList m_armor_groups;
564 float m_reset_textures_timer;
567 LuaEntityCAO(IGameDef *gamedef, ClientEnvironment *env):
568 ClientActiveObject(0, gamedef, env),
570 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
573 m_position(v3f(0,10*BS,0)),
574 m_velocity(v3f(0,0,0)),
575 m_acceleration(v3f(0,0,0)),
578 m_prop(new LuaEntityProperties),
581 m_tx_select_horiz_by_yawpitch(false),
583 m_anim_num_frames(1),
584 m_anim_framelength(0.2),
586 m_reset_textures_timer(-1)
589 ClientActiveObject::registerType(getType(), create);
592 void initialize(const std::string &data)
594 infostream<<"LuaEntityCAO: Got init data"<<std::endl;
596 std::istringstream is(data, std::ios::binary);
598 u8 version = readU8(is);
603 m_position = readV3F1000(is);
605 m_yaw = readF1000(is);
609 std::istringstream prop_is(deSerializeLongString(is), std::ios::binary);
610 m_prop->deSerialize(prop_is);
612 infostream<<"m_prop: "<<m_prop->dump()<<std::endl;
614 m_selection_box = m_prop->collisionbox;
615 m_selection_box.MinEdge *= BS;
616 m_selection_box.MaxEdge *= BS;
618 pos_translator.init(m_position);
620 m_tx_size.X = 1.0 / m_prop->spritediv.X;
621 m_tx_size.Y = 1.0 / m_prop->spritediv.Y;
622 m_tx_basepos.X = m_tx_size.X * m_prop->initial_sprite_basepos.X;
623 m_tx_basepos.Y = m_tx_size.Y * m_prop->initial_sprite_basepos.Y;
633 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
635 return new LuaEntityCAO(gamedef, env);
640 return ACTIVEOBJECT_TYPE_LUAENTITY;
642 core::aabbox3d<f32>* getSelectionBox()
644 return &m_selection_box;
648 return pos_translator.vect_show;
651 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
656 if(m_meshnode != NULL || m_spritenode != NULL)
659 //video::IVideoDriver* driver = smgr->getVideoDriver();
661 if(m_prop->visual == "sprite"){
662 infostream<<"LuaEntityCAO::addToScene(): single_sprite"<<std::endl;
663 m_spritenode = smgr->addBillboardSceneNode(
664 NULL, v2f(1, 1), v3f(0,0,0), -1);
665 m_spritenode->setMaterialTexture(0,
666 tsrc->getTextureRaw("unknown_block.png"));
667 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
668 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
669 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
670 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
671 m_spritenode->setColor(video::SColor(255,0,0,0));
672 m_spritenode->setVisible(false); /* Set visible when brightness is known */
673 m_spritenode->setSize(m_prop->visual_size*BS);
675 const float txs = 1.0 / 1;
676 const float tys = 1.0 / 1;
677 setBillboardTextureMatrix(m_spritenode,
680 } else if(m_prop->visual == "cube"){
681 infostream<<"LuaEntityCAO::addToScene(): cube"<<std::endl;
682 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
683 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
686 m_meshnode->setScale(v3f(1));
687 // Will be shown when we know the brightness
688 m_meshnode->setVisible(false);
690 infostream<<"LuaEntityCAO::addToScene(): \""<<m_prop->visual
691 <<"\" not supported"<<std::endl;
697 void removeFromScene()
700 m_meshnode->remove();
704 m_spritenode->remove();
709 void updateLight(u8 light_at_pos)
711 bool is_visible = (m_hp != 0);
712 u8 li = decode_light(light_at_pos);
713 video::SColor color(255,li,li,li);
715 setMeshColor(m_meshnode->getMesh(), color);
716 m_meshnode->setVisible(is_visible);
719 m_spritenode->setColor(color);
720 m_spritenode->setVisible(is_visible);
724 v3s16 getLightPosition()
726 return floatToInt(m_position, BS);
732 m_meshnode->setPosition(pos_translator.vect_show);
735 m_spritenode->setPosition(pos_translator.vect_show);
739 void step(float dtime, ClientEnvironment *env)
741 if(m_prop->physical){
742 core::aabbox3d<f32> box = m_prop->collisionbox;
745 collisionMoveResult moveresult;
746 f32 pos_max_d = BS*0.25; // Distance per iteration
747 v3f p_pos = m_position;
748 v3f p_velocity = m_velocity;
749 IGameDef *gamedef = env->getGameDef();
750 moveresult = collisionMovePrecise(&env->getMap(), gamedef,
751 pos_max_d, box, dtime, p_pos, p_velocity);
754 m_velocity = p_velocity;
756 bool is_end_position = moveresult.collides;
757 pos_translator.update(m_position, is_end_position, dtime);
758 pos_translator.translate(dtime);
761 m_velocity += dtime * m_acceleration;
763 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
764 m_velocity += dtime * m_acceleration;
765 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
766 pos_translator.translate(dtime);
770 m_anim_timer += dtime;
771 if(m_anim_timer >= m_anim_framelength){
772 m_anim_timer -= m_anim_framelength;
774 if(m_anim_frame >= m_anim_num_frames)
780 if(m_reset_textures_timer >= 0){
781 m_reset_textures_timer -= dtime;
782 if(m_reset_textures_timer <= 0){
783 m_reset_textures_timer = -1;
789 void updateTexturePos()
792 scene::ICameraSceneNode* camera =
793 m_spritenode->getSceneManager()->getActiveCamera();
796 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
797 - camera->getAbsolutePosition();
798 cam_to_entity.normalize();
800 int row = m_tx_basepos.Y;
801 int col = m_tx_basepos.X;
803 if(m_tx_select_horiz_by_yawpitch)
805 if(cam_to_entity.Y > 0.75)
807 else if(cam_to_entity.Y < -0.75)
810 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / PI * 180.;
811 float dir = mob_dir - m_yaw;
812 dir = wrapDegrees_180(dir);
813 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
814 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
816 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
818 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
820 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
827 // Animation goes downwards
830 float txs = m_tx_size.X;
831 float tys = m_tx_size.Y;
832 setBillboardTextureMatrix(m_spritenode,
837 void updateTextures(const std::string &mod)
839 ITextureSource *tsrc = m_gamedef->tsrc();
842 std::string texturestring = "unknown_block.png";
843 if(m_prop->textures.size() >= 1)
844 texturestring = m_prop->textures[0];
845 texturestring += mod;
846 m_spritenode->setMaterialTexture(0,
847 tsrc->getTextureRaw(texturestring));
850 for (u32 i = 0; i < 6; ++i)
852 std::string texturestring = "unknown_block.png";
853 if(m_prop->textures.size() > i)
854 texturestring = m_prop->textures[i];
855 texturestring += mod;
856 AtlasPointer ap = tsrc->getTexture(texturestring);
858 // Get the tile texture and atlas transformation
859 video::ITexture* atlas = ap.atlas;
863 // Set material flags and texture
864 video::SMaterial& material = m_meshnode->getMaterial(i);
865 material.setFlag(video::EMF_LIGHTING, false);
866 material.setFlag(video::EMF_BILINEAR_FILTER, false);
867 material.setTexture(0, atlas);
868 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
869 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
874 void processMessage(const std::string &data)
876 //infostream<<"LuaEntityCAO: Got message"<<std::endl;
877 std::istringstream is(data, std::ios::binary);
880 if(cmd == LUAENTITY_CMD_UPDATE_POSITION) // update position
883 bool do_interpolate = readU8(is);
885 m_position = readV3F1000(is);
887 m_velocity = readV3F1000(is);
889 m_acceleration = readV3F1000(is);
891 m_yaw = readF1000(is);
892 // is_end_position (for interpolation)
893 bool is_end_position = readU8(is);
895 float update_interval = readF1000(is);
898 if(!m_prop->physical)
899 pos_translator.update(m_position, is_end_position, update_interval);
901 pos_translator.init(m_position);
905 else if(cmd == LUAENTITY_CMD_SET_TEXTURE_MOD) // set texture modification
907 std::string mod = deSerializeString(is);
910 else if(cmd == LUAENTITY_CMD_SET_SPRITE) // set sprite
912 v2s16 p = readV2S16(is);
913 int num_frames = readU16(is);
914 float framelength = readF1000(is);
915 bool select_horiz_by_yawpitch = readU8(is);
918 m_anim_num_frames = num_frames;
919 m_anim_framelength = framelength;
920 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
924 else if(cmd == LUAENTITY_CMD_PUNCHED)
926 /*s16 damage =*/ readS16(is);
927 s16 result_hp = readS16(is);
930 // TODO: Execute defined fast response
932 else if(cmd == LUAENTITY_CMD_UPDATE_ARMOR_GROUPS)
934 m_armor_groups.clear();
935 int armor_groups_size = readU16(is);
936 for(int i=0; i<armor_groups_size; i++){
937 std::string name = deSerializeString(is);
938 int rating = readS16(is);
939 m_armor_groups[name] = rating;
944 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
945 float time_from_last_punch=1000000)
948 const ToolCapabilities *toolcap =
949 &punchitem->getToolCapabilities(m_gamedef->idef());
950 PunchDamageResult result = getPunchDamage(
954 time_from_last_punch);
956 if(result.did_punch && result.damage != 0)
958 if(result.damage < m_hp){
959 m_hp -= result.damage;
962 // TODO: Execute defined fast response
963 // As there is no definition, make a smoke puff
964 ClientSimpleObject *simple = createSmokePuff(
965 m_smgr, m_env, m_position,
966 m_prop->visual_size * BS);
967 m_env->addSimpleObject(simple);
969 // TODO: Execute defined fast response
970 // Flashing shall suffice as there is no definition
971 updateTextures("^[brighten");
972 m_reset_textures_timer = 0.1;
978 std::string debugInfoText()
980 std::ostringstream os(std::ios::binary);
981 os<<"LuaEntityCAO hp="<<m_hp<<"\n";
983 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
984 i != m_armor_groups.end(); i++){
985 os<<i->first<<"="<<i->second<<", ";
993 LuaEntityCAO proto_LuaEntityCAO(NULL, NULL);
999 class PlayerCAO : public ClientActiveObject
1002 core::aabbox3d<f32> m_selection_box;
1003 scene::IMeshSceneNode *m_node;
1004 scene::ITextSceneNode* m_text;
1008 SmoothTranslator pos_translator;
1009 bool m_is_local_player;
1010 LocalPlayer *m_local_player;
1011 float m_damage_visual_timer;
1013 float m_step_distance_counter;
1014 std::string m_wielded_item;
1017 PlayerCAO(IGameDef *gamedef, ClientEnvironment *env):
1018 ClientActiveObject(0, gamedef, env),
1019 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.0,BS/3.),
1022 m_position(v3f(0,10*BS,0)),
1024 m_is_local_player(false),
1025 m_local_player(NULL),
1026 m_damage_visual_timer(0),
1028 m_step_distance_counter(0),
1032 ClientActiveObject::registerType(getType(), create);
1035 void initialize(const std::string &data)
1037 infostream<<"PlayerCAO: Got init data"<<std::endl;
1039 std::istringstream is(data, std::ios::binary);
1041 u8 version = readU8(is);
1046 m_name = deSerializeString(is);
1048 m_position = readV3F1000(is);
1050 m_yaw = readF1000(is);
1052 m_dead = readU8(is);
1055 m_wielded_item = deSerializeString(is);
1057 catch(SerializationError &e){}
1059 pos_translator.init(m_position);
1061 Player *player = m_env->getPlayer(m_name.c_str());
1062 if(player && player->isLocal()){
1063 m_is_local_player = true;
1064 m_local_player = (LocalPlayer*)player;
1074 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
1076 return new PlayerCAO(gamedef, env);
1081 return ACTIVEOBJECT_TYPE_PLAYER;
1083 core::aabbox3d<f32>* getSelectionBox()
1085 if(m_is_local_player)
1089 return &m_selection_box;
1093 return pos_translator.vect_show;
1096 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
1097 IrrlichtDevice *irr)
1101 if(m_is_local_player)
1104 //video::IVideoDriver* driver = smgr->getVideoDriver();
1105 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
1107 scene::SMesh *mesh = new scene::SMesh();
1109 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
1110 video::SColor c(255,255,255,255);
1111 video::S3DVertex vertices[4] =
1113 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
1114 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
1115 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
1116 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
1118 u16 indices[] = {0,1,2,2,3,0};
1119 buf->append(vertices, 4, indices, 6);
1121 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
1122 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
1123 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
1124 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
1126 mesh->addMeshBuffer(buf);
1130 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
1131 video::SColor c(255,255,255,255);
1132 video::S3DVertex vertices[4] =
1134 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
1135 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
1136 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
1137 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
1139 u16 indices[] = {0,1,2,2,3,0};
1140 buf->append(vertices, 4, indices, 6);
1142 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
1143 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
1144 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
1145 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
1147 mesh->addMeshBuffer(buf);
1150 m_node = smgr->addMeshSceneNode(mesh, NULL);
1152 // Set it to use the materials of the meshbuffers directly.
1153 // This is needed for changing the texture in the future
1154 m_node->setReadOnlyMaterials(true);
1157 // Add a text node for showing the name
1158 std::wstring wname = narrow_to_wide(m_name);
1159 m_text = smgr->addTextSceneNode(gui->getBuiltInFont(),
1160 wname.c_str(), video::SColor(255,255,255,255), m_node);
1161 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
1168 void removeFromScene()
1177 void updateLight(u8 light_at_pos)
1182 u8 li = decode_light(light_at_pos);
1183 video::SColor color(255,li,li,li);
1184 setMeshColor(m_node->getMesh(), color);
1189 v3s16 getLightPosition()
1191 return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
1194 void updateVisibility()
1199 m_node->setVisible(!m_dead);
1202 void updateNodePos()
1207 m_node->setPosition(pos_translator.vect_show);
1209 v3f rot = m_node->getRotation();
1211 m_node->setRotation(rot);
1214 void step(float dtime, ClientEnvironment *env)
1216 v3f lastpos = pos_translator.vect_show;
1217 pos_translator.translate(dtime);
1218 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1222 if(m_damage_visual_timer > 0){
1223 m_damage_visual_timer -= dtime;
1224 if(m_damage_visual_timer <= 0){
1229 m_step_distance_counter += moved;
1230 if(m_step_distance_counter > 1.5*BS){
1231 m_step_distance_counter = 0;
1232 if(!m_is_local_player){
1233 INodeDefManager *ndef = m_gamedef->ndef();
1234 v3s16 p = floatToInt(getPosition()+v3f(0,-0.5*BS, 0), BS);
1235 MapNode n = m_env->getMap().getNodeNoEx(p);
1236 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1237 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1242 void processMessage(const std::string &data)
1244 //infostream<<"PlayerCAO: Got message"<<std::endl;
1245 std::istringstream is(data, std::ios::binary);
1247 u8 cmd = readU8(is);
1248 if(cmd == 0) // update position
1251 m_position = readV3F1000(is);
1253 m_yaw = readF1000(is);
1255 pos_translator.update(m_position, false);
1259 else if(cmd == 1) // punched
1262 s16 damage = readS16(is);
1263 m_damage_visual_timer = 0.05;
1265 m_damage_visual_timer += 0.05 * damage;
1266 updateTextures("^[brighten");
1268 else if(cmd == 2) // died or respawned
1270 m_dead = readU8(is);
1273 else if(cmd == 3) // wielded item
1275 m_wielded_item = deSerializeString(is);
1276 updateWieldedItem();
1280 void updateTextures(const std::string &mod)
1284 ITextureSource *tsrc = m_gamedef->tsrc();
1285 scene::IMesh *mesh = m_node->getMesh();
1288 std::string tname = "player.png";
1290 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1291 buf->getMaterial().setTexture(0,
1292 tsrc->getTextureRaw(tname));
1295 std::string tname = "player_back.png";
1297 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1298 buf->getMaterial().setTexture(0,
1299 tsrc->getTextureRaw(tname));
1304 void updateWieldedItem()
1306 if(m_is_local_player)
1308 // ignoring player item for local player
1315 item.deSerialize(m_wielded_item, m_gamedef->idef());
1317 catch(SerializationError &e)
1319 errorstream<<"PlayerCAO: SerializationError "
1320 "while reading wielded item: "
1321 <<m_wielded_item<<std::endl;
1325 // do something with the item, for example:
1326 Player *player = m_env->getPlayer(m_name.c_str());
1329 InventoryList *inv = player->inventory.getList("main");
1331 inv->changeItem(0, item);
1336 infostream<<"PlayerCAO: empty player item for player "
1337 <<m_name<<std::endl;
1341 infostream<<"PlayerCAO: player item for player "
1343 <<item.getItemString()<<std::endl;
1350 PlayerCAO proto_PlayerCAO(NULL, NULL);