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;
245 LuaEntityCAO(IGameDef *gamedef, ClientEnvironment *env):
246 ClientActiveObject(0, gamedef, env),
247 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
250 m_position(v3f(0,10*BS,0)),
251 m_velocity(v3f(0,0,0)),
252 m_acceleration(v3f(0,0,0)),
255 m_prop(new LuaEntityProperties),
258 m_tx_select_horiz_by_yawpitch(false),
260 m_anim_num_frames(1),
261 m_anim_framelength(0.2),
265 ClientActiveObject::registerType(getType(), create);
268 void initialize(const std::string &data)
270 infostream<<"LuaEntityCAO: Got init data"<<std::endl;
272 std::istringstream is(data, std::ios::binary);
274 u8 version = readU8(is);
279 m_position = readV3F1000(is);
281 m_yaw = readF1000(is);
285 std::istringstream prop_is(deSerializeLongString(is), std::ios::binary);
286 m_prop->deSerialize(prop_is);
288 infostream<<"m_prop: "<<m_prop->dump()<<std::endl;
290 m_selection_box = m_prop->collisionbox;
291 m_selection_box.MinEdge *= BS;
292 m_selection_box.MaxEdge *= BS;
294 pos_translator.init(m_position);
296 m_tx_size.X = 1.0 / m_prop->spritediv.X;
297 m_tx_size.Y = 1.0 / m_prop->spritediv.Y;
298 m_tx_basepos.X = m_tx_size.X * m_prop->initial_sprite_basepos.X;
299 m_tx_basepos.Y = m_tx_size.Y * m_prop->initial_sprite_basepos.Y;
309 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
311 return new LuaEntityCAO(gamedef, env);
316 return ACTIVEOBJECT_TYPE_LUAENTITY;
318 core::aabbox3d<f32>* getSelectionBox()
320 return &m_selection_box;
324 return pos_translator.vect_show;
327 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
330 if(m_meshnode != NULL || m_spritenode != NULL)
333 //video::IVideoDriver* driver = smgr->getVideoDriver();
335 if(m_prop->visual == "sprite"){
336 infostream<<"LuaEntityCAO::addToScene(): single_sprite"<<std::endl;
337 m_spritenode = smgr->addBillboardSceneNode(
338 NULL, v2f(1, 1), v3f(0,0,0), -1);
339 m_spritenode->setMaterialTexture(0,
340 tsrc->getTextureRaw("unknown_block.png"));
341 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
342 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
343 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
344 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
345 m_spritenode->setColor(video::SColor(255,0,0,0));
346 m_spritenode->setVisible(false); /* Set visible when brightness is known */
347 m_spritenode->setSize(m_prop->visual_size*BS);
349 const float txs = 1.0 / 1;
350 const float tys = 1.0 / 1;
351 setBillboardTextureMatrix(m_spritenode,
354 } else if(m_prop->visual == "cube"){
355 infostream<<"LuaEntityCAO::addToScene(): cube"<<std::endl;
356 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
357 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
360 m_meshnode->setScale(v3f(1));
361 // Will be shown when we know the brightness
362 m_meshnode->setVisible(false);
364 infostream<<"LuaEntityCAO::addToScene(): \""<<m_prop->visual
365 <<"\" not supported"<<std::endl;
371 void removeFromScene()
374 m_meshnode->remove();
378 m_spritenode->remove();
383 void updateLight(u8 light_at_pos)
385 u8 li = decode_light(light_at_pos);
386 video::SColor color(255,li,li,li);
388 setMeshColor(m_meshnode->getMesh(), color);
389 m_meshnode->setVisible(true);
392 m_spritenode->setColor(color);
393 m_spritenode->setVisible(true);
397 v3s16 getLightPosition()
399 return floatToInt(m_position, BS);
405 m_meshnode->setPosition(pos_translator.vect_show);
408 m_spritenode->setPosition(pos_translator.vect_show);
412 void step(float dtime, ClientEnvironment *env)
414 if(m_prop->physical){
415 core::aabbox3d<f32> box = m_prop->collisionbox;
418 collisionMoveResult moveresult;
419 f32 pos_max_d = BS*0.25; // Distance per iteration
420 v3f p_pos = m_position;
421 v3f p_velocity = m_velocity;
422 IGameDef *gamedef = env->getGameDef();
423 moveresult = collisionMovePrecise(&env->getMap(), gamedef,
424 pos_max_d, box, dtime, p_pos, p_velocity);
427 m_velocity = p_velocity;
429 bool is_end_position = moveresult.collides;
430 pos_translator.update(m_position, is_end_position, dtime);
431 pos_translator.translate(dtime);
434 m_velocity += dtime * m_acceleration;
436 m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
437 m_velocity += dtime * m_acceleration;
438 pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
439 pos_translator.translate(dtime);
443 m_anim_timer += dtime;
444 if(m_anim_timer >= m_anim_framelength){
445 m_anim_timer -= m_anim_framelength;
447 if(m_anim_frame >= m_anim_num_frames)
454 void updateTexturePos()
457 scene::ICameraSceneNode* camera =
458 m_spritenode->getSceneManager()->getActiveCamera();
461 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
462 - camera->getAbsolutePosition();
463 cam_to_entity.normalize();
465 int row = m_tx_basepos.Y;
466 int col = m_tx_basepos.X;
468 if(m_tx_select_horiz_by_yawpitch)
470 if(cam_to_entity.Y > 0.75)
472 else if(cam_to_entity.Y < -0.75)
475 float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / PI * 180.;
476 float dir = mob_dir - m_yaw;
477 dir = wrapDegrees_180(dir);
478 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
479 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
481 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
483 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
485 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
492 // Animation goes downwards
495 float txs = m_tx_size.X;
496 float tys = m_tx_size.Y;
497 setBillboardTextureMatrix(m_spritenode,
502 void updateTextures(const std::string &mod)
504 ITextureSource *tsrc = m_gamedef->tsrc();
507 std::string texturestring = "unknown_block.png";
508 if(m_prop->textures.size() >= 1)
509 texturestring = m_prop->textures[0];
510 texturestring += mod;
511 m_spritenode->setMaterialTexture(0,
512 tsrc->getTextureRaw(texturestring));
515 for (u32 i = 0; i < 6; ++i)
517 std::string texturestring = "unknown_block.png";
518 if(m_prop->textures.size() > i)
519 texturestring = m_prop->textures[i];
520 texturestring += mod;
521 AtlasPointer ap = tsrc->getTexture(texturestring);
523 // Get the tile texture and atlas transformation
524 video::ITexture* atlas = ap.atlas;
528 // Set material flags and texture
529 video::SMaterial& material = m_meshnode->getMaterial(i);
530 material.setFlag(video::EMF_LIGHTING, false);
531 material.setFlag(video::EMF_BILINEAR_FILTER, false);
532 material.setTexture(0, atlas);
533 material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
534 material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
539 void processMessage(const std::string &data)
541 //infostream<<"LuaEntityCAO: Got message"<<std::endl;
542 std::istringstream is(data, std::ios::binary);
545 if(cmd == LUAENTITY_CMD_UPDATE_POSITION) // update position
548 bool do_interpolate = readU8(is);
550 m_position = readV3F1000(is);
552 m_velocity = readV3F1000(is);
554 m_acceleration = readV3F1000(is);
556 m_yaw = readF1000(is);
557 // is_end_position (for interpolation)
558 bool is_end_position = readU8(is);
560 float update_interval = readF1000(is);
563 if(!m_prop->physical)
564 pos_translator.update(m_position, is_end_position, update_interval);
566 pos_translator.init(m_position);
570 else if(cmd == LUAENTITY_CMD_SET_TEXTURE_MOD) // set texture modification
572 std::string mod = deSerializeString(is);
575 else if(cmd == LUAENTITY_CMD_SET_SPRITE) // set sprite
577 v2s16 p = readV2S16(is);
578 int num_frames = readU16(is);
579 float framelength = readF1000(is);
580 bool select_horiz_by_yawpitch = readU8(is);
583 m_anim_num_frames = num_frames;
584 m_anim_framelength = framelength;
585 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
589 else if(cmd == LUAENTITY_CMD_PUNCHED)
591 /*s16 damage =*/ readS16(is);
592 s16 result_hp = readS16(is);
595 // TODO: Execute defined fast response
599 bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
600 float time_from_last_punch=1000000)
602 // TODO: Transfer this from the server
603 ItemGroupList m_armor_groups;
606 const ToolCapabilities *toolcap =
607 &punchitem->getToolCapabilities(m_gamedef->idef());
608 PunchDamageResult result = getPunchDamage(
612 time_from_last_punch);
616 // TODO: Decrease hp by
617 if(result.damage < m_hp)
618 m_hp -= result.damage;
621 // TODO: Execute defined fast response
629 LuaEntityCAO proto_LuaEntityCAO(NULL, NULL);
635 class PlayerCAO : public ClientActiveObject
638 core::aabbox3d<f32> m_selection_box;
639 scene::IMeshSceneNode *m_node;
640 scene::ITextSceneNode* m_text;
644 SmoothTranslator pos_translator;
645 bool m_is_local_player;
646 LocalPlayer *m_local_player;
647 float m_damage_visual_timer;
651 PlayerCAO(IGameDef *gamedef, ClientEnvironment *env):
652 ClientActiveObject(0, gamedef, env),
653 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.0,BS/3.),
656 m_position(v3f(0,10*BS,0)),
658 m_is_local_player(false),
659 m_local_player(NULL),
660 m_damage_visual_timer(0),
664 ClientActiveObject::registerType(getType(), create);
667 void initialize(const std::string &data)
669 infostream<<"PlayerCAO: Got init data"<<std::endl;
671 std::istringstream is(data, std::ios::binary);
673 u8 version = readU8(is);
678 m_name = deSerializeString(is);
680 m_position = readV3F1000(is);
682 m_yaw = readF1000(is);
686 pos_translator.init(m_position);
688 Player *player = m_env->getPlayer(m_name.c_str());
689 if(player && player->isLocal()){
690 m_is_local_player = true;
691 m_local_player = (LocalPlayer*)player;
701 static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
703 return new PlayerCAO(gamedef, env);
708 return ACTIVEOBJECT_TYPE_PLAYER;
710 core::aabbox3d<f32>* getSelectionBox()
712 if(m_is_local_player)
716 return &m_selection_box;
720 return pos_translator.vect_show;
723 void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
728 if(m_is_local_player)
731 //video::IVideoDriver* driver = smgr->getVideoDriver();
732 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
734 scene::SMesh *mesh = new scene::SMesh();
736 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
737 video::SColor c(255,255,255,255);
738 video::S3DVertex vertices[4] =
740 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
741 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
742 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
743 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
745 u16 indices[] = {0,1,2,2,3,0};
746 buf->append(vertices, 4, indices, 6);
748 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
749 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
750 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
751 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
753 mesh->addMeshBuffer(buf);
757 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
758 video::SColor c(255,255,255,255);
759 video::S3DVertex vertices[4] =
761 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
762 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
763 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
764 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
766 u16 indices[] = {0,1,2,2,3,0};
767 buf->append(vertices, 4, indices, 6);
769 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
770 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
771 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
772 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
774 mesh->addMeshBuffer(buf);
777 m_node = smgr->addMeshSceneNode(mesh, NULL);
779 // Set it to use the materials of the meshbuffers directly.
780 // This is needed for changing the texture in the future
781 m_node->setReadOnlyMaterials(true);
784 // Add a text node for showing the name
785 std::wstring wname = narrow_to_wide(m_name);
786 m_text = smgr->addTextSceneNode(gui->getBuiltInFont(),
787 wname.c_str(), video::SColor(255,255,255,255), m_node);
788 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
795 void removeFromScene()
804 void updateLight(u8 light_at_pos)
809 u8 li = decode_light(light_at_pos);
810 video::SColor color(255,li,li,li);
811 setMeshColor(m_node->getMesh(), color);
816 v3s16 getLightPosition()
818 return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
821 void updateVisibility()
826 m_node->setVisible(!m_dead);
834 m_node->setPosition(pos_translator.vect_show);
836 v3f rot = m_node->getRotation();
838 m_node->setRotation(rot);
841 void step(float dtime, ClientEnvironment *env)
843 pos_translator.translate(dtime);
847 if(m_damage_visual_timer > 0){
848 m_damage_visual_timer -= dtime;
849 if(m_damage_visual_timer <= 0){
855 void processMessage(const std::string &data)
857 //infostream<<"PlayerCAO: Got message"<<std::endl;
858 std::istringstream is(data, std::ios::binary);
861 if(cmd == 0) // update position
864 m_position = readV3F1000(is);
866 m_yaw = readF1000(is);
868 pos_translator.update(m_position, false);
872 else if(cmd == 1) // punched
875 s16 damage = readS16(is);
876 m_damage_visual_timer = 0.05;
878 m_damage_visual_timer += 0.05 * damage;
879 updateTextures("^[brighten");
881 else if(cmd == 2) // died or respawned
888 void updateTextures(const std::string &mod)
892 ITextureSource *tsrc = m_gamedef->tsrc();
893 scene::IMesh *mesh = m_node->getMesh();
896 std::string tname = "player.png";
898 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
899 buf->getMaterial().setTexture(0,
900 tsrc->getTextureRaw(tname));
903 std::string tname = "player_back.png";
905 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
906 buf->getMaterial().setTexture(0,
907 tsrc->getTextureRaw(tname));
914 PlayerCAO proto_PlayerCAO(NULL, NULL);