#include "scripts/common.txt"
#include "scripts/bp_common.txt"
#include "scripts/map/common_level_script.txt"
#include "scripts/main.txt"
#include "scripts/debug/debug.txt"
#include "scripts/waves/wave1.txt"
#include "scripts/waves/wave2.txt"
#include "scripts/waves/wave3.txt"
#include "scripts/waves/wave4.txt"

//arena is not perfectly straight. it's square but slightly rotated on all axis. so have to make it square on XY. Z will still be slightly off but its very minor so don't need to worry about it.
const kVec3 mapStartPos(-4745.727539f, -1607.423828f, -0.000070f);

//1 model size = 256 game units
const float wallSpace = 1024.0f; //4 * 256 (4 model units)
const kVec3 northWallMin(mapStartPos.x - wallSpace * 2, mapStartPos.y + wallSpace, mapStartPos.z - wallSpace);
const kVec3 northWallMax(mapStartPos.x + wallSpace * 2, mapStartPos.y + wallSpace * 2, mapStartPos.z + wallSpace);
const kVec3 southWallMin(mapStartPos.x - wallSpace * 2, mapStartPos.y - wallSpace * 2, mapStartPos.z - wallSpace);
const kVec3 southWallMax(mapStartPos.x + wallSpace * 2, mapStartPos.y - wallSpace, mapStartPos.z + wallSpace);
const kVec3 eastWallMin(mapStartPos.x + wallSpace, mapStartPos.y - wallSpace * 2, mapStartPos.z - wallSpace);
const kVec3 eastWallMax(mapStartPos.x + wallSpace * 2, mapStartPos.y + wallSpace * 2, mapStartPos.z + wallSpace);
const kVec3 westWallMin(mapStartPos.x - wallSpace * 2, mapStartPos.y - wallSpace * 2, mapStartPos.z - wallSpace);
const kVec3 westWallMax(mapStartPos.x - wallSpace, mapStartPos.y + wallSpace * 2, mapStartPos.z + wallSpace);

