annotate gamelib/animal.py @ 236:9a6ac9c9ff46

chickens turn to face target foxes
author Adrianna Pińska <adrianna.pinska@gmail.com>
date Sat, 05 Sep 2009 09:46:29 +0000
parents 962934b8c7dc
children 1a7000c8211c
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
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
13
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14 class Animal(Sprite):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
15 """Base class for animals"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
16
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
17 STEALTH = 0
199
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
18 VISION_BONUS = 0
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
19 VISION_RANGE_PENALTY = 10
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
20
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
21 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
22 # 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
23 Sprite.__init__(self, image_left, (-1000, -1000))
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
24 self._image_left = image_left
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
25 self.image_left = image_left.copy()
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
26 self._image_right = image_right
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
27 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
28 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
29 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
30 else:
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
31 self.pos = Position(tile_pos[0], tile_pos[1])
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
32 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
33 self.accoutrements = []
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
34 self.abode = None
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
35 self.facing = 'left'
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
36
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
37 def loop(self, tv, _sprite):
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
38 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
39 self.rect.x = ppos[0]
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
40 self.rect.y = ppos[1]
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
42 def move(self, state):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
43 """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
44 # Default is not to move
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
45 pass
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
46
108
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
47 def set_pos(self, tile_pos):
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
48 """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
49 new_pos = Position(*tile_pos)
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
50 self._fix_face(new_pos)
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
51 self.pos = new_pos
437cbd856a03 Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents: 104
diff changeset
52
236
9a6ac9c9ff46 chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 216
diff changeset
53 def _fix_face(self, facing_pos):
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
54 """Set the face correctly"""
236
9a6ac9c9ff46 chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 216
diff changeset
55 if facing_pos.left_of(self.pos):
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
56 self._set_image_facing('left')
236
9a6ac9c9ff46 chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 216
diff changeset
57 elif facing_pos.right_of(self.pos):
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
58 self._set_image_facing('right')
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
59
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
60 def _set_image_facing(self, facing):
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
61 self.facing = facing
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
62 if self.facing == 'left':
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
63 self.setimage(self.image_left)
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
64 elif self.facing == 'right':
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
65 self.setimage(self.image_right)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
66
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
67 def equip(self, item):
200
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
68 if equipment.is_equipment(item):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
69 self.equipment.append(item)
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
70 elif equipment.is_accoutrement(item):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
71 self.accoutrements.append(item)
163
0d6e23dcd3af fixed redrawing
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 161
diff changeset
72 self.redraw()
161
9b4213f6ea7f improved equipment layers; unequip method on animal
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 158
diff changeset
73
9b4213f6ea7f improved equipment layers; unequip method on animal
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 158
diff changeset
74 def unequip(self, item):
200
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
75 if equipment.is_equipment(item):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
76 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
77 elif equipment.is_accoutrement(item):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
78 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
79 self.redraw()
161
9b4213f6ea7f improved equipment layers; unequip method on animal
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 158
diff changeset
80
200
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
81 def unequip_by_name(self, item_name):
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
82 # only remove first match
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
83 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
84 if matches:
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
85 self.unequip(matches[0])
67d10f7e0159 selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 199
diff changeset
86
163
0d6e23dcd3af fixed redrawing
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 161
diff changeset
87 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
88 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
89 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
90 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
91 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
92 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
93 layers.append(images)
158
baf857805867 armour works now
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 149
diff changeset
94
195
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
95 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
96
201
fe1e9c18d4d7 layering bugfix; indoor chickens now use normal chicken icons
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 200
diff changeset
97 # 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
98 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
99 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
100 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
101 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
102 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
103
9d31cfc3afde refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 187
diff changeset
104 self._set_image_facing(self.facing)
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
105
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
106 def weapons(self):
109
48019afde338 Equipment purchasing and some toolbar tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents: 108
diff changeset
107 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
108
174
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
109 def armour(self):
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
110 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
111
104
d17375586866 Add .covers(tile_pos) to animals (to match similar function on buildings).
Simon Cross <hodgestar@gmail.com>
parents: 98
diff changeset
112 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
113 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
114
125
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 122
diff changeset
115 def outside(self):
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 122
diff changeset
116 return self.abode is None
2e3a05b9594d Chickens in buildings\!
Simon Cross <hodgestar@gmail.com>
parents: 122
diff changeset
117
174
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
118 def survive_damage(self):
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
119 for a in self.armour():
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
120 if not a.survive_damage():
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
121 self.unequip(a)
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
122 return True
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
123 return False
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
124
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
125 class Chicken(Animal):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
126 """A chicken"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
127
146
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
128 EQUIPMENT_IMAGE_ATTRIBUTE = 'CHICKEN_IMAGE_FILE'
423050ec188b Equipment images.
Jeremy Thurgood <firxen@gmail.com>
parents: 145
diff changeset
129
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
130 def __init__(self, pos):
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
131 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
132 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
133 ("right_facing",))
53
f20dd3dcb118 foxes don't run backwards
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 47
diff changeset
134 Animal.__init__(self, image_left, image_right, pos)
171
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
135 self.egg = None
116
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
136 self.egg_counter = 0
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
137
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
138 def move(self, gameboard):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
139 """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
140 pass
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
141
116
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
142 def lay(self):
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
143 """See if the chicken lays an egg"""
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
144 if not self.egg:
171
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
145 self.egg = Egg(self.pos)
216
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
146 self.equip(equipment.NestEgg())
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
147
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
148 def remove_egg(self):
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
149 """Clean up the egg state"""
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
150 self.egg = None
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
151 self.unequip_by_name("nestegg")
116
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
152
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
153 def hatch(self):
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
154 """See if we have an egg to hatch"""
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
155 if self.egg:
187
6854e706dcdf Remove hatched eggs
Neil Muller <drnlmuller@gmail.com>
parents: 174
diff changeset
156 chick = self.egg.hatch()
6854e706dcdf Remove hatched eggs
Neil Muller <drnlmuller@gmail.com>
parents: 174
diff changeset
157 if chick:
216
962934b8c7dc Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents: 201
diff changeset
158 self.remove_egg()
187
6854e706dcdf Remove hatched eggs
Neil Muller <drnlmuller@gmail.com>
parents: 174
diff changeset
159 return chick
116
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
160 return None
d539ef5a3333 Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents: 115
diff changeset
161
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
162 def _find_killable_fox(self, weapon, gameboard):
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
163 """Choose a random fox within range of this weapon."""
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
164 killable_foxes = []
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
165 for fox in gameboard.foxes:
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
166 if not visible(self, fox):
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
167 continue
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
168 if weapon.in_range(gameboard, self, fox):
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
169 killable_foxes.append(fox)
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
170 if not killable_foxes:
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
171 return None
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
172 return random.choice(killable_foxes)
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
173
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
174 def attack(self, gameboard):
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
175 """An armed chicken will attack a fox within range."""
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
176 if not self.weapons():
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
177 # Not going to take on a fox bare-winged.
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
178 return
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
179 # Choose the first weapon equipped.
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
180 weapon = self.weapons()[0]
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
181 fox = self._find_killable_fox(weapon, gameboard)
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
182 if not fox:
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
183 return
236
9a6ac9c9ff46 chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 216
diff changeset
184 self._fix_face(fox.pos)
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
185 if weapon.hit(gameboard, self, fox):
98
725b292ca07b Added sounds killing foxes and chickens, and nightfall
David Fraser <davidf@sjsoft.com>
parents: 92
diff changeset
186 sound.play_sound("kill-fox.ogg")
84
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
187 gameboard.kill_fox(fox)
5494af02a0e8 Chickens with rifles!
Jeremy Thurgood <firxen@gmail.com>
parents: 82
diff changeset
188
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
189 class Egg(Animal):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
190 """An egg"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
191
47
be2496df2368 Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents: 44
diff changeset
192 def __init__(self, pos):
be2496df2368 Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents: 44
diff changeset
193 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
194 Animal.__init__(self, image, image, pos)
171
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
195 self.counter = 2
47
be2496df2368 Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents: 44
diff changeset
196
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
197 # Eggs don't move
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
198
171
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
199 def hatch(self):
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
200 self.counter -= 1
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
201 if self.counter == 0:
9ea53eb919cf Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents: 163
diff changeset
202 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
203 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
204
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
205 class Fox(Animal):
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
206 """A fox"""
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
207
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
208 STEALTH = 20
130
96c5ef7613b5 NINJA FOXES!
Jeremy Thurgood <firxen@gmail.com>
parents: 125
diff changeset
209 IMAGE_FILE = 'sprites/fox.png'
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
210
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
211 costs = {
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
212 # weighting for movement calculation
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
213 'grassland' : 2,
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
214 '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
215 'broken fence' : 2,
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
216 'fence' : 10,
72
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
217 '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
218 '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
219 # 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
220 'hendominium' : 30,
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
221 }
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
222
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
223 def __init__(self, pos):
130
96c5ef7613b5 NINJA FOXES!
Jeremy Thurgood <firxen@gmail.com>
parents: 125
diff changeset
224 image_left = imagecache.load_image(self.IMAGE_FILE)
96c5ef7613b5 NINJA FOXES!
Jeremy Thurgood <firxen@gmail.com>
parents: 125
diff changeset
225 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
226 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
227 self.landmarks = [self.pos]
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
228 self.hunting = True
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
229 self.dig_pos = None
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
230 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
231 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
232 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
233 self.last_steps = []
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
234
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
235 def _cost_tile(self, pos, gameboard):
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
236 if gameboard.in_bounds(pos):
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
237 this_tile = gameboard.tv.get(pos.to_tuple())
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
238 cost = self.costs.get(tiles.TILE_MAP[this_tile], 100)
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
239 else:
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
240 cost = 100 # Out of bounds is expensive
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
241 return cost
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
242
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
243 def _cost_path(self, path, gameboard):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
244 """Calculate the cost of a path"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
245 total = 0
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
246 for pos in path:
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
247 total += self._cost_tile(pos, gameboard)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
248 return total
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
249
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
250 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
251 """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
252 excluding start_pos"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
253 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
254 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
255 # pgu gets this case wrong on occasion.
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
256 return [final_pos]
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
257 start = start_pos.to_tuple()
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
258 end = final_pos.to_tuple()
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
259 points = getline(start, end)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
260 points.remove(start) # exclude start_pos
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
261 if end not in points:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
262 # Rounding errors in getline cause this
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
263 points.append(end)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
264 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
265
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
266 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
267 """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
268 along the path."""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
269 # 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
270 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
271 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
272 min_path = direct_path
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
273 # 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
274 # This is delibrately not finding the optimal path, as I don't
00cf9d7f22dc Expand comment
Neil Muller <drnlmuller@gmail.com>
parents: 70
diff changeset
275 # want the foxes to be too intelligent, although the implementation
00cf9d7f22dc Expand comment
Neil Muller <drnlmuller@gmail.com>
parents: 70
diff changeset
276 # isn't well optimised yet
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
277 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
278 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
279 if (x, y) != (0,0)]
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
280 for start in poss:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
281 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
282 self._gen_path(start, final_pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
283 cost = self._cost_path(cand_path, gameboard)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
284 if cost < min_cost:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
285 min_cost = cost
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
286 min_path = cand_path
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
287 if not min_path:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
288 return final_pos
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
289 return min_path[0]
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
290
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
291 def _find_path_to_woodland(self, gameboard):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
292 """Dive back to woodland through the landmarks"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
293 # 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
294 if self.pos == self.landmarks[-1]:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
295 if len(self.landmarks) > 1:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
296 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
297 else:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
298 # 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
299 self.safe = True
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
300 return self.pos
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
301 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
302
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
303 def _find_path_to_chicken(self, gameboard):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
304 """Find the path to the closest chicken"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
305 # Find the closest chicken
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
306 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
307 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
308 # 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
309 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
310 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
311 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
312 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
313 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
314 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
315 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
316 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
317 if not self.closest:
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
318 # No more chickens, so leave
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
319 self.hunting = False
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
320 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
321 if self.closest.pos == self.pos:
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
322 # 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
323 self._catch_chicken(self.closest, gameboard)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
324 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
325 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
326
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
327 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
328 """Catch a chicken"""
174
ff168162974e armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 171
diff changeset
329 if not chicken.survive_damage():
158
baf857805867 armour works now
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 149
diff changeset
330 sound.play_sound("kill-chicken.ogg")
baf857805867 armour works now
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 149
diff changeset
331 gameboard.remove_chicken(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
332 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
333 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
334 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
335
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
336 def _update_pos(self, gameboard, new_pos):
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
337 """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
338 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
339 # 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
340 return new_pos
2a1064fae608 More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents: 146
diff changeset
341 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
342 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
343 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
344 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
345 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
346 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
347 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
348 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
349 blocked = True
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
350 if fox.pos in moves:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
351 moves.remove(fox.pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
352 if blocked:
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
353 # 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
354 final_pos = None
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
355 min_cost = 1000
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
356 for poss in moves:
82
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
357 cost = self._cost_tile(poss, gameboard)
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
358 if cost < min_cost:
bf28f499c6b4 Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 72
diff changeset
359 min_cost = cost
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
360 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
361 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
362 # 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
363 return self.pos
72
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
364 if gameboard.in_bounds(final_pos):
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
365 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
366 else:
aa4bd93575d9 Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents: 71
diff changeset
367 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
368 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
369 # 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
370 self.landmarks.append(final_pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
371 elif tiles.TILE_MAP[this_tile] == 'fence' and not self.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
372 self._dig(final_pos)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
373 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
374 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
375 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
376 self.last_steps.pop(0)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
377 return final_pos
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
378
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
379 def _dig(self, dig_pos):
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
380 """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
381 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
382 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
383
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
384 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
385 """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
386 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
387 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
388 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
389
28
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
390 def move(self, gameboard):
ac3a74352b74 Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
391 """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
392 chicken"""
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
393 if self.dig_pos:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
394 if self.tick:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
395 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
396 # 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
397 # 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
398 # 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
399 # 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
400 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
401 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
402 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
403 return
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
404 else:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
405 # 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
406 self._make_hole(gameboard)
70
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
407 return
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
408 if self.hunting:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
409 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
410 else:
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
411 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
412 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
413 self._fix_face(final_pos)
d92a2f973cc4 Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents: 53
diff changeset
414 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
415
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
416 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
417 """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
418
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
419 STEALTH = 60
130
96c5ef7613b5 NINJA FOXES!
Jeremy Thurgood <firxen@gmail.com>
parents: 125
diff changeset
420 IMAGE_FILE = 'sprites/ninja_fox.png'
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
421
92
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
422 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
423 """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
424
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
425 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
426 """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
427
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
428 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
429 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
430 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
431
bea1b9364583 Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents: 84
diff changeset
432 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
433 gameboard.remove_chicken(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
434 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
435 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
436 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
437 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
438 self.last_steps = []
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
439
199
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
440 def _get_vision_param(parameter, watcher):
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
441 param = getattr(watcher, parameter)
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
442 if watcher.abode:
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
443 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
444 param = modifier(param)
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
445 return param
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
446
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
447 def visible(watcher, watchee):
199
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
448 vision_bonus = _get_vision_param('VISION_BONUS', watcher)
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
449 range_penalty = _get_vision_param('VISION_RANGE_PENALTY', watcher)
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
450 distance = watcher.pos.dist(watchee.pos) - 1
114
4c2fbab20abe Foxes are stealthy now.
Jeremy Thurgood <firxen@gmail.com>
parents: 109
diff changeset
451 roll = random.randint(1, 100)
199
696936621a93 Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents: 196
diff changeset
452 return roll > watchee.STEALTH - vision_bonus + range_penalty*distance