Ticket #3321: 1_TerritoryManager-v2.patch

File 1_TerritoryManager-v2.patch, 3.8 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
     
    260264
    261265    void CalculateTerritories();
    262266
     267    u8 GetTerritoryPercentage(player_id_t player);
     268
    263269    std::vector<STerritoryBoundary> ComputeBoundaries();
    264270
    265271    void UpdateBoundaryLines();
     
    337343    int tilesH = passGrid.m_H / NAVCELLS_PER_TERRITORY_TILE;
    338344
    339345    m_CostGrid = new Grid<u8>(tilesW, tilesH);
     346    m_TerritoryTotalPassableCellCount = 0;
    340347
    341348    for (int i = 0; i < tilesW; ++i)
    342349    {
     
    353360            else if (c & passClassUnrestricted)
    354361                m_CostGrid->set(i, j, 255); // off the world; use maximum cost
    355362            else
     363            {
    356364                m_CostGrid->set(i, j, 1);
     365                ++m_TerritoryTotalPassableCellCount;
     366            }
    357367        }
    358368    }
    359369}
     
    376386
    377387    m_Territories = new Grid<u8>(tilesW, tilesH);
    378388
     389    std::array<u16, 9> territoryCellCounts;
     390    territoryCellCounts.fill(0);
     391
    379392    // Find all territory influence entities
    380393    CComponentManager::InterfaceList influences = GetSimContext().GetComponentManager().GetEntitiesWithInterface(IID_TerritoryInfluence);
    381394
     
    496509            if (m_Territories->get(nx, nz) != owner)
    497510                continue;
    498511            m_Territories->set(nx, nz, owner | TERRITORY_CONNECTED_MASK);
     512            if (m_CostGrid->get(nx, nz) < m_ImpassableCost)
     513                ++territoryCellCounts[owner];
    499514        );
    500515    }
     516
     517    // Calculate current territory control percentages
     518    // We deliberately don't calculate for index 0 as gaia cannot own territory
     519    for (u8 i = 1; i < 9; ++i)
     520        m_TerritoryControlPercentages[i] = (territoryCellCounts[i] * 100) / m_TerritoryTotalPassableCellCount;
    501521}
    502522
    503523std::vector<STerritoryBoundary> CCmpTerritoryManager::ComputeBoundaries()
     
    510530    return CTerritoryBoundaryCalculator::ComputeBoundaries(m_Territories);
    511531}
    512532
     533u8 CCmpTerritoryManager::GetTerritoryPercentage(player_id_t player)
     534{
     535    return m_TerritoryControlPercentages[player];
     536}
     537
    513538void CCmpTerritoryManager::UpdateBoundaryLines()
    514539{
    515540    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