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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
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')
);
}
|