summary refs log tree commit diff stats
path: root/tools/util/naming.cpp
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2025-08-30 12:00:03 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2025-08-30 12:00:03 -0400
commitd2bc5b2811171685e8bdc895122987b53defcf0a (patch)
treedabf111e8d4ba8caceee46189075968e223e3ee8 /tools/util/naming.cpp
parent3b77044a6a53d38a6960eb2a5855283a00b24d75 (diff)
downloadlingo2-archipelago-d2bc5b2811171685e8bdc895122987b53defcf0a.tar.gz
lingo2-archipelago-d2bc5b2811171685e8bdc895122987b53defcf0a.tar.bz2
lingo2-archipelago-d2bc5b2811171685e8bdc895122987b53defcf0a.zip
Changed how door location names are formatted
STANDARD type doors with at most four panels in the same map area and no
other trigger objects will have their location names generated from the
names of the panels used to open the door, similar to Lingo 1. Other
door types will use the door's name. In either case, the name can be
overridden using the new location_name field.

Rooms can also set a panel_display_name field, which will be used in
location names for doors, and is used to group panels into areas.

Panels themselves can set display names, which differentiates their
locations from other panels in the same area.

Many maps were updated for this, but note that the_symbolic and
the_unyielding have validator failures because of duplicate panel names.
This won't matter until panelsanity is implemented.
Diffstat (limited to 'tools/util/naming.cpp')
0 files changed, 0 insertions, 0 deletions
>138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#include <google/protobuf/message.h>
#include <google/protobuf/text_format.h>

#include <cstdint>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>

#include "proto/human.pb.h"
#include "util/ids_yaml_format.h"
#include "util/naming.h"

namespace com::fourisland::lingo2_archipelago {
namespace {

template <typename T>
T ReadMessageFromFile(const std::string& path) {
  std::cout << "Processing " << path << std::endl;

  std::ifstream file(path);
  std::stringstream buffer;
  buffer << file.rdbuf();

  T message;
  google::protobuf::TextFormat::ParseFromString(buffer.str(), &message);

  return message;
}

class AssignIds {
 public:
  AssignIds(const std::string& mapdir) : mapdir_(mapdir) {}

  void Run() {
    std::filesystem::path datadir_path = mapdir_;
    std::filesystem::path ids_path = datadir_path / "ids.yaml";

    ReadIds(ids_path);

    ProcessMaps(datadir_path);

    WriteIds(ids_path);

    std::cout << "Next ID: " << next_id_ << std::endl;
  }

  void ReadIds(std::filesystem::path path) {
    if (!std::filesystem::exists(path)) {
      return;
    }

    id_mappings_ = ReadIdsFromYaml(path.string());

    for (const auto& [_, map] : id_mappings_.maps()) {
      for (const auto& [_, id] : map.doors()) {
        if (id > next_id_) {
          next_id_ = id;
        }
      }

      for (const auto& [_, room] : map.rooms()) {
        for (const auto& [_, id] : room.panels()) {
          if (id > next_id_) {
            next_id_ = id;
          }
        }

        for (const auto& [_, id] : room.masteries()) {
          if (id > next_id_) {
            next_id_ = id;
          }
        }
      }
    }

    for (const auto& [_, id] : id_mappings_.special()) {
      if (id > next_id_) {
        next_id_ = id;
      }
    }

    for (const auto& [_, id] : id_mappings_.letters()) {
      if (id > next_id_) {
        next_id_ = id;
      }
    }

    for (const auto& [_, id] : id_mappings_.endings()) {
      if (id > next_id_) {
        next_id_ = id;
      }
    }

    next_id_++;
  }

  void WriteIds(std::filesystem::path path) {
    WriteIdsAsYaml(id_mappings_, path.string());
  }

  void ProcessMaps(std::filesystem::path path) {
    std::filesystem::path maps_dir = path / "maps";
    for (auto const& dir_entry :
         std::filesystem::directory_iterator(maps_dir)) {
      ProcessMap(dir_entry.path());
    }
  }

