#include "scripts/common.txt"
#include "scripts/BP_common.txt"
#include "scripts/BP_MapCommon.txt"

//Tag IDs
//2015: End Checkpoint
//1000: Start Warp
//1001: Face Door
//1002: Lava Platform 1
//1003: Lava Platform 2
//1004: Lava Platform 3
//1005: Lava Platform 4
//1006: Lava Surprise Door 1
//1007: Lava Platform 5
//1008: Lava Surprise Door 2
//1009: Lava Platform 6
//1010: Lava Destruct Walls + Purlins
//1011: Lava End Door
//1012: Lava End Save
//1013: Pillar 1
//1014: Puzzle Tomb Face
//1015: Puzzle Shaman Face
//1016: Puzzle Lizard Smile
//1017: Puzzle Purlin Face down right
//1018: Puzzle Raptors
//1019: Puzzle Aliens
//1020: Puzzle Blank
//1021: Puzzle Tomb Face (Overlay)
//1022: Puzzle Shaman Face (Overlay)
//1023: Puzzle Lizard Smile (Overlay)
//1024: Puzzle Purlin Face down right (Overlay)
//1025: Puzzle Raptors (Overlay)
//1026: Puzzle Aliens (Overlay)
//1027: Puzzle Blank (Overlay)
//1028: Pillars Blocking Puzzle
//1029: Pillar Puzzle Tomb Face + Enemies drop + Event Link
//1030: Pillar Puzzle Shaman Face
//1031: Pillar Puzzle Lizard Smile
//1032: Pillar Puzzle Purlin Face down right
//1033: Pillar Puzzle Raptors
//1034: Pillar Puzzle Aliens
//1035: Pillar Puzzle Blank
//1036: Warp Enter Puzzle Tomb Face
//1037: Warp Enter Puzzle Shaman Face
//1038: Warp Enter Puzzle Lizard Smile
//1039: Warp Enter Puzzle Purlin Face down right
//1040: Warp Enter Puzzle Raptors
//1041: Warp Enter Puzzle Aliens
//1042: Warp Enter Puzzle Blank
//1043: Warp Exit Puzzle Tomb Face
//1044: Warp Exit Puzzle Shaman Face
//1045: Warp Exit Puzzle Lizard Smile
//1046: Warp Exit Puzzle Purlin Face down right
//1047: Warp Exit Puzzle Raptors
//1048: Warp Exit Puzzle Aliens
//1049: Warp Exit Puzzle Blank
//1050: Purlin Wall Before Lava
//1051: Beginning First Platform
//1052: Purlin Wall Warp Before Lava
//1053: Tar Surprise Door + enemies
//1054: Purlin Wall Warp 2 Before Lava
//1055: Spirit Bridge 1
//1056: Spirit Bridge 2
//1057: First Purlin Wall Surpirse
//1058: Checkpoint 1 Warp
//1059: Checkpoint 2 Warp
//1060: 2 purlins drop down
//1061: center button script
//1062: center button bit
//1063: Exit Warp
//1064: Puzzle Complete Gate
//1065: Leaper Enemies
//1066: Spirit Bridge Secret Warp
//1067: Button 1 for heart doors + pillars
//1068: Button 2 pillar to button 1
//1069: Button 3 pillar to button 1
//1070: Button 4 pillar to button 1
//1071: minigun enemies drop
//1072: Underwater heart of fire gate
//1073: under heart of fire leaper
//1074: end top secret enemies drop
//1075: 


