#include "scripts/map/common_level_script.txt"


//Tag IDs
//500: First Checkpoint/Warp
//1001: Force Field
//1002: Villager - Ammo Store
//1003: Villager - Near Portal
//1004: Villager - Near Jungle Trial
//1005: Villager - Near Cave
//1006: Villager - Fighting 1
//1007: Villager - Fighting 2
//1008: Villager - Near Fireplace
//1009: Villager - Sewer Entrance
//1010: First Trap to jungle
//1011: Rock blocking cave entrance
//1012: Bolok Poster
//1013: Key 1
//1014: Key 2
//1015: 
//1016: Sewer Door Entrance
//1017: Tech Warp 1 Forward
//1018: Tech Warp 1 Backward
//1019: Sewer Checkpoint/Warp
//1020: Tech Warp 2 Forward
//1021: Tech Warp 2 Backward
//1022: Tech Door To Anti Gravity
//1023: Tech Door Near Force Field
//1024: Purlins Drop Near Force Field
//1025: Checkpoint near Force Field
//1026: Save Point
//1027: End Warp Point
//1028: Chrono 1
//1029: Chrono 2
//1030: Chrono 3


float restockAmmoTime;
kActor @villagerFighter1;
kActor @villagerFighter2;
float villagerTalkTimer;
//------------------------------------------------------------------------------------------------------------------------
void RemoveRock(kActor @instigator)
{
	kActor @rock = World.GetActorByTID(1011);
	if (@rock != null)
	{
		World.ChangeSectorHeight(rock.SectorIndex(), rock.Origin().z);
		rock.Remove();
	}
}
//------------------------------------------------------------------------------------------------------------------------
void RemoveChrono(const int index)
{
	kActor @chrono;
	switch(index)
	{
	case 1:
		@chrono = World.GetActorByTID(1028);
		break;
	case 2:
		@chrono = World.GetActorByTID(1029);
		break;
	case 3:
		@chrono = World.GetActorByTID(1030);
		break;
	default:
		break;
	}
	if (@chrono != null)
		chrono.Remove();
}
//------------------------------------------------------------------------------------------------------------------------
void GetChrono(const int index)
{
	SetChrono(index);
	RemoveChrono(index);
	if (HasChrono(1) && HasChrono(2) && HasChrono(3))
	{
		Game.PlaySound("sounds/shaders/generic_234.ksnd");
		Player.GiveWeapon(TW_WEAPON_CHRONO, 5);
		ShowText("Found The Chronoscepter");
	}
	else
	{
		Game.PlaySound("sounds/shaders/generic_4_non_weapon_pickup.ksnd");
		ShowText("Found Chronoscepter Piece");
	}
}
//------------------------------------------------------------------------------------------------------------------------
void RemoveKey(const int keyIndex)
{
	kActor @key;
	switch(keyIndex)
	{
	case 1:
		@key = World.GetActorByTID(1013);
		break;
	case 2:
		@key = World.GetActorByTID(1014);
		break;
	default:
		break;
	}
	key.Remove();
}
//------------------------------------------------------------------------------------------------------------------------
// Start
//------------------------------------------------------------------------------------------------------------------------
$script 0
{
	if (OnLevelStart(instigator))
		return;
	
	if (HasKey(1))
	{
		RemoveKey(1);
		RemoveRock(instigator);
	}
	if (HasKey(2))
	{
		RemoveKey(2);
	}
	if (HasChrono(1))
		RemoveChrono(1);
	if (HasChrono(2))
		RemoveChrono(2);
	if (HasChrono(3))
		RemoveChrono(3);
	
	//villagers //1002-1009
	for (int i = 0; i < 8; i++)
	{
		int tid = 1002 + i;
		kActor @actor = World.GetActorByTID(tid);
		actor.ImpactType() = IT_FLESH_HUMAN;
		actor.Health() = 9999;
		//actor.Flags() |= AF_INVINCIBLE;
	}
	
	@villagerFighter1 = World.GetActorByTID(1006);
	@villagerFighter2 = World.GetActorByTID(1007);

	kActor @poster = World.GetActorByTID(1012);
	poster.Yaw() = Math::Deg2Rad(90.0f);
	poster.Roll() = Math::Deg2Rad(180.0f);
	poster.Pitch() = Math::Deg2Rad(-10.0f);
	
	
	Game.CallDelayedMapScript(1, instigator, 0); //Update Level
	
		
	if (!IsIntroFinished())
	{
		Game.CallDelayedMapScript(2, instigator, 0); //Start Intro Cinematic
	}

}
//------------------------------------------------------------------------------------------------------------------------
// Update Game Level
//------------------------------------------------------------------------------------------------------------------------
$script 1
{
	// uint16 buttons = Player.Buttons();
	// if ((buttons & (BC_MAPZOOMOUT)) != 0)
	// {
		// // Game.CallDelayedMapScript(10, instigator, 0); //Update Sewer Cinematic
		// RemoveRock(instigator);
		// SetKey(1);
	// }
	
	// if ((buttons & (BC_MAPZOOMIN)) != 0)
	// {
		// Sys.Print("AreaID = " + Player.Actor().CastToActor().AreaID());
		// // Game.CallDelayedMapScript(10, instigator, 0); //Update Sewer Cinematic
		// //RemoveRock(instigator);
	// }


	if (restockAmmoTime > 0.0f)
		restockAmmoTime -= GAME_DELTA_TIME;
	
	if (villagerTalkTimer > 0.0f)
		villagerTalkTimer -= GAME_DELTA_TIME;

	if ((villagerFighter1 !is null && !villagerFighter1.IsStale()) && (villagerFighter2 !is null && !villagerFighter2.IsStale()))
	{
		villagerFighter1.SetTarget(villagerFighter2);
		villagerFighter1.CastToAI().AIFlags() |= (1 << 23); //damage panic (forces AI to target)
		villagerFighter1.Health() = 9999;
		villagerFighter2.SetTarget(villagerFighter1);
		villagerFighter2.CastToAI().AIFlags() |= (1 << 23); //damage panic (forces AI to target)
		villagerFighter2.Health() = 9999;
	}
	
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Start Intro Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 2 
{
	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 one, the time has come", "for you to complete the", "Trials of the Warrior", time: 5.0f);
	delay(3.0f);
	ExitCinematic(3);
}
//------------------------------------------------------------------------------------------------------------------------
// Update Intro Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 3 
{
	if (Camera.UserInterrupted())
	{
		ExitCinematic(2);
        return;
    }
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Start Key Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 4
{
	Game.CallDelayedMapScript(5, instigator, 0); //Update Key Cinematic
	
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_UNLOCK_PLAYER_ON_FINISH);
	Camera.SetLookAtActor(Player.Actor().CastToActor());
	Camera.fov = 74.0f;
	Player.Actor().ModelVariation() = TV_KEY_FINAL_OUTER;
	Player.Actor().AnimState().Set(anim_player_acquire_key, 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, 4.0f, CMLT_COSINE);
	delay(1.0f);
	ShowText("Jungle Trial Complete");
	delay(3.0f);
	ExitCinematic(5);
}
//------------------------------------------------------------------------------------------------------------------------
// Update Key Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 5
{
	if (Camera.UserInterrupted())
	{
		ExitCinematic(4);
        return;
    }
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Start Key Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 6
{
	Game.CallDelayedMapScript(7, instigator, 0); //Update Sewer Cinematic
	
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_UNLOCK_PLAYER_ON_FINISH);
	Camera.SetLookAtActor(Player.Actor().CastToActor());
	Camera.fov = 74.0f;
	Player.Actor().ModelVariation() = TV_KNIFE;
	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(180.0f),  // start angle
							Math::Deg2Rad(180.0f),   // dest angle
							10*GAME_SCALE,           // start distance
							15*GAME_SCALE,           // dest distance
							12*GAME_SCALE,           // start height
							12*GAME_SCALE,           // dest height
							10*GAME_SCALE,           // start look at height
							11*GAME_SCALE);          // dest look at height
	Camera.AutoPlayRotationTrack(0, 6.0f, CMLT_COSINE);
	delay(0.5f);
	ShowDelayedText("Well done, warrior", "You have completed your final trial", "Take this trophy", time: 4.0f);

	delay(4.0f);
	ExitCinematic(7);
}
//------------------------------------------------------------------------------------------------------------------------
// Update Sewer Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 7
{
	if (Camera.UserInterrupted())
	{
		ExitCinematic(6);
        return;
    }
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Start Sewer Door Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 8
{
	Game.CallDelayedMapScript(9, instigator, 0); //Update Sewer Cinematic
	
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_UNLOCK_PLAYER_ON_FINISH);
	Camera.SetLookAtActor(Player.Actor().CastToActor());
	Camera.fov = 74.0f;
	Player.Actor().ModelVariation() = TV_KNIFE;
	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(180.0f),  // start angle
							Math::Deg2Rad(180.0f),   // dest angle
							10*GAME_SCALE,           // start distance
							15*GAME_SCALE,           // dest distance
							12*GAME_SCALE,           // start height
							12*GAME_SCALE,           // dest height
							10*GAME_SCALE,           // start look at height
							11*GAME_SCALE);          // dest look at height
	Camera.AutoPlayRotationTrack(0, 6.0f, CMLT_COSINE);
	World.TriggerActorsByTID(instigator, 1016); //Force Field
	delay(2.0f);
	ShowDelayedText("It seems there's more to do, warrior", "Be careful", time: 4.0f);
	ExitCinematic(9);
}
//------------------------------------------------------------------------------------------------------------------------
// Update Sewer Door Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 9
{
	if (Camera.UserInterrupted())
	{
		ExitCinematic(8);
        return;
    }
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
// End Game Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 10
{
	Game.CallDelayedMapScript(11, instigator, 0); //Update End Game Cinematic
	
	Camera.StartCinematic(CMF_LOCK_PLAYER|CMF_UNLOCK_PLAYER_ON_FINISH);
	Camera.SetLookAtActor(Player.Actor().CastToActor());
	Camera.fov = 74.0f;
	Player.Actor().ModelVariation() = TV_KNIFE;
	Player.Actor().AnimState().Set(anim_aiStanding, 8.0f, ANF_LOOP);
	Player.Actor().Flags() &= ~AF_NODRAW;
	Player.Actor().Pitch() = 0;
	Player.Actor().Roll() = 0;
	Player.Actor().Yaw() = Math::Deg2Rad(90.0f);
	Player.Actor().Origin() = kVec3(9676.0f, -1052.0f, Player.Actor().FloorHeight());
	
	Camera.SetFinalView(0);
	Camera.SetRotationTrack(0,
							Math::Deg2Rad(0.0f),  // start angle
							Math::Deg2Rad(0.0f),   // dest angle
							40*GAME_SCALE,           // start distance
							220*GAME_SCALE,           // dest distance
							20*GAME_SCALE,           // start height
							40*GAME_SCALE,           // dest height
							9*GAME_SCALE,           // start look at height
							9*GAME_SCALE);          // dest look at height
	Camera.AutoPlayRotationTrack(0, 10.0f, CMLT_COSINE);
	delay(1.0f);
	ShowText("With his trials complete", "He becomes a true warrior of his village", time: 6.0f);
	delay(6.0f);
	Game.PlaySound("sounds/shaders/generic_234.ksnd");
	delay(2.0f);
	PlayLoop.ChangeMap("levels/level02.map");
	ExitCinematic(11);
}
//------------------------------------------------------------------------------------------------------------------------
// Update End Game Cinematic
//------------------------------------------------------------------------------------------------------------------------
$script 11
{
	if (Camera.UserInterrupted())
	{
		PlayLoop.ChangeMap("levels/level02.map");
		ExitCinematic(10);
        return;
    }
	$restart;
}

//------------------------------------------------------------------------------------------------------------------------
// Pressed Force Field Switch
//------------------------------------------------------------------------------------------------------------------------
$script 10000
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && !IsForceFieldDown())
	{
		Game.PlaySound("sounds/shaders/generic_177.ksnd");
		Game.PrintLine("Force field offline", 0);
		World.TriggerActorsByTID(instigator, 1101); //Force Field
		SetForceField();
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Talk to Cave Entrance Guard
//------------------------------------------------------------------------------------------------------------------------
$script 10001
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && villagerTalkTimer <= 0.0f)
	{
		villagerTalkTimer = 2.0f;
		if (!HasKey(1))
		{
			ShowText("Complete the trials of the jungle before", "entering the caves of despair", time: 4.0f);
		}
		else
		{
			ShowText("Enter the caves of despair and", "complete your trials", time: 3.0f);
		}
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Talk to Store Villager
//------------------------------------------------------------------------------------------------------------------------
$script 10002
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0)
	{
		if (restockAmmoTime <= 0.0f)
		{
			for (int weaponID = 0; weaponID < NUMTUROKWEAPONS; ++weaponID)
			{
				if (Player.HasWeapon(weaponID))
				{
					Player.GiveWeapon(weaponID, 500);
				}
			}
			ShowText("Restock your ammo");
			Game.PlaySound("sounds/shaders/generic_4_non_weapon_pickup.ksnd");
			restockAmmoTime = 60.0f;
		}
		else
		{
			ShowText("Thank you, come again");
		}
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Talk to Village Fighters
//------------------------------------------------------------------------------------------------------------------------
$script 10003
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && villagerTalkTimer <= 0.0f)
	{
		villagerTalkTimer = 2.0f;
		ShowText("True warriors fight to the end");
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Talk to Villager Near Jungle Trial
//------------------------------------------------------------------------------------------------------------------------
$script 10004
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && villagerTalkTimer <= 0.0f)
	{
		villagerTalkTimer = 2.0f;
		if (!HasKey(1))
		{
			ShowText("The Trial of the Jungle is up the hill");
		}
		else
		{
			ShowText("I was observing you from the distance", "You did amazingly well for a newcomer", time: 4.0f);
		}			
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Talk to Villager Near Portal
//------------------------------------------------------------------------------------------------------------------------
$script 10005
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && villagerTalkTimer <= 0.0f)
	{
		villagerTalkTimer = 2.0f;
		if (IsFortDoorOpen(1))
		{
			ShowText("You look like you've just", "been through Oblivion", time: 3.0f);
		}
		else
		{
			ShowText("Once your trials are complete you", "will become a true warrior");
		}
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Talk to Villager Near Fireplace
//------------------------------------------------------------------------------------------------------------------------
$script 10006
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && villagerTalkTimer <= 0.0f)
	{
		villagerTalkTimer = 2.0f;
		ShowText("Complete your trials and", "become strong, warrior");
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Get Key 1
//------------------------------------------------------------------------------------------------------------------------
$script 10007
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && !HasKey(1))
	{
		RemoveRock(instigator);
		Game.CallDelayedMapScript(4, instigator, 0); //Start Key Cinematic
		Game.PlaySound("sounds/shaders/generic_4_non_weapon_pickup.ksnd");
		SetKey(1);
		RemoveKey(1);
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Get Key 2
//------------------------------------------------------------------------------------------------------------------------
$script 10008
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && !HasKey(2))
	{
		Game.CallDelayedMapScript(8, instigator, 0); //Start Sewer Door Cinematic
		Game.PlaySound("sounds/shaders/generic_4_non_weapon_pickup.ksnd");
		SetKey(2);
		RemoveKey(2);
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Talk to Villager Near Sewer Entrance
//------------------------------------------------------------------------------------------------------------------------
$script 10010
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && villagerTalkTimer <= 0.0f)
	{
		villagerTalkTimer = 2.0f;
		for (int weaponID = 0; weaponID < NUMTUROKWEAPONS; ++weaponID)
		{
			if (Player.HasWeapon(weaponID))
			{
				Player.GiveWeapon(weaponID, 500);
			}
		}
		ShowText("Take this ammo You may need it");
		Game.PlaySound("sounds/shaders/generic_4_non_weapon_pickup.ksnd");
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Sewer First Entrance
//------------------------------------------------------------------------------------------------------------------------
$script 10011
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && !IsSewerCinematicFinished())
	{
		SetSewerCinematicFinished();
		Game.CallDelayedMapScript(6, instigator, 0); //Start Sewer Cinematic
	}
}
//------------------------------------------------------------------------------------------------------------------------
// End Game
//------------------------------------------------------------------------------------------------------------------------
$script 10012
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && IsFortDoorOpen(1))
	{
		Game.CallDelayedMapScript(10, instigator, 0); //Start End Game Cinematic
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Chrono 1
//------------------------------------------------------------------------------------------------------------------------
$script 10013
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && !HasChrono(1))
	{
		GetChrono(1);
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Chrono 2
//------------------------------------------------------------------------------------------------------------------------
$script 10014
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && !HasChrono(2))
	{
		GetChrono(2);
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Chrono 3
//------------------------------------------------------------------------------------------------------------------------
$script 10015
{
	if ((instigator.Flags() & AF_ENTEREDAREAEVENT) == 0 && !HasChrono(3))
	{
		GetChrono(3);
	}
}
//------------------------------------------------------------------------------------------------------------------------
