bde0dd320889a3754650c4e4bbd1de42ab6d932a
[oweals/minetest.git] / src / content_cao.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 <ICameraSceneNode.h>
21 #include <ITextSceneNode.h>
22 #include <IBillboardSceneNode.h>
23 #include <IMeshManipulator.h>
24 #include <IAnimatedMeshSceneNode.h>
25 #include <IBoneSceneNode.h>
26 #include "content_cao.h"
27 #include "util/numeric.h" // For IntervalLimiter
28 #include "util/serialize.h"
29 #include "util/mathconstants.h"
30 #include "client/tile.h"
31 #include "environment.h"
32 #include "collision.h"
33 #include "settings.h"
34 #include "serialization.h" // For decompressZlib
35 #include "gamedef.h"
36 #include "clientobject.h"
37 #include "mesh.h"
38 #include "itemdef.h"
39 #include "tool.h"
40 #include "content_cso.h"
41 #include "sound.h"
42 #include "nodedef.h"
43 #include "localplayer.h"
44 #include "map.h"
45 #include "camera.h" // CameraModes
46 #include "wieldmesh.h"
47 #include "log.h"
48
49 class Settings;
50 struct ToolCapabilities;
51
52 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
53
54 std::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
55
56 SmoothTranslator::SmoothTranslator():
57         vect_old(0,0,0),
58         vect_show(0,0,0),
59         vect_aim(0,0,0),
60         anim_counter(0),
61         anim_time(0),
62         anim_time_counter(0),
63         aim_is_end(true)
64 {}
65
66 void SmoothTranslator::init(v3f vect)
67 {
68         vect_old = vect;
69         vect_show = vect;
70         vect_aim = vect;
71         anim_counter = 0;
72         anim_time = 0;
73         anim_time_counter = 0;
74         aim_is_end = true;
75 }
76
77 void SmoothTranslator::sharpen()
78 {
79         init(vect_show);
80 }
81
82 void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
83 {
84         aim_is_end = is_end_position;
85         vect_old = vect_show;
86         vect_aim = vect_new;
87         if(update_interval > 0)
88         {
89                 anim_time = update_interval;
90         } else {
91                 if(anim_time < 0.001 || anim_time > 1.0)
92                         anim_time = anim_time_counter;
93                 else
94                         anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
95         }
96         anim_time_counter = 0;
97         anim_counter = 0;
98 }
99
100 void SmoothTranslator::translate(f32 dtime)
101 {
102         anim_time_counter = anim_time_counter + dtime;
103         anim_counter = anim_counter + dtime;
104         v3f vect_move = vect_aim - vect_old;
105         f32 moveratio = 1.0;
106         if(anim_time > 0.001)
107                 moveratio = anim_time_counter / anim_time;
108         // Move a bit less than should, to avoid oscillation
109         moveratio = moveratio * 0.8;
110         float move_end = 1.5;
111         if(aim_is_end)
112                 move_end = 1.0;
113         if(moveratio > move_end)
114                 moveratio = move_end;
115         vect_show = vect_old + vect_move * moveratio;
116 }
117
118 bool SmoothTranslator::is_moving()
119 {
120         return ((anim_time_counter / anim_time) < 1.4);
121 }
122
123 /*
124         Other stuff
125 */
126
127 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
128                 float txs, float tys, int col, int row)
129 {
130         video::SMaterial& material = bill->getMaterial(0);
131         core::matrix4& matrix = material.getTextureMatrix(0);
132         matrix.setTextureTranslate(txs*col, tys*row);
133         matrix.setTextureScale(txs, tys);
134 }
135
136 /*
137         TestCAO
138 */
139
140 class TestCAO : public ClientActiveObject
141 {
142 public:
143         TestCAO(IGameDef *gamedef, ClientEnvironment *env);
144         virtual ~TestCAO();
145
146         ActiveObjectType getType() const
147         {
148                 return ACTIVEOBJECT_TYPE_TEST;
149         }
150
151         static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
152
153         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
154                         IrrlichtDevice *irr);
155         void removeFromScene(bool permanent);
156         void updateLight(u8 light_at_pos);
157         v3s16 getLightPosition();
158         void updateNodePos();
159
160         void step(float dtime, ClientEnvironment *env);
161
162         void processMessage(const std::string &data);
163
164         bool getCollisionBox(aabb3f *toset) { return false; }
165 private:
166         scene::IMeshSceneNode *m_node;
167         v3f m_position;
168 };
169
170 // Prototype
171 TestCAO proto_TestCAO(NULL, NULL);
172
173 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
174         ClientActiveObject(0, gamedef, env),
175         m_node(NULL),
176         m_position(v3f(0,10*BS,0))
177 {
178         ClientActiveObject::registerType(getType(), create);
179 }
180
181 TestCAO::~TestCAO()
182 {
183 }
184
185 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
186 {
187         return new TestCAO(gamedef, env);
188 }
189
190 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
191                         IrrlichtDevice *irr)
192 {
193         if(m_node != NULL)
194                 return;
195
196         //video::IVideoDriver* driver = smgr->getVideoDriver();
197
198         scene::SMesh *mesh = new scene::SMesh();
199         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
200         video::SColor c(255,255,255,255);
201         video::S3DVertex vertices[4] =
202         {
203                 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
204                 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
205                 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
206                 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
207         };
208         u16 indices[] = {0,1,2,2,3,0};
209         buf->append(vertices, 4, indices, 6);
210         // Set material
211         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
212         buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
213         buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
214         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
215         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
216         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
217         // Add to mesh
218         mesh->addMeshBuffer(buf);
219         buf->drop();
220         m_node = smgr->addMeshSceneNode(mesh, NULL);
221         mesh->drop();
222         updateNodePos();
223 }
224
225 void TestCAO::removeFromScene(bool permanent)
226 {
227         if(m_node == NULL)
228                 return;
229
230         m_node->remove();
231         m_node = NULL;
232 }
233
234 void TestCAO::updateLight(u8 light_at_pos)
235 {
236 }
237
238 v3s16 TestCAO::getLightPosition()
239 {
240         return floatToInt(m_position, BS);
241 }
242
243 void TestCAO::updateNodePos()
244 {
245         if(m_node == NULL)
246                 return;
247
248         m_node->setPosition(m_position);
249         //m_node->setRotation(v3f(0, 45, 0));
250 }
251
252 void TestCAO::step(float dtime, ClientEnvironment *env)
253 {
254         if(m_node)
255         {
256                 v3f rot = m_node->getRotation();
257                 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
258                 rot.Y += dtime * 180;
259                 m_node->setRotation(rot);
260         }
261 }
262
263 void TestCAO::processMessage(const std::string &data)
264 {
265         infostream<<"TestCAO: Got data: "<<data<<std::endl;
266         std::istringstream is(data, std::ios::binary);
267         u16 cmd;
268         is>>cmd;
269         if(cmd == 0)
270         {
271                 v3f newpos;
272                 is>>newpos.X;
273                 is>>newpos.Y;
274                 is>>newpos.Z;
275                 m_position = newpos;
276                 updateNodePos();
277         }
278 }
279
280 /*
281         ItemCAO
282 */
283
284 class ItemCAO : public ClientActiveObject
285 {
286 public:
287         ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
288         virtual ~ItemCAO();
289
290         ActiveObjectType getType() const
291         {
292                 return ACTIVEOBJECT_TYPE_ITEM;
293         }
294
295         static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
296
297         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
298                         IrrlichtDevice *irr);
299         void removeFromScene(bool permanent);
300         void updateLight(u8 light_at_pos);
301         v3s16 getLightPosition();
302         void updateNodePos();
303         void updateInfoText();
304         void updateTexture();
305
306         void step(float dtime, ClientEnvironment *env);
307
308         void processMessage(const std::string &data);
309
310         void initialize(const std::string &data);
311
312         core::aabbox3d<f32>* getSelectionBox()
313                 {return &m_selection_box;}
314         v3f getPosition()
315                 {return m_position;}
316
317         std::string infoText()
318                 {return m_infotext;}
319
320         bool getCollisionBox(aabb3f *toset) { return false; }
321 private:
322         core::aabbox3d<f32> m_selection_box;
323         scene::IMeshSceneNode *m_node;
324         v3f m_position;
325         std::string m_itemstring;
326         std::string m_infotext;
327 };
328
329 #include "inventory.h"
330
331 // Prototype
332 ItemCAO proto_ItemCAO(NULL, NULL);
333
334 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
335         ClientActiveObject(0, gamedef, env),
336         m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
337         m_node(NULL),
338         m_position(v3f(0,10*BS,0))
339 {
340         if(!gamedef && !env)
341         {
342                 ClientActiveObject::registerType(getType(), create);
343         }
344 }
345
346 ItemCAO::~ItemCAO()
347 {
348 }
349
350 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
351 {
352         return new ItemCAO(gamedef, env);
353 }
354
355 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
356                         IrrlichtDevice *irr)
357 {
358         if(m_node != NULL)
359                 return;
360
361         //video::IVideoDriver* driver = smgr->getVideoDriver();
362
363         scene::SMesh *mesh = new scene::SMesh();
364         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
365         video::SColor c(255,255,255,255);
366         video::S3DVertex vertices[4] =
367         {
368                 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
369                 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
370                 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
371                 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
372                 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
373                 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
374                 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
375                 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
376         };
377         u16 indices[] = {0,1,2,2,3,0};
378         buf->append(vertices, 4, indices, 6);
379         // Set material
380         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
381         buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
382         // Initialize with a generated placeholder texture
383         buf->getMaterial().setTexture(0, tsrc->getTexture(""));
384         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
385         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
386         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
387         // Add to mesh
388         mesh->addMeshBuffer(buf);
389         buf->drop();
390         m_node = smgr->addMeshSceneNode(mesh, NULL);
391         mesh->drop();
392         updateNodePos();
393
394         /*
395                 Update image of node
396         */
397
398         updateTexture();
399 }
400
401 void ItemCAO::removeFromScene(bool permanent)
402 {
403         if(m_node == NULL)
404                 return;
405
406         m_node->remove();
407         m_node = NULL;
408 }
409
410 void ItemCAO::updateLight(u8 light_at_pos)
411 {
412         if(m_node == NULL)
413                 return;
414
415         u8 li = decode_light(light_at_pos);
416         video::SColor color(255,li,li,li);
417         setMeshColor(m_node->getMesh(), color);
418 }
419
420 v3s16 ItemCAO::getLightPosition()
421 {
422         return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
423 }
424
425 void ItemCAO::updateNodePos()
426 {
427         if(m_node == NULL)
428                 return;
429
430         m_node->setPosition(m_position);
431 }
432
433 void ItemCAO::updateInfoText()
434 {
435         try{
436                 IItemDefManager *idef = m_gamedef->idef();
437                 ItemStack item;
438                 item.deSerialize(m_itemstring, idef);
439                 if(item.isKnown(idef))
440                         m_infotext = item.getDefinition(idef).description;
441                 else
442                         m_infotext = "Unknown item: '" + m_itemstring + "'";
443                 if(item.count >= 2)
444                         m_infotext += " (" + itos(item.count) + ")";
445         }
446         catch(SerializationError &e)
447         {
448                 m_infotext = "Unknown item: '" + m_itemstring + "'";
449         }
450 }
451
452 void ItemCAO::updateTexture()
453 {
454         if(m_node == NULL)
455                 return;
456
457         // Create an inventory item to see what is its image
458         std::istringstream is(m_itemstring, std::ios_base::binary);
459         video::ITexture *texture = NULL;
460         try{
461                 IItemDefManager *idef = m_gamedef->idef();
462                 ItemStack item;
463                 item.deSerialize(is, idef);
464                 texture = idef->getInventoryTexture(item.getDefinition(idef).name, m_gamedef);
465         }
466         catch(SerializationError &e)
467         {
468                 warningstream<<FUNCTION_NAME
469                                 <<": error deSerializing itemstring \""
470                                 <<m_itemstring<<std::endl;
471         }
472
473         // Set meshbuffer texture
474         m_node->getMaterial(0).setTexture(0, texture);
475 }
476
477
478 void ItemCAO::step(float dtime, ClientEnvironment *env)
479 {
480         if(m_node)
481         {
482                 /*v3f rot = m_node->getRotation();
483                 rot.Y += dtime * 120;
484                 m_node->setRotation(rot);*/
485                 LocalPlayer *player = env->getLocalPlayer();
486                 assert(player);
487                 v3f rot = m_node->getRotation();
488                 rot.Y = 180.0 - (player->getYaw());
489                 m_node->setRotation(rot);
490         }
491 }
492
493 void ItemCAO::processMessage(const std::string &data)
494 {
495         //infostream<<"ItemCAO: Got message"<<std::endl;
496         std::istringstream is(data, std::ios::binary);
497         // command
498         u8 cmd = readU8(is);
499         if(cmd == 0)
500         {
501                 // pos
502                 m_position = readV3F1000(is);
503                 updateNodePos();
504         }
505         if(cmd == 1)
506         {
507                 // itemstring
508                 m_itemstring = deSerializeString(is);
509                 updateInfoText();
510                 updateTexture();
511         }
512 }
513
514 void ItemCAO::initialize(const std::string &data)
515 {
516         infostream<<"ItemCAO: Got init data"<<std::endl;
517
518         {
519                 std::istringstream is(data, std::ios::binary);
520                 // version
521                 u8 version = readU8(is);
522                 // check version
523                 if(version != 0)
524                         return;
525                 // pos
526                 m_position = readV3F1000(is);
527                 // itemstring
528                 m_itemstring = deSerializeString(is);
529         }
530
531         updateNodePos();
532         updateInfoText();
533 }
534
535 /*
536         GenericCAO
537 */
538
539 #include "genericobject.h"
540
541 GenericCAO::GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
542                 ClientActiveObject(0, gamedef, env),
543                 //
544                 m_is_player(false),
545                 m_is_local_player(false),
546                 //
547                 m_smgr(NULL),
548                 m_irr(NULL),
549                 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
550                 m_meshnode(NULL),
551                 m_animated_meshnode(NULL),
552                 m_wield_meshnode(NULL),
553                 m_spritenode(NULL),
554                 m_textnode(NULL),
555                 m_position(v3f(0,10*BS,0)),
556                 m_velocity(v3f(0,0,0)),
557                 m_acceleration(v3f(0,0,0)),
558                 m_yaw(0),
559                 m_hp(1),
560                 m_tx_size(1,1),
561                 m_tx_basepos(0,0),
562                 m_initial_tx_basepos_set(false),
563                 m_tx_select_horiz_by_yawpitch(false),
564                 m_animation_range(v2s32(0,0)),
565                 m_animation_speed(15),
566                 m_animation_blend(0),
567                 m_animation_loop(true),
568                 m_bone_position(std::map<std::string, core::vector2d<v3f> >()),
569                 m_attachment_bone(""),
570                 m_attachment_position(v3f(0,0,0)),
571                 m_attachment_rotation(v3f(0,0,0)),
572                 m_attached_to_local(false),
573                 m_anim_frame(0),
574                 m_anim_num_frames(1),
575                 m_anim_framelength(0.2),
576                 m_anim_timer(0),
577                 m_reset_textures_timer(-1),
578                 m_visuals_expired(false),
579                 m_step_distance_counter(0),
580                 m_last_light(255),
581                 m_is_visible(false)
582 {
583         if(gamedef == NULL)
584                 ClientActiveObject::registerType(getType(), create);
585 }
586
587 bool GenericCAO::getCollisionBox(aabb3f *toset)
588 {
589         if (m_prop.physical)
590         {
591                 //update collision box
592                 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
593                 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
594
595                 toset->MinEdge += m_position;
596                 toset->MaxEdge += m_position;
597
598                 return true;
599         }
600
601         return false;
602 }
603
604 bool GenericCAO::collideWithObjects()
605 {
606         return m_prop.collideWithObjects;
607 }
608
609 void GenericCAO::initialize(const std::string &data)
610 {
611         infostream<<"GenericCAO: Got init data"<<std::endl;
612         std::istringstream is(data, std::ios::binary);
613         int num_messages = 0;
614         // version
615         u8 version = readU8(is);
616         // check version
617         if(version == 1) // In PROTOCOL_VERSION 14
618         {
619                 m_name = deSerializeString(is);
620                 m_is_player = readU8(is);
621                 m_id = readS16(is);
622                 m_position = readV3F1000(is);
623                 m_yaw = readF1000(is);
624                 m_hp = readS16(is);
625                 num_messages = readU8(is);
626         }
627         else if(version == 0) // In PROTOCOL_VERSION 13
628         {
629                 m_name = deSerializeString(is);
630                 m_is_player = readU8(is);
631                 m_position = readV3F1000(is);
632                 m_yaw = readF1000(is);
633                 m_hp = readS16(is);
634                 num_messages = readU8(is);
635         }
636         else
637         {
638                 errorstream<<"GenericCAO: Unsupported init data version"
639                                 <<std::endl;
640                 return;
641         }
642
643         for(int i=0; i<num_messages; i++)
644         {
645                 std::string message = deSerializeLongString(is);
646                 processMessage(message);
647         }
648
649         pos_translator.init(m_position);
650         updateNodePos();
651
652         if(m_is_player)
653         {
654                 Player *player = m_env->getPlayer(m_name.c_str());
655                 if(player && player->isLocal())
656                 {
657                         m_is_local_player = true;
658                         m_is_visible = false;
659                         LocalPlayer* localplayer = dynamic_cast<LocalPlayer*>(player);
660
661                         assert( localplayer != NULL );
662                         localplayer->setCAO(this);
663                 }
664                 m_env->addPlayerName(m_name.c_str());
665         }
666 }
667
668 GenericCAO::~GenericCAO()
669 {
670         if(m_is_player)
671         {
672                 m_env->removePlayerName(m_name.c_str());
673         }
674         removeFromScene(true);
675 }
676
677 core::aabbox3d<f32>* GenericCAO::getSelectionBox()
678 {
679         if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
680                 return NULL;
681         return &m_selection_box;
682 }
683
684 v3f GenericCAO::getPosition()
685 {
686         if (getParent() != NULL) {
687                 scene::ISceneNode *node = getSceneNode();
688                 if (node)
689                         return node->getAbsolutePosition();
690                 else
691                         return m_position;
692         }
693         return pos_translator.vect_show;
694 }
695
696 scene::ISceneNode* GenericCAO::getSceneNode()
697 {
698         if (m_meshnode)
699                 return m_meshnode;
700         if (m_animated_meshnode)
701                 return m_animated_meshnode;
702         if (m_wield_meshnode)
703                 return m_wield_meshnode;
704         if (m_spritenode)
705                 return m_spritenode;
706         return NULL;
707 }
708
709 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
710 {
711         return m_meshnode;
712 }
713
714 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
715 {
716         return m_animated_meshnode;
717 }
718
719 WieldMeshSceneNode* GenericCAO::getWieldMeshSceneNode()
720 {
721         return m_wield_meshnode;
722 }
723
724 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
725 {
726         return m_spritenode;
727 }
728
729 void GenericCAO::setChildrenVisible(bool toset)
730 {
731         for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
732                 GenericCAO *obj = m_env->getGenericCAO(m_children[i]);
733                 if (obj) {
734                         obj->setVisible(toset);
735                 }
736         }
737 }
738
739 void GenericCAO::setAttachments()
740 {
741         updateAttachments();
742 }
743
744 ClientActiveObject* GenericCAO::getParent()
745 {
746         ClientActiveObject *obj = NULL;
747
748         u16 attached_id = m_env->attachement_parent_ids[getId()];
749
750         if ((attached_id != 0) &&
751                         (attached_id != getId())) {
752                 obj = m_env->getActiveObject(attached_id);
753         }
754         return obj;
755 }
756
757 void GenericCAO::removeFromScene(bool permanent)
758 {
759         // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
760         if((m_env != NULL) && (permanent))
761         {
762                 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
763                         u16 ci = m_children[i];
764                         if (m_env->attachement_parent_ids[ci] == getId()) {
765                                 m_env->attachement_parent_ids[ci] = 0;
766                         }
767                 }
768
769                 m_env->attachement_parent_ids[getId()] = 0;
770
771                 LocalPlayer* player = m_env->getLocalPlayer();
772                 if (this == player->parent) {
773                         player->parent = NULL;
774                         player->isAttached = false;
775                 }
776         }
777
778         if(m_meshnode)
779         {
780                 m_meshnode->remove();
781                 m_meshnode->drop();
782                 m_meshnode = NULL;
783         }
784         if(m_animated_meshnode)
785         {
786                 m_animated_meshnode->remove();
787                 m_animated_meshnode->drop();
788                 m_animated_meshnode = NULL;
789         }
790         if(m_wield_meshnode)
791         {
792                 m_wield_meshnode->remove();
793                 m_wield_meshnode->drop();
794                 m_wield_meshnode = NULL;
795         }
796         if(m_spritenode)
797         {
798                 m_spritenode->remove();
799                 m_spritenode->drop();
800                 m_spritenode = NULL;
801         }
802         if (m_textnode)
803         {
804                 m_textnode->remove();
805                 m_textnode->drop();
806                 m_textnode = NULL;
807         }
808 }
809
810 void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
811                 IrrlichtDevice *irr)
812 {
813         m_smgr = smgr;
814         m_irr = irr;
815
816         if (getSceneNode() != NULL)
817                 return;
818
819         m_visuals_expired = false;
820
821         if(!m_prop.is_visible)
822                 return;
823
824         //video::IVideoDriver* driver = smgr->getVideoDriver();
825
826         if(m_prop.visual == "sprite")
827         {
828                 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
829                 m_spritenode = smgr->addBillboardSceneNode(
830                                 NULL, v2f(1, 1), v3f(0,0,0), -1);
831                 m_spritenode->grab();
832                 m_spritenode->setMaterialTexture(0,
833                                 tsrc->getTextureForMesh("unknown_node.png"));
834                 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
835                 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
836                 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
837                 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
838                 u8 li = m_last_light;
839                 m_spritenode->setColor(video::SColor(255,li,li,li));
840                 m_spritenode->setSize(m_prop.visual_size*BS);
841                 {
842                         const float txs = 1.0 / 1;
843                         const float tys = 1.0 / 1;
844                         setBillboardTextureMatrix(m_spritenode,
845                                         txs, tys, 0, 0);
846                 }
847         }
848         else if(m_prop.visual == "upright_sprite") {
849                 scene::SMesh *mesh = new scene::SMesh();
850                 double dx = BS*m_prop.visual_size.X/2;
851                 double dy = BS*m_prop.visual_size.Y/2;
852                 { // Front
853                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
854                 u8 li = m_last_light;
855                 video::SColor c(255,li,li,li);
856                 video::S3DVertex vertices[4] =
857                 {
858                         video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
859                         video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
860                         video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
861                         video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
862                 };
863                 u16 indices[] = {0,1,2,2,3,0};
864                 buf->append(vertices, 4, indices, 6);
865                 // Set material
866                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
867                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
868                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
869                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
870                 // Add to mesh
871                 mesh->addMeshBuffer(buf);
872                 buf->drop();
873                 }
874                 { // Back
875                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
876                 u8 li = m_last_light;
877                 video::SColor c(255,li,li,li);
878                 video::S3DVertex vertices[4] =
879                 {
880                         video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
881                         video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
882                         video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
883                         video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
884                 };
885                 u16 indices[] = {0,1,2,2,3,0};
886                 buf->append(vertices, 4, indices, 6);
887                 // Set material
888                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
889                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
890                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
891                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
892                 // Add to mesh
893                 mesh->addMeshBuffer(buf);
894                 buf->drop();
895                 }
896                 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
897                 m_meshnode->grab();
898                 mesh->drop();
899                 // Set it to use the materials of the meshbuffers directly.
900                 // This is needed for changing the texture in the future
901                 m_meshnode->setReadOnlyMaterials(true);
902         }
903         else if(m_prop.visual == "cube") {
904                 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
905                 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
906                 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
907                 m_meshnode->grab();
908                 mesh->drop();
909
910                 m_meshnode->setScale(v3f(m_prop.visual_size.X,
911                                 m_prop.visual_size.Y,
912                                 m_prop.visual_size.X));
913                 u8 li = m_last_light;
914                 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
915
916                 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
917                 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
918                 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
919                 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
920         }
921         else if(m_prop.visual == "mesh") {
922                 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
923                 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
924                 if(mesh)
925                 {
926                         m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
927                         m_animated_meshnode->grab();
928                         mesh->drop(); // The scene node took hold of it
929                         m_animated_meshnode->animateJoints(); // Needed for some animations
930                         m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
931                                         m_prop.visual_size.Y,
932                                         m_prop.visual_size.X));
933                         u8 li = m_last_light;
934                         setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
935
936                         bool backface_culling = m_prop.backface_culling;
937                         if (m_is_player)
938                                 backface_culling = false;
939
940                         m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
941                         m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
942                         m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
943                         m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
944                         m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
945                 }
946                 else
947                         errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
948         }
949         else if(m_prop.visual == "wielditem") {
950                 infostream<<"GenericCAO::addToScene(): wielditem"<<std::endl;
951                 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
952                 if(m_prop.textures.size() >= 1){
953                         infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
954                         IItemDefManager *idef = m_gamedef->idef();
955                         ItemStack item(m_prop.textures[0], 1, 0, "", idef);
956
957                         if (!item.getDefinition(idef).meshname.empty())
958                         {
959                                 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(item.getDefinition(idef).meshname);
960                                 if(mesh)
961                                 {
962                                         m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
963
964                                         m_animated_meshnode->grab();
965                                         mesh->drop(); // The scene node took hold of it
966                                         m_animated_meshnode->animateJoints(); // Needed for some animations
967                                         m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
968                                                         m_prop.visual_size.Y,
969                                                         m_prop.visual_size.X));
970                                         u8 li = m_last_light;
971                                         setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
972
973                                         bool backface_culling = m_prop.backface_culling;
974                                         if (m_is_player)
975                                                 backface_culling = false;
976
977                                         m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
978                                         m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
979                                         m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
980                                         m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
981                                         m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
982                                 }
983                         }
984                         else {
985                                 m_wield_meshnode = new WieldMeshSceneNode(
986                                                 smgr->getRootSceneNode(), smgr, -1);
987                                 m_wield_meshnode->setItem(item, m_gamedef);
988                                 m_wield_meshnode->setScale(v3f(m_prop.visual_size.X/2,
989                                                 m_prop.visual_size.Y/2,
990                                                 m_prop.visual_size.X/2));
991                                 u8 li = m_last_light;
992                                 m_wield_meshnode->setColor(video::SColor(255,li,li,li));
993                         }
994                 }
995         } else {
996                 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
997                                 <<"\" not supported"<<std::endl;
998         }
999         updateTextures("");
1000
1001         scene::ISceneNode *node = getSceneNode();
1002         if (node && m_prop.nametag != "" && !m_is_local_player) {
1003                 // Add a text node for showing the name
1004                 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
1005                 std::wstring nametag_text = utf8_to_wide(m_prop.nametag);
1006                 m_textnode = smgr->addTextSceneNode(gui->getSkin()->getFont(),
1007                                 nametag_text.c_str(), m_prop.nametag_color, node);
1008                 m_textnode->grab();
1009                 m_textnode->setPosition(v3f(0, BS*1.1, 0));
1010
1011                 // Enforce hiding nametag,
1012                 // because if freetype is enabled, a grey
1013                 // shadow can remain.
1014                 m_textnode->setVisible(m_prop.nametag_color.getAlpha() > 0);
1015         }
1016
1017         updateNodePos();
1018         updateAnimation();
1019         updateBonePosition();
1020         updateAttachments();
1021 }
1022
1023 void GenericCAO::updateLight(u8 light_at_pos)
1024 {
1025         // Don't update light of attached one
1026         if (getParent() != NULL) {
1027                 return;
1028         }
1029
1030         updateLightNoCheck(light_at_pos);
1031
1032         // Update light of all children
1033         for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1034                 ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1035                 if (obj) {
1036                         obj->updateLightNoCheck(light_at_pos);
1037                 }
1038         }
1039 }
1040
1041 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
1042 {
1043         u8 li = decode_light(light_at_pos);
1044         if (li != m_last_light) {
1045                 m_last_light = li;
1046                 video::SColor color(255,li,li,li);
1047                 if (m_meshnode) {
1048                         setMeshColor(m_meshnode->getMesh(), color);
1049                 } else if (m_animated_meshnode) {
1050                         setMeshColor(m_animated_meshnode->getMesh(), color);
1051                 } else if (m_wield_meshnode) {
1052                         m_wield_meshnode->setColor(color);
1053                 } else if (m_spritenode) {
1054                         m_spritenode->setColor(color);
1055                 }
1056         }
1057 }
1058
1059 v3s16 GenericCAO::getLightPosition()
1060 {
1061         return floatToInt(m_position, BS);
1062 }
1063
1064 void GenericCAO::updateNodePos()
1065 {
1066         if (getParent() != NULL)
1067                 return;
1068
1069         scene::ISceneNode *node = getSceneNode();
1070
1071         if (node) {
1072                 v3s16 camera_offset = m_env->getCameraOffset();
1073                 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
1074                 if (node != m_spritenode) { // rotate if not a sprite
1075                         v3f rot = node->getRotation();
1076                         rot.Y = -m_yaw;
1077                         node->setRotation(rot);
1078                 }
1079         }
1080 }
1081
1082 void GenericCAO::step(float dtime, ClientEnvironment *env)
1083 {
1084         // Handel model of local player instantly to prevent lags
1085         if(m_is_local_player)
1086         {
1087                 LocalPlayer *player = m_env->getLocalPlayer();
1088
1089                 if (m_is_visible)
1090                 {
1091                         int old_anim = player->last_animation;
1092                         float old_anim_speed = player->last_animation_speed;
1093                         m_position = player->getPosition() + v3f(0,BS,0);
1094                         m_velocity = v3f(0,0,0);
1095                         m_acceleration = v3f(0,0,0);
1096                         pos_translator.vect_show = m_position;
1097                         m_yaw = player->getYaw();
1098                         PlayerControl controls = player->getPlayerControl();
1099
1100                         bool walking = false;
1101                         if(controls.up || controls.down || controls.left || controls.right)
1102                                 walking = true;
1103
1104                         f32 new_speed = player->local_animation_speed;
1105                         v2s32 new_anim = v2s32(0,0);
1106                         bool allow_update = false;
1107
1108                         // increase speed if using fast or flying fast
1109                         if((g_settings->getBool("fast_move") &&
1110                                         m_gamedef->checkLocalPrivilege("fast")) &&
1111                                         (controls.aux1 ||
1112                                         (!player->touching_ground &&
1113                                         g_settings->getBool("free_move") &&
1114                                         m_gamedef->checkLocalPrivilege("fly"))))
1115                                         new_speed *= 1.5;
1116                         // slowdown speed if sneeking
1117                         if(controls.sneak && walking)
1118                                 new_speed /= 2;
1119
1120                         if(walking && (controls.LMB || controls.RMB))
1121                         {
1122                                 new_anim = player->local_animations[3];
1123                                 player->last_animation = WD_ANIM;
1124                         } else if(walking) {
1125                                 new_anim = player->local_animations[1];
1126                                 player->last_animation = WALK_ANIM;
1127                         } else if(controls.LMB || controls.RMB) {
1128                                 new_anim = player->local_animations[2];
1129                                 player->last_animation = DIG_ANIM;
1130                         }
1131
1132                         // Apply animations if input detected and not attached
1133                         // or set idle animation
1134                         if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1135                         {
1136                                 allow_update = true;
1137                                 m_animation_range = new_anim;
1138                                 m_animation_speed = new_speed;
1139                                 player->last_animation_speed = m_animation_speed;
1140                         } else {
1141                                 player->last_animation = NO_ANIM;
1142
1143                                 if (old_anim != NO_ANIM)
1144                                 {
1145                                         m_animation_range = player->local_animations[0];
1146                                         updateAnimation();
1147                                 }
1148                         }
1149
1150                         // Update local player animations
1151                         if ((player->last_animation != old_anim ||
1152                                 m_animation_speed != old_anim_speed) &&
1153                                 player->last_animation != NO_ANIM && allow_update)
1154                                         updateAnimation();
1155
1156                 }
1157         }
1158
1159         if(m_visuals_expired && m_smgr && m_irr){
1160                 m_visuals_expired = false;
1161
1162                 // Attachments, part 1: All attached objects must be unparented first,
1163                 // or Irrlicht causes a segmentation fault
1164                 for(std::vector<u16>::iterator ci = m_children.begin();
1165                                 ci != m_children.end();)
1166                 {
1167                         if (m_env->attachement_parent_ids[*ci] != getId()) {
1168                                 ci = m_children.erase(ci);
1169                                 continue;
1170                         }
1171                         ClientActiveObject *obj = m_env->getActiveObject(*ci);
1172                         if (obj) {
1173                                 scene::ISceneNode *child_node = obj->getSceneNode();
1174                                 if (child_node)
1175                                         child_node->setParent(m_smgr->getRootSceneNode());
1176                         }
1177                         ++ci;
1178                 }
1179
1180                 removeFromScene(false);
1181                 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1182
1183                 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1184                 for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
1185                         // Get the object of the child
1186                         ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
1187                         if (obj)
1188                                 obj->setAttachments();
1189                 }
1190         }
1191
1192         // Make sure m_is_visible is always applied
1193         scene::ISceneNode *node = getSceneNode();
1194         if (node)
1195                 node->setVisible(m_is_visible);
1196
1197         if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1198         {
1199                 // Set these for later
1200                 m_position = getPosition();
1201                 m_velocity = v3f(0,0,0);
1202                 m_acceleration = v3f(0,0,0);
1203                 pos_translator.vect_show = m_position;
1204
1205                 if(m_is_local_player) // Update local player attachment position
1206                 {
1207                         LocalPlayer *player = m_env->getLocalPlayer();
1208                         player->overridePosition = getParent()->getPosition();
1209                         m_env->getLocalPlayer()->parent = getParent();
1210                 }
1211         } else {
1212                 v3f lastpos = pos_translator.vect_show;
1213
1214                 if(m_prop.physical)
1215                 {
1216                         core::aabbox3d<f32> box = m_prop.collisionbox;
1217                         box.MinEdge *= BS;
1218                         box.MaxEdge *= BS;
1219                         collisionMoveResult moveresult;
1220                         f32 pos_max_d = BS*0.125; // Distance per iteration
1221                         v3f p_pos = m_position;
1222                         v3f p_velocity = m_velocity;
1223                         v3f p_acceleration = m_acceleration;
1224                         moveresult = collisionMoveSimple(env,env->getGameDef(),
1225                                         pos_max_d, box, m_prop.stepheight, dtime,
1226                                         p_pos, p_velocity, p_acceleration,
1227                                         this, m_prop.collideWithObjects);
1228                         // Apply results
1229                         m_position = p_pos;
1230                         m_velocity = p_velocity;
1231                         m_acceleration = p_acceleration;
1232
1233                         bool is_end_position = moveresult.collides;
1234                         pos_translator.update(m_position, is_end_position, dtime);
1235                         pos_translator.translate(dtime);
1236                         updateNodePos();
1237                 } else {
1238                         m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1239                         m_velocity += dtime * m_acceleration;
1240                         pos_translator.update(m_position, pos_translator.aim_is_end,
1241                                         pos_translator.anim_time);
1242                         pos_translator.translate(dtime);
1243                         updateNodePos();
1244                 }
1245
1246                 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1247                 m_step_distance_counter += moved;
1248                 if(m_step_distance_counter > 1.5*BS)
1249                 {
1250                         m_step_distance_counter = 0;
1251                         if(!m_is_local_player && m_prop.makes_footstep_sound)
1252                         {
1253                                 INodeDefManager *ndef = m_gamedef->ndef();
1254                                 v3s16 p = floatToInt(getPosition() + v3f(0,
1255                                                 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1256                                 MapNode n = m_env->getMap().getNodeNoEx(p);
1257                                 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1258                                 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1259                         }
1260                 }
1261         }
1262
1263         m_anim_timer += dtime;
1264         if(m_anim_timer >= m_anim_framelength)
1265         {
1266                 m_anim_timer -= m_anim_framelength;
1267                 m_anim_frame++;
1268                 if(m_anim_frame >= m_anim_num_frames)
1269                         m_anim_frame = 0;
1270         }
1271
1272         updateTexturePos();
1273
1274         if(m_reset_textures_timer >= 0)
1275         {
1276                 m_reset_textures_timer -= dtime;
1277                 if(m_reset_textures_timer <= 0){
1278                         m_reset_textures_timer = -1;
1279                         updateTextures("");
1280                 }
1281         }
1282         if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1283         {
1284                 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1285                 updateNodePos();
1286         }
1287
1288         if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1289                         (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1290         {
1291                 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1292                                 + m_prop.automatic_face_movement_dir_offset;
1293                 float max_rotation_delta =
1294                                 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
1295
1296                 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
1297                         (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
1298
1299                         m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
1300                 } else {
1301                         m_yaw = optimal_yaw;
1302                 }
1303                 updateNodePos();
1304         }
1305 }
1306
1307 void GenericCAO::updateTexturePos()
1308 {
1309         if(m_spritenode)
1310         {
1311                 scene::ICameraSceneNode* camera =
1312                                 m_spritenode->getSceneManager()->getActiveCamera();
1313                 if(!camera)
1314                         return;
1315                 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1316                                 - camera->getAbsolutePosition();
1317                 cam_to_entity.normalize();
1318
1319                 int row = m_tx_basepos.Y;
1320                 int col = m_tx_basepos.X;
1321
1322                 if(m_tx_select_horiz_by_yawpitch)
1323                 {
1324                         if(cam_to_entity.Y > 0.75)
1325                                 col += 5;
1326                         else if(cam_to_entity.Y < -0.75)
1327                                 col += 4;
1328                         else{
1329                                 float mob_dir =
1330                                                 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1331                                 float dir = mob_dir - m_yaw;
1332                                 dir = wrapDegrees_180(dir);
1333                                 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1334                                 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1335                                         col += 2;
1336                                 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1337                                         col += 3;
1338                                 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1339                                         col += 0;
1340                                 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1341                                         col += 1;
1342                                 else
1343                                         col += 4;
1344                         }
1345                 }
1346
1347                 // Animation goes downwards
1348                 row += m_anim_frame;
1349
1350                 float txs = m_tx_size.X;
1351                 float tys = m_tx_size.Y;
1352                 setBillboardTextureMatrix(m_spritenode,
1353                                 txs, tys, col, row);
1354         }
1355 }
1356
1357 void GenericCAO::updateTextures(const std::string &mod)
1358 {
1359         ITextureSource *tsrc = m_gamedef->tsrc();
1360
1361         bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1362         bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1363         bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1364
1365         if(m_spritenode)
1366         {
1367                 if(m_prop.visual == "sprite")
1368                 {
1369                         std::string texturestring = "unknown_node.png";
1370                         if(m_prop.textures.size() >= 1)
1371                                 texturestring = m_prop.textures[0];
1372                         texturestring += mod;
1373                         m_spritenode->setMaterialTexture(0,
1374                                         tsrc->getTextureForMesh(texturestring));
1375
1376                         // This allows setting per-material colors. However, until a real lighting
1377                         // system is added, the code below will have no effect. Once MineTest
1378                         // has directional lighting, it should work automatically.
1379                         if(m_prop.colors.size() >= 1)
1380                         {
1381                                 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1382                                 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1383                                 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1384                         }
1385
1386                         m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1387                         m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1388                         m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1389                 }
1390         }
1391         if(m_animated_meshnode)
1392         {
1393                 if(m_prop.visual == "mesh")
1394                 {
1395                         for (u32 i = 0; i < m_prop.textures.size() &&
1396                                         i < m_animated_meshnode->getMaterialCount(); ++i)
1397                         {
1398                                 std::string texturestring = m_prop.textures[i];
1399                                 if(texturestring == "")
1400                                         continue; // Empty texture string means don't modify that material
1401                                 texturestring += mod;
1402                                 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1403                                 if(!texture)
1404                                 {
1405                                         errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1406                                         continue;
1407                                 }
1408
1409                                 // Set material flags and texture
1410                                 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1411                                 material.TextureLayer[0].Texture = texture;
1412                                 material.setFlag(video::EMF_LIGHTING, false);
1413                                 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1414
1415                                 m_animated_meshnode->getMaterial(i)
1416                                                 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1417                                 m_animated_meshnode->getMaterial(i)
1418                                                 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1419                                 m_animated_meshnode->getMaterial(i)
1420                                                 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1421                         }
1422                         for (u32 i = 0; i < m_prop.colors.size() &&
1423                         i < m_animated_meshnode->getMaterialCount(); ++i)
1424                         {
1425                                 // This allows setting per-material colors. However, until a real lighting
1426                                 // system is added, the code below will have no effect. Once MineTest
1427                                 // has directional lighting, it should work automatically.
1428                                 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1429                                 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1430                                 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1431                         }
1432                 }
1433                 else if (m_prop.visual == "wielditem") {
1434                         IItemDefManager *idef = m_gamedef->idef();
1435                         ItemStack item(m_prop.textures[0], 1, 0, "", idef);
1436
1437                         if (!item.getDefinition(idef).meshname.empty()) {
1438
1439                                 unsigned int materialcount = m_animated_meshnode->getMaterialCount();
1440
1441                                 for (unsigned int i = 0; i < materialcount; i++) {
1442                                         m_animated_meshnode->getMaterial(i)
1443                                                         .setTexture(0, tsrc->getTexture(item
1444                                                                         .getDefinition(idef).meshtexture));
1445                                 }
1446                         }
1447                 }
1448         }
1449         if(m_meshnode)
1450         {
1451                 if(m_prop.visual == "cube")
1452                 {
1453                         for (u32 i = 0; i < 6; ++i)
1454                         {
1455                                 std::string texturestring = "unknown_node.png";
1456                                 if(m_prop.textures.size() > i)
1457                                         texturestring = m_prop.textures[i];
1458                                 texturestring += mod;
1459
1460
1461                                 // Set material flags and texture
1462                                 video::SMaterial& material = m_meshnode->getMaterial(i);
1463                                 material.setFlag(video::EMF_LIGHTING, false);
1464                                 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1465                                 material.setTexture(0,
1466                                                 tsrc->getTextureForMesh(texturestring));
1467                                 material.getTextureMatrix(0).makeIdentity();
1468
1469                                 // This allows setting per-material colors. However, until a real lighting
1470                                 // system is added, the code below will have no effect. Once MineTest
1471                                 // has directional lighting, it should work automatically.
1472                                 if(m_prop.colors.size() > i)
1473                                 {
1474                                         m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1475                                         m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1476                                         m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1477                                 }
1478
1479                                 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1480                                 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1481                                 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1482                         }
1483                 }
1484                 else if(m_prop.visual == "upright_sprite")
1485                 {
1486                         scene::IMesh *mesh = m_meshnode->getMesh();
1487                         {
1488                                 std::string tname = "unknown_object.png";
1489                                 if(m_prop.textures.size() >= 1)
1490                                         tname = m_prop.textures[0];
1491                                 tname += mod;
1492                                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1493                                 buf->getMaterial().setTexture(0,
1494                                                 tsrc->getTextureForMesh(tname));
1495
1496                                 // This allows setting per-material colors. However, until a real lighting
1497                                 // system is added, the code below will have no effect. Once MineTest
1498                                 // has directional lighting, it should work automatically.
1499                                 if(m_prop.colors.size() >= 1)
1500                                 {
1501                                         buf->getMaterial().AmbientColor = m_prop.colors[0];
1502                                         buf->getMaterial().DiffuseColor = m_prop.colors[0];
1503                                         buf->getMaterial().SpecularColor = m_prop.colors[0];
1504                                 }
1505
1506                                 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1507                                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1508                                 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1509                         }
1510                         {
1511                                 std::string tname = "unknown_object.png";
1512                                 if(m_prop.textures.size() >= 2)
1513                                         tname = m_prop.textures[1];
1514                                 else if(m_prop.textures.size() >= 1)
1515                                         tname = m_prop.textures[0];
1516                                 tname += mod;
1517                                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1518                                 buf->getMaterial().setTexture(0,
1519                                                 tsrc->getTextureForMesh(tname));
1520
1521                                 // This allows setting per-material colors. However, until a real lighting
1522                                 // system is added, the code below will have no effect. Once MineTest
1523                                 // has directional lighting, it should work automatically.
1524                                 if(m_prop.colors.size() >= 2)
1525                                 {
1526                                         buf->getMaterial().AmbientColor = m_prop.colors[1];
1527                                         buf->getMaterial().DiffuseColor = m_prop.colors[1];
1528                                         buf->getMaterial().SpecularColor = m_prop.colors[1];
1529                                 }
1530                                 else if(m_prop.colors.size() >= 1)
1531                                 {
1532                                         buf->getMaterial().AmbientColor = m_prop.colors[0];
1533                                         buf->getMaterial().DiffuseColor = m_prop.colors[0];
1534                                         buf->getMaterial().SpecularColor = m_prop.colors[0];
1535                                 }
1536
1537                                 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1538                                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1539                                 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1540                         }
1541                 }
1542         }
1543 }
1544
1545 void GenericCAO::updateAnimation()
1546 {
1547         if(m_animated_meshnode == NULL)
1548                 return;
1549
1550         if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1551                 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1552                         m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1553         if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1554                 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1555         m_animated_meshnode->setTransitionTime(m_animation_blend);
1556 // Requires Irrlicht 1.8 or greater
1557 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1558         if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1559                 m_animated_meshnode->setLoopMode(m_animation_loop);
1560 #endif
1561 }
1562
1563 void GenericCAO::updateBonePosition()
1564 {
1565         if(m_bone_position.empty() || m_animated_meshnode == NULL)
1566                 return;
1567
1568         m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1569         for(std::map<std::string,
1570                         core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1571                         ii != m_bone_position.end(); ++ii)
1572         {
1573                 std::string bone_name = (*ii).first;
1574                 v3f bone_pos = (*ii).second.X;
1575                 v3f bone_rot = (*ii).second.Y;
1576                 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1577                 if(bone)
1578                 {
1579                         bone->setPosition(bone_pos);
1580                         bone->setRotation(bone_rot);
1581                 }
1582         }
1583 }
1584
1585 void GenericCAO::updateAttachments()
1586 {
1587
1588         if (getParent() == NULL) { // Detach or don't attach
1589                 scene::ISceneNode *node = getSceneNode();
1590                 if (node) {
1591                         v3f old_position = node->getAbsolutePosition();
1592                         v3f old_rotation = node->getRotation();
1593                         node->setParent(m_smgr->getRootSceneNode());
1594                         node->setPosition(old_position);
1595                         node->setRotation(old_rotation);
1596                         node->updateAbsolutePosition();
1597                 }
1598                 if (m_is_local_player) {
1599                         LocalPlayer *player = m_env->getLocalPlayer();
1600                         player->isAttached = false;
1601                 }
1602         }
1603         else // Attach
1604         {
1605                 scene::ISceneNode *my_node = getSceneNode();
1606
1607                 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1608                 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1609                                 getParent()->getAnimatedMeshSceneNode();
1610                 if (parent_animated_mesh_node && m_attachment_bone != "") {
1611                         parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1612                 }
1613
1614                 if (my_node && parent_node) {
1615                         my_node->setParent(parent_node);
1616                         my_node->setPosition(m_attachment_position);
1617                         my_node->setRotation(m_attachment_rotation);
1618                         my_node->updateAbsolutePosition();
1619                 }
1620                 if (m_is_local_player) {
1621                         LocalPlayer *player = m_env->getLocalPlayer();
1622                         player->isAttached = true;
1623                 }
1624         }
1625 }
1626
1627 void GenericCAO::processMessage(const std::string &data)
1628 {
1629         //infostream<<"GenericCAO: Got message"<<std::endl;
1630         std::istringstream is(data, std::ios::binary);
1631         // command
1632         u8 cmd = readU8(is);
1633         if(cmd == GENERIC_CMD_SET_PROPERTIES)
1634         {
1635                 m_prop = gob_read_set_properties(is);
1636
1637                 m_selection_box = m_prop.collisionbox;
1638                 m_selection_box.MinEdge *= BS;
1639                 m_selection_box.MaxEdge *= BS;
1640
1641                 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1642                 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1643
1644                 if(!m_initial_tx_basepos_set){
1645                         m_initial_tx_basepos_set = true;
1646                         m_tx_basepos = m_prop.initial_sprite_basepos;
1647                 }
1648
1649                 if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
1650                         m_prop.nametag = m_name;
1651
1652                 expireVisuals();
1653         }
1654         else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1655         {
1656                 // Not sent by the server if this object is an attachment.
1657                 // We might however get here if the server notices the object being detached before the client.
1658                 m_position = readV3F1000(is);
1659                 m_velocity = readV3F1000(is);
1660                 m_acceleration = readV3F1000(is);
1661                 if(fabs(m_prop.automatic_rotate) < 0.001)
1662                         m_yaw = readF1000(is);
1663                 else
1664                         readF1000(is);
1665                 bool do_interpolate = readU8(is);
1666                 bool is_end_position = readU8(is);
1667                 float update_interval = readF1000(is);
1668
1669                 // Place us a bit higher if we're physical, to not sink into
1670                 // the ground due to sucky collision detection...
1671                 if(m_prop.physical)
1672                         m_position += v3f(0,0.002,0);
1673
1674                 if(getParent() != NULL) // Just in case
1675                         return;
1676
1677                 if(do_interpolate)
1678                 {
1679                         if(!m_prop.physical)
1680                                 pos_translator.update(m_position, is_end_position, update_interval);
1681                 } else {
1682                         pos_translator.init(m_position);
1683                 }
1684                 updateNodePos();
1685         }
1686         else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1687                 std::string mod = deSerializeString(is);
1688                 updateTextures(mod);
1689         }
1690         else if(cmd == GENERIC_CMD_SET_SPRITE) {
1691                 v2s16 p = readV2S16(is);
1692                 int num_frames = readU16(is);
1693                 float framelength = readF1000(is);
1694                 bool select_horiz_by_yawpitch = readU8(is);
1695
1696                 m_tx_basepos = p;
1697                 m_anim_num_frames = num_frames;
1698                 m_anim_framelength = framelength;
1699                 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1700
1701                 updateTexturePos();
1702         }
1703         else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1704                 float override_speed = readF1000(is);
1705                 float override_jump = readF1000(is);
1706                 float override_gravity = readF1000(is);
1707                 // these are sent inverted so we get true when the server sends nothing
1708                 bool sneak = !readU8(is);
1709                 bool sneak_glitch = !readU8(is);
1710
1711
1712                 if(m_is_local_player)
1713                 {
1714                         LocalPlayer *player = m_env->getLocalPlayer();
1715                         player->physics_override_speed = override_speed;
1716                         player->physics_override_jump = override_jump;
1717                         player->physics_override_gravity = override_gravity;
1718                         player->physics_override_sneak = sneak;
1719                         player->physics_override_sneak_glitch = sneak_glitch;
1720                 }
1721         }
1722         else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1723                 // TODO: change frames send as v2s32 value
1724                 v2f range = readV2F1000(is);
1725                 if (!m_is_local_player) {
1726                         m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1727                         m_animation_speed = readF1000(is);
1728                         m_animation_blend = readF1000(is);
1729                         // these are sent inverted so we get true when the server sends nothing
1730                         m_animation_loop = !readU8(is);
1731                         updateAnimation();
1732                 } else {
1733                         LocalPlayer *player = m_env->getLocalPlayer();
1734                         if(player->last_animation == NO_ANIM)
1735                         {
1736                                 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1737                                 m_animation_speed = readF1000(is);
1738                                 m_animation_blend = readF1000(is);
1739                                 // these are sent inverted so we get true when the server sends nothing
1740                                 m_animation_loop = !readU8(is);
1741                         }
1742                         // update animation only if local animations present
1743                         // and received animation is unknown (except idle animation)
1744                         bool is_known = false;
1745                         for (int i = 1;i<4;i++)
1746                         {
1747                                 if(m_animation_range.Y == player->local_animations[i].Y)
1748                                         is_known = true;
1749                         }
1750                         if(!is_known ||
1751                                         (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1752                         {
1753                                         updateAnimation();
1754                         }
1755                 }
1756         }
1757         else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1758                 std::string bone = deSerializeString(is);
1759                 v3f position = readV3F1000(is);
1760                 v3f rotation = readV3F1000(is);
1761                 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1762
1763                 updateBonePosition();
1764         } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1765                 u16 parentID = readS16(is);
1766                 u16 oldparent = m_env->attachement_parent_ids[getId()];
1767                 if (oldparent) {
1768                         m_children.erase(std::remove(m_children.begin(), m_children.end(),
1769                                 getId()), m_children.end());
1770                 }
1771                 m_env->attachement_parent_ids[getId()] = parentID;
1772                 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1773
1774                 if (parentobj) {
1775                         parentobj->m_children.push_back(getId());
1776                 }
1777
1778                 m_attachment_bone = deSerializeString(is);
1779                 m_attachment_position = readV3F1000(is);
1780                 m_attachment_rotation = readV3F1000(is);
1781
1782                 // localplayer itself can't be attached to localplayer
1783                 if (!m_is_local_player) {
1784                         m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1785                         // Objects attached to the local player should be hidden by default
1786                         m_is_visible = !m_attached_to_local;
1787                 }
1788
1789                 updateAttachments();
1790         }
1791         else if(cmd == GENERIC_CMD_PUNCHED) {
1792                 /*s16 damage =*/ readS16(is);
1793                 s16 result_hp = readS16(is);
1794
1795                 // Use this instead of the send damage to not interfere with prediction
1796                 s16 damage = m_hp - result_hp;
1797
1798                 m_hp = result_hp;
1799
1800                 if (damage > 0)
1801                 {
1802                         if (m_hp <= 0)
1803                         {
1804                                 // TODO: Execute defined fast response
1805                                 // As there is no definition, make a smoke puff
1806                                 ClientSimpleObject *simple = createSmokePuff(
1807                                                 m_smgr, m_env, m_position,
1808                                                 m_prop.visual_size * BS);
1809                                 m_env->addSimpleObject(simple);
1810                         } else {
1811                                 // TODO: Execute defined fast response
1812                                 // Flashing shall suffice as there is no definition
1813                                 m_reset_textures_timer = 0.05;
1814                                 if(damage >= 2)
1815                                         m_reset_textures_timer += 0.05 * damage;
1816                                 updateTextures("^[brighten");
1817                         }
1818                 }
1819         }
1820         else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1821                 m_armor_groups.clear();
1822                 int armor_groups_size = readU16(is);
1823                 for(int i=0; i<armor_groups_size; i++)
1824                 {
1825                         std::string name = deSerializeString(is);
1826                         int rating = readS16(is);
1827                         m_armor_groups[name] = rating;
1828                 }
1829         } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1830                 // Deprecated, for backwards compatibility only.
1831                 readU8(is); // version
1832                 m_prop.nametag_color = readARGB8(is);
1833                 if (m_textnode != NULL) {
1834                         m_textnode->setTextColor(m_prop.nametag_color);
1835
1836                         // Enforce hiding nametag,
1837                         // because if freetype is enabled, a grey shadow can remain.
1838                         m_textnode->setVisible(m_prop.nametag_color.getAlpha() > 0);
1839                 }
1840         }
1841 }
1842
1843 /* \pre punchitem != NULL
1844  */
1845 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1846                 float time_from_last_punch)
1847 {
1848         assert(punchitem);      // pre-condition
1849         const ToolCapabilities *toolcap =
1850                         &punchitem->getToolCapabilities(m_gamedef->idef());
1851         PunchDamageResult result = getPunchDamage(
1852                         m_armor_groups,
1853                         toolcap,
1854                         punchitem,
1855                         time_from_last_punch);
1856
1857         if(result.did_punch && result.damage != 0)
1858         {
1859                 if(result.damage < m_hp)
1860                 {
1861                         m_hp -= result.damage;
1862                 } else {
1863                         m_hp = 0;
1864                         // TODO: Execute defined fast response
1865                         // As there is no definition, make a smoke puff
1866                         ClientSimpleObject *simple = createSmokePuff(
1867                                         m_smgr, m_env, m_position,
1868                                         m_prop.visual_size * BS);
1869                         m_env->addSimpleObject(simple);
1870                 }
1871                 // TODO: Execute defined fast response
1872                 // Flashing shall suffice as there is no definition
1873                 m_reset_textures_timer = 0.05;
1874                 if(result.damage >= 2)
1875                         m_reset_textures_timer += 0.05 * result.damage;
1876                 updateTextures("^[brighten");
1877         }
1878
1879         return false;
1880 }
1881
1882 std::string GenericCAO::debugInfoText()
1883 {
1884         std::ostringstream os(std::ios::binary);
1885         os<<"GenericCAO hp="<<m_hp<<"\n";
1886         os<<"armor={";
1887         for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1888                         i != m_armor_groups.end(); ++i)
1889         {
1890                 os<<i->first<<"="<<i->second<<", ";
1891         }
1892         os<<"}";
1893         return os.str();
1894 }
1895
1896 // Prototype
1897 GenericCAO proto_GenericCAO(NULL, NULL);