var g_recording_pressed = false;
var g_recording = false;

var g_recording_key = KEY_F9;
var image_record = false
function RecordNow()
{
	var string = "";
	
	if (g_frames < 10)
		string = "000000";
	else if (g_frames < 100)
		string = "00000";
	else if (g_frames < 1000)
		string = "0000";
	else if (g_frames < 10000)
		string = "000";
	else if (g_frames < 100000)
		string = "00";
	else if (g_frames < 1000000)
		string = "0";
	else
		string = "";
		
	GrabSurface(0,0,GetScreenWidth(), GetScreenHeight()).save("recording/grab_level"+g_current_level+"_"+string+""+g_frames+".png");
}

var iframe = 0;
var intro_record = 0;
function IntroRecord()
{
	if (intro_record)
	{
		var string = "";
		
		if (iframe < 10)
			string = "000000";
		else if (iframe < 100)
			string = "00000";
		else if (iframe < 1000)
			string = "0000";
		else if (iframe < 10000)
			string = "000";
		else if (iframe < 100000)
			string = "00";
		else if (iframe < 1000000)
			string = "0";
		else
			string = "";
	
		GrabSurface(0,0,GetScreenWidth(), GetScreenHeight()).save("recording/grab_intro2"+"_"+string+""+iframe+".png");
		iframe++;
	}
}






var key_record = false;
var key_play = false;
var key_array = new Array();
for (i=0;i<300;i++)
	key_array[i] = new Object();
var keyframe = 0;
var frame_st = undefined;

function KeyRecordNow()
{
	if (frame_st == undefined)
		frame_st = g_frames;
	var fra = g_frames-frame_st;

	if (fra == 0 && g_current_level == 4)
		WipeStart();


	if (key_array[fra] == undefined)
		key_array[fra] = new Object();
	key_array[fra].ex = Math.floor(entities[0].x);
	key_array[fra].ey = Math.floor(entities[0].y);
	key_array[fra].mx = GetMouseX();
	key_array[fra].my = GetMouseY();
	key_array[fra].vx = entities[0].vx;
	key_array[fra].vy = entities[0].vy;
	key_array[fra].curr_weap = cur_weap;
	key_array[fra].frame2 = entities[0].frame2;
	key_array[fra].frame1 = entities[0].frame1;
	key_array[fra].dir = entities[0].dir;
	key_array[fra].dir1 = entities[0].dir1;
	key_array[fra].dir2 = entities[0].dir2;
	key_array[fra].dir3 = GetPersonDirection("main_legs");
	key_array[fra].run = g_isrunning;
	key_array[fra].hs = 0;
	
	if (fra == 0)
	{
		key_array[0].frame_st =frame_st;
	}
}

function KeySaveRecord()
{
	//wipe the old file.

	var file = OpenFile("output"+g_current_level+".rec");
	
	file.write("num", key_array.length);
	file.write("level", g_current_level);
	file.write("ka"+0+"_fs_", key_array[0].frame_st);
	for (ks=0;ks<key_array.length;ks++)
	{
		file.write("ka"+ks+"_x_", Math.floor(key_array[ks].ex));
		file.write("ka"+ks+"_y_", Math.floor(key_array[ks].ey));
		file.write("ka"+ks+"_vx_", key_array[ks].vx);
		file.write("ka"+ks+"_vy_", key_array[ks].vy);
		file.write("ka"+ks+"_wp_", key_array[ks].curr_weap);
		file.write("ka"+ks+"_f1_", key_array[ks].frame1);
		file.write("ka"+ks+"_f2_", key_array[ks].frame2);
		file.write("ka"+ks+"_d_", key_array[ks].dir);
		file.write("ka"+ks+"_d1_", key_array[ks].dir1);
		file.write("ka"+ks+"_d2_", key_array[ks].dir2);
		file.write("ka"+ks+"_d3_", key_array[ks].dir3);
		file.write("ka"+ks+"_rn_", key_array[ks].run);

		file.write("ka"+ks+"_mx_", key_array[ks].mx);
		file.write("ka"+ks+"_my_", key_array[ks].my);
		file.write("ka"+ks+"_hs_", key_array[ks].hs);
	}
	frame_st = undefined;
	
	file.flush();
	file.close();
}

