
function callback_at_hog_complete_dialog_show()
    sound_play('SFX_AtWin01')
end

function callback_at_credits_photo_show()
	sound_play_with_delay(random_id("SFX_PaperScheme01 SFX_PaperScheme02 SFX_PaperScheme03 SFX_PaperScheme04 SFX_PaperScheme05 SFX_PaperScheme06 SFX_PaperScheme07"),0.2,0.3)
end

function callback_at_credits_photo_hide()
	sound_play_with_volume(random_id("SFX_PaperScheme01 SFX_PaperScheme02 SFX_PaperScheme03 SFX_PaperScheme04 SFX_PaperScheme05 SFX_PaperScheme06 SFX_PaperScheme07"),0.2)
end

function callback_at_credits_show()
	ambient_fade_to(0.5,1.5)
	bg_music_change('ost_action0',1.5)
	bg_music_volume(1.0)	

	send_message('CREDITS/@light','show_active')
end

function callback_at_credits_hide()
	bg_music_change_playlist('ost_menu',1.5)
	bg_music_volume(0.7)
	ambient_fade_to(1.0,1.5)
	ambient_change('amb_mountain_wind',1.5)
end

function callback_at_more_games_show()
	ambient_fade_to(0.5,1.5)
	bg_music_change('ost_action0',1.5)
	bg_music_volume(1.0)

	--ANIMATIONS
	set_anim_fps('MORE_GAMES', 'lights', 12)
	start_anim('MORE_GAMES', 'lights', 'loop', -1)
	set_anim_fps('MORE_GAMES', 'branch_R', 12)
	start_anim('MORE_GAMES', 'branch_R', 'loop', -1)
	set_anim_fps('MORE_GAMES', 'trees_R', 7)
	start_anim('MORE_GAMES', 'trees_R', 'loop', -1)
	start_anim('MORE_GAMES', 'characters', 'loop', -1)
end

function callback_at_more_games_hide()
	bg_music_change_playlist('ost_menu',1.5)
	bg_music_volume(0.7)
	ambient_fade_to(1.0,1.5)
	ambient_change('amb_mountain_wind',1.5)
end

function more_games_screen_hover(id)
	send_message('MORE_GAMES/@screen0'..id,'zoom_in')
	more_games_bring_to_front(id)
	sound_play('SFX_HudObjectHiLight')
end

function more_games_screen_leave(id)
	send_message('MORE_GAMES/@screen0'..id,'zoom_out')
end

function show_msg_invalid_use()

    local msg=math.random(3);
    if msg==1 then
        show_msg('msg_invalid_use_01');
    elseif msg==2 then
        show_msg('msg_invalid_use_02');
    elseif msg==3 then
        show_msg('msg_invalid_use_03');
    end

end

function put_active_tool()
	put_active_tool_in_place()
	sound_play('SFX_ActiveItemFail')
end

function lock_zoom(id,time)
	disable_zones(''..time)
	lock_hide_zoom()

	screen_id=(id..'/@BUTTON_ZOOM_CLOSE')
	set_visible(screen_id, false)

	add_delayed_action(time,[[
		unlock_hide_zoom()
		set_visible(screen_id, true)
	]])
end

function loc01_mini04_opened()
	return get_value('loc01_mini04_hatch_state')=='1';
end

function loc01_hog01_available()
	if is_tutorial_enabled() and get_difficulty()=='casual' then
		return is_marked('loc01_mini05') and not is_marked('loc01_hog01_01') and not is_marked('loc01_hog01_enter');
	end
	return false;
end

function loc01_is_exit_tutorial()
	if is_tutorial_enabled() then
		return is_marked('loc01_mini01') and not is_marked('loc03_enter');
	end
	return false;
end

function verify_difficulty_and_show_tutorial_msgbox()
	if get_difficulty()=='expert' then
		set_tutorial_enabled(false);
	else
		show_tutorial_msgbox();
	end
end

function loc01_tutorial_start()
	if is_marked('loc01_enter') then
		if not is_marked('loc01_tutorial_msg') then
			if not is_marked('loc01_mini02_mask_left') then
				verify_difficulty_and_show_tutorial_msgbox()
			end
		else
			loc01_tutorial_show_first();
		end
	else
		set_tutorial_enabled(false);
	end
