def ai_search(x,y,x_goal,y_goal,level,got_all_gold=False): q = Queue.Queue() seen = [[False for i in xrange(level.width)]\ for j in xrange(level.height)] # corresponding tile coordinates tx = snap(x)/TILE_SIZE ty = (y+TILE_SIZE-1)/TILE_SIZE tx_goal = None ty_goal = None if x_goal is not None: tx_goal = snap(x_goal)/TILE_SIZE if y_goal is not None: ty_goal = (y_goal+TILE_SIZE-1)/TILE_SIZE if (tx_goal is None or tx == tx_goal) and\ (ty_goal is None or ty == ty_goal): return "s" # you're at the goal if level.person_floats(x,y,got_all_gold): return "s" # you're already falling, why fight it? if y % TILE_SIZE == 0 or not level.tiles[ty][tx].is_climbable(): for i in xrange(len(dx)): if dd[i] != 'u' or level.tiles[ty][tx].is_climbable(): q.put((tx+dx[i],ty+dy[i],dd[i])) else: # ladder special case # enemy may not be able to move if it is on a ladder # and it is not snapped because there may be # solid blocks either side of it. for i in xrange(len(ladder_dx)): q.put((tx+ladder_dx[i],ty+ladder_dy[i],ladder_dd[i])) while not q.empty(): cur = q.get() # bounds check if cur[0] < 0 or cur[0] >= level.width or\ cur[1] < 0 or cur[1] >= level.height: continue # collision if level.tiles[cur[1]][cur[0]].is_solid(): continue # seen if seen[cur[1]][cur[0]]: continue seen[cur[1]][cur[0]] = True # if one of the arguments are None, it means we # don't pay attention to that axis if (tx_goal is None or cur[0] == tx_goal) and\ (ty_goal is None or cur[1] == ty_goal): return cur[2] for i in xrange(len(dx)): if dd[i] != 'u' or level.tiles[cur[1]][cur[0]].is_climbable(): q.put((cur[0]+dx[i],cur[1]+dy[i],cur[2])) # if we can't get to him, just stand still return "s"
def update(self, level): if level.person_collides(self.x, self.y): self.dead = True return if level.person_on_exit(self.x, self.y, self.gold==level.gold): self.winner = True return if level.person_floats(self.x, self.y, self.gold==level.gold): if not level.person_collides(snap(self.x), self.y+3): self.y += 3 self.x = snap(self.x)
def move(self, level, direction): if direction is "l": if not level.person_collides(self.x-3,self.y): self.x -= 3 elif direction is "r": if not level.person_collides(self.x+3,self.y): self.x += 3 elif direction is "u": if level.person_climbs(self.x,self.y,True,self.gold==level.gold) and not\ level.person_collides(snap(self.x),self.y-3): self.y -= 3 self.x = snap(self.x) elif direction is "d": if level.person_climbs(self.x,self.y,False,self.gold==level.gold) and not\ level.person_collides(snap(self.x),self.y+3): self.y += 3 self.x = snap(self.x) self.take_gold(level)
def zap(self, level, left): if not level.person_collides(snap(self.x), self.y) and\ level.zap(snap(self.x),self.y,left,self.gold==level.gold): self.x = snap(self.x)