from itertools import cycle
import random
import sys
from Tilecoder import numTilings, tilecode, numTiles, tiles
import pygame
from pygame.locals import *
from pylab import *
import random


FPS = 30
SCREENWIDTH  = 280
SCREENHEIGHT = 512
# amount by which base can maximum shift to left
PIPEGAPSIZE  = 100 # gap between upper and lower part of pipe
BASEY        = SCREENHEIGHT * 0.79
# image, sound and hitmask  dicts
IMAGES, SOUNDS, HITMASKS = {}, {}, {}
SHARED = {}

# list of all possible players (tuple of 3 positions of flap)
PLAYERS_LIST = (
    # red bird
    (
        'assets/sprites/redbird-upflap.png',
        'assets/sprites/redbird-midflap.png',
        'assets/sprites/redbird-downflap.png',
    ),
    # blue bird
    (
        # amount by which base can maximum shift to left
        'assets/sprites/bluebird-upflap.png',
        'assets/sprites/bluebird-midflap.png',
        'assets/sprites/bluebird-downflap.png',
    ),
    # yellow bird
    (
        'assets/sprites/yellowbird-upflap.png',
        'assets/sprites/yellowbird-midflap.png',
        'assets/sprites/yellowbird-downflap.png',
    ),
)

# list of backgrounds
BACKGROUNDS_LIST = (
    'assets/sprites/background-day.png',
    'assets/sprites/background-night.png',
)

# list of pipes
PIPES_LIST = (
    'assets/sprites/pipe-green.png',
    'assets/sprites/pipe-red.png',
)


def start():
    global SCREEN, FPSCLOCK
    pygame.init()
    FPSCLOCK = pygame.time.Clock()
    SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
    pygame.display.set_caption('Flappy Bird')

    # numbers sprites for score display
    IMAGES['numbers'] = (
        pygame.image.load('assets/sprites/0.png').convert_alpha(),
        pygame.image.load('assets/sprites/1.png').convert_alpha(),
        pygame.image.load('assets/sprites/2.png').convert_alpha(),
        pygame.image.load('assets/sprites/3.png').convert_alpha(),
        pygame.image.load('assets/sprites/4.png').convert_alpha(),
        pygame.image.load('assets/sprites/5.png').convert_alpha(),
        pygame.image.load('assets/sprites/6.png').convert_alpha(),
        pygame.image.load('assets/sprites/7.png').convert_alpha(),
        pygame.image.load('assets/sprites/8.png').convert_alpha(),
        pygame.image.load('assets/sprites/9.png').convert_alpha()
    )

    # game over sprite
    IMAGES['gameover'] = pygame.image.load('assets/sprites/gameover.png').convert_alpha()
    # message sprite for welcome screen
    IMAGES['message'] = pygame.image.load('assets/sprites/message.png').convert_alpha()
    # base (ground) sprite
    IMAGES['base'] = pygame.image.load('assets/sprites/base.png').convert_alpha()

    # sounds
    if 'win' in sys.platform:
        soundExt = '.wav'
    else:
        soundExt = '.ogg'

    SOUNDS['die']    = pygame.mixer.Sound('assets/audio/die' + soundExt)
    SOUNDS['hit']    = pygame.mixer.Sound('assets/audio/hit' + soundExt)
    SOUNDS['point']  = pygame.mixer.Sound('assets/audio/point' + soundExt)
    SOUNDS['swoosh'] = pygame.mixer.Sound('assets/audio/swoosh' + soundExt)
    SOUNDS['wing']   = pygame.mixer.Sound('assets/audio/wing' + soundExt)

        # select random background sprites
    randBg = random.randint(0, len(BACKGROUNDS_LIST) - 1)
    IMAGES['background'] = pygame.image.load(BACKGROUNDS_LIST[randBg]).convert()

        # select random player sprites
    randPlayer = random.randint(0, len(PLAYERS_LIST) - 1)
    IMAGES['player'] = (
    pygame.image.load(PLAYERS_LIST[randPlayer][0]).convert_alpha(),
    pygame.image.load(PLAYERS_LIST[randPlayer][1]).convert_alpha(),
    pygame.image.load(PLAYERS_LIST[randPlayer][2]).convert_alpha(),
    )

        # select random pipe sprites
    pipeindex = random.randint(0, len(PIPES_LIST) - 1)
    IMAGES['pipe'] = (
    pygame.transform.rotate(
    pygame.image.load(PIPES_LIST[pipeindex]).convert_alpha(), 180),
    pygame.image.load(PIPES_LIST[pipeindex]).convert_alpha(),
    )

        # hismask for pipes
    HITMASKS['pipe'] = (
    getHitmask(IMAGES['pipe'][0]),
    getHitmask(IMAGES['pipe'][1]),
    )

        # hitmask for player
    HITMASKS['player'] = (
    getHitmask(IMAGES['player'][0]),
    getHitmask(IMAGES['player'][1]),
    getHitmask(IMAGES['player'][2]),
    )
    
    movementInfo = showWelcomeAnimation()
    
    SHARED['score'] = 0
    SHARED['playerIndex'] = 0
    SHARED['loopIter'] = 0
    SHARED['playerIndexGen'] = movementInfo['playerIndexGen']
    SHARED['playerx'] = int(SCREENWIDTH * 0.2)
    SHARED['playery'] = movementInfo['playery']
    SHARED['basex'] = movementInfo['basex']
    SHARED['baseShift'] = IMAGES['base'].get_width() - IMAGES['background'].get_width()
    SHARED['newPipe'] = getRandomPipe()
    SHARED['upperPipes'] = [{'x': SCREENWIDTH + 20, 'y': SHARED['newPipe'][0]['y']}]
    SHARED['lowerPipes'] = [{'x': SCREENWIDTH + 20, 'y': SHARED['newPipe'][1]['y']}]
    SHARED['pipeVelX'] = -5
    SHARED['playerVelY'] = -9
    SHARED['playerMaxVelY'] = 20
    SHARED['playerAccY'] = 3
    SHARED['playerFlapAcc'] = -7
    SHARED['playerFlapped'] = False
    SHARED['flap'] = 1
    SHARED['birdY'] = 244
    SHARED['pipeX'] = 300
    SHARED['pipeY'] = 0
    SHARED['reward'] = 1
    SHARED['state'] = (None,None)
    return movementInfo
    
