summaryrefslogtreecommitdiff
path: root/game/network/routeWalker.cpp
blob: cdbc879afe9db743b01e006dc7c5d68b3db070d4 (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
38
#include "routeWalker.h"
#include <array>
#include <game/network/link.h>
#include <limits>
#include <memory>
#include <utility>
#include <vector>

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

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

void
// NOLINTNEXTLINE(misc-no-recursion)
RouteWalker::findRouteTo(const Link::End & currentEnd, const Node::Ptr & 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 & next : currentEnd.nexts) {
		const auto link = next.first.lock();
		currentSolution.emplace_back(next);
		findRouteTo(link->ends[!next.second], dest, length + link->length);
		currentSolution.pop_back();
	}
	visited.erase(&currentEnd);
}