var sound_level = 1;
var music_level = 1;


var sounds = new Array();

sounds[0] = LoadSound("shotgun1.wav", false);
sounds[1] = LoadSound("shotgun2.wav", false);
sounds[2] = LoadSound("shotgun3.wav", false);
sounds[3] = LoadSound("shotgun_reload.wav", false);

sounds[4] = LoadSound("backclick.wav", false);
sounds[5] = LoadSound("beep1.wav", false);
sounds[6] = LoadSound("click1.wav", false);

sounds[7] = LoadSound("casing2.wav",false);
sounds[8] = LoadSound("casing2.wav",false);
sounds[9] = LoadSound("pistol1a.wav",false);
sounds[11] = LoadSound("machine1.wav",false);
sounds[10] = LoadSound("magnum1c.wav",false);

sounds[12] = LoadSound("reload1.wav");
sounds[13] = LoadSound("reload2.wav");
sounds[14] = LoadSound("reload3.wav");

sounds[15] = LoadSound("shrap1.wav");
sounds[16] = LoadSound("shrap_reload1.wav");
sounds[17] = LoadSound("oblit1b.wav");
sounds[18] = LoadSound("oblit_reload.wav");

sounds[19] = LoadSound("gunclick.wav",false);
sounds[20] = LoadSound("gunclick2.wav",false);

sounds[21] = LoadSound("railgun1.wav",false);
sounds[22] = LoadSound("railgun_alt_charge.wav",false);
sounds[23] = LoadSound("railgun_tail.wav",false);

sounds[24] = LoadSound("explode1.wav",false);
sounds[25] = LoadSound("missle3a.wav",false);
sounds[26] = LoadSound("missle4b.wav",false);
sounds[27] = LoadSound("explode2c.wav",false);
sounds[28] = LoadSound("grenade_launch2.wav",false);
sounds[29] = LoadSound("grenade_throw.wav",false);

sounds[30] = LoadSound("walk3.wav");
sounds[31] = LoadSound("run4.wav");

sounds[32] = LoadSound("heart.wav");
sounds[33] = LoadSound("thud6.wav", false);//"jump1.wav",false);
sounds[34] = LoadSound("blood1.wav", false);//LoadSound("thud1.wav");
sounds[35] = LoadSound("bounce4.wav", false);//LoadSound("bounce3.wav");
sounds[36] = LoadSound("head_bounce3.wav", false);//LoadSound("thud1.wav");

sounds[37] = LoadSound("shrapnel1.wav", false);
sounds[37].setVolume(128);
sounds[38] = LoadSound("switch1.wav");
sounds[39] = LoadSound("dooropen1.wav",false);
sounds[40] = LoadSound("locker1.wav",false);

sounds[41] = LoadSound("wall_break1.wav",false);

sounds[42] = LoadSound("enemy_hit1.wav",false);
sounds[43] = LoadSound("enemy_die2.wav", true);
sounds[44] = LoadSound("enemy_idle1.wav",false);
sounds[45] = LoadSound("swing1b.wav",false);
sounds[46] = LoadSound("head_explode2.wav",false);

sounds[47] = LoadSound("land_hard2.wav",false);

sounds[48] = LoadSound("walk6.wav",false);
sounds[49] = LoadSound("walk6.wav",false);

sounds[50] = LoadSound("crawl1.wav",true);
sounds[51] = LoadSound("crawl1.wav",true);

sounds[52] = LoadSound("swing3.wav",false);
sounds[53] = LoadSound("enemy_moan2.wav",false);
sounds[54] = LoadSound("enemy_moan2.wav",true);

sounds[55] = LoadSound("flame1.wav",false);
sounds[56] = LoadSound("wipedie1.wav",true);

sounds[57] = LoadSound("mantis_die.wav",false);
sounds[58] = LoadSound("mantis_attack1.wav",false);
sounds[59] = LoadSound("mantis_rise3.wav",false);
sounds[60] = LoadSound("flame4.wav",false);

