local k=require("\105\110\118\111\107\097\116\105\111\110\046\100\111\116\097\050\046\117\110\105\116\115")local V=require("\112\108\046\099\108\097\115\115")local E=require("\105\110\118\111\107\097\116\105\111\110\046\108\097\110\103\046\100\101\108\101\103\097\116\105\111\110")local D=require("\109\111\115\101\115")local l=V()local x={"\072\111\108\100"}E.delegate(l,"\101\110\116\105\116\121",x)local function h(V)local E=k.Create(k.DUMMY_TARGET,{location=V;team=DOTA_TEAM_BADGUYS})E:SetIdleAcquire(false)return E end function l._init(E,V)E.spawn=Entities:FindByName(nil,k.DUMMY_TARGET_SPAWN)V=D.extend({spawn=true},V or{})if V.spawn then E:Spawn()end end function l.IsAlive(k)return D.toBoolean(k.entity and(not k.entity:IsNull()and k.entity:IsAlive()))end function l.IsDead(k)return not k:IsAlive()end function l.Spawn(k)if k:IsAlive()then return end k.entity=h(k.spawn:GetAbsOrigin())k:Hold()end function l.Kill(V)if V:IsAlive()then k.Destroy(V.entity)end V.entity=nil end function l.Reset(k)k:Kill()k:Spawn()end return l
--- DummyTarget class.
-- @classmod invokation.dota2.DummyTarget
local Units = require("invokation.dota2.units")
local class = require("pl.class")
local delegation = require("invokation.lang.delegation")
local m = require("moses")

local M = class()

local DELEGATES = { "Hold" }

delegation.delegate(M, "entity", DELEGATES)

local function createDummy(location)
  local unit = Units.Create(Units.DUMMY_TARGET, { location = location, team = DOTA_TEAM_BADGUYS })

  unit:SetIdleAcquire(false)

  return unit
end

--- Constructor.
-- @tparam table options Options table
-- @tparam[opt=true] bool options.spawn Spawn dummy unit immediately
function M:_init(options)
  self.spawn = Entities:FindByName(nil, Units.DUMMY_TARGET_SPAWN)

  options = m.extend({ spawn = true }, options or {})

  if options.spawn then
    self:Spawn()
  end
end

--- Checks if dummy unit is alive.
-- @treturn bool
function M:IsAlive()
  return m.toBoolean(self.entity and not self.entity:IsNull() and self.entity:IsAlive())
end

--- Checks if the dummy unit is dead.
-- @treturn bool
function M:IsDead()
  return not self:IsAlive()
end

--- Spawns the dummy unit.
function M:Spawn()
  if self:IsAlive() then
    return
  end

  self.entity = createDummy(self.spawn:GetAbsOrigin())

  self:Hold()
end

--- Kills the dummy unit.
function M:Kill()
  if self:IsAlive() then
    Units.Destroy(self.entity)
  end

  self.entity = nil
end

--- Resets the dummy unit.
function M:Reset()
  self:Kill()
  self:Spawn()
end

return M
