#Include "zcommon.acs"
#library "ShadowWarrior"
int UsiReload;
int PositionXOffset;
int PositionYOffset;
#define Magic_Circle_Function 980
#define RiotGun_Reload 985 //Riot Gun Reload Script
#define RiotGun_Reloading 986 //Riot Gun Reload Script

//x = r*cos(a) + u 
//y = r*sin(a) + v

//u & v - Centre
//r - Radius
//a - Angle

Script 900 (int PlayerPress)
{
	//--Activator FaceHugger Monster--//
	int LanyX = GetActorX(0);
	int LanyY = GetActorY(0);
	
	//--Now Activator Player--//
	SetActivatorToTarget(0);
	
	int PlayerX = GetActorX(0);
	int PlayerY = GetActorY(0);
	
	//---And Player Angle--//
	int PlayerAngle = GetActorAngle(0);
	
	int ViewAngle = VectorAngle(LanyX - PlayerX, LanyY - PlayerY);
	
   int x = LanyX - PlayerX >> 16; // Convert fixed point to integer
   int y = LanyY - PlayerY >> 16;
   int distance = sqrt( x*x + y*y );
	
	int Button = GetPlayerInput(-1, INPUT_BUTTONS);
	
	int Result = GetNearestAngle(ViewAngle, PlayerAngle);
	if (Result < 0.1 && Button & BT_USE && distance <= 300)
		{
		
		
		
			PlayerPress = 1;
	}
	
	
	

	SetResultValue(PlayerPress);
}

//----------Thanks ZZYZX---------------//
function int GetNearestAngle(int ang1, int ang2)
{
	if (ang2 > ang1) return ang2-ang1;
	return ang1-ang2;
}



//---------Check Player Health ----////


//------------------Magic Skull Script------------//
script 972 (int SkullZ, int SkullRadius, int SkullAngle)
{
     SkullAngle = GetUserVariable(0, "user_angle") * 360; 
     int Activator = ActivatorTid();
	 Thing_ChangeTID(ActivatorTid(), 5001);
	 SetActivatorToTarget(0);
	 SetActorPosition(5001, SnakePosX(SkullRadius,SkullAngle), SnakePosY(SkullRadius,SkullAngle), GetActorZ(0)+(SkullZ << 16), 0);
     Thing_ChangeTID(5001, Activator);
}
//---------------------------------------------------//

function int SnakePosX(int RadiusCircle,int AngleCircle)       
{
    int X = RadiusCircle*Cos(AngleCircle) + GetActorX(0);
    return X;
} 

Function int SnakePosY(int RadiusCircle,int AngleCircle)
{
	int Y = RadiusCircle*Sin(AngleCircle) + GetActorY(0);
	Return Y;
}




script 973 (void)
{
	 SetActorProperty(0,APROP_Speed, 6.0);
}  

script 974(void)
{
	Thing_Damage(3000+PlayerNumber(), 999, MOD_EXIT);
}

script 975(void)
{
	Thing_ChangeTID(3000, 3000+PlayerNumber());
}

script 976 (int Crouch)
{
   int CrouchButton = GetPlayerInput(-1, INPUT_BUTTONS);
   if (CrouchButton & BT_CROUCH)
   {
   Crouch = 1;
   }
   
SetResultValue(Crouch);
}

script 977 (int Jump)
{
   int JumpButton = GetPlayerInput(-1, INPUT_BUTTONS);
   if (JumpButton & BT_JUMP)
   {
   Jump = 1;
   }
   
SetResultValue(Jump);
}




str FortuneCookieText[19] = { "Sorry, you no win this time, try again.",
                              "Baseball wrong. Man with 4 balls cannot walk.",
							  "No man is island, except Lo Wang.",
							  "You should kill all business associates.",
							  "You should not scratch yourself there.",
							  "Man who stand on toilet, high on pot.",
							  "Man who buy drowned cat pay for wet pussy.",
							  "Don't you know you the scum of society?!",
							  "Man trapped in pantry has ass in jam.",
							  "You never going to score.",
							  "You try harder get along. Be a nice man.",
							  "26-31-43-82-16-29",
							  "Your chi attracts many chicks.",
							  "Man who fart in church sit in own pew.",
							  "There is much death in future.",
							  "(c)1997, 3DRealms fortune cookie company.",
							  "Courtesy is contagious. So is Gonorrhea",
							  "Having sex is like playing bridge, If you don't have a good partner, you better have a good hand."
                              };
							  
							 

Script 978 (void) ClientSide
{
        int cookietext = random(0, 18);
	    SetFont("CookieFont");
	    HudMessage(s:FortuneCookieText[cookietext]; HUDMSG_FADEOUT, 0, CR_UNTRANSLATED, 0.5, 0.75, 5.0, 1.0);
}		





Script 979 (int DieActor)
{
    SetActivatorToTarget(0);
    if(GetActorProperty(0, APROP_Health) <= 0 || CheckInventory("MagicCircle") == 0)
	{
		DieActor = 1;
	}
	SetResultValue(DieActor);
}

