#library "Tomtefar"
#include "zcommon.acs"
bool PistolStuffSpawned;
int ShotgunStuffSpawned;
int Scythe2MapChecker;

global int 0: MapPlayed[];
str Mapname[32] = { "Map01", "Map02", "Map03", "Map04", 
"Map05", "Map06", "Map07", "Map08", "Map09", "Map10", 
"Map11", "Map12", "Map13", "Map14", "Map15", "Map16", 
"Map17", "Map18", "Map19", "Map20", "Map21", "Map22", 
"Map23", "Map24", "Map25", "Map26", "Map27", "Map28", 
"Map29", "Map30", "Map31", "Map32", };

//	Hi!
//	So you're looking at the scripts to see how some things are made? :)
//	As with everything else within my mod, feel free to copy, use and modify
//	any of the following code for your own mod as you wish.
//	It would be nice if you could leave some credits where appropriate though.
//	Cheers!
//
//	Oh, some stuff are quite messy right now since I'm working to fix a few bugs
//	related to playing the mod on net games that I didn't have time to fix for this version.
//	Sorry about that.



//str weapon;
//weapon = GetWeapon();
//SetWeapon(weapon);

//return state
//SetActorState
//FireCustomMissile

//"####"
//"----"

script "TE_UseInput" ENTER
{
	int buttons;

	while (TRUE)
	{
		buttons = GetPlayerInput(-1, INPUT_BUTTONS);

		if (buttons & BT_USE)
		{
			//PrintBold(s:":::::Pressed:::::");
			TakeInventory("PressedUse", 100);
			
			int uid1 = UniqueTID();
			int uid2 = UniqueTID();
			int ptc = GetActorPitch(0)>>8;
			int ang = GetActorAngle(0)>>8;
			int speed = cos(GetActorPitch(0)) * 64 >> 16;
			int vspeed = -sin(GetActorPitch(0)) * 64 >> 16;
			
			SpawnProjectile(0, "TE_UseAttack", ang, speed, vspeed, 0, uid1);
			SetActorPosition(uid1, GetActorX(uid1), GetActorY(uid1), GetActorZ(uid1)+40.0, FALSE);
			SetActorPitch(uid1, ptc);
			SetPointer(AAPTR_TARGET, 0, uid1);
			
			SpawnProjectile(0, "TE_StealthAttack", ang, speed, vspeed, 0, uid2);
			SetActorPosition(uid2, GetActorX(uid2), GetActorY(uid2), GetActorZ(uid2)+40.0, FALSE);
			SetActorPitch(uid2, ptc);
			SetPointer(AAPTR_TARGET, 0, uid2);
			
			//Thing_ChangeTID(uid, 0);
			
			while (buttons & BT_USE)
			{
				buttons = GetPlayerInput(-1, INPUT_BUTTONS);
				delay(3);
			}
		}
		delay(1);
	}
}






// --------------------------------------------------------------------------
//
//	Start stuff
//
// --------------------------------------------------------------------------

script 999 ENTER //Once per player
{
	delay(2);
	if (GameType () != GAME_NET_DEATHMATCH)
	{
		if(CheckInventory("IsRandomlevel") == 0 && CheckInventory("PlayingRandomlevels") == 1)
		{
			GiveInventory("IsRandomlevel", 1);
			ACS_Execute(1000,0,0,0,0);
			terminate;
		} else {
			TakeInventory("IsRandomlevel", 1);
		}
	}
	GiveInventory("LevelCounter", 1);
	ACS_Execute(1046,0,0,0,0); //Idle checker
	ACS_Execute(1050,0,0,0,0); //C-4 charges indicator
	delay(5);
	if(CheckInventory("bd_sbartype1") < 1)
	{ if(CheckInventory("bd_sbartype2") < 1)
	{ if(CheckInventory("bd_sbartype3") < 1)
	{ if(CheckInventory("bd_sbartype4") < 1)
	{ if(CheckInventory("bd_sbartype5") < 1)
	{ if(CheckInventory("bd_sbartype6") < 1)
	{ if(CheckInventory("bd_sbartype7") < 1)
	{
	ACS_NamedExecute("BDRespawn",0);
	}}}}}}}
}