end

function loc01_tutorial_show_first()
	if loc01_tutorial_mini02() then
		show_tutorial('tut_first');
	elseif loc01_hog01_available() then
		show_tutorial('tut_hog_sparkles');
	elseif not loc01_tutorial_hint_finished() then
		show_tutorial('tut_hint');
		if is_tutorial_enabled() then
			recharge_hint();
		end
	end
end

function loc01_tutorial_hint_use()
	if not is_marked('loc01_tutorial_hint') then
		mark_point('loc01_tutorial_hint')
		hide_tutorial_id('tut_hint');
	end
end

function loc01_hog01_tutorial_block()
	if is_tutorial_enabled() then
		return not is_hog_object_found('loc01_hog01',102) and not is_marked('loc01_hog01_01');
	end
	return false;
end

function loc01_hog01_update_tutorial()
	if loc01_hog01_tutorial_block() then
		show_tutorial('tut_white_paint');
	else
		hide_tutorial_id('tut_white_paint');
		if is_marked('loc01_hog01_lock01') and is_marked('loc01_hog01_lock02') then
			hide_tutorial_id('tut_hog_box');
		else
			show_tutorial('tut_hog_box');
		end
	end
end

function loc01_tutorial_hint_finished()
	return true or is_marked('loc01_tutorial_hint') or is_marked('loc01_mini04_star8');
end

function loc01_mini02_tut_star()
	local v = get_value('loc01_mini02_tut_star');
	local s6 = is_marked('loc01_mini02_star6') and not is_marked('loc01_mini05_star6');
	local s8 = is_marked('loc01_mini04_star8') and not is_marked('loc01_mini05_star8');
	if v == '' then
		if is_marked('loc01_mini05_star6') or is_marked('loc01_mini05_star8') then
			v = 'skip';
		elseif s6 then
			v = 'star6';
		elseif s8 then
			v = 'star8';
		end
		if v ~= '' then
			set_value('loc01_mini02_tut_star',v);
		end
	end
	if v == 'star6' and s6 then
		show_tutorial('tut_star6_missing');
	elseif v == 'star8' and s8 then
		show_tutorial('tut_star8_missing');
	end
end

function loc01_need_tut_combine()
	return is_marked('loc01_mini02_mask_left') and is_marked('loc01_hog01_01') and not is_marked('loc01_mask');
end

function loc01_mini02_active()
	if loc01_need_tut_combine() then
		return false
	end
	if is_tutorial_enabled() then
		return (loc01_tutorial_mini02() or loc01_tutorial_hint_finished()) and not loc01_hog01_available() and not loc01_is_exit_tutorial();
	end
	return true;
end

function loc01_tutorial_mini02()
	if is_tutorial_enabled() then
		return not is_marked('loc01_mini02_mask_left'); --or not is_marked('loc01_mini02_star6');
	end
	return false;
end

function loc01_tutorial_block()
	if loc01_need_tut_combine() then
		return true
	end
	if is_tutorial_enabled() then
		return loc01_tutorial_mini02() or loc01_hog01_available() or not loc01_tutorial_hint_finished() or loc01_is_exit_tutorial();
	end
	return false;
end

function loc01_mini02_mask_block()
	if is_tutorial_enabled() then
		return not is_marked('loc01_mini02_mask_left');
	end
	return false;
end

function loc01_check_tut_combine()
	if loc01_need_tut_combine() then
		show_tutorial('tut_mask_cursor');
	end
end

function loc01_check_tut_exit()
	if loc01_is_exit_tutorial() then
		show_tutorial('tut_loc01_exit');
	end
end

function loc05_hog01_tutorial_block()
	if is_tutorial_enabled() then
		return not is_hog_object_found('loc05_hog01',017) and not is_marked('loc05_hog01_01');
	end
	return false;
end

function loc05_hog01_update_tutorial()
	if loc05_hog01_tutorial_block() then
		show_tutorial('tut_loc05_hog_shape');
	else
		hide_tutorial_id('tut_loc05_hog_shape');
	end
end

function loc04_can_exit()
	return  not is_marked('loc04_mini02') or is_marked('loc04_pentagon_trigger');
end

