forked from william-Dic/DeepBattler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeepBattler_code.txt
435 lines (407 loc) · 16.6 KB
/
DeepBattler_code.txt
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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using Hearthstone_Deck_Tracker.API;
using Hearthstone_Deck_Tracker.Hearthstone;
using Hearthstone_Deck_Tracker.Plugins;
using HearthDb.Enums;
using Hearthstone_Deck_Tracker.Enums;
using System.Windows.Controls;
namespace DeepBattlerPlugin
{
public class CardEntityInfo
{
public string Name { get; set; }
public int? Atk { get; set; }
public int? Health { get; set; }
public string Description { get; set; }
public string Race { get; set; }
public bool IsSpell { get; set; }
public int? Tier { get; set; }
}
public enum GamePhaseEnum
{
StartGame,
PlayerTurn,
OpponentTurn,
EndGame
}
public class DeepBattlerPlugin : IPlugin
{
private static readonly int[] BASE_TAVERN_UPGRADE_COSTS = { 5, 7, 8, 10, 10 };
private int? _currentTavernUpgradeCost = null;
public string Name => "DeepBattlerPlugin";
public string Description => "Track BG state with simplified logic";
public string ButtonText => "Do Nothing";
public string Author => "Guanming";
public Version Version => new Version(1, 0, 10);
public MenuItem MenuItem => null;
private bool _isPlayerTurnStarted = false;
private int _updateCounterSincePlayerTurn = 0;
private const int DelayBeforeFetchingTavern = 5;
public GamePhaseEnum CurrentPhase { get; private set; } = GamePhaseEnum.StartGame;
private int _lastHeroHealth;
private int _lastCoins;
private int _lastTavernTier = 1;
private CardEntityInfo[] _lastWarband = Array.Empty<CardEntityInfo>();
private CardEntityInfo[] _lastTavernEntities = Array.Empty<CardEntityInfo>();
private CardEntityInfo[] _lastHand = Array.Empty<CardEntityInfo>();
private readonly string _path = @"C:\Users\Guanming Wang\Desktop\DeepBattler\Agent\game_state.json";
private string _heroName = "Unknown Hero";
private int _playerHeroId = 0;
private bool _upgradedThisTurn = false;
public void OnLoad()
{
GameEvents.OnGameStart.Add(OnGameStart);
GameEvents.OnTurnStart.Add(OnTurnStart);
GameEvents.OnGameEnd.Add(OnGameEnd);
}
public void OnUnload() { }
public void OnButtonPress() { }
private bool IsInRecruitmentPhase()
{
var playerEntity = Core.Game.PlayerEntity;
if (playerEntity == null)
return false;
return Core.Game.Entities.Values.Any(e => e.GetTag(GameTag.IS_BACON_POOL_MINION) == 1);
}
public void OnUpdate()
{
if (_heroName == "Unknown Hero" || _heroName.StartsWith("BaconPHhero"))
UpdateHeroName();
if (_isPlayerTurnStarted)
{
if (!IsInRecruitmentPhase())
return;
_updateCounterSincePlayerTurn++;
if (_updateCounterSincePlayerTurn >= DelayBeforeFetchingTavern)
UpdateGameState();
}
else
{
UpdateGameState();
}
}
private void OnGameStart()
{
var playerHero = Core.Game.Player?.Hero;
if (playerHero != null)
{
var heroCardId = playerHero.CardId;
if (heroCardId != null && HearthDb.Cards.All.TryGetValue(heroCardId, out var foundHeroCard))
_heroName = foundHeroCard?.Name ?? "Unknown Hero";
else
_heroName = "Unknown Hero";
_playerHeroId = playerHero.Id;
}
_lastTavernTier = 1;
CurrentPhase = GamePhaseEnum.StartGame;
_currentTavernUpgradeCost = null;
UpdateGameState(true);
}
private void OnGameEnd()
{
CurrentPhase = GamePhaseEnum.EndGame;
UpdateGameState();
ResetInternalState();
}
private void ResetInternalState()
{
_currentTavernUpgradeCost = null;
_lastHeroHealth = 0;
_lastCoins = 0;
_lastTavernTier = 1;
_lastWarband = Array.Empty<CardEntityInfo>();
_lastTavernEntities = Array.Empty<CardEntityInfo>();
_lastHand = Array.Empty<CardEntityInfo>();
_heroName = "Unknown Hero";
_playerHeroId = 0;
_isPlayerTurnStarted = false;
_upgradedThisTurn = false;
}
private void OnTurnStart(ActivePlayer player)
{
if (player == ActivePlayer.Player)
{
if (_currentTavernUpgradeCost == null)
{
_currentTavernUpgradeCost = BASE_TAVERN_UPGRADE_COSTS[0];
}
else
{
_currentTavernUpgradeCost = Math.Max(0, _currentTavernUpgradeCost.Value - 1);
}
_upgradedThisTurn = false;
CurrentPhase = GamePhaseEnum.PlayerTurn;
_isPlayerTurnStarted = true;
_updateCounterSincePlayerTurn = 0;
UpdateGameState();
}
var playerEntity = Core.Game.PlayerEntity;
if (playerEntity == null)
return;
if (player == ActivePlayer.Opponent)
{
CurrentPhase = GamePhaseEnum.OpponentTurn;
UpdateGameState();
}
}
public void PlayerUpgradedTavern(int newTier)
{
_upgradedThisTurn = true;
_lastTavernTier = newTier;
if (newTier < 6)
{
_currentTavernUpgradeCost = BASE_TAVERN_UPGRADE_COSTS[newTier - 1];
}
else
{
_currentTavernUpgradeCost = null;
}
UpdateGameState(true);
}
private void UpdateHeroName()
{
var playerHero = Core.Game.Player?.Hero;
if (playerHero != null)
{
var heroCardId = playerHero.CardId;
if (heroCardId != null && HearthDb.Cards.All.TryGetValue(heroCardId, out var foundHeroCard))
{
if (!string.IsNullOrEmpty(foundHeroCard.Name) && !foundHeroCard.Name.StartsWith("BaconPHhero"))
{
_heroName = foundHeroCard.Name;
UpdateGameState();
}
}
}
}
private void UpdateGameState(bool forceWrite = false)
{
var heroEntity = Core.Game.Player?.Hero;
var playerEntity = Core.Game.PlayerEntity;
if (heroEntity == null || playerEntity == null)
return;
var playerId = playerEntity.GetTag(GameTag.PLAYER_ID);
var heroPowerEntity = Core.Game.Entities.Values.FirstOrDefault(e =>
e.GetTag(GameTag.CONTROLLER) == playerId &&
e.GetTag(GameTag.CARDTYPE) == (int)CardType.HERO_POWER);
var baseHealth = heroEntity.GetTag(GameTag.HEALTH);
var damage = heroEntity.GetTag(GameTag.DAMAGE);
var actualHealth = baseHealth - damage;
var coins = playerEntity.GetTag(GameTag.RESOURCES);
var coinsUsed = playerEntity.GetTag(GameTag.RESOURCES_USED);
var availableCoins = coins - coinsUsed;
var tavernTier = playerEntity.GetTag(GameTag.PLAYER_TECH_LEVEL);
if (tavernTier > _lastTavernTier)
{
PlayerUpgradedTavern(tavernTier);
return;
}
var currentWarband = ExtractWarband();
var currentHand = ExtractHand();
var currentTavernEntities = ExtractTavernEntitiesWithDetails();
var heroPowerDescription = CleanCardText(heroPowerEntity?.Card?.Text);
var heroPowerCost = heroPowerEntity?.GetTag(GameTag.COST) ?? 0;
var tavernUpgradeCostStr = "None";
if (CurrentPhase == GamePhaseEnum.StartGame)
{
tavernUpgradeCostStr = "undefined";
}
else if (tavernTier < 6)
{
if (_currentTavernUpgradeCost == null && CurrentPhase == GamePhaseEnum.PlayerTurn)
{
_currentTavernUpgradeCost = BASE_TAVERN_UPGRADE_COSTS[0];
}
if (_currentTavernUpgradeCost.HasValue)
{
tavernUpgradeCostStr = _currentTavernUpgradeCost.Value.ToString();
}
}
bool changed = forceWrite
|| actualHealth != _lastHeroHealth
|| availableCoins != _lastCoins
|| tavernTier != _lastTavernTier
|| !EntitiesEqual(currentWarband, _lastWarband)
|| !EntitiesEqual(currentTavernEntities, _lastTavernEntities)
|| !EntitiesEqual(currentHand, _lastHand);
if (changed)
{
_lastHeroHealth = actualHealth;
_lastCoins = availableCoins;
_lastWarband = currentWarband;
_lastTavernEntities = currentTavernEntities;
_lastHand = currentHand;
var json = "{";
json += $"\"HeroName\":\"{EscapeJson(_heroName)}\",";
json += $"\"HeroHealth\":{actualHealth},";
json += $"\"Coins\":{availableCoins},";
json += $"\"TavernUpgradeCost\":\"{tavernUpgradeCostStr}\",";
json += $"\"HeroPowerDescription\":\"{EscapeJson(heroPowerDescription)}\",";
json += $"\"CurrentTavernTier\":{tavernTier},";
json += $"\"HeroPowerCost\":{heroPowerCost},";
json += "\"Warband\":" + EntitiesToJson(currentWarband) + ",";
json += "\"Hand\":" + EntitiesToJsonNoRace(currentHand) + ",";
json += "\"TavernEntities\":" + EntitiesToJson(currentTavernEntities);
json += "}";
try { File.WriteAllText(_path, json); } catch (Exception) { }
}
}
private CardEntityInfo[] ExtractWarband()
{
return (Core.Game.Player?.Board?
.Where(e => e.GetTag(GameTag.ZONE) == (int)Zone.PLAY &&
e.GetTag(GameTag.CARDTYPE) == (int)CardType.MINION &&
e.Card != null)
.Select(e => new CardEntityInfo
{
Name = e.Card.Name,
Atk = e.GetTag(GameTag.ATK),
Health = e.GetTag(GameTag.HEALTH) - e.GetTag(GameTag.DAMAGE),
Description = CleanCardText(e.Card.Text),
Race = GetRaceString(e.Card.Race),
IsSpell = false,
Tier = e.Card?.TechLevel
})
.ToArray()) ?? Array.Empty<CardEntityInfo>();
}
private CardEntityInfo[] ExtractHand()
{
return (Core.Game.Player?.Hand?
.Where(e =>
e.Card != null
&& !string.IsNullOrEmpty(e.Card.Name)
)
.Select(e => new CardEntityInfo
{
Name = e.Card.Name,
Description = CleanCardText(e.Card.Text),
IsSpell = e.GetTag(GameTag.CARDTYPE) == (int)CardType.BATTLEGROUND_SPELL
})
.ToArray()) ?? Array.Empty<CardEntityInfo>();
}
private CardEntityInfo[] ExtractTavernEntitiesWithDetails()
{
if (Core.Game.PlayerEntity == null)
return Array.Empty<CardEntityInfo>();
var playerId = Core.Game.PlayerEntity.GetTag(GameTag.PLAYER_ID);
var tavernMinions = Core.Game.Entities.Values
.Where(e => e.GetTag(GameTag.ZONE) == (int)Zone.PLAY &&
e.Id != _playerHeroId &&
e.Card != null &&
!string.IsNullOrEmpty(e.Card.Name) &&
e.GetTag(GameTag.IS_BACON_POOL_MINION) == 1 &&
e.GetTag(GameTag.CONTROLLER) != playerId &&
e.GetTag(GameTag.CARDTYPE) == (int)CardType.MINION)
.Select(e => new CardEntityInfo
{
Name = e.Card.Name,
Atk = e.GetTag(GameTag.ATK),
Health = e.GetTag(GameTag.HEALTH) - e.GetTag(GameTag.DAMAGE),
Description = CleanCardText(e.Card.Text),
Race = GetRaceString(e.Card.Race),
IsSpell = false,
Tier = e.Card?.TechLevel
});
var tavernSpells = Core.Game.Entities.Values
.Where(e => e.GetTag(GameTag.CARDTYPE) == (int)CardType.BATTLEGROUND_SPELL &&
e.GetTag(GameTag.ZONE) == (int)Zone.PLAY &&
e.Card != null &&
!string.IsNullOrEmpty(e.Card.Name))
.Select(e => new CardEntityInfo
{
Name = e.Card.Name,
Description = CleanCardText(e.Card.Text),
Race = "Tavern Spell",
IsSpell = true,
Tier = e.Card?.TechLevel
});
return tavernMinions.Concat(tavernSpells).ToArray();
}
private bool EntitiesEqual(CardEntityInfo[] arr1, CardEntityInfo[] arr2)
{
if (arr1.Length != arr2.Length)
return false;
for (int i = 0; i < arr1.Length; i++)
{
if (arr1[i].Name != arr2[i].Name ||
arr1[i].Description != arr2[i].Description ||
arr1[i].Race != arr2[i].Race ||
arr1[i].IsSpell != arr2[i].IsSpell ||
arr1[i].Tier != arr2[i].Tier)
return false;
if (!arr1[i].IsSpell && (arr1[i].Atk != arr2[i].Atk || arr1[i].Health != arr2[i].Health))
return false;
}
return true;
}
private string EntitiesToJson(CardEntityInfo[] entities)
{
var parts = new List<string>();
foreach (var e in entities)
{
if (e.IsSpell)
parts.Add(SpellToJson(e));
else
parts.Add(MinionToJson(e));
}
return "[" + string.Join(",", parts) + "]";
}
private string MinionToJson(CardEntityInfo entity)
{
return "{" +
$"\"Name\":\"{EscapeJson(entity.Name)}\"," +
$"\"Atk\":{entity.Atk}," +
$"\"Health\":{entity.Health}," +
$"\"Description\":\"{EscapeJson(entity.Description)}\"," +
$"\"Race\":\"{EscapeJson(entity.Race)}\"," +
$"\"Tier\":{entity.Tier}" +
"}";
}
private string SpellToJson(CardEntityInfo entity)
{
return "{" +
$"\"Name\":\"{EscapeJson(entity.Name)}\"," +
$"\"Description\":\"{EscapeJson(entity.Description)}\"," +
$"\"Race\":\"Tavern Spell\"," +
$"\"Tier\":{entity.Tier}" +
"}";
}
private string EntitiesToJsonNoRace(CardEntityInfo[] entities)
{
var parts = new List<string>();
foreach (var e in entities)
{
var part = "{";
part += $"\"Name\":\"{EscapeJson(e.Name)}\",";
part += $"\"Description\":\"{EscapeJson(e.Description)}\"";
part += "}";
parts.Add(part);
}
return "[" + string.Join(",", parts) + "]";
}
private string EscapeJson(string value)
{
if (string.IsNullOrEmpty(value))
return "";
return value
.Replace("\\", "\\\\")
.Replace("\"", "\\\"")
.Replace("\n", "\\n")
.Replace("\r", "\\r")
.Replace("\t", "\\t");
}
private string CleanCardText(string text)
{
if (string.IsNullOrEmpty(text))
return "";
return text.Replace("\r", " ").Replace("\n", " ").Trim();
}
private string GetRaceString(string race)
{
return string.IsNullOrEmpty(race) ? "" : race;
}
}
}