--[[made by NCKomodo, do not steal. use secondary to switch modes and primary to fire and stuff]]
mode1power = 2000000 --[[how much force the attack mode uses]]
mode3power = 2000000 --[[how much force the manual mode uses]]
accadd = 0.05 --[[the delay between each attack, lower value means higher accuracy, but might also induce lag, where as a lower value will decrease accuracy, and may help with lag]]
MODEL = "models/Roller.mdl" --[[this variable is the Drones model, the default is "models/Roller.mdl" change this to any model you please, but some may not work as well as others]]
--do not fuck with anything below unless you know what you are doing
budtable = {}
buddy = nil
ran = false
selected = false
tehent = nil
firemode = 1
budid = nil
acc = nil
function onInit()
	_swepsetsound(MyIndex, "single_shot", "Weapon_Physcannon.launch")
end

function onThink()
	if (ran == false) then return; end
	if (selected == false) then return; end
	if (_EntExists(tehent) == false) then
		selected = false
		return;
	end
	if (firemode ~= 2) then return; end
	if (acc < _CurTime()) then
		if (table.getn(budtable) > 0) then
			for i = 1, table.getn(budtable) do
				if (budtable[i] ~= nil) and (_EntExists(budtable[i].budid) == true) then
					local plypos = _EntGetPos(tehent)
					local entpos = _EntGetPos(budtable[i].budid)
					local tehhax = vecSub(plypos, entpos)
					local dahax = vecNormalize(tehhax)
					local fireforce = vecMul(dahax, vector3(mode1power, mode1power, mode1power));
					_PhysApplyForce(budtable[i].budid, fireforce);
				end
			end
			acc = _CurTime() + accadd
		end
	end
end

function onPrimaryAttack()
	if (firemode == 1) then
		if (_PlayerInfo(Owner, "alive") == false) then return; end
		buddy = _EntCreate( "prop_physics" );
		_EntPrecacheModel( "models/Roller.mdl" );
		_EntSetKeyValue( buddy, "model", MODEL);
		local barrelLength = 32
		_EntSetPos(buddy, vecAdd(_PlayerGetShootPos(Owner),vecMul(_PlayerGetShootAng(Owner), vector3(barrelLength, barrelLength, barrelLength))))
		_EntSetAng(buddy, _PlayerGetShootAng(Owner));
		_EntSpawn(buddy);
		ran = true
		_SWEPUseAmmo(MyIndex, 0, 1);
		table.insert(budtable, {budid = buddy})
	end
	if (firemode == 2) then
		local plypos = _PlayerGetShootPos(Owner)
		local plyang = _PlayerGetShootAng(Owner)
		_TraceLine(plypos, plyang, 99999, Owner)
		if (table.getn(budtable) > 0) then
			for i = 1, table.getn(budtable) do
				if (budtable[i] ~= nil) then
					if (_TraceGetEnt() == budtable[i].budid) then return; end
				end
			end
		end
		tehent = _TraceGetEnt()
		selected = true
		acc = _CurTime() + accadd
	end
	if (firemode ==3) then
		if (table.getn(budtable) > 0) then
			for i = 1, table.getn(budtable) do
				if (budtable[i] ~= nil) and (_EntExists(budtable[i].budid) == true) then
					local plypos = _PlayerGetShootPos(Owner)
					local plyang = _PlayerGetShootAng(Owner)
					_TraceLine(plypos, plyang, 99999, Owner)
					local endpos = _TraceEndPos()
					local entpos = _EntGetPos(budtable[i].budid)
					local tehhax = vecSub(endpos, entpos)
					local dahax = vecNormalize(tehhax)
					local fireforce = vecMul(dahax, vector3(mode3power, mode3power, mode3power));
					_PhysApplyForce(budtable[i].budid, fireforce);
				end
			end
		end
	end		
end

function onSecondaryAttack()
	if (firemode == 1) then
		firemode = 2
		_GModText_Start("ImpactMassive");
 		_GModText_SetPos(-30, -30);
 		_GModText_SetColor(255, 0, 0, 255);
 		_GModText_SetTime(1, 0.5, 1.5);
 		_GModText_SetText("Attack Mode");
 		_GModText_SetDelay(0.5);
		_GModText_Send(Owner, Owner);		
		return;
	end
	if (firemode == 2) 	then
		_GModText_Start("ImpactMassive");
 		_GModText_SetPos(-30, -30);
 		_GModText_SetColor(255, 0, 0, 255);
 		_GModText_SetTime(1, 0.5, 1.5);
 		_GModText_SetText("Manual Fire");
 		_GModText_SetDelay(0.5);
		_GModText_Send(Owner, Owner);
		firemode = 3
		return;
	end
	if (firemode == 3) then
		_GModText_Start("ImpactMassive");
 		_GModText_SetPos(-30, -30);
 		_GModText_SetColor(255, 0, 0, 255);
 		_GModText_SetTime(1, 0.5, 1.5);
 		_GModText_SetText("Spawn Mode");
 		_GModText_SetDelay(0.5);
		_GModText_Send(Owner);
		firemode = 1
		return;
	end
end

function onReload( )
	return true;
end

function getWeaponSwapHands()
	return false;	
end

function getWeaponFOV()
	return 60;	
end


function getWeaponSlot()
	return 4;	
end

function getWeaponSlotPos()
	return 4;	
end

function getFiresUnderwater()
	return true;
end

function getReloadsSingly()
	return false;
end

function getDamage()
	return 0;
end

function getPrimaryShotDelay()
	return 0.0;
end

function getSecondaryShotDelay()
	return 0.5;
end

function getPrimaryIsAutomatic()
	return false;
end

function getSecondaryIsAutomatic()
	return false;
end

function getPrimaryAmmoType()
	return "pistol";
end

function getSecondaryAmmoType()
	return "none";
end

function getMaxClipPrimary()
	return 5;
end

function getMaxClipSecondary()
	return 1;
end

function getDefClipPrimary()
	return 5;
end


function getDefClipSecondary()
	return 1;
end

function getPrimaryScriptOverride()
	return 1;
end

function getSecondaryScriptOverride()
	return 3;
end

function getBulletSpread()
	return vector3( 0.0, 0.0, 0.0 );
end

function getViewKick()
	return vector3( 0, 0.0, 0.0);
end

function getViewKickRandom()
	return vector3( 0.0, 0.0, 0.0 );
end

function getViewModel( )
	return "models/weapons/v_rpg.mdl";
end

function getWorldModel( )
	return "models/weapons/w_rocket_launcher.mdl";
end

function getClassName()
	return "weapon_attack_bot";
end

function getAnimPrefix()
	return "rpg";
end

function getPrintName()
	return "Attack Drone";
end
