about summary refs log tree commit diff stats
path: root/tools/assign_ids
diff options
context:
space:
mode:
Diffstat (limited to 'tools/assign_ids')
-rw-r--r--tools/assign_ids/main.cpp249
1 files changed, 199 insertions, 50 deletions
diff --git a/tools/assign_ids/main.cpp b/tools/assign_ids/main.cpp index 349c258..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>
@@ -9,6 +10,7 @@
9#include <string> 10#include <string>
10 11
11#include "proto/human.pb.h" 12#include "proto/human.pb.h"
13#include "util/ids_yaml_format.h"
12#include "util/naming.h" 14#include "util/naming.h"
13 15
14namespace com::fourisland::lingo2_archipelago { 16namespace com::fourisland::lingo2_archipelago {
@@ -34,11 +36,15 @@ class AssignIds {
34 36
35 void Run() { 37 void Run() {
36 std::filesystem::path datadir_path = mapdir_; 38 std::filesystem::path datadir_path = mapdir_;
37 std::filesystem::path ids_path = datadir_path / "ids.txtpb"; 39 std::filesystem::path ids_path = datadir_path / "ids.yaml";
38 40
39 ReadIds(ids_path); 41 ReadIds(ids_path);
40 42
41 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");
42 48
43 WriteIds(ids_path); 49 WriteIds(ids_path);
44 50
@@ -46,53 +52,34 @@ class AssignIds {
46 } 52 }
47 53
48 void ReadIds(std::filesystem::path path) { 54 void ReadIds(std::filesystem::path path) {
49 id_mappings_ = ReadMessageFromFile<IdMappings>(path.string()); 55 if (!std::filesystem::exists(path)) {
56 return;
57 }
58
59 id_mappings_ = ReadIdsFromYaml(path.string());
50 60
51 for (const auto& [_, map] : id_mappings_.maps()) { 61 for (const auto& [_, map] : id_mappings_.maps()) {
52 for (const auto& [_, id] : map.doors()) { 62 UpdateNextId(map.doors());
53 if (id > next_id_) {
54 next_id_ = id;
55 }
56 }
57 63
58 for (const auto& [_, room] : map.rooms()) { 64 for (const auto& [_, room] : map.rooms()) {
59 for (const auto& [_, id] : room.panels()) { 65 UpdateNextId(room.panels());
60 if (id > next_id_) { 66 UpdateNextId(room.masteries());
61 next_id_ = id; 67 UpdateNextId(room.keyholders());
62 } 68 UpdateNextId(room.ports());
63 }
64
65 for (const auto& [_, id] : room.masteries()) {
66 if (id > next_id_) {
67 next_id_ = id;
68 }
69 }
70 } 69 }
71 } 70 }
72 71
73 for (const auto& [_, id] : id_mappings_.special()) { 72 UpdateNextId(id_mappings_.special());
74 if (id > next_id_) { 73 UpdateNextId(id_mappings_.letters());
75 next_id_ = id; 74 UpdateNextId(id_mappings_.endings());
76 } 75 UpdateNextId(id_mappings_.progressives());
77 } 76 UpdateNextId(id_mappings_.door_groups());
78
79 for (const auto& [_, id] : id_mappings_.letters()) {
80 if (id > next_id_) {
81 next_id_ = id;
82 }
83 }
84 77
85 next_id_++; 78 next_id_++;
86 } 79 }
87 80
88 void WriteIds(std::filesystem::path path) { 81 void WriteIds(std::filesystem::path path) {
89 std::string output; 82 WriteIdsAsYaml(output_, path.string());
90 google::protobuf::TextFormat::PrintToString(id_mappings_, &output);
91
92 {
93 std::ofstream outputfile(path.string());
94 outputfile << output;
95 }
96 } 83 }
97 84
98 void ProcessMaps(std::filesystem::path path) { 85 void ProcessMaps(std::filesystem::path path) {
@@ -104,7 +91,7 @@ class AssignIds {
104 } 91 }
105 92
106 void ProcessMap(std::filesystem::path path) { 93 void ProcessMap(std::filesystem::path path) {
107 std::string map_name = path.filename(); 94 std::string map_name = path.filename().string();
108 95
109 ProcessDoorsFile(path / "doors.txtpb", map_name); 96 ProcessDoorsFile(path / "doors.txtpb", map_name);
110 ProcessRooms(path / "rooms", map_name); 97 ProcessRooms(path / "rooms", map_name);
@@ -125,18 +112,23 @@ class AssignIds {
125 112
126 void ProcessDoor(const HumanDoor& h_door, 113 void ProcessDoor(const HumanDoor& h_door,
127 const std::string& current_map_name) { 114 const std::string& current_map_name) {
128 if (h_door.type() == DoorType::EVENT) { 115 if (h_door.type() == DoorType::EVENT && !h_door.latch() &&
116 !h_door.legacy_location()) {
129 return; 117 return;
130 } 118 }
131 119
120 auto& maps = *output_.mutable_maps();
121 auto& doors = *maps[current_map_name].mutable_doors();
122
132 if (!id_mappings_.maps().contains(current_map_name) || 123 if (!id_mappings_.maps().contains(current_map_name) ||
133 !id_mappings_.maps() 124 !id_mappings_.maps()
134 .at(current_map_name) 125 .at(current_map_name)
135 .doors() 126 .doors()
136 .contains(h_door.name())) { 127 .contains(h_door.name())) {
137 auto& maps = *id_mappings_.mutable_maps();
138 auto& doors = *maps[current_map_name].mutable_doors();
139 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());
140 } 132 }
141 } 133 }
142 134
@@ -151,6 +143,10 @@ class AssignIds {
151 void ProcessRoom(const HumanRoom& h_room, 143 void ProcessRoom(const HumanRoom& h_room,
152 const std::string& current_map_name) { 144 const std::string& current_map_name) {
153 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
154 if (!id_mappings_.maps().contains(current_map_name) || 150 if (!id_mappings_.maps().contains(current_map_name) ||
155 !id_mappings_.maps() 151 !id_mappings_.maps()
156 .at(current_map_name) 152 .at(current_map_name)
@@ -162,24 +158,33 @@ class AssignIds {
162 .at(h_room.name()) 158 .at(h_room.name())
163 .panels() 159 .panels()
164 .contains(h_panel.name())) { 160 .contains(h_panel.name())) {
165 auto& maps = *id_mappings_.mutable_maps();
166 auto& rooms = *maps[current_map_name].mutable_rooms();
167 auto& panels = *rooms[h_room.name()].mutable_panels();
168 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());
169 } 169 }
170 } 170 }
171 171
172 for (const HumanLetter& h_letter : h_room.letters()) { 172 for (const HumanLetter& h_letter : h_room.letters()) {
173 std::string lettername = 173 std::string lettername = GetLetterName(h_letter.key(), h_letter.level2());
174 GetLetterName(h_letter.key(), h_letter.double_());
175 174
175 auto& letters = *output_.mutable_letters();
176 if (!id_mappings_.letters().contains(lettername)) { 176 if (!id_mappings_.letters().contains(lettername)) {
177 auto& letters = *id_mappings_.mutable_letters();
178 letters[lettername] = next_id_++; 177 letters[lettername] = next_id_++;
178 } else {
179 letters[lettername] = id_mappings_.letters().at(lettername);
179 } 180 }
180 } 181 }
181 182
182 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
183 if (!id_mappings_.maps().contains(current_map_name) || 188 if (!id_mappings_.maps().contains(current_map_name) ||
184 !id_mappings_.maps() 189 !id_mappings_.maps()
185 .at(current_map_name) 190 .at(current_map_name)
@@ -191,20 +196,164 @@ class AssignIds {
191 .at(h_room.name()) 196 .at(h_room.name())
192 .masteries() 197 .masteries()
193 .contains(h_mastery.name())) { 198 .contains(h_mastery.name())) {
194 auto& maps = *id_mappings_.mutable_maps();
195 auto& rooms = *maps[current_map_name].mutable_rooms();
196 auto& masteries = *rooms[h_room.name()].mutable_masteries();
197 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());
207 }
208 }
209
210 for (const HumanEnding& h_ending : h_room.endings()) {
211 auto& endings = *output_.mutable_endings();
212
213 if (!id_mappings_.endings().contains(h_ending.name())) {
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);
198 } 338 }
199 } 339 }
200 } 340 }
201 341
202 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
203 std::string mapdir_; 351 std::string mapdir_;
204 352
205 uint64_t next_id_ = 0; 353 uint64_t next_id_ = 1;
206 354
207 IdMappings id_mappings_; 355 IdMappings id_mappings_;
356 IdMappings output_;
208}; 357};
209 358
210} // namespace 359} // namespace