Repository navigation
Expand file tree
/
Copy pathGameStateService.cs
More file actions
622 lines (561 loc) · 24.2 KB
/
Copy pathGameStateService.cs
File metadata and controls
622 lines (561 loc) · 24.2 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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
using Il2Cpp;
using System;
using UnityEngine;
namespace DigimonNOAccess
{
/// <summary>
/// Centralized service for checking player and game state.
/// Consolidates duplicated state checks from AudioNavigationHandler,
/// NavigationListHandler, and FieldHudHandler into a single source of truth.
/// All methods are static for easy access from any handler.
/// </summary>
public static class GameStateService
{
/// <summary>
/// Check if the battle panel instance exists and is enabled.
/// Lightweight check for whether a battle UI is currently showing.
/// </summary>
public static bool IsInBattle()
{
try
{
var battlePanel = uBattlePanel.m_instance;
if (battlePanel != null && battlePanel.m_enabled)
return true;
}
catch { }
return false;
}
/// <summary>
/// Check if we're in any battle phase, including the result screen.
/// Uses multiple detection methods to catch tutorial battles and battle dialogs.
/// Also checks MainGameBattle's internal step to catch the transition gap
/// between battle ending and result panel appearing.
/// </summary>
public static bool IsInBattlePhase()
{
try
{
// FIRST: Check MainGameComponent's current step - most reliable for battle detection
var mgc = MainGameComponent.m_instance;
if (mgc != null)
{
var curStep = mgc.m_CurStep;
// If we're in Battle mode at all, check the battle's internal step
if (curStep == Il2CppMainGame.STEP.Battle)
{
// Check MainGameBattle's step for Win/Lose/Escape states
// These occur BEFORE the result panel is shown
try
{
var stepProc = mgc.m_StepProc;
if (stepProc != null && stepProc.Length > 1)
{
var battleIF = stepProc[1]; // Index 1 = Battle
if (battleIF != null)
{
var mainGameBattle = battleIF.TryCast<MainGameBattle>();
if (mainGameBattle != null)
{
var stepProg = mainGameBattle.m_Step;
if (stepProg != null)
{
var battleStep = stepProg.step;
// Any step in battle means we're still in battle
// Win, Lose, Miracle, Escape, Break = battle ended but transitioning
if (battleStep != MainGameBattle.STEP.Idle)
{
return true;
}
}
}
}
}
}
catch { }
// Even if we couldn't check internal step, we're in Battle mode
return true;
}
}
// Check the battle panel
var battlePanel = uBattlePanel.m_instance;
if (battlePanel != null)
{
// Battle UI is active
if (battlePanel.m_enabled)
{
return true;
}
// Check if result panel is showing (victory/defeat screen)
// This is shown AFTER battle panel is disabled, player still can't move
// ONLY use m_enabled - gameObject.activeInHierarchy is unreliable
var resultPanel = battlePanel.m_result;
if (resultPanel != null)
{
try
{
if (resultPanel.m_enabled)
{
return true;
}
}
catch { }
}
}
// Check if any partner is in battle action state
// This catches tutorial battles even when battle panel is hidden for dialogs
var partnerCtrls = UnityEngine.Object.FindObjectsOfType<PartnerCtrl>();
foreach (var partner in partnerCtrls)
{
if (partner != null && partner.gameObject.activeInHierarchy)
{
var state = partner.actionState;
if (state == UnitCtrlBase.ActionState.ActionState_Battle)
{
return true;
}
}
}
}
catch { }
return false;
}
/// <summary>
/// Check if the game is paused (m_isPause on MainGameManager).
/// </summary>
public static bool IsGamePaused()
{
try
{
var mgm = MainGameManager.m_instance;
if (mgm != null && mgm.m_isPause)
return true;
}
catch { }
return false;
}
/// <summary>
/// Check if the player is sleeping (Care > Sleep action).
/// MainGameComponent.m_CurStep transitions to STEP.Sleep during the
/// sleep animation and stat recovery sequence (after the care panel closes).
/// </summary>
public static bool IsPlayerSleeping()
{
try
{
var mgc = MainGameComponent.m_instance;
if (mgc != null && mgc.m_CurStep == Il2CppMainGame.STEP.Sleep)
return true;
}
catch { }
return false;
}
/// <summary>
/// Check if the player is in an event/cutscene action state.
/// </summary>
public static bool IsInEvent()
{
try
{
var player = UnityEngine.Object.FindObjectOfType<PlayerCtrl>();
if (player != null)
{
return player.actionState == UnitCtrlBase.ActionState.ActionState_Event;
}
}
catch { }
return false;
}
/// <summary>
/// Check if the player just lost a battle (death/game over recovery).
/// During this time the player is returned to overworld but cannot move.
///
/// Key: Check MainGameField.m_Step.step == RestartLose or RestartEscape,
/// which is the definitive indicator that the game is in the death recovery sequence.
/// </summary>
public static bool IsInDeathRecovery()
{
try
{
var mgc = MainGameComponent.m_instance;
if (mgc == null)
return false;
// Only check when we're supposed to be in field mode
var curStep = mgc.m_CurStep;
if (curStep != Il2CppMainGame.STEP.Field)
return false;
// Check MainGameField's internal step state
var stepProc = mgc.m_StepProc;
if (stepProc == null || stepProc.Length == 0)
return false;
var fieldIF = stepProc[0]; // Index 0 = Field
if (fieldIF == null)
return false;
// Try to cast to MainGameField and check m_Step
var mainGameField = fieldIF.TryCast<MainGameField>();
if (mainGameField == null)
return false;
var stepProg = mainGameField.m_Step;
if (stepProg == null)
return false;
var fieldStep = stepProg.step;
// RestartLose = death recovery, RestartEscape = escape recovery
if (fieldStep == MainGameField.STEP.RestartLose ||
fieldStep == MainGameField.STEP.RestartEscape)
{
DebugLogger.Log($"[GameState] Death recovery detected: {fieldStep}");
return true;
}
}
catch (Exception ex)
{
// Log but don't block - if we can't determine, assume not in recovery
DebugLogger.Log($"[GameState] IsInDeathRecovery error (ignoring): {ex.Message}");
}
return false;
}
/// <summary>
/// Check if the field's internal step is in a playable state (Main or Idle).
/// Returns false during Start (loading), Warp (area transition), Evolution,
/// RestartLose/RestartEscape (death recovery), etc.
/// </summary>
public static bool IsFieldInPlayableState()
{
try
{
var mgc = MainGameComponent.m_instance;
if (mgc == null) return false;
if (mgc.m_CurStep != Il2CppMainGame.STEP.Field)
return false;
var stepProc = mgc.m_StepProc;
if (stepProc == null || stepProc.Length == 0)
return false;
var fieldIF = stepProc[0]; // Index 0 = Field
if (fieldIF == null)
return false;
var mainGameField = fieldIF.TryCast<MainGameField>();
if (mainGameField == null)
return false;
var stepProg = mainGameField.m_Step;
if (stepProg == null)
return false;
var fieldStep = stepProg.step;
// Only Main and Idle are playable states
return fieldStep == MainGameField.STEP.Main || fieldStep == MainGameField.STEP.Idle;
}
catch
{
return false;
}
}
/// <summary>
/// Check if player action state indicates they cannot move.
/// Includes death states, events, battles, damage, and recovery sequences.
/// </summary>
public static bool IsPlayerInNonControllableState()
{
try
{
var player = UnityEngine.Object.FindObjectOfType<PlayerCtrl>();
if (player == null)
return true; // No player = not controllable
var actionState = player.actionState;
// Check for states where player cannot freely move
switch (actionState)
{
case UnitCtrlBase.ActionState.ActionState_Event:
case UnitCtrlBase.ActionState.ActionState_Battle:
case UnitCtrlBase.ActionState.ActionState_Dead:
case UnitCtrlBase.ActionState.ActionState_DeadGataway:
case UnitCtrlBase.ActionState.ActionState_LiquidCrystallization:
case UnitCtrlBase.ActionState.ActionState_Damage:
case UnitCtrlBase.ActionState.ActionState_DownDamage:
case UnitCtrlBase.ActionState.ActionState_BlowDamage:
case UnitCtrlBase.ActionState.ActionState_Getup:
return true;
}
}
catch { }
return false;
}
/// <summary>
/// Check if player action state indicates they cannot move.
/// Uses the provided PlayerCtrl reference to avoid redundant FindObjectOfType calls.
/// Includes death states, events, battles, damage, and recovery sequences.
/// </summary>
public static bool IsPlayerInNonControllableState(PlayerCtrl player)
{
try
{
if (player == null)
return true; // No player = not controllable
var actionState = player.actionState;
// Check for states where player cannot freely move
switch (actionState)
{
case UnitCtrlBase.ActionState.ActionState_Event:
case UnitCtrlBase.ActionState.ActionState_Battle:
case UnitCtrlBase.ActionState.ActionState_Dead:
case UnitCtrlBase.ActionState.ActionState_DeadGataway:
case UnitCtrlBase.ActionState.ActionState_LiquidCrystallization:
case UnitCtrlBase.ActionState.ActionState_Damage:
case UnitCtrlBase.ActionState.ActionState_DownDamage:
case UnitCtrlBase.ActionState.ActionState_BlowDamage:
case UnitCtrlBase.ActionState.ActionState_Getup:
return true;
}
}
catch { }
return false;
}
/// <summary>
/// Composite check: player is controllable when not in battle, not paused,
/// not in event, not in death recovery, and not in a non-controllable action state.
/// Does NOT check menus - use IsPlayerInControl() for that.
/// </summary>
public static bool IsPlayerControllable()
{
try
{
if (IsGamePaused())
return false;
if (IsInBattlePhase())
return false;
if (IsInDeathRecovery())
return false;
if (IsPlayerInNonControllableState())
return false;
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Check if the player is in the field and controllable.
/// Verifies MainGameComponent.m_CurStep == Field, player is not in
/// a non-controllable action state, and the game is not paused.
/// Does NOT check for menus or evolution (callers handle those separately).
/// </summary>
public static bool IsPlayerInField()
{
try
{
// Check MainGameComponent step - must be in Field mode
var mgc = MainGameComponent.m_instance;
if (mgc == null) return false;
if (mgc.m_CurStep != Il2CppMainGame.STEP.Field)
return false;
// Check player exists and is in a controllable action state
var player = UnityEngine.Object.FindObjectOfType<PlayerCtrl>();
if (player == null) return false;
var actionState = player.actionState;
switch (actionState)
{
case UnitCtrlBase.ActionState.ActionState_Event:
case UnitCtrlBase.ActionState.ActionState_Battle:
case UnitCtrlBase.ActionState.ActionState_Dead:
case UnitCtrlBase.ActionState.ActionState_DeadGataway:
case UnitCtrlBase.ActionState.ActionState_LiquidCrystallization:
case UnitCtrlBase.ActionState.ActionState_Damage:
case UnitCtrlBase.ActionState.ActionState_DownDamage:
case UnitCtrlBase.ActionState.ActionState_BlowDamage:
case UnitCtrlBase.ActionState.ActionState_Getup:
return false;
}
// Check game is not paused
if (IsGamePaused())
return false;
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Overload that accepts a cached PlayerCtrl reference to avoid FindObjectOfType calls.
/// Check if the player is in the field and controllable.
/// </summary>
public static bool IsPlayerInField(PlayerCtrl player)
{
try
{
// Check MainGameComponent step - must be in Field mode
var mgc = MainGameComponent.m_instance;
if (mgc == null) return false;
if (mgc.m_CurStep != Il2CppMainGame.STEP.Field)
return false;
// Check player exists and is in a controllable action state
if (player == null) return false;
var actionState = player.actionState;
switch (actionState)
{
case UnitCtrlBase.ActionState.ActionState_Event:
case UnitCtrlBase.ActionState.ActionState_Battle:
case UnitCtrlBase.ActionState.ActionState_Dead:
case UnitCtrlBase.ActionState.ActionState_DeadGataway:
case UnitCtrlBase.ActionState.ActionState_LiquidCrystallization:
case UnitCtrlBase.ActionState.ActionState_Damage:
case UnitCtrlBase.ActionState.ActionState_DownDamage:
case UnitCtrlBase.ActionState.ActionState_BlowDamage:
case UnitCtrlBase.ActionState.ActionState_Getup:
return false;
}
// Check game is not paused
if (IsGamePaused())
return false;
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Check if any menu is open that blocks player control.
/// Checks Digivice, care, save, training, training bonus, training result,
/// and evolution panels.
///
/// Note: Training result cooldown is NOT handled here since it requires
/// instance-level timing state. Callers that need that behavior (e.g.,
/// AudioNavigationHandler) should handle it separately.
/// </summary>
public static bool IsMenuOpen()
{
try
{
var mgm = MainGameManager.m_instance;
if (mgm != null)
{
// Care menu check - reliable state-based detection
var careUI = mgm.careUI;
if (careUI != null && careUI.m_state != uCarePanel.State.None)
return true;
}
// Digivice panel check - has static instance with m_enabled flag
var digivicePanel = uDigivicePanel.m_instance;
if (digivicePanel != null && digivicePanel.m_enabled)
return true;
// Save panel check - use state-based detection via uSavePanelCommand
var savePanel = UnityEngine.Object.FindObjectOfType<uSavePanelCommand>();
if (savePanel != null)
{
var state = savePanel.m_State;
if (state == uSavePanelCommand.State.MAIN ||
state == uSavePanelCommand.State.SAVE_CHECK ||
state == uSavePanelCommand.State.LOAD_CHECK)
return true;
}
// Training panel check - main training selection
var trainingPanel = UnityEngine.Object.FindObjectOfType<uTrainingPanelCommand>();
if (trainingPanel != null && trainingPanel.gameObject.activeInHierarchy)
{
var state = trainingPanel.m_state;
if (state != uTrainingPanelCommand.State.None && state != uTrainingPanelCommand.State.Close)
return true;
}
// Training bonus roulette panel check
var bonusPanel = UnityEngine.Object.FindObjectOfType<uTrainingPanelBonus>();
if (bonusPanel != null && bonusPanel.gameObject.activeInHierarchy)
{
var state = bonusPanel.m_state;
if (state != uTrainingPanelBonus.State.None)
return true;
}
// Training result panel check
var resultPanel = UnityEngine.Object.FindObjectOfType<uTrainingPanelResult>();
if (resultPanel != null && resultPanel.gameObject.activeInHierarchy)
return true;
// Evolution/Digivolution check
var evolution = UnityEngine.Object.FindObjectOfType<EvolutionBase>();
if (evolution != null && evolution.gameObject.activeInHierarchy)
return true;
return false;
}
catch
{
return false;
}
}
/// <summary>
/// Check if an EnemyCtrl is alive and valid (not null, active, not in death state).
/// Uses the game's own actionState to detect defeated enemies immediately,
/// even before their GameObject is deactivated.
/// </summary>
public static bool IsEnemyAlive(EnemyCtrl enemy)
{
if (enemy == null || enemy.gameObject == null || !enemy.gameObject.activeInHierarchy)
return false;
try
{
var state = enemy.actionState;
return state != UnitCtrlBase.ActionState.ActionState_Dead &&
state != UnitCtrlBase.ActionState.ActionState_DeadGataway;
}
catch { return false; }
}
/// <summary>
/// Check if we're currently in a tutorial or event-scripted sequence.
/// Checks MainGameManager.eventScene which holds a TalkMain reference
/// that persists during event-triggered battles (tutorials, story events).
/// Falls back to MainGameComponent.m_tutorialScene for non-battle tutorial phases.
/// </summary>
public static bool IsInTutorial()
{
try
{
// Primary: MainGameManager.eventScene persists during event-triggered battles
var mgm = MainGameManager.m_instance;
if (mgm != null)
{
var evtScene = mgm.eventScene;
if (evtScene != null)
return true;
}
// Fallback: m_tutorialScene on MainGameComponent (set during non-battle tutorial phases)
var mgc = MainGameComponent.m_instance;
if (mgc != null)
{
var tutScene = mgc.m_tutorialScene;
if (tutScene != null)
return true;
}
}
catch { }
return false;
}
/// <summary>
/// Check if the player is in the field and has basic control (not in battle,
/// not in a non-controllable action state). Used by FieldHudHandler for
/// determining when to respond to status queries.
/// Lighter check than full IsPlayerInField - does not check pause or game step.
/// </summary>
public static bool IsPlayerInFieldControl()
{
// Check if battle is active
if (IsInBattle())
return false;
// Check player action state - exclude states where we shouldn't respond
try
{
var player = UnityEngine.Object.FindObjectOfType<PlayerCtrl>();
if (player != null)
{
var state = player.actionState;
if (state == PlayerCtrl.ActionState.ActionState_Event ||
state == PlayerCtrl.ActionState.ActionState_Battle ||
state == PlayerCtrl.ActionState.ActionState_Dead ||
state == PlayerCtrl.ActionState.ActionState_DeadGataway ||
state == PlayerCtrl.ActionState.ActionState_LiquidCrystallization)
{
return false;
}
}
}
catch { }
return true;
}
}
}