void World::randomize(uint32_t width, uint32_t height, uint32_t nbObstacles, uint32_t seed) { if(seed == 0) { seed = static_cast<uint32_t>(std::time(0)); } std::mt19937 rng(seed); std::uniform_int_distribution<uint32_t> heightDistribution(0, height); std::uniform_int_distribution<uint32_t> widthDistribution(0, width); std::uniform_int_distribution<uint32_t> angleDistribution(0, 359); std::normal_distribution<float32> sizeDistribution(10.0, 5.0); for(uint32_t i = 0; i < nbObstacles; ++i) { std::shared_ptr<StaticBox> box = std::make_shared<StaticBox>( b2Vec2(widthDistribution(rng), heightDistribution(rng)), angleDistribution(rng), std::abs(sizeDistribution(rng)), std::abs(sizeDistribution(rng)) ); addDrawable(box); } }
void Map::generateRooms(int attempts) { auto time = clock(); std::uniform_int_distribution<int> xDistribution(0, (this->width - 1) / 2); std::uniform_int_distribution<int> yDistribution(0, (this->height - 1) / 2); std::uniform_int_distribution<int> widthDistribution(3, 15); std::uniform_int_distribution<int> heightDistribution(3, 15); for (int i = 0; i < attempts; i++) { bool intersects = false; std::shared_ptr<Room> room = std::make_shared<Room>(xDistribution(generator) * 2 + 1, yDistribution(generator) * 2 + 1, widthDistribution(generator) * 2 + 1, heightDistribution(generator) * 2 + 1); if (!isInside(room->x, room->y) || !isInside(room->x + room->width, room->y + room->height) || !isInside(room->x + room->width, room->y) ||!isInside(room->x, room->y + room->height)) { continue; } room->applyTiles(); for (auto region : regions) { if (room->intersects(region)) { intersects = true; break; } } if (!intersects) { //room->shrink(1, 1); regions.push_back(room); set(room->tiles, room); } } time = clock() - time; std::cout << "Generated rooms in " << (((float)time)/CLOCKS_PER_SEC) << " seconds" << std::endl; }
static Grid uniformGrid(u_int width, u_int height, double obstacleProbability, int seed = 0) { Grid grid(width, height); std::default_random_engine randomEngine(seed); std::uniform_int_distribution<int> widthDistribution(0, width - 1); std::uniform_int_distribution<int> heightDistribution(0, height - 1); std::bernoulli_distribution obstacleDistribution(obstacleProbability); for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { } } return grid; };