def tryDigHallwayBetween( self, alpha, beta): from pathfind import Pathfinder, infinity import math tx, ty = beta.floorpoint() ox, oy = alpha.floorpoint() assert self.data[oy][ox] == '.' assert self.data[ty][tx] == '.' costs = { '!': infinity, '*': infinity, '.': 1, '#': 100, '-': 10, ':': infinity, '+': infinity, 'X': infinity, '=': infinity, 'C': infinity, '[': infinity, } def cost( (x, y) ): if self.data[y][x] != '-': if not alpha.contains( x, y) and not beta.contains( x, y): return infinity return costs[ self.data[y][x] ] pf = Pathfinder(cost = cost, goal = lambda (x,y) : beta.contains(x, y) and self.data[y][x] == '.', heuristic = lambda (x,y) : math.sqrt( (x-tx)*(x-tx) + (y-tx)*(y-tx) ), neighbours = lambda (x,y) : [ (x+dx,y+dy) for (dx,dy) in ((0,1),(0,-1),(1,0),(-1,0)) if x+dx >= 0 and y+dy >= 0 and x+dx < self.width and y+dy < self.height ], limit = self.hallwayLimit, delay = self.delay ) pf.addOrigin( (ox,oy) ) path = pf.seek() if not path: return False protect = [] for x, y in path: self.data[y][x] = { '#': '+', '-': ':', '.': '.', '=': ':', '[': ':', }[ self.data[y][x] ] if self.data[y][x] != '.': protect.append( (x,y) ) for x, y in protect: for dx, dy in ((1,0),(-1,0),(0,1),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)): try: t = self.data[y+dy][x+dx] except IndexError: continue if t == '#': self.data[y+dy][x+dx] = '[' if t == '-': self.data[y+dy][x+dx] = '=' return True
def getClearTileAround(self, origin, goal = lambda tile: not tile.impassable and not tile.mobile): from pathfind import Pathfinder, infinity import math pf = Pathfinder(cost = lambda tile : 1, goal = goal, heuristic = lambda tile : 0, ) pf.addOrigin( origin ) path = pf.seek() return path[-1]
def seekGoal(mob, target, radius, openDoors = False): from pathfind import Pathfinder, infinity import math pf = Pathfinder(cost = lambda tile : infinity if tile.cannotEnterBecause( mob ) and not target(tile) and not (openDoors and tile.name == "closed door") else 1, goal = target, heuristic = lambda tile : 0, limit = radius, ) pf.addOrigin( mob.tile ) path = pf.seek() return path
def seekMob(mob, target, radius): from pathfind import Pathfinder, infinity import math pf = Pathfinder(cost = lambda tile : infinity if tile.cannotEnterBecause( mob ) and not tile.mobile == target else 1, goal = lambda tile : tile.mobile == target, heuristic = lambda tile : max( abs( tile.x - target.tile.x ), abs( tile.y - target.tile.y ) ), tiebreaker = lambda tile : (tile.x - target.tile.x)**2 + (tile.y - target.tile.y)**2, limit = radius, ) pf.addOrigin( mob.tile ) path = pf.seek() return path