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