require("PGBase") ---@class UnitClass ---@field Name string Unit name. ---@field OwnerId integer Owner Player ID. ---@field BuildCost integer Tactical build cost. ---@field CombatRating integer AI combat power rating. ---@field BuildTime number Game time that this unit was built. ---@field AliveTime number Game time that this unit became "alive". ---@field DeathTime number Game time that this unit was killed. ---@field GameObjectWrapper table|nil The FoC GameObjectWrapper object. ---@field GameObjectTypeWrapper table The FoC GameObjectTypeWrapper object. ---@field Icon string Name of the icon for this object. UnitClass = {} UnitClass.__index = UnitClass ---Constructs a new Unit object. ---@param objectType table FoC GameObjectTypeWrapper object ---@param playerId integer The Owner Player ID function UnitClass:New(objectType, playerId) local self = setmetatable({}, UnitClass) self.Name = objectType.Get_Name() self.OwnerId = playerId self.BuildCost = objectType.Get_Tactical_Build_Cost() self.CombatRating = objectType.Get_Combat_Rating() self.BuildTime = GetCurrentTime() self.AliveTime = -1 self.DeathTime = -1 self.GameObjectWrapper = nil self.GameObjectTypeWrapper = objectType self.Icon = "I_BUTTON_COMMAND_BAR_PIRATE_SYMBOL.TGA" return self end ---Whether this Unit is a reinforcement. ---@return boolean function UnitClass:IsReinforcement() return self.AliveTime < 0 and self.DeathTime < 0 end ---Whether this Unit is currently alive. ---@return boolean function UnitClass:IsAlive() return self.AliveTime >= 0 and self.DeathTime < 0 end ---Whether this Unit is dead. ---@return boolean function UnitClass:IsDead() return self.DeathTime >= 0 end ---Sets the time this unit became "alive". ---@param object table FoC GameObjectWrapper object function UnitClass:SetAlive(object) if self.AliveTime >= 0 then -- Do not override once set return end self.AliveTime = GetCurrentTime() self.GameObjectWrapper = object ScriptMessage("Unit Spawned: %s", self:Debug()) end ---Sets the unit as dead. function UnitClass:SetDead() if self.DeathTime >= 0 then -- Do not override once set return end self.DeathTime = GetCurrentTime() ScriptMessage("Unit Killed: %s", self:Debug()) end function UnitClass:Debug() if self:IsReinforcement() then return string.format("Reinforcement Unit %s, Owned by %d, Built at: %d", self.Name, self.OwnerId, self.BuildTime) elseif self:IsAlive() then return string.format("Alive Unit %s, Owned by %d, Alive at: %d", self.Name, self.OwnerId, self.AliveTime) elseif self:IsDead() then return string.format("Dead Unit %s, Owned by %d, Death at: %d", self.Name, self.OwnerId, self.DeathTime) else return string.format("Unit %s, Owned by %d, Unknown state", self.Name, self.OwnerId) end end