The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
game_data.hpp
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#pragma once
17
18#include "config.hpp"
20#include "map/location.hpp"
21#include "mt_rng.hpp"
22#include "variable_info.hpp"
23
25
26class game_data : public variable_set {
27public:
28 explicit game_data(const config& level);
29 game_data(const game_data& data);
30
31 std::vector<scoped_wml_variable*> scoped_variables;
32
33 const config& get_variables() const { return variables_; }
34 /** throws invalid_variablename_exception if varname is no valid variable name. */
35 config::attribute_value &get_variable(const std::string &varname);
36 /** returns a blank attribute value if varname is no valid variable name. */
37 virtual config::attribute_value get_variable_const(const std::string& varname) const;
38 /** throws invalid_variablename_exception if varname is no valid variable name. */
39 config& get_variable_cfg(const std::string& varname);
40 /** does nothing if varname is no valid variable name. */
41 void set_variable(const std::string& varname, const config::attribute_value& value);
42 /** throws invalid_variablename_exception if varname is no valid variable name. */
43 config& add_variable_cfg(const std::string& varname, const config& value=config());
44 /** returns a variable_access that cannot be used to change the game variables */
45 variable_access_const get_variable_access_read(const std::string& varname) const
46 {
48 return variable_access_const(varname, variables_);
49 }
50 /** returns a variable_access that can be used to change the game variables */
52 {
54 return variable_access_create(varname, variables_);
55 }
56 /**
57 * Clears attributes config children
58 * does nothing if varname is no valid variable name.
59 */
60 void clear_variable(const std::string& varname);
61 /**
62 * Clears only the config children
63 * does nothing if varname is no valid variable name.
64 */
65 void clear_variable_cfg(const std::string& varname);
66
67 const randomness::mt_rng& rng() const { return rng_; }
69
70 enum PHASE {
71 /// creating intitial [unit]s, executing toplevel [lua] etc.
72 /// next phase: PRELOAD
74 /// the preload [event] is fired
75 /// next phase: PRESTART (normal game), TURN_STARTING_WAITING (reloaded game), TURN_PLAYING (reloaded game) or GAME_ENDED (reloadedgame)
77 /// the prestart [event] is fired
78 /// next phase: START (default), GAME_ENDING
80 /// the start [event] is fired
81 /// next phase: TURN_STARTING_WAITING (default), GAME_ENDING
83 /// we are waiting for the turn to start.
84 /// The game can be saved here.
85 /// next phase: TURN_STARTING
87 /// the turn, side turn etc. [event]s are being fired
88 /// next phase: TURN_PLAYING (default), GAME_ENDING
90 /// The User is controlling the game and invoking actions
91 /// The game can be saved here.
92 /// next phase: TURN_PLAYING (default), GAME_ENDING
94 /// The turn_end, side_turn_end etc [events] are fired
95 /// next phase: TURN_STARTING_WAITING (default), GAME_ENDING
97 /// The victory etc. [event]s are fired.
98 /// next phase: GAME_ENDED
100 /// The game has ended and the user is observing the final state "lingering"
101 /// The game can be saved here.
103 };
104
105 PHASE phase() const { return phase_; }
107 /// returns where there is currently a well defiend "current player",
108 /// that is for example not the case during start events or during linger mode.
109 bool has_current_player() const;
110 bool is_before_screen() const;
111 bool is_after_start() const;
112
113 static PHASE read_phase(const config& cfg);
115
119 bool allow_end_turn() const { return can_end_turn_; }
120 void set_allow_end_turn(bool value, const t_string& reason = {}) {
123 }
124
125 /** the last location where a select event fired. Used by wml menu items with needs_select=yes*/
127
128 void write_snapshot(config& cfg) const;
129
130 const std::string& next_scenario() const { return next_scenario_; }
132
133 const std::string& get_id() const { return id_; }
134 void set_id(const std::string& value) { id_ = value; }
135
136 const std::string& get_theme() const { return theme_; }
137 void set_theme(const std::string& value) { theme_ = value; }
138
139 const std::vector<std::string>& get_defeat_music() const { return defeat_music_; }
140 void set_defeat_music(std::vector<std::string> value) { defeat_music_ = std::move(value); }
141
142 const std::vector<std::string>& get_victory_music() const { return victory_music_; }
143 void set_victory_music(std::vector<std::string> value) { victory_music_ = std::move(value); }
144
146 bool end_turn_forced() const { return end_turn_forced_; }
147private:
148 void activate_scope_variable(std::string var_name) const;
149 /** Used to delete variables. */
151 {
153 return variable_access_throw(varname, variables_);
154 }
155
162 /** the scenario coming next (for campaigns) */
163 std::string next_scenario_;
164 // the id of a scenario cannot change during a scenario
165 std::string id_;
166 std::string theme_;
167 std::vector<std::string> defeat_music_;
168 std::vector<std::string> victory_music_;
169};
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
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
void set_allow_end_turn(bool value, const t_string &reason={})
PHASE phase_
bool allow_end_turn() const
t_string cannot_end_turn_reason_
const std::string & get_theme() const
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
void set_end_turn_forced(bool v)
void set_phase(PHASE phase)
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.
void set_defeat_music(std::vector< std::string > value)
@ 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
@ GAME_ENDING
The victory etc.
Definition game_data.hpp:99
@ 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_
map_location last_selected
the last location where a select event fired.
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
const std::vector< std::string > & get_victory_music() const
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
const std::vector< std::string > & get_defeat_music() const
void set_theme(const std::string &value)
const std::string & get_id() const
bool end_turn_forced() const
bool end_turn_forced_
void set_next_scenario(const std::string &next_scenario)
const config & get_variables() const
Definition game_data.hpp:33
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
void set_victory_music(std::vector< std::string > value)
static void write_phase(config &cfg, game_data::PHASE phase)
const t_string & cannot_end_turn_reason()
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
void set_id(const std::string &value)
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
const std::string & next_scenario() const
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_
const randomness::mt_rng & rng() const
Definition game_data.hpp:67
randomness::mt_rng & rng()
Definition game_data.hpp:68
std::string next_scenario_
the scenario coming next (for campaigns)
std::vector< std::string > defeat_music_
bool is_after_start() const
Additional functionality for a non-const variable_info.
Information on a WML variable.
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
Contains the exception interfaces used to signal completion of a scenario, campaign or turn.
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
variable_info< const variable_info_implementation::vi_policy_const > variable_access_const
Read-only access.
variable_info_mutable< variable_info_implementation::vi_policy_throw > variable_access_throw
'Throw if nonexistent' access.
variable_info_mutable< variable_info_implementation::vi_policy_create > variable_access_create
'Create if nonexistent' access.