]> git.sesse.net Git - nageru/blob - nageru/scene.cpp
Small refactoring in Block.
[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 *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
71
72         Block *block = new Block;
73         block->idx = scene->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         scene->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 *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
116
117         Block *block = new Block;
118         block->idx = scene->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(!scene->blocks.empty());
136                 block->inputs.push_back(scene->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 : scene->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 scene", idx - 1);
154                                 }
155                         }
156                         block->inputs.push_back(input_block->idx);
157                 }
158         }
159
160         scene->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 *scene = (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 = scene->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(scene->blocks.size() - 1);
181         scene->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         EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
195
196         Effect *effect;
197         switch (chosen_type) {
198         case LIVE_INPUT_YCBCR:
199         case LIVE_INPUT_YCBCR_WITH_DEINTERLACE:
200         case LIVE_INPUT_YCBCR_PLANAR:
201         case LIVE_INPUT_BGRA: {
202                 bool deinterlace = (chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
203                 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.
204                 bmusb::PixelFormat pixel_format;
205                 if (chosen_type == LIVE_INPUT_BGRA) {
206                         pixel_format = bmusb::PixelFormat_8BitBGRA;
207                 } else if (chosen_type == LIVE_INPUT_YCBCR_PLANAR) {
208                         pixel_format = bmusb::PixelFormat_8BitYCbCrPlanar;
209                 } else if (global_flags.ten_bit_input) {
210                         pixel_format = bmusb::PixelFormat_10BitYCbCr;
211                 } else {
212                         pixel_format = bmusb::PixelFormat_8BitYCbCr;
213                 }
214                 LiveInputWrapper *input = new LiveInputWrapper(theme, instantiation->chain.get(), pixel_format, override_bounce, deinterlace, /*user_connectable=*/true);
215                 effect = input->get_effect();  // Adds itself to the chain, so no need to call add_effect().
216                 instantiation->inputs.emplace(block->idx, input);
217                 break;
218         }
219         case IMAGE_INPUT: {
220                 ImageInput *input = new ImageInput;
221                 instantiation->chain->add_input(input);
222                 instantiation->image_inputs.emplace(block->idx, input);
223                 effect = input;
224                 break;
225         }
226         default:
227                 effect = instantiate_effect(instantiation->chain.get(), chosen_type);
228                 instantiation->chain->add_effect(effect, inputs);
229                 break;
230         }
231         instantiation->effects.emplace(block->idx, effect);
232         return effect;
233 }
234
235 int Scene::finalize(lua_State* L)
236 {
237         bool only_one_mode = false;
238         bool chosen_mode = false;
239         if (lua_gettop(L) == 2) {
240                 only_one_mode = true;
241                 chosen_mode = checkbool(L, 2);
242         } else {
243                 assert(lua_gettop(L) == 1);
244         }
245         Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
246         Theme *theme = get_theme_updata(L);
247
248         size_t base = 1;
249         for (Block *block : scene->blocks) {
250                 block->cardinality_base = base;
251                 base *= block->alternatives.size();
252         }
253
254         const size_t cardinality = base;
255         const size_t total_cardinality = cardinality * (only_one_mode ? 1 : 2);
256         if (total_cardinality > 200) {
257                 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",
258                         total_cardinality);
259         }
260
261         Block *output_block = scene->blocks.back();
262         for (bool is_main_chain : { false, true }) {
263                 for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
264                         if (only_one_mode && is_main_chain != chosen_mode) {
265                                 scene->chains.emplace_back();
266                                 continue;
267                         }
268
269                         Scene::Instantiation instantiation;
270                         instantiation.chain.reset(new EffectChain(scene->aspect_nom, scene->aspect_denom, theme->get_resource_pool()));
271                         scene->instantiate_effects(output_block, chain_idx, &instantiation);
272
273                         add_outputs_and_finalize(instantiation.chain.get(), is_main_chain);
274                         scene->chains.emplace_back(move(instantiation));
275                 }
276         }
277         return 0;
278 }
279
280 std::pair<movit::EffectChain *, std::function<void()>>
281 Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &input_state)
282 {
283         // For video inputs, pick the right interlaced/progressive version
284         // based on the current state of the signals.
285         InputStateInfo info(input_state);
286         for (Block *block : blocks) {
287                 if (block->is_input && block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
288                         EffectType chosen_type = current_type(block);
289                         assert(chosen_type == LIVE_INPUT_YCBCR || chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
290                         if (info.last_interlaced[block->signal_to_connect]) {
291                                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
292                         } else {
293                                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);
294                         }
295                 }
296         }
297
298         // Pick out the right chain based on the current selections,
299         // and snapshot all the set variables so that we can set them
300         // in the prepare function even if they're being changed by
301         // the Lua code later.
302         bool is_main_chain = (num == 0);
303         size_t chain_idx = compute_chain_number(is_main_chain);
304         const Scene::Instantiation &instantiation = chains[chain_idx];
305         EffectChain *effect_chain = instantiation.chain.get();
306
307         map<LiveInputWrapper *, int> signals_to_connect;
308         map<ImageInput *, string> images_to_select;
309         map<pair<Effect *, string>, int> int_to_set;
310         map<pair<Effect *, string>, float> float_to_set;
311         map<pair<Effect *, string>, array<float, 3>> vec3_to_set;
312         map<pair<Effect *, string>, array<float, 4>> vec4_to_set;
313         for (const auto &index_and_input : instantiation.inputs) {
314                 Block *block = blocks[index_and_input.first];
315                 EffectType chosen_type = current_type(block);
316                 LiveInputWrapper *input = index_and_input.second;
317                 if (chosen_type == LIVE_INPUT_YCBCR ||
318                     chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE ||
319                     chosen_type == LIVE_INPUT_YCBCR_PLANAR ||
320                     chosen_type == LIVE_INPUT_BGRA) {
321                         if (block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
322                                 signals_to_connect.emplace(input, block->signal_to_connect);
323 #ifdef HAVE_CEF
324                         } else if (block->signal_type_to_connect == Block::CONNECT_CEF) {
325                                 signals_to_connect.emplace(input, block->cef_to_connect->get_card_index());
326 #endif
327                         } else if (block->signal_type_to_connect == Block::CONNECT_VIDEO) {
328                                 signals_to_connect.emplace(input, block->video_to_connect->get_card_index());
329                         } else if (block->signal_type_to_connect == Block::CONNECT_NONE) {
330                                 luaL_error(L, "An input in a scene was not connected to anything (forgot to call display())");
331                         } else {
332                                 assert(false);
333                         }
334                 }
335         }
336         for (const auto &index_and_input : instantiation.image_inputs) {
337                 Block *block = blocks[index_and_input.first];
338                 ImageInput *input = index_and_input.second;
339                 if (current_type(block) == IMAGE_INPUT) {
340                         images_to_select.emplace(input, block->pathname);
341                 }
342         }
343         for (const auto &index_and_effect : instantiation.effects) {
344                 Block *block = blocks[index_and_effect.first];
345                 Effect *effect = index_and_effect.second;
346
347                 // Get the effects currently set on the block.
348                 if (current_type(block) != IDENTITY_EFFECT) {  // Ignore settings on optional effects.
349                         for (const auto &key_and_tuple : block->int_parameters) {
350                                 int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
351                         }
352                         for (const auto &key_and_tuple : block->float_parameters) {
353                                 float_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
354                         }
355                         for (const auto &key_and_tuple : block->vec3_parameters) {
356                                 vec3_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
357                         }
358                         for (const auto &key_and_tuple : block->vec4_parameters) {
359                                 vec4_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
360                         }
361                 }
362
363                 // Parameters set on the blueprint itself override those that are set for the block,
364                 // so they are set afterwards.
365                 if (!block->alternatives.empty()) {
366                         EffectBlueprint *blueprint = block->alternatives[block->currently_chosen_alternative];
367                         for (const auto &key_and_tuple : blueprint->int_parameters) {
368                                 int_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
369                         }
370                         for (const auto &key_and_tuple : blueprint->float_parameters) {
371                                 float_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
372                         }
373                         for (const auto &key_and_tuple : blueprint->vec3_parameters) {
374                                 vec3_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
375                         }
376                         for (const auto &key_and_tuple : blueprint->vec4_parameters) {
377                                 vec4_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
378                         }
379                 }
380         }
381
382         lua_pop(L, 1);
383
384         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]{
385                 lock_guard<mutex> lock(theme->m);
386
387                 // Set up state, including connecting signals.
388                 for (const auto &input_and_signal : signals_to_connect) {
389                         LiveInputWrapper *input = input_and_signal.first;
390                         input->connect_signal_raw(input_and_signal.second, input_state);
391                 }
392                 for (const auto &input_and_filename : images_to_select) {
393                         input_and_filename.first->switch_image(input_and_filename.second);
394                 }
395                 for (const auto &effect_and_key_and_value : int_to_set) {
396                         Effect *effect = effect_and_key_and_value.first.first;
397                         const string &key = effect_and_key_and_value.first.second;
398                         const int value = effect_and_key_and_value.second;
399                         if (!effect->set_int(key, value)) {
400                                 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), value);
401                         }
402                 }
403                 for (const auto &effect_and_key_and_value : float_to_set) {
404                         Effect *effect = effect_and_key_and_value.first.first;
405                         const string &key = effect_and_key_and_value.first.second;
406                         const float value = effect_and_key_and_value.second;
407                         if (!effect->set_float(key, value)) {
408                                 luaL_error(L, "Effect refused set_float(\"%s\", %f) (invalid key?)", key.c_str(), value);
409                         }
410                 }
411                 for (const auto &effect_and_key_and_value : vec3_to_set) {
412                         Effect *effect = effect_and_key_and_value.first.first;
413                         const string &key = effect_and_key_and_value.first.second;
414                         const float *value = effect_and_key_and_value.second.data();
415                         if (!effect->set_vec3(key, value)) {
416                                 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
417                                                 value[0], value[1], value[2]);
418                         }
419                 }
420                 for (const auto &effect_and_key_and_value : vec4_to_set) {
421                         Effect *effect = effect_and_key_and_value.first.first;
422                         const string &key = effect_and_key_and_value.first.second;
423                         const float *value = effect_and_key_and_value.second.data();
424                         if (!effect->set_vec4(key, value)) {
425                                 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
426                                                 value[0], value[1], value[2], value[3]);
427                         }
428                 }
429         };
430         return make_pair(effect_chain, move(setup_chain));
431 }
432
433 bool display(Block *block, lua_State *L, int idx)
434 {
435         if (lua_isnumber(L, idx)) {
436                 Theme *theme = get_theme_updata(L);
437                 int signal_idx = luaL_checknumber(L, idx);
438                 block->signal_type_to_connect = Block::CONNECT_SIGNAL;
439                 block->signal_to_connect = theme->map_signal(signal_idx);
440                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);  // Will be changed to deinterlaced at get_chain() time if needed.
441                 return true;
442 #ifdef HAVE_CEF
443         } else if (luaL_testudata(L, idx, "HTMLInput")) {
444                 CEFCapture *capture = *(CEFCapture **)luaL_checkudata(L, idx, "HTMLInput");
445                 block->signal_type_to_connect = Block::CONNECT_CEF;
446                 block->cef_to_connect = capture;
447                 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
448                 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
449                 return true;
450 #endif
451         } else if (luaL_testudata(L, idx, "VideoInput")) {
452                 FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, idx, "VideoInput");
453                 block->signal_type_to_connect = Block::CONNECT_VIDEO;
454                 block->video_to_connect = capture;
455                 if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
456                         block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_PLANAR);
457                 } else {
458                         assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
459                         block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
460                 }
461                 return true;
462         } else if (luaL_testudata(L, idx, "ImageInput")) {
463                 ImageInput *image = *(ImageInput **)luaL_checkudata(L, idx, "ImageInput");
464                 block->signal_type_to_connect = Block::CONNECT_NONE;
465                 block->currently_chosen_alternative = find_index_of(block, IMAGE_INPUT);
466                 block->pathname = image->get_pathname();
467                 return true;
468         } else {
469                 return false;
470         }
471 }
472
473 int Block_display(lua_State* L)
474 {
475         assert(lua_gettop(L) == 2);
476         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
477         if (!block->is_input) {
478                 luaL_error(L, "display() called on something that isn't an input");
479         }
480
481         bool ok = display(block, L, 2);
482         if (!ok) {
483                 luaL_error(L, "display() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
484         }
485
486         if (block->currently_chosen_alternative == -1) {
487                 luaL_error(L, "display() called on an input whose type was fixed at construction time, with a signal of different type");
488         }
489
490         return 0;
491 }
492
493 int Block_choose(lua_State* L)
494 {
495         assert(lua_gettop(L) == 2);
496         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
497         int alternative_idx = -1;
498         if (lua_isnumber(L, 2)) {
499                 alternative_idx = luaL_checknumber(L, 2);
500         } else if (lua_istable(L, 2)) {
501                 // See if it's an Effect metatable (e.g. foo:choose(ResampleEffect))
502                 lua_getfield(L, 2, "__effect_type_id");
503                 if (lua_isnumber(L, -1)) {
504                         EffectType effect_type = EffectType(luaL_checknumber(L, -1));
505                         alternative_idx = find_index_of(block, effect_type);
506                 }
507                 lua_pop(L, 1);
508         }
509
510         if (alternative_idx == -1) {
511                 luaL_error(L, "choose() called with something that was not an index or an effect type (e.g. ResampleEffect) that was part of the alternatives");
512         }
513
514         assert(alternative_idx >= 0);
515         assert(size_t(alternative_idx) < block->alternatives.size());
516         block->currently_chosen_alternative = alternative_idx;
517
518         return wrap_lua_existing_object_nonowned<EffectBlueprint>(L, "EffectBlueprint", block->alternatives[alternative_idx]);
519 }
520
521 int Block_enable(lua_State *L)
522 {
523         assert(lua_gettop(L) == 1);
524         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
525
526         if (block->alternatives.size() != 2 ||
527             block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
528                 luaL_error(L, "enable() called on something that wasn't added with add_optional_effect()");
529         }
530         block->currently_chosen_alternative = 0;  // The actual effect.
531         return 0;
532 }
533
534 int Block_disable(lua_State *L)
535 {
536         assert(lua_gettop(L) == 1);
537         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
538
539         block->currently_chosen_alternative = find_index_of(block, IDENTITY_EFFECT);
540         if (block->currently_chosen_alternative == -1) {
541                 luaL_error(L, "disable() called on something that didn't have an IdentityEffect fallback (try add_optional_effect())");
542         }
543         assert(block->currently_chosen_alternative != -1);
544         return 0;
545 }
546
547 int Block_set_int(lua_State *L)
548 {
549         assert(lua_gettop(L) == 3);
550         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
551         string key = checkstdstring(L, 2);
552         float value = luaL_checknumber(L, 3);
553
554         // TODO: check validity already here, if possible?
555         block->int_parameters[key] = value;
556
557         return 0;
558 }
559
560 int Block_set_float(lua_State *L)
561 {
562         assert(lua_gettop(L) == 3);
563         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
564         string key = checkstdstring(L, 2);
565         float value = luaL_checknumber(L, 3);
566
567         // TODO: check validity already here, if possible?
568         block->float_parameters[key] = value;
569
570         return 0;
571 }
572
573 int Block_set_vec3(lua_State *L)
574 {
575         assert(lua_gettop(L) == 5);
576         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
577         string key = checkstdstring(L, 2);
578         array<float, 3> v;
579         v[0] = luaL_checknumber(L, 3);
580         v[1] = luaL_checknumber(L, 4);
581         v[2] = luaL_checknumber(L, 5);
582
583         // TODO: check validity already here, if possible?
584         block->vec3_parameters[key] = v;
585
586         return 0;
587 }
588
589 int Block_set_vec4(lua_State *L)
590 {
591         assert(lua_gettop(L) == 6);
592         Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
593         string key = checkstdstring(L, 2);
594         array<float, 4> v;
595         v[0] = luaL_checknumber(L, 3);
596         v[1] = luaL_checknumber(L, 4);
597         v[2] = luaL_checknumber(L, 5);
598         v[3] = luaL_checknumber(L, 6);
599
600         // TODO: check validity already here, if possible?
601         block->vec4_parameters[key] = v;
602
603         return 0;
604 }
605