def __init__(self, bot_num): self.bot_num = bot_num self.comm = Comm(bot_num) self.max_ammo = 10 self.bullet_speed = 700 self.min_score_dif = 100 self.small_health = 5 self.close_dist = 125 self.medium_dist = 250 self.dist_to_shoot = 200
class ShootCircle: def __init__(self, bot_num): self.bot_num = bot_num self.comm = Comm(bot_num) self.half_screen_size = 450 def run(self): while True: status = self.comm.send() me = status['bots'][self.bot_num] dx = 0 dy = 0 if self.half_screen_size > me['x']: dx = 1 elif self.half_screen_size < me['x']: dx = -1 if self.half_screen_size > me['y']: dy = 1 elif self.half_screen_size < me['y']: dy = -1 self.comm.rotate(1) self.comm.move(dx, dy) self.comm.shoot(True)
def __init__(self, num_of_bots, bot_num): self.num_of_bots = num_of_bots self.bot_num = bot_num self.comm = Comm(bot_num) # posX, posY, velX, velY, angle, ammo, life, shoot, score low = np.tile( np.array([0, 0, -500, -500, 0, 0, 0, 0, 0], dtype=np.float32), self.num_of_bots) high = np.tile( np.array([900, 900, 500, 500, 360, 10, 20, 1, 1000], dtype=np.float32), self.num_of_bots) self.observation_space = spaces.Box(low, high, dtype=np.float32) # rotate: -1, 0, 1 # move: (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1) # shoot: 0, 1 self.action_space = spaces.Discrete(54) self.state = None
class WASDBot: def __init__(self, bot_num): self.bot_num = bot_num self.comm = Comm(bot_num) def getch(self): fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) try: tty.setraw(sys.stdin.fileno()) ch = sys.stdin.read(1) finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) return ch def run(self): while True: ch = self.getch() if ch == '\x03': raise KeyboardInterrupt rot = 0 if ch == 'e': rot = 1 elif ch == 'q': rot = -1 self.comm.rotate(rot) dy = 0 if ch == 'w': dy = 1 elif ch == 's': dy = -1 dx = 0 if ch == 'a': dx = -1 elif ch == 'd': dx = 1 self.comm.move(dx, dy) shoot = False if ch == ' ': shoot = True self.comm.shoot(shoot) print(self.comm.send())
def __init__(self, bot_num): self.bot_num = bot_num self.comm = Comm(bot_num)
class ShootLowestLifeBot: def __init__(self, bot_num): self.bot_num = bot_num self.comm = Comm(bot_num) def run(self): while True: status = self.comm.send() me = status['bots'][self.bot_num] enemy = status['bots'][0] for bot in status['bots']: if bot is not me and bot['life'] != 0: if enemy['life'] > bot['life']: enemy = bot angle = atan2(enemy['y'] - me['y'], enemy['x'] - me['x']) angle = angle * 180 / pi angle_diff = abs(me['angle'] - angle) if angle > me['angle']: if angle_diff <= 180: self.comm.rotate(1) else: self.comm.rotate(-1) elif angle < me['angle']: if angle_diff <= 180: self.comm.rotate(-1) else: self.comm.rotate(1) dx = 0 dy = 0 if enemy['x'] > me['x']: dx = 1 if enemy['y'] > me['y']: dy = 1 if enemy['x'] < me['x']: dx = -1 if enemy['y'] < me['y']: dy = -1 self.comm.move(dx, dy) shoot = False if abs(me['angle'] - angle) < 10: shoot = True self.comm.shoot(shoot)
class ShootClosestBot: def __init__(self, bot_num): self.bot_num = bot_num self.comm = Comm(bot_num) def rotateBot(self, my_bot, closest_bot, main_bot): x = closest_bot['x'] - my_bot['x'] y = closest_bot['y'] - my_bot['y'] ang = my_bot['angle'] angle_to_enemy = 0 if x != 0: angle_to_enemy = math.atan(y / x) * 180 / math.pi if x < 0: angle_to_enemy += 180 angle_to_enemy -= ang angle_to_enemy //= 1 angle_to_enemy %= 360 if angle_to_enemy % 10 != 5: angle_to_enemy = round(angle_to_enemy / 10, 0) angle_to_enemy %= 36 if angle_to_enemy > 18: self.comm.rotate(-(36 - angle_to_enemy)) else: self.comm.rotate(angle_to_enemy) if my_bot['ammo'] < 5: if closest_bot == main_bot: self.comm.shoot(1) else: self.comm.shoot(1) def run(self): status = self.comm.send() new_x = 0 while True: new_x += 1 if new_x == 360: new_x = 0 bots = status['bots'] # my bot my_bot = bots[self.bot_num] my_x = my_bot['x'] my_y = my_bot['y'] # enemies current_bot = { 'id': 0, 'x': 0, 'y': 0, 'vx': 0, 'vy': 0, 'angle': 0, 'ammo': 0, 'life': 100, 'shoot': False, 'score': 0 } lowest_hp_bot = current_bot for bot in bots: if bot == my_bot: continue enemy_hp = bot['life'] if lowest_hp_bot['life'] > enemy_hp > 0: lowest_hp_bot = bot if enemy_hp == lowest_hp_bot['life'] and enemy_hp > 0: if dist(my_bot, bot) < dist( my_bot, lowest_hp_bot) and enemy_hp > 0: lowest_hp_bot = bot closest_bot = lowest_hp_bot for bot in bots: enemy_hp = bot['life'] if bot == my_bot: continue if dist(my_bot, bot) < dist(my_bot, closest_bot) and enemy_hp > 0: closest_bot = bot # move to enemy if dist(my_bot, closest_bot) > 150: self.comm.move(sgn(lowest_hp_bot['x'] - my_x), sgn(lowest_hp_bot['y'] - my_y)) else: self.comm.move(-sgn(lowest_hp_bot['x'] - my_x), -sgn(lowest_hp_bot['y'] - my_y)) if dist(my_bot, lowest_hp_bot) < 300 and lowest_hp_bot['life'] > 0: closest_bot = lowest_hp_bot self.rotateBot(my_bot, closest_bot, lowest_hp_bot) status = self.comm.send()
def __init__(self, bot_num): self.bot_num = bot_num self.comm = Comm(bot_num) self.half_screen_size = 450
class ShootWinner: def __init__(self, bot_num): self.bot_num = bot_num self.comm = Comm(bot_num) self.max_ammo = 10 self.bullet_speed = 700 self.min_score_dif = 100 self.small_health = 5 self.close_dist = 125 self.medium_dist = 250 self.dist_to_shoot = 200 def get_angle_with_speed_prediction(self, bot1, bot2, delta_time): x1 = bot1['x'] x2 = bot2['x'] y1 = bot1['y'] y2 = bot2['y'] vx = bot2['vx'] vy = bot2['vy'] a = vx**2 + vy**2 - self.bullet_speed**2 dx = (x2 - x1) dy = (y2 - y1) angle = (atan2(dy, dx) * 180 / pi) % 360 if a != 0: b = 2 * (vx * dx + vy * dy) c = dx * dx + dy * dy (t1, t2) = quadratic_equation(a, b, c) if t1 == 0: return angle if t1 < 0: t1 = 1000 if t2 < 0: t2 = 1000 if t1 > t2: t1 = t2 if t1 == 1000: return angle dx = x2 - x1 + vx * (t1 + delta_time) dy = y2 - y1 + vy * (t1 + delta_time) angle = (atan2(dy, dx) * 180 / pi) % 360 return angle def run(self): prev_time = time.clock() status = self.comm.send() while True: delta_time = (time.clock() - prev_time) prev_time = time.clock() bots = status['bots'] my = bots[self.bot_num] smallest_hp_bot = bots[0] closest_bot = bots[0] max_score_bot = bots[0] score_dif = 0 for current_bot in bots: if current_bot is not my: if current_bot['score'] > max_score_bot[ 'score'] and current_bot['life'] != 0: score_dif = current_bot['score'] - max_score_bot[ 'score'] max_score_bot = current_bot if dist(my, current_bot) < dist( my, closest_bot) and current_bot['life'] != 0: closest_bot = current_bot if (current_bot['life'] < smallest_hp_bot['life'] ) and current_bot['life'] != 0: smallest_hp_bot = current_bot if (my['life'] <= smallest_hp_bot['life']) or (score_dif < self.min_score_dif) \ or (smallest_hp_bot['life'] < self.small_health and smallest_hp_bot['life'] != 0): bot_to_target = smallest_hp_bot else: bot_to_target = max_score_bot y1 = my['y'] x1 = my['x'] x2 = closest_bot['x'] y2 = closest_bot['y'] x3 = bot_to_target['x'] y3 = bot_to_target['y'] if dist(my, bot_to_target) < self.medium_dist: if dist(my, bot_to_target) < self.close_dist: angle_to_enemy = get_angle_between_bots(my, bot_to_target) else: angle_to_enemy = self.get_angle_with_speed_prediction( my, bot_to_target, delta_time) else: bot_to_target = closest_bot if dist(my, closest_bot) < self.close_dist: angle_to_enemy = get_angle_between_bots(my, closest_bot) else: angle_to_enemy = self.get_angle_with_speed_prediction( my, closest_bot, delta_time) self.comm.rotate( rotation_to_target(angle_to_enemy, my['angle'] % 360)) if my['vx'] > 0: dx = 1 else: dx = -1 if my['vy'] > 0: dy = 1 else: dy = -1 if my['life'] > self.small_health: if dist(my, closest_bot) < self.close_dist: if (y1 - y2) > 0: if (x1 - x2) > 0: dx = 1 dy = -1 else: dy = 1 dx = 1 else: if (y1 - y2) > 0: dy = -1 dx = -1 else: dy = 1 dx = -1 else: dx = x3 - x1 dy = y3 - y1 self.comm.move(dx, dy) shoot = False if my['ammo'] >= self.max_ammo - 1 or dist(my, bot_to_target) < self.dist_to_shoot \ or bot_to_target['life'] < self.small_health: shoot = True self.comm.shoot(shoot) status = self.comm.send()
class AlmubotsEnv(gym.Env): metadata = {'render.modes': ['human']} def __init__(self, num_of_bots, bot_num): self.num_of_bots = num_of_bots self.bot_num = bot_num self.comm = Comm(bot_num) # posX, posY, velX, velY, angle, ammo, life, shoot, score low = np.tile( np.array([0, 0, -500, -500, 0, 0, 0, 0, 0], dtype=np.float32), self.num_of_bots) high = np.tile( np.array([900, 900, 500, 500, 360, 10, 20, 1, 1000], dtype=np.float32), self.num_of_bots) self.observation_space = spaces.Box(low, high, dtype=np.float32) # rotate: -1, 0, 1 # move: (-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1) # shoot: 0, 1 self.action_space = spaces.Discrete(54) self.state = None def step(self, action): err_msg = "%r (%s) invalid" % (action, type(action)) assert self.action_space.contains(action), err_msg self.comm.reset_cmd() # for values go to end of this file # shoot or not if action >= 27: self.comm.shoot(1) action -= 27 else: self.comm.shoot(0) # rotate lef, right, non if action < 9: self.comm.rotate(-1) elif action >= 18: self.comm.rotate(1) action -= 18 else: self.comm.rotate(0) action -= 9 # move x, y movement = { 0: (-1, -1), 1: (-1, 0), 2: (-1, 1), 3: (0, -1), 4: (0, 0), 5: (0, 1), 6: (1, -1), 7: (1, 0), 8: (1, 1) } self.comm.move(movement.get(action)[0], movement.get(action)[1]) state_raw = (self.comm.send()) bots_status = state_raw['bots'] self.state = [] for bot in bots_status: self.state.append(bot["x"]) self.state.append(bot["y"]) self.state.append(bot["vx"]) self.state.append(bot["vy"]) self.state.append(bot["angle"]) self.state.append(bot["ammo"]) self.state.append(bot["life"]) self.state.append(bot["shoot"]) self.state.append(bot["score"]) reward = bots_status[self.bot_num]['score'] done = state_raw['reset'] return np.array(self.state), reward, done, {} def reset(self): # env resets itself pass def render(self, mode='human'): # why render, when u can NOT TO FOR ONLY 19.99$ IF U CALL US RIGHT NOW! pass