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"
24 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
31 TestCAO proto_TestCAO;
34 ClientActiveObject(0),
36 m_position(v3f(0,10*BS,0))
38 ClientActiveObject::registerType(getType(), create);
45 ClientActiveObject* TestCAO::create()
50 void TestCAO::addToScene(scene::ISceneManager *smgr)
55 video::IVideoDriver* driver = smgr->getVideoDriver();
57 scene::SMesh *mesh = new scene::SMesh();
58 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
59 video::SColor c(255,255,255,255);
60 video::S3DVertex vertices[4] =
62 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
63 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
64 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
65 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
67 u16 indices[] = {0,1,2,2,3,0};
68 buf->append(vertices, 4, indices, 6);
70 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
71 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
72 buf->getMaterial().setTexture
73 (0, driver->getTexture(getTexturePath("rat.png").c_str()));
74 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
75 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
76 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
78 mesh->addMeshBuffer(buf);
80 m_node = smgr->addMeshSceneNode(mesh, NULL);
85 void TestCAO::removeFromScene()
94 void TestCAO::updateLight(u8 light_at_pos)
98 v3s16 TestCAO::getLightPosition()
100 return floatToInt(m_position, BS);
103 void TestCAO::updateNodePos()
108 m_node->setPosition(m_position);
109 //m_node->setRotation(v3f(0, 45, 0));
112 void TestCAO::step(float dtime, ClientEnvironment *env)
116 v3f rot = m_node->getRotation();
117 //dstream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
118 rot.Y += dtime * 180;
119 m_node->setRotation(rot);
123 void TestCAO::processMessage(const std::string &data)
125 dstream<<"TestCAO: Got data: "<<data<<std::endl;
126 std::istringstream is(data, std::ios::binary);
144 #include "inventory.h"
147 ItemCAO proto_ItemCAO;
150 ClientActiveObject(0),
151 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
153 m_position(v3f(0,10*BS,0))
155 ClientActiveObject::registerType(getType(), create);
162 ClientActiveObject* ItemCAO::create()
164 return new ItemCAO();
167 void ItemCAO::addToScene(scene::ISceneManager *smgr)
172 video::IVideoDriver* driver = smgr->getVideoDriver();
174 scene::SMesh *mesh = new scene::SMesh();
175 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
176 video::SColor c(255,255,255,255);
177 video::S3DVertex vertices[4] =
179 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
180 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
181 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
182 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
183 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
184 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
185 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
186 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
188 u16 indices[] = {0,1,2,2,3,0};
189 buf->append(vertices, 4, indices, 6);
191 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
192 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
193 //buf->getMaterial().setTexture(0, NULL);
194 // Initialize with the stick texture
195 buf->getMaterial().setTexture
196 (0, driver->getTexture(getTexturePath("stick.png").c_str()));
197 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
198 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
199 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
201 mesh->addMeshBuffer(buf);
203 m_node = smgr->addMeshSceneNode(mesh, NULL);
205 // Set it to use the materials of the meshbuffers directly.
206 // This is needed for changing the texture in the future
207 m_node->setReadOnlyMaterials(true);
211 void ItemCAO::removeFromScene()
220 void ItemCAO::updateLight(u8 light_at_pos)
225 u8 li = decode_light(light_at_pos);
226 video::SColor color(255,li,li,li);
228 scene::IMesh *mesh = m_node->getMesh();
232 u16 mc = mesh->getMeshBufferCount();
233 for(u16 j=0; j<mc; j++)
235 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
236 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
237 u16 vc = buf->getVertexCount();
238 for(u16 i=0; i<vc; i++)
240 vertices[i].Color = color;
245 v3s16 ItemCAO::getLightPosition()
247 return floatToInt(m_position, BS);
250 void ItemCAO::updateNodePos()
255 m_node->setPosition(m_position);
258 void ItemCAO::step(float dtime, ClientEnvironment *env)
262 /*v3f rot = m_node->getRotation();
263 rot.Y += dtime * 120;
264 m_node->setRotation(rot);*/
265 LocalPlayer *player = env->getLocalPlayer();
267 v3f rot = m_node->getRotation();
268 rot.Y = 180.0 - (player->getYaw());
269 m_node->setRotation(rot);
273 void ItemCAO::processMessage(const std::string &data)
275 dstream<<"ItemCAO: Got message"<<std::endl;
276 std::istringstream is(data, std::ios::binary);
282 m_position = readV3F1000(is);
287 void ItemCAO::initialize(const std::string &data)
289 dstream<<"ItemCAO: Got init data"<<std::endl;
292 std::istringstream is(data, std::ios::binary);
294 u8 version = readU8(is);
299 m_position = readV3F1000(is);
301 m_inventorystring = deSerializeString(is);
313 scene::IMesh *mesh = m_node->getMesh();
318 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
323 // Create an inventory item to see what is its image
324 std::istringstream is(m_inventorystring, std::ios_base::binary);
325 video::ITexture *texture = NULL;
327 InventoryItem *item = NULL;
328 item = InventoryItem::deSerialize(is);
329 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
330 <<m_inventorystring<<"\" -> item="<<item
334 texture = item->getImage();
338 catch(SerializationError &e)
340 dstream<<"WARNING: "<<__FUNCTION_NAME
341 <<": error deSerializing inventorystring \""
342 <<m_inventorystring<<"\""<<std::endl;
345 // Set meshbuffer texture
346 buf->getMaterial().setTexture(0, texture);
354 #include "inventory.h"
360 ClientActiveObject(0),
361 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS/2.,BS/3.),
363 m_position(v3f(0,10*BS,0)),
366 ClientActiveObject::registerType(getType(), create);
373 ClientActiveObject* RatCAO::create()
378 void RatCAO::addToScene(scene::ISceneManager *smgr)
383 video::IVideoDriver* driver = smgr->getVideoDriver();
385 scene::SMesh *mesh = new scene::SMesh();
386 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
387 video::SColor c(255,255,255,255);
388 video::S3DVertex vertices[4] =
390 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
391 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
392 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
393 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
395 u16 indices[] = {0,1,2,2,3,0};
396 buf->append(vertices, 4, indices, 6);
398 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
399 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
400 //buf->getMaterial().setTexture(0, NULL);
401 buf->getMaterial().setTexture
402 (0, driver->getTexture(getTexturePath("rat.png").c_str()));
403 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
404 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
405 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
407 mesh->addMeshBuffer(buf);
409 m_node = smgr->addMeshSceneNode(mesh, NULL);
411 // Set it to use the materials of the meshbuffers directly.
412 // This is needed for changing the texture in the future
413 m_node->setReadOnlyMaterials(true);
417 void RatCAO::removeFromScene()
426 void RatCAO::updateLight(u8 light_at_pos)
431 u8 li = decode_light(light_at_pos);
432 video::SColor color(255,li,li,li);
434 scene::IMesh *mesh = m_node->getMesh();
438 u16 mc = mesh->getMeshBufferCount();
439 for(u16 j=0; j<mc; j++)
441 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
442 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
443 u16 vc = buf->getVertexCount();
444 for(u16 i=0; i<vc; i++)
446 vertices[i].Color = color;
451 v3s16 RatCAO::getLightPosition()
453 return floatToInt(m_position+v3f(0,BS*0.5,0), BS);
456 void RatCAO::updateNodePos()
461 //m_node->setPosition(m_position);
462 m_node->setPosition(pos_translator.vect_show);
464 v3f rot = m_node->getRotation();
465 rot.Y = 180.0 - m_yaw;
466 m_node->setRotation(rot);
469 void RatCAO::step(float dtime, ClientEnvironment *env)
471 pos_translator.translate(dtime);
475 void RatCAO::processMessage(const std::string &data)
477 //dstream<<"RatCAO: Got message"<<std::endl;
478 std::istringstream is(data, std::ios::binary);
484 m_position = readV3F1000(is);
485 pos_translator.update(m_position);
487 m_yaw = readF1000(is);
492 void RatCAO::initialize(const std::string &data)
494 //dstream<<"RatCAO: Got init data"<<std::endl;
497 std::istringstream is(data, std::ios::binary);
499 u8 version = readU8(is);
504 m_position = readV3F1000(is);
505 pos_translator.init(m_position);
515 #include "inventory.h"
518 Oerkki1CAO proto_Oerkki1CAO;
520 Oerkki1CAO::Oerkki1CAO():
521 ClientActiveObject(0),
522 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.,BS/3.),
524 m_position(v3f(0,10*BS,0)),
526 m_damage_visual_timer(0),
527 m_damage_texture_enabled(false)
529 ClientActiveObject::registerType(getType(), create);
532 Oerkki1CAO::~Oerkki1CAO()
536 ClientActiveObject* Oerkki1CAO::create()
538 return new Oerkki1CAO();
541 void Oerkki1CAO::addToScene(scene::ISceneManager *smgr)
546 video::IVideoDriver* driver = smgr->getVideoDriver();
548 scene::SMesh *mesh = new scene::SMesh();
549 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
550 video::SColor c(255,255,255,255);
551 video::S3DVertex vertices[4] =
553 video::S3DVertex(-BS/2-BS,0,0, 0,0,0, c, 0,1),
554 video::S3DVertex(BS/2+BS,0,0, 0,0,0, c, 1,1),
555 video::S3DVertex(BS/2+BS,BS*2,0, 0,0,0, c, 1,0),
556 video::S3DVertex(-BS/2-BS,BS*2,0, 0,0,0, c, 0,0),
558 u16 indices[] = {0,1,2,2,3,0};
559 buf->append(vertices, 4, indices, 6);
561 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
562 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
563 //buf->getMaterial().setTexture(0, NULL);
564 buf->getMaterial().setTexture
565 (0, driver->getTexture(getTexturePath("oerkki1.png").c_str()));
566 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
567 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
568 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
570 mesh->addMeshBuffer(buf);
572 m_node = smgr->addMeshSceneNode(mesh, NULL);
574 // Set it to use the materials of the meshbuffers directly.
575 // This is needed for changing the texture in the future
576 m_node->setReadOnlyMaterials(true);
580 void Oerkki1CAO::removeFromScene()
589 void Oerkki1CAO::updateLight(u8 light_at_pos)
594 if(light_at_pos <= 2)
596 m_node->setVisible(false);
600 m_node->setVisible(true);
602 u8 li = decode_light(light_at_pos);
603 video::SColor color(255,li,li,li);
605 scene::IMesh *mesh = m_node->getMesh();
609 u16 mc = mesh->getMeshBufferCount();
610 for(u16 j=0; j<mc; j++)
612 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
613 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
614 u16 vc = buf->getVertexCount();
615 for(u16 i=0; i<vc; i++)
617 vertices[i].Color = color;
622 v3s16 Oerkki1CAO::getLightPosition()
624 return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
627 void Oerkki1CAO::updateNodePos()
632 //m_node->setPosition(m_position);
633 m_node->setPosition(pos_translator.vect_show);
635 v3f rot = m_node->getRotation();
636 rot.Y = 180.0 - m_yaw + 90.0;
637 m_node->setRotation(rot);
640 void Oerkki1CAO::step(float dtime, ClientEnvironment *env)
642 pos_translator.translate(dtime);
645 LocalPlayer *player = env->getLocalPlayer();
648 v3f playerpos = player->getPosition();
649 v2f playerpos_2d(playerpos.X,playerpos.Z);
650 v2f objectpos_2d(m_position.X,m_position.Z);
652 if(fabs(m_position.Y - playerpos.Y) < 3.0*BS &&
653 objectpos_2d.getDistanceFrom(playerpos_2d) < 1.5*BS)
655 if(m_attack_interval.step(dtime, 0.5))
657 env->damageLocalPlayer(2);
661 if(m_damage_visual_timer > 0)
663 if(!m_damage_texture_enabled)
665 // Enable damage texture
668 video::IVideoDriver* driver =
669 m_node->getSceneManager()->getVideoDriver();
671 scene::IMesh *mesh = m_node->getMesh();
675 u16 mc = mesh->getMeshBufferCount();
676 for(u16 j=0; j<mc; j++)
678 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
679 buf->getMaterial().setTexture(0, driver->getTexture(
680 getTexturePath("oerkki1_damaged.png").c_str()));
683 m_damage_texture_enabled = true;
685 m_damage_visual_timer -= dtime;
689 if(m_damage_texture_enabled)
691 // Disable damage texture
694 video::IVideoDriver* driver =
695 m_node->getSceneManager()->getVideoDriver();
697 scene::IMesh *mesh = m_node->getMesh();
701 u16 mc = mesh->getMeshBufferCount();
702 for(u16 j=0; j<mc; j++)
704 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
705 buf->getMaterial().setTexture(0, driver->getTexture(
706 getTexturePath("oerkki1.png").c_str()));
709 m_damage_texture_enabled = false;
714 void Oerkki1CAO::processMessage(const std::string &data)
716 //dstream<<"Oerkki1CAO: Got message"<<std::endl;
717 std::istringstream is(data, std::ios::binary);
723 m_position = readV3F1000(is);
724 pos_translator.update(m_position);
726 m_yaw = readF1000(is);
731 u16 damage = readU8(is);
732 m_damage_visual_timer = 1.0;
736 void Oerkki1CAO::initialize(const std::string &data)
738 //dstream<<"Oerkki1CAO: Got init data"<<std::endl;
741 std::istringstream is(data, std::ios::binary);
743 u8 version = readU8(is);
748 m_position = readV3F1000(is);
749 pos_translator.init(m_position);
760 FireflyCAO proto_FireflyCAO;
762 FireflyCAO::FireflyCAO():
763 ClientActiveObject(0),
764 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS/2.,BS/3.),
766 m_position(v3f(0,10*BS,0)),
769 ClientActiveObject::registerType(getType(), create);
772 FireflyCAO::~FireflyCAO()
776 ClientActiveObject* FireflyCAO::create()
778 return new FireflyCAO();
781 void FireflyCAO::addToScene(scene::ISceneManager *smgr)
786 video::IVideoDriver* driver = smgr->getVideoDriver();
788 scene::SMesh *mesh = new scene::SMesh();
789 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
790 video::SColor c(255,255,255,255);
791 video::S3DVertex vertices[4] =
793 video::S3DVertex(0,0,0, 0,0,0, c, 0,1),
794 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
795 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
796 video::S3DVertex(0,BS/2,0, 0,0,0, c, 0,0),
798 u16 indices[] = {0,1,2,2,3,0};
799 buf->append(vertices, 4, indices, 6);
801 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
802 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
803 //buf->getMaterial().setTexture(0, NULL);
804 buf->getMaterial().setTexture
805 (0, driver->getTexture(getTexturePath("firefly.png").c_str()));
806 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
807 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
808 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
810 mesh->addMeshBuffer(buf);
812 m_node = smgr->addMeshSceneNode(mesh, NULL);
814 // Set it to use the materials of the meshbuffers directly.
815 // This is needed for changing the texture in the future
816 m_node->setReadOnlyMaterials(true);
820 void FireflyCAO::removeFromScene()
829 void FireflyCAO::updateLight(u8 light_at_pos)
835 video::SColor color(255,li,li,li);
837 scene::IMesh *mesh = m_node->getMesh();
841 u16 mc = mesh->getMeshBufferCount();
842 for(u16 j=0; j<mc; j++)
844 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
845 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
846 u16 vc = buf->getVertexCount();
847 for(u16 i=0; i<vc; i++)
849 vertices[i].Color = color;
854 v3s16 FireflyCAO::getLightPosition()
856 return floatToInt(m_position+v3f(0,BS*0.5,0), BS);
859 void FireflyCAO::updateNodePos()
864 //m_node->setPosition(m_position);
865 m_node->setPosition(pos_translator.vect_show);
867 v3f rot = m_node->getRotation();
868 rot.Y = 180.0 - m_yaw;
869 m_node->setRotation(rot);
872 void FireflyCAO::step(float dtime, ClientEnvironment *env)
874 pos_translator.translate(dtime);
878 void FireflyCAO::processMessage(const std::string &data)
880 //dstream<<"FireflyCAO: Got message"<<std::endl;
881 std::istringstream is(data, std::ios::binary);
887 m_position = readV3F1000(is);
888 pos_translator.update(m_position);
890 m_yaw = readF1000(is);
895 void FireflyCAO::initialize(const std::string &data)
897 //dstream<<"FireflyCAO: Got init data"<<std::endl;
900 std::istringstream is(data, std::ios::binary);
902 u8 version = readU8(is);
907 m_position = readV3F1000(is);
908 pos_translator.init(m_position);