summaryrefslogtreecommitdiff
path: root/server/lib/core/methods
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/core/methods')
-rw-r--r--server/lib/core/methods/character_methods.dart393
-rw-r--r--server/lib/core/methods/deck_methods.dart79
-rw-r--r--server/lib/core/methods/element_methods.dart59
-rw-r--r--server/lib/core/methods/game_util_methods.dart11
-rw-r--r--server/lib/core/methods/monster_methods.dart403
-rw-r--r--server/lib/core/methods/round_methods.dart493
-rw-r--r--server/lib/core/methods/scenario_methods.dart530
7 files changed, 1968 insertions, 0 deletions
diff --git a/server/lib/core/methods/character_methods.dart b/server/lib/core/methods/character_methods.dart
new file mode 100644
index 0000000..9c2705a
--- /dev/null
+++ b/server/lib/core/methods/character_methods.dart
@@ -0,0 +1,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) {
+ 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) {
+ 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,
+ }) {
+ 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;
+ 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) {
+ MonsterInstance summon = MonsterInstance.summon(
+ _kSpecialSummonStandeeNr,
+ MonsterType.summon,
+ name,
+ health + level * multiplier,
+ _kSpecialSummonMove,
+ _kSpecialSummonAttack,
+ _kSpecialSummonRange,
+ gfx,
+ _kSpecialSummonInitiative,
+ );
+ summonList.add(summon);
+ }
+
+ break;
+ }
+ }
+ return character;
+ }
+}
diff --git a/server/lib/core/methods/deck_methods.dart b/server/lib/core/methods/deck_methods.dart
new file mode 100644
index 0000000..535324f
--- /dev/null
+++ b/server/lib/core/methods/deck_methods.dart
@@ -0,0 +1,79 @@
+part of '../state/game_state.dart';
+
+// ignore_for_file: library_private_types_in_public_api
+
+class DeckMethods {
+ static void drawAbilityCardFromInactiveDeck(
+ _StateModifier stateModifier, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ for (MonsterAbilityState deck in gs.currentAbilityDecks) {
+ for (final item in gs.currentList) {
+ if (item is Monster) {
+ if (item.type.deck == deck.name) {
+ if (item.isActive &&
+ !GameMethods.isInactiveForRule(item.type.name)) {
+ if (deck.lastRoundDrawn != gs.totalRounds) {
+ //do not draw new card in case drawn already this round
+ deck.draw(stateModifier);
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ static void drawAbilityCards(
+ _StateModifier stateModifier, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ for (MonsterAbilityState deck in gs.currentAbilityDecks) {
+ for (final item in gs.currentList) {
+ if (item is Monster) {
+ if (item.type.deck == deck.name) {
+ bool specialInactive = GameMethods.isInactiveForRule(
+ item.type.name,
+ );
+ if ((item.monsterInstances.isNotEmpty && !specialInactive) ||
+ (item.isActive && !specialInactive)) {
+ deck.draw(stateModifier);
+ //only draw once from each deck
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ static void shuffleDecksIfNeeded(_StateModifier _, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ for (final deck in gs.currentAbilityDecks) {
+ if (deck.discardPileIsNotEmpty && deck.discardPileTop.shuffle ||
+ deck.drawPileIsEmpty) {
+ deck._shuffle();
+ }
+ }
+ }
+
+ static void shuffleDecks(_StateModifier _, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ for (final deck in gs.currentAbilityDecks) {
+ deck._shuffle();
+ }
+ }
+
+ static void returnModifierCard(
+ _StateModifier s,
+ String name, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ final deck = GameMethods.getModifierDeck(name, gs);
+ deck.returnCardToDrawPile(s);
+ }
+}
diff --git a/server/lib/core/methods/element_methods.dart b/server/lib/core/methods/element_methods.dart
new file mode 100644
index 0000000..5da97a3
--- /dev/null
+++ b/server/lib/core/methods/element_methods.dart
@@ -0,0 +1,59 @@
+part of '../state/game_state.dart';
+
+// ignore_for_file: library_private_types_in_public_api
+
+class ElementMethods {
+ static void resetElements(_StateModifier _, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ for (final key in gs.elementState.keys) {
+ gs._elementState[key] = ElementState.inert;
+ }
+ }
+
+ static void updateElements(_StateModifier _, {GameState? gameState}) {
+ //elementalist special:
+ bool elementalistPerk = false;
+ Character? elementalist = GameMethods.getCharacterByName("Elementalist");
+ if (elementalist != null && elementalist.characterState.perkList[15]) {
+ if (elementalist.characterClass.edition == "Gloomhaven 2nd Edition") {
+ elementalistPerk = true;
+ }
+ }
+
+ final gs = gameState ?? getIt<GameState>();
+ for (final key in gs.elementState.keys) {
+ if (gs.elementState[key] == ElementState.full) {
+ if (!elementalistPerk ||
+ key == Elements.light ||
+ key == Elements.dark) {
+ gs._elementState[key] = ElementState.half;
+ }
+ } else if (gs.elementState[key] == ElementState.half) {
+ if (!elementalistPerk ||
+ key == Elements.light ||
+ key == Elements.dark) {
+ gs._elementState[key] = ElementState.inert;
+ }
+ }
+ }
+ }
+
+ static void imbueElement(
+ _StateModifier _,
+ Elements element,
+ bool half, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._elementState[element] = half ? ElementState.half : ElementState.full;
+ }
+
+ static void useElement(
+ _StateModifier _,
+ Elements element, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._elementState[element] = ElementState.inert;
+ }
+}
diff --git a/server/lib/core/methods/game_util_methods.dart b/server/lib/core/methods/game_util_methods.dart
new file mode 100644
index 0000000..8c2220e
--- /dev/null
+++ b/server/lib/core/methods/game_util_methods.dart
@@ -0,0 +1,11 @@
+part of '../state/game_state.dart';
+
+// ignore_for_file: library_private_types_in_public_api
+
+class GameUtilMethods {
+ //note: while this changes the game state, it is a state used also by non game related instances.
+ static void setToastMessage(String message, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._toastMessage = message;
+ }
+}
diff --git a/server/lib/core/methods/monster_methods.dart b/server/lib/core/methods/monster_methods.dart
new file mode 100644
index 0000000..c0743b3
--- /dev/null
+++ b/server/lib/core/methods/monster_methods.dart
@@ -0,0 +1,403 @@
+part of '../state/game_state.dart';
+
+// ignore_for_file: library_private_types_in_public_api
+
+class MonsterMethods {
+ static const int _kMinLevel = 0;
+ static const int _kMaxLevel = 7;
+ static const int _kMinCharacters = 2;
+ static const int _kMaxCharacters = 4;
+ static const int _kCharIndexOffset = 2;
+ static const int _kTrailingCommaLength = 2;
+
+ static void showAllyDeck(_StateModifier _, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._showAllyDeck = true;
+ }
+
+ static void hideAllyDeck(_StateModifier _, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._showAllyDeck = false;
+ }
+
+ static Monster? createMonster(
+ _StateModifier _,
+ String name,
+ int? level,
+ bool isAlly, {
+ GameState? gameState,
+ GameData? gameData,
+ }) {
+ final gd = gameData ?? getIt<GameData>();
+ Map<String, MonsterModel> monsters = {};
+ final modelData = gd.modelData;
+ for (String key in modelData.keys) {
+ monsters.addAll(modelData[key]?.monsters ?? {});
+ }
+ level ??= (gameState ?? getIt<GameState>()).level;
+ return Monster(name, level, isAlly);
+ }
+
+ static void removeMonsters(
+ _StateModifier _,
+ List<Monster> items, {
+ GameState? gameState,
+ }) {
+ List<String> deckIds = [];
+ List<ListItemData> newList = [];
+ final gs = gameState ?? getIt<GameState>();
+ for (final item in gs.currentList) {
+ if (item is Monster) {
+ bool remove = false;
+ for (final name in items) {
+ if (item.id == name.id) {
+ remove = true;
+ deckIds.add(item.type.deck);
+ }
+ }
+ if (!remove) {
+ newList.add(item);
+ }
+ } else {
+ newList.add(item);
+ }
+ }
+
+ gs._currentList = newList;
+
+ for (final deck in deckIds) {
+ bool removeDeck = true;
+ for (final item in gs.currentList) {
+ if (item is Monster) {
+ if (item.type.deck == deck) {
+ removeDeck = false;
+ }
+ }
+ }
+
+ if (removeDeck) {
+ for (final item in gs.currentAbilityDecks) {
+ if (item.name == deck) {
+ gs._currentAbilityDecks.remove(item);
+ break;
+ }
+ }
+ }
+ }
+
+ gs._notifyCurrentList();
+ }
+
+ static void executeAddStandee(
+ _StateModifier s,
+ final int nr,
+ final SummonData? summon,
+ final MonsterType type,
+ final String ownerId,
+ final bool addAsSummon, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ MonsterInstance instance;
+ Monster? monster;
+ if (summon == null) {
+ for (final item in gs.currentList) {
+ if (item.id == ownerId && item is Monster) {
+ monster = item;
+ monster._isActive = true;
+ break;
+ }
+ }
+ if (monster == null) return;
+ instance = MonsterInstance(nr, type, addAsSummon, monster);
+ } else {
+ instance = MonsterInstance.summon(
+ summon.standeeNr,
+ type,
+ summon.name,
+ summon.health,
+ summon.move,
+ summon.attack,
+ summon.range,
+ summon.gfx,
+ gs.round,
+ );
+ }
+
+ List<MonsterInstance> monsterList = [];
+ Character? character;
+ //find list
+ if (monster != null) {
+ monsterList = monster._monsterInstances;
+ } else {
+ for (final item in gs.currentList) {
+ if (item.id == ownerId) {
+ character = item as Character;
+ monsterList = character.characterState._summonList;
+ break;
+ }
+ }
+ }
+
+ //make sure summons can not have same gfx and nr:
+ if (instance.standeeNr != 0 && summon != null) {
+ bool ok = false;
+ while (!ok) {
+ ok = true;
+ for (final item in monsterList) {
+ if (item.standeeNr == instance.standeeNr) {
+ if (item.gfx == instance.gfx) {
+ //can not have same gfx and nr
+ instance = MonsterInstance.summon(
+ instance.standeeNr + 1,
+ type,
+ summon.name,
+ summon.health,
+ summon.move,
+ summon.attack,
+ summon.range,
+ summon.gfx,
+ gs.round,
+ );
+ ok = false;
+ }
+ }
+ }
+ }
+ }
+
+ monsterList.add(instance);
+ if (monster != null) {
+ RoundMethods.sortMonsterInstances(s, monsterList);
+ monster._notifyMonsterInstances();
+ } else {
+ character?.characterState._notifySummonList();
+ }
+ if (monsterList.length == 1 && monster != null) {
+ //first added
+ final roundState = gs.roundState;
+ if (roundState == RoundState.chooseInitiative) {
+ RoundMethods.sortCharactersFirst(s);
+ } else if (roundState == RoundState.playTurns) {
+ DeckMethods.drawAbilityCardFromInactiveDeck(s);
+ RoundMethods.sortItemToPlace(
+ s,
+ monster.id,
+ GameMethods.getInitiative(monster),
+ ); //need to only sort this one item to place
+ }
+ }
+ }
+
+ static void addStandee(
+ int? nr,
+ Monster data,
+ MonsterType type,
+ bool addAsSummon, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ if (nr != null) {
+ gs.action(
+ AddStandeeCommand(nr, null, data.id, type, addAsSummon, gameState: gs),
+ );
+ } else {
+ //add first un added nr
+ for (int i = 1; i <= data.type.count; i++) {
+ bool added = false;
+ for (final item in data.monsterInstances) {
+ if (item.standeeNr == i) {
+ added = true;
+ break;
+ }
+ }
+ if (!added) {
+ gs.action(
+ AddStandeeCommand(
+ i,
+ null,
+ data.id,
+ type,
+ addAsSummon,
+ gameState: gs,
+ ),
+ );
+ return;
+ }
+ }
+ }
+ }
+
+ static void addMonster(
+ _StateModifier s,
+ String monster,
+ List<SpecialRule> specialRules, {
+ GameState? gameState,
+ }) {
+ int levelAdjust = 0;
+ Set<String> alliedMonsters = {};
+ for (final rule in specialRules) {
+ if (rule.name == monster || rule.name == "Enemies") {
+ if (rule.type == "LevelAdjust") {
+ levelAdjust = rule.level;
+ }
+ }
+ if (rule.type == "Allies") {
+ for (String item in rule.list.cast<String>()) {
+ alliedMonsters.add(item);
+ }
+ }
+ }
+
+ final gs = gameState ?? getIt<GameState>();
+ bool add = true;
+ for (final item in gs.currentList) {
+ //don't add duplicates
+ if (item.id == monster) {
+ add = false;
+ break;
+ }
+ }
+ if (add) {
+ bool isAlly = false;
+ if (alliedMonsters.contains(monster)) {
+ isAlly = true;
+ }
+
+ final munster = createMonster(
+ s,
+ monster,
+ (gs.level + levelAdjust).clamp(_kMinLevel, _kMaxLevel),
+ isAlly,
+ );
+ if (munster != null) {
+ gs._currentList.add(munster);
+ }
+ }
+ }
+
+ static String autoAddStandees(
+ _StateModifier stateModifier,
+ List<RoomMonsterData> roomMonsterData,
+ String initMessage, {
+ GameState? gameState,
+ Settings? settings,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ //handle room data
+ int characterIndex =
+ GameMethods.getCurrentCharacterAmount().clamp(
+ _kMinCharacters,
+ _kMaxCharacters,
+ ) -
+ _kCharIndexOffset;
+ for (int i = 0; i < roomMonsterData.length; i++) {
+ final roomMonsters = roomMonsterData[i];
+ addMonster(stateModifier, roomMonsters.name, gs._scenarioSpecialRules);
+ }
+ bool addSorted = gs.currentCampaign == "Buttons and Bugs";
+ final s = settings ?? getIt<Settings>();
+ if (!s.noStandees && s.autoAddStandees) {
+ if (s.randomStandees || addSorted) {
+ if (initMessage.isNotEmpty && !addSorted) {
+ initMessage += "\n";
+ }
+ for (int i = 0; i < roomMonsterData.length; i++) {
+ List<int> normals = [];
+ List<int> elites = [];
+ final roomMonsters = roomMonsterData[i];
+ Monster data =
+ gs.currentList.firstWhereOrNull(
+ (element) => element.id == roomMonsters.name,
+ )
+ as Monster;
+
+ int eliteAmount = roomMonsters.elite[characterIndex];
+ int normalAmount = roomMonsters.normal[characterIndex];
+
+ bool isBoss = false;
+ if (data.type.levels.first.boss != null) {
+ isBoss = true;
+ }
+
+ for (int i = 0; i < eliteAmount; i++) {
+ int randomNr = GameMethods.getRandomStandee(data);
+ if (randomNr != 0) {
+ elites.add(randomNr);
+ executeAddStandee(
+ stateModifier,
+ randomNr,
+ null,
+ MonsterType.elite,
+ data.id,
+ false,
+ );
+ }
+ }
+
+ for (int i = 0; i < normalAmount; i++) {
+ int randomNr = GameMethods.getRandomStandee(data);
+ if (addSorted) {
+ randomNr = GameMethods.getNextAvailableBnBStandee(data);
+ }
+ if (randomNr != 0) {
+ normals.add(randomNr);
+ executeAddStandee(
+ stateModifier,
+ randomNr,
+ null,
+ isBoss ? MonsterType.boss : MonsterType.normal,
+ data.id,
+ false,
+ );
+ }
+ }
+
+ if (!addSorted && (elites.isNotEmpty || normals.isNotEmpty)) {
+ elites.sort();
+ normals.sort();
+ if (i != 0) {
+ initMessage += "\n";
+ }
+ initMessage += "${data.type.display} added - ";
+
+ if (elites.isNotEmpty) {
+ initMessage += "Elite: ";
+ for (int i = 0; i < elites.length; i++) {
+ initMessage += "${elites[i]}, ";
+ if (i == elites.length - 1) {
+ initMessage = initMessage.substring(
+ 0,
+ initMessage.length - _kTrailingCommaLength,
+ );
+ }
+ }
+ }
+ if (normals.isNotEmpty) {
+ if (!isBoss) {
+ if (elites.isNotEmpty) {
+ initMessage += ", ";
+ }
+ initMessage += "Normal: ";
+ }
+ for (int i = 0; i < normals.length; i++) {
+ initMessage += "${normals[i]}, ";
+ if (i == normals.length - 1) {
+ initMessage = initMessage.substring(
+ 0,
+ initMessage.length - _kTrailingCommaLength,
+ );
+ }
+ }
+ }
+ }
+ }
+ } else {
+ if (roomMonsterData.isNotEmpty) {
+ gs.pendingAutoAddDialog = roomMonsterData;
+ }
+ }
+ }
+ return initMessage;
+ }
+}
diff --git a/server/lib/core/methods/round_methods.dart b/server/lib/core/methods/round_methods.dart
new file mode 100644
index 0000000..4ad146d
--- /dev/null
+++ b/server/lib/core/methods/round_methods.dart
@@ -0,0 +1,493 @@
+part of '../state/game_state.dart';
+
+// ignore_for_file: library_private_types_in_public_api
+
+class RoundMethods {
+ static const int _kMaxLevel = 7;
+ static const int _kMinLevel = 0;
+ static void setRoundState(
+ _StateModifier _,
+ RoundState state, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._roundState = state;
+ }
+
+ static void setRound(_StateModifier _, int round, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._round = round;
+ gs._totalRounds++;
+ }
+
+ static void resetRound(
+ _StateModifier _,
+ int round,
+ bool resetTotal, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._round = round;
+ if (resetTotal) {
+ gs._totalRounds = round;
+ }
+ }
+
+ static void sortCharactersFirst(_StateModifier _, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._currentList.sort(_compareForCharactersFirst);
+ // Sorting compares note rows as initiative 0, scattering them; re-glue any
+ // linked notes back under their target. Callers that notify via
+ // updateList.notify() (e.g. DrawCommand) bypass _notifyCurrentList, so the
+ // reflow must happen here rather than only in _notifyCurrentList.
+ gs._reflowNoteRows();
+ }
+
+ static int _compareForCharactersFirst(ListItemData a, ListItemData b) {
+ final deadResult = _compareDeadCharacters(a, b);
+ if (deadResult != null) return deadResult;
+
+ final aIsChar = a is Character;
+ final bIsChar = b is Character;
+ if (aIsChar && bIsChar) return 0;
+ if (bIsChar) return 1;
+ if (aIsChar) return -1;
+
+ // both are monsters — inactive at bottom
+ if (a is Monster && b is Monster) {
+ if (a.isActive && !b.isActive) return -1;
+ if (!a.isActive && b.isActive) return 1;
+ }
+ return -1;
+ }
+
+ // Returns a sort result if either figure is a dead character (dead go last),
+ // or null if neither is dead and the caller should continue comparing.
+ static int? _compareDeadCharacters(ListItemData a, ListItemData b) {
+ final aIsDead = a is Character && a.characterState.health == 0;
+ final bIsDead = b is Character && b.characterState.health == 0;
+ if (!aIsDead && bIsDead) return -1;
+ if (aIsDead && !bIsDead) return 1;
+ if (aIsDead) return -1; // both dead characters
+ return null;
+ }
+
+ static void sortItemToPlace(
+ _StateModifier _,
+ String id,
+ int initiative, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ final newList = gs.currentList.toList();
+ ListItemData? item;
+ int currentTurnItemIndex = 0;
+ for (int i = 0; i < newList.length; i++) {
+ if (newList[i].turnState == TurnsState.current) {
+ currentTurnItemIndex = i;
+ }
+ if (newList[i].id == id) {
+ item = newList.removeAt(i);
+ }
+ }
+ if (item == null) {
+ return;
+ }
+
+ int init = 0;
+ for (int i = 0; i < newList.length; i++) {
+ ListItemData currentItem = newList[i];
+ int currentItemInitiative = GameMethods.getInitiative(currentItem);
+ if (currentItemInitiative > initiative && currentItemInitiative > init) {
+ if (i > currentTurnItemIndex) {
+ newList.insert(i, item);
+ gs._currentList = newList;
+ gs._notifyCurrentList();
+ return;
+ } else {
+ //in case initiative is earlier than current turn, ignore anything current turn, and earlier and place later
+ int insertIndex = currentTurnItemIndex + 1;
+ for (int j = currentTurnItemIndex + 1; j < newList.length; j++) {
+ if (GameMethods.getInitiative(newList[j]) >= initiative) {
+ insertIndex = j;
+ break;
+ }
+ }
+ if (insertIndex > newList.length) {
+ newList.add(item);
+ } else {
+ newList.insert(insertIndex, item);
+ }
+
+ gs._currentList = newList;
+ gs._notifyCurrentList();
+ return;
+ }
+ }
+ init =
+ currentItemInitiative; //this check is for the case user has moved items around the order may be off
+ }
+
+ newList.add(item);
+ gs._currentList = newList;
+ gs._notifyCurrentList();
+ }
+
+ static void sortByInitiative(_StateModifier _, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._currentList.sort((a, b) {
+ final deadResult = _compareDeadCharacters(a, b);
+ if (deadResult != null) return deadResult;
+
+ int aInitiative = 0;
+ int bInitiative = 0;
+ if (a is Character) {
+ aInitiative = a.characterState.initiative;
+ } else if (a is Monster) {
+ if (!a.isActive) {
+ if (b is Monster && !b.isActive) {
+ return -1;
+ }
+ return 1; //inactive at bottom
+ }
+
+ //find the deck
+ for (final item in gs.currentAbilityDecks) {
+ if (item.name == a.type.deck && item.discardPileIsNotEmpty) {
+ aInitiative = item.discardPileTop.initiative;
+ }
+ }
+ }
+ if (b is Character) {
+ bInitiative = b.characterState.initiative;
+ } else if (b is Monster) {
+ if (!b.isActive) {
+ if (a is Monster && !a.isActive) {
+ return 1;
+ }
+ return -1; //inactive at bottom
+ }
+ //find the deck
+ for (final item in gs.currentAbilityDecks) {
+ if (item.name == b.type.deck && item.discardPileIsNotEmpty) {
+ bInitiative = item.discardPileTop.initiative;
+ }
+ }
+ }
+ return aInitiative.compareTo(bInitiative);
+ });
+ // Re-glue linked note rows after the initiative sort (see sortCharactersFirst).
+ gs._reflowNoteRows();
+ }
+
+ static void sortMonsterInstances(
+ _StateModifier _,
+ List<MonsterInstance> instances,
+ ) {
+ instances.sort((a, b) {
+ if (a.type == MonsterType.elite && b.type != MonsterType.elite) {
+ return -1;
+ }
+ if (b.type == MonsterType.elite && a.type != MonsterType.elite) {
+ return 1;
+ }
+ return a.standeeNr.compareTo(b.standeeNr);
+ });
+ }
+
+ static void addToMainList(
+ _StateModifier _,
+ int? index,
+ ListItemData item, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ final newList = gs.currentList.toList();
+ if (index != null) {
+ newList.insert(index, item);
+ } else {
+ newList.add(item);
+ }
+ gs._currentList = newList;
+ gs._notifyCurrentList();
+ }
+
+ static void reorderMainList(
+ _StateModifier _,
+ int newIndex,
+ int oldIndex, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._currentList.insert(newIndex, gs._currentList.removeAt(oldIndex));
+ gs._notifyCurrentList();
+ }
+
+ static void removeFromMainList(
+ _StateModifier _,
+ String id, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._currentList = gs._currentList.where((item) => item.id != id).toList();
+ gs._notifyCurrentList();
+ }
+
+ /// Deletes any note rows tied to a specific standee ([standeeNr] > 0) of the
+ /// monster [linkedId] — called when that standee is defeated so its note goes
+ /// with it. Notes for the whole group (standeeNr 0) are left alone.
+ static void removeNoteRowsForStandee(
+ _StateModifier _,
+ String linkedId,
+ int standeeNr, {
+ GameState? gameState,
+ }) {
+ if (standeeNr <= 0) return;
+ final gs = gameState ?? getIt<GameState>();
+ final filtered = gs._currentList
+ .where(
+ (item) =>
+ !(item is NoteRow &&
+ item.linkedId == linkedId &&
+ item.standeeNr == standeeNr),
+ )
+ .toList();
+ if (filtered.length != gs._currentList.length) {
+ gs._currentList = filtered;
+ gs._notifyCurrentList();
+ }
+ }
+
+ static void updateForSpecialRules(
+ _StateModifier _, {
+ GameState? gameState,
+ GameData? gameData,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ final gd = gameData ?? getIt<GameData>();
+ List<SpecialRule>? rules =
+ gd.modelData[gs.currentCampaign]?.scenarios[gs.scenario]?.specialRules;
+ if (rules != null) {
+ for (SpecialRule rule in rules) {
+ if (rule.type == "Objective" || rule.type == "Escort") {
+ Character? character =
+ gs.currentList.firstWhereOrNull(
+ (element) => element.id == rule.name,
+ )
+ as Character?;
+ if (character != null) {
+ int? newHealth = StatCalculator.calculateFormula(
+ rule.health.toString(),
+ );
+ if (newHealth != character.characterState.maxHealth &&
+ newHealth != null) {
+ character.characterState._maxHealth = newHealth;
+ character.characterState._health = newHealth;
+ }
+ }
+ } else if (rule.type == "LevelAdjust") {
+ Monster? monster =
+ gs.currentList.firstWhereOrNull(
+ (element) => element.id == rule.name,
+ )
+ as Monster?;
+ if (monster != null) {
+ if (gs.level == monster.level) {
+ int newLevel = (monster.level + rule.level).clamp(
+ _kMinLevel,
+ _kMaxLevel,
+ );
+ monster._level = newLevel;
+ for (MonsterInstance instance in monster._monsterInstances) {
+ instance._setLevel(monster);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ static void clearTurnStateConditions(
+ _StateModifier _,
+ FigureState figure,
+ bool clearLastTurnToo,
+ ) {
+ if (!clearLastTurnToo) {
+ figure._conditionsAddedPreviousTurn.clear();
+ figure._conditionsAddedPreviousTurn.addAll(
+ figure.conditionsAddedThisTurn.toSet(),
+ );
+ } else {
+ figure._conditionsAddedPreviousTurn.clear();
+ }
+
+ figure._conditionsAddedThisTurn.clear();
+
+ figure._chill = figure.conditions.where((a) => a == Condition.chill).length;
+ }
+
+ static void clearTurnState(
+ _StateModifier stateModifier,
+ bool clearLastTurnToo, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ for (final item in gs._currentList) {
+ item._turnState = TurnsState.notDone;
+ if (item is Character) {
+ clearTurnStateConditions(
+ stateModifier,
+ item.characterState,
+ clearLastTurnToo,
+ );
+ for (final instance in item.characterState._summonList) {
+ clearTurnStateConditions(stateModifier, instance, clearLastTurnToo);
+ }
+ } else if (item is Monster) {
+ for (final instance in item._monsterInstances) {
+ clearTurnStateConditions(stateModifier, instance, clearLastTurnToo);
+ }
+ }
+ }
+ }
+
+ static void removeExpiringConditions(
+ _StateModifier _,
+ FigureState figure, {
+ Settings? settings,
+ }) {
+ if ((settings ?? getIt<Settings>()).expireConditions) {
+ final conditions = figure._conditions;
+
+ //handle chill separately
+ int nrOfChills = figure.conditions
+ .where((a) => a == Condition.chill)
+ .length;
+ if (nrOfChills > 0) {
+ //remove one if not added this turn
+ if (!figure.conditionsAddedThisTurn.contains(Condition.chill) ||
+ nrOfChills >
+ 1 //this is a cop out: you can add more than one chill in a round, but since we store only if a condition has been added this turn, and not the amount, we have no way to tell
+ ) {
+ conditions.remove(Condition.chill);
+ figure._conditionsAddedPreviousTurn.add(Condition.chill);
+ }
+ }
+
+ figure._chill = figure.conditions
+ .where((a) => a == Condition.chill)
+ .length;
+
+ for (int i = conditions.length - 1; i >= 0; i--) {
+ Condition item = conditions[i];
+ if (GameMethods.canExpire(item)) {
+ if (item != Condition.chill) {
+ if (!figure.conditionsAddedThisTurn.contains(item)) {
+ conditions.removeAt(i);
+ figure._conditionsAddedPreviousTurn.add(item);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ static void removeExpiringConditionsFromListItem(
+ _StateModifier s,
+ ListItemData item,
+ ) {
+ if (item is Character) {
+ removeExpiringConditions(s, item.characterState);
+ for (final summon in item.characterState._summonList) {
+ removeExpiringConditions(s, summon);
+ }
+ } else if (item is Monster) {
+ for (final instance in item._monsterInstances) {
+ removeExpiringConditions(s, instance);
+ }
+ }
+ }
+
+ static void reapplyConditions(_StateModifier _, FigureState figure) {
+ for (final condition in figure.conditionsAddedPreviousTurn) {
+ final conditions = figure._conditions;
+ if (!conditions.contains(condition) || condition == Condition.chill) {
+ conditions.add(condition);
+ figure._conditionsAddedThisTurn.remove(condition);
+ }
+ if (condition == Condition.chill) {
+ figure._chill = figure.conditions
+ .where((a) => a == Condition.chill)
+ .length;
+ }
+ }
+ }
+
+ static void reapplyConditionsFromListItem(
+ _StateModifier s,
+ ListItemData item,
+ ) {
+ if (item is Character) {
+ reapplyConditions(s, item.characterState);
+ for (final summon in item.characterState.summonList) {
+ reapplyConditions(s, summon);
+ }
+ } else if (item is Monster) {
+ for (final instance in item._monsterInstances) {
+ reapplyConditions(s, instance);
+ }
+ }
+ }
+
+ //1 if item WAS done OR not done, then set it to current, all before to done, and all after to not done
+ //2 if item was current: set item to done, all before to done, next to current and rest to not done
+ static void setTurnDone(_StateModifier s, int index, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ //set all before to done.
+ for (int i = 0; i < index; i++) {
+ if (gs.currentList[i].turnState != TurnsState.done) {
+ gs.currentList[i]._turnState = TurnsState.done;
+ removeExpiringConditionsFromListItem(s, gs.currentList[i]);
+ }
+ }
+ //if on index is NOT current then set to current else set to done
+ int newIndex = index + 1;
+ if (gs.currentList[index].turnState == TurnsState.current) {
+ gs.currentList[index]._turnState = TurnsState.done;
+ removeExpiringConditionsFromListItem(s, gs.currentList[index]);
+ //remove expiring conditions
+ } else {
+ newIndex = index;
+ }
+
+ //get next active item and set to current
+ for (; newIndex < gs.currentList.length; newIndex++) {
+ ListItemData data = gs.currentList[newIndex];
+ if (data is Monster) {
+ if (data.isActive && !GameMethods.isInactiveForRule(data.type.name)) {
+ if (data.turnState == TurnsState.done) {
+ reapplyConditionsFromListItem(s, data);
+ }
+ data._turnState = TurnsState.current;
+ break;
+ }
+ } else if (data is Character) {
+ if (data.characterState.health > 0) {
+ if (data.turnState == TurnsState.done) {
+ reapplyConditionsFromListItem(s, data);
+ }
+ data._turnState = TurnsState.current;
+ break;
+ }
+ }
+ }
+ //set rest to not done
+ for (int i = newIndex + 1; i < gs.currentList.length; i++) {
+ if (gs.currentList[i].turnState == TurnsState.done) {
+ reapplyConditionsFromListItem(s, gs.currentList[i]);
+ }
+ gs.currentList[i]._turnState = TurnsState.notDone;
+ }
+ }
+}
diff --git a/server/lib/core/methods/scenario_methods.dart b/server/lib/core/methods/scenario_methods.dart
new file mode 100644
index 0000000..257d076
--- /dev/null
+++ b/server/lib/core/methods/scenario_methods.dart
@@ -0,0 +1,530 @@
+part of '../state/game_state.dart';
+
+// ignore_for_file: library_private_types_in_public_api
+
+class ScenarioMethods {
+ static const int _kMinLevel = 0;
+ static const int _kMaxLevel = 7;
+ static const int _kRound1 = 1;
+ static const int _kTimerAlways = -1;
+ static const int _kRandomSectionCount = 3;
+
+ static void setCampaign(
+ _StateModifier _,
+ String campaign, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._currentCampaign = campaign;
+ }
+
+ static void setSolo(_StateModifier _, bool solo, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._solo = solo;
+ }
+
+ static void unlockClass(
+ _StateModifier _,
+ String name, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._unlockedClasses.add(name);
+ gs._unlockedClassesVersion++;
+ }
+
+ static void clearUnlockedClasses(_StateModifier _, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._unlockedClasses = {};
+ gs._unlockedClassesVersion++;
+ }
+
+ static void clearUnlockedClass(
+ _StateModifier _,
+ String id, {
+ GameState? gameState,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ gs._unlockedClasses.remove(id);
+ gs._unlockedClassesVersion++;
+ }
+
+ static void setLevel(
+ _StateModifier s,
+ int level,
+ String? monsterId, {
+ GameState? gameState,
+ }) {
+ assert(level >= _kMinLevel && level <= _kMaxLevel);
+ final gs = gameState ?? getIt<GameState>();
+ if (monsterId == null) {
+ gs._level = level;
+ for (final item in gs.currentList) {
+ if (item is Monster) {
+ item.setLevel(s, level);
+ }
+ }
+ RoundMethods.updateForSpecialRules(s);
+ } else {
+ Monster? monster;
+ for (final item in gs.currentList) {
+ if (item.id == monsterId) {
+ monster = item as Monster;
+ }
+ }
+ monster?.setLevel(s, level);
+ }
+ }
+
+ static void applyDifficulty(_StateModifier s, {GameState? gameState}) {
+ final gs = gameState ?? getIt<GameState>();
+ if (gs.autoScenarioLevel) {
+ int newLevel = GameMethods.getRecommendedLevel() + gs.difficulty;
+ if (newLevel > _kMaxLevel) {
+ newLevel = _kMaxLevel;
+ }
+ setLevel(s, newLevel, null);
+ }
+ }
+
+ static void _resetForNewScenario(
+ _StateModifier s,
+ String scenario,
+ GameState gs,
+ GameData gd,
+ ) {
+ RoundMethods.resetRound(s, 1, true);
+ gs._showAllyDeck = false;
+ gs._currentAbilityDecks.clear();
+ gs._scenarioSpecialRules.clear();
+ applyDifficulty(s);
+
+ gs.modifierDeck._initDeck();
+ gs.modifierDeckAllies._initDeck();
+ gs._sanctuaryDeck._initDeck();
+
+ RoundMethods.setRoundState(s, RoundState.chooseInitiative);
+ gs._scenario = scenario;
+ gs._scenarioSectionsAdded = [];
+
+ List<ListItemData> newList = [];
+ for (final item in gs.currentList) {
+ if (item is Character) {
+ if (!GameMethods.isObjectiveOrEscort(item.characterClass)) {
+ CharacterMethods.resetCharacter(s, item);
+ newList.add(item);
+ }
+ }
+ }
+ gs._currentList = newList;
+
+ _initLootDeck(scenario, gs, gd);
+
+ RoundMethods.clearTurnState(s, true);
+ gs._toastMessage = "";
+ }
+
+ static void _initLootDeck(String scenario, GameState gs, GameData gd) {
+ if (scenario != "custom") {
+ final campaignModel = gd.modelData[gs.currentCampaign];
+ LootDeckModel? lootDeckModel =
+ campaignModel?.scenarios[scenario]?.lootDeck;
+ lootDeckModel != null
+ ? gs._lootDeck = LootDeck(lootDeckModel, gs.lootDeck)
+ : gs._lootDeck = LootDeck.from(gs.lootDeck);
+ } else {
+ if (gs.currentCampaign == "Frosthaven") {
+ LootDeckModel? lootDeckModel = const LootDeckModel(
+ 2,
+ 2,
+ 2,
+ 12,
+ 1,
+ 1,
+ 1,
+ 1,
+ 1,
+ 1,
+ 0,
+ );
+ gs._lootDeck = LootDeck(lootDeckModel, gs.lootDeck);
+ } else {
+ gs._lootDeck = LootDeck.from(gs.lootDeck);
+ }
+ }
+ }
+
+ static ({
+ List<String> monsters,
+ List<SpecialRule> specialRules,
+ List<RoomMonsterData> roomMonsterData,
+ List<String> subSections,
+ String initMessage,
+ })
+ _loadData(
+ bool section,
+ String scenario,
+ GameState gs,
+ GameData gd,
+ Settings? settings,
+ ) {
+ List<String> monsters = [];
+ List<SpecialRule> specialRules = [];
+ List<RoomMonsterData> roomMonsterData = [];
+ List<String> subSections = [];
+ String initMessage = "";
+
+ if (section) {
+ final sectionData = gd
+ .modelData[gs.currentCampaign]
+ ?.scenarios[gs.scenario]
+ ?.sections
+ .firstWhereOrNull((element) => element.name == scenario);
+ if (sectionData != null) {
+ monsters = sectionData.monsters;
+ specialRules = sectionData.specialRules.toList();
+ initMessage = sectionData.initMessage;
+ final monsterStandees = sectionData.monsterStandees;
+ roomMonsterData = monsterStandees != null
+ ? monsterStandees.toList()
+ : [];
+ }
+ } else {
+ if ((settings ?? getIt<Settings>()).showBattleGoalReminder &&
+ gs.currentCampaign != "Buttons and Bugs") {
+ initMessage += "Remember to choose your Battle Goals.";
+ }
+ if (scenario != "custom") {
+ final scenarioData =
+ gd.modelData[gs.currentCampaign]?.scenarios[scenario];
+ if (scenarioData != null) {
+ monsters = scenarioData.monsters;
+ specialRules = scenarioData.specialRules.toList();
+ initMessage +=
+ initMessage.isNotEmpty && scenarioData.initMessage.isNotEmpty
+ ? "\n\n${scenarioData.initMessage}"
+ : scenarioData.initMessage;
+ final monsterStandees = scenarioData.monsterStandees;
+ roomMonsterData = monsterStandees != null
+ ? monsterStandees.toList()
+ : [];
+ for (final item in scenarioData.sections) {
+ subSections.add(item.name);
+ }
+ }
+ }
+ }
+
+ return (
+ monsters: monsters,
+ specialRules: specialRules,
+ roomMonsterData: roomMonsterData,
+ subSections: subSections,
+ initMessage: initMessage,
+ );
+ }
+
+ // Returns false if setScenario should abort early (banner spear solo edge case).
+ static bool _applyBannerSpearHack(String scenario, GameState gs) {
+ if (!scenario.contains("Scouting Ambush")) return true;
+ final deck = gs.currentAbilityDecks.firstWhereOrNull(
+ (element) => element.name.contains("Scout"),
+ );
+ if (deck == null) return false;
+ final drawPileList = deck._drawPile.getList();
+ for (int i = 0; i < drawPileList.length; i++) {
+ if (drawPileList[i].title == "Rancid Arrow") {
+ deck._drawPile.add(deck._drawPile.removeAt(i));
+ break;
+ }
+ }
+ return true;
+ }
+
+ static String _processSpecialRules(
+ _StateModifier s,
+ List<SpecialRule> specialRules,
+ bool section,
+ GameState gs,
+ String initMessage,
+ Settings? settings,
+ ) {
+ for (final item in specialRules) {
+ if (item.type == "AllyDeck") {
+ gs._showAllyDeck = true;
+ }
+ if (item.type == "Objective") {
+ if (item.condition == "" ||
+ StatCalculator.evaluateCondition(item.condition as Object)) {
+ Character? objective = CharacterMethods.createCharacter(
+ s,
+ "Objective",
+ null,
+ item.name,
+ gs.level + 1,
+ );
+ final health = StatCalculator.calculateFormula(
+ item.health.toString(),
+ );
+ if (health != null) {
+ objective?.characterState._maxHealth = health;
+ }
+ objective?.characterState._health =
+ objective.characterState.maxHealth;
+ objective?.characterState._initiative = item.init;
+ bool add = true;
+ for (final item2 in gs.currentList) {
+ if (item2 is Character &&
+ (item2).characterState.display == item.name) {
+ add = false;
+ break;
+ }
+ }
+ if (add && objective != null) {
+ gs._currentList.add(objective);
+ }
+ }
+ }
+ if (item.type == "Escort") {
+ if (item.condition == "" ||
+ StatCalculator.evaluateCondition(item.condition as Object)) {
+ final objective = CharacterMethods.createCharacter(
+ s,
+ "Escort",
+ null,
+ item.name,
+ gs.level + 1,
+ );
+ if (objective != null) {
+ final maxHealth = StatCalculator.calculateFormula(
+ item.health.toString(),
+ );
+ if (maxHealth != null) {
+ objective.characterState._maxHealth = maxHealth;
+ }
+ objective.characterState._health =
+ objective.characterState.maxHealth;
+ objective.characterState._initiative = item.init;
+ bool add = true;
+ for (final item2 in gs.currentList) {
+ if (item2 is Character &&
+ (item2).characterState.display == item.name) {
+ add = false;
+ break;
+ }
+ }
+ if (add) {
+ gs._currentList.add(objective);
+ }
+ }
+ }
+ }
+ if (!section && item.type == "Timer" && item.startOfRound) {
+ for (int round in item.list.cast<int>()) {
+ if (round == _kRound1 || round == _kTimerAlways) {
+ initMessage += initMessage.isNotEmpty
+ ? "\n\n${item.note}"
+ : item.note;
+ }
+ }
+ }
+ if (item.type == "ResetRound") {
+ RoundMethods.resetRound(s, 1, false);
+ }
+ if (item.type == "Unlock") {
+ unlockClass(s, item.name);
+ initMessage += item.note;
+ }
+ }
+ return initMessage;
+ }
+
+ static List<RoomMonsterData> _applyRound1Spawns(
+ List<RoomMonsterData> roomMonsterData,
+ List<SpecialRule> specialRules,
+ String scenario,
+ GameState gs,
+ GameData gd,
+ Settings? settings,
+ ) {
+ for (final rule in specialRules) {
+ if (rule.type != "Timer" || !rule.startOfRound) continue;
+ for (final round in rule.list.cast<int>()) {
+ if (round != _kRound1 && round != _kTimerAlways) continue;
+ if (!(settings ?? getIt<Settings>()).autoAddSpawns) continue;
+ if (rule.name.isEmpty) continue;
+
+ final scenarioModel =
+ gd.modelData[gs.currentCampaign]?.scenarios[scenario];
+ if (scenarioModel == null) continue;
+
+ final spawnSection = scenarioModel.sections.firstWhereOrNull(
+ (element) => element.name.substring(1) == rule.name,
+ );
+ final spawnStandees = spawnSection?.monsterStandees;
+ if (spawnStandees == null) continue;
+
+ for (final spawnItem in spawnStandees) {
+ final existing = roomMonsterData.firstWhereOrNull(
+ (element) => element.name == spawnItem.name,
+ );
+ if (existing != null) {
+ final merged = RoomMonsterData(
+ existing.name,
+ [
+ existing.normal.first + spawnItem.normal.first,
+ existing.normal[1] + spawnItem.normal[1],
+ existing.normal[2] + spawnItem.normal[2],
+ ],
+ [
+ existing.elite.first + spawnItem.elite.first,
+ existing.elite[1] + spawnItem.elite[1],
+ existing.elite[2] + spawnItem.elite[2],
+ ],
+ );
+ for (int i = 0; i < roomMonsterData.length; i++) {
+ if (roomMonsterData[i].name == existing.name) {
+ roomMonsterData[i] = merged;
+ break;
+ }
+ }
+ } else {
+ roomMonsterData.add(spawnItem);
+ }
+ }
+ }
+ }
+ return roomMonsterData;
+ }
+
+ static void _updateScenarioRules(
+ _StateModifier s,
+ bool section,
+ String scenario,
+ List<SpecialRule> specialRules,
+ List<String> subSections,
+ GameState gs,
+ ) {
+ if (!section) {
+ gs._scenarioSpecialRules = specialRules;
+ ElementMethods.resetElements(s);
+ RoundMethods.sortCharactersFirst(s);
+ } else {
+ if (specialRules.firstWhereOrNull(
+ (element) => element.type == "ResetRound",
+ ) !=
+ null) {
+ gs._scenarioSpecialRules.removeWhere(
+ (oldItem) => oldItem.type == "Timer",
+ );
+ }
+ for (final item in specialRules) {
+ if (item.type == "Timer") {
+ gs._scenarioSpecialRules.removeWhere((oldItem) {
+ if (oldItem.type == "Timer" &&
+ item.startOfRound == oldItem.startOfRound) {
+ if (item.list.contains(-1) || oldItem.list.contains(-1)) {
+ return true;
+ }
+ final set2 = oldItem.list.toSet();
+ return item.list.any(set2.contains);
+ }
+ return false;
+ });
+ }
+ }
+ gs._scenarioSpecialRules.addAll(specialRules);
+ gs._scenarioSectionsAdded.add(scenario);
+ }
+ gs._scenarioSectionsVersion++;
+
+ final randomRule = specialRules.firstWhereOrNull(
+ (element) => element.type == "RandomSections",
+ );
+ if (randomRule != null) {
+ subSections.shuffle();
+ final newRule = SpecialRule(
+ "RandomSections",
+ "",
+ 0,
+ 0,
+ 0,
+ "",
+ subSections.sublist(0, _kRandomSectionCount),
+ false,
+ "",
+ );
+ specialRules.remove(randomRule);
+ specialRules.add(newRule);
+ }
+ }
+
+ static void setScenario(
+ _StateModifier s,
+ String scenario,
+ bool section, {
+ GameState? gameState,
+ GameData? gameData,
+ Settings? settings,
+ }) {
+ final gs = gameState ?? getIt<GameState>();
+ final gd = gameData ?? getIt<GameData>();
+
+ if (!section) {
+ _resetForNewScenario(s, scenario, gs, gd);
+ }
+
+ final data = _loadData(section, scenario, gs, gd, settings);
+
+ for (final monster in data.monsters) {
+ MonsterMethods.addMonster(s, monster, data.specialRules);
+ }
+ if (!section) {
+ DeckMethods.shuffleDecks(s);
+ }
+
+ if (!_applyBannerSpearHack(scenario, gs)) return;
+
+ final initMessage = _processSpecialRules(
+ s,
+ data.specialRules,
+ section,
+ gs,
+ data.initMessage,
+ settings,
+ );
+
+ final roomMonsterData = _applyRound1Spawns(
+ data.roomMonsterData,
+ data.specialRules,
+ scenario,
+ gs,
+ gd,
+ settings,
+ );
+
+ final finalInitMessage = MonsterMethods.autoAddStandees(
+ s,
+ roomMonsterData,
+ initMessage,
+ );
+
+ _updateScenarioRules(
+ s,
+ section,
+ scenario,
+ data.specialRules,
+ data.subSections,
+ gs,
+ );
+
+ gs._notifyCurrentList();
+
+ if (finalInitMessage.isNotEmpty &&
+ (settings ?? getIt<Settings>()).showReminders) {
+ gs._toastMessage += finalInitMessage;
+ } else {
+ gs._toastMessage = "";
+ }
+ }
+}