summaryrefslogtreecommitdiff
path: root/server/lib/core/state/monster_instance.dart
blob: a3fde0c6d19068194603f88df3302eb5e14586c9 (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
part of 'game_state.dart';

// ignore_for_file: library_private_types_in_public_api

class MonsterInstance extends FigureState {
  static const int _kIncarnateHealthMultiplier = 2;
  MonsterInstance(
    this.standeeNr,
    this._type,
    bool summoned,
    Monster monster, {
    GameState? gameState,
  }) {
    _setLevel(monster);
    gfx = monster.type.gfx;
    name = monster.type.name;
    move = 0; //only used for summons
    attack = 0;
    range = 0;
    _roundSummoned = summoned ? (gameState ?? getIt<GameState>()).round : -1;
  }

  MonsterInstance.summon(
    this.standeeNr,
    this._type,
    this.name,
    int summonHealth,
    this.move,
    this.attack,
    this.range,
    this.gfx,
    this._roundSummoned,
  ) {
    //deal with summon init
    _maxHealth = summonHealth;
    _health = summonHealth;
  }

  String getId() {
    return name + gfx + standeeNr.toString();
  }

  int standeeNr = 0;

  void setType(_StateModifier _, MonsterType value) {
    _type = value;
  }

  MonsterType get type => _type;
  MonsterType _type =
      MonsterType.normal; //can't be final due to ice wraith special
  String name = '';
  String gfx = '';

  //summon stats. not used currently
  int move = 0;
  int attack = 0;
  int range = 0;

  int get roundSummoned => _roundSummoned;
  void setRoundSummoned(_StateModifier _, int value) {
    _roundSummoned = value;
  }

  int _roundSummoned = -1;

  void _setLevel(Monster monster, {GameState? gameState}) {
    StatValue newHealthValue = const IntStatValue(
      10,
    ); //need to put something outer than 0 or the standee will die immediately causing glitch
    final levels = monster.type.levels;
    final levelIndex = levels.isEmpty
        ? -1
        : monster.level.clamp(0, levels.length - 1);
    if (levelIndex >= 0) {
      if (type == MonsterType.boss) {
        newHealthValue = levels[levelIndex].boss?.health ?? newHealthValue;
      } else if (type == MonsterType.elite) {
        newHealthValue = levels[levelIndex].elite?.health ?? newHealthValue;
      } else if (type == MonsterType.normal) {
        newHealthValue = levels[levelIndex].normal?.health ?? newHealthValue;
      }
    }
    int? value = StatCalculator.calculateFormula(newHealthValue);
    if (value != null) {
      _maxHealth = value;
    } else {
      final gs = gameState ?? getIt<GameState>();
      //handle edge case
      if (newHealthValue is FormulaStatValue &&
          newHealthValue.formula == "Hollowpact") {
        int value = 7;
        for (final item in gs.currentList) {
          if (item is Character && item.id == "Hollowpact") {
            final healthByLevel = item.characterClass.healthByLevel;
            final level = item.characterState.level;
            if (level >= 1 && level <= healthByLevel.length) {
              value = healthByLevel[level - 1];
            }
            break;
          }
        }
        _maxHealth = value;
      }
      if (newHealthValue is FormulaStatValue &&
          newHealthValue.formula == "Incarnate") {
        int value = 36; //double Incarnates level 5 health
        for (final item in gs.currentList) {
          if (item is Character && item.id == "Incarnate") {
            final healthByLevel = item.characterClass.healthByLevel;
            final level = item.characterState.level;
            if (level >= 1 && level <= healthByLevel.length) {
              value = healthByLevel[level - 1] * _kIncarnateHealthMultiplier;
            }
            break;
          }
        }
        _maxHealth = value;
      }
    }
    _level = monster.level;
    _health = maxHealth;
  }

  Map<String, dynamic> toJson() => {
    'health': health,
    'maxHealth': maxHealth,
    'level': level,
    'standeeNr': standeeNr,
    'move': move,
    'attack': attack,
    'range': range,
    'name': name,
    'gfx': gfx,
    'roundSummoned': roundSummoned,
    'type': type.index,
    'chill': chill,
    'plague': plague,
    'conditions': conditions.map((c) => c.index).toList(),
    'conditionsAddedThisTurn': _conditionsAddedThisTurn
        .map((c) => c.index)
        .toList(),
    'conditionsAddedPreviousTurn': _conditionsAddedPreviousTurn
        .map((c) => c.index)
        .toList(),
  };

  @override
  String toString() => json.encode(toJson());

  MonsterInstance.fromJson(Map<String, dynamic> json) {
    standeeNr = json["standeeNr"];
    _health = json["health"];
    _level = json["level"];
    _maxHealth = json["maxHealth"];
    name = json["name"];
    gfx = json["gfx"];
    final typeIdx = json["type"] as int?;
    _type =
        (typeIdx != null && typeIdx >= 0 && typeIdx < MonsterType.values.length)
        ? MonsterType.values[typeIdx]
        : MonsterType.normal;
    move = json["move"];
    attack = json["attack"];
    range = json["range"];
    _roundSummoned = json.containsKey("roundSummoned")
        ? json["roundSummoned"]
        : -1;
    _chill = json["chill"];
    if (json.containsKey("plague")) {
      _plague = json["plague"];
    }
    final condis = json["conditions"];
    for (int item in condis) {
      if (item >= 0 && item < Condition.values.length) {
        conditions.add(Condition.values[item]);
      }
    }

    if (json.containsKey("conditionsAddedThisTurn")) {
      List<Object?> condis2 = json["conditionsAddedThisTurn"] as List<Object?>;
      for (int item in condis2.cast<int>()) {
        if (item >= 0 && item < Condition.values.length) {
          _conditionsAddedThisTurn.add(Condition.values[item]);
        }
      }
    }
    if (json.containsKey("conditionsAddedPreviousTurn")) {
      List<Object?> condis3 =
          json["conditionsAddedPreviousTurn"] as List<Object?>;
      for (int item in condis3.cast<int>()) {
        if (item >= 0 && item < Condition.values.length) {
          _conditionsAddedPreviousTurn.add(Condition.values[item]);
        }
      }
    }
  }
}