forked from steaphangreene/acidmud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperties.cpp
More file actions
106 lines (97 loc) · 3.58 KB
/
properties.cpp
File metadata and controls
106 lines (97 loc) · 3.58 KB
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
// *************************************************************************
// This file is part of AcidMUD by Steaphan Greene
//
// Copyright 1999-2022 Steaphan Greene <steaphan@gmail.com>
//
// AcidMUD is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// AcidMUD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with AcidMUD (see the file named "COPYING");
// If not, see <http://www.gnu.org/licenses/>.
//
// *************************************************************************
#include <vector>
#include "color.hpp"
#include "log.hpp"
#include "outfile.hpp"
#include "properties.hpp"
// Generate initial skills definitions pair vector automatically from known skills list.
template <size_t L>
constexpr std::array<std::pair<uint32_t, std::u8string>, L> names2entry(
std::array<const char8_t*, L> in) {
std::array<std::pair<uint32_t, std::u8string>, L> ret;
std::transform(in.begin(), in.end(), ret.begin(), [](auto&& x) {
return std::make_pair(crc32c_c(x, 0xFFFFFFFFU, 0) ^ 0xFFFFFFFFU, x);
});
std::sort(ret.begin(), ret.end());
return ret;
};
static const auto skill_defs_array = names2entry(prop_names);
static std::vector<std::pair<uint32_t, std::u8string>> skill_defs(
skill_defs_array.begin(),
skill_defs_array.end());
void save_prop_names_to(outfile& fl) {
std::sort(skill_defs.begin(), skill_defs.end());
skill_defs.erase(std::unique(skill_defs.begin(), skill_defs.end()), skill_defs.end());
fl.append(u8"{}\n", skill_defs.size());
for (auto skn : skill_defs) {
if (skn.second.length() > 255) {
loger(u8"Error: Skill name too long: '{}'", skn.second);
fl.append(u8"{:08X}:Undefined\n", skn.first);
} else {
fl.append(u8"{:08X}:{}\n", skn.first, skn.second);
}
}
}
void load_prop_names_from(std::u8string_view& fl) {
int32_t size = nextnum(fl);
skill_defs.reserve(skill_defs.size() + size);
for (int sk = 0; sk < size; ++sk) {
uint32_t hash = nexthex(fl);
nextchar(fl); // Skip Colon
skill_defs.emplace_back(std::make_pair(hash, getuntil(fl, '\n')));
}
std::sort(skill_defs.begin(), skill_defs.end());
skill_defs.erase(std::unique(skill_defs.begin(), skill_defs.end()), skill_defs.end());
}
void purge_invalid_prop_names() {
for (auto& sk : skill_defs) {
sk.first = crc32c(sk.second);
}
std::sort(skill_defs.begin(), skill_defs.end());
skill_defs.erase(std::unique(skill_defs.begin(), skill_defs.end()), skill_defs.end());
}
void confirm_skill_hash(uint32_t stok) {
auto itn = skill_defs.begin();
for (; itn != skill_defs.end() && itn->first != stok; ++itn) {
}
if (itn == skill_defs.end()) {
loger(u8"Error: bogus skill hash (x{:08X})", stok);
skill_defs.emplace_back(std::make_pair(stok, u8"Unknown"));
}
}
void insert_skill_hash(uint32_t stok, const std::u8string_view& s) {
auto itn = skill_defs.begin();
for (; itn != skill_defs.end() && itn->first != stok; ++itn) {
}
if (itn == skill_defs.end()) {
skill_defs.emplace_back(std::make_pair(stok, s));
}
}
std::u8string SkillName(uint32_t sktok) {
std::u8string name = u8"Undefined";
for (auto n : skill_defs) {
if (n.first == sktok) {
name = n.second;
}
}
return name;
}