]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/content_cao.h
DevTest: Fix broken PNG textures
[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         bool isImmortal() const;
176
177         inline const ObjectProperties &getProperties() const { return m_prop; }
178
179         scene::ISceneNode *getSceneNode() const;
180
181         scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() const;
182
183         // m_matrixnode controls the position and rotation of the child node
184         // for all scene nodes, as a workaround for an Irrlicht problem with
185         // rotations. The child node's position can't be used because it's
186         // rotated, and must remain as 0.
187         // Note that m_matrixnode.setPosition() shouldn't be called. Use
188         // m_matrixnode->getRelativeTransformationMatrix().setTranslation()
189         // instead (aka getPosRotMatrix().setTranslation()).
190         inline core::matrix4 &getPosRotMatrix()
191         {
192                 assert(m_matrixnode);
193                 return m_matrixnode->getRelativeTransformationMatrix();
194         }
195
196         inline const core::matrix4 *getAbsolutePosRotMatrix() const
197         {
198                 if (!m_matrixnode)
199                         return nullptr;
200                 return &m_matrixnode->getAbsoluteTransformation();
201         }
202
203         inline f32 getStepHeight() const
204         {
205                 return m_prop.stepheight;
206         }
207
208         inline bool isLocalPlayer() const
209         {
210                 return m_is_local_player;
211         }
212
213         inline bool isVisible() const
214         {
215                 return m_is_visible;
216         }
217
218         inline void setVisible(bool toset)
219         {
220                 m_is_visible = toset;
221         }
222
223         void setChildrenVisible(bool toset);
224         void setAttachment(int parent_id, const std::string &bone, v3f position,
225                         v3f rotation, bool force_visible);
226         void getAttachment(int *parent_id, std::string *bone, v3f *position,
227                         v3f *rotation, bool *force_visible) const;
228         void clearChildAttachments();
229         void clearParentAttachment();
230         void addAttachmentChild(int child_id);
231         void removeAttachmentChild(int child_id);
232         ClientActiveObject *getParent() const;
233         const std::unordered_set<int> &getAttachmentChildIds() const
234         { return m_attachment_child_ids; }
235         void updateAttachments();
236
237         void removeFromScene(bool permanent);
238
239         void addToScene(ITextureSource *tsrc, scene::ISceneManager *smgr);
240
241         inline void expireVisuals()
242         {
243                 m_visuals_expired = true;
244         }
245
246         void updateLight(u32 day_night_ratio);
247
248         void setNodeLight(const video::SColor &light);
249
250         /* Get light position(s).
251          * returns number of positions written into pos[], which must have space
252          * for at least 3 vectors. */
253         u16 getLightPosition(v3s16 *pos);
254
255         void updateNametag();
256
257         void updateMarker();
258
259         void updateNodePos();
260
261         void step(float dtime, ClientEnvironment *env);
262
263         void updateTexturePos();
264
265         // ffs this HAS TO BE a string copy! See #5739 if you think otherwise
266         // Reason: updateTextures(m_previous_texture_modifier);
267         void updateTextures(std::string mod);
268
269         void updateAnimation();
270
271         void updateAnimationSpeed();
272
273         void updateBonePosition();
274
275         void processMessage(const std::string &data);
276
277         bool directReportPunch(v3f dir, const ItemStack *punchitem=NULL,
278                         float time_from_last_punch=1000000);
279
280         std::string debugInfoText();
281
282         std::string infoText()
283         {
284                 return m_prop.infotext;
285         }
286
287         void updateMeshCulling();
288 };