summaryrefslogtreecommitdiff
path: root/server/lib/core/methods/character_methods.dart
blob: 6cd6c10803442af4974e28f2a2592ba8e06b2766 (plain)
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
part of '../state/game_state.dart';

// ignore_for_file: library_private_types_in_public_api

class CharacterMethods {
  static const int _kHailPerkIndex = 17;
  static const int _kPainConduitPerkIndex = 16;
  static const int _kPainConduitHealthBonus = 5;
  static const int _kObjectiveInitiative = 99;
  // Special summon base stats
  static const int _kBeastHealth = 8;
  static const int _kBeastMultiplier = 2;
  static const int _kDomeHealth = 3;
  static const int _kGlacierHealth = 7;
  static const int _kJesterHealth = 5;
  static const int _kSpecialSummonMove = 3;
  static const int _kSpecialSummonAttack = 2;
  static const int _kSpecialSummonRange = 0;
  static const int _kSpecialSummonStandeeNr = 0;
  static const int _kSpecialSummonInitiative = -1;

  static void addPerk(
    _StateModifier s,
    Character character,
    int index, {
    GameState? gameState,
  }) {
    final gs = gameState ?? getIt<GameState>();
    final deck = character.characterState.modifierDeck;
    final perksFH = character.characterClass.perksFH;
    final useFHPerks =
        character.characterState.useFHPerks && perksFH.isNotEmpty;
    final perks = useFHPerks ? perksFH : character.characterClass.perks;
    final perk = perks[index];
    for (final item in perk.remove) {
      final amount = deck.drawPileSize;
      deck.removeCard(s, item);
      if (deck.drawPileSize == amount) {
        //must be a perk card

        //find missing card from perk list

        for (int i = 0; i < perks.length; i++) {
          //check if other perk added the card previously
          final adds = perks[i].add;
          if (adds.contains(item)) {
            //remove that perk card
            String second = "";
            if (adds.first != adds.last && item == adds.last) {
              second = "-2"; //in case perk adds 2 different cards
            }
            deck.removeCard(s, "P$i$second");
            if (deck.drawPileSize != amount) {
              //found and removed
              break;
            }
          }
        }
      }
    }
    for (final item in perk.add) {
      CardType type = CardType.add;
      if (item.endsWith("/ns2")) {
        //nightshroud hack
        type = CardType.multiply;
      }
      final id = GameMethods.perkGfxIdToCardId(item, perk, index);
      deck.addCard(s, id, type);
    }

    final className = character.characterClass.name;
    if (index == _kHailPerkIndex && className == "Hail") {
      gs.modifierDeck.addHailSpecial(s);
    }
    if (index == _kPainConduitPerkIndex && className == "Pain Conduit") {
      final level = character.characterState.level;
      final healthByLevel = character.characterClass.healthByLevel;
      if (level >= 1 && level <= healthByLevel.length) {
        character.characterState._health =
            healthByLevel[level - 1] + _kPainConduitHealthBonus;
        character.characterState.setMaxHealth(
          s,
          character.characterState._health,
        );
      }
    }
  }

  static void removePerk(
    _StateModifier s,
    Character character,
    int index, {
    GameState? gameState,
  }) {
    final gs = gameState ?? getIt<GameState>();
    final deck = character.characterState.modifierDeck;
    final perksFH = character.characterClass.perksFH;
    final useFHPerks =
        character.characterState.useFHPerks && perksFH.isNotEmpty;
    final perks = useFHPerks ? perksFH : character.characterClass.perks;
    final perk = perks[index];
    for (final item in perk.remove) {
      if (item.startsWith("perks/")) {
        //find id of perk: bull: could be several...
        for (int i = 0; i < perks.length; i++) {
          if (character.characterState.perkList[i] &&
              perks[i].add.contains(item)) {
            final id = GameMethods.perkGfxIdToCardId(item, perks[i], i);
            deck.addCard(s, id, CardType.add);
            break;
          }
        }
      } else {
        deck.addCard(s, item, CardType.add);
      }
    }
    for (final item in perk.add) {
      final id = GameMethods.perkGfxIdToCardId(item, perk, index);
      deck.removeCard(s, id);
    }

    final className = character.characterClass.name;
    if (index == _kHailPerkIndex && className == "Hail") {
      gs.modifierDeck.removeHailSpecial(s);
    }
    if (index == _kPainConduitPerkIndex && className == "Pain Conduit") {
      final state = character.characterState;
      final characterClass = character.characterClass;
      final level = state.level;
      if (level >= 1 && level <= characterClass.healthByLevel.length) {
        final int health = characterClass.healthByLevel[level - 1];
        state.setMaxHealth(s, health);
        state.setHealth(s, health);
      }
    }
  }