sounds[61] = LoadSound("cgun_start.wav",false);
sounds[62] = LoadSound("cgun_fire2.wav",false);
sounds[63] = LoadSound("cgun_fire2.wav",false);
sounds[64] = LoadSound("cgun_fire3.wav",false);

sounds[65] = LoadSound("super_miss1.wav",false);
sounds[66] = LoadSound("superchew1.wav",false);


sounds[67] = LoadSound("super_attack1.wav",false);
sounds[68] = LoadSound("super_attack2.wav",false);
sounds[69] = LoadSound("super_die1.wav",false);
sounds[70] = LoadSound("super_idle1.wav",false);
sounds[71] = LoadSound("super_idle2.wav",false);
sounds[72] = LoadSound("super_intro2.wav",false);
sounds[73] = LoadSound("super_fire3.wav",false);
sounds[74] = LoadSound("super_wave2.wav",false);
sounds[75] = LoadSound("super_wave2b.wav",false);

sounds[76] = LoadSound("railgun2.wav",false);
sounds[78] = LoadSound("idbeam1.wav",false);

sounds[79] = LoadSound("sword1.wav",false);
sounds[80] = LoadSound("sword2.wav",false);
sounds[81] = LoadSound("supernaut_thud1.wav",false);
sounds[82] = LoadSound("sword1b.wav",false);
sounds[83] = LoadSound("sword2b.wav",false);

sounds[84] = LoadSound("boss_scream1.wav",false);

sounds[85] = LoadSound("click1a.wav", false);
sounds[86] = LoadSound("click1b.wav", false);

sounds[87] = LoadSound("flak.wav", false);
sounds[88] = LoadSound("flak_bounce_1.wav", false);
sounds[89] = LoadSound("flak_bounce_2.wav", false);
sounds[90] = LoadSound("flak_bounce_3.wav", false);

sounds[91] = LoadSound("railgun_alt_duration.wav",false);
sounds[92] = LoadSound("jelly1.wav",false);
sounds[93] = LoadSound("guard_die1.wav",false);
sounds[94] = LoadSound("id1.wav",true);
sounds[95] = LoadSound("id2.wav",false);
sounds[96] = LoadSound("id_static.wav",false);

sounds[97] = LoadSound("enemy_moan2.wav",false);
sounds[98] = LoadSound("enemy_moan2.wav",true);

sounds[99] = LoadSound("jump_up1.wav",false);
sounds[100] = LoadSound("jump_up2.wav",false);
sounds[101] = LoadSound("jump_up3.wav",false);

sounds[102] = LoadSound("barrel_hit.wav",false);
sounds[103] = LoadSound("barrel_knock.wav",false);
sounds[104] = LoadSound("barrel_roll_loop.wav",false);

sounds[105] = LoadSound("barrel_collide.wav",false);

sounds[106] = LoadSound("id_stamina_beam_1.wav",false);
sounds[107] = LoadSound("id_stamina_beam_2.wav",false);

sounds[108] = LoadSound("super_firestart.wav",false);

sounds[109] = LoadSound("umberdie1b.wav",false);
sounds[110] = LoadSound("jellydie1.wav",false);
sounds[111] = LoadSound("eyedie1.wav",false);
sounds[112] = LoadSound("dronedie1.wav",false);

sounds[113] = LoadSound("dronefly1.wav",false);
sounds[114] = LoadSound("iddie1b.wav",false);
sounds[115] = LoadSound("idhurt.wav",false);

sounds[116] = LoadSound("fieldsdown.wav",true);
sounds[117] = LoadSound("tele1.wav",true);

sounds[118] = LoadSound("land_crack2.wav",false);
sounds[119] = LoadSound("player_landing_hurt_1.wav",false);
sounds[120] = LoadSound("player_landing_hurt_3.wav",false);

sounds[121] = LoadSound("super_grenade1.wav",true);

