#library "PEEKRIFL"
#include "zcommon.acs"

// ----------------------------------------------------------------------------
//
// Flying Sniper Rifle of Damnation scripts for Russian Overkill
//
// ACS by KeksDose, 28. January 2013
//
// These scripts here set up the control for any rifle you wanna make fly. For
// example, the Ramjet. Reuse and modify these scripts in whatever way you see
// fit, but please credit me and include the source file.
//
// ----------------------------------------------------------------------------

#libdefine	PR_CROSSGFX		"RAMZC0"
#libdefine	PR_HAZEGFX		"DAKTD"
#libdefine	PR_HUDSTART		11113
#libdefine	PR_HUDHAZE		PR_HUDSTART
#libdefine	PR_HUDCROSS		PR_HUDSTART + 1
#libdefine	PR_HUDWIDTH		800.0
#libdefine	PR_HUDHEIGHT	400.0

function int GetButtonPressed(int input, int oldinput, int mask)
{
	return !(oldinput & mask) && input & mask;
}

function int GetButtonHeld(int input, int oldinput, int mask)
{
	return oldinput & mask && input & mask;
}

script 716 (void)
{
	int input = 0;
	int oldinput = 0;
	int tid = ActivatorTid();
	int velx = 0;
	int vely = 0;
	int velz = 0;
	int mouseyaw = 0;
	int mousepitch = 0;
	int angle = 0;
	int pitch = 0;
	int movespeed = GetActorProperty(0, APROP_SPEED);
	
	if(tid == 0)
	{
		tid = 0;
		for (int ii = 20000; ii < 30000; ii++)
		{
			if (ThingCount(T_NONE, ii) < 1) {TID = ii; ii = 99999;}
		}
		
		Thing_ChangeTid(0, tid);
	}
	
	SetHUDSize(PR_HUDWIDTH >> 16, PR_HUDHEIGHT >> 16, false);
	SetActivatorToTarget(0);
	SetPlayerProperty(0, ON, PROP_TOTALLYFROZEN);
	SetActorPitch(tid, GetActorPitch(0));
	ChangeCamera(tid, 0, false);
	
	SetFont("RAMZC0");
	HUDMessage(s:"A"; HUDMSG_PLAIN, PR_HUDCROSS,
			   CR_UNTRANSLATED, PR_HUDWIDTH / 2, PR_HUDHEIGHT / 2, 0.0);
	
	SetFont("DAKTD");
	HUDMessage(s:"A"; HUDMSG_PLAIN, PR_HUDHAZE,
			   CR_UNTRANSLATED, PR_HUDWIDTH / 2, PR_HUDHEIGHT / 2, 0.0);
	
	while(!GetUserVariable(tid, "User_RemoteControl"))
	{
		input = GetPlayerInput(-1, INPUT_BUTTONS);
		oldinput = GetPlayerInput(-1, INPUT_OLDBUTTONS);
		
		if(GetButtonPressed(input, oldinput, BT_ALTATTACK))
		{
			SetUserVariable(tid, "User_RemoteControl", true);
		}
		
		Delay(const:1);
	}
	
	while(GetActorProperty(tid, APROP_HEALTH) > 0 &&
		  GetUserVariable(tid, "User_RemoteControl") == true)
	{
		input = GetPlayerInput(-1, INPUT_BUTTONS);
		oldinput = GetPlayerInput(-1, INPUT_OLDBUTTONS);
		mouseyaw = GetPlayerInput(-1, INPUT_YAW);
		mousepitch = GetPlayerInput(-1, INPUT_PITCH);
		angle = (GetActorAngle(tid) + mouseyaw) % 1.0;
		pitch = GetActorPitch(tid) - mousepitch;
		
		if(pitch >= 0.25) pitch = 0.25;
		else if(pitch <= -0.25) pitch = -0.25;
		
		if(CheckInventory("RO_GotoReload") > 0)
		{
			SetUserVariable(tid, "User_RemoteControl", false);
		}
		
		if(GetButtonPressed(input, oldinput, BT_ATTACK))
		{
			SetActorState(tid, "Fire");
		}
		
		else if(GetButtonPressed(input, oldinput, BT_ALTATTACK))
		{
			SetActorState(tid, "AltFire");
		}
		
		else if(CheckInventory("RO_GotoZoom") > 0)
		{
			SetActorState(tid, "Zoom");
		}
		
		else if(CheckInventory("RO_GotoReload") > 0)
		{
			SetActorState(tid, "Reload");
		}
		
		if(GetButtonHeld(input, oldinput, BT_MOVELEFT))
		{
			velx += FixedMul(Cos(angle + 0.25), movespeed);
			vely += FixedMul(Sin(angle + 0.25), movespeed);
		}
		
		else if(GetButtonHeld(input, oldinput, BT_MOVERIGHT))
		{
			velx += FixedMul(Cos(angle - 0.25), movespeed);
			vely += FixedMul(Sin(angle - 0.25), movespeed);
		}
		
		if(GetButtonHeld(input, oldinput, BT_MOVEUP))
		{
			velz += movespeed;
		}
		
		else if(GetButtonHeld(input, oldinput, BT_MOVEDOWN))
		{
			velz -= movespeed;
		}
		
		if(GetButtonHeld(input, oldinput, BT_FORWARD))
		{
			velx += FixedMul(FixedMul(Cos(angle), movespeed), Cos(pitch));
			vely += FixedMul(FixedMul(Sin(angle), movespeed), Cos(pitch));
			velz += FixedMul(-Sin(pitch), movespeed);
		}
		
		if(GetButtonHeld(input, oldinput, BT_BACK))
		{
			velx += FixedMul(FixedMul(Cos(angle + 0.5), movespeed), Cos(pitch));
			vely += FixedMul(FixedMul(Sin(angle + 0.5), movespeed), Cos(pitch));
			velz += FixedMul(Sin(pitch), movespeed);
		}
		
		SetActorVelocity(tid, velx, vely, velz, false, false);
		SetActorAngle(tid, angle);
		SetActorPitch(tid, pitch);
		
		velx = 0;
		vely = 0;
		velz = 0;
		
		Delay(const:1);
	}
	
	SetActorVelocity(tid, 0.0, 0.0, 0.0, false, false);
	SetActorState(tid, "Retract");	// In case your rifle becomes shootable. :P
	ChangeCamera(0, 0, false);
	SetPlayerProperty(0, OFF, PROP_TOTALLYFROZEN);
	
	HUDMessage(s:""; HUDMSG_PLAIN, PR_HUDCROSS, CR_UNTRANSLATED, 0.0, 0.0, 0.0);
	HUDMessage(s:""; HUDMSG_PLAIN, PR_HUDHAZE, CR_UNTRANSLATED, 0.0, 0.0, 0.0);
}
