def contaminated(blocks):
     """
     Check if blocks are still consistent with the current world
     """
     mx, Mx, my, My, mz, Mz = get_bounds(blocks)
     yzxb = self.agent.get_blocks(mx, Mx, my, My, mz, Mz)
     for b, _ in blocks:
         x, y, z = b
         if loc2ids[b][0] != yzxb[y - my, z - mz, x - mx, 0]:
             return True
     return False
예제 #2
0
def shift_subsegment_corner(S):
    """
    Takes a segment, described as a list of tuples of the form:
        ((x, y, z), (block_id, ?))
    Returns the segment in the same form, shifted to the origin, and the shift vec
    """
    bounds = get_bounds(S)
    shift_zero_vec = [-bounds[0], -bounds[2], -bounds[4]]
    new_S = []
    for s in S:
        new_S.append((tuple([sum(x) for x in zip(s[0], shift_zero_vec)]), s[1]))
    return new_S, shift_zero_vec
예제 #3
0
def get_repeat_arrangement(d,
                           interpreter,
                           speaker,
                           schematic,
                           repeat_num=-1,
                           extra_space=1) -> List[XYZ]:
    shapeparams = {}
    # eventually fix this to allow number based on shape
    shapeparams["N"] = repeat_num
    shapeparams["extra_space"] = extra_space
    if "repeat" in d:
        direction_name = d.get("repeat", {}).get("repeat_dir", "FRONT")
    elif "schematic" in d:
        direction_name = d["schematic"].get("repeat",
                                            {}).get("repeat_dir", "FRONT")
    if direction_name != "AROUND":
        reldir_vec = rotation.DIRECTIONS[direction_name]
        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)
        shapeparams["orient"] = dir_vec
        offsets = shapes.arrange("line", schematic, shapeparams)
    else:
        # TODO vertical "around"
        shapeparams["orient"] = "xy"
        shapeparams["encircled_object_radius"] = 1
        if d.get("location") is not None:
            central_object = interpret_reference_object(
                interpreter,
                speaker,
                d["location"]["reference_object"],
                limit=1)
            # FIXME: .blocks is unsafe, assumes BlockObject only object could be Mob. Ignoring for now.
            central_object_blocks = central_object[0].blocks  # type: ignore
            # .blocks returns a dict of (x, y, z) : (block_id, meta), convert to list
            # to get bounds
            central_object_list = [
                tuple([k, v]) for k, v in central_object_blocks.items()
            ]
            bounds = shapes.get_bounds(central_object_list)
            b = max(bounds[1] - bounds[0], bounds[3] - bounds[2],
                    bounds[5] - bounds[4])
            shapeparams["encircled_object_radius"] = b
        offsets = shapes.arrange("circle", schematic, shapeparams)
    offsets = [tuple(to_block_pos(o)) for o in offsets]
    return offsets
