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