var max_map_zones = 64;
var map_zones = new Array(max_map_zones);
for (i=0;i<max_map_zones;i++)
{
	map_zones[i] = 0;//untriggered
}

var text_window = LoadWindowStyle("window1.rws");

function CurrentZoneAvailable()
{
	if (map_zones[GetCurrentZone()] == 1)
	  return false;
	return true;
}

function StartZone()
{
	map_zones[GetCurrentZone()] = 1;
}

function EnemyAmbush(name, numy, type, override_rise)
{
  var mask = CreateColor(255,255,255,255);
	var tile1 = 179;
	var tile2 = 184;
	var layer = 2;
	if (type == 2)
	{
		tile1 = 384;
		tile2 = 389;
	}	
	else if (type == 6)
	{
		tile1 = 2044;
		tile2 = 2049;
		layer = 1;
	}	
	
	
	for (eai = 0;eai<max_entities;eai++)
	{
		if (numy != undefined)
		{
			eai = numy;
		}
		
		if ((entities[eai].name == name || numy != undefined) && entities[eai].type == 8 && type != 4)
		{
			entities[eai].ambush = false;
			entities[eai].stop = false;
			entities[eai].hp = entities[eai].maxhp;
			entities[eai].hp1 = entities[eai].maxhp1;
			entities[eai].hp2 = entities[eai].maxhp2;
			entities[eai].hp3 = entities[eai].maxhp3;
			if (type != 4)
				entities[eai].nohit = true;
			else
				entities[eai].nohit = false;
			SetPersonIDX(entities[eai].id1, entities[eai].x);
			SetPersonIDX(entities[eai].id2, entities[eai].x);
			SetPersonIDX(entities[eai].id3, entities[eai].x);
			SetPersonIDY(entities[eai].id1, entities[eai].y);
			SetPersonIDY(entities[eai].id2, entities[eai].y);
			SetPersonIDY(entities[eai].id3, entities[eai].y);
			SetPersonIDLayer(entities[eai].id1, entities[eai].layer);
			SetPersonIDLayer(entities[eai].id2, entities[eai].layer);
			SetPersonIDLayer(entities[eai].id3, entities[eai].layer);
			
			UpdateEntityFrame(eai);
			break;
		}
		else if ((entities[eai].name == name || numy != undefined)&& (entities[eai].type != 7 || entities[eai].sight == 2||override_rise))
		{
			entities[eai].stop = false;
			entities[eai].ambush = false;
			entities[eai].hp = entities[eai].maxhp;
			entities[eai].hp1 = entities[eai].maxhp1;
			entities[eai].hp2 = entities[eai].maxhp2;
			entities[eai].hp3 = entities[eai].maxhp3;

			
			var etx = Math.floor(entities[eai].x/GetTileWidth());
			var ety = Math.floor(entities[eai].y/GetTileHeight());
			
			if (type != 4)
			{
				SetTile(etx,ety, layer, tile1);
				SetTile(etx,ety+1, layer, tile2);
				
				PlaySound(41,true);
				sounds[53].stop();
				sounds[54].stop();
		
				if (g_current_level < 16 || Math.random()<0.5)
				{
					SpawnGuts(-2,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
					SpawnGuts(-2,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
					SpawnGuts(-2,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
					SpawnGuts(-1,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
					SpawnGuts(-1,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
				}
			}
			entities[eai].dir2 = "idle";
			
			SetPersonIDX(entities[eai].id1, entities[eai].x);
			SetPersonIDX(entities[eai].id2, entities[eai].x);
			SetPersonIDX(entities[eai].id3, entities[eai].x);
			SetPersonIDY(entities[eai].id1, entities[eai].y);
			SetPersonIDY(entities[eai].id2, entities[eai].y);
			SetPersonIDY(entities[eai].id3, entities[eai].y);
			SetPersonIDLayer(entities[eai].id1, entities[eai].layer);
			SetPersonIDLayer(entities[eai].id2, entities[eai].layer);
			SetPersonIDLayer(entities[eai].id3, entities[eai].layer);
			
			UpdateEntityFrame(eai);
			
			break;
		}
		else if ((entities[eai].name == name || numy != undefined) &&entities[eai].type == 7)
		{
			entities[eai].stop = false;
			entities[eai].ambush = false;
			entities[eai].hp = entities[eai].maxhp;
			entities[eai].hp1 = entities[eai].maxhp1;
			entities[eai].hp2 = entities[eai].maxhp2;
			entities[eai].hp3 = entities[eai].maxhp3;

			
			var etx = Math.floor(entities[eai].x/GetTileWidth());
			var ety = Math.floor(entities[eai].y/GetTileHeight());
			
//			SetTile(etx,ety, 2, 179);
//			SetTile(etx,ety+1, 2, 184);
			if (type != 4)
			{
			entities[eai].dir2 = "rise";
			entities[eai].frame2 = 0;
//			PlaySound(41,true);
			sounds[53].stop();
			sounds[54].stop();


			SpawnGuts(-2,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-2,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-2,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-1,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-1,entities[eai].x,entities[eai].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			}
			UpdateEntityFrame(eai);
			break;
		}
	}

	if (type == 3 && numy > 0)
	{
		var fetx = Math.floor(entities[eai].x/GetTileWidth());
		var fety = Math.floor(entities[eai].y/GetTileHeight());
			
		SetTile(fetx,fety-1, layer, 735);
		SetTile(fetx,fety, layer, 740);
		SetTile(fetx,fety+1, layer, 745);
		SetTile(fetx+1,fety-1, layer, 736);
		SetTile(fetx+1,fety, layer, 741);
		SetTile(fetx+1,fety+1, layer, 746);
	}
	if (type == 5 && numy > 0)
	{
		var fetx = Math.floor(entities[eai].x/GetTileWidth());
		var fety = Math.floor(entities[eai].y/GetTileHeight());
			
		SetTile(fetx,fety-1, layer, 1993);
		SetTile(fetx,fety, layer, 1998);
		SetTile(fetx,fety+1, layer, 2003);
		SetTile(fetx+1,fety-1, layer, 1994);
		SetTile(fetx+1,fety, layer, 1999);
		SetTile(fetx+1,fety+1, layer, 2004);
	}
}

function ShowTutorial(num)
{
  var string;

	while(AreKeysLeft())
	GetKey();

  if (num == 0)
		string = "Welcome to the RTD:Evil Tutorial.\nUse the "+inputs[CKEY_A].toUpperCase()+" and "+inputs[CKEY_D].toUpperCase()+" keys to make Leigh move left or right.\nPress SPACE or "+inputs[CKEY_F].toUpperCase()+" to continue.";
  else if (num == 1)
    string = "When coming up to a door, or other useable object, press "+inputs[CKEY_F].toUpperCase()+" to have Leigh open it.";
  else if (num == 2)
    string = "When confronted with obstacles, you can jump over them by pressing "+inputs[CKEY_W].toUpperCase()+".\n\nJump over these crates to continue.";
  else if (num == 16)
    string = "When "+inputs[CKEY_SHIFT].toUpperCase()+" is pressed, Leigh will try to sprint.  While sprinting, his stamina (the green bar in the hud) will decrease.  When it runs out, he'll stop his sprint.";		
  else if (num == 3)
    string = "Leigh will receive damage to his stamina and hp (the red bar in the hud) if he falls too far.  Split up your fall ahead to avoid damage.";
  else if (num == 26)
    string = "These barrels explode when shot.  Aim the mouse at the barrels ahead and click the "+mouseputs[CMOUSE_LEFT]+" MOUSE BUTTON to shoot it."
  else if (num == 30)
    string = "Remember, stand back to avoid the explosion.";
  else if (num == 10)
    string = "Jump next to the switch above and press "+inputs[CKEY_F].toUpperCase()+" to activate it, opening the door.  Shooting it will also active it.";
  else if (num == 5)
    string = "Certain doors, like the one ahead, need to be blown open with an explosion.  Fortunately, there's some barrels in front of this one.";
  else if (num == 6)
    string = "Reworks are very resistant to damage.  Open the door and aim for the enemy's HEAD to do more damage.";
  else if (num == 11)
    string = "Remember that Leigh shoots in a line from himself to the mouse cursor.  Aiming in front of or behind an enemy will work as well.";
  else if (num == 12)
    string = "While Leigh will automatically reload, you can manually reload by pressing "+inputs[CKEY_E].toUpperCase()+" before confronting enemies.";
  else if (num == 7)
    string = "Use "+inputs[CKEY_F].toUpperCase()+" to open lockers like the one ahead.  Reworks don't use guns, so they'll be your main source of ammo and armour.";
  else if (num == 8)
    string = "All weapons have an alternative fire mode which can be used by pressing the "+mouseputs[CMOUSE_RIGHT].toUpperCase()+" MOUSE BUTTON.";
  else if (num == 13)
    string = "You now have some grenades.  Use the "+mouseputs[CMOUSE_RIGHT].toUpperCase()+" MOUSE BUTTON to throw a grenade and kill the enemy ahead.";
  else if (num == 9)
    string = "Reworks will often try to ambush you.  When you hear their moans, be on the lookout.";
  else if (num == 17)
    string = "At the bottom right of the screen is the adrenaline count and the adrenaline meter.  Pressing "+inputs[CKEY_Q].toUpperCase()+" will use it.";
  else if (num == 18)
    string = "While under its influence, Leigh will recover some of his health and he will not consume stamina when sprinting.";
  else if (num == 42)
    string = "In addition, Leigh will be a little bit more precise with his aim, giving a slight damage bonus to his shots.";
  else if (num == 19)
    string = "After it wears off however, his stamina will be drained and he won't be able to sprint or use adrenaline again until the meter refills.";
  else if (num == 14)
    string = "Use "+inputs[CKEY_F].toUpperCase()+" to access a computer console.  They will allow you to talk to Davis and get valuable information and help.";		
  else if (num == 15)
    string = "Access the console ahead to continue.";		
  else if (num == 20)
    string = "The middle section of the hud shows your current ammo levels for weapons 1 through 8, while the first slot shows your level of armour.";
  else if (num == 21)
    string = "For each weapon which stores more than one shot between reloads, the right half of the meters will show your current clip for that weapon.";
  else if (num == 22)
    string = "For some weapons (like the pistol) the ammo for the alt-fire is different.  For this, there is a small meter on the right end of the slot."
  else if (num == 23)
    string = "You'll need a running start to get past this gap.  From the left end of this platform, sprint right and jump to make it to the other side."; 
  else if (num == 24)
    string = "Keep in mind though, grenades and other explosives can hurt you if you're too close to the blast.";
  else if (num == 25)
    string = "Your stamina will recover over time, and will regenerate faster if you stand still for a few moments.  Health regeneration will be explained later.";
	else if (num == 4)
		string = "When a barrel is not against a wall, Leigh can push it and roll it around.  Rolling these barrels into enemies will make them explode.";
	else if (num == 27)
		string = "The swing of the sword is very powerful.\nIt's alt-fire shoots off a beam of energy, but is less powerful than the normal swing.";
	else if (num == 28)
		string = "In addition, while under adrenaline, Leigh's sword attacks will do twice as much damage!";
	else if (num == 29)
		string = "There seems to be something inside this orb...";
	else if (num == 31)
		string = "Push the barrel ahead onto the unsuspecting rework below.";
	else if (num == 32)
		string = "You don't have any adrenaline at the moment, but you can find some in certain lockers in the future.";
	else if (num == 33)
		string  = "Wave completed.  Please exit combat area while the next phase is being prepared.\n\nPress SPACE or "+inputs[CKEY_F].toUpperCase()+" to continue.";
	else if (num == 34)
		string  = "All keys listed in this tutorial can be changed in the CONFIGURATION section of the main menu.  You can acess this menu by pressing "+inputs[CKEY_ESCAPE].toUpperCase()+".";
	else if (num == 35)
		string  = "The CONFIGURATION menu also has an option for MOUSE MANAGEMENT, which will let RTD:E capture the mouse.  This allows adjustable mouse sensitivity.";
	else if (num == 36)
		string  = "If at any time you wish to regain control of the mouse (for example, switching to a different window), just press "+inputs[CKEY_ESCAPE].toUpperCase()+" to go to the main menu.";
	else if (num == 37)
		string  = "The GORE number in the top right of the hud is your current GORE SCORE.  This increases with each gorey action you perform."
	else if (num == 38)
		string  = "The number above the GORE score is your score multiplier.  With each gorey action you do, it will also increase.";
	else if (num == 39)
		string  = "If you go for a while without doing something gorey however, the gore multiplier will decrease.  Keep up a long streak to maximize your score!";
	else if (num == 40)
		string  = "Combat session complete!  No more enemies left to defeat.  Press "+inputs[CKEY_ESCAPE].toUpperCase()+" to go back to the main menu.\nGore Score: "+gore_score;
	else if (num == 41)
		string  = "Combat session complete!  Play through the single player game to unlock more enemies.  Press "+inputs[CKEY_ESCAPE].toUpperCase()+" to go back to the main menu.";
  else if (num == 43)
    string = "Note that using adrenaline is the only way to recover Leigh's health during a level.  Don't forget this!";
  else if (num == 44)
    string = "Note that when Leigh is sprinting, he'll be using all his strengh to get to wherever he's going, so he won't be able to take aim and shoot.";		

  else if (num == 45)
    string = "The door ahead is locked, but the floor's grating has been removed.  Fall down the gap to continue.";		
  else if (num == 46)
    string = "You've taken some damage from that long fall.  Fortunately, there's some adrenaline in the locker here.  Use it to regain your health.";		
  else if (num == 47)
    string = "The rework in the next room will take about 20 body shots to kill, while with head shots it'll only take 5, so aim well.";
  else if (num == 48)
    string = "Finally, when you get more weapons, you can select them by either pressing the weapon's key, or cycle trough them with "+inputs[CKEY_Z].toUpperCase()+"/"+inputs[CKEY_X].toUpperCase()+" or scrolling the MOUSE WHEEL.";


	lpressedy = true;

	var sword_y = 140;
	var train_y = 85;

	if (num > 26 && num < 29)
	{
		UpdatePlayerFrame(0);
	}
	
	while(true)
	{
		RenderBackground();
		
		if (num > 26 && num < 29)
		{
			text_window.drawWindow(20,180-sword_y,280, 50);
			font2.drawTextBox(20,180-sword_y, 280,50,0,string);
		}
		else if (num >= 40 && num <=41)
		{
			text_window.drawWindow(20,180-train_y,280, 50);
			font2.drawTextBox(20,180-train_y, 280,50,0,string);
		}
		else
		{
			text_window.drawWindow(20,180,280, 50);
			font2.drawTextBox(20,180, 280,50,0,string);
		}
		FlipScreen();
/*		
		if (IsMouseButtonPressed(MOUSE_LEFT) == false)
			lpressedy = false;
			
		if (lpressedy == false && IsMouseButtonPressed(MOUSE_LEFT) == true)
		{
			return;
		}
*/	
		while(AreKeysLeft())
		{
			if (num <= 41 && num >= 40)
			{
				switch(GetKey())
				{
					case CKEY_ESCAPE:
					{
						return;
					}
				}			
			}
			else
			{
				switch(GetKey())
				{
					case CKEY_F:
					{
						return;
					}
					case KEY_SPACE:
					{
						return;
					}
				}
			}
		}
	}
}



function RenderBackground()
{
		DrawBackGroundImage();

			if (oblit_frame > 5)
			{
				SetCameraX(entities[0].x+(oblit_max-oblit_frame)*Math.random()/10);
				SetCameraY(entities[0].y+(oblit_max-oblit_frame)*Math.random()/10);
			}
			
			if ((entities[0].invince > 0 || (fadeout < 75 && entities[0].hp <=0)) && g_frames%2 == 0)
			{
				for (bi=1;bi<10;bi++)
				{
					entities[0]["la"+bi] = GetPersonIDLayer(entities[0]["id"+bi]);
					SetPersonIDLayer(entities[0]["id"+bi], 0);
				}
			}

			RenderMap();

			if ((entities[0].invince > 0 ||entities[0].hp <=0)&& g_frames%2 == 0)
			{
				for (bi=1;bi<10;bi++)
				{
					SetPersonIDLayer(entities[0]["id"+bi], entities[0]["la"+bi]);
				}
			}

			DrawRailguns();
			DrawHud(false);
			if (oblit_shield > 0)
				DrawOblitShield();
			if (oblit_frame > 0)
				DrawObliterator();
				
			if (fadeout > fadestart)
				Rectangle(0,0,GetScreenWidth(),GetScreenHeight(), CreateColor(0,0,0,255*(fadeout-fadestart)/(fademax-fadestart)));

}