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_sao.h"
21 #include "collision.h"
22 #include "environment.h"
24 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
31 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
33 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
34 ServerActiveObject(env, id, pos),
38 ServerActiveObject::registerType(getType(), create);
41 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
42 const std::string &data)
44 return new TestSAO(env, id, pos);
47 void TestSAO::step(float dtime, bool send_recommended)
56 m_base_position.Y += dtime * BS * 2;
57 if(m_base_position.Y > 8*BS)
58 m_base_position.Y = 2*BS;
60 if(send_recommended == false)
67 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
71 data += itos(0); // 0 = position
73 data += itos(m_base_position.X);
75 data += itos(m_base_position.Y);
77 data += itos(m_base_position.Z);
79 ActiveObjectMessage aom(getId(), false, data);
80 m_messages_out.push_back(aom);
90 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
92 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
93 const std::string inventorystring):
94 ServerActiveObject(env, id, pos),
95 m_inventorystring(inventorystring),
97 m_last_sent_position(0,0,0)
99 ServerActiveObject::registerType(getType(), create);
102 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
103 const std::string &data)
105 std::istringstream is(data, std::ios::binary);
110 // check if version is supported
113 std::string inventorystring = deSerializeString(is);
114 dstream<<"ItemSAO::create(): Creating item \""
115 <<inventorystring<<"\""<<std::endl;
116 return new ItemSAO(env, id, pos, inventorystring);
119 void ItemSAO::step(float dtime, bool send_recommended)
123 const float interval = 0.2;
124 if(m_move_interval.step(dtime, interval)==false)
128 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
129 collisionMoveResult moveresult;
131 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
132 // Maximum movement without glitches
133 f32 pos_max_d = BS*0.25;
135 if(m_speed_f.getLength()*dtime > pos_max_d)
136 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
137 v3f pos_f = getBasePosition();
138 v3f pos_f_old = pos_f;
139 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
140 box, dtime, pos_f, m_speed_f);
142 if(send_recommended == false)
145 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
147 setBasePosition(pos_f);
148 m_last_sent_position = pos_f;
150 std::ostringstream os(std::ios::binary);
152 // command (0 = update position)
156 writeS32((u8*)buf, m_base_position.X*1000);
158 writeS32((u8*)buf, m_base_position.Y*1000);
160 writeS32((u8*)buf, m_base_position.Z*1000);
162 // create message and add to list
163 ActiveObjectMessage aom(getId(), false, os.str());
164 m_messages_out.push_back(aom);
168 std::string ItemSAO::getClientInitializationData()
170 std::ostringstream os(std::ios::binary);
176 writeS32((u8*)buf, m_base_position.X*1000);
178 writeS32((u8*)buf, m_base_position.Y*1000);
180 writeS32((u8*)buf, m_base_position.Z*1000);
183 os<<serializeString(m_inventorystring);
187 std::string ItemSAO::getStaticData()
189 dstream<<__FUNCTION_NAME<<std::endl;
190 std::ostringstream os(std::ios::binary);
196 os<<serializeString(m_inventorystring);
200 InventoryItem * ItemSAO::createInventoryItem()
203 std::istringstream is(m_inventorystring, std::ios_base::binary);
204 InventoryItem *item = InventoryItem::deSerialize(is);
205 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
206 <<m_inventorystring<<"\" -> item="<<item
210 catch(SerializationError &e)
212 dstream<<__FUNCTION_NAME<<": serialization error: "
213 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
218 void ItemSAO::rightClick(Player *player)
220 dstream<<__FUNCTION_NAME<<std::endl;
221 InventoryItem *item = createInventoryItem();
225 bool to_be_deleted = item->use(m_env, player);
236 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
238 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
239 ServerActiveObject(env, id, pos),
243 ServerActiveObject::registerType(getType(), create);
245 m_oldpos = v3f(0,0,0);
246 m_last_sent_position = v3f(0,0,0);
247 m_yaw = myrand_range(0,PI*2);
251 m_touching_ground = false;
254 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
255 const std::string &data)
257 std::istringstream is(data, std::ios::binary);
262 // check if version is supported
265 return new RatSAO(env, id, pos);
268 void RatSAO::step(float dtime, bool send_recommended)
272 if(m_is_active == false)
274 if(m_inactive_interval.step(dtime, 0.5)==false)
291 m_speed_f.Y -= dtime*9.81*BS;
294 Move around if some player is close
296 bool player_is_close = false;
297 // Check connected players
298 core::list<Player*> players = m_env->getPlayers(true);
299 core::list<Player*>::Iterator i;
300 for(i = players.begin();
301 i != players.end(); i++)
304 v3f playerpos = player->getPosition();
305 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
307 player_is_close = true;
312 m_is_active = player_is_close;
314 if(player_is_close == false)
322 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
324 m_speed_f.X = speed * dir.X;
325 m_speed_f.Z = speed * dir.Z;
327 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
334 m_speed_f.Y = 5.0*BS;
342 m_counter2 += (float)(myrand()%100)/100*3.0;
343 m_yaw += ((float)(myrand()%200)-100)/100*180;
344 m_yaw = wrapDegrees(m_yaw);
349 m_oldpos = m_base_position;
352 Move it, with collision detection
355 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
356 collisionMoveResult moveresult;
357 // Maximum movement without glitches
358 f32 pos_max_d = BS*0.25;
360 if(m_speed_f.getLength()*dtime > pos_max_d)
361 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
362 v3f pos_f = getBasePosition();
363 v3f pos_f_old = pos_f;
364 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
365 box, dtime, pos_f, m_speed_f);
366 m_touching_ground = moveresult.touching_ground;
368 setBasePosition(pos_f);
370 if(send_recommended == false)
373 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
375 m_last_sent_position = pos_f;
377 std::ostringstream os(std::ios::binary);
378 // command (0 = update position)
381 writeV3F1000(os, m_base_position);
383 writeF1000(os, m_yaw);
384 // create message and add to list
385 ActiveObjectMessage aom(getId(), false, os.str());
386 m_messages_out.push_back(aom);
390 std::string RatSAO::getClientInitializationData()
392 std::ostringstream os(std::ios::binary);
396 writeV3F1000(os, m_base_position);
400 std::string RatSAO::getStaticData()
402 //dstream<<__FUNCTION_NAME<<std::endl;
403 std::ostringstream os(std::ios::binary);
409 InventoryItem* RatSAO::createPickedUpItem()
411 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
412 InventoryItem *item = InventoryItem::deSerialize(is);
420 // Y is copied, X and Z change is limited
421 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
423 v3f d_wanted = target_speed - speed;
425 f32 dl_wanted = d_wanted.getLength();
427 if(dl > max_increase)
430 v3f d = d_wanted.normalize() * dl;
434 speed.Y = target_speed.Y;
438 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
440 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
441 ServerActiveObject(env, id, pos),
445 ServerActiveObject::registerType(getType(), create);
447 m_oldpos = v3f(0,0,0);
448 m_last_sent_position = v3f(0,0,0);
453 m_touching_ground = false;
455 m_after_jump_timer = 0;
458 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
459 const std::string &data)
461 std::istringstream is(data, std::ios::binary);
463 u8 version = readU8(is);
466 // check if version is supported
469 Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
474 void Oerkki1SAO::step(float dtime, bool send_recommended)
478 if(m_is_active == false)
480 if(m_inactive_interval.step(dtime, 0.5)==false)
496 m_after_jump_timer -= dtime;
498 v3f old_speed = m_speed_f;
501 m_speed_f.Y -= dtime*9.81*BS;
504 Move around if some player is close
506 bool player_is_close = false;
507 bool player_is_too_close = false;
509 // Check connected players
510 core::list<Player*> players = m_env->getPlayers(true);
511 core::list<Player*>::Iterator i;
512 for(i = players.begin();
513 i != players.end(); i++)
516 v3f playerpos = player->getPosition();
517 f32 dist = m_base_position.getDistanceFrom(playerpos);
520 player_is_too_close = true;
521 near_player_pos = playerpos;
524 else if(dist < BS*15.0)
526 player_is_close = true;
527 near_player_pos = playerpos;
531 m_is_active = player_is_close;
533 v3f target_speed = m_speed_f;
537 target_speed = v3f(0,0,0);
543 v3f ndir = near_player_pos - m_base_position;
547 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
548 if(nyaw < m_yaw - 180)
550 else if(nyaw > m_yaw + 180)
552 m_yaw = 0.95*m_yaw + 0.05*nyaw;
553 m_yaw = wrapDegrees(m_yaw);
557 if((m_touching_ground || m_after_jump_timer > 0.0)
558 && !player_is_too_close)
560 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
561 target_speed.X = speed * dir.X;
562 target_speed.Z = speed * dir.Z;
565 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
573 target_speed.Y = 5.0*BS;
574 m_after_jump_timer = 1.0;
582 m_counter2 += (float)(myrand()%100)/100*3.0;
583 //m_yaw += ((float)(myrand()%200)-100)/100*180;
584 m_yaw += ((float)(myrand()%200)-100)/100*90;
585 m_yaw = wrapDegrees(m_yaw);
590 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
591 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
593 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
595 m_oldpos = m_base_position;
598 Move it, with collision detection
601 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
602 collisionMoveResult moveresult;
603 // Maximum movement without glitches
604 f32 pos_max_d = BS*0.25;
606 if(m_speed_f.getLength()*dtime > pos_max_d)
607 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
608 v3f pos_f = getBasePosition();
609 v3f pos_f_old = pos_f;
610 moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
611 box, dtime, pos_f, m_speed_f);
612 m_touching_ground = moveresult.touching_ground;
614 // Do collision damage
615 float tolerance = BS*12;
616 float factor = BS*0.5;
617 v3f speed_diff = old_speed - m_speed_f;
618 // Increase effect in X and Z
621 float vel = speed_diff.getLength();
624 f32 damage_f = (vel - tolerance)/BS*factor;
625 u16 damage = (u16)(damage_f+0.5);
629 setBasePosition(pos_f);
631 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
634 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
636 m_last_sent_position = pos_f;
638 std::ostringstream os(std::ios::binary);
639 // command (0 = update position)
642 writeV3F1000(os, m_base_position);
644 writeF1000(os, m_yaw);
645 // create message and add to list
646 ActiveObjectMessage aom(getId(), false, os.str());
647 m_messages_out.push_back(aom);
651 std::string Oerkki1SAO::getClientInitializationData()
653 std::ostringstream os(std::ios::binary);
657 writeV3F1000(os, m_base_position);
661 std::string Oerkki1SAO::getStaticData()
663 //dstream<<__FUNCTION_NAME<<std::endl;
664 std::ostringstream os(std::ios::binary);
672 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
674 m_speed_f += dir*12*BS;
681 void Oerkki1SAO::doDamage(u16 d)
683 dstream<<"oerkki damage: "<<d<<std::endl;
697 std::ostringstream os(std::ios::binary);
698 // command (1 = damage)
702 // create message and add to list
703 ActiveObjectMessage aom(getId(), false, os.str());
704 m_messages_out.push_back(aom);
713 FireflySAO proto_FireflySAO(NULL, 0, v3f(0,0,0));
715 FireflySAO::FireflySAO(ServerEnvironment *env, u16 id, v3f pos):
716 ServerActiveObject(env, id, pos),
720 ServerActiveObject::registerType(getType(), create);
722 m_oldpos = v3f(0,0,0);
723 m_last_sent_position = v3f(0,0,0);
728 m_touching_ground = false;
731 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, u16 id, v3f pos,
732 const std::string &data)
734 std::istringstream is(data, std::ios::binary);
739 // check if version is supported
742 return new FireflySAO(env, id, pos);
745 void FireflySAO::step(float dtime, bool send_recommended)
749 if(m_is_active == false)
751 if(m_inactive_interval.step(dtime, 0.5)==false)
759 // Apply (less) gravity
760 m_speed_f.Y -= dtime*3*BS;
763 Move around if some player is close
765 bool player_is_close = false;
766 // Check connected players
767 core::list<Player*> players = m_env->getPlayers(true);
768 core::list<Player*>::Iterator i;
769 for(i = players.begin();
770 i != players.end(); i++)
773 v3f playerpos = player->getPosition();
774 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
776 player_is_close = true;
781 m_is_active = player_is_close;
783 if(player_is_close == false)
791 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
793 m_speed_f.X = speed * dir.X;
794 m_speed_f.Z = speed * dir.Z;
796 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
803 m_speed_f.Y = 5.0*BS;
811 m_counter2 += (float)(myrand()%100)/100*3.0;
812 m_yaw += ((float)(myrand()%200)-100)/100*180;
813 m_yaw = wrapDegrees(m_yaw);
818 m_oldpos = m_base_position;
821 Move it, with collision detection
824 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
825 collisionMoveResult moveresult;
826 // Maximum movement without glitches
827 f32 pos_max_d = BS*0.25;
829 if(m_speed_f.getLength()*dtime > pos_max_d)
830 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
831 v3f pos_f = getBasePosition();
832 v3f pos_f_old = pos_f;
833 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
834 box, dtime, pos_f, m_speed_f);
835 m_touching_ground = moveresult.touching_ground;
837 setBasePosition(pos_f);
839 if(send_recommended == false)
842 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
844 m_last_sent_position = pos_f;
846 std::ostringstream os(std::ios::binary);
847 // command (0 = update position)
850 writeV3F1000(os, m_base_position);
852 writeF1000(os, m_yaw);
853 // create message and add to list
854 ActiveObjectMessage aom(getId(), false, os.str());
855 m_messages_out.push_back(aom);
859 std::string FireflySAO::getClientInitializationData()
861 std::ostringstream os(std::ios::binary);
865 writeV3F1000(os, m_base_position);
869 std::string FireflySAO::getStaticData()
871 //dstream<<__FUNCTION_NAME<<std::endl;
872 std::ostringstream os(std::ios::binary);
878 InventoryItem* FireflySAO::createPickedUpItem()
880 std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
881 InventoryItem *item = InventoryItem::deSerialize(is);