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