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