Skip to content

Commit

Permalink
Merge branch 'master' of github.com:matrx-software/matrx
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjalling Haije committed Oct 25, 2021
2 parents fa0e9f7 + 5c161bd commit 829c477
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 18 deletions.
2 changes: 1 addition & 1 deletion matrx/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__url__ = 'https://matrx-software.com'
__doc_url__ = 'http://docs.matrx-software.com/en/latest/'
__source_url__ = 'https://github.com/matrx-software/matrx'
__version__ = '2.1.0'
__version__ = '2.1.1'
__author__ = 'MATRX Team at TNO.nl'
__author_email__ = '[email protected]'
__license__ = 'MIT License'
Expand Down
59 changes: 45 additions & 14 deletions matrx/agents/agent_utils/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,28 +279,59 @@ def get_all_room_names(self):
return list({obj['room_name'] for obj in rooms})

def get_room_objects(self, room_name):
# Locate method to identify room content
def is_content(obj):
if 'class_inheritance' in obj.keys():
chain = obj['class_inheritance']
if not (Wall.__name__ in chain or Door.__name__ in chain or AreaTile in chain) \
and obj['room_name'] == room_name:
return obj
else: # the object is a Wall, Door or AreaTile
return None

# Get all room objects
""" This function finds all objects in a rectengular room.
Only works for rectengular rooms.
This is done by finding all objects with the `"room_name":room_name`
property (such as walls, doors, and areatiles), getting their locations,
and finding any other objects on those locations.
All objects are returned, including walls, doors, and areatiles.
"""
# Get all room objects with the {"room_name":room_name} property
room_objs = self.get_room(room_name)

if room_objs is None: # No room with room_name was found
return None

# Filter out all area's, walls and doors
content = map(is_content, room_objs)
content = [c for c in content if c is not None]
# get locations of all room objects
room_locations = [obj['location'] for obj in room_objs]

top_left = room_locations[0]
bottom_right = room_locations[0]
for loc in room_locations:
if loc[0] < top_left[0] or loc[1] < top_left[1]:
top_left = loc
elif loc[0] > bottom_right[0] or loc[1] > bottom_right[1]:
bottom_right = loc

content = self.get_objects_in_area(top_left=top_left, width=5, height=6)#bottom_right=bottom_right)

return content

def get_objects_in_area(self, top_left, width=None, height=None, bottom_right=None):
""" Find all objects within a designated area """
# convert width and height to bottom_right coordinate if passed
if bottom_right is None:
if not width or not height:
raise Exception("Either a bottom_right coordinate, or width and height are required.")
else:
bottom_right = (top_left[0] + width, top_left[1] + height)

# Check if object within area
def within_area(obj):
if "location" not in obj.keys():
return None
# object is within the room
elif obj['location'][0] >= top_left[0] and obj['location'][1] >= top_left[1] and obj['location'][0] <= bottom_right[0] and obj['location'][1] <= bottom_right[1]:
return obj
else:
return None

# Filter out all objects not within the room
objs_in_area = map(within_area, self.__state_dict.values())
objs_in_area = [c for c in objs_in_area if c is not None]

return objs_in_area

def get_room_doors(self, room_name):
# Locate method to identify doors of the right room
def is_content(obj):
Expand Down
2 changes: 1 addition & 1 deletion matrx/cases/simple_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create_builder():

factory.add_logger(logger_class=LogActions, save_path="log_data/")

factory.add_room(top_left_location=[0, 0], width=15, height=15, name="world_bounds")
factory.add_room(top_left_location=[0, 0], width=15, height=15, name="world_bounds", door_locations=[[11,0]], door_custom_properties={"img_name": "fire.gif"})

n_agent = 1

Expand Down
5 changes: 4 additions & 1 deletion matrx/objects/agent_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ def __init__(self, location, possible_actions, sense_capability, class_callable,
"action_result": None}

# We set a placeholder for the 'team' property so that it can be found in self.properties
self.team = ""
if team is not None:
self.team = ""
else:
self.team = team

# Call the super constructor (we do this here because then we have access to all of EnvObject, including a
# unique id
Expand Down
18 changes: 17 additions & 1 deletion matrx/world_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,8 @@ def add_room(self, top_left_location, width, height, name,
door_open_colour=None,
door_closed_colour=None,
door_visualization_opacity=None,
door_custom_properties=None,
door_customizable_properties=None,
wall_visualize_colour=None, wall_visualize_opacity=None,
wall_custom_properties=None,
wall_customizable_properties=None,
Expand Down Expand Up @@ -1952,6 +1954,14 @@ def add_room(self, top_left_location, width, height, name,
door_visualization_opacity: str (optional, 1.0)
Opacity of the object, as a percentge from 0.0 (fully opaque) to 1.0 (no opacity). Defaults to 1.0.
door_custom_properties : dict (optional, default None)
A dictionary of custom properties and their values passed to every
door object.
door_customizable_properties : list (optional, default None)
A list of property names that other objects and agents are allowed
to adjust.
wall_visualize_colour : string (optional, default None)
The colour of the walls.
Expand Down Expand Up @@ -2057,6 +2067,7 @@ def add_room(self, top_left_location, width, height, name,
# those wall locations
door_locations = [] if door_locations is None else door_locations
for door_loc in door_locations:
door_loc = tuple(door_loc)
if door_loc in all_:
all_.remove(door_loc)
else:
Expand All @@ -2078,10 +2089,15 @@ def add_room(self, top_left_location, width, height, name,

# Add all doors
for door_loc in door_locations:
if door_custom_properties is None:
door_custom_properties = {"room_name": name}
else:
door_custom_properties = {**door_custom_properties, "room_name": name}
self.add_object(location=door_loc, name=f"{name} - door@{door_loc}", callable_class=Door,
open_colour=door_open_colour, closed_colour=door_closed_colour,
visualize_opacity=door_visualization_opacity,
is_open=doors_open, **{"room_name": name})
is_open=doors_open, custom_properties=door_custom_properties,
customizable_properties=door_customizable_properties)

# Add all area tiles if required
if with_area_tiles:
Expand Down

0 comments on commit 829c477

Please sign in to comment.