From ec3eb157315e48ce087c7f2aaa500f39f7ea2d2e Mon Sep 17 00:00:00 2001 From: Nate Bowman Date: Sun, 18 Oct 2026 17:44:17 -0400 Subject: [PATCH] Simplify running more than one fight at a time This still requires too much manual work, so I probably will change things up again, but I wanted to have a record just in case. --- example-fighting.py | 66 +++++++++++++++++++----------------- flashcards/constants.py | 75 +++++++++++++++++++++++++++++++++++++++++ flashcards/fighting.py | 56 ++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 30 deletions(-) create mode 100644 flashcards/constants.py diff --git a/example-fighting.py b/example-fighting.py index b3b8ed6..84e7455 100644 --- a/example-fighting.py +++ b/example-fighting.py @@ -1,9 +1,11 @@ +import itertools + +from flashcards.constants import UnitLines, ranges from flashcards.maps import (create_maps, read_data_to_dict, read_name_data_to_dict) -from flashcards.fighting import (generate_fight_results, - get_bonuses_from_names) -from flashcards.types import ArmorClasses, BonusTypes +from flashcards.fighting import (generate_all_combinations, + generate_fight_results_from_names) @@ -17,39 +19,43 @@ namelist = 'units-buildings-trimmed.txt' name_json_keys = 'units_buildings' data_json_keys = ('data', 'Unit') -_, nums = create_maps(namelist, name_json_keys) -data = read_data_to_dict(data_json_keys) - # --- Fight details start here -attacker = 'Spearman' -defender = 'Scout Cavalry' -bonus_range = range(-3, 5) -armor_class = ArmorClasses.MELEE -notes = 'with Bloodlines' -suffix = '' +attacker_line = 'Spearman' +defender_line = 'Knight' +#defender_line = 'Scout Cavalry' +range_type = 'standard' -hp_bonus_names = ['Bloodlines'] +# extra bonuses beyond the usual blacksmith upgrades and bloodlines +hp_bonus_names = [] attack_bonus_names = [] armor_bonus_names = [] -# --- You shouldn't need to change below this line +# --- Shouldn't need to change below this line -hp_bonuses = get_bonuses_from_names(BonusTypes.HP, hp_bonus_names) -attack_bonuses = get_bonuses_from_names(BonusTypes.ATTACK, attack_bonus_names) -armor_bonuses = get_bonuses_from_names(BonusTypes.ARMOR, armor_bonus_names) +_, nums = create_maps(namelist, name_json_keys) +data = read_data_to_dict(data_json_keys) -results = generate_fight_results(attacker, - defender, - armor_class, - bonus_range, - data, - nums, - notes=notes, - extra_attack_bonuses=attack_bonuses, - extra_armor_bonuses=armor_bonuses, - hp_bonuses=hp_bonuses, - verbose=True) +attack_type = UnitLines.get_attack(attacker_line) +bonus_range = ranges[attack_type][range_type] -# --- TODO write file -print(results) +built_in_hp_bonus_names = UnitLines.get_hp_bonus_names(defender_line) +all_hp_bonus_names = hp_bonus_names + built_in_hp_bonus_names +bonus_names_combinations = generate_all_combinations(attack_bonus_names, + armor_bonus_names, + all_hp_bonus_names) + +for attacker, defender in itertools.product(UnitLines.get_names(attacker_line), + UnitLines.get_names(defender_line)): + for combo in bonus_names_combinations: + results = generate_fight_results_from_names(attacker, + defender, + attack_type, + bonus_range, + data, + nums, + combo[0], + combo[1], + combo[2], + True) + print(results) diff --git a/flashcards/constants.py b/flashcards/constants.py new file mode 100644 index 0000000..1ee9a38 --- /dev/null +++ b/flashcards/constants.py @@ -0,0 +1,75 @@ +from flashcards.types import ArmorClasses + + + +ranges = {ArmorClasses.MELEE: { + 'standard': range(-3, 5), + 'parthian tactics (armor)': range(-3, 5), + 'villager': range(-3, 5), + }, + ArmorClasses.PIERCE: { + 'standard': range(-4, 5), + 'parthian tactics (armor)': range(-6, 5), + 'parthian tactics (attack)': range(-4, 7), + 'gambesons': range(-5, 5), + 'villager': range(-2, 5), + } + } + + + +class UnitLines(): + + lines = {'Scout Cavalry': { + 'names': [ + 'Scout Cavalry', + 'Light Cavalry', + 'Hussar' + ], + 'attack_type': ArmorClasses.MELEE, + 'hp_bonus_names': ['Bloodlines'], + }, + 'Spearman': { + 'names': [ + 'Spearman', + 'Pikeman', + 'Halberdier' + ], + 'attack_type': ArmorClasses.MELEE, + }, + 'Knight': { + 'names': [ + 'Knight', + 'Cavalier', + 'Paladin' + ], + 'attack_type': ArmorClasses.MELEE, + 'hp_bonus_names': ['Bloodlines'], + }, + } + + + @classmethod + def get_names(cls, name: str) -> list(str): + line = cls.lines[name] + try: + names = line['names'] + except KeyError: + names = [name] + print(names) + return names + + + @classmethod + def get_attack(cls, name: str) -> ArmorClasses: + return cls.lines[name]['attack_type'] + + + @classmethod + def get_hp_bonus_names(cls, name: str) -> list(str): + line = cls.lines[name] + try: + bonuses = line['hp_bonus_names'] + except KeyError: + bonuses = [] + return bonuses diff --git a/flashcards/fighting.py b/flashcards/fighting.py index 0710a2e..102be15 100644 --- a/flashcards/fighting.py +++ b/flashcards/fighting.py @@ -1,8 +1,10 @@ +import itertools import json import math from copy import deepcopy from flashcards.config import config +from flashcards.types import BonusTypes @@ -55,6 +57,28 @@ def get_bonuses_from_names(clss, names): return bonuses +def generate_notes(bonuses): + if len(bonuses) > 0: + notes = f'with {" and ".join(bonuses)}' + else: + notes = '' + return notes + + +def _powerset(elements): + for size in range(len(elements) + 1): + for combo in itertools.combinations(elements, size): + yield list(combo) + + +def generate_all_combinations(attack, armor, hp): + attack_combos = _powerset(attack) + armor_combos = _powerset(armor) + hp_combos = _powerset(hp) + # must be a list; an iterator will be 'used up' after first time through + return list(itertools.product(attack_combos, armor_combos, hp_combos)) + + def calculate_hits_to_kill(attacker, defender, data, @@ -128,3 +152,35 @@ def generate_fight_results(attacker, results['notes'] = notes return results + + +def generate_fight_results_from_names(attacker, + defender, + armor_class, + bonus_range, + data, + nums, + attack_bonus_names, + armor_bonus_names, + hp_bonus_names, + verbose): + + attack_bonuses = get_bonuses_from_names(BonusTypes.ATTACK, + attack_bonus_names) + armor_bonuses = get_bonuses_from_names(BonusTypes.ARMOR, + armor_bonus_names) + hp_bonuses = get_bonuses_from_names(BonusTypes.HP, + hp_bonus_names) + notes = generate_notes(hp_bonus_names) + + return generate_fight_results(attacker, + defender, + armor_class, + bonus_range, + data, + nums, + notes=notes, + extra_attack_bonuses=attack_bonuses, + extra_armor_bonuses=armor_bonuses, + hp_bonuses=hp_bonuses, + verbose=verbose)