import math 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: orig[iorig]['Amount'] += b_amount used = True if attack and not used: # bonus can add an attack but should not add an armor type orig.append(b) def calculate_hits_to_kill(attacker, defender, data, nums, attack_bonuses=[], armor_bonuses=[], hp_bonus=0): # Get base values attacks = data[nums[attacker]]['Attacks'] armors = 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 += hp_bonus # TODO incorporate percent bonuses as well in separate function # TODO use json data for hp_bonus instead of just a number 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