def act()

in rtfm/dynamics/monster/hostile.py [0:0]


    def act(self, world, engine):
        closer = []
        if random.random() < self.aggression:
            # find nearest agent that is observable
            agents = [a for a in world.agents if self.position_is_observable(a.position)]
            if agents:
                agents.sort(key=lambda a: self.get_dist_to_position(a.position))
                target = agents[0]
                # find moves that would make us closer to agent
                x, y = self.position
                orig_dist = self.get_dist_to_position(target.position)
                if orig_dist == 0:
                    closer.append(E.Stay)
                else:
                    if x > 0 and target.get_dist_to_position((x-1, y)) < orig_dist:
                        closer.append(E.Left)
                    if x < world.width and target.get_dist_to_position((x+1, y)) < orig_dist:
                        closer.append(E.Right)
                    if y > 0 and target.get_dist_to_position((x, y-1)) < orig_dist:
                        closer.append(E.Up)
                    if y < world.height and target.get_dist_to_position((x, y+1)) < orig_dist:
                        closer.append(E.Down)
        if closer:
            Move = random.choice(closer)
            engine.queue_event(Move(self))
        else:
            # behave randomly
            super().act(world, engine)