Changeset 145:490ede177f50


Ignore:
Timestamp:
Sep 3, 2009, 3:14:13 PM (11 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@146
Message:

Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gamelib/animal.py

    r130 r145  
    140140            'fence' : 10,
    141141            'guardtower' : 2, # We can pass under towers
    142             'henhouse' : 2,
     142            'henhouse' : 30, # Don't go into a henhouse unless we're going to
     143                             # catch a chicken there
     144            'hendominium' : 30,
    143145            }
    144146
     
    152154        self.tick = 0
    153155        self.safe = False
     156        self.closest = None
    154157
    155158    def _cost_tile(self, pos, gameboard):
     
    225228        # Find the closest chicken
    226229        min_dist = 999
    227         closest = None
    228         for chicken in gameboard.chickens:
    229             dist = chicken.pos.dist(self.pos)
    230             if dist < min_dist:
    231                 min_dist = dist
    232                 closest = chicken
    233         if not closest:
     230        if self.closest not in gameboard.chickens:
     231            # Either no target, or someone ate it
     232            for chicken in gameboard.chickens:
     233                dist = chicken.pos.dist(self.pos)
     234                if chicken.abode:
     235                    dist += 10 # Prefer free-ranging chickens
     236                if dist < min_dist:
     237                    min_dist = dist
     238                    self.closest = chicken
     239        if not self.closest:
    234240            # No more chickens, so leave
    235241            self.hunting = False
    236242            return self.pos
    237         if closest.pos == self.pos:
     243        if self.closest.pos == self.pos:
    238244            # Caught a chicken
    239             self._catch_chicken(closest, gameboard)
     245            self._catch_chicken(self.closest, gameboard)
    240246            return self.pos
    241         return self._find_best_path_step(closest.pos, gameboard)
     247        return self._find_best_path_step(self.closest.pos, gameboard)
    242248
    243249    def _catch_chicken(self, chicken, gameboard):
    244250        """Catch a chicken"""
    245251        sound.play_sound("kill-chicken.ogg")
     252        self.closest = None
    246253        gameboard.remove_chicken(chicken)
    247254        self.hunting = False
     
    250257        """Update the position, making sure we don't step on other foxes"""
    251258        final_pos = new_pos
     259        if new_pos == self.pos:
     260            # We're not moving, so we can skip all the checks
     261            return final_pos
     262        blocked = False
    252263        moves = [Position(x, y) for x in range(self.pos.x-1, self.pos.x + 2)
    253264                for y in range(self.pos.y-1, self.pos.y + 2)
    254265                if (x,y) != (0,0)]
    255         blocked = False
    256266        for fox in gameboard.foxes:
    257267            if fox is not self and fox.pos == new_pos:
     
    335345    def _catch_chicken(self, chicken, gameboard):
    336346        gameboard.remove_chicken(chicken)
     347        self.closest = None
    337348        self.chickens_eaten += 1
    338349        if self.chickens_eaten > 2:
Note: See TracChangeset for help on using the changeset viewer.