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"
29 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
31 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
32 ServerActiveObject(env, id, pos),
36 ServerActiveObject::registerType(getType(), create);
39 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
40 const std::string &data)
42 return new TestSAO(env, id, pos);
45 void TestSAO::step(float dtime, bool send_recommended)
54 m_base_position.Y += dtime * BS * 2;
55 if(m_base_position.Y > 8*BS)
56 m_base_position.Y = 2*BS;
58 if(send_recommended == false)
65 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
69 data += itos(0); // 0 = position
71 data += itos(m_base_position.X);
73 data += itos(m_base_position.Y);
75 data += itos(m_base_position.Z);
77 ActiveObjectMessage aom(getId(), false, data);
78 m_messages_out.push_back(aom);
88 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
90 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
91 const std::string inventorystring):
92 ServerActiveObject(env, id, pos),
93 m_inventorystring(inventorystring),
95 m_last_sent_position(0,0,0)
97 ServerActiveObject::registerType(getType(), create);
100 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
101 const std::string &data)
103 std::istringstream is(data, std::ios::binary);
108 // check if version is supported
111 std::string inventorystring = deSerializeString(is);
112 dstream<<"ItemSAO::create(): Creating item \""
113 <<inventorystring<<"\""<<std::endl;
114 return new ItemSAO(env, id, pos, inventorystring);
117 void ItemSAO::step(float dtime, bool send_recommended)
121 const float interval = 0.2;
122 if(m_move_interval.step(dtime, interval)==false)
126 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
127 collisionMoveResult moveresult;
129 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
130 // Maximum movement without glitches
131 f32 pos_max_d = BS*0.25;
133 if(m_speed_f.getLength()*dtime > pos_max_d)
134 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
135 v3f pos_f = getBasePosition();
136 v3f pos_f_old = pos_f;
137 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
138 box, dtime, pos_f, m_speed_f);
140 if(send_recommended == false)
143 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
145 setBasePosition(pos_f);
146 m_last_sent_position = pos_f;
148 std::ostringstream os(std::ios::binary);
150 // command (0 = update position)
154 writeS32((u8*)buf, m_base_position.X*1000);
156 writeS32((u8*)buf, m_base_position.Y*1000);
158 writeS32((u8*)buf, m_base_position.Z*1000);
160 // create message and add to list
161 ActiveObjectMessage aom(getId(), false, os.str());
162 m_messages_out.push_back(aom);
166 std::string ItemSAO::getClientInitializationData()
168 std::ostringstream os(std::ios::binary);
174 writeS32((u8*)buf, m_base_position.X*1000);
176 writeS32((u8*)buf, m_base_position.Y*1000);
178 writeS32((u8*)buf, m_base_position.Z*1000);
181 os<<serializeString(m_inventorystring);
185 std::string ItemSAO::getStaticData()
187 dstream<<__FUNCTION_NAME<<std::endl;
188 std::ostringstream os(std::ios::binary);
194 os<<serializeString(m_inventorystring);
198 InventoryItem * ItemSAO::createInventoryItem()
201 std::istringstream is(m_inventorystring, std::ios_base::binary);
202 InventoryItem *item = InventoryItem::deSerialize(is);
203 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
204 <<m_inventorystring<<"\" -> item="<<item
208 catch(SerializationError &e)
210 dstream<<__FUNCTION_NAME<<": serialization error: "
211 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
222 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
224 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
225 ServerActiveObject(env, id, pos),
229 ServerActiveObject::registerType(getType(), create);
231 m_oldpos = v3f(0,0,0);
232 m_last_sent_position = v3f(0,0,0);
237 m_touching_ground = false;
240 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
241 const std::string &data)
243 std::istringstream is(data, std::ios::binary);
248 // check if version is supported
251 return new RatSAO(env, id, pos);
254 void RatSAO::step(float dtime, bool send_recommended)
258 if(m_is_active == false)
260 if(m_inactive_interval.step(dtime, 0.5)==false)
277 m_speed_f.Y -= dtime*9.81*BS;
280 Move around if some player is close
282 bool player_is_close = false;
283 // Check connected players
284 core::list<Player*> players = m_env->getPlayers(true);
285 core::list<Player*>::Iterator i;
286 for(i = players.begin();
287 i != players.end(); i++)
290 v3f playerpos = player->getPosition();
291 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
293 player_is_close = true;
298 m_is_active = player_is_close;
300 if(player_is_close == false)
308 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
310 m_speed_f.X = speed * dir.X;
311 m_speed_f.Z = speed * dir.Z;
313 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
320 m_speed_f.Y = 5.0*BS;
328 m_counter2 += (float)(myrand()%100)/100*3.0;
329 m_yaw += ((float)(myrand()%200)-100)/100*180;
330 m_yaw = wrapDegrees(m_yaw);
335 m_oldpos = m_base_position;
338 Move it, with collision detection
341 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
342 collisionMoveResult moveresult;
343 // Maximum movement without glitches
344 f32 pos_max_d = BS*0.25;
346 if(m_speed_f.getLength()*dtime > pos_max_d)
347 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
348 v3f pos_f = getBasePosition();
349 v3f pos_f_old = pos_f;
350 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
351 box, dtime, pos_f, m_speed_f);
352 m_touching_ground = moveresult.touching_ground;
354 setBasePosition(pos_f);
356 if(send_recommended == false)
359 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
361 m_last_sent_position = pos_f;
363 std::ostringstream os(std::ios::binary);
364 // command (0 = update position)
367 writeV3F1000(os, m_base_position);
369 writeF1000(os, m_yaw);
370 // create message and add to list
371 ActiveObjectMessage aom(getId(), false, os.str());
372 m_messages_out.push_back(aom);
376 std::string RatSAO::getClientInitializationData()
378 std::ostringstream os(std::ios::binary);
382 writeV3F1000(os, m_base_position);
386 std::string RatSAO::getStaticData()
388 //dstream<<__FUNCTION_NAME<<std::endl;
389 std::ostringstream os(std::ios::binary);
395 InventoryItem* RatSAO::createPickedUpItem()
397 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
398 InventoryItem *item = InventoryItem::deSerialize(is);
406 // Y is copied, X and Z change is limited
407 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
409 v3f d_wanted = target_speed - speed;
411 f32 dl_wanted = d_wanted.getLength();
413 if(dl > max_increase)
416 v3f d = d_wanted.normalize() * dl;
420 speed.Y = target_speed.Y;
424 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
426 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
427 ServerActiveObject(env, id, pos),
431 ServerActiveObject::registerType(getType(), create);
433 m_oldpos = v3f(0,0,0);
434 m_last_sent_position = v3f(0,0,0);
439 m_touching_ground = false;
441 m_after_jump_timer = 0;
444 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
445 const std::string &data)
447 std::istringstream is(data, std::ios::binary);
449 u8 version = readU8(is);
452 // check if version is supported
455 Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
460 void Oerkki1SAO::step(float dtime, bool send_recommended)
464 if(m_is_active == false)
466 if(m_inactive_interval.step(dtime, 0.5)==false)
482 m_after_jump_timer -= dtime;
484 v3f old_speed = m_speed_f;
487 m_speed_f.Y -= dtime*9.81*BS;
490 Move around if some player is close
492 bool player_is_close = false;
493 bool player_is_too_close = false;
495 // Check connected players
496 core::list<Player*> players = m_env->getPlayers(true);
497 core::list<Player*>::Iterator i;
498 for(i = players.begin();
499 i != players.end(); i++)
502 v3f playerpos = player->getPosition();
503 f32 dist = m_base_position.getDistanceFrom(playerpos);
506 player_is_too_close = true;
507 near_player_pos = playerpos;
510 else if(dist < BS*15.0)
512 player_is_close = true;
513 near_player_pos = playerpos;
517 m_is_active = player_is_close;
519 v3f target_speed = m_speed_f;
523 target_speed = v3f(0,0,0);
529 v3f ndir = near_player_pos - m_base_position;
533 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
534 if(nyaw < m_yaw - 180)
536 else if(nyaw > m_yaw + 180)
538 m_yaw = 0.95*m_yaw + 0.05*nyaw;
539 m_yaw = wrapDegrees(m_yaw);
543 if((m_touching_ground || m_after_jump_timer > 0.0)
544 && !player_is_too_close)
546 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
547 target_speed.X = speed * dir.X;
548 target_speed.Z = speed * dir.Z;
551 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
559 target_speed.Y = 5.0*BS;
560 m_after_jump_timer = 1.0;
568 m_counter2 += (float)(myrand()%100)/100*3.0;
569 //m_yaw += ((float)(myrand()%200)-100)/100*180;
570 m_yaw += ((float)(myrand()%200)-100)/100*90;
571 m_yaw = wrapDegrees(m_yaw);
576 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
577 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
579 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
581 m_oldpos = m_base_position;
584 Move it, with collision detection
587 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
588 collisionMoveResult moveresult;
589 // Maximum movement without glitches
590 f32 pos_max_d = BS*0.25;
592 if(m_speed_f.getLength()*dtime > pos_max_d)
593 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
594 v3f pos_f = getBasePosition();
595 v3f pos_f_old = pos_f;
596 moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
597 box, dtime, pos_f, m_speed_f);
598 m_touching_ground = moveresult.touching_ground;
600 // Do collision damage
601 float tolerance = BS*12;
602 float factor = BS*0.5;
603 v3f speed_diff = old_speed - m_speed_f;
604 // Increase effect in X and Z
607 float vel = speed_diff.getLength();
610 f32 damage_f = (vel - tolerance)/BS*factor;
611 u16 damage = (u16)(damage_f+0.5);
615 setBasePosition(pos_f);
617 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
620 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
622 m_last_sent_position = pos_f;
624 std::ostringstream os(std::ios::binary);
625 // command (0 = update position)
628 writeV3F1000(os, m_base_position);
630 writeF1000(os, m_yaw);
631 // create message and add to list
632 ActiveObjectMessage aom(getId(), false, os.str());
633 m_messages_out.push_back(aom);
637 std::string Oerkki1SAO::getClientInitializationData()
639 std::ostringstream os(std::ios::binary);
643 writeV3F1000(os, m_base_position);
647 std::string Oerkki1SAO::getStaticData()
649 //dstream<<__FUNCTION_NAME<<std::endl;
650 std::ostringstream os(std::ios::binary);
658 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
660 m_speed_f += dir*12*BS;
667 void Oerkki1SAO::doDamage(u16 d)
669 dstream<<"oerkki damage: "<<d<<std::endl;
683 std::ostringstream os(std::ios::binary);
684 // command (1 = damage)
688 // create message and add to list
689 ActiveObjectMessage aom(getId(), false, os.str());
690 m_messages_out.push_back(aom);