/* FINAL DRAGON METAMAP DECODER 4/25/05
JESSE HARRIS
DECODES A GIVEN METAMAP USING A LOOKUP TABLE AND RETURNS A DECODED MAP
SEE FINAL WRITE UP OF FINAL DRAGON FOR MORE INFO
*/

#include <mygba.h>
#include <hel.h>
u16 * newarray;
short * myoldmap;
u16 * mylookuptable;

u16 * metaMapDecode(short *oldmap, u16 *lookuptable, u8 oldsizex, u8 oldsizey)
{
    //free old array
    free(newarray);
    //set up new array four times the size of the metamap array plus black borders for map edges
    newarray = (u16 *)ham_Malloc((oldsizex + 15) * (oldsizey + 10) * 4 * 2);
    int x;
    int y;
    myoldmap = (short *)oldmap;
    mylookuptable = (u16 *)lookuptable;
    //set all to black
    for(x = 0; x < 2 * oldsizex + 30; x++)
    {
        for(y = 0; y < 2 * oldsizey + 20; y++)
        {
            newarray[x + (y * (2 * oldsizex + 30))] = 0;
        }
        
    }

    newarray[1] = 0;  //not sure why needed
    newarray[2] = 0;  //displays random junk in upper left hand corner if these aren't here, algorithm sounds but results might be hardware related
    //THE GUTS
    for(x = 0; x < oldsizex; x++)
    {
        for(y = 0; y < oldsizey; y++)
        {
        newarray[14 + (2 * x) + (2 * (y + 5) * (oldsizex + 15) * 2)] = mylookuptable[myoldmap[x + y * oldsizex] * 4];
        newarray[14 + (2 * x) + 1 + (2 * (y + 5) * (oldsizex + 15) * 2)] = mylookuptable[myoldmap[x + y * oldsizex] * 4 + 1];
        newarray[14 + (2 * x) + (((2 * (y + 5)) + 1) * (oldsizex + 15) * 2)] = mylookuptable[myoldmap[x + y * oldsizex] * 4 + 2];
        newarray[14 + (2 * x) + 1 + (((2 * (y + 5)) + 1) * (oldsizex + 15) * 2)] = mylookuptable[myoldmap[x + y * oldsizex] * 4 + 3];
        }
    }
    //END THE GUTS
    return newarray;
}

/* END OF FILE */
