about summary refs log tree commit diff stats
path: root/tools/assign_ids/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tools/assign_ids/main.cpp')
-rw-r--r--tools/assign_ids/main.cpp228
1 files changed, 183 insertions, 45 deletions
diff --git a/tools/assign_ids/main.cpp b/tools/assign_ids/main.cpp index e65e5e4..4cf7c3f 100644 --- a/tools/assign_ids/main.cpp +++ b/tools/assign_ids/main.cpp
@@ -2,6 +2,7 @@
2#include <google/protobuf/text_format.h> 2#include <google/protobuf/text_format.h>
3 3
4#include <cstdint> 4#include <cstdint>
5#include <filesystem>
5#include <fstream> 6#include <fstream>
6#include <iostream> 7#include <iostream>
7#include <map> 8#include <map>
@@ -40,6 +41,10 @@ class AssignIds {
40 ReadIds(ids_path); 41 ReadIds(ids_path);
41 42
42 ProcessMaps(datadir_path); 43 ProcessMaps(datadir_path);
44 ProcessSpecialIds();
45 ProcessProgressivesFile(datadir_path / "progressives.txtpb");
46 ProcessDoorGroupsFile(datadir_path / "door_groups.txtpb");
47 ProcessGlobalMetadataFile(datadir_path / "metadata.txtpb");
43 48
44 WriteIds(ids_path); 49 WriteIds(ids_path);
45 50
@@ -54,50 +59,27 @@ class AssignIds {
54 id_mappings_ = ReadIdsFromYaml(path.string()); 59 id_mappings_ = ReadIdsFromYaml(path.string());
55 60
56 for (const auto& [_, map] : id_mappings_.maps()) { 61 for (const auto& [_, map] : id_mappings_.maps()) {
57 for (const auto& [_, id] : map.doors()) { 62 UpdateNextId(map.doors());
58 if (id > next_id_) {
59 next_id_ = id;
60 }
61 }
62 63
63 for (const auto& [_, room] : map.rooms()) { 64 for (const auto& [_, room] : map.rooms()) {
64 for (const auto& [_, id] : room.panels()) { 65 UpdateNextId(room.panels());
65 if (id > next_id_) { 66 UpdateNextId(room.masteries());
66 next_id_ = id; 67 UpdateNextId(room.keyholders());
67 } 68 UpdateNextId(room.ports());
68 }
69
70 for (const auto& [_, id] : room.masteries()) {
71 if (id > next_id_) {
72 next_id_ = id;
73 }
74 }
75 }
76 }
77
78 for (const auto& [_, id] : id_mappings_.special()) {
79 if (id > next_id_) {
80 next_id_ = id;
81 }
82 }
83
84 for (const auto& [_, id] : id_mappings_.letters()) {
85 if (id > next_id_) {
86 next_id_ = id;
87 } 69 }
88 } 70 }
89 71
90 for (const auto& [_, id] : id_mappings_.endings()) { 72 UpdateNextId(id_mappings_.special());
91 if (id > next_id_) { 73 UpdateNextId(id_mappings_.letters());
92 next_id_ = id; 74 UpdateNextId(id_mappings_.endings());
93 } 75 UpdateNextId(id_mappings_.progressives());
94 } 76 UpdateNextId(id_mappings_.door_groups());
95 77
96 next_id_++; 78 next_id_++;
97 } 79 }
98 80
99 void WriteIds(std::filesystem::path path) { 81 void WriteIds(std::filesystem::path path) {
100 WriteIdsAsYaml(id_mappings_, path.string()); 82 WriteIdsAsYaml(output_, path.string());
101 } 83 }
102 84
103 void ProcessMaps(std::filesystem::path path) { 85 void ProcessMaps(std::filesystem::path path) {
@@ -130,18 +112,23 @@ class AssignIds {
130 112
131 void ProcessDoor(const HumanDoor& h_door, 113 void ProcessDoor(const HumanDoor& h_door,
132 const std::string& current_map_name) { 114 const std::string& current_map_name) {
133 if (h_door.type() == DoorType::EVENT) { 115 if (h_door.type() == DoorType::EVENT && !h_door.latch() &&
116 !h_door.legacy_location()) {
134 return; 117 return;
135 } 118 }
136 119
120 auto& maps = *output_.mutable_maps();
121 auto& doors = *maps[current_map_name].mutable_doors();
122
137 if (!id_mappings_.maps().contains(current_map_name) || 123 if (!id_mappings_.maps().contains(current_map_name) ||
138 !id_mappings_.maps() 124 !id_mappings_.maps()
139 .at(current_map_name) 125 .at(current_map_name)
140 .doors() 126 .doors()
141 .contains(h_door.name())) { 127 .contains(h_door.name())) {
142 auto& maps = *id_mappings_.mutable_maps();
143 auto& doors = *maps[current_map_name].mutable_doors();
144 doors[h_door.name()] = next_id_++; 128 doors[h_door.name()] = next_id_++;
129 } else {
130 doors[h_door.name()] =
131 id_mappings_.maps().at(current_map_name).doors().at(h_door.name());
145 } 132 }
146 } 133 }
147 134
@@ -156,6 +143,10 @@ class AssignIds {
156 void ProcessRoom(const HumanRoom& h_room, 143 void ProcessRoom(const HumanRoom& h_room,
157 const std::string& current_map_name) { 144 const std::string& current_map_name) {
158 for (const HumanPanel& h_panel : h_room.panels()) { 145 for (const HumanPanel& h_panel : h_room.panels()) {
146 auto& maps = *output_.mutable_maps();
147 auto& rooms = *maps[current_map_name].mutable_rooms();
148 auto& panels = *rooms[h_room.name()].mutable_panels();
149
159 if (!id_mappings_.maps().contains(current_map_name) || 150 if (!id_mappings_.maps().contains(current_map_name) ||
160 !id_mappings_.maps() 151 !id_mappings_.maps()
161 .at(current_map_name) 152 .at(current_map_name)
@@ -167,23 +158,33 @@ class AssignIds {
167 .at(h_room.name()) 158 .at(h_room.name())
168 .panels() 159 .panels()
169 .contains(h_panel.name())) { 160 .contains(h_panel.name())) {
170 auto& maps = *id_mappings_.mutable_maps();
171 auto& rooms = *maps[current_map_name].mutable_rooms();
172 auto& panels = *rooms[h_room.name()].mutable_panels();
173 panels[h_panel.name()] = next_id_++; 161 panels[h_panel.name()] = next_id_++;
162 } else {
163 panels[h_panel.name()] = id_mappings_.maps()
164 .at(current_map_name)
165 .rooms()
166 .at(h_room.name())
167 .panels()
168 .at(h_panel.name());
174 } 169 }
175 } 170 }
176 171
177 for (const HumanLetter& h_letter : h_room.letters()) { 172 for (const HumanLetter& h_letter : h_room.letters()) {
178 std::string lettername = GetLetterName(h_letter.key(), h_letter.level2()); 173 std::string lettername = GetLetterName(h_letter.key(), h_letter.level2());
179 174
175 auto& letters = *output_.mutable_letters();
180 if (!id_mappings_.letters().contains(lettername)) { 176 if (!id_mappings_.letters().contains(lettername)) {
181 auto& letters = *id_mappings_.mutable_letters();
182 letters[lettername] = next_id_++; 177 letters[lettername] = next_id_++;
178 } else {
179 letters[lettername] = id_mappings_.letters().at(lettername);
183 } 180 }
184 } 181 }
185 182
186 for (const HumanMastery& h_mastery : h_room.masteries()) { 183 for (const HumanMastery& h_mastery : h_room.masteries()) {
184 auto& maps = *output_.mutable_maps();
185 auto& rooms = *maps[current_map_name].mutable_rooms();
186 auto& masteries = *rooms[h_room.name()].mutable_masteries();
187
187 if (!id_mappings_.maps().contains(current_map_name) || 188 if (!id_mappings_.maps().contains(current_map_name) ||
188 !id_mappings_.maps() 189 !id_mappings_.maps()
189 .at(current_map_name) 190 .at(current_map_name)
@@ -195,27 +196,164 @@ class AssignIds {
195 .at(h_room.name()) 196 .at(h_room.name())
196 .masteries() 197 .masteries()
197 .contains(h_mastery.name())) { 198 .contains(h_mastery.name())) {
198 auto& maps = *id_mappings_.mutable_maps();
199 auto& rooms = *maps[current_map_name].mutable_rooms();
200 auto& masteries = *rooms[h_room.name()].mutable_masteries();
201 masteries[h_mastery.name()] = next_id_++; 199 masteries[h_mastery.name()] = next_id_++;
200 } else {
201 masteries[h_mastery.name()] = id_mappings_.maps()
202 .at(current_map_name)
203 .rooms()
204 .at(h_room.name())
205 .masteries()
206 .at(h_mastery.name());
202 } 207 }
203 } 208 }
204 209
205 for (const HumanEnding& h_ending : h_room.endings()) { 210 for (const HumanEnding& h_ending : h_room.endings()) {
211 auto& endings = *output_.mutable_endings();
212
206 if (!id_mappings_.endings().contains(h_ending.name())) { 213 if (!id_mappings_.endings().contains(h_ending.name())) {
207 auto& endings = *id_mappings_.mutable_endings();
208 endings[h_ending.name()] = next_id_++; 214 endings[h_ending.name()] = next_id_++;
215 } else {
216 endings[h_ending.name()] = id_mappings_.endings().at(h_ending.name());
217 }
218 }
219
220 for (const HumanKeyholder& h_keyholder : h_room.keyholders()) {
221 if (!h_keyholder.has_key()) {
222 continue;
223 }
224
225 auto& maps = *output_.mutable_maps();
226 auto& rooms = *maps[current_map_name].mutable_rooms();
227 auto& keyholders = *rooms[h_room.name()].mutable_keyholders();
228
229 if (!id_mappings_.maps().contains(current_map_name) ||
230 !id_mappings_.maps()
231 .at(current_map_name)
232 .rooms()
233 .contains(h_room.name()) ||
234 !id_mappings_.maps()
235 .at(current_map_name)
236 .rooms()
237 .at(h_room.name())
238 .keyholders()
239 .contains(h_keyholder.name())) {
240 keyholders[h_keyholder.name()] = next_id_++;
241 } else {
242 keyholders[h_keyholder.name()] = id_mappings_.maps()
243 .at(current_map_name)
244 .rooms()
245 .at(h_room.name())
246 .keyholders()
247 .at(h_keyholder.name());
248 }
249 }
250
251 for (const HumanPort& h_port : h_room.ports()) {
252 if (h_port.no_shuffle()) {
253 continue;
254 }
255
256 auto& maps = *output_.mutable_maps();
257 auto& rooms = *maps[current_map_name].mutable_rooms();
258 auto& ports = *rooms[h_room.name()].mutable_ports();
259
260 if (!id_mappings_.maps().contains(current_map_name) ||
261 !id_mappings_.maps()
262 .at(current_map_name)
263 .rooms()
264 .contains(h_room.name()) ||
265 !id_mappings_.maps()
266 .at(current_map_name)
267 .rooms()
268 .at(h_room.name())
269 .ports()
270 .contains(h_port.name())) {
271 ports[h_port.name()] = next_id_++;
272 } else {
273 ports[h_port.name()] = id_mappings_.maps()
274 .at(current_map_name)
275 .rooms()
276 .at(h_room.name())
277 .ports()
278 .at(h_port.name());
279 }
280 }
281 }
282
283 void ProcessSpecialIds() {
284 auto& specials = *output_.mutable_special();
285
286 for (const auto& [special_name, ap_id] : id_mappings_.special()) {
287 specials[special_name] = ap_id;
288 }
289 }
290
291 void ProcessProgressivesFile(std::filesystem::path path) {
292 if (!std::filesystem::exists(path)) {
293 return;
294 }
295
296 auto h_progs = ReadMessageFromFile<HumanProgressives>(path.string());
297 auto& progs = *output_.mutable_progressives();
298
299 for (const HumanProgressive& h_prog : h_progs.progressives()) {
300 if (!id_mappings_.progressives().contains(h_prog.name())) {
301 progs[h_prog.name()] = next_id_++;
302 } else {
303 progs[h_prog.name()] = id_mappings_.progressives().at(h_prog.name());
304 }
305 }
306 }
307
308 void ProcessDoorGroupsFile(std::filesystem::path path) {
309 if (!std::filesystem::exists(path)) {
310 return;
311 }
312
313 auto h_groups = ReadMessageFromFile<HumanDoorGroups>(path.string());
314 auto& groups = *output_.mutable_door_groups();
315
316 for (const HumanDoorGroup& h_group : h_groups.door_groups()) {
317 if (!id_mappings_.door_groups().contains(h_group.name())) {
318 groups[h_group.name()] = next_id_++;
319 } else {
320 groups[h_group.name()] = id_mappings_.door_groups().at(h_group.name());
321 }
322 }
323 }
324
325 void ProcessGlobalMetadataFile(std::filesystem::path path) {
326 if (!std::filesystem::exists(path)) {
327 return;
328 }
329
330 auto h_metadata = ReadMessageFromFile<HumanGlobalMetadata>(path.string());
331 auto& specials = *output_.mutable_special();
332
333 for (const std::string& h_special : h_metadata.special_names()) {
334 if (!id_mappings_.special().contains(h_special)) {
335 specials[h_special] = next_id_++;
336 } else {
337 specials[h_special] = id_mappings_.special().at(h_special);
209 } 338 }
210 } 339 }
211 } 340 }
212 341
213 private: 342 private:
343 void UpdateNextId(const google::protobuf::Map<std::string, uint64_t>& ids) {
344 for (const auto& [_, id] : ids) {
345 if (id > next_id_) {
346 next_id_ = id;
347 }
348 }
349 }
350
214 std::string mapdir_; 351 std::string mapdir_;
215 352
216 uint64_t next_id_ = 1; 353 uint64_t next_id_ = 1;
217 354
218 IdMappings id_mappings_; 355 IdMappings id_mappings_;
356 IdMappings output_;
219}; 357};
220 358
221} // namespace 359} // namespace