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
37 struct ToolCapabilities;
39 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
45 struct SmoothTranslator
52 f32 anim_time_counter;
72 anim_time_counter = 0;
81 void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
83 aim_is_end = is_end_position;
86 if(update_interval > 0){
87 anim_time = update_interval;
89 if(anim_time < 0.001 || anim_time > 1.0)
90 anim_time = anim_time_counter;
92 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
94 anim_time_counter = 0;
98 void translate(f32 dtime)
100 anim_time_counter = anim_time_counter + dtime;
101 anim_counter = anim_counter + dtime;
102 v3f vect_move = vect_aim - vect_old;
104 if(anim_time > 0.001)
105 moveratio = anim_time_counter / anim_time;
106 // Move a bit less than should, to avoid oscillation
107 moveratio = moveratio * 0.8;
108 float move_end = 1.5;
111 if(moveratio > move_end)
112 moveratio = move_end;
113 vect_show = vect_old + vect_move * moveratio;
118 return ((anim_time_counter / anim_time) < 1.4);
126 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
127 float txs, float tys, int col, int row)
129 video::SMaterial& material = bill->getMaterial(0);
130 core::matrix4& matrix = material.getTextureMatrix(0);
131 matrix.setTextureTranslate(txs*col, tys*row);
132 matrix.setTextureScale(txs, tys);
139 class TestCAO : public ClientActiveObject
142 TestCAO(IGameDef *gamedef, ClientEnvironment *env);
147 return ACTIVEOBJECT_TYPE_TEST;
150 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
152 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
153 IrrlichtDevice *irr);
154 void removeFromScene();
155 void updateLight(u8 light_at_pos);
156 v3s16 getLightPosition();
157 void updateNodePos();
159 void step(float dtime, ClientEnvironment *env);
161 void processMessage(const std::string &data);
164 scene::IMeshSceneNode *m_node;
172 class ItemCAO : public ClientActiveObject
175 ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
180 return ACTIVEOBJECT_TYPE_ITEM;
183 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
185 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
186 IrrlichtDevice *irr);
187 void removeFromScene();
188 void updateLight(u8 light_at_pos);
189 v3s16 getLightPosition();
190 void updateNodePos();
191 void updateInfoText();
192 void updateTexture();
194 void step(float dtime, ClientEnvironment *env);
196 void processMessage(const std::string &data);
198 void initialize(const std::string &data);
200 core::aabbox3d<f32>* getSelectionBox()
201 {return &m_selection_box;}
205 std::string infoText()
209 core::aabbox3d<f32> m_selection_box;
210 scene::IMeshSceneNode *m_node;
212 std::string m_itemstring;
213 std::string m_infotext;
220 #include "luaentity_common.h"
222 class LuaEntityCAO : public ClientActiveObject
225 core::aabbox3d<f32> m_selection_box;
226 scene::IMeshSceneNode *m_meshnode;
227 scene::IBillboardSceneNode *m_spritenode;
233 struct LuaEntityProperties *m_prop;
234 SmoothTranslator pos_translator;
235 // Spritesheet/animation stuff
238 bool m_tx_select_horiz_by_yawpitch;
240 int m_anim_num_frames;
241 float m_anim_framelength;
243 ItemGroupList m_armor_groups;
246 LuaEntityCAO(IGameDef *gamedef, ClientEnvironment *env):
247 ClientActiveObject(0, gamedef, env),
248 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
251 m_position(v3f(0,10*BS,0)),
252 m_velocity(v3f(0,0,0)),
253 m_acceleration(v3f(0,0,0)),
256 m_prop(new LuaEntityProperties),
259 m_tx_select_horiz_by_yawpitch(false),
261 m_anim_num_frames(1),
262 m_anim_framelength(0.2),
266 ClientActiveObject::registerType(getType(), create);
269 void initialize(const std::string &data)
271 infostream<<"LuaEntityCAO: Got init data"<<std::endl;
273 std::istringstream is(data, std::ios::binary);
275 u8 version = readU8(is);
280 m_position = readV3F1000(is);
282 m_yaw = readF1000(is);
286 std::istringstream prop_is(deSerializeLongString(is), std::ios::binary);
287 m_prop->deSerialize(prop_is);
289 infostream<<"m_prop: "<<m_prop->dump()<<std::endl;
291 m_selection_box = m_prop->collisionbox;
292 m_selection_box.MinEdge *= BS;
293 m_selection_box.MaxEdge *= BS;
295 pos_translator.init(m_position);
297 m_tx_size.X = 1.0 / m_prop->spritediv.X;
298 m_tx_size.Y = 1.0 / m_prop->spritediv.Y;
299 m_tx_basepos.X = m_tx_size.X * m_prop->initial_sprite_basepos.X;
300 m_tx_basepos.Y = m_tx_size.Y * m_prop->initial_sprite_basepos.Y;
310 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
312 return new LuaEntityCAO(gamedef, env);
317 return ACTIVEOBJECT_TYPE_LUAENTITY;
319 core::aabbox3d<f32>* getSelectionBox()
321 return &m_selection_box;
325 return pos_translator.vect_show;
328 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
331 if(m_meshnode != NULL || m_spritenode != NULL)
334 //video::IVideoDriver* driver = smgr->getVideoDriver();
336 if(m_prop->visual == "sprite"){
337 infostream<<"LuaEntityCAO::addToScene(): single_sprite"<<std::endl;
338 m_spritenode = smgr->addBillboardSceneNode(
339 NULL, v2f(1, 1), v3f(0,0,0), -1);
340 m_spritenode->setMaterialTexture(0,
341 tsrc->getTextureRaw("unknown_block.png"));
342 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
343 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
344 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
345 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
346 m_spritenode->setColor(video::SColor(255,0,0,0));
347 m_spritenode->setVisible(false); /* Set visible when brightness is known */
348 m_spritenode->setSize(m_prop->visual_size*BS);
350 const float txs = 1.0 / 1;
351 const float tys = 1.0 / 1;
352 setBillboardTextureMatrix(m_spritenode,
355 } else if(m_prop->visual == "cube"){
356 infostream<<"LuaEntityCAO::addToScene(): cube"<<std::endl;
357 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
358 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
361 m_meshnode->setScale(v3f(1));
362 // Will be shown when we know the brightness
363 m_meshnode->setVisible(false);
365 infostream<<"LuaEntityCAO::addToScene(): \""<<m_prop->visual
366 <<"\" not supported"<<std::endl;
372 void removeFromScene()
375 m_meshnode->remove();
379 m_spritenode->remove();
384 void updateLight(u8 light_at_pos)
386 u8 li = decode_light(light_at_pos);
387 video::SColor color(255,li,li,li);
389 setMeshColor(m_meshnode->getMesh(), color);
390 m_meshnode->setVisible(true);
393 m_spritenode->setColor(color);
394 m_spritenode->setVisible(true);
398 v3s16 getLightPosition()
400 return floatToInt(m_position, BS);
406 m_meshnode->setPosition(pos_translator.vect_show);
409 m_spritenode->setPosition(pos_translator.vect_show);
413 void step(float dtime, ClientEnvironment *env)
415 if(m_prop->physical){
416 core::aabbox3d<f32> box = m_prop->collisionbox;
419 collisionMoveResult moveresult;
420 f32 pos_max_d = BS*0.25; // Distance per iteration
421 v3f p_pos = m_position;
422 v3f p_velocity = m_velocity;
423 IGameDef *gamedef = env->getGameDef();
424 moveresult = collisionMovePrecise(&env->getMap(), gamedef,
425 pos_max_d, box, dtime, p_pos, p_velocity);
428 m_velocity = p_velocity;
430 bool is_end_position = moveresult.collides;
431 pos_translator.update(m_position, is_end_position, dtime);
432 pos_translator.translate(dtime);
435 m_velocity += dtime * m_acceleration;
437 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
438 m_velocity += dtime * m_acceleration;
439 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
440 pos_translator.translate(dtime);
444 m_anim_timer += dtime;
445 if(m_anim_timer >= m_anim_framelength){
446 m_anim_timer -= m_anim_framelength;
448 if(m_anim_frame >= m_anim_num_frames)
455 void updateTexturePos()
458 scene::ICameraSceneNode* camera =
459 m_spritenode->getSceneManager()->getActiveCamera();
462 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
463 - camera->getAbsolutePosition();
464 cam_to_entity.normalize();
466 int row = m_tx_basepos.Y;
467 int col = m_tx_basepos.X;
469 if(m_tx_select_horiz_by_yawpitch)
471 if(cam_to_entity.Y > 0.75)
473 else if(cam_to_entity.Y < -0.75)
476 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / PI * 180.;
477 float dir = mob_dir - m_yaw;
478 dir = wrapDegrees_180(dir);
479 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
480 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
482 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
484 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
486 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
493 // Animation goes downwards
496 float txs = m_tx_size.X;
497 float tys = m_tx_size.Y;
498 setBillboardTextureMatrix(m_spritenode,
503 void updateTextures(const std::string &mod)
505 ITextureSource *tsrc = m_gamedef->tsrc();
508 std::string texturestring = "unknown_block.png";
509 if(m_prop->textures.size() >= 1)
510 texturestring = m_prop->textures[0];
511 texturestring += mod;
512 m_spritenode->setMaterialTexture(0,
513 tsrc->getTextureRaw(texturestring));
516 for (u32 i = 0; i < 6; ++i)
518 std::string texturestring = "unknown_block.png";
519 if(m_prop->textures.size() > i)
520 texturestring = m_prop->textures[i];
521 texturestring += mod;
522 AtlasPointer ap = tsrc->getTexture(texturestring);
524 // Get the tile texture and atlas transformation
525 video::ITexture* atlas = ap.atlas;
529 // Set material flags and texture
530 video::SMaterial& material = m_meshnode->getMaterial(i);
531 material.setFlag(video::EMF_LIGHTING, false);
532 material.setFlag(video::EMF_BILINEAR_FILTER, false);
533 material.setTexture(0, atlas);
534 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
535 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
540 void processMessage(const std::string &data)
542 //infostream<<"LuaEntityCAO: Got message"<<std::endl;
543 std::istringstream is(data, std::ios::binary);
546 if(cmd == LUAENTITY_CMD_UPDATE_POSITION) // update position
549 bool do_interpolate = readU8(is);
551 m_position = readV3F1000(is);
553 m_velocity = readV3F1000(is);
555 m_acceleration = readV3F1000(is);
557 m_yaw = readF1000(is);
558 // is_end_position (for interpolation)
559 bool is_end_position = readU8(is);
561 float update_interval = readF1000(is);
564 if(!m_prop->physical)
565 pos_translator.update(m_position, is_end_position, update_interval);
567 pos_translator.init(m_position);
571 else if(cmd == LUAENTITY_CMD_SET_TEXTURE_MOD) // set texture modification
573 std::string mod = deSerializeString(is);
576 else if(cmd == LUAENTITY_CMD_SET_SPRITE) // set sprite
578 v2s16 p = readV2S16(is);
579 int num_frames = readU16(is);
580 float framelength = readF1000(is);
581 bool select_horiz_by_yawpitch = readU8(is);
584 m_anim_num_frames = num_frames;
585 m_anim_framelength = framelength;
586 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
590 else if(cmd == LUAENTITY_CMD_PUNCHED)
592 /*s16 damage =*/ readS16(is);
593 s16 result_hp = readS16(is);
596 // TODO: Execute defined fast response
598 else if(cmd == LUAENTITY_CMD_UPDATE_ARMOR_GROUPS)
600 m_armor_groups.clear();
601 int armor_groups_size = readU16(is);
602 for(int i=0; i<armor_groups_size; i++){
603 std::string name = deSerializeString(is);
604 int rating = readS16(is);
605 m_armor_groups[name] = rating;
610 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
611 float time_from_last_punch=1000000)
614 const ToolCapabilities *toolcap =
615 &punchitem->getToolCapabilities(m_gamedef->idef());
616 PunchDamageResult result = getPunchDamage(
620 time_from_last_punch);
624 if(result.damage < m_hp)
625 m_hp -= result.damage;
628 // TODO: Execute defined fast response
634 std::string debugInfoText()
636 std::ostringstream os(std::ios::binary);
637 os<<"LuaEntityCAO \n";
639 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
640 i != m_armor_groups.end(); i++){
641 os<<i->first<<"="<<i->second<<", ";
649 LuaEntityCAO proto_LuaEntityCAO(NULL, NULL);
655 class PlayerCAO : public ClientActiveObject
658 core::aabbox3d<f32> m_selection_box;
659 scene::IMeshSceneNode *m_node;
660 scene::ITextSceneNode* m_text;
664 SmoothTranslator pos_translator;
665 bool m_is_local_player;
666 LocalPlayer *m_local_player;
667 float m_damage_visual_timer;
671 PlayerCAO(IGameDef *gamedef, ClientEnvironment *env):
672 ClientActiveObject(0, gamedef, env),
673 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.0,BS/3.),
676 m_position(v3f(0,10*BS,0)),
678 m_is_local_player(false),
679 m_local_player(NULL),
680 m_damage_visual_timer(0),
684 ClientActiveObject::registerType(getType(), create);
687 void initialize(const std::string &data)
689 infostream<<"PlayerCAO: Got init data"<<std::endl;
691 std::istringstream is(data, std::ios::binary);
693 u8 version = readU8(is);
698 m_name = deSerializeString(is);
700 m_position = readV3F1000(is);
702 m_yaw = readF1000(is);
706 pos_translator.init(m_position);
708 Player *player = m_env->getPlayer(m_name.c_str());
709 if(player && player->isLocal()){
710 m_is_local_player = true;
711 m_local_player = (LocalPlayer*)player;
721 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
723 return new PlayerCAO(gamedef, env);
728 return ACTIVEOBJECT_TYPE_PLAYER;
730 core::aabbox3d<f32>* getSelectionBox()
732 if(m_is_local_player)
736 return &m_selection_box;
740 return pos_translator.vect_show;
743 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
748 if(m_is_local_player)
751 //video::IVideoDriver* driver = smgr->getVideoDriver();
752 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
754 scene::SMesh *mesh = new scene::SMesh();
756 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
757 video::SColor c(255,255,255,255);
758 video::S3DVertex vertices[4] =
760 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
761 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
762 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
763 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
765 u16 indices[] = {0,1,2,2,3,0};
766 buf->append(vertices, 4, indices, 6);
768 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
769 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
770 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
771 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
773 mesh->addMeshBuffer(buf);
777 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
778 video::SColor c(255,255,255,255);
779 video::S3DVertex vertices[4] =
781 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
782 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
783 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
784 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
786 u16 indices[] = {0,1,2,2,3,0};
787 buf->append(vertices, 4, indices, 6);
789 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
790 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
791 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
792 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
794 mesh->addMeshBuffer(buf);
797 m_node = smgr->addMeshSceneNode(mesh, NULL);
799 // Set it to use the materials of the meshbuffers directly.
800 // This is needed for changing the texture in the future
801 m_node->setReadOnlyMaterials(true);
804 // Add a text node for showing the name
805 std::wstring wname = narrow_to_wide(m_name);
806 m_text = smgr->addTextSceneNode(gui->getBuiltInFont(),
807 wname.c_str(), video::SColor(255,255,255,255), m_node);
808 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
815 void removeFromScene()
824 void updateLight(u8 light_at_pos)
829 u8 li = decode_light(light_at_pos);
830 video::SColor color(255,li,li,li);
831 setMeshColor(m_node->getMesh(), color);
836 v3s16 getLightPosition()
838 return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
841 void updateVisibility()
846 m_node->setVisible(!m_dead);
854 m_node->setPosition(pos_translator.vect_show);
856 v3f rot = m_node->getRotation();
858 m_node->setRotation(rot);
861 void step(float dtime, ClientEnvironment *env)
863 pos_translator.translate(dtime);
867 if(m_damage_visual_timer > 0){
868 m_damage_visual_timer -= dtime;
869 if(m_damage_visual_timer <= 0){
875 void processMessage(const std::string &data)
877 //infostream<<"PlayerCAO: Got message"<<std::endl;
878 std::istringstream is(data, std::ios::binary);
881 if(cmd == 0) // update position
884 m_position = readV3F1000(is);
886 m_yaw = readF1000(is);
888 pos_translator.update(m_position, false);
892 else if(cmd == 1) // punched
895 s16 damage = readS16(is);
896 m_damage_visual_timer = 0.05;
898 m_damage_visual_timer += 0.05 * damage;
899 updateTextures("^[brighten");
901 else if(cmd == 2) // died or respawned
908 void updateTextures(const std::string &mod)
912 ITextureSource *tsrc = m_gamedef->tsrc();
913 scene::IMesh *mesh = m_node->getMesh();
916 std::string tname = "player.png";
918 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
919 buf->getMaterial().setTexture(0,
920 tsrc->getTextureRaw(tname));
923 std::string tname = "player_back.png";
925 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
926 buf->getMaterial().setTexture(0,
927 tsrc->getTextureRaw(tname));
934 PlayerCAO proto_PlayerCAO(NULL, NULL);