def move_by_tiles_to(me: Wizard, world: World, game: Game, move: Move, x: float, y: float) -> Tuple[float, float]: # We're already there? if me.get_distance_to(x, y) < 1.0: # Reached the destination. return x, y if me.get_distance_to(x, y) < DIRECT_MOVE_DISTANCE: # We can just move there. MyStrategy.move_to(me, world, game, move, x, y) return x, y # Find the nearest tile. my_index, (my_tile_x, my_tile_y) = min(enumerate(KEY_TILES), key=(lambda tile: me.get_distance_to(*tile[1]))) if not MyStrategy.is_in_tile(my_tile_x, my_tile_y, me.x, me.y): # We're away. Go to this tile. MyStrategy.move_to(me, world, game, move, my_tile_x, my_tile_y) return my_tile_x, my_tile_y # Find the destination tile. destination_index = next( i for i, (tile_x, tile_y) in enumerate(KEY_TILES) if MyStrategy.is_in_tile(tile_x, tile_y, x, y) ) # Find route between tiles. bfs_queue = [destination_index] bfs_visited = {destination_index} while bfs_queue: current_index = bfs_queue.pop(0) for previous_index in KEY_ADJACENT[current_index]: if previous_index == my_index: move_x, move_y = KEY_TILES[current_index] MyStrategy.move_to(me, world, game, move, move_x, move_y) return move_x, move_y if previous_index not in bfs_visited: bfs_queue.append(previous_index) bfs_visited.add(current_index) print("Failed to find route from %s, %s to %s, %s" % (me.x, me.y, x, y)) return x, y
def move(self, me: Wizard, world: World, game: Game, move: Move): # First, initialize some common things. attack_faction = self.get_attack_faction(me.faction) skills = set(me.skills) # Learn some skill. move.skill_to_learn = self.skill_to_learn(skills) # Apply some skill. move.status_target_id = me.id # Bonus pick up. bonus_tick_index = world.tick_index % 2500 if self.pick_up_bonus is None and (me.x > 1600.0 or me.y < 2400.0) and bonus_tick_index >= 2000: self.pick_up_bonus = min(BONUSES, key=(lambda bonus: me.get_distance_to(*bonus))) if me.x < 400.0 and me.y > 3600.0: self.pick_up_bonus = None if self.pick_up_bonus is not None: x, y = self.pick_up_bonus if not MyStrategy.attack_nearest_enemy(me, world, game, move, skills, attack_faction): move.turn = me.get_angle_to(x, y) if bonus_tick_index >= 2000 and me.get_distance_to(x, y) < me.radius + 2.0 * game.bonus_radius: # Bonus hasn't appeared yet. Stay nearby. return if 0 < bonus_tick_index < 2000 and ( # Bonus has just been picked up. me.get_distance_to(x, y) < me.radius or # No bonus there. (me.get_distance_to(x, y) < me.vision_range and not world.bonuses) ): self.pick_up_bonus = None else: self.move_by_tiles_to(me, world, game, move, x, y) return # Check if I'm healthy. if self.is_in_danger(me, world, game, me.x, me.y, attack_faction): # Retreat to the nearest safe tile. x, y = min(( (x, y) for x, y in KEY_TILES if not self.is_in_danger(me, world, game, x, y, attack_faction) ), key=(lambda point: me.get_distance_to(*point))) self.move_by_tiles_to(me, world, game, move, x, y) MyStrategy.attack_nearest_enemy(me, world, game, move, skills, attack_faction) return # Else try to attack the best target. if MyStrategy.attack_best_target(me, world, game, move, skills, attack_faction): return # Quick and dirty fix to avoid being stuck near the base. if me.x < 400.0 and me.y > 3600.0: move.turn = me.get_angle_to(*self.move_by_tiles_to(me, world, game, move, 200.0, 200.0)) return # Nothing to do. Just go to enemy base. x, y = self.move_by_tiles_to(me, world, game, move, ATTACK_BASE_X, ATTACK_BASE_Y) move.turn = me.get_angle_to(x, y)
def _near_waypoint(me: Wizard, wp_coords): return me.get_distance_to(*wp_coords) < me.radius * 2
def move(self, me: Wizard, world: World, game: Game, move: Move): self.log('TICK %s' % world.tick_index) self._init(game, me, world, move) self.log('me %s %s %f' % (me.x, me.y, me.get_distance_to(*self.INIT_POINT))) # initial cooldown if self.PASS_TICK_COUNT: self.PASS_TICK_COUNT -= 1 self.log('initial cooldown pass turn') return # select lane self._select_lane(me) # STRATEGY LOGIC enemy_targets = self._enemies_in_attack_distance(me) enemy_who_can_attack_me = self._enemies_who_can_attack_me(me) retreat_move_lock = False retreat_by_low_hp = False # чистим уже погибшие снаряды из карты current_projectiles_id = set( [p.id for p in self.W.projectiles if p.owner_unit_id == me.id]) cached_projectiles_id = set(self.PROJECTILE_MAP.keys()) for k in cached_projectiles_id - current_projectiles_id: del self.PROJECTILE_MAP[k] # ищем последний созданный снаряд и его цель для карты снарядов if self.PROJECTILE_LAST_ENEMY: for p in self.W.projectiles: if p.owner_unit_id == me.id and p.id not in self.PROJECTILE_MAP: self.PROJECTILE_MAP[p.id] = self.PROJECTILE_LAST_ENEMY self.PROJECTILE_LAST_ENEMY = None break self.log('projectile map %s' % str(self.PROJECTILE_MAP)) # если ХП мало отступаем if me.life < me.max_life * self.LOW_HP_FACTOR: retreat_by_low_hp = True self.log('retreat by low HP') if len(enemy_who_can_attack_me): self._goto_backward(me) retreat_move_lock = True if len(enemy_who_can_attack_me) > self.MAX_ENEMIES_IN_DANGER_ZONE: self.log('retreat by enemies in danger zone') self._goto_backward(me) retreat_move_lock = True # если врагов в радиусе обстрела нет - идём к их базе если не находимся в режиме отступления if not enemy_targets and not retreat_by_low_hp: self.log('move to next waypoint') self._goto_forward(me) # если на поле есть наши снаряды и расстояние до цели меньше расстояния каста # пробуем подойти к цели (если не находимся в отступлении) if not retreat_by_low_hp: potential_miss_enemies = self._find_potential_miss_enemy(me) self.log('found %s potential miss enemies' % potential_miss_enemies) if potential_miss_enemies: e = self._sort_by_angle(me, potential_miss_enemies)[0] self._goto_enemy(me, e) if enemy_targets: # есть враги в радиусе обстрела self.log('found %d enemies for attack' % len(enemy_targets)) selected_enemy = self._select_enemy_for_attack(me, enemy_targets) angle_to_enemy = me.get_angle_to_unit(selected_enemy) # если цель не в секторе атаки - поворачиваемся к ней (приоритет за направлением на точку отступления) if not self._enemy_in_attack_sector(me, selected_enemy): if not retreat_move_lock: self.log('select enemy for turn %s' % selected_enemy.id) self.MOVE_TURN = angle_to_enemy else: self.log('ignore select enemy for turn %s by retreat' % selected_enemy.id) else: # если можем атаковать - атакуем self.log('select enemy for attack %s' % selected_enemy.id) move.cast_angle = angle_to_enemy if self._enemy_in_cast_distance(me, selected_enemy): self.log('cast attack') move.action = ActionType.MAGIC_MISSILE move.min_cast_distance = self._cast_distance( me, selected_enemy) self.PROJECTILE_LAST_ENEMY = selected_enemy.id else: self.log('staff attack') move.action = ActionType.STAFF if self.MOVE_TURN is not None: move.turn = self.MOVE_TURN self.MOVE_TURN = None if self.MOVE_SPEED is not None: move.speed = self.MOVE_SPEED self.MOVE_SPEED = None if self.MOVE_STRAFE_SPEED is not None: move.strafe_speed = self.MOVE_STRAFE_SPEED self.MOVE_STRAFE_SPEED = None