sounds[122] = LoadSound("waters1.wav",false);
sounds[123] = LoadSound("waters2.wav",false);
sounds[124] = LoadSound("waters3.wav",false);
sounds[125] = LoadSound("waters4.wav",false);

sounds[126] = LoadSound("stone1.wav",false);
sounds[127] = LoadSound("stone2.wav",false);

sounds[128] = LoadSound("splash.wav",false);

sounds[129] = LoadSound("nodoor.wav",false);
sounds[130] = LoadSound("supernaut_headb1.wav",false);

sounds[131] = LoadSound("shotgun_cock.wav",false);
sounds[132] = LoadSound("ticking1.wav",false);

sounds[133] = LoadSound("eye1_atk.wav",false);
sounds[134] = LoadSound("stimpack1.wav",false);
sounds[135] = LoadSound("stimpack2.wav",false);

var next_amb_call = 1;
var idle_last_call = 0;

var wawa_c = 0;

function StopReloadSounds()
{
	StopSound(3);
	StopSound(12);
	StopSound(13);
	StopSound(14);

	StopSound(16);
	StopSound(18);

	StopSound(22);
	StopSound(23);
	StopSound(28);

	StopSound(91);
}

function StopSound(num)
{
	if (sounds[num].isPlaying() == true)
		sounds[num].stop();

}

function PlayAWaterSound()
{
	for (pi=0;pi<4;pi++)
	{
		if (sounds[122+pi].isPlaying() == true)
			break;
	}

	if (pi>=4)
	{
		var num = Math.floor(Math.random()*4);
		PlaySound(122+num, true);
	}
}

function PlaySound(number, pitch,vol, loop)
{
	if (number > 87 && number <91)
	{
		//shrap sound modify
		vol = 0.8;
	}
	if (number > 98 && number <102)
	{
		//shrap sound modify
		vol = 0.8;
	}

	if (number == 45)
	{
		if (Math.random()<0.5)
			number = 52;
	}
	sounds[number].stop();
	sounds[number].setPosition(0);

	if (pitch == true)
		sounds[number].setPitch(0.95+0.1*Math.random());
	else
		sounds[number].setPitch(1);
		
	if (vol != undefined)
	{
		if (vol < 0)
			vol = 0;
		if (vol > 1)
			vol = 1;
			
		sounds[number].setVolume(vol*255*sound_level);
	}
	else
		sounds[number].setVolume(255*sound_level);
	
	
	
	if (loop == true)
		sounds[number].play(true);
	else
		sounds[number].play(false);
	
}

var case1num = 0;
var case2num = 0;

function PlayCasing(amount)
{
	case1num = (case1num+1)%2
	sounds[7+case1num].setPitch(1.5);
	sounds[7+case1num].setVolume(128*amount/4);
	PlaySound(7+case1num, true, (128*amount/4)/255);
}

var walknum = 0;
var walklast = 0;
var crawlnum = 0;

function PlayEnemyWalk(amount)
{
	if (GetTime()-walklast > 100)
	{
		walklast = GetTime();
		walknum = (walknum+1)%2;
		sounds[48+walknum].setPitch(1);//1.0+0.25*Math.random());
		sounds[48+walknum].setVolume(255);//128+64+128*amount);
		PlaySound(48+walknum);
	}
}

function PlayEnemyCrawl(amount)
{
	crawlnum = (crawlnum+1)%2;
	sounds[50+crawlnum].setPitch(0.66+0.66*Math.random());
	sounds[50+crawlnum].setVolume(128+128*amount);
	PlaySound(50+crawlnum);
}

var ambnum = 0;
function PlayAmbush(pan, vol)
{
	ambnum = (ambnum+1)%2;
	sounds[ambnum].setPan(pan);
	PlaySound(53+ambnum,true, vol*vol*3/2);
}




var chainy = 0;
function PlayChainGun()
{
	if ((g_frames % 8 == 1))
	{
		if (sounds[62+chainy].isPlaying()==false)
		{
			PlaySound(62+chainy, undefined, 0.75);
			chainy=(chainy+1)%2;
		}
	}
}


