"""Interactive Agent that stops until commands
"""

__author__ = "Chang Gao"
__copyright__ = "Copyright (c) 2017 Malmactor"
__license__ = "MIT"

import numpy as np
import time

DEBUG = True

key_act_map = {
    "a": "left_nojump",
    "d": "right_nojump",
    "w": "nolr_jump",
    "q": "left_jump",
    "e": "right_jump",
    "s": "remains"
}

def interactive_agent(simulation, keypoller, render, config=None):

    empty_action = config["empty_action"]
    interval = config["interval"]
    key = None
    none_times = -1
    round_start = False

    pos_act_pairs = []
    postions = []
    actions = []

    while not key == "m":
        key = keypoller()

        if key and key in key_act_map:
            round_start = True
            simulation.advance_frame(key_act_map[key])
            postions.append((simulation.mario.state[0, 0], simulation.mario.state[1, 0]))
            actions.append(key_act_map[key])
            none_times = 0
            renderable = simulation.get_renderable()
            render.render(renderable)
            if DEBUG:
                print key_act_map[key]
                print (simulation.mario.state[0, 0], simulation.mario.state[1, 0])

        elif none_times < interval-1:
            simulation.advance_frame(empty_action)
            if round_start:
                postions.append((simulation.mario.state[0, 0], simulation.mario.state[1, 0]))
                actions.append(empty_action)
            none_times += 1
            if none_times == interval-1:
                if round_start:
                    pos_act_pairs.append((postions, actions))
                postions = []
                actions = []
            renderable = simulation.get_renderable()
            render.render(renderable)

    return pos_act_pairs

@@ -728,63 +728,8 @@
 rue%0A
-            simulation.advance_frame(key_act_map%5Bkey%5D)%0A
     
@@ -859,16 +859,71 @@
 p%5Bkey%5D)%0A
+            simulation.advance_frame(key_act_map%5Bkey%5D)%0A
         
@@ -1097,180 +1097,46 @@
 ey%5D%0A
-                print (simulation.mario.state%5B0, 0%5D, simulation.mario.state%5B1, 0%5D)%0A%0A        elif none_times %3C interval-1:%0A            simulation.advance_frame(empty_action)
+%0A        elif none_times %3C interval-1:
 %0A   
@@ -1299,16 +1299,67 @@
 action)%0A
+            simulation.advance_frame(empty_action)%0A
         

fix state bug