diff --git a/agents.py b/agents.py index 084a752e1..6ab9ea814 100644 --- a/agents.py +++ b/agents.py @@ -27,11 +27,6 @@ """ # TODO -# Implement grabbing correctly. -# When an object is grabbed, does it still have a location? -# What if it is released? -# What if the grabbed or the grabber is deleted? -# What if the grabber moves? # Speed control in GUI does not have any effect -- fix it. from utils import distance_squared, turn_heading @@ -510,14 +505,17 @@ def execute_action(self, agent, action): agent.direction += Direction.L elif action == 'Forward': agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location)) - # elif action == 'Grab': - # things = [thing for thing in self.list_things_at(agent.location) - # if agent.can_grab(thing)] - # if things: - # agent.holding.append(things[0]) + elif action == 'Grab': + things = [thing for thing in self.list_things_at(agent.location) if agent.can_grab(thing)] + if things: + agent.holding.append(things[0]) + print("Grabbing ", things[0].__class__.__name__) + self.delete_thing(things[0]) elif action == 'Release': if agent.holding: - agent.holding.pop() + dropped = agent.holding.pop() + print("Dropping ", dropped.__class__.__name__) + self.add_thing(dropped, location=agent.location) def default_location(self, thing): location = self.random_location_inbounds() @@ -569,10 +567,7 @@ def random_location_inbounds(self, exclude=None): def delete_thing(self, thing): """Deletes thing, and everything it is holding (if thing is an agent)""" if isinstance(thing, Agent): - for obj in thing.holding: - super().delete_thing(obj) - for obs in self.observers: - obs.thing_deleted(obj) + del thing.holding super().delete_thing(thing) for obs in self.observers: @@ -964,24 +959,10 @@ def execute_action(self, agent, action): if isinstance(agent, Explorer) and self.in_danger(agent): return - + agent.bump = False - if action == 'TurnRight': - agent.direction += Direction.R - agent.performance -= 1 - elif action == 'TurnLeft': - agent.direction += Direction.L - agent.performance -= 1 - elif action == 'Forward': - agent.bump = self.move_to(agent, agent.direction.move_forward(agent.location)) - agent.performance -= 1 - elif action == 'Grab': - things = [thing for thing in self.list_things_at(agent.location) - if agent.can_grab(thing)] - if len(things): - print("Grabbing", things[0].__class__.__name__) - if len(things): - agent.holding.append(things[0]) + if action in ['TurnRight', 'TurnLeft', 'Forward', 'Grab']: + super().execute_action(agent, action) agent.performance -= 1 elif action == 'Climb': if agent.location == (1, 1): # Agent can only climb out of (1,1)