#include "scripts/common.txt"

float pShakeDuration;
float pShakeCurrentTime;
float pShakeAngle;
//------------------------------------------------------------------------------------------------------------------------
void ShowText(const kStr &in topText, const kStr &in middleText = "", const kStr &in bottomText = "", float time = 2.0f)
{
	int iTime = int(60.0f * time - 10);
	Game.PrintLine(bottomText, 0, iTime); //bottom
	Game.PrintLine(middleText, 1, iTime); //middle
	Game.PrintLine(topText, 2, iTime); //top
}
//------------------------------------------------------------------------------------------------------------------------
void ShowDelayedText(const kStr &in topText, const kStr &in middleText = "", const kStr &in bottomText = "", float time = 2.0f)
{
	ShowText(topText, middleText, bottomText, time);
	delay(time);
}
//------------------------------------------------------------------------------------------------------------------------
void ClearText()
{
	int iTime = 10;
	Game.PrintLine("", 0, iTime); //bottom
	Game.PrintLine("", 1, iTime); //middle
	Game.PrintLine("", 2, iTime); //top
}
//------------------------------------------------------------------------------------------------------------------------
void ExitCinematic(int haltScriptID)
{
	Game.HaltMapScript(haltScriptID);
	Camera.StopCinematic();
	delay(0.25f);
	Player.Actor().Flags() |= AF_NODRAW;
	ClearText();
}
//------------------------------------------------------------------------------------------------------------------------
void PlayerSetupShake(const float a, const float d)
{
	pShakeAngle = a * 0.3333333432674408f;
	pShakeDuration = Math::Fabs(d);
	pShakeCurrentTime = pShakeDuration;
	Game.CallDelayedMapScript(102, Player.Actor().CastToActor(), 0); //Player View Shake
}
//------------------------------------------------------------------------------------------------------------------------
void PlayerSetToFloor()
{
	//set player position to floor
	kVec3 pos = Player.Actor().Origin();
	pos.z = Player.Actor().FloorHeight();
	Player.Actor().Origin() = pos;
}
//------------------------------------------------------------------------------------------------------------------------
bool IsIntroFinished()
{
	bool value;
	GameVariables.GetBool("BP_TRIAL_INTRO", value);
	return value;
}
//------------------------------------------------------------------------------------------------------------------------
void SetIntroFinished(const bool value = true)
{
	GameVariables.SetValue("BP_TRIAL_INTRO", value ? "1" : "0");
}
//------------------------------------------------------------------------------------------------------------------------
bool IsSewerCinematicFinished()
{
	bool value;
	GameVariables.GetBool("BP_TRIAL_SEWER", value);
	return value;
}
//------------------------------------------------------------------------------------------------------------------------
void SetSewerCinematicFinished(const bool value = true)
{
	GameVariables.SetValue("BP_TRIAL_SEWER", value ? "1" : "0");
}
//------------------------------------------------------------------------------------------------------------------------
bool IsForceFieldDown()
{
	bool value;
	GameVariables.GetBool("BP_TRIAL_FF", value);
	return value;
}
//------------------------------------------------------------------------------------------------------------------------
void SetForceField(const bool value = true)
{
	GameVariables.SetValue("BP_TRIAL_FF", value ? "1" : "0");
}
//------------------------------------------------------------------------------------------------------------------------
bool HasKey(const int keyIndex)
{
	bool hasKey;
	GameVariables.GetBool("BP_TRIAL_KEY" + keyIndex, hasKey);
	return hasKey;
}
//------------------------------------------------------------------------------------------------------------------------
void SetKey(const int keyIndex, const bool value = true)
{
	GameVariables.SetValue("BP_TRIAL_KEY" + keyIndex, value ? "1" : "0");
}
//------------------------------------------------------------------------------------------------------------------------
bool HasChrono(const int index)
{
	bool has;
	GameVariables.GetBool("BP_TRIAL_CHRONO" + index, has);
	return has;
}
//------------------------------------------------------------------------------------------------------------------------
void SetChrono(const int index, const bool value = true)
{
	GameVariables.SetValue("BP_TRIAL_CHRONO" + index, value ? "1" : "0");
}
//------------------------------------------------------------------------------------------------------------------------
bool IsFortDoorOpen(const int index)
{
	bool isOpen;
	GameVariables.GetBool("BP_TRIAL_FORTDOOR" + index, isOpen);
	return isOpen;
}
//------------------------------------------------------------------------------------------------------------------------
void SetFortDoor(const int index, const bool value = true)
{
	GameVariables.SetValue("BP_TRIAL_FORTDOOR" + index, value ? "1" : "0");
}
//------------------------------------------------------------------------------------------------------------------------
// Common Level Startup - Returns true if is changing map
//------------------------------------------------------------------------------------------------------------------------
bool OnLevelStart(kActor @instigator)
{
	bool init;
	GameVariables.GetBool("BP_TRIAL_INIT", init);
	if (!init)
	{
		GameVariables.Add("BP_TRIAL_INIT", "1");
		GameVariables.Add("BP_TRIAL_INTRO", "0");
		GameVariables.Add("BP_TRIAL_KEY1", "0");
		GameVariables.Add("BP_TRIAL_KEY2", "0");
		GameVariables.Add("BP_TRIAL_FF", "0");
		GameVariables.Add("BP_TRIAL_SEWER", "0");
		GameVariables.Add("BP_TRIAL_CHRONO1", "0");
		GameVariables.Add("BP_TRIAL_CHRONO2", "0");
		GameVariables.Add("BP_TRIAL_CHRONO3", "0");
		
		for (int i = 0; i < 2; ++i)
		{
			GameVariables.Add("BP_TRIAL_FORTDOOR" + i, "0");
		}
		
		for (int i = 0; i < 9; ++i)
		{
			GameVariables.Add("longHunterState" + i, "0");
			GameVariables.Add("longHunterHealth" + i, "0");
			GameVariables.Add("longHunterInitHealth" + i, "0");
			GameVariables.Add("longHunterSector" + i, "0");
			GameVariables.Add("longHunterOrigin" + i, "0");
			GameVariables.Add("longHunterYaw" + i, "0");
		}

		for (int i = 0; i < 5; ++i)
		{
			GameVariables.Add("mantisState" + i, "0");
			GameVariables.Add("mantisHealth" + i, "0");
			GameVariables.Add("mantisInitHealth" + i, "0");
			GameVariables.Add("mantisSector" + i, "0");
			GameVariables.Add("mantisOrigin" + i, "0");
			GameVariables.Add("mantisYaw" + i, "0");
		}

		for (int i = 0; i < 3; ++i)
		{
			GameVariables.Add("trexState" + i, "0");
			GameVariables.Add("trexHealth" + i, "0");
			GameVariables.Add("trexInitHealth" + i, "0");
			GameVariables.Add("trexSector" + i, "0");
			GameVariables.Add("trexOrigin" + i, "0");
			GameVariables.Add("trexYaw" + i, "0");
		}
		
		for (int i = 0; i < 1; ++i)
		{
			GameVariables.Add("campaignerState" + i,  "0");
			GameVariables.Add("campaignerHealth" + i, "0");
			GameVariables.Add("campaignerInitHealth" + i, "0");
			GameVariables.Add("campaignerSector" + i, "0");
			GameVariables.Add("campaignerOrigin" + i, "0");
			GameVariables.Add("campaignerYaw" + i, "0");
		}
		
		for (int i = 0; i < 4; ++i)
		{
			GameVariables.Add("hummerState" + i,  "0");
			GameVariables.Add("hummerHealth" + i, "0");
			GameVariables.Add("hummerInitHealth" + i, "0");
			GameVariables.Add("hummerSector" + i, "0");
			GameVariables.Add("hummerOrigin" + i, "0");
			GameVariables.Add("hummerYaw" + i, "0");
		}
		
		if (Game.GetCurrentMapID() != 101)
		{
			PlayLoop.ChangeMap("levels/trial1.map");
			// PlayLoop.ChangeMap("levels/trial2.map");
			return true;
		}
	}
	
	// Player.GiveWeapon(TW_WEAPON_PISTOL, 200);
	// Player.GiveWeapon(TW_WEAPON_SHOTGUN, 200);
	// Player.GiveWeapon(TW_WEAPON_RIFLE, 200);
	// Player.GiveWeapon(TW_WEAPON_PULSERIFLE, 200);
	// Player.GiveWeapon(TW_WEAPON_MINIGUN, 200);
	// Player.GiveWeapon(TW_WEAPON_GRENADE, 200);
	// Player.GiveWeapon(TW_WEAPON_MISSILE, 200);
	// Player.GiveWeapon(TW_WEAPON_CANNON, 200);
	// Player.GiveWeapon(TW_WEAPON_CHRONO, 200);
	
	// Game.CallDelayedMapScript(100, instigator, 0); //Common Level Update
	
	return false;
}
//------------------------------------------------------------------------------------------------------------------------
// Common Level Update
//------------------------------------------------------------------------------------------------------------------------
// $script 100
// {
	// uint16 buttons = Player.Buttons();
	// if ((buttons & (BC_MAPZOOMIN)) != 0)
	// {
		// PlayLoop.ChangeMap("levels/level09.map");
		// PlayLoop.ChangeMap("levels/trial1.map");
	// }
	// // else if ((buttons & (BC_MAPZOOMOUT)) != 0)
	// // {
		// // SetIntroFinished(false);
	// // }
    // $restart;
