Files
Grinch-AP/worlds/smz3/TotalSMZ3/Text/Texts.py

117 lines
4.6 KiB
Python
Raw Normal View History

2023-04-08 16:52:34 -04:00
import io
from pathlib import Path
import sys
from typing import Any, List
import zipfile
from ..Region import Region
from ..Regions.Zelda.GanonsTower import GanonsTower
from ..Item import Item, ItemType
2022-08-15 10:48:13 -04:00
from Utils import unsafe_parse_yaml
2022-03-15 08:55:57 -04:00
import random
import os
2023-04-08 16:52:34 -04:00
text_folder = Path(__file__).parents[3]
2023-05-29 21:05:05 -04:00
def openFile(resource: str, mode: str = "r", encoding: str = None):
2023-04-08 16:52:34 -04:00
filename = sys.modules[__name__].__file__
apworldExt = ".apworld"
game = "smz3/"
if apworldExt in filename:
zip_path = Path(filename[:filename.index(apworldExt) + len(apworldExt)])
with zipfile.ZipFile(zip_path) as zf:
zipFilePath = resource[resource.index(game):]
if mode == 'rb':
return zf.open(zipFilePath, 'r')
else:
return io.TextIOWrapper(zf.open(zipFilePath, 'r'), encoding)
else:
2023-05-29 21:05:05 -04:00
return open(os.path.join(text_folder, resource), mode, encoding=encoding)
2022-03-15 08:55:57 -04:00
class Texts:
@staticmethod
def ParseYamlScripts(resource: str):
2023-04-08 16:52:34 -04:00
with openFile(resource, 'rb') as f:
2022-03-15 08:55:57 -04:00
yaml = str(f.read(), "utf-8")
2022-08-15 10:48:13 -04:00
return unsafe_parse_yaml(yaml)
2022-03-15 08:55:57 -04:00
@staticmethod
def ParseTextScript(resource: str):
2023-05-29 21:05:05 -04:00
with openFile(resource, 'r', encoding="utf-8-sig") as file:
2022-03-15 08:55:57 -04:00
return [text.rstrip('\n') for text in file.read().replace("\r", "").split("---\n") if text]
2023-04-08 16:52:34 -04:00
scripts: Any = ParseYamlScripts.__func__("smz3/TotalSMZ3/Text/Scripts/General.yaml")
blind: List[str] = ParseTextScript.__func__("smz3/TotalSMZ3/Text/Scripts/Blind.txt")
ganon: List[str] = ParseTextScript.__func__("smz3/TotalSMZ3/Text/Scripts/Ganon.txt")
tavernMan: List[str] = ParseTextScript.__func__("smz3/TotalSMZ3/Text/Scripts/TavernMan.txt")
triforceRoom: List[str] = ParseTextScript.__func__("smz3/TotalSMZ3/Text/Scripts/TriforceRoom.txt")
2022-03-15 08:55:57 -04:00
@staticmethod
def SahasrahlaReveal(dungeon: Region):
text = Texts.scripts["SahasrahlaReveal"]
return text.replace("<dungeon>", dungeon.Area)
@staticmethod
def BombShopReveal(dungeons: List[Region]):
text = Texts.scripts["BombShopReveal"]
return text.replace("<first>", dungeons[0].Area).replace("<second>", dungeons[1].Area)
@staticmethod
def GanonThirdPhaseSingle(silvers: Region):
node = Texts.scripts["GanonSilversReveal"]["single"]
text = node["local" if isinstance(silvers, GanonsTower) else "remote"]
return text.replace("<region>", silvers.Area)
@staticmethod
def GanonThirdPhaseMulti(silvers: Region, myWorld: int, silversWorld: int = None, silversPlayer: str = None):
node = Texts.scripts["GanonSilversReveal"]["multi"]
if silvers is None:
if (silversWorld == myWorld.Id):
return node["local"]
player = silversPlayer
else:
if (silvers.world == myWorld):
return node["local"]
player = silvers.world.Player
player = player.rjust(7 + len(player) // 2)
text = node["remote"]
return text.replace("<player>", player)
@staticmethod
def ItemTextbox(item: Item):
nameMap = {
ItemType.BottleWithGoldBee : ItemType.BottleWithBee.name,
ItemType.HeartContainerRefill : ItemType.HeartContainer.name,
ItemType.OneRupee : "PocketRupees",
ItemType.FiveRupees : "PocketRupees",
ItemType.TwentyRupees : "CouchRupees",
ItemType.TwentyRupees2 : "CouchRupees",
ItemType.FiftyRupees : "CouchRupees",
ItemType.BombUpgrade5 : "BombUpgrade",
ItemType.BombUpgrade10 : "BombUpgrade",
ItemType.ArrowUpgrade5 : "ArrowUpgrade",
ItemType.ArrowUpgrade10 : "ArrowUpgrade",
item.Type : item.Type.name,
}
if item.IsMap(): name = "Map"
elif item.IsCompass(): name = "Compass"
2022-08-15 10:48:13 -04:00
elif item.IsSmMap(): name = "SmMap"
2022-03-15 08:55:57 -04:00
else: name = nameMap[item.Type]
items = Texts.scripts["Items"]
return items.get(name, None) or items["default"]
@staticmethod
def Blind(rnd: random): return Texts.RandomLine(rnd, Texts.blind)
@staticmethod
def TavernMan(rnd: random): return Texts.RandomLine(rnd, Texts.tavernMan)
@staticmethod
def GanonFirstPhase(rnd: random): return Texts.RandomLine(rnd, Texts.ganon)
@staticmethod
def TriforceRoom(rnd: random): return Texts.RandomLine(rnd, Texts.triforceRoom)
@staticmethod
def RandomLine(rnd: random, lines: List[str]): return lines[rnd.randrange(0, len(lines))]