about summary refs log tree commit diff stats
path: root/tools/datapacker/container.cpp
blob: 4a656b337b0e1f8084d460bb55d233eb33470d92 (plain) (blame)
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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include "identifiers.h"

#include <string>

#include "proto/human.pb.h"

namespace com::fourisland::lingo2_archipelago {

std::optional<RoomIdentifier> GetCompleteRoomIdentifier(
    RoomIdentifier identifier, std::optional<std::string> map_name) {
  if (!identifier.has_map()) {
    if (!map_name) {
      return std::nullopt;
    }
    identifier.set_map(*map_name);
  }
  return identifier;
}

std::optional<DoorIdentifier> GetCompleteDoorIdentifier(
    DoorIdentifier identifier, std::optional<std::string> map_name) {
  if (!identifier.has_map()) {
    if (!map_name) {
      pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include "container.h"

#include <iostream>

#include "util/naming.h"

namespace com::fourisland::lingo2_archipelago {

uint64_t Container::FindOrAddMap(std::string map_name) {
  auto it = map_id_by_name_.find(map_name);

  if (it == map_id_by_name_.end()) {
    uint64_t new_id = all_objects_.maps_size();
    Map* map = all_objects_.add_maps();
    map->set_id(new_id);
    map->set_name(map_name);

    map_id_by_name_[map_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddRoom(std::optional<std::string> map_name,
                                  std::string room_name,
                                  std::optional<std::string> map_fallback) {
  if (!map_name) {
    if (!map_fallback) {
      std::cout << "No map name provided for " << room_name << std::endl;
      map_name = "global";
    } else {
      map_name = map_fallback;
    }
  }

  auto& map_container = room_id_by_map_room_names_[*map_name];
  auto it = map_container.find(room_name);
  if (it == map_container.end()) {
    uint64_t new_id = all_objects_.rooms_size();
    Room* room = all_objects_.add_rooms();
    room->set_id(new_id);
    room->set_map_id(FindOrAddMap(*map_name));
    room->set_name(room_name);

    map_container[room_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddPainting(
    std::optional<std::string> map_name, std::optional<std::string> room_name,
    std::string painting_name, std::optional<std::string> map_fallback,
    std::optional<std::string> room_fallback) {
  if (!map_name) {
    if (!map_fallback) {
      std::cout << "No map name provided for " << painting_name << std::endl;
      map_name = "global";
    } else {
      map_name = map_fallback;
    }
  }

  if (!room_name) {
    if (!room_fallback) {
      std::cout << "No room name provided for " << painting_name << std::endl;
      room_name = "global";
    } else {
      room_name = room_fallback;
    }
  }

  auto& room_container =
      painting_id_by_map_room_painting_names_[*map_name][*room_name];
  auto it = room_container.find(painting_name);
  if (it == room_container.end()) {
    uint64_t new_id = all_objects_.paintings_size();
    PaintingData* painting = all_objects_.add_paintings();
    painting->set_id(new_id);
    painting->set_room_id(FindOrAddRoom(map_name, *room_name, std::nullopt));
    painting->set_name(painting_name);

    room_container[painting_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddPort(std::optional<std::string> map_name,
                                  std::optional<std::string> room_name,
                                  std::string port_name,
                                  std::optional<std::string> map_fallback,
                                  std::optional<std::string> room_fallback) {
  if (!map_name) {
    if (!map_fallback) {
      std::cout << "No map name provided for " << port_name << std::endl;
      map_name = "global";
    } else {
      map_name = map_fallback;
    }
  }

  if (!room_name) {
    if (!room_fallback) {
      std::cout << "No room name provided for " << port_name << std::endl;
      room_name = "global";
    } else {
      room_name = room_fallback;
    }
  }

  auto& room_container = port_id_by_map_room_port_names_[*map_name][*room_name];
  auto it = room_container.find(port_name);
  if (it == room_container.end()) {
    uint64_t new_id = all_objects_.ports_size();
    Port* port = all_objects_.add_ports();
    port->set_id(new_id);
    port->set_room_id(FindOrAddRoom(map_name, *room_name, std::nullopt));
    port->set_name(port_name);

    room_container[port_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddPanel(std::optional<std::string> map_name,
                                   std::optional<std::string> room_name,
                                   std::string panel_name,
                                   std::optional<std::string> map_fallback,
                                   std::optional<std::string> room_fallback) {
  if (!map_name) {
    if (!map_fallback) {
      std::cout << "No map name provided for " << panel_name << std::endl;
      map_name = "global";
    } else {
      map_name = map_fallback;
    }
  }

  if (!room_name) {
    if (!room_fallback) {
      std::cout << "No room name provided for " << panel_name << std::endl;
      room_name = "global";
    } else {
      room_name = room_fallback;
    }
  }

  auto& room_container =
      panel_id_by_map_room_panel_names_[*map_name][*room_name];
  auto it = room_container.find(panel_name);
  if (it == room_container.end()) {
    uint64_t new_id = all_objects_.panels_size();
    PanelData* panel = all_objects_.add_panels();
    panel->set_id(new_id);
    panel->set_room_id(FindOrAddRoom(map_name, *room_name, std::nullopt));
    panel->set_name(panel_name);

    room_container[panel_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddLetter(std::string key, bool level2) {
  std::string letter_name = GetLetterName(key, level2);

  auto it = letter_id_by_name_.find(letter_name);
  if (it == letter_id_by_name_.end()) {
    uint64_t new_id = all_objects_.letters_size();
    Letter* letter = all_objects_.add_letters();
    letter->set_id(new_id);
    letter->set_key(key);

    if (level2) {
      letter->set_level2(level2);
    }

    letter_id_by_name_[letter_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindLetterByName(std::string letter_name) {
  auto it = letter_id_by_name_.find(letter_name);
  if (it == letter_id_by_name_.end()) {
    std::cout << "Could not find letter by name: " << letter_name << std::endl;
    exit(2);
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddEnding(std::string ending_name) {
  auto it = ending_id_by_name_.find(ending_name);
  if (it == ending_id_by_name_.end()) {
    uint64_t new_id = all_objects_.endings_size();
    Ending* ending = all_objects_.add_endings();
    ending->set_id(new_id);
    ending->set_name(ending_name);

    ending_id_by_name_[ending_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddMastery(std::optional<std::string> map_name,
                                     std::optional<std::string> room_name,
                                     std::string mastery_name,
                                     std::optional<std::string> map_fallback,
                                     std::optional<std::string> room_fallback) {
  if (!map_name) {
    if (!map_fallback) {
      std::cout << "No map name provided for " << mastery_name << std::endl;
      map_name = "global";
    } else {
      map_name = map_fallback;
    }
  }

  if (!room_name) {
    if (!room_fallback) {
      std::cout << "No room name provided for " << mastery_name << std::endl;
      room_name = "global";
    } else {
      room_name = room_fallback;
    }
  }

  auto& room_container =
      mastery_id_by_map_room_mastery_names_[*map_name][*room_name];
  auto it = room_container.find(mastery_name);
  if (it == room_container.end()) {
    uint64_t new_id = all_objects_.masteries_size();
    Mastery* mastery = all_objects_.add_masteries();
    mastery->set_id(new_id);
    mastery->set_room_id(FindOrAddRoom(map_name, *room_name, std::nullopt));
    mastery->set_name(mastery_name);

    room_container[mastery_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddKeyholder(
    std::optional<std::string> map_name, std::optional<std::string> room_name,
    std::string keyholder_name, std::optional<std::string> map_fallback,
    std::optional<std::string> room_fallback) {
  if (!map_name) {
    if (!map_fallback) {
      std::cout << "No map name provided for " << keyholder_name << std::endl;
      map_name = "global";
    } else {
      map_name = map_fallback;
    }
  }

  if (!room_name) {
    if (!room_fallback) {
      std::cout << "No room name provided for " << keyholder_name << std::endl;
      room_name = "global";
    } else {
      room_name = room_fallback;
    }
  }

  auto& room_container =
      keyholder_id_by_map_room_keyholder_names_[*map_name][*room_name];
  auto it = room_container.find(keyholder_name);
  if (it == room_container.end()) {
    uint64_t new_id = all_objects_.keyholders_size();
    KeyholderData* keyholder = all_objects_.add_keyholders();
    keyholder->set_id(new_id);
    keyholder->set_room_id(FindOrAddRoom(map_name, *room_name, std::nullopt));
    keyholder->set_name(keyholder_name);

    room_container[keyholder_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddDoor(std::optional<std::string> map_name,
                                  std::string door_name,
                                  std::optional<std::string> map_fallback) {
  if (!map_name) {
    if (!map_fallback) {
      std::cout << "No map name provided for " << door_name << std::endl;
      map_name = "global";
    } else {
      map_name = map_fallback;
    }
  }

  auto& map_container = door_id_by_map_door_names_[*map_name];
  auto it = map_container.find(door_name);
  if (it == map_container.end()) {
    uint64_t new_id = all_objects_.doors_size();
    Door* door = all_objects_.add_doors();
    door->set_id(new_id);
    door->set_map_id(FindOrAddMap(*map_name));
    door->set_name(door_name);

    map_container[door_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddProgressive(std::string prog_name) {
  auto it = progressive_id_by_name_.find(prog_name);

  if (it == progressive_id_by_name_.end()) {
    uint64_t new_id = all_objects_.progressives_size();
    Progressive* progressive = all_objects_.add_progressives();
    progressive->set_id(new_id);
    progressive->set_name(prog_name);

    progressive_id_by_name_[prog_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

uint64_t Container::FindOrAddDoorGroup(std::string group_name) {
  auto it = door_group_id_by_name_.find(group_name);

  if (it == door_group_id_by_name_.end()) {
    uint64_t new_id = all_objects_.door_groups_size();
    DoorGroup* door_group = all_objects_.add_door_groups();
    door_group->set_id(new_id);
    door_group->set_name(group_name);

    door_group_id_by_name_[group_name] = new_id;

    return new_id;
  } else {
    return it->second;
  }
}

void Container::AddConnection(const Connection& connection) {
  *all_objects_.add_connections() = connection;
}

}  // namespace com::fourisland::lingo2_archipelago