Luanti 5.10.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 {0, nullptr},
122 };
123
124 luaL_checktype(L, -1, LUA_TSTRING);
125 int v = (int)BlendMode::alpha;
126 if (!string_to_enum(opts, v, lua_tostring(L, -1))) {
127 throw LuaError("blend mode must be one of ('alpha', 'add', 'sub', 'screen')");
128 }
129 ret = (BlendMode)v;
130 }
131
132 template <typename T> void
133 readLuaValue(lua_State* L, RangedParameter<T>& field)
134 {
135 if (lua_isnil(L,-1))
136 return;
137 if (!lua_istable(L,-1)) // is this is just a literal value?
138 goto set_uniform;
139
140 lua_getfield(L, -1, "min");
141 // handle convenience syntax for non-range values
142 if (lua_isnil(L,-1)) {
143 lua_pop(L, 1);
144 goto set_uniform;
145 }
146 readLuaValue(L,field.min);
147 lua_pop(L, 1);
148
149 lua_getfield(L, -1, "max");
150 readLuaValue(L,field.max);
151 lua_pop(L, 1);
152
153 lua_getfield(L, -1, "bias");
154 if (!lua_isnil(L,-1))
155 readLuaValue(L,field.bias);
156 lua_pop(L, 1);
157 return;
158
159 set_uniform:
160 readLuaValue(L, field.min);
161 readLuaValue(L, field.max);
162 }
163
164 template <typename T> void
165 readLegacyValue(lua_State* L, const char* name, T& field) {}
166
167 template <typename T> void
168 readLegacyValue(lua_State* L, const char* name, RangedParameter<T>& field)
169 {
170 int tbl = lua_gettop(L);
171 lua_pushliteral(L, "min");
172 lua_pushstring(L, name);
173 lua_concat(L, 2);
174 lua_gettable(L, tbl);
175 if (!lua_isnil(L, -1)) {
176 readLuaValue(L, field.min);
177 }
178 lua_settop(L, tbl);
179
180 lua_pushliteral(L, "max");
181 lua_pushstring(L, name);
182 lua_concat(L, 2);
183 lua_gettable(L, tbl);
184 if (!lua_isnil(L, -1)) {
185 readLuaValue(L, field.max);
186 }
187 lua_settop(L, tbl);
188 }
189
190 template <typename T> void
191 readTweenTable(lua_State* L, const char* name, TweenedParameter<T>& field)
192 {
193 int tbl = lua_gettop(L);
194
195 lua_pushstring(L, name);
196 lua_pushliteral(L, "_tween");
197 lua_concat(L, 2);
198 lua_gettable(L, tbl);
199 if(lua_istable(L, -1)) {
200 int tween = lua_gettop(L);
201 // get the starting value
202 lua_pushinteger(L, 1), lua_gettable(L, tween);
203 readLuaValue(L, field.start);
204 lua_pop(L, 1);
205
206 // get the final value -- use len instead of 2 so that this
207 // gracefully degrades if keyframe support is later added
208 lua_pushinteger(L, (lua_Integer)lua_objlen(L, -1)), lua_gettable(L, tween);
209 readLuaValue(L, field.end);
210 lua_pop(L, 1);
211
212 // get the effect settings
213 lua_getfield(L, -1, "style");
214 if (!lua_isnil(L,-1))
215 readLuaValue(L, field.style);
216 lua_pop(L, 1);
217
218 lua_getfield(L, -1, "reps");
219 if (!lua_isnil(L,-1))
220 readLuaValue(L, field.reps);
221 lua_pop(L, 1);
222
223 lua_getfield(L, -1, "start");
224 if (!lua_isnil(L,-1))
225 readLuaValue(L, field.beginning);
226 lua_pop(L, 1);
227
228 goto done;
229 } else {
230 lua_pop(L,1);
231 }
232 // the table is not present; check for nonanimated values
233
234 lua_getfield(L, tbl, name);
235 if(!lua_isnil(L, -1)) {
236 readLuaValue(L, field.start);
237 lua_settop(L, tbl);
238 goto set_uniform;
239 } else {
240 lua_pop(L,1);
241 }
242
243 // the goto did not trigger, so this table is not present either
244 // check for pre-5.6.0 legacy values
245 readLegacyValue(L, name, field.start);
246
247 set_uniform:
248 field.end = field.start;
249 done:
250 lua_settop(L, tbl); // clean up after ourselves
251 }
252
253 inline u16 readAttachmentID(lua_State* L, const char* name)
254 {
255 u16 id = 0;
256 lua_getfield(L, -1, name);
257 if (!lua_isnil(L, -1)) {
259 if (auto obj = ObjectRef::getobject(ref))
260 id = obj->getId();
261 }
262 lua_pop(L, 1);
263 return id;
264 }
265
266 void readTexValue(lua_State* L, ServerParticleTexture& tex);
267}
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:253
void readTweenTable(lua_State *L, const char *name, TweenedParameter< T > &field)
Definition l_particleparams.h:191
void readTexValue(lua_State *L, ServerParticleTexture &tex)
Definition l_particles.cpp:14
void readLegacyValue(lua_State *L, const char *name, T &field)
Definition l_particleparams.h:165
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:236
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:262