function SaveSoundConfig()
{
	var sfile = OpenFile("config.sav");
	sfile.write("sound", sound_level*10);
	sfile.write("music", music_level*10);
	sfile.flush();
	sfile.close();
}

function LoadSoundConfig()
{
	var sfile = OpenFile("config.sav");
	sound_level = sfile.read("sound", 10)/10;
	music_level = sfile.read("music", 10)/10;
	
	sfile.close();
}

function PauseHeart()
{
	sounds[32].setVolume(0);
	sounds[91].setVolume(0);
	sounds[104].setVolume(0);
}

function StartHeart()
{
	sounds[32].setVolume(255*sound_level);
	sounds[91].setVolume(255*sound_level);
	sounds[104].setVolume(255*sound_level);
}


var level_music = new Array();

level_music[-2] = 23;
level_music[1] = 24;
level_music[2] = 4;
level_music[3] = 5;
level_music[4] = -1;//-1;starts at boss
level_music[5] = 5;
level_music[6] = 4;
level_music[7] = 13;
level_music[8] = 12;
level_music[9] = 14;//-1;starts at boss
level_music[10] = 15;
level_music[11] = -1;//-1;starts at change
level_music[12] = 10;
level_music[13] = -1;//-1;starts at boss
level_music[14] = 2;
level_music[15] = -1;//-1;starts at boss
level_music[16] = 9;
level_music[17] = -1;
level_music[18] = -1;//passes from previous

function StartMusic()
{
	curr_music = level_music[g_current_level];
	if (curr_music > -1)
	{
		music[curr_music].play(true);
		music[curr_music].setVolume(music_vol[curr_music]*music_level);
	}
}

var g_mtransition = -1;
var m_transitions = new Array()
var max_mtransitions = 9;
for (i=0;i<max_mtransitions;i++)
{
	m_transitions[i] = new Object();
	m_transitions[i].playtrack = -1;//play this track (-1 means no playing)
	m_transitions[i].newtrack = -1;//stop music.
	m_transitions[i].fadeper = -1;//stop the old music at this perc of the transition
}

m_transitions[0].playtrack = 3;
m_transitions[0].newtrack = 20;
m_transitions[0].fadeper = 0.05;//stop the old music at this perc of the transition
m_transitions[0].newfade = 1.00;//start the new music at this perc of the transition

m_transitions[1].playtrack = 27;
m_transitions[1].newtrack = 16;
m_transitions[1].fadeper = 0.05;//stop the old music at this perc of the transition
m_transitions[1].newfade = 1.00;//start the new music at this perc of the transition

m_transitions[2].playtrack = 8;
m_transitions[2].newtrack = 8;
m_transitions[2].fadeper = 0.05;//stop the old music at this perc of the transition
m_transitions[2].newfade = 1.00;//start the new music at this perc of the transition


m_transitions[3].playtrack = 7;
m_transitions[3].newtrack = 7;
m_transitions[3].fadeper = 0.05;//stop the old music at this perc of the transition
m_transitions[3].newfade = 1.00;//start the new music at this perc of the transition


m_transitions[4].playtrack = 17;
m_transitions[4].newtrack = -1;
m_transitions[4].fadeper = 0.01;//stop the old music at this perc of the transition
m_transitions[4].newfade = 1.00;//start the new music at this perc of the transition

m_transitions[5].playtrack = 18;
m_transitions[5].newtrack = 19;
m_transitions[5].fadeper = 0.5;//stop the old music at this perc of the transition
m_transitions[5].newfade = 1.00;//start the new music at this perc of the transition

m_transitions[6].playtrack = 22;
m_transitions[6].newtrack = 22;
m_transitions[6].fadeper = 0.5;//stop the old music at this perc of the transition
m_transitions[6].newfade = 1.00;//start the new music at this perc of the transition

