The Battle for Wesnoth  1.19.0-dev
map_create.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 <sstream>
24 
25 static lg::log_domain log_config("config");
26 #define ERR_CF LOG_STREAM(err, log_config)
27 
28 map_generator* create_map_generator(const std::string& name, const config &cfg, const config* vars)
29 {
30  if(name == "default" || name.empty()) {
31  return new default_map_generator(cfg);
32  } else if(name == "cave") {
33  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.";
34  return new cave_map_generator(cfg);
35  } else if(name == "lua") {
36  return new lua_map_generator(cfg, vars);
37  } else {
38  return nullptr;
39  }
40 }
41 
42 //function to generate a random map, from a string which describes
43 //the generator to use
44 std::string random_generate_map(const std::string& name, const config &cfg, const config* vars)
45 {
46  std::unique_ptr<map_generator> generator(create_map_generator(name, cfg, vars));
47  if(generator == nullptr) {
48  std::stringstream ss;
49  ss << "could not find map generator '" << name << "'";
50  throw mapgen_exception(ss.str());
51  }
52 
53  return generator->create_map();
54 }
55 
56 config random_generate_scenario(const std::string& name, const config &cfg, const config* vars)
57 {
58  std::unique_ptr<map_generator> generator(create_map_generator(name, cfg, vars));
59  if(generator == nullptr) {
60  std::stringstream ss;
61  ss << "could not find map generator '" << name << "'";
62  throw mapgen_exception(ss.str());
63  }
64 
65  return generator->create_scenario();
66 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Standard logging facilities (interface).
std::string random_generate_map(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:44
config random_generate_scenario(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:56
map_generator * create_map_generator(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:28
#define ERR_CF
Definition: map_create.cpp:26
static lg::log_domain log_config("config")
rng * generator
This generator is automatically synced during synced context.
Definition: random.cpp:60