def create_community(self): community = [] for i in range(self.community_size): loadout = Loadout("Kensa Undercover Brella", "Sprinkler", "Ink Armor") loadout.randomize_abilities() loadout.get_fitness() community.append(loadout) return community
def __init__(self, steam_id, team, acc, hs_percentage): # the player's id self.steam_id = steam_id # percentage of shots that hit, accuracy self.acc = acc # percentage of hits that hit the head self.hs_percentage = hs_percentage # the team self.team = team # the player's health, this changes when the teams "fight" self.health = 100 # the current hotspot that the player is in self.current_location = 0 self.action = None # if the player is alive or dead self.is_alive = True self.env = None self.money = Constants.STARTING_MONEY self.loadout = Loadout()
class Player(object): """ Class that represents a CSGO player""" def __init__(self, steam_id, team, acc, hs_percentage): # the player's id self.steam_id = steam_id # percentage of shots that hit, accuracy self.acc = acc # percentage of hits that hit the head self.hs_percentage = hs_percentage # the team self.team = team # the player's health, this changes when the teams "fight" self.health = 100 # the current hotspot that the player is in self.current_location = 0 self.action = None # if the player is alive or dead self.is_alive = True self.env = None self.money = Constants.STARTING_MONEY self.loadout = Loadout() def respawn(self, env, pistol): """ Add the player to the new environment and reset player statistics""" self.env = env self.action = env.process(self.play(self.env)) self.health = 100 self.is_alive = True if (pistol): self.money = Constants.STARTING_MONEY def play(self, env): """Process that simulates the player's actions. This is run once every round until the round is over""" # choose a random time, this will make it so the first to attack is non deterministic yield env.timeout(np.random.uniform(0.0, 5.0)) while(self.is_alive): # if self.is_alive == True: target = self.choose_target() shots_fired = np.random.uniform(0.0, 5.0) weapon = self.loadout.get_weapon() if (weapon.out_of_ammo()): yield env.timeout(weapon.reload()) elif target == -1: yield env.timeout(np.random.uniform(0.0, 5.0)) else: target.inflict_self(self.determine_damage(weapon, shots_fired)) yield env.timeout(np.random.uniform(0.0, 5.0)) def determine_damage(self, weapon, rounds): """The amount of damage the player will inflict on the enemy""" shots_fired = weapon.fire_burst(rounds) shots_hit = np.random.binomial(shots_fired, self.acc) head_shots = np.random.binomial(shots_hit, self.hs_percentage) body_shots = shots_hit - head_shots head_damage = head_shots * weapon.damage * 4 helmet_damage = head_shots * weapon.helmet_damage body_damage = body_shots * weapon.damage armour_damage = body_damage * weapon.pen_power return [body_damage, head_damage, armour_damage, helmet_damage] def choose_target(self): """Choose a target to attack from the enemies in the hotspot""" # 1 - side converts 0 to 1 and 1 to 0 enemy_list = self.current_location.players[1 - self.team.side] num_enemies = len(enemy_list) # if there are no enemies currently in the same location of the player # simply return 0 if num_enemies == 0: return -1 # pick an enemy randomly from the list of enemies and return their object return enemy_list[np.random.random_integers(0, num_enemies - 1)] def get_side(self): return self.team.side def inflict_self(self, damage): """Inflict damage onto own class. If damage moves health below 0, mark the player as "Dead" and remove them from the map""" body_damage = damage[0] head_damage = damage[1] armour_damage = damage[2] helmet_damage = damage[3] damage_to_kevlar = body_damage - armour_damage through_damage = self.loadout.damage_kevlar(damage_to_kevlar) final_damage = 0 if (self.loadout.helmet == True): final_damage = helmet_damage + armour_damage + through_damage else: final_damage = head_damage + armour_damage + through_damage self.health = self.health - final_damage if self.health <= 0: self.current_location.players[self.team.side].remove(self) self.is_alive = False self.loadout.clear() def charge(self, cost): """Subtract cost from the player's money. If the amount of money they have goes below 0 throw a NegativeMoneyException""" try: money = self.money money -= cost if (money < 0): raise NegativeMoneyException() else: self.money = money except NegativeMoneyException as e: print "{0} spent more money than they have in their bank\n".format(self.steam_id) print e print "\n" def add_money(self, amount): money = self.money + amount if (money >= Constants.MAX_MONEY): self.money = Constants.MAX_MONEY else: self.money = money def reset_money(self): self.money = Constants.STARTING_MONEY def __str__(self): return "Steam id: {0}\tIs Alive: {1}\tCurrent Bank: {2}".format(self.steam_id, self.is_alive, self.money)
def crossover(self, community): community = self.sort_by_fitness(community) alpha_primaries = community[0].primaries = self.order_primaries( community[0].primaries) alpha_secondaries = community[0].secondaries candidates = community[1:] new_community = [community[0]] for i in range(len(candidates)): candidate_primaries = self.order_primaries(candidates[i].primaries) candidate_secondaries = candidates[i].secondaries while True: alpha_primaries_clone = copy.deepcopy(alpha_primaries) for item in SplatoonData.REQUIRED_PRIMARIES: alpha_primaries_clone.remove(item) candidate_primaries_clone = copy.deepcopy(candidate_primaries) for item in SplatoonData.REQUIRED_PRIMARIES: candidate_primaries_clone.remove(item) new_primaries = [] for i in range(len(alpha_primaries_clone)): if SplatoonData.RANDOM.randint(0, 1): new_primaries.append(alpha_primaries_clone[i]) else: new_primaries.append(candidate_primaries_clone[i]) new_primaries = new_primaries + SplatoonData.REQUIRED_PRIMARIES if (SplatoonData.list_in_list(SplatoonData.REQUIRED_PRIMARIES, new_primaries)): break while True: alpha_secondaries_clone = copy.deepcopy(alpha_secondaries) for item in SplatoonData.REQUIRED_SECONDARIES: alpha_secondaries_clone.remove(item) candidate_secondaries_clone = copy.deepcopy( candidate_secondaries) for item in SplatoonData.REQUIRED_SECONDARIES: candidate_secondaries_clone.remove(item) new_secondaries = [] for i in range(len(alpha_secondaries_clone)): if SplatoonData.RANDOM.randint(0, 1): new_secondaries.append(alpha_secondaries_clone[i]) else: new_secondaries.append(candidate_secondaries_clone[i]) new_secondaries = new_secondaries + SplatoonData.REQUIRED_SECONDARIES if (SplatoonData.list_in_list( SplatoonData.REQUIRED_SECONDARIES, new_secondaries)): break while True: mutated_primaries = self.mutate_primaries(new_primaries) if (SplatoonData.list_in_list(SplatoonData.REQUIRED_PRIMARIES, mutated_primaries)): break while True: mutated_secondaries = self.mutate_secondaries(new_secondaries) if (SplatoonData.list_in_list( SplatoonData.REQUIRED_SECONDARIES, mutated_secondaries)): break new_primaries = mutated_primaries new_secondaries = mutated_secondaries loadout = Loadout(community[0].weapon["name"], community[0].sub["name"], community[0].special["name"]) loadout.primaries = new_primaries loadout.secondaries = new_secondaries loadout.get_fitness() new_community.append(loadout) return new_community