// }
//------------------------------------------------------------------------------------------------------------------------
// Constant Screen Shake
//------------------------------------------------------------------------------------------------------------------------
$script 101
{
	Camera.Tremor().x = Math::RandRange(Math::Deg2Rad(-0.5f), Math::Deg2Rad(0.5f));
    Camera.Tremor().y = Math::RandRange(Math::Deg2Rad(-0.5f), Math::Deg2Rad(0.5f));
    Camera.Tremor().z = Math::RandRange(Math::Deg2Rad(-0.5f), Math::Deg2Rad(0.5f));
    delay(0.01f);
    $restart;
}
//------------------------------------------------------------------------------------------------------------------------
// Player View Shake
//------------------------------------------------------------------------------------------------------------------------
$script 102
{
	if (pShakeCurrentTime <= 0.0f)
	{
		return;
	}
	
	if((PlayLoop.Ticks() & 3) == 0)
	{
		float power;
		float dist;
		float av, rv;
	
		power = pShakeCurrentTime / pShakeDuration;
		av = (Math::RandCFloat() * (pShakeAngle * 0.5f)) * power;
		rv = (Math::RandCFloat() * (pShakeAngle * 0.5f)) * power;
			
		kPuppet @target = Player.Actor();
		target.RecoilPitch() = (32.0f * av * 0.00390625f);
		target.Roll() = (32.0f * rv * 0.00390625f);
	}
	
	pShakeCurrentTime -= 0.02f;
		
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
