Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
particles.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <string>
8#include <sstream>
9#include <vector>
10#include <type_traits>
13#include "tileanimation.h"
14#include "mapnode.h"
15#include "util/serialize.h"
16#include "util/numeric.h"
17
18// This file defines the particle-related structures that both the server and
19// client need. The ParticleManager and rendering is in client/particles.h
20
21namespace ParticleParamTypes
22{
23 template <bool cond, typename T>
24 using enableIf = typename std::enable_if<cond, T>::type;
25 // std::enable_if_t does not appear to be present in GCC????
26 // std::is_enum_v also missing. wtf. these are supposed to be
27 // present as of c++14
28
29 template<typename T> using BlendFunction = T(float,T,T);
30 #define DECL_PARAM_SRZRS(type) \
31 void serializeParameterValue (std::ostream& os, type v); \
32 void deSerializeParameterValue(std::istream& is, type& r);
33 #define DECL_PARAM_OVERLOADS(type) DECL_PARAM_SRZRS(type) \
34 type interpolateParameterValue(float fac, const type a, const type b); \
35 type pickParameterValue (float* facs, const type a, const type b);
36
37 // Function definition: see "particles.cpp"
44
45 /* C++ is a strongly typed language. this means that enums cannot be implicitly
46 * cast to integers, as they can be in C. while this may sound good in principle,
47 * it means that our normal serialization functions cannot be called on
48 * enumerations unless they are explicitly cast to a particular type first. this
49 * is problematic, because in C++ enums can have any integral type as an underlying
50 * type, and that type would need to be named everywhere an enumeration is
51 * de/serialized.
52 *
53 * this is obviously not cool, both in terms of writing legible, succinct code,
54 * and in terms of robustness: the underlying type might be changed at some point,
55 * e.g. if a bitmask gets too big for its britches. we could use an equivalent of
56 * `std::to_underlying(value)` everywhere we need to deal with enumerations, but
57 * that's hideous and unintuitive. instead, we supply the following functions to
58 * transparently map enumeration types to their underlying values. */
59
60 template <typename E, enableIf<std::is_enum<E>::value, bool> = true>
61 void serializeParameterValue(std::ostream& os, E k) {
62 serializeParameterValue(os, (std::underlying_type_t<E>)k);
63 }
64
65 template <typename E, enableIf<std::is_enum<E>::value, bool> = true>
66 void deSerializeParameterValue(std::istream& is, E& k) {
67 std::underlying_type_t<E> v;
69 k = (E)v;
70 }
71
72 // Describes a single value
73 template <typename T, size_t PN>
74 struct Parameter
75 {
76 using ValType = T;
77 using pickFactors = float[PN];
78
79 T val = T();
81
82 Parameter() = default;
83
84 template <typename... Args>
85 Parameter(Args... args) : val(args...) {}
86
87 virtual void serialize(std::ostream &os) const
88 { serializeParameterValue (os, this->val); }
89 virtual void deSerialize(std::istream &is)
90 { deSerializeParameterValue(is, this->val); }
91
92 virtual T interpolate(float fac, const This& against) const
93 {
94 return interpolateParameterValue(fac, this->val, against.val);
95 }
96
97 static T pick(float* f, const This& a, const This& b)
98 {
99 return pickParameterValue(f, a.val, b.val);
100 }
101
102 operator T() const { return val; }
103 T operator=(T b) { return val = b; }
104
105 };
106
107 // New struct required to differentiate between core::vectorNd-compatible
108 // structs for proper value dumping (debugging)
109 template <typename T, size_t N>
110 struct VectorParameter : public Parameter<T,N> {
112 template <typename... Args>
113 VectorParameter(Args... args) : Parameter<T,N>(args...) {}
114 };
115
116 template <typename T, size_t PN>
117 inline std::string dump(const Parameter<T,PN>& p)
118 {
119 return std::to_string(p.val);
120 }
121
122 template <typename T, size_t N>
123 inline std::string dump(const VectorParameter<T,N>& v)
124 {
125 std::ostringstream oss;
126 oss << v.val;
127 return oss.str();
128 }
129
133 // Add more parameter types here if you need them ...
134
135 // Bound limits information based on "Parameter" types
136 template <typename T>
138 {
139 using ValType = T;
141
143 f32 bias = 0;
144
145 RangedParameter() = default;
146 RangedParameter(T _min, T _max) : min(_min), max(_max) {}
147 template <typename M> RangedParameter(M b) : min(b), max(b) {}
148
149 // Binary format must not be changed. Function is to be deprecated.
150 void legacySerialize(std::ostream &os) const
151 {
152 min.serialize(os);
153 max.serialize(os);
154 }
155 void legacyDeSerialize(std::istream &is)
156 {
157 min.deSerialize(is);
158 max.deSerialize(is);
159 }
160
161 void serialize(std::ostream &os) const;
162 void deSerialize(std::istream &is);
163
164 This interpolate(float fac, const This against) const
165 {
166 This r;
167 r.min = min.interpolate(fac, against.min);
168 r.max = max.interpolate(fac, against.max);
169 r.bias = bias;
170 return r;
171 }
172
173 // Pick a random value (e.g. position) within bounds
174 T pickWithin() const;
175 };
176
177 template <typename T>
178 inline std::string dump(const RangedParameter<T>& r)
179 {
180 std::ostringstream s;
181 s << "range<" << dump(r.min) << " ~ " << dump(r.max);
182 if (r.bias != 0)
183 s << " :: " << r.bias;
184 s << ">";
185 return s.str();
186 }
187
188 // Animation styles (fwd is normal, linear interpolation)
189 // TweenStyle_END is a dummy value for validity check
191
192 // "Tweened" pretty much means "animated" in this context
193 template <typename T>
195 {
196 using ValType = T;
198
200 u16 reps = 1; // Blending repetitions (same pattern)
201 f32 beginning = 0.0f; // Blending start offset
202
204
205 TweenedParameter() = default;
206 TweenedParameter(T _start, T _end) : start(_start), end(_end) {}
207 // For initializer lists and assignment
208 template <typename M> TweenedParameter(M b) : start(b), end(b) {}
209
210 // Blend (or animate) the current value
211 T blend(float fac) const;
212
213 void serialize(std::ostream &os) const;
214 void deSerialize(std::istream &is);
215 };
216
217 template <typename T>
218 inline std::string dump(const TweenedParameter<T>& t)
219 {
220 std::ostringstream s;
221 const char* icon;
222 switch (t.style) {
223 case TweenStyle::fwd: icon = "→"; break;
224 case TweenStyle::rev: icon = "←"; break;
225 case TweenStyle::pulse: icon = "↔"; break;
226 case TweenStyle::flicker: icon = "↯"; break;
227 }
228 s << "tween<";
229 if (t.reps != 1)
230 s << t.reps << "x ";
231 s << dump(t.start) << " "<<icon<<" " << dump(t.end) << ">";
232 return s.str();
233 }
234
235 enum class AttractorKind : u8 { none, point, line, plane };
236 // Note: Allows at most 8 enum members (due to how this is serialized)
237 enum class BlendMode : u8 { alpha, add, sub, screen, clip, BlendMode_END };
238
239 // these are consistently-named convenience aliases to make code more readable without `using ParticleParamTypes` declarations
242
248
249 #undef DECL_PARAM_SRZRS
250 #undef DECL_PARAM_OVERLOADS
251}
252
261
263{
264 std::string string;
265 void serialize(std::ostream &os, u16 protocol_ver, bool newPropertiesOnly = false,
266 bool skipAnimation = false) const;
267 void deSerialize(std::istream &is, u16 protocol_ver, bool newPropertiesOnly = false,
268 bool skipAnimation = false);
269};
270
272{
273 bool collisiondetection = false;
274 bool collision_removal = false;
275 bool object_collision = false;
276 bool vertical = false;
279 u8 glow = 0;
281 u8 node_tile = 0;
282
287
288 /* This helper is useful for copying params from
289 * ParticleSpawnerParameters to ParticleParameters */
301};
302
304{
306 f32 size = 1, expirationtime = 1;
309
310 void serialize(std::ostream &os, u16 protocol_ver) const;
311 void deSerialize(std::istream &is, u16 protocol_ver);
312};
313
315{
316 u16 amount = 1;
317 f32 time = 1;
318
319 std::vector<ServerParticleTexture> texpool;
320
323
328 // object IDs
331 // do particles disappear when they cross the attractor threshold?
332 bool attractor_kill = true;
333
335 exptime{1.0f},
336 size {1.0f},
337 attract{0.0f},
338 bounce {0.0f};
339
340 // For historical reasons no (de-)serialization methods here
341};
core::vector2d< f32 > v2f
Definition irr_v2d.h:11
core::vector3df v3f
Definition irr_v3d.h:11
#define CONTENT_IGNORE
Definition mapnode.h:58
Definition particles.cpp:108
std::string dump(const Parameter< T, PN > &p)
Definition particles.h:117
typename std::enable_if< cond, T >::type enableIf
Definition particles.h:24
void serializeParameterValue(std::ostream &os, E k)
Definition particles.h:61
T(float, T, T) BlendFunction
Definition particles.h:29
void deSerializeParameterValue(std::istream &is, E &k)
Definition particles.h:66
TweenStyle
Definition particles.h:190
BlendMode
Definition particles.h:237
AttractorKind
Definition particles.h:235
#define DECL_PARAM_OVERLOADS(type)
Definition particles.h:33
Definition particles.h:272
bool object_collision
Definition particles.h:275
MapNode node
Definition particles.h:280
bool collisiondetection
Definition particles.h:273
struct TileAnimationParams animation
Definition particles.h:278
ServerParticleTexture texture
Definition particles.h:277
void copyCommon(CommonParticleParams &to) const
Definition particles.h:290
u8 glow
Definition particles.h:279
bool collision_removal
Definition particles.h:274
bool vertical
Definition particles.h:276
CommonParticleParams()
Definition particles.h:283
u8 node_tile
Definition particles.h:281
Definition mapnode.h:124
void setContent(content_t c) noexcept
Definition mapnode.h:166
Definition particles.h:75
T ValType
Definition particles.h:76
T val
Definition particles.h:79
Parameter(Args... args)
Definition particles.h:85
virtual T interpolate(float fac, const This &against) const
Definition particles.h:92
virtual void deSerialize(std::istream &is)
Definition particles.h:89
T operator=(T b)
Definition particles.h:103
static T pick(float *f, const This &a, const This &b)
Definition particles.h:97
float[PN] pickFactors
Definition particles.h:77
virtual void serialize(std::ostream &os) const
Definition particles.h:87
Definition particles.h:138
T ValType
Definition particles.h:139
T max
Definition particles.h:142
f32 bias
Definition particles.h:143
T pickWithin() const
Definition particles.cpp:28
void deSerialize(std::istream &is)
Definition particles.cpp:19
T min
Definition particles.h:142
void legacySerialize(std::ostream &os) const
Definition particles.h:150
void legacyDeSerialize(std::istream &is)
Definition particles.h:155
RangedParameter(M b)
Definition particles.h:147
void serialize(std::ostream &os) const
Definition particles.cpp:11
RangedParameter(T _min, T _max)
Definition particles.h:146
This interpolate(float fac, const This against) const
Definition particles.h:164
Definition particles.h:195
TweenedParameter(M b)
Definition particles.h:208
void deSerialize(std::istream &is)
Definition particles.cpp:97
T start
Definition particles.h:203
void serialize(std::ostream &os) const
Definition particles.cpp:87
T ValType
Definition particles.h:196
f32 beginning
Definition particles.h:201
TweenedParameter(T _start, T _end)
Definition particles.h:206
T end
Definition particles.h:203
u16 reps
Definition particles.h:200
TweenStyle style
Definition particles.h:199
Definition particles.h:110
VectorParameter(Args... args)
Definition particles.h:113
Definition particles.h:304
v3f pos
Definition particles.h:305
void serialize(std::ostream &os, u16 protocol_ver) const
Definition particles.cpp:232
v3f acc
Definition particles.h:305
v3f vel
Definition particles.h:305
v3f drag
Definition particles.h:305
ParticleParamTypes::v3fRange jitter
Definition particles.h:308
f32 size
Definition particles.h:306
f32 expirationtime
Definition particles.h:306
void deSerialize(std::istream &is, u16 protocol_ver)
Definition particles.cpp:266
ParticleParamTypes::f32Range bounce
Definition particles.h:307
Definition particles.h:315
ParticleParamTypes::v3fTween attractor_direction
Definition particles.h:327
ParticleParamTypes::v3fRangeTween vel
Definition particles.h:322
std::vector< ServerParticleTexture > texpool
Definition particles.h:319
u16 amount
Definition particles.h:316
ParticleParamTypes::v3fRangeTween jitter
Definition particles.h:322
ParticleParamTypes::AttractorKind attractor_kind
Definition particles.h:325
ParticleParamTypes::v3fRangeTween drag
Definition particles.h:322
u16 attractor_attachment
Definition particles.h:329
ParticleParamTypes::v3fRangeTween pos
Definition particles.h:322
ParticleParamTypes::v3fRangeTween radius
Definition particles.h:322
u16 attractor_direction_attachment
Definition particles.h:330
ParticleParamTypes::f32RangeTween attract
Definition particles.h:337
ParticleParamTypes::v3fTween attractor_origin
Definition particles.h:327
ParticleParamTypes::f32RangeTween bounce
Definition particles.h:338
f32 time
Definition particles.h:317
ParticleParamTypes::v3fRangeTween acc
Definition particles.h:322
ParticleParamTypes::f32RangeTween size
Definition particles.h:336
ParticleParamTypes::f32RangeTween exptime
Definition particles.h:335
bool attractor_kill
Definition particles.h:332
Definition particles.h:254
bool animated
Definition particles.h:255
ParticleParamTypes::BlendMode blendmode
Definition particles.h:256
ParticleParamTypes::f32Tween alpha
Definition particles.h:258
ParticleParamTypes::v2fTween scale
Definition particles.h:259
TileAnimationParams animation
Definition particles.h:257
Definition particles.h:263
void serialize(std::ostream &os, u16 protocol_ver, bool newPropertiesOnly=false, bool skipAnimation=false) const
Definition particles.cpp:185
std::string string
Definition particles.h:264
void deSerialize(std::istream &is, u16 protocol_ver, bool newPropertiesOnly=false, bool skipAnimation=false)
Definition particles.cpp:208
Definition tileanimation.h:18
enum TileAnimationType type
Definition tileanimation.h:19
static std::string p(std::string path)
Definition test_filesys.cpp:53
@ TAT_NONE
Definition tileanimation.h:12