def update(self, world): """DOCSTRING NEEDED HERE""" self.move(world.cellmap) nearbytiles = coords.neighbours(self.position) + [self.position] for player in world.players: if player.position in nearbytiles: self._suggestmessage("Quack", 15) if self.position in player.visibletiles: if not self.visible: self._suggestmessage("Oh look, a duckie", 1) self.visible = True else: self.visible = False
def followpath(self): def subtuple(a, b): return coords.sum(a, coords.mul(b, -1)) oldpos = subtuple(self.position, self.direction) pathnbrs = [] for nbrpos in coords.neighbours(self.position): if (nbrpos == oldpos) or (self.cellmap[nbrpos]['roughness'] > 2): continue pathnbrs.append(nbrpos) if len(pathnbrs) != 1: return False self.move(*subtuple(pathnbrs[0], self.position)) return True
def update(self, world): """Move; tell player if becoming visible""" self.move() nearbytiles = coords.neighbours(self.position) + [self.position] for player in world.players: if player.position in nearbytiles: phrase = self.phrasebook[random.randint(0, len(self.phrasebook)-1)] self._suggestmessage("Pixie: " + phrase, 50) if self.position in player.visibletiles: if not self.visible: self._suggestmessage("You see a pixie in the distance", 1) self.visible = True else: self.visible = False
def firstmove(startpos, destpos, costs, cellmap, pfmapsize=15, maxcost=40): """Find the best direction to move towards the destination""" if (any(ax > pfmapsize for ax in coords.mindist(startpos, destpos, cellmap.size))): # Player is outside pathfinder area return False def mapcoord(pfcoord): """Get map coordinate from pathfinder one""" return coords.modsum(startpos, pfcoord, (-pfmapsize,)*2, cellmap.size) foundtarget = False dijkstramap = [[[0, (pfmapsize,)*2, False] for x in xrange(2*pfmapsize)] for x in xrange(2*pfmapsize)] import heapq openlist = [] heapq.heappush(openlist, (0, (pfmapsize,)*2)) curpos = None while openlist: curnode = heapq.heappop(openlist) curdist = curnode[0] if curdist > maxcost: # Give up if player is painfully unreachable. break curpos = curnode[1] if mapcoord(curpos) == tuple(destpos): foundtarget = True break if dijkstramap[curpos[0]][curpos[1]][2] == True: continue else: dijkstramap[curpos[0]][curpos[1]][2] = True for nbrpos in coords.neighbours(curpos): if (nbrpos[0] < 0 or nbrpos[1] < 0 or nbrpos[0] >= 2*pfmapsize or nbrpos[1] >= 2*pfmapsize or nbrpos == (pfmapsize, pfmapsize)): continue cellcost = costs(cellmap[mapcoord(nbrpos)]) newdist = curdist+cellcost if ((dijkstramap[nbrpos[0]][nbrpos[1]][0] <= newdist and dijkstramap[nbrpos[0]][nbrpos[1]][0] != 0) or cellmap[mapcoord(nbrpos)]['solid'] or cellcost > 8): continue dijkstramap[nbrpos[0]][nbrpos[1]] = [newdist, curpos, False] heapq.heappush(openlist, (newdist, nbrpos)) if not foundtarget: return False while dijkstramap[curpos[0]][curpos[1]][1] != (pfmapsize, pfmapsize): curpos = dijkstramap[curpos[0]][curpos[1]][1] return coords.sum(curpos, (-pfmapsize,)*2)