8 #include "cef_capture.h"
10 #include "ffmpeg_capture.h"
12 #include "image_input.h"
13 #include "input_state.h"
14 #include "lua_utils.h"
18 using namespace movit;
21 bool display(Block *block, lua_State *L, int idx);
23 EffectType current_type(const Block *block)
25 return block->alternatives[block->currently_chosen_alternative]->effect_type;
28 int find_index_of(const Block *block, EffectType val)
30 for (size_t idx = 0; idx < block->alternatives.size(); ++idx) {
31 if (block->alternatives[idx]->effect_type == val) {
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()) {}
41 size_t Scene::compute_chain_number(bool is_main_chain) const
43 assert(chains.size() > 0);
44 assert(chains.size() % 2 == 0);
45 bitset<256> disabled = find_disabled_blocks(size_t(-1));
47 size_t chain_number = compute_chain_number_for_block(blocks.size() - 1, disabled);
48 assert(chain_number < chains.size() / 2);
50 chain_number += chains.size() / 2;
55 size_t Scene::compute_chain_number_for_block(size_t block_idx, const bitset<256> &disabled) const
57 Block *block = blocks[block_idx];
60 size_t currently_chosen_alternative;
61 if (disabled.test(block_idx)) {
62 // It doesn't matter, so pick the canonical choice
63 // (this is the only one that is actually instantiated).
64 currently_chosen_alternative = block->canonical_alternative;
66 currently_chosen_alternative = block->currently_chosen_alternative;
68 assert(currently_chosen_alternative < block->alternatives.size());
71 assert(block->cardinality_base == 1);
72 chain_number = currently_chosen_alternative;
74 chain_number = compute_chain_number_for_block(block_idx - 1, disabled) + block->cardinality_base * currently_chosen_alternative;
79 bitset<256> Scene::find_disabled_blocks(size_t chain_idx) const
81 assert(blocks.size() < 256);
83 // The find_disabled_blocks() recursion logic needs only one pass by itself,
84 // but the disabler logic is not so smart, so we just run multiple times
85 // until it converges.
86 bitset<256> prev, ret;
88 find_disabled_blocks(chain_idx, blocks.size() - 1, /*currently_disabled=*/false, &ret);
90 for (Block *block : blocks) {
91 EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
92 if (ret.test(block->idx) || chosen_type == IDENTITY_EFFECT) continue; // Already disabled.
94 for (Block::Index disabler_idx : block->disablers) {
95 Block *disabler = blocks[disabler_idx];
96 EffectType chosen_type = disabler->alternatives[disabler->chosen_alternative(chain_idx)]->effect_type;
97 if (ret.test(disabler->idx) || chosen_type == IDENTITY_EFFECT) {
103 } while (prev != ret);
107 void Scene::find_disabled_blocks(size_t chain_idx, size_t block_idx, bool currently_disabled, bitset<256> *disabled) const
109 if (currently_disabled) {
110 disabled->set(block_idx);
112 Block *block = blocks[block_idx];
113 EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
114 for (size_t input_idx = 0; input_idx < block->inputs.size(); ++input_idx) {
115 if (chosen_type == IDENTITY_EFFECT && input_idx > 0) {
116 // Multi-input effect that has been replaced by
117 // IdentityEffect, so every effect but the first are
118 // disabled and will not participate in the chain.
119 find_disabled_blocks(chain_idx, block->inputs[input_idx], /*currently_disabled=*/true, disabled);
121 // Just keep on recursing down.
122 find_disabled_blocks(chain_idx, block->inputs[input_idx], currently_disabled, disabled);
127 bool Scene::is_noncanonical_chain(size_t chain_idx) const
129 bitset<256> disabled = find_disabled_blocks(chain_idx);
130 if (disabled.none()) {
133 assert(blocks.size() < 256);
134 for (size_t block_idx = 0; block_idx < blocks.size(); ++block_idx) {
135 Block *block = blocks[block_idx];
136 if (disabled.test(block_idx) && block->chosen_alternative(chain_idx) != block->canonical_alternative) {
143 int Scene::add_input(lua_State* L)
145 assert(lua_gettop(L) == 1 || lua_gettop(L) == 2);
146 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
148 Block *block = new Block;
149 block->idx = scene->blocks.size();
150 if (lua_gettop(L) == 1) {
151 // No parameter given, so a flexible input.
152 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
153 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
154 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
155 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
156 block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
158 // Input of a given type. We'll specialize it here, plus connect the input as given.
159 if (lua_isnumber(L, 2)) {
160 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR));
161 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_WITH_DEINTERLACE));
163 } else if (luaL_testudata(L, 2, "HTMLInput")) {
164 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
166 } else if (luaL_testudata(L, 2, "VideoInput")) {
167 FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, 2, "VideoInput");
168 if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
169 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_YCBCR_PLANAR));
171 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
172 block->alternatives.emplace_back(new EffectBlueprint(LIVE_INPUT_BGRA));
174 } else if (luaL_testudata(L, 2, "ImageInput")) {
175 block->alternatives.emplace_back(new EffectBlueprint(IMAGE_INPUT));
177 luaL_error(L, "add_input() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
179 bool ok = display(block, L, 2);
182 block->is_input = true;
183 scene->blocks.push_back(block);
185 return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
188 void Scene::find_inputs_for_block(lua_State *L, Scene *scene, Block *block)
190 if (lua_gettop(L) == 2) {
191 // Implicitly the last added effect.
192 assert(!scene->blocks.empty());
193 block->inputs.push_back(scene->blocks.size() - 1);
197 for (int idx = 3; idx <= lua_gettop(L); ++idx) {
198 Block *input_block = nullptr;
199 if (luaL_testudata(L, idx, "Block")) {
200 input_block = *(Block **)luaL_checkudata(L, idx, "Block");
202 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, idx, "EffectBlueprint");
204 // Search through all the blocks to figure out which one contains this effect.
205 for (Block *block : scene->blocks) {
206 if (find(block->alternatives.begin(), block->alternatives.end(), blueprint) != block->alternatives.end()) {
211 if (input_block == nullptr) {
212 luaL_error(L, "Input effect in parameter #%d has not been added to this scene", idx - 1);
215 block->inputs.push_back(input_block->idx);
219 int Scene::add_effect(lua_State* L)
221 assert(lua_gettop(L) >= 2);
222 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
224 Block *block = new Block;
225 block->idx = scene->blocks.size();
227 if (lua_istable(L, 2)) {
228 size_t len = lua_objlen(L, 2);
229 for (size_t i = 0; i < len; ++i) {
230 lua_rawgeti(L, 2, i + 1);
231 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, -1, "EffectBlueprint");
232 block->alternatives.push_back(blueprint);
236 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
237 block->alternatives.push_back(blueprint);
240 int identity_index = find_index_of(block, IDENTITY_EFFECT);
241 if (identity_index == -1) {
242 block->canonical_alternative = 0;
244 // Pick the IdentityEffect as the canonical alternative, in case it
245 // helps us disable more stuff.
246 block->canonical_alternative = identity_index;
249 find_inputs_for_block(L, scene, block);
250 scene->blocks.push_back(block);
252 return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
255 int Scene::add_optional_effect(lua_State* L)
257 assert(lua_gettop(L) >= 2);
258 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
260 Block *block = new Block;
261 block->idx = scene->blocks.size();
263 EffectBlueprint *blueprint = *(EffectBlueprint **)luaL_checkudata(L, 2, "EffectBlueprint");
264 block->alternatives.push_back(blueprint);
266 // An IdentityEffect will be the alternative for when the effect is disabled.
267 block->alternatives.push_back(new EffectBlueprint(IDENTITY_EFFECT));
269 block->canonical_alternative = 1;
271 find_inputs_for_block(L, scene, block);
272 scene->blocks.push_back(block);
274 return wrap_lua_existing_object_nonowned<Block>(L, "Block", block);
277 Effect *Scene::instantiate_effects(const Block *block, size_t chain_idx, Scene::Instantiation *instantiation)
279 // Find the chosen alternative for this block in this instance.
280 EffectType chosen_type = block->alternatives[block->chosen_alternative(chain_idx)]->effect_type;
282 vector<Effect *> inputs;
283 for (size_t input_idx : block->inputs) {
284 inputs.push_back(instantiate_effects(blocks[input_idx], chain_idx, instantiation));
286 // As a special case, we allow IdentityEffect to take only one input
287 // even if the other alternative (or alternatives) is multi-input.
288 // Thus, even if there are more than one inputs, instantiate only
290 if (chosen_type == IDENTITY_EFFECT) {
296 switch (chosen_type) {
297 case LIVE_INPUT_YCBCR:
298 case LIVE_INPUT_YCBCR_WITH_DEINTERLACE:
299 case LIVE_INPUT_YCBCR_PLANAR:
300 case LIVE_INPUT_BGRA: {
301 bool deinterlace = (chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
302 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.
303 bmusb::PixelFormat pixel_format;
304 if (chosen_type == LIVE_INPUT_BGRA) {
305 pixel_format = bmusb::PixelFormat_8BitBGRA;
306 } else if (chosen_type == LIVE_INPUT_YCBCR_PLANAR) {
307 pixel_format = bmusb::PixelFormat_8BitYCbCrPlanar;
308 } else if (global_flags.ten_bit_input) {
309 pixel_format = bmusb::PixelFormat_10BitYCbCr;
311 pixel_format = bmusb::PixelFormat_8BitYCbCr;
313 LiveInputWrapper *input = new LiveInputWrapper(theme, instantiation->chain.get(), pixel_format, override_bounce, deinterlace, /*user_connectable=*/true);
314 effect = input->get_effect(); // Adds itself to the chain, so no need to call add_effect().
315 instantiation->inputs.emplace(block->idx, input);
319 ImageInput *input = new ImageInput;
320 instantiation->chain->add_input(input);
321 instantiation->image_inputs.emplace(block->idx, input);
326 effect = instantiate_effect(instantiation->chain.get(), chosen_type);
327 instantiation->chain->add_effect(effect, inputs);
330 instantiation->effects.emplace(block->idx, effect);
334 int Scene::finalize(lua_State* L)
336 bool only_one_mode = false;
337 bool chosen_mode = false;
338 if (lua_gettop(L) == 2) {
339 only_one_mode = true;
340 chosen_mode = checkbool(L, 2);
342 assert(lua_gettop(L) == 1);
344 Scene *scene = (Scene *)luaL_checkudata(L, 1, "Scene");
345 Theme *theme = get_theme_updata(L);
348 for (Block *block : scene->blocks) {
349 block->cardinality_base = base;
350 base *= block->alternatives.size();
353 const size_t cardinality = base;
354 size_t real_cardinality = 0;
355 for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
356 if (!scene->is_noncanonical_chain(chain_idx)) {
360 const size_t total_cardinality = real_cardinality * (only_one_mode ? 1 : 2);
361 if (total_cardinality > 200) {
362 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",
366 Block *output_block = scene->blocks.back();
367 for (bool is_main_chain : { false, true }) {
368 for (size_t chain_idx = 0; chain_idx < cardinality; ++chain_idx) {
369 if ((only_one_mode && is_main_chain != chosen_mode) ||
370 scene->is_noncanonical_chain(chain_idx)) {
371 scene->chains.emplace_back();
375 Scene::Instantiation instantiation;
376 instantiation.chain.reset(new EffectChain(scene->aspect_nom, scene->aspect_denom, theme->get_resource_pool()));
377 scene->instantiate_effects(output_block, chain_idx, &instantiation);
379 add_outputs_and_finalize(instantiation.chain.get(), is_main_chain);
380 scene->chains.emplace_back(move(instantiation));
386 std::pair<movit::EffectChain *, std::function<void()>>
387 Scene::get_chain(Theme *theme, lua_State *L, unsigned num, const InputState &input_state)
389 // For video inputs, pick the right interlaced/progressive version
390 // based on the current state of the signals.
391 InputStateInfo info(input_state);
392 for (Block *block : blocks) {
393 if (block->is_input && block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
394 EffectType chosen_type = current_type(block);
395 assert(chosen_type == LIVE_INPUT_YCBCR || chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
396 if (info.last_interlaced[block->signal_to_connect]) {
397 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_WITH_DEINTERLACE);
399 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR);
404 // Pick out the right chain based on the current selections,
405 // and snapshot all the set variables so that we can set them
406 // in the prepare function even if they're being changed by
407 // the Lua code later.
408 bool is_main_chain = (num == 0);
409 size_t chain_idx = compute_chain_number(is_main_chain);
410 const Scene::Instantiation &instantiation = chains[chain_idx];
411 EffectChain *effect_chain = instantiation.chain.get();
413 map<LiveInputWrapper *, int> signals_to_connect;
414 map<ImageInput *, string> images_to_select;
415 map<pair<Effect *, string>, int> int_to_set;
416 map<pair<Effect *, string>, float> float_to_set;
417 map<pair<Effect *, string>, array<float, 3>> vec3_to_set;
418 map<pair<Effect *, string>, array<float, 4>> vec4_to_set;
419 for (const auto &index_and_input : instantiation.inputs) {
420 Block *block = blocks[index_and_input.first];
421 EffectType chosen_type = current_type(block);
422 LiveInputWrapper *input = index_and_input.second;
423 if (chosen_type == LIVE_INPUT_YCBCR ||
424 chosen_type == LIVE_INPUT_YCBCR_WITH_DEINTERLACE ||
425 chosen_type == LIVE_INPUT_YCBCR_PLANAR ||
426 chosen_type == LIVE_INPUT_BGRA) {
427 if (block->signal_type_to_connect == Block::CONNECT_SIGNAL) {
428 signals_to_connect.emplace(input, block->signal_to_connect);
430 } else if (block->signal_type_to_connect == Block::CONNECT_CEF) {
431 signals_to_connect.emplace(input, block->cef_to_connect->get_card_index());
433 } else if (block->signal_type_to_connect == Block::CONNECT_VIDEO) {
434 signals_to_connect.emplace(input, block->video_to_connect->get_card_index());
435 } else if (block->signal_type_to_connect == Block::CONNECT_NONE) {
436 luaL_error(L, "An input in a scene was not connected to anything (forgot to call display())");
442 for (const auto &index_and_input : instantiation.image_inputs) {
443 Block *block = blocks[index_and_input.first];
444 ImageInput *input = index_and_input.second;
445 if (current_type(block) == IMAGE_INPUT) {
446 images_to_select.emplace(input, block->pathname);
449 for (const auto &index_and_effect : instantiation.effects) {
450 Block *block = blocks[index_and_effect.first];
451 Effect *effect = index_and_effect.second;
453 // Get the effects currently set on the block.
454 if (current_type(block) != IDENTITY_EFFECT) { // Ignore settings on optional effects.
455 for (const auto &key_and_tuple : block->int_parameters) {
456 int_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
458 for (const auto &key_and_tuple : block->float_parameters) {
459 float_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
461 for (const auto &key_and_tuple : block->vec3_parameters) {
462 vec3_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
464 for (const auto &key_and_tuple : block->vec4_parameters) {
465 vec4_to_set.emplace(make_pair(effect, key_and_tuple.first), key_and_tuple.second);
469 // Parameters set on the blueprint itself override those that are set for the block,
470 // so they are set afterwards.
471 if (!block->alternatives.empty()) {
472 EffectBlueprint *blueprint = block->alternatives[block->currently_chosen_alternative];
473 for (const auto &key_and_tuple : blueprint->int_parameters) {
474 int_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
476 for (const auto &key_and_tuple : blueprint->float_parameters) {
477 float_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
479 for (const auto &key_and_tuple : blueprint->vec3_parameters) {
480 vec3_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
482 for (const auto &key_and_tuple : blueprint->vec4_parameters) {
483 vec4_to_set[make_pair(effect, key_and_tuple.first)] = key_and_tuple.second;
490 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]{
491 lock_guard<mutex> lock(theme->m);
493 // Set up state, including connecting signals.
494 for (const auto &input_and_signal : signals_to_connect) {
495 LiveInputWrapper *input = input_and_signal.first;
496 input->connect_signal_raw(input_and_signal.second, input_state);
498 for (const auto &input_and_filename : images_to_select) {
499 input_and_filename.first->switch_image(input_and_filename.second);
501 for (const auto &effect_and_key_and_value : int_to_set) {
502 Effect *effect = effect_and_key_and_value.first.first;
503 const string &key = effect_and_key_and_value.first.second;
504 const int value = effect_and_key_and_value.second;
505 if (!effect->set_int(key, value)) {
506 luaL_error(L, "Effect refused set_int(\"%s\", %d) (invalid key?)", key.c_str(), value);
509 for (const auto &effect_and_key_and_value : float_to_set) {
510 Effect *effect = effect_and_key_and_value.first.first;
511 const string &key = effect_and_key_and_value.first.second;
512 const float value = effect_and_key_and_value.second;
513 if (!effect->set_float(key, value)) {
514 luaL_error(L, "Effect refused set_float(\"%s\", %f) (invalid key?)", key.c_str(), value);
517 for (const auto &effect_and_key_and_value : vec3_to_set) {
518 Effect *effect = effect_and_key_and_value.first.first;
519 const string &key = effect_and_key_and_value.first.second;
520 const float *value = effect_and_key_and_value.second.data();
521 if (!effect->set_vec3(key, value)) {
522 luaL_error(L, "Effect refused set_vec3(\"%s\", %f, %f, %f) (invalid key?)", key.c_str(),
523 value[0], value[1], value[2]);
526 for (const auto &effect_and_key_and_value : vec4_to_set) {
527 Effect *effect = effect_and_key_and_value.first.first;
528 const string &key = effect_and_key_and_value.first.second;
529 const float *value = effect_and_key_and_value.second.data();
530 if (!effect->set_vec4(key, value)) {
531 luaL_error(L, "Effect refused set_vec4(\"%s\", %f, %f, %f, %f) (invalid key?)", key.c_str(),
532 value[0], value[1], value[2], value[3]);
536 return make_pair(effect_chain, move(setup_chain));
539 bool display(Block *block, lua_State *L, int idx)
541 if (lua_isnumber(L, idx)) {
542 Theme *theme = get_theme_updata(L);
543 int signal_idx = luaL_checknumber(L, idx);
544 block->signal_type_to_connect = Block::CONNECT_SIGNAL;
545 block->signal_to_connect = theme->map_signal(signal_idx);
546 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR); // Will be changed to deinterlaced at get_chain() time if needed.
549 } else if (luaL_testudata(L, idx, "HTMLInput")) {
550 CEFCapture *capture = *(CEFCapture **)luaL_checkudata(L, idx, "HTMLInput");
551 block->signal_type_to_connect = Block::CONNECT_CEF;
552 block->cef_to_connect = capture;
553 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
554 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
557 } else if (luaL_testudata(L, idx, "VideoInput")) {
558 FFmpegCapture *capture = *(FFmpegCapture **)luaL_checkudata(L, idx, "VideoInput");
559 block->signal_type_to_connect = Block::CONNECT_VIDEO;
560 block->video_to_connect = capture;
561 if (capture->get_current_pixel_format() == bmusb::PixelFormat_8BitYCbCrPlanar) {
562 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_YCBCR_PLANAR);
564 assert(capture->get_current_pixel_format() == bmusb::PixelFormat_8BitBGRA);
565 block->currently_chosen_alternative = find_index_of(block, LIVE_INPUT_BGRA);
568 } else if (luaL_testudata(L, idx, "ImageInput")) {
569 ImageInput *image = *(ImageInput **)luaL_checkudata(L, idx, "ImageInput");
570 block->signal_type_to_connect = Block::CONNECT_NONE;
571 block->currently_chosen_alternative = find_index_of(block, IMAGE_INPUT);
572 block->pathname = image->get_pathname();
579 int Block_display(lua_State* L)
581 assert(lua_gettop(L) == 2);
582 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
583 if (!block->is_input) {
584 luaL_error(L, "display() called on something that isn't an input");
587 bool ok = display(block, L, 2);
589 luaL_error(L, "display() called with something that's not a signal (a signal number, a HTML input, or a VideoInput)");
592 if (block->currently_chosen_alternative == -1) {
593 luaL_error(L, "display() called on an input whose type was fixed at construction time, with a signal of different type");
599 int Block_choose(lua_State* L)
601 assert(lua_gettop(L) == 2);
602 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
603 int alternative_idx = -1;
604 if (lua_isnumber(L, 2)) {
605 alternative_idx = luaL_checknumber(L, 2);
606 } else if (lua_istable(L, 2)) {
607 // See if it's an Effect metatable (e.g. foo:choose(ResampleEffect))
608 lua_getfield(L, 2, "__effect_type_id");
609 if (lua_isnumber(L, -1)) {
610 EffectType effect_type = EffectType(luaL_checknumber(L, -1));
611 alternative_idx = find_index_of(block, effect_type);
616 if (alternative_idx == -1) {
617 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");
620 assert(alternative_idx >= 0);
621 assert(size_t(alternative_idx) < block->alternatives.size());
622 block->currently_chosen_alternative = alternative_idx;
624 return wrap_lua_existing_object_nonowned<EffectBlueprint>(L, "EffectBlueprint", block->alternatives[alternative_idx]);
627 int Block_enable(lua_State *L)
629 assert(lua_gettop(L) == 1);
630 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
632 if (block->alternatives.size() != 2 ||
633 block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
634 luaL_error(L, "enable() called on something that wasn't added with add_optional_effect()");
636 block->currently_chosen_alternative = 0; // The actual effect.
640 int Block_enable_if(lua_State *L)
642 assert(lua_gettop(L) == 2);
643 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
645 if (block->alternatives.size() != 2 ||
646 block->alternatives[1]->effect_type != IDENTITY_EFFECT) {
647 luaL_error(L, "enable_if() called on something that wasn't added with add_optional_effect()");
649 bool enabled = checkbool(L, 2);
650 block->currently_chosen_alternative = enabled ? 0 : 1;
654 int Block_disable(lua_State *L)
656 assert(lua_gettop(L) == 1);
657 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
659 block->currently_chosen_alternative = find_index_of(block, IDENTITY_EFFECT);
660 if (block->currently_chosen_alternative == -1) {
661 luaL_error(L, "disable() called on something that didn't have an IdentityEffect fallback (try add_optional_effect())");
663 assert(block->currently_chosen_alternative != -1);
667 int Block_always_disable_if_disabled(lua_State *L)
669 assert(lua_gettop(L) == 2);
670 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
671 Block *disabler_block = *(Block **)luaL_checkudata(L, 2, "Block");
673 int my_alternative = find_index_of(block, IDENTITY_EFFECT);
674 int their_alternative = find_index_of(disabler_block, IDENTITY_EFFECT);
675 if (my_alternative == -1) {
676 luaL_error(L, "always_disable_if_disabled() called on something that didn't have an IdentityEffect fallback (try add_optional_effect())");
678 if (their_alternative == -1) {
679 luaL_error(L, "always_disable_if_disabled() with an argument that didn't have an IdentityEffect fallback (try add_optional_effect())");
682 block->disablers.push_back(disabler_block->idx);
688 int Block_set_int(lua_State *L)
690 assert(lua_gettop(L) == 3);
691 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
692 string key = checkstdstring(L, 2);
693 float value = luaL_checknumber(L, 3);
695 // TODO: check validity already here, if possible?
696 block->int_parameters[key] = value;
701 int Block_set_float(lua_State *L)
703 assert(lua_gettop(L) == 3);
704 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
705 string key = checkstdstring(L, 2);
706 float value = luaL_checknumber(L, 3);
708 // TODO: check validity already here, if possible?
709 block->float_parameters[key] = value;
714 int Block_set_vec3(lua_State *L)
716 assert(lua_gettop(L) == 5);
717 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
718 string key = checkstdstring(L, 2);
720 v[0] = luaL_checknumber(L, 3);
721 v[1] = luaL_checknumber(L, 4);
722 v[2] = luaL_checknumber(L, 5);
724 // TODO: check validity already here, if possible?
725 block->vec3_parameters[key] = v;
730 int Block_set_vec4(lua_State *L)
732 assert(lua_gettop(L) == 6);
733 Block *block = *(Block **)luaL_checkudata(L, 1, "Block");
734 string key = checkstdstring(L, 2);
736 v[0] = luaL_checknumber(L, 3);
737 v[1] = luaL_checknumber(L, 4);
738 v[2] = luaL_checknumber(L, 5);
739 v[3] = luaL_checknumber(L, 6);
741 // TODO: check validity already here, if possible?
742 block->vec4_parameters[key] = v;