function SupernautEndScene()
{
	PauseHeart();
	PauseAllMusic();
	ClearPauseMusicStats();
	
	var end_images = new Array();
	end_images[0] = LoadImage("ending/supernaut1.png");
	end_images[1] = LoadImage("ending/lock1_01.png");
	end_images[2] = LoadImage("ending/lock1_02.png");
	end_images[3] = LoadImage("ending/lock1_03.png");
	end_images[4] = LoadImage("ending/lock1_04.png");
	end_images[5] = LoadImage("ending/lock1_05.png");
	end_images[6] = LoadImage("ending/lock1_06.png");
	end_images[7] = LoadImage("ending/lock1_07.png");
	end_images[8] = LoadImage("ending/lock1_08.png");
	end_images[9] = LoadImage("ending/lock1_09.png");
	end_images[10] = LoadImage("ending/console1.png");
	end_images[11] = LoadImage("ending/sprites1.png");
	end_images[12] = LoadImage("ending/reworks1.png");
	end_images[13] = LoadImage("ending/walls1.png");
	end_images[14] = LoadImage("ending/liegh_screen_01.png");
	end_images[15] = LoadImage("ending/liegh_screen_02.png");
	end_images[16] = LoadImage("ending/liegh_screen_03.png");

	end_images[17] = LoadImage("ending/liegh_gun_01.png");
	end_images[18] = LoadImage("ending/liegh_talk_01.png");
	end_images[19] = LoadImage("ending/liegh_talk_02.png");
	end_images[20] = LoadImage("ending/liegh_talk_03.png");
	end_images[21] = LoadImage("ending/liegh_talk_04.png");

	end_images[22] = LoadImage("ending/liegh_shoulder_01.png");
	end_images[23] = LoadImage("ending/liegh_shoulder_02.png");
	end_images[24] = LoadImage("ending/liegh_shoulder_03.png");
	end_images[25] = LoadImage("ending/liegh_shoulder_04.png");

	end_images[26] = LoadImage("ending/liegh_gun_02.png");
	end_images[27] = LoadImage("ending/liegh_gun_03.png");
	end_images[28] = LoadImage("ending/liegh_gun_04.png");
	end_images[29] = LoadImage("ending/liegh_gun_05.png");
	end_images[30] = LoadImage("ending/liegh_gun_06.png");
	end_images[31] = LoadImage("ending/liegh_gun_07.png");

	end_images[32] = LoadImage("ending/front1_01.png");
	end_images[33] = LoadImage("ending/Blood_Spatter.png");

	var end_texts = new Array();
	
	end_texts[0]  = "Damnit...that was tough.";
	end_texts[1]  = "Whatever it was though...it's dead now.";
	end_texts[2]  = "...The bunker's ahead.";
	end_texts[3]  = "This is it...";
	end_texts[4]  = "...Looks empty.";
	end_texts[5]  = "Well, I guess all that's left is to wait here."
	end_texts[6]  = "...";
	end_texts[7]  = "The terminal looks powered and logged in.";
	end_texts[8]  = "...Davis?  You there?";
	end_texts[9]  = "..."
	end_texts[10] = "...Hmm, he doesn't have access I guess.";
	end_texts[11] = "Let's have a look at this...";
	end_texts[12] = "(There are several research logs.  Most deal";
	end_texts[13] = "with the development of an alternative to";
	end_texts[14] = "nanotechonology: the so called \"sprites.\"";
	end_texts[15] = "Some however, involve reworks in the labs.";
	end_texts[16] = "One deals with the symptoms of the infection.)";
	end_texts[17] = "\"Hallucinations, particularly of strange," ;
	end_texts[18] = "horrific creatures is usually the first sign";
	end_texts[19] = "of a passive infection.  After some time,";
	end_texts[20] = "these hallucinations expand to include the";
	end_texts[21] = "surrounding environment as well.\"";
	end_texts[22] = "\"Sometimes they see blood pouring";
	end_texts[23] = "out of the vents, or from under the doors.";
	end_texts[24] = "Sometimes the walls turn to stone, or flesh.";
	end_texts[25] = "The ability to reason goes next, and then";
	end_texts[26] = "their bodies change into rework form.\"";
	end_texts[27] = "\"At that point, they change completely";
	end_texts[28] = "into reworks, and the only thing we can do";
	end_texts[29] = "is kill them before they kill anyone else.\""
	end_texts[30] = "\"Unfortunately, that's all we can do.";
	end_texts[31] = "Even if we had more time, I doubt we'd ever";
	end_texts[32] = "be able to find a cure... the damage seems...";
	end_texts[33] = "...irreversible.\"";
	end_texts[34] = "\"All we can do is hold up here, and wait";
	end_texts[35] = "for someone to rescue us...\"";
	end_texts[36] = "...";
	end_texts[37] = "So... I'm infected.";
	end_texts[38] = "...";
	end_texts[39] = "That explains... everything.";
	end_texts[40] = "...";
	end_texts[41] = "...";
	end_texts[42] = "...Become one of them,";
	end_texts[43] = "...or die.";
	end_texts[44] = "...";
	end_texts[45] = "...Fuck.";
	end_texts[46] = "...";
	end_texts[47] = "...I guess this is it.";
	end_texts[48] = "...";

/*
					//fade in
					-show the dead supernaut
						-Whatever it was... it's dead now.
						-...The bunker's ahead.
					-show lock
						-This is it...
					-show computer
						-...
						-Well... I guess all that's left is to wait here.
						-...
						
					-black screen
						-...
						-The computer terminal is powered and logged in.
						-For whatever the reason, Davis hasn't been able to gain
						 access here.
					-Panning view of white sketches of sprites.
						-There are several research logs.  Most deal with the development of an alternative to
						 nanotechonology: the so called "sprites."
					-Panning view of white sketches of reworks
						-The latest logs however, involve reworks overrunning the labs.
						-One of them in particular, deals with the symptoms of the infection.
					-Some other view of the walls
						-"Hallucinations, particularly of strange, horrific creatures is usually the first sign of
						  a passive infection.  After some time, these Hallucinations expand to include the surrounding environment as well."
						-"Sometimes they see blood pouring into the room.  Sometimes the walls turn to fire, stone, or flesh.
						  The ability to reason goes next, and then their bodies change into rework form."
						-"At that point, they change completely into reworks, and the only thing we can do is kill them before they kill anyone else."
					-Liegh facing the monitor
						-"Unfortunately, that's all we can do.  Even if we had more time, I doubt we'd ever be able to find a cure... the damage seems
						  irreversible."
						-"All we can do is hold up here, and wait for someone to rescue us..."
					-Fade to Liegh standing up.
						-"..."
						-"...So I'm infected."
						-"..."
						-"..."
						-Liegh Looking at the screen, side view
						
						-"...Become one of them,"
						-"...or die."
						-"..."
						-"..."
						-"...I guess death never looked so good."
					-gun to jaw.
						-hammer arming sound
					-fade to black
					-decision time.

				-if yes, credits
					-maybe a blood splatter with sound?
*/

	var scroll_var = 0;
	var scroll_count = 0;
	var scroll_amt = 10;
	function DrawScene(num, text, fade, time, override_text,ignore_text)
	{
		var timey = GetTime();
		var current_text = "";
		if (text >= 0)
			current_text = end_texts[text];



		var current_text_pos = 0;
		if ((text >=0 && fade == "out")||override_text == true)
		{
			current_text_pos = current_text.length;
		}
		
		var text_length = 0;
		if (text>=0)
			text_length = current_text.length;
		if (ignore_text == true || fade=="out")
			text_length = 0;
		var wait_frames = 1000/30;
		var text_speed = 2;
		var talk_counter
			
		for (ses=0;ses<time+text_length*text_speed;ses++)
		{
			if (num >= 14 && num < 17)
			{
				if (text_length > 4 && ses<text_length*text_speed)
				{
					if (ses%4 == 0)
						num = 14+Math.floor(Math.random()*3);
				}
				else
					num = 14;
			}
			else if (num >= 17 && num < 22)
			{
				if (text_length > 4&& ses<text_length*text_speed)
				{
					if (ses%4 == 0)
						num = 17+Math.floor(Math.random()*5);
				}
				else
					num = 17;
			}
			end_images[num].blit(0-scroll_var,20);

			if (text >= 0)
				font2.drawText(5,226, current_text.substr(0,current_text_pos));
			if (fade == "in")
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-ses*255/time));
			if (fade == "out")
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,ses*255/time));
			
			FlipScreen();
			while(GetTime()-timey < wait_frames);
			timey = GetTime();
			if (ses%text_speed == 0)
				current_text_pos++;
			if (scroll_var > 0)
			{
				scroll_count++;
				if (scroll_count%scroll_amt == 0)
					scroll_var++;
			}
			if (AreKeysLeft())
			{
				GetKey();
				return;
			}
		}
	}
	while (AreKeysLeft())GetKey();

	//show supernaut
	DrawScene(0,-1,"in",30);
	DrawScene(0,0,"",60);
	DrawScene(0,1,"",60);
	DrawScene(0,2,"",60);
	DrawScene(0,2,"out",30);
	//show lock
	DrawScene(1,-1,"in",30);
	DrawScene(1,3,"",60);
	for (ei=2;ei<10;ei++)
		DrawScene(ei,3,"",2,true,true);
	PlaySound(38);
