131 lines
3.8 KiB
Python
131 lines
3.8 KiB
Python
import json
|
|
import math
|
|
from copy import deepcopy
|
|
|
|
from flashcards.config import config
|
|
|
|
|
|
|
|
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 calculate_hits_to_kill(attacker,
|
|
defender,
|
|
data,
|
|
nums,
|
|
attack_bonuses=[],
|
|
armor_bonuses=[],
|
|
hp_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):
|
|
|
|
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
|