Ticket #3321: 1_TerritoryManager.patch

File 1_TerritoryManager.patch, 3.1 KB (added by s0600204, 9 years ago)

First patch (see ticket description)

  • source/simulation2/components/CCmpTerritoryManager.cpp

     
    8686    // processed flag in bit 7 (TERRITORY_PROCESSED_MASK)
    8787    Grid<u8>* m_Territories;
    8888
     89    // idx 0   : total passible territory cells
     90    // idx n>0 : player territory counts
     91    std::vector<u16> m_TerritoryCounts;
     92
    8993    // Saves the cost per tile (to stop territory on impassable tiles)
    9094    Grid<u8>* m_CostGrid;
    9195
     
    260264
    261265    void CalculateTerritories();
    262266
     267    u8 GetTerritoryPercentage(player_id_t player);
     268
    263269    std::vector<STerritoryBoundary> ComputeBoundaries();
    264270
    265271    void UpdateBoundaryLines();
     
    498504            m_Territories->set(nx, nz, owner | TERRITORY_CONNECTED_MASK);
    499505        );
    500506    }
     507
     508    m_TerritoryCounts.clear();
     509    m_TerritoryCounts.resize(9, 0);
     510    for (u16 w = 0; w < tilesW; ++w)
     511    {
     512        for (u16 h = 0; h < tilesH; ++h)
     513        {
     514            if (m_CostGrid->get(w,h) >= m_ImpassableCost)
     515                continue;
     516            ++m_TerritoryCounts[0];
     517
     518            player_id_t owner = m_Territories->get(w,h) & TERRITORY_PLAYER_MASK;
     519            if (owner > 0)  // gaia is owner 0
     520                ++m_TerritoryCounts[owner];
     521        }
     522    }
    501523}
    502524
    503525std::vector<STerritoryBoundary> CCmpTerritoryManager::ComputeBoundaries()
     
    510532    return CTerritoryBoundaryCalculator::ComputeBoundaries(m_Territories);
    511533}
    512534
     535u8 CCmpTerritoryManager::GetTerritoryPercentage(player_id_t player)
     536{
     537    float result = (float)m_TerritoryCounts[player] / m_TerritoryCounts[0];
     538    return (u8)floor(result * 100);
     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     * Once finished, this should return the percentage of the world covered by a given
     77     * player's territory
     78     */
     79     virtual u8 GetTerritoryPercentage(player_id_t player) = 0;
     80
    7581    DECLARE_INTERFACE_TYPE(TerritoryManager)
    7682};
    7783