script 1000 (VOID)
{
	int RandomNumber = random(0,31);
	//print(s:":::::MapScript:::::");
	
	if(MapPlayed[RandomNumber] == 1){
		//print(s:"Har redan spelat ",d:RandomNumber, s:Mapname[RandomNumber] );
		Delay(1);
		restart;
	} else {
		//print(s:"Ny bana: ",d:RandomNumber, s:Mapname[RandomNumber] );
		
		if(CheckInventory("LevelCounter") < 30 && RandomNumber == 29)
		{
			Delay(1);
			restart;
		}
		
		MapPlayed[RandomNumber] = 1;
		ChangeLevel(Mapname[RandomNumber],0,CHANGELEVEL_NOINTERMISSION,-1);
	}
}


// --------------------------------------------------------------------------
//
//	Spawn checkers
//
// --------------------------------------------------------------------------

Script 1001 (int X)	//New weapon checker
{
	if(X == 1){
	SetResultValue(GetCVar("TE_Weap_Pistol"));
	}else if(X == 2){
	SetResultValue(GetCVar("TE_Weap_Revolver"));
	}else if(X == 3){
	SetResultValue(GetCVar("TE_Weap_AutoShotgun"));
	}else if(X == 4){
	SetResultValue(GetCVar("TE_Weap_Sniper"));
	}else if(X == 5){
	SetResultValue(GetCVar("TE_Weap_Railgun"));
	}else if(X == 6){
	SetResultValue(GetCVar("TE_Weap_C4"));
	}else{
	SetResultValue(0);
	}
}

Script 1002 (int X) //BDv21 weapon checker
{
	if(X == 0){
	SetResultValue(GetCVar("TE_Weap_SMG"));
	}else if(X == 1){
	SetResultValue(GetCVar("TE_Weap_AA12"));
	}else if(X == 2){
	SetResultValue(GetCVar("TE_Weap_Machinegun"));
	}else if(X == 3){
	SetResultValue(GetCVar("TE_Weap_GrenadeLauncher"));
	}else if(X == 4){
	SetResultValue(GetCVar("TE_Weap_Unmaker"));
	}else if(X == 5){
	SetResultValue(GetCVar("TE_Weap_BFG10k"));
	}else if(X == 6){
	SetResultValue(GetCVar("TE_Weap_Rifle"));
	}else if(X == 7){
	SetResultValue(GetCVar("TE_Weap_Flamer"));
	}else if(X == 8){
	SetResultValue(GetCVar("TE_Weap_HellLauncher"));
	}else if(X == 9){
	SetResultValue(GetCVar("TE_Weap_MP40"));
	}else if(X == 10){
	SetResultValue(GetCVar("TE_Weap_Buzzsaw"));
	}else if(X == 11){
	SetResultValue(GetCVar("TE_Weap_Grenades"));
	}else{
	SetResultValue(0);
	}
}

Script 1003 (int X)	//New enemy checker
{
	if(X == 0){
	SetResultValue(GetCVar("TE_DynamicSpawn"));
	}else if(X == 1){
	SetResultValue(GetCVar("TE_PistolZombie"));
	}else if(X == 2){
	SetResultValue(GetCVar("TE_SMGZombie"));
	}else if(X == 3){
	SetResultValue(GetCVar("TE_SSGZombie"));
	}else if(X == 4){
	SetResultValue(GetCVar("TE_AxeZombie"));
	}else{
	SetResultValue(0);
	}
}


// --------------------------------------------------------------------------
//
//	Monster spawner
//
// --------------------------------------------------------------------------

Script "TE_MonsterChecker" (int X) NET
{
	if(X == 0) //Hell Knight
	{
		if(GetCvar("TE_Knight") == 0)
		{
			SetActorState(0, "Vanilla");
			terminate;
		}
	}
	if(X == 1) //Baron of Hell
	{
		if(GetCvar("TE_Baron") == 0)
		{
			SetActorState(0, "Vanilla");
			terminate;
		}
	}
	if(X == 2) //Cacodemon
	{
		if(GetCvar("TE_Cacodemon") == 0)
		{
			SetActorState(0, "Vanilla");
			terminate;
		}
	}
	if(X == 3) //Ifrit
	{
		if(GetCvar("TE_Ifrit") == 0)
		{
			SetActorState(0, "Vanilla");
			terminate;
		}
	}
	if(X == 4) //Pistol Zombie
	{
		if(GetCvar("TE_PistolZombie") == 0)
		{
			SetActorState(0, "Vanilla");
			terminate;
		}
	}
	if(X == 5) //SMG Zombie
	{
		if(GetCvar("TE_SMGZombie") == 0)
		{
			SetActorState(0, "Vanilla");
			terminate;
		}
	}
	if(X == 6) //SSG Zombie
	{
		if(GetCvar("TE_SSGZombie") == 0)
		{
			SetActorState(0, "Vanilla");
			terminate;
		}
	}
	if(X == 7) //Axe Zombie
	{
		if(GetCvar("TE_AxeZombie") == 0)
		{
			SetActorState(0, "Vanilla");
			terminate;
		}
	}
}


