Ticket #3321: 1_TerritoryManager-v3.patch
File 1_TerritoryManager-v3.patch, 4.3 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 … … 123 127 124 128 m_AnimTime = 0.0; 125 129 130 m_TerritoryControlPercentages.fill(0); 131 m_TerritoryTotalPassableCellCount = USHRT_MAX; 132 126 133 // Register Relax NG validator 127 134 CXeromyces::AddValidator(g_VFS, "territorymanager", "simulation/data/territorymanager.rng"); 128 135 … … 260 267 261 268 void CalculateTerritories(); 262 269 270 u8 GetTerritoryPercentage(player_id_t player); 271 263 272 std::vector<STerritoryBoundary> ComputeBoundaries(); 264 273 265 274 void UpdateBoundaryLines(); … … 337 346 int tilesH = passGrid.m_H / NAVCELLS_PER_TERRITORY_TILE; 338 347 339 348 m_CostGrid = new Grid<u8>(tilesW, tilesH); 349 m_TerritoryTotalPassableCellCount = 0; 340 350 341 351 for (int i = 0; i < tilesW; ++i) 342 352 { … … 353 363 else if (c & passClassUnrestricted) 354 364 m_CostGrid->set(i, j, 255); // off the world; use maximum cost 355 365 else 366 { 356 367 m_CostGrid->set(i, j, 1); 368 ++m_TerritoryTotalPassableCellCount; 369 } 357 370 } 358 371 } 359 372 } … … 376 389 377 390 m_Territories = new Grid<u8>(tilesW, tilesH); 378 391 392 CmpPtr<ICmpPlayerManager> cmpPlayerManager(GetSystemEntity()); 393 u8 playerCount = (cmpPlayerManager) ? cmpPlayerManager->GetNumPlayers() : m_TerritoryControlPercentages.size(); 394 std::vector<u16> territoryCellCounts(playerCount, 0); 395 379 396 // Find all territory influence entities 380 397 CComponentManager::InterfaceList influences = GetSimContext().GetComponentManager().GetEntitiesWithInterface(IID_TerritoryInfluence); 381 398 … … 496 513 if (m_Territories->get(nx, nz) != owner) 497 514 continue; 498 515 m_Territories->set(nx, nz, owner | TERRITORY_CONNECTED_MASK); 516 if (m_CostGrid->get(nx, nz) < m_ImpassableCost) 517 ++territoryCellCounts[owner]; 499 518 ); 500 519 } 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; 501 525 } 502 526 503 527 std::vector<STerritoryBoundary> CCmpTerritoryManager::ComputeBoundaries() … … 510 534 return CTerritoryBoundaryCalculator::ComputeBoundaries(m_Territories); 511 535 } 512 536 537 u8 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 513 545 void CCmpTerritoryManager::UpdateBoundaryLines() 514 546 { 515 547 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