# Perform game-specific transformations.
import gmapi
import math
import pygame
try:
    gmapi.glowing_spr.subimages[0] = gmapi.renpy_glue.readsurf('glowing_spr_inv.png',loud=False)
except:
    pass

def switch_fadeout():
 global self
 global other
 global room
 global keyboard_string
 global score
 global lives
 gmapi.py_reserved_varnames["global"].soundtrack_title=""
 if (gmapi.SS_IsHandleValid(gmapi.py_reserved_varnames["global"].soundfade)):
  gmapi.SS_StopSound(gmapi.py_reserved_varnames["global"].soundfade)

 gmapi.py_reserved_varnames["global"].soundfade=gmapi.py_reserved_varnames["global"].current_soundtrack
 gmapi.exit_monitor.fvol=0.0001
 gmapi.exit_monitor.alarm[1]=9.0*gmapi.room.speed
 gmapi.py_reserved_varnames["global"].current_soundtrack=-1.0

#gmapi.switch_fadeout = switch_fadeout

# XXX testing cave_maptrack replacements:
cave_maptrack_surf = None

def cave_maptrack_step():
    sx = int(math.floor(gmapi.girl_walker_village.x/80));
    sy = int(math.floor(gmapi.girl_walker_village.y/80));
    room = gmapi.room
    if room==gmapi.wcave:
        curmap = gmapi.py_reserved_varnames['global'].lvl3map
    elif room==gmapi.icave:
        curmap = gmapi.py_reserved_varnames['global'].lvl2map
    elif room==gmapi.lcave:
        curmap = gmapi.py_reserved_varnames['global'].lvl4map
    elif room==gmapi.scave:
        curmap = gmapi.py_reserved_varnames['global'].lvl5map
    changed = False
    if not curmap[sx,sy]:
        curmap[sx,sy] = 1
        changed = True
    if sx>0 and not curmap[sx-1,sy]:
        curmap[sx-1,sy] = 1
        changed = True
    if sy>0 and not curmap[sx,sy-1]:
        curmap[sx,sy-1] = 1
        changed = True
    if sx<19 and not curmap[sx+1,sy]:
        curmap[sx+1,sy] = 1
        changed = True
    if sy<19 and not curmap[sx,sy+1]:
        curmap[sx,sy+1] = 1
        changed = True
    if changed:
        global cave_maptrack_surf
        cave_maptrack_surf = None
gmapi.py_cave_maptrack_step = cave_maptrack_step

def cave_maptrack_draw():
    room = gmapi.room
    if room==gmapi.wcave:
        curmap = gmapi.py_reserved_varnames['global'].lvl3map
    elif room==gmapi.icave:
        curmap = gmapi.py_reserved_varnames['global'].lvl2map
    elif room==gmapi.lcave:
        curmap = gmapi.py_reserved_varnames['global'].lvl4map
    elif room==gmapi.scave:
        curmap = gmapi.py_reserved_varnames['global'].lvl5map
    
    if room.view_current==1:
        global cave_maptrack_surf
        if not cave_maptrack_surf:
            cave_maptrack_surf = pygame.Surface((room.view_wport[1],room.view_hport[1]))
            cave_maptrack_surf.blit(gmapi.__pygame_screen,(0,0),(room.view_xport[1],room.view_yport[1],150,150))
            i = 0
            while i<20:
                j = 0
                while j<20:
                    if not curmap[i,j]:
                        cave_maptrack_surf.fill((0,0,0),
                           (
                               round(7.5*i),round(7.5*j),8,8)
                           )
                    j += 1
                i += 1
        gmapi.__pygame_screen.blit(cave_maptrack_surf,(650,450))

def hook():
    print "Hook in.\n"
    gmapi.py_cave_maptrack_draw = cave_maptrack_draw
    gmapi.cave_maptrack.py_objectdata.events[8][0].python = compile('py_cave_maptrack_draw()','cave_maptrack_draw_caller','exec')
    gmapi.cave_maptrack.py_objectdata.events[3][0].python = compile('py_cave_maptrack_step()','cave_maptrack_step_caller','exec')