//	DrawScene(9,3,"",30,true);
	DrawScene(9,3,"out",30,true);
	PlaySound(39);
	DrawScene(10,-1,"in",30);
	DrawScene(10,4,"",60);
	DrawScene(10,5,"",60);
	DrawScene(10,6,"",60);
	DrawScene(10,7,"",60);
	DrawScene(10,8,"",60);
	DrawScene(10,9,"",60);
	DrawScene(10,10,"",60);
	DrawScene(10,11,"",60);
	DrawScene(10,11,"out",30);
		scroll_var = 1;
		scroll_amt = 5;
		scroll_count = 0;
	DrawScene(11,-1,"in",30);
	DrawScene(11,12,"",60);
	DrawScene(11,13,"",60);
	DrawScene(11,14,"",60);
	DrawScene(11,14,"out",30);
		scroll_var = 1;
		scroll_amt = 9;	
		scroll_count = 0;
	DrawScene(12,-1,"in",30);
	DrawScene(12,15,"",60);
	DrawScene(12,16,"",60);
	DrawScene(12,17,"",60);
	DrawScene(12,18,"",60);
	DrawScene(12,19,"",60);
	DrawScene(12,20,"",60);
	DrawScene(12,21,"",60);
	DrawScene(12,21,"out",30);
		scroll_var = 0;
		scroll_amt = 10;	
		scroll_count = 0;
	DrawScene(13,-1,"in",30);
	DrawScene(13,22,"",60);
	DrawScene(13,23,"",60);
	DrawScene(13,24,"",60);
	DrawScene(13,25,"",60);
	DrawScene(13,26,"",60);
	DrawScene(13,27,"",60);
	DrawScene(13,28,"",60);
	DrawScene(13,29,"",60);
	DrawScene(13,29,"out",30);
	DrawScene(32,-1,"in",30);
	DrawScene(32,30,"",60);
	DrawScene(32,31,"",60);
	DrawScene(32,32,"",60);
	DrawScene(32,33,"",60);
	DrawScene(32,34,"",60);
	DrawScene(32,35,"",60);
