Ejemplo n.º 1
0
def get_holes(interpreter,
              speaker,
              location,
              limit=1,
              all_proximity=10) -> List[Tuple[XYZ, Hole]]:
    holes: List[Hole] = perception.get_all_nearby_holes(
        interpreter.agent, location)
    candidates: List[Tuple[XYZ, Hole]] = [
        (to_block_pos(np.mean(hole[0], axis=0)), hole) for hole in holes
    ]
    if len(candidates) > 0:
        # NB(demiguo): by default, we fill the hole the player is looking at
        player = interpreter.memory.get_player_struct_by_name(speaker)
        centroid_hole = object_looked_at(interpreter.agent,
                                         candidates,
                                         player,
                                         limit=limit)
        if centroid_hole is None or len(centroid_hole) == 0:
            # NB(demiguo): if there's no hole in front of the player, we will fill the nearest hole
            speaker_pos = interpreter.memory.get_player_struct_by_name(
                speaker).pos
            speaker_pos = to_block_pos(pos_to_np(speaker_pos))
            if limit == "ALL":
                return list(
                    filter(
                        lambda c: euclid_dist(c[0], speaker_pos) <=
                        all_proximity, candidates))
            else:
                candidates.sort(key=lambda c: euclid_dist(c[0], speaker_pos))
                return candidates[:limit]
        else:
            return centroid_hole
    else:
        return []
Ejemplo n.º 2
0
def come_to_player(agent):
    """Go to where the player is."""
    op = agent.get_other_players()
    if len(op) == 0:
        return
    p = random.choice(agent.get_other_players())
    agent.memory.task_stack_push(tasks.Move(agent, {"target": pos_to_np(p.pos), "approx": 3}))
Ejemplo n.º 3
0
 def step(self):
     """Extract bot's current location."""
     self.finished = True
     # Get the agent's current location
     agent_pos = pos_to_np(self.agent.get_player().pos)
     agent_coordinates = " , ".join([str(pos) for pos in agent_pos])
     answer_options = [
         "I am here at location : %r" % (agent_coordinates),
         "I am standing at : %r" % (agent_coordinates),
         "I am at : %r" % (agent_coordinates),
     ]
     return random.choice(answer_options), None
Ejemplo n.º 4
0
    def perceive(self, force=False):
        # FIXME (low pri) remove these in code, get from sql
        self.agent.pos = to_block_pos(pos_to_np(self.agent.get_player().pos))

        if self.agent.count % self.perceive_freq == 0 or force:
            for mob in self.agent.get_mobs():
                if euclid_dist(self.agent.pos, pos_to_np(
                        mob.pos)) < self.memory.perception_range:
                    self.memory.set_mob_position(mob)
            for item_stack in self.agent.get_item_stacks():
                if (euclid_dist(self.agent.pos, pos_to_np(item_stack.pos)) <
                        self.memory.perception_range):
                    self.memory.set_item_stack_position(item_stack)

        # note: no "force"; these run on every perceive call.  assumed to be fast
        self.update_self_memory()
        self.update_other_players(self.agent.get_other_players())

        # use safe_get_changed_blocks to deal with pointing
        for (xyz, idm) in self.agent.safe_get_changed_blocks():
            self.on_block_changed(xyz, idm)
Ejemplo n.º 5
0
    def perceive(self, force=False):
        # FIXME (low pri) remove these in code, get from sql
        self.agent.pos = to_block_pos(pos_to_np(self.agent.get_player().pos))

        # note: no "force"; these run on every perceive call.  assumed to be fast
        for mob in self.agent.get_mobs():
            self.memory.set_mob_position(mob)

        self.update_other_players(self.agent.get_other_players())

        # use safe_get_changed_blocks to deal with pointing
        for (xyz, idm) in self.agent.safe_get_changed_blocks():
            self.on_block_changed(xyz, idm)