m_transitions[7].playtrack = 25;
m_transitions[7].newtrack = 25;
m_transitions[7].fadeper = 0.5;//stop the old music at this perc of the transition
m_transitions[7].newfade = 1.00;//start the new music at this perc of the transition

m_transitions[8].playtrack = 18;
m_transitions[8].newtrack = -1;
m_transitions[8].fadeper = 0.5;//stop the old music at this perc of the transition
m_transitions[8].newfade = 1.00;//start the new music at this perc of the transition


function StopAllMusics()
{
	for (pi=0;pi<music.length;pi++)
	{
		if (music[pi].isPlaying() == true)
		{
			music[pi].stop();
		}
	}	
	g_mtransition=-1;
}

function UpdateMusic()
{
	var m = g_mtransition;
	if (m > -1)
	{
		if (m_transitions[m].playtrack > -1)
		{
			music[m_transitions[m].playtrack].setVolume(music_vol[m_transitions[m].playtrack]*music_level);

		
			var pos = music[m_transitions[m].playtrack].getPosition();
			var length = music[m_transitions[m].playtrack].getLength();
			if (pos/length < m_transitions[m].fadeper)
			{
				if (curr_music > -1)
					music[curr_music].setVolume(music_vol[curr_music]*music_level*(1-(pos/length)/m_transitions[m].fadeper));
			}
			else
			{
				if (curr_music > -1)
				{
					if (music[curr_music].isPlaying())
						music[curr_music].stop();
					music[curr_music].setVolume(0);
				}
			}
			
			if (pos/length > m_transitions[m].newfade)
			{
				if (m_transitions[m].newtrack > -1)
				{
					if (music[m_transitions[m].newtrack].isPlaying() == false)
					{
						music[m_transitions[m].newtrack].setVolume(0);
						music[m_transitions[m].newtrack].play(true);
						curr_music = m_transitions[m].newtrack;
					}
					else if (music[m_transitions[num].playtrack].isPlaying())
					{
						music[m_transitions[m].newtrack].setVolume(music_vol[m_transitions[m].newtrack]*music_level*(((pos/length)-m_transitions[m].newfade)/(1-m_transitions[m].newfade)));
					}
					else 
					{
						music[m_transitions[m].newtrack].setVolume(music_vol[m_transitions[m].newtrack]*music_level);
						g_mtransition = -1;
					}
				}
				else
				{
					g_mtransition = -1;
				}
			}
			else if (m_transitions[m].newfade == 1.00 && music[m_transitions[m].playtrack].isPlaying() == false)
			{
				if (m_transitions[m].newtrack > -1)
				{
					music[m_transitions[m].newtrack].setPosition(0)
					music[m_transitions[m].newtrack].setVolume(music_vol[m_transitions[m].newtrack]*music_level);
					music[m_transitions[m].newtrack].play(true);
					curr_music = m_transitions[m].newtrack;
					g_mtransition = -1;
				}
				else
				{
					g_mtransition = -1;
				}
			}
		}
	}

	else
	{
		if (curr_music > -1)
			music[curr_music].setVolume(music_vol[curr_music]*music_level);
	}
}

function MakeTransition(num)
{
	if (m_transitions[num].playtrack > -1)
	{
		music[m_transitions[num].playtrack].setPosition(0);
		music[m_transitions[num].playtrack].play(false);
		music[m_transitions[num].playtrack].setVolume(music_level*music_vol[m_transitions[num].playtrack]);
		g_mtransition=num;
	}
	
}