예제 #4
0
def build_ref_obj_scene(ad, ref_objs, sl=32, flat=False):
    # first place the fake observer, we put them on a ring 75% away from the center of the cube, at ground level
    c = np.random.randn(2)
    c /= np.linalg.norm(c)
    radius = 0.75 * sl / 2
    p = radius * c
    # the fake observer will look at the ground somewhere along a cone starting from the observer pointing towards
    # the center of the square

    #    n = np.random.randn(2)
    #    n /= np.linalg.norm(n)
    #    n *= 0.05
    #    view_vector = -c + n
    view_vector = -c
    view_vector = view_vector / np.linalg.norm(
        view_vector
    )  # unit vector in the direction of my look
    d = radius + radius * np.random.uniform()
    view_pos = p + d * view_vector  # which grid location/cube (at ground height) am I looking at?
    scene = {"mobs": [], "block_obj": [], "holes": []}
    scene["fake_human"] = {"position": p, "view_vector": view_vector, "view_pos": view_pos}
    scene["action_dict"] = ad
    scene["ref_obj_dicts"] = {}

    at = ad["action_type"]

    if len(ref_objs) == 1 and at != "FILL" and at != "SPAWN":
        # FIXME if loc is given in ad
        loc = maybe_speaker_look(ref_objs[0][1], view_pos, sl)
        obj = specify_object(ref_objs[0][1], sl=sl, flat=flat)
        robj_id = uuid.uuid4().hex
        scene["ref_obj_dicts"][robj_id] = ref_objs[0][1]
        if type(obj) is str:
            scene["mobs"].append({"mobname": obj, "loc": loc, "dict_id": robj_id})
            return scene
        else:
            scene["block_obj"].append({"blocks": obj, "loc": loc, "dict_id": robj_id})
            return scene

    if len(ref_objs) == 1 and at == "FILL":
        obj_loc = maybe_speaker_look(ref_objs[0][1], view_pos, sl)
        obj = specify_object(ref_objs[0][1], sl=sl, flat=flat)
        reldir = ad["location"]["relative_direction"]
        robj_id = uuid.uuid4().hex
        scene["ref_obj_dicts"][robj_id] = ref_objs[0][1]
        if type(obj) is str:
            scene["mobs"].append({"mobname": obj, "loc": obj_loc, "dict_id": robj_id})
            hole_loc = choose_loc(1, reldir, view_pos, view_vector, obj_loc, sl=sl)
            scene["holes"].append({"loc": hole_loc})
        elif obj is not None and len(obj) > 0:
            scene["block_obj"].append({"blocks": obj, "loc": obj_loc, "dict_id": robj_id})
            bounds = shapes.get_bounds(obj)
            rad = max(bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4])
            hole_loc = choose_loc(rad, reldir, view_pos, view_vector, obj_loc, sl=sl)
            scene["holes"].append({"loc": hole_loc})
        # TODO fix relative location; put this in choose_loc, input the ref_obj
        elif ref_objs[0][1].get("location")["location_type"] == "SPEAKER_LOOK":
            scene["holes"].append({"loc": view_pos.astype("int64")})
        elif ref_objs[0][1].get("location")["location_type"] == "SPEAKER_POS":
            scene["holes"].append({"loc": p.astype("int64")})

        return scene

    if len(ref_objs) == 2:
        if at == "DESTROY":
            reldir = ad["reference_object"]["location"]["relative_direction"]
            if ref_objs[0][0] == "action":
                d = ref_objs[0][1]
                r = ref_objs[1][1]
            else:
                d = ref_objs[1][1]
                r = ref_objs[0][1]
            d_id = uuid.uuid4().hex
            r_id = uuid.uuid4().hex
            scene["ref_obj_dicts"][d_id] = d
            scene["ref_obj_dicts"][r_id] = r
            to_destroy_obj = specify_object(d, sl=sl, flat=flat)
            rel_obj = specify_object(r, sl=sl, flat=flat)
            rel_obj_loc = maybe_speaker_look(r, view_pos, sl)
            if type(rel_obj) is str:
                rad = 1
                scene["mobs"].append({"mobname": rel_obj, "loc": rel_obj_loc, "dict_id": r_id})
            else:
                scene["block_obj"].append({"blocks": rel_obj, "loc": rel_obj_loc, "dict_id": r_id})
                bounds = shapes.get_bounds(rel_obj)
                rad = max(bounds[1] - bounds[0], bounds[3] - bounds[2], bounds[5] - bounds[4])
            to_destroy_loc = choose_loc(rad, reldir, view_pos, view_vector, rel_obj_loc, sl=sl)
            scene["block_obj"].append(
                {"blocks": to_destroy_obj, "loc": to_destroy_loc, "dict_id": d_id}
            )
            return scene

        # this is a copy, one ref object is to be copied, and the other gives
        # the *target* location, so locations are independent
        elif at == "BUILD":
            if ref_objs[0][0] == "location":
                c = ref_objs[1][1]
                r = ref_objs[0][1]
            else:
                c = ref_objs[0][1]
                r = ref_objs[1][1]

            c_id = uuid.uuid4().hex
            r_id = uuid.uuid4().hex
            scene["ref_obj_dicts"][c_id] = c
            scene["ref_obj_dicts"][r_id] = r
            to_copy_obj = specify_object(c, sl=sl, flat=flat)
            to_copy_obj_loc = maybe_speaker_look(c, view_pos, sl)
            rel_obj = specify_object(r, sl=sl, flat=flat)
            rel_obj_loc = np.random.randint(-sl // 2, sl // 2, size=(2))
            scene["block_obj"].append(
                {"blocks": to_copy_obj, "loc": to_copy_obj_loc, "dict_id": c_id}
            )
            if type(rel_obj) is str:
                scene["mobs"].append({"mobname": rel_obj, "loc": rel_obj_loc, "dict_id": r_id})
            else:
                scene["block_obj"].append({"blocks": rel_obj, "loc": rel_obj_loc, "dict_id": r_id})
            return scene
    return scene