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"
29 TestCAO proto_TestCAO;
32 ClientActiveObject(0),
34 m_position(v3f(0,10*BS,0))
36 ClientActiveObject::registerType(getType(), create);
43 ClientActiveObject* TestCAO::create()
48 void TestCAO::addToScene(scene::ISceneManager *smgr)
53 video::IVideoDriver* driver = smgr->getVideoDriver();
55 scene::SMesh *mesh = new scene::SMesh();
56 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
57 video::SColor c(255,255,255,255);
58 video::S3DVertex vertices[4] =
60 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
61 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
62 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
63 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
65 u16 indices[] = {0,1,2,2,3,0};
66 buf->append(vertices, 4, indices, 6);
68 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
69 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
70 buf->getMaterial().setTexture
71 (0, driver->getTexture(getTexturePath("rat.png").c_str()));
72 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
73 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
74 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
76 mesh->addMeshBuffer(buf);
78 m_node = smgr->addMeshSceneNode(mesh, NULL);
83 void TestCAO::removeFromScene()
92 void TestCAO::updateLight(u8 light_at_pos)
96 v3s16 TestCAO::getLightPosition()
98 return floatToInt(m_position, BS);
101 void TestCAO::updateNodePos()
106 m_node->setPosition(m_position);
107 //m_node->setRotation(v3f(0, 45, 0));
110 void TestCAO::step(float dtime, ClientEnvironment *env)
114 v3f rot = m_node->getRotation();
115 //dstream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
116 rot.Y += dtime * 180;
117 m_node->setRotation(rot);
121 void TestCAO::processMessage(const std::string &data)
123 dstream<<"TestCAO: Got data: "<<data<<std::endl;
124 std::istringstream is(data, std::ios::binary);
142 #include "inventory.h"
145 ItemCAO proto_ItemCAO;
148 ClientActiveObject(0),
149 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
151 m_position(v3f(0,10*BS,0))
153 ClientActiveObject::registerType(getType(), create);
160 ClientActiveObject* ItemCAO::create()
162 return new ItemCAO();
165 void ItemCAO::addToScene(scene::ISceneManager *smgr)
170 video::IVideoDriver* driver = smgr->getVideoDriver();
172 scene::SMesh *mesh = new scene::SMesh();
173 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
174 video::SColor c(255,255,255,255);
175 video::S3DVertex vertices[4] =
177 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
178 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
179 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
180 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
181 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
182 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
183 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
184 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
186 u16 indices[] = {0,1,2,2,3,0};
187 buf->append(vertices, 4, indices, 6);
189 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
190 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
191 //buf->getMaterial().setTexture(0, NULL);
192 // Initialize with the stick texture
193 buf->getMaterial().setTexture
194 (0, driver->getTexture(getTexturePath("stick.png").c_str()));
195 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
196 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
197 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
199 mesh->addMeshBuffer(buf);
201 m_node = smgr->addMeshSceneNode(mesh, NULL);
203 // Set it to use the materials of the meshbuffers directly.
204 // This is needed for changing the texture in the future
205 m_node->setReadOnlyMaterials(true);
209 void ItemCAO::removeFromScene()
218 void ItemCAO::updateLight(u8 light_at_pos)
223 u8 li = decode_light(light_at_pos);
224 video::SColor color(255,li,li,li);
226 scene::IMesh *mesh = m_node->getMesh();
230 u16 mc = mesh->getMeshBufferCount();
231 for(u16 j=0; j<mc; j++)
233 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
234 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
235 u16 vc = buf->getVertexCount();
236 for(u16 i=0; i<vc; i++)
238 vertices[i].Color = color;
243 v3s16 ItemCAO::getLightPosition()
245 return floatToInt(m_position, BS);
248 void ItemCAO::updateNodePos()
253 m_node->setPosition(m_position);
256 void ItemCAO::step(float dtime, ClientEnvironment *env)
260 /*v3f rot = m_node->getRotation();
261 rot.Y += dtime * 120;
262 m_node->setRotation(rot);*/
263 LocalPlayer *player = env->getLocalPlayer();
265 v3f rot = m_node->getRotation();
266 rot.Y = 180.0 - (player->getYaw());
267 m_node->setRotation(rot);
271 void ItemCAO::processMessage(const std::string &data)
273 dstream<<"ItemCAO: Got message"<<std::endl;
274 std::istringstream is(data, std::ios::binary);
280 m_position = readV3F1000(is);
285 void ItemCAO::initialize(const std::string &data)
287 dstream<<"ItemCAO: Got init data"<<std::endl;
290 std::istringstream is(data, std::ios::binary);
292 u8 version = readU8(is);
297 m_position = readV3F1000(is);
299 m_inventorystring = deSerializeString(is);
311 scene::IMesh *mesh = m_node->getMesh();
316 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
321 // Create an inventory item to see what is its image
322 std::istringstream is(m_inventorystring, std::ios_base::binary);
323 video::ITexture *texture = NULL;
325 InventoryItem *item = NULL;
326 item = InventoryItem::deSerialize(is);
327 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
328 <<m_inventorystring<<"\" -> item="<<item
332 texture = item->getImage();
336 catch(SerializationError &e)
338 dstream<<"WARNING: "<<__FUNCTION_NAME
339 <<": error deSerializing inventorystring \""
340 <<m_inventorystring<<"\""<<std::endl;
343 // Set meshbuffer texture
344 buf->getMaterial().setTexture(0, texture);
352 #include "inventory.h"
358 ClientActiveObject(0),
359 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS/2.,BS/3.),
361 m_position(v3f(0,10*BS,0)),
364 ClientActiveObject::registerType(getType(), create);
371 ClientActiveObject* RatCAO::create()
376 void RatCAO::addToScene(scene::ISceneManager *smgr)
381 video::IVideoDriver* driver = smgr->getVideoDriver();
383 scene::SMesh *mesh = new scene::SMesh();
384 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
385 video::SColor c(255,255,255,255);
386 video::S3DVertex vertices[4] =
388 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
389 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
390 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
391 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
393 u16 indices[] = {0,1,2,2,3,0};
394 buf->append(vertices, 4, indices, 6);
396 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
397 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
398 //buf->getMaterial().setTexture(0, NULL);
399 buf->getMaterial().setTexture
400 (0, driver->getTexture(getTexturePath("rat.png").c_str()));
401 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
402 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
403 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
405 mesh->addMeshBuffer(buf);
407 m_node = smgr->addMeshSceneNode(mesh, NULL);
409 // Set it to use the materials of the meshbuffers directly.
410 // This is needed for changing the texture in the future
411 m_node->setReadOnlyMaterials(true);
415 void RatCAO::removeFromScene()
424 void RatCAO::updateLight(u8 light_at_pos)
429 u8 li = decode_light(light_at_pos);
430 video::SColor color(255,li,li,li);
432 scene::IMesh *mesh = m_node->getMesh();
436 u16 mc = mesh->getMeshBufferCount();
437 for(u16 j=0; j<mc; j++)
439 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
440 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
441 u16 vc = buf->getVertexCount();
442 for(u16 i=0; i<vc; i++)
444 vertices[i].Color = color;
449 v3s16 RatCAO::getLightPosition()
451 return floatToInt(m_position+v3f(0,BS*0.5,0), BS);
454 void RatCAO::updateNodePos()
459 //m_node->setPosition(m_position);
460 m_node->setPosition(pos_translator.vect_show);
462 v3f rot = m_node->getRotation();
463 rot.Y = 180.0 - m_yaw;
464 m_node->setRotation(rot);
467 void RatCAO::step(float dtime, ClientEnvironment *env)
469 pos_translator.translate(dtime);
473 void RatCAO::processMessage(const std::string &data)
475 //dstream<<"RatCAO: Got message"<<std::endl;
476 std::istringstream is(data, std::ios::binary);
482 m_position = readV3F1000(is);
483 pos_translator.update(m_position);
485 m_yaw = readF1000(is);
490 void RatCAO::initialize(const std::string &data)
492 //dstream<<"RatCAO: Got init data"<<std::endl;
495 std::istringstream is(data, std::ios::binary);
497 u8 version = readU8(is);
502 m_position = readV3F1000(is);
503 pos_translator.init(m_position);
513 #include "inventory.h"
516 Oerkki1CAO proto_Oerkki1CAO;
518 Oerkki1CAO::Oerkki1CAO():
519 ClientActiveObject(0),
520 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.,BS/3.),
522 m_position(v3f(0,10*BS,0)),
524 m_damage_visual_timer(0),
525 m_damage_texture_enabled(false)
527 ClientActiveObject::registerType(getType(), create);
530 Oerkki1CAO::~Oerkki1CAO()
534 ClientActiveObject* Oerkki1CAO::create()
536 return new Oerkki1CAO();
539 void Oerkki1CAO::addToScene(scene::ISceneManager *smgr)
544 video::IVideoDriver* driver = smgr->getVideoDriver();
546 scene::SMesh *mesh = new scene::SMesh();
547 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
548 video::SColor c(255,255,255,255);
549 video::S3DVertex vertices[4] =
551 video::S3DVertex(-BS/2-BS,0,0, 0,0,0, c, 0,1),
552 video::S3DVertex(BS/2+BS,0,0, 0,0,0, c, 1,1),
553 video::S3DVertex(BS/2+BS,BS*2,0, 0,0,0, c, 1,0),
554 video::S3DVertex(-BS/2-BS,BS*2,0, 0,0,0, c, 0,0),
556 u16 indices[] = {0,1,2,2,3,0};
557 buf->append(vertices, 4, indices, 6);
559 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
560 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
561 //buf->getMaterial().setTexture(0, NULL);
562 buf->getMaterial().setTexture
563 (0, driver->getTexture(getTexturePath("oerkki1.png").c_str()));
564 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
565 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
566 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
568 mesh->addMeshBuffer(buf);
570 m_node = smgr->addMeshSceneNode(mesh, NULL);
572 // Set it to use the materials of the meshbuffers directly.
573 // This is needed for changing the texture in the future
574 m_node->setReadOnlyMaterials(true);
578 void Oerkki1CAO::removeFromScene()
587 void Oerkki1CAO::updateLight(u8 light_at_pos)
592 if(light_at_pos <= 2)
594 m_node->setVisible(false);
598 m_node->setVisible(true);
600 u8 li = decode_light(light_at_pos);
601 video::SColor color(255,li,li,li);
603 scene::IMesh *mesh = m_node->getMesh();
607 u16 mc = mesh->getMeshBufferCount();
608 for(u16 j=0; j<mc; j++)
610 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
611 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
612 u16 vc = buf->getVertexCount();
613 for(u16 i=0; i<vc; i++)
615 vertices[i].Color = color;
620 v3s16 Oerkki1CAO::getLightPosition()
622 return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
625 void Oerkki1CAO::updateNodePos()
630 //m_node->setPosition(m_position);
631 m_node->setPosition(pos_translator.vect_show);
633 v3f rot = m_node->getRotation();
634 rot.Y = 180.0 - m_yaw + 90.0;
635 m_node->setRotation(rot);
638 void Oerkki1CAO::step(float dtime, ClientEnvironment *env)
640 pos_translator.translate(dtime);
643 LocalPlayer *player = env->getLocalPlayer();
646 v3f playerpos = player->getPosition();
647 v2f playerpos_2d(playerpos.X,playerpos.Z);
648 v2f objectpos_2d(m_position.X,m_position.Z);
650 if(fabs(m_position.Y - playerpos.Y) < 3.0*BS &&
651 objectpos_2d.getDistanceFrom(playerpos_2d) < 1.5*BS)
653 if(m_attack_interval.step(dtime, 0.5))
655 env->damageLocalPlayer(2);
659 if(m_damage_visual_timer > 0)
661 if(!m_damage_texture_enabled)
663 // Enable damage texture
666 video::IVideoDriver* driver =
667 m_node->getSceneManager()->getVideoDriver();
669 scene::IMesh *mesh = m_node->getMesh();
673 u16 mc = mesh->getMeshBufferCount();
674 for(u16 j=0; j<mc; j++)
676 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
677 buf->getMaterial().setTexture(0, driver->getTexture(
678 getTexturePath("oerkki1_damaged.png").c_str()));
681 m_damage_texture_enabled = true;
683 m_damage_visual_timer -= dtime;
687 if(m_damage_texture_enabled)
689 // Disable damage texture
692 video::IVideoDriver* driver =
693 m_node->getSceneManager()->getVideoDriver();
695 scene::IMesh *mesh = m_node->getMesh();
699 u16 mc = mesh->getMeshBufferCount();
700 for(u16 j=0; j<mc; j++)
702 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
703 buf->getMaterial().setTexture(0, driver->getTexture(
704 getTexturePath("oerkki1.png").c_str()));
707 m_damage_texture_enabled = false;
712 void Oerkki1CAO::processMessage(const std::string &data)
714 //dstream<<"Oerkki1CAO: Got message"<<std::endl;
715 std::istringstream is(data, std::ios::binary);
721 m_position = readV3F1000(is);
722 pos_translator.update(m_position);
724 m_yaw = readF1000(is);
729 u16 damage = readU8(is);
730 m_damage_visual_timer = 1.0;
734 void Oerkki1CAO::initialize(const std::string &data)
736 //dstream<<"Oerkki1CAO: Got init data"<<std::endl;
739 std::istringstream is(data, std::ios::binary);
741 u8 version = readU8(is);
746 m_position = readV3F1000(is);
747 pos_translator.init(m_position);