// --------------------------------------------------------------------------
//
//	Graphics
//
// --------------------------------------------------------------------------

Script 1020 (int X)//Check Graphics
{
	if(X == 0){
	SetResultValue(GetCVar("TE_Enviro"));
	}else if(X == 1){
	SetResultValue(GetCVar("TE_Graphics"));
	}else if(X == 2){
	SetResultValue(GetCVar("TE_Flashlight"));
	}else{
	SetResultValue(0);
	}
}

Script 1021 (VOID)//SSG animation
{
	SetResultValue(GetCVar("TE_SSG"));
}

Script 1022 (VOID)//Check shotgun strap physics
{
	if(GetCvar("TE_Shotgunstrap") == 1)
	{
		GiveInventory("useshotgunstrap", 1);
	}
}

Script "TE_WeaponSprites" (VOID)
{
	if(GetCvar("TE_Sprites_Shotgun") == 1 && CheckInventory("ShotgunSelected") == 1)
	{
		GiveInventory("BD21graphics", 1);
	}
	if(GetCvar("TE_Sprites_Buzzsaw") == 1 && CheckInventory("MinigunSelected") == 1)
	{
		GiveInventory("BD21graphics", 1);
	}
	if(GetCvar("TE_Sprites_SSG") == 1 && CheckInventory("SSGSelected") == 1)
	{
		GiveInventory("BD21graphics", 1);
	}
	if(GetCvar("TE_Sprites_SSG") == 2 && CheckInventory("SSGSelected") == 1)
	{
		GiveInventory("BD21graphics", 2);
	}
	if(GetCvar("TE_Sprites_Grenade") == 1 && CheckInventory("FistsSelected") == 1)
	{
		GiveInventory("BD21graphics", 1);
	}
}

Script 1025 (int X) NET//Check weapon sprites
{
	if(X == 0){
	SetResultValue(GetUserCVar(PlayerNumber(),("TE_Sprites_Shotgun")));
	}else if(X == 1){
	SetResultValue(GetUserCVar(PlayerNumber(),("TE_Sprites_Buzzsaw")));
	}else if(X == 2){
	SetResultValue(GetUserCVar(PlayerNumber(),("TE_Sprites_SSG")));
	}else if(X == 3){
	SetResultValue(GetUserCVar(PlayerNumber(),("TE_Sprites_Grenade")));
	}else{
	SetResultValue(0);
	}
}


// --------------------------------------------------------------------------
//
//	Misc. Options
//
// --------------------------------------------------------------------------

Script "TE_StartWeapon" (VOID)
{
	While(CheckInventory("dummytoken") < GetCVar("TE_StartWeapon"))
	{
		GiveInventory("dummytoken", 1);
	}
}

Script "TE_SmartAmmo" (VOID)
{
	if(GetCvar("TE_SmartAmmo") == 0)
	{
		SetActorState(0, "NoSmart");
	}
}

Script 1023 (int X) NET
{
	if(X == 0){
	SetResultValue(GetCVar("TE_StartWeapon"));
	}else if(X == 1){
	SetResultValue(GetCVar("TE_SmartAmmo"));
	}else if(X == 2){
	SetResultValue(GetCVar("TE_BFGbehaviour"));
	}else if(X == 3){
	SetResultValue(GetCVar("TE_RocketLauncherAltFire"));
	}else{
	SetResultValue(0);
	}
}
/*
Script 1024 (VOID)//BFG behaviour
{
	SetResultValue(GetCVar("TE_BFGbehaviour"));
}

Script 1026 (VOID)//Rocket Launcher alt fire type
{
	SetResultValue(GetCVar("TE_RocketLauncherAltFire"));
}
*/
Script 1027 (VOID)//Check if this game should play random level progression
{
	SetResultValue(GetCVar("TE_RandomLevel"));
}

