Use true pitch/yaw/roll rotations without loss of precision by pgimeno (#8019)
[oweals/minetest.git] / src / client / content_cao.h
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 #pragma once
21
22 #include <map>
23 #include "irrlichttypes_extrabloated.h"
24 #include "clientobject.h"
25 #include "object_properties.h"
26 #include "itemgroup.h"
27 #include "constants.h"
28 #include <cassert>
29
30 class Camera;
31 class Client;
32 struct Nametag;
33
34 /*
35         SmoothTranslator
36 */
37
38 template<typename T>
39 struct SmoothTranslator
40 {
41         T val_old;
42         T val_current;
43         T val_target;
44         f32 anim_time = 0;
45         f32 anim_time_counter = 0;
46         bool aim_is_end = true;
47
48         SmoothTranslator() = default;
49
50         void init(T current);
51
52         void update(T new_target, bool is_end_position = false,
53                 float update_interval = -1);
54
55         void translate(f32 dtime);
56 };
57
58 struct SmoothTranslatorWrapped : SmoothTranslator<f32>
59 {
60         void translate(f32 dtime);
61 };
62
63 struct SmoothTranslatorWrappedv3f : SmoothTranslator<v3f>
64 {
65         void translate(f32 dtime);
66 };
67
68 class GenericCAO : public ClientActiveObject
69 {
70 private:
71         // Only set at initialization
72         std::string m_name = "";
73         bool m_is_player = false;
74         bool m_is_local_player = false;
75         // Property-ish things
76         ObjectProperties m_prop;
77         //
78         scene::ISceneManager *m_smgr = nullptr;
79         Client *m_client = nullptr;
80         aabb3f m_selection_box = aabb3f(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.);
81         scene::IMeshSceneNode *m_meshnode = nullptr;
82         scene::IAnimatedMeshSceneNode *m_animated_meshnode = nullptr;
83         WieldMeshSceneNode *m_wield_meshnode = nullptr;
84         scene::IBillboardSceneNode *m_spritenode = nullptr;
85         scene::IDummyTransformationSceneNode *m_matrixnode = nullptr;
86         Nametag *m_nametag = nullptr;
87         v3f m_position = v3f(0.0f, 10.0f * BS, 0);
88         v3f m_velocity;
89         v3f m_acceleration;
90         v3f m_rotation;
91         s16 m_hp = 1;
92         SmoothTranslator<v3f> pos_translator;
93         SmoothTranslatorWrappedv3f rot_translator;
94         // Spritesheet/animation stuff
95         v2f m_tx_size = v2f(1,1);
96         v2s16 m_tx_basepos;
97         bool m_initial_tx_basepos_set = false;
98         bool m_tx_select_horiz_by_yawpitch = false;
99         v2s32 m_animation_range;
100         float m_animation_speed = 15.0f;
101         float m_animation_blend = 0.0f;
102         bool m_animation_loop = true;
103         // stores position and rotation for each bone name
104         std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
105         std::string m_attachment_bone = "";
106         v3f m_attachment_position;
107         v3f m_attachment_rotation;
108         bool m_attached_to_local = false;
109         int m_anim_frame = 0;
110         int m_anim_num_frames = 1;
111         float m_anim_framelength = 0.2f;
112         float m_anim_timer = 0.0f;
113         ItemGroupList m_armor_groups;
114         float m_reset_textures_timer = -1.0f;
115         // stores texture modifier before punch update
116         std::string m_previous_texture_modifier = "";
117         // last applied texture modifier
118         std::string m_current_texture_modifier = "";
119         bool m_visuals_expired = false;
120         float m_step_distance_counter = 0.0f;
121         u8 m_last_light = 255;
122         bool m_is_visible = false;
123         s8 m_glow = 0;
124
125         std::vector<u16> m_children;
126
127 public:
128         GenericCAO(Client *client, ClientEnvironment *env);
129
130         ~GenericCAO();
131
132         static ClientActiveObject* create(Client *client, ClientEnvironment *env)
133         {
134                 return new GenericCAO(client, env);
135         }
136
137         inline ActiveObjectType getType() const
138         {
139                 return ACTIVEOBJECT_TYPE_GENERIC;
140         }
141         inline const ItemGroupList &getGroups() const
142         {
143                 return m_armor_groups;
144         }
145         void initialize(const std::string &data);
146
147         void processInitData(const std::string &data);
148
149         bool getCollisionBox(aabb3f *toset) const;
150
151         bool collideWithObjects() const;
152
153         virtual bool getSelectionBox(aabb3f *toset) const;
154
155         v3f getPosition();
156
157         inline const v3f &getRotation()
158         {
159                 return m_rotation;
160         }
161
162         const bool isImmortal();
163
164         scene::ISceneNode *getSceneNode();
165
166         scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode();
167
168         // m_matrixnode controls the position and rotation of the child node
169         // for all scene nodes, as a workaround for an Irrlicht problem with
170         // rotations. The child node's position can't be used because it's
171         // rotated, and must remain as 0.
172         // Note that m_matrixnode.setPosition() shouldn't be called. Use
173         // m_matrixnode->getRelativeTransformationMatrix().setTranslation()
174         // instead (aka getPosRotMatrix().setTranslation()).
175         inline core::matrix4 &getPosRotMatrix()
176         {
177                 assert(m_matrixnode);
178                 return m_matrixnode->getRelativeTransformationMatrix();
179         }
180
181         inline f32 getStepHeight() const
182         {
183                 return m_prop.stepheight;
184         }
185
186         inline bool isLocalPlayer() const
187         {
188                 return m_is_local_player;
189         }
190
191         inline bool isVisible() const
192         {
193                 return m_is_visible;
194         }
195
196         inline void setVisible(bool toset)
197         {
198                 m_is_visible = toset;
199         }
200
201         void setChildrenVisible(bool toset);
202
203         ClientActiveObject *getParent() const;
204
205         void setAttachments();
206
207         void removeFromScene(bool permanent);
208
209         void addToScene(ITextureSource *tsrc);
210
211         inline void expireVisuals()
212         {
213                 m_visuals_expired = true;
214         }
215
216         void updateLight(u8 light_at_pos);
217
218         void updateLightNoCheck(u8 light_at_pos);
219
220         v3s16 getLightPosition();
221
222         void updateNodePos();
223
224         void step(float dtime, ClientEnvironment *env);
225
226         void updateTexturePos();
227
228         // std::string copy is mandatory as mod can be a class member and there is a swap
229         // on those class members... do NOT pass by reference
230         void updateTextures(std::string mod);
231
232         void updateAnimation();
233
234         void updateAnimationSpeed();
235
236         void updateBonePosition();
237
238         void updateAttachments();
239
240         void processMessage(const std::string &data);
241
242         bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
243                         float time_from_last_punch=1000000);
244
245         std::string debugInfoText();
246
247         std::string infoText()
248         {
249                 return m_prop.infotext;
250         }
251 };