From e4e756b41a88e7d4246ed2359f62e3e61536cf4d Mon Sep 17 00:00:00 2001
From: u229589 <christian.baumberger@sbb.ch>
Date: Tue, 29 Oct 2019 08:44:50 +0100
Subject: [PATCH] only call cell_free() if new cell is inside scene, otherwise
 cell_free is False (#259)

---
 flatland/envs/rail_env.py | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/flatland/envs/rail_env.py b/flatland/envs/rail_env.py
index 32dd70e5..0887c0ca 100644
--- a/flatland/envs/rail_env.py
+++ b/flatland/envs/rail_env.py
@@ -717,9 +717,15 @@ class RailEnv(Environment):
                 (*agent.position, agent.direction),
                 new_direction)
 
-        # Check the new position is not the same as any of the existing agent positions
-        # (including itself, for simplicity, since it is moving)
-        cell_free = self.cell_free(new_position)
+
+        # only call cell_free() if new cell is inside the scene
+        if new_cell_valid:
+            # Check the new position is not the same as any of the existing agent positions
+            # (including itself, for simplicity, since it is moving)
+            cell_free = self.cell_free(new_position)
+        else:
+            # if new cell is outside of scene -> cell_free is False
+            cell_free = False
         return cell_free, new_cell_valid, new_direction, new_position, transition_valid
 
     def cell_free(self, position: IntVector2D) -> bool:
@@ -736,11 +742,7 @@ class RailEnv(Environment):
             is the cell free or not?
 
         """
-        try:
-            return not self.agent_positions[position]
-        except IndexError as error:
-            print(error)
-            return False
+        return not self.agent_positions[position]
 
     def check_action(self, agent: EnvAgent, action: RailEnvActions):
         """
-- 
GitLab