def move(self): '''Predator is pretty dumb: if it sees a pray, it follows it to autodestruction. ''' # Can always do indexing on filtered instances, as it should be a pray # always, else the game should have finished (bug?). pray = filter(lambda x: isinstance(x, Pray), self.game.instances)[0] # In case it sees the pray, change direction to follow it. if BaseObject.object_sees_object(self, pray): self.direction = Helper.get_direction_towards(self.coord, pray.coord) # Try and find close predators and sync with them to arrive at the # prey in the same time. # # The sync works the following way: # - each predator finds the max speed and max distance to pray # (from all predators visible from him that they follow the pray) # - they update their speed v' = v_max * d / d_max # where d = own distance to pray max_speed = self.speed own_dist = max_dist = Helper.euclidian_distance(self.coord, pray.coord) for instance in self.game.instances: if instance == self or not isinstance(instance, Predator): continue # Look only for visible predators other than myself. if BaseObject.object_sees_object(self, instance): dist = Helper.euclidian_distance(instance.coord, pray.coord) max_dist = max(max_dist, dist) max_speed = max(max_speed, instance.speed) # Sync speed with other predators. self.speed = max_speed * own_dist / float(max_dist) super(Predator, self).move()
def __find_best_state(self, state, states): '''Find the best state out of states array to match the given state. ''' best_d = Helper.euclidian_distance(state, states[0]) best_state = states[0] for state2 in states[1:]: d = Helper.euclidian_distance(state, state2) if best_d > d: best_d = d best_state = state2 return best_d, best_state