#include "scripts/map/common_level_script.txt"


//Tag IDs
//1000: Key 3
//1001: Door 1
//1002: Start Teleport
//1003: Longhunter 1
//1004: Longhunter 2
//1005: Longhunter 3
//1006: Longhunter 4
//1007: Mantis 1
//1008: Mantis 2
//1009: TRex 1
//1010: TRex 2
//1011: TRex 3
//1012: Boss HP Counter
//1013: Campainger 1
//1014: Longhunter 5
//1015: Longhunter 6
//1016: Longhunter 7
//1017: Longhunter 8
//1018: Mantis 3
//1019: Mantis 4
//1020: Hummer 1
//1021: Hummer 2
//1022: Hummer 3
//1023: Hummer 4
//1024: 


array<array<kActor@>> doorTargets = { {}, {} };
uint doorIndex;
kActor @hpCounter;
array<int> totalHealth(2, 0);

//------------------------------------------------------------------------------------------------------------------------
// Start
//------------------------------------------------------------------------------------------------------------------------
$script 0
{
	if (OnLevelStart(instigator))
		return;
	
	doorIndex = 0;
	@hpCounter = World.GetActorByTID(1012);
	totalHealth[0] = 0;
	totalHealth[1] = 0;
	Game.CallDelayedMapScript(2, instigator, 0.0f); //Delayed Start
}
//------------------------------------------------------------------------------------------------------------------------
// Delayed Start
//------------------------------------------------------------------------------------------------------------------------
$script 2
{
	//door 1 enemies
	for (int i = 0; i < 9; ++i)
	{
		kActor @actor = World.GetActorByTID(1003 + i);
		doorTargets[0].insertLast(@actor);
		if (!IsFortDoorOpen(0) && (actor.Flags() & AF_DEAD) == 0 && actor.Health() > 0)
		{
			totalHealth[0] += actor.Health();
		}
	}
	//door 2 enemies
	for (int i = 0; i < 11; ++i)
	{
		kActor @actor = World.GetActorByTID(1013 + i);
		doorTargets[1].insertLast(@actor);
		if (!IsFortDoorOpen(1) && (actor.Flags() & AF_DEAD) == 0 && actor.Health() > 0)
			totalHealth[1] += actor.Health();
	}

	if (totalHealth[0] <= 0)
	{
		doorIndex++;
		totalHealth[0] = 1;
	}
	if (totalHealth[1] <= 0)
	{
		doorIndex++;
		totalHealth[1] = 1;
	}
	GetHP();
	if (doorIndex >= 0 && doorIndex < doorTargets.length())
	{
		PlayLoop.TagActorForBossBar(hpCounter);
	}
	Game.CallDelayedMapScript(1, instigator, 0); //Update Level
}
//------------------------------------------------------------------------------------------------------------------------
// Update Game Level
//------------------------------------------------------------------------------------------------------------------------
$script 1
{
	// uint16 buttons = Player.Buttons();
	// if ((buttons & (BC_MAPZOOMOUT)) != 0)
	// {
		// Game.CallDelayedMapScript(10000, instigator, 0); //Door 1
		
		// for (int i = 0; i < 9; ++i)
		// {
			// kActor @actor = World.GetActorByTID(1003 + i);
			// doorTargets[0][i].Flags() |= AF_DEAD;
		// }
		// for (int i = 0; i < 11; ++i)
		// {
			// kActor @actor = World.GetActorByTID(1013 + i);
			// doorTargets[1][i].Flags() |= AF_DEAD;
		// }
		
	// }
	
	//check if current door should open
	if (doorIndex >= 0 && doorIndex < doorTargets.length())
	{
		GetHP();
		
		bool targetsDead = true;
		for (uint i = 0; i < doorTargets[doorIndex].length; ++i)
		{
			kActor @actor = doorTargets[doorIndex][i];
			if ((actor.Flags() & AF_DEAD) == 0)
			{
				targetsDead = false;
				break;
			}
		}
		
		if (targetsDead)
		{
			switch (doorIndex)
			{
				case 0:
				{
					PlayLoop.RemoveBossActor();
					Game.CallDelayedMapScript(10000, instigator, 0); //Door 1
					break;
				}
				case 1:
				{
					PlayLoop.RemoveBossActor();
					SetFortDoor(1);
					break;
				}
			}
			++doorIndex;
		}
	}
	
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Start Intro Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 3
{
	SetIntroFinished();
	Game.CallDelayedMapScript(3, instigator, 0); //Update Intro Cinematic
	
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_UNLOCK_PLAYER_ON_FINISH);
	Camera.SetLookAtActor(Player.Actor().CastToActor());
	Camera.fov = 74.0f;
	Player.Actor().ModelVariation() = TV_BOW;
	Player.Actor().AnimState().Set(anim_player_idle, 8.0f, ANF_LOOP);
	Player.Actor().Flags() &= ~AF_NODRAW;
	Player.Actor().Pitch() = 0;
	Player.Actor().Roll() = 0;
	PlayerSetToFloor();
	
	Camera.SetFinalView(0);
	Camera.SetRotationTrack(0,
							Math::Deg2Rad(345.0f),  // start angle
							Math::Deg2Rad(180.0f),   // dest angle
							20*GAME_SCALE,           // start distance
							10*GAME_SCALE,           // dest distance
							12*GAME_SCALE,           // start height
							17*GAME_SCALE,           // dest height
							8*GAME_SCALE,           // start look at height
							11*GAME_SCALE);          // dest look at height
	Camera.AutoPlayRotationTrack(0, 8.0f, CMLT_COSINE);
	
	delay(0.25f);
	ShowDelayedText("Young warrior, the time has come", "for you to complete the trials of the jungle", time: 4.0f);
	delay(4.0f);
	ExitCinematic(3);
}
//------------------------------------------------------------------------------------------------------------------------
// Update Intro Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 4
{
	if (Camera.UserInterrupted())
	{
		ExitCinematic(2);
        return;
    }
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Door 1
//------------------------------------------------------------------------------------------------------------------------
$script 10000
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && !IsFortDoorOpen(0))
	{
		//Game.PlaySound("sounds/shaders/generic_177.ksnd");
		//Game.PrintLine("Force field offline", 0);
		World.TriggerActorsByTID(instigator, 1001); //Door 1
		SetFortDoor(0);
		Game.CallDelayedMapScript(10001, instigator, 10.0f);
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Show Door 2 Enemies HP
//------------------------------------------------------------------------------------------------------------------------
$script 10001
{
	if (doorIndex >= 0 && doorIndex < doorTargets.length())
	{
		PlayLoop.TagActorForBossBar(hpCounter);
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Enter Portal
//------------------------------------------------------------------------------------------------------------------------
$script 10002
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && IsFortDoorOpen(1))
	{
		//teleport to map 1
		PlayLoop.StartWarp(Player.Actor().CastToActor(), 1027, 101);
	}
}
//------------------------------------------------------------------------------------------------------------------------
void GetHP()
{
	if (doorIndex >= 0 && doorIndex < doorTargets.length())
	{
		int hp = 0;
		for (uint i = 0; i < doorTargets[doorIndex].length; ++i)
		{
			kActor @actor = doorTargets[doorIndex][i];
			if ((actor.Flags() & AF_DEAD) == 0 && actor.Health() > 0)
			{
				hp += actor.Health();
			}
		}
		
		hpCounter.Health() = int(10000.0 * (hp / float(totalHealth[doorIndex])));		
		//Sys.Print("hp = " + hp + ", totalHP = " + totalHealth + ", CounterHP = " + hpCounter.Health());
	}

}