summaryrefslogtreecommitdiff
path: root/web/src/lib/game/line/frosthavenConverter.ts
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/lib/game/line/frosthavenConverter.ts')
-rw-r--r--web/src/lib/game/line/frosthavenConverter.ts199
1 files changed, 199 insertions, 0 deletions
diff --git a/web/src/lib/game/line/frosthavenConverter.ts b/web/src/lib/game/line/frosthavenConverter.ts
new file mode 100644
index 0000000..534efd9
--- /dev/null
+++ b/web/src/lib/game/line/frosthavenConverter.ts
@@ -0,0 +1,199 @@
+/**
+ * Frosthaven-style line conversion (client port of the reference
+ * `FrosthavenConverter`, PORTING.md §8b). Pure string → string: expands
+ * embedded newlines, rewrites keywords into token/line markers, and tracks the
+ * sub-line / conditional / element-use state so the line parser can lay lines
+ * out as the reference does (sub-lines boxed, element-use blocks, …).
+ */
+
+const LOOKBACK = 2;
+
+/** Split lines containing `\n`, propagating the `^` / `^^` prefix. */
+export function expandEmbeddedNewlines(lines: string[]): string[] {
+ const result: string[] = [];
+ for (const line of lines) {
+ if (!line.includes('\n')) {
+ result.push(line);
+ continue;
+ }
+ const parts = line.split('\n');
+ result.push(parts[0]);
+ const outerPrefix = parts[0].startsWith('^^')
+ ? '^^'
+ : parts[0].startsWith('^')
+ ? '^'
+ : '';
+ for (let i = 1; i < parts.length; i++) result.push(outerPrefix + parts[i]);
+ }
+ return result;
+}
+
+/** The keyword → token/line-marker rewrite + sub-line state machine. */
+export function convertLinesToFH(lines: string[], applyStats: boolean): string[] {
+ const retVal: string[] = [];
+ let isSubLine = false;
+ let isReallySubLine = false;
+ let isConditional = false;
+ let isElementUse = false;
+
+ for (let i = 0; i < lines.length; i++) {
+ let startOfConditional = false;
+ let line = lines[i];
+
+ if (line === '[newLine]') line = '';
+
+ if (
+ (line === '[r]' || line === '[s]') &&
+ lines[i + 1]?.includes('%use')
+ ) {
+ isElementUse = true;
+ isConditional = true;
+ startOfConditional = true;
+ }
+ if ((line === '[/r]' || line === '[/s]') && isConditional) {
+ isConditional = false;
+ isElementUse = false;
+ }
+
+ if (i > 0 && lines[i - 1].includes('%use')) startOfConditional = true;
+ if (
+ i > 1 &&
+ lines[i - LOOKBACK].includes('%use') &&
+ (lines[i - 1] === '[r]' || lines[i - 1] === '[s]' || lines[i - 1] === '[c]')
+ ) {
+ startOfConditional = true;
+ }
+
+ line = line.replaceAll('Affect', 'Target');
+ line = line.replaceAll('damage', '%damage%');
+ line = line.replaceAll('%damage%d', 'damaged');
+ if (!applyStats) {
+ line = line.replaceAll(' - ', '-');
+ line = line.replaceAll(' + ', '+');
+ }
+ line = line.replaceAll('% ', '%');
+
+ if (line.startsWith('*')) {
+ if (isReallySubLine) retVal.push('[subLineEnd]');
+ isReallySubLine = false;
+ isSubLine = false;
+ isConditional = false;
+ isElementUse = false;
+ }
+
+ const isSubLineCandidate =
+ line.startsWith('^') &&
+ (line[1] === '%' ||
+ line.startsWith('^Self') ||
+ line.startsWith('^-') ||
+ line.startsWith('^+') ||
+ line.startsWith('^Advantage') ||
+ line.startsWith('^Target') ||
+ line.startsWith('^%target%') ||
+ line.startsWith('^Normal') ||
+ line.startsWith('^all') ||
+ (line.startsWith('^All') &&
+ !line.startsWith('^All attacks') &&
+ !line.startsWith('^All targets')));
+
+ if (line.startsWith('^') && isSubLine && !startOfConditional) {
+ if (isSubLineCandidate) {
+ if (
+ !isReallySubLine &&
+ (!isConditional ||
+ (isElementUse &&
+ !line.startsWith('^Perform') &&
+ !line.startsWith('^^for each')))
+ ) {
+ retVal.push('[subLineStart]');
+ isReallySubLine = true;
+ }
+
+ if (
+ isElementUse &&
+ (lines[i - 1]?.includes('use') ||
+ (lines[i - LOOKBACK]?.includes('use') &&
+ lines[i - 1]?.includes('[c]'))) &&
+ !line.startsWith('^Target') &&
+ !line.startsWith('^all') &&
+ !line.startsWith('^All') &&
+ !line.includes('instead')
+ ) {
+ line = line.substring(1);
+ if (retVal[retVal.length - 1] === '[subLineStart]') {
+ retVal.pop();
+ }
+ isReallySubLine = false;
+ }
+ line = '!' + line;
+ line = line.replace(/Self/, 'self').replace(/All/, 'all');
+
+ if (retVal[retVal.length - 1] === '[subLineStart]') {
+ retVal[retVal.length - 1] = '![subLineStart]';
+ }
+ } else {
+ isSubLine = false;
+ }
+ }
+
+ if (line.startsWith('^') && isReallySubLine) {
+ if (retVal[retVal.length - 1] !== '[subLineStart]') {
+ retVal.push('!^[lineBreak]');
+ }
+ line = '!' + line;
+ } else if (
+ !line.startsWith('!') &&
+ !line.startsWith('*') &&
+ !line.startsWith('^')
+ ) {
+ if (!isSubLine && !line.includes('%use%')) {
+ isSubLine = true;
+ } else if (isReallySubLine) {
+ retVal.push('[subLineEnd]');
+ isReallySubLine = false;
+ isSubLine = false;
+ }
+ }
+
+ // Use iconography instead of words (applied to every line, last).
+ line = line.replaceAll('Target', '%target%');
+ retVal.push(line);
+ }
+
+ if (isReallySubLine && (!isConditional || isElementUse)) {
+ retVal.push('[subLineEnd]');
+ }
+ return retVal;
+}
+
+/** Whether the token's graphic should overflow its line (FH condition icons). */
+export function shouldOverflow(frosthavenStyle: boolean, iconToken: string): boolean {
+ if (!frosthavenStyle) return false;
+ const exact = [
+ 'pierce',
+ 'brittle',
+ 'curse',
+ 'enfeeble',
+ 'bless',
+ 'invisible',
+ 'strengthen',
+ 'bane',
+ 'push',
+ 'pull',
+ 'poison',
+ 'wound',
+ 'infect',
+ 'chill',
+ 'disarm',
+ 'immobilize',
+ 'stun',
+ 'impair',
+ 'safeguard',
+ 'muddle'
+ ];
+ return (
+ exact.includes(iconToken) ||
+ iconToken.includes('poison') ||
+ iconToken.includes('wound')
+ );
+}