Example #1
0
cave_map_generator::cave_map_generator_job::cave_map_generator_job(const cave_map_generator& pparams, boost::optional<uint32_t> randomseed)
	: params(pparams)
	, flipx_(false)
	, flipy_(false)
	, map_(t_translation::ter_map(params.width_ + 2 * gamemap::default_border, params.height_ + 2 * gamemap::default_border/*, params.wall_*/))
	, starting_positions_()
	, chamber_ids_()
	, chambers_()
	, passages_()
	, res_(params.cfg_.child_or_empty("settings"))
	, rng_() //initialises with rand()
{
	res_.add_child("event", config_of
		("name", "start")
		("message", config_of
			("message", "scenario_generation=cave is deprecated and will be removed soon.")
		)
	);
	uint32_t seed = randomseed.get_ptr() ? *randomseed.get_ptr() : seed_rng::next_seed();
	rng_.seed(seed);
	LOG_NG << "creating random cave with seed: " << seed << '\n';
	flipx_ = int(rng_() % 100) < params.flipx_chance_;
	flipy_ = int(rng_() % 100) < params.flipy_chance_;

	LOG_NG << "creating scenario....\n";
	generate_chambers();

	LOG_NG << "placing chambers...\n";
	for(std::vector<chamber>::const_iterator c = chambers_.begin(); c != chambers_.end(); ++c) {
		place_chamber(*c);
	}

	LOG_NG << "placing passages...\n";

	for(std::vector<passage>::const_iterator p = passages_.begin(); p != passages_.end(); ++p) {
		place_passage(*p);
	}
	LOG_NG << "outputting map....\n";

	res_["map_data"] = t_translation::write_game_map(map_, starting_positions_);
}
cave_map_generator::cave_map_generator_job::cave_map_generator_job(const cave_map_generator& pparams, boost::optional<boost::uint32_t> randomseed)
	: params(pparams)
	, flipx_(false)
	, flipy_(false)
	, map_(t_translation::t_map(params.width_ + 2 * gamemap::default_border,
	       t_translation::t_list(params.height_ + 2 * gamemap::default_border, params.wall_)))
	, starting_positions_()
	, chamber_ids_()
	, chambers_()
	, passages_()
	, res_(params.cfg_.child_or_empty("settings"))
	, rng_() //initialises with rand()
{
	uint32_t seed = randomseed.get_ptr() ? *randomseed.get_ptr() : seed_rng::next_seed();
	rng_.seed(seed);
	std::cerr << "creating random cave with seed:" << seed;
	flipx_ = int(rng_() % 100) < params.flipx_chance_;
	flipy_ = int(rng_() % 100) < params.flipy_chance_;

	LOG_NG << "creating scenario....\n";
	generate_chambers();

	LOG_NG << "placing chambers...\n";
	for(std::vector<chamber>::const_iterator c = chambers_.begin(); c != chambers_.end(); ++c) {
		place_chamber(*c);
	}

	LOG_NG << "placing passages...\n";

	for(std::vector<passage>::const_iterator p = passages_.begin(); p != passages_.end(); ++p) {
		place_passage(*p);
	}
	LOG_NG << "outputting map....\n";

	config& map = res_.add_child("map");
	map["data"] = t_translation::write_game_map(map_, starting_positions_);
	map["usage"] = "map";
	map["border_size"] = gamemap::default_border;
}
std::string default_map_generator::generate_map(std::map<map_location,std::string>* labels, boost::optional<uint32_t> randomseed)
{
    uint32_t seed;
    if(const uint32_t* pseed = randomseed.get_ptr()) {
        seed = *pseed;
    }
    else {
        seed = seed_rng::next_seed();
    }

    // Suppress labels?
    if ( !show_labels_ )
        labels = nullptr;

    // the random generator thinks odd widths are nasty, so make them even
    if (is_odd(width_))
        ++width_;

    size_t iterations = (iterations_*width_*height_)/(default_width_*default_height_);
    size_t island_size = 0;
    size_t island_off_center = 0;
    size_t max_lakes = max_lakes_;

    if(island_size_ >= max_coastal) {

        //islands look good with much fewer iterations than normal, and fewer lake
        iterations /= 10;
        max_lakes /= 9;

        //the radius of the island should be up to half the width of the map
        const size_t island_radius = 50 + ((max_island - island_size_)*50)/(max_island - max_coastal);
        island_size = (island_radius*(width_/2))/100;
    } else if(island_size_ > 0) {
        DBG_NG << "coastal...\n";
        //the radius of the island should be up to twice the width of the map
        const size_t island_radius = 40 + ((max_coastal - island_size_)*40)/max_coastal;
        island_size = (island_radius*width_*2)/100;
        island_off_center = std::min<size_t>(width_,height_);
        DBG_NG << "calculated coastal params...\n";
    }

    // A map generator can fail so try a few times to get a map before aborting.
    std::string map;
    // Keep a copy of labels as it can be written to by the map generator func
    std::map<map_location,std::string> labels_copy;
    std::map<map_location,std::string> * labels_ptr =  labels ? &labels_copy : nullptr;
    std::string error_message;
    //initilize the job outside the loop so that we really get a different result everytime we run the loop.
    default_map_generator_job job(seed);
    int tries = 10;
    do {
        if (labels) {
            // Reset the labels.
            labels_copy = *labels;
        }
        try {
            map = job.default_generate_map(width_, height_, island_size, island_off_center,
                                           iterations, hill_size_, max_lakes, (nvillages_ * width_ * height_) / 1000,
                                           castle_size_, nplayers_, link_castles_, labels_ptr, cfg_);
            error_message = "";
        }
        catch (mapgen_exception& exc) {
            error_message = exc.message;
        }
        --tries;
    } while (tries && map.empty());
    if (labels) {
        labels->swap(labels_copy);
    }

    if (error_message != "")
        throw mapgen_exception(error_message);

    return map;
}
Example #4
0
		const Label* onInit() const { return _on_init.get_ptr(); }
Example #5
0
		const Label* onPeriodic() const { return _on_periodic.get_ptr(); }
Example #6
0
		const Label* onPacket() const { return _on_packet.get_ptr(); }