
local FISH = {}

FISH.ClassName = "rowing_zombie"
FISH.Model = "models/props_canal/boat002b.mdl"
FISH.Base = "fishing_fish_base_swimming"
FISH.Forward = Vector(0,1,0)
FISH.BuoyancyRatio = 0.01
FISH.AnglesDamping = 0.1
FISH.IdleZMult = 0
FISH.DrawPitch = 0

FISH.Rareness = 0.999
FISH.MaxSpawned = 5

function FISH:OnSetupDataTables()
	self:DTVar("Vector", 1, "aimpos")
	self:DTVar("Bool", 1, "fire")
end

function FISH:ShootBullets(pos, dir, ent)
	local bullet = {}
		bullet.Num = 1

		bullet.Src = pos
		bullet.Dir = dir

		bullet.Spread = VectorRand()*0.1
		bullet.Tracer = SERVER and 0 or 5
		bullet.TracerName = "Tracer"
		bullet.Force = 1
		bullet.Damage = 1
		bullet.AmmoType = "Pistol"
	(ent or self):FireBullets(bullet)

	if CLIENT then
		for i=1, 50 do
			self:EmitSound("npc/turret_floor/shoot"..math.random(3)..".wav", 160, math.random(70,80))
		end
	end
end

if CLIENT then
	pac.SetupENT(FISH)

	function FISH:PostInit()
		self:AttachPACPart(outfit)
		self:SetPACDrawDistance(0)

		local aim = self:FindPACPart(outfit, "aimbone")
		local shootpos = self:FindPACPart(outfit, "shootpos")

		aim.ThinkTime = 0.1

		aim.OnThink = function()
			if self.dt.dead then return end

			local ang = self:WorldToLocalAngles((shootpos.cached_pos - self.dt.aimpos):Angle())
			aim:SetAngles(Angle(ang.y - 180, 180, -ang.p))

			if self.dt.fire then
				self:ShootBullets(shootpos.cached_pos, -shootpos.cached_ang:Forward(), shootpos.Entity)
			end
		end
	end

	function FISH:OnRemove()
		self:RemovePACPart(outfit)
	end
end

if SERVER then
	function FISH:PreMove(phys)

		if self.dt.dead then return end

		local found = false

		for key, ent in pairs(ents.FindInSphere(self:GetPos(), 2000)) do
			if ent.IsFishingSeagull and (not ent.dt or not ent.dt.dead) then
				self.dt.aimpos = ent:GetPos()
				found = true
				break
			end
		end

		self.dt.fire = found

		if self.dt.fire then
			self:ShootBullets(self:GetPos(), (self.dt.aimpos - self:GetPos()):GetNormalized())
		end
	end
end

fishing.RegisterFish(FISH)