Ejemplo n.º 1
0
    def enemies_in_range(self):
        """
        Returns a list of all the enemies whose distance from the hill is
        less than self.danger_radius.
        """

        enemies = list(
            e[0] for e in self.world.enemy_ants()
            if castar.pathdist(e[0], self.myhill, self.danger_radius))
        return enemies
Ejemplo n.º 2
0
    def newturn(self):
        """
        Add ants to this group. Calculate the policy.
        """
        super(WarriorsFlock, self).newturn()
        bot = self.leader.bot
        self.all_enemies = set(self.ants_enemies_in_range(self.danger_radius))
        self.log.info("Enemies around: %s", self.all_enemies)
        close_enemy = len(self.all_enemies) > 0

        ehill_d = self.world.distance(self.leader.pos, self.attack_pos)

        mhill_l = self.leader.my_hills()
        if len(mhill_l) == 0: #strange things happen with ants_numpy
            mhill_d = 0
        else:
            mhill_d = min(mhill_l)[0]

        #the enemy hill is closer than the home hill
        if (ehill_d < 2*mhill_d):
            self.log.info("The enemy hill is close, copting")
            antlist = bot.ants
        else:
            self.log.info("The enemy hills is not close, not copting")
            antlist = []
        
        copted_ants = (a for a in antlist
                       if self.check_if_grab(a) and 
                       castar.pathdist(self.leader.pos, a.pos, 
                                       self.min_dist_to_copt)
                      )
        
        for ant in copted_ants:
            if len(self.controlled_ants) >= self.max_ants:
                self.log.info("max number of ants reached")
                break
            self.control(ant)

        self.calculate_grouping()
        
        #transitions
        if close_enemy:
            self.calculate_policy()
            self.log.info("Moving all the ants with a policy")
            for ant, d in self.policy.iteritems():
                ant.move_heading(d)
            self.transition_delayed("follow_policy")
            self.setup_planner(True)
        else:
            self.log.info("I can move freely towards the target %s", 
                    self.attack_pos)
            self.setup_planner(False)
            return self.transition_delayed("attack")
    def step(self):
        """
        Creates a DefendersFlock for each threatened hill, if it has not been
        assigned yet.
        """
        self.my_hills = set(self.world.my_hills())

        for h in self.my_hills:
            enemies = list( e[0] for e in self.world.enemy_ants()
                            if castar.pathdist(e[0], h, self.danger_radius))
            if len(enemies):
                self.create_flock(h, enemies)
Ejemplo n.º 4
0
    def step(self):
        """
        Creates a DefendersFlock for each threatened hill, if it has not been
        assigned yet.
        """
        self.my_hills = set(self.world.my_hills())

        for h in self.my_hills:
            enemies = list(e[0] for e in self.world.enemy_ants()
                           if castar.pathdist(e[0], h, self.danger_radius))
            if len(enemies):
                self.create_flock(h, enemies)
Ejemplo n.º 5
0
    def newturn(self):
        """
        Add ants to this group. Calculate the policy.
        """
        super(WarriorsFlock, self).newturn()
        bot = self.leader.bot
        self.all_enemies = set(self.ants_enemies_in_range(self.danger_radius))
        self.log.info("Enemies around: %s", self.all_enemies)
        close_enemy = len(self.all_enemies) > 0

        ehill_d = self.world.distance(self.leader.pos, self.attack_pos)

        mhill_l = self.leader.my_hills()
        if len(mhill_l) == 0:  #strange things happen with ants_numpy
            mhill_d = 0
        else:
            mhill_d = min(mhill_l)[0]

        #the enemy hill is closer than the home hill
        if (ehill_d < 2 * mhill_d):
            self.log.info("The enemy hill is close, copting")
            antlist = bot.ants
        else:
            self.log.info("The enemy hills is not close, not copting")
            antlist = []

        copted_ants = (a for a in antlist
                       if self.check_if_grab(a) and castar.pathdist(
                           self.leader.pos, a.pos, self.min_dist_to_copt))

        for ant in copted_ants:
            if len(self.controlled_ants) >= self.max_ants:
                self.log.info("max number of ants reached")
                break
            self.control(ant)

        self.calculate_grouping()

        #transitions
        if close_enemy:
            self.calculate_policy()
            self.log.info("Moving all the ants with a policy")
            for ant, d in self.policy.iteritems():
                ant.move_heading(d)
            self.transition_delayed("follow_policy")
            self.setup_planner(True)
        else:
            self.log.info("I can move freely towards the target %s",
                          self.attack_pos)
            self.setup_planner(False)
            return self.transition_delayed("attack")
Ejemplo n.º 6
0
def create(calling_ant, neighbour_dist):
    """
    Create a new WarriorsFlock made by all the non aggregated warrior within
    neighbour_dist.
    """
    ant_list = set([calling_ant])
    bot = calling_ant.bot
    free_ants = (
        a for a in bot.ants
        if WarriorsFlock.check_if_grab(a) and type(a) is warrior.Warrior
        and castar.pathdist(calling_ant.pos, a.pos, neighbour_dist))
    for ant in free_ants:
        if len(ant_list) >= WarriorsFlock.max_ants:
            break
        ant_list.add(ant)
    if len(ant_list) < WarriorsFlock.min_group_size:
        return False

    WarriorsFlock(calling_ant, ant_list, neighbour_dist)
    return True
Ejemplo n.º 7
0
def create(calling_ant, neighbour_dist):
    """
    Create a new WarriorsFlock made by all the non aggregated warrior within
    neighbour_dist.
    """
    ant_list = set([calling_ant])
    bot = calling_ant.bot
    free_ants = (a for a in bot.ants
                    if WarriorsFlock.check_if_grab(a) and
                    type(a) is warrior.Warrior and
                    castar.pathdist(calling_ant.pos, a.pos, neighbour_dist)
                 )
    for ant in free_ants:
        if len(ant_list) >= WarriorsFlock.max_ants:
            break
        ant_list.add(ant)
    if len(ant_list) < WarriorsFlock.min_group_size:
        return False
    
    WarriorsFlock(calling_ant, ant_list, neighbour_dist)
    return True
Ejemplo n.º 8
0
 def check_enemy_hills(self):
     return (any(castar.pathdist(self.pos, h[0], self.danger_radius) 
                         for h in self.world.enemy_hills()) 
             and
             (len(self.enemies) == 0)
            )
Ejemplo n.º 9
0
 def dist_criterion(ant):
     return castar.pathdist(ant.pos, calling_ant.pos, neighbour_dist)
Ejemplo n.º 10
0
 def check_enemy_hills(self):
     return (any(
         castar.pathdist(self.pos, h[0], self.danger_radius)
         for h in self.world.enemy_hills()) and (len(self.enemies) == 0))