The Battle for Wesnoth  1.17.0-dev
map_create.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
17 
21 #include "log.hpp"
22 
23 #include <cassert>
24 #include <sstream>
25 
26 static lg::log_domain log_config("config");
27 #define ERR_CF LOG_STREAM(err, log_config)
28 
29 map_generator* create_map_generator(const std::string& name, const config &cfg, const config* vars)
30 {
31  if(name == "default" || name.empty()) {
32  return new default_map_generator(cfg);
33  } else if(name == "cave") {
34  ERR_CF << "map/scenario_generation=cave is deprecatd and will be removed soon, use map/scenario_generation=lua with lua/cave_map_generator.lua instead.\n";
35  return new cave_map_generator(cfg);
36  } else if(name == "lua") {
37  return new lua_map_generator(cfg, vars);
38  } else {
39  return nullptr;
40  }
41 }
42 
43 //function to generate a random map, from a string which describes
44 //the generator to use
45 std::string random_generate_map(const std::string& name, const config &cfg, const config* vars)
46 {
47  std::unique_ptr<map_generator> generator(create_map_generator(name, cfg, vars));
48  if(generator == nullptr) {
49  std::stringstream ss;
50  ss << "could not find map generator '" << name << "'";
51  throw mapgen_exception(ss.str());
52  }
53 
54  return generator->create_map();
55 }
56 
57 config random_generate_scenario(const std::string& name, const config &cfg, const config* vars)
58 {
59  std::unique_ptr<map_generator> generator(create_map_generator(name, cfg, vars));
60  if(generator == nullptr) {
61  std::stringstream ss;
62  ss << "could not find map generator '" << name << "'";
63  throw mapgen_exception(ss.str());
64  }
65 
66  return generator->create_scenario();
67 }
static lg::log_domain log_config("config")
std::string random_generate_map(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:45
#define ERR_CF
Definition: map_create.cpp:27
virtual config create_scenario(std::optional< uint32_t > randomseed={})
map_generator * create_map_generator(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:29
rng * generator
This generator is automatically synced during synced context.
Definition: random.cpp:61
config random_generate_scenario(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:57
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
virtual std::string create_map(std::optional< uint32_t > randomseed={})=0
Creates a new map and returns it.