49 private static final Map<Integer, DialogueScript> scripts =
new HashMap<>();
51 private static final Pattern DIALOGUE_PATTERN = Pattern.compile(
"@dialogue\\s+npc:(.+)");
52 private static final Pattern NPC_PATTERN = Pattern.compile(
"npc\\(\"(.+?)\"(?:,\\s*\"(.+?)\")?\\)");
53 private static final Pattern PLAYER_PATTERN = Pattern.compile(
"player\\(\"(.+?)\"(?:,\\s*\"(.+?)\")?\\)");
54 private static final Pattern MESSAGE_PATTERN = Pattern.compile(
"message\\(\"(.+?)\"\\)");
55 private static final Pattern OPTION_PATTERN = Pattern.compile(
"option\\((.+)\\)");
57 public static void loadAll() {
61 Path scriptDir = Paths.get(
"./data/dialogue/scripts");
62 if (!Files.exists(scriptDir)) {
63 Files.createDirectories(scriptDir);
64 logger.info(
"Created dialogue scripts directory");
68 try (Stream<Path> paths = Files.walk(scriptDir)) {
69 paths.filter(Files::isRegularFile)
70 .filter(path -> path.toString().endsWith(
".asc"))
71 .forEach(DialogueScriptParser::loadScript);
74 logger.info(
"Loaded {} dialogue scripts",
MessageColor.DARK_GREEN, scripts.size());
75 }
catch (IOException e) {
76 logger.error(
"Error loading dialogue scripts", e);
80 private static void loadScript(Path path) {
81 try (BufferedReader reader =
new BufferedReader(
new FileReader(path.toFile()))) {
84 List<String> currentMessages =
new ArrayList<>();
86 while ((line = reader.readLine()) !=
null) {
89 if (line.isEmpty() || line.startsWith(
"#") || line.startsWith(
"//")) {
93 Matcher dialogueMatcher = DIALOGUE_PATTERN.matcher(line);
94 if (dialogueMatcher.matches()) {
95 if (currentScript !=
null) {
96 registerScript(currentScript);
99 String npcIdString = dialogueMatcher.group(1).trim();
100 int[] npcIds = parseNpcIds(npcIdString);
105 if (currentScript ==
null) {
109 Matcher npcMatcher = NPC_PATTERN.matcher(line);
110 if (npcMatcher.matches()) {
111 String message1 = npcMatcher.group(1);
112 String message2 = npcMatcher.group(2);
114 String[] messages = message2 !=
null ?
115 new String[] { message1, message2 } :
116 new String[] { message1 };
122 Matcher playerMatcher = PLAYER_PATTERN.matcher(line);
123 if (playerMatcher.matches()) {
124 String message1 = playerMatcher.group(1);
125 String message2 = playerMatcher.group(2);
127 String[] messages = message2 !=
null ?
128 new String[] { message1, message2 } :
129 new String[] { message1 };
135 Matcher messageMatcher = MESSAGE_PATTERN.matcher(line);
136 if (messageMatcher.matches()) {
137 String message = messageMatcher.group(1);
142 Matcher optionMatcher = OPTION_PATTERN.matcher(line);
143 if (optionMatcher.matches()) {
144 String optionsString = optionMatcher.group(1);
145 String[] options = parseOptions(optionsString);
152 if (currentScript !=
null) {
153 registerScript(currentScript);
156 }
catch (IOException e) {
157 logger.error(
"Error loading script: " + path, e);
161 private static int[] parseNpcIds(String npcIdString) {
162 String[] parts = npcIdString.split(
",");
163 int[] ids =
new int[parts.length];
165 for (
int i = 0; i < parts.length; i++) {
167 ids[i] = Integer.parseInt(parts[i].trim());
168 }
catch (NumberFormatException e) {
169 logger.error(
"Invalid NPC ID: " + parts[i]);
176 private static String[] parseOptions(String optionsString) {
177 List<String> options =
new ArrayList<>();
178 Pattern quotedPattern = Pattern.compile(
"\"([^\"]+)\"");
179 Matcher matcher = quotedPattern.matcher(optionsString);
181 while (matcher.find()) {
182 options.add(matcher.group(1));
185 return options.toArray(
new String[0]);
189 for (
int npcId : script.getNpcIds()) {
190 scripts.put(npcId, script);
194 public static Optional<DialogueScript> getScript(
int npcId) {
195 return Optional.ofNullable(scripts.get(npcId));
198 public static Map<Integer, DialogueScript> getScripts() {
199 return Collections.unmodifiableMap(scripts);