Ticket #3321: 1_TerritoryManager-v3.patch

File 1_TerritoryManager-v3.patch, 4.3 KB (added by s0600204, 9 years ago)
  • simulation2/components/CCmpTerritoryManager.cpp

     
    8686    // processed flag in bit 7 (TERRITORY_PROCESSED_MASK)
    8787    Grid<u8>* m_Territories;
    8888
     89    std::array<u8, 9> m_TerritoryControlPercentages;
     90
     91    u16 m_TerritoryTotalPassableCellCount;
     92
    8993    // Saves the cost per tile (to stop territory on impassable tiles)
    9094    Grid<u8>* m_CostGrid;
    9195
     
    123127
    124128        m_AnimTime = 0.0;
    125129
     130        m_TerritoryControlPercentages.fill(0);
     131        m_TerritoryTotalPassableCellCount = USHRT_MAX;
     132
    126133        // Register Relax NG validator
    127134        CXeromyces::AddValidator(g_VFS, "territorymanager", "simulation/data/territorymanager.rng");
    128135
     
    260267
    261268    void CalculateTerritories();
    262269
     270    u8 GetTerritoryPercentage(player_id_t player);
     271
    263272    std::vector<STerritoryBoundary> ComputeBoundaries();
    264273
    265274    void UpdateBoundaryLines();
     
    337346    int tilesH = passGrid.m_H / NAVCELLS_PER_TERRITORY_TILE;
    338347
    339348    m_CostGrid = new Grid<u8>(tilesW, tilesH);
     349    m_TerritoryTotalPassableCellCount = 0;
    340350
    341351    for (int i = 0; i < tilesW; ++i)
    342352    {
     
    353363            else if (c & passClassUnrestricted)
    354364                m_CostGrid->set(i, j, 255); // off the world; use maximum cost
    355365            else
     366            {
    356367                m_CostGrid->set(i, j, 1);
     368                ++m_TerritoryTotalPassableCellCount;
     369            }
    357370        }
    358371    }
    359372}
     
    376389
    377390    m_Territories = new Grid<u8>(tilesW, tilesH);
    378391
     392    CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity());
     393    u8 playerCount = (cmpPlayerManager) ? cmpPlayerManager->GetNumPlayers() : m_TerritoryControlPercentages.size();
     394    std::vector<u16> territoryCellCounts(playerCount, 0);
     395
    379396    // Find all territory influence entities
    380397    CComponentManager::InterfaceList influences = GetSimContext().GetComponentManager().GetEntitiesWithInterface(IID_TerritoryInfluence);
    381398
     
    496513            if (m_Territories->get(nx, nz) != owner)
    497514                continue;
    498515            m_Territories->set(nx, nz, owner | TERRITORY_CONNECTED_MASK);
     516            if (m_CostGrid->get(nx, nz) < m_ImpassableCost)
     517                ++territoryCellCounts[owner];
    499518        );
    500519    }
     520
     521    // Calculate current territory control percentages
     522    // We deliberately don't calculate for index 0 as gaia cannot own territory
     523    for (u8 i = 1; i < playerCount; ++i)
     524        m_TerritoryControlPercentages[i] = (territoryCellCounts[i] * 100) / m_TerritoryTotalPassableCellCount;
    501525}
    502526
    503527std::vector<STerritoryBoundary> CCmpTerritoryManager::ComputeBoundaries()
     
    510534    return CTerritoryBoundaryCalculator::ComputeBoundaries(m_Territories);
    511535}
    512536
     537u8 CCmpTerritoryManager::GetTerritoryPercentage(player_id_t player)
     538{
     539    ENSURE(0 <= player && (u8)player < m_TerritoryControlPercentages.size());
     540    u8 percentage = m_TerritoryControlPercentages[player];
     541    ENSURE(percentage <= 100);
     542    return percentage;
     543}
     544
    513545void CCmpTerritoryManager::UpdateBoundaryLines()
    514546{
    515547    PROFILE("update boundary lines");
  • simulation2/components/ICmpTerritoryManager.cpp

     
    2626DEFINE_INTERFACE_METHOD_3("GetNeighbours", std::vector<u32>, ICmpTerritoryManager, GetNeighbours, entity_pos_t, entity_pos_t, bool)
    2727DEFINE_INTERFACE_METHOD_2("IsConnected", bool, ICmpTerritoryManager, IsConnected, entity_pos_t, entity_pos_t)
    2828DEFINE_INTERFACE_METHOD_2("SetTerritoryBlinking", void, ICmpTerritoryManager, SetTerritoryBlinking, entity_pos_t, entity_pos_t)
     29DEFINE_INTERFACE_METHOD_1("GetTerritoryPercentage", u8, ICmpTerritoryManager, GetTerritoryPercentage, player_id_t)
    2930END_INTERFACE_WRAPPER(TerritoryManager)
  • simulation2/components/ICmpTerritoryManager.h

     
    7272     */
    7373    virtual void SetTerritoryBlinking(entity_pos_t x, entity_pos_t z) = 0;
    7474
     75    /**
     76     * Returns the percentage of the world controlled by a given player as defined by
     77     * the number of territory cells the given player owns
     78     */
     79     virtual u8 GetTerritoryPercentage(player_id_t player) = 0;
     80
    7581    DECLARE_INTERFACE_TYPE(TerritoryManager)
    7682};
    7783