//	DrawScene(32,35,"out",30);
//	DrawScene(14,-1,"in",30);
	DrawScene(14,36,"",60);
	DrawScene(14,37,"",60);
	DrawScene(14,38,"",60);
	DrawScene(14,39,"",60);
	DrawScene(14,40,"",60);

	DrawScene(17,41,"",60);
	DrawScene(17,42,"",60);
	DrawScene(17,43,"",60);
	DrawScene(17,44,"",60);
	DrawScene(17,45,"",60);
	for (ei=22;ei<26;ei++)
		DrawScene(ei,-1,"",4,true,true);
	DrawScene(17,46,"",60);
	DrawScene(17,47,"",60);
	DrawScene(17,48,"",60);
	DrawScene(17,-1,"",15);
	PlaySound(12);
	DrawScene(17,-1,"",30);
	for (ei=26;ei<32;ei++)
		DrawScene(ei,-1,"",4,true,true);
	DrawScene(31,-1,"",30,true,true);
	DrawScene(31,-1,"out",30,true,true);
	
	
	//do the click to fire thing
	FlipScreen();
	timey = GetTime();
	while(GetTime()-timey < 1200);
	
	var string = "Click Mouse Button To Shoot";
	//do the click to fire thing
	while(GetTime()-timey < 1200);
	
	var fire = false;
	for (ses=0;ses<300;ses++)
	{
		while(GetTime()-timey < 1000/30)
		{
			font2.drawText(GetScreenWidth()/2-font2.getStringWidth(string)/2, GetScreenHeight()/2-font2.getHeight()/2, string);
			if (ses%60 < 30)
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-255*(ses%30)/30));
			if (ses%60 > 30)
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255*(ses%30)/30));
			FlipScreen();
			
			if (IsMouseButtonPressed(MOUSE_LEFT) || IsMouseButtonPressed(MOUSE_RIGHT))
			{
				fire = true
				ses = 300;
				break;
			}
		}
		timey = GetTime();
	}
	FlipScreen();
	if (fire)
	{
		PlaySound(9);
		timey = GetTime();
		while(GetTime()-timey < 200);
		end_images[33].blit(0,0);
		FlipScreen();
		timey = GetTime();
		while(GetTime()-timey < 4000);
		for (ses=0;ses<60;ses++)
		{
			end_images[33].blit(0,0);
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255*ses/60));
			FlipScreen();
		}
		FlipScreen();
		return true;
	}
	
	return false;
}

