Luanti 5.17.0-dev
Loading...
Searching...
No Matches
l_particleparams.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2021 velartrill, Lexi Hale <lexi@hale.su>
4
5#pragma once
6#include "lua_api/l_object.h"
8#include "common/c_content.h"
10#include "particles.h"
11
13{
14 using namespace ParticleParamTypes;
15
16 template<typename T>
17 inline void readNumericLuaValue(lua_State* L, T& ret)
18 {
19 if (lua_isnil(L,-1))
20 return;
21
22 if (std::is_integral<T>())
23 ret = lua_tointeger(L, -1);
24 else
25 ret = lua_tonumber(L, -1);
26 }
27
28 template <typename T, size_t N>
29 inline void readNumericLuaValue(lua_State* L, Parameter<T,N>& ret)
30 {
32 }
33
34 // these are unfortunately necessary as C++ intentionally disallows function template
35 // specialization and there's no way to make template overloads reliably resolve correctly
36 inline void readLuaValue(lua_State* L, f32Parameter& ret) { readNumericLuaValue(L, ret); }
37 inline void readLuaValue(lua_State* L, f32& ret) { readNumericLuaValue(L, ret); }
38 inline void readLuaValue(lua_State* L, u16& ret) { readNumericLuaValue(L, ret); }
39 inline void readLuaValue(lua_State* L, u8& ret) { readNumericLuaValue(L, ret); }
40
41 inline void readLuaValue(lua_State* L, v3fParameter& ret)
42 {
43 if (lua_isnil(L, -1))
44 return;
45
46 if (lua_isnumber(L, -1)) { // shortcut for uniform vectors
47 auto n = lua_tonumber(L, -1);
48 ret = v3fParameter(n,n,n);
49 } else {
50 ret = (v3fParameter)check_v3f(L, -1);
51 }
52 }
53
54 inline void readLuaValue(lua_State* L, v2fParameter& ret)
55 {
56 if (lua_isnil(L, -1))
57 return;
58
59 if (lua_isnumber(L, -1)) { // shortcut for uniform vectors
60 auto n = lua_tonumber(L, -1);
61 ret = v2fParameter(n,n);
62 } else {
63 ret = (v2fParameter)check_v2f(L, -1);
64 }
65 }
66
67 inline void readLuaValue(lua_State* L, TweenStyle& ret)
68 {
69 if (lua_isnil(L, -1))
70 return;
71
72 static const EnumString opts[] = {
73 {(int)TweenStyle::fwd, "fwd"},
74 {(int)TweenStyle::rev, "rev"},
75 {(int)TweenStyle::pulse, "pulse"},
76 {(int)TweenStyle::flicker, "flicker"},
77 {0, nullptr},
78 };
79
80 luaL_checktype(L, -1, LUA_TSTRING);
81 int v = (int)TweenStyle::fwd;
82 if (!string_to_enum(opts, v, lua_tostring(L, -1))) {
83 throw LuaError("tween style must be one of ('fwd', 'rev', 'pulse', 'flicker')");
84 }
85 ret = (TweenStyle)v;
86 }
87
88 inline void readLuaValue(lua_State* L, AttractorKind& ret)
89 {
90 if (lua_isnil(L, -1))
91 return;
92
93 static const EnumString opts[] = {
94 {(int)AttractorKind::none, "none"},
95 {(int)AttractorKind::point, "point"},
96 {(int)AttractorKind::line, "line"},
97 {(int)AttractorKind::plane, "plane"},
98 {0, nullptr},
99 };
100
101 luaL_checktype(L, -1, LUA_TSTRING);
102 int v = (int)AttractorKind::none;
103 if (!string_to_enum(opts, v, lua_tostring(L, -1))) {
104 throw LuaError("attractor kind must be one of ('none', 'point', 'line', 'plane')");
105 }
106 ret = (AttractorKind)v;
107 }
108
109 inline void readLuaValue(lua_State* L, BlendMode& ret)
110 {
111 if (lua_isnil(L, -1))
112 return;
113
114 static const EnumString opts[] = {
115 {(int)BlendMode::alpha, "alpha"},
116 {(int)BlendMode::add, "add"},
117 {(int)BlendMode::sub, "sub"},
118 {(int)BlendMode::screen, "screen"},
119 {(int)BlendMode::clip, "clip"},
120 {0, nullptr},
121 };
122
123 luaL_checktype(L, -1, LUA_TSTRING);
124 int v = (int)BlendMode::alpha;
125 if (!string_to_enum(opts, v, lua_tostring(L, -1))) {
126 throw LuaError("blend mode must be one of ('alpha', 'clip', 'add', 'sub', 'screen')");
127 }
128 ret = (BlendMode)v;
129 }
130
131 template <typename T> void
132 readLuaValue(lua_State* L, RangedParameter<T>& field)
133 {
134 if (lua_isnil(L,-1))
135 return;
136 if (!lua_istable(L,-1)) // is this is just a literal value?
137 goto set_uniform;
138
139 lua_getfield(L, -1, "min");
140 // handle convenience syntax for non-range values
141 if (lua_isnil(L,-1)) {
142 lua_pop(L, 1);
143 goto set_uniform;
144 }
145 readLuaValue(L,field.min);
146 lua_pop(L, 1);
147
148 lua_getfield(L, -1, "max");
149 readLuaValue(L,field.max);
150 lua_pop(L, 1);
151
152 lua_getfield(L, -1, "bias");
153 if (!lua_isnil(L,-1))
154 readLuaValue(L,field.bias);
155 lua_pop(L, 1);
156 return;
157
158 set_uniform:
159 readLuaValue(L, field.min);
160 readLuaValue(L, field.max);
161 }
162
163 template <typename T> void
164 readLegacyValue(lua_State* L, const char* name, T& field) {}
165
166 template <typename T> void
167 readLegacyValue(lua_State* L, const char* name, RangedParameter<T>& field)
168 {
169 int tbl = lua_gettop(L);
170 lua_pushliteral(L, "min");
171 lua_pushstring(L, name);
172 lua_concat(L, 2);
173 lua_gettable(L, tbl);
174 if (!lua_isnil(L, -1)) {
175 readLuaValue(L, field.min);
176 }
177 lua_settop(L, tbl);
178
179 lua_pushliteral(L, "max");
180 lua_pushstring(L, name);
181 lua_concat(L, 2);
182 lua_gettable(L, tbl);
183 if (!lua_isnil(L, -1)) {
184 readLuaValue(L, field.max);
185 }
186 lua_settop(L, tbl);
187 }
188
189 template <typename T> void
190 readTweenTable(lua_State* L, const char* name, TweenedParameter<T>& field)
191 {
192 int tbl = lua_gettop(L);
193
194 lua_pushstring(L, name);
195 lua_pushliteral(L, "_tween");
196 lua_concat(L, 2);
197 lua_gettable(L, tbl);
198 if(lua_istable(L, -1)) {
199 int tween = lua_gettop(L);
200 // get the starting value
201 lua_pushinteger(L, 1), lua_gettable(L, tween);
202 readLuaValue(L, field.start);
203 lua_pop(L, 1);
204
205 // get the final value -- use len instead of 2 so that this
206 // gracefully degrades if keyframe support is later added
207 lua_pushinteger(L, (lua_Integer)lua_objlen(L, -1)), lua_gettable(L, tween);
208 readLuaValue(L, field.end);
209 lua_pop(L, 1);
210
211 // get the effect settings
212 lua_getfield(L, -1, "style");
213 if (!lua_isnil(L,-1))
214 readLuaValue(L, field.style);
215 lua_pop(L, 1);
216
217 lua_getfield(L, -1, "reps");
218 if (!lua_isnil(L,-1))
219 readLuaValue(L, field.reps);
220 lua_pop(L, 1);
221
222 lua_getfield(L, -1, "start");
223 if (!lua_isnil(L,-1))
224 readLuaValue(L, field.beginning);
225 lua_pop(L, 1);
226
227 goto done;
228 } else {
229 lua_pop(L,1);
230 }
231 // the table is not present; check for nonanimated values
232
233 lua_getfield(L, tbl, name);
234 if(!lua_isnil(L, -1)) {
235 readLuaValue(L, field.start);
236 lua_settop(L, tbl);
237 goto set_uniform;
238 } else {
239 lua_pop(L,1);
240 }
241
242 // the goto did not trigger, so this table is not present either
243 // check for pre-5.6.0 legacy values
244 readLegacyValue(L, name, field.start);
245
246 set_uniform:
247 field.end = field.start;
248 done:
249 lua_settop(L, tbl); // clean up after ourselves
250 }
251
252 inline u16 readAttachmentID(lua_State* L, const char* name)
253 {
254 u16 id = 0;
255 lua_getfield(L, -1, name);
256 if (!lua_isnil(L, -1)) {
258 if (auto obj = ObjectRef::getobject(ref))
259 id = obj->getId();
260 }
261 lua_pop(L, 1);
262 return id;
263 }
264
265 void readTexValue(lua_State* L, ServerParticleTexture& tex);
266}
v2f check_v2f(lua_State *L, int index)
Definition c_converter.cpp:162
v3f check_v3f(lua_State *L, int index)
Definition c_converter.cpp:180
Definition c_types.h:32
static T * checkObject(lua_State *L, int narg)
Definition l_base.h:97
Definition l_object.h:20
static ServerActiveObject * getobject(ObjectRef *ref)
Definition l_object.cpp:38
bool string_to_enum(const EnumString *spec, int &result, std::string_view str)
Definition enum_string.cpp:8
Definition l_particleparams.h:13
void readNumericLuaValue(lua_State *L, T &ret)
Definition l_particleparams.h:17
u16 readAttachmentID(lua_State *L, const char *name)
Definition l_particleparams.h:252
void readTweenTable(lua_State *L, const char *name, TweenedParameter< T > &field)
Definition l_particleparams.h:190
void readTexValue(lua_State *L, ServerParticleTexture &tex)
Definition l_particles.cpp:15
void readLegacyValue(lua_State *L, const char *name, T &field)
Definition l_particleparams.h:164
void readLuaValue(lua_State *L, f32Parameter &ret)
Definition l_particleparams.h:36
Definition particles.cpp:110
Parameter< f32, 1 > f32Parameter
Definition particles.h:121
VectorParameter< v3f, 3 > v3fParameter
Definition particles.h:123
TweenStyle
Definition particles.h:181
@ pulse
Definition particles.h:181
@ flicker
Definition particles.h:181
@ fwd
Definition particles.h:181
@ rev
Definition particles.h:181
VectorParameter< v2f, 2 > v2fParameter
Definition particles.h:122
BlendMode
Definition particles.h:228
AttractorKind
Definition particles.h:226
@ none
Definition particles.h:226
@ line
Definition particles.h:226
@ point
Definition particles.h:226
@ plane
Definition particles.h:226
Definition enum_string.h:11
Definition particles.h:66
T val
Definition particles.h:70
Definition particles.h:129
T max
Definition particles.h:133
f32 bias
Definition particles.h:134
T min
Definition particles.h:133
Definition particles.h:186
T start
Definition particles.h:194
f32 beginning
Definition particles.h:192
T end
Definition particles.h:194
u16 reps
Definition particles.h:191
TweenStyle style
Definition particles.h:190
Definition particles.h:254