The Battle for Wesnoth  1.17.0-dev
game_classification.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
3  by David White <dave@whitevine.net>
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 
16 #include "game_classification.hpp"
17 
18 #include "config.hpp"
19 #include "log.hpp"
21 #include "game_version.hpp"
22 #include "game_config_manager.hpp"
23 
24 #include <list>
25 
26 static lg::log_domain log_engine("engine");
27 #define ERR_NG LOG_STREAM(err, log_engine)
28 #define WRN_NG LOG_STREAM(warn, log_engine)
29 #define LOG_NG LOG_STREAM(info, log_engine)
30 #define DBG_NG LOG_STREAM(debug, log_engine)
31 
32 /** The default difficulty setting for campaigns. */
33 const std::string DEFAULT_DIFFICULTY("NORMAL");
34 
36  : label(cfg["label"])
37  , version(cfg["version"])
38  , campaign_type(
39  cfg["campaign_type"].to_enum<game_classification::CAMPAIGN_TYPE>(game_classification::CAMPAIGN_TYPE::SCENARIO))
40  , campaign_define(cfg["campaign_define"])
41  , campaign_xtra_defines(utils::split(cfg["campaign_extra_defines"]))
42  , scenario_define(cfg["scenario_define"])
43  , era_define(cfg["era_define"])
44  , mod_defines(utils::split(cfg["mod_defines"]))
45  , active_mods(utils::split(cfg["active_mods"]))
46  , era_id(cfg["era_id"])
47  , campaign(cfg["campaign"])
48  , campaign_name(cfg["campaign_name"])
49  , abbrev(cfg["abbrev"])
50  , end_credits(cfg["end_credits"].to_bool(true))
51  , end_text(cfg["end_text"])
52  , end_text_duration(std::clamp<unsigned>(cfg["end_text_duration"].to_unsigned(0), 0, 5000))
53  , difficulty(cfg["difficulty"].empty() ? DEFAULT_DIFFICULTY : cfg["difficulty"].str())
54  , random_mode(cfg["random_mode"])
55  , oos_debug(cfg["oos_debug"].to_bool(false))
56 {
57 }
58 
60 {
61  config cfg;
62  cfg["label"] = label;
63  cfg["version"] = game_config::wesnoth_version.str();
64  cfg["campaign_type"] = campaign_type.to_string();
65  cfg["campaign_define"] = campaign_define;
66  cfg["campaign_extra_defines"] = utils::join(campaign_xtra_defines);
67  cfg["scenario_define"] = scenario_define;
68  cfg["era_define"] = era_define;
69  cfg["mod_defines"] = utils::join(mod_defines);
70  cfg["active_mods"] = utils::join(active_mods);
71  cfg["era_id"] = era_id;
72  cfg["campaign"] = campaign;
73  cfg["campaign_name"] = campaign_name;
74  cfg["abbrev"] = abbrev;
75  cfg["end_credits"] = end_credits;
76  cfg["end_text"] = end_text;
77  cfg["end_text_duration"] = std::to_string(end_text_duration);
78  cfg["difficulty"] = difficulty;
79  cfg["random_mode"] = random_mode;
80  cfg["oos_debug"] = oos_debug;
81 
82  return cfg;
83 }
84 
86 {
87  if(is_multiplayer()) {
88  return campaign.empty() ? "multiplayer" : "scenario";
89  }
90 
91  if(is_tutorial()) {
92  return "scenario";
93  }
94 
95  return campaign_type.to_string();
96 }
97 
98 namespace
99 {
100 // helper objects for saved_game::expand_mp_events()
101 struct modevents_entry
102 {
103  modevents_entry(const std::string& _type, const std::string& _id)
104  : type(_type)
105  , id(_id)
106  {
107  }
108 
109  std::string type;
110  std::string id;
111 };
112 }
113 
114 std::set<std::string> game_classification::active_addons(const std::string& scenario_id) const
115 {
116  //FIXME: this doesn't include mods from the current scenario.
117  std::list<modevents_entry> mods;
118  std::set<std::string> loaded_resources;
119  std::set<std::string> res;
120 
121  std::transform(active_mods.begin(), active_mods.end(), std::back_inserter(mods),
122  [](const std::string& id) { return modevents_entry("modification", id); }
123  );
124 
125  // We don't want the error message below if there is no era (= if this is a sp game).
126  if(!era_id.empty()) {
127  mods.emplace_back(get_tagname(), scenario_id);
128  }
129 
130  if(!era_id.empty()) {
131  mods.emplace_back("era", era_id);
132  }
133 
134  if(!campaign.empty()) {
135  mods.emplace_back("campaign", campaign);
136  }
137  while(!mods.empty()) {
138 
139  const modevents_entry& current = mods.front();
140  if(current.type == "resource") {
141  if(!loaded_resources.insert(current.id).second) {
142  mods.pop_front();
143  continue;
144  }
145  }
146  if(const config& cfg = game_config_manager::get()->game_config().find_child(current.type, "id", current.id)) {
147  if(!cfg["addon_id"].empty()) {
148  res.insert(cfg["addon_id"]);
149  }
150  for (const config& load_res : cfg.child_range("load_resource")) {
151  mods.emplace_back("resource", load_res["id"].str());
152  }
153  }
154  mods.pop_front( );
155  }
156  return res;
157 }
unsigned int end_text_duration
for how long the end-of-campaign text is shown
bool end_credits
whether to show the standard credits at the end
std::string label
Name of the game (e.g.
Interfaces for manipulating version numbers of engine, add-ons, etc.
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
child_itors child_range(config_key_type key)
Definition: config.cpp:344
std::string campaign_name
The name of the campaign being played.
STL namespace.
const std::string DEFAULT_DIFFICULTY("NORMAL")
The default difficulty setting for campaigns.
Definitions for the interface to Wesnoth Markup Language (WML).
static lg::log_domain log_engine("engine")
static game_config_manager * get()
std::set< std::string > active_addons(const std::string &scenario_id) const
std::string label
What to show in the filter&#39;s drop-down list.
Definition: manager.cpp:217
std::vector< std::string > active_mods
std::string campaign_define
If there is a define the campaign uses to customize data.
std::string end_text
end-of-campaign text
std::string campaign
The id of the campaign being played.
std::string era_define
If there is a define the era uses to customize data.
std::string abbrev
the campaign abbreviation
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
std::vector< std::string > campaign_xtra_defines
more customization of data
Game configuration data as global variables.
Definition: build_info.cpp:59
std::string scenario_define
If there is a define the scenario uses to customize data.
std::vector< std::string > mod_defines
If there are defines the modifications use to customize data.
game_classification()=default
const version_info wesnoth_version(VERSION)
std::string difficulty
The difficulty level the game is being played on.
std::vector< std::string > split(const config_attribute_value &val)
Standard logging facilities (interface).
std::string str() const
Serializes the version number into string form.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
std::string get_tagname() const