def mainGame(movementInfo,SHARED):
    score            = SHARED['score']
    playerIndex      = SHARED['playerIndex']
    loopIter         = SHARED['loopIter']    
    playerIndexGen   = SHARED['playerIndexGen']
    playerx, playery = SHARED['playerx'],SHARED['playery']

    basex     = SHARED['basex']
    baseShift = SHARED['baseShift']

    # get 2 new pipes to add to upperPipes lowerPipes list
    newPipe = SHARED['newPipe']

    pipeVelX = SHARED['pipeVelX']

    # player velocity, max velocity, downward accleration, accleration on flap
    playerVelY    =  SHARED['playerVelY']   # player's velocity along Y, default same as playerFlapped
    playerMaxVelY =  SHARED['playerMaxVelY']   # max vel along Y, max descend speed
    playerAccY    =  SHARED['playerAccY']   # players downward accleration
    playerFlapAcc =  SHARED['playerFlapAcc']   # players speed on flapping
    playerFlapped =  SHARED['playerFlapped'] # True when player flaps
    flap          =  SHARED['flap']

#    while True:
    if(flap==1):
        if playery > -2 * IMAGES['player'][0].get_height():
            playerVelY = playerFlapAcc
            playerFlapped = True
            SHARED['playerVelY'] = playerVelY
            SHARED['playerFlapped'] = True
                    
                    
        # check for crash here
    crashTest = checkCrash({'x': playerx, 'y': playery, 'index': playerIndex},
                SHARED['upperPipes'], SHARED['lowerPipes'])
    if crashTest[0]:
        SHARED['reward'] = -1000#-abs(SHARED['playery']-SHARED['pipeY'])
        
        return {
        'y': playery,
        'groundCrash': crashTest[1],
        'basex': basex,
        'upperPipes': SHARED['upperPipes'],
        'lowerPipes': SHARED['lowerPipes'],
        'score': score,
        'playerVelY': playerVelY,
        }

        # check for score
    playerMidPos = playerx + IMAGES['player'][0].get_width() / 2
    for pipe in SHARED['upperPipes']:
        pipeMidPos = pipe['x'] + IMAGES['pipe'][0].get_width() / 2
        if pipeMidPos <= playerMidPos < pipeMidPos + 4:
            score += 1
            SHARED['score'] += 1
