Ticket #3321: 1_TerritoryManager-v2.patch
File 1_TerritoryManager-v2.patch, 3.8 KB (added by , 9 years ago) |
---|
-
simulation2/components/CCmpTerritoryManager.cpp
86 86 // processed flag in bit 7 (TERRITORY_PROCESSED_MASK) 87 87 Grid<u8>* m_Territories; 88 88 89 std::array<u8, 9> m_TerritoryControlPercentages; 90 91 u16 m_TerritoryTotalPassableCellCount; 92 89 93 // Saves the cost per tile (to stop territory on impassable tiles) 90 94 Grid<u8>* m_CostGrid; 91 95 … … 260 264 261 265 void CalculateTerritories(); 262 266 267 u8 GetTerritoryPercentage(player_id_t player); 268 263 269 std::vector<STerritoryBoundary> ComputeBoundaries(); 264 270 265 271 void UpdateBoundaryLines(); … … 337 343 int tilesH = passGrid.m_H / NAVCELLS_PER_TERRITORY_TILE; 338 344 339 345 m_CostGrid = new Grid<u8>(tilesW, tilesH); 346 m_TerritoryTotalPassableCellCount = 0; 340 347 341 348 for (int i = 0; i < tilesW; ++i) 342 349 { … … 353 360 else if (c & passClassUnrestricted) 354 361 m_CostGrid->set(i, j, 255); // off the world; use maximum cost 355 362 else 363 { 356 364 m_CostGrid->set(i, j, 1); 365 ++m_TerritoryTotalPassableCellCount; 366 } 357 367 } 358 368 } 359 369 } … … 376 386 377 387 m_Territories = new Grid<u8>(tilesW, tilesH); 378 388 389 std::array<u16, 9> territoryCellCounts; 390 territoryCellCounts.fill(0); 391 379 392 // Find all territory influence entities 380 393 CComponentManager::InterfaceList influences = GetSimContext().GetComponentManager().GetEntitiesWithInterface(IID_TerritoryInfluence); 381 394 … … 496 509 if (m_Territories->get(nx, nz) != owner) 497 510 continue; 498 511 m_Territories->set(nx, nz, owner | TERRITORY_CONNECTED_MASK); 512 if (m_CostGrid->get(nx, nz) < m_ImpassableCost) 513 ++territoryCellCounts[owner]; 499 514 ); 500 515 } 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; 501 521 } 502 522 503 523 std::vector<STerritoryBoundary> CCmpTerritoryManager::ComputeBoundaries() … … 510 530 return CTerritoryBoundaryCalculator::ComputeBoundaries(m_Territories); 511 531 } 512 532 533 u8 CCmpTerritoryManager::GetTerritoryPercentage(player_id_t player) 534 { 535 return m_TerritoryControlPercentages[player]; 536 } 537 513 538 void CCmpTerritoryManager::UpdateBoundaryLines() 514 539 { 515 540 PROFILE("update boundary lines"); -
simulation2/components/ICmpTerritoryManager.cpp
26 26 DEFINE_INTERFACE_METHOD_3("GetNeighbours", std::vector<u32>, ICmpTerritoryManager, GetNeighbours, entity_pos_t, entity_pos_t, bool) 27 27 DEFINE_INTERFACE_METHOD_2("IsConnected", bool, ICmpTerritoryManager, IsConnected, entity_pos_t, entity_pos_t) 28 28 DEFINE_INTERFACE_METHOD_2("SetTerritoryBlinking", void, ICmpTerritoryManager, SetTerritoryBlinking, entity_pos_t, entity_pos_t) 29 DEFINE_INTERFACE_METHOD_1("GetTerritoryPercentage", u8, ICmpTerritoryManager, GetTerritoryPercentage, player_id_t) 29 30 END_INTERFACE_WRAPPER(TerritoryManager) -
simulation2/components/ICmpTerritoryManager.h
72 72 */ 73 73 virtual void SetTerritoryBlinking(entity_pos_t x, entity_pos_t z) = 0; 74 74 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 75 81 DECLARE_INTERFACE_TYPE(TerritoryManager) 76 82 }; 77 83