Mercurial > rinkhals
comparison gamelib/animal.py @ 92:bea1b9364583
Refactor Fox so we can have different types. Add a greedy fox
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Wed, 02 Sep 2009 10:47:46 +0000 |
parents | 5494af02a0e8 |
children | 725b292ca07b |
comparison
equal
deleted
inserted
replaced
91:db78e8b1f8b0 | 92:bea1b9364583 |
---|---|
192 # No more chickens, so leave | 192 # No more chickens, so leave |
193 self.hunting = False | 193 self.hunting = False |
194 return self.pos | 194 return self.pos |
195 if closest.pos == self.pos: | 195 if closest.pos == self.pos: |
196 # Caught a chicken | 196 # Caught a chicken |
197 gameboard.remove_chicken(closest) | 197 self._catch_chicken(closest, gameboard) |
198 self.hunting = False | |
199 return self.pos | 198 return self.pos |
200 return self._find_best_path_step(closest.pos, gameboard) | 199 return self._find_best_path_step(closest.pos, gameboard) |
200 | |
201 def _catch_chicken(self, chicken, gameboard): | |
202 """Catch a chicken""" | |
203 gameboard.remove_chicken(chicken) | |
204 self.hunting = False | |
201 | 205 |
202 def _update_pos(self, gameboard, new_pos): | 206 def _update_pos(self, gameboard, new_pos): |
203 """Update the position, making sure we don't step on other foxes""" | 207 """Update the position, making sure we don't step on other foxes""" |
204 final_pos = new_pos | 208 final_pos = new_pos |
205 moves = [Position(x, y) for x in range(self.pos.x-1, self.pos.x + 2) | 209 moves = [Position(x, y) for x in range(self.pos.x-1, self.pos.x + 2) |
226 this_tile = tiles.REVERSE_TILE_MAP['woodland'] | 230 this_tile = tiles.REVERSE_TILE_MAP['woodland'] |
227 if tiles.TILE_MAP[this_tile] == 'broken fence' and self.hunting: | 231 if tiles.TILE_MAP[this_tile] == 'broken fence' and self.hunting: |
228 # We'll head back towards the holes we make/find | 232 # We'll head back towards the holes we make/find |
229 self.landmarks.append(final_pos) | 233 self.landmarks.append(final_pos) |
230 elif tiles.TILE_MAP[this_tile] == 'fence' and not self.dig_pos: | 234 elif tiles.TILE_MAP[this_tile] == 'fence' and not self.dig_pos: |
231 self.tick = 5 | 235 self._dig(final_pos) |
232 self.dig_pos = final_pos | |
233 return self.pos | 236 return self.pos |
234 return final_pos | 237 return final_pos |
238 | |
239 def _dig(self, dig_pos): | |
240 """Setup dig parameters, to be overridden if needed""" | |
241 self.tick = 5 | |
242 self.dig_pos = dig_pos | |
243 | |
244 def _make_hole(self, gameboard): | |
245 """Make a hole in the fence""" | |
246 gameboard.tv.set(self.dig_pos.to_tuple(), | |
247 tiles.REVERSE_TILE_MAP['broken fence']) | |
248 self.dig_pos = None | |
235 | 249 |
236 def move(self, gameboard): | 250 def move(self, gameboard): |
237 """Foxes will aim to move towards the closest henhouse or free | 251 """Foxes will aim to move towards the closest henhouse or free |
238 chicken""" | 252 chicken""" |
239 if self.dig_pos: | 253 if self.dig_pos: |
240 if self.tick: | 254 if self.tick: |
241 # We're digging through the fence | |
242 self.tick -= 1 | 255 self.tick -= 1 |
256 # We're still digging through the fence | |
243 # Check the another fox hasn't dug a hole for us | 257 # Check the another fox hasn't dug a hole for us |
244 # We're top busy digging to notice if a hole appears nearby, | 258 # We're too busy digging to notice if a hole appears nearby, |
245 # but we'll notice if the fence we're digging vanishes | 259 # but we'll notice if the fence we're digging vanishes |
246 this_tile = gameboard.tv.get(self.dig_pos.to_tuple()) | 260 this_tile = gameboard.tv.get(self.dig_pos.to_tuple()) |
247 if tiles.TILE_MAP[this_tile] == 'broken fence': | 261 if tiles.TILE_MAP[this_tile] == 'broken fence': |
248 self.tick = 0 | 262 self.tick = 0 |
263 return | |
249 else: | 264 else: |
250 # We've dug through the fence, so make a hole | 265 # We've dug through the fence, so make a hole |
251 gameboard.tv.set(self.dig_pos.to_tuple(), | 266 self._make_hole(gameboard) |
252 tiles.REVERSE_TILE_MAP['broken fence']) | |
253 self.dig_pos = None | |
254 return | 267 return |
255 if self.hunting: | 268 if self.hunting: |
256 desired_pos = self._find_path_to_chicken(gameboard) | 269 desired_pos = self._find_path_to_chicken(gameboard) |
257 else: | 270 else: |
258 desired_pos = self._find_path_to_woodland(gameboard) | 271 desired_pos = self._find_path_to_woodland(gameboard) |
259 final_pos = self._update_pos(gameboard, desired_pos) | 272 final_pos = self._update_pos(gameboard, desired_pos) |
260 self._fix_face(final_pos) | 273 self._fix_face(final_pos) |
261 self.pos = final_pos | 274 self.pos = final_pos |
262 | 275 |
263 | 276 class NinjaFox(Fox): |
277 """Ninja foxes are hard to see""" | |
278 | |
279 class DemoFox(Fox): | |
280 """Demolition Foxes destroy fences easily""" | |
281 | |
282 class GreedyFox(Fox): | |
283 """Greedy foxes eat more chickens""" | |
284 | |
285 def __init__(self, pos): | |
286 Fox.__init__(self, pos) | |
287 self.chickens_eaten = 0 | |
288 | |
289 def _catch_chicken(self, chicken, gameboard): | |
290 gameboard.remove_chicken(chicken) | |
291 self.chickens_eaten += 1 | |
292 if self.chickens_eaten > 2: | |
293 self.hunting = False |