The Battle for Wesnoth  1.17.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 #include "log.hpp"
21 
22 #include <array>
23 #include <string>
24 
25 static lg::log_domain log_mapgen("mapgen");
26 #define ERR_NG LOG_STREAM(err, log_mapgen)
27 #define LOG_NG LOG_STREAM(info, log_mapgen)
28 
30  : id_(cfg["id"])
31  , config_name_(cfg["config_name"])
32  , user_config_(cfg["user_config"])
33  , create_map_(cfg["create_map"])
34  , create_scenario_(cfg["create_scenario"])
35  , lk_(vars)
36  , generator_data_(cfg)
37 {
38  lk_.load_core();
39  const std::array<std::string, 3> required {{"id", "config_name", "create_map"}};
40  for(const std::string& req : required) {
41  if (!cfg.has_attribute(req)) {
42  if(req == "create_map" && cfg.has_attribute("create_scenario")) {
43  // One of these is required, but not both
44  continue;
45  }
46  std::string msg = "Error when constructing a lua map generator -- missing a required attribute '";
47  msg += req + "'\n";
48  msg += "Config was '" + cfg.debug() + "'";
49  throw mapgen_exception(msg);
50  }
51  }
52 }
53 
55 {
56  try {
58  } catch(const game::lua_error & e) {
59  std::string msg = "Error when running lua_map_generator user_config.\n";
60  msg += "The generator was: " + config_name_ + "\n";
61  msg += e.what();
62  throw mapgen_exception(msg);
63  }
64 }
65 
66 std::string lua_map_generator::create_map(std::optional<uint32_t> seed)
67 {
68  if(create_map_.empty()) {
69  return map_generator::create_map(seed);
70  }
71 
72  try {
73  return lk_.create_map(create_map_.c_str(), generator_data_, seed);
74  } catch (const game::lua_error & e) {
75  std::string msg = "Error when running lua_map_generator create_map.\n";
76  msg += "The generator was: " + config_name_ + "\n";
77  msg += e.what();
78  throw mapgen_exception(msg);
79  }
80 }
81 
82 config lua_map_generator::create_scenario(std::optional<uint32_t> seed)
83 {
84  if(create_scenario_.empty()) {
85  return map_generator::create_scenario(seed);
86  }
87 
88  try {
89  return lk_.create_scenario(create_scenario_.c_str(), generator_data_, seed);
90  } catch (const game::lua_error & e) {
91  std::string msg = "Error when running lua_map_generator create_scenario.\n";
92  msg += "The generator was: " + config_name_ + "\n";
93  msg += e.what();
94  ERR_NG << msg;
95  throw mapgen_exception(msg);
96  }
97 }
static lg::log_domain log_mapgen("mapgen")
bool has_attribute(config_key_type key) const
Definition: config.cpp:207
lua_map_generator(const config &cfg, const config *vars)
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
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)
const char * what() const noexcept
Definition: exceptions.hpp:35
virtual config create_scenario(std::optional< uint32_t > randomseed={})
std::string create_map(const char *prog, const config &generator, std::optional< uint32_t > seed)
#define ERR_NG
config create_scenario(const char *prog, const config &generator, std::optional< uint32_t > seed)
Standard logging facilities (interface).
virtual std::string create_map(std::optional< uint32_t > randomseed) override
Creates a new map and returns it.
#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:59
virtual std::string create_map(std::optional< uint32_t > randomseed={})=0
Creates a new map and returns it.
mapgen_lua_kernel lk_
std::string debug() const
Definition: config.cpp:1322
virtual config create_scenario(std::optional< uint32_t > randomseed) override
Error used to report an error in a lua script or in the lua interpreter.
Definition: game_errors.hpp:53