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 "clientobject.h"
23 #include "constants.h"
25 #include "environment.h"
32 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
34 ClientActiveObject::ClientActiveObject(u16 id):
39 ClientActiveObject::~ClientActiveObject()
44 ClientActiveObject* ClientActiveObject::create(u8 type)
46 // Find factory function
47 core::map<u16, Factory>::Node *n;
48 n = m_types.find(type);
51 // If factory is not found, just return.
52 dstream<<"WARNING: ClientActiveObject: No factory for type="
57 Factory f = n->getValue();
58 ClientActiveObject *object = (*f)();
62 void ClientActiveObject::registerType(u16 type, Factory f)
64 core::map<u16, Factory>::Node *n;
65 n = m_types.find(type);
68 m_types.insert(type, f);
76 TestCAO proto_TestCAO;
79 ClientActiveObject(0),
81 m_position(v3f(0,10*BS,0))
83 ClientActiveObject::registerType(getType(), create);
90 ClientActiveObject* TestCAO::create()
95 void TestCAO::addToScene(scene::ISceneManager *smgr)
100 video::IVideoDriver* driver = smgr->getVideoDriver();
102 scene::SMesh *mesh = new scene::SMesh();
103 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
104 video::SColor c(255,255,255,255);
105 video::S3DVertex vertices[4] =
107 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
108 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
109 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
110 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
112 u16 indices[] = {0,1,2,2,3,0};
113 buf->append(vertices, 4, indices, 6);
115 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
116 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
117 buf->getMaterial().setTexture
118 (0, driver->getTexture(getTexturePath("rat.png").c_str()));
119 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
120 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
121 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
123 mesh->addMeshBuffer(buf);
125 m_node = smgr->addMeshSceneNode(mesh, NULL);
130 void TestCAO::removeFromScene()
139 void TestCAO::updateLight(u8 light_at_pos)
143 v3s16 TestCAO::getLightPosition()
145 return floatToInt(m_position, BS);
148 void TestCAO::updateNodePos()
153 m_node->setPosition(m_position);
154 //m_node->setRotation(v3f(0, 45, 0));
157 void TestCAO::step(float dtime, ClientEnvironment *env)
161 v3f rot = m_node->getRotation();
162 //dstream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
163 rot.Y += dtime * 180;
164 m_node->setRotation(rot);
168 void TestCAO::processMessage(const std::string &data)
170 dstream<<"TestCAO: Got data: "<<data<<std::endl;
171 std::istringstream is(data, std::ios::binary);
189 #include "inventory.h"
192 ItemCAO proto_ItemCAO;
195 ClientActiveObject(0),
196 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
198 m_position(v3f(0,10*BS,0))
200 ClientActiveObject::registerType(getType(), create);
207 ClientActiveObject* ItemCAO::create()
209 return new ItemCAO();
212 void ItemCAO::addToScene(scene::ISceneManager *smgr)
217 video::IVideoDriver* driver = smgr->getVideoDriver();
219 scene::SMesh *mesh = new scene::SMesh();
220 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
221 video::SColor c(255,255,255,255);
222 video::S3DVertex vertices[4] =
224 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
225 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
226 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
227 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
228 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
229 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
230 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
231 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
233 u16 indices[] = {0,1,2,2,3,0};
234 buf->append(vertices, 4, indices, 6);
236 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
237 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
238 //buf->getMaterial().setTexture(0, NULL);
239 // Initialize with the stick texture
240 buf->getMaterial().setTexture
241 (0, driver->getTexture(getTexturePath("stick.png").c_str()));
242 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
243 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
244 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
246 mesh->addMeshBuffer(buf);
248 m_node = smgr->addMeshSceneNode(mesh, NULL);
250 // Set it to use the materials of the meshbuffers directly.
251 // This is needed for changing the texture in the future
252 m_node->setReadOnlyMaterials(true);
256 void ItemCAO::removeFromScene()
265 void ItemCAO::updateLight(u8 light_at_pos)
270 u8 li = decode_light(light_at_pos);
271 video::SColor color(255,li,li,li);
273 scene::IMesh *mesh = m_node->getMesh();
277 u16 mc = mesh->getMeshBufferCount();
278 for(u16 j=0; j<mc; j++)
280 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
281 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
282 u16 vc = buf->getVertexCount();
283 for(u16 i=0; i<vc; i++)
285 vertices[i].Color = color;
290 v3s16 ItemCAO::getLightPosition()
292 return floatToInt(m_position, BS);
295 void ItemCAO::updateNodePos()
300 m_node->setPosition(m_position);
303 void ItemCAO::step(float dtime, ClientEnvironment *env)
307 /*v3f rot = m_node->getRotation();
308 rot.Y += dtime * 120;
309 m_node->setRotation(rot);*/
310 LocalPlayer *player = env->getLocalPlayer();
312 v3f rot = m_node->getRotation();
313 rot.Y = 180.0 - (player->getYaw());
314 m_node->setRotation(rot);
318 void ItemCAO::processMessage(const std::string &data)
320 dstream<<"ItemCAO: Got message"<<std::endl;
321 std::istringstream is(data, std::ios::binary);
327 m_position = readV3F1000(is);
332 void ItemCAO::initialize(const std::string &data)
334 dstream<<"ItemCAO: Got init data"<<std::endl;
337 std::istringstream is(data, std::ios::binary);
339 u8 version = readU8(is);
344 m_position = readV3F1000(is);
346 m_inventorystring = deSerializeString(is);
358 scene::IMesh *mesh = m_node->getMesh();
363 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
368 // Create an inventory item to see what is its image
369 std::istringstream is(m_inventorystring, std::ios_base::binary);
370 video::ITexture *texture = NULL;
372 InventoryItem *item = NULL;
373 item = InventoryItem::deSerialize(is);
374 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
375 <<m_inventorystring<<"\" -> item="<<item
379 texture = item->getImage();
383 catch(SerializationError &e)
385 dstream<<"WARNING: "<<__FUNCTION_NAME
386 <<": error deSerializing inventorystring \""
387 <<m_inventorystring<<"\""<<std::endl;
390 // Set meshbuffer texture
391 buf->getMaterial().setTexture(0, texture);
399 #include "inventory.h"
405 ClientActiveObject(0),
406 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS/2.,BS/3.),
408 m_position(v3f(0,10*BS,0)),
411 ClientActiveObject::registerType(getType(), create);
418 ClientActiveObject* RatCAO::create()
423 void RatCAO::addToScene(scene::ISceneManager *smgr)
428 video::IVideoDriver* driver = smgr->getVideoDriver();
430 scene::SMesh *mesh = new scene::SMesh();
431 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
432 video::SColor c(255,255,255,255);
433 video::S3DVertex vertices[4] =
435 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
436 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
437 video::S3DVertex(BS/2,BS/2,0, 0,0,0, c, 1,0),
438 video::S3DVertex(-BS/2,BS/2,0, 0,0,0, c, 0,0),
440 u16 indices[] = {0,1,2,2,3,0};
441 buf->append(vertices, 4, indices, 6);
443 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
444 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
445 //buf->getMaterial().setTexture(0, NULL);
446 buf->getMaterial().setTexture
447 (0, driver->getTexture(getTexturePath("rat.png").c_str()));
448 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
449 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
450 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
452 mesh->addMeshBuffer(buf);
454 m_node = smgr->addMeshSceneNode(mesh, NULL);
456 // Set it to use the materials of the meshbuffers directly.
457 // This is needed for changing the texture in the future
458 m_node->setReadOnlyMaterials(true);
462 void RatCAO::removeFromScene()
471 void RatCAO::updateLight(u8 light_at_pos)
476 u8 li = decode_light(light_at_pos);
477 video::SColor color(255,li,li,li);
479 scene::IMesh *mesh = m_node->getMesh();
483 u16 mc = mesh->getMeshBufferCount();
484 for(u16 j=0; j<mc; j++)
486 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
487 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
488 u16 vc = buf->getVertexCount();
489 for(u16 i=0; i<vc; i++)
491 vertices[i].Color = color;
496 v3s16 RatCAO::getLightPosition()
498 return floatToInt(m_position+v3f(0,BS*0.5,0), BS);
501 void RatCAO::updateNodePos()
506 //m_node->setPosition(m_position);
507 m_node->setPosition(pos_translator.vect_show);
509 v3f rot = m_node->getRotation();
510 rot.Y = 180.0 - m_yaw;
511 m_node->setRotation(rot);
514 void RatCAO::step(float dtime, ClientEnvironment *env)
516 pos_translator.translate(dtime);
520 void RatCAO::processMessage(const std::string &data)
522 //dstream<<"RatCAO: Got message"<<std::endl;
523 std::istringstream is(data, std::ios::binary);
529 m_position = readV3F1000(is);
530 pos_translator.update(m_position);
532 m_yaw = readF1000(is);
537 void RatCAO::initialize(const std::string &data)
539 //dstream<<"RatCAO: Got init data"<<std::endl;
542 std::istringstream is(data, std::ios::binary);
544 u8 version = readU8(is);
549 m_position = readV3F1000(is);
550 pos_translator.init(m_position);
560 #include "inventory.h"
563 Oerkki1CAO proto_Oerkki1CAO;
565 Oerkki1CAO::Oerkki1CAO():
566 ClientActiveObject(0),
567 m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2.,BS/3.),
569 m_position(v3f(0,10*BS,0)),
572 ClientActiveObject::registerType(getType(), create);
575 Oerkki1CAO::~Oerkki1CAO()
579 ClientActiveObject* Oerkki1CAO::create()
581 return new Oerkki1CAO();
584 void Oerkki1CAO::addToScene(scene::ISceneManager *smgr)
589 video::IVideoDriver* driver = smgr->getVideoDriver();
591 scene::SMesh *mesh = new scene::SMesh();
592 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
593 video::SColor c(255,255,255,255);
594 video::S3DVertex vertices[4] =
596 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
597 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
598 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
599 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
601 u16 indices[] = {0,1,2,2,3,0};
602 buf->append(vertices, 4, indices, 6);
604 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
605 buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
606 //buf->getMaterial().setTexture(0, NULL);
607 buf->getMaterial().setTexture
608 (0, driver->getTexture(getTexturePath("oerkki1.png").c_str()));
609 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
610 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
611 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
613 mesh->addMeshBuffer(buf);
615 m_node = smgr->addMeshSceneNode(mesh, NULL);
617 // Set it to use the materials of the meshbuffers directly.
618 // This is needed for changing the texture in the future
619 m_node->setReadOnlyMaterials(true);
623 void Oerkki1CAO::removeFromScene()
632 void Oerkki1CAO::updateLight(u8 light_at_pos)
637 if(light_at_pos <= 2)
639 m_node->setVisible(false);
643 m_node->setVisible(true);
645 u8 li = decode_light(light_at_pos);
646 video::SColor color(255,li,li,li);
648 scene::IMesh *mesh = m_node->getMesh();
652 u16 mc = mesh->getMeshBufferCount();
653 for(u16 j=0; j<mc; j++)
655 scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
656 video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
657 u16 vc = buf->getVertexCount();
658 for(u16 i=0; i<vc; i++)
660 vertices[i].Color = color;
665 v3s16 Oerkki1CAO::getLightPosition()
667 return floatToInt(m_position+v3f(0,BS*1.5,0), BS);
670 void Oerkki1CAO::updateNodePos()
675 //m_node->setPosition(m_position);
676 m_node->setPosition(pos_translator.vect_show);
678 v3f rot = m_node->getRotation();
679 rot.Y = 180.0 - m_yaw + 90.0;
680 m_node->setRotation(rot);
683 void Oerkki1CAO::step(float dtime, ClientEnvironment *env)
685 pos_translator.translate(dtime);
688 LocalPlayer *player = env->getLocalPlayer();
691 v3f playerpos = player->getPosition();
692 v2f playerpos_2d(playerpos.X,playerpos.Z);
693 v2f objectpos_2d(m_position.X,m_position.Z);
695 if(fabs(m_position.Y - playerpos.Y) < 3.0*BS &&
696 objectpos_2d.getDistanceFrom(playerpos_2d) < 1.0*BS)
698 if(m_attack_interval.step(dtime, 0.5))
700 env->damageLocalPlayer(2);
705 void Oerkki1CAO::processMessage(const std::string &data)
707 //dstream<<"Oerkki1CAO: Got message"<<std::endl;
708 std::istringstream is(data, std::ios::binary);
714 m_position = readV3F1000(is);
715 pos_translator.update(m_position);
717 m_yaw = readF1000(is);
722 void Oerkki1CAO::initialize(const std::string &data)
724 //dstream<<"Oerkki1CAO: Got init data"<<std::endl;
727 std::istringstream is(data, std::ios::binary);
729 u8 version = readU8(is);
734 m_position = readV3F1000(is);
735 pos_translator.init(m_position);