var doors = new Array()
var max_doors = 25;

for (i=0;i<max_doors;i++)
{
	doors[i] = new Object();
	doors[i].x = blood_dx;
	doors[i].y = 0;
	doors[i].layer = 0;
	doors[i].dir = 0;
	doors[i].frame = 0;
	doors[i].type = 0;
	doors[i].hp = 0;
		//type 1-regular door
		//type 2-switch
		//type 3-switch door
		//type 4-hp door

	doors[i].active = 0;

	doors[i].target = 0;
	doors[i].name = "";
	doors[i].id1 = "";
}

var max_lockers = 33;
var ammolocker = new Array(max_lockers);

for (i=0;i<max_lockers;i++)
{
	ammolocker[i] = new Array(16);
	for (j=0;j<16;j++)
		ammolocker[i][j] = 0;
}


ammolocker[0][0] = 12*3;
ammolocker[0][1] = 7;

ammolocker[1][0] = 0;
ammolocker[1][1] = 0;
ammolocker[1][2] = 32;
ammolocker[1][3] = 0;

ammolocker[2][0] = 0;
ammolocker[2][1] = 0;
ammolocker[2][2] = 0;
ammolocker[2][3] = 50;


ammolocker[3][0] = 0;
ammolocker[3][1] = 0;
ammolocker[3][2] = 0;
ammolocker[3][3] = 0;
ammolocker[3][4] = 150;

ammolocker[4][0] = 0;
ammolocker[4][1] = 0;
ammolocker[4][2] = 0;
ammolocker[4][3] = 0;
ammolocker[4][4] = 50;
ammolocker[4][5] = 5;

ammolocker[5][0] = 0;
ammolocker[5][1] = 0;
ammolocker[5][2] = 0;
ammolocker[5][3] = 0;
ammolocker[5][4] = 0;
ammolocker[5][5] = 0;
ammolocker[5][10] = 2;
ammolocker[5][11] = 3;

ammolocker[6][0] = 48;
ammolocker[6][1] = 8;
ammolocker[6][2] = 0;

ammolocker[7][0] = 0;
ammolocker[7][1] = 0;
ammolocker[7][2] = 0;
ammolocker[7][3] = 0;
ammolocker[7][4] = 0;
ammolocker[7][5] = 0;
ammolocker[7][6] = 8;

ammolocker[8][0] = 0;
ammolocker[8][1] = 0;
ammolocker[8][2] = 0;
ammolocker[8][3] = 25;
ammolocker[8][4] = 100;
ammolocker[8][5] = 2;
ammolocker[8][6] = 2;
ammolocker[8][10] = 2;
ammolocker[8][11] = 3;

ammolocker[9][0] = 0;
ammolocker[9][1] = 0;
ammolocker[9][2] = 0;
ammolocker[9][3] = 0;
ammolocker[9][4] = 0;
ammolocker[9][5] = 0;
ammolocker[9][6] = 0;
ammolocker[9][7] = 50;

ammolocker[10][0] = 0;
ammolocker[10][1] = 0;
ammolocker[10][2] = 0;
ammolocker[10][3] = 25;
ammolocker[10][4] = 100;
ammolocker[10][5] = 2;

ammolocker[11][0] = 0;
ammolocker[11][1] = 0;
ammolocker[11][2] = 0;
ammolocker[11][3] = 25;
ammolocker[11][10] = 2;
ammolocker[11][11] = 4;

ammolocker[12][0] = 252;
ammolocker[12][1] = 20;

ammolocker[13][2] = 150;
ammolocker[13][3] = 200;

ammolocker[14][4] = 300;
ammolocker[14][5] = 25;

ammolocker[15][6] = 25;
ammolocker[15][7] = 150;

ammolocker[16][8] = 100;

ammolocker[17][10] = 10;
ammolocker[17][11] = 2;

ammolocker[18][12] = 1000;

ammolocker[19][9] = 10;

ammolocker[20][8] = 35;

