Luanti 5.15.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"
9#include "server.h"
11#include "particles.h"
12
14{
15 using namespace ParticleParamTypes;
16
17 template<typename T>
18 inline void readNumericLuaValue(lua_State* L, T& ret)
19 {
20 if (lua_isnil(L,-1))
21 return;
22
23 if (std::is_integral<T>())
24 ret = lua_tointeger(L, -1);
25 else
26 ret = lua_tonumber(L, -1);
27 }
28
29 template <typename T, size_t N>
30 inline void readNumericLuaValue(lua_State* L, Parameter<T,N>& ret)
31 {
33 }
34
35 // these are unfortunately necessary as C++ intentionally disallows function template
36 // specialization and there's no way to make template overloads reliably resolve correctly
37 inline void readLuaValue(lua_State* L, f32Parameter& ret) { readNumericLuaValue(L, ret); }
38 inline void readLuaValue(lua_State* L, f32& ret) { readNumericLuaValue(L, ret); }
39 inline void readLuaValue(lua_State* L, u16& ret) { readNumericLuaValue(L, ret); }
40 inline void readLuaValue(lua_State* L, u8& ret) { readNumericLuaValue(L, ret); }
41
42 inline void readLuaValue(lua_State* L, v3fParameter& ret)
43 {
44 if (lua_isnil(L, -1))
45 return;
46
47 if (lua_isnumber(L, -1)) { // shortcut for uniform vectors
48 auto n = lua_tonumber(L, -1);
49 ret = v3fParameter(n,n,n);
50 } else {
51 ret = (v3fParameter)check_v3f(L, -1);
52 }
53 }
54
55 inline void readLuaValue(lua_State* L, v2fParameter& ret)
56 {
57 if (lua_isnil(L, -1))
58 return;
59
60 if (lua_isnumber(L, -1)) { // shortcut for uniform vectors
61 auto n = lua_tonumber(L, -1);
62 ret = v2fParameter(n,n);
63 } else {
64 ret = (v2fParameter)check_v2f(L, -1);
65 }
66 }
67
68 inline void readLuaValue(lua_State* L, TweenStyle& ret)
69 {
70 if (lua_isnil(L, -1))
71 return;
72
73 static const EnumString opts[] = {
74 {(int)TweenStyle::fwd, "fwd"},
75 {(int)TweenStyle::rev, "rev"},
76 {(int)TweenStyle::pulse, "pulse"},
77 {(int)TweenStyle::flicker, "flicker"},
78 {0, nullptr},
79 };
80
81 luaL_checktype(L, -1, LUA_TSTRING);
82 int v = (int)TweenStyle::fwd;
83 if (!string_to_enum(opts, v, lua_tostring(L, -1))) {
84 throw LuaError("tween style must be one of ('fwd', 'rev', 'pulse', 'flicker')");
85 }
86 ret = (TweenStyle)v;
87 }
88
89 inline void readLuaValue(lua_State* L, AttractorKind& ret)
90 {
91 if (lua_isnil(L, -1))
92 return;
93
94 static const EnumString opts[] = {
95 {(int)AttractorKind::none, "none"},
96 {(int)AttractorKind::point, "point"},
97 {(int)AttractorKind::line, "line"},
98 {(int)AttractorKind::plane, "plane"},
99 {0, nullptr},
100 };
101
102 luaL_checktype(L, -1, LUA_TSTRING);
103 int v = (int)AttractorKind::none;
104 if (!string_to_enum(opts, v, lua_tostring(L, -1))) {
105 throw LuaError("attractor kind must be one of ('none', 'point', 'line', 'plane')");
106 }
107 ret = (AttractorKind)v;
108 }
109
110 inline void readLuaValue(lua_State* L, BlendMode& ret)
111 {
112 if (lua_isnil(L, -1))
113 return;
114
115 static const EnumString opts[] = {
116 {(int)BlendMode::alpha, "alpha"},
117 {(int)BlendMode::add, "add"},
118 {(int)BlendMode::sub, "sub"},
119 {(int)BlendMode::screen, "screen"},
120 {(int)BlendMode::clip, "clip"},
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', 'clip', '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}
v2f check_v2f(lua_State *L, int index)
Definition c_converter.cpp:154
v3f check_v3f(lua_State *L, int index)
Definition c_converter.cpp:176
Definition c_types.h:32
static T * checkObject(lua_State *L, int narg)
Definition l_base.h:95
Definition l_object.h:20
static ServerActiveObject * getobject(ObjectRef *ref)
Definition l_object.cpp:34
bool string_to_enum(const EnumString *spec, int &result, std::string_view str)
Definition enum_string.cpp:8
Definition l_particleparams.h:14
void readNumericLuaValue(lua_State *L, T &ret)
Definition l_particleparams.h:18
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:15
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:37
Definition particles.cpp:110
VectorParameter< v3f, 3 > v3fParameter
Definition particles.h:123
TweenStyle
Definition particles.h:181
VectorParameter< v2f, 2 > v2fParameter
Definition particles.h:122
BlendMode
Definition particles.h:228
AttractorKind
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:101
Definition particles.h:254