Example #1
0
// Loads the map into the memory
bool CMapLoader::load( Uint8 episode, Uint8 level, const std::string& path, bool loadNewMusic, bool stategame )
{
    std::string levelname = "level";
    if(level < 10) levelname += "0";
    levelname += itoa(level) + ".ck" + itoa(episode);

    mp_map->resetScrolls();
    mp_map->m_gamepath = path;
    mp_map->m_worldmap = (level == 80);

    // HQ Music. Load Music for a level if you have HQP
    if(loadNewMusic)
    {
        g_pMusicPlayer->stop();

        // If no music from the songlist could be loaded try the normal table which
        // has another format. It is part of HQP
        if(!g_pMusicPlayer->LoadfromSonglist(path, level))
            g_pMusicPlayer->LoadfromMusicTable(path, levelname);
    }

    // decompress map RLEW data
    std::ifstream MapFile;
    bool fileopen = OpenGameFileR(MapFile, getResourceFilename(levelname,path,true,false), std::ios::binary);

    if (!fileopen)
    {
        // only record this error message on build platforms that log errors
        // to a file and not to the screen.
        g_pLogFile->ftextOut("MapLoader: unable to open file %s<br>", levelname.c_str());
        return 0;
    }
    g_pLogFile->ftextOut("MapLoader: file %s opened. Loading...<br>", levelname.c_str());

    MapFile.seekg (0, std::ios::beg);

    // load the compressed data into the memory
    std::vector<Uint8>	compdata;
    while( !MapFile.eof() )
    {
        compdata.push_back(static_cast<Uint8>(MapFile.get()));
    }

    MapFile.close();

    CRLE RLE;
    std::vector<Uint16> planeitems;
    RLE.expandSwapped(planeitems,compdata, 0xFEFE);

    // Here goes the memory allocation function
    mp_map->createEmptyDataPlane(1, planeitems.at(1), planeitems.at(2));

    int t;
    unsigned int planesize = 0;
    unsigned int curmapx=0, curmapy=0;
    planesize = planeitems.at(8);
    planesize /= 2; // Size of two planes, but we only need one


    const char &fixlevel_error = g_pBehaviorEngine->m_option[OPT_FIXLEVELERRORS].value;

    Uint32 c;
    for( c=17 ; c<planesize+17 ; c++ ) // Check against Tilesize
    {
        t = planeitems.at(c);

        if( fixlevel_error )
            fixLevelTiles(t, curmapx, curmapy, episode, level);

        addTile(t, curmapx, curmapy);

        curmapx++;
        if (curmapx >= mp_map->m_width)
        {
            curmapx = 0;
            curmapy++;
            if (curmapy >= mp_map->m_height) break;
        }

        if(t > 255)
            t=0; // If there are some invalid values in the file
    }

    // now do the sprites
    // get sprite data
    int resetcnt, resetpt;
    curmapx = curmapy = 0;
    resetcnt = resetpt = 0;

    if(mp_objvect && stategame == false)
    {
        std::vector<CObject*>::iterator obj = mp_objvect->begin();
        for( ; obj != mp_objvect->end() ; obj++ )
        {
            delete *obj;
            mp_objvect->pop_back();
        }

        mp_objvect->reserve(2000);

        for( c=planesize+17 ; c<2*planesize+16 ; c++ )
        {
            // in case the planesizes are bigger than the actual file content itself
            if(planeitems.size() <= c) break;

            t = planeitems.at(c);

            if (mp_map->m_worldmap) addWorldMapObject(t, curmapx, curmapy,  episode );
            else addEnemyObject(t, curmapx, curmapy, episode, level);

            curmapx++;
            if (curmapx >= mp_map->m_width)
            {
                curmapx = 0;
                curmapy++;
                if (curmapy >= mp_map->m_height) break;
            }

            if (++resetcnt==resetpt) curmapx = curmapy = 0;
        }
    }
    planeitems.clear();

    // Do some post calculations
    // Limit the scroll screens so the blocking (blue in EP1) tiles are3 never seen
    SDL_Rect gamerect = g_pVideoDriver->getGameResolution();
    mp_map->m_maxscrollx = (mp_map->m_width<<4) - gamerect.w - 32;
    mp_map->m_maxscrolly = (mp_map->m_height<<4) - gamerect.h - 32;

    // Set Scrollbuffer
    g_pVideoDriver->setScrollBuffer(&mp_map->m_scrollx_buf, &mp_map->m_scrolly_buf);

    return true;
}
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);
}
Example #3
0
bool CVorticonMapLoaderBase::loadBase(  Uint8 episode, 
					Uint8 level, 
					const std::string& path, 
					bool loadNewMusic )
{
    
	std::vector<Uint16> planeitems;
    
    std::string levelname = "level";
	if(level < 10) levelname += "0";
	levelname += itoa(level) + ".ck" + itoa(episode);

    mpMap->resetScrolls();
	mpMap->m_gamepath = path;
	mpMap->m_worldmap = (level == 80);

	// HQ Music. Load Music for a level if you have HQP
	if(loadNewMusic)
	{
		gMusicPlayer.stop();

		// If no music from the songlist could be loaded try the normal table which
		// has another format. It is part of HQP
		if(!gMusicPlayer.LoadfromSonglist(path, level))
			gMusicPlayer.LoadfromMusicTable(path, levelname);
	}

	// decompress map RLEW data
	std::ifstream MapFile;
	bool fileopen = OpenGameFileR(MapFile, getResourceFilename(levelname,path,true,false), std::ios::binary);

	if (!fileopen)
	{
		// only record this error message on build platforms that log errors
		// to a file and not to the screen.
		gLogging.ftextOut("MapLoader: unable to open file %s<br>", levelname.c_str());
		return false;
	}
	gLogging.ftextOut("MapLoader: file %s opened. Loading...<br>", levelname.c_str());

	MapFile.seekg (0, std::ios::beg);

	// load the compressed data into the memory
	std::vector<Uint8>	compdata;
	while( !MapFile.eof() )
	{
		compdata.push_back(static_cast<Uint8>(MapFile.get()));
	}

	MapFile.close();

	CRLE RLE;
    RLE.expandSwapped(planeitems, compdata, 0xFEFE);

	// Here goes the memory allocation function
	const Uint16 w =  planeitems.at(1);
	const Uint16 h =  planeitems.at(2);
    mpMap->setupEmptyDataPlanes(3, w, h);

	unsigned int planesize = 0;
	planesize = planeitems.at(8);
	planesize /= 2; // We have two planes

	blitPlaneToMap( planeitems, planesize, 0, 0);
	blitPlaneToMap( planeitems, planesize, 0, 1);
	blitPlaneToMap( planeitems, planesize, 1, 2);
	
	mpMap->collectBlockersCoordiantes();
    mpMap->setupAnimationTimer();
	return true;
}