The Battle for Wesnoth  1.19.7+dev
lua_map_generator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
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  using namespace std::string_literals;
41  const std::array required {"id"s, "config_name"s, "create_map"s};
42  for(const auto& req : required) {
43  if (!cfg.has_attribute(req)) {
44  if(req == "create_map" && cfg.has_attribute("create_scenario")) {
45  // One of these is required, but not both
46  continue;
47  }
48  std::string msg = "Error when constructing a lua map generator -- missing a required attribute '";
49  msg += req + "'\n";
50  msg += "Config was '" + cfg.debug() + "'";
51  throw mapgen_exception(msg);
52  }
53  }
54 }
55 
57 {
58  try {
60  } catch(const game::lua_error & e) {
61  std::string msg = "Error when running lua_map_generator user_config.\n";
62  msg += "The generator was: " + config_name_ + "\n";
63  msg += e.what();
64  throw mapgen_exception(msg);
65  }
66 }
67 
68 std::string lua_map_generator::create_map(utils::optional<uint32_t> seed)
69 {
70  if(create_map_.empty()) {
71  return map_generator::create_map(seed);
72  }
73 
74  try {
75  return lk_.create_map(create_map_.c_str(), generator_data_, seed);
76  } catch (const game::lua_error & e) {
77  std::string msg = "Error when running lua_map_generator create_map.\n";
78  msg += "The generator was: " + config_name_ + "\n";
79  msg += e.what();
80  throw mapgen_exception(msg);
81  }
82 }
83 
84 config lua_map_generator::create_scenario(utils::optional<uint32_t> seed)
85 {
86  if(create_scenario_.empty()) {
87  return map_generator::create_scenario(seed);
88  }
89 
90  try {
91  return lk_.create_scenario(create_scenario_.c_str(), generator_data_, seed);
92  } catch (const game::lua_error & e) {
93  std::string msg = "Error when running lua_map_generator create_scenario.\n";
94  msg += "The generator was: " + config_name_ + "\n";
95  msg += e.what();
96  ERR_NG << msg;
97  throw mapgen_exception(msg);
98  }
99 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
bool has_attribute(config_key_type key) const
Definition: config.cpp:157
std::string debug() const
Definition: config.cpp:1240
void load_core()
Loads the "core" library into the Lua environment.
std::string create_scenario_
mapgen_lua_kernel lk_
lua_map_generator(const config &cfg, const config *vars)
virtual void user_config() override
Display the interactive screen, which allows the user to modify how the generator behaves.
virtual std::string create_map(utils::optional< uint32_t > randomseed) override
Creates a new map and returns it.
virtual config create_scenario(utils::optional< uint32_t > randomseed) override
virtual config create_scenario(utils::optional< uint32_t > randomseed={})
virtual std::string create_map(utils::optional< uint32_t > randomseed={})=0
Creates a new map and returns it.
void user_config(const char *prog, const config &generator)
std::string create_map(const char *prog, const config &generator, utils::optional< uint32_t > seed)
config create_scenario(const char *prog, const config &generator, utils::optional< uint32_t > seed)
Definitions for the interface to Wesnoth Markup Language (WML).
Standard logging facilities (interface).
#define ERR_NG
static lg::log_domain log_mapgen("mapgen")
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
Error used to report an error in a lua script or in the lua interpreter.
Definition: game_errors.hpp:54
static map_location::direction s
#define e