new rules, new spawn logic, small fixes, default and narrow corridor debugged

This commit is contained in:
Steffen Illium
2023-11-09 17:50:20 +01:00
parent 9b9c6e0385
commit 06a5130b25
67 changed files with 768 additions and 921 deletions

View File

@@ -1,4 +1,5 @@
from marl_factory_grid.environment.entity.entity import Entity
from marl_factory_grid.utils import Result
from marl_factory_grid.utils.utility_classes import RenderEntity
from marl_factory_grid.environment import constants as c
@@ -41,21 +42,6 @@ class Door(Entity):
def str_state(self):
return 'open' if self.is_open else 'closed'
def __init__(self, *args, closed_on_init=True, auto_close_interval=10, **kwargs):
self._status = d.STATE_CLOSED
super(Door, self).__init__(*args, **kwargs)
self.auto_close_interval = auto_close_interval
self.time_to_close = 0
if not closed_on_init:
self._open()
else:
self._close()
def summarize_state(self):
state_dict = super().summarize_state()
state_dict.update(state=str(self.str_state), time_to_close=int(self.time_to_close))
return state_dict
@property
def is_closed(self):
return self._status == d.STATE_CLOSED
@@ -68,6 +54,25 @@ class Door(Entity):
def status(self):
return self._status
@property
def time_to_close(self):
return self._time_to_close
def __init__(self, *args, closed_on_init=True, auto_close_interval=10, **kwargs):
self._status = d.STATE_CLOSED
super(Door, self).__init__(*args, **kwargs)
self._auto_close_interval = auto_close_interval
self._time_to_close = 0
if not closed_on_init:
self._open()
else:
self._close()
def summarize_state(self):
state_dict = super().summarize_state()
state_dict.update(state=str(self.str_state), time_to_close=self.time_to_close)
return state_dict
def render(self):
name, state = 'door_open' if self.is_open else 'door_closed', 'blank'
return RenderEntity(name, self.pos, 1, 'none', state, self.u_int + 1)
@@ -80,18 +85,35 @@ class Door(Entity):
return c.VALID
def tick(self, state):
if self.is_open and len(state.entities.pos_dict[self.pos]) == 2 and self.time_to_close:
self.time_to_close -= 1
return c.NOT_VALID
elif self.is_open and not self.time_to_close and len(state.entities.pos_dict[self.pos]) == 2:
self.use()
return c.VALID
# Check if no entity is standing in the door
if len(state.entities.pos_dict[self.pos]) <= 2:
if self.is_open and self.time_to_close:
self._decrement_timer()
return Result(f"{d.DOOR}_tick", c.VALID, entity=self)
elif self.is_open and not self.time_to_close:
self.use()
return Result(f"{d.DOOR}_closed", c.VALID, entity=self)
else:
# No one is in door, but it is closed... Nothing to do....
return None
else:
return c.NOT_VALID
# Entity is standing in the door, reset timer
self._reset_timer()
return Result(f"{d.DOOR}_reset", c.VALID, entity=self)
def _open(self):
self._status = d.STATE_OPEN
self.time_to_close = self.auto_close_interval
self._reset_timer()
return True
def _close(self):
self._status = d.STATE_CLOSED
return True
def _decrement_timer(self):
self._time_to_close -= 1
return True
def _reset_timer(self):
self._time_to_close = self._auto_close_interval
return True

View File

@@ -18,8 +18,10 @@ class Doors(Collection):
super(Doors, self).__init__(*args, can_collide=True, **kwargs)
def tick_doors(self, state):
result_dict = dict()
results = list()
for door in self:
did_tick = door.tick(state)
result_dict.update({door.name: did_tick})
return result_dict
tick_result = door.tick(state)
if tick_result is not None:
results.append(tick_result)
# TODO: Should return a Result object, not a random dict.
return results

View File

@@ -19,10 +19,10 @@ class DoorAutoClose(Rule):
def tick_step(self, state):
if doors := state[d.DOORS]:
doors_tick_result = doors.tick_doors(state)
doors_that_ticked = [key for key, val in doors_tick_result.items() if val]
state.print(f'{doors_that_ticked} were auto-closed'
if doors_that_ticked else 'No Doors were auto-closed')
doors_tick_results = doors.tick_doors(state)
doors_that_closed = [x.entity.name for x in doors_tick_results if 'closed' in x.identifier]
door_str = doors_that_closed if doors_that_closed else "No Doors"
state.print(f'{door_str} were auto-closed')
return [TickResult(self.name, validity=c.VALID, value=1)]
state.print('There are no doors, but you loaded the corresponding Module')
return []