]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_sao.cpp
Use the logger; also, default to not showing much crap in console. Use --info-on...
[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 #include "settings.h"
24
25 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
26
27 /* Some helper functions */
28
29 // Y is copied, X and Z change is limited
30 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
31 {
32         v3f d_wanted = target_speed - speed;
33         d_wanted.Y = 0;
34         f32 dl_wanted = d_wanted.getLength();
35         f32 dl = dl_wanted;
36         if(dl > max_increase)
37                 dl = max_increase;
38         
39         v3f d = d_wanted.normalize() * dl;
40
41         speed.X += d.X;
42         speed.Z += d.Z;
43         speed.Y = target_speed.Y;
44 }
45
46 /*
47         TestSAO
48 */
49
50 // Prototype
51 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
52
53 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
54         ServerActiveObject(env, id, pos),
55         m_timer1(0),
56         m_age(0)
57 {
58         ServerActiveObject::registerType(getType(), create);
59 }
60
61 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
62                 const std::string &data)
63 {
64         return new TestSAO(env, id, pos);
65 }
66
67 void TestSAO::step(float dtime, bool send_recommended)
68 {
69         m_age += dtime;
70         if(m_age > 10)
71         {
72                 m_removed = true;
73                 return;
74         }
75
76         m_base_position.Y += dtime * BS * 2;
77         if(m_base_position.Y > 8*BS)
78                 m_base_position.Y = 2*BS;
79
80         if(send_recommended == false)
81                 return;
82
83         m_timer1 -= dtime;
84         if(m_timer1 < 0.0)
85         {
86                 m_timer1 += 0.125;
87                 //infostream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
88
89                 std::string data;
90
91                 data += itos(0); // 0 = position
92                 data += " ";
93                 data += itos(m_base_position.X);
94                 data += " ";
95                 data += itos(m_base_position.Y);
96                 data += " ";
97                 data += itos(m_base_position.Z);
98
99                 ActiveObjectMessage aom(getId(), false, data);
100                 m_messages_out.push_back(aom);
101         }
102 }
103
104
105 /*
106         ItemSAO
107 */
108
109 // Prototype
110 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
111
112 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
113                 const std::string inventorystring):
114         ServerActiveObject(env, id, pos),
115         m_inventorystring(inventorystring),
116         m_speed_f(0,0,0),
117         m_last_sent_position(0,0,0)
118 {
119         ServerActiveObject::registerType(getType(), create);
120 }
121
122 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
123                 const std::string &data)
124 {
125         std::istringstream is(data, std::ios::binary);
126         char buf[1];
127         // read version
128         is.read(buf, 1);
129         u8 version = buf[0];
130         // check if version is supported
131         if(version != 0)
132                 return NULL;
133         std::string inventorystring = deSerializeString(is);
134         infostream<<"ItemSAO::create(): Creating item \""
135                         <<inventorystring<<"\""<<std::endl;
136         return new ItemSAO(env, id, pos, inventorystring);
137 }
138
139 void ItemSAO::step(float dtime, bool send_recommended)
140 {
141         assert(m_env);
142
143         const float interval = 0.2;
144         if(m_move_interval.step(dtime, interval)==false)
145                 return;
146         dtime = interval;
147         
148         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
149         collisionMoveResult moveresult;
150         // Apply gravity
151         m_speed_f += v3f(0, -dtime*9.81*BS, 0);
152         // Maximum movement without glitches
153         f32 pos_max_d = BS*0.25;
154         // Limit speed
155         if(m_speed_f.getLength()*dtime > pos_max_d)
156                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
157         v3f pos_f = getBasePosition();
158         v3f pos_f_old = pos_f;
159         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
160                         box, dtime, pos_f, m_speed_f);
161         
162         if(send_recommended == false)
163                 return;
164
165         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
166         {
167                 setBasePosition(pos_f);
168                 m_last_sent_position = pos_f;
169
170                 std::ostringstream os(std::ios::binary);
171                 char buf[6];
172                 // command (0 = update position)
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                 // create message and add to list
183                 ActiveObjectMessage aom(getId(), false, os.str());
184                 m_messages_out.push_back(aom);
185         }
186 }
187
188 std::string ItemSAO::getClientInitializationData()
189 {
190         std::ostringstream os(std::ios::binary);
191         char buf[6];
192         // version
193         buf[0] = 0;
194         os.write(buf, 1);
195         // pos
196         writeS32((u8*)buf, m_base_position.X*1000);
197         os.write(buf, 4);
198         writeS32((u8*)buf, m_base_position.Y*1000);
199         os.write(buf, 4);
200         writeS32((u8*)buf, m_base_position.Z*1000);
201         os.write(buf, 4);
202         // inventorystring
203         os<<serializeString(m_inventorystring);
204         return os.str();
205 }
206
207 std::string ItemSAO::getStaticData()
208 {
209         infostream<<__FUNCTION_NAME<<std::endl;
210         std::ostringstream os(std::ios::binary);
211         char buf[1];
212         // version
213         buf[0] = 0;
214         os.write(buf, 1);
215         // inventorystring
216         os<<serializeString(m_inventorystring);
217         return os.str();
218 }
219
220 InventoryItem * ItemSAO::createInventoryItem()
221 {
222         try{
223                 std::istringstream is(m_inventorystring, std::ios_base::binary);
224                 InventoryItem *item = InventoryItem::deSerialize(is);
225                 infostream<<__FUNCTION_NAME<<": m_inventorystring=\""
226                                 <<m_inventorystring<<"\" -> item="<<item
227                                 <<std::endl;
228                 return item;
229         }
230         catch(SerializationError &e)
231         {
232                 infostream<<__FUNCTION_NAME<<": serialization error: "
233                                 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
234                 return NULL;
235         }
236 }
237
238 void ItemSAO::rightClick(Player *player)
239 {
240         infostream<<__FUNCTION_NAME<<std::endl;
241         InventoryItem *item = createInventoryItem();
242         if(item == NULL)
243                 return;
244         
245         bool to_be_deleted = item->use(m_env, player);
246
247         if(to_be_deleted)
248                 m_removed = true;
249         else
250                 // Reflect changes to the item here
251                 m_inventorystring = item->getItemString();
252         
253         delete item;
254 }
255
256 /*
257         RatSAO
258 */
259
260 // Prototype
261 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
262
263 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
264         ServerActiveObject(env, id, pos),
265         m_is_active(false),
266         m_speed_f(0,0,0)
267 {
268         ServerActiveObject::registerType(getType(), create);
269
270         m_oldpos = v3f(0,0,0);
271         m_last_sent_position = v3f(0,0,0);
272         m_yaw = myrand_range(0,PI*2);
273         m_counter1 = 0;
274         m_counter2 = 0;
275         m_age = 0;
276         m_touching_ground = false;
277 }
278
279 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
280                 const std::string &data)
281 {
282         std::istringstream is(data, std::ios::binary);
283         char buf[1];
284         // read version
285         is.read(buf, 1);
286         u8 version = buf[0];
287         // check if version is supported
288         if(version != 0)
289                 return NULL;
290         return new RatSAO(env, id, pos);
291 }
292
293 void RatSAO::step(float dtime, bool send_recommended)
294 {
295         assert(m_env);
296
297         if(m_is_active == false)
298         {
299                 if(m_inactive_interval.step(dtime, 0.5)==false)
300                         return;
301         }
302
303         /*
304                 The AI
305         */
306
307         /*m_age += dtime;
308         if(m_age > 60)
309         {
310                 // Die
311                 m_removed = true;
312                 return;
313         }*/
314
315         // Apply gravity
316         m_speed_f.Y -= dtime*9.81*BS;
317
318         /*
319                 Move around if some player is close
320         */
321         bool player_is_close = false;
322         // Check connected players
323         core::list<Player*> players = m_env->getPlayers(true);
324         core::list<Player*>::Iterator i;
325         for(i = players.begin();
326                         i != players.end(); i++)
327         {
328                 Player *player = *i;
329                 v3f playerpos = player->getPosition();
330                 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
331                 {
332                         player_is_close = true;
333                         break;
334                 }
335         }
336
337         m_is_active = player_is_close;
338         
339         if(player_is_close == false)
340         {
341                 m_speed_f.X = 0;
342                 m_speed_f.Z = 0;
343         }
344         else
345         {
346                 // Move around
347                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
348                 f32 speed = 2*BS;
349                 m_speed_f.X = speed * dir.X;
350                 m_speed_f.Z = speed * dir.Z;
351
352                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
353                                 < dtime*speed/2)
354                 {
355                         m_counter1 -= dtime;
356                         if(m_counter1 < 0.0)
357                         {
358                                 m_counter1 += 1.0;
359                                 m_speed_f.Y = 5.0*BS;
360                         }
361                 }
362
363                 {
364                         m_counter2 -= dtime;
365                         if(m_counter2 < 0.0)
366                         {
367                                 m_counter2 += (float)(myrand()%100)/100*3.0;
368                                 m_yaw += ((float)(myrand()%200)-100)/100*180;
369                                 m_yaw = wrapDegrees(m_yaw);
370                         }
371                 }
372         }
373         
374         m_oldpos = m_base_position;
375
376         /*
377                 Move it, with collision detection
378         */
379
380         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
381         collisionMoveResult moveresult;
382         // Maximum movement without glitches
383         f32 pos_max_d = BS*0.25;
384         // Limit speed
385         if(m_speed_f.getLength()*dtime > pos_max_d)
386                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
387         v3f pos_f = getBasePosition();
388         v3f pos_f_old = pos_f;
389         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
390                         box, dtime, pos_f, m_speed_f);
391         m_touching_ground = moveresult.touching_ground;
392         
393         setBasePosition(pos_f);
394
395         if(send_recommended == false)
396                 return;
397
398         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
399         {
400                 m_last_sent_position = pos_f;
401
402                 std::ostringstream os(std::ios::binary);
403                 // command (0 = update position)
404                 writeU8(os, 0);
405                 // pos
406                 writeV3F1000(os, m_base_position);
407                 // yaw
408                 writeF1000(os, m_yaw);
409                 // create message and add to list
410                 ActiveObjectMessage aom(getId(), false, os.str());
411                 m_messages_out.push_back(aom);
412         }
413 }
414
415 std::string RatSAO::getClientInitializationData()
416 {
417         std::ostringstream os(std::ios::binary);
418         // version
419         writeU8(os, 0);
420         // pos
421         writeV3F1000(os, m_base_position);
422         return os.str();
423 }
424
425 std::string RatSAO::getStaticData()
426 {
427         //infostream<<__FUNCTION_NAME<<std::endl;
428         std::ostringstream os(std::ios::binary);
429         // version
430         writeU8(os, 0);
431         return os.str();
432 }
433
434 InventoryItem* RatSAO::createPickedUpItem()
435 {
436         std::istringstream is("CraftItem rat 1", std::ios_base::binary);
437         InventoryItem *item = InventoryItem::deSerialize(is);
438         return item;
439 }
440
441 /*
442         Oerkki1SAO
443 */
444
445 // Prototype
446 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
447
448 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
449         ServerActiveObject(env, id, pos),
450         m_is_active(false),
451         m_speed_f(0,0,0)
452 {
453         ServerActiveObject::registerType(getType(), create);
454
455         m_oldpos = v3f(0,0,0);
456         m_last_sent_position = v3f(0,0,0);
457         m_yaw = 0;
458         m_counter1 = 0;
459         m_counter2 = 0;
460         m_age = 0;
461         m_touching_ground = false;
462         m_hp = 20;
463         m_after_jump_timer = 0;
464 }
465
466 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
467                 const std::string &data)
468 {
469         std::istringstream is(data, std::ios::binary);
470         // read version
471         u8 version = readU8(is);
472         // read hp
473         u8 hp = readU8(is);
474         // check if version is supported
475         if(version != 0)
476                 return NULL;
477         Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
478         o->m_hp = hp;
479         return o;
480 }
481
482 void Oerkki1SAO::step(float dtime, bool send_recommended)
483 {
484         assert(m_env);
485
486         if(m_is_active == false)
487         {
488                 if(m_inactive_interval.step(dtime, 0.5)==false)
489                         return;
490         }
491
492         /*
493                 The AI
494         */
495
496         m_age += dtime;
497         if(m_age > 120)
498         {
499                 // Die
500                 m_removed = true;
501                 return;
502         }
503
504         m_after_jump_timer -= dtime;
505
506         v3f old_speed = m_speed_f;
507
508         // Apply gravity
509         m_speed_f.Y -= dtime*9.81*BS;
510
511         /*
512                 Move around if some player is close
513         */
514         bool player_is_close = false;
515         bool player_is_too_close = false;
516         v3f near_player_pos;
517         // Check connected players
518         core::list<Player*> players = m_env->getPlayers(true);
519         core::list<Player*>::Iterator i;
520         for(i = players.begin();
521                         i != players.end(); i++)
522         {
523                 Player *player = *i;
524                 v3f playerpos = player->getPosition();
525                 f32 dist = m_base_position.getDistanceFrom(playerpos);
526                 if(dist < BS*0.6)
527                 {
528                         m_removed = true;
529                         return;
530                         player_is_too_close = true;
531                         near_player_pos = playerpos;
532                 }
533                 else if(dist < BS*15.0 && !player_is_too_close)
534                 {
535                         player_is_close = true;
536                         near_player_pos = playerpos;
537                 }
538         }
539
540         m_is_active = player_is_close;
541
542         v3f target_speed = m_speed_f;
543
544         if(!player_is_close)
545         {
546                 target_speed = v3f(0,0,0);
547         }
548         else
549         {
550                 // Move around
551
552                 v3f ndir = near_player_pos - m_base_position;
553                 ndir.Y = 0;
554                 ndir.normalize();
555
556                 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
557                 if(nyaw < m_yaw - 180)
558                         nyaw += 360;
559                 else if(nyaw > m_yaw + 180)
560                         nyaw -= 360;
561                 m_yaw = 0.95*m_yaw + 0.05*nyaw;
562                 m_yaw = wrapDegrees(m_yaw);
563                 
564                 f32 speed = 2*BS;
565
566                 if((m_touching_ground || m_after_jump_timer > 0.0)
567                                 && !player_is_too_close)
568                 {
569                         v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
570                         target_speed.X = speed * dir.X;
571                         target_speed.Z = speed * dir.Z;
572                 }
573
574                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
575                                 < dtime*speed/2)
576                 {
577                         m_counter1 -= dtime;
578                         if(m_counter1 < 0.0)
579                         {
580                                 m_counter1 += 0.2;
581                                 // Jump
582                                 target_speed.Y = 5.0*BS;
583                                 m_after_jump_timer = 1.0;
584                         }
585                 }
586
587                 {
588                         m_counter2 -= dtime;
589                         if(m_counter2 < 0.0)
590                         {
591                                 m_counter2 += (float)(myrand()%100)/100*3.0;
592                                 //m_yaw += ((float)(myrand()%200)-100)/100*180;
593                                 m_yaw += ((float)(myrand()%200)-100)/100*90;
594                                 m_yaw = wrapDegrees(m_yaw);
595                         }
596                 }
597         }
598         
599         if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
600                 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
601         else
602                 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
603         
604         m_oldpos = m_base_position;
605
606         /*
607                 Move it, with collision detection
608         */
609
610         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
611         collisionMoveResult moveresult;
612         // Maximum movement without glitches
613         f32 pos_max_d = BS*0.25;
614         /*// Limit speed
615         if(m_speed_f.getLength()*dtime > pos_max_d)
616                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
617         v3f pos_f = getBasePosition();
618         v3f pos_f_old = pos_f;
619         moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
620                         box, dtime, pos_f, m_speed_f);
621         m_touching_ground = moveresult.touching_ground;
622         
623         // Do collision damage
624         float tolerance = BS*30;
625         float factor = BS*0.5;
626         v3f speed_diff = old_speed - m_speed_f;
627         // Increase effect in X and Z
628         speed_diff.X *= 2;
629         speed_diff.Z *= 2;
630         float vel = speed_diff.getLength();
631         if(vel > tolerance)
632         {
633                 f32 damage_f = (vel - tolerance)/BS*factor;
634                 u16 damage = (u16)(damage_f+0.5);
635                 doDamage(damage);
636         }
637
638         setBasePosition(pos_f);
639
640         if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
641                 return;
642
643         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
644         {
645                 m_last_sent_position = pos_f;
646
647                 std::ostringstream os(std::ios::binary);
648                 // command (0 = update position)
649                 writeU8(os, 0);
650                 // pos
651                 writeV3F1000(os, m_base_position);
652                 // yaw
653                 writeF1000(os, m_yaw);
654                 // create message and add to list
655                 ActiveObjectMessage aom(getId(), false, os.str());
656                 m_messages_out.push_back(aom);
657         }
658 }
659
660 std::string Oerkki1SAO::getClientInitializationData()
661 {
662         std::ostringstream os(std::ios::binary);
663         // version
664         writeU8(os, 0);
665         // pos
666         writeV3F1000(os, m_base_position);
667         return os.str();
668 }
669
670 std::string Oerkki1SAO::getStaticData()
671 {
672         //infostream<<__FUNCTION_NAME<<std::endl;
673         std::ostringstream os(std::ios::binary);
674         // version
675         writeU8(os, 0);
676         // hp
677         writeU8(os, m_hp);
678         return os.str();
679 }
680
681 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
682 {
683         m_speed_f += dir*12*BS;
684
685         u16 amount = 20;
686         doDamage(amount);
687         return 65536/100;
688 }
689
690 void Oerkki1SAO::doDamage(u16 d)
691 {
692         infostream<<"oerkki damage: "<<d<<std::endl;
693         
694         if(d < m_hp)
695         {
696                 m_hp -= d;
697         }
698         else
699         {
700                 // Die
701                 m_hp = 0;
702                 m_removed = true;
703         }
704
705         {
706                 std::ostringstream os(std::ios::binary);
707                 // command (1 = damage)
708                 writeU8(os, 1);
709                 // amount
710                 writeU8(os, d);
711                 // create message and add to list
712                 ActiveObjectMessage aom(getId(), false, os.str());
713                 m_messages_out.push_back(aom);
714         }
715 }
716
717 /*
718         FireflySAO
719 */
720
721 // Prototype
722 FireflySAO proto_FireflySAO(NULL, 0, v3f(0,0,0));
723
724 FireflySAO::FireflySAO(ServerEnvironment *env, u16 id, v3f pos):
725         ServerActiveObject(env, id, pos),
726         m_is_active(false),
727         m_speed_f(0,0,0)
728 {
729         ServerActiveObject::registerType(getType(), create);
730
731         m_oldpos = v3f(0,0,0);
732         m_last_sent_position = v3f(0,0,0);
733         m_yaw = 0;
734         m_counter1 = 0;
735         m_counter2 = 0;
736         m_age = 0;
737         m_touching_ground = false;
738 }
739
740 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, u16 id, v3f pos,
741                 const std::string &data)
742 {
743         std::istringstream is(data, std::ios::binary);
744         char buf[1];
745         // read version
746         is.read(buf, 1);
747         u8 version = buf[0];
748         // check if version is supported
749         if(version != 0)
750                 return NULL;
751         return new FireflySAO(env, id, pos);
752 }
753
754 void FireflySAO::step(float dtime, bool send_recommended)
755 {
756         assert(m_env);
757
758         if(m_is_active == false)
759         {
760                 if(m_inactive_interval.step(dtime, 0.5)==false)
761                         return;
762         }
763
764         /*
765                 The AI
766         */
767
768         // Apply (less) gravity
769         m_speed_f.Y -= dtime*3*BS;
770
771         /*
772                 Move around if some player is close
773         */
774         bool player_is_close = false;
775         // Check connected players
776         core::list<Player*> players = m_env->getPlayers(true);
777         core::list<Player*>::Iterator i;
778         for(i = players.begin();
779                         i != players.end(); i++)
780         {
781                 Player *player = *i;
782                 v3f playerpos = player->getPosition();
783                 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
784                 {
785                         player_is_close = true;
786                         break;
787                 }
788         }
789
790         m_is_active = player_is_close;
791         
792         if(player_is_close == false)
793         {
794                 m_speed_f.X = 0;
795                 m_speed_f.Z = 0;
796         }
797         else
798         {
799                 // Move around
800                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
801                 f32 speed = BS/2;
802                 m_speed_f.X = speed * dir.X;
803                 m_speed_f.Z = speed * dir.Z;
804
805                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
806                                 < dtime*speed/2)
807                 {
808                         m_counter1 -= dtime;
809                         if(m_counter1 < 0.0)
810                         {
811                                 m_counter1 += 1.0;
812                                 m_speed_f.Y = 5.0*BS;
813                         }
814                 }
815
816                 {
817                         m_counter2 -= dtime;
818                         if(m_counter2 < 0.0)
819                         {
820                                 m_counter2 += (float)(myrand()%100)/100*3.0;
821                                 m_yaw += ((float)(myrand()%200)-100)/100*180;
822                                 m_yaw = wrapDegrees(m_yaw);
823                         }
824                 }
825         }
826         
827         m_oldpos = m_base_position;
828
829         /*
830                 Move it, with collision detection
831         */
832
833         core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
834         collisionMoveResult moveresult;
835         // Maximum movement without glitches
836         f32 pos_max_d = BS*0.25;
837         // Limit speed
838         if(m_speed_f.getLength()*dtime > pos_max_d)
839                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
840         v3f pos_f = getBasePosition();
841         v3f pos_f_old = pos_f;
842         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
843                         box, dtime, pos_f, m_speed_f);
844         m_touching_ground = moveresult.touching_ground;
845         
846         setBasePosition(pos_f);
847
848         if(send_recommended == false)
849                 return;
850
851         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
852         {
853                 m_last_sent_position = pos_f;
854
855                 std::ostringstream os(std::ios::binary);
856                 // command (0 = update position)
857                 writeU8(os, 0);
858                 // pos
859                 writeV3F1000(os, m_base_position);
860                 // yaw
861                 writeF1000(os, m_yaw);
862                 // create message and add to list
863                 ActiveObjectMessage aom(getId(), false, os.str());
864                 m_messages_out.push_back(aom);
865         }
866 }
867
868 std::string FireflySAO::getClientInitializationData()
869 {
870         std::ostringstream os(std::ios::binary);
871         // version
872         writeU8(os, 0);
873         // pos
874         writeV3F1000(os, m_base_position);
875         return os.str();
876 }
877
878 std::string FireflySAO::getStaticData()
879 {
880         //infostream<<__FUNCTION_NAME<<std::endl;
881         std::ostringstream os(std::ios::binary);
882         // version
883         writeU8(os, 0);
884         return os.str();
885 }
886
887 InventoryItem* FireflySAO::createPickedUpItem()
888 {
889         std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
890         InventoryItem *item = InventoryItem::deSerialize(is);
891         return item;
892 }
893
894 /*
895         MobV2SAO
896 */
897
898 // Prototype
899 MobV2SAO proto_MobV2SAO(NULL, 0, v3f(0,0,0), NULL);
900
901 MobV2SAO::MobV2SAO(ServerEnvironment *env, u16 id, v3f pos,
902                 Settings *init_properties):
903         ServerActiveObject(env, id, pos),
904         m_move_type("ground_nodes"),
905         m_speed(0,0,0),
906         m_last_sent_position(0,0,0),
907         m_oldpos(0,0,0),
908         m_yaw(0),
909         m_counter1(0),
910         m_counter2(0),
911         m_age(0),
912         m_touching_ground(false),
913         m_hp(10),
914         m_walk_around(false),
915         m_walk_around_timer(0),
916         m_next_pos_exists(false),
917         m_shoot_reload_timer(0),
918         m_shooting(false),
919         m_shooting_timer(0),
920         m_falling(false),
921         m_disturb_timer(100000),
922         m_random_disturb_timer(0),
923         m_shoot_y(0)
924 {
925         ServerActiveObject::registerType(getType(), create);
926         
927         m_properties = new Settings();
928         if(init_properties)
929                 m_properties->update(*init_properties);
930         
931         m_properties->setV3F("pos", pos);
932         
933         setPropertyDefaults();
934         readProperties();
935 }
936         
937 MobV2SAO::~MobV2SAO()
938 {
939         delete m_properties;
940 }
941
942 ServerActiveObject* MobV2SAO::create(ServerEnvironment *env, u16 id, v3f pos,
943                 const std::string &data)
944 {
945         std::istringstream is(data, std::ios::binary);
946         Settings properties;
947         properties.parseConfigLines(is, "MobArgsEnd");
948         MobV2SAO *o = new MobV2SAO(env, id, pos, &properties);
949         return o;
950 }
951
952 std::string MobV2SAO::getStaticData()
953 {
954         updateProperties();
955
956         std::ostringstream os(std::ios::binary);
957         m_properties->writeLines(os);
958         return os.str();
959 }
960
961 std::string MobV2SAO::getClientInitializationData()
962 {
963         //infostream<<__FUNCTION_NAME<<std::endl;
964
965         updateProperties();
966
967         std::ostringstream os(std::ios::binary);
968
969         // version
970         writeU8(os, 0);
971         
972         Settings client_properties;
973         
974         /*client_properties.set("version", "0");
975         client_properties.updateValue(*m_properties, "pos");
976         client_properties.updateValue(*m_properties, "yaw");
977         client_properties.updateValue(*m_properties, "hp");*/
978
979         // Just send everything for simplicity
980         client_properties.update(*m_properties);
981
982         std::ostringstream os2(std::ios::binary);
983         client_properties.writeLines(os2);
984         compressZlib(os2.str(), os);
985
986         return os.str();
987 }
988
989 bool checkFreePosition(Map *map, v3s16 p0, v3s16 size)
990 {
991         for(int dx=0; dx<size.X; dx++)
992         for(int dy=0; dy<size.Y; dy++)
993         for(int dz=0; dz<size.Z; dz++){
994                 v3s16 dp(dx, dy, dz);
995                 v3s16 p = p0 + dp;
996                 MapNode n = map->getNodeNoEx(p);
997                 if(n.getContent() != CONTENT_AIR)
998                         return false;
999         }
1000         return true;
1001 }
1002
1003 bool checkWalkablePosition(Map *map, v3s16 p0)
1004 {
1005         v3s16 p = p0 + v3s16(0,-1,0);
1006         MapNode n = map->getNodeNoEx(p);
1007         if(n.getContent() != CONTENT_AIR)
1008                 return true;
1009         return false;
1010 }
1011
1012 bool checkFreeAndWalkablePosition(Map *map, v3s16 p0, v3s16 size)
1013 {
1014         if(!checkFreePosition(map, p0, size))
1015                 return false;
1016         if(!checkWalkablePosition(map, p0))
1017                 return false;
1018         return true;
1019 }
1020
1021 static void get_random_u32_array(u32 a[], u32 len)
1022 {
1023         u32 i, n;
1024         for(i=0; i<len; i++)
1025                 a[i] = i;
1026         n = len;
1027         while(n > 1){
1028                 u32 k = myrand() % n;
1029                 n--;
1030                 u32 temp = a[n];
1031                 a[n] = a[k];
1032                 a[k] = temp;
1033         }
1034 }
1035
1036 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
1037
1038 static void explodeSquare(Map *map, v3s16 p0, v3s16 size)
1039 {
1040         core::map<v3s16, MapBlock*> modified_blocks;
1041
1042         for(int dx=0; dx<size.X; dx++)
1043         for(int dy=0; dy<size.Y; dy++)
1044         for(int dz=0; dz<size.Z; dz++){
1045                 v3s16 dp(dx - size.X/2, dy - size.Y/2, dz - size.Z/2);
1046                 v3s16 p = p0 + dp;
1047                 MapNode n = map->getNodeNoEx(p);
1048                 if(n.getContent() == CONTENT_IGNORE)
1049                         continue;
1050                 //map->removeNodeWithEvent(p);
1051                 map->removeNodeAndUpdate(p, modified_blocks);
1052         }
1053
1054         // Send a MEET_OTHER event
1055         MapEditEvent event;
1056         event.type = MEET_OTHER;
1057         for(core::map<v3s16, MapBlock*>::Iterator
1058                   i = modified_blocks.getIterator();
1059                   i.atEnd() == false; i++)
1060         {
1061                 v3s16 p = i.getNode()->getKey();
1062                 event.modified_blocks.insert(p, true);
1063         }
1064         map->dispatchEvent(&event);
1065 }
1066
1067 void MobV2SAO::step(float dtime, bool send_recommended)
1068 {
1069         assert(m_env);
1070         Map *map = &m_env->getMap();
1071
1072         m_age += dtime;
1073
1074         if(m_die_age >= 0.0 && m_age >= m_die_age){
1075                 m_removed = true;
1076                 return;
1077         }
1078
1079         m_random_disturb_timer += dtime;
1080         if(m_random_disturb_timer >= 5.0)
1081         {
1082                 m_random_disturb_timer = 0;
1083                 // Check connected players
1084                 core::list<Player*> players = m_env->getPlayers(true);
1085                 core::list<Player*>::Iterator i;
1086                 for(i = players.begin();
1087                                 i != players.end(); i++)
1088                 {
1089                         Player *player = *i;
1090                         v3f playerpos = player->getPosition();
1091                         f32 dist = m_base_position.getDistanceFrom(playerpos);
1092                         if(dist < BS*16)
1093                         {
1094                                 if(myrand_range(0,2) == 0){
1095                                         infostream<<"ACTION: id="<<m_id<<" got randomly disturbed by "
1096                                                         <<player->getName()<<std::endl;
1097                                         m_disturbing_player = player->getName();
1098                                         m_disturb_timer = 0;
1099                                         break;
1100                                 }
1101                         }
1102                 }
1103         }
1104
1105         Player *disturbing_player =
1106                         m_env->getPlayer(m_disturbing_player.c_str());
1107         v3f disturbing_player_off = v3f(0,1,0);
1108         v3f disturbing_player_norm = v3f(0,1,0);
1109         float disturbing_player_distance = 1000000;
1110         float disturbing_player_dir = 0;
1111         if(disturbing_player){
1112                 disturbing_player_off =
1113                                 disturbing_player->getPosition() - m_base_position;
1114                 disturbing_player_distance = disturbing_player_off.getLength();
1115                 disturbing_player_norm = disturbing_player_off;
1116                 disturbing_player_norm.normalize();
1117                 disturbing_player_dir = 180./PI*atan2(disturbing_player_norm.Z,
1118                                 disturbing_player_norm.X);
1119         }
1120
1121         m_disturb_timer += dtime;
1122         
1123         if(!m_falling)
1124         {
1125                 m_shooting_timer -= dtime;
1126                 if(m_shooting_timer <= 0.0 && m_shooting){
1127                         m_shooting = false;
1128                         
1129                         std::string shoot_type = m_properties->get("shoot_type");
1130                         v3f shoot_pos(0,0,0);
1131                         shoot_pos.Y += m_properties->getFloat("shoot_y") * BS;
1132                         if(shoot_type == "fireball"){
1133                                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
1134                                 dir.Y = m_shoot_y;
1135                                 dir.normalize();
1136                                 v3f speed = dir * BS * 10.0;
1137                                 v3f pos = m_base_position + shoot_pos;
1138                                 infostream<<__FUNCTION_NAME<<": Shooting fireball from "<<PP(pos)
1139                                                 <<" at speed "<<PP(speed)<<std::endl;
1140                                 Settings properties;
1141                                 properties.set("looks", "fireball");
1142                                 properties.setV3F("speed", speed);
1143                                 properties.setFloat("die_age", 5.0);
1144                                 properties.set("move_type", "constant_speed");
1145                                 properties.setFloat("hp", 1000);
1146                                 properties.set("lock_full_brightness", "true");
1147                                 properties.set("player_hit_damage", "9");
1148                                 properties.set("player_hit_distance", "2");
1149                                 properties.set("player_hit_interval", "1");
1150                                 ServerActiveObject *obj = new MobV2SAO(m_env, 0,
1151                                                 pos, &properties);
1152                                 //m_env->addActiveObjectAsStatic(obj);
1153                                 m_env->addActiveObject(obj);
1154                         } else {
1155                                 infostream<<__FUNCTION_NAME<<": Unknown shoot_type="<<shoot_type
1156                                                 <<std::endl;
1157                         }
1158                 }
1159
1160                 m_shoot_reload_timer += dtime;
1161
1162                 float reload_time = 15.0;
1163                 if(m_disturb_timer <= 15.0)
1164                         reload_time = 3.0;
1165
1166                 if(!m_shooting && m_shoot_reload_timer >= reload_time &&
1167                                 !m_next_pos_exists)
1168                 {
1169                         m_shoot_y = 0;
1170                         if(m_disturb_timer < 30.0 && disturbing_player &&
1171                                         disturbing_player_distance < 16*BS &&
1172                                         fabs(disturbing_player_norm.Y) < 0.8){
1173                                 m_yaw = disturbing_player_dir;
1174                                 sendPosition();
1175                                 m_shoot_y += disturbing_player_norm.Y;
1176                         }
1177                         m_shoot_reload_timer = 0.0;
1178                         m_shooting = true;
1179                         m_shooting_timer = 1.5;
1180                         {
1181                                 std::ostringstream os(std::ios::binary);
1182                                 // command (2 = shooting)
1183                                 writeU8(os, 2);
1184                                 // time
1185                                 writeF1000(os, m_shooting_timer + 0.1);
1186                                 // bright?
1187                                 writeU8(os, true);
1188                                 // create message and add to list
1189                                 ActiveObjectMessage aom(getId(), false, os.str());
1190                                 m_messages_out.push_back(aom);
1191                         }
1192                 }
1193         }
1194         
1195         if(m_move_type == "ground_nodes")
1196         {
1197                 if(!m_shooting){
1198                         m_walk_around_timer -= dtime;
1199                         if(m_walk_around_timer <= 0.0){
1200                                 m_walk_around = !m_walk_around;
1201                                 if(m_walk_around)
1202                                         m_walk_around_timer = 0.1*myrand_range(10,50);
1203                                 else
1204                                         m_walk_around_timer = 0.1*myrand_range(30,70);
1205                         }
1206                 }
1207
1208                 /* Move */
1209                 if(m_next_pos_exists){
1210                         v3f pos_f = m_base_position;
1211                         v3f next_pos_f = intToFloat(m_next_pos_i, BS);
1212
1213                         v3f v = next_pos_f - pos_f;
1214                         m_yaw = atan2(v.Z, v.X) / PI * 180;
1215                         
1216                         v3f diff = next_pos_f - pos_f;
1217                         v3f dir = diff;
1218                         dir.normalize();
1219                         float speed = BS * 0.5;
1220                         if(m_falling)
1221                                 speed = BS * 3.0;
1222                         dir *= dtime * speed;
1223                         bool arrived = false;
1224                         if(dir.getLength() > diff.getLength()){
1225                                 dir = diff;
1226                                 arrived = true;
1227                         }
1228                         pos_f += dir;
1229                         m_base_position = pos_f;
1230
1231                         if((pos_f - next_pos_f).getLength() < 0.1 || arrived){
1232                                 //infostream<<"id="<<m_id<<": arrived to "<<PP(m_next_pos_i)<<std::endl;
1233                                 m_next_pos_exists = false;
1234                         }
1235                 }
1236
1237                 v3s16 pos_i = floatToInt(m_base_position, BS);
1238                 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1239                 v3s16 pos_size_off(0,0,0);
1240                 if(m_size.X >= 2.5){
1241                         pos_size_off.X = -1;
1242                         pos_size_off.Y = -1;
1243                 }
1244                 
1245                 if(!m_next_pos_exists){
1246                         /* Check whether to drop down */
1247                         if(checkFreePosition(map,
1248                                         pos_i + pos_size_off + v3s16(0,-1,0), size_blocks)){
1249                                 m_next_pos_i = pos_i + v3s16(0,-1,0);
1250                                 m_next_pos_exists = true;
1251                                 m_falling = true;
1252                         } else {
1253                                 m_falling = false;
1254                         }
1255                 }
1256
1257                 if(m_walk_around)
1258                 {
1259                         if(!m_next_pos_exists){
1260                                 /* Find some position where to go next */
1261                                 v3s16 dps[3*3*3];
1262                                 int num_dps = 0;
1263                                 for(int dx=-1; dx<=1; dx++)
1264                                 for(int dy=-1; dy<=1; dy++)
1265                                 for(int dz=-1; dz<=1; dz++){
1266                                         if(dx == 0 && dy == 0)
1267                                                 continue;
1268                                         if(dx != 0 && dz != 0 && dy != 0)
1269                                                 continue;
1270                                         dps[num_dps++] = v3s16(dx,dy,dz);
1271                                 }
1272                                 u32 order[3*3*3];
1273                                 get_random_u32_array(order, num_dps);
1274                                 /*infostream<<"At pos "<<PP(pos_i)<<"; Random array: ";
1275                                 for(int i=0; i<num_dps; i++){
1276                                         infostream<<order[i]<<" ";
1277                                 }
1278                                 infostream<<std::endl;*/
1279                                 for(int i=0; i<num_dps; i++){
1280                                         v3s16 p = dps[order[i]] + pos_i;
1281                                         bool is_free = checkFreeAndWalkablePosition(map,
1282                                                         p + pos_size_off, size_blocks);
1283                                         //infostream<<PP(p)<<" is_free="<<is_free<<std::endl;
1284                                         if(!is_free)
1285                                                 continue;
1286                                         m_next_pos_i = p;
1287                                         m_next_pos_exists = true;
1288                                         break;
1289                                 }
1290                         }
1291                 }
1292         }
1293         else if(m_move_type == "constant_speed")
1294         {
1295                 m_base_position += m_speed * dtime;
1296                 
1297                 v3s16 pos_i = floatToInt(m_base_position, BS);
1298                 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1299                 v3s16 pos_size_off(0,0,0);
1300                 if(m_size.X >= 2.5){
1301                         pos_size_off.X = -1;
1302                         pos_size_off.Y = -1;
1303                 }
1304                 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1305                 if(!free){
1306                         explodeSquare(map, pos_i, v3s16(3,3,3));
1307                         m_removed = true;
1308                         return;
1309                 }
1310         }
1311         else
1312         {
1313                 infostream<<"MobV2SAO::step(): id="<<m_id<<" unknown move_type=\""
1314                                 <<m_move_type<<"\""<<std::endl;
1315         }
1316
1317         if(send_recommended == false)
1318                 return;
1319
1320         if(m_base_position.getDistanceFrom(m_last_sent_position) > 0.05*BS)
1321         {
1322                 sendPosition();
1323         }
1324 }
1325
1326 u16 MobV2SAO::punch(const std::string &toolname, v3f dir,
1327                 const std::string &playername)
1328 {
1329         assert(m_env);
1330         Map *map = &m_env->getMap();
1331         
1332         infostream<<"ACTION: "<<playername<<" punches id="<<m_id
1333                         <<" with a \""<<toolname<<"\" at "
1334                         <<PP(m_base_position/BS)<<std::endl;
1335
1336         m_disturb_timer = 0;
1337         m_disturbing_player = playername;
1338         m_next_pos_exists = false; // Cancel moving immediately
1339         
1340         m_yaw = wrapDegrees_180(180./PI*atan2(dir.Z, dir.X) + 180.);
1341         v3f new_base_position = m_base_position + dir * BS;
1342         {
1343                 v3s16 pos_i = floatToInt(new_base_position, BS);
1344                 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1345                 v3s16 pos_size_off(0,0,0);
1346                 if(m_size.X >= 2.5){
1347                         pos_size_off.X = -1;
1348                         pos_size_off.Y = -1;
1349                 }
1350                 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1351                 if(free)
1352                         m_base_position = new_base_position;
1353         }
1354         sendPosition();
1355         
1356         u16 amount = 2;
1357         /* See tool names in inventory.h */
1358         if(toolname == "WSword")
1359                 amount = 4;
1360         if(toolname == "STSword")
1361                 amount = 6;
1362         if(toolname == "SteelSword")
1363                 amount = 8;
1364         if(toolname == "STAxe")
1365                 amount = 3;
1366         if(toolname == "SteelAxe")
1367                 amount = 4;
1368         if(toolname == "SteelPick")
1369                 amount = 3;
1370         doDamage(amount);
1371         return 65536/100;
1372 }
1373
1374 bool MobV2SAO::isPeaceful()
1375 {
1376         return m_properties->getBool("is_peaceful");
1377 }
1378
1379 void MobV2SAO::sendPosition()
1380 {
1381         m_last_sent_position = m_base_position;
1382
1383         std::ostringstream os(std::ios::binary);
1384         // command (0 = update position)
1385         writeU8(os, 0);
1386         // pos
1387         writeV3F1000(os, m_base_position);
1388         // yaw
1389         writeF1000(os, m_yaw);
1390         // create message and add to list
1391         ActiveObjectMessage aom(getId(), false, os.str());
1392         m_messages_out.push_back(aom);
1393 }
1394
1395 void MobV2SAO::setPropertyDefaults()
1396 {
1397         m_properties->setDefault("is_peaceful", "false");
1398         m_properties->setDefault("move_type", "ground_nodes");
1399         m_properties->setDefault("speed", "(0,0,0)");
1400         m_properties->setDefault("age", "0");
1401         m_properties->setDefault("yaw", "0");
1402         m_properties->setDefault("pos", "(0,0,0)");
1403         m_properties->setDefault("hp", "0");
1404         m_properties->setDefault("die_age", "-1");
1405         m_properties->setDefault("size", "(1,2)");
1406         m_properties->setDefault("shoot_type", "fireball");
1407         m_properties->setDefault("shoot_y", "0");
1408 }
1409 void MobV2SAO::readProperties()
1410 {
1411         m_move_type = m_properties->get("move_type");
1412         m_speed = m_properties->getV3F("speed");
1413         m_age = m_properties->getFloat("age");
1414         m_yaw = m_properties->getFloat("yaw");
1415         m_base_position = m_properties->getV3F("pos");
1416         m_hp = m_properties->getS32("hp");
1417         m_die_age = m_properties->getFloat("die_age");
1418         m_size = m_properties->getV2F("size");
1419 }
1420 void MobV2SAO::updateProperties()
1421 {
1422         m_properties->set("move_type", m_move_type);
1423         m_properties->setV3F("speed", m_speed);
1424         m_properties->setFloat("age", m_age);
1425         m_properties->setFloat("yaw", m_yaw);
1426         m_properties->setV3F("pos", m_base_position);
1427         m_properties->setS32("hp", m_hp);
1428         m_properties->setFloat("die_age", m_die_age);
1429         m_properties->setV2F("size", m_size);
1430
1431         m_properties->setS32("version", 0);
1432 }
1433
1434 void MobV2SAO::doDamage(u16 d)
1435 {
1436         infostream<<"MobV2 hp="<<m_hp<<" damage="<<d<<std::endl;
1437         
1438         if(d < m_hp)
1439         {
1440                 m_hp -= d;
1441         }
1442         else
1443         {
1444                 actionstream<<"A "<<(isPeaceful()?"peaceful":"non-peaceful")
1445                                 <<" mob id="<<m_id<<" dies at "<<PP(m_base_position)<<std::endl;
1446                 // Die
1447                 m_hp = 0;
1448                 m_removed = true;
1449         }
1450
1451         {
1452                 std::ostringstream os(std::ios::binary);
1453                 // command (1 = damage)
1454                 writeU8(os, 1);
1455                 // amount
1456                 writeU16(os, d);
1457                 // create message and add to list
1458                 ActiveObjectMessage aom(getId(), false, os.str());
1459                 m_messages_out.push_back(aom);
1460         }
1461 }
1462
1463