function ShowCredits()
{
	var image = LoadImage("ending/credits.png")
	var sfrate = 1000/30;
	var llt = GetTime();
	
	if (g_chosen_diff == 2 && g_current_level > 15)
	{
		
	}
	else
	{
		for (i=0;i<60;i++)
		{
			image.blit(0,0);
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-255*i/60));
			FlipScreen();
			while(GetTime()-llt < sfrate);
			llt = GetTime();
		}

		for (i=0;i<240;i++)
		{
			image.blit(0,0);
			FlipScreen();
			while(GetTime()-llt < sfrate);
			llt = GetTime();
		}

		for (i=0;i<60;i++)
		{
			image.blit(0,0);
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255*i/60));
			FlipScreen();
			while(GetTime()-llt < sfrate);
			llt = GetTime();
		}
	}
	

	var mins=Math.floor(g_total_time/60)%60;
	var hours=Math.floor(g_total_time/3600);
	var secs=Math.floor(g_total_time)%60;
	
	if (hours > 9)
	{
		hours = 9;
		mins = 59;
		secs = 59;
	}
	
	var string = hours+":";
	

	if (mins<10)
	  string+= "0"+mins+":";
	else
	  string+= ""+mins+":";

	if (secs<10)
		string += "0"+secs;
	else
		string += ""+secs;
	
	var stringy1 = "Gore Score: "+gore_score;
	var stringy2 = "Time Spent: "+string;
	var stringy3 = "Kills: "+g_total_kills;
	
		
	function DrawScores()
	{
		font2.drawText(GetScreenWidth()/2-font.getStringWidth(stringy1)/2, 110-4, stringy1);
		font2.drawText(GetScreenWidth()/2-font.getStringWidth(stringy2)/2, 120-4, stringy2);
		font2.drawText(GetScreenWidth()/2-font.getStringWidth(stringy3)/2, 130-4, stringy3);
	}
	
	for (i=0;i<30;i++)
	{
		DrawScores();
		Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-255*i/30));
		FlipScreen();
		while(GetTime()-llt < sfrate);
		llt = GetTime();
	}

	for (i=0;i<160;i++)
	{
		DrawScores();
		FlipScreen();
		while(GetTime()-llt < sfrate);
		llt = GetTime();
	}

	for (i=0;i<30;i++)
	{
		DrawScores();
		Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255*i/30));
		FlipScreen();
		while(GetTime()-llt < sfrate);
		llt = GetTime();
	}
	FlipScreen();
	
	
	if (g_chosen_diff == 2 && g_current_level > 15)
	{
		ShowCredits2();
	}
	
	RestartGame();
	

	//show credits screen
	//show gore score
	//show time spent.
	
	//possible goals, etc.
}

function ShowCredits2()
{
	//hard ending credits
	var images = new Array();
	images[0] = LoadImage("ending/creds1.png");
	images[1] = LoadImage("ending/creds2.png");
	images[2] = LoadImage("ending/creds3.png");

	images[3] = LoadImage("background11.png");
	images[4] = LoadImage("background15.png");
	
	
	var texts = new Array();
	
	texts[0] = "CODE:";
	texts[1] = "  BEAKER";
	texts[2] = "GRAPHICS:";
	texts[3] = "  BEAKER";
	texts[4] = "MUSIC: ";
	texts[5] = "  QUALO";
	texts[6] = "SOUND: ";
	texts[7] = " QUALO,BEAKER";
	texts[8] = "CHARACTERS: BEAKER,";
	texts[9] = " HOPKINS,TURNER";
	texts[10] ="QA: QUALO, TUNG";
	texts[11] =" DRUMSTICK, KENTONA";
	texts[12] = "     CREDITS      ";
	texts[13] = "  THANKS FOR";
	texts[14] = "       PLAYING!"

	var pers = new Array();
	var pers2 = new Array();
	for (i=0;i<12;i++)
	{
		pers[i] = (i+1)/12;
	}
	for (i=0;i<16;i++)
	{
		pers2[i] = (i+1)/16;
	}

	music[6].setVolume(255*music_level);
	music[6].play(false);

	var perc = 0;
	
	var tp, tpl;
	function DrawCredits(num, p,y, booy)
	{
		if (booy == undefined || booy == true)
			Rectangle(0,0,GetScreenWidth(), 60, CreateColor(0,0,0,255));

		if (num == 12)
			FunkyDrawText(20,30+y, texts[num],p);
		else
			FunkyDrawText(5,30+y, texts[num],p);
		
		if (booy == undefined || booy == false)
		{
			if (p<0.25)
				Rectangle(0,0,GetScreenWidth(), 60, CreateColor(0,0,0,255-255*p/0.25));
			else if (p>0.75)
				Rectangle(0,0,GetScreenWidth(), 60, CreateColor(0,0,0,255*(p-0.75)/0.25));
		}
	}


	while(music[6].isPlaying())
	{
		perc = music[6].getPosition()/music[6].getLength();
		if (perc < pers[0])
		{
			//fade in
			images[4].blit(0,0);
			images[0].blit(0,0);
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-255*perc/pers[0]));
		}
		else if (perc < pers[2])
		{
			images[4].blit(0,0);
			images[0].blit(0,0);
		}
		else if (perc < pers[3])
		{
			//fade out
			images[4].blit(0,0);
			images[0].blit(0,0);
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255*(perc-pers[2])/(pers[3]-pers[2])));
		}
		else if (perc < pers[4])
		{
			//fade in
			images[1].blit(0,0);
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-255*(perc-pers[3])/(pers[4]-pers[3])));
		}
		else if (perc < pers[6])
		{
			images[1].blit(0,0);
		}
		else if (perc < pers[7])
		{
			//fade out
			images[1].blit(0,0);
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255*(perc-pers[6])/(pers[7]-pers[6])));
		}
		else if (perc < pers[8])
		{
			//fade in
			images[3].blit(0,0);
			images[2].blit(0,0);
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-255*(perc-pers[7])/(pers[8]-pers[7])));
		}
		else if (perc < pers[10])
		{
			images[3].blit(0,0);
			images[2].blit(0,0);
		}
		else if (perc < pers[11])
		{
			//fade out
			images[3].blit(0,0);
			images[2].blit(0,0);
			Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255*(perc-pers[10])/(pers[11]-pers[10])));
		}
		
		if (perc < pers2[1])
		{
			DrawCredits(12, (pers2[1]-perc)/pers2[1],0);
		}
		else if (perc < pers2[3])
		{
//			DrawCredits(8, (pers[1]-perc)/pers[1],-15,true);
			DrawCredits(0, (pers2[3]-perc)/(pers2[3]-pers2[1]),-15,true);
			DrawCredits(1, (pers2[3]-perc)/(pers2[3]-pers2[1]),15,false);
		}
		else if (perc < pers2[5])
		{
			DrawCredits(2, (pers2[5]-perc)/(pers2[5]-pers2[3]),-15, true);
			DrawCredits(3, (pers2[5]-perc)/(pers2[5]-pers2[3]),15, false);
		}
		else if (perc < pers2[7])
		{
			DrawCredits(4, (pers2[7]-perc)/(pers2[7]-pers2[5]),-15, true);
			DrawCredits(5, (pers2[7]-perc)/(pers2[7]-pers2[5]),15,false);
		}
		else if (perc < pers2[9])
		{
			DrawCredits(6, (pers2[9]-perc)/(pers2[9]-pers2[7]),-15, true);
			DrawCredits(7, (pers2[9]-perc)/(pers2[9]-pers2[7]),15,false);
		}
		else if (perc < pers2[11])
		{
			DrawCredits(8, (pers2[11]-perc)/(pers2[11]-pers2[9]),-15,true);
			DrawCredits(9, (pers2[11]-perc)/(pers2[11]-pers2[9]),15,false);
		}
		else if (perc < pers2[13])
		{
			DrawCredits(10, (pers2[13]-perc)/(pers2[13]-pers2[11]),-15,true);
			DrawCredits(11, (pers2[13]-perc)/(pers2[13]-pers2[11]),15,false);
		}
		else if (perc < pers2[15])
		{
			DrawCredits(13, (pers2[15]-perc)/(pers2[15]-pers2[13]),-15,true);
			DrawCredits(14, (pers2[15]-perc)/(pers2[15]-pers2[13]),15,false);
		}

		//draw the texts here;

		FlipScreen();

	}
	FlipScreen();
}

