in randomized_uncertain_social_preferences/rusp/env_oasis.py [0:0]
def step(self, action):
attack_matrix = np.zeros((self.n_agents, self.n_agents), dtype=bool)
for i in range(self.n_agents):
target_actors = self._get_target_actor(i, action)
if len(target_actors):
# See if the targeted agent can be attacked (in range and in front)
aa_ranges = np.linalg.norm(self.previous_obs['agent_pos'][i] - self.previous_obs['agent_pos'][target_actors], axis=1)
in_range = aa_ranges < self.attack_range
in_front = self.previous_obs['mask_aa_obs'][i, target_actors]
able_to_attack = np.logical_and(in_range, in_front) if self.only_attack_in_front else in_range
if np.any(able_to_attack):
# Filter down to those that are in range and in front
target_actors = target_actors[able_to_attack]
aa_ranges = aa_ranges[able_to_attack]
# Only attack the closest agent to you
target_actor = target_actors[np.argsort(aa_ranges)[0]]
attack_matrix[i, target_actor] = 1
self.attacked_me = attack_matrix.T
self.attack_counts += np.sum(attack_matrix, 1)
# Compute health updates
health_deltas = np.zeros((self.n_agents, self.n_agents))
health_deltas[self.attacked_me] += self.attack_damage
health_deltas = np.sum(health_deltas, 1)
# Turn off the eat action if you were attacked
if self.mask_eat_if_attacked:
action['action_eat_food'] *= ~np.any(self.attacked_me, 1, keepdims=True)
obs, rew, done, info = self.env.step(action)
info['health_delta'] += health_deltas
self.previous_obs = self.observation(obs)
if done:
info['n_attacks'] = np.sum(self.attack_counts)
info['n_attacks_per_agent'] = np.mean(self.attack_counts)
return self.previous_obs, rew, done, info