The Battle for Wesnoth  1.19.0-dev
game_data.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 /**
17  * @file
18  * Maintain game variables + misc.
19  */
20 
21 #include "game_data.hpp"
22 
23 #include "log.hpp" //LOG_STREAM
24 #include "variable.hpp" //scoped_wml_variable
26 #include "utils/ranges.hpp"
27 
28 static lg::log_domain log_engine("engine");
29 #define ERR_NG LOG_STREAM(err, log_engine)
30 #define WRN_NG LOG_STREAM(warn, log_engine)
31 #define LOG_NG LOG_STREAM(info, log_engine)
32 #define DBG_NG LOG_STREAM(debug, log_engine)
33 
35  : variable_set()
36  , scoped_variables()
37  , last_selected(map_location::null_location())
38  , rng_(level)
39  , variables_(level.child_or_empty("variables"))
40  , phase_(INITIAL)
41  , can_end_turn_(level["can_end_turn"].to_bool(true))
42  , end_turn_forced_(level["end_turn"].to_bool())
43  , cannot_end_turn_reason_(level["cannot_end_turn_reason"].t_str())
44  , next_scenario_(level["next_scenario"])
45  , id_(level["id"])
46  , theme_(level["theme"])
47  , defeat_music_(utils::split(level["defeat_music"]))
48  , victory_music_(utils::split(level["victory_music"]))
49 {
50 }
51 
53  : variable_set() // variable set is just an interface.
54  , scoped_variables(data.scoped_variables)
55  , last_selected(data.last_selected)
56  , rng_(data.rng_)
57  , variables_(data.variables_)
58  , phase_(data.phase_)
59  , can_end_turn_(data.can_end_turn_)
60  , end_turn_forced_(data.end_turn_forced_)
61  , next_scenario_(data.next_scenario_)
62 {
63  //TODO: is there a reason why this cctor is not "=default" ? (or whether it is used in the first place)
64 }
65 //throws
67 {
69 }
70 
72 {
73  try
74  {
75  return get_variable_access_read(key).as_scalar();
76  }
77  catch(const invalid_variablename_exception&)
78  {
79  return config::attribute_value();
80  }
81 }
82 //throws
83 config& game_data::get_variable_cfg(const std::string& key)
84 {
86 }
87 
88 void game_data::set_variable(const std::string& key, const t_string& value)
89 {
90  try
91  {
92  get_variable(key) = value;
93  }
94  catch(const invalid_variablename_exception&)
95  {
96  ERR_NG << "variable " << key << "cannot be set to " << value;
97  }
98 }
99 //throws
100 config& game_data::add_variable_cfg(const std::string& key, const config& value)
101 {
102  std::vector<config> temp {value};
103  return get_variable_access_write(key).append_array(temp).front();
104 }
105 
106 void game_data::clear_variable_cfg(const std::string& varname)
107 {
108  try
109  {
110  get_variable_access_throw(varname).clear(true);
111  }
112  catch(const invalid_variablename_exception&)
113  {
114  //variable doesn't exist, nothing to delete
115  }
116 }
117 
118 void game_data::clear_variable(const std::string& varname)
119 {
120  try
121  {
122  get_variable_access_throw(varname).clear(false);
123  }
124  catch(const invalid_variablename_exception&)
125  {
126  //variable doesn't exist, nothing to delete
127  }
128 }
129 
131 {
132  write_phase(cfg, phase_);
133  cfg["next_scenario"] = next_scenario_;
134  cfg["id"] = id_;
135  cfg["theme"] = theme_;
136  cfg["defeat_music"] = utils::join(defeat_music_);
137  cfg["victory_music"] = utils::join(victory_music_);
138 
139  cfg["can_end_turn"] = can_end_turn_;
140  cfg["cannot_end_turn_reason"] = cannot_end_turn_reason_;
141 
142  cfg["random_seed"] = rng_.get_random_seed_str();
143  cfg["random_calls"] = rng_.get_random_calls();
144 
145  cfg.add_child("variables", variables_);
146 
147 }
148 
149 namespace {
150  bool recursive_activation = false;
151 
152 } // end anonymous namespace
153 
154 void game_data::activate_scope_variable(std::string var_name) const
155 {
156  if (recursive_activation) {
157  return;
158  }
159 
160  const std::string::iterator itor = std::find(var_name.begin(),var_name.end(),'.');
161  if(itor != var_name.end()) {
162  var_name.erase(itor, var_name.end());
163  }
164 
166  if (v->name() == var_name) {
167  recursive_activation = true;
168  if (!v->activated()) {
169  v->activate();
170  }
171  recursive_activation = false;
172  break;
173  }
174  }
175 }
176 
178 {
179  if(cfg["playing_team"].empty()) {
180  return game_data::PRELOAD;
181  }
182  if(!cfg["init_side_done"]) {
184  }
185  if(cfg.has_child("end_level_data")) {
186  return game_data::GAME_ENDED;
187  }
189 }
190 
191 
193 {
194  return phase() == TURN_STARTING || phase() == TURN_PLAYING || phase() == TURN_ENDED;
195 }
196 
198 {
199  return phase() == INITIAL || phase() == PRELOAD || phase() == PRESTART;
200 }
201 
203 {
204  return !(phase() == INITIAL || phase() == PRELOAD || phase() == PRESTART || phase() == START);
205 }
206 
208 {
209  cfg["init_side_done"] = !(phase == INITIAL || phase == PRELOAD || phase == PRESTART || phase == TURN_STARTING_WAITING);
210 }
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
config_attribute_value attribute_value
Variant for storing WML attributes.
Definition: config.hpp:292
bool has_child(config_key_type key) const
Determine whether a config has a child or not.
Definition: config.cpp:318
config & add_child(config_key_type key)
Definition: config.cpp:442
void clear_variable(const std::string &varname)
Clears attributes config children does nothing if varname is no valid variable name.
Definition: game_data.cpp:118
std::vector< scoped_wml_variable * > scoped_variables
Definition: game_data.hpp:32
PHASE phase_
Definition: game_data.hpp:159
t_string cannot_end_turn_reason_
Definition: game_data.hpp:162
virtual config::attribute_value get_variable_const(const std::string &varname) const
returns a blank attribute value if varname is no valid variable name.
Definition: game_data.cpp:71
bool is_before_screen() const
Definition: game_data.cpp:197
void clear_variable_cfg(const std::string &varname)
Clears only the config children does nothing if varname is no valid variable name.
Definition: game_data.cpp:106
@ PRELOAD
the preload [event] is fired next phase: PRESTART (normal game), TURN_STARTING_WAITING (reloaded game...
Definition: game_data.hpp:77
@ INITIAL
creating intitial [unit]s, executing toplevel [lua] etc.
Definition: game_data.hpp:74
@ GAME_ENDED
The game has ended and the user is observing the final state "lingering" The game can be saved here.
Definition: game_data.hpp:103
@ PRESTART
the prestart [event] is fired next phase: START (default), GAME_ENDING
Definition: game_data.hpp:80
@ TURN_PLAYING
The User is controlling the game and invoking actions The game can be saved here.
Definition: game_data.hpp:94
@ TURN_ENDED
The turn_end, side_turn_end etc [events] are fired next phase: TURN_STARTING_WAITING (default),...
Definition: game_data.hpp:97
@ TURN_STARTING_WAITING
we are waiting for the turn to start.
Definition: game_data.hpp:87
@ START
the start [event] is fired next phase: TURN_STARTING_WAITING (default), GAME_ENDING
Definition: game_data.hpp:83
@ TURN_STARTING
the turn, side turn etc.
Definition: game_data.hpp:90
static PHASE read_phase(const config &cfg)
Definition: game_data.cpp:177
PHASE phase() const
Definition: game_data.hpp:106
bool can_end_turn_
Definition: game_data.hpp:160
variable_access_throw get_variable_access_throw(const std::string &varname)
Used to delete variables.
Definition: game_data.hpp:151
void activate_scope_variable(std::string var_name) const
Definition: game_data.cpp:154
bool has_current_player() const
returns where there is currently a well defiend "current player", that is for example not the case du...
Definition: game_data.cpp:192
std::string id_
Definition: game_data.hpp:166
variable_access_create get_variable_access_write(const std::string &varname)
returns a variable_access that can be used to change the game variables
Definition: game_data.hpp:52
std::string theme_
Definition: game_data.hpp:167
config & get_variable_cfg(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:83
void write_snapshot(config &cfg) const
Definition: game_data.cpp:130
variable_access_const get_variable_access_read(const std::string &varname) const
returns a variable_access that cannot be used to change the game variables
Definition: game_data.hpp:46
static void write_phase(config &cfg, game_data::PHASE phase)
Definition: game_data.cpp:207
randomness::mt_rng rng_
Definition: game_data.hpp:157
config::attribute_value & get_variable(const std::string &varname)
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:66
config & add_variable_cfg(const std::string &varname, const config &value=config())
throws invalid_variablename_exception if varname is no valid variable name.
Definition: game_data.cpp:100
config variables_
Definition: game_data.hpp:158
std::vector< std::string > victory_music_
Definition: game_data.hpp:169
game_data(const config &level)
Definition: game_data.cpp:34
std::string next_scenario_
the scenario coming next (for campaigns)
Definition: game_data.hpp:164
std::vector< std::string > defeat_music_
Definition: game_data.hpp:168
bool is_after_start() const
Definition: game_data.cpp:202
void set_variable(const std::string &varname, const t_string &value)
does nothing if varname is no valid variable name.
Definition: game_data.cpp:88
std::string get_random_seed_str() const
Definition: mt_rng.cpp:100
unsigned int get_random_calls() const
Definition: mt_rng.hpp:56
void clear(bool only_tables=false) const
Clears the value this object points to.
config::child_itors append_array(std::vector< config > children) const
maybe_const_t< config::attribute_value, V > & as_scalar() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
maybe_const_t< config, V > & as_container() const
If instantiated with vi_policy_const, the lifetime of the returned const attribute_value reference mi...
static lg::log_domain log_engine("engine")
#define ERR_NG
Definition: game_data.cpp:29
Standard logging facilities (interface).
auto reversed_view(T &container)
Definition: ranges.hpp:28
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::vector< std::string > split(const config_attribute_value &val)
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
std::string_view data
Definition: picture.cpp:199
Encapsulates the map of the game.
Definition: location.hpp:38