ammolocker[21][9] = 2;
ammolocker[21][10] = 4;
ammolocker[21][11] = 5;













ammolocker[22][0] = 48;
ammolocker[22][1] = 2;

ammolocker[23][2] = 18;

ammolocker[24][3] = 15;

ammolocker[25][4] = 25;
ammolocker[25][5] = 3;

ammolocker[26][6] = 3;

ammolocker[27][7] = 20;

ammolocker[28][8] = 10;

ammolocker[29][9] = 1;

ammolocker[30][10] = 2;
ammolocker[30][11] = 3;




ammolocker[31][10] = 2;
ammolocker[31][11] = 3;

ammolocker[32][0] = 0;
ammolocker[32][1] = 0;
ammolocker[32][2] = 0;
ammolocker[32][3] = 0;
ammolocker[32][4] = 150;
ammolocker[32][5] = 5;






var g_switch_rangex = 32;
var g_switch_rangey = 32;

function HitSwitch(x,y)
{
	var flagy = false;
	for (hs=0;hs<max_doors;hs++)
	{
		if (Math.abs(x-doors[hs].x) < g_switch_rangex &&Math.abs(y-doors[hs].y) < g_switch_rangey)
		{
			if (doors[hs].type == 1 && doors[hs].active ==0)
			{
				doors[hs].active = (doors[hs].active+1)%2;
				
				doors[hs].dir = "closed1";
				doors[hs].frame = 0;
				PlaySound(39);

				SetPersonIDDirection(doors[hs].id1, doors[hs].dir);
				SetPersonIDFrame(doors[hs].id1, doors[hs].frame);
				flagy = true;
			}
			else if (doors[hs].type == 2 && doors[hs].active == 0)
			{
				doors[hs].dir = "on1";
				doors[hs].frame = 0;
				SetPersonIDDirection(doors[hs].id1, doors[hs].dir);
				SetPersonIDFrame(doors[hs].id1, doors[hs].frame);

				PlaySound(38);

				doors[hs].active = (doors[hs].active+1)%2;
				
				doors[doors[hs].target].dir = "closed1";
				doors[doors[hs].target].frame = 0;
				doors[doors[hs].target].active = 1;

				SetPersonIDDirection(doors[doors[hs].target].id1, doors[doors[hs].target].dir);
				SetPersonIDFrame(doors[doors[hs].target].id1, doors[doors[hs].target].frame);
				flagy = true;
			}
			else if (doors[hs].type == 5)
			{
				AccessTerminal(doors[hs].target);
				flagy = true;
			}
			else if (doors[hs].type == 6 && doors[hs].active == 0)
			{
				doors[hs].active = 1;
				AccessLocker(doors[hs].target);
				PlaySound(40);
				
				doors[hs].dir = "open1";
				doors[hs].frame = 0;
				SetPersonIDDirection(doors[hs].id1, doors[hs].dir);
				SetPersonIDFrame(doors[hs].id1, doors[hs].frame);
				flagy = true;
			}
		}
	}
	
	return flagy;
}


