{***********************************************
Battery 3 - Slot 1 (Group Start Options)
Author: Native Instruments
Written by: Josef Natterer (natterer-script@web.de)
Modified: August 24, 2006
Version 0.9.25
*************************************************}

on init
	SET_CONDITION(NO_SYS_SCRIPT_GROUP_START )
	SET_CONDITION(NO_SYS_SCRIPT_RLS_TRIG )
	message("")
	declare const $CYCLE_NR := 16
	
	declare ui_label $trigger_label (1,1)
	set_text ($trigger_label,"Trigger")
	
	declare ui_button $Cond1_status
	declare ui_button $Cond2_status
	declare ui_button $Cond_both_status
	declare ui_button $Key_status
	declare ui_button $Cell_green
	declare ui_button $Cell_red

	declare ui_label $condition2_label (1,1)
	set_text ($condition2_label,"Condition 2")
	
	declare ui_menu $trigger_menu
	add_menu_item ($trigger_menu,"Note On",0)
	add_menu_item ($trigger_menu,"Note Off",1)
	{add_menu_item ($trigger_menu,"Controller",2)}
	declare %trigger_menu_store[128]
	
	declare ui_value_edit $Cell_Nr(0,127,1)
	declare ui_label $Cell_label (1,1)
	set_text($Cell_label,"Cell:")
	
	
	
	{CONDITION 1}
	
	declare ui_label $condition1_label (1,1)
	set_text ($condition1_label,"Condition 1")
	
	declare ui_menu $condition1_menu
	add_menu_item ($condition1_menu,"Always",0)
	add_menu_item ($condition1_menu,"On Key",1)
	add_menu_item ($condition1_menu,"On Controller",2)
	add_menu_item ($condition1_menu,"Round Robin",3)
	add_menu_item ($condition1_menu,"Cycle Random",4)
	
	declare ui_value_edit $low_key_1 (0,127,1)
	set_text ($low_key_1,"Lo Key")
	
	declare ui_value_edit $high_key_1 (0,127,1)
	set_text ($high_key_1,"Hi Key")
	
	declare ui_value_edit $ctrl_1 (0,127,1)
	set_text ($ctrl_1,"CC")
	
	declare ui_value_edit $low_ctrl_1 (0,127,1)
	set_text ($low_ctrl_1,"Lo Ctrl")
	
	declare ui_value_edit $high_ctrl_1 (0,127,1)
	set_text ($high_ctrl_1,"Hi Ctrl")
	
	declare ui_value_edit $position_1 (0,100,1)
	set_text ($position_1,"Position")
	declare ui_value_edit $round_nr_1 (1,$CYCLE_NR,1)
	set_text ($round_nr_1,"Cycle Nr 1")
	declare ui_value_edit $CC_reset_1 (-1,127,1)
	set_text ($CC_reset_1,"Reset CC 1")
	
	
	{CONDITION 2}
	
	declare ui_menu $condition2_menu
	add_menu_item ($condition2_menu,"Always",0)
	add_menu_item ($condition2_menu,"On Key",1)
	add_menu_item ($condition2_menu,"On Controller",2)
	add_menu_item ($condition2_menu,"Round Robin",3)
	add_menu_item ($condition2_menu,"Cycle Random",4)
	
	declare ui_value_edit $low_key_2 (0,127,1)
	set_text ($low_key_2,"Lo Key")
	
	declare ui_value_edit $high_key_2 (0,127,1)
	set_text ($high_key_2,"Hi Key")
	
	
	declare ui_value_edit $ctrl_2 (0,127,1)
	set_text ($ctrl_2,"CC")
	
	declare ui_value_edit $low_ctrl_2 (0,127,1)
	set_text ($low_ctrl_2,"Lo Ctrl")
	
	declare ui_value_edit $high_ctrl_2 (0,127,1)
	set_text ($high_ctrl_2,"Hi Ctrl")
	
	declare ui_value_edit $position_2 (0,100,1)
	set_text ($position_2,"Position")
	declare ui_value_edit $round_nr_2 (1,$CYCLE_NR,1)
	set_text ($round_nr_2,"Cycle Nr 2")

	declare ui_value_edit $CC_reset_2 (-1,127,1)
	set_text ($CC_reset_2,"Reset CC 2")

	declare %condition_1_store[128]
	declare %low_key_1_store[128]
	declare %high_key_1_store[128]
	declare %low_ctrl_1_store[128]
	declare %high_ctrl_1_store[128]
	declare %position_1_store[128]
	declare %round_nr_1_store[128]
	declare %rand_nr_1_store[128]
	declare %rand_pos_1_store[128]
	declare %CC_1_store[128]
	declare %condition_2_store[128]
	declare %low_key_2_store[128]
	declare %high_key_2_store[128]
	declare %low_ctrl_2_store[128]
	declare %high_ctrl_2_store[128]
	declare %position_2_store[128]
	declare %round_nr_2_store[128]
	declare %rand_nr_2_store[128]
	declare %rand_pos_2_store[128]
	declare %CC_2_store[128]
	declare %key_status_store[128]
	
	declare %round_max_store[$CYCLE_NR]
	declare %round_max_old_store[$CYCLE_NR]
	declare %round_cursor_store[$CYCLE_NR]
	declare %c_store[$CYCLE_NR]
	declare %cc_reset_store[$CYCLE_NR]

	declare %rand_cursor_store[$CYCLE_NR]
	declare %rand_c_store[$CYCLE_NR]
	declare %rand_max_old_store[$CYCLE_NR]
	declare %rand_max_store[$CYCLE_NR]
	declare %rand_old_max_store[$CYCLE_NR]
	{INIT }
	
	$low_key_1 := 24
	$high_key_1 := 24
	
	$low_key_2 := 24
	$high_key_2 := 24
	
	$ctrl_1 := 1
	$low_ctrl_1 := 0
	$high_ctrl_1 := 64
	
	$ctrl_2 := 1
	$low_ctrl_2 := 0
	$high_ctrl_2 := 64
	
	$position_1 := 1
	$position_2 := 1
	
	
	
	move_control ($Cond1_status,2,1)
	move_control ($Cond2_status,2,2)
	move_control ($Cond_both_status,2,3)
	move_control ($Key_status,1,1)
	move_control ($Cell_green,3,1)
	move_control ($Cell_red,3,2)

	move_control ($trigger_label,0,0)
	move_control ($trigger_menu,1,2)
	
	move_control ($condition1_label,0,0)
	move_control ($condition1_menu,0,0)
	
	move_control ($low_key_1,0,0)
	move_control ($high_key_1,0,0)
	
	move_control ($ctrl_1,0,0)
	move_control ($low_ctrl_1,0,0)
	move_control ($high_ctrl_1,0,0)
	
	move_control ($position_1,0,0)
	move_control ($round_nr_1,0,0)
	move_control ($cc_reset_1,0,0)	
	
	move_control ($condition2_label,0,0)
	move_control ($condition2_menu,0,0)
	
	move_control ($low_key_2,0,0)
	move_control ($high_key_2,0,0)
	
	move_control ($ctrl_2,0,0)
	move_control ($low_ctrl_2,0,0)
	move_control ($high_ctrl_2,0,0)
	
	move_control ($position_2,0,0)
	move_control ($round_nr_2,0,0)
	move_control ($cc_reset_2,0,0)	
	
	move_control ($cell_label,0,0)
	move_control ($cell_nr,0,0)
	$round_nr_1 := 1
	$round_nr_2 := 1
	declare $a
	declare $rand_helper
	declare $id
	declare $duration
	declare %allowed_store[128]
	declare $active_key
	declare $default_active_key
	make_persistent($active_key)
	_read_persistent_var($active_key)
	declare %kw_keys[128]
	declare $round_max_old
	declare polyphonic $trigger_time
	declare polyphonic $cur_group
	make_persistent(%round_cursor_store )
	make_persistent(%round_max_store )
	make_persistent(%round_nr_1_store)
	make_persistent(%round_nr_2_store)
	make_persistent(%rand_nr_1_store)
	make_persistent(%rand_nr_2_store)
	make_persistent(%rand_pos_1_store)
	make_persistent(%rand_pos_2_store)
	make_persistent(%rand_max_store )
	make_persistent(%cc_reset_store)
	_read_persistent_var(%round_max_store)
	_read_persistent_var(%round_cursor_store)
	_read_persistent_var(%round_nr_1_store)
	_read_persistent_var(%round_nr_2_store)
	_read_persistent_var(%rand_nr_1_store)
	_read_persistent_var(%rand_nr_2_store)
	_read_persistent_var(%rand_pos_1_store)
	_read_persistent_var(%rand_pos_2_store)
	_read_persistent_var(%rand_max_store)
	declare $b
	$a := 0
	while($a < $NUM_GROUPS)
		%condition_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_MODE,$a,0,-1)
		select (%condition_1_store[$a])
			case $START_CRITERIA_ON_KEY
				%low_key_1_store[$a] :=_get_engine_par ($ENGINE_PAR_START_CRITERIA_KEY_MIN,$a,0,-1)
				%high_key_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_KEY_MAX,$a,0,-1)
				if (%high_key_1_store[$a] < %low_key_1_store[$a])
					%high_key_1_store[$a] := %low_key_1_store[$a]
				end if
			case $START_CRITERIA_ON_CONTROLLER
				%CC_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CONTROLLER,$a,0,-1)
				%low_ctrl_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CC_MIN,$a,0,-1)
				%high_ctrl_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CC_MAX,$a,0,-1)
			case $START_CRITERIA_CYCLE_ROUND_ROBIN
				%position_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CYCLE_CLASS,$a,0,-1)+1
		end select
		%condition_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_MODE,$a,1,-1)
		select (%condition_2_store[$a])
			case $START_CRITERIA_ON_KEY
				%low_key_2_store[$a] :=_get_engine_par ($ENGINE_PAR_START_CRITERIA_KEY_MIN,$a,1,-1)
				%high_key_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_KEY_MAX,$a,1,-1)
				if (%high_key_2_store[$a] < %low_key_2_store[$a])
					%high_key_2_store[$a] := %low_key_2_store[$a]
				end if
			case $START_CRITERIA_ON_CONTROLLER
				%CC_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CONTROLLER,$a,1,-1)
				%low_ctrl_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CC_MIN,$a,1,-1)
				%high_ctrl_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CC_MAX,$a,1,-1)
			case $START_CRITERIA_CYCLE_ROUND_ROBIN
				%position_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CYCLE_CLASS,$a,1,-1)+1
		end select
		%trigger_menu_store[$a] := _get_engine_par ($ENGINE_PAR_RELEASE_TRIGGER,$a,0,-1)
		inc($a)
	end while
	$condition1_menu := %condition_1_store[$REF_GROUP_IDX]
	select (%condition_1_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			$low_key_1 := %low_key_1_store[$REF_GROUP_IDX]
			$high_key_1 := %high_key_1_store[$REF_GROUP_IDX]
		case $START_CRITERIA_ON_CONTROLLER
			$ctrl_1 := %CC_1_store[$REF_GROUP_IDX]
			$low_ctrl_1 := %low_ctrl_1_store[$REF_GROUP_IDX]
			$high_ctrl_1 := %high_ctrl_1_store[$REF_GROUP_IDX]
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			$position_1 := %position_1_store[$REF_GROUP_IDX]
			$round_nr_1 := %round_nr_1_store[$REF_GROUP_IDX]+1
			move_control ($round_nr_1,6,3)
			move_control ($cc_reset_1,5,3)	
		case $START_CRITERIA_CYCLE_RANDOM
			$round_nr_1 := %rand_nr_1_store[$REF_GROUP_IDX]+1
			move_control ($round_nr_1,6,3)
	end select
	$condition2_menu := %condition_2_store[$REF_GROUP_IDX]
	select (%condition_2_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			$low_key_2 := %low_key_2_store[$REF_GROUP_IDX]
			$high_key_2 := %high_key_2_store[$REF_GROUP_IDX]
		case $START_CRITERIA_ON_CONTROLLER
			$ctrl_2 := %CC_2_store[$REF_GROUP_IDX]
			$low_ctrl_2 := %low_ctrl_2_store[$REF_GROUP_IDX]
			$high_ctrl_2 := %high_ctrl_2_store[$REF_GROUP_IDX]
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			$position_2 := %position_2_store[$REF_GROUP_IDX]
			$round_nr_2 := %round_nr_2_store[$REF_GROUP_IDX]+1
			move_control ($round_nr_2,6,5)
			move_control ($cc_reset_2,5,5)	
		case $START_CRITERIA_CYCLE_RANDOM
			$round_nr_2 := %rand_nr_2_store[$REF_GROUP_IDX]+1
			move_control ($round_nr_2,6,5)
	end select
	$trigger_menu := %trigger_menu_store[$REF_GROUP_IDX]
 	$Cell_Nr := $REF_GROUP_IDX
	$cond1_status := 1
	$cond2_status := 1
	$cond_both_status := 1
	$a := 0
	while ($a < 128)
		%kw_keys[$a] := 0 
		inc($a)
	end while
	$a := 0
	while ($a < $CYCLE_NR)
		%round_max_store[$a] := 0
		%rand_max_store[$a] := 0
		%cc_reset_store[$a] := -1
		inc($a)
	end while
	$a := 0
	while ($a < 128)
		if (%condition_1_store[$a] = $START_CRITERIA_ON_KEY)
			$b := %low_key_1_store[$a]
			%kw_keys[$b] := 1
			$default_active_key := $b
			inc($b)
			while($b <= %high_key_1_store[$a])
				%kw_keys[$b] := 1
				inc($b)
			end while
		end if
		if (%condition_2_store[$a] = $START_CRITERIA_ON_KEY)
			$b := %low_key_2_store[$a]
			%kw_keys[$b] := 1
			$default_active_key := $b
			inc($b)
			while($b <= %high_key_2_store[$a])
				%kw_keys[$b] := 1
				inc($b)
			end while
		end if
		if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_1_store[$a] > %round_max_store[%round_nr_1_store[$a]])
				%round_max_store[%round_nr_1_store[$a]] := %position_1_store[$a]
			end if
		end if
		if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_2_store[$a] > %round_max_store[%round_nr_2_store[$a]])
				%round_max_store[%round_nr_2_store[$a]] := %position_2_store[$a]
			end if
		end if		
		if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
			inc(%rand_max_store[%rand_nr_1_store[$a]])
		end if
		if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
			inc(%rand_max_store[%rand_nr_2_store[$a]])
		end if		
		inc($a)
	end while
	if (%kw_keys[$active_key] = 0)
		$active_key := 24
		$a := 0
		while($a < 128)
			if (%kw_keys[$a] = 1)
				$active_key := $a
				$a := 128
			end if
			inc($a)
		end while
	end if
	$a := 0
	while($a < $CYCLE_NR)
		if (%round_max_store[$a] > 0)
			%round_cursor_store[$a] := 1
		end if
		if (%rand_max_store[$a] > 0)
			if (%rand_max_store[$a] > 2)
				$rand_helper := random(1,%rand_max_store[$a])
				$b := 0
				while($b < 100 and %rand_cursor_store[$a] = $rand_helper)
					$rand_helper := random(1,%rand_max_store[$a])
					inc($b)
				end while
				%rand_cursor_store[$a] := $rand_helper
			else
				%rand_cursor_store[$a] := random(1,%rand_max_store[$a])
			end if
		end if
		inc($a)
	end while
	select (%condition_1_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_1_store[$REF_GROUP_IDX],%high_key_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(0,%low_ctrl_1_store[$REF_GROUP_IDX],%high_ctrl_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_1_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]])
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_1_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_1_store[$REF_GROUP_IDX]])
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
	end select
	select (%condition_2_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_2_store[$REF_GROUP_IDX],%high_key_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(0,%low_ctrl_2_store[$REF_GROUP_IDX],%high_ctrl_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_2_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_2_store[$REF_GROUP_IDX]])
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_2_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_2_store[$REF_GROUP_IDX]])
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
	end select
	_read_persistent_var(%cc_reset_store)
	$cc_reset_1 := %cc_reset_store[%round_nr_1_store[$REF_GROUP_IDX]]
	$cc_reset_2 := %cc_reset_store[%round_nr_2_store[$REF_GROUP_IDX]]
	declare polyphonic $pressed_id
	if (%condition_2_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_RANDOM)
		$round_nr_2 := %rand_nr_2_store[$REF_GROUP_IDX]+1
	else
		$round_nr_2 := %round_nr_2_store[$REF_GROUP_IDX]+1	
	end if
	if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_RANDOM)
		$round_nr_1 := %rand_nr_1_store[$REF_GROUP_IDX]+1
	else
		$round_nr_1 := %round_nr_1_store[$REF_GROUP_IDX]+1	
	end if
	if ($cond1_status = 1 and $cond2_status = 1)
		$cond_both_status := 1
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_green := 1
			$Cell_red := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	else
		$cond_both_status := 0
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_red := 1
			$Cell_green := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	end if
