MCPcopy Create free account
hub / github.com/OpenApoc/OpenApoc / updatePathfinding

Method updatePathfinding

game/state/battle/battle.cpp:1511–1622  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1509}
1510
1511void Battle::updatePathfinding(GameState &, unsigned int ticks)
1512{
1513 // Throttling updates so that big explosions won't lag
1514 static const int LIMIT_PER_TICK = 10;
1515
1516 // How much attempts are given to the pathfinding until giving up and concluding that
1517 // there is no path between two sectors. This is a multiplier for "distance", which is
1518 // a minimum number of iterations required to pathfind between two locations
1519 static const int PATH_ITERATION_LIMIT_MULTIPLIER = 2;
1520
1521 // How much can resulting path differ from optimal path
1522 static const int PATH_COST_LIMIT_MULTIPLIER = 2;
1523
1524 int lbCount = losBlocks.size();
1525 auto &mapRef = *map;
1526
1527 // Fill up map of helpers
1528 // It would be appropriate to use a std::map here, alas, it doesn't work when there's
1529 // no default constructor available, so I have to use this kludge
1530 std::vector<BattleUnitTileHelper> helperMap = {BattleUnitTileHelper(mapRef, (BattleUnitType)0),
1531 BattleUnitTileHelper(mapRef, (BattleUnitType)1),
1532 BattleUnitTileHelper(mapRef, (BattleUnitType)2),
1533 BattleUnitTileHelper(mapRef, (BattleUnitType)3)};
1534
1535 // First update all center positions
1536 for (int i = 0; i < lbCount; i++)
1537 {
1538 if (!blockNeedsUpdate[i])
1539 {
1540 continue;
1541 }
1542 blockNeedsUpdate[i] = false;
1543
1544 // Mark all paths including this block as needing an update
1545 for (int j = 0; j < lbCount; j++)
1546 {
1547 if (linkAvailable[i + lbCount * j])
1548 {
1549 linkNeedsUpdate[std::min(i, j) + std::max(i, j) * lbCount] = true;
1550 }
1551 }
1552
1553 // Find closest to center valid position for every kind of unit
1554 auto &lb = *losBlocks[i];
1555 auto center = (lb.start + lb.end) / 2;
1556 for (auto &type : BattleUnitTypeList)
1557 {
1558 blockAvailable[type][i] =
1559 findLosBlockCenter(mapRef, type, lb, center, blockCenterPos[type][i]);
1560 }
1561 }
1562
1563 int updatesRemaining = ticks > 0 ? LIMIT_PER_TICK * ticks : -1;
1564
1565 // Now update all paths
1566 for (int i = 0; i < lbCount - 1; i++)
1567 {
1568 for (int j = i + 1; j < lbCount; j++)

Callers

nothing calls this directly

Calls 5

findLosBlockCenterFunction · 0.85
emptyMethod · 0.80
sizeMethod · 0.45
findShortestPathMethod · 0.45

Tested by

no test coverage detected