]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/content_cao.h
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.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 struct MinimapMarker;
34
35 /*
36         SmoothTranslator
37 */
38
39 template<typename T>
40 struct SmoothTranslator
41 {
42         T val_old;
43         T val_current;
44         T val_target;
45         f32 anim_time = 0;
46         f32 anim_time_counter = 0;
47         bool aim_is_end = true;
48
49         SmoothTranslator() = default;
50
51         void init(T current);
52
53         void update(T new_target, bool is_end_position = false,
54                 float update_interval = -1);
55
56         void translate(f32 dtime);
57 };
58
59 struct SmoothTranslatorWrapped : SmoothTranslator<f32>
60 {
61         void translate(f32 dtime);
62 };
63
64 struct SmoothTranslatorWrappedv3f : SmoothTranslator<v3f>
65 {
66         void translate(f32 dtime);
67 };
68
69 class GenericCAO : public ClientActiveObject
70 {
71 private:
72         // Only set at initialization
73         std::string m_name = "";
74         bool m_is_player = false;
75         bool m_is_local_player = false;
76         // Property-ish things
77         ObjectProperties m_prop;
78         //
79         scene::ISceneManager *m_smgr = nullptr;
80         Client *m_client = nullptr;
81         aabb3f m_selection_box = aabb3f(-BS/3.,-BS/3.,-BS/3., BS/3.,BS/3.,BS/3.);
82         scene::IMeshSceneNode *m_meshnode = nullptr;
83         scene::IAnimatedMeshSceneNode *m_animated_meshnode = nullptr;
84         WieldMeshSceneNode *m_wield_meshnode = nullptr;
85         scene::IBillboardSceneNode *m_spritenode = nullptr;
86         scene::IDummyTransformationSceneNode *m_matrixnode = nullptr;
87         Nametag *m_nametag = nullptr;
88         MinimapMarker *m_marker = nullptr;
89         v3f m_position = v3f(0.0f, 10.0f * BS, 0);
90         v3f m_velocity;
91         v3f m_acceleration;
92         v3f m_rotation;
93         u16 m_hp = 1;
94         SmoothTranslator<v3f> pos_translator;
95         SmoothTranslatorWrappedv3f rot_translator;
96         // Spritesheet/animation stuff
97         v2f m_tx_size = v2f(1,1);
98         v2s16 m_tx_basepos;
99         bool m_initial_tx_basepos_set = false;
100         bool m_tx_select_horiz_by_yawpitch = false;
101         v2s32 m_animation_range;
102         float m_animation_speed = 15.0f;
103         float m_animation_blend = 0.0f;
104         bool m_animation_loop = true;
105         // stores position and rotation for each bone name
106         std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
107
108         int m_attachment_parent_id = 0;
109         std::unordered_set<int> m_attachment_child_ids;
110         std::string m_attachment_bone = "";
111         v3f m_attachment_position;
112         v3f m_attachment_rotation;
113         bool m_attached_to_local = false;
114         bool m_force_visible = false;
115
116         int m_anim_frame = 0;
117         int m_anim_num_frames = 1;
118         float m_anim_framelength = 0.2f;
119         float m_anim_timer = 0.0f;
120         ItemGroupList m_armor_groups;
121         float m_reset_textures_timer = -1.0f;
122         // stores texture modifier before punch update
123         std::string m_previous_texture_modifier = "";
124         // last applied texture modifier
125         std::string m_current_texture_modifier = "";
126         bool m_visuals_expired = false;
127         float m_step_distance_counter = 0.0f;
128         video::SColor m_last_light = video::SColor(0xFFFFFFFF);
129         bool m_is_visible = false;
130         s8 m_glow = 0;
131         // Material
132         video::E_MATERIAL_TYPE m_material_type;
133         // Settings
134         bool m_enable_shaders = false;
135
136         bool visualExpiryRequired(const ObjectProperties &newprops) const;
137
138 public:
139         GenericCAO(Client *client, ClientEnvironment *env);
140
141         ~GenericCAO();
142
143         static ClientActiveObject* create(Client *client, ClientEnvironment *env)
144         {
145                 return new GenericCAO(client, env);
146         }
147
148         inline ActiveObjectType getType() const
149         {
150                 return ACTIVEOBJECT_TYPE_GENERIC;
151         }
152         inline const ItemGroupList &getGroups() const
153         {
154                 return m_armor_groups;
155         }
156         void initialize(const std::string &data);
157
158         void processInitData(const std::string &data);
159
160         bool getCollisionBox(aabb3f *toset) const;
161
162         bool collideWithObjects() const;
163
164         virtual bool getSelectionBox(aabb3f *toset) const;
165
166         const v3f getPosition() const;
167
168         void setPosition(const v3f &pos)
169         {
170                 pos_translator.val_current = pos;
171         }
172
173         inline const v3f &getRotation() const { return m_rotation; }
174
175         inline const v3f getAcceleration() const
176         {
177                 return m_acceleration;
178         }
179
180         inline const v3f getVelocity() const
181         {
182                 return m_velocity;
183         }
184
185         inline u16 getHp() const
186         {
187                 return m_hp;
188         }
189
190         bool isImmortal() const;
191
192         inline const ObjectProperties &getProperties() const { return m_prop; }
193
194         scene::ISceneNode *getSceneNode() const;
195
196         scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const;
197
198         // m_matrixnode controls the position and rotation of the child node
199         // for all scene nodes, as a workaround for an Irrlicht problem with
200         // rotations. The child node's position can't be used because it's
201         // rotated, and must remain as 0.
202         // Note that m_matrixnode.setPosition() shouldn't be called. Use
203         // m_matrixnode->getRelativeTransformationMatrix().setTranslation()
204         // instead (aka getPosRotMatrix().setTranslation()).
205         inline core::matrix4 &getPosRotMatrix()
206         {
207                 assert(m_matrixnode);
208                 return m_matrixnode->getRelativeTransformationMatrix();
209         }
210
211         inline const core::matrix4 *getAbsolutePosRotMatrix() const
212         {
213                 if (!m_matrixnode)
214                         return nullptr;
215                 return &m_matrixnode->getAbsoluteTransformation();
216         }
217
218         inline f32 getStepHeight() const
219         {
220                 return m_prop.stepheight;
221         }
222
223         inline bool isLocalPlayer() const
224         {
225                 return m_is_local_player;
226         }
227
228         inline std::string getName() const
229         {
230                 return m_name;
231         }
232
233         inline bool isPlayer() const
234         {
235                 return m_is_player;
236         }
237
238         inline bool isVisible() const
239         {
240                 return m_is_visible;
241         }
242
243         inline void setVisible(bool toset)
244         {
245                 m_is_visible = toset;
246         }
247
248         void setChildrenVisible(bool toset);
249         void setAttachment(int parent_id, const std::string &bone, v3f position,
250                         v3f rotation, bool force_visible);
251         void getAttachment(int *parent_id, std::string *bone, v3f *position,
252                         v3f *rotation, bool *force_visible) const;
253         void clearChildAttachments();
254         void clearParentAttachment();
255         void addAttachmentChild(int child_id);
256         void removeAttachmentChild(int child_id);
257         ClientActiveObject *getParent() const;
258         int getParentId() const { return m_attachment_parent_id; }
259         const std::unordered_set<int> &getAttachmentChildIds() const
260         { return m_attachment_child_ids; }
261         void updateAttachments();
262
263         void removeFromScene(bool permanent);
264
265         void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr);
266
267         inline void expireVisuals()
268         {
269                 m_visuals_expired = true;
270         }
271
272         void updateLight(u32 day_night_ratio);
273
274         void setNodeLight(const video::SColor &light);
275
276         /* Get light position(s).
277          * returns number of positions written into pos[], which must have space
278          * for at least 3 vectors. */
279         u16 getLightPosition(v3s16 *pos);
280
281         void updateNametag();
282
283         void updateMarker();
284
285         void updateNodePos();
286
287         void step(float dtime, ClientEnvironment *env);
288
289         void updateTexturePos();
290
291         // ffs this HAS TO BE a string copy! See #5739 if you think otherwise
292         // Reason: updateTextures(m_previous_texture_modifier);
293         void updateTextures(std::string mod);
294
295         void updateAnimation();
296
297         void updateAnimationSpeed();
298
299         void updateBonePosition();
300
301         void processMessage(const std::string &data);
302
303         bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
304                         float time_from_last_punch=1000000);
305
306         std::string debugInfoText();
307
308         std::string infoText()
309         {
310                 return m_prop.infotext;
311         }
312
313         float m_waiting_for_reattach;
314
315         ObjectProperties *getProperties()
316         {
317                 return &m_prop;
318         }
319
320         void setProperties(ObjectProperties newprops);
321
322         void updateMeshCulling();
323
324         std::vector<std::string> nametag_images = {};
325 };