#                SOUNDS['point'].play()

        # playerIndex basex change
    if (loopIter + 1) % 3 == 0:
        playerIndex = playerIndexGen.next()
        SHARED['playerIndex'] = playerIndex
    loopIter = (loopIter + 1) % 30
    basex = -((-basex + 100) % baseShift)
    SHARED['loopIter'] = loopIter
    SHARED['basex'] = basex

        # player's movement
    if playerVelY < playerMaxVelY and not playerFlapped:
        playerVelY += playerAccY
        SHARED['playerVelY'] = playerVelY
    if playerFlapped:
        playerFlapped = False
        SHARED['playerFlapped'] = False
    playerHeight = IMAGES['player'][playerIndex].get_height()
    playery += min(playerVelY, BASEY - playery - playerHeight)
        
    playery = max(0,playery)
    SHARED['playery'] = playery

        # move pipes to left
    for uPipe, lPipe in zip(SHARED['upperPipes'], SHARED['lowerPipes']):
        uPipe['x'] += pipeVelX
        lPipe['x'] += pipeVelX

        # add new pipe when first pipe is about to touch left of screen
    if 0 < SHARED['upperPipes'][0]['x'] < 10:
        SHARED['newPipe'] = getRandomPipe()
        SHARED['upperPipes'].append(newPipe[0])
        SHARED['lowerPipes'].append(newPipe[1])

        # remove first pipe if its out of the screen
    if SHARED['upperPipes'][0]['x'] < -IMAGES['pipe'][0].get_width():
        SHARED['upperPipes'].pop(0)
        SHARED['lowerPipes'].pop(0)

        # draw sprites
    SCREEN.blit(IMAGES['background'], (0,0))

    for uPipe, lPipe in zip(SHARED['upperPipes'], SHARED['lowerPipes']):
        SCREEN.blit(IMAGES['pipe'][0], (uPipe['x'], uPipe['y']))
        SCREEN.blit(IMAGES['pipe'][1], (lPipe['x'], lPipe['y']))

    SCREEN.blit(IMAGES['base'], (basex, BASEY))
        # print score so player overlaps the score
    showScore(score)
    SCREEN.blit(IMAGES['player'][playerIndex], (playerx, playery))
    SHARED['birdY'] = playery
    SHARED['pipeX'] = lPipe['x']
    SHARED['pipeY'] = lPipe['y']
    
        #print flap
        
    pygame.display.update()
    FPSCLOCK.tick(FPS)
    SHARED['reward'] = 1
    SHARED['state'] = (SHARED['pipeX'],SHARED['pipeY']-SHARED['playery'])
    return 1
        

def showWelcomeAnimation():
    """Shows welcome screen animation of flappy bird"""
    # index of player to blit on screen
    playerIndex = 0
    playerIndexGen = cycle([0, 1, 2, 1])
    # iterator used to change playerIndex after every 5th iteration
    loopIter = 0

    playerx = int(SCREENWIDTH * 0.2)
    playery = int((SCREENHEIGHT - IMAGES['player'][0].get_height()) / 2)

    messagex = int((SCREENWIDTH - IMAGES['message'].get_width()) / 2)
    messagey = int(SCREENHEIGHT * 0.12)

    basex = 0
    # amount by which base can maximum shift to left
    baseShift = IMAGES['base'].get_width() - IMAGES['background'].get_width()

    # player shm for up-down motion on welcome screen
    playerShmVals = {'val': 0, 'dir': 1}
    
    return {'playery': playery + playerShmVals['val'],
            'basex': basex,
            'playerIndexGen': playerIndexGen,}

    while True:
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP):
                # make first flap sound and return values for mainGame
                SOUNDS['wing'].play()
                return {
                    'playery': playery + playerShmVals['val'],
                    'basex': basex,
                    'playerIndexGen': playerIndexGen,
                }

        # adjust playery, playerIndex, basex
        if (loopIter + 1) % 5 == 0:
            playerIndex = playerIndexGen.next()
        loopIter = (loopIter + 1) % 30
        basex = -((-basex + 4) % baseShift)
        playerShm(playerShmVals)

        # draw sprites
        SCREEN.blit(IMAGES['background'], (0,0))
        SCREEN.blit(IMAGES['player'][playerIndex],
                    (playerx, playery + playerShmVals['val']))
        SCREEN.blit(IMAGES['message'], (messagex, messagey))
        SCREEN.blit(IMAGES['base'], (basex, BASEY))

        pygame.display.update()
        FPSCLOCK.tick(FPS)


def playerShm(playerShm):
    """oscillates the value of playerShm['val'] between 8 and -8"""
    if abs(playerShm['val']) == 8:
        playerShm['dir'] *= -1

    if playerShm['dir'] == 1:
         playerShm['val'] += 1
    else:
        playerShm['val'] -= 1


def getRandomPipe():
    """returns a randomly generated pipe"""
    # y of gap between upper and lower pipe
    gapY = random.randrange(0, int(BASEY * 0.6 - PIPEGAPSIZE))
    gapY += int(BASEY * 0.2)
    pipeHeight = IMAGES['pipe'][0].get_height()
    pipeX = SCREENWIDTH + 10

    return [
        {'x': pipeX, 'y': gapY - pipeHeight},  # upper pipe
        {'x': pipeX, 'y': gapY + PIPEGAPSIZE}, # lower pipe
    ]


