Merge remote-tracking branch 'speedprog/banByIp'
[oweals/minetest.git] / src / content_sao.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "content_sao.h"
21 #include "collision.h"
22 #include "environment.h"
23
24 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
25
26 /*
27         TestSAO
28 */
29
30 // Prototype
31 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
32
33 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
34         ServerActiveObject(env, id, pos),
35         m_timer1(0),
36         m_age(0)
37 {
38         ServerActiveObject::registerType(getType(), create);
39 }
40
41 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
42                 const std::string &data)
43 {
44         return new TestSAO(env, id, pos);
45 }
46
47 void TestSAO::step(float dtime, bool send_recommended)
48 {
49         m_age += dtime;
50         if(m_age > 10)
51         {
52                 m_removed = true;
53                 return;
54         }
55
56         m_base_position.Y += dtime * BS * 2;
57         if(m_base_position.Y > 8*BS)
58                 m_base_position.Y = 2*BS;
59
60         if(send_recommended == false)
61                 return;
62
63         m_timer1 -= dtime;
64         if(m_timer1 < 0.0)
65         {
66                 m_timer1 += 0.125;
67                 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
68
69                 std::string data;
70
71                 data += itos(0); // 0 = position
72                 data += " ";
73                 data += itos(m_base_position.X);
74                 data += " ";
75                 data += itos(m_base_position.Y);
76                 data += " ";
77                 data += itos(m_base_position.Z);
78
79                 ActiveObjectMessage aom(getId(), false, data);
80                 m_messages_out.push_back(aom);
81         }
82 }
83
84
85 /*
86         ItemSAO
87 */
88
89 // Prototype
90 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
91
92 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
93                 const std::string inventorystring):
94         ServerActiveObject(env, id, pos),
95         m_inventorystring(inventorystring),
96         m_speed_f(0,0,0),
97         m_last_sent_position(0,0,0)
98 {
99         ServerActiveObject::registerType(getType(), create);
100 }
101
102 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
103                 const std::string &data)
104 {
105         std::istringstream is(data, std::ios::binary);
106         char buf[1];
107         // read version
108         is.read(buf, 1);
109         u8 version = buf[0];
110         // check if version is supported
111         if(version != 0)
112                 return NULL;
113         std::string inventorystring = deSerializeString(is);
114         dstream<<"ItemSAO::create(): Creating item \""
115                         <<inventorystring<<"\""<<std::endl;
116         return new ItemSAO(env, id, pos, inventorystring);
117 }
118
119 void ItemSAO::step(float dtime, bool send_recommended)
120 {
121         assert(m_env);
122
123         const float interval = 0.2;
124         if(m_move_interval.step(dtime, interval)==false)
125                 return;
126         dtime = interval;
127         
128         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
129         collisionMoveResult moveresult;
130         // Apply gravity
131         m_speed_f += v3f(0, -dtime*9.81*BS, 0);
132         // Maximum movement without glitches
133         f32 pos_max_d = BS*0.25;
134         // Limit speed
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);
141         
142         if(send_recommended == false)
143                 return;
144
145         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
146         {
147                 setBasePosition(pos_f);
148                 m_last_sent_position = pos_f;
149
150                 std::ostringstream os(std::ios::binary);
151                 char buf[6];
152                 // command (0 = update position)
153                 buf[0] = 0;
154                 os.write(buf, 1);
155                 // pos
156                 writeS32((u8*)buf, m_base_position.X*1000);
157                 os.write(buf, 4);
158                 writeS32((u8*)buf, m_base_position.Y*1000);
159                 os.write(buf, 4);
160                 writeS32((u8*)buf, m_base_position.Z*1000);
161                 os.write(buf, 4);
162                 // create message and add to list
163                 ActiveObjectMessage aom(getId(), false, os.str());
164                 m_messages_out.push_back(aom);
165         }
166 }
167
168 std::string ItemSAO::getClientInitializationData()
169 {
170         std::ostringstream os(std::ios::binary);
171         char buf[6];
172         // version
173         buf[0] = 0;
174         os.write(buf, 1);
175         // pos
176         writeS32((u8*)buf, m_base_position.X*1000);
177         os.write(buf, 4);
178         writeS32((u8*)buf, m_base_position.Y*1000);
179         os.write(buf, 4);
180         writeS32((u8*)buf, m_base_position.Z*1000);
181         os.write(buf, 4);
182         // inventorystring
183         os<<serializeString(m_inventorystring);
184         return os.str();
185 }
186
187 std::string ItemSAO::getStaticData()
188 {
189         dstream<<__FUNCTION_NAME<<std::endl;
190         std::ostringstream os(std::ios::binary);
191         char buf[1];
192         // version
193         buf[0] = 0;
194         os.write(buf, 1);
195         // inventorystring
196         os<<serializeString(m_inventorystring);
197         return os.str();
198 }
199
200 InventoryItem * ItemSAO::createInventoryItem()
201 {
202         try{
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
207                                 <<std::endl;
208                 return item;
209         }
210         catch(SerializationError &e)
211         {
212                 dstream<<__FUNCTION_NAME<<": serialization error: "
213                                 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
214                 return NULL;
215         }
216 }
217
218 void ItemSAO::rightClick(Player *player)
219 {
220         dstream<<__FUNCTION_NAME<<std::endl;
221         InventoryItem *item = createInventoryItem();
222         if(item == NULL)
223                 return;
224         
225         bool to_be_deleted = item->use(m_env, player);
226
227         if(to_be_deleted)
228                 m_removed = true;
229 }
230
231 /*
232         RatSAO
233 */
234
235 // Prototype
236 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
237
238 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
239         ServerActiveObject(env, id, pos),
240         m_is_active(false),
241         m_speed_f(0,0,0)
242 {
243         ServerActiveObject::registerType(getType(), create);
244
245         m_oldpos = v3f(0,0,0);
246         m_last_sent_position = v3f(0,0,0);
247         m_yaw = myrand_range(0,PI*2);
248         m_counter1 = 0;
249         m_counter2 = 0;
250         m_age = 0;
251         m_touching_ground = false;
252 }
253
254 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
255                 const std::string &data)
256 {
257         std::istringstream is(data, std::ios::binary);
258         char buf[1];
259         // read version
260         is.read(buf, 1);
261         u8 version = buf[0];
262         // check if version is supported
263         if(version != 0)
264                 return NULL;
265         return new RatSAO(env, id, pos);
266 }
267
268 void RatSAO::step(float dtime, bool send_recommended)
269 {
270         assert(m_env);
271
272         if(m_is_active == false)
273         {
274                 if(m_inactive_interval.step(dtime, 0.5)==false)
275                         return;
276         }
277
278         /*
279                 The AI
280         */
281
282         /*m_age += dtime;
283         if(m_age > 60)
284         {
285                 // Die
286                 m_removed = true;
287                 return;
288         }*/
289
290         // Apply gravity
291         m_speed_f.Y -= dtime*9.81*BS;
292
293         /*
294                 Move around if some player is close
295         */
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++)
302         {
303                 Player *player = *i;
304                 v3f playerpos = player->getPosition();
305                 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
306                 {
307                         player_is_close = true;
308                         break;
309                 }
310         }
311
312         m_is_active = player_is_close;
313         
314         if(player_is_close == false)
315         {
316                 m_speed_f.X = 0;
317                 m_speed_f.Z = 0;
318         }
319         else
320         {
321                 // Move around
322                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
323                 f32 speed = 2*BS;
324                 m_speed_f.X = speed * dir.X;
325                 m_speed_f.Z = speed * dir.Z;
326
327                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
328                                 < dtime*speed/2)
329                 {
330                         m_counter1 -= dtime;
331                         if(m_counter1 < 0.0)
332                         {
333                                 m_counter1 += 1.0;
334                                 m_speed_f.Y = 5.0*BS;
335                         }
336                 }
337
338                 {
339                         m_counter2 -= dtime;
340                         if(m_counter2 < 0.0)
341                         {
342                                 m_counter2 += (float)(myrand()%100)/100*3.0;
343                                 m_yaw += ((float)(myrand()%200)-100)/100*180;
344                                 m_yaw = wrapDegrees(m_yaw);
345                         }
346                 }
347         }
348         
349         m_oldpos = m_base_position;
350
351         /*
352                 Move it, with collision detection
353         */
354
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;
359         // Limit speed
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;
367         
368         setBasePosition(pos_f);
369
370         if(send_recommended == false)
371                 return;
372
373         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
374         {
375                 m_last_sent_position = pos_f;
376
377                 std::ostringstream os(std::ios::binary);
378                 // command (0 = update position)
379                 writeU8(os, 0);
380                 // pos
381                 writeV3F1000(os, m_base_position);
382                 // yaw
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);
387         }
388 }
389
390 std::string RatSAO::getClientInitializationData()
391 {
392         std::ostringstream os(std::ios::binary);
393         // version
394         writeU8(os, 0);
395         // pos
396         writeV3F1000(os, m_base_position);
397         return os.str();
398 }
399
400 std::string RatSAO::getStaticData()
401 {
402         //dstream<<__FUNCTION_NAME<<std::endl;
403         std::ostringstream os(std::ios::binary);
404         // version
405         writeU8(os, 0);
406         return os.str();
407 }
408
409 InventoryItem* RatSAO::createPickedUpItem()
410 {
411         std::istringstream is("CraftItem rat 1", std::ios_base::binary);
412         InventoryItem *item = InventoryItem::deSerialize(is);
413         return item;
414 }
415
416 /*
417         Oerkki1SAO
418 */
419
420 // Y is copied, X and Z change is limited
421 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
422 {
423         v3f d_wanted = target_speed - speed;
424         d_wanted.Y = 0;
425         f32 dl_wanted = d_wanted.getLength();
426         f32 dl = dl_wanted;
427         if(dl > max_increase)
428                 dl = max_increase;
429         
430         v3f d = d_wanted.normalize() * dl;
431
432         speed.X += d.X;
433         speed.Z += d.Z;
434         speed.Y = target_speed.Y;
435 }
436
437 // Prototype
438 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
439
440 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
441         ServerActiveObject(env, id, pos),
442         m_is_active(false),
443         m_speed_f(0,0,0)
444 {
445         ServerActiveObject::registerType(getType(), create);
446
447         m_oldpos = v3f(0,0,0);
448         m_last_sent_position = v3f(0,0,0);
449         m_yaw = 0;
450         m_counter1 = 0;
451         m_counter2 = 0;
452         m_age = 0;
453         m_touching_ground = false;
454         m_hp = 20;
455         m_after_jump_timer = 0;
456 }
457
458 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
459                 const std::string &data)
460 {
461         std::istringstream is(data, std::ios::binary);
462         // read version
463         u8 version = readU8(is);
464         // read hp
465         u8 hp = readU8(is);
466         // check if version is supported
467         if(version != 0)
468                 return NULL;
469         Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
470         o->m_hp = hp;
471         return o;
472 }
473
474 void Oerkki1SAO::step(float dtime, bool send_recommended)
475 {
476         assert(m_env);
477
478         if(m_is_active == false)
479         {
480                 if(m_inactive_interval.step(dtime, 0.5)==false)
481                         return;
482         }
483
484         /*
485                 The AI
486         */
487
488         m_age += dtime;
489         if(m_age > 120)
490         {
491                 // Die
492                 m_removed = true;
493                 return;
494         }
495
496         m_after_jump_timer -= dtime;
497
498         v3f old_speed = m_speed_f;
499
500         // Apply gravity
501         m_speed_f.Y -= dtime*9.81*BS;
502
503         /*
504                 Move around if some player is close
505         */
506         bool player_is_close = false;
507         bool player_is_too_close = false;
508         v3f near_player_pos;
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++)
514         {
515                 Player *player = *i;
516                 v3f playerpos = player->getPosition();
517                 f32 dist = m_base_position.getDistanceFrom(playerpos);
518                 if(dist < BS*1.45)
519                 {
520                         player_is_too_close = true;
521                         near_player_pos = playerpos;
522                         break;
523                 }
524                 else if(dist < BS*15.0)
525                 {
526                         player_is_close = true;
527                         near_player_pos = playerpos;
528                 }
529         }
530
531         m_is_active = player_is_close;
532
533         v3f target_speed = m_speed_f;
534
535         if(!player_is_close)
536         {
537                 target_speed = v3f(0,0,0);
538         }
539         else
540         {
541                 // Move around
542
543                 v3f ndir = near_player_pos - m_base_position;
544                 ndir.Y = 0;
545                 ndir.normalize();
546
547                 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
548                 if(nyaw < m_yaw - 180)
549                         nyaw += 360;
550                 else if(nyaw > m_yaw + 180)
551                         nyaw -= 360;
552                 m_yaw = 0.95*m_yaw + 0.05*nyaw;
553                 m_yaw = wrapDegrees(m_yaw);
554                 
555                 f32 speed = 2*BS;
556
557                 if((m_touching_ground || m_after_jump_timer > 0.0)
558                                 && !player_is_too_close)
559                 {
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;
563                 }
564
565                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
566                                 < dtime*speed/2)
567                 {
568                         m_counter1 -= dtime;
569                         if(m_counter1 < 0.0)
570                         {
571                                 m_counter1 += 0.2;
572                                 // Jump
573                                 target_speed.Y = 5.0*BS;
574                                 m_after_jump_timer = 1.0;
575                         }
576                 }
577
578                 {
579                         m_counter2 -= dtime;
580                         if(m_counter2 < 0.0)
581                         {
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);
586                         }
587                 }
588         }
589         
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);
592         else
593                 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
594         
595         m_oldpos = m_base_position;
596
597         /*
598                 Move it, with collision detection
599         */
600
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;
605         /*// Limit speed
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;
613         
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
619         speed_diff.X *= 2;
620         speed_diff.Z *= 2;
621         float vel = speed_diff.getLength();
622         if(vel > tolerance)
623         {
624                 f32 damage_f = (vel - tolerance)/BS*factor;
625                 u16 damage = (u16)(damage_f+0.5);
626                 doDamage(damage);
627         }
628
629         setBasePosition(pos_f);
630
631         if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
632                 return;
633
634         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
635         {
636                 m_last_sent_position = pos_f;
637
638                 std::ostringstream os(std::ios::binary);
639                 // command (0 = update position)
640                 writeU8(os, 0);
641                 // pos
642                 writeV3F1000(os, m_base_position);
643                 // yaw
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);
648         }
649 }
650
651 std::string Oerkki1SAO::getClientInitializationData()
652 {
653         std::ostringstream os(std::ios::binary);
654         // version
655         writeU8(os, 0);
656         // pos
657         writeV3F1000(os, m_base_position);
658         return os.str();
659 }
660
661 std::string Oerkki1SAO::getStaticData()
662 {
663         //dstream<<__FUNCTION_NAME<<std::endl;
664         std::ostringstream os(std::ios::binary);
665         // version
666         writeU8(os, 0);
667         // hp
668         writeU8(os, m_hp);
669         return os.str();
670 }
671
672 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
673 {
674         m_speed_f += dir*12*BS;
675
676         u16 amount = 5;
677         doDamage(amount);
678         return 65536/100;
679 }
680
681 void Oerkki1SAO::doDamage(u16 d)
682 {
683         dstream<<"oerkki damage: "<<d<<std::endl;
684         
685         if(d < m_hp)
686         {
687                 m_hp -= d;
688         }
689         else
690         {
691                 // Die
692                 m_hp = 0;
693                 m_removed = true;
694         }
695
696         {
697                 std::ostringstream os(std::ios::binary);
698                 // command (1 = damage)
699                 writeU8(os, 1);
700                 // amount
701                 writeU8(os, d);
702                 // create message and add to list
703                 ActiveObjectMessage aom(getId(), false, os.str());
704                 m_messages_out.push_back(aom);
705         }
706 }
707
708 /*
709         FireflySAO
710 */
711
712 // Prototype
713 FireflySAO proto_FireflySAO(NULL, 0, v3f(0,0,0));
714
715 FireflySAO::FireflySAO(ServerEnvironment *env, u16 id, v3f pos):
716         ServerActiveObject(env, id, pos),
717         m_is_active(false),
718         m_speed_f(0,0,0)
719 {
720         ServerActiveObject::registerType(getType(), create);
721
722         m_oldpos = v3f(0,0,0);
723         m_last_sent_position = v3f(0,0,0);
724         m_yaw = 0;
725         m_counter1 = 0;
726         m_counter2 = 0;
727         m_age = 0;
728         m_touching_ground = false;
729 }
730
731 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, u16 id, v3f pos,
732                 const std::string &data)
733 {
734         std::istringstream is(data, std::ios::binary);
735         char buf[1];
736         // read version
737         is.read(buf, 1);
738         u8 version = buf[0];
739         // check if version is supported
740         if(version != 0)
741                 return NULL;
742         return new FireflySAO(env, id, pos);
743 }
744
745 void FireflySAO::step(float dtime, bool send_recommended)
746 {
747         assert(m_env);
748
749         if(m_is_active == false)
750         {
751                 if(m_inactive_interval.step(dtime, 0.5)==false)
752                         return;
753         }
754
755         /*
756                 The AI
757         */
758
759         // Apply (less) gravity
760         m_speed_f.Y -= dtime*3*BS;
761
762         /*
763                 Move around if some player is close
764         */
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++)
771         {
772                 Player *player = *i;
773                 v3f playerpos = player->getPosition();
774                 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
775                 {
776                         player_is_close = true;
777                         break;
778                 }
779         }
780
781         m_is_active = player_is_close;
782         
783         if(player_is_close == false)
784         {
785                 m_speed_f.X = 0;
786                 m_speed_f.Z = 0;
787         }
788         else
789         {
790                 // Move around
791                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
792                 f32 speed = BS/2;
793                 m_speed_f.X = speed * dir.X;
794                 m_speed_f.Z = speed * dir.Z;
795
796                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
797                                 < dtime*speed/2)
798                 {
799                         m_counter1 -= dtime;
800                         if(m_counter1 < 0.0)
801                         {
802                                 m_counter1 += 1.0;
803                                 m_speed_f.Y = 5.0*BS;
804                         }
805                 }
806
807                 {
808                         m_counter2 -= dtime;
809                         if(m_counter2 < 0.0)
810                         {
811                                 m_counter2 += (float)(myrand()%100)/100*3.0;
812                                 m_yaw += ((float)(myrand()%200)-100)/100*180;
813                                 m_yaw = wrapDegrees(m_yaw);
814                         }
815                 }
816         }
817         
818         m_oldpos = m_base_position;
819
820         /*
821                 Move it, with collision detection
822         */
823
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;
828         // Limit speed
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;
836         
837         setBasePosition(pos_f);
838
839         if(send_recommended == false)
840                 return;
841
842         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
843         {
844                 m_last_sent_position = pos_f;
845
846                 std::ostringstream os(std::ios::binary);
847                 // command (0 = update position)
848                 writeU8(os, 0);
849                 // pos
850                 writeV3F1000(os, m_base_position);
851                 // yaw
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);
856         }
857 }
858
859 std::string FireflySAO::getClientInitializationData()
860 {
861         std::ostringstream os(std::ios::binary);
862         // version
863         writeU8(os, 0);
864         // pos
865         writeV3F1000(os, m_base_position);
866         return os.str();
867 }
868
869 std::string FireflySAO::getStaticData()
870 {
871         //dstream<<__FUNCTION_NAME<<std::endl;
872         std::ostringstream os(std::ios::binary);
873         // version
874         writeU8(os, 0);
875         return os.str();
876 }
877
878 InventoryItem* FireflySAO::createPickedUpItem()
879 {
880         std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
881         InventoryItem *item = InventoryItem::deSerialize(is);
882         return item;
883 }