The Battle for Wesnoth  1.15.0-dev
lua_map_generator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
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 
17 #include "config.hpp"
18 #include "game_errors.hpp"
20 
21 #include <array>
22 #include <string>
23 
25  : id_(cfg["id"])
26  , config_name_(cfg["config_name"])
27  , user_config_(cfg["user_config"])
28  , create_map_(cfg["create_map"])
29  , create_scenario_(cfg["create_scenario"])
30  , lk_()
31  , generator_data_(cfg)
32 {
33  lk_.load_core();
34  const std::array<std::string, 3> required {{"id", "config_name", "create_map"}};
35  for(const std::string& req : required) {
36  if (!cfg.has_attribute(req)) {
37  if(req == "create_map" && cfg.has_attribute("create_scenario")) {
38  // One of these is required, but not both
39  continue;
40  }
41  std::string msg = "Error when constructing a lua map generator -- missing a required attribute '";
42  msg += req + "'\n";
43  msg += "Config was '" + cfg.debug() + "'";
44  throw mapgen_exception(msg);
45  }
46  }
47 }
48 
50 {
51  try {
53  } catch(const game::lua_error & e) {
54  std::string msg = "Error when running lua_map_generator user_config.\n";
55  msg += "The generator was: " + config_name_ + "\n";
56  msg += e.what();
57  throw mapgen_exception(msg);
58  }
59 }
60 
61 std::string lua_map_generator::create_map(boost::optional<uint32_t> seed)
62 {
63  if(create_map_.empty()) {
64  return map_generator::create_map(seed);
65  }
66 
67  try {
68  return lk_.create_map(create_map_.c_str(), generator_data_, seed);
69  } catch (const game::lua_error & e) {
70  std::string msg = "Error when running lua_map_generator create_map.\n";
71  msg += "The generator was: " + config_name_ + "\n";
72  msg += e.what();
73  throw mapgen_exception(msg);
74  }
75 }
76 
77 config lua_map_generator::create_scenario(boost::optional<uint32_t> seed)
78 {
79  if(create_scenario_.empty()) {
80  return map_generator::create_scenario(seed);
81  }
82 
83  try {
84  return lk_.create_scenario(create_scenario_.c_str(), generator_data_, seed);
85  } catch (const game::lua_error & e) {
86  std::string msg = "Error when running lua_map_generator create_scenario.\n";
87  msg += "The generator was: " + config_name_ + "\n";
88  msg += e.what();
89  throw mapgen_exception(msg);
90  }
91 }
std::string create_map(const char *prog, const config &generator, boost::optional< uint32_t > seed)
config create_scenario(const char *prog, const config &generator, boost::optional< uint32_t > seed)
bool has_attribute(config_key_type key) const
Definition: config.cpp:217
virtual config create_scenario(boost::optional< uint32_t > randomseed=boost::none)
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:108
Definitions for the interface to Wesnoth Markup Language (WML).
virtual void user_config() override
Display the interactive screen, which allows the user to modify how the generator behaves...
void load_core()
Loads the "core" library into the Lua environment.
void user_config(const char *prog, const config &generator)
virtual std::string create_map(boost::optional< uint32_t > randomseed) override
Creates a new map and returns it.
const char * what() const noexcept
Definition: exceptions.hpp:37
virtual std::string create_map(boost::optional< uint32_t > randomseed=boost::none)=0
Creates a new map and returns it.
virtual config create_scenario(boost::optional< uint32_t > randomseed) override
lua_map_generator(const config &cfg)
#define e
std::string create_scenario_
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
mapgen_lua_kernel lk_
std::string debug() const
Definition: config.cpp:1266
Error used to report an error in a lua script or in the lua interpreter.
Definition: game_errors.hpp:53