# HG changeset patch # User Simon Cross # Date 1252105154 0 # Node ID c279ad59b8e2651038c86e2dab6a0c76156bd6f3 # Parent 527a5d4e3fa3430f8c73f104b92fe0eea7cbf382 Chicks fill hen house, then adjacent spaces, then die. diff -r 527a5d4e3fa3 -r c279ad59b8e2 gamelib/buildings.py --- a/gamelib/buildings.py Fri Sep 04 22:39:19 2009 +0000 +++ b/gamelib/buildings.py Fri Sep 04 22:59:14 2009 +0000 @@ -121,6 +121,21 @@ for dy in xrange(ysize): yield (xpos + dx, ypos + dy) + def adjacent_tiles(self): + """Return pairs of (x, y) tile positions for each of the tiles + adjacent to the building. + """ + xpos, ypos = self.pos + xsize, ysize = self.size + + for dx in xrange(xsize): # top and bottom + yield (xpos + dx, ypos - 1) + yield (xpos + dx, ypos + ysize) + + for dy in xrange(ysize): # left and right + yield (xpos - 1, ypos + dy) + yield (xpos + xsize, ypos + dy) + def loop(self, tv, _sprite): ppos = tv.tile_to_view(self.pos) self.rect.x = ppos[0] diff -r 527a5d4e3fa3 -r c279ad59b8e2 gamelib/gameboard.py --- a/gamelib/gameboard.py Fri Sep 04 22:39:19 2009 +0000 +++ b/gamelib/gameboard.py Fri Sep 04 22:59:14 2009 +0000 @@ -712,10 +712,17 @@ self.add_chicken(new_chick) new_chick.equip(equipment.Nest()) except buildings.BuildingFullError: + # No space in the hen house, look nearby + for tile_pos in building.adjacent_tiles(): + if self.tv.get(tile_pos) != self.GRASSLAND: + continue + if self.get_outside_chicken(tile_pos) is None: + self.add_chicken(new_chick) + self.relocate_animal(new_chick, tile_pos=tile_pos) + break # if there isn't a space for the # new chick it dies. :/ Farm life # is cruel. - pass self.toolbar.update_egg_counter(self.eggs) def kill_fox(self, fox):