function UpdateDoors()
{
	var t_layer;
	
	for (i=0;i<max_doors;i++)
	{
		if ((doors[i].spriteset == "locker2" || doors[i].spriteset == "locker3")&& doors[i].dir =="closed1"&&g_frames%4 == 0)
		{
			doors[i].frame++;
			if (doors[i].frame%4 == 0)
			{
				doors[i].frame = 4*(Math.floor(Math.random()*2+Math.floor(doors[i].frame/4))%3);
			}
			SetPersonIDDirection(doors[i].id1, doors[i].dir);
			SetPersonIDFrame(doors[i].id1, doors[i].frame);
			
		}
		if (doors[i].type != 2 && g_frames%2 == 0)
		{
			if (doors[i].active == true && doors[i].dir !="open1")
			{
				doors[i].frame++;

				if ((doors[i].frame == 3 && doors[i].spriteset != "door1b")||(doors[i].frame == 1 && doors[i].spriteset == "door1b"))
				{
					var tdx = Math.floor(doors[i].x/GetTileWidth());
					var tdy = Math.floor(doors[i].y/GetTileWidth());
					
					if (GetTile(tdx,tdy, 0) == 0 && doors[i].type != 4)
					{
						if (doors[i].spriteset!="door5")
						{
							SetTile(tdx,tdy, 0, 4);
							SetTile(tdx,tdy+1, 0, 4);
						}
						//if any entities are obstructed by this, hold off

						for (den=0;den<max_entities;den++)
						{
							if ((entities[den].type > 0&& entities[den].hp > 0)|| den == 0)
							{
								
								t_layer = GetPersonIDLayer(entities[den].id1);
								SetPersonIDLayer(entities[den].id1, 0);
								if (IsPersonIDObstructed(entities[den].id1, entities[den].x, entities[den].y))
								{
									if (doors[i].spriteset!="door5")
									{
										SetTile(tdx,tdy, 0, 0);
										SetTile(tdx,tdy+1, 0, 0);
									}
									if (!IsPersonIDObstructed(entities[den].id1, entities[den].x, entities[den].y))
									{
										//keep it open
										doors[i].frame--;
										SetPersonIDLayer(entities[den].id1, t_layer);
										break;
									}
									else if (doors[i].spriteset != "door5")
									{
										SetTile(tdx,tdy, 0, 4);
										SetTile(tdx,tdy+1, 0, 4);
									}
								}
								SetPersonIDLayer(entities[den].id1, t_layer);
							}
						}
					}
					else if (doors[i].spriteset != "door5")
					{
						SetTile(tdx,tdy, 0, 0);
						SetTile(tdx,tdy+1, 0, 0);
					}
				}

				if (doors[i].spriteset == "door1b" && doors[i].permclose == true && doors[i].frame == 4)
				{
					doors[i].active = false;
					doors[i].dir = "open1";
					doors[i].frame=0;
				}
				else if (doors[i].frame == 5 && doors[i].spriteset != "door4" && doors[i].permclose != true)
				{
					doors[i].dir = "open1";
					doors[i].frame=0;
				}
				else if (doors[i].frame == 8 && doors[i].spriteset == "door4" && doors[i].permclose != true)
				{
					doors[i].dir = "open1";
					doors[i].frame = 0;
				}

				SetPersonIDDirection(doors[i].id1, doors[i].dir);
				SetPersonIDFrame(doors[i].id1, doors[i].frame);
			}
		}
	}
}






