"""Generates src/platform/ui/ui_town_signs.h: the motto of each town / city sign of the game (data/maps//scripts.inc, _Text_TownSign / _Text_CitySign), shown under the zone name by the new UI's zone sign. Run from the repository root: python tools/ui2/gen_town_signs.py """ import os import re ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) OUT = os.path.join(ROOT, "src", "platform", "ui", "ui_town_signs.h") TOWNS = [("LittlerootTown", "MAPSEC_LITTLEROOT_TOWN"), ("OldaleTown", "MAPSEC_OLDALE_TOWN"), ("DewfordTown", "MAPSEC_DEWFORD_TOWN"), ("LavaridgeTown", "MAPSEC_LAVARIDGE_TOWN"), ("FallarborTown", "MAPSEC_FALLARBOR_TOWN"), ("VerdanturfTown", "MAPSEC_VERDANTURF_TOWN"), ("PacifidlogTown", "MAPSEC_PACIFIDLOG_TOWN"), ("PetalburgCity", "MAPSEC_PETALBURG_CITY"), ("SlateportCity", "MAPSEC_SLATEPORT_CITY"), ("MauvilleCity", "MAPSEC_MAUVILLE_CITY"), ("RustboroCity", "MAPSEC_RUSTBORO_CITY"), ("FortreeCity", "MAPSEC_FORTREE_CITY"), ("LilycoveCity", "MAPSEC_LILYCOVE_CITY"), ("MossdeepCity", "MAPSEC_MOSSDEEP_CITY"), ("SootopolisCity", "MAPSEC_SOOTOPOLIS_CITY"), ("EverGrandeCity", "MAPSEC_EVER_GRANDE_CITY")] def sign_text(town): src = open(os.path.join(ROOT, "data", "maps", town, "scripts.inc"), encoding="utf-8").read() m = re.search(rf"^{town}_Text_(?:Town|City)Sign:\s*\n((?:\s*\.string \".*\"\s*\n)+)", src, re.M) text = "".join(re.findall(r'\.string "(.*)"', m.group(1))) text = text.rstrip("$") parts = re.split(r"\\[npl]", text) motto = " ".join(p.strip() for p in parts[1:] if p.strip()) # the first line is the town name motto = motto.replace("“", '"').replace("”", '"').replace("’", "'").replace("…", "...") return motto.replace("\\", "\\\\").replace('"', '\\"') def main(): lines = ["// Generated by tools/ui2/gen_town_signs.py from the town / city signs of data/maps.", "// Do not edit.", "", "#ifndef UI_TOWN_SIGNS_H", "#define UI_TOWN_SIGNS_H", "", "#include \"constants/region_map_sections.h\"", "", "static const struct { uint16_t mapSec; const char *motto; } sTownSigns[] = {"] for town, sec in TOWNS: lines.append(f' {{ {sec}, "{sign_text(town)}" }},') lines += ["};", "", "#endif", ""] with open(OUT, "w", encoding="utf-8", newline="\n") as fh: fh.write("\n".join(lines)) print(OUT) if __name__ == "__main__": main()