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