Mercurial > rinkhals
annotate gamelib/animal.py @ 601:23fbf4651f99
Roosters.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sun, 06 Dec 2009 10:11:37 +0000 |
parents | 50d6c68ce267 |
children |
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 | 3 import random |
4 | |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
5 from pgu.vid import Sprite |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
6 |
44
7e884084e7b1
Move animal sprites to imagecache.
Simon Cross <hodgestar@gmail.com>
parents:
38
diff
changeset
|
7 import imagecache |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
8 import tiles |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
9 from misc import Position |
98
725b292ca07b
Added sounds killing foxes and chickens, and nightfall
David Fraser <davidf@sjsoft.com>
parents:
92
diff
changeset
|
10 import sound |
109
48019afde338
Equipment purchasing and some toolbar tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
108
diff
changeset
|
11 import equipment |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
236
diff
changeset
|
12 import animations |
415
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
13 import serializer |
438
16437cf4a2b8
Trees provide a modicum of cover.
Jeremy Thurgood <firxen@gmail.com>
parents:
437
diff
changeset
|
14 import constants |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
15 |
507 | 16 |
17 NEIGHBOUR_4 = [Position(-1, 0), Position(1, 0), Position(0, 1), Position(0, -1)] | |
18 | |
19 | |
20 NEIGHBOUR_8 = [Position(-1, 0), Position(1, 0), Position(0, 1), Position(0, -1), | |
21 Position(1, 1), Position(1, -1), Position(-1, 1), Position(-1, -1)] | |
22 | |
23 | |
24 TILE_FENCE = tiles.REVERSE_TILE_MAP['fence'] | |
25 | |
415
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
26 class Animal(Sprite, serializer.Simplifiable): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
27 """Base class for animals""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
28 |
114 | 29 STEALTH = 0 |
199
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
30 VISION_BONUS = 0 |
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
31 VISION_RANGE_PENALTY = 10 |
114 | 32 |
420
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
33 # sub-class must set this to the name of an image |
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
34 # file |
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
35 IMAGE_FILE = None |
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
36 |
415
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
37 SIMPLIFY = [ |
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
38 'pos', |
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
39 'equipment', |
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
40 'accoutrements', |
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
41 'abode', |
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
42 'facing', |
477
c1439f6705a2
Start of fix for save/restore after animals gained gameboards.
Simon Cross <hodgestar@gmail.com>
parents:
476
diff
changeset
|
43 'gameboard', |
415
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
44 ] |
8f012ef1f64f
Start of ability to serialize game state.
Simon Cross <hodgestar@gmail.com>
parents:
414
diff
changeset
|
45 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
46 def __init__(self, tile_pos, gameboard): |
420
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
47 # load images |
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
48 self._image_left = imagecache.load_image(self.IMAGE_FILE) |
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
49 self._image_right = imagecache.load_image(self.IMAGE_FILE, ("right_facing",)) |
38
03121c89d5fd
Make the secret foxes really secret
Neil Muller <drnlmuller@gmail.com>
parents:
32
diff
changeset
|
50 # Create the animal somewhere far off screen |
420
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
51 Sprite.__init__(self, self._image_left, (-1000, -1000)) |
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
52 self.image_left = self._image_left.copy() |
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
53 self.image_right = self._image_right.copy() |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
394
diff
changeset
|
54 if hasattr(tile_pos, 'to_tile_tuple'): |
171
9ea53eb919cf
Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents:
163
diff
changeset
|
55 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
|
56 else: |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
394
diff
changeset
|
57 self.pos = Position(tile_pos[0], tile_pos[1], 0) |
84 | 58 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
|
59 self.accoutrements = [] |
108
437cbd856a03
Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents:
104
diff
changeset
|
60 self.abode = None |
146 | 61 self.facing = 'left' |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
62 self.gameboard = gameboard |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
63 |
505
3ed6c011106d
Switch to classmethod decorators.
Simon Cross <hodgestar@gmail.com>
parents:
477
diff
changeset
|
64 @classmethod |
423
97dd557504a2
Override make methods for simplifiable objects.
Simon Cross <hodgestar@gmail.com>
parents:
422
diff
changeset
|
65 def make(cls): |
97dd557504a2
Override make methods for simplifiable objects.
Simon Cross <hodgestar@gmail.com>
parents:
422
diff
changeset
|
66 """Override default Simplifiable object creation.""" |
477
c1439f6705a2
Start of fix for save/restore after animals gained gameboards.
Simon Cross <hodgestar@gmail.com>
parents:
476
diff
changeset
|
67 return cls((0, 0), None) |
423
97dd557504a2
Override make methods for simplifiable objects.
Simon Cross <hodgestar@gmail.com>
parents:
422
diff
changeset
|
68 |
505
3ed6c011106d
Switch to classmethod decorators.
Simon Cross <hodgestar@gmail.com>
parents:
477
diff
changeset
|
69 @classmethod |
439
cf4b020e6385
Start of serializer for buildings and support for reference cycles.
Simon Cross <hodgestar@gmail.com>
parents:
438
diff
changeset
|
70 def unsimplify(cls, *args, **kwargs): |
427
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
426
diff
changeset
|
71 """Override default Simplifiable unsimplification.""" |
439
cf4b020e6385
Start of serializer for buildings and support for reference cycles.
Simon Cross <hodgestar@gmail.com>
parents:
438
diff
changeset
|
72 obj = super(Animal, cls).unsimplify(*args, **kwargs) |
427
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
426
diff
changeset
|
73 obj.redraw() |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
426
diff
changeset
|
74 return obj |
e89a1afe4e84
Make Position simplifiable. Override unsimplify for animals to update their image after recreating.
Simon Cross <hodgestar@gmail.com>
parents:
426
diff
changeset
|
75 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
76 def loop(self, tv, _sprite): |
395
2d0ff46118e2
Basic support for z coordinate
Neil Muller <drnlmuller@gmail.com>
parents:
394
diff
changeset
|
77 ppos = tv.tile_to_view(self.pos.to_tile_tuple()) |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
78 self.rect.x = ppos[0] |
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
79 self.rect.y = ppos[1] |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
80 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
81 def die(self): |
251
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
82 """Play death animation, noises, whatever.""" |
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
83 if hasattr(self, 'DEATH_SOUND'): |
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
84 sound.play_sound(self.DEATH_SOUND) |
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
85 if hasattr(self, 'DEATH_ANIMATION'): |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
86 self.DEATH_ANIMATION(self.gameboard.tv, self.pos.to_tile_tuple()) |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
87 self._game_death() |
251
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
88 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
89 def _game_death(self): |
251
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
90 # Call appropriate gameboard cleanup here. |
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
91 pass |
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
92 |
507 | 93 def move(self): |
94 """Return a new position for the object""" | |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
95 # Default is not to move |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
96 pass |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
97 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
98 def attack(self): |
396
19e583e5cdc0
Refactor for further move work
Neil Muller <drnlmuller@gmail.com>
parents:
395
diff
changeset
|
99 """Given the game state, attack a suitable target""" |
19e583e5cdc0
Refactor for further move work
Neil Muller <drnlmuller@gmail.com>
parents:
395
diff
changeset
|
100 # Default is not to attack |
19e583e5cdc0
Refactor for further move work
Neil Muller <drnlmuller@gmail.com>
parents:
395
diff
changeset
|
101 pass |
19e583e5cdc0
Refactor for further move work
Neil Muller <drnlmuller@gmail.com>
parents:
395
diff
changeset
|
102 |
108
437cbd856a03
Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents:
104
diff
changeset
|
103 def set_pos(self, tile_pos): |
437cbd856a03
Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents:
104
diff
changeset
|
104 """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
|
105 new_pos = Position(*tile_pos) |
437cbd856a03
Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents:
104
diff
changeset
|
106 self._fix_face(new_pos) |
437cbd856a03
Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents:
104
diff
changeset
|
107 self.pos = new_pos |
437cbd856a03
Add occupants and abodes. Allowing moving chickens around.
Simon Cross <hodgestar@gmail.com>
parents:
104
diff
changeset
|
108 |
236
9a6ac9c9ff46
chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
216
diff
changeset
|
109 def _fix_face(self, facing_pos): |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
110 """Set the face correctly""" |
236
9a6ac9c9ff46
chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
216
diff
changeset
|
111 if facing_pos.left_of(self.pos): |
146 | 112 self._set_image_facing('left') |
236
9a6ac9c9ff46
chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
216
diff
changeset
|
113 elif facing_pos.right_of(self.pos): |
146 | 114 self._set_image_facing('right') |
115 | |
116 def _set_image_facing(self, facing): | |
117 self.facing = facing | |
118 if self.facing == 'left': | |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
119 self.setimage(self.image_left) |
146 | 120 elif self.facing == 'right': |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
121 self.setimage(self.image_right) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
122 |
84 | 123 def equip(self, item): |
200
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
124 if equipment.is_equipment(item): |
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
125 self.equipment.append(item) |
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
126 elif equipment.is_accoutrement(item): |
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
127 self.accoutrements.append(item) |
163
0d6e23dcd3af
fixed redrawing
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
161
diff
changeset
|
128 self.redraw() |
161
9b4213f6ea7f
improved equipment layers; unequip method on animal
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
158
diff
changeset
|
129 |
9b4213f6ea7f
improved equipment layers; unequip method on animal
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
158
diff
changeset
|
130 def unequip(self, item): |
200
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
131 if equipment.is_equipment(item): |
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
132 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
|
133 elif equipment.is_accoutrement(item): |
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
134 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
|
135 self.redraw() |
161
9b4213f6ea7f
improved equipment layers; unequip method on animal
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
158
diff
changeset
|
136 |
200
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
137 def unequip_by_name(self, item_name): |
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
138 # only remove first match |
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
139 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
|
140 if matches: |
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
141 self.unequip(matches[0]) |
67d10f7e0159
selected chickens are selected
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
199
diff
changeset
|
142 |
163
0d6e23dcd3af
fixed redrawing
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
161
diff
changeset
|
143 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
|
144 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
|
145 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
|
146 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
|
147 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
|
148 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
|
149 layers.append(images) |
158
baf857805867
armour works now
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
149
diff
changeset
|
150 |
195
9d31cfc3afde
refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
187
diff
changeset
|
151 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
|
152 |
201
fe1e9c18d4d7
layering bugfix; indoor chickens now use normal chicken icons
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
200
diff
changeset
|
153 # 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
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 |
9d31cfc3afde
refactor of layers drawn on chicken, to allow for extended functionality
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
187
diff
changeset
|
160 self._set_image_facing(self.facing) |
84 | 161 |
162 def weapons(self): | |
109
48019afde338
Equipment purchasing and some toolbar tweaks.
Jeremy Thurgood <firxen@gmail.com>
parents:
108
diff
changeset
|
163 return [e for e in self.equipment if equipment.is_weapon(e)] |
84 | 164 |
174
ff168162974e
armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
171
diff
changeset
|
165 def armour(self): |
ff168162974e
armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
171
diff
changeset
|
166 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
|
167 |
104
d17375586866
Add .covers(tile_pos) to animals (to match similar function on buildings).
Simon Cross <hodgestar@gmail.com>
parents:
98
diff
changeset
|
168 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
|
169 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
|
170 |
125 | 171 def outside(self): |
172 return self.abode is None | |
173 | |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
174 def damage(self): |
174
ff168162974e
armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
171
diff
changeset
|
175 for a in self.armour(): |
ff168162974e
armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
171
diff
changeset
|
176 if not a.survive_damage(): |
ff168162974e
armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
171
diff
changeset
|
177 self.unequip(a) |
ff168162974e
armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
171
diff
changeset
|
178 return True |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
179 self.die() |
174
ff168162974e
armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
171
diff
changeset
|
180 return False |
ff168162974e
armour gets damaged
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
171
diff
changeset
|
181 |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
182 class Chicken(Animal): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
183 """A chicken""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
184 |
146 | 185 EQUIPMENT_IMAGE_ATTRIBUTE = 'CHICKEN_IMAGE_FILE' |
251
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
186 DEATH_ANIMATION = animations.ChickenDeath |
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
187 DEATH_SOUND = 'kill-chicken.ogg' |
420
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
188 IMAGE_FILE = 'sprites/chkn.png' |
146 | 189 |
426
d34be925b9fc
Fix SIMPLIFY for chickens and eggs. Clean-up greedy fox cost overriding.
Simon Cross <hodgestar@gmail.com>
parents:
423
diff
changeset
|
190 SIMPLIFY = Animal.SIMPLIFY + ['eggs'] |
d34be925b9fc
Fix SIMPLIFY for chickens and eggs. Clean-up greedy fox cost overriding.
Simon Cross <hodgestar@gmail.com>
parents:
423
diff
changeset
|
191 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
192 def __init__(self, pos, gameboard): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
193 Animal.__init__(self, pos, gameboard) |
243
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
194 self.eggs = [] |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
195 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
196 def start_night(self): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
197 self.lay() |
414
9096c237928c
Dear most illustrious brother, I seek your cooperation with the refactoring of egg layerings and the reloading of guns. Please to provide bank details.
Jeremy Thurgood <firxen@gmail.com>
parents:
413
diff
changeset
|
198 self.reload_weapon() |
9096c237928c
Dear most illustrious brother, I seek your cooperation with the refactoring of egg layerings and the reloading of guns. Please to provide bank details.
Jeremy Thurgood <firxen@gmail.com>
parents:
413
diff
changeset
|
199 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
200 def start_day(self): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
201 self.hatch() |
419
d110d55c8449
Move hatching logic into chickens.
Jeremy Thurgood <firxen@gmail.com>
parents:
415
diff
changeset
|
202 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
203 def _game_death(self): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
204 self.gameboard.remove_chicken(self) |
251
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
205 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
206 def move(self): |
422
ab4fc3fe0f96
chickens scatter; chop wood
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
420
diff
changeset
|
207 """A free chicken will wander around aimlessly""" |
ab4fc3fe0f96
chickens scatter; chop wood
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
420
diff
changeset
|
208 pos_x, pos_y = self.pos.to_tile_tuple() |
ab4fc3fe0f96
chickens scatter; chop wood
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
420
diff
changeset
|
209 surrounds = [Position(pos_x + dx, pos_y + dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]] |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
210 pos_options = [pos for pos in surrounds if self.gameboard.in_bounds(pos) and self.gameboard.tv.get(pos.to_tile_tuple()) == self.gameboard.GRASSLAND and not self.gameboard.get_outside_chicken(pos.to_tile_tuple())] + [self.pos] |
422
ab4fc3fe0f96
chickens scatter; chop wood
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
420
diff
changeset
|
211 self.pos = pos_options[random.randint(0, len(pos_options)-1)] |
ab4fc3fe0f96
chickens scatter; chop wood
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
420
diff
changeset
|
212 |
441
7b5e4b6dd889
fixed unarmed logging bug
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
439
diff
changeset
|
213 def has_axe(self): |
445 | 214 return bool([e for e in self.weapons() if e.TYPE == "AXE"]) |
441
7b5e4b6dd889
fixed unarmed logging bug
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
439
diff
changeset
|
215 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
216 def chop(self): |
441
7b5e4b6dd889
fixed unarmed logging bug
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
439
diff
changeset
|
217 if self.has_axe(): |
7b5e4b6dd889
fixed unarmed logging bug
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
439
diff
changeset
|
218 pos_x, pos_y = self.pos.to_tile_tuple() |
7b5e4b6dd889
fixed unarmed logging bug
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
439
diff
changeset
|
219 surrounds = [Position(pos_x + dx, pos_y + dy) for dx in [-1, 0, 1] for dy in [-1, 0, 1]] |
552
11c4cebfe4c5
Preparatory work for woodland biodiversity.
Jeremy Thurgood <firxen@gmail.com>
parents:
533
diff
changeset
|
220 tree_options = [pos for pos in surrounds if self.gameboard.in_bounds(pos) and self.gameboard.is_woodland_tile(pos)] |
441
7b5e4b6dd889
fixed unarmed logging bug
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
439
diff
changeset
|
221 if tree_options: |
7b5e4b6dd889
fixed unarmed logging bug
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
439
diff
changeset
|
222 num_trees_to_cut = random.randint(1, len(tree_options)) |
7b5e4b6dd889
fixed unarmed logging bug
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
439
diff
changeset
|
223 trees_to_cut = random.sample(tree_options, num_trees_to_cut) |
7b5e4b6dd889
fixed unarmed logging bug
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
439
diff
changeset
|
224 for tree_pos in trees_to_cut: |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
225 self.gameboard.add_wood(5) |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
226 self.gameboard.tv.set(tree_pos.to_tile_tuple(), self.gameboard.GRASSLAND) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
227 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
228 def lay(self): |
116
d539ef5a3333
Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents:
115
diff
changeset
|
229 """See if the chicken lays an egg""" |
414
9096c237928c
Dear most illustrious brother, I seek your cooperation with the refactoring of egg layerings and the reloading of guns. Please to provide bank details.
Jeremy Thurgood <firxen@gmail.com>
parents:
413
diff
changeset
|
230 if self.abode and self.abode.building.HENHOUSE: |
601 | 231 # TODO: Find a cleaner way to do this |
232 fertilised = False | |
233 for bird in self.abode.building.occupants(): | |
234 if getattr(bird, 'ROOSTER', None): | |
235 fertilised = True | |
414
9096c237928c
Dear most illustrious brother, I seek your cooperation with the refactoring of egg layerings and the reloading of guns. Please to provide bank details.
Jeremy Thurgood <firxen@gmail.com>
parents:
413
diff
changeset
|
236 if not self.eggs: |
9096c237928c
Dear most illustrious brother, I seek your cooperation with the refactoring of egg layerings and the reloading of guns. Please to provide bank details.
Jeremy Thurgood <firxen@gmail.com>
parents:
413
diff
changeset
|
237 for x in range(random.randint(1, 4)): |
601 | 238 self.eggs.append(Egg(self.pos, self.gameboard, fertilised=fertilised)) |
414
9096c237928c
Dear most illustrious brother, I seek your cooperation with the refactoring of egg layerings and the reloading of guns. Please to provide bank details.
Jeremy Thurgood <firxen@gmail.com>
parents:
413
diff
changeset
|
239 self.equip(equipment.NestEgg()) |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
240 self.gameboard.eggs += self.get_num_eggs() |
216
962934b8c7dc
Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents:
201
diff
changeset
|
241 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
242 def remove_eggs(self): |
216
962934b8c7dc
Implement UI part of egg selling
Neil Muller <drnlmuller@gmail.com>
parents:
201
diff
changeset
|
243 """Clean up the egg state""" |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
244 self.gameboard.remove_eggs(len(self.eggs)) |
243
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
245 self.eggs = [] |
305
32149b1d9fd2
Capitalised all equipment names.
Jeremy Thurgood <firxen@gmail.com>
parents:
293
diff
changeset
|
246 self.unequip_by_name("Nestegg") |
116
d539ef5a3333
Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents:
115
diff
changeset
|
247 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
248 def remove_one_egg(self): |
243
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
249 """Clean up the egg state""" |
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
250 self.eggs.pop() |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
251 self.gameboard.remove_eggs(1) |
243
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
252 if not self.eggs: |
305
32149b1d9fd2
Capitalised all equipment names.
Jeremy Thurgood <firxen@gmail.com>
parents:
293
diff
changeset
|
253 self.unequip_by_name("Nestegg") |
243
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
254 |
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
255 def get_num_eggs(self): |
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
256 return len(self.eggs) |
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
257 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
258 def hatch(self): |
116
d539ef5a3333
Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents:
115
diff
changeset
|
259 """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
|
260 if self.eggs: |
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
261 chick = self.eggs[0].hatch() |
187 | 262 if chick: |
243
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
263 # sell the remaining eggs |
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
264 # Remove hatched egg |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
265 self.remove_one_egg() |
243
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
266 # Sell other eggs |
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
267 for egg in self.eggs[:]: |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
268 self.gameboard.sell_one_egg(self) |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
269 self.remove_eggs() # clean up stale images, etc. |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
270 self.gameboard.place_hatched_chicken(chick, self.abode.building) |
116
d539ef5a3333
Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents:
115
diff
changeset
|
271 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
272 def _find_killable_fox(self, weapon): |
84 | 273 """Choose a random fox within range of this weapon.""" |
274 killable_foxes = [] | |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
275 for fox in self.gameboard.foxes: |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
276 if not weapon.in_range(self.gameboard, self, fox): |
114 | 277 continue |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
278 if visible(self, fox, self.gameboard): |
84 | 279 killable_foxes.append(fox) |
280 if not killable_foxes: | |
281 return None | |
282 return random.choice(killable_foxes) | |
283 | |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
284 def attack(self): |
84 | 285 """An armed chicken will attack a fox within range.""" |
286 if not self.weapons(): | |
287 # Not going to take on a fox bare-winged. | |
288 return | |
289 # Choose the first weapon equipped. | |
290 weapon = self.weapons()[0] | |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
291 fox = self._find_killable_fox(weapon) |
84 | 292 if not fox: |
293 return | |
236
9a6ac9c9ff46
chickens turn to face target foxes
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
216
diff
changeset
|
294 self._fix_face(fox.pos) |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
295 if weapon.hit(self.gameboard, self, fox): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
296 fox.damage() |
84 | 297 |
413
bdc4757e0497
Add Sniper Rifle and give guns limited ammunition.
Jeremy Thurgood <firxen@gmail.com>
parents:
407
diff
changeset
|
298 def reload_weapon(self): |
bdc4757e0497
Add Sniper Rifle and give guns limited ammunition.
Jeremy Thurgood <firxen@gmail.com>
parents:
407
diff
changeset
|
299 """If we have a weapon that takes ammunition, reload it.""" |
bdc4757e0497
Add Sniper Rifle and give guns limited ammunition.
Jeremy Thurgood <firxen@gmail.com>
parents:
407
diff
changeset
|
300 for weapon in self.weapons(): |
429
42777630956a
Add ammo to things serialized and deserialized.
Simon Cross <hodgestar@gmail.com>
parents:
428
diff
changeset
|
301 weapon.refresh_ammo() |
413
bdc4757e0497
Add Sniper Rifle and give guns limited ammunition.
Jeremy Thurgood <firxen@gmail.com>
parents:
407
diff
changeset
|
302 |
601 | 303 |
304 class Rooster(Chicken): | |
305 """A rooster""" | |
306 | |
307 IMAGE_FILE = 'sprites/rooster.png' | |
308 ROOSTER = True | |
309 | |
310 AGGRESSION = 50 | |
311 | |
312 def lay(self): | |
313 # Roosters don't lay eggs. | |
314 pass | |
315 | |
316 def start_night(self): | |
317 Chicken.start_night(self) | |
318 self._manly_fight() | |
319 | |
320 def _manly_fight(self): | |
321 if self.abode: | |
322 for rival in [occ for occ in self.abode.building.occupants() | |
323 if getattr(occ, 'ROOSTER', False)]: | |
324 if random.randint(1, 100) <= self.AGGRESSION: | |
325 rival.damage() | |
326 | |
327 | |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
328 class Egg(Animal): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
329 """An egg""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
330 |
420
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
331 IMAGE_FILE = 'sprites/equip_egg.png' |
f20ccd43a282
Rework Animal.__init__ to make it easier to unsimplify them.
Simon Cross <hodgestar@gmail.com>
parents:
419
diff
changeset
|
332 |
601 | 333 SIMPLIFY = Animal.SIMPLIFY + ['timer', 'fertilised'] |
426
d34be925b9fc
Fix SIMPLIFY for chickens and eggs. Clean-up greedy fox cost overriding.
Simon Cross <hodgestar@gmail.com>
parents:
423
diff
changeset
|
334 |
601 | 335 def __init__(self, pos, gameboard, fertilised=False): |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
336 Animal.__init__(self, pos, gameboard) |
601 | 337 self.fertilised = fertilised |
243
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
338 self.timer = 2 |
47
be2496df2368
Add egg image to egg sprite.
Simon Cross <hodgestar@gmail.com>
parents:
44
diff
changeset
|
339 |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
340 # Eggs don't move |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
341 |
171
9ea53eb919cf
Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents:
163
diff
changeset
|
342 def hatch(self): |
243
4f86c2616cdf
Variable number of eggs for chickens
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
343 self.timer -= 1 |
601 | 344 if self.timer == 0 and self.fertilised: |
345 return random.choice([Chicken, Rooster])(self.pos, self.gameboard) | |
171
9ea53eb919cf
Make Animals accept Positions when created. move hatch watching from Chicken to Egg
Neil Muller <drnlmuller@gmail.com>
parents:
163
diff
changeset
|
346 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
|
347 |
601 | 348 |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
349 class Fox(Animal): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
350 """A fox""" |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
diff
changeset
|
351 |
114 | 352 STEALTH = 20 |
130 | 353 IMAGE_FILE = 'sprites/fox.png' |
251
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
354 DEATH_ANIMATION = animations.FoxDeath |
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
355 DEATH_SOUND = 'kill-fox.ogg' |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
356 CONFIG_NAME = 'fox' |
114 | 357 |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
358 costs = { |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
359 # weighting for movement calculation |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
360 'grassland' : 2, |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
361 'woodland' : 1, # Try to keep to the woods if possible |
507 | 362 'broken fence' : 1, |
404
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
363 'fence' : 25, |
72
aa4bd93575d9
Fix some bound checking and tweak movement costs
Neil Muller <drnlmuller@gmail.com>
parents:
71
diff
changeset
|
364 '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
|
365 '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
|
366 # 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
|
367 'hendominium' : 30, |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
368 } |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
369 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
370 def __init__(self, pos, gameboard): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
371 Animal.__init__(self, pos, gameboard) |
507 | 372 self.start_pos = self.pos |
373 self.target = None | |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
374 self.hunting = True |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
375 self.dig_pos = None |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
376 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
|
377 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
|
378 self.closest = None |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
379 # Foxes don't occupy places in the same way chickens do, but they |
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
380 # can still be inside |
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
381 self.building = None |
507 | 382 self._last_steps = [] |
383 self.path = [] | |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
384 |
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
385 def outside(self): |
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
386 return self.building is None |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
387 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
388 def _game_death(self): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
389 self.gameboard.kill_fox(self) |
251
844bfb23d4b6
Refactored animal death and added death animations.
Jeremy Thurgood <firxen@gmail.com>
parents:
243
diff
changeset
|
390 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
391 def _cost_tile(self, pos): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
392 if self.gameboard.in_bounds(pos): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
393 this_tile = self.gameboard.tv.get(pos.to_tile_tuple()) |
82
bf28f499c6b4
Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
394 cost = self.costs.get(tiles.TILE_MAP[this_tile], 100) |
bf28f499c6b4
Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
395 else: |
bf28f499c6b4
Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
396 cost = 100 # Out of bounds is expensive |
bf28f499c6b4
Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
397 return cost |
bf28f499c6b4
Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
398 |
507 | 399 def _is_fence(self, pos): |
400 if self.gameboard.in_bounds(pos): | |
401 this_tile = self.gameboard.tv.get(pos.to_tile_tuple()) | |
402 return this_tile == TILE_FENCE | |
403 return False | |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
404 |
507 | 405 def _check_steps(self, step, border_func, end_func, max_steps): |
406 steps = 0 | |
407 cur_pos = self.pos | |
408 path = [] | |
409 while steps < max_steps: | |
410 if not border_func(cur_pos): | |
411 # Not walking the edge | |
412 return None | |
413 path.append(cur_pos) | |
414 if end_func(cur_pos): | |
415 # is there an 8-NEIGHBOUR that also satisfies end_func and is | |
416 # closer to target | |
417 dist = self.target.dist(cur_pos) | |
418 fin_pos = None | |
419 for pos in [cur_pos + x for x in NEIGHBOUR_8]: | |
511
57f9077fb7fb
Remove some left-over code.
Neil Muller <drnlmuller@gmail.com>
parents:
508
diff
changeset
|
420 if pos in path: |
57f9077fb7fb
Remove some left-over code.
Neil Muller <drnlmuller@gmail.com>
parents:
508
diff
changeset
|
421 continue |
507 | 422 if end_func(pos) and self.target.dist(pos) < dist: |
423 fin_pos = pos | |
424 dist = self.target.dist(pos) | |
425 if fin_pos: | |
426 path.append(fin_pos) | |
427 return path | |
428 steps += 1 | |
429 cur_pos = cur_pos + step | |
430 return None | |
431 | |
432 def _search_for_path(self, border_func, end_func, max_steps): | |
433 paths = [None] * 4 | |
434 paths[0] = self._check_steps(Position(-1, 0), border_func, end_func, max_steps) | |
435 paths[1] = self._check_steps(Position(0, -1), border_func, end_func, max_steps) | |
436 paths[2] = self._check_steps(Position(1, 0), border_func, end_func, max_steps) | |
437 paths[3] = self._check_steps(Position(0, 1), border_func, end_func, max_steps) | |
438 cands = [x for x in paths if x is not None] | |
439 if not cands: | |
440 return None | |
441 elif len(cands) == 1: | |
442 return cands[0][1:] | |
443 # take the end point closest to our target | |
444 final_path = cands[0] | |
445 min_dist = final_path[-1].dist(self.target) | |
446 for this_path in cands[1:]: | |
447 dist = this_path[-1].dist(self.target) | |
448 if dist < min_dist: | |
449 min_dist = dist | |
450 final_path = this_path | |
451 elif dist == min_dist and random.randint(0, 1) == 0: | |
452 final_path = this_path | |
453 return final_path[1:] # path's include self.pos | |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
454 |
507 | 455 def _find_nearest_corner(self): |
456 """Find the nearest corner of the bulding""" | |
457 COST_MARGIN = 25 | |
458 def border(pos): | |
459 cost = self._cost_tile(pos) | |
460 if cost >= COST_MARGIN: | |
461 return False # in building isn't border | |
462 for tpos in [pos + step for step in NEIGHBOUR_8]: | |
463 if self.gameboard.in_bounds(tpos): | |
464 cost = self._cost_tile(tpos) | |
465 if cost >= COST_MARGIN: | |
466 return True | |
467 return False | |
468 | |
469 def corner(pos): | |
470 # A corner is not 4-connected to a building | |
471 if not border(pos): | |
472 return False | |
473 for tpos in [pos + step for step in NEIGHBOUR_4]: | |
474 if self.gameboard.in_bounds(tpos): | |
475 cost = self._cost_tile(tpos) | |
476 if cost >= COST_MARGIN: | |
477 return False | |
478 return True | |
479 | |
480 return self._search_for_path(border, corner, 6) | |
481 | |
482 def _find_fence_gap(self): | |
483 # We search for a gap in the fence | |
484 # we know we are next to fence. A gap in the fence is | |
485 # a point that borders the fence where the fence is not | |
486 # 4-connected | |
487 | |
488 COST_MARGIN = 25 | |
489 | |
490 def border(pos): | |
491 if self._is_fence(pos) or self._cost_tile(pos) >= COST_MARGIN: | |
492 return False | |
493 for tpos in [pos + step for step in NEIGHBOUR_8]: | |
494 if self._is_fence(tpos) or self._cost_tile(tpos) >= COST_MARGIN: | |
495 return True | |
496 return False | |
497 | |
498 def is_gap(pos): | |
499 # A gap neighbours only fence tiles which has < 2 4-neighbours | |
500 if self._is_fence(pos): | |
501 return False | |
502 fence_neighbours = [0] | |
503 for tpos in [pos + step for step in NEIGHBOUR_8]: | |
504 if self._is_fence(tpos): | |
505 connections = 0 | |
506 for fpos in [tpos + step for step in NEIGHBOUR_4]: | |
507 if self.gameboard.in_bounds(fpos): | |
508 if self._is_fence(fpos) or self._cost_tile(tpos) >= COST_MARGIN: | |
509 # Expensive building is considered fence | |
510 connections += 1 | |
511 else: | |
512 # Fence connecting to out of bounds counts as fence | |
513 connections += 1 | |
514 fence_neighbours.append(connections) | |
515 return max(fence_neighbours) < 2 | |
516 | |
517 return self._search_for_path(border, is_gap, 7) | |
518 | |
519 def _find_min_cost_neighbour(self, target): | |
520 """Find the minimum cost neighbour that's closer to target""" | |
521 cur_dist = target.dist(self.pos) | |
522 neighbours = [self.pos + step for step in NEIGHBOUR_8] | |
404
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
523 min_cost = 1000 |
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
524 min_dist = cur_dist |
507 | 525 best = self.pos |
404
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
526 for point in neighbours: |
507 | 527 if point in self._last_steps: |
528 continue | |
529 dist = point.dist(target) | |
530 if dist <= min_dist: | |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
531 cost = self._cost_tile(point) |
404
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
532 if cost < min_cost or (min_cost == cost and dist < min_dist): |
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
533 # Prefer closest of equal cost points |
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
534 min_dist = dist |
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
535 min_cost = cost |
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
536 best = point |
507 | 537 elif min_cost == cost and random.randint(0, 1) == 0: |
538 # Be slightly non-deterministic when presented with | |
539 # equal choices | |
540 best = point | |
541 return best, min_cost | |
542 | |
543 def _find_best_path_step(self): | |
544 """Find the cheapest path to final_pos, and return the next step | |
545 along the path.""" | |
546 if self.path: | |
547 next_step = self.path.pop(0) | |
548 if next_step.dist(self.pos) < 2: | |
549 return next_step | |
550 else: | |
551 # Been bounced off the path | |
552 self.path = [] | |
533
7addf41b6abb
Allow backtracking on ladders
Neil Muller <drnlmuller@gmail.com>
parents:
531
diff
changeset
|
553 new_pos = None |
507 | 554 if self.target.z < self.pos.z: |
555 # We need to try heading down. | |
533
7addf41b6abb
Allow backtracking on ladders
Neil Muller <drnlmuller@gmail.com>
parents:
531
diff
changeset
|
556 new_pos = Position(self.pos.x, self.pos.y, self.pos.z - 1) |
507 | 557 if self.target.x == self.pos.x and self.target.y == self.pos.y and \ |
558 self.target.z > self.pos.z: | |
559 # We try heading up | |
533
7addf41b6abb
Allow backtracking on ladders
Neil Muller <drnlmuller@gmail.com>
parents:
531
diff
changeset
|
560 new_pos = Position(self.pos.x, self.pos.y, self.pos.z + 1) |
7addf41b6abb
Allow backtracking on ladders
Neil Muller <drnlmuller@gmail.com>
parents:
531
diff
changeset
|
561 if new_pos: |
7addf41b6abb
Allow backtracking on ladders
Neil Muller <drnlmuller@gmail.com>
parents:
531
diff
changeset
|
562 if new_pos in self._last_steps: |
7addf41b6abb
Allow backtracking on ladders
Neil Muller <drnlmuller@gmail.com>
parents:
531
diff
changeset
|
563 # ladder, so we allow backtracking |
7addf41b6abb
Allow backtracking on ladders
Neil Muller <drnlmuller@gmail.com>
parents:
531
diff
changeset
|
564 self._last_steps.remove(new_pos) |
7addf41b6abb
Allow backtracking on ladders
Neil Muller <drnlmuller@gmail.com>
parents:
531
diff
changeset
|
565 return new_pos |
507 | 566 cur_dist = self.target.dist(self.pos) |
567 if cur_dist < 2: | |
568 # We're right ontop of our target, so just go there | |
569 return self.target | |
570 # Find the cheapest spot close to us that moves us closer to the target | |
531
452cde9af2a2
Fix order of checks to avoid unneeded work
Neil Muller <drnlmuller@gmail.com>
parents:
518
diff
changeset
|
571 best, min_cost = self._find_min_cost_neighbour(self.target) |
507 | 572 if min_cost < 20 or not self.gameboard.in_bounds(self.pos) \ |
573 or not self.gameboard.in_bounds(best): | |
407
bdc8bc78a796
Handle corner case aroudn entering the map
Neil Muller <drnlmuller@gmail.com>
parents:
406
diff
changeset
|
574 # If we're not on the gameboard yet, there's no point in looking |
bdc8bc78a796
Handle corner case aroudn entering the map
Neil Muller <drnlmuller@gmail.com>
parents:
406
diff
changeset
|
575 # for an optimal path. |
404
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
576 return best |
e5247ec76f24
The new faster deciding (but somewhat dumber) fox
Neil Muller <drnlmuller@gmail.com>
parents:
402
diff
changeset
|
577 # Else expensive step, so think further |
507 | 578 if self._is_fence(best): |
579 path = self._find_fence_gap() | |
580 elif min_cost == 30: | |
581 # building | |
582 path = self._find_nearest_corner() | |
583 else: | |
584 # We're looping | |
585 self._last_steps = [] | |
586 return self.pos | |
587 if path: | |
588 self.path = path[1:] # exclude 1st step | |
589 return path[0] | |
590 return best | |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
591 |
507 | 592 def _calc_next_move(self): |
593 """Find the path to the target""" | |
594 if self.hunting: | |
595 # Check if we need to update our idea of a target | |
596 if not self.closest or self.closest not in self.gameboard.chickens: | |
597 # Either no target, or someone ate it | |
598 self._select_prey() | |
599 elif not self.target: | |
600 self.target = self.closest.pos | |
601 if not self.target: | |
602 self.target = self.start_pos | |
603 self._last_steps = [] | |
604 if self.target == self.pos: | |
605 # No need to move, but we will need to update the target | |
606 self.target = None | |
402
3a469d46b820
Tweak safe logic to avoid some corner cases
Neil Muller <drnlmuller@gmail.com>
parents:
401
diff
changeset
|
607 return self.pos |
507 | 608 if self.target.to_tile_tuple() == self.pos.to_tile_tuple(): |
609 # Only differ in z, so next step is in z | |
610 if self.target.z < self.pos.z: | |
611 new_z = self.pos.z - 1 | |
612 else: | |
613 new_z = self.pos.z + 1 | |
614 return Position(self.pos.x, self.pos.y, new_z) | |
615 return self._find_best_path_step() | |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
616 |
518
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
617 def _calculate_dist(self, chicken): |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
618 """Calculate the distance to the chicken""" |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
619 dist = chicken.pos.dist(self.pos) |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
620 if chicken.abode: |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
621 dist += 5 # Prefer free-ranging chickens |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
622 if len(chicken.weapons()) > 0: |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
623 dist += 5 # Prefer unarmed chickens |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
624 return dist |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
625 |
507 | 626 def _select_prey(self): |
475
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
627 min_dist = 999 |
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
628 self.closest = None |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
629 for chicken in self.gameboard.chickens: |
518
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
630 dist = self._calculate_dist(chicken) |
475
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
631 if dist < min_dist: |
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
632 min_dist = dist |
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
633 self.closest = chicken |
507 | 634 self.target = chicken.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
|
635 if not self.closest: |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
636 # No more chickens, so leave |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
637 self.hunting = False |
507 | 638 self.target = self.start_pos |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
639 return self.pos |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
640 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
641 def attack(self): |
396
19e583e5cdc0
Refactor for further move work
Neil Muller <drnlmuller@gmail.com>
parents:
395
diff
changeset
|
642 """Attack a chicken""" |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
643 chicken = self.gameboard.get_animal_at_pos(self.pos, 'chicken') |
401
7405f7db469f
Tweak fox attack logic - we no longer ignore chickens we accidently step on
Neil Muller <drnlmuller@gmail.com>
parents:
399
diff
changeset
|
644 if chicken: |
7405f7db469f
Tweak fox attack logic - we no longer ignore chickens we accidently step on
Neil Muller <drnlmuller@gmail.com>
parents:
399
diff
changeset
|
645 # Always attack a chicken we step on, even if not hunting |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
646 self._catch_chicken(chicken) |
396
19e583e5cdc0
Refactor for further move work
Neil Muller <drnlmuller@gmail.com>
parents:
395
diff
changeset
|
647 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
648 def _catch_chicken(self, chicken): |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
649 """Catch a chicken""" |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
650 chicken.damage() |
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
|
651 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
|
652 self.hunting = False |
507 | 653 self.target = self.start_pos |
654 self._last_steps = [] | |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
655 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
656 def _update_pos(self, new_pos): |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
657 """Update the position, making sure we don't step on other foxes""" |
507 | 658 if not self.hunting and not self.gameboard.in_bounds(self.pos): |
659 self.safe = True | |
660 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
|
661 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
|
662 # 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
|
663 return new_pos |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
664 blocked = self.gameboard.get_animal_at_pos(new_pos, 'fox') is not None |
149
2a1064fae608
More memory for the foxes, to ensure we avoid short loops
Neil Muller <drnlmuller@gmail.com>
parents:
146
diff
changeset
|
665 final_pos = new_pos |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
666 if blocked: |
507 | 667 if new_pos.z != self.pos.z or self.pos.z != 0: |
399
3294929223bd
Cache fox positions to avoid a repeated loop
Neil Muller <drnlmuller@gmail.com>
parents:
398
diff
changeset
|
668 # We can only move up and down a ladder |
3294929223bd
Cache fox positions to avoid a repeated loop
Neil Muller <drnlmuller@gmail.com>
parents:
398
diff
changeset
|
669 moves = [Position(self.pos.x, self.pos.y, z) for z |
3294929223bd
Cache fox positions to avoid a repeated loop
Neil Muller <drnlmuller@gmail.com>
parents:
398
diff
changeset
|
670 in range(self.pos.z-1, self.pos.z + 2) if z >= 0] |
3294929223bd
Cache fox positions to avoid a repeated loop
Neil Muller <drnlmuller@gmail.com>
parents:
398
diff
changeset
|
671 else: |
507 | 672 moves = [self.pos + step for step in NEIGHBOUR_8] |
399
3294929223bd
Cache fox positions to avoid a repeated loop
Neil Muller <drnlmuller@gmail.com>
parents:
398
diff
changeset
|
673 # find the cheapest point in moves that's not blocked |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
674 final_pos = None |
82
bf28f499c6b4
Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
675 min_cost = 1000 |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
676 for poss in moves: |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
677 if self.gameboard.get_animal_at_pos(poss, 'fox'): |
399
3294929223bd
Cache fox positions to avoid a repeated loop
Neil Muller <drnlmuller@gmail.com>
parents:
398
diff
changeset
|
678 continue # blocked |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
679 cost = self._cost_tile(poss) |
82
bf28f499c6b4
Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
680 if cost < min_cost: |
bf28f499c6b4
Tweak fox avoidance behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
72
diff
changeset
|
681 min_cost = cost |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
682 final_pos = poss |
257
fcaae2cfe3cd
Make foxes less determistic when avoiding other foxes
Neil Muller <drnlmuller@gmail.com>
parents:
251
diff
changeset
|
683 if cost == min_cost and random.randint(0, 1) > 0: |
fcaae2cfe3cd
Make foxes less determistic when avoiding other foxes
Neil Muller <drnlmuller@gmail.com>
parents:
251
diff
changeset
|
684 # Add some randomness in this case |
fcaae2cfe3cd
Make foxes less determistic when avoiding other foxes
Neil Muller <drnlmuller@gmail.com>
parents:
251
diff
changeset
|
685 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
|
686 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
|
687 # 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
|
688 return self.pos |
507 | 689 if self._is_fence(final_pos) and not self.dig_pos: |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
690 return self._dig(final_pos) |
507 | 691 self._last_steps.append(final_pos) |
692 if len(self._last_steps) > 6: | |
693 self._last_steps.pop(0) | |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
694 return final_pos |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
695 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
696 def _dig(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
|
697 """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
|
698 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
|
699 self.dig_pos = dig_pos |
475
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
700 return self.pos |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
701 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
702 def _make_hole(self): |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
703 """Make a hole in the fence""" |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
704 fence = self.gameboard.get_building(self.dig_pos.to_tile_tuple()) |
394
ad77b3b71b08
Fix crash when multiple foxes finish digging at the same time
Neil Muller <drnlmuller@gmail.com>
parents:
389
diff
changeset
|
705 # Another fox could have made the same hole this turn |
ad77b3b71b08
Fix crash when multiple foxes finish digging at the same time
Neil Muller <drnlmuller@gmail.com>
parents:
389
diff
changeset
|
706 if fence: |
557
50d6c68ce267
Add gameboard to buildings. Update save version as this breaks old save games
Neil Muller <drnlmuller@gmail.com>
parents:
552
diff
changeset
|
707 fence.damage() |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
708 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
|
709 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
710 def move(self): |
28
ac3a74352b74
Change animal.py to four space indents.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
711 """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
|
712 chicken""" |
398
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
713 if self.safe: |
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
714 # We're safe, so do nothing |
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
715 return |
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
716 elif self.dig_pos: |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
717 if self.tick: |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
718 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
|
719 # 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
|
720 # 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
|
721 # 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
|
722 # but we'll notice if the fence we're digging vanishes |
507 | 723 if not self._is_fence(self.dig_pos): |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
724 self.tick = 0 |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
725 else: |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
726 # We've dug through the fence, so make a hole |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
727 self._make_hole() |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
728 return |
533
7addf41b6abb
Allow backtracking on ladders
Neil Muller <drnlmuller@gmail.com>
parents:
531
diff
changeset
|
729 desired_pos = self._calc_next_move() |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
730 final_pos = self._update_pos(desired_pos) |
70
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
731 self._fix_face(final_pos) |
d92a2f973cc4
Make foxes move 'better' and break fences
Neil Muller <drnlmuller@gmail.com>
parents:
53
diff
changeset
|
732 self.pos = final_pos |
398
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
733 change_visible = False |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
734 # See if we're entering/leaving a building |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
735 building = self.gameboard.get_building(final_pos.to_tile_tuple()) |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
736 if building and self.outside(): |
398
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
737 # Check if we need to enter |
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
738 if self.closest and not self.closest.outside() and \ |
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
739 self.closest.abode.building is building: |
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
740 building.add_predator(self) |
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
741 change_visible = True |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
742 elif self.building and final_pos.z == 0: |
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
743 # can only leave from the ground floor |
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
744 if building == self.building: |
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
745 # Check if we need to leave the building |
515 | 746 if not self.hunting or (self.closest and self.closest.abode |
747 and self.closest.abode.building is not building): | |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
748 self.building.remove_predator(self) |
398
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
749 change_visible = True |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
750 else: |
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
751 # we've moved away from the building we were in |
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
752 self.building.remove_predator(self) |
398
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
753 change_visible = True |
082b1ea5f98d
Regain time lost when adding foxes to buildings
Neil Muller <drnlmuller@gmail.com>
parents:
397
diff
changeset
|
754 if change_visible: |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
755 self.gameboard.set_visibility(self) |
397
532f1ea476ff
Make foxes enter buildings, with a seperate count for them
Neil Muller <drnlmuller@gmail.com>
parents:
396
diff
changeset
|
756 |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
757 |
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
758 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
|
759 """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
|
760 |
114 | 761 STEALTH = 60 |
130 | 762 IMAGE_FILE = 'sprites/ninja_fox.png' |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
763 CONFIG_NAME = 'ninja fox' |
114 | 764 |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
765 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
|
766 """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
|
767 |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
236
diff
changeset
|
768 DIG_ANIMATION = animations.FenceExplosion |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
236
diff
changeset
|
769 IMAGE_FILE = 'sprites/sapper_fox.png' |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
770 CONFIG_NAME = 'sapper fox' |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
236
diff
changeset
|
771 |
406
f8072b2d6dd1
Fix bug where sapper foxes overwrite costs for all foxes
Neil Muller <drnlmuller@gmail.com>
parents:
404
diff
changeset
|
772 costs = Fox.costs.copy() |
426
d34be925b9fc
Fix SIMPLIFY for chickens and eggs. Clean-up greedy fox cost overriding.
Simon Cross <hodgestar@gmail.com>
parents:
423
diff
changeset
|
773 costs['fence'] = 2 |
269
445f746449fa
Tweak sapper fox path weighting
Neil Muller <drnlmuller@gmail.com>
parents:
257
diff
changeset
|
774 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
775 def _dig(self, dig_pos): |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
236
diff
changeset
|
776 """Setup dig parameters, to be overridden if needed""" |
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
236
diff
changeset
|
777 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
|
778 self.dig_pos = dig_pos |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
779 self.DIG_ANIMATION(self.gameboard.tv, dig_pos.to_tile_tuple()) |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
780 self._make_hole() |
475
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
781 return self.pos |
241
1a7000c8211c
Demolition foxes, including better fox selection.
Jeremy Thurgood <firxen@gmail.com>
parents:
236
diff
changeset
|
782 |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
783 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
|
784 """Greedy foxes eat more chickens""" |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
785 CONFIG_NAME = 'greedy fox' |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
786 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
787 def __init__(self, pos, gameboard): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
788 Fox.__init__(self, pos, gameboard) |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
789 self.chickens_eaten = 0 |
518
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
790 self.last_chicken = None |
92
bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
Neil Muller <drnlmuller@gmail.com>
parents:
84
diff
changeset
|
791 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
792 def _catch_chicken(self, chicken): |
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
793 chicken.damage() |
518
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
794 self.last_chicken = self.closest |
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
|
795 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
|
796 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
|
797 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
|
798 self.hunting = False |
507 | 799 self.target = self.start_pos |
800 self._last_steps = [] | |
518
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
801 else: |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
802 self._select_prey() # select new target |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
803 |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
804 def _calculate_dist(self, chicken): |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
805 """Calculate the distance to the chicken""" |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
806 dist = super(GreedyFox, self)._calculate_dist(chicken) |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
807 if self.last_chicken and self.last_chicken is chicken: |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
808 # We hurt our teeth, only attack the same chicken if it's the |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
809 # only one nearby |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
810 dist += 15 |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
811 return dist |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
812 |
114 | 813 |
290 | 814 class Rinkhals(Fox): |
815 """The Rinkhals has eclectic tastes""" | |
816 STEALTH = 80 | |
817 IMAGE_FILE = 'sprites/rinkhals.png' | |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
818 CONFIG_NAME = 'rinkhals' |
290 | 819 |
507 | 820 costs = Fox.costs.copy() |
821 costs['fence'] = 2 | |
822 | |
518
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
823 def _calculate_dist(self, chicken): |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
824 """The Rinkhals eats eggs, so tweak distance accordingly""" |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
825 dist = chicken.pos.dist(self.pos) |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
826 if not chicken.eggs: |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
827 dist += 100 # The closest eggs have to be *far* away to be safe |
a42852e50df1
Rework prey selection. Greedy Foxes are less inclined to attack the same chicken multiple times
Neil Muller <drnlmuller@gmail.com>
parents:
515
diff
changeset
|
828 return dist |
475
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
829 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
830 def _catch_chicken(self, chicken): |
475
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
831 """The Rinkhals eats eggs, but does not harm chickens""" |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
832 chicken.remove_eggs() |
290 | 833 self.closest = None |
834 self.hunting = False | |
507 | 835 self.target = self.start_pos |
836 self._last_steps = [] | |
290 | 837 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
838 def _dig(self, dig_pos): |
475
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
839 """Snakes ignore fences""" |
d4f04d81fe54
Rinkhals eats eggs, along with associated changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
457
diff
changeset
|
840 return dig_pos |
290 | 841 |
476
3dae0fc14009
Animals have their own gameboard references instead of lugging them around in params all the time.
Jeremy Thurgood <firxen@gmail.com>
parents:
475
diff
changeset
|
842 def damage(self): |
290 | 843 """The Rinkhals is invincible!""" |
844 return True | |
845 | |
199
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
846 def _get_vision_param(parameter, watcher): |
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
847 param = getattr(watcher, parameter) |
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
848 if watcher.abode: |
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
849 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
|
850 param = modifier(param) |
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
851 return param |
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
852 |
437
95b81e917399
Buildings block line-of-sight, except for occupants.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
853 def visible(watcher, watchee, gameboard): |
443
4efe57fcc1d7
Anything off the board is invisible.
Jeremy Thurgood <firxen@gmail.com>
parents:
441
diff
changeset
|
854 if not gameboard.in_bounds(watchee.pos): |
4efe57fcc1d7
Anything off the board is invisible.
Jeremy Thurgood <firxen@gmail.com>
parents:
441
diff
changeset
|
855 # We can't see anything off the edge of the board. |
4efe57fcc1d7
Anything off the board is invisible.
Jeremy Thurgood <firxen@gmail.com>
parents:
441
diff
changeset
|
856 return False |
199
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
857 vision_bonus = _get_vision_param('VISION_BONUS', watcher) |
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
858 range_penalty = _get_vision_param('VISION_RANGE_PENALTY', watcher) |
437
95b81e917399
Buildings block line-of-sight, except for occupants.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
859 positions = watcher.pos.intermediate_positions(watchee.pos) |
95b81e917399
Buildings block line-of-sight, except for occupants.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
860 for pos in positions: |
95b81e917399
Buildings block line-of-sight, except for occupants.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
861 building = gameboard.get_building(pos.to_tile_tuple()) |
95b81e917399
Buildings block line-of-sight, except for occupants.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
862 # This allows chickens to fire across GuardTowers and Fences. |
95b81e917399
Buildings block line-of-sight, except for occupants.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
863 if building and building.BLOCKS_VISION and not (watcher in building.occupants()): |
95b81e917399
Buildings block line-of-sight, except for occupants.
Jeremy Thurgood <firxen@gmail.com>
parents:
431
diff
changeset
|
864 return False |
199
696936621a93
Buildings can affect visual acuity.
Jeremy Thurgood <firxen@gmail.com>
parents:
196
diff
changeset
|
865 distance = watcher.pos.dist(watchee.pos) - 1 |
438
16437cf4a2b8
Trees provide a modicum of cover.
Jeremy Thurgood <firxen@gmail.com>
parents:
437
diff
changeset
|
866 # Intervening forests get in the way a bit. |
552
11c4cebfe4c5
Preparatory work for woodland biodiversity.
Jeremy Thurgood <firxen@gmail.com>
parents:
533
diff
changeset
|
867 woods = len([pos for pos in positions if gameboard.is_woodland_tile(pos)]) |
114 | 868 roll = random.randint(1, 100) |
438
16437cf4a2b8
Trees provide a modicum of cover.
Jeremy Thurgood <firxen@gmail.com>
parents:
437
diff
changeset
|
869 return roll > watchee.STEALTH - vision_bonus + range_penalty*distance + constants.WOODLAND_CONCEALMENT*woods |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
870 |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
871 # These don't have to add up to 100, but it's easier to think |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
872 # about them if they do. |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
873 DEFAULT_FOX_WEIGHTINGS = ( |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
874 (Fox, 59), |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
875 (GreedyFox, 30), |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
876 (NinjaFox, 5), |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
877 (DemoFox, 5), |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
878 (Rinkhals, 1), |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
879 ) |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
380
diff
changeset
|
880 |