comparison gamelib/gameboard.py @ 599:460b06ac7d99

Better chicken placement, now with fewer crashes.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 30 Nov 2009 17:39:07 +0000
parents 2899ddc4d92c
children 23fbf4651f99
comparison
equal deleted inserted replaced
598:029e60f37743 599:460b06ac7d99
569 if cur_chick == chicken: 569 if cur_chick == chicken:
570 continue 570 continue
571 if cur_chick: 571 if cur_chick:
572 try_pos = None 572 try_pos = None
573 # find a free square nearby 573 # find a free square nearby
574 poss = [(tile_pos[0] + x, tile_pos[1] + y) 574 diff_pos = misc.Position(*tile_pos)
575 for x in range(-1, 2) for y in range(-1, 2) 575 poss = [diff_pos + misc.Position(x, y)
576 for x in range(-2, 3)
577 for y in range(-2, 3)
576 if (x, y) != (0, 0)] 578 if (x, y) != (0, 0)]
577 poss.extend([(tile_pos[0] + x, tile_pos[1] + y) 579 poss.sort(key=lambda p: p.dist(diff_pos))
578 for x in range(-2, 3, 2) for y in range(-2, 3)
579 if (x, y) != (0, 0)])
580 for cand in poss: 580 for cand in poss:
581 if self.tv.get(cand) == self.GRASSLAND and \ 581 if not self.in_bounds(cand):
582 not self.get_outside_chicken(cand): 582 continue
583 try_pos = cand 583 if self.tv.get(cand.to_tile_tuple()) == self.GRASSLAND and \
584 not self.get_outside_chicken(cand.to_tile_tuple()):
585 try_pos = cand.to_tile_tuple()
584 break 586 break
585 if try_pos: 587 if try_pos:
586 chicken.unequip_by_name("Nest") 588 chicken.unequip_by_name("Nest")
587 self.relocate_animal(chicken, tile_pos=try_pos) 589 self.relocate_animal(chicken, tile_pos=try_pos)
588 chicken.remove_eggs() 590 chicken.remove_eggs()