The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
game_data.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
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
28static 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
70
72{
73 try
74 {
76 }
78 {
80 }
81}
82//throws
83config& game_data::get_variable_cfg(const std::string& key)
84{
86}
87
88void game_data::set_variable(const std::string& key, const config::attribute_value& value)
89{
90 try
91 {
92 get_variable(key) = value;
93 }
95 {
96 ERR_NG << "variable " << key << "cannot be set to " << value;
97 }
98}
99//throws
100config& 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
106void game_data::clear_variable_cfg(const std::string& varname)
107{
108 try
109 {
110 get_variable_access_throw(varname).clear(true);
111 }
113 {
114 //variable doesn't exist, nothing to delete
115 }
116}
117
118void game_data::clear_variable(const std::string& varname)
119{
120 try
121 {
122 get_variable_access_throw(varname).clear(false);
123 }
125 {
126 //variable doesn't exist, nothing to delete
127 }
128}
129
131{
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
149namespace {
150 bool recursive_activation = false;
151
152} // end anonymous namespace
153
154void 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"].to_bool()) {
184 }
185 if(cfg.has_child("end_level_data")) {
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}
#define ERR_NG
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
config & add_child(std::string_view key)
Definition config.cpp:436
config_attribute_value attribute_value
Variant for storing WML attributes.
Definition config.hpp:290
bool has_child(std::string_view key) const
Determine whether a config has a child or not.
Definition config.cpp:312
void clear_variable(const std::string &varname)
Clears attributes config children does nothing if varname is no valid variable name.
std::vector< scoped_wml_variable * > scoped_variables
Definition game_data.hpp:31
PHASE phase_
t_string cannot_end_turn_reason_
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
void clear_variable_cfg(const std::string &varname)
Clears only the config children does nothing if varname is no valid variable name.
@ PRELOAD
the preload [event] is fired next phase: PRESTART (normal game), TURN_STARTING_WAITING (reloaded game...
Definition game_data.hpp:76
@ INITIAL
creating intitial [unit]s, executing toplevel [lua] etc.
Definition game_data.hpp:73
@ GAME_ENDED
The game has ended and the user is observing the final state "lingering" The game can be saved here.
@ PRESTART
the prestart [event] is fired next phase: START (default), GAME_ENDING
Definition game_data.hpp:79
@ TURN_PLAYING
The User is controlling the game and invoking actions The game can be saved here.
Definition game_data.hpp:93
@ TURN_ENDED
The turn_end, side_turn_end etc [events] are fired next phase: TURN_STARTING_WAITING (default),...
Definition game_data.hpp:96
@ TURN_STARTING_WAITING
we are waiting for the turn to start.
Definition game_data.hpp:86
@ START
the start [event] is fired next phase: TURN_STARTING_WAITING (default), GAME_ENDING
Definition game_data.hpp:82
@ TURN_STARTING
the turn, side turn etc.
Definition game_data.hpp:89
static PHASE read_phase(const config &cfg)
PHASE phase() const
bool can_end_turn_
variable_access_throw get_variable_access_throw(const std::string &varname)
Used to delete variables.
void activate_scope_variable(std::string var_name) const
bool has_current_player() const
returns where there is currently a well defiend "current player", that is for example not the case du...
std::string id_
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:51
std::string theme_
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
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:45
static void write_phase(config &cfg, game_data::PHASE phase)
randomness::mt_rng rng_
void set_variable(const std::string &varname, const config::attribute_value &value)
does nothing if varname is no valid variable name.
Definition game_data.cpp:88
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.
config variables_
std::vector< std::string > victory_music_
game_data(const config &level)
Definition game_data.cpp:34
std::string next_scenario_
the scenario coming next (for campaigns)
std::vector< std::string > defeat_music_
bool is_after_start() const
std::string get_random_seed_str() const
Definition mt_rng.cpp:99
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...
const config * cfg
static lg::log_domain log_engine("engine")
Standard logging facilities (interface).
constexpr auto reverse
Definition ranges.hpp:44
std::string join(const Range &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::string_view data
Definition picture.cpp:188
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
Encapsulates the map of the game.
Definition location.hpp:46