The Battle for Wesnoth  1.15.0-dev
map_create.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
16 
20 #include "log.hpp"
21 
22 #include <cassert>
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.\n";
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 }
static lg::log_domain log_config("config")
virtual config create_scenario(boost::optional< uint32_t > randomseed=boost::none)
std::string random_generate_map(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:44
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
#define ERR_CF
Definition: map_create.cpp:26
virtual std::string create_map(boost::optional< uint32_t > randomseed=boost::none)=0
Creates a new map and returns it.
map_generator * create_map_generator(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:28
rng * generator
This generator is automatically synced during synced context.
Definition: random.cpp:60
config random_generate_scenario(const std::string &name, const config &cfg, const config *vars)
Definition: map_create.cpp:56
Standard logging facilities (interface).
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92