Ticket #3321: 1_TerritoryManager-v4.patch

File 1_TerritoryManager-v4.patch, 4.0 KB (added by s0600204, 9 years ago)

Updated patch with Itms' suggestions applied

  • source/simulation2/components/CCmpTerritoryManager.cpp

     
    8686    // processed flag in bit 7 (TERRITORY_PROCESSED_MASK)
    8787    Grid<u8>* m_Territories;
    8888
     89    std::vector<u16> m_TerritoryCellCounts;
     90    u16 m_TerritoryTotalPassableCellCount;
     91
    8992    // Saves the cost per tile (to stop territory on impassable tiles)
    9093    Grid<u8>* m_CostGrid;
    9194
     
    123126
    124127        m_AnimTime = 0.0;
    125128
     129        m_TerritoryTotalPassableCellCount = 0;
     130
    126131        // Register Relax NG validator
    127132        CXeromyces::AddValidator(g_VFS, "territorymanager", "simulation/data/territorymanager.rng");
    128133
     
    260265
    261266    void CalculateTerritories();
    262267
     268    u8 GetTerritoryPercentage(player_id_t player);
     269
    263270    std::vector<STerritoryBoundary> ComputeBoundaries();
    264271
    265272    void UpdateBoundaryLines();
     
    337344    int tilesH = passGrid.m_H / NAVCELLS_PER_TERRITORY_TILE;
    338345
    339346    m_CostGrid = new Grid<u8>(tilesW, tilesH);
     347    m_TerritoryTotalPassableCellCount = 0;
    340348
    341349    for (int i = 0; i < tilesW; ++i)
    342350    {
     
    353361            else if (c & passClassUnrestricted)
    354362                m_CostGrid->set(i, j, 255); // off the world; use maximum cost
    355363            else
     364            {
    356365                m_CostGrid->set(i, j, 1);
     366                ++m_TerritoryTotalPassableCellCount;
     367            }
    357368        }
    358369    }
    359370}
     
    376387
    377388    m_Territories = new Grid<u8>(tilesW, tilesH);
    378389
     390    CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity());
     391    m_TerritoryCellCounts.clear();
     392    m_TerritoryCellCounts.resize(cmpPlayerManager->GetNumPlayers(), 0);
     393
    379394    // Find all territory influence entities
    380395    CComponentManager::InterfaceList influences = GetSimContext().GetComponentManager().GetEntitiesWithInterface(IID_TerritoryInfluence);
    381396
     
    496511            if (m_Territories->get(nx, nz) != owner)
    497512                continue;
    498513            m_Territories->set(nx, nz, owner | TERRITORY_CONNECTED_MASK);
     514            if (m_CostGrid->get(nx, nz) < m_ImpassableCost)
     515                ++m_TerritoryCellCounts[owner];
    499516        );
    500517    }
    501518}
     
    510527    return CTerritoryBoundaryCalculator::ComputeBoundaries(m_Territories);
    511528}
    512529
     530u8 CCmpTerritoryManager::GetTerritoryPercentage(player_id_t player)
     531{
     532    if (player <= 0 && (u8)player > m_TerritoryCellCounts.size())
     533        return 0;
     534
     535    ENSURE(m_TerritoryTotalPassableCellCount > 0);
     536    u8 percentage = (m_TerritoryCellCounts[player] * 100) / m_TerritoryTotalPassableCellCount;
     537    ENSURE(percentage <= 100);
     538    return percentage;
     539}
     540
    513541void CCmpTerritoryManager::UpdateBoundaryLines()
    514542{
    515543    PROFILE("update boundary lines");
  • source/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)
  • source/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