int killCount = 0;
bool didWave2BossAnger = false;
bool w4Trigger1, w4Trigger2, w4Trigger3;
kActor @w4Plate1;
kActor @w4Plate2;
kActor @w4Plate3;
//------------------------------------------------------------------------------------------------------------------------
// Start
//------------------------------------------------------------------------------------------------------------------------
$script 0 {
	World.ChangeAreaArg(0, 2, 1);
	World.ChangeAreaFlag(0, AAF_CHECKPOINT, true);
	
	// GameVariables.SetValue("BP.ArenaCinID", "4");
	
	GameVariables.GetInt("BP.ArenaCinID", mapArenaCinID, 1);
	
	//north force field actors
	for (int i = 0; i < 8; i++) {
		ActorFactory.Spawn("BP_WallForceField", (mapStartPos.x - wallSpace) + (i * 256.0f) + 128.0f, mapStartPos.y + wallSpace, mapStartPos.z, 0, 0);
	}
	//south force field actors
	for (int i = 0; i < 8; i++) {
		ActorFactory.Spawn("BP_WallForceField", (mapStartPos.x - wallSpace) + (i * 256.0f) + 128.0f, mapStartPos.y - wallSpace, mapStartPos.z, 0, 0);
	}
	//east force field actors
	for (int i = 0; i < 8; i++) {
		ActorFactory.Spawn("BP_WallForceField", mapStartPos.x + wallSpace, (mapStartPos.y - wallSpace) + (i * 256.0f) + 128.0f, mapStartPos.z, Math::Deg2Rad(90), 0);
	}
	//west force field actors
	for (int i = 0; i < 8; i++) {
		ActorFactory.Spawn("BP_WallForceField", mapStartPos.x - wallSpace, (mapStartPos.y - wallSpace) + (i * 256.0f) + 128.0f, mapStartPos.z, Math::Deg2Rad(90), 0);
	}
	
	//pyrmaid to the south
	// kActor @pyrmaid = ActorFactory.Spawn("BP_Pyrmaid", mapStartPos.x, mapStartPos.y - wallSpace - 512.0f, mapStartPos.z, 0, 0);
	// pyrmaid.Scale() = kVec3(0.35f, 0.35f, 0.35f);
	
	Game.CallDelayedMapScript(1, instigator, 0); //Game Update
	delay(0.05f);
	Player.Actor().Gravity() = NormalGravity; //disable the eagle feather effect
	// Game.PlayMusic("music/track04.ogg", true);
	
	// SpawnActor("BPStalker", mapStartPos.x, mapStartPos.y - 512.0f, mapStartPos.z);
	
	// //The floor
	// for (int y = 0; y < 16; y++) {
		// for (int x = 0; x < 16; x++) {
			// kActor @t = ActorFactory.Spawn("BP_ArenaFloor", (mapStartPos.x - wallSpace) + (x * 128.0f) + 64.0f, (mapStartPos.y - wallSpace) + (y * 128.0f) + 128.0f, mapStartPos.z, 0, 0);
			// t.Scale() = kVec3(0.5f, 0.5f, 0.5f);
			// t.Pitch() = Math::Deg2Rad(-90.0f);
			// if (x >= 7 and x <= 8) {
				// t.RenderModel().SetTexture(0, 1);
			// }
		// }
	// }
		
	// if (true) {
		// return;
	// }
	
	if (Debug::IsActorEditorOn()) {
		//The floor
		for (int y = 0; y < 16; y++) {
			for (int x = 0; x < 16; x++) {
				kActor @t = ActorFactory.Spawn("BP_ArenaFloor", (mapStartPos.x - wallSpace) + (x * 128.0f) + 64.0f, (mapStartPos.y - wallSpace) + (y * 128.0f) + 128.0f, mapStartPos.z, 0, 0);
				t.Scale() = kVec3(0.5f, 0.5f, 0.5f);
				t.Pitch() = Math::Deg2Rad(-90.0f);
				if (x >= 7 and x <= 8) {
					t.RenderModel().SetTexture(0, 1);
				}
			}
		}
		
		return;
	}
	
	if (mapArenaCinID == 1) {
		Game.CallDelayedMapScript(3, instigator, 0); //spawn wave 1 enemies
		Game.CallDelayedMapScript(4, instigator, 0); //update wave 1
	} else if (mapArenaCinID == 2) {
		Game.CallDelayedMapScript(8, instigator, 0); //start wave 2
		Game.CallDelayedMapScript(7, instigator, 0); //update wave 2
	} else if (mapArenaCinID == 3) {
		Game.CallDelayedMapScript(10, instigator, 0); //start wave 3
		Game.CallDelayedMapScript(11, instigator, 0); //update wave 3
	} else if (mapArenaCinID == 4) {
		Game.CallDelayedMapScript(12, instigator, 0); //start wave 4
	} else {
		Game.CallDelayedMapScript(9, instigator, 0); //End Map
	}
	
}
//------------------------------------------------------------------------------------------------------------------------
// Game Update
//------------------------------------------------------------------------------------------------------------------------
$script 1 {
	BlockAreaMinMaxArena(northWallMin, northWallMax, kVec3(0.0f, -1.0f, 0.0f));
	BlockAreaMinMaxArena(southWallMin, southWallMax, kVec3(0.0f, 1.0f, 0.0f));
	BlockAreaMinMaxArena(eastWallMin, eastWallMax, kVec3(-1.0f, 0.0f, 0.0f));
	BlockAreaMinMaxArena(westWallMin, westWallMax, kVec3(1.0f, 0.0f, 0.0f));
	//block top area
	
	// uint16 buttons = Player.Buttons();
	// if ((buttons & (BC_MAPZOOMIN)) != 0) {
		// ActorFactory.Spawn("Human_Grunt_Soldier2", mapStartPos.x, mapStartPos.y - 512.0f, mapStartPos.z, 0, 2);
		// //e.Flags() |= AF_SOLID;
	// }
	
	UpdateGeneralGame();
    $restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Enemy was Killed
//------------------------------------------------------------------------------------------------------------------------
$script 2 {
	killCount++;
	// Sys.Print("killCount = " + killCount);
}
//------------------------------------------------------------------------------------------------------------------------
// Start Wave 1
//------------------------------------------------------------------------------------------------------------------------
$script 3 {
	Game.StopMusic();
	
	Waves::Wave1();
	
	Player.Actor().ModelVariation() = TV_BOW;
	Player.Actor().AnimState().Set(anim_player_idle, 8.0f, ANF_LOOP);
	Player.Actor().Flags() &= ~AF_NODRAW;
    Camera.fov = 70.0f;
	Camera.SetLookAtActor(Player.Actor().CastToActor());
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_UNLOCK_PLAYER_ON_FINISH|CMF_NO_INITIAL_FADEOUT);
	Camera.SetFinalView(0);
	Camera.SetRotationTrack(0,
							Math::Deg2Rad(-180.0f),  // start angle
							Math::Deg2Rad(180.0f),   // dest angle
							20*GAME_SCALE,           // start distance
							15*GAME_SCALE,           // dest distance
							20*GAME_SCALE,           // start height
							12*GAME_SCALE,           // dest height
							7*GAME_SCALE,           // start look at height
							7*GAME_SCALE);          // dest look at height
	Camera.AutoPlayRotationTrack(0, 7.0f, CMLT_COSINE);
	
	Game.CallDelayedMapScript(20, instigator, 0); //Update Wave 1 Cinematic
	delay(0.5f);
	Player.Actor().PlaySound("sounds/shaders/arena_vo_01.ksnd");
	DoCinematicText(7.0f, "Darkness is upon you little Turok", "The sons of stone will fall this day", "");
	delay(7.5f);
	Camera.StopCinematic();
	Game.HaltMapScript(20);
	delay(0.5f);
	Player.Actor().Flags() |= AF_NODRAW;
	Game.PlayMusic("music/track04.ogg", true);
	delay(1.0f);
	SpawnActor("BP_PickupSpawner", mapStartPos.x, mapStartPos.y, mapStartPos.z);
	SpawnActor("BP_PickupSpawner", mapStartPos.x + 800, mapStartPos.y + 800, mapStartPos.z);
	SpawnActor("BP_PickupSpawner", mapStartPos.x - 800, mapStartPos.y + 800, mapStartPos.z);
	SpawnActor("BP_PickupSpawner", mapStartPos.x - 800, mapStartPos.y - 800, mapStartPos.z);
	SpawnActor("BP_PickupSpawner", mapStartPos.x - 800, mapStartPos.y - 800, mapStartPos.z);
	for (int i = 0; i < 10; i++) {
		SpawnWave1Raptor(2, 20);
		delay(Math::RandRange(2.5f, 5.0f));
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Update Wave 1
//------------------------------------------------------------------------------------------------------------------------
$script 4 {
	if (killCount >= 20) {
		delay(2.0f);
		Player.Actor().ModelVariation() = TV_BOW;
		Player.Actor().AnimState().Set(anim_player_idle, 8.0f, ANF_LOOP);
		Player.Actor().Flags() &= ~AF_NODRAW;
		Camera.fov = 70.0f;
		Camera.SetLookAtActor(Player.Actor().CastToActor());
		Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_UNLOCK_PLAYER_ON_FINISH|CMF_NO_INITIAL_FADEOUT);
		Camera.SetFinalView(0);
		Camera.SetRotationTrack(0,
								Math::Deg2Rad(-135.0f),  // start angle
								Math::Deg2Rad(45.0f),   // dest angle
								20*GAME_SCALE,           // start distance
								15*GAME_SCALE,           // dest distance
								20*GAME_SCALE,           // start height
								12*GAME_SCALE,           // dest height
								7*GAME_SCALE,           // start look at height
								7*GAME_SCALE);          // dest look at height
		Camera.AutoPlayRotationTrack(0, 38.0f, CMLT_COSINE);
		Game.PlayMusic("music/track01.ogg", true); //adonTheme
		delay(3.5f);
		Game.PlaySound("sounds/shaders/arena_vo_02.ksnd");
		DoCinematicText(8.0f, "Oblivion has infested the bodies", "of the dead and dying", "The infection has created horrible mutations");
		delay(8.0f);
		Game.PlaySound("sounds/shaders/arena_vo_03.ksnd");
		DoCinematicText(3.5f, "This is the Nexus", "The heart of the lost land", "");
		delay(3.5f);
		DoCinematicText(3.5f, "Here time has no meaning and", "all things are possible", "");
		delay(4.5f);
		DoCinematicText(8.0f, "Temporal disturbances in the time stream", "occasionally pull objects and lifeforms through", "from other worlds and deposit them here");
		delay(8.0f);
		DoCinematicText(4.0f, "These lands are dangerous and unstable", "", "");
		delay(4.0f);
		Game.PlaySound("sounds/shaders/arena_vo_04.ksnd");
		Game.PrintLine("Oblivion seeks to crush the son of stone", 0, 180);
		delay(4.0f);
		Game.PlaySound("sounds/shaders/arena_vo_05.ksnd");
		Game.PrintLine("You must stop Oblivion", 0, 180);
		delay(2.0f);
		Game.CallDelayedMapScript(9, instigator, 0); //End Map
		return;
	}
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Purlin Boss is Angry
//------------------------------------------------------------------------------------------------------------------------
$script 5 {
	//if wave == 2
	if (!didWave2BossAnger) {
		didWave2BossAnger = true;
		SpawnWave2Purlins(6, 50);
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Purlin Boss is Dead
//------------------------------------------------------------------------------------------------------------------------
$script 6 {
	PlayLoop.RemoveBossActor();
	//end level
	if (!didWave2BossAnger) {
		Game.CallDelayedMapScript(5, instigator, 0); //Spawn Purlin Angry Enemies
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Update Wave 2
//------------------------------------------------------------------------------------------------------------------------
$script 7 {
	if (killCount >= 9) {
		Game.CallDelayedMapScript(9, instigator, 3.0f); //End Map
		return;
	}
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Start Wave 2
//------------------------------------------------------------------------------------------------------------------------
$script 8 {
	Game.PlayMusic("music/track12.ogg", true);

	Waves::Wave2();
	
	SpawnActor("BP_PickupSpawner", mapStartPos.x - 500, mapStartPos.y + 500, mapStartPos.z);
	SpawnActor("BP_PickupSpawner", mapStartPos.x + 500, mapStartPos.y - 500, mapStartPos.z);
	
	kActor @actor = ActorFactory.Spawn("BPPurlin_Boss", mapStartPos.x, mapStartPos.y - 900, mapStartPos.z, 0, 2);
	PlayLoop.TagActorForBossBar(@actor);
	SpawnWave2Purlins(2, 50);
}
//------------------------------------------------------------------------------------------------------------------------
// End Map
//------------------------------------------------------------------------------------------------------------------------
$script 9 {
	delay(2.0f);
	Game.PlayMusic("music/track01.ogg", true);
	
	int sector;
	kVec3 pos;
	float yaw;
	int mapID;
	GameVariables.GetInt("BP.ArenaWarpSector", sector);
	GameVariables.GetVector("BP.ArenaWarpPos", pos);
	GameVariables.GetFloat("BP.ArenaWarpYaw", yaw);
	GameVariables.GetInt("BP.ArenaWarpMap", mapID);
	// yaw += Math::Deg2Rad(180);
	// Sys.Print("Arena Warp End ---- Sector: " + sector + ", Pos: " + pos.ToString() + ", yaw: " + yaw + ", mapID: " + mapID);

	// Player.Actor().Yaw() = 0;
	PlayLoop.StartFreeWarp(Player.Actor().CastToActor(), pos, yaw, sector, mapID);
}
//------------------------------------------------------------------------------------------------------------------------
// Start Wave 3
//------------------------------------------------------------------------------------------------------------------------
$script 10 {
	Game.PlayMusic("music/track04.ogg", true);
	Waves::Wave3();
	
	kActor @spawner1 = SpawnActor("BP_PickupSpawner", mapStartPos.x, mapStartPos.y, mapStartPos.z);
	spawner1.Radius() = 15.0f; //spawn time
	
	SpawnActorRandom("BPGruntMelee_PoacherKnife", 3, 20);
	while (killCount < 3) { delay(1.0f); }
	SpawnActorRandom("BPGruntMelee_PoacherKnife", 5, 20);
	SpawnActorRandom("BPGruntRange_Poacher", 3, 20); //11
	while (killCount < 8) { delay(1.0f); }
	SpawnActorRandom("BPRaptor_Common", 5, 20); //16
	while (killCount < 11) { delay(1.0f); }
	SpawnActorRandom("BPGruntMelee_PoacherKnife", 5, 20);
	SpawnActorRandom("BPGruntRange_Poacher", 1, 20); //22
	while (killCount < 18) { delay(1.0f); }
	SpawnActorRandom("BPGruntMelee_PoacherKnife", 5, 20);
	SpawnActorRandom("BPRaptor_Common", 5, 20);
	SpawnActorRandom("BPGruntRange_Poacher", 2, 20); //34
	while (killCount < 25) { delay(1.0f); }
	SpawnActorRandom("BPGruntMelee_PoacherKnife", 2, 20);
	SpawnActorRandom("BPRaptor_Common", 2, 20); //38
	while (killCount < 34) { delay(1.0f); }
	SpawnActorRandom("BPGruntMelee_PoacherKnife", 5, 20);
	SpawnActorRandom("BPRaptor_Common", 2, 20);
	SpawnActorRandom("BPGruntRange_Poacher", 2, 20); //47
}
//------------------------------------------------------------------------------------------------------------------------
// Update Wave 3
//------------------------------------------------------------------------------------------------------------------------
$script 11 {
	if (killCount >= 47) {
		Game.CallDelayedMapScript(9, instigator, 0); //End Map
		return;
	}
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Start Wave 4
//------------------------------------------------------------------------------------------------------------------------
$script 12 {
	Game.PlayMusic("music/track12.ogg", true);
	
	Waves::Wave4();
	
	@w4Plate1 = SpawnActor("BPDetail-TechPlate", -5376.794434f, -1313.143188f, 0.999970f, 0.000000f);
	@w4Plate2 = SpawnActor("BPDetail-TechPlate", -4126.250488f, -1110.681030f, 0.999954f, 0.000000f);
	@w4Plate3 = SpawnActor("BPDetail-TechPlate", -4168.870117f, -2397.886230f, 0.999946f, -3.141593f);
	w4Plate1.Flags() |= AF_NODRAW;
	w4Plate2.Flags() |= AF_NODRAW;
	w4Plate3.Flags() |= AF_NODRAW;

	kActor @izzie = SpawnActor("BPIZZIE", mapStartPos.x, mapStartPos.y - 900.0f, mapStartPos.z);
	izzie.ImpactType() = 0;
	izzie.Scale() = kVec3(0.01f, 0.01f, 0.01f);
	
    Camera.fov = 70.0f;
	Camera.SetLookAtActor(@izzie);
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_UNLOCK_PLAYER_ON_FINISH|CMF_NO_INITIAL_FADEOUT);
	Camera.SetFinalView(0);
	Camera.SetRotationTrack(0,
						Math::Deg2Rad(25.0f),    // start angle
						Math::Deg2Rad(0.0f),  // dest angle
						5*GAME_SCALE,          // start distance
						20*GAME_SCALE,           // dest distance
						2*GAME_SCALE,           // start height
						8*GAME_SCALE,           // dest height
						1*GAME_SCALE,           // start look at height
						10*GAME_SCALE);            // dest look at height
	Camera.AutoPlayRotationTrack(0, 6.0f, CMLT_COSINE);
	izzie.Scale() = kVec3(1.0f, 1.0f, 1.0f);
	izzie.RunFxEvent("IzzieGrow");
	Game.PlaySound("sounds/shaders/izzie01.ksnd");
	delay(6.0f);
	Game.PlaySound("sounds/shaders/generic_167.ksnd");
	delay(1.0f);
	Camera.StopCinematic();
	izzie.ImpactType() = IT_FLESH_HUMAN;
	PlayLoop.TagActorForBossBar(@izzie);
	
	SpawnActor("BP_PickupSpawner", mapStartPos.x, -2305, 0, 2).Radius() = 10.0f;
	SpawnActor("BP_PickupSpawner", mapStartPos.x, -2005, 0, 2).Radius() = 20.0f;
	
	SpawnActor("BP_PickupSpawner", mapStartPos.x, mapStartPos.y, mapStartPos.z);
	Game.CallDelayedMapScript(19, instigator, 5.0f); //Spawn Wave4 Enemies
	
	Game.CallDelayedMapScript(13, izzie, 0); //update wave 4
}
//------------------------------------------------------------------------------------------------------------------------
// Update Wave 4 - instigator is izzie boss
//------------------------------------------------------------------------------------------------------------------------
$script 13 {
	if (killCount >= 6) {
		killCount = 0;
		Game.CallDelayedMapScript(19, instigator, 15.0f); //Spawn Wave4 Enemies
	}
	
	if (instigator.ImpactType() == IT_FORCEFIELD) {
		if ((PlayLoop.Ticks() & 31) == 0) {
			instigator.SpawnFx("fx/campainger_shield.kfx", Math::vecZero);
		}
		if ((PlayLoop.Ticks() & 255) == 0) {
			instigator.PlaySound("sounds/shaders/energy_shield_hum.ksnd");
		}
		
		if (!w4Trigger1) {
			if (PlayLoop.Ticks() % 60 == 0) {
				w4Plate1.SpawnFx("fx/generic_232.kfx", kVec3(0, 0, 1400*GAME_SCALE));
			}
			if (InArea(kVec3(-5436, -1253, -1) + PressurePlate2Offset, PressurePlate2Size) and Player.Actor().OnGround()) {
				w4Trigger1 = true;
				w4Plate1.Flags() |= AF_NODRAW;
				Game.CallDelayedMapScript(18, instigator, 0); //rock falls on izzie
			}
		} else if (!w4Trigger2) {
			if (PlayLoop.Ticks() % 60 == 0) {
				w4Plate2.SpawnFx("fx/generic_232.kfx", kVec3(0, 0, 1400*GAME_SCALE));
			}
			if (InArea(kVec3(-4186, -1050, -1) + PressurePlate2Offset, PressurePlate2Size) and Player.Actor().OnGround()) {
				w4Trigger2 = true;
				w4Plate2.Flags() |= AF_NODRAW;
				Game.CallDelayedMapScript(18, instigator, 0); //rock falls on izzie
			}
		} else if (!w4Trigger3) {
			if (PlayLoop.Ticks() % 60 == 0) {
				w4Plate3.SpawnFx("fx/generic_232.kfx", kVec3(0, 0, 1400*GAME_SCALE));
			}
			if (InArea(kVec3(-4228, -2337, -1) + PressurePlate2Offset, PressurePlate2Size) and Player.Actor().OnGround()) {
				w4Trigger3 = true;
				w4Plate3.Flags() |= AF_NODRAW;
				Game.CallDelayedMapScript(18, instigator, 0); //rock falls on izzie
			}
		}
	}
	
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// On Wave 4 Boss Izzie Death
//------------------------------------------------------------------------------------------------------------------------
$script 14 {
	Game.HaltMapScript(13);
	Game.HaltMapScript(19);
	PlayLoop.RemoveBossActor();
	delay(8.0f);
	Game.CallDelayedMapScript(9, instigator, 0); //End Map
}
//------------------------------------------------------------------------------------------------------------------------
// On Wave 4 Boss Izzie HP 75%
//------------------------------------------------------------------------------------------------------------------------
$script 15 {
	w4Plate1.Flags() &= ~AF_NODRAW;
}
//------------------------------------------------------------------------------------------------------------------------
// On Wave 4 Boss Izzie HP 50%
//------------------------------------------------------------------------------------------------------------------------
$script 16 {
	w4Plate2.Flags() &= ~AF_NODRAW;
}
//------------------------------------------------------------------------------------------------------------------------
// On Wave 4 Boss Izzie HP 25%
//------------------------------------------------------------------------------------------------------------------------
$script 17 {
	w4Plate3.Flags() &= ~AF_NODRAW;
}
//------------------------------------------------------------------------------------------------------------------------
// On Wave 4 Rock falls on Izzie
//------------------------------------------------------------------------------------------------------------------------
$script 18 {
	Game.PlaySound("sounds/shaders/generic_177.ksnd");
	kActor @rock = SpawnActor("BP_FallingRocks2", instigator.Origin().x, instigator.Origin().y, instigator.Origin().z);
	rock.AnimState().Blend(anim_trapActivate, 4.0f, 8.0f, 0);
	
	Camera.fov = 70.0f;
	Camera.SetLookAtActor(instigator);
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_UNLOCK_PLAYER_ON_FINISH|CMF_NO_INITIAL_FADEOUT);
	Camera.SetFinalView(0);
	Camera.SetRotationTrack(0,
						Math::Deg2Rad(0.0f),    // start angle
						Math::Deg2Rad(0.0f),  // dest angle
						40*GAME_SCALE,          // start distance
						40*GAME_SCALE,           // dest distance
						15*GAME_SCALE,           // start height
						15*GAME_SCALE,           // dest height
						12*GAME_SCALE,           // start look at height
						12*GAME_SCALE);            // dest look at height
	Camera.AutoPlayRotationTrack(0, 1.0f, CMLT_COSINE);
	delay(0.75f);
	Game.PlaySound("sounds/shaders/izzie02.ksnd");
	instigator.ImpactType() = IT_FLESH_HUMAN;
	delay(2.25f);
	Camera.StopCinematic();
	delay(1.0f);
	rock.Remove();
}
//------------------------------------------------------------------------------------------------------------------------
// Spawn Wave 4 Enemies
//------------------------------------------------------------------------------------------------------------------------
$script 19 {
	SpawnActorRandom("BPHead", 2, 10);
	SpawnActorRandom("BPStalker", 4, 10);
}
//------------------------------------------------------------------------------------------------------------------------
// Update Wave 1 Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 20 {
	if (Camera.UserInterrupted()) {
		Game.HaltMapScript(3);
		Camera.StopCinematic();
		ClearCinematicText();
		Player.Actor().StopSound();
		delay(0.5f);
		Player.Actor().Flags() |= AF_NODRAW;
		Game.PlayMusic("music/track04.ogg", true);
		delay(1.0f);
		SpawnActor("BP_PickupSpawner", mapStartPos.x, mapStartPos.y, mapStartPos.z);
		SpawnActor("BP_PickupSpawner", mapStartPos.x + 800, mapStartPos.y + 800, mapStartPos.z);
		SpawnActor("BP_PickupSpawner", mapStartPos.x - 800, mapStartPos.y + 800, mapStartPos.z);
		SpawnActor("BP_PickupSpawner", mapStartPos.x - 800, mapStartPos.y - 800, mapStartPos.z);
		SpawnActor("BP_PickupSpawner", mapStartPos.x - 800, mapStartPos.y - 800, mapStartPos.z);
		for (int i = 0; i < 10; i++) {
			SpawnWave1Raptor(2, 20);
			delay(Math::RandRange(2.5f, 5.0f));
		}
        return;
    }
    $restart;
}
//------------------------------------------------------------------------------------------------------------------------
void AddRandomPlant(float x, float y) {
	float ryaw = Math::Deg2Rad(Math::RandRange(0.0f, 360.0f));
	int plantType = Math::RandMax(19);
	kActor @plant = ActorFactory.Spawn("Plant" + plantType, mapStartPos.x + x, mapStartPos.y + y, mapStartPos.z, ryaw, 2);
	float rs = Math::RandRange(0.25f, 0.45f);
	plant.Scale() = kVec3(rs, rs, rs);
}
//------------------------------------------------------------------------------------------------------------------------
void AddDestructableTree(float x, float y) {
	float ryaw = Math::Deg2Rad(Math::RandRange(0.0f, 360.0f));
	kActor @tree1 = ActorFactory.Spawn("BPDestruct-PalmTree", mapStartPos.x + x, mapStartPos.y + y, mapStartPos.z, ryaw, 2);
	tree1.Height() = 200.0f;
	tree1.Flags() |= AF_SOLID;
}
//------------------------------------------------------------------------------------------------------------------------
void SpawnWave1Raptor(int amount, int health) {
	for (int i = 0; i < amount; i++) {
		float rx = Math::RandRange(-wallSpace, wallSpace);
		float ry = Math::RandRange(-wallSpace, wallSpace);
		float ryaw = Math::Deg2Rad(Math::RandRange(0.0f, 360.0f));
		ActorFactory.Spawn("BPRaptor_Common", mapStartPos.x + rx, mapStartPos.y + ry, mapStartPos.z, ryaw, 2).Health() = health;
	}
}
//------------------------------------------------------------------------------------------------------------------------
void SpawnWave2Purlins(int amount, int health) {
	for (int i = 0; i < amount; i++) {
		float rx = Math::RandRange(-wallSpace, wallSpace);
		float ry = Math::RandRange(-wallSpace, wallSpace);
		float ryaw = Math::Deg2Rad(Math::RandRange(0.0f, 360.0f));
		
		ActorFactory.Spawn("BPPurlin_MeleeCommon", mapStartPos.x + rx, mapStartPos.y + ry, mapStartPos.z, ryaw, 2).Health() = health;
	}
}
//------------------------------------------------------------------------------------------------------------------------
void SpawnActorRandom(const kStr &in name, int amount, int health) {
	for (int i = 0; i < amount; i++) {
		float rx = Math::RandRange(-wallSpace, wallSpace);
		float ry = Math::RandRange(-wallSpace, wallSpace);
		float ryaw = Math::Deg2Rad(Math::RandRange(0.0f, 360.0f));
		
		ActorFactory.Spawn(name, mapStartPos.x + rx, mapStartPos.y + ry, mapStartPos.z, ryaw, 2).Health() = health;
	}
}
//------------------------------------------------------------------------------------------------------------------------