Script 1028 (VOID)//Check dualwielding
{
	SetResultValue(GetCVar("TE_DualWielding"));
}

Script 1029 (VOID)//Check ADS mode
{
	SetResultValue(GetCVar("TE_ADSmode"));
}

Script "TE_DMweapons" (VOID)//Check if we should use the new weapons in DM
{
	SetResultValue(GetCVar("TE_DMweapons"));
}

// --------------------------------------------------------------------------
//
//	Keybinds
//
// --------------------------------------------------------------------------

Script 1031 (VOID) NET//ThrowC4
{
	if(CheckInventory("C4Weapon"))
	{
		if(CheckInventory("C4Ammo"))
		{
			GiveInventory("GoThrowC4", 1);
			SetWeapon("C4Weapon");
		} else {
			Print(s:"No C-4 Left.");
		}
	} else {
		Print(s:"You don't have a C-4 detonator.");
	}
}

Script 1032 (VOID) NET//WeapSpecial
{
	GiveInventory("WeapSpecial", 1);
}


// --------------------------------------------------------------------------
//
//	Gameplay stuff
//
// --------------------------------------------------------------------------

Script "TE_ColorTranslations" Open
{
	CreateTranslation (98, 200:207=112:119, 240:247=120:127);
	CreateTranslation (99, 200:207=200:207, 240:247=240:247);
}

Script "TE_BruiserBlood1" (VOID)
{
	Thing_SetTranslation(0, 98);
}

Script "TE_BruiserBlood2" (VOID)
{
	Thing_SetTranslation(0, 99);
}

Script 1041 (VOID)//Check if player is running
{
	int buttonSpeed = GetPlayerInput(-1, INPUT_BUTTONS);
		
	if (buttonSpeed & BT_SPEED)
	{
		SetResultValue(1);
	} else {
		SetResultValue(0);
	}
}

Script 1042 (VOID)//Pistol stuff spawned?
{
	if (PistolStuffSpawned == ON)
	{
		SetResultValue(1);
	} else {
		PistolStuffSpawned = ON;
		SetResultValue(0);
	}
}

Script 1043 (VOID)//Ammo drop counters.
{
	while (CheckWeapon("U_AmmoDropper"))
	{
		SetFont("BIGFONT");
		HudMessage(s:"Press any weapon number to exit";	HUDMSG_PLAIN, 0, CR_GRAY, 0.5, 0.20, 0.2);
		HudMessage(s:"Drop ammo with primary fire";		HUDMSG_PLAIN, 0, CR_GRAY, 0.5, 0.80, 0.2);
		HudMessage(s:"Cycle ammo with secondary fire";	HUDMSG_PLAIN, 0, CR_GRAY, 0.5, 0.85, 0.2);
		SetFont("SMALLFONT");

		int ammo1 = CheckInventory("Magazine9mm");
		HudMessage(d:ammo1; HUDMSG_PLAIN, 0, CR_WHITE, 0.140, 0.605, 0.2, 2.0);
		int ammo2 = CheckInventory("RevolverBullets");
		HudMessage(d:ammo2; HUDMSG_PLAIN, 0, CR_WHITE, 0.212, 0.605, 0.2, 2.0);
		int ammo3 = CheckInventory("AmmoShell");
		HudMessage(d:ammo3; HUDMSG_PLAIN, 0, CR_WHITE, 0.284, 0.605, 0.2, 2.0);
		int ammo4 = CheckInventory("GreenShell");
		HudMessage(d:ammo4; HUDMSG_PLAIN, 0, CR_WHITE, 0.356, 0.605, 0.2, 2.0);
		int ammo5 = CheckInventory("Clip2");
		HudMessage(d:ammo5; HUDMSG_PLAIN, 0, CR_WHITE, 0.428, 0.605, 0.2, 2.0);
		int ammo6 = CheckInventory("MinigunAmmo");
		HudMessage(d:ammo6; HUDMSG_PLAIN, 0, CR_WHITE, 0.500, 0.605, 0.2, 2.0);
		int ammo7 = CheckInventory("AmmoRocket");
		HudMessage(d:ammo7; HUDMSG_PLAIN, 0, CR_WHITE, 0.572, 0.605, 0.2, 2.0);
		int ammo8 = CheckInventory("AmmoCell");
		HudMessage(d:ammo8; HUDMSG_PLAIN, 0, CR_WHITE, 0.644, 0.605, 0.2, 2.0);
		int ammo9 = CheckInventory("RailCore");
		HudMessage(d:ammo9; HUDMSG_PLAIN, 0, CR_WHITE, 0.716, 0.605, 0.2, 2.0);
		int ammo10 = CheckInventory("Gasoline");
		HudMessage(d:ammo10; HUDMSG_PLAIN, 0, CR_WHITE, 0.788, 0.605, 0.2, 2.0);
		int ammo11 = CheckInventory("GrenadeAmmo");
		HudMessage(d:ammo11; HUDMSG_PLAIN, 0, CR_WHITE, 0.860, 0.605, 0.2, 2.0);
		delay(6);
	}
}