function finder_tool_found_effect()
	send_message('HUD/@finder_tool_found_effect','finder_use');
end

function at_fail_active_item()
	sound_play_with_volume('SFX_ActiveItemFail',1.0);
end

function at_item_combine()
	hide_tutorial_id('tut_mask_cursor');
	sound_play_with_volume('SFX_CombineItem',1.0);
end

function at_hint_tool_cursor()
	hide_tutorial_id('tut_lens');
	if not is_marked('loc04_pentagon_trigger') and get_active_screen()=='loc04' then
		show_tutorial('tut_lens_search');
	end
end

function at_hint_tool_cancel()
	hide_tutorial_id('tut_lens_search');
	if not is_marked('loc04_pentagon_trigger') and get_active_screen()=='loc04' then
		show_tutorial('tut_lens');
	end
end

function at_hint_tool_zone()
	hide_tutorial_id('tut_lens_search');
	finder_tool_found_effect();
	sound_play_with_volume('SFX_LensDetect01',1.0);
end

function at_hint_tool_zonewrong()
	sound_play_with_volume('SFX_LensDetectMiss',1.0);
end

function loc04_mini03_correct_state()
	return get_logic_param(1)==5 and get_logic_param(2)==3
end

function loc06_lever_state()
	local s;
	local v = get_value('loc06_game01_lever_state');
	if v=='3' then
		s = 3;
	elseif v=='2' then
		s = 2;
	else
		s = 1;
	end
	set_visible('SCREEN/@red',s==3);
	set_visible('SCREEN/@white',s==2);
	set_visible('SCREEN/@green',s==1);
end

function loc13_cannonball_on_hatch()
	return is_marked('loc13_ball_on_hatch');
end

function loc18_mini03_current_mask()
	return get_value('loc18_mini03_mask');
end

function loc18_mini03_init_circle()
	if get_value('loc18_mini03_circle_pos')=='1' then
		send_message('loc18_mini03/@mask_circle','show_mask_instant');
	else
		send_message('loc18_mini03/@mask_circle','hide_mask_instant');
	end
end

function loc18_mini03_rotate_circle()
	sound_play_with_volume('SFX_RotateMaskCircle',1.0);
	if get_value('loc18_mini03_circle_pos')=='1' then
		send_message('loc18_mini03/@mask_circle','hide_mask');
		set_value('loc18_mini03_circle_pos','0');
	else
		send_message('loc18_mini03/@mask_circle','show_mask');
		set_value('loc18_mini03_circle_pos','1');		
	end
end

function loc06_eye_state()
	local s;
	if is_marked('loc06_eye') then
		s = 0;
	elseif is_marked('loc06_game02') then
		s = 2;
	else
		s = 1;
	end
	set_visible('SCREEN/@eye_red',s==1);
	set_visible('SCREEN/@eye_green',s==2);
end

function loc21_mini02_clear_progress()
	set_value('loc21_mini02_top_state','')
	set_value('loc21_mini02_bottom_state','')
end

function loc01_mini03_set_to_end(anim_name)
	ae_anim_set_to_end('SCREEN',anim_name);
end

function loc01_mini03_set_state(state,instant)
	if state > 0 then
		local anim = "loc01_mini03_arrow_"..state;
		local animgears = anim.."_gears";
		if instant then
			loc01_mini03_set_to_end(anim);
			loc01_mini03_set_to_end(animgears);
			ae_anim_set_frame('SCREEN','loc01_mini03_key',0);
		else
			start_anim('SCREEN', 'loc01_mini03_key', 'forward', 1)
			start_anim('SCREEN', anim, 'forward', 1);
			start_anim('SCREEN', animgears, 'forward', 1);
		end
		set_value('loc01_mini03_clock_state',tostring(state));
	else
		if instant then
			start_anim('SCREEN', 'loc01_mini03_arrow_zero_pose', 'forward', 1)
		end
	end
end

function loc01_mini03_get_state()
	local v = get_value('loc01_mini03_clock_state');
	if v~='' then
		return tonumber(v);
	end
	return 0;
end

