]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_sao.cpp
d81e3f87135ad779c8d9b61daa94e2ab45ee4fd4
[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                 //dstream<<"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         dstream<<"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         dstream<<__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                 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
226                                 <<m_inventorystring<<"\" -> item="<<item
227                                 <<std::endl;
228                 return item;
229         }
230         catch(SerializationError &e)
231         {
232                 dstream<<__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         dstream<<__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         //dstream<<__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*1.45)
527                 {
528                         player_is_too_close = true;
529                         near_player_pos = playerpos;
530                         break;
531                 }
532                 else if(dist < BS*15.0)
533                 {
534                         player_is_close = true;
535                         near_player_pos = playerpos;
536                 }
537         }
538
539         m_is_active = player_is_close;
540
541         v3f target_speed = m_speed_f;
542
543         if(!player_is_close)
544         {
545                 target_speed = v3f(0,0,0);
546         }
547         else
548         {
549                 // Move around
550
551                 v3f ndir = near_player_pos - m_base_position;
552                 ndir.Y = 0;
553                 ndir.normalize();
554
555                 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
556                 if(nyaw < m_yaw - 180)
557                         nyaw += 360;
558                 else if(nyaw > m_yaw + 180)
559                         nyaw -= 360;
560                 m_yaw = 0.95*m_yaw + 0.05*nyaw;
561                 m_yaw = wrapDegrees(m_yaw);
562                 
563                 f32 speed = 2*BS;
564
565                 if((m_touching_ground || m_after_jump_timer > 0.0)
566                                 && !player_is_too_close)
567                 {
568                         v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
569                         target_speed.X = speed * dir.X;
570                         target_speed.Z = speed * dir.Z;
571                 }
572
573                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
574                                 < dtime*speed/2)
575                 {
576                         m_counter1 -= dtime;
577                         if(m_counter1 < 0.0)
578                         {
579                                 m_counter1 += 0.2;
580                                 // Jump
581                                 target_speed.Y = 5.0*BS;
582                                 m_after_jump_timer = 1.0;
583                         }
584                 }
585
586                 {
587                         m_counter2 -= dtime;
588                         if(m_counter2 < 0.0)
589                         {
590                                 m_counter2 += (float)(myrand()%100)/100*3.0;
591                                 //m_yaw += ((float)(myrand()%200)-100)/100*180;
592                                 m_yaw += ((float)(myrand()%200)-100)/100*90;
593                                 m_yaw = wrapDegrees(m_yaw);
594                         }
595                 }
596         }
597         
598         if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
599                 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
600         else
601                 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
602         
603         m_oldpos = m_base_position;
604
605         /*
606                 Move it, with collision detection
607         */
608
609         core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
610         collisionMoveResult moveresult;
611         // Maximum movement without glitches
612         f32 pos_max_d = BS*0.25;
613         /*// Limit speed
614         if(m_speed_f.getLength()*dtime > pos_max_d)
615                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
616         v3f pos_f = getBasePosition();
617         v3f pos_f_old = pos_f;
618         moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
619                         box, dtime, pos_f, m_speed_f);
620         m_touching_ground = moveresult.touching_ground;
621         
622         // Do collision damage
623         float tolerance = BS*30;
624         float factor = BS*0.5;
625         v3f speed_diff = old_speed - m_speed_f;
626         // Increase effect in X and Z
627         speed_diff.X *= 2;
628         speed_diff.Z *= 2;
629         float vel = speed_diff.getLength();
630         if(vel > tolerance)
631         {
632                 f32 damage_f = (vel - tolerance)/BS*factor;
633                 u16 damage = (u16)(damage_f+0.5);
634                 doDamage(damage);
635         }
636
637         setBasePosition(pos_f);
638
639         if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
640                 return;
641
642         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
643         {
644                 m_last_sent_position = pos_f;
645
646                 std::ostringstream os(std::ios::binary);
647                 // command (0 = update position)
648                 writeU8(os, 0);
649                 // pos
650                 writeV3F1000(os, m_base_position);
651                 // yaw
652                 writeF1000(os, m_yaw);
653                 // create message and add to list
654                 ActiveObjectMessage aom(getId(), false, os.str());
655                 m_messages_out.push_back(aom);
656         }
657 }
658
659 std::string Oerkki1SAO::getClientInitializationData()
660 {
661         std::ostringstream os(std::ios::binary);
662         // version
663         writeU8(os, 0);
664         // pos
665         writeV3F1000(os, m_base_position);
666         return os.str();
667 }
668
669 std::string Oerkki1SAO::getStaticData()
670 {
671         //dstream<<__FUNCTION_NAME<<std::endl;
672         std::ostringstream os(std::ios::binary);
673         // version
674         writeU8(os, 0);
675         // hp
676         writeU8(os, m_hp);
677         return os.str();
678 }
679
680 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
681 {
682         m_speed_f += dir*12*BS;
683
684         u16 amount = 5;
685         doDamage(amount);
686         return 65536/100;
687 }
688
689 void Oerkki1SAO::doDamage(u16 d)
690 {
691         dstream<<"oerkki damage: "<<d<<std::endl;
692         
693         if(d < m_hp)
694         {
695                 m_hp -= d;
696         }
697         else
698         {
699                 // Die
700                 m_hp = 0;
701                 m_removed = true;
702         }
703
704         {
705                 std::ostringstream os(std::ios::binary);
706                 // command (1 = damage)
707                 writeU8(os, 1);
708                 // amount
709                 writeU8(os, d);
710                 // create message and add to list
711                 ActiveObjectMessage aom(getId(), false, os.str());
712                 m_messages_out.push_back(aom);
713         }
714 }
715
716 /*
717         FireflySAO
718 */
719
720 // Prototype
721 FireflySAO proto_FireflySAO(NULL, 0, v3f(0,0,0));
722
723 FireflySAO::FireflySAO(ServerEnvironment *env, u16 id, v3f pos):
724         ServerActiveObject(env, id, pos),
725         m_is_active(false),
726         m_speed_f(0,0,0)
727 {
728         ServerActiveObject::registerType(getType(), create);
729
730         m_oldpos = v3f(0,0,0);
731         m_last_sent_position = v3f(0,0,0);
732         m_yaw = 0;
733         m_counter1 = 0;
734         m_counter2 = 0;
735         m_age = 0;
736         m_touching_ground = false;
737 }
738
739 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, u16 id, v3f pos,
740                 const std::string &data)
741 {
742         std::istringstream is(data, std::ios::binary);
743         char buf[1];
744         // read version
745         is.read(buf, 1);
746         u8 version = buf[0];
747         // check if version is supported
748         if(version != 0)
749                 return NULL;
750         return new FireflySAO(env, id, pos);
751 }
752
753 void FireflySAO::step(float dtime, bool send_recommended)
754 {
755         assert(m_env);
756
757         if(m_is_active == false)
758         {
759                 if(m_inactive_interval.step(dtime, 0.5)==false)
760                         return;
761         }
762
763         /*
764                 The AI
765         */
766
767         // Apply (less) gravity
768         m_speed_f.Y -= dtime*3*BS;
769
770         /*
771                 Move around if some player is close
772         */
773         bool player_is_close = false;
774         // Check connected players
775         core::list<Player*> players = m_env->getPlayers(true);
776         core::list<Player*>::Iterator i;
777         for(i = players.begin();
778                         i != players.end(); i++)
779         {
780                 Player *player = *i;
781                 v3f playerpos = player->getPosition();
782                 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
783                 {
784                         player_is_close = true;
785                         break;
786                 }
787         }
788
789         m_is_active = player_is_close;
790         
791         if(player_is_close == false)
792         {
793                 m_speed_f.X = 0;
794                 m_speed_f.Z = 0;
795         }
796         else
797         {
798                 // Move around
799                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
800                 f32 speed = BS/2;
801                 m_speed_f.X = speed * dir.X;
802                 m_speed_f.Z = speed * dir.Z;
803
804                 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
805                                 < dtime*speed/2)
806                 {
807                         m_counter1 -= dtime;
808                         if(m_counter1 < 0.0)
809                         {
810                                 m_counter1 += 1.0;
811                                 m_speed_f.Y = 5.0*BS;
812                         }
813                 }
814
815                 {
816                         m_counter2 -= dtime;
817                         if(m_counter2 < 0.0)
818                         {
819                                 m_counter2 += (float)(myrand()%100)/100*3.0;
820                                 m_yaw += ((float)(myrand()%200)-100)/100*180;
821                                 m_yaw = wrapDegrees(m_yaw);
822                         }
823                 }
824         }
825         
826         m_oldpos = m_base_position;
827
828         /*
829                 Move it, with collision detection
830         */
831
832         core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
833         collisionMoveResult moveresult;
834         // Maximum movement without glitches
835         f32 pos_max_d = BS*0.25;
836         // Limit speed
837         if(m_speed_f.getLength()*dtime > pos_max_d)
838                 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
839         v3f pos_f = getBasePosition();
840         v3f pos_f_old = pos_f;
841         moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
842                         box, dtime, pos_f, m_speed_f);
843         m_touching_ground = moveresult.touching_ground;
844         
845         setBasePosition(pos_f);
846
847         if(send_recommended == false)
848                 return;
849
850         if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
851         {
852                 m_last_sent_position = pos_f;
853
854                 std::ostringstream os(std::ios::binary);
855                 // command (0 = update position)
856                 writeU8(os, 0);
857                 // pos
858                 writeV3F1000(os, m_base_position);
859                 // yaw
860                 writeF1000(os, m_yaw);
861                 // create message and add to list
862                 ActiveObjectMessage aom(getId(), false, os.str());
863                 m_messages_out.push_back(aom);
864         }
865 }
866
867 std::string FireflySAO::getClientInitializationData()
868 {
869         std::ostringstream os(std::ios::binary);
870         // version
871         writeU8(os, 0);
872         // pos
873         writeV3F1000(os, m_base_position);
874         return os.str();
875 }
876
877 std::string FireflySAO::getStaticData()
878 {
879         //dstream<<__FUNCTION_NAME<<std::endl;
880         std::ostringstream os(std::ios::binary);
881         // version
882         writeU8(os, 0);
883         return os.str();
884 }
885
886 InventoryItem* FireflySAO::createPickedUpItem()
887 {
888         std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
889         InventoryItem *item = InventoryItem::deSerialize(is);
890         return item;
891 }
892
893 /*
894         MobV2SAO
895 */
896
897 // Prototype
898 MobV2SAO proto_MobV2SAO(NULL, 0, v3f(0,0,0), NULL);
899
900 MobV2SAO::MobV2SAO(ServerEnvironment *env, u16 id, v3f pos,
901                 Settings *init_properties):
902         ServerActiveObject(env, id, pos),
903         m_move_type("ground_nodes"),
904         m_speed(0,0,0),
905         m_last_sent_position(0,0,0),
906         m_oldpos(0,0,0),
907         m_yaw(0),
908         m_counter1(0),
909         m_counter2(0),
910         m_age(0),
911         m_touching_ground(false),
912         m_hp(10),
913         m_walk_around(false),
914         m_walk_around_timer(0),
915         m_next_pos_exists(false),
916         m_shoot_reload_timer(0),
917         m_shooting(false),
918         m_shooting_timer(0),
919         m_falling(false),
920         m_disturb_timer(100000)
921 {
922         ServerActiveObject::registerType(getType(), create);
923         
924         m_properties = new Settings();
925         if(init_properties)
926                 m_properties->update(*init_properties);
927         
928         m_properties->setV3F("pos", pos);
929         
930         setPropertyDefaults();
931         readProperties();
932 }
933         
934 MobV2SAO::~MobV2SAO()
935 {
936         delete m_properties;
937 }
938
939 ServerActiveObject* MobV2SAO::create(ServerEnvironment *env, u16 id, v3f pos,
940                 const std::string &data)
941 {
942         std::istringstream is(data, std::ios::binary);
943         Settings properties;
944         properties.parseConfigLines(is, "MobArgsEnd");
945         MobV2SAO *o = new MobV2SAO(env, id, pos, &properties);
946         return o;
947 }
948
949 std::string MobV2SAO::getStaticData()
950 {
951         updateProperties();
952
953         std::ostringstream os(std::ios::binary);
954         m_properties->writeLines(os);
955         return os.str();
956 }
957
958 std::string MobV2SAO::getClientInitializationData()
959 {
960         //dstream<<__FUNCTION_NAME<<std::endl;
961
962         updateProperties();
963
964         std::ostringstream os(std::ios::binary);
965
966         // version
967         writeU8(os, 0);
968         
969         Settings client_properties;
970         
971         /*client_properties.set("version", "0");
972         client_properties.updateValue(*m_properties, "pos");
973         client_properties.updateValue(*m_properties, "yaw");
974         client_properties.updateValue(*m_properties, "hp");*/
975
976         // Just send everything for simplicity
977         client_properties.update(*m_properties);
978
979         std::ostringstream os2(std::ios::binary);
980         client_properties.writeLines(os2);
981         compressZlib(os2.str(), os);
982
983         return os.str();
984 }
985
986 bool checkFreePosition(Map *map, v3s16 p0, v3s16 size)
987 {
988         for(int dx=0; dx<size.X; dx++)
989         for(int dy=0; dy<size.Y; dy++)
990         for(int dz=0; dz<size.Z; dz++){
991                 v3s16 dp(dx, dy, dz);
992                 v3s16 p = p0 + dp;
993                 MapNode n = map->getNodeNoEx(p);
994                 if(n.getContent() != CONTENT_AIR)
995                         return false;
996         }
997         return true;
998 }
999
1000 bool checkWalkablePosition(Map *map, v3s16 p0)
1001 {
1002         v3s16 p = p0 + v3s16(0,-1,0);
1003         MapNode n = map->getNodeNoEx(p);
1004         if(n.getContent() != CONTENT_AIR)
1005                 return true;
1006         return false;
1007 }
1008
1009 bool checkFreeAndWalkablePosition(Map *map, v3s16 p0, v3s16 size)
1010 {
1011         if(!checkFreePosition(map, p0, size))
1012                 return false;
1013         if(!checkWalkablePosition(map, p0))
1014                 return false;
1015         return true;
1016 }
1017
1018 static void get_random_u32_array(u32 a[], u32 len)
1019 {
1020         u32 i, n;
1021         for(i=0; i<len; i++)
1022                 a[i] = i;
1023         n = len;
1024         while(n > 1){
1025                 u32 k = myrand() % n;
1026                 n--;
1027                 u32 temp = a[n];
1028                 a[n] = a[k];
1029                 a[k] = temp;
1030         }
1031 }
1032
1033 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
1034
1035 static void explodeSquare(Map *map, v3s16 p0, v3s16 size)
1036 {
1037         core::map<v3s16, MapBlock*> modified_blocks;
1038
1039         for(int dx=0; dx<size.X; dx++)
1040         for(int dy=0; dy<size.Y; dy++)
1041         for(int dz=0; dz<size.Z; dz++){
1042                 v3s16 dp(dx - size.X/2, dy - size.Y/2, dz - size.Z/2);
1043                 v3s16 p = p0 + dp;
1044                 MapNode n = map->getNodeNoEx(p);
1045                 if(n.getContent() == CONTENT_IGNORE)
1046                         continue;
1047                 //map->removeNodeWithEvent(p);
1048                 map->removeNodeAndUpdate(p, modified_blocks);
1049         }
1050
1051         // Send a MEET_OTHER event
1052         MapEditEvent event;
1053         event.type = MEET_OTHER;
1054         for(core::map<v3s16, MapBlock*>::Iterator
1055                   i = modified_blocks.getIterator();
1056                   i.atEnd() == false; i++)
1057         {
1058                 v3s16 p = i.getNode()->getKey();
1059                 event.modified_blocks.insert(p, true);
1060         }
1061         map->dispatchEvent(&event);
1062 }
1063
1064 void MobV2SAO::step(float dtime, bool send_recommended)
1065 {
1066         assert(m_env);
1067         Map *map = &m_env->getMap();
1068
1069         m_age += dtime;
1070
1071         if(m_die_age >= 0.0 && m_age >= m_die_age){
1072                 m_removed = true;
1073                 return;
1074         }
1075
1076         Player *disturbing_player =
1077                         m_env->getPlayer(m_disturbing_player.c_str());
1078         v3f disturbing_player_off = v3f(0,1,0);
1079         float disturbing_player_distance = 1000000;
1080         float disturbing_player_dir = 0;
1081         if(disturbing_player){
1082                 disturbing_player_off =
1083                                 disturbing_player->getPosition() - m_base_position;
1084                 disturbing_player_distance = disturbing_player_off.getLength();
1085                 disturbing_player_off.normalize();
1086                 disturbing_player_dir = 180./M_PI*atan2(disturbing_player_off.Z,
1087                                 disturbing_player_off.X);
1088         }
1089
1090         m_disturb_timer += dtime;
1091         
1092         if(!m_falling)
1093         {
1094                 m_shooting_timer -= dtime;
1095                 if(m_shooting_timer <= 0.0 && m_shooting){
1096                         m_shooting = false;
1097                         
1098                         std::string shoot_type = m_properties->get("shoot_type");
1099                         v3f shoot_pos(0,0,0);
1100                         shoot_pos.Y += m_properties->getFloat("shoot_y") * BS;
1101                         if(shoot_type == "fireball"){
1102                                 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
1103                                 v3f speed = dir * BS * 10.0;
1104                                 v3f pos = m_base_position + shoot_pos;
1105                                 dstream<<__FUNCTION_NAME<<": Shooting fireball from "<<PP(pos)
1106                                                 <<" at speed "<<PP(speed)<<std::endl;
1107                                 Settings properties;
1108                                 properties.set("looks", "fireball");
1109                                 properties.setV3F("speed", speed);
1110                                 properties.setFloat("die_age", 5.0);
1111                                 properties.set("move_type", "constant_speed");
1112                                 properties.setFloat("hp", 1000);
1113                                 properties.set("lock_full_brightness", "true");
1114                                 properties.set("player_hit_damage", "9");
1115                                 properties.set("player_hit_distance", "2");
1116                                 properties.set("player_hit_interval", "1");
1117                                 ServerActiveObject *obj = new MobV2SAO(m_env, 0,
1118                                                 pos, &properties);
1119                                 //m_env->addActiveObjectAsStatic(obj);
1120                                 m_env->addActiveObject(obj);
1121                         } else {
1122                                 dstream<<__FUNCTION_NAME<<": Unknown shoot_type="<<shoot_type
1123                                                 <<std::endl;
1124                         }
1125                 }
1126
1127                 m_shoot_reload_timer += dtime;
1128
1129                 float reload_time = 15.0;
1130                 if(m_disturb_timer <= 15.0)
1131                         reload_time = 3.0;
1132
1133                 if(m_shoot_reload_timer >= reload_time && !m_next_pos_exists)
1134                 {
1135                         if(m_disturb_timer < 30.0 && disturbing_player &&
1136                                         disturbing_player_distance < 16*BS &&
1137                                         fabs(disturbing_player_off.Y) < 5*BS){
1138                                 m_yaw = disturbing_player_dir;
1139                                 sendPosition();
1140                         }
1141                         m_shoot_reload_timer = 0.0;
1142                         m_shooting = true;
1143                         m_shooting_timer = 1.5;
1144                         {
1145                                 std::ostringstream os(std::ios::binary);
1146                                 // command (2 = shooting)
1147                                 writeU8(os, 2);
1148                                 // time
1149                                 writeF1000(os, m_shooting_timer + 0.1);
1150                                 // bright?
1151                                 writeU8(os, true);
1152                                 // create message and add to list
1153                                 ActiveObjectMessage aom(getId(), false, os.str());
1154                                 m_messages_out.push_back(aom);
1155                         }
1156                 }
1157         }
1158         
1159         if(m_move_type == "ground_nodes")
1160         {
1161                 if(!m_shooting){
1162                         m_walk_around_timer -= dtime;
1163                         if(m_walk_around_timer <= 0.0){
1164                                 m_walk_around = !m_walk_around;
1165                                 if(m_walk_around)
1166                                         m_walk_around_timer = 0.1*myrand_range(10,50);
1167                                 else
1168                                         m_walk_around_timer = 0.1*myrand_range(30,70);
1169                         }
1170                 }
1171
1172                 /* Move */
1173                 if(m_next_pos_exists){
1174                         v3f pos_f = m_base_position;
1175                         v3f next_pos_f = intToFloat(m_next_pos_i, BS);
1176
1177                         v3f v = next_pos_f - pos_f;
1178                         m_yaw = atan2(v.Z, v.X) / M_PI * 180;
1179                         
1180                         v3f diff = next_pos_f - pos_f;
1181                         v3f dir = diff;
1182                         dir.normalize();
1183                         float speed = BS * 0.5;
1184                         if(m_falling)
1185                                 speed = BS * 3.0;
1186                         dir *= dtime * speed;
1187                         bool arrived = false;
1188                         if(dir.getLength() > diff.getLength()){
1189                                 dir = diff;
1190                                 arrived = true;
1191                         }
1192                         pos_f += dir;
1193                         m_base_position = pos_f;
1194
1195                         if((pos_f - next_pos_f).getLength() < 0.1 || arrived){
1196                                 //dstream<<"id="<<m_id<<": arrived to "<<PP(m_next_pos_i)<<std::endl;
1197                                 m_next_pos_exists = false;
1198                         }
1199                 }
1200
1201                 v3s16 pos_i = floatToInt(m_base_position, BS);
1202                 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1203                 v3s16 pos_size_off(0,0,0);
1204                 if(m_size.X >= 2.5){
1205                         pos_size_off.X = -1;
1206                         pos_size_off.Y = -1;
1207                 }
1208                 
1209                 if(!m_next_pos_exists){
1210                         /* Check whether to drop down */
1211                         if(checkFreePosition(map,
1212                                         pos_i + pos_size_off + v3s16(0,-1,0), size_blocks)){
1213                                 m_next_pos_i = pos_i + v3s16(0,-1,0);
1214                                 m_next_pos_exists = true;
1215                                 m_falling = true;
1216                         } else {
1217                                 m_falling = false;
1218                         }
1219                 }
1220
1221                 if(m_walk_around)
1222                 {
1223                         if(!m_next_pos_exists){
1224                                 /* Find some position where to go next */
1225                                 v3s16 dps[3*3*3];
1226                                 int num_dps = 0;
1227                                 for(int dx=-1; dx<=1; dx++)
1228                                 for(int dy=-1; dy<=1; dy++)
1229                                 for(int dz=-1; dz<=1; dz++){
1230                                         if(dx == 0 && dy == 0)
1231                                                 continue;
1232                                         if(dx != 0 && dz != 0 && dy != 0)
1233                                                 continue;
1234                                         dps[num_dps++] = v3s16(dx,dy,dz);
1235                                 }
1236                                 u32 order[3*3*3];
1237                                 get_random_u32_array(order, num_dps);
1238                                 /*dstream<<"At pos "<<PP(pos_i)<<"; Random array: ";
1239                                 for(int i=0; i<num_dps; i++){
1240                                         dstream<<order[i]<<" ";
1241                                 }
1242                                 dstream<<std::endl;*/
1243                                 for(int i=0; i<num_dps; i++){
1244                                         v3s16 p = dps[order[i]] + pos_i;
1245                                         bool is_free = checkFreeAndWalkablePosition(map,
1246                                                         p + pos_size_off, size_blocks);
1247                                         //dstream<<PP(p)<<" is_free="<<is_free<<std::endl;
1248                                         if(!is_free)
1249                                                 continue;
1250                                         m_next_pos_i = p;
1251                                         m_next_pos_exists = true;
1252                                         break;
1253                                 }
1254                         }
1255                 }
1256         }
1257         else if(m_move_type == "constant_speed")
1258         {
1259                 m_base_position += m_speed * dtime;
1260                 
1261                 v3s16 pos_i = floatToInt(m_base_position, BS);
1262                 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1263                 v3s16 pos_size_off(0,0,0);
1264                 if(m_size.X >= 2.5){
1265                         pos_size_off.X = -1;
1266                         pos_size_off.Y = -1;
1267                 }
1268                 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1269                 if(!free){
1270                         explodeSquare(map, pos_i, v3s16(3,3,3));
1271                         m_removed = true;
1272                         return;
1273                 }
1274         }
1275         else
1276         {
1277                 dstream<<"MobV2SAO::step(): id="<<m_id<<" unknown move_type=\""
1278                                 <<m_move_type<<"\""<<std::endl;
1279         }
1280
1281         if(send_recommended == false)
1282                 return;
1283
1284         if(m_base_position.getDistanceFrom(m_last_sent_position) > 0.05*BS)
1285         {
1286                 sendPosition();
1287         }
1288 }
1289
1290 u16 MobV2SAO::punch(const std::string &toolname, v3f dir,
1291                 const std::string &playername)
1292 {
1293         assert(m_env);
1294         Map *map = &m_env->getMap();
1295
1296         m_disturb_timer = 0;
1297         m_disturbing_player = playername;
1298         
1299         m_yaw = wrapDegrees_180(180./M_PI*atan2(dir.Z, dir.X) + 180.);
1300         v3f new_base_position = m_base_position + dir * BS;
1301         {
1302                 v3s16 pos_i = floatToInt(new_base_position, BS);
1303                 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1304                 v3s16 pos_size_off(0,0,0);
1305                 if(m_size.X >= 2.5){
1306                         pos_size_off.X = -1;
1307                         pos_size_off.Y = -1;
1308                 }
1309                 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1310                 if(free)
1311                         m_base_position = new_base_position;
1312         }
1313         sendPosition();
1314         
1315         u16 amount = 2;
1316         dstream<<"id="<<m_id<<": punch with \""<<toolname<<"\""<<std::endl;
1317         /* See tool names in inventory.h */
1318         if(toolname == "WSword")
1319                 amount = 4;
1320         if(toolname == "STSword")
1321                 amount = 7;
1322         if(toolname == "SteelSword")
1323                 amount = 10;
1324         if(toolname == "STAxe")
1325                 amount = 3;
1326         if(toolname == "SteelAxe")
1327                 amount = 4;
1328         if(toolname == "SteelPick")
1329                 amount = 3;
1330         doDamage(amount);
1331         return 65536/100;
1332 }
1333
1334 void MobV2SAO::sendPosition()
1335 {
1336         m_last_sent_position = m_base_position;
1337
1338         std::ostringstream os(std::ios::binary);
1339         // command (0 = update position)
1340         writeU8(os, 0);
1341         // pos
1342         writeV3F1000(os, m_base_position);
1343         // yaw
1344         writeF1000(os, m_yaw);
1345         // create message and add to list
1346         ActiveObjectMessage aom(getId(), false, os.str());
1347         m_messages_out.push_back(aom);
1348 }
1349
1350 void MobV2SAO::setPropertyDefaults()
1351 {
1352         m_properties->setDefault("move_type", "ground_nodes");
1353         m_properties->setDefault("speed", "(0,0,0)");
1354         m_properties->setDefault("age", "0");
1355         m_properties->setDefault("yaw", "0");
1356         m_properties->setDefault("pos", "(0,0,0)");
1357         m_properties->setDefault("hp", "0");
1358         m_properties->setDefault("die_age", "-1");
1359         m_properties->setDefault("size", "(1,2)");
1360         m_properties->setDefault("shoot_type", "fireball");
1361         m_properties->setDefault("shoot_y", "0");
1362 }
1363 void MobV2SAO::readProperties()
1364 {
1365         m_move_type = m_properties->get("move_type");
1366         m_speed = m_properties->getV3F("speed");
1367         m_age = m_properties->getFloat("age");
1368         m_yaw = m_properties->getFloat("yaw");
1369         m_base_position = m_properties->getV3F("pos");
1370         m_hp = m_properties->getS32("hp");
1371         m_die_age = m_properties->getFloat("die_age");
1372         m_size = m_properties->getV2F("size");
1373 }
1374 void MobV2SAO::updateProperties()
1375 {
1376         m_properties->set("move_type", m_move_type);
1377         m_properties->setV3F("speed", m_speed);
1378         m_properties->setFloat("age", m_age);
1379         m_properties->setFloat("yaw", m_yaw);
1380         m_properties->setV3F("pos", m_base_position);
1381         m_properties->setS32("hp", m_hp);
1382         m_properties->setFloat("die_age", m_die_age);
1383         m_properties->setV2F("size", m_size);
1384
1385         m_properties->setS32("version", 0);
1386 }
1387
1388 void MobV2SAO::doDamage(u16 d)
1389 {
1390         dstream<<"MobV2 hp="<<m_hp<<" damage="<<d<<std::endl;
1391         
1392         if(d < m_hp)
1393         {
1394                 m_hp -= d;
1395         }
1396         else
1397         {
1398                 // Die
1399                 m_hp = 0;
1400                 m_removed = true;
1401         }
1402
1403         {
1404                 std::ostringstream os(std::ios::binary);
1405                 // command (1 = damage)
1406                 writeU8(os, 1);
1407                 // amount
1408                 writeU16(os, d);
1409                 // create message and add to list
1410                 ActiveObjectMessage aom(getId(), false, os.str());
1411                 m_messages_out.push_back(aom);
1412         }
1413 }
1414
1415