require("PGBase") require("GameManager.ResourceClass") require("GameManager.UnitClass") ---@class PlayerClass ---@field Id integer Player ID. ---@field TeamId integer Team ID. ---@field Name string Player name. ---@field Faction string Faction name. ---@field IsHuman boolean Whether this player is a human. ---@field QuitTime integer The game time this player quit, or -1 if they are still in the game. ---@field Resource ResourceClass The player's resources. ---@field PlayerWrapper table The FoC PlayerWrapper object. ---@field Units UnitClass[] The Units belonging to this player. PlayerClass = {} PlayerClass.__index = PlayerClass ---Constructs a new Player object. ---@param player table FoC PlayerWrapper object ---@return PlayerClass function PlayerClass:New(player) local self = setmetatable({}, PlayerClass) self.Id = player.Get_ID() self.TeamId = player.Get_Team() self.Name = player.Get_Name() self.Faction = player.Get_Faction_Name() self.IsHuman = player.Is_Human() self.QuitTime = -1 self.Resource = ResourceClass:New() self.PlayerWrapper = player self.Units = {} return self end ---Updates credits and income. function PlayerClass:Service() if not self:HasQuit() then -- Update player resources self.Resource:SetCredits(self.PlayerWrapper.Get_Credits()) -- Service Alive Units local assignedUnits = {} -- Pass 1: Collect assigned game object wrappers from alive units for _, unit in pairs(self:GetAliveUnits()) do if TestValid(unit.GameObjectWrapper) then assignedUnits[unit.GameObjectWrapper] = true else unit:SetDead() end end -- Pass 2: Assign unclaimed wrappers to reinforcement units for k, unit in pairs(self:GetReinforcementUnits()) do local matchingUnits = Find_All_Objects_Of_Type(unit.Name, self.PlayerWrapper) for _, object in pairs(matchingUnits) do if not assignedUnits[object] then unit:SetAlive(object) assignedUnits[object] = true break end end end end end ---Sets that the player has quit. function PlayerClass:Quit() self.QuitTime = GetCurrentTime() end ---Whether this player has quit the game. ---@return boolean function PlayerClass:HasQuit() return self.QuitTime >= 0 end ---Adds a Unit type to the Player's reinforcement list. ---@param objectType table FoC GameObjectTypeWrapper object that was built. function PlayerClass:AddUnit(objectType) local unit = UnitClass:New(objectType, self.Id) table.insert(self.Units, unit) ScriptMessage("Unit Added: %s", unit:Debug()) end ---Gets all Reinforcement Units for this Player. ---@return UnitClass[] function PlayerClass:GetReinforcementUnits() ---@type UnitClass[] local units = {} for k, unit in pairs(self.Units) do if unit:IsReinforcement() then table.insert(units, unit) end end return units end ---Gets all Alive Units for this Player. ---@return UnitClass[] function PlayerClass:GetAliveUnits() ---@type UnitClass[] local units = {} for k, unit in pairs(self.Units) do if unit:IsAlive() then table.insert(units, unit) end end return units end function PlayerClass:Debug() return string.format("Player %s (%d) on Faction %s, Team %d", self.Name, self.Id, self.Faction, self.TeamId) end