Repository navigation
Expand file tree
/
Copy pathMcpMod.Actions.cs
More file actions
1893 lines (1624 loc) · 77.7 KB
/
Copy pathMcpMod.Actions.cs
File metadata and controls
1893 lines (1624 loc) · 77.7 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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using MegaCrit.Sts2.Core.Nodes.Cards;
using MegaCrit.Sts2.Core.Nodes.Cards.Holders;
using MegaCrit.Sts2.Core.Nodes.CommonUi;
using MegaCrit.Sts2.Core.Nodes.Rewards;
using MegaCrit.Sts2.Core.Nodes.Screens;
using MegaCrit.Sts2.Core.Nodes.Screens.CardSelection;
using MegaCrit.Sts2.Core.Nodes.Screens.Map;
using MegaCrit.Sts2.Core.Nodes.Relics;
using MegaCrit.Sts2.Core.Nodes.Screens.Overlays;
using MegaCrit.Sts2.Core.Nodes.Screens.TreasureRoomRelic;
using MegaCrit.Sts2.Core.Entities.Merchant;
using MegaCrit.Sts2.Core.Models.Events;
using MegaCrit.Sts2.Core.Nodes.Events;
using MegaCrit.Sts2.Core.Nodes.Events.Custom;
using MegaCrit.Sts2.Core.Nodes.Events.Custom.CrystalSphere;
using MegaCrit.Sts2.Core.Nodes.Screens.Shops;
using MegaCrit.Sts2.Core.Nodes.GodotExtensions;
using MegaCrit.Sts2.Core.Map;
using MegaCrit.Sts2.Core.Rooms;
using MegaCrit.Sts2.Core.Nodes.RestSite;
using MegaCrit.Sts2.Core.Nodes.Combat;
using MegaCrit.Sts2.Core.Nodes.Rooms;
using MegaCrit.Sts2.Core.Rewards;
using MegaCrit.Sts2.Core.Combat;
using MegaCrit.Sts2.Core.Commands;
using MegaCrit.Sts2.Core.Context;
using MegaCrit.Sts2.Core.Entities.Cards;
using MegaCrit.Sts2.Core.Entities.Creatures;
using MegaCrit.Sts2.Core.Entities.Players;
using MegaCrit.Sts2.Core.Entities.Potions;
using MegaCrit.Sts2.Core.GameActions;
using MegaCrit.Sts2.Core.Runs;
using MegaCrit.Sts2.Core.Nodes.Screens.CharacterSelect;
using MegaCrit.Sts2.Core.Nodes.Screens.MainMenu;
using MegaCrit.Sts2.Core.Nodes.Screens.GameOverScreen;
using MegaCrit.Sts2.Core.Nodes.Screens.Timeline;
using MegaCrit.Sts2.Core.Nodes.Screens.ProfileScreen;
using Godot;
namespace STS2_MCP;
public static partial class McpMod
{
private static Dictionary<string, object?> ExecuteAction(string action, Dictionary<string, JsonElement> data)
{
if (!RunManager.Instance.IsInProgress)
return Error("No run in progress");
var runState = RunManager.Instance.DebugOnlyGetState()!;
var player = LocalContext.GetMe(runState);
if (player == null)
return Error("Could not find local player");
var tree = (Godot.Engine.GetMainLoop()) as SceneTree;
if (tree?.Root != null && IsAnyFtueVisible(tree.Root))
return Error("Blocking popup active. Use menu_select with one of the advertised popup options before gameplay actions.");
return action switch
{
"play_card" => ExecutePlayCard(player, data),
"use_potion" => ExecuteUsePotion(player, data),
"discard_potion" => ExecuteDiscardPotion(player, data),
"end_turn" => ExecuteEndTurn(player),
"choose_map_node" => ExecuteChooseMapNode(data),
"choose_event_option" => ExecuteChooseEventOption(data),
"advance_dialogue" => ExecuteAdvanceDialogue(),
"choose_rest_option" => ExecuteChooseRestOption(data),
"shop_purchase" => ExecuteShopPurchase(player, data),
"claim_reward" => ExecuteClaimReward(data),
"select_card_reward" => ExecuteSelectCardReward(data),
"skip_card_reward" => ExecuteSkipCardReward(),
"proceed" => ExecuteProceed(),
"select_card" => ExecuteSelectCard(data),
"confirm_selection" => ExecuteConfirmSelection(),
"cancel_selection" => ExecuteCancelSelection(),
"select_bundle" => ExecuteSelectBundle(data),
"confirm_bundle_selection" => ExecuteConfirmBundleSelection(),
"cancel_bundle_selection" => ExecuteCancelBundleSelection(),
"combat_select_card" => ExecuteCombatSelectCard(data),
"combat_confirm_selection" => ExecuteCombatConfirmSelection(),
"select_relic" => ExecuteSelectRelic(data),
"skip_relic_selection" => ExecuteSkipRelicSelection(),
"claim_treasure_relic" => ExecuteClaimTreasureRelic(data),
"crystal_sphere_set_tool" => ExecuteCrystalSphereSetTool(data),
"crystal_sphere_click_cell" => ExecuteCrystalSphereClickCell(data),
"crystal_sphere_proceed" => ExecuteCrystalSphereProceed(),
_ => Error($"Unknown action: {action}")
};
}
private static Dictionary<string, object?> ExecutePlayCard(Player player, Dictionary<string, JsonElement> data)
{
if (!CombatManager.Instance.IsInProgress)
return Error("Not in combat");
if (!IsPlayPhase(player))
return Error("Not in play phase - cannot act during enemy turn");
if (CombatManager.Instance.PlayerActionsDisabled)
return Error("Player actions are currently disabled");
if (!player.Creature.IsAlive)
return Error("Player creature is dead - cannot play cards");
var combatState = player.Creature.CombatState;
if (combatState == null)
return Error("No combat state");
// Get card by index in hand
if (!data.TryGetValue("card_index", out var indexElem))
return Error("Missing 'card_index'");
int cardIndex = indexElem.GetInt32();
var hand = player.PlayerCombatState?.Hand;
if (hand == null)
return Error("No hand available");
if (cardIndex < 0 || cardIndex >= hand.Cards.Count)
return Error($"card_index {cardIndex} out of range (hand has {hand.Cards.Count} cards)");
var card = hand.Cards[cardIndex];
if (!card.CanPlay(out var reason, out _))
return Error($"Card '{card.Title}' cannot be played: {reason}");
// Resolve target
Creature? target = null;
if (card.TargetType == TargetType.AnyEnemy)
{
if (!data.TryGetValue("target", out var targetElem))
return Error("Card requires a target. Provide 'target' with an entity_id.");
string targetId = targetElem.GetString() ?? "";
target = ResolveTarget(combatState, targetId);
if (target == null)
return Error($"Target '{targetId}' not found among alive enemies");
}
// Play the card via the action queue (same path as the game UI)
RunManager.Instance.ActionQueueSynchronizer.RequestEnqueue(new PlayCardAction(card, target));
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Playing '{card.Title}'" + (target != null ? $" targeting {SafeGetText(() => target.Monster?.Title) ?? "target"}" : "")
};
}
private static Dictionary<string, object?> ExecuteEndTurn(Player player)
{
if (!CombatManager.Instance.IsInProgress)
return Error("Not in combat");
if (!IsPlayPhase(player))
return Error("Not in play phase - cannot act during enemy turn");
if (CombatManager.Instance.PlayerActionsDisabled)
return Error("Player actions are currently disabled (turn may already be ending)");
// Match the game's own CanTurnBeEnded guard (NEndTurnButton.cs:114-123)
var hand = NCombatRoom.Instance?.Ui?.Hand;
if (hand != null && (hand.InCardPlay || hand.CurrentMode != NPlayerHand.Mode.Play))
return Error("Cannot end turn while a card is being played or hand is in selection mode");
PlayerCmd.EndTurn(player, canBackOut: false);
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Ending turn"
};
}
private static Dictionary<string, object?> ExecuteUsePotion(Player player, Dictionary<string, JsonElement> data)
{
if (!data.TryGetValue("slot", out var slotElem))
return Error("Missing 'slot' (potion slot index)");
int slot = slotElem.GetInt32();
if (slot < 0 || slot >= player.PotionSlots.Count)
return Error($"Potion slot {slot} out of range (player has {player.PotionSlots.Count} slots)");
var potion = player.GetPotionAtSlotIndex(slot);
if (potion == null)
return Error($"No potion in slot {slot}");
if (potion.IsQueued)
return Error($"Potion '{SafeGetText(() => potion.Title)}' is already queued for use");
if (potion.Owner.Creature.IsDead)
return Error("Cannot use potion - player creature is dead");
if (!potion.PassesCustomUsabilityCheck)
return Error($"Potion '{SafeGetText(() => potion.Title)}' cannot be used right now");
bool inCombat = CombatManager.Instance.IsInProgress;
if (potion.Usage == PotionUsage.CombatOnly)
{
if (!inCombat)
return Error($"Potion '{SafeGetText(() => potion.Title)}' can only be used in combat");
if (!IsPlayPhase(player))
return Error("Cannot use potions outside of play phase");
}
else if (potion.Usage == PotionUsage.Automatic)
return Error($"Potion '{SafeGetText(() => potion.Title)}' is automatic and cannot be manually used");
if (inCombat && CombatManager.Instance.PlayerActionsDisabled)
return Error("Player actions are currently disabled");
// Resolve target
Creature? target = null;
var combatState = player.Creature.CombatState;
switch (potion.TargetType)
{
case TargetType.AnyEnemy:
if (!data.TryGetValue("target", out var targetElem))
return Error("Potion requires a target enemy. Provide 'target' with an entity_id.");
string targetId = targetElem.GetString() ?? "";
if (combatState == null)
return Error("No combat state for target resolution");
target = ResolveTarget(combatState, targetId);
if (target == null)
return Error($"Target '{targetId}' not found among alive enemies");
break;
case TargetType.Self:
case TargetType.AnyAlly:
case TargetType.AnyPlayer:
target = player.Creature;
break;
default:
target = null;
break;
}
potion.EnqueueManualUse(target);
string targetMsg = potion.TargetType switch
{
TargetType.AnyEnemy => $" targeting {SafeGetText(() => target?.Monster?.Title) ?? "enemy"}",
TargetType.Self or TargetType.AnyPlayer or TargetType.AnyAlly => " on self",
_ => ""
};
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Using potion '{SafeGetText(() => potion.Title)}' from slot {slot}{targetMsg}"
};
}
private static Dictionary<string, object?> ExecuteDiscardPotion(Player player, Dictionary<string, JsonElement> data)
{
if (!data.TryGetValue("slot", out var slotElem))
return Error("Missing 'slot' (potion slot index)");
int slot = slotElem.GetInt32();
if (slot < 0 || slot >= player.PotionSlots.Count)
return Error($"Potion slot {slot} out of range (player has {player.PotionSlots.Count} slots)");
var potion = player.GetPotionAtSlotIndex(slot);
if (potion == null)
return Error($"No potion in slot {slot}");
string potionName = SafeGetText(() => potion.Title) ?? "unknown";
_ = PotionCmd.Discard(potion);
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Discarded potion '{potionName}' from slot {slot}"
};
}
private static Dictionary<string, object?> ExecuteChooseEventOption(Dictionary<string, JsonElement> data)
{
var uiRoom = NEventRoom.Instance;
if (uiRoom == null)
return Error("Event room is not open");
if (!data.TryGetValue("index", out var indexElem))
return Error("Missing 'index' (event option index)");
int index = indexElem.GetInt32();
var buttons = FindAll<NEventOptionButton>(uiRoom);
if (buttons.Count == 0)
return Error("No event options available");
if (index < 0 || index >= buttons.Count)
return Error($"Event option index {index} out of range ({buttons.Count} options)");
var button = buttons[index];
if (button.Option.IsLocked)
return Error($"Event option {index} is locked");
string title = SafeGetText(() => button.Option.Title) ?? "option";
button.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Choosing event option: {title}"
};
}
private static Dictionary<string, object?> ExecuteAdvanceDialogue()
{
var uiRoom = NEventRoom.Instance;
if (uiRoom == null)
return Error("Event room is not open");
var ancientLayout = FindFirst<NAncientEventLayout>(uiRoom);
if (ancientLayout == null)
return Error("No ancient dialogue active");
var hitbox = ancientLayout.GetNodeOrNull<NClickableControl>("%DialogueHitbox");
if (hitbox == null || !hitbox.Visible || !hitbox.IsEnabled)
return Error("Dialogue hitbox not available - dialogue may have ended");
hitbox.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Advancing dialogue"
};
}
private static Dictionary<string, object?> ExecuteChooseRestOption(Dictionary<string, JsonElement> data)
{
if (!data.TryGetValue("index", out var indexElem))
return Error("Missing 'index' (rest site option index)");
int index = indexElem.GetInt32();
var restRoom = NRestSiteRoom.Instance;
if (restRoom == null)
return Error("Rest site room is not open");
var buttons = FindAll<NRestSiteButton>(restRoom);
if (buttons.Count == 0)
return Error("No rest site options available");
if (index < 0 || index >= buttons.Count)
return Error($"Rest option index {index} out of range ({buttons.Count} options)");
var button = buttons[index];
if (!button.Option.IsEnabled)
return Error($"Rest option {index} ({button.Option.OptionId}) is disabled");
string optionName = SafeGetText(() => button.Option.Title) ?? button.Option.OptionId;
button.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Selecting rest site option: {optionName}"
};
}
private static Dictionary<string, object?> ExecuteShopPurchase(Player player, Dictionary<string, JsonElement> data)
{
MerchantInventory? inventory = null;
if (player.RunState.CurrentRoom is MerchantRoom merchantRoom)
{
// Regular merchant - auto-open inventory if needed
var merchUI = NMerchantRoom.Instance;
if (merchUI?.Inventory != null && !merchUI.Inventory.IsOpen)
merchUI.OpenInventory();
// v0.107 split the merchant into per-player inventories; take the local one.
inventory = merchantRoom.GetLocalInventory();
}
else if (player.RunState.CurrentRoom is EventRoom eventRoom
&& eventRoom.CanonicalEvent is FakeMerchant
&& (eventRoom.LocalMutableEvent ?? eventRoom.CanonicalEvent) is FakeMerchant fakeMerchant)
{
// Fake merchant event - auto-open via button if needed
if (!fakeMerchant.StartedFight)
{
var uiRoom = NEventRoom.Instance;
if (uiRoom != null)
{
var fmNode = FindFirst<NFakeMerchant>(uiRoom);
if (fmNode != null)
{
var inventoryUI = FindFirst<NMerchantInventory>(fmNode);
if (inventoryUI != null && !inventoryUI.IsOpen)
{
var btn = fmNode.MerchantButton;
if (btn != null && btn.Visible && btn.IsEnabled)
btn.ForceClick();
}
}
}
}
inventory = fakeMerchant.Inventory;
}
else
{
return Error("Not in a shop");
}
if (inventory == null)
return Error("Shop inventory not ready yet; wait a moment and retry");
if (!data.TryGetValue("index", out var indexElem))
return Error("Missing 'index' (shop item index)");
int index = indexElem.GetInt32();
var allEntries = inventory.AllEntries.ToList();
if (index < 0 || index >= allEntries.Count)
return Error($"Shop item index {index} out of range ({allEntries.Count} items)");
var entry = allEntries[index];
if (!entry.IsStocked)
return Error("Item is sold out");
if (!entry.EnoughGold)
return Error($"Not enough gold (need {entry.Cost}, have {player.Gold})");
// Fire-and-forget purchase (same path as AutoSlay)
_ = entry.OnTryPurchaseWrapper(inventory);
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Purchasing item for {entry.Cost} gold"
};
}
private static Dictionary<string, object?> ExecuteChooseMapNode(Dictionary<string, JsonElement> data)
{
var mapScreen = NMapScreen.Instance;
if (mapScreen == null || (!mapScreen.IsOpen && !IsNodeVisible(mapScreen)))
return Error("Map screen is not open");
if (!data.TryGetValue("index", out var indexElem))
return Error("Missing 'index' (map node index from next_options)");
int index = indexElem.GetInt32();
var travelable = FindAll<NMapPoint>(mapScreen)
.Where(mp => mp.State == MapPointState.Travelable && mp.Point != null)
.OrderBy(mp => mp.Point!.coord.col)
.ToList();
if (travelable.Count == 0)
return Error("No travelable map nodes available");
if (index < 0 || index >= travelable.Count)
return Error($"Map node index {index} out of range ({travelable.Count} options available)");
var target = travelable[index];
var pt = target.Point!;
mapScreen.OnMapPointSelectedLocally(target);
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Traveling to {pt.PointType} at ({pt.coord.col},{pt.coord.row})"
};
}
private static Dictionary<string, object?> ExecuteClaimReward(Dictionary<string, JsonElement> data)
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NRewardsScreen rewardsScreen)
return Error("Rewards screen is not open");
if (!data.TryGetValue("index", out var indexElem))
return Error("Missing 'index' (reward index)");
int index = indexElem.GetInt32();
var enabledButtons = FindAll<NRewardButton>(rewardsScreen)
.Where(b => b.IsEnabled && b.Reward != null)
.ToList();
if (index < 0 || index >= enabledButtons.Count)
return Error($"Reward index {index} out of range (screen has {enabledButtons.Count} claimable rewards)");
var button = enabledButtons[index];
var reward = button.Reward!;
string rewardDesc = GetRewardTypeName(reward);
if (reward is GoldReward g)
rewardDesc = $"gold ({g.Amount})";
else if (reward is PotionReward p)
rewardDesc = $"potion ({SafeGetText(() => p.Potion?.Title)})";
else if (reward is CardReward)
rewardDesc = "card (opens card selection)";
button.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Claiming reward: {rewardDesc}"
};
}
private static Dictionary<string, object?> ExecuteSelectCardReward(Dictionary<string, JsonElement> data)
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NCardRewardSelectionScreen cardScreen)
return Error("Card reward selection screen is not open");
if (!data.TryGetValue("card_index", out var indexElem))
return Error("Missing 'card_index'");
int cardIndex = indexElem.GetInt32();
var cardHolders = FindAllSortedByPosition<NCardHolder>(cardScreen);
if (cardIndex < 0 || cardIndex >= cardHolders.Count)
return Error($"Card index {cardIndex} out of range (screen has {cardHolders.Count} cards)");
var holder = cardHolders[cardIndex];
string cardName = SafeGetText(() => holder.CardModel?.Title) ?? "unknown";
holder.EmitSignal(NCardHolder.SignalName.Pressed, holder);
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Selecting card: {cardName}"
};
}
private static Dictionary<string, object?> ExecuteSkipCardReward()
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NCardRewardSelectionScreen cardScreen)
return Error("Card reward selection screen is not open");
var altButtons = FindAll<NCardRewardAlternativeButton>(cardScreen);
if (altButtons.Count == 0)
return Error("No skip option available on this card reward");
altButtons[0].ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Skipping card reward"
};
}
private static Dictionary<string, object?> ExecuteProceed()
{
// Try rewards overlay
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is NRewardsScreen rewardsScreen)
{
var btn = FindFirst<NProceedButton>(rewardsScreen);
if (btn is { IsEnabled: true })
{
btn.ForceClick();
return new Dictionary<string, object?> { ["status"] = "ok", ["message"] = "Proceeding from rewards" };
}
}
// Try rest site
if (NRestSiteRoom.Instance is { } restRoom && restRoom.ProceedButton.IsEnabled)
{
restRoom.ProceedButton.ForceClick();
return new Dictionary<string, object?> { ["status"] = "ok", ["message"] = "Proceeding from rest site" };
}
// Try merchant - close inventory first if open, then proceed
if (NMerchantRoom.Instance is { } merchRoom)
{
if (merchRoom.Inventory?.IsOpen == true)
{
var backBtn = FindFirst<NBackButton>(merchRoom);
if (backBtn is { IsEnabled: true })
backBtn.ForceClick();
}
if (merchRoom.ProceedButton.IsEnabled)
{
merchRoom.ProceedButton.ForceClick();
return new Dictionary<string, object?> { ["status"] = "ok", ["message"] = "Proceeding from shop" };
}
}
// Try fake merchant - close inventory first if open, then proceed
if (NEventRoom.Instance is { } evtRoom)
{
var fmNode = FindFirst<NFakeMerchant>(evtRoom);
if (fmNode != null)
{
var fmInventory = FindFirst<NMerchantInventory>(fmNode);
if (fmInventory is { IsOpen: true })
{
var backBtn = FindFirst<NBackButton>(fmNode);
if (backBtn is { IsEnabled: true })
backBtn.ForceClick();
}
var proceedBtn = FindFirst<NProceedButton>(fmNode);
if (proceedBtn is { IsEnabled: true })
{
proceedBtn.ForceClick();
return new Dictionary<string, object?> { ["status"] = "ok", ["message"] = "Proceeding from fake merchant" };
}
}
}
// Try treasure room
var treasureUI = FindFirst<NTreasureRoom>(
((Godot.SceneTree)Godot.Engine.GetMainLoop()).Root);
if (treasureUI != null && treasureUI.ProceedButton.IsEnabled)
{
treasureUI.ProceedButton.ForceClick();
return new Dictionary<string, object?> { ["status"] = "ok", ["message"] = "Proceeding from treasure room" };
}
return Error("No proceed button available or enabled");
}
private static Dictionary<string, object?> ExecuteSelectCard(Dictionary<string, JsonElement> data)
{
var overlay = NOverlayStack.Instance?.Peek();
if (!data.TryGetValue("index", out var indexElem))
return Error("Missing 'index' (card index in the grid)");
int index = indexElem.GetInt32();
if (overlay is NCardGridSelectionScreen gridScreen)
{
var grid = FindFirst<NCardGrid>(gridScreen);
if (grid == null)
return Error("Card grid not found in selection screen");
var holders = FindAllSortedByPosition<NGridCardHolder>(gridScreen);
if (index < 0 || index >= holders.Count)
return Error($"Card index {index} out of range ({holders.Count} cards available)");
var holder = holders[index];
string cardName = SafeGetText(() => holder.CardModel?.Title) ?? "unknown";
grid.EmitSignal(NCardGrid.SignalName.HolderPressed, holder);
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Toggling card selection: {cardName}"
};
}
else if (overlay is NChooseACardSelectionScreen chooseScreen)
{
var holders = FindAllSortedByPosition<NGridCardHolder>(chooseScreen);
if (index < 0 || index >= holders.Count)
return Error($"Card index {index} out of range ({holders.Count} cards available)");
var holder = holders[index];
string cardName = SafeGetText(() => holder.CardModel?.Title) ?? "unknown";
holder.EmitSignal(NCardHolder.SignalName.Pressed, holder);
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Choosing card: {cardName}"
};
}
return Error("No card selection screen is open");
}
private static Dictionary<string, object?> ExecuteConfirmSelection()
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is NChooseACardSelectionScreen)
return Error("Choose-a-card screen requires no confirmation - use select_card(index) to pick directly");
if (overlay is not NCardGridSelectionScreen screen)
return Error("No card selection screen is open");
// Check all preview containers (upgrade uses UpgradeSinglePreviewContainer / UpgradeMultiPreviewContainer,
// NDeckCardSelectScreen uses PreviewContainer with %PreviewConfirm)
foreach (var containerName in new[] { "%UpgradeSinglePreviewContainer", "%UpgradeMultiPreviewContainer", "%PreviewContainer" })
{
var container = screen.GetNodeOrNull<Godot.Control>(containerName);
if (container?.Visible == true)
{
var confirm = container.GetNodeOrNull<NConfirmButton>("Confirm")
?? container.GetNodeOrNull<NConfirmButton>("%PreviewConfirm");
if (confirm is { IsEnabled: true })
{
confirm.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Confirming selection from preview"
};
}
}
}
// Try main confirm button
var mainConfirm = screen.GetNodeOrNull<NConfirmButton>("Confirm")
?? screen.GetNodeOrNull<NConfirmButton>("%Confirm");
if (mainConfirm is { IsEnabled: true })
{
mainConfirm.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Confirming selection"
};
}
// Fallback: find ANY enabled NConfirmButton in the screen tree.
// Covers NCardGridSelectionScreen subclasses (like NDeckEnchantSelectScreen)
// whose confirm button isn't in any of the known container paths above.
var allConfirmButtons = FindAll<NConfirmButton>(screen);
foreach (var btn in allConfirmButtons)
{
if (btn.IsEnabled && btn.IsVisibleInTree())
{
btn.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Confirming selection"
};
}
}
return Error("No confirm button is currently enabled - select more cards first");
}
private static Dictionary<string, object?> ExecuteCancelSelection()
{
var overlay = NOverlayStack.Instance?.Peek();
// Handle choose-a-card screen (skip button)
if (overlay is NChooseACardSelectionScreen chooseScreen)
{
var skipButton = chooseScreen.GetNodeOrNull<NClickableControl>("SkipButton");
if (skipButton is { IsEnabled: true })
{
skipButton.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Skipping card choice"
};
}
return Error("No skip option available - a card must be chosen");
}
if (overlay is not NCardGridSelectionScreen screen)
return Error("No card selection screen is open");
// If preview is showing, cancel back to selection
foreach (var containerName in new[] { "%UpgradeSinglePreviewContainer", "%UpgradeMultiPreviewContainer", "%PreviewContainer" })
{
var container = screen.GetNodeOrNull<Godot.Control>(containerName);
if (container?.Visible == true)
{
var cancelBtn = container.GetNodeOrNull<NBackButton>("Cancel")
?? container.GetNodeOrNull<NBackButton>("%PreviewCancel");
if (cancelBtn is { IsEnabled: true })
{
cancelBtn.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Cancelling preview - returning to card selection"
};
}
}
}
// Close the screen entirely
var closeButton = screen.GetNodeOrNull<NBackButton>("%Close");
if (closeButton is { IsEnabled: true })
{
closeButton.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Closing card selection screen"
};
}
return Error("No cancel/close button is currently enabled - selection may be mandatory");
}
private static Dictionary<string, object?> ExecuteSelectBundle(Dictionary<string, JsonElement> data)
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NChooseABundleSelectionScreen screen)
return Error("No bundle selection screen is open");
if (!data.TryGetValue("index", out var indexElem))
return Error("Missing 'index' (bundle index)");
int index = indexElem.GetInt32();
var previewContainer = screen.GetNodeOrNull<Godot.Control>("%BundlePreviewContainer");
if (previewContainer?.Visible == true)
return Error("A bundle preview is already open - confirm or cancel it first");
var bundles = FindAll<NCardBundle>(screen);
if (index < 0 || index >= bundles.Count)
return Error($"Bundle index {index} out of range ({bundles.Count} bundles available)");
bundles[index].Hitbox.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Selecting bundle {index}"
};
}
private static Dictionary<string, object?> ExecuteConfirmBundleSelection()
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NChooseABundleSelectionScreen screen)
return Error("No bundle selection screen is open");
var confirmButton = screen.GetNodeOrNull<NConfirmButton>("%Confirm");
if (confirmButton is not { IsEnabled: true })
return Error("Bundle confirm button is not enabled");
confirmButton.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Confirming bundle selection"
};
}
private static Dictionary<string, object?> ExecuteCancelBundleSelection()
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NChooseABundleSelectionScreen screen)
return Error("No bundle selection screen is open");
var cancelButton = screen.GetNodeOrNull<NBackButton>("%Cancel");
if (cancelButton is not { IsEnabled: true })
return Error("Bundle cancel button is not enabled");
cancelButton.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Cancelling bundle selection"
};
}
private static Dictionary<string, object?> ExecuteCombatSelectCard(Dictionary<string, JsonElement> data)
{
var hand = NPlayerHand.Instance;
if (hand == null || !hand.IsInCardSelection)
return Error("No in-combat card selection is active");
if (!data.TryGetValue("card_index", out var indexElem))
return Error("Missing 'card_index' (index of the card in hand)");
int index = indexElem.GetInt32();
var holders = hand.ActiveHolders;
if (index < 0 || index >= holders.Count)
return Error($"Card index {index} out of range ({holders.Count} selectable cards)");
var holder = holders[index];
string cardName = SafeGetText(() => holder.CardModel?.Title) ?? "unknown";
// Emit the Pressed signal - same path the game UI uses
holder.EmitSignal(NCardHolder.SignalName.Pressed, holder);
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Selecting card from hand: {cardName}"
};
}
private static Dictionary<string, object?> ExecuteCombatConfirmSelection()
{
var hand = NPlayerHand.Instance;
if (hand == null || !hand.IsInCardSelection)
return Error("No in-combat card selection is active");
var confirmBtn = hand.GetNodeOrNull<NConfirmButton>("%SelectModeConfirmButton");
if (confirmBtn == null || !confirmBtn.IsEnabled)
return Error("Confirm button is not enabled - select more cards first");
confirmBtn.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Confirming combat card selection"
};
}
private static Dictionary<string, object?> ExecuteSelectRelic(Dictionary<string, JsonElement> data)
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NChooseARelicSelection screen)
return Error("No relic selection screen is open");
if (!data.TryGetValue("index", out var indexElem))
return Error("Missing 'index' (relic index)");
int index = indexElem.GetInt32();
var holders = FindAll<NRelicBasicHolder>(screen);
if (index < 0 || index >= holders.Count)
return Error($"Relic index {index} out of range ({holders.Count} relics available)");
var holder = holders[index];
string relicName = SafeGetText(() => holder.Relic?.Model?.Title) ?? "unknown";
holder.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Selecting relic: {relicName}"
};
}
private static Dictionary<string, object?> ExecuteSkipRelicSelection()
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NChooseARelicSelection screen)
return Error("No relic selection screen is open");
var skipButton = screen.GetNodeOrNull<NClickableControl>("SkipButton");
if (skipButton is not { IsEnabled: true })
return Error("No skip option available");
skipButton.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = "Skipping relic selection"
};
}
private static Dictionary<string, object?> ExecuteClaimTreasureRelic(Dictionary<string, JsonElement> data)
{
var treasureUI = FindFirst<NTreasureRoom>(
((Godot.SceneTree)Godot.Engine.GetMainLoop()).Root);
if (treasureUI == null)
return Error("Treasure room is not open");
var relicCollection = treasureUI.GetNodeOrNull<NTreasureRoomRelicCollection>("%RelicCollection");
if (relicCollection?.Visible != true)
return Error("Relic collection is not visible - chest may not be opened yet");
if (!data.TryGetValue("index", out var indexElem))
return Error("Missing 'index' (relic index)");
int index = indexElem.GetInt32();
var holders = FindAll<NTreasureRoomRelicHolder>(relicCollection)
.Where(h => h.IsEnabled && h.Visible)
.ToList();
if (index < 0 || index >= holders.Count)
return Error($"Relic index {index} out of range ({holders.Count} relics available)");
var holder = holders[index];
string relicName = SafeGetText(() => holder.Relic?.Model?.Title) ?? "unknown";
holder.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Claiming treasure relic: {relicName}"
};
}
private static Dictionary<string, object?> ExecuteCrystalSphereSetTool(Dictionary<string, JsonElement> data)
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NCrystalSphereScreen screen)
return Error("Crystal Sphere screen is not open");
if (!data.TryGetValue("tool", out var toolElem))
return Error("Missing 'tool' (expected 'big' or 'small')");
string tool = toolElem.GetString() ?? "";
var button = tool switch
{
"big" => screen.GetNodeOrNull<NClickableControl>("%BigDivinationButton"),
"small" => screen.GetNodeOrNull<NClickableControl>("%SmallDivinationButton"),
_ => null
};
if (button == null)
return Error($"Unknown Crystal Sphere tool: {tool}");
if (!button.Visible || !button.IsEnabled)
return Error($"Crystal Sphere tool '{tool}' is not available");
button.ForceClick();
return new Dictionary<string, object?>
{
["status"] = "ok",
["message"] = $"Setting Crystal Sphere tool to {tool}"
};
}
private static Dictionary<string, object?> ExecuteCrystalSphereClickCell(Dictionary<string, JsonElement> data)
{
var overlay = NOverlayStack.Instance?.Peek();
if (overlay is not NCrystalSphereScreen screen)