function FunkyDrawText(x,y,text,p)
{
	var width = 0;
	for (fd=0;fd<text.length;fd++)
	{
		width = fonty.getStringWidth(text.substr(0,fd));
		fonty.drawText(x+width, y+Math.sin(p*30+fd+y)*Math.sin(p*30+fd+y)*5-5-fonty.getHeight()/2, text.charAt(fd));
	}
}


function SupernautEndScene2()
{
	FlipScreen();
	var guy_images = new Array();
	guy_images[0] = LoadSurface("ending/guy1_front_01.png");
	guy_images[1] = LoadSurface("ending/guy1_front_02.png");
	var guy_sounds = new Array();
	for (si=1;si<7;si++)
	{
		guy_sounds[si] = LoadSound("ending/guy_0"+si+".wav");
		if (sound_level >0.1)
			guy_sounds[si].setVolume(255*sound_level);
		else
			guy_sounds[si].setVolume(26);
	}
	var timey = GetTime();
	var frate = 1000/14;
	
	var orbits = new Array();
	
	var orbit_num = 6;
	for (si=1;si<orbit_num+1;si++)
	{
		orbits[si] = new Object();
		orbits[si].angle =Math.random()*2*3.14159;
		orbits[si].speed = si/50+Math.random()*0.2;
		orbits[si].radius1 = si/2+Math.random()*3;
		orbits[si].radius2 = si/2+Math.random()*3;
	}
	
	var blink_frame = 0;
	var blinks=new Array()
	blinks[0] = 0;
	blinks[1] = 100;
	blinks[2] = 240;
	blinks[3] = 150;
	blinks[4] = 75;
	var blink_counter = 75;
		
	function DrawGuy2()
	{
		while(GetTime()-timey < frate);
		timey = GetTime();
		guy_images[1].setAlpha(255);
		guy_images[1].blit(0,20);
		Rectangle(0,0,GetScreenWidth(), GetScreenHeight(),CreateColor(0,0,0,128));
	}
	function DrawGuy()
	{
		while(GetTime()-timey < frate);
		timey = GetTime();

		for (sej=orbit_num;sej > 0;sej--)
		{
			orbits[sej].angle+=orbits[sej].speed;
					
			guy_images[0].setAlpha(255*sej/orbit_num);
			guy_images[0].blit(Math.cos(orbits[sej].angle)*orbits[sej].radius1,
												 Math.sin(orbits[sej].angle)*orbits[sej].radius2+20);
		}
		Rectangle(0,0,GetScreenWidth(), 30,CreateColor(0,0,0,255));
		Rectangle(0,210,GetScreenWidth(), 30,CreateColor(0,0,0,255));
		Rectangle(0,0,GetScreenWidth(), GetScreenHeight(),CreateColor(0,0,0,128));
		if (blink_frame > 0)
		{
			Rectangle(0,0,GetScreenWidth(), blinks[blink_frame], CreateColor(0,0,0,255));
			blink_frame++;
			if (blink_frame > 4)
				blink_frame = 0;
		}
	}
	
	function DrawTimey(amt,overblink)
	{
		for (ses=0;ses<amt;ses++)
		{
			DrawGuy();
			FlipScreen();
			blink_counter--;
			if (blink_counter <0 && overblink != true)
			{
				blink_counter = 50*(1.5+Math.random());
				blink_frame = 1;
			}
		}
	}
	function DrawTimey2(amt)
	{
		for (ses=0;ses<amt;ses++)
		{
			DrawGuy2();
			FlipScreen();
		}
	}

	guy_sounds[1].play(false);
	while(guy_sounds[1].isPlaying() == true);
	
	for (ses=0;ses<14;ses++)
	{
		DrawGuy();
		Rectangle(0,0,GetScreenWidth(),GetScreenHeight(), CreateColor(0,0,0,255-255*ses/14));
		FlipScreen();
		if (ses == 6)
			blink_frame = 1;
		
	}

	for (sek=2;sek<7;sek++)
	{
		guy_sounds[sek].play(false);
		while(guy_sounds[sek].isPlaying() == true)
		{
			DrawTimey(1);
		}
		DrawTimey(14);
	}
	DrawTimey(5,true);
	blink_counter = 10000;
	blink_frame = 1;
	DrawTimey(2,true);
	DrawTimey2(14*3);
	
	for (ses=0;ses<14;ses++)
	{
		DrawGuy2();
		Rectangle(0,0,GetScreenWidth(),GetScreenHeight(), CreateColor(0,0,0,255*ses/14));
		FlipScreen();
	}
	FlipScreen();
	
/*	guy_sounds[7].play(false);
	while(guy_sounds[7].isPlaying() == true)
	{
		FlipScreen();
	}
	*/
}

