//****************************************************************************************************
// 	Sound function
//	Author: Drake, Helepolis
// 	Version 1.0
//	
//	Handles the sound processing by preloading all sounds + using @Event to call them. 
//	Sounds are played anywhere using const_sounds.txt as base + using playSFX(SFX_SOUND); 
//	
// 	With many thanks mainly to Drake for helping me out on this and the people at MotK! You know who you are! Thank you!
//
//****************************************************************************************************

#include "script/thdcs/functions/const_sound.txt"

SetCommonData("soundScriptID", GetOwnScriptID);			// used for EVENT_USER method

@Initialize { }
@MainLoop { yield; }
@Finalize { }
@Event {
	// Play sfx based on event input
	if(GetEventType() == EV_USER+193) {
		ObjSound_SetVolumeRate(ALL_SOUNDS[GetEventArgument(0)],GetAreaCommonData("system","sfxvol",100));
		ObjSound_Play(ALL_SOUNDS[GetEventArgument(0)]);
	}

	// Play voices based on event input
	if(GetEventType() == EV_USER+143) {
		//ObjSound_Play(ALL_VOICES[GetEventArgument(0)] );
	}

	// Play music based on event input
	if(GetEventType() == EV_USER+104) {
		ObjSound_SetVolumeRate(ALL_SOUNDS[GetEventArgument(0)],GetAreaCommonData("system","bgmvol",100));
		ObjSound_Play( ALL_MUSIC[GetEventArgument(0)] );
		SetCommonData("songID",ALL_MUSIC[GetEventArgument(0)]);
	}

	// Stop music based on event input
	if(GetEventType() == EV_USER+1040) {
		ObjSound_SetFade(GetEventArgument(0),0); 
		ObjSound_Stop(ALL_MUSIC[GetEventArgument(0)] );
	}

	// Stop music based on event input (Fade out)
	if(GetEventType() == EV_USER+1041) {
		ObjSound_SetFade(GetEventArgument(0),120); 
	}

	// Stop all music instantly
	if(GetEventType() == EV_USER+1042) {
		ascent(i in 0..length(ALL_MUSIC)) { 
			ObjSound_Stop(ALL_MUSIC[i]); 
		}
	}

	// Enable/disable restart from where the song was stopped
	if(GetEventType() == EV_USER+100) {
		let tempArgument = GetEventArgument(0);
		let isRestartEnable;
		if(tempArgument[1] == 0) { isRestartEnable = false; } 
		if(tempArgument[1] == 1) { isRestartEnable = true; } 
		ObjSound_SetRestartEnable(ALL_MUSIC[tempArgument[0]],isRestartEnable);
	}

	// Set volume rate
	if(GetEventType() == EV_USER+101) {
		let tempArgument = GetEventArgument(0);
		ObjSound_SetVolumeRate(ALL_MUSIC[tempArgument[0]],tempArgument[1]);
	}

	// Stop the SFX being played
	if(GetEventType() == EV_USER+102) {
		ascent(i in 0..length(ALL_SOUNDS)) { 
			if(ObjSound_IsPlaying(ALL_SOUNDS[i])) {
				ObjSound_Stop(ALL_SOUNDS[i]); 
			}
		}
	}		
}

function PrepareSE(path) {
	let seObject = ObjSound_Create();

	ObjSound_Load(seObject, path);
	ObjSound_SetLoopEnable(seObject, false);
	ObjSound_SetVolumeRate(seObject, GetAreaCommonData("system", "sfxvol", 80));
	ObjSound_SetSoundDivision(seObject, SOUND_SE);

	return seObject;
}

function PrepareBGM(path,sampleStart,sampleEnd,restart) {
	let bgmObj = ObjSound_Create;

	ObjSound_SetSoundDivision(bgmObj, SOUND_BGM);
	ObjSound_Load(bgmObj, path);
	ObjSound_SetLoopEnable(bgmObj, true);
	//ObjSound_SetRestartEnable(bgmObj, restart);
	ObjSound_SetLoopSampleCount(bgmObj, sampleStart, sampleEnd);
	ObjSound_SetVolumeRate(bgmObj, GetAreaCommonData("system", "bgmvol", 100));

	return bgmObj;
}