function loc01_mini03_turn_key()
	local snake_ok = is_marked('loc01_mini03_snake');
	local max_state = 5;
	local state = loc01_mini03_get_state();
	state = state + 1;
	if state == max_state then
		if snake_ok then
			mark_point('loc01_mini03')
			disable_zones('2');
			sound_play_with_volume('SFX_WatchCacheOpen',1.0);
			loc01_mini03_set_state(state,false);
			set_visible('loc01_mini03/@locker', true);
			set_visible('loc01_mini03/@lens', true);
			set_visible('SCREEN/@clock_locker',true);
			start_anim('SCREEN', 'loc01_mini03_locker', 'forward', 1);
			add_delayed_action(2, [[
				show_msg('loc01_mini03_solved')
				update_hilight()
			]]);
		else
			show_msg('loc01_mini03_fail')	
			disable_zones('2.4')
			sound_play_with_volume('SFX_ClockWindUpReverse',1.0);
			start_anim('SCREEN', 'loc01_mini03_key', 'forward', 1);
			add_delayed_action(0.3, [[
				start_anim('SCREEN', 'loc01_mini03_arrow_revers', 'forward', 1)
				start_anim('SCREEN', 'loc01_mini03_arrow_reverse_gears', 'forward', 1)
			]]);
			set_value('loc01_mini03_clock_state','');
		end
	else
		disable_zones('1.1');
		sound_play_with_volume('SFX_ClockWindUp',1.0);
		loc01_mini03_set_state(state,false);
	end
end

function loc01_mini03_restore_state()
	loc01_mini03_set_state(loc01_mini03_get_state(),true)
end

function loc20_game01_enabled()
	for i=1,5 do
		if not is_marked('loc20_game01_blue0'..i) then
			return false
		end
	end
	for i=1,6 do
		if not is_marked('loc20_game01_black0'..i) then
			return false
		end
	end
	for i=1,4 do
		if not is_marked('loc20_game01_red0'..i) then
			return false
		end
	end
	return true
end

loc25_valid_orb_points = {'loc25_orb_red','loc25_orb_green','loc25_orb_blue'}
loc25_find_orb_points = {'loc12_mini02_ball','loc14_mini02','loc15_game01_ball03'}

function loc25_update_hint_state()
	local valid_count = 0
	local total_count = 0
	for i=1,3 do
		if is_marked(loc25_valid_orb_points[i]) then
			valid_count = valid_count + 1
		end
		if is_marked(loc25_find_orb_points[i]) then
			total_count = total_count + 1
		end
	end
	local hint_needed = valid_count < total_count
	for i=1,3 do
		if hint_needed then
			enable_point_hint(loc25_valid_orb_points[i])
		else
			disable_point_hint(loc25_valid_orb_points[i])
		end
	end
end

function loc25_hint_get_orb(color)
	if get_logic_param(0)~=3 then
		hint_zone('zone_prism_lightening')
		show_msg('generic_hint')
		return
	end
	if loc25_can_take_orb() then
		local orb_color = loc25_placed_orb_color()
		if orb_color == 'red' or orb_color == 'green' or orb_color == 'blue' then
			-- Orb of this color wasn't created yet
			if not is_marked('loc25_orb_'..orb_color) then
				hint_zone_with_scale('zone_ball',1)
				show_msg('generic_hint_take')
				return
			end
		end
		-- Ask player to make specific orb
		hint_zone('zone_puzzle')
		show_msg('loc25_hint_'..color..'_orb')
		return
	else
		hint_zone('zone_support')
		show_msg('generic_hint')
		return
	end
end

function loc25_place_orb()
	if activeTool()=='' then
		return false
	else
		sound_play_with_volume('SFX_GlassToMetal',0.8)
	end
	if not loc25_place_item(activeTool()) then
		fail_active_tool()
		return false
	end
	loc25_update_hint_state()
	return true
end

function loc27_give_orb(character,color,item)
	if #item < #'loc25_orb01_red' then
		return false
	end
	local pref = item:sub(1,#'loc25_orb')
	if pref ~= 'loc25_orb' then
		return false
	end
	local num_pos = #pref+1
	local orb_num = item:sub(num_pos,num_pos+1)
	local orb_color = item:sub(num_pos+3)
	if orb_color ~= color then
		return false
	end
	mark_point('loc27_ball_'..character..'_'..orb_num)
	mark_point('loc27_ball_'..character)
	return true
end