]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_cao.cpp
Fix OSX builds (closes #6289, fixes #6270) (#6306)
[dragonfireclient.git] / src / content_cao.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <ICameraSceneNode.h>
21 #include <ITextSceneNode.h>
22 #include <IBillboardSceneNode.h>
23 #include <IMeshManipulator.h>
24 #include <IAnimatedMeshSceneNode.h>
25 #include "content_cao.h"
26 #include "util/numeric.h" // For IntervalLimiter
27 #include "util/serialize.h"
28 #include "util/basic_macros.h"
29 #include "client/tile.h"
30 #include "environment.h"
31 #include "collision.h"
32 #include "settings.h"
33 #include "serialization.h" // For decompressZlib
34 #include "clientobject.h"
35 #include "mesh.h"
36 #include "itemdef.h"
37 #include "tool.h"
38 #include "content_cso.h"
39 #include "sound.h"
40 #include "nodedef.h"
41 #include "localplayer.h"
42 #include "map.h"
43 #include "camera.h" // CameraModes
44 #include "client.h"
45 #include "wieldmesh.h"
46 #include <algorithm>
47 #include "client/renderingengine.h"
48
49 class Settings;
50 struct ToolCapabilities;
51
52 std::unordered_map<u16, ClientActiveObject::Factory> ClientActiveObject::m_types;
53
54 void SmoothTranslator::init(v3f vect)
55 {
56         vect_old = vect;
57         vect_show = vect;
58         vect_aim = vect;
59         anim_counter = 0;
60         anim_time = 0;
61         anim_time_counter = 0;
62         aim_is_end = true;
63 }
64
65 void SmoothTranslator::update(v3f vect_new, bool is_end_position, float update_interval)
66 {
67         aim_is_end = is_end_position;
68         vect_old = vect_show;
69         vect_aim = vect_new;
70         if(update_interval > 0)
71         {
72                 anim_time = update_interval;
73         } else {
74                 if(anim_time < 0.001 || anim_time > 1.0)
75                         anim_time = anim_time_counter;
76                 else
77                         anim_time = anim_time * 0.9 + anim_time_counter * 0.1;
78         }
79         anim_time_counter = 0;
80         anim_counter = 0;
81 }
82
83 void SmoothTranslator::translate(f32 dtime)
84 {
85         anim_time_counter = anim_time_counter + dtime;
86         anim_counter = anim_counter + dtime;
87         v3f vect_move = vect_aim - vect_old;
88         f32 moveratio = 1.0;
89         if(anim_time > 0.001)
90                 moveratio = anim_time_counter / anim_time;
91         // Move a bit less than should, to avoid oscillation
92         moveratio = moveratio * 0.8;
93         float move_end = 1.5;
94         if(aim_is_end)
95                 move_end = 1.0;
96         if(moveratio > move_end)
97                 moveratio = move_end;
98         vect_show = vect_old + vect_move * moveratio;
99 }
100
101 /*
102         Other stuff
103 */
104
105 static void setBillboardTextureMatrix(scene::IBillboardSceneNode *bill,
106                 float txs, float tys, int col, int row)
107 {
108         video::SMaterial& material = bill->getMaterial(0);
109         core::matrix4& matrix = material.getTextureMatrix(0);
110         matrix.setTextureTranslate(txs*col, tys*row);
111         matrix.setTextureScale(txs, tys);
112 }
113
114 /*
115         TestCAO
116 */
117
118 class TestCAO : public ClientActiveObject
119 {
120 public:
121         TestCAO(Client *client, ClientEnvironment *env);
122         virtual ~TestCAO() = default;
123
124         ActiveObjectType getType() const
125         {
126                 return ACTIVEOBJECT_TYPE_TEST;
127         }
128
129         static ClientActiveObject* create(Client *client, ClientEnvironment *env);
130
131         void addToScene(ITextureSource *tsrc);
132         void removeFromScene(bool permanent);
133         void updateLight(u8 light_at_pos);
134         v3s16 getLightPosition();
135         void updateNodePos();
136
137         void step(float dtime, ClientEnvironment *env);
138
139         void processMessage(const std::string &data);
140
141         bool getCollisionBox(aabb3f *toset) const { return false; }
142 private:
143         scene::IMeshSceneNode *m_node;
144         v3f m_position;
145 };
146
147 // Prototype
148 TestCAO proto_TestCAO(NULL, NULL);
149
150 TestCAO::TestCAO(Client *client, ClientEnvironment *env):
151         ClientActiveObject(0, client, env),
152         m_node(NULL),
153         m_position(v3f(0,10*BS,0))
154 {
155         ClientActiveObject::registerType(getType(), create);
156 }
157
158 ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
159 {
160         return new TestCAO(client, env);
161 }
162
163 void TestCAO::addToScene(ITextureSource *tsrc)
164 {
165         if(m_node != NULL)
166                 return;
167
168         //video::IVideoDriver* driver = smgr->getVideoDriver();
169
170         scene::SMesh *mesh = new scene::SMesh();
171         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
172         video::SColor c(255,255,255,255);
173         video::S3DVertex vertices[4] =
174         {
175                 video::S3DVertex(-BS/2,-BS/4,0, 0,0,0, c, 0,1),
176                 video::S3DVertex(BS/2,-BS/4,0, 0,0,0, c, 1,1),
177                 video::S3DVertex(BS/2,BS/4,0, 0,0,0, c, 1,0),
178                 video::S3DVertex(-BS/2,BS/4,0, 0,0,0, c, 0,0),
179         };
180         u16 indices[] = {0,1,2,2,3,0};
181         buf->append(vertices, 4, indices, 6);
182         // Set material
183         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
184         buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
185         buf->getMaterial().setTexture(0, tsrc->getTextureForMesh("rat.png"));
186         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
187         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
188         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
189         // Add to mesh
190         mesh->addMeshBuffer(buf);
191         buf->drop();
192         m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
193         mesh->drop();
194         updateNodePos();
195 }
196
197 void TestCAO::removeFromScene(bool permanent)
198 {
199         if (!m_node)
200                 return;
201
202         m_node->remove();
203         m_node = NULL;
204 }
205
206 void TestCAO::updateLight(u8 light_at_pos)
207 {
208 }
209
210 v3s16 TestCAO::getLightPosition()
211 {
212         return floatToInt(m_position, BS);
213 }
214
215 void TestCAO::updateNodePos()
216 {
217         if (!m_node)
218                 return;
219
220         m_node->setPosition(m_position);
221         //m_node->setRotation(v3f(0, 45, 0));
222 }
223
224 void TestCAO::step(float dtime, ClientEnvironment *env)
225 {
226         if(m_node)
227         {
228                 v3f rot = m_node->getRotation();
229                 //infostream<<"dtime="<<dtime<<", rot.Y="<<rot.Y<<std::endl;
230                 rot.Y += dtime * 180;
231                 m_node->setRotation(rot);
232         }
233 }
234
235 void TestCAO::processMessage(const std::string &data)
236 {
237         infostream<<"TestCAO: Got data: "<<data<<std::endl;
238         std::istringstream is(data, std::ios::binary);
239         u16 cmd;
240         is>>cmd;
241         if(cmd == 0)
242         {
243                 v3f newpos;
244                 is>>newpos.X;
245                 is>>newpos.Y;
246                 is>>newpos.Z;
247                 m_position = newpos;
248                 updateNodePos();
249         }
250 }
251
252 /*
253         GenericCAO
254 */
255
256 #include "genericobject.h"
257
258 GenericCAO::GenericCAO(Client *client, ClientEnvironment *env):
259                 ClientActiveObject(0, client, env)
260 {
261         if (client == NULL) {
262                 ClientActiveObject::registerType(getType(), create);
263         } else {
264                 m_client = client;
265         }
266 }
267
268 bool GenericCAO::getCollisionBox(aabb3f *toset) const
269 {
270         if (m_prop.physical)
271         {
272                 //update collision box
273                 toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
274                 toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
275
276                 toset->MinEdge += m_position;
277                 toset->MaxEdge += m_position;
278
279                 return true;
280         }
281
282         return false;
283 }
284
285 bool GenericCAO::collideWithObjects() const
286 {
287         return m_prop.collideWithObjects;
288 }
289
290 void GenericCAO::initialize(const std::string &data)
291 {
292         infostream<<"GenericCAO: Got init data"<<std::endl;
293         processInitData(data);
294
295         if (m_is_player) {
296                 // Check if it's the current player
297                 LocalPlayer *player = m_env->getLocalPlayer();
298                 if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
299                         m_is_local_player = true;
300                         m_is_visible = false;
301                         player->setCAO(this);
302                 }
303                 if (m_client->getProtoVersion() < 33)
304                         m_env->addPlayerName(m_name);
305         }
306 }
307
308 void GenericCAO::processInitData(const std::string &data)
309 {
310         std::istringstream is(data, std::ios::binary);
311         int num_messages = 0;
312         // version
313         u8 version = readU8(is);
314         // check version
315         if (version == 1) { // In PROTOCOL_VERSION 14
316                 m_name = deSerializeString(is);
317                 m_is_player = readU8(is);
318                 m_id = readU16(is);
319                 m_position = readV3F1000(is);
320                 m_yaw = readF1000(is);
321                 m_hp = readS16(is);
322                 num_messages = readU8(is);
323         } else {
324                 errorstream<<"GenericCAO: Unsupported init data version"
325                                 <<std::endl;
326                 return;
327         }
328
329         for (int i = 0; i < num_messages; i++) {
330                 std::string message = deSerializeLongString(is);
331                 processMessage(message);
332         }
333
334         pos_translator.init(m_position);
335         updateNodePos();
336 }
337
338 GenericCAO::~GenericCAO()
339 {
340         if (m_is_player && m_client->getProtoVersion() < 33) {
341                 m_env->removePlayerName(m_name);
342         }
343         removeFromScene(true);
344 }
345
346 bool GenericCAO::getSelectionBox(aabb3f *toset) const
347 {
348         if (!m_prop.is_visible || !m_is_visible || m_is_local_player
349                         || getParent() != NULL){
350                 return false;
351         }
352         *toset = m_selection_box;
353         return true;
354 }
355
356 v3f GenericCAO::getPosition()
357 {
358         if (getParent() != NULL) {
359                 scene::ISceneNode *node = getSceneNode();
360                 if (node)
361                         return node->getAbsolutePosition();
362
363                 return m_position;
364         }
365         return pos_translator.vect_show;
366 }
367
368 scene::ISceneNode* GenericCAO::getSceneNode()
369 {
370         if (m_meshnode) {
371                 return m_meshnode;
372         }
373
374         if (m_animated_meshnode) {
375                 return m_animated_meshnode;
376         }
377
378         if (m_wield_meshnode) {
379                 return m_wield_meshnode;
380         }
381
382         if (m_spritenode) {
383                 return m_spritenode;
384         }
385         return NULL;
386 }
387
388 scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()
389 {
390         return m_animated_meshnode;
391 }
392
393 void GenericCAO::setChildrenVisible(bool toset)
394 {
395         for (u16 cao_id : m_children) {
396                 GenericCAO *obj = m_env->getGenericCAO(cao_id);
397                 if (obj) {
398                         obj->setVisible(toset);
399                 }
400         }
401 }
402
403 void GenericCAO::setAttachments()
404 {
405         updateAttachments();
406 }
407
408 ClientActiveObject* GenericCAO::getParent() const
409 {
410         ClientActiveObject *obj = NULL;
411
412         u16 attached_id = m_env->attachement_parent_ids[getId()];
413
414         if ((attached_id != 0) &&
415                         (attached_id != getId())) {
416                 obj = m_env->getActiveObject(attached_id);
417         }
418         return obj;
419 }
420
421 void GenericCAO::removeFromScene(bool permanent)
422 {
423         // Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
424         if((m_env != NULL) && (permanent))
425         {
426                 for (u16 ci : m_children) {
427                         if (m_env->attachement_parent_ids[ci] == getId()) {
428                                 m_env->attachement_parent_ids[ci] = 0;
429                         }
430                 }
431
432                 m_env->attachement_parent_ids[getId()] = 0;
433
434                 LocalPlayer* player = m_env->getLocalPlayer();
435                 if (this == player->parent) {
436                         player->parent = NULL;
437                         player->isAttached = false;
438                 }
439         }
440
441         if (m_meshnode) {
442                 m_meshnode->remove();
443                 m_meshnode->drop();
444                 m_meshnode = NULL;
445         } else if (m_animated_meshnode) {
446                 m_animated_meshnode->remove();
447                 m_animated_meshnode->drop();
448                 m_animated_meshnode = NULL;
449         } else if (m_wield_meshnode) {
450                 m_wield_meshnode->remove();
451                 m_wield_meshnode->drop();
452                 m_wield_meshnode = NULL;
453         } else if (m_spritenode) {
454                 m_spritenode->remove();
455                 m_spritenode->drop();
456                 m_spritenode = NULL;
457         }
458
459         if (m_nametag) {
460                 m_client->getCamera()->removeNametag(m_nametag);
461                 m_nametag = NULL;
462         }
463 }
464
465 void GenericCAO::addToScene(ITextureSource *tsrc)
466 {
467         m_smgr = RenderingEngine::get_scene_manager();
468
469         if (getSceneNode() != NULL) {
470                 return;
471         }
472
473         m_visuals_expired = false;
474
475         if (!m_prop.is_visible) {
476                 return;
477         }
478
479         if (m_prop.visual == "sprite") {
480                 infostream<<"GenericCAO::addToScene(): single_sprite"<<std::endl;
481                 m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
482                                 NULL, v2f(1, 1), v3f(0,0,0), -1);
483                 m_spritenode->grab();
484                 m_spritenode->setMaterialTexture(0,
485                                 tsrc->getTextureForMesh("unknown_node.png"));
486                 m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
487                 m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
488                 m_spritenode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
489                 m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
490                 u8 li = m_last_light;
491                 m_spritenode->setColor(video::SColor(255,li,li,li));
492                 m_spritenode->setSize(m_prop.visual_size*BS);
493                 {
494                         const float txs = 1.0 / 1;
495                         const float tys = 1.0 / 1;
496                         setBillboardTextureMatrix(m_spritenode,
497                                         txs, tys, 0, 0);
498                 }
499         } else if (m_prop.visual == "upright_sprite") {
500                 scene::SMesh *mesh = new scene::SMesh();
501                 double dx = BS * m_prop.visual_size.X / 2;
502                 double dy = BS * m_prop.visual_size.Y / 2;
503                 u8 li = m_last_light;
504                 video::SColor c(255, li, li, li);
505
506                 { // Front
507                         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
508                         video::S3DVertex vertices[4] = {
509                                 video::S3DVertex(-dx, -dy, 0, 0,0,0, c, 1,1),
510                                 video::S3DVertex( dx, -dy, 0, 0,0,0, c, 0,1),
511                                 video::S3DVertex( dx,  dy, 0, 0,0,0, c, 0,0),
512                                 video::S3DVertex(-dx,  dy, 0, 0,0,0, c, 1,0),
513                         };
514                         u16 indices[] = {0,1,2,2,3,0};
515                         buf->append(vertices, 4, indices, 6);
516                         // Set material
517                         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
518                         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
519                         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
520                         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
521                         // Add to mesh
522                         mesh->addMeshBuffer(buf);
523                         buf->drop();
524                 }
525                 { // Back
526                         scene::IMeshBuffer *buf = new scene::SMeshBuffer();
527                         video::S3DVertex vertices[4] = {
528                                 video::S3DVertex( dx,-dy, 0, 0,0,0, c, 1,1),
529                                 video::S3DVertex(-dx,-dy, 0, 0,0,0, c, 0,1),
530                                 video::S3DVertex(-dx, dy, 0, 0,0,0, c, 0,0),
531                                 video::S3DVertex( dx, dy, 0, 0,0,0, c, 1,0),
532                         };
533                         u16 indices[] = {0,1,2,2,3,0};
534                         buf->append(vertices, 4, indices, 6);
535                         // Set material
536                         buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
537                         buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
538                         buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
539                         buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
540                         // Add to mesh
541                         mesh->addMeshBuffer(buf);
542                         buf->drop();
543                 }
544                 m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
545                 m_meshnode->grab();
546                 mesh->drop();
547                 // Set it to use the materials of the meshbuffers directly.
548                 // This is needed for changing the texture in the future
549                 m_meshnode->setReadOnlyMaterials(true);
550         }
551         else if(m_prop.visual == "cube") {
552                 infostream<<"GenericCAO::addToScene(): cube"<<std::endl;
553                 scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
554                 m_meshnode = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
555                 m_meshnode->grab();
556                 mesh->drop();
557
558                 m_meshnode->setScale(v3f(m_prop.visual_size.X,
559                                 m_prop.visual_size.Y,
560                                 m_prop.visual_size.X));
561                 u8 li = m_last_light;
562                 setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
563
564                 m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
565                 m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
566                 m_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
567                 m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
568         }
569         else if(m_prop.visual == "mesh") {
570                 infostream<<"GenericCAO::addToScene(): mesh"<<std::endl;
571                 scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh);
572                 if(mesh)
573                 {
574                         m_animated_meshnode = RenderingEngine::get_scene_manager()->
575                                 addAnimatedMeshSceneNode(mesh, NULL);
576                         m_animated_meshnode->grab();
577                         mesh->drop(); // The scene node took hold of it
578                         m_animated_meshnode->animateJoints(); // Needed for some animations
579                         m_animated_meshnode->setScale(v3f(m_prop.visual_size.X,
580                                         m_prop.visual_size.Y,
581                                         m_prop.visual_size.X));
582                         u8 li = m_last_light;
583                         setMeshColor(m_animated_meshnode->getMesh(), video::SColor(255,li,li,li));
584
585                         bool backface_culling = m_prop.backface_culling;
586                         if (m_is_player)
587                                 backface_culling = false;
588
589                         m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
590                         m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
591                         m_animated_meshnode->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
592                         m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
593                         m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING, backface_culling);
594                 }
595                 else
596                         errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
597         } else if (m_prop.visual == "wielditem") {
598                 ItemStack item;
599                 infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
600                 if (m_prop.wield_item.empty()) {
601                         // Old format, only textures are specified.
602                         infostream << "textures: " << m_prop.textures.size() << std::endl;
603                         if (!m_prop.textures.empty()) {
604                                 infostream << "textures[0]: " << m_prop.textures[0]
605                                         << std::endl;
606                                 IItemDefManager *idef = m_client->idef();
607                                 item = ItemStack(m_prop.textures[0], 1, 0, idef);
608                         }
609                 } else {
610                         infostream << "serialized form: " << m_prop.wield_item << std::endl;
611                         item.deSerialize(m_prop.wield_item, m_client->idef());
612                 }
613                 m_wield_meshnode = new WieldMeshSceneNode(
614                         RenderingEngine::get_scene_manager(), -1);
615                 m_wield_meshnode->setItem(item, m_client);
616
617                 m_wield_meshnode->setScale(
618                         v3f(m_prop.visual_size.X / 2, m_prop.visual_size.Y / 2,
619                                 m_prop.visual_size.X / 2));
620                 u8 li = m_last_light;
621                 m_wield_meshnode->setColor(video::SColor(255, li, li, li));
622         } else {
623                 infostream<<"GenericCAO::addToScene(): \""<<m_prop.visual
624                                 <<"\" not supported"<<std::endl;
625         }
626
627         /* don't update while punch texture modifier is active */
628         if (m_reset_textures_timer < 0)
629                 updateTextures(m_current_texture_modifier);
630
631         scene::ISceneNode *node = getSceneNode();
632         if (node && !m_prop.nametag.empty() && !m_is_local_player) {
633                 // Add nametag
634                 v3f pos;
635                 pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
636                 m_nametag = m_client->getCamera()->addNametag(node,
637                         m_prop.nametag, m_prop.nametag_color,
638                         pos);
639         }
640
641         updateNodePos();
642         updateAnimation();
643         updateBonePosition();
644         updateAttachments();
645 }
646
647 void GenericCAO::updateLight(u8 light_at_pos)
648 {
649         // Don't update light of attached one
650         if (getParent() != NULL) {
651                 return;
652         }
653
654         updateLightNoCheck(light_at_pos);
655
656         // Update light of all children
657         for (u16 i : m_children) {
658                 ClientActiveObject *obj = m_env->getActiveObject(i);
659                 if (obj) {
660                         obj->updateLightNoCheck(light_at_pos);
661                 }
662         }
663 }
664
665 void GenericCAO::updateLightNoCheck(u8 light_at_pos)
666 {
667         u8 li = decode_light(light_at_pos);
668         if (li != m_last_light) {
669                 m_last_light = li;
670                 video::SColor color(255,li,li,li);
671                 if (m_meshnode) {
672                         setMeshColor(m_meshnode->getMesh(), color);
673                 } else if (m_animated_meshnode) {
674                         setMeshColor(m_animated_meshnode->getMesh(), color);
675                 } else if (m_wield_meshnode) {
676                         m_wield_meshnode->setColor(color);
677                 } else if (m_spritenode) {
678                         m_spritenode->setColor(color);
679                 }
680         }
681 }
682
683 v3s16 GenericCAO::getLightPosition()
684 {
685         if (m_is_player)
686                 return floatToInt(m_position + v3f(0, 0.5 * BS, 0), BS);
687
688         return floatToInt(m_position, BS);
689 }
690
691 void GenericCAO::updateNodePos()
692 {
693         if (getParent() != NULL)
694                 return;
695
696         scene::ISceneNode *node = getSceneNode();
697
698         if (node) {
699                 v3s16 camera_offset = m_env->getCameraOffset();
700                 node->setPosition(pos_translator.vect_show - intToFloat(camera_offset, BS));
701                 if (node != m_spritenode) { // rotate if not a sprite
702                         v3f rot = node->getRotation();
703                         rot.Y = -m_yaw;
704                         node->setRotation(rot);
705                 }
706         }
707 }
708
709 void GenericCAO::step(float dtime, ClientEnvironment *env)
710 {
711         // Handel model of local player instantly to prevent lags
712         if (m_is_local_player) {
713                 LocalPlayer *player = m_env->getLocalPlayer();
714                 if (m_is_visible) {
715                         int old_anim = player->last_animation;
716                         float old_anim_speed = player->last_animation_speed;
717                         m_position = player->getPosition();
718                         m_velocity = v3f(0,0,0);
719                         m_acceleration = v3f(0,0,0);
720                         pos_translator.vect_show = m_position;
721                         m_yaw = player->getYaw();
722                         const PlayerControl &controls = player->getPlayerControl();
723
724                         bool walking = false;
725                         if (controls.up || controls.down || controls.left || controls.right ||
726                                         controls.forw_move_joystick_axis != 0.f ||
727                                         controls.sidew_move_joystick_axis != 0.f)
728                                 walking = true;
729
730                         f32 new_speed = player->local_animation_speed;
731                         v2s32 new_anim = v2s32(0,0);
732                         bool allow_update = false;
733
734                         // increase speed if using fast or flying fast
735                         if((g_settings->getBool("fast_move") &&
736                                         m_client->checkLocalPrivilege("fast")) &&
737                                         (controls.aux1 ||
738                                         (!player->touching_ground &&
739                                         g_settings->getBool("free_move") &&
740                                         m_client->checkLocalPrivilege("fly"))))
741                                         new_speed *= 1.5;
742                         // slowdown speed if sneeking
743                         if (controls.sneak && walking)
744                                 new_speed /= 2;
745
746                         if (walking && (controls.LMB || controls.RMB)) {
747                                 new_anim = player->local_animations[3];
748                                 player->last_animation = WD_ANIM;
749                         } else if(walking) {
750                                 new_anim = player->local_animations[1];
751                                 player->last_animation = WALK_ANIM;
752                         } else if(controls.LMB || controls.RMB) {
753                                 new_anim = player->local_animations[2];
754                                 player->last_animation = DIG_ANIM;
755                         }
756
757                         // Apply animations if input detected and not attached
758                         // or set idle animation
759                         if ((new_anim.X + new_anim.Y) > 0 && !player->isAttached) {
760                                 allow_update = true;
761                                 m_animation_range = new_anim;
762                                 m_animation_speed = new_speed;
763                                 player->last_animation_speed = m_animation_speed;
764                         } else {
765                                 player->last_animation = NO_ANIM;
766
767                                 if (old_anim != NO_ANIM) {
768                                         m_animation_range = player->local_animations[0];
769                                         updateAnimation();
770                                 }
771                         }
772
773                         // Update local player animations
774                         if ((player->last_animation != old_anim ||
775                                 m_animation_speed != old_anim_speed) &&
776                                 player->last_animation != NO_ANIM && allow_update)
777                                         updateAnimation();
778
779                 }
780         }
781
782         if (m_visuals_expired && m_smgr) {
783                 m_visuals_expired = false;
784
785                 // Attachments, part 1: All attached objects must be unparented first,
786                 // or Irrlicht causes a segmentation fault
787                 for (auto ci = m_children.begin(); ci != m_children.end();) {
788                         if (m_env->attachement_parent_ids[*ci] != getId()) {
789                                 ci = m_children.erase(ci);
790                                 continue;
791                         }
792                         ClientActiveObject *obj = m_env->getActiveObject(*ci);
793                         if (obj) {
794                                 scene::ISceneNode *child_node = obj->getSceneNode();
795                                 if (child_node)
796                                         child_node->setParent(m_smgr->getRootSceneNode());
797                         }
798                         ++ci;
799                 }
800
801                 removeFromScene(false);
802                 addToScene(m_client->tsrc());
803
804                 // Attachments, part 2: Now that the parent has been refreshed, put its attachments back
805                 for (u16 cao_id : m_children) {
806                         // Get the object of the child
807                         ClientActiveObject *obj = m_env->getActiveObject(cao_id);
808                         if (obj)
809                                 obj->setAttachments();
810                 }
811         }
812
813         // Make sure m_is_visible is always applied
814         scene::ISceneNode *node = getSceneNode();
815         if (node)
816                 node->setVisible(m_is_visible);
817
818         if(getParent() != NULL) // Attachments should be glued to their parent by Irrlicht
819         {
820                 // Set these for later
821                 m_position = getPosition();
822                 m_velocity = v3f(0,0,0);
823                 m_acceleration = v3f(0,0,0);
824                 pos_translator.vect_show = m_position;
825
826                 if(m_is_local_player) // Update local player attachment position
827                 {
828                         LocalPlayer *player = m_env->getLocalPlayer();
829                         player->overridePosition = getParent()->getPosition();
830                         m_env->getLocalPlayer()->parent = getParent();
831                 }
832         } else {
833                 v3f lastpos = pos_translator.vect_show;
834
835                 if(m_prop.physical)
836                 {
837                         aabb3f box = m_prop.collisionbox;
838                         box.MinEdge *= BS;
839                         box.MaxEdge *= BS;
840                         collisionMoveResult moveresult;
841                         f32 pos_max_d = BS*0.125; // Distance per iteration
842                         v3f p_pos = m_position;
843                         v3f p_velocity = m_velocity;
844                         moveresult = collisionMoveSimple(env,env->getGameDef(),
845                                         pos_max_d, box, m_prop.stepheight, dtime,
846                                         &p_pos, &p_velocity, m_acceleration,
847                                         this, m_prop.collideWithObjects);
848                         // Apply results
849                         m_position = p_pos;
850                         m_velocity = p_velocity;
851
852                         bool is_end_position = moveresult.collides;
853                         pos_translator.update(m_position, is_end_position, dtime);
854                         pos_translator.translate(dtime);
855                         updateNodePos();
856                 } else {
857                         m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration;
858                         m_velocity += dtime * m_acceleration;
859                         pos_translator.update(m_position, pos_translator.aim_is_end,
860                                         pos_translator.anim_time);
861                         pos_translator.translate(dtime);
862                         updateNodePos();
863                 }
864
865                 float moved = lastpos.getDistanceFrom(pos_translator.vect_show);
866                 m_step_distance_counter += moved;
867                 if(m_step_distance_counter > 1.5*BS)
868                 {
869                         m_step_distance_counter = 0;
870                         if(!m_is_local_player && m_prop.makes_footstep_sound)
871                         {
872                                 INodeDefManager *ndef = m_client->ndef();
873                                 v3s16 p = floatToInt(getPosition() + v3f(0,
874                                                 (m_prop.collisionbox.MinEdge.Y-0.5)*BS, 0), BS);
875                                 MapNode n = m_env->getMap().getNodeNoEx(p);
876                                 SimpleSoundSpec spec = ndef->get(n).sound_footstep;
877                                 m_client->sound()->playSoundAt(spec, false, getPosition());
878                         }
879                 }
880         }
881
882         m_anim_timer += dtime;
883         if(m_anim_timer >= m_anim_framelength)
884         {
885                 m_anim_timer -= m_anim_framelength;
886                 m_anim_frame++;
887                 if(m_anim_frame >= m_anim_num_frames)
888                         m_anim_frame = 0;
889         }
890
891         updateTexturePos();
892
893         if(m_reset_textures_timer >= 0)
894         {
895                 m_reset_textures_timer -= dtime;
896                 if(m_reset_textures_timer <= 0) {
897                         m_reset_textures_timer = -1;
898                         updateTextures(m_previous_texture_modifier);
899                 }
900         }
901         if(!getParent() && fabs(m_prop.automatic_rotate) > 0.001)
902         {
903                 m_yaw += dtime * m_prop.automatic_rotate * 180 / M_PI;
904                 updateNodePos();
905         }
906
907         if (!getParent() && m_prop.automatic_face_movement_dir &&
908                         (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001))
909         {
910                 float optimal_yaw = atan2(m_velocity.Z,m_velocity.X) * 180 / M_PI
911                                 + m_prop.automatic_face_movement_dir_offset;
912                 float max_rotation_delta =
913                                 dtime * m_prop.automatic_face_movement_max_rotation_per_sec;
914
915                 if ((m_prop.automatic_face_movement_max_rotation_per_sec > 0) &&
916                         (fabs(m_yaw - optimal_yaw) > max_rotation_delta)) {
917
918                         m_yaw = optimal_yaw < m_yaw ? m_yaw - max_rotation_delta : m_yaw + max_rotation_delta;
919                 } else {
920                         m_yaw = optimal_yaw;
921                 }
922                 updateNodePos();
923         }
924 }
925
926 void GenericCAO::updateTexturePos()
927 {
928         if(m_spritenode)
929         {
930                 scene::ICameraSceneNode* camera =
931                                 m_spritenode->getSceneManager()->getActiveCamera();
932                 if(!camera)
933                         return;
934                 v3f cam_to_entity = m_spritenode->getAbsolutePosition()
935                                 - camera->getAbsolutePosition();
936                 cam_to_entity.normalize();
937
938                 int row = m_tx_basepos.Y;
939                 int col = m_tx_basepos.X;
940
941                 if(m_tx_select_horiz_by_yawpitch)
942                 {
943                         if(cam_to_entity.Y > 0.75)
944                                 col += 5;
945                         else if(cam_to_entity.Y < -0.75)
946                                 col += 4;
947                         else{
948                                 float mob_dir =
949                                                 atan2(cam_to_entity.Z, cam_to_entity.X) / M_PI * 180.;
950                                 float dir = mob_dir - m_yaw;
951                                 dir = wrapDegrees_180(dir);
952                                 //infostream<<"id="<<m_id<<" dir="<<dir<<std::endl;
953                                 if(fabs(wrapDegrees_180(dir - 0)) <= 45.1)
954                                         col += 2;
955                                 else if(fabs(wrapDegrees_180(dir - 90)) <= 45.1)
956                                         col += 3;
957                                 else if(fabs(wrapDegrees_180(dir - 180)) <= 45.1)
958                                         col += 0;
959                                 else if(fabs(wrapDegrees_180(dir + 90)) <= 45.1)
960                                         col += 1;
961                                 else
962                                         col += 4;
963                         }
964                 }
965
966                 // Animation goes downwards
967                 row += m_anim_frame;
968
969                 float txs = m_tx_size.X;
970                 float tys = m_tx_size.Y;
971                 setBillboardTextureMatrix(m_spritenode,
972                                 txs, tys, col, row);
973         }
974 }
975
976 void GenericCAO::updateTextures(std::string mod)
977 {
978         ITextureSource *tsrc = m_client->tsrc();
979
980         bool use_trilinear_filter = g_settings->getBool("trilinear_filter");
981         bool use_bilinear_filter = g_settings->getBool("bilinear_filter");
982         bool use_anisotropic_filter = g_settings->getBool("anisotropic_filter");
983
984         m_previous_texture_modifier = m_current_texture_modifier;
985         m_current_texture_modifier = mod;
986
987         if (m_spritenode) {
988                 if (m_prop.visual == "sprite") {
989                         std::string texturestring = "unknown_node.png";
990                         if (!m_prop.textures.empty())
991                                 texturestring = m_prop.textures[0];
992                         texturestring += mod;
993                         m_spritenode->setMaterialTexture(0,
994                                         tsrc->getTextureForMesh(texturestring));
995
996                         // This allows setting per-material colors. However, until a real lighting
997                         // system is added, the code below will have no effect. Once MineTest
998                         // has directional lighting, it should work automatically.
999                         if (!m_prop.colors.empty()) {
1000                                 m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
1001                                 m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
1002                                 m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
1003                         }
1004
1005                         m_spritenode->getMaterial(0).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1006                         m_spritenode->getMaterial(0).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1007                         m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1008                 }
1009         }
1010
1011         if (m_animated_meshnode) {
1012                 if (m_prop.visual == "mesh") {
1013                         for (u32 i = 0; i < m_prop.textures.size() &&
1014                                         i < m_animated_meshnode->getMaterialCount(); ++i) {
1015                                 std::string texturestring = m_prop.textures[i];
1016                                 if (texturestring.empty())
1017                                         continue; // Empty texture string means don't modify that material
1018                                 texturestring += mod;
1019                                 video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
1020                                 if (!texture) {
1021                                         errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
1022                                         continue;
1023                                 }
1024
1025                                 // Set material flags and texture
1026                                 video::SMaterial& material = m_animated_meshnode->getMaterial(i);
1027                                 material.TextureLayer[0].Texture = texture;
1028                                 material.setFlag(video::EMF_LIGHTING, false);
1029                                 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1030
1031                                 m_animated_meshnode->getMaterial(i)
1032                                                 .setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1033                                 m_animated_meshnode->getMaterial(i)
1034                                                 .setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1035                                 m_animated_meshnode->getMaterial(i)
1036                                                 .setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1037                         }
1038                         for (u32 i = 0; i < m_prop.colors.size() &&
1039                         i < m_animated_meshnode->getMaterialCount(); ++i)
1040                         {
1041                                 // This allows setting per-material colors. However, until a real lighting
1042                                 // system is added, the code below will have no effect. Once MineTest
1043                                 // has directional lighting, it should work automatically.
1044                                 m_animated_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1045                                 m_animated_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1046                                 m_animated_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1047                         }
1048                 }
1049         }
1050         if(m_meshnode)
1051         {
1052                 if(m_prop.visual == "cube")
1053                 {
1054                         for (u32 i = 0; i < 6; ++i)
1055                         {
1056                                 std::string texturestring = "unknown_node.png";
1057                                 if(m_prop.textures.size() > i)
1058                                         texturestring = m_prop.textures[i];
1059                                 texturestring += mod;
1060
1061
1062                                 // Set material flags and texture
1063                                 video::SMaterial& material = m_meshnode->getMaterial(i);
1064                                 material.setFlag(video::EMF_LIGHTING, false);
1065                                 material.setFlag(video::EMF_BILINEAR_FILTER, false);
1066                                 material.setTexture(0,
1067                                                 tsrc->getTextureForMesh(texturestring));
1068                                 material.getTextureMatrix(0).makeIdentity();
1069
1070                                 // This allows setting per-material colors. However, until a real lighting
1071                                 // system is added, the code below will have no effect. Once MineTest
1072                                 // has directional lighting, it should work automatically.
1073                                 if(m_prop.colors.size() > i)
1074                                 {
1075                                         m_meshnode->getMaterial(i).AmbientColor = m_prop.colors[i];
1076                                         m_meshnode->getMaterial(i).DiffuseColor = m_prop.colors[i];
1077                                         m_meshnode->getMaterial(i).SpecularColor = m_prop.colors[i];
1078                                 }
1079
1080                                 m_meshnode->getMaterial(i).setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1081                                 m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1082                                 m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1083                         }
1084                 } else if (m_prop.visual == "upright_sprite") {
1085                         scene::IMesh *mesh = m_meshnode->getMesh();
1086                         {
1087                                 std::string tname = "unknown_object.png";
1088                                 if (!m_prop.textures.empty())
1089                                         tname = m_prop.textures[0];
1090                                 tname += mod;
1091                                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
1092                                 buf->getMaterial().setTexture(0,
1093                                                 tsrc->getTextureForMesh(tname));
1094
1095                                 // This allows setting per-material colors. However, until a real lighting
1096                                 // system is added, the code below will have no effect. Once MineTest
1097                                 // has directional lighting, it should work automatically.
1098                                 if(!m_prop.colors.empty()) {
1099                                         buf->getMaterial().AmbientColor = m_prop.colors[0];
1100                                         buf->getMaterial().DiffuseColor = m_prop.colors[0];
1101                                         buf->getMaterial().SpecularColor = m_prop.colors[0];
1102                                 }
1103
1104                                 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1105                                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1106                                 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1107                         }
1108                         {
1109                                 std::string tname = "unknown_object.png";
1110                                 if (m_prop.textures.size() >= 2)
1111                                         tname = m_prop.textures[1];
1112                                 else if (!m_prop.textures.empty())
1113                                         tname = m_prop.textures[0];
1114                                 tname += mod;
1115                                 scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
1116                                 buf->getMaterial().setTexture(0,
1117                                                 tsrc->getTextureForMesh(tname));
1118
1119                                 // This allows setting per-material colors. However, until a real lighting
1120                                 // system is added, the code below will have no effect. Once MineTest
1121                                 // has directional lighting, it should work automatically.
1122                                 if (m_prop.colors.size() >= 2) {
1123                                         buf->getMaterial().AmbientColor = m_prop.colors[1];
1124                                         buf->getMaterial().DiffuseColor = m_prop.colors[1];
1125                                         buf->getMaterial().SpecularColor = m_prop.colors[1];
1126                                 } else if (!m_prop.colors.empty()) {
1127                                         buf->getMaterial().AmbientColor = m_prop.colors[0];
1128                                         buf->getMaterial().DiffuseColor = m_prop.colors[0];
1129                                         buf->getMaterial().SpecularColor = m_prop.colors[0];
1130                                 }
1131
1132                                 buf->getMaterial().setFlag(video::EMF_TRILINEAR_FILTER, use_trilinear_filter);
1133                                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
1134                                 buf->getMaterial().setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
1135                         }
1136                 }
1137         }
1138 }
1139
1140 void GenericCAO::updateAnimation()
1141 {
1142         if (!m_animated_meshnode)
1143                 return;
1144
1145         if (m_animated_meshnode->getStartFrame() != m_animation_range.X ||
1146                 m_animated_meshnode->getEndFrame() != m_animation_range.Y)
1147                         m_animated_meshnode->setFrameLoop(m_animation_range.X, m_animation_range.Y);
1148         if (m_animated_meshnode->getAnimationSpeed() != m_animation_speed)
1149                 m_animated_meshnode->setAnimationSpeed(m_animation_speed);
1150         m_animated_meshnode->setTransitionTime(m_animation_blend);
1151 // Requires Irrlicht 1.8 or greater
1152 #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR > 1
1153         if (m_animated_meshnode->getLoopMode() != m_animation_loop)
1154                 m_animated_meshnode->setLoopMode(m_animation_loop);
1155 #endif
1156 }
1157
1158 void GenericCAO::updateBonePosition()
1159 {
1160         if(m_bone_position.empty() || !m_animated_meshnode)
1161                 return;
1162
1163         m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render
1164         for(std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
1165                         ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
1166                 std::string bone_name = (*ii).first;
1167                 v3f bone_pos = (*ii).second.X;
1168                 v3f bone_rot = (*ii).second.Y;
1169                 irr::scene::IBoneSceneNode* bone = m_animated_meshnode->getJointNode(bone_name.c_str());
1170                 if(bone)
1171                 {
1172                         bone->setPosition(bone_pos);
1173                         bone->setRotation(bone_rot);
1174                 }
1175         }
1176 }
1177
1178 void GenericCAO::updateAttachments()
1179 {
1180
1181         if (!getParent()) { // Detach or don't attach
1182                 scene::ISceneNode *node = getSceneNode();
1183                 if (node) {
1184                         v3f old_position = node->getAbsolutePosition();
1185                         v3f old_rotation = node->getRotation();
1186                         node->setParent(m_smgr->getRootSceneNode());
1187                         node->setPosition(old_position);
1188                         node->setRotation(old_rotation);
1189                         node->updateAbsolutePosition();
1190                 }
1191                 if (m_is_local_player) {
1192                         LocalPlayer *player = m_env->getLocalPlayer();
1193                         player->isAttached = false;
1194                 }
1195         }
1196         else // Attach
1197         {
1198                 scene::ISceneNode *my_node = getSceneNode();
1199
1200                 scene::ISceneNode *parent_node = getParent()->getSceneNode();
1201                 scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
1202                                 getParent()->getAnimatedMeshSceneNode();
1203                 if (parent_animated_mesh_node && !m_attachment_bone.empty()) {
1204                         parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
1205                 }
1206
1207                 if (my_node && parent_node) {
1208                         my_node->setParent(parent_node);
1209                         my_node->setPosition(m_attachment_position);
1210                         my_node->setRotation(m_attachment_rotation);
1211                         my_node->updateAbsolutePosition();
1212                 }
1213                 if (m_is_local_player) {
1214                         LocalPlayer *player = m_env->getLocalPlayer();
1215                         player->isAttached = true;
1216                 }
1217         }
1218 }
1219
1220 void GenericCAO::processMessage(const std::string &data)
1221 {
1222         //infostream<<"GenericCAO: Got message"<<std::endl;
1223         std::istringstream is(data, std::ios::binary);
1224         // command
1225         u8 cmd = readU8(is);
1226         if (cmd == GENERIC_CMD_SET_PROPERTIES) {
1227                 m_prop = gob_read_set_properties(is);
1228
1229                 m_selection_box = m_prop.collisionbox;
1230                 m_selection_box.MinEdge *= BS;
1231                 m_selection_box.MaxEdge *= BS;
1232
1233                 m_tx_size.X = 1.0 / m_prop.spritediv.X;
1234                 m_tx_size.Y = 1.0 / m_prop.spritediv.Y;
1235
1236                 if(!m_initial_tx_basepos_set){
1237                         m_initial_tx_basepos_set = true;
1238                         m_tx_basepos = m_prop.initial_sprite_basepos;
1239                 }
1240                 if (m_is_local_player) {
1241                         LocalPlayer *player = m_env->getLocalPlayer();
1242                         player->makes_footstep_sound = m_prop.makes_footstep_sound;
1243                         player->setCollisionbox(m_selection_box);
1244                 }
1245
1246                 if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
1247                         m_prop.nametag = m_name;
1248
1249                 expireVisuals();
1250         } else if (cmd == GENERIC_CMD_UPDATE_POSITION) {
1251                 // Not sent by the server if this object is an attachment.
1252                 // We might however get here if the server notices the object being detached before the client.
1253                 m_position = readV3F1000(is);
1254                 m_velocity = readV3F1000(is);
1255                 m_acceleration = readV3F1000(is);
1256                 if(fabs(m_prop.automatic_rotate) < 0.001)
1257                         m_yaw = readF1000(is);
1258                 else
1259                         readF1000(is);
1260                 bool do_interpolate = readU8(is);
1261                 bool is_end_position = readU8(is);
1262                 float update_interval = readF1000(is);
1263
1264                 // Place us a bit higher if we're physical, to not sink into
1265                 // the ground due to sucky collision detection...
1266                 if(m_prop.physical)
1267                         m_position += v3f(0,0.002,0);
1268
1269                 if(getParent() != NULL) // Just in case
1270                         return;
1271
1272                 if(do_interpolate)
1273                 {
1274                         if(!m_prop.physical)
1275                                 pos_translator.update(m_position, is_end_position, update_interval);
1276                 } else {
1277                         pos_translator.init(m_position);
1278                 }
1279                 updateNodePos();
1280         } else if (cmd == GENERIC_CMD_SET_TEXTURE_MOD) {
1281                 std::string mod = deSerializeString(is);
1282
1283                 // immediatly reset a engine issued texture modifier if a mod sends a different one
1284                 if (m_reset_textures_timer > 0) {
1285                         m_reset_textures_timer = -1;
1286                         updateTextures(m_previous_texture_modifier);
1287                 }
1288                 updateTextures(mod);
1289         } else if (cmd == GENERIC_CMD_SET_SPRITE) {
1290                 v2s16 p = readV2S16(is);
1291                 int num_frames = readU16(is);
1292                 float framelength = readF1000(is);
1293                 bool select_horiz_by_yawpitch = readU8(is);
1294
1295                 m_tx_basepos = p;
1296                 m_anim_num_frames = num_frames;
1297                 m_anim_framelength = framelength;
1298                 m_tx_select_horiz_by_yawpitch = select_horiz_by_yawpitch;
1299
1300                 updateTexturePos();
1301         } else if (cmd == GENERIC_CMD_SET_PHYSICS_OVERRIDE) {
1302                 float override_speed = readF1000(is);
1303                 float override_jump = readF1000(is);
1304                 float override_gravity = readF1000(is);
1305                 // these are sent inverted so we get true when the server sends nothing
1306                 bool sneak = !readU8(is);
1307                 bool sneak_glitch = !readU8(is);
1308                 bool new_move = !readU8(is);
1309
1310
1311                 if(m_is_local_player)
1312                 {
1313                         LocalPlayer *player = m_env->getLocalPlayer();
1314                         player->physics_override_speed = override_speed;
1315                         player->physics_override_jump = override_jump;
1316                         player->physics_override_gravity = override_gravity;
1317                         player->physics_override_sneak = sneak;
1318                         player->physics_override_sneak_glitch = sneak_glitch;
1319                         player->physics_override_new_move = new_move;
1320                 }
1321         } else if (cmd == GENERIC_CMD_SET_ANIMATION) {
1322                 // TODO: change frames send as v2s32 value
1323                 v2f range = readV2F1000(is);
1324                 if (!m_is_local_player) {
1325                         m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1326                         m_animation_speed = readF1000(is);
1327                         m_animation_blend = readF1000(is);
1328                         // these are sent inverted so we get true when the server sends nothing
1329                         m_animation_loop = !readU8(is);
1330                         updateAnimation();
1331                 } else {
1332                         LocalPlayer *player = m_env->getLocalPlayer();
1333                         if(player->last_animation == NO_ANIM)
1334                         {
1335                                 m_animation_range = v2s32((s32)range.X, (s32)range.Y);
1336                                 m_animation_speed = readF1000(is);
1337                                 m_animation_blend = readF1000(is);
1338                                 // these are sent inverted so we get true when the server sends nothing
1339                                 m_animation_loop = !readU8(is);
1340                         }
1341                         // update animation only if local animations present
1342                         // and received animation is unknown (except idle animation)
1343                         bool is_known = false;
1344                         for (int i = 1;i<4;i++)
1345                         {
1346                                 if(m_animation_range.Y == player->local_animations[i].Y)
1347                                         is_known = true;
1348                         }
1349                         if(!is_known ||
1350                                         (player->local_animations[1].Y + player->local_animations[2].Y < 1))
1351                         {
1352                                         updateAnimation();
1353                         }
1354                 }
1355         } else if (cmd == GENERIC_CMD_SET_BONE_POSITION) {
1356                 std::string bone = deSerializeString(is);
1357                 v3f position = readV3F1000(is);
1358                 v3f rotation = readV3F1000(is);
1359                 m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
1360
1361                 updateBonePosition();
1362         } else if (cmd == GENERIC_CMD_ATTACH_TO) {
1363                 u16 parentID = readS16(is);
1364                 u16 oldparent = m_env->attachement_parent_ids[getId()];
1365                 if (oldparent) {
1366                         m_children.erase(std::remove(m_children.begin(), m_children.end(),
1367                                 getId()), m_children.end());
1368                 }
1369                 m_env->attachement_parent_ids[getId()] = parentID;
1370                 GenericCAO *parentobj = m_env->getGenericCAO(parentID);
1371
1372                 if (parentobj) {
1373                         parentobj->m_children.push_back(getId());
1374                 }
1375
1376                 m_attachment_bone = deSerializeString(is);
1377                 m_attachment_position = readV3F1000(is);
1378                 m_attachment_rotation = readV3F1000(is);
1379
1380                 // localplayer itself can't be attached to localplayer
1381                 if (!m_is_local_player) {
1382                         m_attached_to_local = getParent() != NULL && getParent()->isLocalPlayer();
1383                         // Objects attached to the local player should be hidden by default
1384                         m_is_visible = !m_attached_to_local;
1385                 }
1386
1387                 updateAttachments();
1388         } else if (cmd == GENERIC_CMD_PUNCHED) {
1389                 /*s16 damage =*/ readS16(is);
1390                 s16 result_hp = readS16(is);
1391
1392                 // Use this instead of the send damage to not interfere with prediction
1393                 s16 damage = m_hp - result_hp;
1394
1395                 m_hp = result_hp;
1396
1397                 if (damage > 0)
1398                 {
1399                         if (m_hp <= 0)
1400                         {
1401                                 // TODO: Execute defined fast response
1402                                 // As there is no definition, make a smoke puff
1403                                 ClientSimpleObject *simple = createSmokePuff(
1404                                                 m_smgr, m_env, m_position,
1405                                                 m_prop.visual_size * BS);
1406                                 m_env->addSimpleObject(simple);
1407                         } else if (m_reset_textures_timer < 0) {
1408                                 // TODO: Execute defined fast response
1409                                 // Flashing shall suffice as there is no definition
1410                                 m_reset_textures_timer = 0.05;
1411                                 if(damage >= 2)
1412                                         m_reset_textures_timer += 0.05 * damage;
1413                                 updateTextures(m_current_texture_modifier + "^[brighten");
1414                         }
1415                 }
1416         } else if (cmd == GENERIC_CMD_UPDATE_ARMOR_GROUPS) {
1417                 m_armor_groups.clear();
1418                 int armor_groups_size = readU16(is);
1419                 for(int i=0; i<armor_groups_size; i++)
1420                 {
1421                         std::string name = deSerializeString(is);
1422                         int rating = readS16(is);
1423                         m_armor_groups[name] = rating;
1424                 }
1425         } else if (cmd == GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) {
1426                 // Deprecated, for backwards compatibility only.
1427                 readU8(is); // version
1428                 m_prop.nametag_color = readARGB8(is);
1429                 if (m_nametag != NULL) {
1430                         m_nametag->nametag_color = m_prop.nametag_color;
1431                         v3f pos;
1432                         pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
1433                         m_nametag->nametag_pos = pos;
1434                 }
1435         } else if (cmd == GENERIC_CMD_SPAWN_INFANT) {
1436                 u16 child_id = readU16(is);
1437                 u8 type = readU8(is);
1438
1439                 if (GenericCAO *childobj = m_env->getGenericCAO(child_id)) {
1440                         childobj->processInitData(deSerializeLongString(is));
1441                 } else {
1442                         m_env->addActiveObject(child_id, type, deSerializeLongString(is));
1443                 }
1444         } else {
1445                 warningstream << FUNCTION_NAME
1446                         << ": unknown command or outdated client \""
1447                         << +cmd << "\"" << std::endl;
1448         }
1449 }
1450
1451 /* \pre punchitem != NULL
1452  */
1453 bool GenericCAO::directReportPunch(v3f dir, const ItemStack *punchitem,
1454                 float time_from_last_punch)
1455 {
1456         assert(punchitem);      // pre-condition
1457         const ToolCapabilities *toolcap =
1458                         &punchitem->getToolCapabilities(m_client->idef());
1459         PunchDamageResult result = getPunchDamage(
1460                         m_armor_groups,
1461                         toolcap,
1462                         punchitem,
1463                         time_from_last_punch);
1464
1465         if(result.did_punch && result.damage != 0)
1466         {
1467                 if(result.damage < m_hp)
1468                 {
1469                         m_hp -= result.damage;
1470                 } else {
1471                         m_hp = 0;
1472                         // TODO: Execute defined fast response
1473                         // As there is no definition, make a smoke puff
1474                         ClientSimpleObject *simple = createSmokePuff(
1475                                         m_smgr, m_env, m_position,
1476                                         m_prop.visual_size * BS);
1477                         m_env->addSimpleObject(simple);
1478                 }
1479                 // TODO: Execute defined fast response
1480                 // Flashing shall suffice as there is no definition
1481                 if (m_reset_textures_timer < 0) {
1482                         m_reset_textures_timer = 0.05;
1483                         if (result.damage >= 2)
1484                                 m_reset_textures_timer += 0.05 * result.damage;
1485                         updateTextures(m_current_texture_modifier + "^[brighten");
1486                 }
1487         }
1488
1489         return false;
1490 }
1491
1492 std::string GenericCAO::debugInfoText()
1493 {
1494         std::ostringstream os(std::ios::binary);
1495         os<<"GenericCAO hp="<<m_hp<<"\n";
1496         os<<"armor={";
1497         for(ItemGroupList::const_iterator i = m_armor_groups.begin();
1498                         i != m_armor_groups.end(); ++i)
1499         {
1500                 os<<i->first<<"="<<i->second<<", ";
1501         }
1502         os<<"}";
1503         return os.str();
1504 }
1505
1506 // Prototype
1507 GenericCAO proto_GenericCAO(NULL, NULL);