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