Repository navigation
Expand file tree
/
Copy pathBattleMonitorHandler.cs
More file actions
523 lines (444 loc) · 18.3 KB
/
Copy pathBattleMonitorHandler.cs
File metadata and controls
523 lines (444 loc) · 18.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
using Il2Cpp;
using System;
using UnityEngine;
namespace DigimonNOAccess
{
/// <summary>
/// Background battle monitor that polls battle state every frame and
/// triggers audio cues and speech for important state changes.
///
/// Monitors:
/// - Enemy special attack charging (yellow aura) -> warning tone
/// - Partner OP reaching 150 -> milestone chime + speech
/// - Enemy target switching -> tone + speech
/// - ExE and SP attack availability -> one-time speech
///
/// Also stores last-known SP charging state for on-demand speech (F12 hotkey).
/// </summary>
public class BattleMonitorHandler : IAccessibilityHandler
{
// Background handler: high priority number, never "open" (doesn't block other handlers)
public int Priority => 998;
public bool IsOpen() => false;
// State tracking
private bool _wasBattleActive = false;
private bool _enemyCountAnnounced = false;
// SP attack tracking per enemy (up to 20 enemies max)
private const int MaxEnemies = 20;
private readonly float[] _lastSpTimer = new float[MaxEnemies];
private readonly bool[] _spWarningPlayed = new bool[MaxEnemies];
// OP milestone tracking per partner
private readonly bool[] _opMilestoneAnnounced = new bool[2];
private readonly int[] _lastOP = new int[2];
// Last known SP charging info for on-demand query
private static string _lastSPChargeInfo = "";
// Cheer cooldown tracking (null = not yet initialized this battle)
private bool? _cheerWasOnCooldown = null;
// Cooldowns to prevent spam
private float _lastSPWarningTime = 0f;
private const float SPWarningCooldown = 1.0f;
public void Update()
{
// Detect battle via m_CurStep (fires earlier than uBattlePanel.m_enabled).
var mgc = MainGameComponent.m_instance;
bool inBattleStep = mgc != null && mgc.m_CurStep == Il2CppMainGame.STEP.Battle;
if (!inBattleStep)
{
if (_wasBattleActive)
ResetState();
return;
}
if (!_wasBattleActive)
{
_wasBattleActive = true;
BattleAudioCues.Initialize();
DebugLogger.Log("[BattleMonitor] Battle started, monitoring active");
}
// Poll for enemy count each frame until announced.
// m_enemy_hps becomes available before m_enabled, giving us an early
// announcement window that fires before tutorial/event messages.
if (!_enemyCountAnnounced)
{
TryAnnounceEnemyCount();
}
// Ongoing monitoring requires the battle panel to be fully enabled.
var battlePanel = uBattlePanel.m_instance;
if (battlePanel == null || !battlePanel.m_enabled)
return;
try
{
MonitorEnemySPAttacks(battlePanel);
MonitorOPMilestones(battlePanel);
MonitorCheerAvailability(battlePanel);
}
catch (Exception ex)
{
DebugLogger.Log($"[BattleMonitor] Update error: {ex.Message}");
}
}
/// <summary>
/// Monitor enemy m_spAttackTimer to detect special attack charging.
/// When timer transitions from 0 to >0, the enemy is charging (yellow aura).
/// </summary>
private void MonitorEnemySPAttacks(uBattlePanel battlePanel)
{
var enemyBars = battlePanel.m_enemy_hps;
if (enemyBars == null) return;
float now = Time.time;
bool anyCharging = false;
string chargingInfo = "";
for (int i = 0; i < enemyBars.Length && i < MaxEnemies; i++)
{
try
{
var bar = enemyBars[i];
if (bar == null || bar.gameObject == null || !bar.gameObject.activeInHierarchy)
continue;
var unit = bar.m_unit;
if (unit == null) continue;
float spTimer = unit.m_spAttackTimer;
// Detect transition: was not charging -> now charging
if (spTimer > 0 && _lastSpTimer[i] <= 0 && !_spWarningPlayed[i])
{
_spWarningPlayed[i] = true;
// Play warning tone (with cooldown to prevent stacking)
if (now - _lastSPWarningTime > SPWarningCooldown)
{
BattleAudioCues.PlaySPWarning();
_lastSPWarningTime = now;
}
string enemyName = GetEnemyName(bar, unit);
DebugLogger.Log($"[BattleMonitor] Enemy {i} ({enemyName}) charging SP attack! Timer: {spTimer}");
anyCharging = true;
if (!string.IsNullOrEmpty(chargingInfo))
chargingInfo += ". ";
chargingInfo += $"{enemyName} charging special attack";
}
// Reset when timer goes back to 0
if (spTimer <= 0 && _lastSpTimer[i] > 0)
{
_spWarningPlayed[i] = false;
}
_lastSpTimer[i] = spTimer;
}
catch { }
}
if (anyCharging)
{
_lastSPChargeInfo = chargingInfo;
}
}
/// <summary>
/// Monitor partner Order Power and announce when reaching 150 (special attack ready).
/// </summary>
private void MonitorOPMilestones(uBattlePanel battlePanel)
{
var digimonPanels = battlePanel.m_digimon;
if (digimonPanels == null) return;
for (int i = 0; i < digimonPanels.Length && i < 2; i++)
{
try
{
var panel = digimonPanels[i];
if (panel == null) continue;
int op = panel.m_dispOrderPower;
// Detect crossing 150 threshold
if (op >= 150 && _lastOP[i] < 150 && !_opMilestoneAnnounced[i])
{
_opMilestoneAnnounced[i] = true;
BattleAudioCues.PlayOPMilestone();
string partnerLabel = PartnerUtilities.GetPartnerLabel(i);
ScreenReader.SayQueued($"{partnerLabel}, 150 OP ready");
DebugLogger.Log($"[BattleMonitor] {partnerLabel} reached 150 OP");
}
// Reset when OP drops below 150 (after using special)
if (op < 150 && _lastOP[i] >= 150)
{
_opMilestoneAnnounced[i] = false;
}
_lastOP[i] = op;
}
catch { }
}
}
/// <summary>
/// Poll for battle enemy HP bars each frame. Once active bars are found,
/// announce the count and stop polling. This fires as soon as the battle
/// panel's m_enemy_hps array is populated (before m_enabled becomes true),
/// giving us an early announcement before tutorial/event messages appear.
/// </summary>
private void TryAnnounceEnemyCount()
{
try
{
var battlePanel = uBattlePanel.m_instance;
if (battlePanel == null) return;
var enemyBars = battlePanel.m_enemy_hps;
if (enemyBars == null || enemyBars.Length == 0) return;
int count = 0;
for (int i = 0; i < enemyBars.Length; i++)
{
var bar = enemyBars[i];
if (bar != null && bar.gameObject != null && bar.gameObject.activeInHierarchy)
count++;
}
if (count > 0)
{
_enemyCountAnnounced = true;
string msg = count == 1 ? "1 enemy" : $"{count} enemies";
ScreenReader.SayQueued(msg);
DebugLogger.Log($"[BattleMonitor] Battle start: {msg}");
}
}
catch { }
}
/// <summary>
/// Monitor cheer cooldown and announce when it becomes available.
/// The beep tones handle per-hit timing; this gives a one-time speech cue.
/// </summary>
private void MonitorCheerAvailability(uBattlePanel battlePanel)
{
try
{
var cheerCmd = battlePanel.m_cheerCommand;
if (cheerCmd == null) return;
bool onCooldown = cheerCmd.DisableTimer > 0f;
// First frame: just record the initial state, don't announce
if (_cheerWasOnCooldown == null)
{
_cheerWasOnCooldown = onCooldown;
return;
}
if (_cheerWasOnCooldown.Value && !onCooldown)
{
ScreenReader.SayQueued("Cheer ready");
DebugLogger.Log("[BattleMonitor] Cheer came off cooldown");
}
_cheerWasOnCooldown = onCooldown;
}
catch { }
}
/// <summary>
/// Get a human-readable name for an enemy from its HP bar and DigimonCtrl.
/// </summary>
public static string GetEnemyName(uEnemyHpBar bar, DigimonCtrl unit)
{
// Try commonData.m_name first (pre-localized, most reliable)
try
{
var gameData = unit.gameData;
if (gameData != null)
{
string name = gameData.m_commonData?.m_name;
if (!string.IsNullOrEmpty(name) && !name.Contains("ランゲージ"))
return TextUtilities.StripRichTextTags(name);
// Fallback: look up via ParameterDigimonData
int digimonId = gameData.m_no;
if (digimonId > 0)
{
var paramData = ParameterDigimonData.GetParam((uint)digimonId);
if (paramData != null)
{
name = paramData.GetDefaultName();
if (!string.IsNullOrEmpty(name) && !name.Contains("ランゲージ"))
return TextUtilities.StripRichTextTags(name);
}
}
}
}
catch { }
// Fallback: try the GameObject name
try
{
string objName = unit.gameObject?.name;
if (!string.IsNullOrEmpty(objName))
return objName;
}
catch { }
return "Enemy";
}
/// <summary>
/// Determine which partner an enemy is currently targeting.
/// Returns "Partner 1", "Partner 2", or empty string if unknown.
/// </summary>
public static string GetEnemyTargetPartner(DigimonCtrl enemy)
{
try
{
var targetTransform = enemy.m_targetTransform;
if (targetTransform == null) return "";
for (int i = 0; i < 2; i++)
{
var partner = MainGameManager.GetPartnerCtrl(i);
if (partner != null && partner.gameObject != null &&
partner.gameObject.transform.Pointer == targetTransform.Pointer)
{
// Try to get the partner's Digimon name
try
{
string name = partner.gameData?.m_commonData?.m_name;
if (!string.IsNullOrEmpty(name) && !name.Contains("ランゲージ"))
return TextUtilities.StripRichTextTags(name);
}
catch { }
return PartnerUtilities.GetPartnerLabel(i);
}
}
}
catch { }
return "";
}
/// <summary>
/// Get information about all enemies currently in battle.
/// Used by BattleHudHandler for on-demand enemy info hotkey.
/// </summary>
public static string GetAllEnemyInfo()
{
var battlePanel = uBattlePanel.m_instance;
if (battlePanel == null || !battlePanel.m_enabled)
return "Not in battle";
var enemyBars = battlePanel.m_enemy_hps;
if (enemyBars == null || enemyBars.Length == 0)
return "No enemies";
string result = "";
int count = 0;
for (int i = 0; i < enemyBars.Length; i++)
{
try
{
var bar = enemyBars[i];
if (bar == null || bar.gameObject == null || !bar.gameObject.activeInHierarchy)
continue;
var unit = bar.m_unit;
if (unit == null) continue;
string name = GetEnemyName(bar, unit);
string level = "";
try
{
level = bar.m_levelText?.text ?? "";
}
catch { }
float hpRate = bar.m_now_hp_rate;
int hpPercent = (int)(hpRate * 100);
string targeting = GetEnemyTargetPartner(unit);
if (count > 0) result += ". ";
result += $"{name}";
if (!string.IsNullOrEmpty(level))
result += $" level {level}";
result += $", HP {hpPercent} percent";
if (!string.IsNullOrEmpty(targeting))
result += $", targeting {targeting}";
count++;
}
catch { }
}
if (count == 0)
return "No enemies visible";
DebugLogger.Log($"[BattleMonitor] GetAllEnemyInfo result: {result}");
return result;
}
/// <summary>
/// Get information about a specific enemy by index (0-based).
/// Used by BattleHudHandler for per-enemy hotkeys.
/// </summary>
public static string GetEnemyInfoByIndex(int index)
{
var battlePanel = uBattlePanel.m_instance;
if (battlePanel == null || !battlePanel.m_enabled)
return "Not in battle";
var enemyBars = battlePanel.m_enemy_hps;
if (enemyBars == null || enemyBars.Length == 0)
return "No enemies";
// Find the Nth active enemy
int count = 0;
for (int i = 0; i < enemyBars.Length; i++)
{
try
{
var bar = enemyBars[i];
if (bar == null || bar.gameObject == null || !bar.gameObject.activeInHierarchy)
continue;
var unit = bar.m_unit;
if (unit == null) continue;
if (count == index)
{
string name = GetEnemyName(bar, unit);
string level = "";
try
{
level = bar.m_levelText?.text ?? "";
}
catch { }
float hpRate = bar.m_now_hp_rate;
int hpPercent = (int)(hpRate * 100);
string targeting = GetEnemyTargetPartner(unit);
string result = name;
if (!string.IsNullOrEmpty(level))
result += $" level {level}";
result += $", HP {hpPercent} percent";
if (!string.IsNullOrEmpty(targeting))
result += $", targeting {targeting}";
return result;
}
count++;
}
catch { }
}
return $"No enemy {index + 1}";
}
/// <summary>
/// Get the current combined Order Power for both partners.
/// </summary>
public static string GetOrderPower()
{
var battlePanel = uBattlePanel.m_instance;
if (battlePanel == null || !battlePanel.m_enabled)
return "Not in battle";
var digimonPanels = battlePanel.m_digimon;
if (digimonPanels == null) return "No data";
int totalOP = 0;
for (int i = 0; i < digimonPanels.Length && i < 2; i++)
{
try
{
var panel = digimonPanels[i];
if (panel != null)
totalOP += panel.m_dispOrderPower;
}
catch { }
}
return $"{totalOP} OP";
}
/// <summary>
/// Get the last SP charge warning info for on-demand speech.
/// </summary>
public static string GetLastSPChargeInfo()
{
if (string.IsNullOrEmpty(_lastSPChargeInfo))
return "No special attack detected";
return _lastSPChargeInfo;
}
private void ResetState()
{
_wasBattleActive = false;
_enemyCountAnnounced = false;
_lastSPChargeInfo = "";
_cheerWasOnCooldown = null;
for (int i = 0; i < MaxEnemies; i++)
{
_lastSpTimer[i] = 0;
_spWarningPlayed[i] = false;
}
for (int i = 0; i < 2; i++)
{
_opMilestoneAnnounced[i] = false;
_lastOP[i] = 0;
}
DebugLogger.Log("[BattleMonitor] State reset (battle ended)");
}
public void AnnounceStatus()
{
// Background handler, doesn't announce status
}
}
}