AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

local rpgsnd = Sound( "weapons/rpg/rocket1.wav" ) -- DEFAULT GUNSHIP SOUND IN MOVEMENT

function ENT:Initialize()
	
	if( not self.Sound ) then
	
 		self.Sound = CreateSound( self.Entity, rpgsnd );
		
 	end
	
	self.Sound:Play();
	
	self.Owner = nil;
	
	self.Entity:SetModel( "models/Items/combine_rifle_ammo01.mdl" );
	self.Entity:PhysicsInit( SOLID_VPHYSICS );
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS );
	self.Entity:SetSolid( SOLID_VPHYSICS );
	
	local phy = self.Entity:GetPhysicsObject();
	local vec = self.Entity:GetForward() * 30 + Vector( 0, 0, 0 );
	phy:SetVelocity( Vector( vec.x, vec.z, -vec.y ) ); -- Iunno why this has to be done but meh
	
	util.SpriteTrail( self.Entity, 0, Color( 255,255,255,179 ), false, 12, 1, 4, 1 / ( 10 + 1 ) * 0.5, "trails/smoke.vmt" );

end

function ENT:SetROwner( lol )
	
	self.Owner = lol;
	
end

function ENT:Think()
	
	if( self.Owner ) then
	
		local tr = {};
			tr.start = self.Owner:GetPos();
			tr.endpos = tr.start + self.Owner:GetAimVector() * 32768;
			tr.filter = { self.Entity, self.Owner };
		local t = util.TraceLine( tr );
		local targ = t.HitPos;
		
		local phy = self.Entity:GetPhysicsObject();
		local vel = phy:GetVelocity();
		
		local toTarg = ( targ - self.Entity:GetPos() ):GetNormal();
		local res = ( toTarg * vel:Length() * 0.6 + vel ):GetNormal();
		phy:SetVelocity( res*( vel:Length() + 40 ) );
		phy:SetAngle( res:Angle() + Angle( 90, 0, 0 ) );
		
		self.Entity:NextThink( CurTime() + 0.08 );
		
	end
	
	return true;
	
end

function ENT:PhysicsCollide( data, physobj )
	
	local trace = { };
		trace.start = self.Entity:GetPos() + Vector( 0, 0, 32 );
		trace.endpos = self.Entity:GetPos() - Vector( 0, 0, 128 );
		trace.Entity = self.Entity;
		trace.mask  = 16395;
	local Normal = util.TraceLine( trace ).HitNormal;
	
	local effectdata = EffectData();
		effectdata:SetOrigin( self.Entity:GetPos() );
		effectdata:SetNormal( Normal );
		util.Effect( "effect_nuke", effectdata );
	
	util.BlastDamage( self.Entity, self.Entity, self.Entity:GetPos(), 1024, 10000 ); -- Proper deaths; doesn't affect observe etc
	util.ScreenShake( self.Entity:GetPos(), 900, 150, 10, 8192 );
	
	self.Sound:Stop();
	self.Entity:Remove();
	
end