script 1044 (VOID)//Machinegun grenade indicator
{
	while (CheckWeapon("U_Machinegun"))
	{
		int X = GetCVar("TE_MGX");
		int Y = GetCVar("TE_MGY");
		int Z = GetCVar("TE_MGimg");
		if(CheckInventory("FiredMGGrenade"))
		{
			if (Z == 0) SetFont("TEMGOff0");
			if (Z == 1) SetFont("TEMGOff1");
			if (Z == 2) SetFont("TEMGOff2");
			if (Z == 3) SetFont("TEMGOff3");
			if (Z == 4) SetFont("TNT1A0");
			HudMessage(s:"A"; HUDMSG_PLAIN, 0, 0, (X*0.01), (Y*0.01), 0.2);
		} else {
			if (Z == 0) SetFont("TEMGOn0");
			if (Z == 1) SetFont("TEMGOn1");
			if (Z == 2) SetFont("TEMGOn2");
			if (Z == 3) SetFont("TEMGOn3");
			if (Z == 4) SetFont("TNT1A0");
			HudMessage(s:"A"; HUDMSG_PLAIN, 0, 0, (X*0.01), (Y*0.01), 0.2);
		}
		delay(6);
	}
}

script 1045 (VOID)//Check if on a liquid surface.
// 1 = water
// 2 = nukage
// 3 = blood
// 4 = lava
// 5 = slime
// 0 = default
{
	if (CheckActorFloorTexture(0, "FWATER1")){SetResultValue(1);}
	else if (CheckActorFloorTexture(0, "FWATER2")){SetResultValue(1);}
	else if (CheckActorFloorTexture(0, "FWATER3")){SetResultValue(1);}
	else if (CheckActorFloorTexture(0, "FWATER4")){SetResultValue(1);}
	else if (CheckActorFloorTexture(0, "NUKAGE1")){SetResultValue(2);}
	else if (CheckActorFloorTexture(0, "NUKAGE2")){SetResultValue(2);}
	else if (CheckActorFloorTexture(0, "NUKAGE3")){SetResultValue(2);}
	else if (CheckActorFloorTexture(0, "NUKAGE4")){SetResultValue(2);}
	else if (CheckActorFloorTexture(0, "BLOOD1")){SetResultValue(3);}
	else if (CheckActorFloorTexture(0, "BLOOD2")){SetResultValue(3);}
	else if (CheckActorFloorTexture(0, "BLOOD3")){SetResultValue(3);}
	else if (CheckActorFloorTexture(0, "BLOOD4")){SetResultValue(3);}
	else if (CheckActorFloorTexture(0, "LAVA1")){SetResultValue(4);}
	else if (CheckActorFloorTexture(0, "LAVA2")){SetResultValue(4);}
	else if (CheckActorFloorTexture(0, "LAVA3")){SetResultValue(4);}
	else if (CheckActorFloorTexture(0, "LAVA4")){SetResultValue(4);}
	else if (CheckActorFloorTexture(0, "SLIME01")){SetResultValue(5);}
	else if (CheckActorFloorTexture(0, "SLIME02")){SetResultValue(5);}
	else if (CheckActorFloorTexture(0, "SLIME03")){SetResultValue(5);}
	else if (CheckActorFloorTexture(0, "SLIME04")){SetResultValue(5);}
	else if (CheckActorFloorTexture(0, "SLIME05")){SetResultValue(5);}
	else if (CheckActorFloorTexture(0, "SLIME06")){SetResultValue(5);}
	else if (CheckActorFloorTexture(0, "SLIME07")){SetResultValue(5);}
	else if (CheckActorFloorTexture(0, "SLIME08")){SetResultValue(5);}
	else {SetResultValue(0);}
}