  static void setCharacterLevel(
    _StateModifier s,
    int level,
    String characterId, {
    GameState? gameState,
  }) {
    final gs = gameState ?? getIt<GameState>();
    Character? character;
    for (final item in gs.currentList) {
      if (item.id == characterId && item is Character) {
        character = item;
        break;
      }
    }
    if (character != null) {
      final healthByLevel = character.characterClass.healthByLevel;
      if (healthByLevel.length < level) {
        level = healthByLevel.length;
      }
      character.characterState.setFigureLevel(s, level);
      character.characterState.setHealth(s, healthByLevel[level - 1]);

      if (character.id == "Pain Conduit" &&
          character.characterState.perkList[_kPainConduitPerkIndex]) {
        character.characterState.setHealth(
          s,
          healthByLevel[level - 1] + _kPainConduitHealthBonus,
        );
      }

      character.characterState.setMaxHealth(s, character.characterState.health);

      //handle special summons
      int health = 0;
      int multiplier = 1;
      String name = "";
      if (character.id == "Beast Tyrant" || character.id == "Wildfury") {
        //create the bear summon
        health = _kBeastHealth;
        multiplier = _kBeastMultiplier;
        name = "Beast";
      }
      if (character.id == "D.O.M.E.") {
        health = _kDomeHealth;
        name = "Barrier";
      }
      if (character.id == "Glacial Torrent") {
        health = _kGlacierHealth;
        name = "Glacier";
      }
      if (character.id == "Jester Twins") {
        //create the barrier as a summon
        health = _kJesterHealth;
        name = "Jester Twin";
      }

      final list = character.characterState.summonList;
      if (list.isNotEmpty && list.first.name == name) {
        final int hp = health + character.characterState.level * multiplier;
        list.first.setMaxHealth(s, hp);
        list.first.setHealth(s, hp);
      }
    }

    ScenarioMethods.applyDifficulty(s);
  }

  static void resetCharacter(_StateModifier s, Character item) {
    item.characterState._initiative = 0;
    final level = item.characterState.level;
    item.characterState._health = item.characterClass.healthByLevel[level - 1];

    if (item.characterClass.name == "Pain Conduit") {
      if (item.characterState.perkList[_kPainConduitPerkIndex]) {
        item.characterState._health += _kPainConduitHealthBonus;
      }
    }

    item.characterState._maxHealth = item.characterState.health;
    item.characterState._xp = 0;
    item.characterState._conditions.clear();
    item.characterState._chill = 0;
    item.characterState._plague = 0;
    item.characterState.modifierDeck._initDeck();
    //reapply perks
    final perksSetList = item.characterState.perkList;
    final perks = item.characterState.useFHPerks
        ? item.characterClass.perksFH
        : item.characterClass.perks;
    for (int i = 0; i < perks.length; i++) {
      if (perksSetList[i]) {
        addPerk(s, item, i);
      }
    }
    //handle special summons
    final summonList = item.characterState._summonList;
    summonList.clear();
    int health = 0;
    int multiplier = 1;
    String gfx = "";
    String name = "";
    if (item.id == "Beast Tyrant" || item.id == "Wildfury") {
      //create the bear summon
      health = _kBeastHealth;
      multiplier = _kBeastMultiplier;
      gfx = item.id == "Beast Tyrant" ? "beast" : "Beast v2";
      name = "Beast";
    }
    if (item.id == "D.O.M.E.") {
      health = _kDomeHealth;
      gfx = "DOM barrier";
      name = "Barrier";
    }
    if (item.id == "Glacial Torrent") {
      health = _kGlacierHealth;
      gfx = "GLA glacier";
      name = "Glacier";
    }
    if (item.id == "Jester Twins") {
      //create the barrier as a summon
      health = _kJesterHealth;
      gfx = "JES twin";
      name = "Jester Twin";
    }

    if (name.isNotEmpty) {
      final MonsterInstance summon = MonsterInstance.summon(
        _kSpecialSummonStandeeNr,
        MonsterType.summon,
        name,
        health + level * multiplier,
        _kSpecialSummonMove,
        _kSpecialSummonAttack,
        _kSpecialSummonRange,
        gfx,
        _kSpecialSummonInitiative,
      );
      summonList.add(summon);
    }
    item.characterState._notifySummonList();
  }

