summaryrefslogtreecommitdiff
path: root/game/vehicles/railloco.cpp
blob: 6745c8fbc03ae65fd1d4a4e8ec26c9ea76df2ddf (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
140
141
142
143
144
145
146
#include "railloco.h"
#include "gfx/gl/shader.h"
#include "gfx/models/obj.h"
#include "gfx/models/texture.h"
#include <algorithm>
#include <array>
#include <cache.h>
#include <filesystem>
#include <functional>
#include <glm/glm.hpp>
#include <iterator>
#include <lib/resource.h>
#include <location.hpp>
#include <map>
#include <maths.h>
#include <memory>
#include <random>
#include <set>
#include <utility>
#include <vector>

RailVehicleClass::RailVehicleClass(const std::string & name) :
	RailVehicleClass {std::make_unique<ObjParser>(Resource::mapPath(name + ".obj")),
			Texture::cachedTexture.get(Resource::mapPath(name + ".png"))}
{
}

RailVehicleClass::RailVehicleClass(std::unique_ptr<ObjParser> o, std::shared_ptr<Texture> t) : texture {std::move(t)}
{
	wheelBase = bogieOffset(*o);
	length = objectLength(*o);
	const auto m = o->createMeshes();
	bodyMesh = m.at("Body");
	bogies[0] = m.at("Bogie1");
	bogies[1] = m.at("Bogie2");
}

void
RailVehicleClass::render(const Shader & shader, const Location & location, const std::array<Location, 2> & bl) const
{
	texture->Bind();
	for (auto b = 0U; b < bogies.size(); ++b) {
		shader.setModel(bl[b]);
		bogies[b]->Draw();
	}
	shader.setModel(location);
	bodyMesh->Draw();
}

float
RailVehicleClass::bogieOffset(ObjParser & o)
{
	float wheelBase {0};
	// offset bogie positions so they can be set directly
	for (auto & object : o.objects) { // bogie object index
		if (!object.first.starts_with("Bogie")) {
			continue;
		}
		std::set<std::pair<float, int>> vertexIds;
		for (const auto & face : object.second) {
			for (const auto & faceElement : face) {
				vertexIds.emplace(o.vertices[faceElement.x - 1].z, faceElement.x - 1);
			}
		}
		const auto offset = (vertexIds.begin()->first + vertexIds.rbegin()->first) / 2;
		for (const auto & v : vertexIds) {
			o.vertices[v.second].z -= offset;
		}
		wheelBase += std::abs(offset);
	}
	return wheelBase;
}

float
RailVehicleClass::objectLength(ObjParser & o)
{
	const auto mme = std::minmax_element(o.vertices.begin(), o.vertices.end(), [](const auto & v1, const auto & v2) {
		return v1.z < v2.z;
	});
	return mme.second->z - mme.first->z;
}

void
RailVehicle::render(const Shader & shader) const
{
	rvClass->render(shader, location, bogies);
}

void
Train::move(TickDuration dur)
{
	static std::mt19937 gen(std::random_device {}());
	linkDist += dur.count() * speed;
	auto curLink {linkHist.getCurrent()};
	while (linkDist > curLink.first->length) {
		const auto location = curLink.first->positionAt(curLink.first->length, curLink.second);
		auto nexts {curLink.first->nexts[1 - curLink.second]};
		auto last = std::remove_if(nexts.begin(), nexts.end(), [ang = location.rot.y](const Link::Next & n) {
			return std::abs(normalize(n.first.lock()->ends[n.second].second - ang)) > 0.1F;
		});
		if (last != nexts.begin()) {
			auto off = std::uniform_int_distribution<>(0, std::distance(nexts.begin(), last) - 1)(gen);
			linkDist -= curLink.first->length;
			curLink = linkHist.add(nexts[off].first, nexts[off].second);
		}
		else {
			linkDist = curLink.first->length;
			speed = 0;
		}
	}
}

void
Train::render(const Shader & shader) const
{
	apply(&Renderable::render, shader);
}

Location
Train::getBogiePosition(float linkDist, float dist) const
{
	float b2linkDist {};
	const auto b2Link = linkHist.getAt(dist - linkDist, &b2linkDist);
	return b2Link.first->positionAt(b2linkDist, b2Link.second);
}

void
RailVehicle::move(const Train * t, float & trailBy)
{
	const auto overhang {(rvClass->length - rvClass->wheelBase) / 2};
	const auto & b1Pos = bogies[0] = t->getBogiePosition(t->linkDist, trailBy += overhang);
	const auto & b2Pos = bogies[1] = t->getBogiePosition(t->linkDist, trailBy += rvClass->wheelBase);
	const auto diff = glm::normalize(b2Pos.pos - b1Pos.pos);
	location.pos = (b1Pos.pos + b2Pos.pos) / 2.F;
	location.rot = {-vector_pitch(diff), vector_yaw(diff), 0};
	trailBy += 0.6F + overhang;
}

void
Train::tick(TickDuration dur)
{
	move(dur);

	float trailBy {0.F};
	apply(&RailVehicle::move, this, std::ref(trailBy));
}