summaryrefslogtreecommitdiff
path: root/game/network/network.cpp
blob: 083b08e1c7860b99ecfc7fb846e0548dc3e4a922 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "network.h"
#include "routeWalker.h"
#include <array>
#include <cache.h>
#include <filesystem>
#include <game/network/link.h>
#include <gfx/models/texture.h>
#include <glm/gtx/intersect.hpp>
#include <initializer_list>
#include <ray.h>
#include <stdexcept>
#include <utility>

Network::Network(const std::string & tn) : texture {Texture::cachedTexture.get(tn)} { }

Node::Ptr
Network::nodeAt(glm::vec3 pos)
{
	return newNodeAt(pos).first;
}

Network::NodeInsertion
Network::newNodeAt(glm::vec3 pos)
{
	if (auto [n, i] = candidateNodeAt(pos); i == NodeIs::NotInNetwork) {
		return {*nodes.insert(std::move(n)).first, i};
	}
	else {
		return {std::move(n), NodeIs::InNetwork};
	}
}

Node::Ptr
Network::findNodeAt(glm::vec3 pos) const
{
	if (const auto n = nodes.find(pos); n != nodes.end()) {
		return *n;
	}
	return {};
}

Network::NodeInsertion
Network::candidateNodeAt(glm::vec3 pos) const
{
	if (const auto n = nodes.find(pos); n != nodes.end()) {
		return {*n, NodeIs::InNetwork};
	}
	return {std::make_shared<Node>(pos), NodeIs::NotInNetwork};
}

Node::Ptr
Network::intersectRayNodes(const Ray & ray) const
{
	// Click within 2m of a node
	if (const auto node = std::find_if(nodes.begin(), nodes.end(),
				[&ray](const Node::Ptr & node) {
					glm::vec3 ipos, inorm;
					return glm::intersectRaySphere(ray.start, ray.direction, node->pos, 2.F, ipos, inorm);
				});
			node != nodes.end()) {
		return *node;
	}
	return {};
}

void
Network::joinLinks(const Link::Ptr & l, const Link::Ptr & ol)
{
	if (l != ol) {
		for (const auto oe : {0U, 1U}) {
			for (const auto te : {0U, 1U}) {
				if (l->ends[te].node == ol->ends[oe].node) {
					l->ends[te].nexts.emplace_back(ol, oe);
					ol->ends[oe].nexts.emplace_back(l, te);
				}
			}
		}
	}
}

Link::Nexts
Network::routeFromTo(const Link::End & start, glm::vec3 dest) const
{
	auto destNode {findNodeAt(dest)};
	if (!destNode) {
		throw std::out_of_range("Node does not exist in network");
	}
	return routeFromTo(start, destNode);
}

Link::Nexts
Network::routeFromTo(const Link::End & end, const Node::Ptr & dest) const
{
	return RouteWalker().findRouteTo(end, dest);
}

GenCurveDef
Network::genCurveDef(const glm::vec3 & start, const glm::vec3 & end, float startDir)
{
	const auto diff {end - start};
	const auto vy {vector_yaw(diff)};
	const auto dir = pi + startDir;
	const auto flatStart {!start}, flatEnd {!end};
	const auto n2ed {(vy * 2) - dir - pi};
	const auto centre {find_arc_centre(flatStart, dir, flatEnd, n2ed)};

	if (centre.second) { // right hand arc
		return {end, start, centre.first};
	}
	return {start, end, centre.first};
}

std::pair<GenCurveDef, GenCurveDef>
Network::genCurveDef(const glm::vec3 & start, const glm::vec3 & end, float startDir, float endDir)
{
	startDir += pi;
	endDir += pi;
	const glm::vec2 flatStart {!start}, flatEnd {!end};
	auto midheight = [&](auto mid) {
		const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid);
		return start.z + ((end.z - start.z) * (sm / (sm + em)));
	};
	if (const auto radii = find_arcs_radius(flatStart, startDir, flatEnd, endDir); radii.first < radii.second) {
		const auto radius {radii.first};
		const auto c1 = flatStart + sincosf(startDir + half_pi) * radius;
		const auto c2 = flatEnd + sincosf(endDir + half_pi) * radius;
		const auto mid = (c1 + c2) / 2.F;
		const auto midh = mid ^ midheight(mid);
		return {{start, midh, c1}, {end, midh, c2}};
	}
	else {
		const auto radius {radii.second};
		const auto c1 = flatStart + sincosf(startDir - half_pi) * radius;
		const auto c2 = flatEnd + sincosf(endDir - half_pi) * radius;
		const auto mid = (c1 + c2) / 2.F;
		const auto midh = mid ^ midheight(mid);
		return {{midh, start, c1}, {midh, end, c2}};
	}
}