mirror of
https://github.com/illiumst/marl-factory-grid.git
synced 2025-09-15 23:37:14 +02:00
all vars are properties, removed comments
This commit is contained in:
@@ -5,10 +5,8 @@ import numpy as np
|
||||
|
||||
from .object import Object
|
||||
from .. import constants as c
|
||||
from .object import EnvObject
|
||||
from ...utils.utility_classes import RenderEntity
|
||||
from ...utils.render import RenderEntity
|
||||
from ...utils.results import ActionResult
|
||||
from ...utils.utility_classes import RenderEntity
|
||||
|
||||
|
||||
class Entity(Object, abc.ABC):
|
||||
@@ -152,7 +150,6 @@ class Entity(Object, abc.ABC):
|
||||
pos = tuple(pos)
|
||||
try:
|
||||
return self.state.entities.pos_dict[pos]
|
||||
# return next(e for e in self if e.pos == pos)
|
||||
except StopIteration:
|
||||
pass
|
||||
except ValueError:
|
||||
|
@@ -13,7 +13,7 @@ class Object:
|
||||
return True
|
||||
|
||||
@property
|
||||
def var_has_position(self): # brauchen wir das hier jetzt?
|
||||
def var_has_position(self):
|
||||
try:
|
||||
return self.pos != c.VALUE_NO_POS or False
|
||||
except AttributeError:
|
||||
|
31
marl_factory_grid/environment/entity/wall.py
Normal file
31
marl_factory_grid/environment/entity/wall.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from typing import List
|
||||
|
||||
import numpy as np
|
||||
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.environment.entity.entity import Entity
|
||||
from marl_factory_grid.environment.entity.object import EnvObject
|
||||
from marl_factory_grid.utils.utility_classes import RenderEntity
|
||||
from marl_factory_grid.utils import helpers as h
|
||||
|
||||
|
||||
class Wall(Entity):
|
||||
|
||||
@property
|
||||
def var_can_collide(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
return c.VALUE_OCCUPIED_CELL
|
||||
|
||||
def render(self):
|
||||
return RenderEntity(c.WALL, self.pos)
|
||||
|
||||
@property
|
||||
def var_is_blocking_pos(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def var_is_blocking_light(self):
|
||||
return True
|
@@ -1,121 +0,0 @@
|
||||
from typing import List
|
||||
|
||||
import numpy as np
|
||||
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.environment.entity.entity import Entity
|
||||
from marl_factory_grid.environment.entity.object import EnvObject
|
||||
from marl_factory_grid.utils.utility_classes import RenderEntity
|
||||
from marl_factory_grid.utils import helpers as h
|
||||
|
||||
|
||||
class Floor(EnvObject):
|
||||
|
||||
@property
|
||||
def var_has_position(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def var_can_collide(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_can_move(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_is_blocking_pos(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_is_blocking_light(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
return c.VALUE_OCCUPIED_CELL
|
||||
|
||||
# @property
|
||||
# def guests_that_can_collide(self):
|
||||
# return [x for x in self.guests if x.var_can_collide]
|
||||
|
||||
@property
|
||||
def guests(self):
|
||||
return self._guests.values()
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self.pos[0]
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self.pos[1]
|
||||
|
||||
@property
|
||||
def is_blocked(self):
|
||||
return any([x.var_is_blocking_pos for x in self.guests])
|
||||
|
||||
def __init__(self, pos, **kwargs):
|
||||
super(Floor, self).__init__(**kwargs)
|
||||
self._guests = dict()
|
||||
self.pos = tuple(pos)
|
||||
self._neighboring_floor: List[Floor] = list()
|
||||
self._blocked_by = None
|
||||
|
||||
def __len__(self):
|
||||
return len(self._guests)
|
||||
|
||||
def is_empty(self):
|
||||
return not len(self._guests)
|
||||
|
||||
def is_occupied(self):
|
||||
return bool(len(self._guests))
|
||||
|
||||
def enter(self, guest, spawn=False):
|
||||
same_pos = guest.name not in self._guests
|
||||
not_blocked = not self.is_blocked
|
||||
no_become_blocked_when_occupied = not (guest.var_is_blocking_pos and self.is_occupied())
|
||||
not_introduce_collision = not (spawn and guest.var_can_collide and any(x.var_can_collide for x in self.guests))
|
||||
if same_pos and not_blocked and no_become_blocked_when_occupied and not_introduce_collision:
|
||||
self._guests.update({guest.name: guest})
|
||||
return c.VALID
|
||||
else:
|
||||
return c.NOT_VALID
|
||||
|
||||
def leave(self, guest):
|
||||
try:
|
||||
del self._guests[guest.name]
|
||||
except (ValueError, KeyError):
|
||||
return c.NOT_VALID
|
||||
return c.VALID
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.name}(@{self.pos})'
|
||||
|
||||
def summarize_state(self, **_):
|
||||
return dict(name=self.name, x=int(self.x), y=int(self.y))
|
||||
|
||||
def render(self):
|
||||
return None
|
||||
|
||||
|
||||
class Wall(Entity):
|
||||
|
||||
@property
|
||||
def var_can_collide(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def encoding(self):
|
||||
return c.VALUE_OCCUPIED_CELL
|
||||
|
||||
def render(self):
|
||||
return RenderEntity(c.WALL, self.pos)
|
||||
|
||||
@property
|
||||
def var_is_blocking_pos(self):
|
||||
return True
|
||||
|
||||
@property
|
||||
def var_is_blocking_light(self):
|
||||
return True
|
@@ -4,13 +4,29 @@ from marl_factory_grid.environment.entity.object import EnvObject
|
||||
|
||||
class Collection(Objects):
|
||||
_entity = EnvObject
|
||||
var_is_blocking_light: bool = False
|
||||
var_can_collide: bool = False
|
||||
var_can_move: bool = False
|
||||
|
||||
var_has_position: bool = False # alles was posmixin hat true
|
||||
var_has_bound = False # batteries, globalpos, inventories true
|
||||
var_can_be_bound: bool = False # == ^
|
||||
@property
|
||||
def var_is_blocking_light(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_can_collide(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_can_move(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def var_has_position(self):
|
||||
return False # alles was posmixin hat true
|
||||
@property
|
||||
def var_has_bound(self):
|
||||
return False # batteries, globalpos, inventories true
|
||||
|
||||
@property # beide bounds hier? inventory can be bound
|
||||
def var_can_be_bound(self):
|
||||
return False
|
||||
|
||||
@property
|
||||
def encodings(self):
|
||||
|
@@ -33,9 +33,6 @@ class Entities(Objects):
|
||||
self.pos_dict = defaultdict(list)
|
||||
super().__init__()
|
||||
|
||||
# def all_floors(self):
|
||||
# return[key for key, val in self.pos_dict.items() if any('floor' in x.name.lower() for x in val)]
|
||||
|
||||
def guests_that_can_collide(self, pos):
|
||||
return[x for val in self.pos_dict[pos] for x in val if x.var_can_collide]
|
||||
|
||||
@@ -91,8 +88,6 @@ class Entities(Objects):
|
||||
|
||||
def by_pos(self, pos: (int, int)):
|
||||
return self.pos_dict[pos]
|
||||
# found_entities = [y for y in (x.by_pos(pos) for x in self.values() if hasattr(x, 'by_pos')) if y is not None]
|
||||
# return found_entities
|
||||
|
||||
@property
|
||||
def positions(self):
|
||||
|
@@ -1,10 +1,7 @@
|
||||
from typing import List, Tuple
|
||||
|
||||
import numpy as np
|
||||
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.environment.entity.entity import Entity
|
||||
from marl_factory_grid.environment.entity.wall_floor import Floor
|
||||
|
||||
|
||||
class PositionMixin:
|
||||
@@ -39,7 +36,6 @@ class PositionMixin:
|
||||
pos = tuple(pos)
|
||||
try:
|
||||
return self.pos_dict[pos]
|
||||
# return next(e for e in self if e.pos == pos)
|
||||
except StopIteration:
|
||||
pass
|
||||
except ValueError:
|
||||
|
@@ -54,7 +54,6 @@ class Objects:
|
||||
assert self._data[item.name] is None, f'{item.name} allready exists!!!'
|
||||
self._data.update({item.name: item})
|
||||
item.set_collection(self)
|
||||
# self.notify_add_entity(item)
|
||||
for observer in self.observers:
|
||||
observer.notify_add_entity(item)
|
||||
return self
|
||||
|
@@ -1,10 +1,7 @@
|
||||
import random
|
||||
from typing import List, Tuple
|
||||
|
||||
from marl_factory_grid.environment import constants as c
|
||||
from marl_factory_grid.environment.entity.wall import Wall
|
||||
from marl_factory_grid.environment.groups.collection import Collection
|
||||
from marl_factory_grid.environment.groups.mixins import PositionMixin
|
||||
from marl_factory_grid.environment.entity.wall_floor import Wall, Floor
|
||||
|
||||
|
||||
class Walls(PositionMixin, Collection):
|
||||
@@ -28,16 +25,3 @@ class Walls(PositionMixin, Collection):
|
||||
return super().by_pos(pos)[0]
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
|
||||
class Floors(Walls):
|
||||
_entity = Floor
|
||||
symbol = c.SYMBOL_FLOOR
|
||||
var_is_blocking_light: bool = False
|
||||
var_can_collide: bool = False
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Floors, self).__init__(*args, **kwargs)
|
||||
self._value = c.VALUE_FREE_CELL
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user