-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultRoutine.cs
1439 lines (1257 loc) · 59.7 KB
/
DefaultRoutine.cs
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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Markup;
using Buddy.Coroutines;
using HREngine.Bots;
using IronPython.Modules;
using log4net;
using Microsoft.Scripting.Hosting;
using Triton.Bot;
using Triton.Common;
using Triton.Game;
using Triton.Game.Data;
//!CompilerOption|AddRef|IronPython.dll
//!CompilerOption|AddRef|IronPython.Modules.dll
//!CompilerOption|AddRef|Microsoft.Scripting.dll
//!CompilerOption|AddRef|Microsoft.Dynamic.dll
//!CompilerOption|AddRef|Microsoft.Scripting.Metadata.dll
using Triton.Game.Mapping;
using Logger = Triton.Common.LogUtilities.Logger;
namespace HREngine.Bots
{
public class DefaultRoutine : IRoutine
{
private static readonly ILog Log = Logger.GetLoggerInstanceForType();
private readonly ScriptManager _scriptManager = new ScriptManager();
private readonly List<Tuple<string, string>> _mulliganRules = new List<Tuple<string, string>>();
private int dirtyTargetSource = -1;
private int stopAfterWins = 30;
private int concedeLvl = 5; // the rank, till you want to concede
private int dirtytarget = -1;
private int dirtychoice = -1;
private string choiceCardId = "";
DateTime starttime = DateTime.Now;
bool enemyConcede = false;
public bool learnmode = false;
public bool printlearnmode = true;
Silverfish sf = Silverfish.Instance;
//uncomment the desired option, or leave it as is to select via the interface
Behavior behave = new BehaviorControl();
//Behavior behave = new BehaviorRush();
public DefaultRoutine()
{
// Global rules. Never keep a 4+ minion, unless it's Bolvar Fordragon (paladin).
_mulliganRules.Add(new Tuple<string, string>("True", "card.Entity.Cost >= 4 and card.Entity.Id != \"GVG_063\""));
// Never keep Tracking.
_mulliganRules.Add(new Tuple<string, string>("mulliganData.UserClass == TAG_CLASS.HUNTER", "card.Entity.Id == \"DS1_184\""));
// Example rule for self.
//_mulliganRules.Add(new Tuple<string, string>("mulliganData.UserClass == TAG_CLASS.MAGE", "card.Cost >= 5"));
// Example rule for opponents.
//_mulliganRules.Add(new Tuple<string, string>("mulliganData.OpponentClass == TAG_CLASS.MAGE", "card.Cost >= 3"));
// Example rule for matchups.
//_mulliganRules.Add(new Tuple<string, string>("mulliganData.userClass == TAG_CLASS.HUNTER && mulliganData.OpponentClass == TAG_CLASS.DRUID", "card.Cost >= 2"));
bool concede = false;
bool teststuff = false;
// set to true, to run a testfile (requires test.txt file in folder where _cardDB.txt file is located)
bool printstuff = false; // if true, the best board of the tested file is printet stepp by stepp
Helpfunctions.Instance.ErrorLog("----------------------------");
Helpfunctions.Instance.ErrorLog("you are running uai V" + Silverfish.Instance.versionnumber);
Helpfunctions.Instance.ErrorLog("----------------------------");
if (teststuff)
{
Ai.Instance.autoTester(printstuff);
}
}
#region Scripting
private const string BoilerPlateExecute = @"
import sys
sys.stdout=ioproxy
def Execute():
return bool({0})";
public delegate void RegisterScriptVariableDelegate(ScriptScope scope);
public bool GetCondition(string expression, IEnumerable<RegisterScriptVariableDelegate> variables)
{
var code = string.Format(BoilerPlateExecute, expression);
var scope = _scriptManager.Scope;
var scriptSource = _scriptManager.Engine.CreateScriptSourceFromString(code);
scope.SetVariable("ioproxy", _scriptManager.IoProxy);
foreach (var variable in variables)
{
variable(scope);
}
scriptSource.Execute(scope);
return scope.GetVariable<Func<bool>>("Execute")();
}
public bool VerifyCondition(string expression,
IEnumerable<string> variables, out Exception ex)
{
ex = null;
try
{
var code = string.Format(BoilerPlateExecute, expression);
var scope = _scriptManager.Scope;
var scriptSource = _scriptManager.Engine.CreateScriptSourceFromString(code);
scope.SetVariable("ioproxy", _scriptManager.IoProxy);
foreach (var variable in variables)
{
scope.SetVariable(variable, new object());
}
scriptSource.Compile();
}
catch (Exception e)
{
ex = e;
return false;
}
return true;
}
#endregion
#region Implementation of IAuthored
/// <summary> The name of the routine. </summary>
public string Name
{
get { return "DefaultRoutine"; }
}
/// <summary> The description of the routine. </summary>
public string Description
{
get { return "The default routine for Hearthbuddy."; }
}
/// <summary>The author of this routine.</summary>
public string Author
{
get { return "Bossland GmbH"; }
}
/// <summary>The version of this routine.</summary>
public string Version
{
get { return "0.0.1.1"; }
}
#endregion
#region Implementation of IBase
/// <summary>Initializes this routine.</summary>
public void Initialize()
{
_scriptManager.Initialize(null,
new List<string>
{
"Triton.Game",
"Triton.Bot",
"Triton.Common",
"Triton.Game.Mapping",
"Triton.Game.Abstraction"
});
}
/// <summary>Deinitializes this routine.</summary>
public void Deinitialize()
{
_scriptManager.Deinitialize();
}
#endregion
#region Implementation of IRunnable
/// <summary> The routine start callback. Do any initialization here. </summary>
public void Start()
{
GameEventManager.NewGame += GameEventManagerOnNewGame;
GameEventManager.GameOver += GameEventManagerOnGameOver;
GameEventManager.QuestUpdate += GameEventManagerOnQuestUpdate;
GameEventManager.ArenaRewards += GameEventManagerOnArenaRewards;
if (Hrtprozis.Instance.settings == null)
{
Hrtprozis.Instance.setInstances();
ComboBreaker.Instance.setInstances();
PenalityManager.Instance.setInstances();
}
behave = sf.getBehaviorByName(DefaultRoutineSettings.Instance.DefaultBehavior);
foreach (var tuple in _mulliganRules)
{
Exception ex;
if (
!VerifyCondition(tuple.Item1, new List<string> {"mulliganData"}, out ex))
{
Log.ErrorFormat("[Start] There is an error with a mulligan execution condition [{1}]: {0}.", ex,
tuple.Item1);
BotManager.Stop();
}
if (
!VerifyCondition(tuple.Item2, new List<string> {"mulliganData", "card"},
out ex))
{
Log.ErrorFormat("[Start] There is an error with a mulligan card condition [{1}]: {0}.", ex,
tuple.Item2);
BotManager.Stop();
}
}
}
/// <summary> The routine tick callback. Do any update logic here. </summary>
public void Tick()
{
}
/// <summary> The routine stop callback. Do any pre-dispose cleanup here. </summary>
public void Stop()
{
GameEventManager.NewGame -= GameEventManagerOnNewGame;
GameEventManager.GameOver -= GameEventManagerOnGameOver;
GameEventManager.QuestUpdate -= GameEventManagerOnQuestUpdate;
GameEventManager.ArenaRewards -= GameEventManagerOnArenaRewards;
}
#endregion
#region Implementation of IConfigurable
/// <summary> The routine's settings control. This will be added to the Hearthbuddy Settings tab.</summary>
public UserControl Control
{
get
{
using (var fs = new FileStream(@"Routines\DefaultRoutine\SettingsGui.xaml", FileMode.Open))
{
var root = (UserControl) XamlReader.Load(fs);
// Your settings binding here.
// ArenaPreferredClass1
if (
!Wpf.SetupComboBoxItemsBinding(root, "ArenaPreferredClass1ComboBox", "AllClasses",
BindingMode.OneWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxItemsBinding failed for 'ArenaPreferredClass1ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
if (
!Wpf.SetupComboBoxSelectedItemBinding(root, "ArenaPreferredClass1ComboBox",
"ArenaPreferredClass1", BindingMode.TwoWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxSelectedItemBinding failed for 'ArenaPreferredClass1ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
// ArenaPreferredClass2
if (
!Wpf.SetupComboBoxItemsBinding(root, "ArenaPreferredClass2ComboBox", "AllClasses",
BindingMode.OneWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxItemsBinding failed for 'ArenaPreferredClass2ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
if (
!Wpf.SetupComboBoxSelectedItemBinding(root, "ArenaPreferredClass2ComboBox",
"ArenaPreferredClass2", BindingMode.TwoWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxSelectedItemBinding failed for 'ArenaPreferredClass2ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
// ArenaPreferredClass3
if (
!Wpf.SetupComboBoxItemsBinding(root, "ArenaPreferredClass3ComboBox", "AllClasses",
BindingMode.OneWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxItemsBinding failed for 'ArenaPreferredClass3ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
if (
!Wpf.SetupComboBoxSelectedItemBinding(root, "ArenaPreferredClass3ComboBox",
"ArenaPreferredClass3", BindingMode.TwoWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxSelectedItemBinding failed for 'ArenaPreferredClass3ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
// ArenaPreferredClass4
if (
!Wpf.SetupComboBoxItemsBinding(root, "ArenaPreferredClass4ComboBox", "AllClasses",
BindingMode.OneWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxItemsBinding failed for 'ArenaPreferredClass4ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
if (
!Wpf.SetupComboBoxSelectedItemBinding(root, "ArenaPreferredClass4ComboBox",
"ArenaPreferredClass4", BindingMode.TwoWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxSelectedItemBinding failed for 'ArenaPreferredClass4ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
// ArenaPreferredClass5
if (
!Wpf.SetupComboBoxItemsBinding(root, "ArenaPreferredClass5ComboBox", "AllClasses",
BindingMode.OneWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxItemsBinding failed for 'ArenaPreferredClass5ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
if (
!Wpf.SetupComboBoxSelectedItemBinding(root, "ArenaPreferredClass5ComboBox",
"ArenaPreferredClass5", BindingMode.TwoWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxSelectedItemBinding failed for 'ArenaPreferredClass5ComboBox'.");
throw new Exception("The SettingsControl could not be created.");
}
// defaultBehaviorComboBox1
if (
!Wpf.SetupComboBoxItemsBinding(root, "defaultBehaviorComboBox1", "AllBehav",
BindingMode.OneWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxItemsBinding failed for 'defaultBehaviorComboBox1'.");
throw new Exception("The SettingsControl could not be created.");
}
if (
!Wpf.SetupComboBoxSelectedItemBinding(root, "defaultBehaviorComboBox1",
"DefaultBehavior", BindingMode.TwoWay, DefaultRoutineSettings.Instance))
{
Log.DebugFormat(
"[SettingsControl] SetupComboBoxSelectedItemBinding failed for 'defaultBehaviorComboBox1'.");
throw new Exception("The SettingsControl could not be created.");
}
// Your settings event handlers here.
return root;
}
}
}
/// <summary>The settings object. This will be registered in the current configuration.</summary>
public JsonSettings Settings
{
get { return DefaultRoutineSettings.Instance; }
}
#endregion
#region Implementation of IRoutine
/// <summary>
/// Sends data to the routine with the associated name.
/// </summary>
/// <param name="name">The name of the configuration.</param>
/// <param name="param">The data passed for the configuration.</param>
public void SetConfiguration(string name, params object[] param)
{
}
/// <summary>
/// Requests data from the routine with the associated name.
/// </summary>
/// <param name="name">The name of the configuration.</param>
/// <returns>Data from the routine.</returns>
public object GetConfiguration(string name)
{
return null;
}
/// <summary>
/// The routine's coroutine logic to execute.
/// </summary>
/// <param name="type">The requested type of logic to execute.</param>
/// <param name="context">Data sent to the routine from the bot for the current logic.</param>
/// <returns>true if logic was executed to handle this type and false otherwise.</returns>
public async Task<bool> Logic(string type, object context)
{
// The bot is requesting mulligan logic.
if (type == "mulligan")
{
await MulliganLogic(context as MulliganData);
return true;
}
// The bot is requesting emote logic.
if (type == "emote")
{
await EmoteLogic(context as EmoteData);
return true;
}
// The bot is requesting our turn logic.
if (type == "our_turn")
{
await OurTurnLogic();
return true;
}
// The bot is requesting opponent turn logic.
if (type == "opponent_turn")
{
await OpponentTurnLogic();
return true;
}
// The bot is requesting our turn logic.
if (type == "our_turn_combat")
{
await OurTurnCombatLogic();
return true;
}
// The bot is requesting opponent turn logic.
if (type == "opponent_turn_combat")
{
await OpponentTurnCombatLogic();
return true;
}
// The bot is requesting arena draft logic.
if (type == "arena_draft")
{
await ArenaDraftLogic(context as ArenaDraftData);
return true;
}
// The bot is requesting quest handling logic.
if (type == "handle_quests")
{
await HandleQuestsLogic(context as QuestData);
return true;
}
// Whatever the current logic type is, this routine doesn't implement it.
return false;
}
#region Mulligan
private int RandomMulliganThinkTime()
{
var random = Client.Random;
var type = random.Next(0, 100)%4;
if (type == 0) return random.Next(800, 1200);
if (type == 1) return random.Next(1200, 2500);
if (type == 2) return random.Next(2500, 3700);
return 0;
}
/// <summary>
/// This task implements custom mulligan choosing logic for the bot.
/// The user is expected to set the Mulligans list elements to true/false
/// to signal to the bot which cards should/shouldn't be mulliganed.
/// This task should also implement humanization factors, such as hovering
/// over cards, or delaying randomly before returning, as the mulligan
/// process takes place as soon as the task completes.
/// </summary>
/// <param name="mulliganData">An object that contains relevant data for the mulligan process.</param>
/// <returns></returns>
public async Task MulliganLogic(MulliganData mulliganData)
{
Log.InfoFormat("[Mulligan] {0} vs {1}.", mulliganData.UserClass, mulliganData.OpponentClass);
var count = mulliganData.Cards.Count;
if (this.behave.BehaviorName() != DefaultRoutineSettings.Instance.DefaultBehavior)
{
behave = sf.getBehaviorByName(DefaultRoutineSettings.Instance.DefaultBehavior);
}
if (!Mulligan.Instance.getHoldList(mulliganData, this.behave))
{
for (var i = 0; i < count; i++)
{
var card = mulliganData.Cards[i];
try
{
foreach (var tuple in _mulliganRules)
{
if (GetCondition(tuple.Item1,
new List<RegisterScriptVariableDelegate>
{
scope => scope.SetVariable("mulliganData", mulliganData)
}))
{
if (GetCondition(tuple.Item2,
new List<RegisterScriptVariableDelegate>
{
scope => scope.SetVariable("mulliganData", mulliganData),
scope => scope.SetVariable("card", card)
}))
{
mulliganData.Mulligans[i] = true;
Log.InfoFormat(
"[Mulligan] {0} should be mulliganed because it matches the user's mulligan rule: [{1}] ({2}).",
card.Entity.Id, tuple.Item2, tuple.Item1);
}
}
else
{
Log.InfoFormat(
"[Mulligan] The mulligan execution check [{0}] is false, so the mulligan criteria [{1}] will not be evaluated.",
tuple.Item1, tuple.Item2);
}
}
}
catch (Exception ex)
{
Log.ErrorFormat("[Mulligan] An exception occurred: {0}.", ex);
BotManager.Stop();
return;
}
}
}
var thinkList = new List<KeyValuePair<int, int>>();
for (var i = 0; i < count; i++)
{
thinkList.Add(new KeyValuePair<int, int>(i%count, RandomMulliganThinkTime()));
}
thinkList.Shuffle();
foreach (var entry in thinkList)
{
var card = mulliganData.Cards[entry.Key];
Log.InfoFormat("[Mulligan] Now thinking about mulliganing {0} for {1} ms.", card.Entity.Id, entry.Value);
// Instant think time, skip the card.
if (entry.Value == 0)
continue;
Client.MouseOver(card.InteractPoint);
await Coroutine.Sleep(entry.Value);
}
}
#endregion
#region Emote
/// <summary>
/// This task implements player emote detection logic for the bot.
/// </summary>
/// <param name="data">An object that contains relevant data for the emote event.</param>
/// <returns></returns>
public async Task EmoteLogic(EmoteData data)
{
Log.InfoFormat("[Emote] The enemy is using the emote [{0}].", data.Emote);
if (data.Emote == EmoteType.GREETINGS)
{
}
else if (data.Emote == EmoteType.WELL_PLAYED)
{
}
else if (data.Emote == EmoteType.OOPS)
{
}
else if (data.Emote == EmoteType.THREATEN)
{
}
else if (data.Emote == EmoteType.THANKS)
{
}
else if (data.Emote == EmoteType.SORRY)
{
}
}
#endregion
#region Turn
public async Task OurTurnCombatLogic()
{
Log.InfoFormat("[OurTurnCombatLogic]");
await Coroutine.Sleep(555 + makeChoice());
switch (dirtychoice)
{
case 0: TritonHs.ChooseOneClickMiddle(); break;
case 1: TritonHs.ChooseOneClickLeft(); break;
case 2: TritonHs.ChooseOneClickRight(); break;
}
dirtychoice = -1;
await Coroutine.Sleep(555);
Silverfish.Instance.lastpf = null;
return;
}
public async Task OpponentTurnCombatLogic()
{
Log.Info("[OpponentTurnCombatLogic]");
}
/// <summary>
/// Under construction.
/// </summary>
/// <returns></returns>
public async Task OurTurnLogic()
{
if (this.behave.BehaviorName() != DefaultRoutineSettings.Instance.DefaultBehavior)
{
behave = sf.getBehaviorByName(DefaultRoutineSettings.Instance.DefaultBehavior);
Silverfish.Instance.lastpf = null;
}
if (this.learnmode && (TritonHs.IsInTargetMode() || TritonHs.IsInChoiceMode()))
{
await Coroutine.Sleep(50);
return;
}
if (TritonHs.IsInTargetMode())
{
if (dirtytarget >= 0)
{
Log.Info("targeting...");
HSCard source = null;
if (dirtyTargetSource == 9000) // 9000 = ability
{
source = TritonHs.OurHeroPowerCard;
}
else
{
source = getEntityWithNumber(dirtyTargetSource);
}
HSCard target = getEntityWithNumber(dirtytarget);
if (target == null)
{
Log.Error("target is null...");
TritonHs.CancelTargetingMode();
return;
}
dirtytarget = -1;
dirtyTargetSource = -1;
if (source == null) await TritonHs.DoTarget(target);
else await source.DoTarget(target);
await Coroutine.Sleep(555);
return;
}
Log.Error("target failure...");
TritonHs.CancelTargetingMode();
return;
}
if (TritonHs.IsInChoiceMode())
{
await Coroutine.Sleep(555 + makeChoice());
switch (dirtychoice)
{
case 0: TritonHs.ChooseOneClickMiddle(); break;
case 1: TritonHs.ChooseOneClickLeft(); break;
case 2: TritonHs.ChooseOneClickRight(); break;
}
dirtychoice = -1;
await Coroutine.Sleep(555);
return;
}
bool sleepRetry = false;
bool templearn = Silverfish.Instance.updateEverything(behave, 0, out sleepRetry);
if (sleepRetry)
{
Log.Error("[AI] Readiness error. Attempting recover...");
await Coroutine.Sleep(500);
templearn = Silverfish.Instance.updateEverything(behave, 1, out sleepRetry);
}
if (templearn == true) this.printlearnmode = true;
if (this.learnmode)
{
if (this.printlearnmode)
{
Ai.Instance.simmulateWholeTurnandPrint();
}
this.printlearnmode = false;
//do nothing
await Coroutine.Sleep(50);
return;
}
var moveTodo = Ai.Instance.bestmove;
if (moveTodo == null || moveTodo.actionType == actionEnum.endturn || Ai.Instance.bestmoveValue < -9999)
{
bool doEndTurn = false;
bool doConcede = false;
if (Ai.Instance.bestmoveValue > -10000) doEndTurn = true;
else if (HREngine.Bots.Settings.Instance.concedeMode != 0) doConcede = true;
else
{
if (new Playfield().ownHeroHasDirectLethal())
{
Playfield lastChancePl = Ai.Instance.bestplay;
bool lastChance = false;
if (lastChancePl.owncarddraw > 0)
{
foreach (Handmanager.Handcard hc in lastChancePl.owncards)
{
if (hc.card.name == CardDB.cardName.unknown) lastChance = true;
}
if (!lastChance) doConcede = true;
}
else doConcede = true;
if (doConcede)
{
foreach (Minion m in lastChancePl.ownMinions)
{
if (!m.playedThisTurn) continue;
switch (m.handcard.card.name)
{
case CardDB.cardName.cthun: lastChance = true; break;
case CardDB.cardName.nzoththecorruptor: lastChance = true; break;
case CardDB.cardName.yoggsaronhopesend: lastChance = true; break;
case CardDB.cardName.sirfinleymrrgglton: lastChance = true; break;
case CardDB.cardName.ragnarosthefirelord: if (lastChancePl.enemyHero.Hp < 9) lastChance = true; break;
case CardDB.cardName.barongeddon: if (lastChancePl.enemyHero.Hp < 3) lastChance = true; break;
}
}
}
if (lastChance) doConcede = false;
}
else if (moveTodo == null || moveTodo.actionType == actionEnum.endturn) doEndTurn = true;
}
if (doEndTurn)
{
Helpfunctions.Instance.ErrorLog("end turn");
await TritonHs.EndTurn();
return;
}
else if (doConcede)
{
Helpfunctions.Instance.ErrorLog("Lethal detected. Concede...");
Helpfunctions.Instance.logg("Concede... Lethal detected###############################################");
TritonHs.Concede(true);
return;
}
}
Helpfunctions.Instance.ErrorLog("play action");
if (moveTodo == null)
{
Helpfunctions.Instance.ErrorLog("moveTodo == null. EndTurn");
await TritonHs.EndTurn();
return;
}
//play the move#########################################################################
{
moveTodo.print();
//play a card form hand
if (moveTodo.actionType == actionEnum.playcard)
{
Questmanager.Instance.updatePlayedCardFromHand(moveTodo.card);
HSCard cardtoplay = getCardWithNumber(moveTodo.card.entity);
if (cardtoplay == null)
{
Helpfunctions.Instance.ErrorLog("[Tick] cardtoplay == null");
return;
}
if (moveTodo.target != null)
{
HSCard target = getEntityWithNumber(moveTodo.target.entitiyID);
if (target != null)
{
Helpfunctions.Instance.ErrorLog("play: " + cardtoplay.Name + " (" + cardtoplay.EntityId + ") target: " + target.Name + " (" + target.EntityId + ")");
Helpfunctions.Instance.logg("play: " + cardtoplay.Name + " (" + cardtoplay.EntityId + ") target: " + target.Name + " (" + target.EntityId + ") choice: " + moveTodo.druidchoice);
if (moveTodo.druidchoice >= 1)
{
dirtytarget = moveTodo.target.entitiyID;
dirtychoice = moveTodo.druidchoice; //1=leftcard, 2= rightcard
choiceCardId = moveTodo.card.card.cardIDenum.ToString();
}
//safe targeting stuff for hsbuddy
dirtyTargetSource = moveTodo.card.entity;
dirtytarget = moveTodo.target.entitiyID;
await cardtoplay.Pickup();
if (moveTodo.card.card.type == CardDB.cardtype.MOB)
{
await cardtoplay.UseAt(moveTodo.place);
}
else if (moveTodo.card.card.type == CardDB.cardtype.WEAPON) // This fixes perdition's blade
{
await cardtoplay.UseOn(target.Card);
}
else if (moveTodo.card.card.type == CardDB.cardtype.SPELL)
{
await cardtoplay.UseOn(target.Card);
}
else
{
await cardtoplay.UseOn(target.Card);
}
}
else
{
Helpfunctions.Instance.ErrorLog("[AI] Target is missing. Attempting recover...");
Helpfunctions.Instance.logg("[AI] Target " + moveTodo.target.entitiyID + "is missing. Attempting recover...");
}
await Coroutine.Sleep(500);
return;
}
Helpfunctions.Instance.ErrorLog("play: " + cardtoplay.Name + " (" + cardtoplay.EntityId + ") target nothing");
Helpfunctions.Instance.logg("play: " + cardtoplay.Name + " (" + cardtoplay.EntityId + ") choice: " + moveTodo.druidchoice);
if (moveTodo.druidchoice >= 1)
{
dirtychoice = moveTodo.druidchoice; //1=leftcard, 2= rightcard
choiceCardId = moveTodo.card.card.cardIDenum.ToString();
}
dirtyTargetSource = -1;
dirtytarget = -1;
await cardtoplay.Pickup();
if (moveTodo.card.card.type == CardDB.cardtype.MOB)
{
await cardtoplay.UseAt(moveTodo.place);
}
else
{
await cardtoplay.Use();
}
await Coroutine.Sleep(500);
return;
}
//attack with minion
if (moveTodo.actionType == actionEnum.attackWithMinion)
{
HSCard attacker = getEntityWithNumber(moveTodo.own.entitiyID);
HSCard target = getEntityWithNumber(moveTodo.target.entitiyID);
if (attacker != null)
{
if (target != null)
{
Helpfunctions.Instance.ErrorLog("minion attack: " + attacker.Name + " target: " + target.Name);
Helpfunctions.Instance.logg("minion attack: " + attacker.Name + " target: " + target.Name);
await attacker.DoAttack(target);
}
else
{
Helpfunctions.Instance.ErrorLog("[AI] Target is missing. Attempting recover...");
Helpfunctions.Instance.logg("[AI] Target " + moveTodo.target.entitiyID + "is missing. Attempting recover...");
}
}
else
{
Helpfunctions.Instance.ErrorLog("[AI] Attacker is missing. Attempting recover...");
Helpfunctions.Instance.logg("[AI] Attacker " + moveTodo.own.entitiyID + " is missing. Attempting recover...");
}
await Coroutine.Sleep(250);
return;
}
//attack with hero
if (moveTodo.actionType == actionEnum.attackWithHero)
{
HSCard attacker = getEntityWithNumber(moveTodo.own.entitiyID);
HSCard target = getEntityWithNumber(moveTodo.target.entitiyID);
if (attacker != null)
{
if (target != null)
{
dirtytarget = moveTodo.target.entitiyID;
Helpfunctions.Instance.ErrorLog("heroattack: " + attacker.Name + " target: " + target.Name);
Helpfunctions.Instance.logg("heroattack: " + attacker.Name + " target: " + target.Name);
//safe targeting stuff for hsbuddy
dirtyTargetSource = moveTodo.own.entitiyID;
dirtytarget = moveTodo.target.entitiyID;
await attacker.DoAttack(target);
}
else
{
Helpfunctions.Instance.ErrorLog("[AI] Target is missing. Attempting recover...");
Helpfunctions.Instance.logg("[AI] Target " + moveTodo.target.entitiyID + "is missing (H). Attempting recover...");
}
}
else
{
Helpfunctions.Instance.ErrorLog("[AI] Attacker is missing. Attempting recover...");
Helpfunctions.Instance.logg("[AI] Attacker " + moveTodo.own.entitiyID + " is missing (H). Attempting recover...");
}
await Coroutine.Sleep(250);
return;
}
//use ability
if (moveTodo.actionType == actionEnum.useHeroPower)
{
HSCard cardtoplay = TritonHs.OurHeroPowerCard;
if (moveTodo.target != null)
{
HSCard target = getEntityWithNumber(moveTodo.target.entitiyID);
if (target != null)
{
Helpfunctions.Instance.ErrorLog("use ablitiy: " + cardtoplay.Name + " target " + target.Name);
Helpfunctions.Instance.logg("use ablitiy: " + cardtoplay.Name + " target " + target.Name + (moveTodo.druidchoice > 0 ? (" choice: " + moveTodo.druidchoice) : ""));
if (moveTodo.druidchoice > 0)
{
dirtytarget = moveTodo.target.entitiyID;
dirtychoice = moveTodo.druidchoice; //1=leftcard, 2= rightcard
choiceCardId = moveTodo.card.card.cardIDenum.ToString();
}
dirtyTargetSource = 9000;
dirtytarget = moveTodo.target.entitiyID;
await cardtoplay.Pickup();
await cardtoplay.UseOn(target.Card);
}
else
{
Helpfunctions.Instance.ErrorLog("[AI] Target is missing. Attempting recover...");
Helpfunctions.Instance.logg("[AI] Target " + moveTodo.target.entitiyID + "is missing. Attempting recover...");
}
await Coroutine.Sleep(500);
}
else
{
Helpfunctions.Instance.ErrorLog("use ablitiy: " + cardtoplay.Name + " target nothing");
Helpfunctions.Instance.logg("use ablitiy: " + cardtoplay.Name + " target nothing" + (moveTodo.druidchoice > 0 ? (" choice: " + moveTodo.druidchoice) : ""));
if (moveTodo.druidchoice >= 1)
{
dirtychoice = moveTodo.druidchoice; //1=leftcard, 2= rightcard
choiceCardId = moveTodo.card.card.cardIDenum.ToString();
}
dirtyTargetSource = -1;
dirtytarget = -1;
await cardtoplay.Pickup();
}