var g_silence = 0;

function ShowFinalEnding()
{
	sound_level = 0;
	function WaitABit(time)
	{
		var gt = GetTime();
		while(GetTime()-gt < time*2)
		{
			FlipScreen();
		}
	}
	PauseHeart();
	
	g_total_kills+=g_kills;
	var tseconds=g_total_time+g_frames*25/750;
	g_total_time=tseconds;


	StartFunct(18, false);
	//now for the spritesets:
	

	oblit_shield = 0;


	
	//PlayRecording1
	//PlayRecording2...etc.
	
	var ttringy1 = gore_score;
	var ttringy2 = g_total_time;
	var ttringy3 = g_total_kills;
	
	
	PlayingLoop("output17_2.rec");
	WaitABit(1000);
	SetLayerVisible(2, false);
	PlayingLoop("output17_3.rec");
	WaitABit(1000);
	PlayingLoop("output17_4.rec");
	WaitABit(1000);
	entities[g_wipe_active].active = false;
	PlayingLoop("output17_8.rec");
	WaitABit(1000);
	SetLayerVisible(2, false);
	PlayingLoop("output17_9.rec");
	WaitABit(1000);
	SetLayerVisible(2, true);
	PlayingLoop("output17_10.rec");
	WaitABit(1000);
	PlayingLoop("output17_11.rec");
	WaitABit(1000);
	PlayingLoop("output17_13.rec");
	WaitABit(1000);
	g_supernaut_active = -1;
	PlayingLoop("output17_14.rec");
	WaitABit(1000);
	PlayingLoop("output17_16.rec");
	WaitABit(1000);
	//

	
	gore_score = ttringy1;
	g_total_time = ttringy2;
	g_total_kills = ttringy3;



	ShowFinalEnding2();
}


