-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterritory.cpp
81 lines (73 loc) · 2.49 KB
/
territory.cpp
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
#include "pch.h"
#include "territory.h"
#include "resource.h"
#include "util.h"
#include <algorithm>
#include <array>
namespace trainlist8::territory {
namespace {
// The known territory IDs and their associated string resource IDs.
constexpr std::array<std::pair<unsigned int, unsigned int>, count> resourceIDs{{
{100, IDS_TERRITORY_MOJAVE},
{110, IDS_TERRITORY_NEEDLES},
{120, IDS_TERRITORY_CAJON},
{130, IDS_TERRITORY_SELIGMAN},
{150, IDS_TERRITORY_BARSTOW},
{200, IDS_TERRITORY_SBD},
{250, IDS_TERRITORY_BAKERSFIELD},
{260, IDS_TERRITORY_ROSEVILLE},
}};
static_assert(std::is_sorted(resourceIDs.cbegin(), resourceIDs.cend()));
// The loaded string resources, in the same order as resourceIDs.
constinit std::array<std::unique_ptr<std::wstring>, count> strings;
}
}
// Loads the territory name strings.
void trainlist8::territory::init(HINSTANCE instance) {
for(size_t i = 0; i != resourceIDs.size(); ++i) {
strings[i].reset(new std::wstring(util::loadString(instance, resourceIDs[i].second)));
}
}
// Returns the ID of a territory for a given block number.
std::optional<unsigned int> trainlist8::territory::idByBlock(int32_t block) {
if(block < 0) {
// A block number of −1 means the train is in an unsignalled location.
return {};
} else {
// The first three digits of the block number are the territory; the rest are the block within the territory.
unsigned int territory = block;
while(territory > 1000) {
territory /= 10;
}
return territory;
}
}
// Returns the ID of a territory in a given position.
unsigned int trainlist8::territory::idByIndex(size_t index) {
return resourceIDs[index].first;
}
// Returns the position of a territory with a given ID.
std::optional<size_t> trainlist8::territory::indexByID(unsigned int territory) {
auto i = std::lower_bound(resourceIDs.cbegin(), resourceIDs.cend(), territory,
[](const std::pair<unsigned int, unsigned int> &candidate, unsigned int target) -> bool {
return candidate.first < target;
});
if(i != resourceIDs.cend() && i->first == territory) {
return {i - resourceIDs.cbegin()};
} else {
return {};
}
}
// Returns the name of a territory in a given position.
const std::wstring *trainlist8::territory::nameByIndex(size_t index) {
return strings[index].get();
}
// Finds the name of a territory, if known, or nullptr if not.
const std::wstring *trainlist8::territory::nameByID(unsigned int territory) {
std::optional<size_t> index = indexByID(territory);
if(index) {
return strings[*index].get();
} else {
return nullptr;
}
}