The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_map_generator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2017 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project http://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 <string>
22 
24  : id_(cfg["id"])
25  , config_name_(cfg["config_name"])
26  , user_config_(cfg["user_config"])
27  , create_map_(cfg["create_map"])
28  , create_scenario_(cfg["create_scenario"])
29  , lk_()
30  , generator_data_(cfg)
31 {
32  lk_.load_core();
33  const char* required[] {"id", "config_name", "create_map"};
34  for (std::string req : required) {
35  if (!cfg.has_attribute(req)) {
36  if(req == "create_map" && cfg.has_attribute("create_scenario")) {
37  // One of these is required, but not both
38  continue;
39  }
40  std::string msg = "Error when constructing a lua map generator -- missing a required attribute '";
41  msg += req + "'\n";
42  msg += "Config was '" + cfg.debug() + "'";
43  throw mapgen_exception(msg);
44  }
45  }
46 }
47 
49 {
50  try {
52  } catch (game::lua_error & e) {
53  std::string msg = "Error when running lua_map_generator user_config.\n";
54  msg += "The generator was: " + config_name_ + "\n";
55  msg += e.what();
56  throw mapgen_exception(msg);
57  }
58 }
59 
60 std::string lua_map_generator::create_map(boost::optional<uint32_t> seed)
61 {
62  if(create_map_.empty()) {
63  return map_generator::create_map(seed);
64  }
65 
66  try {
67  return lk_.create_map(create_map_.c_str(), generator_data_, seed);
68  } catch (game::lua_error & e) {
69  std::string msg = "Error when running lua_map_generator create_map.\n";
70  msg += "The generator was: " + config_name_ + "\n";
71  msg += e.what();
72  throw mapgen_exception(msg);
73  }
74 }
75 
76 config lua_map_generator::create_scenario(boost::optional<uint32_t> seed)
77 {
78  if(create_scenario_.empty()) {
79  return map_generator::create_scenario(seed);
80  }
81 
82  try {
83  return lk_.create_scenario(create_scenario_.c_str(), generator_data_, seed);
84  } catch (game::lua_error & e) {
85  std::string msg = "Error when running lua_map_generator create_scenario.\n";
86  msg += "The generator was: " + config_name_ + "\n";
87  msg += e.what();
88  throw mapgen_exception(msg);
89  }
90 }
const char * what() const NOEXCEPT
Definition: exceptions.hpp:37
std::vector< char_t > string
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)
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:110
std::string debug() const
Definition: config.cpp:1192
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.
virtual std::string create_map(boost::optional< uint32_t > randomseed=boost::none)=0
Creates a new map and returns it.
bool has_attribute(config_key_type key) const
Definition: config.cpp:196
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:93
mapgen_lua_kernel lk_
Error used to report an error in a lua script or in the lua interpreter.
Definition: game_errors.hpp:53