function ShowFinalEnding2()
{
	SetCameraX(147*16+8);
	SetCameraY(69*16+8);
	var last_time = GetTime();
	var hei = 25;

	var musvol = music[1].getVolume();

	for (i=0;i<40;i++)
	{
		music[1].setVolume((40-i)*musvol/40);
		while(GetTime()-last_time < 1000/30);
		last_time = GetTime();
	}
	music[1].stop();

	for (i=0;i<40;i++)
	{
		while(GetTime()-last_time < 1000/30);
		last_time = GetTime();
	}

	for (i=0;i<40;i++)
	{
		RenderMap();
		Rectangle(0,0,GetScreenWidth(), hei, CreateColor(0,0,0,255));
		Rectangle(0,GetScreenHeight()-hei,GetScreenWidth(), hei, CreateColor(0,0,0,255));
		Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-255*i/40));
		FlipScreen();
		while(GetTime()-last_time < 1000/30);
		last_time = GetTime();
	}
	
	//wait a bit
	function DoWait(amt, nomap)
	{
		for (i=0;i<amt;i++)
		{
			if (nomap != true)
				RenderMap();
			Rectangle(0,0,GetScreenWidth(), hei, CreateColor(0,0,0,255));
			Rectangle(0,GetScreenHeight()-hei,GetScreenWidth(), hei, CreateColor(0,0,0,255));
			FlipScreen();
			while(GetTime()-last_time < 1000/30);
			last_time = GetTime();
		}
	}
	function DoTalk(text, nomap)
	{
		var count = 0;
		var count2 = 0;
		while(count<text.length+40)
		{
			count2++;
			if (count2%4==0)
				count++;
			if (nomap != true)
				RenderMap();
			Rectangle(0,0,GetScreenWidth(), hei, CreateColor(0,0,0,255));
			Rectangle(0,GetScreenHeight()-hei,GetScreenWidth(), hei, CreateColor(0,0,0,255));
			if (count<text.length)
				font2.drawText(5,220,text.substr(0,count));
			else
				font2.drawText(5,220,text);
			if (count > text.length+20)
				Rectangle(0,GetScreenHeight()-hei,GetScreenWidth(), hei, CreateColor(0,0,0,255*(count-text.length-20)/20));

			FlipScreen();
			while(GetTime()-last_time < 1000/30);
			last_time = GetTime();
		}
		
	}
	
	DoWait(120);
	DoTalk("Leigh: ...Finally, it's done.");
	DoWait(60);
	DoTalk("...");
	DoWait(60);
	DoTalk("I have a feeling though...");
	DoWait(60);

	for (i=0;i<120;i++)
	{
		RenderMap();
		Rectangle(0,0,GetScreenWidth(), hei, CreateColor(0,0,0,255));
		Rectangle(0,GetScreenHeight()-hei,GetScreenWidth(), hei, CreateColor(0,0,0,255));
		Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255*i/120));
		FlipScreen();
		while(GetTime()-last_time < 1000/30);
		last_time = GetTime();
	}
	DoWait(60,true);
	DoTalk("...It's going to be a long way back.", true);
	DoWait(120,true);


	ShowFinalEnding3();
	//Exit();//????
}

function ShowFinalEnding3()
{
	//show credits
	ShowCredits();
}

