def __add_army(self, offset_coordinates: Tuple[int, int], army: Army) -> AI_Army: tile = self.__get_tile(offset_coordinates) ai_a = AI_Army(tile) ai_a.population = army.get_population() ai_a.knights = army.get_population_by_unit(UnitType.KNIGHT) ai_a.mercenaries = army.get_population_by_unit(UnitType.MERCENARY) ai_a.barbaric_soldiers = army.get_population_by_unit( UnitType.BABARIC_SOLDIER) tile.army = ai_a return ai_a
def army_vs_building(attacker: Army, defender: Building) -> BattleAfterMath: attack_value = attacker.get_attack_strength() defencive_value = defender.defensive_value attacker_won = attack_value >= defencive_value # they can both win if their values are equal defender_won = defencive_value >= attack_value if attacker_won: attacker_losses = defencive_value if attacker_won else attack_value attacker_alive_ratio = (attack_value - attacker_losses) / attack_value attacker_alive_ratio = attacker_alive_ratio + (attacker_losses / 3.0) / attack_value attacker_pop = attacker.get_population() attacker_surviving_pop_ratio = attacker_pop * attacker_alive_ratio for unit in UnitType: attacker_unit_x = attacker.get_population_by_unit(unit) attacker_kill_count_unit_x = attacker_unit_x - ((attacker_unit_x / attacker_pop) * attacker_surviving_pop_ratio) attacker.remove_units_of_type(ceil(attacker_kill_count_unit_x), unit) defender.defensive_value = -1 # building is destroyed if defender_won: attacker.remove_all_units() # army is destroyed if attacker.get_population() == 0: defender_won = True if attacker_won and defender_won: return BattleAfterMath.DRAW elif attacker_won: return BattleAfterMath.ATTACKER_WON else: return BattleAfterMath.DEFENDER_WON
def __init__(self, center_x, center_y, army: Army): super().__init__(center_x, center_y, "", panel_tex="../resources/other/panel_army.png", no_header=True) u = army.get_units_as_tuple() a = (Unit.get_unit_stats(UnitType.KNIGHT)) b = (Unit.get_unit_stats(UnitType.MERCENARY)) c = (Unit.get_unit_stats(UnitType.BABARIC_SOLDIER)) self.units = f"{u[1]} {ls} {ls}{ts}{u[0]} {ls}{ls}{ts}{u[2]}" self.m_value = f"{a[0]}{ls}{ls}{ls}{b[0]}{ls}{ls}{ls}{c[0]}\n{a[1]}{ls}{ls}{ls}{b[1]}{ls}{ls}{ls}{c[1]}\n{a[2]}{ls}{ls}{ls}{b[2]}{ls}{ls}{ls}{c[2]}"
def sim_fight(attacker_constellation, defender_constellation): attacker: Army = Army(None, 0) defender: Army = Army(None, 0) c = 0 for ut in UnitType: for i in range(attacker_constellation[c]): unit: Unit = Unit(ut) attacker.add_unit(unit) for j in range(defender_constellation[c]): unit: Unit = Unit(ut) defender.add_unit(unit) c = c + 1 print("\n \n") print_army(attacker) print_army(defender) FightCalculator.army_vs_army(attacker, defender) print("aftermath:") print_army(attacker) print_army(defender)
def set_init_values(player: Player, base_hex: Hexagon, gl): if player.player_type is PlayerType.HUMAN or player.player_type is PlayerType.AI: player.food = 35 player.amount_of_resources = 20 unit = Unit(player.get_initial_unit_type()) army = Army(gl.hex_map.get_hex_by_offset(player.init_army_loc), player.id) army.add_unit(unit) gl.add_army(army, player) elif player.player_type is PlayerType.VILLAGER: player.food = 30 player.amount_of_resources = 5 elif player.player_type is PlayerType.BARBARIC: player.amount_of_resources = 5 player.food = 10 player.discovered_tiles.add(base_hex) b_type = player.get_initial_building_type() b: Building = Building(base_hex, b_type, player.id) gl.add_building(b, player) b.construction_time = 0 b.set_state_active() tmp = gl.hex_map.get_neighbours_dist(base_hex, b.sight_range) player.discovered_tiles.update(tmp)
def army_vs_army(attacker: Army, defender: Army) -> BattleAfterMath: attack_value = attacker.get_attack_strength() defencive_value = defender.get_defence_strength() if attack_value == 0: error("Attack value is 0 -> will adjust it to 0.5") attack_value = 0.5 if defencive_value == 0: error("Defence value is 0 -> will adjust it to 0.5") defencive_value = 0.5 attacker_won = attack_value >= defencive_value defender_won = defencive_value >= attack_value attacker_losses = defencive_value if attacker_won else attack_value defender_losses = attack_value if defender_won else defencive_value attacker_alive_pop_ratio = (attack_value - attacker_losses) / attack_value defender_alive_pop_ratio = (defencive_value - defender_losses) / defencive_value if attacker_won: attacker_alive_pop_ratio = attacker_alive_pop_ratio + (attacker_losses/3.0) / attack_value if defender_won: defender_alive_pop_ratio = defender_alive_pop_ratio + (defender_losses/3.0) / defencive_value attacker_pop = attacker.get_population() defender_pop = defender.get_population() attacker_surviving_pop_ratio = attacker_pop * attacker_alive_pop_ratio defender_surviving_pop_ratio = defender_pop * defender_alive_pop_ratio for unit in UnitType: attacker_unit_x = attacker.get_population_by_unit(unit) attacker_kill_count_unit_x = attacker_unit_x - ((attacker_unit_x / attacker_pop) * attacker_surviving_pop_ratio) attacker.remove_units_of_type(ceil(attacker_kill_count_unit_x), unit) defender_unit_x = defender.get_population_by_unit(unit) defender_kill_count_unit_x = defender_unit_x - ((defender_unit_x / defender_pop) * defender_surviving_pop_ratio) defender.remove_units_of_type(ceil(defender_kill_count_unit_x), unit) if attacker_won and defender_won: return BattleAfterMath.DRAW elif attacker_won: return BattleAfterMath.ATTACKER_WON else: return BattleAfterMath.DEFENDER_WON
def print_army(army: Army): print("({}|{}|{})".format( army.get_population_by_unit(UnitType.KNIGHT), army.get_population_by_unit(UnitType.MERCENARY), army.get_population_by_unit(UnitType.BABARIC_SOLDIER)))
def add_opp_army(self, offset_coordinates: Tuple[int, int], army: Army, id: int): army: AI_Army = self.__add_army(offset_coordinates, army) army.owner = id self.opp_army_list.append(army)