summaryrefslogtreecommitdiff
path: root/game/network/routeWalker.cpp
blob: 496f9f3271789c4bae680da612599a490a03a137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "routeWalker.h"
#include <array>
#include <game/network/link.h>
#include <limits>
#include <memory>
#include <utility>

RouteWalker::RouteWalker() : solutionLength {std::numeric_limits<float>::max()} { }

RouteWalker::Solution
RouteWalker::findRouteTo(const Link::End & currentEnd, const NodePtr & dest)
{
	findRouteTo(currentEnd, dest, 0);
	return bestSolution;
}

void
// NOLINTNEXTLINE(misc-no-recursion)
RouteWalker::findRouteTo(const Link::End & currentEnd, const NodePtr & dest, float length)
{
	if (currentEnd.node == dest && length < solutionLength) {
		bestSolution = currentSolution;
		solutionLength = length;
		return;
	}
	if (visited.contains(&currentEnd)) { // We've been here before
		return;
	}
	visited.insert(&currentEnd);
	for (const auto & nexts : currentEnd.nexts) {
		const auto link = nexts.first.lock();
		currentSolution.push_back(link);
		findRouteTo(link->ends[!nexts.second], dest, length + link->length);
		currentSolution.pop_back();
	}
	visited.erase(&currentEnd);
}