Ejemplo n.º 6
0
    def step(self):
        self.pos = to_block_pos(pos_to_np(self.get_player().pos))

        # Update memory with current world state
        # Removed get_objects call due to very slow updates on non-flatworlds
        with TimingWarn(2):
            self.memory.update(self)

        # Process incoming chats
        self.dialogue_step()

        # Step topmost task on stack
        self.task_step()
Ejemplo n.º 7
0
 def _is_placed_block_interesting(self, xyz: XYZ, bid: int) -> Tuple[bool, bool]:
     """Return two values:
     - bool: is the placed block interesting?
     - bool: is it interesting because it was placed by a player?
     """
     if xyz in self.pending_agent_placed_blocks:
         self.pending_agent_placed_blocks.remove(xyz)
         return True, True
     for player in self.other_players.values():
         if util.euclid_dist(util.pos_to_np(player.pos), xyz) < 5 and player.mainHand.id == bid:
             return True, True
     if bid not in BORING_BLOCKS:
         return True, False
     return False, False
def compute_location_heuristic(interpreter, speaker, d, mems):
    # handle relative direction
    reldir = d.get("relative_direction")
    loc = mems[0].get_pos()
    if reldir is not None:
        if reldir == "BETWEEN":
            loc = (np.add(mems[0].get_pos(), mems[1].get_pos())) / 2
            loc = (loc[0], loc[1], loc[2])
        elif reldir == "INSIDE":
            ref_obj_dict = d.get("reference_object", SPEAKERLOOK["reference_object"])
            special = ref_obj_dict.get("special_reference")
            if not special:
                for i in range(len(mems)):
                    mem = mems[i]
                    locs = heuristic_perception.find_inside(mem)
                    if len(locs) > 0:
                        break
                if len(locs) == 0:
                    raise ErrorWithResponse("I don't know how to go inside there")
                else:
                    interpreter.memory.update_recent_entities([mem])
                    loc = locs[0]
            else:
                raise ErrorWithResponse("I don't know how to go inside there")
        elif reldir == "AWAY":
            apos = pos_to_np(interpreter.agent.get_player().pos)
            dir_vec = (apos - loc) / np.linalg.norm(apos - loc)
            num_steps = word_to_num(d.get("steps", "5"))
            loc = num_steps * np.array(dir_vec) + to_block_center(loc)
        elif reldir == "NEAR":
            pass
        else:  # LEFT, RIGHT, etc...
            reldir_vec = rotation.DIRECTIONS[reldir]
            look = (
                interpreter.agent.perception_modules["low_level"]
                .get_player_struct_by_name(speaker)
                .look
            )
            # this should be an inverse transform so we set inverted=True
            dir_vec = rotation.transform(reldir_vec, look.yaw, 0, inverted=True)
            num_steps = word_to_num(d.get("steps", "5"))
            loc = num_steps * np.array(dir_vec) + to_block_center(loc)

    # if steps without relative direction
    elif "steps" in d:
        num_steps = word_to_num(d.get("steps", "5"))
        loc = to_block_center(loc) + [0, 0, num_steps]
    return post_process_loc(loc, interpreter)
Ejemplo n.º 9
0
    def step(self):
        self.pos = to_block_pos(pos_to_np(self.get_player().pos))

        # remove old point targets
        self.point_targets = [pt for pt in self.point_targets if time.time() - pt[1] < 6]

        # Update memory with current world state
        # Removed perceive call due to very slow updates on non-flatworlds
        with TimingWarn(2):
            self.memory.update(self)

        # Process incoming chats
        self.dialogue_step()

        # Step topmost task on stack
        self.task_step()
Ejemplo n.º 10
0
 def _is_placed_block_interesting(self, xyz: XYZ,
                                  bid: int) -> Tuple[bool, bool, bool]:
     """Return three values:
     - bool: is the placed block interesting?
     - bool: is it interesting because it was placed by a player?
     - bool: is it interesting because it was placed by the agent?
     """
     interesting = False
     player_placed = False
     agent_placed = False
     # TODO record *which* player placed it
     if xyz in self.pending_agent_placed_blocks:
         self.pending_agent_placed_blocks.remove(xyz)
         interesting = True
         agent_placed = True
     for player in self.other_players.values():
         if util.euclid_dist(util.pos_to_np(player.pos),
                             xyz) < 5 and player.mainHand.id == bid:
             interesting = True
             player_placed = True
     if bid not in BORING_BLOCKS:
         interesting = True
     return interesting, player_placed, agent_placed
