Make player files saving again
[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 "content_cao.h"
21 #include "tile.h"
22 #include "environment.h"
23 #include "collision.h"
24 #include "settings.h"
25 #include <ICameraSceneNode.h>
26 #include <ITextSceneNode.h>
27 #include <IBillboardSceneNode.h>
28 #include "serialization.h" // For decompressZlib
29 #include "gamedef.h"
30 #include "clientobject.h"
31 #include "content_object.h"
32 #include "mesh.h"
33 #include "itemdef.h"
34 #include "tool.h"
35 #include "content_cso.h"
36 #include "sound.h"
37 #include "nodedef.h"
38 #include "localplayer.h"
39 #include "util/numeric.h" // For IntervalLimiter
40 #include "util/serialize.h"
41 #include "util/mathconstants.h"
42 #include "map.h"
43 #include "main.h" // g_settings
44 #include "camera.h" // CameraModes
45 #include <IMeshManipulator.h>
46 #include <IAnimatedMeshSceneNode.h>
47 #include <IBoneSceneNode.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         u8 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->getTexture("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         u8 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                 infostream<<"WARNING: "<<__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                 m_id(0),
547                 //
548                 m_smgr(NULL),
549                 m_irr(NULL),
550                 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
551                 m_meshnode(NULL),
552                 m_animated_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_bone_position(std::map<std::string, core::vector2d<v3f> >()),
568                 m_attachment_bone(""),
569                 m_attachment_position(v3f(0,0,0)),
570                 m_attachment_rotation(v3f(0,0,0)),
571                 m_attached_to_local(false),
572                 m_anim_frame(0),
573                 m_anim_num_frames(1),
574                 m_anim_framelength(0.2),
575                 m_anim_timer(0),
576                 m_reset_textures_timer(-1),
577                 m_visuals_expired(false),
578                 m_step_distance_counter(0),
579                 m_last_light(255),
580                 m_is_visible(false)
581 {
582         if(gamedef == NULL)
583                 ClientActiveObject::registerType(getType(), create);
584 }
585
586 bool GenericCAO::getCollisionBox(aabb3f *toset)
587 {
588         if (m_prop.physical)
589         {
590                 //update collision box
591                 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
592                 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
593
594                 toset->MinEdge += m_position;
595                 toset->MaxEdge += m_position;
596
597                 return true;
598         }
599
600         return false;
601 }
602
603 bool GenericCAO::collideWithObjects()
604 {
605         return m_prop.collideWithObjects;
606 }
607
608 void GenericCAO::initialize(const std::string &data)
609 {
610         infostream<<"GenericCAO: Got init data"<<std::endl;
611         std::istringstream is(data, std::ios::binary);
612         int num_messages = 0;
613         // version
614         u8 version = readU8(is);
615         // check version
616         if(version == 1) // In PROTOCOL_VERSION 14
617         {
618                 m_name = deSerializeString(is);
619                 m_is_player = readU8(is);
620                 m_id = readS16(is);
621                 m_position = readV3F1000(is);
622                 m_yaw = readF1000(is);
623                 m_hp = readS16(is);
624                 num_messages = readU8(is);
625         }
626         else if(version == 0) // In PROTOCOL_VERSION 13
627         {
628                 m_name = deSerializeString(is);
629                 m_is_player = readU8(is);
630                 m_position = readV3F1000(is);
631                 m_yaw = readF1000(is);
632                 m_hp = readS16(is);
633                 num_messages = readU8(is);
634         }
635         else
636         {
637                 errorstream<<"GenericCAO: Unsupported init data version"
638                                 <<std::endl;
639                 return;
640         }
641
642         for(int i=0; i<num_messages; i++)
643         {
644                 std::string message = deSerializeLongString(is);
645                 processMessage(message);
646         }
647
648         pos_translator.init(m_position);
649         updateNodePos();
650
651         if(m_is_player)
652         {
653                 Player *player = m_env->getPlayer(m_name.c_str());
654                 if(player && player->isLocal())
655                 {
656                         m_is_local_player = true;
657                         m_is_visible = false;
658                         LocalPlayer* localplayer = dynamic_cast<LocalPlayer*>(player);
659
660                         assert( localplayer != NULL );
661                         localplayer->setCAO(this);
662                 }
663                 m_env->addPlayerName(m_name.c_str());
664         }
665 }
666
667 GenericCAO::~GenericCAO()
668 {
669         if(m_is_player)
670         {
671                 m_env->removePlayerName(m_name.c_str());
672         }
673 }
674
675 core::aabbox3d<f32>* GenericCAO::getSelectionBox()
676 {
677         if(!m_prop.is_visible || !m_is_visible || m_is_local_player || getParent() != NULL)
678                 return NULL;
679         return &m_selection_box;
680 }
681
682 v3f GenericCAO::getPosition()
683 {
684         if(getParent() != NULL)
685         {
686                 if(m_meshnode)
687                         return m_meshnode->getAbsolutePosition();
688                 if(m_animated_meshnode)
689                         return m_animated_meshnode->getAbsolutePosition();
690                 if(m_spritenode)
691                         return m_spritenode->getAbsolutePosition();
692                 return m_position;
693         }
694         return pos_translator.vect_show;
695 }
696
697 scene::IMeshSceneNode* GenericCAO::getMeshSceneNode()
698 {
699         if(m_meshnode)
700                 return m_meshnode;
701         return NULL;
702 }
703
704 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
705 {
706         if(m_animated_meshnode)
707                 return m_animated_meshnode;
708         return NULL;
709 }
710
711 scene::IBillboardSceneNode* GenericCAO::getSpriteSceneNode()
712 {
713         if(m_spritenode)
714                 return m_spritenode;
715         return NULL;
716 }
717
718 void GenericCAO::setAttachments()
719 {
720         updateAttachments();
721 }
722
723 ClientActiveObject* GenericCAO::getParent()
724 {
725         ClientActiveObject *obj = NULL;
726
727         u16 attached_id = m_env->m_attachements[getId()];
728
729         if ((attached_id != 0) &&
730                         (attached_id != getId())) {
731                 obj = m_env->getActiveObject(attached_id);
732         }
733         return obj;
734 }
735
736 void GenericCAO::removeFromScene(bool permanent)
737 {
738         if(permanent) // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
739         {
740                 for(std::vector<u16>::iterator ci = m_children.begin();
741                                                 ci != m_children.end(); ci++)
742                 {
743                         if (m_env->m_attachements[*ci] == getId()) {
744                                 m_env->m_attachements[*ci] = 0;
745                         }
746                 }
747
748                 m_env->m_attachements[getId()] = 0;
749         }
750
751         if(m_meshnode)
752         {
753                 m_meshnode->remove();
754                 m_meshnode = NULL;
755         }
756         if(m_animated_meshnode)
757         {
758                 m_animated_meshnode->remove();
759                 m_animated_meshnode->drop();
760                 m_animated_meshnode = NULL;
761         }
762         if(m_spritenode)
763         {
764                 m_spritenode->remove();
765                 m_spritenode = NULL;
766         }
767 }
768
769 void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
770                 IrrlichtDevice *irr)
771 {
772         m_smgr = smgr;
773         m_irr = irr;
774
775         if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
776                 return;
777
778         m_visuals_expired = false;
779
780         if(!m_prop.is_visible)
781                 return;
782
783         //video::IVideoDriver* driver = smgr->getVideoDriver();
784
785         if(m_prop.visual == "sprite")
786         {
787                 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
788                 m_spritenode = smgr->addBillboardSceneNode(
789                                 NULL, v2f(1, 1), v3f(0,0,0), -1);
790                 m_spritenode->setMaterialTexture(0,
791                                 tsrc->getTexture("unknown_node.png"));
792                 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
793                 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
794                 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
795                 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
796                 u8 li = m_last_light;
797                 m_spritenode->setColor(video::SColor(255,li,li,li));
798                 m_spritenode->setSize(m_prop.visual_size*BS);
799                 {
800                         const float txs = 1.0 / 1;
801                         const float tys = 1.0 / 1;
802                         setBillboardTextureMatrix(m_spritenode,
803                                         txs, tys, 0, 0);
804                 }
805         }
806         else if(m_prop.visual == "upright_sprite") {
807                 scene::SMesh *mesh = new scene::SMesh();
808                 double dx = BS*m_prop.visual_size.X/2;
809                 double dy = BS*m_prop.visual_size.Y/2;
810                 { // Front
811                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
812                 u8 li = m_last_light;
813                 video::SColor c(255,li,li,li);
814                 video::S3DVertex vertices[4] =
815                 {
816                         video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
817                         video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
818                         video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
819                         video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
820                 };
821                 u16 indices[] = {0,1,2,2,3,0};
822                 buf->append(vertices, 4, indices, 6);
823                 // Set material
824                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
825                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
826                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
827                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
828                 // Add to mesh
829                 mesh->addMeshBuffer(buf);
830                 buf->drop();
831                 }
832                 { // Back
833                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
834                 u8 li = m_last_light;
835                 video::SColor c(255,li,li,li);
836                 video::S3DVertex vertices[4] =
837                 {
838                         video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
839                         video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
840                         video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
841                         video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
842                 };
843                 u16 indices[] = {0,1,2,2,3,0};
844                 buf->append(vertices, 4, indices, 6);
845                 // Set material
846                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
847                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
848                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
849                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
850                 // Add to mesh
851                 mesh->addMeshBuffer(buf);
852                 buf->drop();
853                 }
854                 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
855                 mesh->drop();
856                 // Set it to use the materials of the meshbuffers directly.
857                 // This is needed for changing the texture in the future
858                 m_meshnode->setReadOnlyMaterials(true);
859         }
860         else if(m_prop.visual == "cube") {
861                 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
862                 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
863                 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
864                 mesh->drop();
865                 
866                 m_meshnode->setScale(v3f(m_prop.visual_size.X,
867                                 m_prop.visual_size.Y,
868                                 m_prop.visual_size.X));
869                 u8 li = m_last_light;
870                 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
871
872                 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
873                 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
874                 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
875                 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
876         }
877         else if(m_prop.visual == "mesh") {
878                 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
879                 scene::IAnimatedMesh *mesh = m_gamedef->getMesh(m_prop.mesh);
880                 if(mesh)
881                 {
882                         m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
883                         m_animated_meshnode->grab();
884                         mesh->drop(); // The scene node took hold of it
885                         m_animated_meshnode->animateJoints(); // Needed for some animations
886                         m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
887                                         m_prop.visual_size.Y,
888                                         m_prop.visual_size.X));
889                         u8 li = m_last_light;
890                         setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
891
892                         m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
893                         m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
894                         m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
895                         m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
896                 }
897                 else
898                         errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
899         }
900         else if(m_prop.visual == "wielditem") {
901                 infostream<<"GenericCAO::addToScene(): node"<<std::endl;
902                 infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
903                 if(m_prop.textures.size() >= 1){
904                         infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
905                         IItemDefManager *idef = m_gamedef->idef();
906                         ItemStack item(m_prop.textures[0], 1, 0, "", idef);
907                         scene::IMesh *item_mesh = idef->getWieldMesh(item.getDefinition(idef).name, m_gamedef);
908
909                         // Copy mesh to be able to set unique vertex colors
910                         scene::IMeshManipulator *manip =
911                                         irr->getVideoDriver()->getMeshManipulator();
912                         scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
913
914                         m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
915                         mesh->drop();
916                         
917                         m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
918                                         m_prop.visual_size.Y/2,
919                                         m_prop.visual_size.X/2));
920                         u8 li = m_last_light;
921                         setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
922                 }
923         } else {
924                 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
925                                 <<"\" not supported"<<std::endl;
926         }
927         updateTextures("");
928
929         scene::ISceneNode *node = NULL;
930         if(m_spritenode)
931                 node = m_spritenode;
932         else if(m_animated_meshnode)
933                 node = m_animated_meshnode;
934         else if(m_meshnode)
935                 node = m_meshnode;
936         if(node && m_is_player && !m_is_local_player){
937                 // Add a text node for showing the name
938                 gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
939                 std::wstring wname = narrow_to_wide(m_name);
940                 m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
941                                 wname.c_str(), video::SColor(255,255,255,255), node);
942                 m_textnode->setPosition(v3f(0, BS*1.1, 0));
943         }
944
945         updateNodePos();
946         updateAnimation();
947         updateBonePosition();
948         updateAttachments();
949 }
950                 
951 void GenericCAO::updateLight(u8 light_at_pos)
952 {
953         u8 li = decode_light(light_at_pos);
954         if(li != m_last_light)
955         {
956                 m_last_light = li;
957                 video::SColor color(255,li,li,li);
958                 if(m_meshnode)
959                         setMeshColor(m_meshnode->getMesh(), color);
960                 if(m_animated_meshnode)
961                         setMeshColor(m_animated_meshnode->getMesh(), color);
962                 if(m_spritenode)
963                         m_spritenode->setColor(color);
964         }
965 }
966
967 v3s16 GenericCAO::getLightPosition()
968 {
969         return floatToInt(m_position, BS);
970 }
971
972 void GenericCAO::updateNodePos()
973 {
974         if(getParent() != NULL)
975                 return;
976
977         v3s16 camera_offset = m_env->getCameraOffset();
978         if(m_meshnode)
979         {
980                 m_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
981                 v3f rot = m_meshnode->getRotation();
982                 rot.Y = -m_yaw;
983                 m_meshnode->setRotation(rot);
984         }
985         if(m_animated_meshnode)
986         {
987                 m_animated_meshnode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
988                 v3f rot = m_animated_meshnode->getRotation();
989                 rot.Y = -m_yaw;
990                 m_animated_meshnode->setRotation(rot);
991         }
992         if(m_spritenode)
993         {
994                 m_spritenode->setPosition(pos_translator.vect_show-intToFloat(camera_offset, BS));
995         }
996 }
997         
998 void GenericCAO::step(float dtime, ClientEnvironment *env)
999 {
1000         // Handel model of local player instantly to prevent lags
1001         if(m_is_local_player)
1002         {
1003                 LocalPlayer *player = m_env->getLocalPlayer();
1004
1005                 if (m_is_visible)
1006                 {
1007                         int old_anim = player->last_animation;
1008                         float old_anim_speed = player->last_animation_speed;
1009                         m_position = player->getPosition() + v3f(0,BS,0);
1010                         m_velocity = v3f(0,0,0);
1011                         m_acceleration = v3f(0,0,0);
1012                         pos_translator.vect_show = m_position;
1013                         m_yaw = player->getYaw();
1014                         PlayerControl controls = player->getPlayerControl();
1015
1016                         bool walking = false;
1017                         if(controls.up || controls.down || controls.left || controls.right)
1018                                 walking = true;
1019
1020                         f32 new_speed = player->local_animation_speed;
1021                         v2s32 new_anim = v2s32(0,0);
1022                         bool allow_update = false;
1023
1024                         // increase speed if using fast or flying fast
1025                         if((g_settings->getBool("fast_move") &&
1026                                         m_gamedef->checkLocalPrivilege("fast")) &&
1027                                         (controls.aux1 ||
1028                                         (!player->touching_ground &&
1029                                         g_settings->getBool("free_move") &&
1030                                         m_gamedef->checkLocalPrivilege("fly"))))
1031                                         new_speed *= 1.5;
1032                         // slowdown speed if sneeking
1033                         if(controls.sneak && walking)
1034                                 new_speed /= 2;
1035
1036                         if(walking && (controls.LMB || controls.RMB))
1037                         {
1038                                 new_anim = player->local_animations[3];
1039                                 player->last_animation = WD_ANIM;
1040                         } else if(walking) {
1041                                 new_anim = player->local_animations[1];
1042                                 player->last_animation = WALK_ANIM;
1043                         } else if(controls.LMB || controls.RMB) {
1044                                 new_anim = player->local_animations[2];
1045                                 player->last_animation = DIG_ANIM;
1046                         }
1047
1048                         // Apply animations if input detected and not attached
1049                         // or set idle animation
1050                         if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached)
1051                         {
1052                                 allow_update = true;
1053                                 m_animation_range = new_anim;
1054                                 m_animation_speed = new_speed;
1055                                 player->last_animation_speed = m_animation_speed;
1056                         } else {
1057                                 player->last_animation = NO_ANIM;
1058
1059                                 if (old_anim != NO_ANIM)
1060                                 {
1061                                         m_animation_range = player->local_animations[0];
1062                                         updateAnimation();
1063                                 }
1064                         }
1065
1066                         // Update local player animations
1067                         if ((player->last_animation != old_anim ||
1068                                 m_animation_speed != old_anim_speed) &&
1069                                 player->last_animation != NO_ANIM && allow_update)
1070                                         updateAnimation();
1071
1072                 }
1073         }
1074
1075         if(m_visuals_expired && m_smgr && m_irr){
1076                 m_visuals_expired = false;
1077
1078                 // Attachments, part 1: All attached objects must be unparented first,
1079                 // or Irrlicht causes a segmentation fault
1080                 for(std::vector<u16>::iterator ci = m_children.begin();
1081                                 ci != m_children.end();)
1082                 {
1083                         if (m_env->m_attachements[*ci] != getId()) {
1084                                 ci = m_children.erase(ci);
1085                                 continue;
1086                         }
1087                         ClientActiveObject *obj = m_env->getActiveObject(*ci);
1088                         if(obj)
1089                         {
1090                                 scene::IMeshSceneNode *m_child_meshnode
1091                                                 = obj->getMeshSceneNode();
1092                                 scene::IAnimatedMeshSceneNode *m_child_animated_meshnode
1093                                                 = obj->getAnimatedMeshSceneNode();
1094                                 scene::IBillboardSceneNode *m_child_spritenode
1095                                                 = obj->getSpriteSceneNode();
1096                                 if(m_child_meshnode)
1097                                         m_child_meshnode->setParent(m_smgr->getRootSceneNode());
1098                                 if(m_child_animated_meshnode)
1099                                         m_child_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1100                                 if(m_child_spritenode)
1101                                         m_child_spritenode->setParent(m_smgr->getRootSceneNode());
1102                         }
1103                         ++ci;
1104                 }
1105
1106                 removeFromScene(false);
1107                 addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
1108
1109                 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
1110                 for(std::vector<u16>::iterator ci = m_children.begin();
1111                                                 ci != m_children.end(); ci++)
1112                 {
1113                                 // Get the object of the child
1114                                 ClientActiveObject *obj = m_env->getActiveObject(*ci);
1115                                 if(obj)
1116                                         obj->setAttachments();
1117                 }
1118         }
1119
1120         // Make sure m_is_visible is always applied
1121         if(m_meshnode)
1122                 m_meshnode->setVisible(m_is_visible);
1123         if(m_animated_meshnode)
1124                 m_animated_meshnode->setVisible(m_is_visible);
1125         if(m_spritenode)
1126                 m_spritenode->setVisible(m_is_visible);
1127         if(m_textnode)
1128                 m_textnode->setVisible(m_is_visible);
1129
1130         if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
1131         {
1132                 // Set these for later
1133                 m_position = getPosition();
1134                 m_velocity = v3f(0,0,0);
1135                 m_acceleration = v3f(0,0,0);
1136                 pos_translator.vect_show = m_position;
1137
1138                 if(m_is_local_player) // Update local player attachment position
1139                 {
1140                         LocalPlayer *player = m_env->getLocalPlayer();
1141                         player->overridePosition = getParent()->getPosition();
1142                         m_env->getLocalPlayer()->parent = getParent();
1143                 }
1144         } else {
1145                 v3f lastpos = pos_translator.vect_show;
1146
1147                 if(m_prop.physical)
1148                 {
1149                         core::aabbox3d<f32> box = m_prop.collisionbox;
1150                         box.MinEdge *= BS;
1151                         box.MaxEdge *= BS;
1152                         collisionMoveResult moveresult;
1153                         f32 pos_max_d = BS*0.125; // Distance per iteration
1154                         v3f p_pos = m_position;
1155                         v3f p_velocity = m_velocity;
1156                         v3f p_acceleration = m_acceleration;
1157                         moveresult = collisionMoveSimple(env,env->getGameDef(),
1158                                         pos_max_d, box, m_prop.stepheight, dtime,
1159                                         p_pos, p_velocity, p_acceleration,
1160                                         this, m_prop.collideWithObjects);
1161                         // Apply results
1162                         m_position = p_pos;
1163                         m_velocity = p_velocity;
1164                         m_acceleration = p_acceleration;
1165
1166                         bool is_end_position = moveresult.collides;
1167                         pos_translator.update(m_position, is_end_position, dtime);
1168                         pos_translator.translate(dtime);
1169                         updateNodePos();
1170                 } else {
1171                         m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
1172                         m_velocity += dtime * m_acceleration;
1173                         pos_translator.update(m_position, pos_translator.aim_is_end,
1174                                         pos_translator.anim_time);
1175                         pos_translator.translate(dtime);
1176                         updateNodePos();
1177                 }
1178
1179                 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
1180                 m_step_distance_counter += moved;
1181                 if(m_step_distance_counter > 1.5*BS)
1182                 {
1183                         m_step_distance_counter = 0;
1184                         if(!m_is_local_player && m_prop.makes_footstep_sound)
1185                         {
1186                                 INodeDefManager *ndef = m_gamedef->ndef();
1187                                 v3s16 p = floatToInt(getPosition() + v3f(0,
1188                                                 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
1189                                 MapNode n = m_env->getMap().getNodeNoEx(p);
1190                                 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
1191                                 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
1192                         }
1193                 }
1194         }
1195
1196         m_anim_timer += dtime;
1197         if(m_anim_timer >= m_anim_framelength)
1198         {
1199                 m_anim_timer -= m_anim_framelength;
1200                 m_anim_frame++;
1201                 if(m_anim_frame >= m_anim_num_frames)
1202                         m_anim_frame = 0;
1203         }
1204
1205         updateTexturePos();
1206
1207         if(m_reset_textures_timer >= 0)
1208         {
1209                 m_reset_textures_timer -= dtime;
1210                 if(m_reset_textures_timer <= 0){
1211                         m_reset_textures_timer = -1;
1212                         updateTextures("");
1213                 }
1214         }
1215         if(getParent() == NULL && fabs(m_prop.automatic_rotate) > 0.001)
1216         {
1217                 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1218                 updateNodePos();
1219         }
1220
1221         if (getParent() == NULL && m_prop.automatic_face_movement_dir &&
1222                         (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
1223         {
1224                 m_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
1225                                 + m_prop.automatic_face_movement_dir_offset;
1226                 updateNodePos();
1227         }
1228 }
1229
1230 void GenericCAO::updateTexturePos()
1231 {
1232         if(m_spritenode)
1233         {
1234                 scene::ICameraSceneNode* camera =
1235                                 m_spritenode->getSceneManager()->getActiveCamera();
1236                 if(!camera)
1237                         return;
1238                 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1239                                 - camera->getAbsolutePosition();
1240                 cam_to_entity.normalize();
1241
1242                 int row = m_tx_basepos.Y;
1243                 int col = m_tx_basepos.X;
1244
1245                 if(m_tx_select_horiz_by_yawpitch)
1246                 {
1247                         if(cam_to_entity.Y > 0.75)
1248                                 col += 5;
1249                         else if(cam_to_entity.Y < -0.75)
1250                                 col += 4;
1251                         else{
1252                                 float mob_dir =
1253                                                 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1254                                 float dir = mob_dir - m_yaw;
1255                                 dir = wrapDegrees_180(dir);
1256                                 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1257                                 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1258                                         col += 2;
1259                                 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1260                                         col += 3;
1261                                 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1262                                         col += 0;
1263                                 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1264                                         col += 1;
1265                                 else
1266                                         col += 4;
1267                         }
1268                 }
1269
1270                 // Animation goes downwards
1271                 row += m_anim_frame;
1272
1273                 float txs = m_tx_size.X;
1274                 float tys = m_tx_size.Y;
1275                 setBillboardTextureMatrix(m_spritenode,
1276                                 txs, tys, col, row);
1277         }
1278 }
1279
1280 void GenericCAO::updateTextures(const std::string &mod)
1281 {
1282         ITextureSource *tsrc = m_gamedef->tsrc();
1283
1284         bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
1285         bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
1286         bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
1287
1288         if(m_spritenode)
1289         {
1290                 if(m_prop.visual == "sprite")
1291                 {
1292                         std::string texturestring = "unknown_node.png";
1293                         if(m_prop.textures.size() >= 1)
1294                                 texturestring = m_prop.textures[0];
1295                         texturestring += mod;
1296                         m_spritenode->setMaterialTexture(0,
1297                                         tsrc->getTexture(texturestring));
1298
1299                         // This allows setting per-material colors. However, until a real lighting
1300                         // system is added, the code below will have no effect. Once MineTest
1301                         // has directional lighting, it should work automatically.
1302                         if(m_prop.colors.size() >= 1)
1303                         {
1304                                 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1305                                 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1306                                 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1307                         }
1308
1309                         m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1310                         m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1311                         m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1312                 }
1313         }
1314         if(m_animated_meshnode)
1315         {
1316                 if(m_prop.visual == "mesh")
1317                 {
1318                         for (u32 i = 0; i < m_prop.textures.size() &&
1319                                         i < m_animated_meshnode->getMaterialCount(); ++i)
1320                         {
1321                                 std::string texturestring = m_prop.textures[i];
1322                                 if(texturestring == "")
1323                                         continue; // Empty texture string means don't modify that material
1324                                 texturestring += mod;
1325                                 video::ITexture* texture = tsrc->getTexture(texturestring);
1326                                 if(!texture)
1327                                 {
1328                                         errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1329                                         continue;
1330                                 }
1331
1332                                 // Set material flags and texture
1333                                 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1334                                 material.TextureLayer[0].Texture = texture;
1335                                 material.setFlag(video::EMF_LIGHTING, false);
1336                                 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1337
1338                                 m_animated_meshnode->getMaterial(i)
1339                                                 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1340                                 m_animated_meshnode->getMaterial(i)
1341                                                 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1342                                 m_animated_meshnode->getMaterial(i)
1343                                                 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1344                         }
1345                         for (u32 i = 0; i < m_prop.colors.size() &&
1346                         i < m_animated_meshnode->getMaterialCount(); ++i)
1347                         {
1348                                 // This allows setting per-material colors. However, until a real lighting
1349                                 // system is added, the code below will have no effect. Once MineTest
1350                                 // has directional lighting, it should work automatically.
1351                                 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1352                                 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1353                                 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1354                         }
1355                 }
1356         }
1357         if(m_meshnode)
1358         {
1359                 if(m_prop.visual == "cube")
1360                 {
1361                         for (u32 i = 0; i < 6; ++i)
1362                         {
1363                                 std::string texturestring = "unknown_node.png";
1364                                 if(m_prop.textures.size() > i)
1365                                         texturestring = m_prop.textures[i];
1366                                 texturestring += mod;
1367
1368
1369                                 // Set material flags and texture
1370                                 video::SMaterial& material = m_meshnode->getMaterial(i);
1371                                 material.setFlag(video::EMF_LIGHTING, false);
1372                                 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1373                                 material.setTexture(0,
1374                                                 tsrc->getTexture(texturestring));
1375                                 material.getTextureMatrix(0).makeIdentity();
1376
1377                                 // This allows setting per-material colors. However, until a real lighting
1378                                 // system is added, the code below will have no effect. Once MineTest
1379                                 // has directional lighting, it should work automatically.
1380                                 if(m_prop.colors.size() > i)
1381                                 {
1382                                         m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1383                                         m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1384                                         m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1385                                 }
1386
1387                                 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1388                                 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1389                                 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1390                         }
1391                 }
1392                 else if(m_prop.visual == "upright_sprite")
1393                 {
1394                         scene::IMesh *mesh = m_meshnode->getMesh();
1395                         {
1396                                 std::string tname = "unknown_object.png";
1397                                 if(m_prop.textures.size() >= 1)
1398                                         tname = m_prop.textures[0];
1399                                 tname += mod;
1400                                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1401                                 buf->getMaterial().setTexture(0,
1402                                                 tsrc->getTexture(tname));
1403
1404                                 // This allows setting per-material colors. However, until a real lighting
1405                                 // system is added, the code below will have no effect. Once MineTest
1406                                 // has directional lighting, it should work automatically.
1407                                 if(m_prop.colors.size() >= 1)
1408                                 {
1409                                         buf->getMaterial().AmbientColor = m_prop.colors[0];
1410                                         buf->getMaterial().DiffuseColor = m_prop.colors[0];
1411                                         buf->getMaterial().SpecularColor = m_prop.colors[0];
1412                                 }
1413
1414                                 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1415                                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1416                                 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1417                         }
1418                         {
1419                                 std::string tname = "unknown_object.png";
1420                                 if(m_prop.textures.size() >= 2)
1421                                         tname = m_prop.textures[1];
1422                                 else if(m_prop.textures.size() >= 1)
1423                                         tname = m_prop.textures[0];
1424                                 tname += mod;
1425                                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1426                                 buf->getMaterial().setTexture(0,
1427                                                 tsrc->getTexture(tname));
1428
1429                                 // This allows setting per-material colors. However, until a real lighting
1430                                 // system is added, the code below will have no effect. Once MineTest
1431                                 // has directional lighting, it should work automatically.
1432                                 if(m_prop.colors.size() >= 2)
1433                                 {
1434                                         buf->getMaterial().AmbientColor = m_prop.colors[1];
1435                                         buf->getMaterial().DiffuseColor = m_prop.colors[1];
1436                                         buf->getMaterial().SpecularColor = m_prop.colors[1];
1437                                 }
1438                                 else if(m_prop.colors.size() >= 1)
1439                                 {
1440                                         buf->getMaterial().AmbientColor = m_prop.colors[0];
1441                                         buf->getMaterial().DiffuseColor = m_prop.colors[0];
1442                                         buf->getMaterial().SpecularColor = m_prop.colors[0];
1443                                 }
1444
1445                                 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1446                                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1447                                 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1448                         }
1449                 }
1450         }
1451 }
1452
1453 void GenericCAO::updateAnimation()
1454 {
1455         if(m_animated_meshnode == NULL)
1456                 return;
1457         m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1458         m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1459         m_animated_meshnode->setTransitionTime(m_animation_blend);
1460 }
1461
1462 void GenericCAO::updateBonePosition()
1463 {
1464         if(!m_bone_position.size() || m_animated_meshnode == NULL)
1465                 return;
1466
1467         m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1468         for(std::map<std::string,
1469                         core::vector2d<v3f> >::const_iterator ii = m_bone_position.begin();
1470                         ii != m_bone_position.end(); ++ii)
1471         {
1472                 std::string bone_name = (*ii).first;
1473                 v3f bone_pos = (*ii).second.X;
1474                 v3f bone_rot = (*ii).second.Y;
1475                 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1476                 if(bone)
1477                 {
1478                         bone->setPosition(bone_pos);
1479                         bone->setRotation(bone_rot);
1480                 }
1481         }
1482 }
1483         
1484 void GenericCAO::updateAttachments()
1485 {
1486
1487         // localplayer itself can't be attached to localplayer
1488         if (!m_is_local_player)
1489         {
1490                 m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1491                 // Objects attached to the local player should always be hidden
1492                 m_is_visible = !m_attached_to_local;
1493         }
1494
1495         if(getParent() == NULL || m_attached_to_local) // Detach or don't attach
1496         {
1497                 if(m_meshnode)
1498                 {
1499                         v3f old_position = m_meshnode->getAbsolutePosition();
1500                         v3f old_rotation = m_meshnode->getRotation();
1501                         m_meshnode->setParent(m_smgr->getRootSceneNode());
1502                         m_meshnode->setPosition(old_position);
1503                         m_meshnode->setRotation(old_rotation);
1504                         m_meshnode->updateAbsolutePosition();
1505                 }
1506                 if(m_animated_meshnode)
1507                 {
1508                         v3f old_position = m_animated_meshnode->getAbsolutePosition();
1509                         v3f old_rotation = m_animated_meshnode->getRotation();
1510                         m_animated_meshnode->setParent(m_smgr->getRootSceneNode());
1511                         m_animated_meshnode->setPosition(old_position);
1512                         m_animated_meshnode->setRotation(old_rotation);
1513                         m_animated_meshnode->updateAbsolutePosition();
1514                 }
1515                 if(m_spritenode)
1516                 {
1517                         v3f old_position = m_spritenode->getAbsolutePosition();
1518                         v3f old_rotation = m_spritenode->getRotation();
1519                         m_spritenode->setParent(m_smgr->getRootSceneNode());
1520                         m_spritenode->setPosition(old_position);
1521                         m_spritenode->setRotation(old_rotation);
1522                         m_spritenode->updateAbsolutePosition();
1523                 }
1524                 if(m_is_local_player)
1525                 {
1526                         LocalPlayer *player = m_env->getLocalPlayer();
1527                         player->isAttached = false;
1528                 }
1529         }
1530         else // Attach
1531         {
1532                 scene::IMeshSceneNode *parent_mesh = NULL;
1533                 if(getParent()->getMeshSceneNode())
1534                         parent_mesh = getParent()->getMeshSceneNode();
1535                 scene::IAnimatedMeshSceneNode *parent_animated_mesh = NULL;
1536                 if(getParent()->getAnimatedMeshSceneNode())
1537                         parent_animated_mesh = getParent()->getAnimatedMeshSceneNode();
1538                 scene::IBillboardSceneNode *parent_sprite = NULL;
1539                 if(getParent()->getSpriteSceneNode())
1540                         parent_sprite = getParent()->getSpriteSceneNode();
1541
1542                 scene::IBoneSceneNode *parent_bone = NULL;
1543                 if(parent_animated_mesh && m_attachment_bone != "")
1544                 {
1545                         parent_bone =
1546                                         parent_animated_mesh->getJointNode(m_attachment_bone.c_str());
1547                 }
1548                 // The spaghetti code below makes sure attaching works if either the
1549                 // parent or child is a spritenode, meshnode, or animatedmeshnode
1550                 // TODO: Perhaps use polymorphism here to save code duplication
1551                 if(m_meshnode)
1552                 {
1553                         if(parent_bone)
1554                         {
1555                                 m_meshnode->setParent(parent_bone);
1556                                 m_meshnode->setPosition(m_attachment_position);
1557                                 m_meshnode->setRotation(m_attachment_rotation);
1558                                 m_meshnode->updateAbsolutePosition();
1559                         }
1560                         else
1561                         {
1562                                 if(parent_mesh)
1563                                 {
1564                                         m_meshnode->setParent(parent_mesh);
1565                                         m_meshnode->setPosition(m_attachment_position);
1566                                         m_meshnode->setRotation(m_attachment_rotation);
1567                                         m_meshnode->updateAbsolutePosition();
1568                                 }
1569                                 else if(parent_animated_mesh) {
1570                                         m_meshnode->setParent(parent_animated_mesh);
1571                                         m_meshnode->setPosition(m_attachment_position);
1572                                         m_meshnode->setRotation(m_attachment_rotation);
1573                                         m_meshnode->updateAbsolutePosition();
1574                                 }
1575                                 else if(parent_sprite) {
1576                                         m_meshnode->setParent(parent_sprite);
1577                                         m_meshnode->setPosition(m_attachment_position);
1578                                         m_meshnode->setRotation(m_attachment_rotation);
1579                                         m_meshnode->updateAbsolutePosition();
1580                                 }
1581                         }
1582                 }
1583                 if(m_animated_meshnode)
1584                 {
1585                         if(parent_bone)
1586                         {
1587                                 m_animated_meshnode->setParent(parent_bone);
1588                                 m_animated_meshnode->setPosition(m_attachment_position);
1589                                 m_animated_meshnode->setRotation(m_attachment_rotation);
1590                                 m_animated_meshnode->updateAbsolutePosition();
1591                         }
1592                         else
1593                         {
1594                                 if(parent_mesh)
1595                                 {
1596                                         m_animated_meshnode->setParent(parent_mesh);
1597                                         m_animated_meshnode->setPosition(m_attachment_position);
1598                                         m_animated_meshnode->setRotation(m_attachment_rotation);
1599                                         m_animated_meshnode->updateAbsolutePosition();
1600                                 } else if(parent_animated_mesh) {
1601                                         m_animated_meshnode->setParent(parent_animated_mesh);
1602                                         m_animated_meshnode->setPosition(m_attachment_position);
1603                                         m_animated_meshnode->setRotation(m_attachment_rotation);
1604                                         m_animated_meshnode->updateAbsolutePosition();
1605                                 } else if(parent_sprite) {
1606                                         m_animated_meshnode->setParent(parent_sprite);
1607                                         m_animated_meshnode->setPosition(m_attachment_position);
1608                                         m_animated_meshnode->setRotation(m_attachment_rotation);
1609                                         m_animated_meshnode->updateAbsolutePosition();
1610                                 }
1611                         }
1612                 }
1613                 if(m_spritenode)
1614                 {
1615                         if(parent_bone)
1616                         {
1617                                 m_spritenode->setParent(parent_bone);
1618                                 m_spritenode->setPosition(m_attachment_position);
1619                                 m_spritenode->setRotation(m_attachment_rotation);
1620                                 m_spritenode->updateAbsolutePosition();
1621                         } else {
1622                                 if(parent_mesh)
1623                                 {
1624                                         m_spritenode->setParent(parent_mesh);
1625                                         m_spritenode->setPosition(m_attachment_position);
1626                                         m_spritenode->setRotation(m_attachment_rotation);
1627                                         m_spritenode->updateAbsolutePosition();
1628                                 }
1629                                 else if(parent_animated_mesh) {
1630                                         m_spritenode->setParent(parent_animated_mesh);
1631                                         m_spritenode->setPosition(m_attachment_position);
1632                                         m_spritenode->setRotation(m_attachment_rotation);
1633                                         m_spritenode->updateAbsolutePosition();
1634                                 }
1635                                 else if(parent_sprite) {
1636                                         m_spritenode->setParent(parent_sprite);
1637                                         m_spritenode->setPosition(m_attachment_position);
1638                                         m_spritenode->setRotation(m_attachment_rotation);
1639                                         m_spritenode->updateAbsolutePosition();
1640                                 }
1641                         }
1642                 }
1643                 if(m_is_local_player)
1644                 {
1645                         LocalPlayer *player = m_env->getLocalPlayer();
1646                         player->isAttached = true;
1647                 }
1648         }
1649 }
1650
1651 void GenericCAO::processMessage(const std::string &data)
1652 {
1653         //infostream<<"GenericCAO: Got message"<<std::endl;
1654         std::istringstream is(data, std::ios::binary);
1655         // command
1656         u8 cmd = readU8(is);
1657         if(cmd == GENERIC_CMD_SET_PROPERTIES)
1658         {
1659                 m_prop = gob_read_set_properties(is);
1660
1661                 m_selection_box = m_prop.collisionbox;
1662                 m_selection_box.MinEdge *= BS;
1663                 m_selection_box.MaxEdge *= BS;
1664
1665                 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1666                 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1667
1668                 if(!m_initial_tx_basepos_set){
1669                         m_initial_tx_basepos_set = true;
1670                         m_tx_basepos = m_prop.initial_sprite_basepos;
1671                 }
1672
1673                 expireVisuals();
1674         }
1675         else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1676         {
1677                 // Not sent by the server if this object is an attachment.
1678                 // We might however get here if the server notices the object being detached before the client.
1679                 m_position = readV3F1000(is);
1680                 m_velocity = readV3F1000(is);
1681                 m_acceleration = readV3F1000(is);
1682                 if(fabs(m_prop.automatic_rotate) < 0.001)
1683                         m_yaw = readF1000(is);
1684                 else
1685                         readF1000(is);
1686                 bool do_interpolate = readU8(is);
1687                 bool is_end_position = readU8(is);
1688                 float update_interval = readF1000(is);
1689
1690                 // Place us a bit higher if we're physical, to not sink into
1691                 // the ground due to sucky collision detection...
1692                 if(m_prop.physical)
1693                         m_position += v3f(0,0.002,0);
1694
1695                 if(getParent() != NULL) // Just in case
1696                         return;
1697
1698                 if(do_interpolate)
1699                 {
1700                         if(!m_prop.physical)
1701                                 pos_translator.update(m_position, is_end_position, update_interval);
1702                 } else {
1703                         pos_translator.init(m_position);
1704                 }
1705                 updateNodePos();
1706         }
1707         else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1708                 std::string mod = deSerializeString(is);
1709                 updateTextures(mod);
1710         }
1711         else if(cmd == GENERIC_CMD_SET_SPRITE) {
1712                 v2s16 p = readV2S16(is);
1713                 int num_frames = readU16(is);
1714                 float framelength = readF1000(is);
1715                 bool select_horiz_by_yawpitch = readU8(is);
1716
1717                 m_tx_basepos = p;
1718                 m_anim_num_frames = num_frames;
1719                 m_anim_framelength = framelength;
1720                 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1721
1722                 updateTexturePos();
1723         }
1724         else if(cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1725                 float override_speed = readF1000(is);
1726                 float override_jump = readF1000(is);
1727                 float override_gravity = readF1000(is);
1728                 // these are sent inverted so we get true when the server sends nothing
1729                 bool sneak = !readU8(is);
1730                 bool sneak_glitch = !readU8(is);
1731
1732
1733                 if(m_is_local_player)
1734                 {
1735                         LocalPlayer *player = m_env->getLocalPlayer();
1736                         player->physics_override_speed = override_speed;
1737                         player->physics_override_jump = override_jump;
1738                         player->physics_override_gravity = override_gravity;
1739                         player->physics_override_sneak = sneak;
1740                         player->physics_override_sneak_glitch = sneak_glitch;
1741                 }
1742         }
1743         else if(cmd == GENERIC_CMD_SET_ANIMATION) {
1744                 // TODO: change frames send as v2s32 value
1745                 v2f range = readV2F1000(is);
1746                 if (!m_is_local_player) {
1747                         m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1748                         m_animation_speed = readF1000(is);
1749                         m_animation_blend = readF1000(is);
1750                         updateAnimation();
1751                 } else {
1752                         LocalPlayer *player = m_env->getLocalPlayer();
1753                         if(player->last_animation == NO_ANIM)
1754                         {
1755                                 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1756                                 m_animation_speed = readF1000(is);
1757                                 m_animation_blend = readF1000(is);
1758                         }
1759                         // update animation only if local animations present
1760                         // and received animation is unknown (except idle animation)
1761                         bool is_known = false;
1762                         for (int i = 1;i<4;i++)
1763                         {
1764                                 if(m_animation_range.Y == player->local_animations[i].Y)
1765                                         is_known = true;
1766                         }
1767                         if(!is_known ||
1768                                         (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1769                         {
1770                                         updateAnimation();
1771                         }
1772                 }
1773         }
1774         else if(cmd == GENERIC_CMD_SET_BONE_POSITION) {
1775                 std::string bone = deSerializeString(is);
1776                 v3f position = readV3F1000(is);
1777                 v3f rotation = readV3F1000(is);
1778                 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1779
1780                 updateBonePosition();
1781         }
1782         else if(cmd == GENERIC_CMD_SET_ATTACHMENT) {
1783                 m_env->m_attachements[getId()] = readS16(is);
1784                 m_children.push_back(m_env->m_attachements[getId()]);
1785                 m_attachment_bone = deSerializeString(is);
1786                 m_attachment_position = readV3F1000(is);
1787                 m_attachment_rotation = readV3F1000(is);
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         }
1830 }
1831         
1832 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1833                 float time_from_last_punch)
1834 {
1835         assert(punchitem);
1836         const ToolCapabilities *toolcap =
1837                         &punchitem->getToolCapabilities(m_gamedef->idef());
1838         PunchDamageResult result = getPunchDamage(
1839                         m_armor_groups,
1840                         toolcap,
1841                         punchitem,
1842                         time_from_last_punch);
1843
1844         if(result.did_punch && result.damage != 0)
1845         {
1846                 if(result.damage < m_hp)
1847                 {
1848                         m_hp -= result.damage;
1849                 } else {
1850                         m_hp = 0;
1851                         // TODO: Execute defined fast response
1852                         // As there is no definition, make a smoke puff
1853                         ClientSimpleObject *simple = createSmokePuff(
1854                                         m_smgr, m_env, m_position,
1855                                         m_prop.visual_size * BS);
1856                         m_env->addSimpleObject(simple);
1857                 }
1858                 // TODO: Execute defined fast response
1859                 // Flashing shall suffice as there is no definition
1860                 m_reset_textures_timer = 0.05;
1861                 if(result.damage >= 2)
1862                         m_reset_textures_timer += 0.05 * result.damage;
1863                 updateTextures("^[brighten");
1864         }
1865
1866         return false;
1867 }
1868
1869 std::string GenericCAO::debugInfoText()
1870 {
1871         std::ostringstream os(std::ios::binary);
1872         os<<"GenericCAO hp="<<m_hp<<"\n";
1873         os<<"armor={";
1874         for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1875                         i != m_armor_groups.end(); i++)
1876         {
1877                 os<<i->first<<"="<<i->second<<", ";
1878         }
1879         os<<"}";
1880         return os.str();
1881 }
1882
1883 // Prototype
1884 GenericCAO proto_GenericCAO(NULL, NULL);