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;
224 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
226 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
227 ServerActiveObject(env, id, pos),
231 ServerActiveObject::registerType(getType(), create);
233 m_oldpos = v3f(0,0,0);
234 m_last_sent_position = v3f(0,0,0);
239 m_touching_ground = false;
242 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
243 const std::string &data)
245 std::istringstream is(data, std::ios::binary);
250 // check if version is supported
253 return new RatSAO(env, id, pos);
256 void RatSAO::step(float dtime, bool send_recommended)
260 if(m_is_active == false)
262 if(m_inactive_interval.step(dtime, 0.5)==false)
279 m_speed_f.Y -= dtime*9.81*BS;
282 Move around if some player is close
284 bool player_is_close = false;
285 // Check connected players
286 core::list<Player*> players = m_env->getPlayers(true);
287 core::list<Player*>::Iterator i;
288 for(i = players.begin();
289 i != players.end(); i++)
292 v3f playerpos = player->getPosition();
293 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
295 player_is_close = true;
300 m_is_active = player_is_close;
302 if(player_is_close == false)
310 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
312 m_speed_f.X = speed * dir.X;
313 m_speed_f.Z = speed * dir.Z;
315 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
322 m_speed_f.Y = 5.0*BS;
330 m_counter2 += (float)(myrand()%100)/100*3.0;
331 m_yaw += ((float)(myrand()%200)-100)/100*180;
332 m_yaw = wrapDegrees(m_yaw);
337 m_oldpos = m_base_position;
340 Move it, with collision detection
343 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
344 collisionMoveResult moveresult;
345 // Maximum movement without glitches
346 f32 pos_max_d = BS*0.25;
348 if(m_speed_f.getLength()*dtime > pos_max_d)
349 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
350 v3f pos_f = getBasePosition();
351 v3f pos_f_old = pos_f;
352 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
353 box, dtime, pos_f, m_speed_f);
354 m_touching_ground = moveresult.touching_ground;
356 setBasePosition(pos_f);
358 if(send_recommended == false)
361 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
363 m_last_sent_position = pos_f;
365 std::ostringstream os(std::ios::binary);
366 // command (0 = update position)
369 writeV3F1000(os, m_base_position);
371 writeF1000(os, m_yaw);
372 // create message and add to list
373 ActiveObjectMessage aom(getId(), false, os.str());
374 m_messages_out.push_back(aom);
378 std::string RatSAO::getClientInitializationData()
380 std::ostringstream os(std::ios::binary);
384 writeV3F1000(os, m_base_position);
388 std::string RatSAO::getStaticData()
390 //dstream<<__FUNCTION_NAME<<std::endl;
391 std::ostringstream os(std::ios::binary);
397 InventoryItem* RatSAO::createPickedUpItem()
399 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
400 InventoryItem *item = InventoryItem::deSerialize(is);
408 // Y is copied, X and Z change is limited
409 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
411 v3f d_wanted = target_speed - speed;
413 f32 dl_wanted = d_wanted.getLength();
415 if(dl > max_increase)
418 v3f d = d_wanted.normalize() * dl;
422 speed.Y = target_speed.Y;
426 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
428 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
429 ServerActiveObject(env, id, pos),
433 ServerActiveObject::registerType(getType(), create);
435 m_oldpos = v3f(0,0,0);
436 m_last_sent_position = v3f(0,0,0);
441 m_touching_ground = false;
443 m_after_jump_timer = 0;
446 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
447 const std::string &data)
449 std::istringstream is(data, std::ios::binary);
451 u8 version = readU8(is);
454 // check if version is supported
457 Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
462 void Oerkki1SAO::step(float dtime, bool send_recommended)
466 if(m_is_active == false)
468 if(m_inactive_interval.step(dtime, 0.5)==false)
484 m_after_jump_timer -= dtime;
486 v3f old_speed = m_speed_f;
489 m_speed_f.Y -= dtime*9.81*BS;
492 Move around if some player is close
494 bool player_is_close = false;
495 bool player_is_too_close = false;
497 // Check connected players
498 core::list<Player*> players = m_env->getPlayers(true);
499 core::list<Player*>::Iterator i;
500 for(i = players.begin();
501 i != players.end(); i++)
504 v3f playerpos = player->getPosition();
505 f32 dist = m_base_position.getDistanceFrom(playerpos);
508 player_is_too_close = true;
509 near_player_pos = playerpos;
512 else if(dist < BS*15.0)
514 player_is_close = true;
515 near_player_pos = playerpos;
519 m_is_active = player_is_close;
521 v3f target_speed = m_speed_f;
525 target_speed = v3f(0,0,0);
531 v3f ndir = near_player_pos - m_base_position;
535 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
536 if(nyaw < m_yaw - 180)
538 else if(nyaw > m_yaw + 180)
540 m_yaw = 0.95*m_yaw + 0.05*nyaw;
541 m_yaw = wrapDegrees(m_yaw);
545 if((m_touching_ground || m_after_jump_timer > 0.0)
546 && !player_is_too_close)
548 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
549 target_speed.X = speed * dir.X;
550 target_speed.Z = speed * dir.Z;
553 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
561 target_speed.Y = 5.0*BS;
562 m_after_jump_timer = 1.0;
570 m_counter2 += (float)(myrand()%100)/100*3.0;
571 //m_yaw += ((float)(myrand()%200)-100)/100*180;
572 m_yaw += ((float)(myrand()%200)-100)/100*90;
573 m_yaw = wrapDegrees(m_yaw);
578 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
579 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
581 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
583 m_oldpos = m_base_position;
586 Move it, with collision detection
589 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
590 collisionMoveResult moveresult;
591 // Maximum movement without glitches
592 f32 pos_max_d = BS*0.25;
594 if(m_speed_f.getLength()*dtime > pos_max_d)
595 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
596 v3f pos_f = getBasePosition();
597 v3f pos_f_old = pos_f;
598 moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
599 box, dtime, pos_f, m_speed_f);
600 m_touching_ground = moveresult.touching_ground;
602 // Do collision damage
603 float tolerance = BS*12;
604 float factor = BS*0.5;
605 v3f speed_diff = old_speed - m_speed_f;
606 // Increase effect in X and Z
609 float vel = speed_diff.getLength();
612 f32 damage_f = (vel - tolerance)/BS*factor;
613 u16 damage = (u16)(damage_f+0.5);
617 setBasePosition(pos_f);
619 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
622 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
624 m_last_sent_position = pos_f;
626 std::ostringstream os(std::ios::binary);
627 // command (0 = update position)
630 writeV3F1000(os, m_base_position);
632 writeF1000(os, m_yaw);
633 // create message and add to list
634 ActiveObjectMessage aom(getId(), false, os.str());
635 m_messages_out.push_back(aom);
639 std::string Oerkki1SAO::getClientInitializationData()
641 std::ostringstream os(std::ios::binary);
645 writeV3F1000(os, m_base_position);
649 std::string Oerkki1SAO::getStaticData()
651 //dstream<<__FUNCTION_NAME<<std::endl;
652 std::ostringstream os(std::ios::binary);
660 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
662 m_speed_f += dir*12*BS;
669 void Oerkki1SAO::doDamage(u16 d)
671 dstream<<"oerkki damage: "<<d<<std::endl;
685 std::ostringstream os(std::ios::binary);
686 // command (1 = damage)
690 // create message and add to list
691 ActiveObjectMessage aom(getId(), false, os.str());
692 m_messages_out.push_back(aom);
701 FireflySAO proto_FireflySAO(NULL, 0, v3f(0,0,0));
703 FireflySAO::FireflySAO(ServerEnvironment *env, u16 id, v3f pos):
704 ServerActiveObject(env, id, pos),
708 ServerActiveObject::registerType(getType(), create);
710 m_oldpos = v3f(0,0,0);
711 m_last_sent_position = v3f(0,0,0);
716 m_touching_ground = false;
719 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, u16 id, v3f pos,
720 const std::string &data)
722 std::istringstream is(data, std::ios::binary);
727 // check if version is supported
730 return new FireflySAO(env, id, pos);
733 void FireflySAO::step(float dtime, bool send_recommended)
737 if(m_is_active == false)
739 if(m_inactive_interval.step(dtime, 0.5)==false)
747 // Apply (less) gravity
748 m_speed_f.Y -= dtime*3*BS;
751 Move around if some player is close
753 bool player_is_close = false;
754 // Check connected players
755 core::list<Player*> players = m_env->getPlayers(true);
756 core::list<Player*>::Iterator i;
757 for(i = players.begin();
758 i != players.end(); i++)
761 v3f playerpos = player->getPosition();
762 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
764 player_is_close = true;
769 m_is_active = player_is_close;
771 if(player_is_close == false)
779 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
781 m_speed_f.X = speed * dir.X;
782 m_speed_f.Z = speed * dir.Z;
784 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
791 m_speed_f.Y = 5.0*BS;
799 m_counter2 += (float)(myrand()%100)/100*3.0;
800 m_yaw += ((float)(myrand()%200)-100)/100*180;
801 m_yaw = wrapDegrees(m_yaw);
806 m_oldpos = m_base_position;
809 Move it, with collision detection
812 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
813 collisionMoveResult moveresult;
814 // Maximum movement without glitches
815 f32 pos_max_d = BS*0.25;
817 if(m_speed_f.getLength()*dtime > pos_max_d)
818 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
819 v3f pos_f = getBasePosition();
820 v3f pos_f_old = pos_f;
821 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
822 box, dtime, pos_f, m_speed_f);
823 m_touching_ground = moveresult.touching_ground;
825 setBasePosition(pos_f);
827 if(send_recommended == false)
830 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
832 m_last_sent_position = pos_f;
834 std::ostringstream os(std::ios::binary);
835 // command (0 = update position)
838 writeV3F1000(os, m_base_position);
840 writeF1000(os, m_yaw);
841 // create message and add to list
842 ActiveObjectMessage aom(getId(), false, os.str());
843 m_messages_out.push_back(aom);
847 std::string FireflySAO::getClientInitializationData()
849 std::ostringstream os(std::ios::binary);
853 writeV3F1000(os, m_base_position);
857 std::string FireflySAO::getStaticData()
859 //dstream<<__FUNCTION_NAME<<std::endl;
860 std::ostringstream os(std::ios::binary);
866 InventoryItem* FireflySAO::createPickedUpItem()
868 std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
869 InventoryItem *item = InventoryItem::deSerialize(is);