summaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_particleparams.h
blob: 4fefc5e3a1af8107eeafb59c0f62fe15f324c6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
Minetest
Copyright (C) 2021 velartrill, Lexi Hale <lexi@hale.su>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once
#include "lua_api/l_particles.h"
#include "lua_api/l_object.h"
#include "lua_api/l_internal.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "server.h"
#include "particles.h"

namespace LuaParticleParams
{
	using namespace ParticleParamTypes;

	template<typename T>
	inline void readNumericLuaValue(lua_State* L, T& ret)
	{
		if (lua_isnil(L,-1))
			return;

		if (std::is_integral<T>())
			ret = lua_tointeger(L, -1);
		else
			ret = lua_tonumber(L, -1);
	}

	template <typename T, size_t N>
	inline void readNumericLuaValue(lua_State* L, Parameter<T,N>& ret)
	{
		readNumericLuaValue<T>(L, ret.val);
	}

	// these are unfortunately necessary as C++ intentionally disallows function template
	// specialization and there's no way to make template overloads reliably resolve correctly
	inline void readLuaValue(lua_State* L, f32Parameter& ret) { readNumericLuaValue(L, ret); }
	inline void readLuaValue(lua_State* L, f32& ret)          { readNumericLuaValue(L, ret); }
	inline void readLuaValue(lua_State* L, u16& ret)          { readNumericLuaValue(L, ret); }
	inline void readLuaValue(lua_State* L, u8& ret)           { readNumericLuaValue(L, ret); }

	inline void readLuaValue(lua_State* L, v3fParameter& ret)
	{
		if (lua_isnil(L, -1))
			return;

		if (lua_isnumber(L, -1)) { // shortcut for uniform vectors
			auto n = lua_tonumber(L, -1);
			ret = v3fParameter(n,n,n);
		} else {
			ret = (v3fParameter)check_v3f(L, -1);
		}
	}

	inline void readLuaValue(lua_State* L, v2fParameter& ret)
	{
		if (lua_isnil(L, -1))
			return;

		if (lua_isnumber(L, -1)) { // shortcut for uniform vectors
			auto n = lua_tonumber(L, -1);
			ret = v2fParameter(n,n);
		} else {
			ret = (v2fParameter)check_v2f(L, -1);
		}
	}

	inline void readLuaValue(lua_State* L, TweenStyle& ret)
	{
		if (lua_isnil(L, -1))
			return;

		static const EnumString opts[] = {
			{(int)TweenStyle::fwd,     "fwd"},
			{(int)TweenStyle::rev,     "rev"},
			{(int)TweenStyle::pulse,   "pulse"},
			{(int)TweenStyle::flicker, "flicker"},
			{0, nullptr},
		};

		luaL_checktype(L, -1, LUA_TSTRING);
		int v = (int)TweenStyle::fwd;
		if (!string_to_enum(opts, v, lua_tostring(L, -1))) {
			throw LuaError("tween style must be one of ('fwd', 'rev', 'pulse', 'flicker')");
		}
		ret = (TweenStyle)v;
	}

	inline void readLuaValue(lua_State* L, AttractorKind& ret)
	{
		if (lua_isnil(L, -1))
			return;

		static const EnumString opts[] = {
			{(int)AttractorKind::none,  "none"},
			{(int)AttractorKind::point, "point"},
			{(int)AttractorKind::line,  "line"},
			{(int)AttractorKind::plane, "plane"},
			{0, nullptr},
		};

		luaL_checktype(L, -1, LUA_TSTRING);
		int v = (int)AttractorKind::none;
		if (!string_to_enum(opts, v, lua_tostring(L, -1))) {
			throw LuaError("attractor kind must be one of ('none', 'point', 'line', 'plane')");
		}
		ret = (AttractorKind)v;
	}

	inline void readLuaValue(lua_State* L, BlendMode& ret)
	{
		if (lua_isnil(L, -1))
			return;

		static const EnumString opts[] = {
			{(int)BlendMode::alpha,  "alpha"},
			{(int)BlendMode::add,    "add"},
			{(int)BlendMode::sub,    "sub"},
			{(int)BlendMode::screen, "screen"},
			{0, nullptr},
		};

		luaL_checktype(L, -1, LUA_TSTRING);
		int v = (int)BlendMode::alpha;
		if (!string_to_enum(opts, v, lua_tostring(L, -1))) {
			throw LuaError("blend mode must be one of ('alpha', 'add', 'sub', 'screen')");
		}
		ret = (BlendMode)v;
	}

