The Battle for Wesnoth  1.13.4+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 - 2016 by Chris Beck <[email protected]>
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 
15 #include "lua_map_generator.hpp"
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  const char* required[] = {"id", "config_name", "create_map"};
33  for (std::string req : required) {
34  if (!cfg.has_attribute(req)) {
35  std::string msg = "Error when constructing a lua map generator -- missing a required attribute '";
36  msg += req + "'\n";
37  msg += "Config was '" + cfg.debug() + "'";
38  throw mapgen_exception(msg);
39  }
40  }
41 }
42 
44 {
45  lk_.set_video(&v);
46  try {
48  } catch (game::lua_error & e) {
49  std::string msg = "Error when running lua_map_generator user_config.\n";
50  msg += "The generator was: " + config_name_ + "\n";
51  msg += e.what();
52  throw mapgen_exception(msg);
53  }
54 }
55 
56 std::string lua_map_generator::create_map(boost::optional<boost::uint32_t> seed)
57 {
58  try {
59  return lk_.create_map(create_map_.c_str(), generator_data_, seed);
60  } catch (game::lua_error & e) {
61  std::string msg = "Error when running lua_map_generator create_map.\n";
62  msg += "The generator was: " + config_name_ + "\n";
63  msg += e.what();
64  throw mapgen_exception(msg);
65  }
66 }
67 
68 config lua_map_generator::create_scenario(boost::optional<boost::uint32_t> seed)
69 {
70  if (!create_scenario_.size()) {
72  }
73 
74  try {
75  return lk_.create_scenario(create_scenario_.c_str(), generator_data_, seed);
76  } catch (game::lua_error & e) {
77  std::string msg = "Error when running lua_map_generator create_scenario.\n";
78  msg += "The generator was: " + config_name_ + "\n";
79  msg += e.what();
80  throw mapgen_exception(msg);
81  }
82 }
std::string id_
Definition: formula.cpp:636
const char * what() const
Definition: exceptions.hpp:35
Definition: video.hpp:58
virtual std::string create_map(boost::optional< boost::uint32_t > randomseed)
Creates a new map and returns it.
std::string debug() const
Definition: config.cpp:1438
virtual config create_scenario(boost::optional< boost::uint32_t > randomseed=boost::none)
Definitions for the interface to Wesnoth Markup Language (WML).
config create_scenario(const char *prog, const config &generator, boost::optional< boost::uint32_t > seed)
virtual void user_config(CVideo &v)
Display the interactive screen, which allows the user to modify how the generator behaves...
virtual config create_scenario(boost::optional< boost::uint32_t > randomseed)
const GLdouble * v
Definition: glew.h:1359
void user_config(const char *prog, const config &generator)
void set_video(CVideo *ptr)
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
lua_map_generator(const config &cfg)
bool has_attribute(const std::string &key) const
Definition: config.cpp:514
#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:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
mapgen_lua_kernel lk_
std::string create_map(const char *prog, const config &generator, boost::optional< boost::uint32_t > seed)
Error used to report an error in a lua script or in the lua interpreter.
Definition: game_errors.hpp:53