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
|
part of 'game_state.dart';
// ignore_for_file: library_private_types_in_public_api
class LootCard {
// ignore: prefer-match-file-name, file contains multiple loot deck state types
static const int _kHighCharCount = 4;
static const int _kLowCharCount = 2;
final String gfx;
final int id;
final LootBaseValue baseValue;
final LootType lootType;
String owner = "";
int _enhanced = 0;
int get enhanced => _enhanced;
LootCard({
required this.id,
required this.lootType,
required this.baseValue,
required enhanced,
required this.gfx,
}) {
_enhanced = enhanced;
}
Map<String, dynamic> toJson() => {
'gfx': gfx,
'owner': owner,
'id': id,
'enhanced': enhanced,
'baseValue': baseValue.index,
'lootType': lootType.index,
};
@override
String toString() => json.encode(toJson());
int? getValue() {
int value = 1;
if (lootType == LootType.other) {
if (enhanced > 0) {
return enhanced;
}
return null;
}
if (enhanced > 0) {
value += enhanced;
}
final int characters = GameMethods.getCurrentCharacterAmount();
if (characters >= _kHighCharCount) {
return value;
}
if (baseValue == LootBaseValue.oneIf4twoIfNot) {
value++;
} else if (characters <= _kLowCharCount &&
baseValue == LootBaseValue.oneIf3or4twoIfNot) {
value++;
}
return value;
}
}
class LootDeck {
static const int _kSpecialCard1418 = 1418;
static const int _kSpecialCard1419 = 1419;
static const int _kTreasureCardId = 9999;
static const int _kMaterialOneCount = 2;
static const int _kMaterialTwoCount = 3;
static const int _kHerbPoolCount = 2;
static const int _kCoin1Count = 12;
static const int _kCoin2Count = 6;
static const int _kMaterialPoolSize = 8;
static const int _kHerbPoolSize = 2;
List<LootCard> _coinPool = [];
List<LootCard> _lumberPool = [];
List<LootCard> _hidePool = [];
List<LootCard> _metalPool = [];
List<LootCard> _corpsecapPool = [];
List<LootCard> _arrowvinePool = [];
List<LootCard> _flamefruitPool = [];
List<LootCard> _axenutPool = [];
List<LootCard> _rockrootPool = [];
List<LootCard> _snowthistlePool = [];
BuiltList<LootCard> get coinPool => BuiltList.of(_coinPool);
BuiltList<LootCard> get lumberPool => BuiltList.of(_lumberPool);
BuiltList<LootCard> get hidePool => BuiltList.of(_hidePool);
BuiltList<LootCard> get metalPool => BuiltList.of(_metalPool);
BuiltList<LootCard> get corpsecapPool => BuiltList.of(_corpsecapPool);
BuiltList<LootCard> get arrowvinePool => BuiltList.of(_arrowvinePool);
BuiltList<LootCard> get flamefruitPool => BuiltList.of(_flamefruitPool);
BuiltList<LootCard> get axenutPool => BuiltList.of(_axenutPool);
BuiltList<LootCard> get rockrootPool => BuiltList.of(_rockrootPool);
BuiltList<LootCard> get snowthistlePool => BuiltList.of(_snowthistlePool);
//2 +1, 3 oneIf3or4twoIfNot, 3 oneIf4twoIfNot
BuiltList<int> get addedCards => BuiltList.of(_addedCards);
List<int> _addedCards = [0, 0, 0, 0, 0, 0, 0, 0, 0];
Map<String, int> _enhancements = {};
final CardStack<LootCard> _drawPile = CardStack<LootCard>();
final CardStack<LootCard> _discardPile = CardStack<LootCard>();
bool get drawPileIsEmpty => _drawPile.isEmpty;
bool get drawPileIsNotEmpty => _drawPile.isNotEmpty;
bool get discardPileIsEmpty => _discardPile.isEmpty;
bool get discardPileIsNotEmpty => _discardPile.isNotEmpty;
LootCard get drawPileTop => _drawPile.peek;
LootCard get discardPileTop => _discardPile.peek;
BuiltList<LootCard> get drawPileContents => BuiltList.of(_drawPile.getList());
BuiltList<LootCard> get discardPileContents =>
BuiltList.of(_discardPile.getList());
int get drawPileSize => _drawPile.size();
int get discardPileSize => _discardPile.size();
bool get hasCard1418 => _hasCard1418;
bool _hasCard1418 = false;
bool get hasCard1419 => _hasCard1419;
bool _hasCard1419 = false;
CardStack<LootCard> get drawPileNotifier => _drawPile;
LootDeck(LootDeckModel model, LootDeck other) {
_hasCard1418 = other._hasCard1418;
_hasCard1419 = other._hasCard1419;
_enhancements = other._enhancements;
//build deck
_initPools();
_setDeck(model);
}
LootDeck.from(LootDeck other) {
_hasCard1418 = other._hasCard1418;
_hasCard1419 = other._hasCard1419;
_enhancements = other._enhancements;
_initPools();
}
LootDeck.empty() {
_initPools();
}
LootDeck.fromJson(Map<String, dynamic> lootDeckData) {
updateFromJson(lootDeckData);
}
/// Updates this deck in-place from [lootDeckData].
void updateFromJson(Map<String, dynamic> lootDeckData) {
_hasCard1418 = lootDeckData["1418"] as bool;
_hasCard1419 = lootDeckData["1419"] as bool;
_addedCards = lootDeckData.containsKey('addedCards')
? List<int>.from(lootDeckData['addedCards'] as List)
: [0, 0, 0, 0, 0, 0, 0, 0, 0];
_enhancements = lootDeckData.containsKey('enhancements')
? Map<String, int>.from(lootDeckData['enhancements'] as Map)
: {};
_initPools();
LootCard cardFromJson(Map<dynamic, dynamic> item, int fallbackId) {
final String gfx = item["gfx"] as String;
final String owner = item.containsKey('owner')
? item["owner"] as String
: "";
final int id = item.containsKey('id') ? item["id"] as int : fallbackId;
// 'enhanced' was stored as bool in older saves, int in newer ones.
int enhanced = 0;
if (item['enhanced'] is bool) {
enhanced = (item['enhanced'] as bool) ? 1 : 0;
} else if (item['enhanced'] is num) {
enhanced = (item['enhanced'] as num).toInt();
}
final baseIdx = item["baseValue"] is int ? item["baseValue"] as int : 0;
final lootTypeIdx = item["lootType"] is int ? item["lootType"] as int : 0;
final LootBaseValue baseValue =
(baseIdx >= 0 && baseIdx < LootBaseValue.values.length)
? LootBaseValue.values[baseIdx]
: LootBaseValue.values.first;
final LootType lootType =
(lootTypeIdx >= 0 && lootTypeIdx < LootType.values.length)
? LootType.values[lootTypeIdx]
: LootType.values.first;
return LootCard(
id: id,
gfx: gfx,
enhanced: enhanced,
baseValue: baseValue,
lootType: lootType,
)..owner = owner;
}
final List<LootCard> newDrawList = [];
final List drawPile = lootDeckData["drawPile"] as List;
int id = 0;
for (final item in drawPile) {
if (item.containsKey('id')) id = item["id"] as int;
newDrawList.add(cardFromJson(item as Map, id));
}
final List<LootCard> newDiscardList = [];
for (final item in lootDeckData["discardPile"] as List) {
if (item.containsKey('id')) id = item["id"] as int;
newDiscardList.add(cardFromJson(item as Map, id));
}
_drawPile.clear();
_discardPile.clear();
_drawPile.setList(newDrawList);
_discardPile.setList(newDiscardList);
}
void setDeck(_StateModifier _, LootDeckModel model) {
_setDeck(model);
}
void returnLootCard(_StateModifier _, bool top) {
final card = _discardPile.pop();
card.owner = "";
if (top) {
_drawPile.push(card);
} else {
_drawPile.insert(0, card);
}
}
void _addCardFromPool(int amount, List<LootCard> pool, List<LootCard> cards) {
pool.shuffle();
if (amount > pool.length) {
amount = pool.length;
}
for (int i = 0; i < amount; i++) {
cards.add(pool[i]);
}
pool.sort((a, b) => a.id - b.id); //may not be needed
}
void _setDeck(LootDeckModel model) {
final List<LootCard> cards = [];
if (_hasCard1419) {
_addOtherType(_kSpecialCard1419, cards, "special 1419");
}
if (_hasCard1418) {
_addOtherType(_kSpecialCard1418, cards, "special 1418");
}
_addCardFromPool(model.arrowvine, _arrowvinePool, cards);
_addCardFromPool(model.corpsecap, _corpsecapPool, cards);
_addCardFromPool(model.axenut, _axenutPool, cards);
_addCardFromPool(model.flamefruit, _flamefruitPool, cards);
_addCardFromPool(model.rockroot, _rockrootPool, cards);
_addCardFromPool(model.snowthistle, _snowthistlePool, cards);
_addCardFromPool(model.coin, _coinPool, cards);
_addCardFromPool(model.metal, _metalPool, cards);
_addCardFromPool(model.hide, _hidePool, cards);
_addCardFromPool(model.lumber, _lumberPool, cards);
for (int i = 0; i < model.treasure; i++) {
_addOtherType(_kTreasureCardId, cards, "treasure");
}
_drawPile.setList(cards);
_discardPile.setList([]);
_shuffle();
}
void _addOtherType(int id, List<LootCard> cards, String gfx) {
cards.add(
LootCard(
id: id,
baseValue: LootBaseValue.one,
enhanced: _enhancements[id.toString()] ?? 0,
lootType: LootType.other,
gfx: gfx,
),
);
}
void _initMaterialPool(int startId, List<LootCard> list, String gfx) {
list.clear();
for (int i = 0; i < _kMaterialOneCount; i++) {
list.add(
LootCard(
id: startId,
baseValue: LootBaseValue.one,
enhanced: _enhancements[startId.toString()] ?? 0,
lootType: LootType.materiel,
gfx: gfx,
),
);
startId++;
}
for (int i = 0; i < _kMaterialTwoCount; i++) {
list.add(
LootCard(
id: startId,
baseValue: LootBaseValue.oneIf3or4twoIfNot,
enhanced: _enhancements[startId.toString()] ?? 0,
lootType: LootType.materiel,
gfx: gfx,
),
);
startId++;
}
for (int i = 0; i < _kMaterialTwoCount; i++) {
list.add(
LootCard(
id: startId,
baseValue: LootBaseValue.oneIf4twoIfNot,
enhanced: _enhancements[startId.toString()] ?? 0,
lootType: LootType.materiel,
gfx: gfx,
),
);
startId++;
}
}
void _initHerbPool(int startId, List<LootCard> list, String gfx) {
list.clear();
for (int i = 0; i < _kHerbPoolCount; i++) {
list.add(
LootCard(
id: startId,
baseValue: LootBaseValue.one,
enhanced: _enhancements[startId.toString()] ?? 0,
lootType: LootType.materiel,
gfx: gfx,
),
);
startId++;
}
}
void _initPools() {
_coinPool = [];
_lumberPool = [];
_hidePool = [];
_metalPool = [];
_axenutPool = [];
_arrowvinePool = [];
_rockrootPool = [];
_flamefruitPool = [];
_snowthistlePool = [];
_corpsecapPool = [];
int id = 1;
for (int i = 0; i < _kCoin1Count; i++) {
_addOtherType(id, _coinPool, "coin 1");
id++;
}
for (int i = 0; i < _kCoin2Count; i++) {
_addOtherType(id, _coinPool, "coin 2");
id++;
}
_addOtherType(id, _coinPool, "coin 3");
id++;
_addOtherType(id, _coinPool, "coin 3");
id++;
_initMaterialPool(id, _lumberPool, "lumber");
id += _kMaterialPoolSize;
_initMaterialPool(id, _hidePool, "hide");
id += _kMaterialPoolSize;
_initMaterialPool(id, _metalPool, "metal");
id += _kMaterialPoolSize;
_initHerbPool(id, _arrowvinePool, "arrowvine");
id += _kHerbPoolSize;
_initHerbPool(id, _axenutPool, "axenut");
id += _kHerbPoolSize;
_initHerbPool(id, _corpsecapPool, "corpsecap");
id += _kHerbPoolSize;
_initHerbPool(id, _flamefruitPool, "flamefruit");
id += _kHerbPoolSize;
_initHerbPool(id, _snowthistlePool, "snowthistle");
id += _kHerbPoolSize;
_initHerbPool(id, _rockrootPool, "rockroot");
}
void addSpecial1418(_StateModifier _) {
if (!_hasCard1418) {
_hasCard1418 = true;
_drawPile.add(
LootCard(
id: _kSpecialCard1418,
lootType: LootType.other,
baseValue: LootBaseValue.one,
enhanced: 0,
gfx: "special 1418",
),
);
//add directly to current deck and shuffle. save state separately
_shuffle();
}
}
void addSpecial1419(_StateModifier _) {
if (!_hasCard1419) {
_hasCard1419 = true;
_drawPile.add(
LootCard(
id: _kSpecialCard1419,
lootType: LootType.other,
baseValue: LootBaseValue.one,
enhanced: 0,
gfx: "special 1419",
),
);
_shuffle();
}
}
void removeSpecial1418(_StateModifier _) {
_hasCard1418 = false;
_drawPile.removeWhere((element) => element.id == _kSpecialCard1418);
_discardPile.removeWhere((element) => element.id == _kSpecialCard1418);
}
void removeSpecial1419(_StateModifier _) {
_hasCard1419 = false;
_drawPile.removeWhere((element) => element.id == _kSpecialCard1419);
_discardPile.removeWhere((element) => element.id == _kSpecialCard1419);
}
List<LootCard> _getAvailableCards(List<LootCard> pool) {
final List<LootCard> list = [];
for (final item in pool) {
if (!_drawPile.getList().any((element) => element.id == item.id)) {
list.add(item);
}
}
list.shuffle();
return list;
}
void addExtraCard(_StateModifier _, String identifier) {
_initPools();
_shuffle();
if (identifier == "hide") {
final pool = _getAvailableCards(_hidePool);
if (pool.isNotEmpty) {
_drawPile.add(pool.first);
_addedCards[0] = _addedCards.first + 1;
}
}
if (identifier == "lumber") {
final pool = _getAvailableCards(_lumberPool);
if (pool.isNotEmpty) {
_drawPile.add(pool.first);
_addedCards[1]++;
}
}
if (identifier == "metal") {
final pool = _getAvailableCards(_metalPool);
if (pool.isNotEmpty) {
_drawPile.add(pool.first);
_addedCards[2]++;
}
}
if (identifier == "arrowvine") {
final pool = _getAvailableCards(_arrowvinePool);
if (pool.isNotEmpty) {
_drawPile.add(pool.first);
_addedCards[3]++;
}
}
if (identifier == "axenut") {
final pool = _getAvailableCards(_axenutPool);
if (pool.isNotEmpty) {
_drawPile.add(pool.first);
_addedCards[4]++;
}
}
if (identifier == "corpsecap") {
final pool = _getAvailableCards(_corpsecapPool);
if (pool.isNotEmpty) {
_drawPile.add(pool.first);
_addedCards[5]++;
}
}
if (identifier == "flamefruit") {
final pool = _getAvailableCards(_flamefruitPool);
if (pool.isNotEmpty) {
_drawPile.add(pool.first);
_addedCards[6]++;
}
}
if (identifier == "rockroot") {
final pool = _getAvailableCards(_rockrootPool);
if (pool.isNotEmpty) {
_drawPile.add(pool.first);
_addedCards[7]++;
}
}
if (identifier == "snowthistle") {
final pool = _getAvailableCards(_snowthistlePool);
if (pool.isNotEmpty) {
_drawPile.add(pool.first);
_addedCards[8]++;
}
}
_shuffle();
}
void addEnhancement(
_StateModifier _,
int id,
int value, {
GameState? gameState,
GameData? gameData,
}) {
_enhancements[id.toString()] = value;
//reset loot deck
_initPools();
final gs = gameState ?? getIt<GameState>();
final gd = gameData ?? getIt<GameData>();
final String scenario = gs.scenario;
final LootDeckModel? lootDeckModel =
gd.modelData[gs.currentCampaign]?.scenarios[scenario]?.lootDeck;
if (lootDeckModel != null) {
_setDeck(lootDeckModel);
}
}
void _shuffle() {
while (_discardPile.isNotEmpty) {
final LootCard card = _discardPile.pop();
_drawPile.push(card);
}
_drawPile.shuffle();
}
void draw(_StateModifier _, {GameState? gameState}) {
//put top of draw pile on discard pile
final LootCard card = _drawPile.pop();
//mark owner
final gs = gameState ?? getIt<GameState>();
for (final item in gs.currentList) {
if (item.turnState == TurnsState.current && item is Character) {
if (!GameMethods.isObjectiveOrEscort(item.characterClass)) {
card.owner = item.characterClass.id;
break;
}
}
}
_discardPile.push(card);
}
Map<String, dynamic> toJson() => {
'drawPile': _drawPile.getList().map((c) => c.toJson()).toList(),
'discardPile': _discardPile.getList().map((c) => c.toJson()).toList(),
'addedCards': _addedCards,
'enhancements': _enhancements,
'1418': _hasCard1418,
'1419': _hasCard1419,
// Pool card ids only: pool cards are the same live instances that enter
// the draw/discard piles (and mutate with them), so their full JSON would
// leak those mutations into the static pool. The enhancement menu needs
// the ids plus the `enhancements` map (card id → level).
'pools': {
'coin': _coinPool.map((c) => c.id).toList(),
'lumber': _lumberPool.map((c) => c.id).toList(),
'hide': _hidePool.map((c) => c.id).toList(),
'metal': _metalPool.map((c) => c.id).toList(),
'corpsecap': _corpsecapPool.map((c) => c.id).toList(),
'arrowvine': _arrowvinePool.map((c) => c.id).toList(),
'flamefruit': _flamefruitPool.map((c) => c.id).toList(),
'axenut': _axenutPool.map((c) => c.id).toList(),
'rockroot': _rockrootPool.map((c) => c.id).toList(),
'snowthistle': _snowthistlePool.map((c) => c.id).toList(),
},
};
@override
String toString() => json.encode(toJson());
}
enum LootType { materiel, other }
enum LootBaseValue { one, oneIf4twoIfNot, oneIf3or4twoIfNot }
|