include("shared.lua")

include("common/ai_translations.lua")
include("common/anims.lua")
include("common/autodetection.lua")
include("common/utils.lua")
include("common/stat.lua")
include("common/attachments.lua")
include("common/bullet.lua")
include("common/effects.lua")
include("common/calc.lua")
include("common/akimbo.lua")
include("common/events.lua")
include("common/nzombies.lua")
include("common/ttt.lua")
include("common/viewmodel.lua")
include("common/skins.lua")

AddCSLuaFile("common/ai_translations.lua")
AddCSLuaFile("common/anims.lua")
AddCSLuaFile("common/autodetection.lua")
AddCSLuaFile("common/utils.lua")
AddCSLuaFile("common/stat.lua")
AddCSLuaFile("common/attachments.lua")
AddCSLuaFile("common/bullet.lua")
AddCSLuaFile("common/effects.lua")
AddCSLuaFile("common/calc.lua")
AddCSLuaFile("common/akimbo.lua")
AddCSLuaFile("common/events.lua")
AddCSLuaFile("common/nzombies.lua")
AddCSLuaFile("common/ttt.lua")
AddCSLuaFile("common/viewmodel.lua")
AddCSLuaFile("common/skins.lua")

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

AddCSLuaFile("client/effects.lua")
AddCSLuaFile("client/viewbob.lua")
AddCSLuaFile("client/hud.lua")
AddCSLuaFile("client/mods.lua")
AddCSLuaFile("client/laser.lua")
AddCSLuaFile("client/fov.lua")
AddCSLuaFile("client/flashlight.lua")
AddCSLuaFile("client/viewmodel.lua")
AddCSLuaFile("client/bobcode.lua")

SWEP.Weight = 60 -- Decides whether we should switch from/to this
SWEP.AutoSwitchTo = true -- Auto switch to
SWEP.AutoSwitchFrom = true -- Auto switch from

local sv_tfa_npc_burst = GetConVar("sv_tfa_npc_burst")

function SWEP:NPCShoot_Primary()
	if self:Clip1() <= 0 and self:GetMaxClip1() > 0 then
		self:GetOwner():SetSchedule(SCHED_RELOAD)
		return
	end

	return self:PrimaryAttack()
end

function SWEP:GetNPCRestTimes()
	if sv_tfa_npc_burst:GetBool() or self:GetStatL("NPCBurstOverride", false) then
		return self:GetStatL("NPCMinRest", self:GetFireDelay()), self:GetStatL("NPCMaxRest", self:GetFireDelay() * 2)
	end

	if self:GetStatL("Primary.Automatic") then
		return 0, 0
	else
		return self:GetFireDelay(), self:GetFireDelay() * 2
	end
end

function SWEP:GetNPCBurstSettings()
	if sv_tfa_npc_burst:GetBool() or self:GetStatL("NPCBurstOverride", false) then
		return self:GetStatL("NPCMinBurst", 1), self:GetStatL("NPCMinBurst", 6), self:GetStatL("NPCBurstDelay", self:GetFireDelay() * self:GetMaxBurst())
	end

	if self:GetMaxClip1() > 0 then
		local burst = self:GetMaxBurst()
		local value = math.ceil(self:Clip1() / burst)
		local delay = self:GetFireDelay() * burst

		if self:GetStatL("Primary.Automatic") then
			return math.min(4, value), math.min(12, value), delay
		else
			return 1, math.min(4, value), delay
		end
	else
		return 1, 30, self:GetFireDelay() * self:GetMaxBurst()
	end
end

function SWEP:GetNPCBulletSpread()
	return 1 -- we handle this manually, in calculate cone, recoil and shootbullet
end

function SWEP:CanBePickedUpByNPCs()
	return true
end

local sv_tfa_npc_randomize_atts = GetConVar("sv_tfa_npc_randomize_atts")

function SWEP:Equip(...)
	local owner = self:GetOwner()

	if owner:IsNPC() then
		self.IsNPCOwned = true

		if not self.IsFirstEquip and sv_tfa_npc_randomize_atts:GetBool() then
			self:RandomizeAttachments(true)
		end

		local function closure()
			self:NPCWeaponThinkHook()
		end

		hook.Add("TFA_NPCWeaponThink", self, function()
			ProtectedCall(closure)
		end)
	else
		self.IsNPCOwned = false
	end

	self.IsFirstEquip = true
	self.OwnerViewModel = nil
	self:EquipTTT(...)
end

TFA.FillMissingMetaValues(SWEP)
