| 1335 | } |
| 1336 | |
| 1337 | std::list<Tile*> GameMap::findBestPath(const Creature* creature, Tile* tileStart, const std::vector<Tile*> possibleDests, |
| 1338 | Tile*& chosenTile) |
| 1339 | { |
| 1340 | chosenTile = nullptr; |
| 1341 | std::list<Tile*> returnList; |
| 1342 | if(possibleDests.empty()) |
| 1343 | return returnList; |
| 1344 | |
| 1345 | // We start by sorting the vector |
| 1346 | std::vector<Tile*> possibleDestsTmp = possibleDests; |
| 1347 | std::sort(possibleDestsTmp.begin(), possibleDestsTmp.end(), [this, &tileStart](Tile* tile1, Tile* tile2){ |
| 1348 | int d1 = Pathfinding::squaredDistanceTile(*tile1, *tileStart); |
| 1349 | int d2 = Pathfinding::squaredDistanceTile(*tile2, *tileStart); |
| 1350 | return d1 < d2; |
| 1351 | }); |
| 1352 | |
| 1353 | double magic = 2.0; |
| 1354 | for(Tile* tile : possibleDestsTmp) |
| 1355 | { |
| 1356 | if(!pathExists(creature, tileStart, tile)) |
| 1357 | continue; |
| 1358 | |
| 1359 | // The first reachable tile is the best by default |
| 1360 | Ogre::Real dist = std::hypotf(tile->getX() - tileStart->getX(), tile->getY() - tileStart->getY()); |
| 1361 | if(chosenTile == nullptr) |
| 1362 | { |
| 1363 | chosenTile = tile; |
| 1364 | returnList = path(tileStart, tile, creature, creature->getSeat(), false); |
| 1365 | continue; |
| 1366 | } |
| 1367 | |
| 1368 | // We compute the path only if walkableDist < (dist * magic). |
| 1369 | double walkableDist = static_cast<double>(returnList.size()); |
| 1370 | if(walkableDist < (dist * magic)) |
| 1371 | continue; |
| 1372 | |
| 1373 | std::list<Tile*> pathTmp = path(tileStart, tile, creature, creature->getSeat(), false); |
| 1374 | if(pathTmp.size() < returnList.size()) |
| 1375 | { |
| 1376 | // The path is shorter |
| 1377 | chosenTile = tile; |
| 1378 | returnList = pathTmp; |
| 1379 | } |
| 1380 | } |
| 1381 | return returnList; |
| 1382 | } |
| 1383 | |
| 1384 | bool GameMap::pathExists(const Creature* creature, Tile* tileStart, Tile* tileEnd) |
| 1385 | { |
no test coverage detected