'''
standard mountain car for Python 3
NOT modified to have an extra penalty for non-zero action
'''

from pylab import random, cos

def init():
    position = -0.6 + random()*0.2
    return position, 0.0

def sample(s, a):
    position,velocity = s
    if not (a in (0,1,2)):
        print('Invalid action:', a)
        raise Exception
    r = -1 # if A==1 else -1.5
    a = a - 1
    velocity += 0.001*a - 0.0025*cos(3*position)
    if velocity < -0.07:
        velocity = -0.07
    elif velocity >= 0.07:
        velocity = 0.06999999
    position += velocity
    if position >= 0.5:
        return r,None
    if position < -1.2:
        position = -1.2
        velocity = 0.0
    return r,(position,velocity)

def terminal(s):
    return True if s is None else False


