Skip to content

Commit f6f191b

Browse files
committed
display only edge of the water
1 parent c339dcc commit f6f191b

File tree

1 file changed

+20
-0
lines changed
  • balrog/environments/crafter

1 file changed

+20
-0
lines changed

balrog/environments/crafter/env.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import gym
55
import numpy as np
66
from PIL import Image
7+
from scipy import ndimage
78

89
from balrog.environments import Strings
910

@@ -99,6 +100,13 @@ def distange_to_string(distance, direction):
99100
return " and ".join(desc) if desc else "at your location"
100101

101102

103+
def get_edge_items(semantic, item_idx):
104+
item_mask = semantic == item_idx
105+
not_item_mask = semantic != item_idx
106+
item_edge = ndimage.binary_dilation(not_item_mask) & item_mask
107+
return item_edge
108+
109+
102110
def describe_env(info):
103111
assert info["semantic"][info["player_pos"][0], info["player_pos"][1]] == player_idx
104112
semantic = info["semantic"][
@@ -126,12 +134,24 @@ def describe_env(info):
126134
else:
127135
obs = "You face nothing at your front."
128136

137+
# Edge detection
138+
edge_only_items = ["water"]
139+
edge_masks = {}
140+
141+
for item_name in edge_only_items:
142+
item_idx = id_to_item.index(item_name)
143+
edge_masks[item_idx] = get_edge_items(semantic, item_idx)
144+
129145
for i in range(semantic.shape[0]):
130146
for j in range(semantic.shape[1]):
131147
idx = semantic[i, j]
132148
if idx == player_idx:
133149
continue
134150

151+
# only display the edge of items that are in edge_only_items
152+
if idx in edge_masks and not edge_masks[idx][i, j]:
153+
continue
154+
135155
# skip grass, sand or path so obs is not too long
136156
if idx in [id_to_item.index(o) for o in ["grass", "sand", "path"]]:
137157
continue

0 commit comments

Comments
 (0)