function AccessLocker(num)
{
	for (li=0;li<max_ammos;li++)
	{
	  amt = ammolocker[num][li];
	  
	  if (g_chosen_diff == 2 && g_current_level >= 0)
	  {
			if (amt > 0)
			{
				if (li == 12)
					amt = Math.floor(amt*2/3+1)
				else
					amt = Math.floor(amt/2+1)
			}
			else
				amt = 0;
	  }
	  
	  if (ammo_array[li].curclip < ammo_array[li].maxclip && li != 10 && li != 11)
	  {
			if (amt < ammo_array[li].maxclip-ammo_array[li].curclip)
			{
				ammo_array[li].curclip+=amt;
				amt = 0;
			}
			else
			{
				amt-=ammo_array[li].maxclip-ammo_array[li].curclip;
				ammo_array[li].curclip=ammo_array[li].maxclip;
			}
	  }

		if (ammolocker[num][li] > 0)
			AddAmmoHudText(li, ammolocker[num][li]);
		ammo_array[li].curammo+=amt;

		if (ammo_array[li].curammo > ammo_array[li].maxammo)
		  ammo_array[li].curammo = ammo_array[li].maxammo;
	}
	
	if (ammo_array[2].curammo > 0|| ammo_array[2].curclip>0)
	{ 
		if (weap_possess[1] == 0)
			AddAmmoHudText(2, 1,"Obtained Magnum!");
		weap_possess[1] = 1;
	}
	if (ammo_array[3].curammo > 0|| ammo_array[3].curclip>0) 
	{ 
		if (weap_possess[2] == 0)
			AddAmmoHudText(2, 1,"Obtained Shotgun!");
		weap_possess[2] = 1;
	}

	if (ammo_array[4].curammo > 0|| ammo_array[4].curclip>0) 
	{ 
		if (weap_possess[3] == 0)
			AddAmmoHudText(2, 1,"Obtained Assault Rifle!");
		weap_possess[3] = 1;
	}

	if (ammo_array[6].curammo > 0|| ammo_array[6].curclip>0) 
	{ 
		if (weap_possess[4] == 0)
			AddAmmoHudText(2, 1,"Obtained Rocket Launcher!");
		weap_possess[4] = 1;
	}

	if (ammo_array[7].curammo > 0|| ammo_array[7].curclip>0) 
	{ 
		if (weap_possess[5] == 0)
			AddAmmoHudText(2, 1,"Obtained Railgun!");
		weap_possess[5] = 1;
	}

	if (ammo_array[8].curammo > 0|| ammo_array[8].curclip>0) 
	{ 
		if (weap_possess[6] == 0)
			AddAmmoHudText(2, 1,"Obtained Shrapnel Gun!");
		weap_possess[6] = 1;
	}

	if (ammo_array[9].curammo > 0 || ammo_array[9].curclip>0) 
	{ 
		if (weap_possess[7] == 0)
			AddAmmoHudText(2, 1,"Obtained Obliterator!");
		weap_possess[7] = 1;
	}
	//expanded weapons
	//chaingun
	if (ammo_array[12].curammo > 0|| ammo_array[12].curclip>0) 
	{ 
		if (weap_possess[10] == 0)
			AddAmmoHudText(2, 1,"Obtained Chaingun!");
		weap_possess[10] = 1;
	}
}


function CloseDoor(name)
{
	for (hs=0;hs<max_doors;hs++)
	{
		if (doors[hs].name == name)
		{
			doors[hs].active = true;
			doors[hs].permclose = true;
			
			doors[hs].dir = "closed1";
			doors[hs].frame = 0;
			PlaySound(39);

			SetPersonIDDirection(doors[hs].id1, doors[hs].dir);
			SetPersonIDFrame(doors[hs].id1, doors[hs].frame);
			return;
		}
	}
}

function CloseNameDoor(name, obs)
{
	var tdx;
	var tdy;

	for (hs=0;hs<max_doors;hs++)
	{
		if (doors[hs].name == name)
		{
			tdx = Math.floor(doors[hs].x/GetTileWidth());
			tdy = Math.floor(doors[hs].y/GetTileWidth());
			
			if (obs != false)
			{
				SetTile(tdx,tdy, 0, 4);
				SetTile(tdx,tdy+1, 0, 4);
			}

			doors[hs].active = true;
			doors[hs].permclose = true;
			
			doors[hs].dir = "closed2";
			doors[hs].frame = 0;

			SetPersonIDDirection(doors[hs].id1, doors[hs].dir);
			SetPersonIDFrame(doors[hs].id1, doors[hs].frame);
			return;
		}
	}
}


function OpenNameDoor(name, obs, sound)
{
	var tdx,tdy;
					
	for (ond = 0;ond<max_doors;ond++)
	{
		if (doors[ond].name == name)
		{
			if (doors[ond].spriteset == "door5" && sound == true)
				PlaySound(126);
		
			tdx = Math.floor(doors[ond].x/GetTileWidth());
			tdy = Math.floor(doors[ond].y/GetTileWidth());
			
			if (obs != false)
			{
				SetTile(tdx,tdy, 0, 0);
				SetTile(tdx,tdy+1, 0, 0);
			}
			
				doors[ond].dir = "closed1";
				doors[ond].permclose = false;
				doors[ond].frame = 0;
				doors[ond].active = 1;

				SetPersonIDDirection(doors[ond].id1, doors[ond].dir);
				SetPersonIDFrame(doors[ond].id1, doors[ond].frame);
				return;

		}
	}
}