-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattle-challenge-runtime.js
More file actions
388 lines (362 loc) · 11.3 KB
/
battle-challenge-runtime.js
File metadata and controls
388 lines (362 loc) · 11.3 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Owns: battle mini-drill state, cursor movement, prompt flow, and challenge
// completion/failure handling. Does not own: damage math, encounter setup, or
// broader battle turn sequencing.
import { createBattleChallenge } from "./battle-challenges.js";
function parseBattleMotion(key) {
const match = String(key).match(/^([1-9][0-9]*)([hjklwbe])$/);
if (!match) {
return { key, count: 1 };
}
return {
key: match[2],
count: Number(match[1]),
};
}
function reverseFindDirection(direction) {
if (direction === "f") {
return "F";
}
if (direction === "t") {
return "T";
}
if (direction === "F") {
return "f";
}
if (direction === "T") {
return "t";
}
return direction;
}
export function createBattleChallengeRuntime(deps) {
const {
state,
activeMonster,
setMessage,
onAttack,
onSlam,
onThrow,
} = deps;
function resolveBattleRepeatFindKey(challenge, key) {
if (!challenge || !challenge.lastFind) {
return null;
}
const direction = key === "," ? reverseFindDirection(challenge.lastFind.direction) : challenge.lastFind.direction;
return `${direction}${challenge.lastFind.targetChar}`;
}
function currentBattleChallengeStep() {
if (!state.battle || !state.battle.challenge) {
return null;
}
return state.battle.challenge.steps[state.battle.challenge.stepIndex] || null;
}
function setBattleChallengeFeedback(message, tone) {
if (!state.battle || !state.battle.challenge) {
return;
}
state.battle.challenge.feedback = message;
state.battle.challenge.feedbackTone = tone || "hint";
}
function challengeLineLength(challenge, row) {
return Math.max(1, (challenge.lines[row] || "").length);
}
function setChallengeCursor(challenge, row, col) {
const nextRow = Math.max(0, Math.min(row, challenge.lines.length - 1));
const maxCol = challengeLineLength(challenge, nextRow) - 1;
challenge.cursor.row = nextRow;
challenge.cursor.col = Math.max(0, Math.min(col, maxCol));
if (!challenge.cursorVisual) {
challenge.cursorVisual = {
row: challenge.cursor.row,
col: challenge.cursor.col,
};
}
}
function challengeWordTokens(challenge) {
const tokens = [];
challenge.lines.forEach((line, row) => {
[...String(line).matchAll(/[A-Za-z_]+/g)].forEach((match) => {
tokens.push({
row,
col: match.index,
end: match.index + match[0].length - 1,
text: match[0],
});
});
});
return tokens;
}
function challengeMoveWordForward(challenge, count) {
const tokens = challengeWordTokens(challenge);
let row = challenge.cursor.row;
let col = challenge.cursor.col;
for (let step = 0; step < count; step += 1) {
const next = tokens.find((token) => token.row > row || (token.row === row && token.col > col));
if (!next) {
return false;
}
row = next.row;
col = next.col;
}
setChallengeCursor(challenge, row, col);
return true;
}
function challengeMoveWordBackward(challenge, count) {
const tokens = challengeWordTokens(challenge);
let row = challenge.cursor.row;
let col = challenge.cursor.col;
for (let step = 0; step < count; step += 1) {
let previous = null;
tokens.forEach((token) => {
if (token.row < row || (token.row === row && token.col < col)) {
previous = token;
}
});
if (!previous) {
return false;
}
row = previous.row;
col = previous.col;
}
setChallengeCursor(challenge, row, col);
return true;
}
function challengeMoveWordEndForward(challenge, count) {
const tokens = challengeWordTokens(challenge);
let row = challenge.cursor.row;
let col = challenge.cursor.col;
for (let step = 0; step < count; step += 1) {
const next = tokens.find((token) => token.row > row || (token.row === row && token.end >= col));
if (!next) {
return false;
}
row = next.row;
col = next.end;
}
setChallengeCursor(challenge, row, col);
return true;
}
function applyBattleChallengeMotion(challenge, key) {
const parsed = parseBattleMotion(key);
const actionKey = parsed.key;
const count = parsed.count;
const row = challenge.cursor.row;
const col = challenge.cursor.col;
if (actionKey === "h") {
setChallengeCursor(challenge, row, col - count);
return true;
}
if (actionKey === "l") {
setChallengeCursor(challenge, row, col + count);
return true;
}
if (actionKey === "j") {
setChallengeCursor(challenge, row + count, col);
return true;
}
if (actionKey === "k") {
setChallengeCursor(challenge, row - count, col);
return true;
}
if (actionKey === "0") {
setChallengeCursor(challenge, row, 0);
return true;
}
if (actionKey === "$") {
setChallengeCursor(challenge, row, challengeLineLength(challenge, row) - 1);
return true;
}
if (actionKey === "gg") {
setChallengeCursor(challenge, 0, 0);
return true;
}
if (actionKey === "G") {
setChallengeCursor(challenge, challenge.lines.length - 1, 0);
return true;
}
if (actionKey === "w") {
return challengeMoveWordForward(challenge, count);
}
if (actionKey === "b") {
return challengeMoveWordBackward(challenge, count);
}
if (actionKey === "e") {
return challengeMoveWordEndForward(challenge, count);
}
if (key === ";" || key === ",") {
const repeated = resolveBattleRepeatFindKey(challenge, key);
if (!repeated) {
return false;
}
key = repeated;
}
if (/^[ftFT].$/.test(key)) {
const direction = key[0];
const targetChar = key[1];
const line = challenge.lines[row] || "";
challenge.lastFind = {
direction,
targetChar,
};
if (direction === "f") {
const next = line.indexOf(targetChar, col + 1);
if (next < 0) {
return false;
}
setChallengeCursor(challenge, row, next);
return true;
}
if (direction === "t") {
const next = line.indexOf(targetChar, col + 1);
if (next < 1) {
return false;
}
setChallengeCursor(challenge, row, next - 1);
return true;
}
if (direction === "F") {
const next = line.lastIndexOf(targetChar, col - 1);
if (next < 0) {
return false;
}
setChallengeCursor(challenge, row, next);
return true;
}
if (direction === "T") {
const next = line.lastIndexOf(targetChar, col - 1);
if (next < 0 || next + 1 > col) {
return false;
}
setChallengeCursor(challenge, row, next + 1);
return true;
}
}
return false;
}
function completeBattleChallenge() {
if (!state.battle || !state.battle.challenge) {
return;
}
const challenge = state.battle.challenge;
challenge.stepIndex += 1;
challenge.pendingPrefix = "";
challenge.countBuffer = "";
const step = currentBattleChallengeStep();
if (step) {
setBattleChallengeFeedback(step.type === "action"
? `Now use ${step.expect} to finish the technique.`
: `Locked in. ${challenge.instruction}`, "success");
return;
}
state.battle.challenge = null;
if (challenge.actionId === "attack") {
onAttack();
return;
}
if (challenge.actionId === "slam") {
onSlam();
return;
}
onThrow();
}
function failBattleChallenge(message) {
if (!state.battle || !state.battle.challenge) {
return;
}
const actionId = state.battle.challenge.actionId;
state.battle.challenge = null;
state.score = Math.max(0, state.score - 8);
setMessage(`${message} Turn lost. -8 score.`, state.battle.enemy.id);
state.battle.pendingEnemyTurn = {
resolveAt: performance.now() + 420,
prefix:
actionId === "throw"
? `${state.battle.enemy.name} punishes the failed throw setup.`
: `${state.battle.enemy.name} punishes the missed technique.`,
};
}
function startBattleChallenge(actionId) {
if (!state.battle) {
return;
}
state.battle.challenge = createBattleChallenge(actionId);
setMessage(
actionId === "attack"
? "Battle drill: move the cursor, then use a to release the strike."
: actionId === "slam"
? "Battle drill: line up the row, then use dd to drop the slam."
: "Battle drill: line up the marked character with repeated find motions to throw the VimOrb.",
activeMonster().id
);
}
function handleBattleChallengeKey(key) {
const challenge = state.battle && state.battle.challenge;
const step = currentBattleChallengeStep();
if (!challenge || !step) {
return false;
}
if (/^[1-9]$/.test(key)) {
challenge.countBuffer += key;
setBattleChallengeFeedback(`Count prefix: ${challenge.countBuffer}`, "accent");
return true;
}
if (challenge.countBuffer && key === "0") {
challenge.countBuffer += "0";
setBattleChallengeFeedback(`Count prefix: ${challenge.countBuffer}`, "accent");
return true;
}
if (challenge.pendingPrefix === "g") {
challenge.pendingPrefix = "";
if (key === "g") {
key = "gg";
}
} else if (["f", "t", "F", "T"].includes(challenge.pendingPrefix)) {
key = `${challenge.pendingPrefix}${key}`;
challenge.pendingPrefix = "";
} else if (challenge.pendingPrefix === "d") {
challenge.pendingPrefix = "";
if (key === "d") {
key = "dd";
}
} else if (key === "g") {
challenge.pendingPrefix = "g";
setBattleChallengeFeedback("Waiting for the second g.", "accent");
return true;
} else if (["f", "t", "F", "T"].includes(key)) {
challenge.pendingPrefix = key;
setBattleChallengeFeedback(`Waiting for the target character after ${key}.`, "accent");
return true;
} else if (key === "d" && step.type === "action" && step.expect === "dd") {
challenge.pendingPrefix = "d";
setBattleChallengeFeedback("Heavy Slam primed. Press d again to complete dd.", "accent");
return true;
}
if (step.type === "action") {
if (key === step.expect && challenge.cursor.row === step.target.row && challenge.cursor.col === step.target.col) {
completeBattleChallenge();
return true;
}
failBattleChallenge(`Need ${step.expect} at line ${step.target.row + 1}, col ${step.target.col + 1}.`);
return true;
}
const withCount = challenge.countBuffer && /^(h|j|k|l|w|b|e)$/.test(key)
? `${challenge.countBuffer}${key}`
: key;
challenge.countBuffer = "";
const moved = applyBattleChallengeMotion(challenge, withCount);
if (!moved) {
failBattleChallenge(`That key does not move the battle drill cursor. Expected ${step.expect}.`);
return true;
}
if (withCount === step.expect && challenge.cursor.row === step.target.row && challenge.cursor.col === step.target.col) {
completeBattleChallenge();
return true;
}
failBattleChallenge(`Wrong motion. Expected ${step.expect}.`);
return true;
}
return {
currentBattleChallengeStep,
startBattleChallenge,
handleBattleChallengeKey,
};
}