var curr_music = 0;
var music = new Array();
music[0] = LoadSound("rtde_amb1.mp3",true);
music[1] = LoadSound("music_end_fade.mp3",true);
music[2] = LoadSound("slowdown_vs_bloop.ogg",true);//rtde_amb1.ogg");
music[3] = LoadSound("music_cave_transition.ogg",true);//.mp3",true);
music[4] = LoadSound("ambience_hangar1.ogg",true);
music[5] = LoadSound("ambience_hangar2.ogg",true);
music[6] = LoadSound("creds.mp3",true);
music[7] = LoadSound("dual_4b.mp3",true);
music[8] = LoadSound("qualo-mindsplat.ogg",true);
music[9] = LoadSound("retaliation.mp3",true);
music[10] = LoadSound("rep.mp3",true);
music[11] = LoadSound("dj_skrillex.mp3",true);
music[12] = LoadSound("ambience_lab.mp3",true);
music[13] = LoadSound("sul_drone.ogg",true);
music[14] = LoadSound("watermelons_a.ogg",true);
music[15] = LoadSound("watermelons_b.ogg",true);
music[16] = LoadSound("music_idsprint.mp3",true);
music[17] = LoadSound("crimson_stinger.mp3",true);
music[18] = LoadSound("silence.ogg",true);
music[19] = LoadSound("music_end_boss.mp3",true);

music[20] = LoadSound("ambience_cave.mp3",true);
music[21] = LoadSound("sci.mp3",true);

music[22] = LoadSound("naut.mp3",true);
music[23] = LoadSound("danger.mp3",true);
music[24] = LoadSound("music_tutorial.mp3",true);
music[25] = LoadSound("music_naut2.mp3",true);
music[26] = LoadSound("end_boss_stinger.mp3",true);
music[27] = LoadSound("silence2.ogg",true);



var music_vol = new Array();
music_vol[0] = 128;
music_vol[1] = 255;
music_vol[2] = 255;
music_vol[3] = 255;
music_vol[4] = 255;
music_vol[5] = 255;
music_vol[6] = 255;
music_vol[7] = 255;
music_vol[8] = 160;
music_vol[9] = 255;
music_vol[10] = 255;
music_vol[11] = 255;
music_vol[12] = 255;
music_vol[13] = 255;
music_vol[14] = 128;
music_vol[15] = 160;
music_vol[16] = 255;
music_vol[17] = 255;
music_vol[18] = 255;
music_vol[19] = 255;
music_vol[20] = 255;
music_vol[21] = 160;
music_vol[22] = 255;
music_vol[23] = 255;
music_vol[24] = 160;
music_vol[25] = 255;
music_vol[26] = 255;
music_vol[27] = 255;



var music_length = music.length

function PlayMusic(num)
{
	if (num == 0)
		return;
	for (pm=0;pm<music_length;pm++)
	{
		music[pm].stop();
		music[pm].setPosition(0);
		music[pm].setVolume(music_vol[pm]*music_level);
	}
	music[num].play(true);
	curr_music = num;
}

function FadeMusic(amt)
{
	for (ll=0;ll<music.length;ll++)
	{
		if (music[ll].isPlaying() == true)
		{
			if (amt <0.05)
			{
				music[ll].setVolume(0);
				music[ll].stop();
			}
			else
				music[ll].setVolume(amt*music_vol[ll]*music_level);
		}
	}
}

var pause_music = new Array();
var pause_music_r = new Array();

for (i=0;i<music.length;i++)
{
	pause_music[i] = 0;
	pause_music_r[i] = 0;
}

function ClearPauseMusicStats()
{
	for (pi=0;pi<music.length;pi++)
	{
		pause_music[pi] = 0;
		pause_music_r[pi] = 0;
	}
}


function PauseAllMusic()
{
	PauseHeart();
	//figure out which music is playing;
	for (pi=0;pi<music.length;pi++)
	{
		if (music[pi].isPlaying() == true)
		{
			music[pi].pause();
			pause_music[pi] = 1;
			if (music[pi].getRepeat() == true)
				pause_music_r[pi] = 1;
			else
				pause_music_r[pi] = 0;
		}
		else
		{
			pause_music[pi] = 0;
			pause_music_r[pi] = 0;
		}
	}


}

function UnPauseAllMusic()
{
	//figure out which music is playing;
	for (pi=0;pi<music.length;pi++)
	{
		if (pause_music[pi] == 1)
		{
			music[pi].play();
			music[pi].setRepeat(pause_music_r[pi]);
		}
	}
	StartHeart();
}