Script Magic_Circle_Function (int Radius, int HeightOffset) NET
{
if(CheckInventory("MagicCircleWorked") >= 1)
{
terminate;
}
int Angle = 0;
int FireBolls=0;
int TidsForBolls=500;
////Create ForeBolls and Tids
While(FireBolls < 15)
   {
   Spawn("MagicCircleFlameBall", GetActorX(0), GetActorY(0), GetActorZ(0), TidsForBolls);
   TidsForBolls += 1;
   FireBolls += 1;
   delay(1);
   }
/////////////////////////////////
Angle = 0;
TidsForBolls=500;


//////////////////////////////////////////   
While(CheckInventory("MagicCircle") != 0)
{
   SetActorPosition(500, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(501, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(502, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(503, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(504, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(505, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(506, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(507, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(508, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(509, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(510, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(511, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(512, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(513, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(514, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   SetActorPosition(515, XPositionInCircle(Radius,Angle), YPositionInCircle(Radius,Angle), GetActorZ(4)+(HeightOffset << 16), 0);
   Angle += 360*12;
   if(GetActorProperty(0,APROP_Health) <= 0 || CheckInventory("MagicCircle") == 0)
   {
   TakeInventory("MagicCircleWorked", 999);
   }
   if(GetActorProperty(0,APROP_Health) <= 0 || CheckInventory("MagicCircle") == 0)
   {
   Thing_Destroy(500,0);
   Thing_Destroy(501,0);
   Thing_Destroy(502,0);
   Thing_Destroy(503,0);
   Thing_Destroy(504,0);
   Thing_Destroy(505,0);
   Thing_Destroy(506,0);
   Thing_Destroy(507,0);
   Thing_Destroy(508,0);
   Thing_Destroy(509,0);
   Thing_Destroy(510,0);
   Thing_Destroy(511,0);
   Thing_Destroy(512,0);
   Thing_Destroy(513,0);
   Thing_Destroy(514,0);
   Thing_Destroy(515,0);
   Terminate;
   }
   TakeInventory("MagicCircle", 1);
   delay(1);
   }   

}
///////////////////////////////////////////
/////////Function For Magic Circle/////////
function int XPositionInCircle(int RadiusCircle,int AngleCircle)       
{
    int X = RadiusCircle*Cos(AngleCircle) + GetActorX(4);
    return X;
} 

Function int YPositionInCircle(int RadiusCircle,int AngleCircle)
{
	int Y = RadiusCircle*Sin(AngleCircle) + GetActorY(4);
	Return Y;
}
////////////////////////////////////////////

//-------------------------------Reloading Hack-------------------------------//

//It's reloading for Micro Usi
Script 982 (int DoubleReloading)
{
    if(CheckInventory("UsiAmmo") == 100 && UsiReload == 0)
	{
		DoubleReloading = 1;
	}
	SetResultValue(DoubleReloading);
}


Script 983 (int Reloading)
{
    if(CheckInventory("UsiAmmo") == 150 && UsiReload == 0)
	{
		Reloading = 1;
	}
	else if(CheckInventory("UsiAmmo") == 100 && UsiReload == 0)
	{
		Reloading = 1;
	}
	else if(CheckInventory("UsiAmmo") == 50 && UsiReload == 0)
	{
		Reloading = 1;
	}
	SetResultValue(Reloading);
}

//Special Reload War
Script 984 (int ReloadVar)
{
UsiReload = ReloadVar;
}


//---------------------------Reloading for Riot Gun---------------------------//
Script RiotGun_Reload (int Reloading)
{
    if(CheckInventory("RiotGunShell") == 48 || CheckInventory("RiotGunShell") == 44 ||
	CheckInventory("RiotGunShell") == 40 || CheckInventory("RiotGunShell") == 36 || CheckInventory("RiotGunShell") == 32 ||
	CheckInventory("RiotGunShell") == 28 || CheckInventory("RiotGunShell") == 24 || CheckInventory("RiotGunShell") == 20 ||
	CheckInventory("RiotGunShell") == 16 || CheckInventory("RiotGunShell") == 12 || CheckInventory("RiotGunShell") == 8 ||
	CheckInventory("RiotGunShell") == 4)
	{
		Reloading = 1;
	}	   
	SetResultValue(Reloading);
}
//----------------------------------------------------------------------------//



Script 997 (int TrailXOffset, int TrailYOffset)
{
      PositionXOffset = TrailXOffset;
      PositionYOffset = TrailYOffset;
}	  


////Special Script for Spawn Trails////
Script 998 (int TrailsSpawnOffset,int HeightOffset, int FirtsPositionOffset) clientside
{
    //Projectile Coordinates (not fixed type!)
	int ProjectileX = GetActorX(0) >> 16;
	int ProjectileY = GetActorY(0) >> 16;
	int ProjectileZ = GetActorZ(0) >> 16;
    
	//Now Activator Player//
	SetActivatorToTarget(0);
	
	//Player Coordinates(not fixed type!)
	int PlayerX = (GetActorX(0) >> 16) + PositionXOffset; 
	int PlayerY = (GetActorY(0) >> 16) + PositionYOffset;
	int PlayerZ = (GetActorZ(0) >> 16) + HeightOffset;
	
	//Player Angle(not fixed type!)
	int PlayerAngle = GetActorAngle(0) >> 8;
	
    //Difference Coordinates
	int DifferenceX = ProjectileX - PlayerX;
	int DifferenceY = ProjectileY - PlayerY;
	int DifferenceZ = ProjectileZ - PlayerZ;
	
	//Square Coordinates
	int SquareX = abs(DifferenceX*DifferenceX);
	int SquareY = abs(DifferenceY*DifferenceY);
	int SquareZ = abs(DifferenceZ*DifferenceZ);
	
	//All Summe//
	int AllResult = SquareX + SquareY + SquareZ;
	//And SQRT ALL
	int Length = sqrt(AllResult);
	
	//Get Current Player Coodinates, now fixed type.
	int CurrentX = PlayerX << 16; 
	int CurrentY = PlayerY << 16; 
	int CurrentZ = PlayerZ << 16;
	
	//Here Angle for trails
	int SegmentsX = (DifferenceX << 16) / Length;
	int SegmentsY = (DifferenceY << 16) / Length;
	int SegmentsZ = (DifferenceZ << 16) / Length;
	
	//Here Spesial Trigers For While
    int PositionInWhile = 0;
	int PositionSpawn = 0;
	int FirstPosition = 0;
	
	//While, Spawn Triles
    while(PositionInWhile < Length) //This is a peculiar lineup for our triles
    {
	    CurrentX += SegmentsX;
		CurrentY += SegmentsY;
		CurrentZ += SegmentsZ;
		PositionInWhile += 1;
		PositionSpawn += 1;
		//First Trile Position 
		If(FirstPosition == 0 && PositionSpawn > FirtsPositionOffset)
		{
		Spawn("RailGunTrail",CurrentX,CurrentY,CurrentZ,0,PlayerAngle);
		FirstPosition = 1;
		PositionSpawn = 0;
		}
		else 
		//All Positions Trile
		if (PositionSpawn > TrailsSpawnOffset && FirstPosition == 1)
		{
			Spawn("RailGunTrail",CurrentX,CurrentY,CurrentZ,0,PlayerAngle);
			PositionSpawn = 0;
		}
	
	
	}
}



script 987 (int climb) clientside
{ 
	int playerpitch = GetActorPitch(0) - (8*climb);
	if (playerpitch > -5780)
		{
		SetActorPitch (0, playerpitch);
		}
}


script 288 (int ButtonPress)
{
   ButtonPress = 0;
   int FireButton1 = GetPlayerInput(-1, INPUT_BUTTONS);
   if (FireButton1 & BT_ATTACK)
   {
   ButtonPress = 1;
   }
   
SetResultValue(ButtonPress);
}

//-------------------Night Vision--------------------------------------------//
//New Code Guys)))
Script 289 (void)
{
int N;
GiveInventory("PowerLightAmp", 1);
While(CheckInventory("NightVisionSWCheck") != 0 && CheckInventory("NightVisionSW") != 0)
{
   if(N == 35)
      {
       GiveInventory("PowerLightAmp", 1);
       TakeInventory("NightVisionSW", 3);
	   N = 0;
	   }
    Delay(1);
	N += 1;
}
TakeInventory("PowerLightAmp", 999);
TakeInventory("NightVisionSWCheck", 1);
    if(CheckInventory("NightVisionSW") == 0)
	    {
		ActivatorSound("NVOFF",127);
		}
terminate;
}
//----------------------------------------------------------------------------//


Script 290 (void)
{
int M;
SetActorState(0, "TranslucentOn", True);
While(CheckInventory("SmokeBombCheck") != 0 && CheckInventory("SmokeBomb") != 0)
{
   if(M == 35)
      {
       TakeInventory("SmokeBomb", 4);
	   M = 0;
	   }
    Delay(1);
	M += 1;
}
TakeInventory("SmokeBombCheck", 1);
SetActorState(0, "TranslucentOff", True);
    if(CheckInventory("SmokeBomb") == 0)
	    {
		ActivatorSound("GASPOP",127);
		}
terminate;
}


/////////////Functions/////////////////////////////////////////
//The square root of a number//
function int sqrt(int number)
{
	if(number <= 3)
	{
		if(number > 0)
		{
			return 1;
		}
		return 0;
	}

	int oldAns = number >> 1,                     // initial guess
	    newAns = (oldAns + number / oldAns) >> 1; // first iteration

	// main iterative method
	while(newAns < oldAns)
	{
		oldAns = newAns;
		newAns = (oldAns + number / oldAns) >> 1;
	}

	return oldAns;
}
///////////////////////////////////////////////////////////////////

//////The module number////////////////////////////
function int abs (int x)
{
    if (x < 0)
        return -x;

    return x;
}
//////////////////////////////////