-- Leeroy Jenkins suicide weapon by JetBoom

Tick = 0
Fired = false

	function onInit( )
	end

	function onThink( )
		if Tick > 0 then
			if Tick >= 125 then
				DoExplode()
				Tick = 0
			else
				Tick = Tick + 1
			end
		end
	end
	
	function onPrimaryAttack( )
		if Tick < 1 and not Fired then
			Tick = 1
			_EntEmitSoundEx(Owner, "leeroyjenkins.wav", 1.0, 1.0)
			Fired = true
			local ent = _EntCreate("env_fire_trail")
			if ent > 0 then
				_EntSetPos(ent, _EntGetPos(Owner))
				_EntSetParent(ent, Owner)
				_EntSpawn(ent)
				_EntFire(ent, "kill", "", 1.5)
			end
		end
	end

	function DoExplode()
		if Tick < 125 then return end
		_EntEmitSoundEx(Owner, "ambient/explosions/explode_"..math.random(1,9)..".wav", 1.5, 0.9)
		_EntEmitSoundEx(Owner, "ambient/explosions/explode_"..math.random(1,9)..".wav", 1.0, 1.1)
		_util.ScreenShake(_EntGetPos(Owner), 50, 475, 3, 550)
		local ents = _EntitiesFindInSphere(_EntGetPos(Owner), 330)
		local damage = 0
		for i=1, table.getn(ents) do
			if _phys.HasPhysics(ents[i]) then
				if ents[i] <= _MaxPlayers() then
					if _PlayerInfo(ents[i], "alive") and _PlayerInfo(ents[i], "connected") then
						_EntSetVelocity(ents[i], vecAdd(vector3(math.random(-1000, 1000), math.random(-1000, 1000), math.random(500, 1500)), _EntGetVelocity(ents[i])))
						for i=1, 3 do _RunString("DamagelessExplosion("..ents[i]..", "..(i*0.25)..")") end
						damage = 355-(math.abs(vecLength(vecSub(_EntGetPos(Owner),_EntGetPos(ents[i])))))*1.0
						if damage >= _PlayerInfo(ents[i], "health") then damage = 2000 end
						_TraceAttack(ents[i], Owner, Owner, damage)
					end
				else
					_TraceAttack(ents[i], Owner, Owner, 355-(math.abs(vecLength(vecSub(_EntGetPos(Owner),_EntGetPos(ents[i])))))*1.0)
				end
			end
		end
		local x = -90
		local y = -90
		local ent = 0
		for i=1, 3 do
			ent = 0
			ent = _EntCreate("env_explosion")
			if ent > 0 then
				_EntSetPos(ent, vecAdd(vector3(x, 0, 0), _EntGetPos(Owner)))
				_EntSetKeyValue(ent, "spawnflags", "65")
				_EntSpawn(ent)
				_EntFire(ent, "explode", "", 0)
				_EntFire(ent, "kill", "", 0.15)
			end
			x = x + 90
		end
		for ii=1, 3 do
			if y ~= 0 then
				ent = 0
				ent = _EntCreate("env_explosion")
				if ent > 0 then
					_EntSetPos(ent, vecAdd(vector3(0, y, 0), _EntGetPos(Owner)))
					_EntSetKeyValue(ent, "spawnflags", "65")
					_EntSpawn(ent)
					_EntFire(ent, "explode", "", 0)
					_EntFire(ent, "kill", "", 0.15)
				end
			end
			y = y + 90
		end
	end
	
	function onSecondaryAttack( )
	end

	function onReload( )
		return false
	end

	function getWeaponSwapHands()
		return false	
	end

	function getWeaponFOV()
		return 60
	end

	function getWeaponSlot()
		return 0	
	end

	function getWeaponSlotPos()
		return 2;	
	end

	function getFiresUnderwater()
		return true;
	end

	function getReloadsSingly()
		return false;
	end

	function getDamage()
		return 10;
	end

	function getPrimaryShotDelay()
		return 1.0;
	end
	
	function getPrimaryIsAutomatic()
		return true;
	end
		
	function getBulletSpread()
		return vector3( 0.00, 0.00, 0.00 );
	end

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

	function getViewKickRandom()
		return vector3( 0.0, 8.0, 0.0 );
	end
	
	function getNumShotsPrimary()
		return 1;
	end

	function getPrimaryAmmoType()
		return "none";
	end

	function getDamageSecondary()
		return 10;
	end

	function getSecondaryShotDelay()
		return 2.5;
	end

	function getSecondaryIsAutomatic()
		return false;
	end

	function getBulletSpreadSecondary()
		return vector3( 0.001, 0.001, 0.001 );
	end

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

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

	function getNumShotsSecondary()
		return 1;
	end

	function getSecondaryAmmoType()
		return "none";
	end

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

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

	function getClassName() 
		return "weapon_leeroy";
	end

	function getMaxClipPrimary()
		return 1;
	end

	function getMaxClipSecondary()
		return -1;
	end

	function getDefClipPrimary() -- ammo in gun by default
		return 1;
	end

	function getDefClipSecondary()
		return 0;
	end

	function getAnimPrefix() -- How the player holds the weapon: pistol, smg, ar2, shotgun, rpg, phys, crossbow, melee, slam, grenade
		return "phys";
	end

	function getPrintName()
		return "LEEEEEROOOOOOY JEENKINSS"
	end

	function getPrimaryScriptOverride()
		return 2;
	end

	function getSecondaryScriptOverride()
		return 3;
	end

	function getDeathIcon( )
		return "d_pwn"
	end