	template <typename T> void
	readLuaValue(lua_State* L, RangedParameter<T>& field)
	{
		if (lua_isnil(L,-1))
			return;
		if (!lua_istable(L,-1)) // is this is just a literal value?
			goto set_uniform;

		lua_getfield(L, -1, "min");
		// handle convenience syntax for non-range values
		if (lua_isnil(L,-1)) {
			lua_pop(L, 1);
			goto set_uniform;
		}
		readLuaValue(L,field.min);
		lua_pop(L, 1);

		lua_getfield(L, -1, "max");
		readLuaValue(L,field.max);
		lua_pop(L, 1);

		lua_getfield(L, -1, "bias");
		if (!lua_isnil(L,-1))
			readLuaValue(L,field.bias);
		lua_pop(L, 1);
		return;

		set_uniform:
		readLuaValue(L, field.min);
		readLuaValue(L, field.max);
	}

	template <typename T> void
	readLegacyValue(lua_State* L, const char* name, T& field) {}

	template <typename T> void
	readLegacyValue(lua_State* L, const char* name, RangedParameter<T>& field)
	{
		int tbl = lua_gettop(L);
		lua_pushliteral(L, "min");
		lua_pushstring(L, name);
		lua_concat(L, 2);
		lua_gettable(L, tbl);
		if (!lua_isnil(L, -1)) {
			readLuaValue(L, field.min);
		}
		lua_settop(L, tbl);

		lua_pushliteral(L, "max");
		lua_pushstring(L, name);
		lua_concat(L, 2);
		lua_gettable(L, tbl);
		if (!lua_isnil(L, -1)) {
			readLuaValue(L, field.max);
		}
		lua_settop(L, tbl);
	}

	template <typename T> void
	readTweenTable(lua_State* L, const char* name, TweenedParameter<T>& field)
	{
		int tbl = lua_gettop(L);

		lua_pushstring(L, name);
		lua_pushliteral(L, "_tween");
		lua_concat(L, 2);
		lua_gettable(L, tbl);
		if(lua_istable(L, -1)) {
			int tween = lua_gettop(L);
			// get the starting value
			lua_pushinteger(L, 1), lua_gettable(L, tween);
			readLuaValue(L, field.start);
			lua_pop(L, 1);

			// get the final value -- use len instead of 2 so that this
			// gracefully degrades if keyframe support is later added
			lua_pushinteger(L, (lua_Integer)lua_objlen(L, -1)), lua_gettable(L, tween);
			readLuaValue(L, field.end);
			lua_pop(L, 1);

			// get the effect settings
			lua_getfield(L, -1, "style");
			lua_isnil(L,-1) || (readLuaValue(L, field.style), true);
			lua_pop(L, 1);

			lua_getfield(L, -1, "reps");
			lua_isnil(L,-1) || (readLuaValue(L, field.reps), true);
			lua_pop(L, 1);

			lua_getfield(L, -1, "start");
			lua_isnil(L,-1) || (readLuaValue(L, field.beginning), true);
			lua_pop(L, 1);

			goto done;
		} else {
			lua_pop(L,1);
		}
		// the table is not present; check for nonanimated values

		lua_getfield(L, tbl, name);
		if(!lua_isnil(L, -1)) {
			readLuaValue(L, field.start);
			lua_settop(L, tbl);
			goto set_uniform;
		} else {
			lua_pop(L,1);
		}

		// the goto did not trigger, so this table is not present either
		// check for pre-5.6.0 legacy values
		readLegacyValue(L, name, field.start);

		set_uniform:
		field.end = field.start;
		done:
		lua_settop(L, tbl); // clean up after ourselves
	}

	inline u16 readAttachmentID(lua_State* L, const char* name)
	{
		u16 id = 0;
		lua_getfield(L, -1, name);
		if (!lua_isnil(L, -1)) {
			ObjectRef *ref = ObjectRef::checkobject(L, -1);
			if (auto obj = ObjectRef::getobject(ref))
				id = obj->getId();
		}
		lua_pop(L, 1);
		return id;
	}

	void readTexValue(lua_State* L, ServerParticleTexture& tex);
}