From 14c0460013306ed94af1494727b44020d60c6561 Mon Sep 17 00:00:00 2001
From: hagrid67 <jdhwatson@gmail.com>
Date: Mon, 27 May 2019 18:20:09 +0100
Subject: [PATCH] changed renderer to use agent.old_position instead of
 actions, also implies that agents are drawn in their old locations, ie from
 previous timestep

---
 examples/play_model.py         |  2 +-
 flatland/envs/agent_utils.py   |  8 ++++++--
 flatland/envs/rail_env.py      |  3 ++-
 flatland/utils/graphics_pil.py |  2 +-
 flatland/utils/rendertools.py  | 33 +++++++++------------------------
 5 files changed, 19 insertions(+), 29 deletions(-)

diff --git a/examples/play_model.py b/examples/play_model.py
index 08dadeb..5e2d898 100644
--- a/examples/play_model.py
+++ b/examples/play_model.py
@@ -277,4 +277,4 @@ def main_old(render=True, delay=0.0):
 
 
 if __name__ == "__main__":
-    main(render=True, delay=0.5)
+    main(render=True, delay=0)
diff --git a/flatland/envs/agent_utils.py b/flatland/envs/agent_utils.py
index db7f9ae..a4dc696 100644
--- a/flatland/envs/agent_utils.py
+++ b/flatland/envs/agent_utils.py
@@ -66,14 +66,18 @@ class EnvAgent(EnvAgentStatic):
     """
     handle = attrib(default=None)
     old_direction = attrib(default=None)
+    old_position = attrib(default=None)
 
-    def __init__(self, position, direction, target, handle, old_direction):
+    def __init__(self, position, direction, target, handle, old_direction, old_position):
         super(EnvAgent, self).__init__(position, direction, target)
         self.handle = handle
         self.old_direction = old_direction
+        self.old_position = old_position
 
     def to_list(self):
-        return [self.position, self.direction, self.target, self.handle, self.old_direction]
+        return [
+            self.position, self.direction, self.target, self.handle, 
+            self.old_direction, self.old_position]
 
     @classmethod
     def from_static(cls, oStatic):
diff --git a/flatland/envs/rail_env.py b/flatland/envs/rail_env.py
index 89feaab..a23b6ac 100644
--- a/flatland/envs/rail_env.py
+++ b/flatland/envs/rail_env.py
@@ -263,8 +263,9 @@ class RailEnv(Environment):
                     # performed
                     # self.agents_position[i] = new_position
                     # self.agents_direction[i] = new_direction
-                    agent.position = new_position
                     agent.old_direction = agent.direction
+                    agent.old_position = agent.position
+                    agent.position = new_position
                     agent.direction = new_direction
                 else:
                     # the action was not valid, add penalty
diff --git a/flatland/utils/graphics_pil.py b/flatland/utils/graphics_pil.py
index 294251e..8471881 100644
--- a/flatland/utils/graphics_pil.py
+++ b/flatland/utils/graphics_pil.py
@@ -325,7 +325,7 @@ class PILSVG(PILGL):
                     rgbaZug3[xy_color_mask, 0:3] = tnNewColor
                     self.dPilZug[(iDirIn2, iDirOut2, iColor)] = Image.fromarray(rgbaZug3)
 
-    def setAgentAt(self, iAgent, row, col, iDirIn, iDirOut, color=None):
+    def setAgentAt(self, iAgent, row, col, iDirIn, iDirOut):
         delta_dir = (iDirOut - iDirIn) % 4
         iColor = iAgent % self.nAgentColors
         # when flipping direction at a dead end, use the "iDirOut" direction.
diff --git a/flatland/utils/rendertools.py b/flatland/utils/rendertools.py
index 03c1c63..a8f9f1c 100644
--- a/flatland/utils/rendertools.py
+++ b/flatland/utils/rendertools.py
@@ -757,35 +757,20 @@ class RenderTool(object):
                     binTrans = env.rail.grid[r, c]
                     self.gl.setRailAt(r, c, binTrans)
 
-        cmap = self.gl.get_cmap('hsv',
-                                lut=max(len(self.env.agents), len(self.env.agents_static) + 1))
-
         for iAgent, agent in enumerate(self.env.agents):
             if agent is None:
                 continue
 
-            oColor = self.gl.adaptColor(cmap(iAgent))
-
-            new_direction = agent.direction
-            action_isValid = False
-
-            if iAgent in action_dict:
-                iAction = action_dict[iAgent]
-                new_direction, action_isValid = self.env.check_action(agent, iAction)
-
-            # ** TODO ***
-            # why should we only update if the action is valid ?
-            if True:
-                if action_isValid:
-                    self.gl.setAgentAt(iAgent, *agent.position, agent.direction, new_direction, color=oColor)
-                else:
-                    # pass
-                    print("invalid action - agent ", iAgent, " bend ", agent.direction, new_direction)
-                    self.gl.setAgentAt(iAgent, *agent.position, agent.direction, new_direction)
+            if agent.old_position is not None:
+                position = agent.old_position
+                direction = agent.direction
+                old_direction = agent.old_direction
             else:
-                print("invalid action - agent ", iAgent, " bend ", agent.direction, new_direction)
-                self.gl.setAgentAt(iAgent, *agent.position, agent.direction, new_direction, color=oColor)
-                # self.gl.setAgentAt(iAgent, *agent.position, agent.direction, new_direction)
+                position = agent.position
+                direction = agent.direction
+                old_direction = agent.direction
+            
+            self.gl.setAgentAt(iAgent, *position, old_direction, direction)
 
         if show:
             self.gl.show()
-- 
GitLab