annotate gamelib/animal.py @ 251:844bfb23d4b6

Refactored animal death and added death animations.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 05 Sep 2009 12:35:37 +0000
parents 4f86c2616cdf
children fcaae2cfe3cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 """Class for the various animals in the game"""
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
3 import random
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
4
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5 from pgu.vid import Sprite
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
6 from pgu.algo import getline
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7
44
7e884084e7b1 Move animal sprites to imagecache.
Simon Cross <hodgestar@gmail.com>
parents: 38
diff changeset
8 import imagecache
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
9 import tiles
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
10 from misc import Position
98
725b292ca07b Added sounds killing foxes and chickens, and nightfall
David Fraser <davidf@sjsoft.com>
parents: 92
diff changeset
11 import sound
109
48019afde338 Equipment purchasing and some toolbar tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 108
diff changeset
12 import equipment
241
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
13 import animations
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
15 class Animal(Sprite):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
16 """Base class for animals"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
17
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
18 STEALTH = 0
199
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
19 VISION_BONUS = 0
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
20 VISION_RANGE_PENALTY = 10
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
21
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
22 def __init__(self, image_left, image_right, tile_pos):
38
03121c89d5fd Make the secret foxes really secret
Neil Muller <drnlmuller@gmail.com>
parents: 32
diff changeset
23 # Create the animal somewhere far off screen
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
24 Sprite.__init__(self, image_left, (-1000, -1000))
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
25 self._image_left = image_left
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
26 self.image_left = image_left.copy()
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
27 self._image_right = image_right
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
28 self.image_right = image_right.copy()
171
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
29 if hasattr(tile_pos, 'to_tuple'):
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
30 self.pos = tile_pos
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
31 else:
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
32 self.pos = Position(tile_pos[0], tile_pos[1])
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
33 self.equipment = []
195
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
34 self.accoutrements = []
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
35 self.abode = None
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
36 self.facing = 'left'
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
38 def loop(self, tv, _sprite):
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
39 ppos = tv.tile_to_view(self.pos.to_tuple())
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
40 self.rect.x = ppos[0]
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
41 self.rect.y = ppos[1]
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
43 def die(self, gameboard):
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
44 """Play death animation, noises, whatever."""
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
45 if hasattr(self, 'DEATH_SOUND'):
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
46 sound.play_sound(self.DEATH_SOUND)
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
47 if hasattr(self, 'DEATH_ANIMATION'):
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
48 gameboard.animations.append(self.DEATH_ANIMATION(self.pos))
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
49 self._game_death(gameboard)
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
50
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
51 def _game_death(self, gameboard):
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
52 # Call appropriate gameboard cleanup here.
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
53 pass
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
54
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
55 def move(self, state):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
56 """Given the game state, return a new position for the object"""
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
57 # Default is not to move
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
58 pass
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
59
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
60 def set_pos(self, tile_pos):
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
61 """Move an animal to the given tile_pos."""
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
62 new_pos = Position(*tile_pos)
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
63 self._fix_face(new_pos)
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
64 self.pos = new_pos
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
65
236
9a6ac9c9ff46 chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 216
diff changeset
66 def _fix_face(self, facing_pos):
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
67 """Set the face correctly"""
236
9a6ac9c9ff46 chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 216
diff changeset
68 if facing_pos.left_of(self.pos):
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
69 self._set_image_facing('left')
236
9a6ac9c9ff46 chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 216
diff changeset
70 elif facing_pos.right_of(self.pos):
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
71 self._set_image_facing('right')
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
72
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
73 def _set_image_facing(self, facing):
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
74 self.facing = facing
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
75 if self.facing == 'left':
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
76 self.setimage(self.image_left)
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
77 elif self.facing == 'right':
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
78 self.setimage(self.image_right)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
79
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
80 def equip(self, item):
200
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
81 if equipment.is_equipment(item):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
82 self.equipment.append(item)
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
83 elif equipment.is_accoutrement(item):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
84 self.accoutrements.append(item)
163
0d6e23dcd3af fixed redrawing
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 161
diff changeset
85 self.redraw()
161
9b4213f6ea7f improved equipment layers; unequip method on animal
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 158
diff changeset
86
9b4213f6ea7f improved equipment layers; unequip method on animal
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 158
diff changeset
87 def unequip(self, item):
200
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
88 if equipment.is_equipment(item):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
89 self.equipment = [e for e in self.equipment if e != item]
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
90 elif equipment.is_accoutrement(item):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
91 self.accoutrements = [e for e in self.accoutrements if e != item]
163
0d6e23dcd3af fixed redrawing
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 161
diff changeset
92 self.redraw()
161
9b4213f6ea7f improved equipment layers; unequip method on animal
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 158
diff changeset
93
200
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
94 def unequip_by_name(self, item_name):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
95 # only remove first match
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
96 matches = [item for item in self.equipment + self.accoutrements if item.NAME == item_name]
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
97 if matches:
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
98 self.unequip(matches[0])
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
99
163
0d6e23dcd3af fixed redrawing
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 161
diff changeset
100 def redraw(self):
195
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
101 layers = [(self._image_left.copy(), self._image_right.copy(), 0)]
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
102 if hasattr(self, 'EQUIPMENT_IMAGE_ATTRIBUTE'):
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
103 for item in self.accoutrements + self.equipment:
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
104 images = item.images(self.EQUIPMENT_IMAGE_ATTRIBUTE)
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
105 if images:
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
106 layers.append(images)
158
baf857805867 armour works now
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 149
diff changeset
107
195
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
108 layers.sort(key=lambda l: l[2])
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
109
201
fe1e9c18d4d7 layering bugfix; indoor chickens now use normal chicken icons
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 200
diff changeset
110 # these always go on the bottom so that other layers don't get overwritten
fe1e9c18d4d7 layering bugfix; indoor chickens now use normal chicken icons
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 200
diff changeset
111 self.image_left = self._image_left.copy()
fe1e9c18d4d7 layering bugfix; indoor chickens now use normal chicken icons
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 200
diff changeset
112 self.image_right = self._image_right.copy()
fe1e9c18d4d7 layering bugfix; indoor chickens now use normal chicken icons
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 200
diff changeset
113 for l in layers:
195
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
114 self.image_left.blit(l[0], (0,0))
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
115 self.image_right.blit(l[1], (0,0))
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
116
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
117 self._set_image_facing(self.facing)
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
118
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
119 def weapons(self):
109
48019afde338 Equipment purchasing and some toolbar tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 108
diff changeset
120 return [e for e in self.equipment if equipment.is_weapon(e)]
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
121
174
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
122 def armour(self):
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
123 return [e for e in self.equipment if equipment.is_armour(e)]
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
124
104
d17375586866 Add .covers(tile_pos) to animals (to match similar function on buildings).
Simon Cross <hodgestar@gmail.com>
parents: 98
diff changeset
125 def covers(self, tile_pos):
d17375586866 Add .covers(tile_pos) to animals (to match similar function on buildings).
Simon Cross <hodgestar@gmail.com>
parents: 98
diff changeset
126 return tile_pos[0] == self.pos.x and tile_pos[1] == self.pos.y
d17375586866 Add .covers(tile_pos) to animals (to match similar function on buildings).
Simon Cross <hodgestar@gmail.com>
parents: 98
diff changeset
127
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 122
diff changeset
128 def outside(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 122
diff changeset
129 return self.abode is None
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 122
diff changeset
130
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
131 def damage(self, gameboard):
174
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
132 for a in self.armour():
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
133 if not a.survive_damage():
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
134 self.unequip(a)
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
135 return True
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
136 self.die(gameboard)
174
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
137 return False
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
138
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
139 class Chicken(Animal):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
140 """A chicken"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
141
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
142 EQUIPMENT_IMAGE_ATTRIBUTE = 'CHICKEN_IMAGE_FILE'
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
143 DEATH_ANIMATION = animations.ChickenDeath
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
144 DEATH_SOUND = 'kill-chicken.ogg'
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
145
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
146 def __init__(self, pos):
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
147 image_left = imagecache.load_image('sprites/chkn.png')
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
148 image_right = imagecache.load_image('sprites/chkn.png',
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
149 ("right_facing",))
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
150 Animal.__init__(self, image_left, image_right, pos)
243
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
151 self.eggs = []
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
152
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
153 def _game_death(self, gameboard):
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
154 gameboard.remove_chicken(self)
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
155
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
156 def move(self, gameboard):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
157 """A free chicken will move away from other free chickens"""
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
158 pass
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
159
116
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
160 def lay(self):
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
161 """See if the chicken lays an egg"""
243
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
162 if not self.eggs:
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
163 for x in range(random.randint(1, 4)):
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
164 self.eggs.append(Egg(self.pos))
216
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
165 self.equip(equipment.NestEgg())
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
166
243
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
167 def remove_eggs(self):
216
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
168 """Clean up the egg state"""
243
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
169 self.eggs = []
216
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
170 self.unequip_by_name("nestegg")
116
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
171
243
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
172 def remove_one_egg(self):
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
173 """Clean up the egg state"""
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
174 self.eggs.pop()
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
175 if not self.eggs:
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
176 self.unequip_by_name("nestegg")
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
177
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
178 def get_num_eggs(self):
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
179 return len(self.eggs)
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
180
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
181 def hatch(self, gameboard):
116
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
182 """See if we have an egg to hatch"""
243
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
183 if self.eggs:
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
184 chick = self.eggs[0].hatch()
187
6854e706dcdf Remove hatched eggs
Neil Muller <drnlmuller@gmail.com>
parents: 174
diff changeset
185 if chick:
243
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
186 # sell the remaining eggs
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
187 # Remove hatched egg
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
188 self.eggs.pop()
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
189 gameboard.eggs -= 1
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
190 # Sell other eggs
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
191 for egg in self.eggs[:]:
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
192 gameboard.sell_one_egg(self)
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
193 self.remove_eggs() # clean up stale images, etc.
187
6854e706dcdf Remove hatched eggs
Neil Muller <drnlmuller@gmail.com>
parents: 174
diff changeset
194 return chick
116
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
195 return None
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
196
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
197 def _find_killable_fox(self, weapon, gameboard):
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
198 """Choose a random fox within range of this weapon."""
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
199 killable_foxes = []
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
200 for fox in gameboard.foxes:
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
201 if not visible(self, fox):
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
202 continue
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
203 if weapon.in_range(gameboard, self, fox):
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
204 killable_foxes.append(fox)
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
205 if not killable_foxes:
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
206 return None
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
207 return random.choice(killable_foxes)
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
208
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
209 def attack(self, gameboard):
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
210 """An armed chicken will attack a fox within range."""
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
211 if not self.weapons():
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
212 # Not going to take on a fox bare-winged.
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
213 return
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
214 # Choose the first weapon equipped.
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
215 weapon = self.weapons()[0]
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
216 fox = self._find_killable_fox(weapon, gameboard)
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
217 if not fox:
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
218 return
236
9a6ac9c9ff46 chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 216
diff changeset
219 self._fix_face(fox.pos)
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
220 if weapon.hit(gameboard, self, fox):
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
221 fox.damage(gameboard)
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
222
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
223 class Egg(Animal):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
224 """An egg"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
225
47
be2496df2368 Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents: 44
diff changeset
226 def __init__(self, pos):
be2496df2368 Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents: 44
diff changeset
227 image = imagecache.load_image('sprites/egg.png')
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
228 Animal.__init__(self, image, image, pos)
243
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
229 self.timer = 2
47
be2496df2368 Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents: 44
diff changeset
230
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
231 # Eggs don't move
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
232
171
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
233 def hatch(self):
243
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
234 self.timer -= 1
4f86c2616cdf Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents: 241
diff changeset
235 if self.timer == 0:
171
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
236 return Chicken(self.pos)
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
237 return None
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
238
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
239 class Fox(Animal):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
240 """A fox"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
241
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
242 STEALTH = 20
130
96c5ef7613b5 NINJA FOXES!
Jeremy Thurgood <firxen@gmail.com>
parents: 125
diff changeset
243 IMAGE_FILE = 'sprites/fox.png'
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
244 DEATH_ANIMATION = animations.FoxDeath
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
245 DEATH_SOUND = 'kill-fox.ogg'
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
246
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
247 costs = {
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
248 # weighting for movement calculation
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
249 'grassland' : 2,
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
250 'woodland' : 1, # Try to keep to the woods if possible
72
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
251 'broken fence' : 2,
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
252 'fence' : 10,
72
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
253 'guardtower' : 2, # We can pass under towers
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
254 'henhouse' : 30, # Don't go into a henhouse unless we're going to
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
255 # catch a chicken there
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
256 'hendominium' : 30,
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
257 }
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
258
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
259 def __init__(self, pos):
130
96c5ef7613b5 NINJA FOXES!
Jeremy Thurgood <firxen@gmail.com>
parents: 125
diff changeset
260 image_left = imagecache.load_image(self.IMAGE_FILE)
96c5ef7613b5 NINJA FOXES!
Jeremy Thurgood <firxen@gmail.com>
parents: 125
diff changeset
261 image_right = imagecache.load_image(self.IMAGE_FILE, ("right_facing",))
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
262 Animal.__init__(self, image_left, image_right, pos)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
263 self.landmarks = [self.pos]
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
264 self.hunting = True
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
265 self.dig_pos = None
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
266 self.tick = 0
122
d2b19131d537 Don't continue the night if we're not doing anything anymore
Neil Muller <drnlmuller@gmail.com>
parents: 116
diff changeset
267 self.safe = False
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
268 self.closest = None
149
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
269 self.last_steps = []
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
270
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
271 def _game_death(self, gameboard):
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
272 gameboard.kill_fox(self)
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
273
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
274 def _cost_tile(self, pos, gameboard):
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
275 if gameboard.in_bounds(pos):
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
276 this_tile = gameboard.tv.get(pos.to_tuple())
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
277 cost = self.costs.get(tiles.TILE_MAP[this_tile], 100)
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
278 else:
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
279 cost = 100 # Out of bounds is expensive
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
280 return cost
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
281
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
282 def _cost_path(self, path, gameboard):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
283 """Calculate the cost of a path"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
284 total = 0
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
285 for pos in path:
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
286 total += self._cost_tile(pos, gameboard)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
287 return total
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
288
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
289 def _gen_path(self, start_pos, final_pos):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
290 """Construct a direct path from start_pos to final_pos,
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
291 excluding start_pos"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
292 if abs(start_pos.x - final_pos.x) < 2 and \
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
293 abs(start_pos.y - final_pos.y) < 2:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
294 # pgu gets this case wrong on occasion.
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
295 return [final_pos]
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
296 start = start_pos.to_tuple()
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
297 end = final_pos.to_tuple()
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
298 points = getline(start, end)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
299 points.remove(start) # exclude start_pos
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
300 if end not in points:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
301 # Rounding errors in getline cause this
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
302 points.append(end)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
303 return [Position(x[0], x[1]) for x in points]
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
304
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
305 def _find_best_path_step(self, final_pos, gameboard):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
306 """Find the cheapest path to final_pos, and return the next step
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
307 along the path."""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
308 # We calculate the cost of the direct path
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
309 direct_path = self._gen_path(self.pos, final_pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
310 min_cost = self._cost_path(direct_path, gameboard)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
311 min_path = direct_path
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
312 # is there a point nearby that gives us a cheaper direct path?
71
00cf9d7f22dc Expand comment
Neil Muller <drnlmuller@gmail.com>
parents: 70
diff changeset
313 # This is delibrately not finding the optimal path, as I don't
00cf9d7f22dc Expand comment
Neil Muller <drnlmuller@gmail.com>
parents: 70
diff changeset
314 # want the foxes to be too intelligent, although the implementation
00cf9d7f22dc Expand comment
Neil Muller <drnlmuller@gmail.com>
parents: 70
diff changeset
315 # isn't well optimised yet
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
316 poss = [Position(x, y) for x in range(self.pos.x - 3, self.pos.x + 4)
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
317 for y in range(self.pos.y - 3, self.pos.y + 4)
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
318 if (x, y) != (0,0)]
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
319 for start in poss:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
320 cand_path = self._gen_path(self.pos, start) + \
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
321 self._gen_path(start, final_pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
322 cost = self._cost_path(cand_path, gameboard)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
323 if cost < min_cost:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
324 min_cost = cost
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
325 min_path = cand_path
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
326 if not min_path:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
327 return final_pos
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
328 return min_path[0]
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
329
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
330 def _find_path_to_woodland(self, gameboard):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
331 """Dive back to woodland through the landmarks"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
332 # find the closest point to our current location in walked path
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
333 if self.pos == self.landmarks[-1]:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
334 if len(self.landmarks) > 1:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
335 self.landmarks.pop() # Moving to the next landmark
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
336 else:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
337 # Safely back at the start
122
d2b19131d537 Don't continue the night if we're not doing anything anymore
Neil Muller <drnlmuller@gmail.com>
parents: 116
diff changeset
338 self.safe = True
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
339 return self.pos
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
340 return self._find_best_path_step(self.landmarks[-1], gameboard)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
341
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
342 def _find_path_to_chicken(self, gameboard):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
343 """Find the path to the closest chicken"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
344 # Find the closest chicken
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
345 min_dist = 999
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
346 if self.closest not in gameboard.chickens:
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
347 # Either no target, or someone ate it
196
edcb5edfa0ff If the closest chicken is gone, clear it.
Jeremy Thurgood <firxen@gmail.com>
parents: 195
diff changeset
348 self.closest = None
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
349 for chicken in gameboard.chickens:
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
350 dist = chicken.pos.dist(self.pos)
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
351 if chicken.abode:
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
352 dist += 10 # Prefer free-ranging chickens
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
353 if dist < min_dist:
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
354 min_dist = dist
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
355 self.closest = chicken
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
356 if not self.closest:
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
357 # No more chickens, so leave
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
358 self.hunting = False
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
359 return self.pos
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
360 if self.closest.pos == self.pos:
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
361 # Caught a chicken
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
362 self._catch_chicken(self.closest, gameboard)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
363 return self.pos
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
364 return self._find_best_path_step(self.closest.pos, gameboard)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
365
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
366 def _catch_chicken(self, chicken, gameboard):
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
367 """Catch a chicken"""
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
368 chicken.damage(gameboard)
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
369 self.closest = None
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
370 self.hunting = False
149
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
371 self.last_steps = [] # Forget history here
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
372
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
373 def _update_pos(self, gameboard, new_pos):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
374 """Update the position, making sure we don't step on other foxes"""
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
375 if new_pos == self.pos:
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
376 # We're not moving, so we can skip all the checks
149
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
377 return new_pos
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
378 final_pos = new_pos
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
379 blocked = final_pos in self.last_steps
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
380 moves = [Position(x, y) for x in range(self.pos.x-1, self.pos.x + 2)
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
381 for y in range(self.pos.y-1, self.pos.y + 2)
149
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
382 if Position(x,y) != self.pos and \
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
383 Position(x, y) not in self.last_steps]
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
384 for fox in gameboard.foxes:
149
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
385 if fox is not self and fox.pos == final_pos:
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
386 blocked = True
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
387 if fox.pos in moves:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
388 moves.remove(fox.pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
389 if blocked:
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
390 # find the cheapest point in moves to new_pos that's not blocked
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
391 final_pos = None
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
392 min_cost = 1000
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
393 for poss in moves:
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
394 cost = self._cost_tile(poss, gameboard)
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
395 if cost < min_cost:
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
396 min_cost = cost
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
397 final_pos = poss
149
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
398 if not final_pos:
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
399 # No good choice, so stay put
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
400 return self.pos
72
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
401 if gameboard.in_bounds(final_pos):
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
402 this_tile = gameboard.tv.get(final_pos.to_tuple())
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
403 else:
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
404 this_tile = tiles.REVERSE_TILE_MAP['woodland']
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
405 if tiles.TILE_MAP[this_tile] == 'broken fence' and self.hunting:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
406 # We'll head back towards the holes we make/find
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
407 self.landmarks.append(final_pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
408 elif tiles.TILE_MAP[this_tile] == 'fence' and not self.dig_pos:
241
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
409 self._dig(gameboard, final_pos)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
410 return self.pos
149
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
411 self.last_steps.append(final_pos)
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
412 if len(self.last_steps) > 3:
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
413 self.last_steps.pop(0)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
414 return final_pos
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
415
241
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
416 def _dig(self, gameboard, dig_pos):
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
417 """Setup dig parameters, to be overridden if needed"""
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
418 self.tick = 5
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
419 self.dig_pos = dig_pos
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
420
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
421 def _make_hole(self, gameboard):
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
422 """Make a hole in the fence"""
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
423 gameboard.tv.set(self.dig_pos.to_tuple(),
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
424 tiles.REVERSE_TILE_MAP['broken fence'])
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
425 self.dig_pos = None
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
426
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
427 def move(self, gameboard):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
428 """Foxes will aim to move towards the closest henhouse or free
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
429 chicken"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
430 if self.dig_pos:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
431 if self.tick:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
432 self.tick -= 1
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
433 # We're still digging through the fence
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
434 # Check the another fox hasn't dug a hole for us
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
435 # We're too busy digging to notice if a hole appears nearby,
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
436 # but we'll notice if the fence we're digging vanishes
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
437 this_tile = gameboard.tv.get(self.dig_pos.to_tuple())
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
438 if tiles.TILE_MAP[this_tile] == 'broken fence':
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
439 self.tick = 0
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
440 return
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
441 else:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
442 # We've dug through the fence, so make a hole
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
443 self._make_hole(gameboard)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
444 return
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
445 if self.hunting:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
446 desired_pos = self._find_path_to_chicken(gameboard)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
447 else:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
448 desired_pos = self._find_path_to_woodland(gameboard)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
449 final_pos = self._update_pos(gameboard, desired_pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
450 self._fix_face(final_pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
451 self.pos = final_pos
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
452
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
453 class NinjaFox(Fox):
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
454 """Ninja foxes are hard to see"""
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
455
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
456 STEALTH = 60
130
96c5ef7613b5 NINJA FOXES!
Jeremy Thurgood <firxen@gmail.com>
parents: 125
diff changeset
457 IMAGE_FILE = 'sprites/ninja_fox.png'
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
458
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
459 class DemoFox(Fox):
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
460 """Demolition Foxes destroy fences easily"""
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
461
241
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
462 DIG_ANIMATION = animations.FenceExplosion
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
463 IMAGE_FILE = 'sprites/sapper_fox.png'
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
464
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
465 def _dig(self, gameboard, dig_pos):
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
466 """Setup dig parameters, to be overridden if needed"""
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
467 self.tick = 0 # Costs us nothing to go through a fence.
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
468 self.dig_pos = dig_pos
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
469 gameboard.animations.append(self.DIG_ANIMATION(dig_pos))
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
470 self._make_hole(gameboard)
1a7000c8211c Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents: 236
diff changeset
471
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
472 class GreedyFox(Fox):
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
473 """Greedy foxes eat more chickens"""
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
474
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
475 def __init__(self, pos):
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
476 Fox.__init__(self, pos)
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
477 self.chickens_eaten = 0
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
478
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
479 def _catch_chicken(self, chicken, gameboard):
251
844bfb23d4b6 Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents: 243
diff changeset
480 chicken.damage(gameboard)
145
490ede177f50 Tweak fox behaviour around henhouses. Add some memory to avoid the indecisive fox loop
Neil Muller <drnlmuller@gmail.com>
parents: 130
diff changeset
481 self.closest = None
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
482 self.chickens_eaten += 1
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
483 if self.chickens_eaten > 2:
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
484 self.hunting = False
149
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
485 self.last_steps = []
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
486
199
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
487 def _get_vision_param(parameter, watcher):
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
488 param = getattr(watcher, parameter)
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
489 if watcher.abode:
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
490 modifier = getattr(watcher.abode.building, 'MODIFY_'+parameter, lambda r: r)
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
491 param = modifier(param)
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
492 return param
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
493
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
494 def visible(watcher, watchee):
199
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
495 vision_bonus = _get_vision_param('VISION_BONUS', watcher)
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
496 range_penalty = _get_vision_param('VISION_RANGE_PENALTY', watcher)
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
497 distance = watcher.pos.dist(watchee.pos) - 1
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
498 roll = random.randint(1, 100)
199
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
499 return roll > watchee.STEALTH - vision_bonus + range_penalty*distance