function LoadPlayKey(string)
{
	if (string == undefined)
		var file = OpenFile("output"+g_current_level+".rec");
	else
	{
		var file = OpenFile(string);
	}
		
	
	var num = file.read("num", 1);
	
	for (ks=0;ks<num;ks++)
	{
		if (key_array[ks] == undefined)
			key_array[ks] = new Object();
		key_array[ks].ex = Math.floor(file.read("ka"+ks+"_x_", 0));
		key_array[ks].ey = Math.floor(file.read("ka"+ks+"_y_", 0));
		
		key_array[ks].mx = file.read("ka"+ks+"_mx_", 0);
		key_array[ks].my = file.read("ka"+ks+"_my_", 0);

		key_array[ks].vx = file.read("ka"+ks+"_vx_", 1.0);
		key_array[ks].vy = file.read("ka"+ks+"_vy_", 1.0);
		key_array[ks].curr_weap = file.read("ka"+ks+"_wp_", 0);
		key_array[ks].frame2 = file.read("ka"+ks+"_f2_", 0);
		key_array[ks].frame1 = file.read("ka"+ks+"_f1_", 0);
		key_array[ks].dir = file.read("ka"+ks+"_d_", 0);
		key_array[ks].dir1 = file.read("ka"+ks+"_d1_", "idle");
		key_array[ks].dir2 = file.read("ka"+ks+"_d2_", "idle");
		key_array[ks].dir3 = file.read("ka"+ks+"_d3_", "idle");
		key_array[ks].run = file.read("ka"+ks+"_rn_", 0);
		key_array[ks].hs = file.read("ka"+ks+"_hs_", 0);
	}
	key_array[0].frame_st = file.read("ka"+0+"_fs_", 0);
	key_array[0].num = file.read("num", 0);
	key_array[0].level = file.read("level", 0);
	
	g_frames=key_array[0].frame_st;
}

function KeyPlayNow()
{
	var fra = g_frames-key_array[0].frame_st;

	if (fra >= key_array.length || fra >=key_array[0].num)
	{
		key_play = false;
		return;
	}
	if (fra < 0)
	{
		g_frames = key_array[0].frame_st;
		frame_st = g_frames;
		fra = 0;
	}/*
	font.drawText(0,0,fra);
	FlipScreen();
	GetKey();
*/
	if (fra==0 && g_current_level==4)
	{
		WipeStart();
	}

	if (key_array[fra].ex > 20)
	{
		entities[0].x=key_array[fra].ex;
		entities[0].y=key_array[fra].ey;
		entities[0].vx=key_array[fra].vx;
		entities[0].vy=key_array[fra].vy;
		cur_weap=key_array[fra].curr_weap;
		entities[0].frame2=key_array[fra].frame2;
		entities[0].frame1=key_array[fra].frame1;
		entities[0].dir=key_array[fra].dir;
		entities[0].dir1=key_array[fra].dir1;
		entities[0].dir2=key_array[fra].dir2;
		g_isrunning=key_array[fra].run;
		if (key_array[fra].hs == 1)
			HitSwitch(entities[0].x, entities[0].y);
		SetMousePosition(key_array[fra].mx,key_array[fra].my);
	}
}


function FixKeyPlay()
{
	if (key_play == true)
	{
		var fra = g_frames-key_array[0].frame_st;
		if (fra < 0)
		{
			g_frames = key_array[0].frame_st;
			frame_st = g_frames;
			fra = 0;
		}

		if (key_array[fra].dir3 != "undefined")
		{
			entities[0].frame2 = key_array[fra].frame2;
			SetPersonDirection("main_legs", key_array[fra].dir3);
			SetPersonFrame("main_legs", entities[0].frame2);
			if (fra== 0)
				return true;
		}
	}
	return false;
}