let ALL_SOUNDS = [
	PrepareSE("script/thdcs/sfx/se_ack.wav"), // SFX_ACK
	PrepareSE("script/thdcs/sfx/se_ack3.wav"), // SFX_ACK3
	PrepareSE("script/thdcs/sfx/se_alert.wav"), // SFX_ALERT
	PrepareSE("script/thdcs/sfx/se_ballhum.wav"), // SFX_BALLHUM
	PrepareSE("script/thdcs/sfx/se_ballmove.wav"), // SFX_BALLMOVE
	PrepareSE("script/thdcs/sfx/se_boon00.wav"), // SFX_BOON00
	PrepareSE("script/thdcs/sfx/se_boon01.wav"), // SFX_BOON01
	PrepareSE("script/thdcs/sfx/se_boxbeat.wav"), // SFX_BOXBEAT
	PrepareSE("script/thdcs/sfx/se_boxnoise.wav"), // SFX_BOXNOISE
	PrepareSE("script/thdcs/sfx/se_bzz.wav"), // SFX_BZZ
	PrepareSE("script/thdcs/sfx/se_bzz00.wav"), // SFX_BZZ0
	PrepareSE("script/thdcs/sfx/se_bzz01.wav"), // SFX_BZZ1
	PrepareSE("script/thdcs/sfx/se_cardget.wav"), // SFX_CARDGET
	PrepareSE("script/thdcs/sfx/se_cat00.wav"), // SFX_CAT00
	PrepareSE("script/thdcs/sfx/se_ch00.wav"), // SFX_CH00
	PrepareSE("script/thdcs/sfx/se_ch01.wav"), // SFX_CH01
	PrepareSE("script/thdcs/sfx/se_ch02.wav"), // SFX_CH02
	PrepareSE("script/thdcs/sfx/se_ch03.wav"), // SFX_CH03
	PrepareSE("script/thdcs/sfx/se_chanting.wav"), // SFX_CHANTING
	PrepareSE("script/thdcs/sfx/se_charge00.wav"), // SFX_CHARGE00
	PrepareSE("script/thdcs/sfx/se_circstart.wav"), // SFX_CIRCSTART
	PrepareSE("script/thdcs/sfx/se_crowd00.wav"), // SFX_CROWD00
	PrepareSE("script/thdcs/sfx/se_crowd01.wav"), // SFX_CROWD01
	PrepareSE("script/thdcs/sfx/se_damage00.wav"), // SFX_DAMAGE00
	PrepareSE("script/thdcs/sfx/se_damage01.wav"), // SFX_DAMAGE01
	PrepareSE("script/thdcs/sfx/se_enep00.wav"), // SFX_ENEP00
	PrepareSE("script/thdcs/sfx/se_enep01.wav"), // SFX_ENEP01
	PrepareSE("script/thdcs/sfx/se_extend.wav"), // SFX_EXTEND
	PrepareSE("script/thdcs/sfx/se_fault.wav"), // SFX_FAULT
	PrepareSE("script/thdcs/sfx/se_graze.wav"), // SFX_GRAZE
	PrepareSE("script/thdcs/sfx/se_gun00.wav"), // SFX_GUN00
	PrepareSE("script/thdcs/sfx/se_ice.wav"), // SFX_ICE
	PrepareSE("script/thdcs/sfx/se_inferno.wav"), // SFX_INFERNO
	PrepareSE("script/thdcs/sfx/se_item00.wav"), // SFX_ITEM00
	PrepareSE("script/thdcs/sfx/se_item01.wav"), // SFX_ITEM01
	PrepareSE("script/thdcs/sfx/se_kill.wav"), // SFX_KILL
	PrepareSE("script/thdcs/sfx/se_kira00.wav"), // SFX_KIRA00
	PrepareSE("script/thdcs/sfx/se_kira01.wav"), // SFX_KIRA01
	PrepareSE("script/thdcs/sfx/se_kira02.wav"), // SFX_KIRA02
	PrepareSE("script/thdcs/sfx/se_lance.wav"), // SFX_LANCE
	PrepareSE("script/thdcs/sfx/se_lazer00.wav"), // SFX_LAZER00
	PrepareSE("script/thdcs/sfx/se_lazer01.wav"), // SFX_LAZER01
	PrepareSE("script/thdcs/sfx/se_lazer02.wav"), // SFX_LAZER02
	PrepareSE("script/thdcs/sfx/se_lazer03.wav"), // SFX_LAZER03
	PrepareSE("script/thdcs/sfx/se_mencancel.wav"), // SFX_MENCANCEL
	PrepareSE("script/thdcs/sfx/se_menpick.wav"), // SFX_MENPICK
	PrepareSE("script/thdcs/sfx/se_missile.wav"), // SFX_MISSILE
	PrepareSE("script/thdcs/sfx/se_mix00.wav"), // SFX_MIX00
	PrepareSE("script/thdcs/sfx/se_mix01.wav"), // SFX_MIX01
	PrepareSE("script/thdcs/sfx/se_nep00.wav"), // SFX_NEP00
	PrepareSE("script/thdcs/sfx/se_nodamage.wav"), // SFX_NODAMAGE
	PrepareSE("script/thdcs/sfx/se_ok00.wav"), // SFX_OK00
	PrepareSE("script/thdcs/sfx/se_ophide.wav"), // SFX_OPHIDE
	PrepareSE("script/thdcs/sfx/se_opshow.wav"), // SFX_OPSHOW
	PrepareSE("script/thdcs/sfx/se_option.wav"), // SFX_OPTION
	PrepareSE("script/thdcs/sfx/se_pause.wav"), // SFX_PAUSE
	PrepareSE("script/thdcs/sfx/se_plane.wav"), // SFX_PLANE
	PrepareSE("script/thdcs/sfx/se_power0.wav"), // SFX_POWER0
	PrepareSE("script/thdcs/sfx/se_power1.wav"), // SFX_POWER1
	PrepareSE("script/thdcs/sfx/se_quake.wav"), // SFX_QUAKE
	PrepareSE("script/thdcs/sfx/se_select00.wav"), // SFX_SELECT00
	PrepareSE("script/thdcs/sfx/se_slash.wav"), // SFX_SLASH
	PrepareSE("script/thdcs/sfx/se_swoosh.wav"), // SFX_SWOOSH
	PrepareSE("script/thdcs/sfx/se_swoosh1.wav"), // SFX_SWOOSH1
	PrepareSE("script/thdcs/sfx/se_swoosh2.wav"), // SFX_SWOOSH2
	PrepareSE("script/thdcs/sfx/se_tan00.wav"), // SFX_TAN00
	PrepareSE("script/thdcs/sfx/se_tan01.wav"), // SFX_TAN01
	PrepareSE("script/thdcs/sfx/se_tan02.wav"), // SFX_TAN02
	PrepareSE("script/thdcs/sfx/se_thunder00.wav"), // SFX_THUNDER00
	PrepareSE("script/thdcs/sfx/se_thunder01.wav"), // SFX_THUNDER01
	PrepareSE("script/thdcs/sfx/se_tick00.wav"), // SFX_TICK00
	PrepareSE("script/thdcs/sfx/se_tick01.wav"), // SFX_TICK01
	PrepareSE("script/thdcs/sfx/se_timeout.wav"), // SFX_TIMEOUT
	PrepareSE("script/thdcs/sfx/se_timeout2.wav"), // SFX_TIMEOUT2
	PrepareSE("script/thdcs/sfx/se_ufo.wav"), // SFX_UFO
	PrepareSE("script/thdcs/sfx/se_ufoalert.wav"), // SFX_UFOALERT
	PrepareSE("script/thdcs/sfx/se_usa00.wav"), // SFX_USA00
	PrepareSE("script/thdcs/sfx/se_veer.wav"), // SFX_FEATHER
	PrepareSE("script/thdcs/sfx/se_water.wav"), // SFX_WATER
	PrepareSE("script/thdcs/sfx/se_wind.wav"), // SFX_WIND
	PrepareSE("script/thdcs/sfx/se_wind0.wav"), // SFX_WIND0
	PrepareSE("script/thdcs/sfx/se_disconep00.wav"), // SFX_DISCONEP00
	PrepareSE("script/thdcs/sfx/se_expl00.wav"), // SFX_EXPL00
	PrepareSE("script/thdcs/sfx/se_fever00.wav"), // SFX_FEVER00
	PrepareSE("script/thdcs/sfx/se_pichuun.wav"), // SFX_PLDEAD00
	PrepareSE("script/thdcs/sfx/se_plst00.wav"), // SFX_PLST00
	PrepareSE("script/thdcs/sfx/se_pshot.wav"), // SFX_PSHOT
	PrepareSE("script/thdcs/sfx/se_shanghaiboom.wav"), // SFX_SHANGHAIBOOM
	PrepareSE("script/thdcs/sfx/se_spellcard.wav"), // SFX_SPELLCARD
	PrepareSE("script/thdcs/sfx/se_sukima.wav"), // SFX_SUKIMA
	PrepareSE("script/thdcs/sfx/se_dame.wav"), // SFX_DAME
	PrepareSE("script/thdcs/sfx/se_spellup.wav"), // SFX_SPELLUP
	PrepareSE("script/thdcs/sfx/se_ack2.wav"), // SFX_SPELLUP
	PrepareSE("script/thdcs/sfx/se_pichuun1.wav"), // SFX_PLDEAD01
	PrepareSE("script/thdcs/sfx/se_creak.wav"), // SFX_CREAK
	PrepareSE("script/thdcs/sfx/se_watersplash.wav"), // SFX_WATERSTREAM
	PrepareSE("script/thdcs/sfx/se_waterstream.wav") // SFX_WATERSPLASH
];

// All music array
let ALL_MUSIC = [
	PrepareBGM("",0,0,true),											// Dummy to create silence
	PrepareBGM("script/thdcs/bgm/stg01.ogg",710085,6605701,true),		// Stage 1
	PrepareBGM("script/thdcs/bgm/stg02.ogg",452853,10240136,true),		// Stage 2
	PrepareBGM("script/thdcs/bgm/stg03.ogg",583281,7349357,true),		// Stage 3
	PrepareBGM("script/thdcs/bgm/boss01.ogg",533275,2976879,true),		// Boss 1
	PrepareBGM("script/thdcs/bgm/boss02.ogg",2213,4814177,true),		// Boss 2
	PrepareBGM("script/thdcs/bgm/boss03.ogg",0,3445249,true),			// Boss 3
	PrepareBGM("script/thdcs/bgm/pause.ogg",0,534329,true),				// Pause
	PrepareBGM("script/thdcs/bgm/title.ogg",0,2886172,true),			// Title
	PrepareBGM("script/thdcs/bgm/ending.ogg",0,6283111,true)			// Ending
];

// All voices array
let ALL_VOICES = [

];

