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