]> git.lizzy.rs Git - minetest.git/blob - src/content_cao.cpp
Framework for the attachment system, new object property which allows changing the...
[minetest.git] / src / content_cao.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 <IMeshManipulator.h>
44 #include <IAnimatedMeshSceneNode.h>
45 #include <IBoneSceneNode.h>
46
47 class Settings;
48 struct ToolCapabilities;
49
50 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
51
52 core::map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
53
54 /*
55         SmoothTranslator
56 */
57
58 struct SmoothTranslator
59 {
60         v3f vect_old;
61         v3f vect_show;
62         v3f vect_aim;
63         f32 anim_counter;
64         f32 anim_time;
65         f32 anim_time_counter;
66         bool aim_is_end;
67
68         SmoothTranslator():
69                 vect_old(0,0,0),
70                 vect_show(0,0,0),
71                 vect_aim(0,0,0),
72                 anim_counter(0),
73                 anim_time(0),
74                 anim_time_counter(0),
75                 aim_is_end(true)
76         {}
77
78         void init(v3f vect)
79         {
80                 vect_old = vect;
81                 vect_show = vect;
82                 vect_aim = vect;
83                 anim_counter = 0;
84                 anim_time = 0;
85                 anim_time_counter = 0;
86                 aim_is_end = true;
87         }
88
89         void sharpen()
90         {
91                 init(vect_show);
92         }
93
94         void update(v3f vect_new, bool is_end_position=false, float update_interval=-1)
95         {
96                 aim_is_end = is_end_position;
97                 vect_old = vect_show;
98                 vect_aim = vect_new;
99                 if(update_interval > 0){
100                         anim_time = update_interval;
101                 } else {
102                         if(anim_time < 0.001 || anim_time > 1.0)
103                                 anim_time = anim_time_counter;
104                         else
105                                 anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
106                 }
107                 anim_time_counter = 0;
108                 anim_counter = 0;
109         }
110
111         void translate(f32 dtime)
112         {
113                 anim_time_counter = anim_time_counter + dtime;
114                 anim_counter = anim_counter + dtime;
115                 v3f vect_move = vect_aim - vect_old;
116                 f32 moveratio = 1.0;
117                 if(anim_time > 0.001)
118                         moveratio = anim_time_counter / anim_time;
119                 // Move a bit less than should, to avoid oscillation
120                 moveratio = moveratio * 0.8;
121                 float move_end = 1.5;
122                 if(aim_is_end)
123                         move_end = 1.0;
124                 if(moveratio > move_end)
125                         moveratio = move_end;
126                 vect_show = vect_old + vect_move * moveratio;
127         }
128
129         bool is_moving()
130         {
131                 return ((anim_time_counter / anim_time) < 1.4);
132         }
133 };
134
135 /*
136         Other stuff
137 */
138
139 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
140                 float txs, float tys, int col, int row)
141 {
142         video::SMaterial& material = bill->getMaterial(0);
143         core::matrix4& matrix = material.getTextureMatrix(0);
144         matrix.setTextureTranslate(txs*col, tys*row);
145         matrix.setTextureScale(txs, tys);
146 }
147
148 /*
149         TestCAO
150 */
151
152 class TestCAO : public ClientActiveObject
153 {
154 public:
155         TestCAO(IGameDef *gamedef, ClientEnvironment *env);
156         virtual ~TestCAO();
157         
158         u8 getType() const
159         {
160                 return ACTIVEOBJECT_TYPE_TEST;
161         }
162         
163         static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
164
165         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
166                         IrrlichtDevice *irr);
167         void removeFromScene();
168         void updateLight(u8 light_at_pos);
169         v3s16 getLightPosition();
170         void updateNodePos();
171
172         void step(float dtime, ClientEnvironment *env);
173
174         void processMessage(const std::string &data);
175
176 private:
177         scene::IMeshSceneNode *m_node;
178         v3f m_position;
179 };
180
181 // Prototype
182 TestCAO proto_TestCAO(NULL, NULL);
183
184 TestCAO::TestCAO(IGameDef *gamedef, ClientEnvironment *env):
185         ClientActiveObject(0, gamedef, env),
186         m_node(NULL),
187         m_position(v3f(0,10*BS,0))
188 {
189         ClientActiveObject::registerType(getType(), create);
190 }
191
192 TestCAO::~TestCAO()
193 {
194 }
195
196 ClientActiveObject* TestCAO::create(IGameDef *gamedef, ClientEnvironment *env)
197 {
198         return new TestCAO(gamedef, env);
199 }
200
201 void TestCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
202                         IrrlichtDevice *irr)
203 {
204         if(m_node != NULL)
205                 return;
206         
207         //video::IVideoDriver* driver = smgr->getVideoDriver();
208         
209         scene::SMesh *mesh = new scene::SMesh();
210         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
211         video::SColor c(255,255,255,255);
212         video::S3DVertex vertices[4] =
213         {
214                 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
215                 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
216                 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
217                 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
218         };
219         u16 indices[] = {0,1,2,2,3,0};
220         buf->append(vertices, 4, indices, 6);
221         // Set material
222         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
223         buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
224         buf->getMaterial().setTexture(0, tsrc->getTextureRaw("rat.png"));
225         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
226         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
227         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
228         // Add to mesh
229         mesh->addMeshBuffer(buf);
230         buf->drop();
231         m_node = smgr->addMeshSceneNode(mesh, NULL);
232         mesh->drop();
233         updateNodePos();
234 }
235
236 void TestCAO::removeFromScene()
237 {
238         if(m_node == NULL)
239                 return;
240
241         m_node->remove();
242         m_node = NULL;
243 }
244
245 void TestCAO::updateLight(u8 light_at_pos)
246 {
247 }
248
249 v3s16 TestCAO::getLightPosition()
250 {
251         return floatToInt(m_position, BS);
252 }
253
254 void TestCAO::updateNodePos()
255 {
256         if(m_node == NULL)
257                 return;
258
259         m_node->setPosition(m_position);
260         //m_node->setRotation(v3f(0, 45, 0));
261 }
262
263 void TestCAO::step(float dtime, ClientEnvironment *env)
264 {
265         if(m_node)
266         {
267                 v3f rot = m_node->getRotation();
268                 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
269                 rot.Y += dtime * 180;
270                 m_node->setRotation(rot);
271         }
272 }
273
274 void TestCAO::processMessage(const std::string &data)
275 {
276         infostream<<"TestCAO: Got data: "<<data<<std::endl;
277         std::istringstream is(data, std::ios::binary);
278         u16 cmd;
279         is>>cmd;
280         if(cmd == 0)
281         {
282                 v3f newpos;
283                 is>>newpos.X;
284                 is>>newpos.Y;
285                 is>>newpos.Z;
286                 m_position = newpos;
287                 updateNodePos();
288         }
289 }
290
291 /*
292         ItemCAO
293 */
294
295 class ItemCAO : public ClientActiveObject
296 {
297 public:
298         ItemCAO(IGameDef *gamedef, ClientEnvironment *env);
299         virtual ~ItemCAO();
300         
301         u8 getType() const
302         {
303                 return ACTIVEOBJECT_TYPE_ITEM;
304         }
305         
306         static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env);
307
308         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
309                         IrrlichtDevice *irr);
310         void removeFromScene();
311         void updateLight(u8 light_at_pos);
312         v3s16 getLightPosition();
313         void updateNodePos();
314         void updateInfoText();
315         void updateTexture();
316
317         void step(float dtime, ClientEnvironment *env);
318
319         void processMessage(const std::string &data);
320
321         void initialize(const std::string &data);
322         
323         core::aabbox3d<f32>* getSelectionBox()
324                 {return &m_selection_box;}
325         v3f getPosition()
326                 {return m_position;}
327         
328         std::string infoText()
329                 {return m_infotext;}
330
331 private:
332         core::aabbox3d<f32> m_selection_box;
333         scene::IMeshSceneNode *m_node;
334         v3f m_position;
335         std::string m_itemstring;
336         std::string m_infotext;
337 };
338
339 #include "inventory.h"
340
341 // Prototype
342 ItemCAO proto_ItemCAO(NULL, NULL);
343
344 ItemCAO::ItemCAO(IGameDef *gamedef, ClientEnvironment *env):
345         ClientActiveObject(0, gamedef, env),
346         m_selection_box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.),
347         m_node(NULL),
348         m_position(v3f(0,10*BS,0))
349 {
350         if(!gamedef && !env)
351         {
352                 ClientActiveObject::registerType(getType(), create);
353         }
354 }
355
356 ItemCAO::~ItemCAO()
357 {
358 }
359
360 ClientActiveObject* ItemCAO::create(IGameDef *gamedef, ClientEnvironment *env)
361 {
362         return new ItemCAO(gamedef, env);
363 }
364
365 void ItemCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
366                         IrrlichtDevice *irr)
367 {
368         if(m_node != NULL)
369                 return;
370         
371         //video::IVideoDriver* driver = smgr->getVideoDriver();
372         
373         scene::SMesh *mesh = new scene::SMesh();
374         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
375         video::SColor c(255,255,255,255);
376         video::S3DVertex vertices[4] =
377         {
378                 /*video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
379                 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
380                 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
381                 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),*/
382                 video::S3DVertex(BS/3.,0,0, 0,0,0, c, 0,1),
383                 video::S3DVertex(-BS/3.,0,0, 0,0,0, c, 1,1),
384                 video::S3DVertex(-BS/3.,0+BS*2./3.,0, 0,0,0, c, 1,0),
385                 video::S3DVertex(BS/3.,0+BS*2./3.,0, 0,0,0, c, 0,0),
386         };
387         u16 indices[] = {0,1,2,2,3,0};
388         buf->append(vertices, 4, indices, 6);
389         // Set material
390         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
391         buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
392         // Initialize with a generated placeholder texture
393         buf->getMaterial().setTexture(0, tsrc->getTextureRaw(""));
394         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
395         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
396         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
397         // Add to mesh
398         mesh->addMeshBuffer(buf);
399         buf->drop();
400         m_node = smgr->addMeshSceneNode(mesh, NULL);
401         mesh->drop();
402         updateNodePos();
403
404         /*
405                 Update image of node
406         */
407
408         updateTexture();
409 }
410
411 void ItemCAO::removeFromScene()
412 {
413         if(m_node == NULL)
414                 return;
415
416         m_node->remove();
417         m_node = NULL;
418 }
419
420 void ItemCAO::updateLight(u8 light_at_pos)
421 {
422         if(m_node == NULL)
423                 return;
424
425         u8 li = decode_light(light_at_pos);
426         video::SColor color(255,li,li,li);
427         setMeshColor(m_node->getMesh(), color);
428 }
429
430 v3s16 ItemCAO::getLightPosition()
431 {
432         return floatToInt(m_position + v3f(0,0.5*BS,0), BS);
433 }
434
435 void ItemCAO::updateNodePos()
436 {
437         if(m_node == NULL)
438                 return;
439
440         m_node->setPosition(m_position);
441 }
442
443 void ItemCAO::updateInfoText()
444 {
445         try{
446                 IItemDefManager *idef = m_gamedef->idef();
447                 ItemStack item;
448                 item.deSerialize(m_itemstring, idef);
449                 if(item.isKnown(idef))
450                         m_infotext = item.getDefinition(idef).description;
451                 else
452                         m_infotext = "Unknown item: '" + m_itemstring + "'";
453                 if(item.count >= 2)
454                         m_infotext += " (" + itos(item.count) + ")";
455         }
456         catch(SerializationError &e)
457         {
458                 m_infotext = "Unknown item: '" + m_itemstring + "'";
459         }
460 }
461
462 void ItemCAO::updateTexture()
463 {
464         if(m_node == NULL)
465                 return;
466
467         // Create an inventory item to see what is its image
468         std::istringstream is(m_itemstring, std::ios_base::binary);
469         video::ITexture *texture = NULL;
470         try{
471                 IItemDefManager *idef = m_gamedef->idef();
472                 ItemStack item;
473                 item.deSerialize(is, idef);
474                 texture = item.getDefinition(idef).inventory_texture;
475         }
476         catch(SerializationError &e)
477         {
478                 infostream<<"WARNING: "<<__FUNCTION_NAME
479                                 <<": error deSerializing itemstring \""
480                                 <<m_itemstring<<std::endl;
481         }
482         
483         // Set meshbuffer texture
484         m_node->getMaterial(0).setTexture(0, texture);
485 }
486
487
488 void ItemCAO::step(float dtime, ClientEnvironment *env)
489 {
490         if(m_node)
491         {
492                 /*v3f rot = m_node->getRotation();
493                 rot.Y += dtime * 120;
494                 m_node->setRotation(rot);*/
495                 LocalPlayer *player = env->getLocalPlayer();
496                 assert(player);
497                 v3f rot = m_node->getRotation();
498                 rot.Y = 180.0 - (player->getYaw());
499                 m_node->setRotation(rot);
500         }
501 }
502
503 void ItemCAO::processMessage(const std::string &data)
504 {
505         //infostream<<"ItemCAO: Got message"<<std::endl;
506         std::istringstream is(data, std::ios::binary);
507         // command
508         u8 cmd = readU8(is);
509         if(cmd == 0)
510         {
511                 // pos
512                 m_position = readV3F1000(is);
513                 updateNodePos();
514         }
515         if(cmd == 1)
516         {
517                 // itemstring
518                 m_itemstring = deSerializeString(is);
519                 updateInfoText();
520                 updateTexture();
521         }
522 }
523
524 void ItemCAO::initialize(const std::string &data)
525 {
526         infostream<<"ItemCAO: Got init data"<<std::endl;
527         
528         {
529                 std::istringstream is(data, std::ios::binary);
530                 // version
531                 u8 version = readU8(is);
532                 // check version
533                 if(version != 0)
534                         return;
535                 // pos
536                 m_position = readV3F1000(is);
537                 // itemstring
538                 m_itemstring = deSerializeString(is);
539         }
540         
541         updateNodePos();
542         updateInfoText();
543 }
544
545 /*
546         GenericCAO
547 */
548
549 #include "genericobject.h"
550
551 class GenericCAO : public ClientActiveObject
552 {
553 private:
554         // Only set at initialization
555         std::string m_name;
556         bool m_is_player;
557         bool m_is_local_player; // determined locally
558         int m_id;
559         // Property-ish things
560         ObjectProperties m_prop;
561         //
562         scene::ISceneManager *m_smgr;
563         IrrlichtDevice *m_irr;
564         core::aabbox3d<f32> m_selection_box;
565         scene::IMeshSceneNode *m_meshnode;
566         scene::IAnimatedMeshSceneNode *m_animated_meshnode;
567         scene::IBillboardSceneNode *m_spritenode;
568         scene::ITextSceneNode* m_textnode;
569         v3f m_position;
570         v3f m_velocity;
571         v3f m_acceleration;
572         float m_yaw;
573         s16 m_hp;
574         SmoothTranslator pos_translator;
575         // Spritesheet/animation stuff
576         v2f m_tx_size;
577         v2s16 m_tx_basepos;
578         bool m_initial_tx_basepos_set;
579         bool m_tx_select_horiz_by_yawpitch;
580         v2f m_frames;
581         int m_frame_speed;
582         int m_frame_blend;
583         std::map<std::string, core::vector2d<v3f> > m_bone_posrot;
584         int m_anim_frame;
585         int m_anim_num_frames;
586         float m_anim_framelength;
587         float m_anim_timer;
588         ItemGroupList m_armor_groups;
589         float m_reset_textures_timer;
590         bool m_visuals_expired;
591         float m_step_distance_counter;
592         u8 m_last_light;
593
594 public:
595         GenericCAO(IGameDef *gamedef, ClientEnvironment *env):
596                 ClientActiveObject(0, gamedef, env),
597                 //
598                 m_is_player(false),
599                 m_is_local_player(false),
600                 m_id(0),
601                 //
602                 m_smgr(NULL),
603                 m_irr(NULL),
604                 m_selection_box(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.),
605                 m_meshnode(NULL),
606                 m_animated_meshnode(NULL),
607                 m_spritenode(NULL),
608                 m_textnode(NULL),
609                 m_position(v3f(0,10*BS,0)),
610                 m_velocity(v3f(0,0,0)),
611                 m_acceleration(v3f(0,0,0)),
612                 m_yaw(0),
613                 m_hp(1),
614                 m_tx_size(1,1),
615                 m_tx_basepos(0,0),
616                 m_initial_tx_basepos_set(false),
617                 m_tx_select_horiz_by_yawpitch(false),
618                 m_anim_frame(0),
619                 m_anim_num_frames(1),
620                 m_anim_framelength(0.2),
621                 m_anim_timer(0),
622                 m_reset_textures_timer(-1),
623                 m_visuals_expired(false),
624                 m_step_distance_counter(0),
625                 m_last_light(255)
626         {
627                 if(gamedef == NULL)
628                         ClientActiveObject::registerType(getType(), create);
629         }
630
631         void initialize(const std::string &data)
632         {
633                 infostream<<"GenericCAO: Got init data"<<std::endl;
634                 std::istringstream is(data, std::ios::binary);
635                 // version
636                 u8 version = readU8(is);
637                 // check version
638                 if(version != 0){
639                         errorstream<<"GenericCAO: Unsupported init data version"
640                                         <<std::endl;
641                         return;
642                 }
643                 m_name = deSerializeString(is);
644                 m_is_player = readU8(is);
645                 m_id = readS16(is);
646                 m_position = readV3F1000(is);
647                 m_yaw = readF1000(is);
648                 m_hp = readS16(is);
649                 
650                 int num_messages = readU8(is);
651                 for(int i=0; i<num_messages; i++){
652                         std::string message = deSerializeLongString(is);
653                         processMessage(message);
654                 }
655
656                 pos_translator.init(m_position);
657                 updateNodePos();
658                 
659                 if(m_is_player){
660                         Player *player = m_env->getPlayer(m_name.c_str());
661                         if(player && player->isLocal()){
662                                 m_is_local_player = true;
663                         }
664                 }
665         }
666
667         ~GenericCAO()
668         {
669         }
670
671         static ClientActiveObject* create(IGameDef *gamedef, ClientEnvironment *env)
672         {
673                 return new GenericCAO(gamedef, env);
674         }
675
676         u8 getType() const
677         {
678                 return ACTIVEOBJECT_TYPE_GENERIC;
679         }
680         core::aabbox3d<f32>* getSelectionBox()
681         {
682                 if(!m_prop.is_visible || m_is_local_player)
683                         return NULL;
684                 return &m_selection_box;
685         }
686         v3f getPosition()
687         {
688                 return pos_translator.vect_show;
689         }
690
691         void removeFromScene()
692         {
693                 if(m_meshnode){
694                         m_meshnode->remove();
695                         m_meshnode = NULL;
696                 }
697                 if(m_animated_meshnode){
698                         m_animated_meshnode->remove();
699                         m_animated_meshnode = NULL;
700                 }
701                 if(m_spritenode){
702                         m_spritenode->remove();
703                         m_spritenode = NULL;
704                 }
705         }
706
707         void addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
708                         IrrlichtDevice *irr)
709         {
710                 m_smgr = smgr;
711                 m_irr = irr;
712
713                 if(m_meshnode != NULL || m_animated_meshnode != NULL || m_spritenode != NULL)
714                         return;
715                 
716                 m_visuals_expired = false;
717
718                 if(!m_prop.is_visible || m_is_local_player)
719                         return;
720         
721                 //video::IVideoDriver* driver = smgr->getVideoDriver();
722
723                 if(m_prop.visual == "sprite"){
724                         infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
725                         m_spritenode = smgr->addBillboardSceneNode(
726                                         NULL, v2f(1, 1), v3f(0,0,0), -1);
727                         m_spritenode->setMaterialTexture(0,
728                                         tsrc->getTextureRaw("unknown_block.png"));
729                         m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
730                         m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
731                         m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
732                         m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
733                         u8 li = m_last_light;
734                         m_spritenode->setColor(video::SColor(255,li,li,li));
735                         m_spritenode->setSize(m_prop.visual_size*BS);
736                         {
737                                 const float txs = 1.0 / 1;
738                                 const float tys = 1.0 / 1;
739                                 setBillboardTextureMatrix(m_spritenode,
740                                                 txs, tys, 0, 0);
741                         }
742                 }
743                 else if(m_prop.visual == "upright_sprite")
744                 {
745                         scene::SMesh *mesh = new scene::SMesh();
746                         double dx = BS*m_prop.visual_size.X/2;
747                         double dy = BS*m_prop.visual_size.Y/2;
748                         { // Front
749                         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
750                         u8 li = m_last_light;
751                         video::SColor c(255,li,li,li);
752                         video::S3DVertex vertices[4] =
753                         {
754                                 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
755                                 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
756                                 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
757                                 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
758                         };
759                         u16 indices[] = {0,1,2,2,3,0};
760                         buf->append(vertices, 4, indices, 6);
761                         // Set material
762                         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
763                         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
764                         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
765                         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
766                         // Add to mesh
767                         mesh->addMeshBuffer(buf);
768                         buf->drop();
769                         }
770                         { // Back
771                         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
772                         u8 li = m_last_light;
773                         video::SColor c(255,li,li,li);
774                         video::S3DVertex vertices[4] =
775                         {
776                                 video::S3DVertex(dx,-dy,0, 0,0,0, c, 1,1),
777                                 video::S3DVertex(-dx,-dy,0, 0,0,0, c, 0,1),
778                                 video::S3DVertex(-dx,dy,0, 0,0,0, c, 0,0),
779                                 video::S3DVertex(dx,dy,0, 0,0,0, c, 1,0),
780                         };
781                         u16 indices[] = {0,1,2,2,3,0};
782                         buf->append(vertices, 4, indices, 6);
783                         // Set material
784                         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
785                         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
786                         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
787                         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
788                         // Add to mesh
789                         mesh->addMeshBuffer(buf);
790                         buf->drop();
791                         }
792                         m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
793                         mesh->drop();
794                         // Set it to use the materials of the meshbuffers directly.
795                         // This is needed for changing the texture in the future
796                         m_meshnode->setReadOnlyMaterials(true);
797                 }
798                 else if(m_prop.visual == "cube"){
799                         infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
800                         scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
801                         m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
802                         mesh->drop();
803                         
804                         m_meshnode->setScale(v3f(m_prop.visual_size.X,
805                                         m_prop.visual_size.Y,
806                                         m_prop.visual_size.X));
807                         u8 li = m_last_light;
808                         setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
809                 }
810                 else if(m_prop.visual == "mesh"){
811                         infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
812                         scene::IAnimatedMesh *mesh = smgr->getMesh(m_prop.mesh.c_str());
813                         if(mesh)
814                         {
815                                 m_animated_meshnode = smgr->addAnimatedMeshSceneNode(mesh, NULL);
816                                 m_animated_meshnode->setMD2Animation(scene::EMAT_STAND);
817                                 m_animated_meshnode->animateJoints(); // Needed for some animations
818                                 m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
819                                                 m_prop.visual_size.Y,
820                                                 m_prop.visual_size.X));
821                                 u8 li = m_last_light;
822                                 setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
823                         }
824                         else
825                                 errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
826                 }
827                 else if(m_prop.visual == "wielditem"){
828                         infostream<<"GenericCAO::addToScene(): node"<<std::endl;
829                         infostream<<"textures: "<<m_prop.textures.size()<<std::endl;
830                         if(m_prop.textures.size() >= 1){
831                                 infostream<<"textures[0]: "<<m_prop.textures[0]<<std::endl;
832                                 IItemDefManager *idef = m_gamedef->idef();
833                                 ItemStack item(m_prop.textures[0], 1, 0, "", idef);
834                                 scene::IMesh *item_mesh = item.getDefinition(idef).wield_mesh;
835                                 
836                                 // Copy mesh to be able to set unique vertex colors
837                                 scene::IMeshManipulator *manip =
838                                                 irr->getVideoDriver()->getMeshManipulator();
839                                 scene::IMesh *mesh = manip->createMeshUniquePrimitives(item_mesh);
840
841                                 m_meshnode = smgr->addMeshSceneNode(mesh, NULL);
842                                 mesh->drop();
843                                 
844                                 m_meshnode->setScale(v3f(m_prop.visual_size.X/2,
845                                                 m_prop.visual_size.Y/2,
846                                                 m_prop.visual_size.X/2));
847                                 u8 li = m_last_light;
848                                 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
849                         }
850                 } else {
851                         infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
852                                         <<"\" not supported"<<std::endl;
853                 }
854                 updateTextures("");
855                 
856                 scene::ISceneNode *node = NULL;
857                 if(m_spritenode)
858                         node = m_spritenode;
859                 else if(m_animated_meshnode)
860                         node = m_animated_meshnode;
861                 else if(m_meshnode)
862                         node = m_meshnode;
863                 if(node && m_is_player && !m_is_local_player){
864                         // Add a text node for showing the name
865                         gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
866                         std::wstring wname = narrow_to_wide(m_name);
867                         m_textnode = smgr->addTextSceneNode(gui->getBuiltInFont(),
868                                         wname.c_str(), video::SColor(255,255,255,255), node);
869                         m_textnode->setPosition(v3f(0, BS*1.1, 0));
870                 }
871                 
872                 updateNodePos();
873         }
874
875         void expireVisuals()
876         {
877                 m_visuals_expired = true;
878         }
879                 
880         void updateLight(u8 light_at_pos)
881         {
882                 bool is_visible = (m_hp != 0);
883                 u8 li = decode_light(light_at_pos);
884                 if(li != m_last_light){
885                         m_last_light = li;
886                         video::SColor color(255,li,li,li);
887                         if(m_meshnode){
888                                 setMeshColor(m_meshnode->getMesh(), color);
889                                 m_meshnode->setVisible(is_visible);
890                         }
891                         if(m_animated_meshnode){
892                                 setMeshColor(m_animated_meshnode->getMesh(), color);
893                                 m_animated_meshnode->setVisible(is_visible);
894                         }
895                         if(m_spritenode){
896                                 m_spritenode->setColor(color);
897                                 m_spritenode->setVisible(is_visible);
898                         }
899                 }
900         }
901
902         v3s16 getLightPosition()
903         {
904                 return floatToInt(m_position, BS);
905         }
906
907         void updateNodePos()
908         {
909                 if(m_meshnode){
910                         m_meshnode->setPosition(pos_translator.vect_show);
911                         v3f rot = m_meshnode->getRotation();
912                         rot.Y = -m_yaw;
913                         m_meshnode->setRotation(rot);
914                 }
915                 if(m_animated_meshnode){
916                         m_animated_meshnode->setPosition(pos_translator.vect_show);
917                         v3f rot = m_animated_meshnode->getRotation();
918                         rot.Y = -m_yaw;
919                         m_animated_meshnode->setRotation(rot);
920                 }
921                 if(m_spritenode){
922                         m_spritenode->setPosition(pos_translator.vect_show);
923                 }
924         }
925
926         void step(float dtime, ClientEnvironment *env)
927         {
928                 v3f lastpos = pos_translator.vect_show;
929
930                 if(m_visuals_expired && m_smgr && m_irr){
931                         m_visuals_expired = false;
932                         removeFromScene();
933                         addToScene(m_smgr, m_gamedef->tsrc(), m_irr);
934                         updateAnimations();
935                         updateBonePosRot();
936                         updateAttachment();
937                 }
938
939                 if(m_prop.physical){
940                         core::aabbox3d<f32> box = m_prop.collisionbox;
941                         box.MinEdge *= BS;
942                         box.MaxEdge *= BS;
943                         collisionMoveResult moveresult;
944                         f32 pos_max_d = BS*0.125; // Distance per iteration
945                         f32 stepheight = 0;
946                         v3f p_pos = m_position;
947                         v3f p_velocity = m_velocity;
948                         v3f p_acceleration = m_acceleration;
949                         IGameDef *gamedef = env->getGameDef();
950                         moveresult = collisionMoveSimple(&env->getMap(), gamedef,
951                                         pos_max_d, box, stepheight, dtime,
952                                         p_pos, p_velocity, p_acceleration);
953                         // Apply results
954                         m_position = p_pos;
955                         m_velocity = p_velocity;
956                         m_acceleration = p_acceleration;
957                         
958                         bool is_end_position = moveresult.collides;
959                         pos_translator.update(m_position, is_end_position, dtime);
960                         pos_translator.translate(dtime);
961                         updateNodePos();
962                 } else {
963                         m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
964                         m_velocity += dtime * m_acceleration;
965                         pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time);
966                         pos_translator.translate(dtime);
967                         updateNodePos();
968                 }
969
970                 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
971                 m_step_distance_counter += moved;
972                 if(m_step_distance_counter > 1.5*BS){
973                         m_step_distance_counter = 0;
974                         if(!m_is_local_player && m_prop.makes_footstep_sound){
975                                 INodeDefManager *ndef = m_gamedef->ndef();
976                                 v3s16 p = floatToInt(getPosition() + v3f(0,
977                                                 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
978                                 MapNode n = m_env->getMap().getNodeNoEx(p);
979                                 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
980                                 m_gamedef->sound()->playSoundAt(spec, false, getPosition());
981                         }
982                 }
983
984                 m_anim_timer += dtime;
985                 if(m_anim_timer >= m_anim_framelength){
986                         m_anim_timer -= m_anim_framelength;
987                         m_anim_frame++;
988                         if(m_anim_frame >= m_anim_num_frames)
989                                 m_anim_frame = 0;
990                 }
991
992                 updateTexturePos();
993
994                 if(m_reset_textures_timer >= 0){
995                         m_reset_textures_timer -= dtime;
996                         if(m_reset_textures_timer <= 0){
997                                 m_reset_textures_timer = -1;
998                                 updateTextures("");
999                         }
1000                 }
1001                 if(fabs(m_prop.automatic_rotate) > 0.001){
1002                         m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
1003                         updateNodePos();
1004                 }
1005         }
1006
1007         void updateTexturePos()
1008         {
1009                 if(m_spritenode){
1010                         scene::ICameraSceneNode* camera =
1011                                         m_spritenode->getSceneManager()->getActiveCamera();
1012                         if(!camera)
1013                                 return;
1014                         v3f cam_to_entity = m_spritenode->getAbsolutePosition()
1015                                         - camera->getAbsolutePosition();
1016                         cam_to_entity.normalize();
1017
1018                         int row = m_tx_basepos.Y;
1019                         int col = m_tx_basepos.X;
1020                         
1021                         if(m_tx_select_horiz_by_yawpitch)
1022                         {
1023                                 if(cam_to_entity.Y > 0.75)
1024                                         col += 5;
1025                                 else if(cam_to_entity.Y < -0.75)
1026                                         col += 4;
1027                                 else{
1028                                         float mob_dir = atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
1029                                         float dir = mob_dir - m_yaw;
1030                                         dir = wrapDegrees_180(dir);
1031                                         //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
1032                                         if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
1033                                                 col += 2;
1034                                         else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
1035                                                 col += 3;
1036                                         else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
1037                                                 col += 0;
1038                                         else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
1039                                                 col += 1;
1040                                         else
1041                                                 col += 4;
1042                                 }
1043                         }
1044                         
1045                         // Animation goes downwards
1046                         row += m_anim_frame;
1047
1048                         float txs = m_tx_size.X;
1049                         float tys = m_tx_size.Y;
1050                         setBillboardTextureMatrix(m_spritenode,
1051                                         txs, tys, col, row);
1052                 }
1053         }
1054
1055         void updateTextures(const std::string &mod)
1056         {
1057                 ITextureSource *tsrc = m_gamedef->tsrc();
1058
1059                 if(m_spritenode)
1060                 {
1061                         if(m_prop.visual == "sprite")
1062                         {
1063                                 std::string texturestring = "unknown_block.png";
1064                                 if(m_prop.textures.size() >= 1)
1065                                         texturestring = m_prop.textures[0];
1066                                 texturestring += mod;
1067                                 m_spritenode->setMaterialTexture(0,
1068                                                 tsrc->getTextureRaw(texturestring));
1069
1070                                 // Does not work yet with the current lighting settings
1071                                 m_meshnode->getMaterial(0).AmbientColor = m_prop.colors[0];
1072                                 m_meshnode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1073                         }
1074                 }
1075                 if(m_animated_meshnode)
1076                 {
1077                         if(m_prop.visual == "mesh")
1078                         {
1079                                 for (u32 i = 0; i < m_prop.textures.size(); ++i)
1080                                 {
1081                                         std::string texturestring = m_prop.textures[i];
1082                                         if(texturestring == "")
1083                                                 continue; // Empty texture string means don't modify that material
1084                                         texturestring += mod;
1085                                         video::ITexture* texture = tsrc->getTextureRaw(texturestring);
1086                                         if(!texture)
1087                                         {
1088                                                 errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1089                                                 continue;
1090                                         }
1091
1092                                         // Set material flags and texture
1093                                         m_animated_meshnode->setMaterialTexture(i, texture);
1094                                         video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1095                                         material.setFlag(video::EMF_LIGHTING, false);
1096                                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
1097                                 }
1098                                 for (u32 i = 0; i < m_prop.colors.size(); ++i)
1099                                 {
1100                                         // Does not work yet with the current lighting settings
1101                                         m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1102                                         m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1103                                 }
1104                         }
1105                 }
1106                 if(m_meshnode)
1107                 {
1108                         if(m_prop.visual == "cube")
1109                         {
1110                                 for (u32 i = 0; i < 6; ++i)
1111                                 {
1112                                         std::string texturestring = "unknown_block.png";
1113                                         if(m_prop.textures.size() > i)
1114                                                 texturestring = m_prop.textures[i];
1115                                         texturestring += mod;
1116                                         AtlasPointer ap = tsrc->getTexture(texturestring);
1117
1118                                         // Get the tile texture and atlas transformation
1119                                         video::ITexture* atlas = ap.atlas;
1120                                         v2f pos = ap.pos;
1121                                         v2f size = ap.size;
1122
1123                                         // Set material flags and texture
1124                                         video::SMaterial& material = m_meshnode->getMaterial(i);
1125                                         material.setFlag(video::EMF_LIGHTING, false);
1126                                         material.setFlag(video::EMF_BILINEAR_FILTER, false);
1127                                         material.setTexture(0, atlas);
1128                                         material.getTextureMatrix(0).setTextureTranslate(pos.X, pos.Y);
1129                                         material.getTextureMatrix(0).setTextureScale(size.X, size.Y);
1130
1131                                         // Does not work yet with the current lighting settings
1132                                         m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1133                                         m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1134                                 }
1135                         }
1136                         else if(m_prop.visual == "upright_sprite")
1137                         {
1138                                 scene::IMesh *mesh = m_meshnode->getMesh();
1139                                 {
1140                                         std::string tname = "unknown_object.png";
1141                                         if(m_prop.textures.size() >= 1)
1142                                                 tname = m_prop.textures[0];
1143                                         tname += mod;
1144                                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1145                                         buf->getMaterial().setTexture(0,
1146                                                         tsrc->getTextureRaw(tname));
1147                                         
1148                                         // Does not work yet with the current lighting settings
1149                                         m_meshnode->getMaterial(0).AmbientColor = m_prop.colors[0];
1150                                         m_meshnode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1151                                 }
1152                                 {
1153                                         std::string tname = "unknown_object.png";
1154                                         if(m_prop.textures.size() >= 2)
1155                                                 tname = m_prop.textures[1];
1156                                         else if(m_prop.textures.size() >= 1)
1157                                                 tname = m_prop.textures[0];
1158                                         tname += mod;
1159                                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1160                                         buf->getMaterial().setTexture(0,
1161                                                         tsrc->getTextureRaw(tname));
1162
1163                                         // Does not work yet with the current lighting settings
1164                                         m_meshnode->getMaterial(1).AmbientColor = m_prop.colors[1]; 
1165                                         m_meshnode->getMaterial(1).DiffuseColor = m_prop.colors[1]; 
1166                                 }
1167                         }
1168                 }
1169         }
1170
1171         void updateAnimations()
1172         {
1173                 if(!m_animated_meshnode)
1174                         return;
1175
1176                 m_animated_meshnode->setFrameLoop((int)m_frames.X, (int)m_frames.Y);
1177                 m_animated_meshnode->setAnimationSpeed(m_frame_speed);
1178                 m_animated_meshnode->setTransitionTime(m_frame_blend);
1179         }
1180
1181         void updateBonePosRot()
1182         {
1183                 if(m_bone_posrot.size() > 0)
1184                 {
1185                         m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1186                         for(std::map<std::string, core::vector2d<v3f> >::const_iterator ii = m_bone_posrot.begin(); ii != m_bone_posrot.end(); ++ii){
1187                                 std::string bone_name = (*ii).first;
1188                                 v3f bone_pos = (*ii).second.X;
1189                                 v3f bone_rot = (*ii).second.Y;
1190                                 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1191                                 bone->setPosition(bone_pos);
1192                                 bone->setRotation(bone_rot);
1193                         }
1194                 }
1195         }
1196
1197         void updateAttachment()
1198         {
1199                 // Code for attachments goes here
1200         }
1201
1202         void processMessage(const std::string &data)
1203         {
1204                 //infostream<<"GenericCAO: Got message"<<std::endl;
1205                 std::istringstream is(data, std::ios::binary);
1206                 // command
1207                 u8 cmd = readU8(is);
1208                 if(cmd == GENERIC_CMD_SET_PROPERTIES)
1209                 {
1210                         m_prop = gob_read_set_properties(is);
1211
1212                         m_selection_box = m_prop.collisionbox;
1213                         m_selection_box.MinEdge *= BS;
1214                         m_selection_box.MaxEdge *= BS;
1215                                 
1216                         m_tx_size.X = 1.0 / m_prop.spritediv.X;
1217                         m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1218
1219                         if(!m_initial_tx_basepos_set){
1220                                 m_initial_tx_basepos_set = true;
1221                                 m_tx_basepos = m_prop.initial_sprite_basepos;
1222                         }
1223                         
1224                         expireVisuals();
1225                 }
1226                 else if(cmd == GENERIC_CMD_UPDATE_POSITION)
1227                 {
1228                         m_position = readV3F1000(is);
1229                         m_velocity = readV3F1000(is);
1230                         m_acceleration = readV3F1000(is);
1231                         if(fabs(m_prop.automatic_rotate) < 0.001)
1232                                 m_yaw = readF1000(is);
1233                         bool do_interpolate = readU8(is);
1234                         bool is_end_position = readU8(is);
1235                         float update_interval = readF1000(is);
1236
1237                         // Place us a bit higher if we're physical, to not sink into
1238                         // the ground due to sucky collision detection...
1239                         if(m_prop.physical)
1240                                 m_position += v3f(0,0.002,0);
1241                         
1242                         if(do_interpolate){
1243                                 if(!m_prop.physical)
1244                                         pos_translator.update(m_position, is_end_position, update_interval);
1245                         } else {
1246                                 pos_translator.init(m_position);
1247                         }
1248                         updateNodePos();
1249                 }
1250                 else if(cmd == GENERIC_CMD_SET_TEXTURE_MOD)
1251                 {
1252                         std::string mod = deSerializeString(is);
1253                         updateTextures(mod);
1254                 }
1255                 else if(cmd == GENERIC_CMD_SET_SPRITE)
1256                 {
1257                         v2s16 p = readV2S16(is);
1258                         int num_frames = readU16(is);
1259                         float framelength = readF1000(is);
1260                         bool select_horiz_by_yawpitch = readU8(is);
1261                         
1262                         m_tx_basepos = p;
1263                         m_anim_num_frames = num_frames;
1264                         m_anim_framelength = framelength;
1265                         m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1266
1267                         updateTexturePos();
1268                 }
1269                 else if(cmd == GENERIC_CMD_SET_ANIMATIONS)
1270                 {
1271                         m_frames = readV2F1000(is);
1272                         m_frame_speed = readF1000(is);
1273                         m_frame_blend = readF1000(is);
1274
1275                         updateAnimations();
1276                         expireVisuals();
1277                 }
1278                 else if(cmd == GENERIC_CMD_SET_BONE_POSROT)
1279                 {
1280                         std::string bone = deSerializeString(is);
1281                         v3f position = readV3F1000(is);
1282                         v3f rotation = readV3F1000(is);
1283                         m_bone_posrot[bone] = core::vector2d<v3f>(position, rotation);
1284
1285                         updateBonePosRot();
1286                         expireVisuals();
1287                 }
1288                 else if(cmd == GENERIC_CMD_SET_ATTACHMENT)
1289                 {
1290                         // Part of the attachment structure, not used yet!
1291
1292                         // Get properties here.
1293
1294                         updateAttachment();
1295                         expireVisuals();
1296                 }
1297                 else if(cmd == GENERIC_CMD_PUNCHED)
1298                 {
1299                         /*s16 damage =*/ readS16(is);
1300                         s16 result_hp = readS16(is);
1301                         
1302                         m_hp = result_hp;
1303                 }
1304                 else if(cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS)
1305                 {
1306                         m_armor_groups.clear();
1307                         int armor_groups_size = readU16(is);
1308                         for(int i=0; i<armor_groups_size; i++){
1309                                 std::string name = deSerializeString(is);
1310                                 int rating = readS16(is);
1311                                 m_armor_groups[name] = rating;
1312                         }
1313                 }
1314         }
1315         
1316         bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
1317                         float time_from_last_punch=1000000)
1318         {
1319                 assert(punchitem);
1320                 const ToolCapabilities *toolcap =
1321                                 &punchitem->getToolCapabilities(m_gamedef->idef());
1322                 PunchDamageResult result = getPunchDamage(
1323                                 m_armor_groups,
1324                                 toolcap,
1325                                 punchitem,
1326                                 time_from_last_punch);
1327
1328                 if(result.did_punch && result.damage != 0)
1329                 {
1330                         if(result.damage < m_hp){
1331                                 m_hp -= result.damage;
1332                         } else {
1333                                 m_hp = 0;
1334                                 // TODO: Execute defined fast response
1335                                 // As there is no definition, make a smoke puff
1336                                 ClientSimpleObject *simple = createSmokePuff(
1337                                                 m_smgr, m_env, m_position,
1338                                                 m_prop.visual_size * BS);
1339                                 m_env->addSimpleObject(simple);
1340                         }
1341                         // TODO: Execute defined fast response
1342                         // Flashing shall suffice as there is no definition
1343                         m_reset_textures_timer = 0.05;
1344                         if(result.damage >= 2)
1345                                 m_reset_textures_timer += 0.05 * result.damage;
1346                         updateTextures("^[brighten");
1347                 }
1348                 
1349                 return false;
1350         }
1351         
1352         std::string debugInfoText()
1353         {
1354                 std::ostringstream os(std::ios::binary);
1355                 os<<"GenericCAO hp="<<m_hp<<"\n";
1356                 os<<"armor={";
1357                 for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1358                                 i != m_armor_groups.end(); i++){
1359                         os<<i->first<<"="<<i->second<<", ";
1360                 }
1361                 os<<"}";
1362                 return os.str();
1363         }
1364 };
1365
1366 // Prototype
1367 GenericCAO proto_GenericCAO(NULL, NULL);
1368
1369