require "map"

local arcade_config = require("arcade_config")

tiletype={void=0,nonpc=1,stair=2,switch=3,half=4,block=5}
mapa.wait=0
mapa.step=0
function mapa_do_backup()
    mapa_backup={}
    for i=1,#mapa do
        mapa_backup[i]={}
        for j=1,#mapa[i] do
            mapa_backup[i][j]=mapa[i][j]
        end
    end
end

function mapa_restore_backup()
    for i=1,#mapa do
        for j=1,#mapa[i] do
            mapa[i][j]=mapa_backup[i][j]
        end
    end
end

function mapa_new()
    for my=0,7 do
        for mx=0,9 do
            local mi=1+mx+my*10
            mapa[mi]={}
            for ty=0,5 do
                for tx=0,11 do
                    local tile=256
                    if mx==0 and tx==0 then
                        tile=16
                    elseif mx==4 and tx==11 then
                        tile=16
                    elseif ty==0 or ty==5 then
                        if tx%2==0 then
                            tile=20
                        else
                            tile=21
                        end
                    end
                    mapa[mi][1+tx+ty*12]=tile
                end
            end
        end
    end
end

function mapa_save()
    local file = io.open("data/map.lua", "w")
    io.output(file)
    io.write("mapa={\n")
    for i=1,80 do
        io.write("    -- "..i.."\n    {\n        ")
        for j=1,72 do
            io.write(mapa[i][j]..",")
            if j%12==0 then io.write("\n        ") end
        end
        io.write("\n    },\n")
    end
    io.write("}\n")
    io.close(file)
end

anim_tiles={113,114,112,116,117,115,119,120,118,122,121}
function mapa_update(hab1,hab2)
    mapa.wait=mapa.wait+1

    if mapa.wait==6 then
        mapa.wait=0
        mapa.step=(mapa.step+1)&31

        local hab=hab1

        repeat
            for ty=0,5 do
                for tx=0,11 do
                    local tile=mapa[1+hab][1+tx+ty*12]
                    if tile>=112 and tile<126 then
                        mapa[1+hab][1+tx+ty*12]=anim_tiles[tile-111]
                    end
                end
            end
            if hab==hab2 then break end
            hab=hab2
        until false
    end
end

function mapa_draw(hab)
    for ty=0,5 do
        for tx=0,11 do
            local tile=mapa[1+hab][1+tx+ty*12]
            if tile~=256 and (tile<126 or mapa.step>4) then
                -- draw.surf((tile&15)*8,64+(tile>>4)*8,8,8,tx*8,ty*8)
				local txr = arcade_config.tiles_per_row-1
				local txr2 = arcade_config.tiles_per_row_base2
				local toff= arcade_config.tiles_offset
				local tw  = arcade_config.tiles_width
				local th  = arcade_config.tiles_height
                draw.surf((tile&txr)*tw,toff+(tile>>txr2)*th,tw,th,tx*tw,ty*th)
            end
        end
    end
end

function mapa_set_tile(hab,x,y,tile)
    mapa[1+hab][1+x+y*12]=tile
end

function mapa_set_tile_by_index(hab,index,tile)
    mapa[1+hab][index]=tile
end

function mapa_get_tile(hab,x,y)
    return mapa[1+hab][1+x+y*12]
end

function mapa_cycle_colors(hab)
    for i=1,72 do
        local tile=mapa[1+hab][i]
        if tile<4 then
            tile=(tile+1)&3
        elseif tile>=16 and tile<48 then
            tile=tile+8
            if tile>=48 then tile=tile-32 end
        end
        mapa[1+hab][i]=tile
    end
end

function check_tile(hab,x,y)
    local xx=math.min(11,math.max(0,math.floor(x/8)))
    local yy=math.min(5,math.max(0,math.floor(y/8)))
    --rect(xx*8,yy*8,xx*8+8,yy*8+8,3)

    local tile=mapa_get_tile(hab,xx,yy)
    if tile<8 then
        return tiletype.half
    elseif tile<15 then
        return tiletype.stair
    elseif tile==15 then
        return tiletype.switch
    elseif tile<64 then
        return tiletype.block
    elseif tile==111 then
        return tiletype.nonpc
    else
        return tiletype.void
    end
end

pix={14,10,7,1,0,0,0,0,4,4,4,4,0,0,0,8,14,14,14,14,14,14,0,0,10,10,10,10,10,10,10,10,7,7,7,7,7,7,2,2,1,1,1,1,1,1,12,12,1,1,1,9,9,9,10,10,10,0,0,0,0,0,0,0}

function mapa_create_minimap()
    minimap = surf.new(128,96)
    surf.target(minimap)
    for h=0,79 do
        for y=0,5 do
            for x=0,11 do
                local tile=mapa[h+1][1+x+y*12]
                if h==45 then
                    print(x..","..y.."="..tile)
                end
                if tile<64 then
                    surf.pixel(x+(h%10)*12,y+math.floor(h/10)*6,pix[1+tile])
                end
            end
        end
    end
    game_update=mapa_draw_minimap
end

function mapa_draw_minimap()
    --print("HOLA")
    surf.source(minimap)
    surf.target(0)
    surf.cls(16)
    draw.surf(0,0,128*o2aX,96*o2Ax,0,0)
end