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