end on

on note
	ignore_event($EVENT_ID)
	$a := 0
	while($a < num_elements(%GROUPS_AFFECTED))
		disallow_group(%GROUPS_AFFECTED[$a])
		inc($a)
	end while
	$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
	set_event_par($id,0,1000)
	$pressed_id := $EVENT_ID
	$trigger_time := $ENGINE_UPTIME
	if (%kw_keys[$EVENT_NOTE] = 1)
		$active_key := $EVENT_NOTE
		if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_ON_KEY)
			if (in_range($active_key,%low_key_1_store[$REF_GROUP_IDX],%high_key_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		end if
		if (%condition_2_store[$REF_GROUP_IDX] = $START_CRITERIA_ON_KEY)
			if (in_range($active_key,%low_key_2_store[$REF_GROUP_IDX],%high_key_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		end if
	end if
	$a := 0
	while ($a < $CYCLE_NR)
		%c_store[$a] := 0
		%rand_c_store[$a] := 0
		inc($a)
	end while
	$a := 0
	while($a < num_elements(%GROUPS_AFFECTED))
		if (%trigger_menu_store[%GROUPS_AFFECTED[$a]] = 0)
			%key_status_store[%GROUPS_AFFECTED[$a]] := 1 
			if (%GROUPS_AFFECTED[$a] = $REF_GROUP_IDX)
				$key_status := 1
			end if
			
		select (%condition_1_store[%GROUPS_AFFECTED[$a]]) 
			case $START_CRITERIA_NONE
				disallow_group(-1)
				allow_group(%GROUPS_AFFECTED[$a])
				$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
				set_event_par($id,0,%GROUPS_AFFECTED[$a])
			case $START_CRITERIA_ON_KEY
				if (in_range($active_key,%low_key_1_store[%GROUPS_AFFECTED[$a]],%high_key_1_store[%GROUPS_AFFECTED[$a]]))
					select (%condition_2_store[%GROUPS_AFFECTED[$a]]) 
						case $START_CRITERIA_NONE
							disallow_group(-1)
							allow_group(%GROUPS_AFFECTED[$a])
							$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
							set_event_par($id,0,%GROUPS_AFFECTED[$a])
						case $START_CRITERIA_ON_KEY
							if (in_range($active_key,%low_key_2_store[%GROUPS_AFFECTED[$a]],%high_key_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_ON_CONTROLLER
							if (in_range(%CC[%CC_2_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_2_store[%GROUPS_AFFECTED[$a]],%high_ctrl_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])
							end if
						case $START_CRITERIA_CYCLE_ROUND_ROBIN
							%c_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%position_2_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_CYCLE_RANDOM
							%rand_c_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%rand_pos_2_store[%GROUPS_AFFECTED[$a]] = %rand_cursor_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
					end select
				end if
			case $START_CRITERIA_ON_CONTROLLER
				if (in_range(%CC[%CC_1_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_1_store[%GROUPS_AFFECTED[$a]],%high_ctrl_1_store[%GROUPS_AFFECTED[$a]]))
					select (%condition_2_store[%GROUPS_AFFECTED[$a]]) 
						case $START_CRITERIA_NONE
							disallow_group(-1)
							allow_group(%GROUPS_AFFECTED[$a])
							$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
							set_event_par($id,0,%GROUPS_AFFECTED[$a])
						case $START_CRITERIA_ON_KEY
							if (in_range($active_key,%low_key_2_store[%GROUPS_AFFECTED[$a]],%high_key_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_ON_CONTROLLER
							if (in_range(%CC[%CC_2_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_2_store[%GROUPS_AFFECTED[$a]],%high_ctrl_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])
							end if
						case $START_CRITERIA_CYCLE_ROUND_ROBIN
							%c_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%position_2_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_CYCLE_RANDOM
							%rand_c_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%rand_pos_2_store[%GROUPS_AFFECTED[$a]] = %rand_cursor_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
					end select
				end if
			case $START_CRITERIA_CYCLE_ROUND_ROBIN
				%c_store[%round_nr_1_store[%GROUPS_AFFECTED[$a]]] := 1
				if (%position_1_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_1_store[%GROUPS_AFFECTED[$a]]])
					select (%condition_2_store[%GROUPS_AFFECTED[$a]]) 
						case $START_CRITERIA_NONE
							disallow_group(-1)
							allow_group(%GROUPS_AFFECTED[$a])
							$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
							set_event_par($id,0,%GROUPS_AFFECTED[$a])
						case $START_CRITERIA_ON_KEY
							if (in_range($active_key,%low_key_2_store[%GROUPS_AFFECTED[$a]],%high_key_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_ON_CONTROLLER
							if (in_range(%CC[%CC_2_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_2_store[%GROUPS_AFFECTED[$a]],%high_ctrl_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])
							end if
						case $START_CRITERIA_CYCLE_ROUND_ROBIN
							%rand_c_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%position_2_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
					end select
				end if
			case $START_CRITERIA_CYCLE_RANDOM
				%rand_c_store[%rand_nr_1_store[%GROUPS_AFFECTED[$a]]] := 1
				if (%rand_pos_1_store[%GROUPS_AFFECTED[$a]] = %rand_cursor_store[%rand_nr_1_store[%GROUPS_AFFECTED[$a]]])
					select (%condition_2_store[%GROUPS_AFFECTED[$a]]) 
						case $START_CRITERIA_NONE
							disallow_group(-1)
							allow_group(%GROUPS_AFFECTED[$a])
							$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
							set_event_par($id,0,%GROUPS_AFFECTED[$a])
						case $START_CRITERIA_ON_KEY
							if (in_range($active_key,%low_key_2_store[%GROUPS_AFFECTED[$a]],%high_key_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_ON_CONTROLLER
							if (in_range(%CC[%CC_2_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_2_store[%GROUPS_AFFECTED[$a]],%high_ctrl_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])
							end if
						case $START_CRITERIA_CYCLE_ROUND_ROBIN
							%c_store[%round_nr_1_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%position_2_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_CYCLE_RANDOM
							if (%rand_pos_2_store[%GROUPS_AFFECTED[$a]] = %rand_cursor_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
					end select
				end if
		end select
		end if
		inc($a)							
	end while
	$a := 0
	while($a < $CYCLE_NR)
		if (%c_store[$a] = 1)
			inc(%round_cursor_store[$a])
			if (%round_cursor_store[$a] > %round_max_store[$a])
				%round_cursor_store[$a] := 1
			end if	
		end if
		if (%rand_c_store[$a] = 1)
			if (%rand_max_store[$a] > 2)
				$rand_helper := random(1,%rand_max_store[$a])
				$b := 0
				while($b < 100 and %rand_cursor_store[$a] = $rand_helper)
					$rand_helper := random(1,%rand_max_store[$a])
					inc($b)
				end while
				%rand_cursor_store[$a] := $rand_helper
			else
				%rand_cursor_store[$a] := random(1,%rand_max_store[$a])
			end if
		end if
		%c_store[$a] := 0
		%rand_c_store[$a] := 0
		inc($a)
	end while
	{if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
		if (%position_1_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]])
			$cond1_status := 1
		else
			$cond1_status := 0
		end if
	end if
	if (%condition_2_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
		if (%position_2_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_2_store[$REF_GROUP_IDX]])
			$cond2_status := 1
		else
			$cond2_status := 0
		end if
	end if}
	if (%trigger_menu_store[$REF_GROUP_IDX] = 0)
		if ($cond1_status = 1 and $cond2_status = 1)
			$cond_both_status := 1
			if (%key_status_store[$REF_GROUP_IDX] = 1)
				$Cell_green := 1
				$Cell_red := 0
			else
				$Cell_green := 0
				$Cell_red := 0		
			end if
		else
			$cond_both_status := 0
			if (%key_status_store[$REF_GROUP_IDX] = 1)
				$Cell_red := 1
				$Cell_green := 0
			else
				$Cell_green := 0
				$Cell_red := 0		
			end if
		end if
	end if
{	if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_NONE)
		move_control($cond2_status,0,0)
	else
		move_control($cond2_status,2,2)	
	end if}

end on

on release
	if (%EVENT_PAR[0] = 1000)
		exit
	end if
	if ($EVENT_ID # $pressed_id)
		exit
	end if
	$a := 0
	while ($a < $CYCLE_NR)
		%c_store[$a] := 0
		%rand_c_store[$a] := 0
		inc($a)
	end while
	$cur_group := -1000			
	$a := 0
	$duration := ($ENGINE_UPTIME - $trigger_time + 2)*1000
	while($a < num_elements(%GROUPS_AFFECTED))
		if (%trigger_menu_store[%GROUPS_AFFECTED[$a]] = 1)
			if (%GROUPS_AFFECTED[$a] = $REF_GROUP_IDX)
				inc(%key_status_store[$REF_GROUP_IDX])
				$key_status := 1
				$cur_group := %GROUPS_AFFECTED[$a]
			end if
		else
			%key_status_store[%GROUPS_AFFECTED[$a]] := 0 
			if (%GROUPS_AFFECTED[$a] = $REF_GROUP_IDX)
				$key_status := 0
			end if
		end if
		if (%trigger_menu_store[%GROUPS_AFFECTED[$a]] = 1)
		select (%condition_1_store[%GROUPS_AFFECTED[$a]]) 
			case $START_CRITERIA_NONE
				disallow_group(-1)
				allow_group(%GROUPS_AFFECTED[$a])
				$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
				set_event_par($id,0,%GROUPS_AFFECTED[$a])
			case $START_CRITERIA_ON_KEY
				if (in_range($active_key,%low_key_1_store[%GROUPS_AFFECTED[$a]],%high_key_1_store[%GROUPS_AFFECTED[$a]]))
					select (%condition_2_store[%GROUPS_AFFECTED[$a]]) 
						case $START_CRITERIA_NONE
							disallow_group(-1)
							allow_group(%GROUPS_AFFECTED[$a])
							$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
							set_event_par($id,0,%GROUPS_AFFECTED[$a])
						case $START_CRITERIA_ON_KEY
							if (in_range($active_key,%low_key_2_store[%GROUPS_AFFECTED[$a]],%high_key_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_ON_CONTROLLER
							if (in_range(%CC[%CC_2_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_2_store[%GROUPS_AFFECTED[$a]],%high_ctrl_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])
							end if
						case $START_CRITERIA_CYCLE_ROUND_ROBIN
								%c_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%position_2_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_CYCLE_RANDOM
							%rand_c_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%rand_pos_2_store[%GROUPS_AFFECTED[$a]] = %rand_cursor_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
					end select
				end if
			case $START_CRITERIA_ON_CONTROLLER
				if (in_range(%CC[%CC_1_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_1_store[%GROUPS_AFFECTED[$a]],%high_ctrl_1_store[%GROUPS_AFFECTED[$a]]))
					select (%condition_2_store[%GROUPS_AFFECTED[$a]]) 
						case $START_CRITERIA_NONE
							disallow_group(-1)
							allow_group(%GROUPS_AFFECTED[$a])
							$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
							set_event_par($id,0,%GROUPS_AFFECTED[$a])
						case $START_CRITERIA_ON_KEY
							if (in_range($active_key,%low_key_2_store[%GROUPS_AFFECTED[$a]],%high_key_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_ON_CONTROLLER
							if (in_range(%CC[%CC_2_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_2_store[%GROUPS_AFFECTED[$a]],%high_ctrl_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])
							end if
						case $START_CRITERIA_CYCLE_ROUND_ROBIN
								%c_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%position_2_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_CYCLE_RANDOM
							%rand_c_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%rand_pos_2_store[%GROUPS_AFFECTED[$a]] = %rand_cursor_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
					end select
				end if
			case $START_CRITERIA_CYCLE_ROUND_ROBIN
				%c_store[%round_nr_1_store[%GROUPS_AFFECTED[$a]]] := 1
				if (%position_1_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_1_store[%GROUPS_AFFECTED[$a]]])
					select (%condition_2_store[%GROUPS_AFFECTED[$a]]) 
						case $START_CRITERIA_NONE
							disallow_group(-1)
							allow_group(%GROUPS_AFFECTED[$a])
							$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
							set_event_par($id,0,%GROUPS_AFFECTED[$a])
						case $START_CRITERIA_ON_KEY
							if (in_range($active_key,%low_key_2_store[%GROUPS_AFFECTED[$a]],%high_key_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_ON_CONTROLLER
							if (in_range(%CC[%CC_2_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_2_store[%GROUPS_AFFECTED[$a]],%high_ctrl_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])
							end if
						case $START_CRITERIA_CYCLE_ROUND_ROBIN
							if (%position_2_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,$duration)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_CYCLE_RANDOM
							%rand_c_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%rand_pos_2_store[%GROUPS_AFFECTED[$a]] = %rand_cursor_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
					end select
				end if
			case $START_CRITERIA_CYCLE_RANDOM
				%rand_c_store[%rand_nr_1_store[%GROUPS_AFFECTED[$a]]] := 1
				if (%rand_pos_1_store[%GROUPS_AFFECTED[$a]] = %rand_cursor_store[%rand_nr_1_store[%GROUPS_AFFECTED[$a]]])
					select (%condition_2_store[%GROUPS_AFFECTED[$a]]) 
						case $START_CRITERIA_NONE
							disallow_group(-1)
							allow_group(%GROUPS_AFFECTED[$a])
							$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
							set_event_par($id,0,%GROUPS_AFFECTED[$a])
						case $START_CRITERIA_ON_KEY
							if (in_range($active_key,%low_key_2_store[%GROUPS_AFFECTED[$a]],%high_key_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_ON_CONTROLLER
							if (in_range(%CC[%CC_2_store[%GROUPS_AFFECTED[$a]]],%low_ctrl_2_store[%GROUPS_AFFECTED[$a]],%high_ctrl_2_store[%GROUPS_AFFECTED[$a]]))
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])
							end if
						case $START_CRITERIA_CYCLE_ROUND_ROBIN
							%c_store[%round_nr_1_store[%GROUPS_AFFECTED[$a]]] := 1
							if (%position_2_store[%GROUPS_AFFECTED[$a]] = %round_cursor_store[%round_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
						case $START_CRITERIA_CYCLE_RANDOM
							if (%rand_pos_2_store[%GROUPS_AFFECTED[$a]] = %rand_cursor_store[%rand_nr_2_store[%GROUPS_AFFECTED[$a]]])
								disallow_group(-1)
								allow_group(%GROUPS_AFFECTED[$a])
								$id := play_note($EVENT_NOTE,$EVENT_VELOCITY,0,-1)
								set_event_par($id,0,%GROUPS_AFFECTED[$a])				
							end if
					end select
				end if
		end select
		end if
		inc($a)							
	end while
	$a := 0
	while($a < $CYCLE_NR)
		if (%c_store[$a] = 1)
			inc(%round_cursor_store[$a])
			if (%round_cursor_store[$a] > %round_max_store[$a])
				%round_cursor_store[$a] := 1
			end if	
		end if
		if (%rand_c_store[$a] = 1)
			if (%rand_max_store[$a] > 2)
				$rand_helper := random(1,%rand_max_store[$a])
				$b := 0
				while($b < 100 and %rand_cursor_store[$a] = $rand_helper)
					$rand_helper := random(1,%rand_max_store[$a])
					inc($b)
				end while
				%rand_cursor_store[$a] := $rand_helper
			else
				%rand_cursor_store[$a] := random(1,%rand_max_store[$a])
			end if
		end if
		%c_store[$a] := 0
		%rand_c_store[$a] := 0
		inc($a)
	end while
	if (%trigger_menu_store[$REF_GROUP_IDX] = 0)
		if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_1_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]])
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		end if
		if (%condition_2_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_2_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_2_store[$REF_GROUP_IDX]])
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		end if
		if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_RANDOM)
			if (%rand_pos_1_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_1_store[$REF_GROUP_IDX]])
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		end if
		if (%condition_2_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_RANDOM)
			if (%rand_pos_2_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_2_store[$REF_GROUP_IDX]])
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		end if
		if ($cond1_status = 1 and $cond2_status = 1)
			$cond_both_status := 1
			if (%key_status_store[$REF_GROUP_IDX] = 1)
				$Cell_green := 1
				$Cell_red := 0
			else
				$Cell_green := 0
				$Cell_red := 0		
			end if
		else
			$cond_both_status := 0
			if (%key_status_store[$REF_GROUP_IDX] = 1)
				$Cell_red := 1
				$Cell_green := 0
			else
				$Cell_green := 0
				$Cell_red := 0		
			end if
		end if
	else
		if ($cond1_status = 1 and $cond2_status = 1)
			$cond_both_status := 1
			if (%key_status_store[$REF_GROUP_IDX] > 0)
				$Cell_green := 1
				$Cell_red := 0
			else
				$Cell_green := 0
				$Cell_red := 0		
			end if
		else
			$cond_both_status := 0
			if (%key_status_store[$REF_GROUP_IDX] > 0)
				$Cell_red := 1
				$Cell_green := 0
			else
				$Cell_green := 0
				$Cell_red := 0		
			end if
		end if		
	end if
		
	if ($cur_group >= 0)
		if (%trigger_menu_store[$cur_group] = 1)
			wait($duration)
			if ($cur_group = $REF_GROUP_IDX)
				dec(%key_status_store[$cur_group])
				if (%key_status_store[$cur_group] < 0)
					%key_status_store[$cur_group] := 0
				end if
				if (%key_status_store[$cur_group] = 0)
					$key_status := 0
				end if
				if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
					if (%position_1_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]])
						$cond1_status := 1
					else
						$cond1_status := 0
					end if
				end if
				if (%condition_2_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
					if (%position_2_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_2_store[$REF_GROUP_IDX]])
						$cond2_status := 1
					else
						$cond2_status := 0
					end if
				end if
				if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_RANDOM)
					if (%rand_pos_1_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_1_store[$REF_GROUP_IDX]])
						$cond1_status := 1
					else
						$cond1_status := 0
					end if
				end if
				if (%condition_2_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_RANDOM)
					if (%rand_pos_2_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_2_store[$REF_GROUP_IDX]])
						$cond2_status := 1
					else
						$cond2_status := 0
					end if
				end if
				if ($cond1_status = 1 and $cond2_status = 1)
					$cond_both_status := 1
					if (%key_status_store[$REF_GROUP_IDX] > 0)
						$Cell_green := 1
						$Cell_red := 0
					else
						$Cell_green := 0
						$Cell_red := 0		
					end if
				else
					$cond_both_status := 0
					if (%key_status_store[$REF_GROUP_IDX] > 0)
						$Cell_red := 1
						$Cell_green := 0
					else
						$Cell_green := 0
						$Cell_red := 0		
					end if
				end if
			else
				%key_status_store[$cur_group] := 0				
			end if
		end if
	end if

end on

on controller
	$a := 0
	while($a < $CYCLE_NR)
		if ($CC_NUM = %cc_reset_store[$a])
			%round_cursor_store[$a] := 1
		end if
		inc($a)
	end while
	$cond1_status := 1
	$cond2_status := 1
	select (%condition_1_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_1_store[$REF_GROUP_IDX],%high_key_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(%CC[%CC_1_store[$REF_GROUP_IDX]],%low_ctrl_1_store[$REF_GROUP_IDX],%high_ctrl_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_1_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_1_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_1_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
	end select
	select (%condition_2_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_2_store[$REF_GROUP_IDX],%high_key_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(%CC[%CC_2_store[$REF_GROUP_IDX]],%low_ctrl_2_store[$REF_GROUP_IDX],%high_ctrl_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_2_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_2_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_2_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_2_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
	end select
	if ($cond1_status = 1 and $cond2_status = 1)
		$cond_both_status := 1
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_green := 1
			$Cell_red := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	else
		$cond_both_status := 0
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_red := 1
			$Cell_green := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	end if
end on
	
on ui_update
	$a := 0
	while($a < $CYCLE_NR)
		%rand_old_max_store[$a] := %rand_max_store[$a]
		%rand_max_store[$a] := 0
		inc($a)
	end while
	$a := 0
	while($a < $NUM_GROUPS)
		if (%GROUPS_SELECTED[$a] = 1)
			%condition_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_MODE,$a,0,-1)
			select (%condition_1_store[$a])
				case $START_CRITERIA_ON_KEY
					%low_key_1_store[$a] :=_get_engine_par ($ENGINE_PAR_START_CRITERIA_KEY_MIN,$a,0,-1)
					%high_key_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_KEY_MAX,$a,0,-1)
					if (%high_key_1_store[$a] < %low_key_1_store[$a])
						%high_key_1_store[$a] := %low_key_1_store[$a]
					end if
				case $START_CRITERIA_ON_CONTROLLER
					%CC_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CONTROLLER,$a,0,-1)
					%low_ctrl_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CC_MIN,$a,0,-1)
					%high_ctrl_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CC_MAX,$a,0,-1)
				case $START_CRITERIA_CYCLE_ROUND_ROBIN
					%position_1_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CYCLE_CLASS,$a,0,-1)+1
			end select
			%condition_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_MODE,$a,1,-1)
			select (%condition_2_store[$a])
				case $START_CRITERIA_ON_KEY
					%low_key_2_store[$a] :=_get_engine_par ($ENGINE_PAR_START_CRITERIA_KEY_MIN,$a,1,-1)
					%high_key_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_KEY_MAX,$a,1,-1)
					if (%high_key_2_store[$a] < %low_key_2_store[$a])
						%high_key_2_store[$a] := %low_key_2_store[$a]
					end if
				case $START_CRITERIA_ON_CONTROLLER
					%CC_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CONTROLLER,$a,1,-1)
					%low_ctrl_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CC_MIN,$a,1,-1)
					%high_ctrl_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CC_MAX,$a,1,-1)
				case $START_CRITERIA_CYCLE_ROUND_ROBIN
					%position_2_store[$a] := _get_engine_par ($ENGINE_PAR_START_CRITERIA_CYCLE_CLASS,$a,1,-1)+1
			end select
			%trigger_menu_store[$a] := _get_engine_par ($ENGINE_PAR_RELEASE_TRIGGER,$a,0,-1)
		end if
		if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
			inc(%rand_max_store[%rand_nr_1_store[$a]])
			%rand_pos_1_store[$a] := %rand_max_store[%rand_nr_1_store[$a]]
		end if
		if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
			inc(%rand_max_store[%rand_nr_1_store[$a]])
			%rand_pos_2_store[$a] := %rand_max_store[%rand_nr_2_store[$a]]
		end if
		inc($a)
	end while
	$condition1_menu := %condition_1_store[$REF_GROUP_IDX]
	select (%condition_1_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			$low_key_1 := %low_key_1_store[$REF_GROUP_IDX]
			$high_key_1 := %high_key_1_store[$REF_GROUP_IDX]
		case $START_CRITERIA_ON_CONTROLLER
			$ctrl_1 := %CC_1_store[$REF_GROUP_IDX]
			$low_ctrl_1 := %low_ctrl_1_store[$REF_GROUP_IDX]
			$high_ctrl_1 := %high_ctrl_1_store[$REF_GROUP_IDX]
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			$position_1 := %position_1_store[$REF_GROUP_IDX]
			$round_nr_1 := %round_nr_1_store[$REF_GROUP_IDX]+1
		case $START_CRITERIA_CYCLE_RANDOM
			$round_nr_1 := %rand_nr_1_store[$REF_GROUP_IDX]+1
	end select
	$condition2_menu := %condition_2_store[$REF_GROUP_IDX]
	select (%condition_2_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			$low_key_2 := %low_key_2_store[$REF_GROUP_IDX]
			$high_key_2 := %high_key_2_store[$REF_GROUP_IDX]
		case $START_CRITERIA_ON_CONTROLLER
			$ctrl_2 := %CC_2_store[$REF_GROUP_IDX]
			$low_ctrl_2 := %low_ctrl_2_store[$REF_GROUP_IDX]
			$high_ctrl_2 := %high_ctrl_2_store[$REF_GROUP_IDX]
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			$position_2 := %position_2_store[$REF_GROUP_IDX]
			$round_nr_2 := %round_nr_2_store[$REF_GROUP_IDX]+1
		case $START_CRITERIA_CYCLE_RANDOM
			$round_nr_2 := %rand_nr_2_store[$REF_GROUP_IDX]+1
	end select
	$trigger_menu := %trigger_menu_store[$REF_GROUP_IDX]
 	$Cell_Nr := $REF_GROUP_IDX
	$a := 0
	while ($a < 128)
		%kw_keys[$a] := 0 
		%key_status_store[$a] := 0
		inc($a)
	end while
	$default_active_key := $active_key
	$a := 0
	while($a < $CYCLE_NR)
		%round_max_old_store[$a] := %round_max_store[$a]
		%round_max_store[$a] := 0
		inc($a)
	end while
	$a := 0
	while ($a < 128)
		if (%condition_2_store[$a] = $START_CRITERIA_ON_KEY)
			$b := %low_key_2_store[$a]
			%kw_keys[$b] := 1
			$default_active_key := $b
			inc($b)
			while($b <= %high_key_2_store[$a])
				%kw_keys[$b] := 1
				inc($b)
			end while
		end if
		if (%condition_1_store[$a] = $START_CRITERIA_ON_KEY)
			$b := %low_key_1_store[$a]
			%kw_keys[$b] := 1
			$default_active_key := $b
			inc($b)
			while($b <= %high_key_1_store[$a])
				%kw_keys[$b] := 1
				inc($b)
			end while
		end if
		if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_1_store[$a] > %round_max_store[%round_nr_1_store[$a]])
				%round_max_store[%round_nr_1_store[$a]] := %position_1_store[$a]
			end if
		end if
		if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_2_store[$a] > %round_max_store[%round_nr_2_store[$a]])
				%round_max_store[%round_nr_2_store[$a]] := %position_2_store[$a]
			end if
		end if		
		inc($a)
	end while
	$a := 0
	while($a < $CYCLE_NR)
		if ((%round_max_old_store[$a] = 0 and %round_max_store[$a] > 0) or %round_cursor_store[$a] > %round_max_store[$a] )
			%round_cursor_store[$a] := 1
		end if
		if ((%rand_max_old_store[$a] = 0 and %rand_max_store[$a] > 0) or %rand_cursor_store[$a] > %rand_max_store[$a] )
			%rand_cursor_store[$a] := random(1,%rand_max_store[$a])
		end if
		inc($a)
	end while
	if (%kw_keys[$active_key] = 0)
		$active_key := 24
		$a := 0
		while($a < 128)
			if (%kw_keys[$a] = 1)
				$active_key := $a
				$a := 128
			end if
			inc($a)
		end while
	end if
	$cond1_status := 1
	$cond2_status := 1
	select (%condition_1_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_1_store[$REF_GROUP_IDX],%high_key_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(%CC[%CC_1_store[$REF_GROUP_IDX]],%low_ctrl_1_store[$REF_GROUP_IDX],%high_ctrl_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_1_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_1_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_1_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
	end select
	select (%condition_2_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_2_store[$REF_GROUP_IDX],%high_key_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(%CC[%CC_2_store[$REF_GROUP_IDX]],%low_ctrl_2_store[$REF_GROUP_IDX],%high_ctrl_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_2_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_2_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_2_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_2_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
	end select
	$key_status := %key_status_store[$REF_GROUP_IDX]
	$CC_reset_1 := %cc_reset_store[%round_nr_1_store[$REF_GROUP_IDX]]		
	$CC_reset_2 := %cc_reset_store[%round_nr_2_store[$REF_GROUP_IDX]]		
	if ($cond1_status = 1 and $cond2_status = 1)
		$cond_both_status := 1
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_green := 1
			$Cell_red := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	else
		$cond_both_status := 0
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_red := 1
			$Cell_green := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	end if
	if (%condition_2_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
		move_control ($round_nr_2,6,5)
		move_control ($cc_reset_2,5,5)	
	else
		if (%condition_2_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_RANDOM)
			move_control ($round_nr_2,6,5)
			move_control ($cc_reset_2,0,0)	
		else
			move_control ($round_nr_2,0,0)
			move_control ($cc_reset_2,0,0)	
		end if
	end if
	if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
		move_control ($round_nr_1,6,3)
		move_control ($cc_reset_1,5,3)	
	else
		if (%condition_1_store[$REF_GROUP_IDX] = $START_CRITERIA_CYCLE_RANDOM)
			move_control ($round_nr_1,6,3)
			move_control ($cc_reset_1,0,0)	
		else
			move_control ($round_nr_1,0,0)
			move_control ($cc_reset_1,0,0)	
		end if
	end if
end on

on ui_control ($trigger_menu)
	$a := 0
	while($a < 128)
		if (%GROUPS_SELECTED[$a] = 1)
			%trigger_menu_store[$REF_GROUP_IDX] := $trigger_menu
		end if
		inc($a)
	end while		
end on

on ui_control ($round_nr_1)
	$a := 0
	while($a < 128)
		if (%GROUPS_SELECTED[$a] = 1)
			if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
				%round_nr_1_store[$a] := $round_nr_1 -1
			end if
			if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
				%rand_nr_1_store[$a] := $round_nr_1 -1	
			end if			
		end if
		inc($a)
	end while
	$a := 0
	while($a < $CYCLE_NR)
		%round_max_old_store[$a] := %round_max_store[$a]
		%round_max_store[$a] := 0
		%rand_max_store[$a] := 0
		inc($a)
	end while
	$a := 0
	while ($a < 128)
		if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_1_store[$a] > %round_max_store[%round_nr_1_store[$a]])
				%round_max_store[%round_nr_1_store[$a]] := %position_1_store[$a]
			end if
		end if
		if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_2_store[$a] > %round_max_store[%round_nr_2_store[$a]])
				%round_max_store[%round_nr_2_store[$a]] := %position_2_store[$a]
			end if
		end if		
		if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
			inc(%rand_max_store[%rand_nr_1_store[$a]])
			%rand_pos_1_store[$a] := %rand_max_store[%rand_nr_1_store[$a]]
		end if
		if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
			inc(%rand_max_store[%rand_nr_2_store[$a]])
			%rand_pos_2_store[$a] := %rand_max_store[%rand_nr_2_store[$a]]
		end if
		inc($a)
	end while
	$a := 0
	while($a < $CYCLE_NR)
		if ((%round_max_old_store[$a] = 0 and %round_max_store[$a] > 0) or %round_cursor_store[$a] > %round_max_store[$a] )
			%round_cursor_store[$a] := 1
		end if
		if (%rand_max_store[$a] > 0 and %rand_cursor_store[$a] > %rand_max_store[$a] )
			if (%rand_max_store[$a] > 2)
				$rand_helper := random(1,%rand_max_store[$a])
				$b := 0
				while($b < 100 and %rand_cursor_store[$a] = $rand_helper)
					$rand_helper := random(1,%rand_max_store[$a])
					inc($b)
				end while
				%rand_cursor_store[$a] := $rand_helper
			else
				%rand_cursor_store[$a] := random(1,%rand_max_store[$a])
			end if
		end if
		inc($a)
	end while
	$cond1_status := 1
	$cond2_status := 1
	select (%condition_1_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_1_store[$REF_GROUP_IDX],%high_key_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(%CC[%CC_1_store[$REF_GROUP_IDX]],%low_ctrl_1_store[$REF_GROUP_IDX],%high_ctrl_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_1_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_1_store[$REF_GROUP_IDX] = %rand_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
	end select
	select (%condition_2_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_2_store[$REF_GROUP_IDX],%high_key_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(%CC[%CC_2_store[$REF_GROUP_IDX]],%low_ctrl_2_store[$REF_GROUP_IDX],%high_ctrl_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_2_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_2_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_2_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_2_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
	end select
	$CC_reset_1 := %cc_reset_store[%round_nr_1_store[$REF_GROUP_IDX]]		
	$CC_reset_2 := %cc_reset_store[%round_nr_2_store[$REF_GROUP_IDX]]	
	if ($cond1_status = 1 and $cond2_status = 1)
		$cond_both_status := 1
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_green := 1
			$Cell_red := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	else
		$cond_both_status := 0
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_red := 1
			$Cell_green := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	end if
end on

on ui_control ($round_nr_2)
	$a := 0
	while($a < 128)
		if (%GROUPS_SELECTED[$a] = 1)
			if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
				%round_nr_2_store[$a] := $round_nr_2 -1
			end if
			if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
				%rand_nr_2_store[$a] := $round_nr_2 -1	
			end if			
		end if
		inc($a)
	end while
	$a := 0
	while($a < $CYCLE_NR)
		%round_max_old_store[$a] := %round_max_store[$a]
		%round_max_store[$a] := 0
		%rand_max_store[$a] := 0
		inc($a)
	end while
	$a := 0
	while ($a < 128)
		if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_1_store[$a] > %round_max_store[%round_nr_1_store[$a]])
				%round_max_store[%round_nr_1_store[$a]] := %position_1_store[$a]
			end if
		end if
		if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_ROUND_ROBIN)
			if (%position_2_store[$a] > %round_max_store[%round_nr_2_store[$a]])
				%round_max_store[%round_nr_2_store[$a]] := %position_2_store[$a]
			end if
		end if		
		if (%condition_1_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
			inc(%rand_max_store[%rand_nr_1_store[$a]])
			%rand_pos_1_store[$a] := %rand_max_store[%rand_nr_1_store[$a]]
		end if
		if (%condition_2_store[$a] = $START_CRITERIA_CYCLE_RANDOM)
			inc(%rand_max_store[%rand_nr_2_store[$a]])
			%rand_pos_2_store[$a] := %rand_max_store[%rand_nr_2_store[$a]]
		end if
		inc($a)
	end while
	$a := 0
	while($a < $CYCLE_NR)
		if ((%round_max_old_store[$a] = 0 and %round_max_store[$a] > 0) or %round_cursor_store[$a] > %round_max_store[$a] )
			%round_cursor_store[$a] := 1
		end if
		if (%rand_max_store[$a] > 0 and %rand_cursor_store[$a] > %rand_max_store[$a] )
			if (%rand_max_store[$a] > 2)
				$rand_helper := random(1,%rand_max_store[$a])
				$b := 0
				while($b < 100 and %rand_cursor_store[$a] = $rand_helper)
					$rand_helper := random(1,%rand_max_store[$a])
					inc($b)
				end while
				%rand_cursor_store[$a] := $rand_helper
			else
				%rand_cursor_store[$a] := random(1,%rand_max_store[$a])
			end if
		end if
		inc($a)
	end while
	$cond1_status := 1
	$cond2_status := 1
	select (%condition_1_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_1_store[$REF_GROUP_IDX],%high_key_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(%CC[%CC_1_store[$REF_GROUP_IDX]],%low_ctrl_1_store[$REF_GROUP_IDX],%high_ctrl_1_store[$REF_GROUP_IDX]))
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_1_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_1_store[$REF_GROUP_IDX] = %rand_cursor_store[%round_nr_1_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond1_status := 1
			else
				$cond1_status := 0
			end if
	end select
	select (%condition_2_store[$REF_GROUP_IDX])
		case $START_CRITERIA_ON_KEY
			if (in_range($active_key,%low_key_2_store[$REF_GROUP_IDX],%high_key_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_ON_CONTROLLER
			if (in_range(%CC[%CC_2_store[$REF_GROUP_IDX]],%low_ctrl_2_store[$REF_GROUP_IDX],%high_ctrl_2_store[$REF_GROUP_IDX]))
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_ROUND_ROBIN
			if (%position_2_store[$REF_GROUP_IDX] = %round_cursor_store[%round_nr_2_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
		case $START_CRITERIA_CYCLE_RANDOM
			if (%rand_pos_2_store[$REF_GROUP_IDX] = %rand_cursor_store[%rand_nr_2_store[$REF_GROUP_IDX]] and %key_status_store[$REF_GROUP_IDX] = 0)
				$cond2_status := 1
			else
				$cond2_status := 0
			end if
	end select
	$CC_reset_1 := %cc_reset_store[%round_nr_1_store[$REF_GROUP_IDX]]		
	$CC_reset_2 := %cc_reset_store[%round_nr_2_store[$REF_GROUP_IDX]]	
	if ($cond1_status = 1 and $cond2_status = 1)
		$cond_both_status := 1
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_green := 1
			$Cell_red := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	else
		$cond_both_status := 0
		if (%key_status_store[$REF_GROUP_IDX] = 1)
			$Cell_red := 1
			$Cell_green := 0
		else
			$Cell_green := 0
			$Cell_red := 0		
		end if
	end if
end on

on ui_control ($CC_reset_1)
	$a := 0
	while($a < 128)
		if (%GROUPS_SELECTED[$a] = 1)
			%cc_reset_store[%round_nr_1_store[$a]] := $CC_reset_1
		end if
		inc($a)
	end while
	if (%round_nr_1_store[$REF_GROUP_IDX] = %round_nr_2_store[$REF_GROUP_IDX])
		$CC_reset_2 := %cc_reset_store[%round_nr_1_store[$REF_GROUP_IDX]]		
	end if
end on

on ui_control ($CC_reset_2)
	$a := 0
	while($a < 128)
		if (%GROUPS_SELECTED[$a] = 1)
			%cc_reset_store[%round_nr_2_store[$a]] := $CC_reset_2
		end if
		inc($a)
	end while
	if (%round_nr_1_store[$REF_GROUP_IDX] = %round_nr_2_store[$REF_GROUP_IDX])
		$CC_reset_1 := %cc_reset_store[%round_nr_2_store[$REF_GROUP_IDX]]		
	end if
end on

on ui_control ($Cond1_status)
	$Cond1_status := ($Cond1_status + 1) mod 2
end on
on ui_control ($Cond2_status)
	$Cond2_status := ($Cond2_status + 1) mod 2
end on
on ui_control ($Cond_both_status)
	$Cond_both_status := ($Cond_both_status + 1) mod 2
end on
on ui_control ($Key_status)
	$Key_status := ($Key_status + 1) mod 2
end on
on ui_control ($Cell_green)
	$Cell_green := ($Cell_green + 1) mod 2
end on
on ui_control ($Cell_red)
	$Cell_red := ($Cell_red + 1) mod 2
end on
