Example #1
0
void Grid::buildRooms()
{


    std::random_device rd;
    std::mt19937_64 gen(rd());
    std::uniform_int_distribution<int> sizeDistrib(m_roomMin,m_roomMax);
    std::uniform_int_distribution<int> widthDistrib(2,m_width-2);
    std::uniform_int_distribution<int> heightDistrib(2,m_height-2);
    for(int i=0;i<m_roomAttempts;i++)
    {

        printGrid();
       int discard=false;
       while(!discard)
       {
            int origY=heightDistrib(gen);
            int origX=widthDistrib(gen);
            int sizeX=sizeDistrib(gen);

            int sizeY=sizeDistrib(gen);
            std::cout<<"Y="<<sizeY<<"\n";
           if((m_grid[origY-1][origX]->getWall()==false)||(m_grid[origY][origX-1]->getWall()==false)||(m_grid[origY-1][origX-1]->getWall()==false))
           {
               discard=true;
               break;
           }

           for(int y=origY;(y<=(origY+sizeY));y++)
           {

                if(y>=m_height-1){break;}

               for(int x=origX;(x<=(origX+sizeX));x++)
               {

                    if(x>=m_width-1){break;}

                    if((m_grid[y+1][x]->getWall()==false)\
                     ||(m_grid[y][x+1]->getWall()==false)\
                     ||(m_grid[y+1][x+1]->getWall()==false)\
                     ||(m_grid[origY-1][x]->getWall()==false)\
                     ||(m_grid[y][origX-1]->getWall()==false))
                    {
                            deleteRoom(origY,y,origX,x);
                            discard=true;
                            break;
                    }

                    else
                    {
                        m_grid[y][x]->setWall(false);
                    }
               }

           }
           discard=true;

       }
    }
}
void VoronoiSeedsGenerator::generateSeeds(
	std::vector<Seed>& listOfSeeds
)
{
	/*
	 * In order to retain the current number of seeds by subdivision of the
	 * grid, we use a bi-dimensional array.
	 */
	int** nbSeedsBySub = new int* [m_nbOfHeightSubdivisions];
	for (int i = 0; i < m_nbOfHeightSubdivisions; i++) {
		nbSeedsBySub[i] = new int[m_nbOfWidthSubdivisions];
        for (int j = 0; j < m_nbOfWidthSubdivisions; j++) {
            nbSeedsBySub[i][j] = 0;
        }
	}

    /*
     * So as to retain the repartition of the inserted seeds, we need to allocate another array.
     * This one is dynamically allocated since it will be necessary to use it as an argument
     * for a function.
     */
    std::vector<Seed>** seedsBySub = new std::vector<Seed>* [m_nbOfHeightSubdivisions];
    for (int i = 0; i < m_nbOfHeightSubdivisions; i++) {
        seedsBySub[i] = new std::vector<Seed> [m_nbOfWidthSubdivisions];
    }

	/*
	 * Initializing the random generator which is going to be used in order to
	 * generate the seeds.
	 */
    std::random_device randomDevice;
	std::mt19937 generator(randomDevice());
	std::normal_distribution<float> widthDistrib(m_width/2.0, m_width/5.0);
	std::normal_distribution<float> heightDistrib(m_height/2.0, m_height/5.0);

	/*
	 * Main loop of the function in which the seeds are generated.
     * On top of that, in order to ease the implementation of the "Whittaker
     * step", the seeds have to be sorted according to their distance to the
     * center of the map.
     * So as to do so, the seeds are sorted by "insertion" in a temporary list,
     * and then pushed back in the given vector.
	 */
    std::list<Seed> tmpList;
	int currentNbOfSeeds = 0;
	while (currentNbOfSeeds < m_nbOfSeeds) {
		float wPosition = widthDistrib(generator);
		float hPosition = heightDistrib(generator);
		/*
		 * Testing if the subdivision which is going to contain the new seed
		 * is "full".
		 */
		int widthID = (int)(floor(wPosition/m_width*m_nbOfWidthSubdivisions));
		int heightID = (int)(floor(hPosition/m_height*m_nbOfHeightSubdivisions));

        if (
                (widthID >= 0) && (widthID < m_nbOfWidthSubdivisions) 
            &&  (heightID >=0) && (heightID < m_nbOfHeightSubdivisions)
        ) {
            if (nbSeedsBySub[heightID][widthID] < m_maxNbOfSeedsBySubdivision) {
                Seed seed(wPosition, hPosition);

                /*
                 * Inserting only if the new seed is at a minimal distance 
                 * of the other ones previously inserted.
                 */
                if (isMinDistVerified(seedsBySub, widthID, heightID, seed)) {
                    insertIntoList(tmpList, seed);
                    currentNbOfSeeds++;
                    nbSeedsBySub[heightID][widthID]++;
                    seedsBySub[heightID][widthID].push_back(seed);
                }
            }
        }
	}

    /*
     * Freeing the allocated memory zone related to seeds' repartition.
     */
    for (int i = 0; i < m_nbOfHeightSubdivisions; i++) {
		seedsBySub[i]->clear();
        delete [] seedsBySub[i];
		delete[] nbSeedsBySub[i];
    }
    delete [] seedsBySub;
	delete[] nbSeedsBySub;


    /*
     * Finally, we just have to insert the content of the list into the
     * given vector.
     */
    for (
        auto iterator = tmpList.begin();
        iterator != tmpList.end();
        iterator++
    ) {
        listOfSeeds.push_back(*iterator);
    }
}