def showScore(score):
    """displays score in center of screen"""
    scoreDigits = [int(x) for x in list(str(score))]
    totalWidth = 0 # total width of all numbers to be printed

    for digit in scoreDigits:
        totalWidth += IMAGES['numbers'][digit].get_width()

    Xoffset = (SCREENWIDTH - totalWidth) / 2

    for digit in scoreDigits:
        SCREEN.blit(IMAGES['numbers'][digit], (Xoffset, SCREENHEIGHT * 0.1))
        Xoffset += IMAGES['numbers'][digit].get_width()


def checkCrash(player, upperPipes, lowerPipes):
    """returns True if player collders with base or pipes."""
    pi = player['index']
    player['w'] = IMAGES['player'][0].get_width()
    player['h'] = IMAGES['player'][0].get_height()

    # if player crashes into ground
    if player['y'] + player['h'] >= BASEY - 1:
        return [True, True]
    else:

        playerRect = pygame.Rect(player['x'], player['y'],
                      player['w'], player['h'])
        pipeW = IMAGES['pipe'][0].get_width()
        pipeH = IMAGES['pipe'][0].get_height()

        for uPipe, lPipe in zip(upperPipes, lowerPipes):
            # upper and lower pipe rects
            uPipeRect = pygame.Rect(uPipe['x'], uPipe['y'], pipeW, pipeH)
            lPipeRect = pygame.Rect(lPipe['x'], lPipe['y'], pipeW, pipeH)

            # player and upper/lower pipe hitmasks
            pHitMask = HITMASKS['player'][pi]
            uHitmask = HITMASKS['pipe'][0]
            lHitmask = HITMASKS['pipe'][1]

            # if bird collided with upipe or lpipe
            uCollide = pixelCollision(playerRect, uPipeRect, pHitMask, uHitmask)
            lCollide = pixelCollision(playerRect, lPipeRect, pHitMask, lHitmask)

            if uCollide or lCollide:
                return [True, False]

    return [False, False]

def pixelCollision(rect1, rect2, hitmask1, hitmask2):
    """Checks if two objects collide and not just their rects"""
    rect = rect1.clip(rect2)

    if rect.width == 0 or rect.height == 0:
        return False

    x1, y1 = rect.x - rect1.x, rect.y - rect1.y
    x2, y2 = rect.x - rect2.x, rect.y - rect2.y

    for x in xrange(rect.width):
        for y in xrange(rect.height):
            if hitmask1[x1+x][y1+y] and hitmask2[x2+x][y2+y]:
                return True
    return False

def getHitmask(image):
    """returns a hitmask using an image's alpha."""
    mask = []
    for x in range(image.get_width()):
        mask.append([])
        for y in range(image.get_height()):
            mask[x].append(bool(image.get_at((x,y))[3]))
    return mask


def actionTileCode(F,S,A):
    tilecqode(S[0],S[1],F)
    F = [x + A*(numTilings*tiles*tiles) for x in F]
    return F

def getExpected(q):
    expectedVal = 0
    a = argmax(q)
    for i in range(2):
        if(a==i):
            expectedVal = expectedVal + (1 - (epsilon/2))*q[i]
        else:
            expectedVal = expectedVal + (epsilon/2)*q[i]
    return expectedVal
        
def eligibilityTrace(zerovec,F):
    zerovec = alpha*lmbda*zerovec
    zerovec[F] = 1
    return zerovec

episodeNum = 100000
count = 0
alpha = 0.1/numTilings
gamma = 1
lmbda = 0.5
epsilon = 0.1
n = numTiles * 2
F = [-1]*numTilings
w = -0.01*rand(n)
returnSum = 0

while(count<episodeNum):
    moveInfo = start()
    crashInfo = mainGame(moveInfo, SHARED)
    S = SHARED['state']
    zerovec = zeros(n)
    G = 0
    A = 0
    F = actionTileCode(F,S,A)
    zerovec[F] = 1
    while(crashInfo == 1):
        crashInfo = mainGame(moveInfo,SHARED)
        S = SHARED['state']
        R = SHARED['reward']
        G = G + R
        delta = R - sum(w[F])
        q = zeros(2)
        if(crashInfo == 1):
            for a in range(2):
                F = actionTileCode(F,S,a)
                q[a] = sum(w[F])
        else:
            w = w + alpha*delta*zerovec
            break
        
        expected_q = getExpected(q)
        delta = delta + expected_q
            
        A = argmax(q) if rand() >= epsilon else random.choice([0,1])
        SHARED['flap'] = A    
        w = w + alpha*delta*zerovec
            
        F = actionTileCode(F,S,A)

        zerovec = eligibilityTrace(zerovec,F)
   
    count += 1
    returnSum = returnSum + G    
    print 'Return from Episode ', count, 'is ', G
    if(G>1000):
        break
            

print returnSum/episodeNum
pygame.quit()  