Mercurial > rinkhals
comparison gamelib/animal.py @ 404:e5247ec76f24
The new faster deciding (but somewhat dumber) fox
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 19 Nov 2009 13:33:57 +0000 |
parents | 3a469d46b820 |
children | f8072b2d6dd1 |
comparison
equal
deleted
inserted
replaced
403:c7cfa230f5d4 | 404:e5247ec76f24 |
---|---|
253 costs = { | 253 costs = { |
254 # weighting for movement calculation | 254 # weighting for movement calculation |
255 'grassland' : 2, | 255 'grassland' : 2, |
256 'woodland' : 1, # Try to keep to the woods if possible | 256 'woodland' : 1, # Try to keep to the woods if possible |
257 'broken fence' : 2, | 257 'broken fence' : 2, |
258 'fence' : 10, | 258 'fence' : 25, |
259 'guardtower' : 2, # We can pass under towers | 259 'guardtower' : 2, # We can pass under towers |
260 'henhouse' : 30, # Don't go into a henhouse unless we're going to | 260 'henhouse' : 30, # Don't go into a henhouse unless we're going to |
261 # catch a chicken there | 261 # catch a chicken there |
262 'hendominium' : 30, | 262 'hendominium' : 30, |
263 } | 263 } |
319 along the path.""" | 319 along the path.""" |
320 # We calculate the cost of the direct path | 320 # We calculate the cost of the direct path |
321 if final_pos.z < self.pos.z: | 321 if final_pos.z < self.pos.z: |
322 # We need to try heading down. | 322 # We need to try heading down. |
323 return Position(self.pos.x, self.pos.y, self.pos.z - 1) | 323 return Position(self.pos.x, self.pos.y, self.pos.z - 1) |
324 if final_pos.x == self.pos.x and final_pos.y == self.pos.y and \ | |
325 final_pos.z > self.pos.z: | |
326 # We try heading up | |
327 return Position(self.pos.x, self.pos.y, self.pos.z + 1) | |
328 cur_dist = final_pos.dist(self.pos) | |
329 if cur_dist < 2: | |
330 # We're right ontop of our target, so just go there | |
331 return final_pos | |
332 # Find the cheapest spot close to us that moves us closer to the target | |
333 neighbours = [Position(self.pos.x + x, self.pos.y + y, self.pos.z) for | |
334 x in range(-1, 2) for y in range(-1, 2) if (x, y) != (0, 0)] | |
335 best_pos = self.pos | |
336 min_cost = 1000 | |
337 min_dist = cur_dist | |
338 for point in neighbours: | |
339 dist = point.dist(final_pos) | |
340 if dist < cur_dist: | |
341 cost = self._cost_tile(point, gameboard) | |
342 if cost < min_cost or (min_cost == cost and dist < min_dist): | |
343 # Prefer closest of equal cost points | |
344 min_dist = dist | |
345 min_cost = cost | |
346 best = point | |
347 if min_cost < 20: | |
348 return best | |
349 # Else expensive step, so think further | |
324 direct_path = self._gen_path(self.pos, final_pos) | 350 direct_path = self._gen_path(self.pos, final_pos) |
325 min_cost = self._cost_path(direct_path, gameboard) | 351 min_cost = self._cost_path(direct_path, gameboard) |
326 min_path = direct_path | 352 min_path = direct_path |
327 # is there a point nearby that gives us a cheaper direct path? | 353 # is there a point nearby that gives us a cheaper direct path? |
328 # This is delibrately not finding the optimal path, as I don't | 354 # This is delibrately not finding the optimal path, as I don't |
329 # want the foxes to be too intelligent, although the implementation | 355 # want the foxes to be too intelligent, although the implementation |
330 # isn't well optimised yet | 356 # isn't well optimised yet |
331 poss = [Position(x, y) for x in range(self.pos.x - 3, self.pos.x + 4) | 357 # FIXME: Currently, this introduces loops, but the memory kind |
332 for y in range(self.pos.y - 3, self.pos.y + 4) | 358 # avoids that. Fixing this is the next goal. |
333 if (x, y) != (0,0)] | 359 poss = [Position(self.pos.x + x, self.pos.y + y, self.pos.z) for |
360 x in range(-3, 4) for y in range(-3, 4) if (x, y) != (0, 0)] | |
334 for start in poss: | 361 for start in poss: |
335 cand_path = self._gen_path(self.pos, start) + \ | 362 cand_path = self._gen_path(self.pos, start) + \ |
336 self._gen_path(start, final_pos) | 363 self._gen_path(start, final_pos) |
337 cost = self._cost_path(cand_path, gameboard) | 364 cost = self._cost_path(cand_path, gameboard) |
338 if cost < min_cost: | 365 if cost < min_cost: |