Mercurial > rinkhals
comparison gamelib/gameboard.py @ 399:3294929223bd
Cache fox positions to avoid a repeated loop
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Tue, 17 Nov 2009 15:43:30 +0000 |
parents | 532f1ea476ff |
children | d146b7bb9b99 |
comparison
equal
deleted
inserted
replaced
398:082b1ea5f98d | 399:3294929223bd |
---|---|
290 self.animal_to_place = None | 290 self.animal_to_place = None |
291 self.sprite_cursor = None | 291 self.sprite_cursor = None |
292 self.chickens = set() | 292 self.chickens = set() |
293 self.foxes = set() | 293 self.foxes = set() |
294 self.buildings = [] | 294 self.buildings = [] |
295 self._fox_pos_cache = [] | |
295 self.cash = 0 | 296 self.cash = 0 |
296 self.eggs = 0 | 297 self.eggs = 0 |
297 self.days = 0 | 298 self.days = 0 |
298 self.killed_foxes = 0 | 299 self.killed_foxes = 0 |
299 self.add_cash(level.starting_cash) | 300 self.add_cash(level.starting_cash) |
364 def start_night(self): | 365 def start_night(self): |
365 self.day, self.night = False, True | 366 self.day, self.night = False, True |
366 self.tv.sun(False) | 367 self.tv.sun(False) |
367 self.reset_states() | 368 self.reset_states() |
368 self.toolbar.update_fin_tool(self.day) | 369 self.toolbar.update_fin_tool(self.day) |
370 self._cache_fox_positions() | |
369 | 371 |
370 def start_day(self): | 372 def start_day(self): |
371 self.day, self.night = True, False | 373 self.day, self.night = True, False |
372 self.tv.sun(True) | 374 self.tv.sun(True) |
373 self.reset_states() | 375 self.reset_states() |
800 if not over: | 802 if not over: |
801 self.foxes_attack() | 803 self.foxes_attack() |
802 self.chickens_attack() | 804 self.chickens_attack() |
803 return over | 805 return over |
804 | 806 |
807 def _cache_fox_positions(self): | |
808 """Cache the current set of fox positions for the avoiding checks""" | |
809 w, h = self.tv.size | |
810 self._fox_pos_cache = [[[False for z in range(5)] for y in range(h)] | |
811 for x in range(w)] # NB: Assumes z in [0, 4] | |
812 for fox in self.foxes: | |
813 if self.in_bounds(fox.pos): | |
814 self._fox_pos_cache[pos.x][pos.y][pos.z] = True | |
815 | |
816 def _update_fox_pos_cache(self, old_pos, new_pos): | |
817 if self.is_fox_at_pos(old_pos): | |
818 self._fox_pos_cache[old_pos.x][old_pos.y][old_pos.z] = False | |
819 if new_pos and self.in_bounds(new_pos): | |
820 self._fox_pos_cache[new_pos.x][new_pos.y][new_pos.z] = True | |
821 | |
822 def is_fox_at_pos(self, pos): | |
823 if self.in_bounds(pos): | |
824 return self._fox_pos_cache[pos.x][pos.y][pos.z] | |
825 return False | |
826 | |
805 def foxes_move(self): | 827 def foxes_move(self): |
806 over = True | 828 over = True |
807 for fox in self.foxes: | 829 for fox in self.foxes: |
830 old_pos = fox.pos | |
808 fox.move(self) | 831 fox.move(self) |
809 if not fox.safe: | 832 if not fox.safe: |
810 over = False | 833 over = False |
834 if fox.pos != old_pos: | |
835 self._update_fox_pos_cache(old_pos, fox.pos) | |
811 return over | 836 return over |
812 | 837 |
813 def foxes_attack(self): | 838 def foxes_attack(self): |
814 for fox in self.foxes: | 839 for fox in self.foxes: |
815 fox.attack(self) | 840 fox.attack(self) |
867 | 892 |
868 def kill_fox(self, fox): | 893 def kill_fox(self, fox): |
869 self.killed_foxes += 1 | 894 self.killed_foxes += 1 |
870 self.toolbar.update_fox_counter(self.killed_foxes) | 895 self.toolbar.update_fox_counter(self.killed_foxes) |
871 self.add_cash(self.level.sell_price_dead_fox) | 896 self.add_cash(self.level.sell_price_dead_fox) |
897 self._update_fox_pos_cache(fox.pos, None) | |
872 self.remove_fox(fox) | 898 self.remove_fox(fox) |
873 | 899 |
874 def remove_fox(self, fox): | 900 def remove_fox(self, fox): |
875 self.foxes.discard(fox) | 901 self.foxes.discard(fox) |
876 if fox.building: | 902 if fox.building: |