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