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);
227 setMeshVerticesColor(m_node->getMesh(), color);
230 v3s16 ItemCAO::getLightPosition()
232 return floatToInt(m_position, BS);
235 void ItemCAO::updateNodePos()
240 m_node->setPosition(m_position);
243 void ItemCAO::step(float dtime, ClientEnvironment *env)
247 /*v3f rot = m_node->getRotation();
248 rot.Y += dtime * 120;
249 m_node->setRotation(rot);*/
250 LocalPlayer *player = env->getLocalPlayer();
252 v3f rot = m_node->getRotation();
253 rot.Y = 180.0 - (player->getYaw());
254 m_node->setRotation(rot);
258 void ItemCAO::processMessage(const std::string &data)
260 dstream<<"ItemCAO: Got message"<<std::endl;
261 std::istringstream is(data, std::ios::binary);
267 m_position = readV3F1000(is);
272 void ItemCAO::initialize(const std::string &data)
274 dstream<<"ItemCAO: Got init data"<<std::endl;
277 std::istringstream is(data, std::ios::binary);
279 u8 version = readU8(is);
284 m_position = readV3F1000(is);
286 m_inventorystring = deSerializeString(is);
298 scene::IMesh *mesh = m_node->getMesh();
303 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
308 // Create an inventory item to see what is its image
309 std::istringstream is(m_inventorystring, std::ios_base::binary);
310 video::ITexture *texture = NULL;
312 InventoryItem *item = NULL;
313 item = InventoryItem::deSerialize(is);
314 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
315 <<m_inventorystring<<"\" -> item="<<item
319 texture = item->getImage();
323 catch(SerializationError &e)
325 dstream<<"WARNING: "<<__FUNCTION_NAME
326 <<": error deSerializing inventorystring \""
327 <<m_inventorystring<<"\""<<std::endl;
330 // Set meshbuffer texture
331 buf->getMaterial().setTexture(0, texture);
339 #include "inventory.h"
345 ClientActiveObject(0),
346 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS/2.,BS/3.),
348 m_position(v3f(0,10*BS,0)),
351 ClientActiveObject::registerType(getType(), create);
358 ClientActiveObject* RatCAO::create()
363 void RatCAO::addToScene(scene::ISceneManager *smgr)
368 video::IVideoDriver* driver = smgr->getVideoDriver();
370 scene::SMesh *mesh = new scene::SMesh();
371 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
372 video::SColor c(255,255,255,255);
373 video::S3DVertex vertices[4] =
375 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
376 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
377 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
378 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
380 u16 indices[] = {0,1,2,2,3,0};
381 buf->append(vertices, 4, indices, 6);
383 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
384 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
385 //buf->getMaterial().setTexture(0, NULL);
386 buf->getMaterial().setTexture
387 (0, driver->getTexture(getTexturePath("rat.png").c_str()));
388 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
389 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
390 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
392 mesh->addMeshBuffer(buf);
394 m_node = smgr->addMeshSceneNode(mesh, NULL);
396 // Set it to use the materials of the meshbuffers directly.
397 // This is needed for changing the texture in the future
398 m_node->setReadOnlyMaterials(true);
402 void RatCAO::removeFromScene()
411 void RatCAO::updateLight(u8 light_at_pos)
416 u8 li = decode_light(light_at_pos);
417 video::SColor color(255,li,li,li);
418 setMeshVerticesColor(m_node->getMesh(), color);
421 v3s16 RatCAO::getLightPosition()
423 return floatToInt(m_position+v3f(0,BS*0.5,0), BS);
426 void RatCAO::updateNodePos()
431 //m_node->setPosition(m_position);
432 m_node->setPosition(pos_translator.vect_show);
434 v3f rot = m_node->getRotation();
435 rot.Y = 180.0 - m_yaw;
436 m_node->setRotation(rot);
439 void RatCAO::step(float dtime, ClientEnvironment *env)
441 pos_translator.translate(dtime);
445 void RatCAO::processMessage(const std::string &data)
447 //dstream<<"RatCAO: Got message"<<std::endl;
448 std::istringstream is(data, std::ios::binary);
454 m_position = readV3F1000(is);
455 pos_translator.update(m_position);
457 m_yaw = readF1000(is);
462 void RatCAO::initialize(const std::string &data)
464 //dstream<<"RatCAO: Got init data"<<std::endl;
467 std::istringstream is(data, std::ios::binary);
469 u8 version = readU8(is);
474 m_position = readV3F1000(is);
475 pos_translator.init(m_position);
485 #include "inventory.h"
488 Oerkki1CAO proto_Oerkki1CAO;
490 Oerkki1CAO::Oerkki1CAO():
491 ClientActiveObject(0),
492 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.,BS/3.),
494 m_position(v3f(0,10*BS,0)),
496 m_damage_visual_timer(0),
497 m_damage_texture_enabled(false)
499 ClientActiveObject::registerType(getType(), create);
502 Oerkki1CAO::~Oerkki1CAO()
506 ClientActiveObject* Oerkki1CAO::create()
508 return new Oerkki1CAO();
511 void Oerkki1CAO::addToScene(scene::ISceneManager *smgr)
516 video::IVideoDriver* driver = smgr->getVideoDriver();
518 scene::SMesh *mesh = new scene::SMesh();
519 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
520 video::SColor c(255,255,255,255);
521 video::S3DVertex vertices[4] =
523 video::S3DVertex(-BS/2-BS,0,0, 0,0,0, c, 0,1),
524 video::S3DVertex(BS/2+BS,0,0, 0,0,0, c, 1,1),
525 video::S3DVertex(BS/2+BS,BS*2,0, 0,0,0, c, 1,0),
526 video::S3DVertex(-BS/2-BS,BS*2,0, 0,0,0, c, 0,0),
528 u16 indices[] = {0,1,2,2,3,0};
529 buf->append(vertices, 4, indices, 6);
531 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
532 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
533 //buf->getMaterial().setTexture(0, NULL);
534 buf->getMaterial().setTexture
535 (0, driver->getTexture(getTexturePath("oerkki1.png").c_str()));
536 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
537 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
538 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
540 mesh->addMeshBuffer(buf);
542 m_node = smgr->addMeshSceneNode(mesh, NULL);
544 // Set it to use the materials of the meshbuffers directly.
545 // This is needed for changing the texture in the future
546 m_node->setReadOnlyMaterials(true);
550 void Oerkki1CAO::removeFromScene()
559 void Oerkki1CAO::updateLight(u8 light_at_pos)
564 if(light_at_pos <= 2)
566 m_node->setVisible(false);
570 m_node->setVisible(true);
572 u8 li = decode_light(light_at_pos);
573 video::SColor color(255,li,li,li);
574 setMeshVerticesColor(m_node->getMesh(), color);
577 v3s16 Oerkki1CAO::getLightPosition()
579 return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
582 void Oerkki1CAO::updateNodePos()
587 //m_node->setPosition(m_position);
588 m_node->setPosition(pos_translator.vect_show);
590 v3f rot = m_node->getRotation();
591 rot.Y = 180.0 - m_yaw + 90.0;
592 m_node->setRotation(rot);
595 void Oerkki1CAO::step(float dtime, ClientEnvironment *env)
597 pos_translator.translate(dtime);
600 LocalPlayer *player = env->getLocalPlayer();
603 v3f playerpos = player->getPosition();
604 v2f playerpos_2d(playerpos.X,playerpos.Z);
605 v2f objectpos_2d(m_position.X,m_position.Z);
607 if(fabs(m_position.Y - playerpos.Y) < 3.0*BS &&
608 objectpos_2d.getDistanceFrom(playerpos_2d) < 1.5*BS)
610 if(m_attack_interval.step(dtime, 0.5))
612 env->damageLocalPlayer(2);
616 if(m_damage_visual_timer > 0)
618 if(!m_damage_texture_enabled)
620 // Enable damage texture
623 video::IVideoDriver* driver =
624 m_node->getSceneManager()->getVideoDriver();
626 scene::IMesh *mesh = m_node->getMesh();
630 u16 mc = mesh->getMeshBufferCount();
631 for(u16 j=0; j<mc; j++)
633 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
634 buf->getMaterial().setTexture(0, driver->getTexture(
635 getTexturePath("oerkki1_damaged.png").c_str()));
638 m_damage_texture_enabled = true;
640 m_damage_visual_timer -= dtime;
644 if(m_damage_texture_enabled)
646 // Disable damage texture
649 video::IVideoDriver* driver =
650 m_node->getSceneManager()->getVideoDriver();
652 scene::IMesh *mesh = m_node->getMesh();
656 u16 mc = mesh->getMeshBufferCount();
657 for(u16 j=0; j<mc; j++)
659 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
660 buf->getMaterial().setTexture(0, driver->getTexture(
661 getTexturePath("oerkki1.png").c_str()));
664 m_damage_texture_enabled = false;
669 void Oerkki1CAO::processMessage(const std::string &data)
671 //dstream<<"Oerkki1CAO: Got message"<<std::endl;
672 std::istringstream is(data, std::ios::binary);
678 m_position = readV3F1000(is);
679 pos_translator.update(m_position);
681 m_yaw = readF1000(is);
686 u16 damage = readU8(is);
687 m_damage_visual_timer = 1.0;
691 void Oerkki1CAO::initialize(const std::string &data)
693 //dstream<<"Oerkki1CAO: Got init data"<<std::endl;
696 std::istringstream is(data, std::ios::binary);
698 u8 version = readU8(is);
703 m_position = readV3F1000(is);
704 pos_translator.init(m_position);
715 FireflyCAO proto_FireflyCAO;
717 FireflyCAO::FireflyCAO():
718 ClientActiveObject(0),
719 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS/2.,BS/3.),
721 m_position(v3f(0,10*BS,0)),
724 ClientActiveObject::registerType(getType(), create);
727 FireflyCAO::~FireflyCAO()
731 ClientActiveObject* FireflyCAO::create()
733 return new FireflyCAO();
736 void FireflyCAO::addToScene(scene::ISceneManager *smgr)
741 video::IVideoDriver* driver = smgr->getVideoDriver();
743 scene::SMesh *mesh = new scene::SMesh();
744 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
745 video::SColor c(255,255,255,255);
746 video::S3DVertex vertices[4] =
748 video::S3DVertex(0,0,0, 0,0,0, c, 0,1),
749 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
750 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
751 video::S3DVertex(0,BS/2,0, 0,0,0, c, 0,0),
753 u16 indices[] = {0,1,2,2,3,0};
754 buf->append(vertices, 4, indices, 6);
756 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
757 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
758 //buf->getMaterial().setTexture(0, NULL);
759 buf->getMaterial().setTexture
760 (0, driver->getTexture(getTexturePath("firefly.png").c_str()));
761 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
762 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
763 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
765 mesh->addMeshBuffer(buf);
767 m_node = smgr->addMeshSceneNode(mesh, NULL);
769 // Set it to use the materials of the meshbuffers directly.
770 // This is needed for changing the texture in the future
771 m_node->setReadOnlyMaterials(true);
775 void FireflyCAO::removeFromScene()
784 void FireflyCAO::updateLight(u8 light_at_pos)
790 video::SColor color(255,li,li,li);
791 setMeshVerticesColor(m_node->getMesh(), color);
794 v3s16 FireflyCAO::getLightPosition()
796 return floatToInt(m_position+v3f(0,BS*0.5,0), BS);
799 void FireflyCAO::updateNodePos()
804 //m_node->setPosition(m_position);
805 m_node->setPosition(pos_translator.vect_show);
807 v3f rot = m_node->getRotation();
808 rot.Y = 180.0 - m_yaw;
809 m_node->setRotation(rot);
812 void FireflyCAO::step(float dtime, ClientEnvironment *env)
814 pos_translator.translate(dtime);
818 void FireflyCAO::processMessage(const std::string &data)
820 //dstream<<"FireflyCAO: Got message"<<std::endl;
821 std::istringstream is(data, std::ios::binary);
827 m_position = readV3F1000(is);
828 pos_translator.update(m_position);
830 m_yaw = readF1000(is);
835 void FireflyCAO::initialize(const std::string &data)
837 //dstream<<"FireflyCAO: Got init data"<<std::endl;
840 std::istringstream is(data, std::ios::binary);
842 u8 version = readU8(is);
847 m_position = readV3F1000(is);
848 pos_translator.init(m_position);