Add time-to-kill and clean up code

This commit is contained in:
Nate Bowman
2026-10-28 19:28:20 -04:00
parent aa6edd37de
commit 3fcbf0d0af
2 changed files with 73 additions and 27 deletions

View File

@@ -13,3 +13,5 @@ data_path = Path(c['storage']['data'])
config = SimpleNamespace()
config.bonus_data_file = data_path / 'bonuses.json'
config.rounding_digits = 1

View File

@@ -18,6 +18,10 @@ class Fighting():
self.bonus_data = json.load(f)
def list_units(self):
return list(self.nums.keys())
@staticmethod
def _calculate_hp_bonus(hp, bonuses):
for b in bonuses:
@@ -98,7 +102,34 @@ class Fighting():
return (attack_bonuses, armor_bonuses, hp_bonuses)
def get_bonus_range(attacker, defender, attack_type, extras):
def _remove_names_not_in_list(names, valid_names):
for b in names:
if b not in valid_names:
names.remove(b)
def _make_valid_bonus_names(attacker,
defender,
attack_bonus_names,
armor_bonus_names,
hp_bonus_names):
all_valid_names = Fighting.get_possible_bonus_names_for_fight(attacker,
defender)
Fighting._remove_names_not_in_list(attack_bonus_names,
all_valid_names[0])
Fighting._remove_names_not_in_list(armor_bonus_names,
all_valid_names[1])
Fighting._remove_names_not_in_list(hp_bonus_names,
all_valid_names[2])
def get_bonus_range(attacker,
defender,
attack_type,
extra_attack_bonuses,
extra_armor_bonuses,
hp_bonuses):
'''
Determines the appropriate "bonus range" for an attacker-defender pair.
The poorly-named "bonus range" is the set of all possible
@@ -117,16 +148,16 @@ class Fighting():
possible = constants.ranges[attack_type]
if defender == 'Villager':
if 'Loom' in extras[2]:
if 'Loom' in hp_bonuses:
return possible['villager (loom)']
else:
return possible['villager']
elif 'Parthian Tactics' in extras[1]:
elif 'Parthian Tactics' in extra_armor_bonuses:
return possible['parthian tactics (armor)']
elif attack_type == ArmorClasses.PIERCE:
if 'Parthian Tactics' in extras[0]:
if 'Parthian Tactics' in extra_attack_bonuses:
return possible['parthian tactics (attack)']
elif 'Gambesons' in extras[1]:
elif 'Gambesons' in extra_armor_bonuses:
return possible['gambesons']
return possible['standard']
@@ -201,6 +232,7 @@ class Fighting():
defender,
attack_bonuses=[],
armor_bonuses=[],
time_to_kill=False,
hp_bonuses=[]):
'''
Calculates hits to kill for a single attacker and defender with one set of
@@ -226,18 +258,24 @@ class Fighting():
damage = max(1, damage)
hits = math.ceil(hp/damage)
if time_to_kill:
reload = self.data[self.nums[attacker]]['ReloadTime']
delay = self.data[self.nums[attacker]]['AttackDelaySeconds']
hits = delay + (hits - 1)*reload
hits = round(hits, config.rounding_digits)
return hits
def generate_fight_results(self,
attacker,
defender,
armor_class,
bonus_range,
notes='',
extra_attack_bonuses=[],
extra_armor_bonuses=[],
hp_bonuses=[],
time_to_kill=False,
verbose=False):
'''
For a single attacker and defender, loops over all values of
@@ -246,6 +284,13 @@ class Fighting():
fixed for the duration of this function. Does not loop over attackers,
defenders, or any non-blacksmith (+Chemistry) bonuses.
'''
armor_class = self.get_attack_type(self.nums[attacker])
bonus_range = Fighting.get_bonus_range(attacker,
defender,
armor_class,
extra_attack_bonuses,
extra_armor_bonuses,
hp_bonuses)
if verbose:
print(f'{attacker} attacking {defender}')
if len(notes) > 0:
@@ -267,7 +312,8 @@ class Fighting():
defender,
attack_bonuses=attack_bonuses,
armor_bonuses=armor_bonuses,
hp_bonuses=hp_bonuses)
hp_bonuses=hp_bonuses,
time_to_kill=time_to_kill)
results['hits'][i] = hits
if verbose:
@@ -280,12 +326,11 @@ class Fighting():
def generate_fight_results_from_names(self,
attacker,
defender,
attack_type,
bonus_range,
attack_bonus_names,
armor_bonus_names,
hp_bonus_names,
verbose):
attack_bonus_names=[],
armor_bonus_names=[],
hp_bonus_names=[],
time_to_kill=False,
verbose=False):
'''
Simple convenience wrapper for generate_fight_results. Accepts
non-blacksmith bonuses' names (e.g., 'Bloodlines') and obtains the
@@ -298,6 +343,12 @@ class Fighting():
and armor upgrades are not actually applied as bonuses. It is assumed that
they are already incorporated into the provided bonus range.
'''
Fighting._make_valid_bonus_names(attacker,
defender,
attack_bonus_names,
armor_bonus_names,
hp_bonus_names)
hp_bonuses = self.get_bonuses_from_names(BonusTypes.HP,
hp_bonus_names)
notes = Fighting.generate_notes(attack_bonus_names,
@@ -306,12 +357,11 @@ class Fighting():
return self.generate_fight_results(attacker,
defender,
attack_type,
bonus_range,
notes=notes,
extra_attack_bonuses=[],
extra_armor_bonuses=[],
hp_bonuses=hp_bonuses,
time_to_kill=time_to_kill,
verbose=verbose)
@@ -327,23 +377,17 @@ class Fighting():
for attacker, defender in itertools.product(self.nums.keys(),
self.nums.keys()):
fight_results = []
attack_type = self.get_attack_type(self.nums[attacker])
bonus_names = Fighting.get_possible_bonus_names_for_fight(attacker,
defender)
combos = Fighting.generate_all_combinations(*bonus_names)
for combo in combos:
bonus_range = Fighting.get_bonus_range(attacker,
defender,
attack_type,
combo)
results = self.generate_fight_results_from_names(attacker,
defender,
attack_type,
bonus_range,
combo[0],
combo[1],
combo[2],
False)
attack_bonus_names=combo[0],
armor_bonus_names=combo[1],
hp_bonus_names=combo[2],
time_to_kill=False,
verbose=False)
fight_results.append(results)
all_results[(attacker, defender)] = fight_results
return all_results