]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_sao.cpp
0bb518c165149678a8747acc1b6ff96f6140aa29
[dragonfireclient.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         else
230                 // Reflect changes to the item here
231                 m_inventorystring = item->getItemString();
232         
233         delete item;
234 }
235
236 /*
237         RatSAO
238 */
239
240 // Prototype
241 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
242
243 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
244         ServerActiveObject(env, id, pos),
245         m_is_active(false),
246         m_speed_f(0,0,0)
247 {
248         ServerActiveObject::registerType(getType(), create);
249
250         m_oldpos = v3f(0,0,0);
251         m_last_sent_position = v3f(0,0,0);
252         m_yaw = myrand_range(0,PI*2);
253         m_counter1 = 0;
254         m_counter2 = 0;
255         m_age = 0;
256         m_touching_ground = false;
257 }
258
259 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
260                 const std::string &data)
261 {
262         std::istringstream is(data, std::ios::binary);
263         char buf[1];
264         // read version
265         is.read(buf, 1);
266         u8 version = buf[0];
267         // check if version is supported
268         if(version != 0)
269                 return NULL;
270         return new RatSAO(env, id, pos);
271 }
272
273 void RatSAO::step(float dtime, bool send_recommended)
274 {
275         assert(m_env);
276
277         if(m_is_active == false)
278         {
279                 if(m_inactive_interval.step(dtime, 0.5)==false)
280                         return;
281         }
282
283         /*
284                 The AI
285         */
286
287         /*m_age += dtime;
288         if(m_age > 60)
289         {
290                 // Die
291                 m_removed = true;
292                 return;
293         }*/
294
295         // Apply gravity
296         m_speed_f.Y -= dtime*9.81*BS;
297
298         /*
299                 Move around if some player is close
300         */
301         bool player_is_close = false;
302         // Check connected players
303         core::list<Player*> players = m_env->getPlayers(true);
304         core::list<Player*>::Iterator i;
305         for(i = players.begin();
306                         i != players.end(); i++)
307         {
308                 Player *player = *i;
309                 v3f playerpos = player->getPosition();
310                 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
311                 {
312                         player_is_close = true;
313                         break;
314                 }
315         }
316
317         m_is_active = player_is_close;
318         
319         if(player_is_close == false)
320         {
321                 m_speed_f.X = 0;
322                 m_speed_f.Z = 0;
323         }
324         else
325         {
326                 // Move around
327                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
328                 f32 speed = 2*BS;
329                 m_speed_f.X = speed * dir.X;
330                 m_speed_f.Z = speed * dir.Z;
331
332                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
333                                 < dtime*speed/2)
334                 {
335                         m_counter1 -= dtime;
336                         if(m_counter1 < 0.0)
337                         {
338                                 m_counter1 += 1.0;
339                                 m_speed_f.Y = 5.0*BS;
340                         }
341                 }
342
343                 {
344                         m_counter2 -= dtime;
345                         if(m_counter2 < 0.0)
346                         {
347                                 m_counter2 += (float)(myrand()%100)/100*3.0;
348                                 m_yaw += ((float)(myrand()%200)-100)/100*180;
349                                 m_yaw = wrapDegrees(m_yaw);
350                         }
351                 }
352         }
353         
354         m_oldpos = m_base_position;
355
356         /*
357                 Move it, with collision detection
358         */
359
360         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
361         collisionMoveResult moveresult;
362         // Maximum movement without glitches
363         f32 pos_max_d = BS*0.25;
364         // Limit speed
365         if(m_speed_f.getLength()*dtime > pos_max_d)
366                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
367         v3f pos_f = getBasePosition();
368         v3f pos_f_old = pos_f;
369         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
370                         box, dtime, pos_f, m_speed_f);
371         m_touching_ground = moveresult.touching_ground;
372         
373         setBasePosition(pos_f);
374
375         if(send_recommended == false)
376                 return;
377
378         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
379         {
380                 m_last_sent_position = pos_f;
381
382                 std::ostringstream os(std::ios::binary);
383                 // command (0 = update position)
384                 writeU8(os, 0);
385                 // pos
386                 writeV3F1000(os, m_base_position);
387                 // yaw
388                 writeF1000(os, m_yaw);
389                 // create message and add to list
390                 ActiveObjectMessage aom(getId(), false, os.str());
391                 m_messages_out.push_back(aom);
392         }
393 }
394
395 std::string RatSAO::getClientInitializationData()
396 {
397         std::ostringstream os(std::ios::binary);
398         // version
399         writeU8(os, 0);
400         // pos
401         writeV3F1000(os, m_base_position);
402         return os.str();
403 }
404
405 std::string RatSAO::getStaticData()
406 {
407         //dstream<<__FUNCTION_NAME<<std::endl;
408         std::ostringstream os(std::ios::binary);
409         // version
410         writeU8(os, 0);
411         return os.str();
412 }
413
414 InventoryItem* RatSAO::createPickedUpItem()
415 {
416         std::istringstream is("CraftItem rat 1", std::ios_base::binary);
417         InventoryItem *item = InventoryItem::deSerialize(is);
418         return item;
419 }
420
421 /*
422         Oerkki1SAO
423 */
424
425 // Y is copied, X and Z change is limited
426 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
427 {
428         v3f d_wanted = target_speed - speed;
429         d_wanted.Y = 0;
430         f32 dl_wanted = d_wanted.getLength();
431         f32 dl = dl_wanted;
432         if(dl > max_increase)
433                 dl = max_increase;
434         
435         v3f d = d_wanted.normalize() * dl;
436
437         speed.X += d.X;
438         speed.Z += d.Z;
439         speed.Y = target_speed.Y;
440 }
441
442 // Prototype
443 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
444
445 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
446         ServerActiveObject(env, id, pos),
447         m_is_active(false),
448         m_speed_f(0,0,0)
449 {
450         ServerActiveObject::registerType(getType(), create);
451
452         m_oldpos = v3f(0,0,0);
453         m_last_sent_position = v3f(0,0,0);
454         m_yaw = 0;
455         m_counter1 = 0;
456         m_counter2 = 0;
457         m_age = 0;
458         m_touching_ground = false;
459         m_hp = 20;
460         m_after_jump_timer = 0;
461 }
462
463 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
464                 const std::string &data)
465 {
466         std::istringstream is(data, std::ios::binary);
467         // read version
468         u8 version = readU8(is);
469         // read hp
470         u8 hp = readU8(is);
471         // check if version is supported
472         if(version != 0)
473                 return NULL;
474         Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
475         o->m_hp = hp;
476         return o;
477 }
478
479 void Oerkki1SAO::step(float dtime, bool send_recommended)
480 {
481         assert(m_env);
482
483         if(m_is_active == false)
484         {
485                 if(m_inactive_interval.step(dtime, 0.5)==false)
486                         return;
487         }
488
489         /*
490                 The AI
491         */
492
493         m_age += dtime;
494         if(m_age > 120)
495         {
496                 // Die
497                 m_removed = true;
498                 return;
499         }
500
501         m_after_jump_timer -= dtime;
502
503         v3f old_speed = m_speed_f;
504
505         // Apply gravity
506         m_speed_f.Y -= dtime*9.81*BS;
507
508         /*
509                 Move around if some player is close
510         */
511         bool player_is_close = false;
512         bool player_is_too_close = false;
513         v3f near_player_pos;
514         // Check connected players
515         core::list<Player*> players = m_env->getPlayers(true);
516         core::list<Player*>::Iterator i;
517         for(i = players.begin();
518                         i != players.end(); i++)
519         {
520                 Player *player = *i;
521                 v3f playerpos = player->getPosition();
522                 f32 dist = m_base_position.getDistanceFrom(playerpos);
523                 if(dist < BS*1.45)
524                 {
525                         player_is_too_close = true;
526                         near_player_pos = playerpos;
527                         break;
528                 }
529                 else if(dist < BS*15.0)
530                 {
531                         player_is_close = true;
532                         near_player_pos = playerpos;
533                 }
534         }
535
536         m_is_active = player_is_close;
537
538         v3f target_speed = m_speed_f;
539
540         if(!player_is_close)
541         {
542                 target_speed = v3f(0,0,0);
543         }
544         else
545         {
546                 // Move around
547
548                 v3f ndir = near_player_pos - m_base_position;
549                 ndir.Y = 0;
550                 ndir.normalize();
551
552                 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
553                 if(nyaw < m_yaw - 180)
554                         nyaw += 360;
555                 else if(nyaw > m_yaw + 180)
556                         nyaw -= 360;
557                 m_yaw = 0.95*m_yaw + 0.05*nyaw;
558                 m_yaw = wrapDegrees(m_yaw);
559                 
560                 f32 speed = 2*BS;
561
562                 if((m_touching_ground || m_after_jump_timer > 0.0)
563                                 && !player_is_too_close)
564                 {
565                         v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
566                         target_speed.X = speed * dir.X;
567                         target_speed.Z = speed * dir.Z;
568                 }
569
570                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
571                                 < dtime*speed/2)
572                 {
573                         m_counter1 -= dtime;
574                         if(m_counter1 < 0.0)
575                         {
576                                 m_counter1 += 0.2;
577                                 // Jump
578                                 target_speed.Y = 5.0*BS;
579                                 m_after_jump_timer = 1.0;
580                         }
581                 }
582
583                 {
584                         m_counter2 -= dtime;
585                         if(m_counter2 < 0.0)
586                         {
587                                 m_counter2 += (float)(myrand()%100)/100*3.0;
588                                 //m_yaw += ((float)(myrand()%200)-100)/100*180;
589                                 m_yaw += ((float)(myrand()%200)-100)/100*90;
590                                 m_yaw = wrapDegrees(m_yaw);
591                         }
592                 }
593         }
594         
595         if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
596                 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
597         else
598                 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
599         
600         m_oldpos = m_base_position;
601
602         /*
603                 Move it, with collision detection
604         */
605
606         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
607         collisionMoveResult moveresult;
608         // Maximum movement without glitches
609         f32 pos_max_d = BS*0.25;
610         /*// Limit speed
611         if(m_speed_f.getLength()*dtime > pos_max_d)
612                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
613         v3f pos_f = getBasePosition();
614         v3f pos_f_old = pos_f;
615         moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
616                         box, dtime, pos_f, m_speed_f);
617         m_touching_ground = moveresult.touching_ground;
618         
619         // Do collision damage
620         float tolerance = BS*12;
621         float factor = BS*0.5;
622         v3f speed_diff = old_speed - m_speed_f;
623         // Increase effect in X and Z
624         speed_diff.X *= 2;
625         speed_diff.Z *= 2;
626         float vel = speed_diff.getLength();
627         if(vel > tolerance)
628         {
629                 f32 damage_f = (vel - tolerance)/BS*factor;
630                 u16 damage = (u16)(damage_f+0.5);
631                 doDamage(damage);
632         }
633
634         setBasePosition(pos_f);
635
636         if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
637                 return;
638
639         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
640         {
641                 m_last_sent_position = pos_f;
642
643                 std::ostringstream os(std::ios::binary);
644                 // command (0 = update position)
645                 writeU8(os, 0);
646                 // pos
647                 writeV3F1000(os, m_base_position);
648                 // yaw
649                 writeF1000(os, m_yaw);
650                 // create message and add to list
651                 ActiveObjectMessage aom(getId(), false, os.str());
652                 m_messages_out.push_back(aom);
653         }
654 }
655
656 std::string Oerkki1SAO::getClientInitializationData()
657 {
658         std::ostringstream os(std::ios::binary);
659         // version
660         writeU8(os, 0);
661         // pos
662         writeV3F1000(os, m_base_position);
663         return os.str();
664 }
665
666 std::string Oerkki1SAO::getStaticData()
667 {
668         //dstream<<__FUNCTION_NAME<<std::endl;
669         std::ostringstream os(std::ios::binary);
670         // version
671         writeU8(os, 0);
672         // hp
673         writeU8(os, m_hp);
674         return os.str();
675 }
676
677 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
678 {
679         m_speed_f += dir*12*BS;
680
681         u16 amount = 5;
682         doDamage(amount);
683         return 65536/100;
684 }
685
686 void Oerkki1SAO::doDamage(u16 d)
687 {
688         dstream<<"oerkki damage: "<<d<<std::endl;
689         
690         if(d < m_hp)
691         {
692                 m_hp -= d;
693         }
694         else
695         {
696                 // Die
697                 m_hp = 0;
698                 m_removed = true;
699         }
700
701         {
702                 std::ostringstream os(std::ios::binary);
703                 // command (1 = damage)
704                 writeU8(os, 1);
705                 // amount
706                 writeU8(os, d);
707                 // create message and add to list
708                 ActiveObjectMessage aom(getId(), false, os.str());
709                 m_messages_out.push_back(aom);
710         }
711 }
712
713 /*
714         FireflySAO
715 */
716
717 // Prototype
718 FireflySAO proto_FireflySAO(NULL, 0, v3f(0,0,0));
719
720 FireflySAO::FireflySAO(ServerEnvironment *env, u16 id, v3f pos):
721         ServerActiveObject(env, id, pos),
722         m_is_active(false),
723         m_speed_f(0,0,0)
724 {
725         ServerActiveObject::registerType(getType(), create);
726
727         m_oldpos = v3f(0,0,0);
728         m_last_sent_position = v3f(0,0,0);
729         m_yaw = 0;
730         m_counter1 = 0;
731         m_counter2 = 0;
732         m_age = 0;
733         m_touching_ground = false;
734 }
735
736 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, u16 id, v3f pos,
737                 const std::string &data)
738 {
739         std::istringstream is(data, std::ios::binary);
740         char buf[1];
741         // read version
742         is.read(buf, 1);
743         u8 version = buf[0];
744         // check if version is supported
745         if(version != 0)
746                 return NULL;
747         return new FireflySAO(env, id, pos);
748 }
749
750 void FireflySAO::step(float dtime, bool send_recommended)
751 {
752         assert(m_env);
753
754         if(m_is_active == false)
755         {
756                 if(m_inactive_interval.step(dtime, 0.5)==false)
757                         return;
758         }
759
760         /*
761                 The AI
762         */
763
764         // Apply (less) gravity
765         m_speed_f.Y -= dtime*3*BS;
766
767         /*
768                 Move around if some player is close
769         */
770         bool player_is_close = false;
771         // Check connected players
772         core::list<Player*> players = m_env->getPlayers(true);
773         core::list<Player*>::Iterator i;
774         for(i = players.begin();
775                         i != players.end(); i++)
776         {
777                 Player *player = *i;
778                 v3f playerpos = player->getPosition();
779                 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
780                 {
781                         player_is_close = true;
782                         break;
783                 }
784         }
785
786         m_is_active = player_is_close;
787         
788         if(player_is_close == false)
789         {
790                 m_speed_f.X = 0;
791                 m_speed_f.Z = 0;
792         }
793         else
794         {
795                 // Move around
796                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
797                 f32 speed = BS/2;
798                 m_speed_f.X = speed * dir.X;
799                 m_speed_f.Z = speed * dir.Z;
800
801                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
802                                 < dtime*speed/2)
803                 {
804                         m_counter1 -= dtime;
805                         if(m_counter1 < 0.0)
806                         {
807                                 m_counter1 += 1.0;
808                                 m_speed_f.Y = 5.0*BS;
809                         }
810                 }
811
812                 {
813                         m_counter2 -= dtime;
814                         if(m_counter2 < 0.0)
815                         {
816                                 m_counter2 += (float)(myrand()%100)/100*3.0;
817                                 m_yaw += ((float)(myrand()%200)-100)/100*180;
818                                 m_yaw = wrapDegrees(m_yaw);
819                         }
820                 }
821         }
822         
823         m_oldpos = m_base_position;
824
825         /*
826                 Move it, with collision detection
827         */
828
829         core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
830         collisionMoveResult moveresult;
831         // Maximum movement without glitches
832         f32 pos_max_d = BS*0.25;
833         // Limit speed
834         if(m_speed_f.getLength()*dtime > pos_max_d)
835                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
836         v3f pos_f = getBasePosition();
837         v3f pos_f_old = pos_f;
838         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
839                         box, dtime, pos_f, m_speed_f);
840         m_touching_ground = moveresult.touching_ground;
841         
842         setBasePosition(pos_f);
843
844         if(send_recommended == false)
845                 return;
846
847         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
848         {
849                 m_last_sent_position = pos_f;
850
851                 std::ostringstream os(std::ios::binary);
852                 // command (0 = update position)
853                 writeU8(os, 0);
854                 // pos
855                 writeV3F1000(os, m_base_position);
856                 // yaw
857                 writeF1000(os, m_yaw);
858                 // create message and add to list
859                 ActiveObjectMessage aom(getId(), false, os.str());
860                 m_messages_out.push_back(aom);
861         }
862 }
863
864 std::string FireflySAO::getClientInitializationData()
865 {
866         std::ostringstream os(std::ios::binary);
867         // version
868         writeU8(os, 0);
869         // pos
870         writeV3F1000(os, m_base_position);
871         return os.str();
872 }
873
874 std::string FireflySAO::getStaticData()
875 {
876         //dstream<<__FUNCTION_NAME<<std::endl;
877         std::ostringstream os(std::ios::binary);
878         // version
879         writeU8(os, 0);
880         return os.str();
881 }
882
883 InventoryItem* FireflySAO::createPickedUpItem()
884 {
885         std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
886         InventoryItem *item = InventoryItem::deSerialize(is);
887         return item;
888 }