local PLUGIN = PLUGIN

AddCSLuaFile()

ENT.Base              = "tacrp_proj_base"
ENT.PrintName         = "40mm HE"
ENT.Spawnable         = false

ENT.Model             = "models/weapons/tacint/grenade_40mm.mdl"

ENT.IsRocket          = false -- projectile has a booster and will not drop.

ENT.InstantFuse       = false -- projectile is armed immediately after firing.
ENT.RemoteFuse        = false -- allow this projectile to be triggered by remote detonator.
ENT.ImpactFuse        = true  -- projectile explodes on impact.

ENT.ExplodeOnDamage   = false -- projectile explodes when it takes damage.
ENT.ExplodeUnderwater = true

ENT.Delay             = 0

ENT.ExplodeSounds     = {
  "^TacRP/weapons/grenade/frag_explode-1.wav",
  "^TacRP/weapons/grenade/frag_explode-2.wav",
  "^TacRP/weapons/grenade/frag_explode-3.wav",
}

ENT.AudioLoop         = "TacRP/weapons/rpg7/rocket_flight-1.wav"

function ENT:Detonate()
  local attacker = self.Attacker or self:GetOwner() or self
  local mult = PLUGIN.ConVars["mult_damage_explosive"]:GetFloat() * (self.NPCDamage and 0.5 or 1)
  local dmg = 150
  if engine.ActiveGamemode() == "terrortown" then
    dmg = 55
  end

  util.BlastDamage(self, attacker, self:GetPos(), 300, dmg * mult)
  self:FireBullets({
    Attacker = attacker,
    Damage = dmg * mult,
    Tracer = 0,
    Src = self:GetPos(),
    Dir = self:GetForward(),
    HullSize = 0,
    Distance = 32,
    IgnoreEntity = self,
    Callback = function(atk, btr, dmginfo)
      dmginfo:SetDamageType(DMG_AIRBOAT + DMG_BLAST)   -- airboat damage for helicopters and LVS vehicles
      dmginfo:SetDamageForce(self:GetForward() * 7000) -- LVS uses this to calculate penetration!
    end,
  })

  local fx = EffectData()
  fx:SetOrigin(self:GetPos())

  if self:WaterLevel() > 0 then
    util.Effect("WaterSurfaceExplosion", fx)
  else
    util.Effect("Explosion", fx)
  end

  self:EmitSound(table.Random(self.ExplodeSounds), 125)

  self:Remove()
end