  void ProcessMap(std::filesystem::path path) {
    std::string map_name = path.filename().string();

    ProcessDoorsFile(path / "doors.txtpb", map_name);
    ProcessRooms(path / "rooms", map_name);
  }

  void ProcessDoorsFile(std::filesystem::path path,
                        const std::string& current_map_name) {
    if (!std::filesystem::exists(path)) {
      return;
    }

    auto doors = ReadMessageFromFile<HumanDoors>(path.string());

    for (const HumanDoor& door : doors.doors()) {
      ProcessDoor(door, current_map_name);
    }
  }

  void ProcessDoor(const HumanDoor& h_door,
                   const std::string& current_map_name) {
    if (h_door.type() == DoorType::EVENT) {
      return;
    }

    if (!id_mappings_.maps().contains(current_map_name) ||
        !id_mappings_.maps()
             .at(current_map_name)
             .doors()
             .contains(h_door.name())) {
      auto& maps = *id_mappings_.mutable_maps();
      auto& doors = *maps[current_map_name].mutable_doors();
      doors[h_door.name()] = next_id_++;
    }
  }

  void ProcessRooms(std::filesystem::path path,
                    const std::string& current_map_name) {
    for (auto const& dir_entry : std::filesystem::directory_iterator(path)) {
      auto room = ReadMessageFromFile<HumanRoom>(dir_entry.path().string());
      ProcessRoom(room, current_map_name);
    }
  }

  void ProcessRoom(const HumanRoom& h_room,
                   const std::string& current_map_name) {
    for (const HumanPanel& h_panel : h_room.panels()) {
      if (!id_mappings_.maps().contains(current_map_name) ||
          !id_mappings_.maps()
               .at(current_map_name)
               .rooms()
               .contains(h_room.name()) ||
          !id_mappings_.maps()
               .at(current_map_name)
               .rooms()
               .at(h_room.name())
               .panels()
               .contains(h_panel.name())) {
        auto& maps = *id_mappings_.mutable_maps();
        auto& rooms = *maps[current_map_name].mutable_rooms();
        auto& panels = *rooms[h_room.name()].mutable_panels();
        panels[h_panel.name()] = next_id_++;
      }
    }

    for (const HumanLetter& h_letter : h_room.letters()) {
      std::string lettername = GetLetterName(h_letter.key(), h_letter.level2());

      if (!id_mappings_.letters().contains(lettername)) {
        auto& letters = *id_mappings_.mutable_letters();
        letters[lettername] = next_id_++;
      }
    }

    for (const HumanMastery& h_mastery : h_room.masteries()) {
      if (!id_mappings_.maps().contains(current_map_name) ||
          !id_mappings_.maps()
               .at(current_map_name)
               .rooms()
               .contains(h_room.name()) ||
          !id_mappings_.maps()
               .at(current_map_name)
               .rooms()
               .at(h_room.name())
               .masteries()
               .contains(h_mastery.name())) {
        auto& maps = *id_mappings_.mutable_maps();
        auto& rooms = *maps[current_map_name].mutable_rooms();
        auto& masteries = *rooms[h_room.name()].mutable_masteries();
        masteries[h_mastery.name()] = next_id_++;
      }
    }

    for (const HumanEnding& h_ending : h_room.endings()) {
      if (!id_mappings_.endings().contains(h_ending.name())) {
        auto& endings = *id_mappings_.mutable_endings();
        endings[h_ending.name()] = next_id_++;
      }
    }
  }

 private:
  std::string mapdir_;

  uint64_t next_id_ = 0;

  IdMappings id_mappings_;
};

}  // namespace
}  // namespace com::fourisland::lingo2_archipelago

int main(int argc, char** argv) {
  if (argc != 2) {
    std::cout << "Incorrect argument count." << std::endl;
    std::cout << "Usage: assign_ids [path to map directory]" << std::endl;
    return 1;
  }

  std::string mapdir = argv[1];

  com::fourisland::lingo2_archipelago::AssignIds assign_ids(mapdir);
  assign_ids.Run();

  return 0;
}