/**
 * @brief	This will setup the enemies on the map. They are pushed in a objects vector,
 * 			so they can interact all the time
 */
void CMapLoaderGalaxy::spawnFoes(CMap &Map)
{
    word *start_data = Map.getData(2);
    word *data_ptr;
    word width = Map.m_width;
    word height = Map.m_height;

    // If objects are in the buffer clean them up

    if(!m_ObjectPtr.empty())
        m_ObjectPtr.clear();

    const int numPlayers = g_pBehaviorEngine->mPlayers;

    // he we go to the adding objects
    Map.mNumFuses = 0;
    Map.mFuseInLevel = false;
    data_ptr = start_data;
    for(size_t y=0 ; y<height ; y++)
    {
        for(size_t x=0 ; x<width ; x++)
        {
            const int foeID = *data_ptr;

            // Check if it is the player, because in multiplayer we spawn multiple keens
            if(isKeenPlayer(foeID)) // World Map only
            {
                for(int i = 0 ; i < numPlayers ; i++)
                {
                    std::shared_ptr<CGalaxySpriteObject> pNewfoe( addFoe(Map, foeID, x<<CSF, y<<CSF) );

                    if(pNewfoe)
                        m_ObjectPtr.push_back(pNewfoe);
                }
            }
            else
            {
                std::shared_ptr<CGalaxySpriteObject> pNewfoe( addFoe(Map, foeID, x<<CSF, y<<CSF) );

                if(pNewfoe)
                    m_ObjectPtr.push_back(pNewfoe);
            }

            data_ptr++;
        }
    }

    /// Only for testing loaded Objects
    /*std::ofstream File("objlayer.txt");

    // Note: This is just for tracing stuff from the level maps
    data_ptr = start_data;
    for(size_t y=0 ; y<height ; y++)
    {
    	for(size_t x=0 ; x<width ; x++)
    	{
    		File << *data_ptr++ << " ";
    	}
    	File << std::endl;
    }

    File.close();*/


    // Print out information about first plane of the map
    /*std::ofstream FileTile("tilelayer1.txt");

    // he we go to the adding objects
    data_ptr = Map.getData(1);
    for(size_t y=0 ; y<height ; y++)
    {
    	for(size_t x=0 ; x<width ; x++)
    	{
    		data_ptr++;
    		FileTile << *data_ptr << " ";
    	}
    	FileTile << std::endl;
    }*/

    /*std::ofstream FileTile0("tilelayer0.txt");

    // he we go to the adding objects
    data_ptr = Map.getData(0);
    for(size_t y=0 ; y<height ; y++)
    {
    	for(size_t x=0 ; x<width ; x++)
    	{
    		data_ptr++;
    		FileTile0 << *data_ptr << " ";
    	}
    	FileTile0 << std::endl;
    }

    FileTile0.close();*/
}
void CMapLoaderGalaxy::unpackPlaneData(std::ifstream &MapFile,
                                       CMap &Map, const size_t planeNumber,
                                       longword offset, longword length,
                                       word magic_word)
{
    size_t initial_pos = MapFile.tellg();

    std::vector<word> Plane;

    MapFile.seekg(offset);
    std::vector<byte> Carmack_Plane;
    std::vector<byte> RLE_Plane;
    for(size_t i=0 ; i<length ; i++)
        Carmack_Plane.push_back(MapFile.get());

    size_t decarmacksize = (Carmack_Plane.at(1)<<8)+Carmack_Plane.at(0);





    // Now use the Carmack Decompression
    CCarmack Carmack;
    Carmack.expand(RLE_Plane, Carmack_Plane);
    Carmack_Plane.clear();

    if( decarmacksize > RLE_Plane.size() )
    {
        gLogging.textOut( "\nWARNING Plane Uncompress Carmack Size differs to the one of the headers: Actual " + itoa(RLE_Plane.size()) +
                          " bytes Expected " + itoa(decarmacksize) + " bytes. Trying to reconstruct level anyway!<br>");

        while( decarmacksize > RLE_Plane.size() )
            RLE_Plane.push_back(0);
    }


    if( decarmacksize >= RLE_Plane.size() )
    {

        // Now use the RLE Decompression
        CRLE RLE;
        size_t derlesize = (RLE_Plane[0]<<8)+RLE_Plane[1];           // Bytes already swapped
        RLE.expand(Plane, RLE_Plane, magic_word);
        RLE_Plane.clear();

        word *ptr = Map.getData(planeNumber);
        for(size_t y=0; y<Map.m_height ; ++y)
        {
            const int stride = y*Map.m_width;

            for(size_t x=0; x<Map.m_width ; ++x)
            {
                const int offset = stride+x;
                word tile = Plane.at(offset);

                *ptr = tile;
                ptr++;
            }
        }

        if( derlesize/2 != Plane.size() )
        {
            gLogging.textOut( "\nERROR Plane Uncompress RLE Size Failed: Actual "+ itoa(2*Plane.size()) +" bytes Expected " + itoa(derlesize) + " bytes<br>");
        }
    }
    else
    {
        gLogging.textOut( "\nERROR Plane Uncompress Carmack Size Failed: Actual " + itoa(RLE_Plane.size()) + " bytes Expected " + itoa(decarmacksize) + " bytes<br>");
    }

    MapFile.seekg(initial_pos);
}