]> git.sesse.net Git - nageru/blob - nageru/scene.cpp
Rework the chain concept.
[nageru] / nageru / scene.cpp
1 #include <assert.h>
2 extern "C" {
3 #include <lauxlib.h>
4 #include <lua.hpp>
5 }
6
7 #ifdef HAVE_CEF
8 #include "cef_capture.h"
9 #endif
10 #include "ffmpeg_capture.h"
11 #include "flags.h"
12 #include "image_input.h"
13 #include "input_state.h"
14 #include "lua_utils.h"
15 #include "scene.h"
16 #include "theme.h"
17
18 using namespace movit;
19 using namespace std;
20
21 bool display(Block *block, lua_State *L, int idx);
22
23 EffectType current_type(const Block *block)
24 {
25         return block->alternatives[block->currently_chosen_alternative]->effect_type;
26 }
27
28 int find_index_of(const Block *block, EffectType val)
29 {
30         for (size_t idx = 0; idx < block->alternatives.size(); ++idx) {
31                 if (block->alternatives[idx]->effect_type == val) {
32                         return idx;
33                 }
34         }
35         return -1;
36 }
37
38 Scene::Scene(Theme *theme, float aspect_nom, float aspect_denom)
39         : theme(theme), aspect_nom(aspect_nom), aspect_denom(aspect_denom), resource_pool(theme->get_resource_pool()) {}
40
41 size_t Scene::compute_chain_number(bool is_main_chain) const
42 {
43         assert(chains.size() > 0);
44         assert(chains.size() % 2 == 0);
45         size_t chain_number = compute_chain_number_for_block(blocks.size() - 1);
46         assert(chain_number < chains.size() / 2);
47         if (is_main_chain) {
48                 chain_number += chains.size() / 2;
49         }
50         return chain_number;
51 }
52
53 size_t Scene::compute_chain_number_for_block(size_t block_idx) const
54 {
55         Block *block = blocks[block_idx];
56         size_t chain_number;
57         if (block_idx == 0) {
58                 assert(block->cardinality_base == 1);
59                 chain_number = block->currently_chosen_alternative;
60         } else {
61                 chain_number = compute_chain_number_for_block(block_idx - 1) + block->cardinality_base * block->currently_chosen_alternative;
62         }
63         assert(block->currently_chosen_alternative < int(block->alternatives.size()));
64         return chain_number;
65 }
66
67 int Scene::add_input(lua_State* L)
68 {
69         assert(lua_gettop(L) == 1 || lua_gettop(L) == 2);
70         Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
71
72         Block *block = new Block;
73         block->idx = chain->blocks.size();
74         if (lua_gettop(L) == 1) {
75                 // No parameter given, so a flexible input.
76                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
77                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
78                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
79                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
80                 block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
81         } else {
82                 // Input of a given type. We'll specialize it here, plus connect the input as given.
83                 if (lua_isnumber(L, 2)) {
84                         block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
85                         block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
86 #ifdef HAVE_CEF
87                 } else if (luaL_testudata(L, 2, "HTMLInput")) {
88                         block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
89 #endif
90                 } else if (luaL_testudata(L, 2, "VideoInput")) {
91                         FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, 2, "VideoInput");
92                         if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
93                                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
94                         } else {
95                                 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
96                                 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
97                         }
98                 } else if (luaL_testudata(L, 2, "ImageInput")) {
99                         block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
100                 } else {
101                         luaL_error(L, "add_input() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
102                 }
103                 bool ok = display(block, L, 2);
104                 assert(ok);
105         }
106         block->is_input = true;
107         chain->blocks.push_back(block);
108
109         return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
110 }
111
112 int Scene::add_effect(lua_State* L)
113 {
114         assert(lua_gettop(L) >= 2);
115         Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
116
117         Block *block = new Block;
118         block->idx = chain->blocks.size();
119
120         if (lua_istable(L, 2)) {
121                 size_t len = lua_objlen(L, 2);
122                 for (size_t i = 0; i < len; ++i) {
123                         lua_rawgeti(L, 2, i + 1);
124                         EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, -1, "EffectBlueprint");
125                         block->alternatives.push_back(blueprint);
126                         lua_settop(L, -2);
127                 }
128         } else {
129                 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
130                 block->alternatives.push_back(blueprint);
131         }
132
133         // Find the inputs.
134         if (lua_gettop(L) == 2) {
135                 assert(!chain->blocks.empty());
136                 block->inputs.push_back(chain->blocks.size() - 1);
137         } else {
138                 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
139                         Block *input_block = nullptr;
140                         if (luaL_testudata(L, idx, "Block")) {
141                                 input_block = *(Block **)luaL_checkudata(L, idx, "Block");
142                         } else {
143                                 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint");
144
145                                 // Search through all the blocks to figure out which one contains this effect.
146                                 for (Block *block : chain->blocks) {
147                                         if (find(block->alternatives.begin(), block->alternatives.end(), blueprint) != block->alternatives.end()) {
148                                                 input_block = block;
149                                                 break;
150                                         }
151                                 }
152                                 if (input_block == nullptr) {
153                                         luaL_error(L, "Input effect in parameter #%d has not been added to this chain", idx - 1);
154                                 }
155                         }
156                         block->inputs.push_back(input_block->idx);
157                 }
158         }
159
160         chain->blocks.push_back(block);
161
162         return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
163 }
164
165 int Scene::add_optional_effect(lua_State* L)
166 {
167         assert(lua_gettop(L) >= 2);
168         Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
169
170         // NOTE: We only support effects with a single parent, since that's what IdentityEffect does.
171         Block *block = new Block;
172         block->idx = chain->blocks.size();
173
174         EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
175         block->alternatives.push_back(blueprint);
176
177         // An IdentityEffect will be the alternative for when the effect is disabled.
178         block->alternatives.push_back(new EffectBlueprint(IDENTITY_EFFECT));
179
180         block->inputs.push_back(chain->blocks.size() - 1);
181         chain->blocks.push_back(block);
182
183         return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
184 }
185
186 Effect *Scene::instantiate_effects(const Block *block, size_t chain_idx, Scene::Instantiation *instantiation)
187 {
188         vector<Effect *> inputs;
189         for (size_t input_idx : block->inputs) {
190                 inputs.push_back(instantiate_effects(blocks[input_idx], chain_idx, instantiation));
191         }
192
193         // Find the chosen alternative for this block in this instance.
194         size_t chosen_alternative = (chain_idx / block->cardinality_base) % block->alternatives.size();
195         EffectType chosen_type = block->alternatives[chosen_alternative]->effect_type;
196
197         Effect *effect;
198         switch (chosen_type) {
199         case LIVE_INPUT_YCBCR:
200         case LIVE_INPUT_YCBCR_WITH_DEINTERLACE:
201         case LIVE_INPUT_YCBCR_PLANAR:
202         case LIVE_INPUT_BGRA: {
203                 bool deinterlace = (chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
204                 bool override_bounce = !deinterlace;  // For most chains, this will be fine. Reconsider if we see real problems somewhere; it's better than having the user try to understand it.
205                 bmusb::PixelFormat pixel_format;
206                 if (chosen_type == LIVE_INPUT_BGRA) {
207                         pixel_format = bmusb::PixelFormat_8BitBGRA;
208                 } else if (chosen_type == LIVE_INPUT_YCBCR_PLANAR) {
209                         pixel_format = bmusb::PixelFormat_8BitYCbCrPlanar;
210                 } else if (global_flags.ten_bit_input) {
211                         pixel_format = bmusb::PixelFormat_10BitYCbCr;
212                 } else {
213                         pixel_format = bmusb::PixelFormat_8BitYCbCr;
214                 }
215                 LiveInputWrapper *input = new LiveInputWrapper(theme, instantiation->chain.get(), pixel_format, override_bounce, deinterlace, /*user_connectable=*/true);
216                 effect = input->get_effect();  // Adds itself to the chain, so no need to call add_effect().
217                 instantiation->inputs.emplace(block->idx, input);
218                 break;
219         }
220         case IMAGE_INPUT: {
221                 ImageInput *input = new ImageInput;
222                 instantiation->chain->add_input(input);
223                 instantiation->image_inputs.emplace(block->idx, input);
224                 effect = input;
225                 break;
226         }
227         default:
228                 effect = instantiate_effect(instantiation->chain.get(), block->alternatives[chosen_alternative]->effect_type);
229                 instantiation->chain->add_effect(effect, inputs);
230                 break;
231         }
232         instantiation->effects.emplace(block->idx, effect);
233         return effect;
234 }
235
236 int Scene::finalize(lua_State* L)
237 {
238         bool only_one_mode = false;
239         bool chosen_mode = false;
240         if (lua_gettop(L) == 2) {
241                 only_one_mode = true;
242                 chosen_mode = checkbool(L, 2);
243         } else {
244                 assert(lua_gettop(L) == 1);
245         }
246         Scene *chain = (Scene *)luaL_checkudata(L, 1, "Scene");
247         Theme *theme = get_theme_updata(L);
248
249         size_t base = 1;
250         for (Block *block : chain->blocks) {
251                 block->cardinality_base = base;
252                 base *= block->alternatives.size();
253         }
254
255         const size_t cardinality = base;
256         const size_t total_cardinality = cardinality * (only_one_mode ? 1 : 2);
257         if (total_cardinality > 200) {
258                 print_warning(L, "The given Scene will instantiate %zu different versions. This will take a lot of time and RAM to compile; see if you could limit some options by e.g. locking the input type in some cases (by giving a fixed input to add_input()).\n",
259                         total_cardinality);
260         }
261
262         Block *output_block = chain->blocks.back();
263         for (bool is_main_chain : { false, true }) {
264                 for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
265                         if (only_one_mode && is_main_chain != chosen_mode) {
266                                 chain->chains.emplace_back();
267                                 continue;
268                         }
269
270                         Scene::Instantiation instantiation;
271                         instantiation.chain.reset(new EffectChain(chain->aspect_nom, chain->aspect_denom, theme->get_resource_pool()));
272                         chain->instantiate_effects(output_block, chain_idx, &instantiation);
273
274                         add_outputs_and_finalize(instantiation.chain.get(), is_main_chain);
275                         chain->chains.emplace_back(move(instantiation));
276                 }
277         }
278         return 0;
279 }
280
281 std::pair<movit::EffectChain *, std::function<void()>>
282 Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &input_state)
283 {
284         // For video inputs, pick the right interlaced/progressive version
285         // based on the current state of the signals.
286         InputStateInfo info(input_state);
287         for (Block *block : blocks) {
288                 if (block->is_input && block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
289                         EffectType chosen_type = current_type(block);
290                         assert(chosen_type == LIVE_INPUT_YCBCR || chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
291                         if (info.last_interlaced[block->signal_to_connect]) {
292                                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
293                         } else {
294                                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);
295                         }
296                 }
297         }
298
299         // Pick out the right chain based on the current selections,
300         // and snapshot all the set variables so that we can set them
301         // in the prepare function even if they're being changed by
302         // the Lua code later.
303         bool is_main_chain = (num == 0);
304         size_t chain_idx = compute_chain_number(is_main_chain);
305         const Scene::Instantiation &instantiation = chains[chain_idx];
306         EffectChain *effect_chain = instantiation.chain.get();
307
308         map<LiveInputWrapper *, int> signals_to_connect;
309         map<ImageInput *, string> images_to_select;
310         map<pair<Effect *, string>, int> int_to_set;
311         map<pair<Effect *, string>, float> float_to_set;
312         map<pair<Effect *, string>, array<float, 3>> vec3_to_set;
313         map<pair<Effect *, string>, array<float, 4>> vec4_to_set;
314         for (const auto &index_and_input : instantiation.inputs) {
315                 Block *block = blocks[index_and_input.first];
316                 EffectType chosen_type = current_type(block);
317                 LiveInputWrapper *input = index_and_input.second;
318                 if (chosen_type == LIVE_INPUT_YCBCR ||
319                     chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE ||
320                     chosen_type == LIVE_INPUT_YCBCR_PLANAR ||
321                     chosen_type == LIVE_INPUT_BGRA) {
322                         if (block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
323                                 signals_to_connect.emplace(input, block->signal_to_connect);
324 #ifdef HAVE_CEF
325                         } else if (block->signal_type_to_connect == Block::CONNECT_CEF) {
326                                 signals_to_connect.emplace(input, block->cef_to_connect->get_card_index());
327 #endif
328                         } else if (block->signal_type_to_connect == Block::CONNECT_VIDEO) {
329                                 signals_to_connect.emplace(input, block->video_to_connect->get_card_index());
330                         } else {
331                                 assert(false);
332                         }
333                 }
334         }
335         for (const auto &index_and_input : instantiation.image_inputs) {
336                 Block *block = blocks[index_and_input.first];
337                 ImageInput *input = index_and_input.second;
338                 if (current_type(block) == IMAGE_INPUT) {
339                         images_to_select.emplace(input, block->pathname);
340                 }
341         }
342         for (const auto &index_and_effect : instantiation.effects) {
343                 Block *block = blocks[index_and_effect.first];
344                 Effect *effect = index_and_effect.second;
345
346                 // Get the effects currently set on the block.
347                 if (current_type(block) != IDENTITY_EFFECT) {  // Ignore settings on optional effects.
348                         for (const auto &key_and_tuple : block->int_parameters) {
349                                 int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
350                         }
351                         for (const auto &key_and_tuple : block->float_parameters) {
352                                 float_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
353                         }
354                         for (const auto &key_and_tuple : block->vec3_parameters) {
355                                 vec3_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
356                         }
357                         for (const auto &key_and_tuple : block->vec4_parameters) {
358                                 vec4_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
359                         }
360                 }
361
362                 // Parameters set on the blueprint itself override those that are set for the block,
363                 // so they are set afterwards.
364                 if (!block->alternatives.empty()) {
365                         EffectBlueprint *blueprint = block->alternatives[block->currently_chosen_alternative];
366                         for (const auto &key_and_tuple : blueprint->int_parameters) {
367                                 int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
368                         }
369                         for (const auto &key_and_tuple : blueprint->float_parameters) {
370                                 float_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
371                         }
372                         for (const auto &key_and_tuple : blueprint->vec3_parameters) {
373                                 vec3_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
374                         }
375                         for (const auto &key_and_tuple : blueprint->vec4_parameters) {
376                                 vec4_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
377                         }
378                 }
379         }
380
381         lua_pop(L, 1);
382
383         auto setup_chain = [L, theme, signals_to_connect, images_to_select, int_to_set, float_to_set, vec3_to_set, vec4_to_set, input_state]{
384                 lock_guard<mutex> lock(theme->m);
385
386                 assert(theme->input_state == nullptr);
387                 theme->input_state = &input_state;
388
389                 // Set up state, including connecting signals.
390                 for (const auto &input_and_signal : signals_to_connect) {
391                         LiveInputWrapper *input = input_and_signal.first;
392                         input->connect_signal_raw(input_and_signal.second, input_state);
393                 }
394                 for (const auto &input_and_filename : images_to_select) {
395                         input_and_filename.first->switch_image(input_and_filename.second);
396                 }
397                 for (const auto &effect_and_key_and_value : int_to_set) {
398                         Effect *effect = effect_and_key_and_value.first.first;
399                         const string &key = effect_and_key_and_value.first.second;
400                         const int value = effect_and_key_and_value.second;
401                         if (!effect->set_int(key, value)) {
402                                 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), value);
403                         }
404                 }
405                 for (const auto &effect_and_key_and_value : float_to_set) {
406                         Effect *effect = effect_and_key_and_value.first.first;
407                         const string &key = effect_and_key_and_value.first.second;
408                         const float value = effect_and_key_and_value.second;
409                         if (!effect->set_float(key, value)) {
410                                 luaL_error(L, "Effect refused set_float(\"%s\", %f) (invalid key?)", key.c_str(), value);
411                         }
412                 }
413                 for (const auto &effect_and_key_and_value : vec3_to_set) {
414                         Effect *effect = effect_and_key_and_value.first.first;
415                         const string &key = effect_and_key_and_value.first.second;
416                         const float *value = effect_and_key_and_value.second.data();
417                         if (!effect->set_vec3(key, value)) {
418                                 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
419                                                 value[0], value[1], value[2]);
420                         }
421                 }
422                 for (const auto &effect_and_key_and_value : vec4_to_set) {
423                         Effect *effect = effect_and_key_and_value.first.first;
424                         const string &key = effect_and_key_and_value.first.second;
425                         const float *value = effect_and_key_and_value.second.data();
426                         if (!effect->set_vec4(key, value)) {
427                                 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
428                                                 value[0], value[1], value[2], value[3]);
429                         }
430                 }
431
432                 theme->input_state = nullptr;
433         };
434         return make_pair(effect_chain, move(setup_chain));
435 }
436
437 bool display(Block *block, lua_State *L, int idx)
438 {
439         if (lua_isnumber(L, idx)) {
440                 Theme *theme = get_theme_updata(L);
441                 int signal_idx = luaL_checknumber(L, idx);
442                 block->signal_type_to_connect = Block::CONNECT_SIGNAL;
443                 block->signal_to_connect = theme->map_signal(signal_idx);
444                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);  // Will be changed to deinterlaced at get_chain() time if needed.
445                 return true;
446 #ifdef HAVE_CEF
447         } else if (luaL_testudata(L, idx, "HTMLInput")) {
448                 CEFCapture *capture = *(CEFCapture **)luaL_checkudata(L, idx, "HTMLInput");
449                 block->signal_type_to_connect = Block::CONNECT_CEF;
450                 block->cef_to_connect = capture;
451                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
452                 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
453                 return true;
454 #endif
455         } else if (luaL_testudata(L, idx, "VideoInput")) {
456                 FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, idx, "VideoInput");
457                 block->signal_type_to_connect = Block::CONNECT_VIDEO;
458                 block->video_to_connect = capture;
459                 if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
460                         block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_PLANAR);
461                 } else {
462                         assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
463                         block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
464                 }
465                 return true;
466         } else if (luaL_testudata(L, idx, "ImageInput")) {
467                 ImageInput *image = *(ImageInput **)luaL_checkudata(L, idx, "ImageInput");
468                 block->signal_type_to_connect = Block::CONNECT_NONE;
469                 block->currently_chosen_alternative = find_index_of(block, IMAGE_INPUT);
470                 block->pathname = image->get_pathname();
471                 return true;
472         } else {
473                 return false;
474         }
475 }
476
477 int Block_display(lua_State* L)
478 {
479         assert(lua_gettop(L) == 2);
480         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
481         if (!block->is_input) {
482                 luaL_error(L, "display() called on something that isn't an input");
483         }
484
485         bool ok = display(block, L, 2);
486         if (!ok) {
487                 luaL_error(L, "display() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
488         }
489
490         if (block->currently_chosen_alternative == -1) {
491                 luaL_error(L, "display() called on an input whose type was fixed at construction time, with a signal of different type");
492         }
493
494         return 0;
495 }
496
497 int Block_choose_alternative(lua_State* L)
498 {
499         assert(lua_gettop(L) == 2);
500         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
501         int alternative_idx = luaL_checknumber(L, 2);
502
503         assert(alternative_idx >= 0);
504         assert(size_t(alternative_idx) < block->alternatives.size());
505         block->currently_chosen_alternative = alternative_idx;
506
507         return 0;
508 }
509
510 int Block_enable(lua_State *L)
511 {
512         assert(lua_gettop(L) == 1);
513         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
514
515         if (block->alternatives.size() != 2 ||
516             block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
517                 luaL_error(L, "enable() called on something that wasn't added with add_optional_effect()");
518         }
519         block->currently_chosen_alternative = 0;  // The actual effect.
520         return 0;
521 }
522
523 int Block_disable(lua_State *L)
524 {
525         assert(lua_gettop(L) == 1);
526         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
527
528         if (block->alternatives.size() != 2 ||
529             block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
530                 luaL_error(L, "disable() called on something that wasn't added with add_optional_effect()");
531         }
532         block->currently_chosen_alternative = find_index_of(block, IDENTITY_EFFECT);
533         assert(block->currently_chosen_alternative != -1);
534         return 0;
535 }
536
537 int Block_set_int(lua_State *L)
538 {
539         assert(lua_gettop(L) == 3);
540         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
541         string key = checkstdstring(L, 2);
542         float value = luaL_checknumber(L, 3);
543
544         // TODO: check validity already here, if possible?
545         block->int_parameters[key] = value;
546
547         return 0;
548 }
549
550 int Block_set_float(lua_State *L)
551 {
552         assert(lua_gettop(L) == 3);
553         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
554         string key = checkstdstring(L, 2);
555         float value = luaL_checknumber(L, 3);
556
557         // TODO: check validity already here, if possible?
558         block->float_parameters[key] = value;
559
560         return 0;
561 }
562
563 int Block_set_vec3(lua_State *L)
564 {
565         assert(lua_gettop(L) == 5);
566         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
567         string key = checkstdstring(L, 2);
568         array<float, 3> v;
569         v[0] = luaL_checknumber(L, 3);
570         v[1] = luaL_checknumber(L, 4);
571         v[2] = luaL_checknumber(L, 5);
572
573         // TODO: check validity already here, if possible?
574         block->vec3_parameters[key] = v;
575
576         return 0;
577 }
578
579 int Block_set_vec4(lua_State *L)
580 {
581         assert(lua_gettop(L) == 6);
582         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
583         string key = checkstdstring(L, 2);
584         array<float, 4> v;
585         v[0] = luaL_checknumber(L, 3);
586         v[1] = luaL_checknumber(L, 4);
587         v[2] = luaL_checknumber(L, 5);
588         v[3] = luaL_checknumber(L, 6);
589
590         // TODO: check validity already here, if possible?
591         block->vec4_parameters[key] = v;
592
593         return 0;
594 }
595