Automate generation of fight results
This commit is contained in:
@@ -1,11 +1,9 @@
|
|||||||
import itertools
|
import itertools
|
||||||
|
|
||||||
from flashcards.constants import UnitLines, ranges
|
|
||||||
from flashcards.maps import (create_maps,
|
from flashcards.maps import (create_maps,
|
||||||
read_data_to_dict,
|
read_data_to_dict,
|
||||||
read_name_data_to_dict)
|
read_name_data_to_dict)
|
||||||
from flashcards.fighting import (generate_all_combinations,
|
from flashcards.fighting import generate_all_fight_results
|
||||||
generate_fight_results_from_names)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -19,43 +17,7 @@ namelist = 'units-buildings-trimmed.txt'
|
|||||||
name_json_keys = 'units_buildings'
|
name_json_keys = 'units_buildings'
|
||||||
data_json_keys = ('data', 'Unit')
|
data_json_keys = ('data', 'Unit')
|
||||||
|
|
||||||
# --- Fight details start here
|
|
||||||
|
|
||||||
attacker_line = 'Spearman'
|
|
||||||
defender_line = 'Knight'
|
|
||||||
#defender_line = 'Scout Cavalry'
|
|
||||||
range_type = 'standard'
|
|
||||||
|
|
||||||
# extra bonuses beyond the usual blacksmith upgrades and bloodlines
|
|
||||||
hp_bonus_names = []
|
|
||||||
attack_bonus_names = []
|
|
||||||
armor_bonus_names = []
|
|
||||||
|
|
||||||
# --- Shouldn't need to change below this line
|
|
||||||
|
|
||||||
_, nums = create_maps(namelist, name_json_keys)
|
_, nums = create_maps(namelist, name_json_keys)
|
||||||
data = read_data_to_dict(data_json_keys)
|
data = read_data_to_dict(data_json_keys)
|
||||||
|
|
||||||
attack_type = UnitLines.get_attack(attacker_line)
|
generate_all_fight_results(data, nums)
|
||||||
bonus_range = ranges[attack_type][range_type]
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|||||||
@@ -5,71 +5,137 @@ from flashcards.types import ArmorClasses
|
|||||||
ranges = {ArmorClasses.MELEE: {
|
ranges = {ArmorClasses.MELEE: {
|
||||||
'standard': range(-3, 5),
|
'standard': range(-3, 5),
|
||||||
'parthian tactics (armor)': range(-3, 5),
|
'parthian tactics (armor)': range(-3, 5),
|
||||||
'villager': range(-3, 5),
|
'villager': range(0, 5),
|
||||||
|
'villager (loom)': range(-1, 4),
|
||||||
},
|
},
|
||||||
ArmorClasses.PIERCE: {
|
ArmorClasses.PIERCE: {
|
||||||
'standard': range(-4, 5),
|
'standard': range(-4, 5),
|
||||||
'parthian tactics (armor)': range(-6, 5),
|
'parthian tactics (armor)': range(-6, 5),
|
||||||
'parthian tactics (attack)': range(-4, 7),
|
'parthian tactics (attack)': range(-4, 7),
|
||||||
'gambesons': range(-5, 5),
|
'gambesons': range(-5, 5),
|
||||||
'villager': range(-2, 5),
|
'villager': range(0, 5),
|
||||||
|
'villager (loom)': range(-2, 4),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bonuses = {
|
||||||
class UnitLines():
|
'attack': {
|
||||||
|
'Parthian Tactics': {
|
||||||
lines = {'Scout Cavalry': {
|
'attackers': [
|
||||||
'names': [
|
'Cavalry Archer',
|
||||||
'Scout Cavalry',
|
'Heavy Cavalry Archer',
|
||||||
'Light Cavalry',
|
'Mounted Crossbowman',
|
||||||
'Hussar'
|
'Heavy Mounted Crossbowman',
|
||||||
],
|
'Elephant Archer',
|
||||||
'attack_type': ArmorClasses.MELEE,
|
'Mangudai',
|
||||||
'hp_bonus_names': ['Bloodlines'],
|
'Elite Mangudai',
|
||||||
},
|
'War Wagon',
|
||||||
'Spearman': {
|
'Elite War Wagon',
|
||||||
'names': [
|
'Camel Archer',
|
||||||
'Spearman',
|
'Elite Camel Archer',
|
||||||
'Pikeman',
|
'Genitour',
|
||||||
'Halberdier'
|
'Elite Genitour',
|
||||||
],
|
'Kipchak',
|
||||||
'attack_type': ArmorClasses.MELEE,
|
'Elite Kipchak',
|
||||||
},
|
'Ratha (Ranged)',
|
||||||
'Knight': {
|
'Elite Ratha (Ranged)',
|
||||||
'names': [
|
'Xianbei Raider',
|
||||||
'Knight',
|
'Bolas Rider',
|
||||||
'Cavalier',
|
'Elite Bolas Rider',
|
||||||
'Paladin'
|
'Arambai',
|
||||||
],
|
'Elite Arambai',
|
||||||
'attack_type': ArmorClasses.MELEE,
|
],
|
||||||
'hp_bonus_names': ['Bloodlines'],
|
'defenders': [
|
||||||
},
|
'Spearman',
|
||||||
}
|
'Pikeman',
|
||||||
|
'Halberdier',
|
||||||
|
],
|
||||||
@classmethod
|
},
|
||||||
def get_names(cls, name: str) -> list(str):
|
},
|
||||||
line = cls.lines[name]
|
'armor': {
|
||||||
try:
|
'Gambesons': [
|
||||||
names = line['names']
|
'Milita',
|
||||||
except KeyError:
|
'Man-at-Arms',
|
||||||
names = [name]
|
'Long Swordsman',
|
||||||
print(names)
|
'Two-Handed Swordsman',
|
||||||
return names
|
'Champion',
|
||||||
|
'Fire Lancer',
|
||||||
|
'Elite Fire Lancer',
|
||||||
@classmethod
|
'Varangian Guard',
|
||||||
def get_attack(cls, name: str) -> ArmorClasses:
|
'Elite Varangian Guard',
|
||||||
return cls.lines[name]['attack_type']
|
],
|
||||||
|
'Loom': ['Villager'],
|
||||||
|
},
|
||||||
@classmethod
|
'hp': {
|
||||||
def get_hp_bonus_names(cls, name: str) -> list(str):
|
'Bloodlines': [
|
||||||
line = cls.lines[name]
|
'Arambai',
|
||||||
try:
|
'Elite Arambai',
|
||||||
bonuses = line['hp_bonus_names']
|
'Ballista Elephant',
|
||||||
except KeyError:
|
'Elite Ballista Elephant',
|
||||||
bonuses = []
|
'Battle Elephant',
|
||||||
return bonuses
|
'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']
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import math
|
|||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
from flashcards.config import config
|
from flashcards.config import config
|
||||||
from flashcards.types import BonusTypes
|
from flashcards.types import BonusTypes, ArmorClasses
|
||||||
|
from flashcards import constants
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -57,11 +58,94 @@ def get_bonuses_from_names(clss, names):
|
|||||||
return bonuses
|
return bonuses
|
||||||
|
|
||||||
|
|
||||||
def generate_notes(bonuses):
|
def get_possible_bonus_names_for_fight(attacker, defender):
|
||||||
if len(bonuses) > 0:
|
'''
|
||||||
notes = f'with {" and ".join(bonuses)}'
|
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:
|
else:
|
||||||
notes = ''
|
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. '
|
||||||
|
|
||||||
|
other = attack_bonus_names + armor_bonus_names
|
||||||
|
if len(other) > 0:
|
||||||
|
verb = 'is'
|
||||||
|
if len(other) > 1:
|
||||||
|
verb = 'are'
|
||||||
|
notes += f'{" and ".join(other)} {verb} incoporated into net attack range'
|
||||||
|
|
||||||
return notes
|
return notes
|
||||||
|
|
||||||
|
|
||||||
@@ -72,11 +156,35 @@ def _powerset(elements):
|
|||||||
|
|
||||||
|
|
||||||
def generate_all_combinations(attack, armor, hp):
|
def generate_all_combinations(attack, armor, hp):
|
||||||
attack_combos = _powerset(attack)
|
'''
|
||||||
armor_combos = _powerset(armor)
|
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)
|
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))
|
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,
|
def calculate_hits_to_kill(attacker,
|
||||||
@@ -86,6 +194,10 @@ def calculate_hits_to_kill(attacker,
|
|||||||
attack_bonuses=[],
|
attack_bonuses=[],
|
||||||
armor_bonuses=[],
|
armor_bonuses=[],
|
||||||
hp_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
|
# Get base values
|
||||||
attacks = deepcopy(data[nums[attacker]]['Attacks'])
|
attacks = deepcopy(data[nums[attacker]]['Attacks'])
|
||||||
armors = deepcopy(data[nums[defender]]['Armours'])
|
armors = deepcopy(data[nums[defender]]['Armours'])
|
||||||
@@ -120,7 +232,13 @@ def generate_fight_results(attacker,
|
|||||||
extra_armor_bonuses=[],
|
extra_armor_bonuses=[],
|
||||||
hp_bonuses=[],
|
hp_bonuses=[],
|
||||||
verbose=False):
|
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:
|
if verbose:
|
||||||
print(f'{attacker} attacking {defender}')
|
print(f'{attacker} attacking {defender}')
|
||||||
if len(notes) > 0:
|
if len(notes) > 0:
|
||||||
@@ -156,7 +274,7 @@ def generate_fight_results(attacker,
|
|||||||
|
|
||||||
def generate_fight_results_from_names(attacker,
|
def generate_fight_results_from_names(attacker,
|
||||||
defender,
|
defender,
|
||||||
armor_class,
|
attack_type,
|
||||||
bonus_range,
|
bonus_range,
|
||||||
data,
|
data,
|
||||||
nums,
|
nums,
|
||||||
@@ -164,23 +282,59 @@ def generate_fight_results_from_names(attacker,
|
|||||||
armor_bonus_names,
|
armor_bonus_names,
|
||||||
hp_bonus_names,
|
hp_bonus_names,
|
||||||
verbose):
|
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.
|
||||||
|
|
||||||
attack_bonuses = get_bonuses_from_names(BonusTypes.ATTACK,
|
Note: when using this wrapper for generate_fight_results, any extra attack
|
||||||
attack_bonus_names)
|
and armor upgrades are not actually applied as bonuses. It is assumed that
|
||||||
armor_bonuses = get_bonuses_from_names(BonusTypes.ARMOR,
|
they are already incorporated into the provided bonus range.
|
||||||
armor_bonus_names)
|
'''
|
||||||
hp_bonuses = get_bonuses_from_names(BonusTypes.HP,
|
hp_bonuses = get_bonuses_from_names(BonusTypes.HP,
|
||||||
hp_bonus_names)
|
hp_bonus_names)
|
||||||
notes = generate_notes(hp_bonus_names)
|
notes = generate_notes(attack_bonus_names,
|
||||||
|
armor_bonus_names,
|
||||||
|
hp_bonus_names)
|
||||||
|
|
||||||
return generate_fight_results(attacker,
|
return generate_fight_results(attacker,
|
||||||
defender,
|
defender,
|
||||||
armor_class,
|
attack_type,
|
||||||
bonus_range,
|
bonus_range,
|
||||||
data,
|
data,
|
||||||
nums,
|
nums,
|
||||||
notes=notes,
|
notes=notes,
|
||||||
extra_attack_bonuses=attack_bonuses,
|
extra_attack_bonuses=[],
|
||||||
extra_armor_bonuses=armor_bonuses,
|
extra_armor_bonuses=[],
|
||||||
hp_bonuses=hp_bonuses,
|
hp_bonuses=hp_bonuses,
|
||||||
verbose=verbose)
|
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.
|
||||||
|
'''
|
||||||
|
for attacker, defender in itertools.product(nums.keys(), nums.keys()):
|
||||||
|
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],
|
||||||
|
True)
|
||||||
|
print(results)
|
||||||
|
|||||||
Reference in New Issue
Block a user