The Battle for Wesnoth  1.17.0-dev
lua_map_generator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  by Chris Beck <render787@gmail.com>
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 
18 #include "config.hpp"
19 #include "game_errors.hpp"
21 #include "log.hpp"
22 
23 #include <array>
24 #include <string>
25 
26 static lg::log_domain log_mapgen("mapgen");
27 #define ERR_NG LOG_STREAM(err, log_mapgen)
28 #define LOG_NG LOG_STREAM(info, log_mapgen)
29 
31  : id_(cfg["id"])
32  , config_name_(cfg["config_name"])
33  , user_config_(cfg["user_config"])
34  , create_map_(cfg["create_map"])
35  , create_scenario_(cfg["create_scenario"])
36  , lk_(vars)
37  , generator_data_(cfg)
38 {
39  lk_.load_core();
40  const std::array<std::string, 3> required {{"id", "config_name", "create_map"}};
41  for(const std::string& req : required) {
42  if (!cfg.has_attribute(req)) {
43  if(req == "create_map" && cfg.has_attribute("create_scenario")) {
44  // One of these is required, but not both
45  continue;
46  }
47  std::string msg = "Error when constructing a lua map generator -- missing a required attribute '";
48  msg += req + "'\n";
49  msg += "Config was '" + cfg.debug() + "'";
50  throw mapgen_exception(msg);
51  }
52  }
53 }
54 
56 {
57  try {
59  } catch(const game::lua_error & e) {
60  std::string msg = "Error when running lua_map_generator user_config.\n";
61  msg += "The generator was: " + config_name_ + "\n";
62  msg += e.what();
63  throw mapgen_exception(msg);
64  }
65 }
66 
67 std::string lua_map_generator::create_map(std::optional<uint32_t> seed)
68 {
69  if(create_map_.empty()) {
70  return map_generator::create_map(seed);
71  }
72 
73  try {
74  return lk_.create_map(create_map_.c_str(), generator_data_, seed);
75  } catch (const game::lua_error & e) {
76  std::string msg = "Error when running lua_map_generator create_map.\n";
77  msg += "The generator was: " + config_name_ + "\n";
78  msg += e.what();
79  throw mapgen_exception(msg);
80  }
81 }
82 
83 config lua_map_generator::create_scenario(std::optional<uint32_t> seed)
84 {
85  if(create_scenario_.empty()) {
86  return map_generator::create_scenario(seed);
87  }
88 
89  try {
90  return lk_.create_scenario(create_scenario_.c_str(), generator_data_, seed);
91  } catch (const game::lua_error & e) {
92  std::string msg = "Error when running lua_map_generator create_scenario.\n";
93  msg += "The generator was: " + config_name_ + "\n";
94  msg += e.what();
95  ERR_NG << msg;
96  throw mapgen_exception(msg);
97  }
98 }
static lg::log_domain log_mapgen("mapgen")
bool has_attribute(config_key_type key) const
Definition: config.cpp:211
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:110
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:36
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:61
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:1347
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:54