Skip to content

Commit c339dcc

Browse files
committed
fix crafter observations
1 parent bf9e75b commit c339dcc

File tree

1 file changed

+29
-24
lines changed
  • balrog/environments/crafter

1 file changed

+29
-24
lines changed

balrog/environments/crafter/env.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,25 @@ def rotation_matrix(v1, v2):
7878

7979

8080
def describe_loc(ref, P):
81+
"""
82+
Describe the location of P relative to ref.
83+
Example: `1 step south and 4 steps west`
84+
"""
8185
desc = []
86+
87+
def distange_to_string(distance, direction):
88+
return f"{abs(distance)} step{'s' if abs(distance) > 1 else ''} {direction}"
89+
8290
if ref[1] > P[1]:
83-
desc.append("north")
91+
desc.append(distange_to_string(ref[1] - P[1], "north"))
8492
elif ref[1] < P[1]:
85-
desc.append("south")
93+
desc.append(distange_to_string(ref[1] - P[1], "south"))
8694
if ref[0] > P[0]:
87-
desc.append("west")
95+
desc.append(distange_to_string(ref[0] - P[0], "west"))
8896
elif ref[0] < P[0]:
89-
desc.append("east")
97+
desc.append(distange_to_string(ref[0] - P[0], "east"))
9098

91-
return "-".join(desc)
99+
return " and ".join(desc) if desc else "at your location"
92100

93101

94102
def describe_env(info):
@@ -99,11 +107,6 @@ def describe_env(info):
99107
]
100108
center = np.array([info["view"][0] // 2, info["view"][1] // 2 - 1])
101109
result = ""
102-
x = np.arange(semantic.shape[1])
103-
y = np.arange(semantic.shape[0])
104-
x1, y1 = np.meshgrid(x, y)
105-
loc = np.stack((y1, x1), axis=-1)
106-
dist = np.absolute(center - loc).sum(axis=-1)
107110
obj_info_list = []
108111

109112
facing = info["player_facing"]
@@ -114,27 +117,29 @@ def describe_env(info):
114117
if 0 <= target_x < max_x and 0 <= target_y < max_y:
115118
target_id = semantic[int(target_x), int(target_y)]
116119
target_item = id_to_item[target_id]
120+
121+
# skip grass, sand or path so obs here, since we are not displaying them
122+
if target_id in [id_to_item.index(o) for o in ["grass", "sand", "path"]]:
123+
target_item = "nothing"
124+
117125
obs = "You face {} at your front.".format(target_item)
118126
else:
119127
obs = "You face nothing at your front."
120128

121-
for idx in np.unique(semantic):
122-
if idx == player_idx:
123-
continue
129+
for i in range(semantic.shape[0]):
130+
for j in range(semantic.shape[1]):
131+
idx = semantic[i, j]
132+
if idx == player_idx:
133+
continue
134+
135+
# skip grass, sand or path so obs is not too long
136+
if idx in [id_to_item.index(o) for o in ["grass", "sand", "path"]]:
137+
continue
124138

125-
smallest = np.unravel_index(np.argmin(np.where(semantic == idx, dist, np.inf)), semantic.shape)
126-
obj_info_list.append(
127-
(
128-
id_to_item[idx],
129-
dist[smallest],
130-
describe_loc(np.array([0, 0]), smallest - center),
131-
)
132-
)
139+
obj_info_list.append((id_to_item[idx], describe_loc(np.array([0, 0]), np.array([i, j]) - center)))
133140

134141
if len(obj_info_list) > 0:
135-
status_str = "You see:\n{}".format(
136-
"\n".join(["- {} {} steps to your {}".format(name, dist, loc) for name, dist, loc in obj_info_list])
137-
)
142+
status_str = "You see:\n{}".format("\n".join(["- {} {}".format(name, loc) for name, loc in obj_info_list]))
138143
else:
139144
status_str = "You see nothing away from you."
140145
result += status_str + "\n\n"

0 commit comments

Comments
 (0)