  static void removeCharacters(
    _StateModifier s,
    List<Character> characters, {
    GameState? gameState,
  }) {
    final List<ListItemData> newList = [];
    final gs = gameState ?? getIt<GameState>();
    for (final item in gs.currentList) {
      if (item is Character) {
        bool remove = false;
        for (final name in characters) {
          if (item.characterState.display == name.characterState.display) {
            remove = true;
            break;
          }
        }
        if (!remove) {
          newList.add(item);
        }
      } else {
        newList.add(item);
      }
    }
    gs._currentList = newList;
    RoundMethods.updateForSpecialRules(s);
    gs._notifyCurrentList();
  }

  static Character? createCharacter(
    _StateModifier _,
    String id,
    String? edition,
    String? display,
    int level, {
    GameData? gameData,
  }) {
    Character? character;
    final List<CharacterClass> characters = [];
    final gd = gameData ?? getIt<GameData>();
    final modelData = gd.modelData;
    for (String key in modelData.keys) {
      characters.addAll(modelData[key]?.characters ?? []);
    }
    for (CharacterClass characterClass in characters) {
      if (characterClass.id == id &&
          (edition == null || edition == characterClass.edition)) {
        final characterState = CharacterState(id);
        characterState._level = level;

        if (GameMethods.isObjectiveOrEscort(characterClass)) {
          characterState._initiative = _kObjectiveInitiative;
        }
        characterState._health = characterClass.healthByLevel[level - 1];
        characterState._maxHealth = characterState.health;

        if (display != null) {
          characterState._display = display;
        } else {
          characterState._display = characterClass.name;
        }
        character = Character(characterState, characterClass);

        //handle special summons
        final summonList = character.characterState._summonList;
        summonList.clear();
        int health = 0;
        int multiplier = 1;
        String gfx = "";
        String name = "";
        if (character.id == "Beast Tyrant" || character.id == "Wildfury") {
          //create the bear summon
          health = _kBeastHealth;
          multiplier = _kBeastMultiplier;
          gfx = character.id == "Beast Tyrant" ? "beast" : "Beast v2";
          name = "Beast";
        }
        if (character.id == "D.O.M.E.") {
          health = _kDomeHealth;
          gfx = "DOM barrier";
          name = "Barrier";
        }
        if (character.id == "Glacial Torrent") {
          health = _kGlacierHealth;
          gfx = "GLA glacier";
          name = "Glacier";
        }
        if (character.id == "Jester Twins") {
          //create the barrier as a summon
          health = _kJesterHealth;
          gfx = "JES twin";
          name = "Jester Twin";
        }

        if (name.isNotEmpty) {
          final MonsterInstance summon = MonsterInstance.summon(
            _kSpecialSummonStandeeNr,
            MonsterType.summon,
            name,
            health + level * multiplier,
            _kSpecialSummonMove,
            _kSpecialSummonAttack,
            _kSpecialSummonRange,
            gfx,
            _kSpecialSummonInitiative,
          );
          summonList.add(summon);
        }

        break;
      }
    }
    return character;
  }
}