Script 1046 (VOID)//Misc stuff to do all the time.
{
	int x, y, z, speed;
	while (TRUE)
	{
		//Idle checker
		x = GetActorVelX(0);
		y = GetActorVelY(0);
		z = GetActorVelZ(0);
		speed = x + y + z;
		if (speed != 0)
		{
			TakeInventory("IdleCounter", 200);
		} else {
			GiveInventory("IdleCounter", 1);
		}
		//New ammo dropper
		if (CheckWeapon("AmmoDroper"))
		{
			TakeInventory("AmmoDroper", 1);
			SetWeapon("U_AmmoDropper");	
		}
		//Give soul ammo
		GiveInventory("U_SoulAmmo", 1);
		delay(50);
	}
}

Script "TE_GametypeCheck" (VOID)
{
	if	(PlayerIsBot(PlayerNumber()))
	{
		GiveInventory("PlayerIsABot", 1);
	}
	if (GameType() == GAME_NET_DEATHMATCH)
	{
		GiveInventory("DMGame", 1);
	}
}
/*
Script 1047 (VOID)//Check current gametype and if calling player is a bot.
// 0 = Single player
// 1 = Multiplayer, player is not a bot
// 2 = Multiplayer, player is a bot
// 3 = Coop
{
	if (GameType () == GAME_NET_DEATHMATCH)
	{
		if (PlayerIsBot(PlayerNumber()))
		{
			SetResultValue(2);
		} else {
			SetResultValue(1);
		}
	} else if (GameType () == GAME_NET_COOPERATIVE )
	{
		SetResultValue(3);
	} else {
		SetResultValue(3);
	}
}
*/
Script 1048 (VOID)//Blow C-4 charges.
{
	SetActorState(1999,"explode");
}

Script 1049 (VOID)//Shotgun stuff spawned?
{
	if (ShotgunStuffSpawned == 0)
	{
		ShotgunStuffSpawned = 1;
		SetResultValue(0);
	} else if (ShotgunStuffSpawned == 1)
	{
		ShotgunStuffSpawned = 2;
		SetResultValue(1);
	} else
	{
		SetResultValue(2);
	}
}

script 1050 (VOID)//C-4 charges indicator
{
	while (TRUE)
	{
		int Ammo = CheckInventory("C4Ammo");
		int Weapon = CheckInventory("C4Weapon");
		int X = GetCVar("TE_C4X");
		int Y = GetCVar("TE_C4Y");
		int Z = GetCVar("TE_C4img");
		
		if(Z == 1) //Smart show - Only shows C-4 if player has the weapon and ammo.
		{
			if(Ammo != 0 && Weapon != 0)
			{
				SetFont("BIGFONT");
				HudMessage(d:Ammo; HUDMSG_PLAIN, 0, CR_RED, (X*0.01-0.02), (Y*0.01-0.04), 0.2, 2.0);
				SetFont("C4EWA0");
				HudMessage(s:"A"; HUDMSG_PLAIN, 0, 0, (X*0.01), (Y*0.01), 0.2);
			}
		}
		
		if(Z == 2) //Always show
		{
			SetFont("BIGFONT");
			HudMessage(d:Ammo; HUDMSG_PLAIN, 0, CR_RED, (X*0.01-0.02), (Y*0.01-0.04), 0.2, 2.0);
			SetFont("C4EWA0");
			HudMessage(s:"A"; HUDMSG_PLAIN, 0, 0, (X*0.01), (Y*0.01), 0.2);
		}
		delay(6);
	}
}


// --------------------------------------------------------------------------
//
//	Map checking scripts
//
// --------------------------------------------------------------------------

Script "TE_Scythe2Detected" (VOID)
{
	Scythe2MapChecker = Scythe2MapChecker + 1;
}

Script "TE_Scythe2Check" (VOID)
{
	if (Scythe2MapChecker == 3)
	{
		SetActorState(0, "Scythe2");
	}
}