Пример #1
0
void
TerrainGenerator::generateGrassLand(VoxelMatrix& cubes,
                                    double height,
                                    int xOff,
                                    int zOff,
                                    int x,
                                    int z) {

  module::Perlin tree;
  tree.SetFrequency(2);
  tree.SetSeed(m_seed);

  module::Perlin treeArea;
  treeArea.SetFrequency(0.25);
  treeArea.SetSeed(m_seed);

  for (int y{1}; y < height; ++y) {
    Voxel& v = cubes[x][y][z];

    static const double stonePercentage{1.0 / 2.0};
    if (y < height * stonePercentage)
      v.setId(config::cube_data::STONE);
    else
      v.setId(config::cube_data::DIRT);

    if (y + 1 > height) {
      v.setId(config::cube_data::GRASS);

      double shouldHaveTrees =
        treeArea.GetValue((xOff + static_cast<int>(x)) / 10.0,
                          (zOff + static_cast<int>(z)) / 10.0,
                          0.5);

      if (shouldHaveTrees < 0.5)
        break;

      double value = tree.GetValue((xOff + static_cast<int>(x)) / 10.0,
                                   (zOff + static_cast<int>(z)) / 10.0,
                                   0.5);

      bool isNotAtBorder{x > 0 && x + 1 < m_width && z > 0 && z + 1 < m_depth};
      bool shouldPlaceTree{value > -0.02 && value < 0.02};
      // Short circuit is important here.
      // Does not seem to work properly :s
      bool hasNeighboor{isNotAtBorder &&
                        hasTreeNeighboor(cubes, x, y + 1, z, treeHeight)};

      if (isNotAtBorder && shouldPlaceTree && !hasNeighboor)
        placeTree(cubes, x, y + 1, z);

      break;
    }
  }
}
Пример #2
0
void MapGenerator::generateNew(size_t w, size_t h)
{
	mMap.resize(w, h);

	std::srand(static_cast<unsigned int>(std::time(0)));
	std::vector<sf::Vector2i> groundSprites;
	groundSprites.push_back(sf::Vector2i(1, 4));


	//place ground
	for (size_t x = 0; x < mMap.getWidth(); ++x)
	{
		for (size_t y = 0; y < mMap.getHeight(); ++y)
		{
			mMap[x][y].setSprite(0, groundSprites[std::rand() % groundSprites.size()]);
		}
	}

		
	while(!placeWalkWay(sf::Vector2i(0, 0), sf::Vector2i(w - 1, h - 1)))
	{
		std::srand(static_cast<unsigned int>(std::clock()));
	}
	
	for (int i = 0; (i < w + h / 2) * 5; i++)
	{
		placeHut(std::rand() % mMap.getWidth(), std::rand() % mMap.getHeight());
		placeTree(std::rand() % mMap.getWidth(), std::rand() % mMap.getHeight());
		placeDoodad(std::rand() % mMap.getWidth(), std::rand() % mMap.getHeight());
		placeDoodad(std::rand() % mMap.getWidth(), std::rand() % mMap.getHeight());
		placeKrimsKrams(std::rand() % mMap.getWidth(), std::rand() % mMap.getHeight());
		placeKrimsKrams(std::rand() % mMap.getWidth(), std::rand() % mMap.getHeight());


	}
}