function PlayingLoop(file)
{
	LoadPlayKey(file);
	key_play = true;
	g_supernaut_active = 0;
//	naut1.hp = naut
	eat_camera = 0;
	g_upate_input_suspend = false;
	for (i=0;i<max_scripts_commands;i++)
		script_commands[i] = "";
	var framerate = 1000/30;
	var surface;
	var fadein = 0;
	g_recording_pressed = false;
	g_recording = false;
	

	UpdatePlayer();
	
	var start_time = GetTime();
	fadeout = 0;
	fadestart = 100;
	fademax = 150;
	g_frames = 0;
	var frame_skip = 0;
	var flag = 0;
	var gl_last = GetTime();

	cam_xoff = 0
	cam_yoff = 0;

	SetCameraX(entities[0].x+cam_xoff);
	SetCameraY(entities[0].y+cam_yoff);			

	PlayMusic(0);

	StartHeart();
	g_rumble = -1;

	bg_x1 = -(bg11.width-320)*(entities[0].x/(GetLayerWidth(0)*16));
	bg_y1 = -(bg11.height-240)*(entities[0].y/(GetLayerWidth(0)*16));
	bg15_x1 = -(bg15.width-320)*(entities[0].x/(GetLayerWidth(0)*16));
	bg15_y1 = -(bg15.height-240)*(entities[0].y/(GetLayerWidth(0)*16));
	bg18_x1 = -(bg18.width-320)*(entities[0].x/(GetLayerWidth(0)*16));
	bg18_y1 = -(bg18.height-240)*(entities[0].y/(GetLayerWidth(0)*16));

	g_exit_level =-1;
	
	ftimes.start = GetTime();
	ftimes.draw = GetTime();
	ftimes.entities = GetTime();
	ftimes.etc = GetTime();
	
	for (i=1;i<max_weapons_1;i++)
	{
		entities[0]["la"+i] = GetPersonIDLayer(entities[0]["id"+i]);
	}
	
	
	var hei = 25;
	for (ed1=0;ed1<key_array[0].num;ed1++)
	{
			ftimes.start = GetTime();
	
			flag = 0;

			if (oblit_frame > 5)
			{
				SetCameraX(entities[0].x+cam_xoff+(oblit_max-oblit_frame)*Math.random()/10);
				SetCameraY(entities[0].y+cam_yoff+(oblit_max-oblit_frame)*Math.random()/10);
			}
			if (sounds[81].isPlaying() == true)
				g_rumble = 2*(1-sounds[81].getPosition()/sounds[81].getLength());
			else
				g_rumble = -1;


			if ((entities[0].invince > 0 || (fadeout < 75 && entities[0].hp <=0)) && g_frames%2 == 0)
			{
				for (i=1;i<max_weapons_1;i++)
				{
					entities[0]["la"+i] = GetPersonIDLayer(entities[0]["id"+i]);
					SetPersonIDLayer(entities[0]["id"+i], 0);
				}
			}
			DrawBackGroundImage();
			
			if (FixKeyPlay() == true)
			{
				start_time = GetTime()-g_frames*framerate;
			}
			if (file == "output17_4.rec")
				SetCameraY(0);
			
			RenderMap();
			
			if ((entities[0].invince > 0 ||entities[0].hp <=0)&& g_frames%2 == 0)
			{
				for (i=1;i<max_weapons_1;i++)
				{
					SetPersonIDLayer(entities[0]["id"+i], entities[0]["la"+i]);
				}
			}

			DrawRailguns();
			if (oblit_shield > 0)
				DrawOblitShield();
			if (oblit_frame > 0)
				DrawObliterator();
			if (ed1 < 5)
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255));
			else if (ed1 < 25)
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-(ed1-5)*255/20));

			if (ed1 > key_array[0].num-5)
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255));
			else if (ed1>key_array[0].num-25)
				Rectangle(0,0,GetScreenWidth(), GetScreenHeight(), CreateColor(0,0,0,255-(key_array[0].num-ed1-5)*255/20));

			Rectangle(0,0,GetScreenWidth(), hei, CreateColor(0,0,0,255));
			Rectangle(0,GetScreenHeight()-hei,GetScreenWidth(), hei, CreateColor(0,0,0,255));

			FlipScreen();
			
			if (g_frames%ftimes_amount == 0)
				ftimes.draw = GetTime()-ftimes.start;

		while(GetTime()-start_time < g_frames*framerate)
			flag=1;

		if (flag !=1)
		  frame_skip++;
		else
		  frame_skip = 0;
		  
		if (frame_skip>0)
		{
			frame_skip=0;
			start_time = GetTime()-g_frames*framerate;
		}
		g_frames++;
		if (script_commands[(g_frames)%max_scripts_commands] != "")
		{
			DoScript(script_commands[(g_frames)%max_scripts_commands]);
			script_commands[(g_frames)%max_scripts_commands] = "";
		}

		if (entities[0].hp > 0)
			UpdateVariables();


		if (g_rumble<0)
		{
			SetCameraX(entities[0].x+cam_xoff);
			SetCameraY(entities[0].y+cam_yoff);
		}
		else
		{
			SetCameraX(entities[0].x-g_rumble+2*g_rumble*Math.random()+cam_xoff);
			SetCameraY(entities[0].y-g_rumble+2*g_rumble*Math.random()+cam_yoff);
		}

		if (oblit_frame > 6)
		{
			oblit_frame--;
		}
		else
		{
		  if (oblit_frame <1)
		  {
				if (g_rumble<0)
				{
					SetCameraX(entities[0].x+cam_xoff);
					SetCameraY(entities[0].y+cam_yoff);
				}
				else
				{
					SetCameraX(entities[0].x-g_rumble+2*g_rumble*Math.random()+cam_xoff);
					SetCameraY(entities[0].y-g_rumble+2*g_rumble*Math.random()+cam_yoff);
				}

				ftimes.entities_start = GetTime();
				
				UpdateEntities();

				if (g_frames%ftimes_amount == 0)
					ftimes.entities = GetTime()-ftimes.entities_start;
				ftimes.entities_start = GetTime();

				UpdateDoors();
				UpdateGrenades();
				UpdateExplosions();
				UpdateShrapnel();
//				UpdateShrapnel(1);
				//UpdateShrapnel(1);
				UpdateRailguns();
				UpdateBeams();
				
				if (g_frames%ftimes_amount == 0)
					ftimes.etc = GetTime()-ftimes.entities_start;

				if (entities[0].hp > 0)
				{
					var thingy = UpdateInput();
					if (thingy != 0)
					{
						return thingy;
					}
				}
			}
			UpdateCasings();
			UpdateBlood();	

		  if (oblit_frame <= 6 && oblit_frame > 0)
		  {
 				UpdateEntitiesFrames();
				UpdateRailguns();
// 				if (g_frames%8 == 0)
//					oblit_frame--;
				oblit_frame-=0.125;
				start_time = GetTime()-(g_frames-1)*framerate;
		  }
		}
	}
	for (p=1;p<max_weapons_1;p++)
	{
		SetPersonIDLayer(entities[0]["id"+p], entities[0]["la"+p]);
	}
	entities[0].invince = 0;
}