Ejemplo n.º 11
0
 def get_speaker_pos(self) -> XYZ:
     return tuple(
         pos_to_np(self.memory.get_player_struct_by_name(self.speaker).pos))
Ejemplo n.º 12
0
def interpret_location(interpreter, speaker, d, ignore_reldir=False) -> XYZ:
    """Location dict -> coordinates
    Side effect:  adds mems to agent_memory.recent_entities
    if a reference object is interpreted;
    and loc to memory
    """
    location_type = d.get("location_type", "SPEAKER_LOOK")
    if location_type == "REFERENCE_OBJECT":
        mems = interpret_reference_object(interpreter, speaker,
                                          d["reference_object"])
        if len(mems) == 0:
            raise ErrorWithResponse("I don't know what you're referring to")
        assert len(mems) == 1, mems
        interpreter.memory.update_recent_entities(mems)
        mem = mems[0]
        loc = mem.get_pos()

    elif location_type == "SPEAKER_LOOK":
        player = interpreter.memory.get_player_struct_by_name(speaker)
        loc = capped_line_of_sight(interpreter.agent, player)

    elif location_type == "SPEAKER_POS":
        loc = pos_to_np(
            interpreter.memory.get_player_struct_by_name(speaker).pos)

    elif location_type == "AGENT_POS":
        loc = pos_to_np(interpreter.agent.get_player().pos)

    elif location_type == "COORDINATES":
        loc = cast(
            XYZ,
            tuple(
                int(float(w))
                for w in re.findall("[-0-9.]+", d["coordinates"])))
        if len(loc) != 3:
            logging.error("Bad coordinates: {}".format(d["coordinates"]))
            raise ErrorWithResponse(
                "I don't understand what location you're referring to")

    else:
        raise ValueError(
            "Can't handle Location type: {}".format(location_type))

    # handle relative direction
    reldir = d.get("relative_direction")
    if reldir is not None and not ignore_reldir:
        if reldir == "INSIDE":
            if location_type == "REFERENCE_OBJECT":
                locs = perception.find_inside(mem)
                if len(locs) == 0:
                    raise ErrorWithResponse(
                        "I don't know how to go inside there")
                else:
                    loc = locs[0]
        elif reldir == "AWAY":
            apos = pos_to_np(interpreter.agent.get_player().pos)
            dir_vec = (apos - loc) / np.linalg.norm(apos - loc)
            num_steps = word_to_num(d.get("steps", "5"))
            loc = num_steps * np.array(dir_vec) + to_block_center(loc)
        elif reldir == "NEAR":
            pass
        else:  # LEFT, RIGHT, etc...
            reldir_vec = rotation.DIRECTIONS[reldir]
            look = interpreter.memory.get_player_struct_by_name(speaker).look
            # this should be an inverse transform so we set inverted=True
            dir_vec = rotation.transform(reldir_vec,
                                         look.yaw,
                                         0,
                                         inverted=True)
            num_steps = word_to_num(d.get("steps", "5"))
            loc = num_steps * np.array(dir_vec) + to_block_center(loc)

    # if steps without relative direction
    elif "steps" in d:
        num_steps = word_to_num(d.get("steps", "5"))
        loc = to_block_center(loc) + [0, 0, num_steps]
    return to_block_pos(loc)
Ejemplo n.º 13
0
def get_players(interpreter, *tags) -> List[Tuple[XYZ, PlayerNode]]:
    """Return a list of (xyz, memory) tuples, filtered by tags"""
    players = interpreter.memory.get_players_tagged(*tags)
    return [(to_block_pos(pos_to_np(player.pos)), player)
            for player in players]