From 80bb9790bf42925e3291f5d729e586ca1e106272 Mon Sep 17 00:00:00 2001 From: Nate Bowman Date: Tue, 27 Oct 2026 19:51:53 -0400 Subject: [PATCH] Remove fighting files from module Moved them to their own module. Must now import that module to use any of the fighting functions. --- data/bonuses.json | 150 ---------------- flashcards/config.py | 1 - flashcards/constants.py | 141 --------------- flashcards/fighting.py | 337 ------------------------------------ flashcards/types.py | 14 -- scripts/example-fighting.py | 22 --- 6 files changed, 665 deletions(-) delete mode 100644 data/bonuses.json delete mode 100644 flashcards/constants.py delete mode 100644 flashcards/fighting.py delete mode 100644 flashcards/types.py delete mode 100644 scripts/example-fighting.py diff --git a/data/bonuses.json b/data/bonuses.json deleted file mode 100644 index 5c6a8a0..0000000 --- a/data/bonuses.json +++ /dev/null @@ -1,150 +0,0 @@ -{ - "armor": { - "Parthian Tactics": [ - {"Amount": 1, "Class": 4}, - {"Amount": 2, "Class": 3} - ], - "Gambesons": [ - {"Amount": 1, "Class": 3} - ], - "Padded Archer Armor": [ - {"Amount": 1, "Class": 4}, - {"Amount": 1, "Class": 3} - ], - "Leather Archer Armor": [ - {"Amount": 1, "Class": 4}, - {"Amount": 1, "Class": 3} - ], - "Ring Archer Armor": [ - {"Amount": 1, "Class": 4}, - {"Amount": 2, "Class": 3} - ], - "Scale Barding Armor": [ - {"Amount": 1, "Class": 4}, - {"Amount": 1, "Class": 3} - ], - "Chain Barding Armor": [ - {"Amount": 1, "Class": 4}, - {"Amount": 1, "Class": 3} - ], - "Plate Barding Armor": [ - {"Amount": 1, "Class": 4}, - {"Amount": 2, "Class": 3} - ], - "Scale Mail Armor": [ - {"Amount": 1, "Class": 4}, - {"Amount": 1, "Class": 3} - ], - "Chain Mail Armor": [ - {"Amount": 1, "Class": 4}, - {"Amount": 1, "Class": 3} - ], - "Plate Mail Armor": [ - {"Amount": 1, "Class": 4}, - {"Amount": 2, "Class": 3} - ], - "Masonry": [ - {"Amount": 1, "Class": 4}, - {"Amount": 1, "Class": 3}, - {"Amount": 3, "Class": 11} - ], - "Architecture": [ - {"Amount": 1, "Class": 4}, - {"Amount": 1, "Class": 3}, - {"Amount": 3, "Class": 11} - ], - "Careening": [ - {"Amount": 1, "Class": 3} - ], - "Dry Dock": [ - {"Amount": 1, "Class": 3} - ], - "Loom": [ - {"Amount": 1, "Class": 4}, - {"Amount": 2, "Class": 3} - ] - }, - "attack": { - "Parthian Tactics": [ - {"Amount": 2, "Class": 3} - ], - "Arson": [ - {"Amount": 2, "Class": 16} - ], - "Forging": [ - {"Amount": 1, "Class": 4} - ], - "Iron Casting": [ - {"Amount": 1, "Class": 4} - ], - "Blast Furnace": [ - {"Amount": 2, "Class": 4} - ], - "Fletching": [ - {"Amount": 1, "Class": 3} - ], - "Bodkin Arrow": [ - {"Amount": 1, "Class": 3} - ], - "Bracer": [ - {"Amount": 1, "Class": 3} - ], - "Chemistry": [ - {"Amount": 1, "Class": 3} - ], - "Heated Shot (Castle, Krepost, or Harbor)": [ - {"Amount": 4, "Class": 16} - ], - "Sappers": [ - {"Amount": 15, "Class": 11}, - {"Amount": 15, "Class": 13}, - {"Amount": 3, "Class": 17} - ], - "Arrowslits (Watch Tower)": [ - {"Amount": 1, "Class": 3} - ], - "Arrowslits (Guard Tower)": [ - {"Amount": 2, "Class": 3} - ], - "Arrowslits (Keep or Donjon)": [ - {"Amount": 3, "Class": 3} - ], - "Siege Engineers (siege)": [ - {"Percent": 20, "Class": 11}, - {"Percent": 20, "Class": 13}, - {"Percent": 20, "Class": 21}, - {"Percent": 20, "Class": 22}, - {"Percent": 20, "Class": 26} - ], - "Siege Engineers (demo)": [ - {"Percent": 40, "Class": 11}, - {"Percent": 40, "Class": 13}, - {"Percent": 40, "Class": 21}, - {"Percent": 40, "Class": 22}, - {"Percent": 40, "Class": 26} - ], - "Heated Shot (Tower)": [ - {"Percent": 125, "Class": 16} - ] - }, - "hp": { - "Bloodlines": [ - {"Amount": 20, "Class": -1} - ], - "Hoardings": [ - {"Amount": 1000, "Class": -1} - ], - "Sanctity": [ - {"Amount": 15, "Class": -1} - ], - "Loom": [ - {"Amount": 15, "Class": -1} - ], - "Masonry": [ - {"Percent": 10, "Class": -1} - ], - "Architecture": [ - {"Percent": 10, "Class": -1} - ] - } -} diff --git a/flashcards/config.py b/flashcards/config.py index 88b83af..5ea0332 100644 --- a/flashcards/config.py +++ b/flashcards/config.py @@ -13,7 +13,6 @@ output_path = Path(c['storage']['output']) config = SimpleNamespace() config.data_file = data_path / 'data.json' -config.bonus_data_file = data_path / 'bonuses.json' config.name_data_file = data_path / 'name_data.json' config.string_data_file = data_path / 'strings.json' config.namelists_dir = output_path / 'namelists' diff --git a/flashcards/constants.py b/flashcards/constants.py deleted file mode 100644 index 6bb8ec2..0000000 --- a/flashcards/constants.py +++ /dev/null @@ -1,141 +0,0 @@ -from flashcards.types import ArmorClasses - - - -ranges = {ArmorClasses.MELEE: { - 'standard': range(-3, 5), - 'parthian tactics (armor)': range(-3, 5), - 'villager': range(0, 5), - 'villager (loom)': range(-1, 4), - }, - ArmorClasses.PIERCE: { - 'standard': range(-4, 5), - 'parthian tactics (armor)': range(-6, 5), - 'parthian tactics (attack)': range(-4, 7), - 'gambesons': range(-5, 5), - 'villager': range(0, 5), - 'villager (loom)': range(-2, 4), - } - } - - -bonuses = { - 'attack': { - 'Parthian Tactics': { - 'attackers': [ - 'Cavalry Archer', - 'Heavy Cavalry Archer', - 'Mounted Crossbowman', - 'Heavy Mounted Crossbowman', - 'Elephant Archer', - 'Mangudai', - 'Elite Mangudai', - 'War Wagon', - 'Elite War Wagon', - 'Camel Archer', - 'Elite Camel Archer', - 'Genitour', - 'Elite Genitour', - 'Kipchak', - 'Elite Kipchak', - 'Ratha (Ranged)', - 'Elite Ratha (Ranged)', - 'Xianbei Raider', - 'Bolas Rider', - 'Elite Bolas Rider', - 'Arambai', - 'Elite Arambai', - ], - 'defenders': [ - 'Spearman', - 'Pikeman', - 'Halberdier', - ], - }, - }, - 'armor': { - 'Gambesons': [ - 'Milita', - 'Man-at-Arms', - 'Long Swordsman', - 'Two-Handed Swordsman', - 'Champion', - 'Fire Lancer', - 'Elite Fire Lancer', - 'Varangian Guard', - 'Elite Varangian Guard', - ], - 'Loom': ['Villager'], - }, - 'hp': { - 'Bloodlines': [ - 'Arambai', - 'Elite Arambai', - 'Ballista Elephant', - 'Elite Ballista Elephant', - 'Battle Elephant', - 'Elite Battle Elephant', - 'Boyar', - 'Elite Boyar', - 'Camel Archer', - 'Camel Rider', - 'Camel Scout', - 'Cavalier', - 'Cavalry Archer', - 'Heavy Cavalry Archer', - 'Centurion', - 'Elite Centurion', - 'Conquistador', - 'Elite Conquistador', - 'Elephant Archer', - 'Elite Elephant Archer', - 'Flaming Camel', - 'Genitour', - 'Elite Genitour', - 'Heavy Camel Rider', - 'Hussar', - 'Imperial Camel Rider', - 'Iron Pagoda', - 'Elite Iron Pagoda', - 'Keshik', - 'Elite Keshik', - 'Kipchak', - 'Elite Kipchak', - 'Knight', - 'Konnik', - 'Elite Konnik', - 'Leitis', - 'Elite Leitis', - 'Light Cavalry', - 'Magyar Huszar', - 'Elite Magyar Huszar', - 'Mameluke', - 'Elite Mameluke', - 'Mangudai', - 'Elite Mangudai', - 'Missionary', - 'Elite Missionary', - 'Monaspa', - 'Elite Monaspa', - 'Paladin', - 'Savar', - 'Elite Savar', - 'Scout Cavalry', - 'Shrivamsha Rider', - 'Elite Shrivamsha Rider', - 'Steppe Lancer', - 'Elite Steppe Lancer', - 'Tarkan', - 'Elite Tarkan', - 'Tiger Cavalry', - 'Elite Tiger Cavalry', - 'War Elephant', - 'Elite War Elephant', - 'Winged Hussar', - 'Xianbei Raider', - 'Elite Xianbei Raider', - ], - 'Loom': ['Villager'], - }, - } -bonuses['armor']['Parthian Tactics'] = bonuses['attack']['Parthian Tactics']['attackers'] diff --git a/flashcards/fighting.py b/flashcards/fighting.py deleted file mode 100644 index 0d063ca..0000000 --- a/flashcards/fighting.py +++ /dev/null @@ -1,337 +0,0 @@ -import itertools -import json -import math -from copy import deepcopy - -from flashcards.config import config -from flashcards.types import BonusTypes, ArmorClasses -from flashcards import constants - - - -def _calculate_hp_bonus(hp, bonuses): - for b in bonuses: - b_amount, _ = b.values() - if 'Amount' in b.keys(): - hp += b_amount - else: - hp *= b_amount - hp = int(hp) - return hp - - -def _calculate_bonus(orig, bonuses, attack=False): - for b in bonuses: - b_amount, b_clss = b.values() - used = False - for iorig in range(len(orig)): - _, clss = orig[iorig].values() - if clss == b_clss: - if 'Amount' in b.keys(): - orig[iorig]['Amount'] += b_amount - else: - orig[iorig]['Amount'] *= b_amount - orig[iorig]['Amount'] = int(orig[iorig]['Amount']) - used = True - if attack and not used: - # bonus can add an attack but should not add an armor type - orig.append(b) - - -def generate_bonus_with_value(amount, clss): - return {"Amount": amount, "Class": clss} - - -def generate_bonus_with_percent(amount, clss): - return {"Percent": amount, "Class": clss} - - -def get_bonuses_from_names(clss, names): - bonuses = [] - - if len(names) > 0: - with open(config.bonus_data_file, 'r') as f: - bonus_data = json.load(f) - for name in names: - bonuses.extend(bonus_data[clss.value][name]) - - return bonuses - - -def get_possible_bonus_names_for_fight(attacker, defender): - ''' - Returns the bonuses as a tuple of lists. The first list contains attack - bonuses, the second armor bonuses, and the third hp bonuses. - ''' - attack_bonuses = [] - armor_bonuses = [] - hp_bonuses = [] - - for bonus, names in constants.bonuses['attack'].items(): - if bonus in ['Parthian Tactics']: - if (attacker in names['attackers'] and - defender in names['defenders']): - attack_bonuses.append(bonus) - elif attacker in names: - attack_bonuses.append(bonus) - - for bonus, names in constants.bonuses['armor'].items(): - if defender in names: - armor_bonuses.append(bonus) - - for bonus, names in constants.bonuses['hp'].items(): - if defender in names: - hp_bonuses.append(bonus) - - return (attack_bonuses, armor_bonuses, hp_bonuses) - - -def get_bonus_range(attacker, defender, attack_type, extras): - ''' - Determines the appropriate "bonus range" for an attacker-defender pair. - The poorly-named "bonus range" is the set of all possible - (attack - defense) values when considering all blacksmith research and any - extra research described in the `extras` argument. An example of "extra" - research would be Parthian Tactics. - - For example, you can compute a single "bonus" (i.e., one element of the - "bonus range") as follows. If there are no `extra` bonuses, and if the - attacker has Iron Casting (i.e., +2 attack) and the defender has Scale - Barding Armor (i.e., +1 defense), then the "bonus" for that combination is - 2 (attack) - 1 (defense) = 1 (bonus). By considering bonus as a single - number in this way, we do not need to consider all possible combinations of - attack and armor blacksmith research. - ''' - possible = constants.ranges[attack_type] - - if defender == 'Villager': - if 'Loom' in extras[2]: - return possible['villager (loom)'] - else: - return possible['villager'] - elif 'Parthian Tactics' in extras[1]: - return possible['parthian tactics (armor)'] - elif attack_type == ArmorClasses.PIERCE: - if 'Parthian Tactics' in extras[0]: - return possible['parthian tactics (attack)'] - elif 'Gambesons' in extras[1]: - return possible['gambesons'] - - return possible['standard'] - - -def get_attack_type(attacker_id, data): - attacks = data[attacker_id]['Attacks'] - - val = 0 - for attack in attacks: - if attack['Class'] == ArmorClasses.MELEE: - val = attack['Amount'] - - if val > 0: - return ArmorClasses.MELEE - else: - return ArmorClasses.PIERCE - - -def generate_notes(attack_bonus_names, armor_bonus_names, hp_bonus_names): - notes = '' - if len(hp_bonus_names) > 0: - notes += f'with {" and ".join(hp_bonus_names)} applied. ' - - 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): - ''' - Generates all combinations of bonuses, but assumes that most attack and - armor bonuses are always active. This is done because those bonuses are - already accounted for in the "bonus ranges" if using - generate_all_fight_results. If you are not using that function, it may not - make sense to call this one. - - Note: avoids nonsensical combinations such as having Loom for HP but not - Loom for armor. - ''' - - armor = deepcopy(armor) - for b in armor: - if b in hp: - armor.remove(b) - - attack_combos = [deepcopy(attack)] - armor_combos = [armor] - hp_combos = _powerset(hp) - - all_combos = itertools.product(attack_combos, armor_combos, hp_combos) - return [_make_valid_combination(*val) for val in all_combos] - - -def _make_valid_combination(attack, armor, hp): - for name in hp: - if name in constants.bonuses['armor'].keys(): - armor.append(name) - return (attack, armor, hp) - - -def calculate_hits_to_kill(attacker, - defender, - data, - nums, - attack_bonuses=[], - armor_bonuses=[], - hp_bonuses=[]): - ''' - Calculates hits to kill for a single attacker and defender with one set of - bonuses (i.e., no looping over attackers, defenders, or bonuses). - ''' - # Get base values - attacks = deepcopy(data[nums[attacker]]['Attacks']) - armors = deepcopy(data[nums[defender]]['Armours']) - hp = data[nums[defender]]['HP'] - - # Apply any bonuses from technologies or civilizations - _calculate_bonus(attacks, attack_bonuses, attack=True) - _calculate_bonus(armors, armor_bonuses) - hp = _calculate_hp_bonus(hp, hp_bonuses) - - damage = 0 - for a in attacks: - amount, clss = a.values() - for d in armors: - d_amount, d_clss = d.values() - if clss == d_clss: - damage += max(0, amount - d_amount) - damage = max(1, damage) - - hits = math.ceil(hp/damage) - return hits - - -def generate_fight_results(attacker, - defender, - armor_class, - bonus_range, - data, - nums, - notes='', - extra_attack_bonuses=[], - extra_armor_bonuses=[], - hp_bonuses=[], - verbose=False): - ''' - For a single attacker and defender, loops over all values of - (attack - defense) for blacksmith bonuses and Chemistry. Accepts extra - bonuses (e.g., Bloodlines), but the values of those extra bonuses are held - fixed for the duration of this function. Does not loop over attackers, - defenders, or any non-blacksmith (+Chemistry) bonuses. - ''' - if verbose: - print(f'{attacker} attacking {defender}') - if len(notes) > 0: - print(notes) - print('--------------------------------') - - results = {'hits': {}} - for i in bonus_range: - attack_bonuses = deepcopy(extra_attack_bonuses) - armor_bonuses = deepcopy(extra_armor_bonuses) - - b = generate_bonus_with_value(abs(i), armor_class) - if i < 0: - armor_bonuses.append(b) - else: - attack_bonuses.append(b) - - hits = calculate_hits_to_kill(attacker, - defender, - data, - nums, - attack_bonuses=attack_bonuses, - armor_bonuses=armor_bonuses, - hp_bonuses=hp_bonuses) - - results['hits'][i] = hits - if verbose: - print(f'Net attack bonus: {i}\thits: {hits}') - - results['notes'] = notes - return results - - -def generate_fight_results_from_names(attacker, - defender, - attack_type, - bonus_range, - data, - nums, - attack_bonus_names, - armor_bonus_names, - hp_bonus_names, - verbose): - ''' - Simple convenience wrapper for generate_fight_results. Accepts - non-blacksmith bonuses' names (e.g., 'Bloodlines') and obtains the - corresponding bonuses in the form expected by generate_fight_results (i.e., - lists of dictionaries). This function does not loop over anything itself, - although generate_fight_results, which this function calls, has its own - looping as described in its documentation. - - Note: when using this wrapper for generate_fight_results, any extra attack - and armor upgrades are not actually applied as bonuses. It is assumed that - they are already incorporated into the provided bonus range. - ''' - hp_bonuses = get_bonuses_from_names(BonusTypes.HP, - hp_bonus_names) - notes = generate_notes(attack_bonus_names, - armor_bonus_names, - hp_bonus_names) - - return generate_fight_results(attacker, - defender, - attack_type, - bonus_range, - data, - nums, - notes=notes, - extra_attack_bonuses=[], - extra_armor_bonuses=[], - hp_bonuses=hp_bonuses, - verbose=verbose) - - -def generate_all_fight_results(data, nums): - ''' - For a given group of units (specified implicitly by the nums argument, - which was itself probably generated from a namelist), determines the number - of hits for each unit to kill each other unit. For each pair, this function - loops over all possible values of blacksmith research and any other - relevant non-blacksmith bonuses found in fighting.constants.bonuses. - ''' - all_results = {} - for attacker, defender in itertools.product(nums.keys(), nums.keys()): - fight_results = [] - attack_type = get_attack_type(nums[attacker], data) - bonus_names = get_possible_bonus_names_for_fight(attacker, defender) - combos = generate_all_combinations(*bonus_names) - for combo in combos: - bonus_range = get_bonus_range(attacker, defender, attack_type, combo) - results = generate_fight_results_from_names(attacker, - defender, - attack_type, - bonus_range, - data, - nums, - combo[0], - combo[1], - combo[2], - False) - fight_results.append(results) - all_results[(attacker, defender)] = fight_results - return all_results diff --git a/flashcards/types.py b/flashcards/types.py deleted file mode 100644 index 4efed07..0000000 --- a/flashcards/types.py +++ /dev/null @@ -1,14 +0,0 @@ -from enum import Enum, IntEnum - - -class ArmorClasses(IntEnum): - # taken as genie editor value unless otherwise noted - HP = -1 # my own addition - PIERCE = 3 - MELEE = 4 - - -class BonusTypes(Enum): - ARMOR = 'armor' - ATTACK = 'attack' - HP = 'hp' diff --git a/scripts/example-fighting.py b/scripts/example-fighting.py deleted file mode 100644 index 3f5b892..0000000 --- a/scripts/example-fighting.py +++ /dev/null @@ -1,22 +0,0 @@ -from flashcards.maps import (create_maps, - read_data_to_dict) -from flashcards.fighting import generate_all_fight_results - - - -# --- General section to read in names and data - -# specify the namelist to be used to create the deck -# give the relative path of the file from config.namelists_dir -namelist = 'units-test.txt' -#namelist = 'units-non-unique.txt' - -# key into json file specifying which data you want -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) - -results = generate_all_fight_results(data, nums) -print(results)