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