kActor@ buttonBit;
array<kActor@> puzzlesActive;
int spikeDamageAccum;
int spikeDamageAccum2;
//------------------------------------------------------------------------------------------------------------------------
// Start Map
//------------------------------------------------------------------------------------------------------------------------
$script 0
{
	StartMap();

	@buttonBit = World.GetActorByTID(1062);
	
	for (int i = 0; i < 7; i++)
	{
		puzzlesActive.insertLast(World.GetActorByTID(1021 + i));
	}

	if (buttonBit.IsPersistentMarked())
	{
		World.TriggerActorsByTID(instigator, 1028); //Puzzle Pillars + enemies
	}
	
	Game.CallDelayedMapScript(1, instigator, 0); //Update Map	
}
//------------------------------------------------------------------------------------------------------------------------
// Update Map
//------------------------------------------------------------------------------------------------------------------------
$script 1
{
	TickMap();
	
	kActor@ pActor = Player.Actor().CastToActor();
	kVec3 p = pActor.Origin();
	if (p.y < -4000.0f && p.y > -5100.0f && p.z >= 3400.0f)
	{
		spikeDamageAccum2++;
		if (spikeDamageAccum2 > 5)
		{
			spikeDamageAccum2 = 0;
			pActor.InflictGenericDamage(pActor, 4);
		}
	}
	
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Enemy Killed
//------------------------------------------------------------------------------------------------------------------------
$script 9999
{

}
//------------------------------------------------------------------------------------------------------------------------
void PuzzleWarp(kActor @instigator, const int warpID, array<int> triggerIDs)
{
	PlayLoop.StartWarp(Player.Actor().CastToActor(), warpID, Game.GetCurrentMapID());
	Game.PlaySound("sounds/shaders/trex_scream.ksnd");
	for (int i = 0; i < int(triggerIDs.length()); i++)
	{
		World.TriggerActorsByTID(instigator, triggerIDs[i]);
	}
	
	int activeCount = 0;
	for (int i = 0; i < int(puzzlesActive.length()); i++)
	{
		if (puzzlesActive[i].IsPersistentMarked())
		{
			activeCount++;
		}
	}
	if (activeCount == 6)
	{
		World.TriggerActorsByTID(instigator, 1027); //Puzzle Blank (Overlay)
		World.TriggerActorsByTID(instigator, 1064); //Puzzle Complete Gate
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Puzzle Tomb Face
//------------------------------------------------------------------------------------------------------------------------
$script 1014
{
	if (!puzzlesActive[0].IsPersistentMarked() && Player.Actor().OnGround())
	{
		PuzzleWarp(instigator, 1036, array<int>={1021,1029}); //Puzzle Tomb Face (Overlay) and Pillar Tomb Face
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Puzzle Shaman Face
//------------------------------------------------------------------------------------------------------------------------
$script 1015
{
	if (!puzzlesActive[1].IsPersistentMarked() && Player.Actor().OnGround())
	{
		PuzzleWarp(instigator, 1037, array<int>={1022,1030}); //Puzzle Shaman Face (Overlay) and Pillar Shaman Face
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Puzzle Lizard Smile
//------------------------------------------------------------------------------------------------------------------------
$script 1016
{
	if (!puzzlesActive[2].IsPersistentMarked() && Player.Actor().OnGround())
	{
		PuzzleWarp(instigator, 1038, array<int>={1023,1031}); //Puzzle Lizard Smile (Overlay) and Pillar Lizard Smile
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Puzzle Purlin Face down right
//------------------------------------------------------------------------------------------------------------------------
$script 1017
{
	if (!puzzlesActive[3].IsPersistentMarked() && Player.Actor().OnGround())
	{
		PuzzleWarp(instigator, 1039, array<int>={1024,1032}); //Puzzle Purlin Face down right (Overlay) and Pillar Purlin Face down right
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Puzzle Raptors
//------------------------------------------------------------------------------------------------------------------------
$script 1018
{
	if (!puzzlesActive[4].IsPersistentMarked() && Player.Actor().OnGround())
	{
		PuzzleWarp(instigator, 1040, array<int>={1025,1033}); //Puzzle Raptors (Overlay) and Pillar Raptors
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Puzzle Aliens
//------------------------------------------------------------------------------------------------------------------------
$script 1019
{
	if (!puzzlesActive[5].IsPersistentMarked() && Player.Actor().OnGround())
	{
		PuzzleWarp(instigator, 1041, array<int>={1026,1034}); //Puzzle Aliens (Overlay) and Pillar Aliens
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Puzzle Blank
//------------------------------------------------------------------------------------------------------------------------
$script 1020
{

}
//------------------------------------------------------------------------------------------------------------------------
// Center Button Pressed
//------------------------------------------------------------------------------------------------------------------------
$script 1061
{
	if (!buttonBit.IsPersistentMarked())
	{
		World.TriggerActorsByTID(instigator, 1028); //Puzzle Pillars + enemies
		World.TriggerActorsByTID(instigator, 1062); //Center Button Bit
	}
}
//------------------------------------------------------------------------------------------------------------------------
// First lava platform fall trap
//------------------------------------------------------------------------------------------------------------------------
$script 10000
{
	if (Game.GetDifficulty() >= DIFFICULTY_HARDCORE)
	{
		World.TriggerActorsByTID(instigator, 1004); //lava platform 3
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Minigun lava platform fall trap
//------------------------------------------------------------------------------------------------------------------------
$script 10001
{
	if (Game.GetDifficulty() >= DIFFICULTY_HARDCORE)
	{
		World.TriggerActorsByTID(instigator, 1007); //lava platform 3
	}
	World.TriggerActorsByTID(instigator, 1071); //mingun enemies drop
}
//------------------------------------------------------------------------------------------------------------------------
// In area with Tar and spike ceiling
//------------------------------------------------------------------------------------------------------------------------
$script 10002
{
	kActor@ pActor = Player.Actor().CastToActor();
	if (pActor.Origin().z >= pActor.CeilingHeight() - 150.0f)
	{
		spikeDamageAccum++;
		if (spikeDamageAccum > 5)
		{
			spikeDamageAccum = 0;
			pActor.InflictGenericDamage(pActor, 4);
		}
	}
}
//------------------------------------------------------------------------------------------------------------------------

