The Battle for Wesnoth  1.19.16+dev
tod_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2025
3  by Eugen Jiresch
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 "map/location.hpp"
19 #include "config.hpp"
20 #include "time_of_day.hpp"
21 
22 class gamemap;
23 class unit_map;
24 class game_data;
25 
26 namespace randomness
27 {
28  class rng;
29 }
30 
31 //time of day and turn functionality
33 {
34 public:
35  explicit tod_manager(const config& scenario_cfg = config());
37  tod_manager& operator=(const tod_manager& manager) = default;
38 
39  config to_config(const std::string& textdomain = "") const;
40  /**
41  handles random_start_time, should be called before the game starts.
42  */
44  int get_current_time() const { return currentTime_; }
45  int get_current_time(const map_location& loc) const;
46 
47  void set_current_time(int time);
48  void set_current_time(int time, int area_index);
49  void set_current_time(int time, const std::string& area_id);
50  void set_area_id(int area_index, const std::string& id);
51  const std::string& get_area_id(int area_index) const;
52 
53  /**
54  * Returns global time of day for the passed turn.
55  * for_turn = 0 means current turn
56  */
57  const time_of_day& get_time_of_day(int for_turn = 0) const {
58  return get_time_of_day_turn(times_, for_turn ? for_turn : turn_, currentTime_);
59  }
60 
61  /**
62  * Returns time of day for the passed turn at a location.
63  * tod areas matter, for_turn = 0 means current turn
64  * ignoring illumination
65  */
66  const time_of_day& get_time_of_day(const map_location& loc, int for_turn = 0) const;
67  /**
68  * Returns time of day for the passed turn in the specified tod area.
69  * for_turn = 0 means current turn, ignoring illumination
70  */
71  const time_of_day& get_area_time_of_day(int area_i, int for_turn = 0) const;
72 
73  int get_current_area_time(int index) const;
74 
75  /**
76  * Returns time of day object for the passed turn at a location.
77  * tod areas matter, for_turn = 0 means current turn
78  * taking account of illumination caused by units
79  */
81  const unit_map& units, const gamemap& map, const map_location& loc, int for_turn = 0) const;
82 
84 
85  static bool is_start_ToD(const std::string&);
86 
87  /**
88  * Replace the time of day schedule
89  */
90  void replace_schedule(const config& time_cfg);
91  void replace_schedule(const std::vector<time_of_day>& schedule, int initial_time=0);
92  void replace_local_schedule(const std::vector<time_of_day>& schedule, int area_index, int initial_time=0);
93 
94  void replace_area_locations(int index, const std::set<map_location>& locs);
95 
96  /** Shift all hex locations for all time areas by the given x/y offsets. */
97  void move_all_areas(int x_offset, int y_offset);
98 
99  /**
100  * @returns the [time_area]s' ids.
101  */
102  std::vector<std::string> get_area_ids() const;
103 
104  /**
105  * @returns the nth area.
106  */
107  const std::set<map_location>& get_area_by_index(int index) const;
108 
109  /**
110  * @param id The id of the area to return.
111  * @returns The area with id @p id.
112  */
113  const std::set<map_location>& get_area_by_id(const std::string& id) const;
114 
115  /**
116  * @returns the area ID and index active on the given location.
117  */
118  std::pair<int, std::string> get_area_on_hex(const map_location& loc) const;
119 
120  /**
121  * Adds a new local time area from config, making it follow its own
122  * time-of-day sequence.
123  *
124  * @param map The game's map.
125  * @param cfg Config object containing x,y range/list of
126  * locations and desired [time] information.
127  */
128  void add_time_area(const gamemap & map, const config& cfg);
129 
130  /**
131  * Adds a new local time area from a set of locations, making those
132  * follow a different time-of-day sequence.
133  *
134  * @param id Identifier string to associate this time area
135  * with.
136  * @param locs Set of locations to be affected.
137  * @param time_cfg Config object containing [time] information.
138  */
139  void add_time_area(const std::string& id, const std::set<map_location>& locs,
140  const config& time_cfg);
141 
142  /**
143  * Removes a time area from config, making it follow the scenario's
144  * normal time-of-day sequence.
145  *
146  * @param id Identifier of time_area to remove. Supply an
147  * empty one to remove all local time areas.
148  */
149  void remove_time_area(const std::string& id);
150 
151  void remove_time_area(int index);
152 
153 
154  bool has_time_area() const {return !areas_.empty();}
155 
156  const std::vector<time_of_day>& times() const { return times_; }
157  const std::vector<time_of_day>& times(const map_location& loc) const;
158 
159  const std::vector<time_of_day>& times(int index) const {
160  assert(index < static_cast<int>(areas_.size()));
161  return areas_[index].times;
162  }
163 
164  //turn functions
165  int turn() const { return turn_; }
166  int number_of_turns() const {return num_turns_;}
167  void modify_turns(const std::string& mod);
168  void set_number_of_turns(int num);
169 
170  void update_server_information() const;
171  void modify_turns_by_wml(const std::string& mod);
172  void set_number_of_turns_by_wml(int num);
173 
174  /** Dynamically change the current turn number. */
175  void set_turn(const int num, game_data* vars = nullptr, const bool increase_limit_if_needed = true);
176  /** Dynamically change the current turn number. */
177  void set_turn_by_wml(const int num, game_data* vars = nullptr, const bool increase_limit_if_needed = true);
178 
179  /**
180  * Function to move to the next turn.
181  *
182  * @returns True if time has not expired.
183  */
184  bool next_turn(game_data* vars);
185 
186  /**
187  * Function to check the end of turns.
188  *
189  * @returns True if time has not expired.
190  */
191  bool is_time_left() const;
192  bool has_turn_event_fired() const
193  { return has_turn_event_fired_; }
195  { has_turn_event_fired_ = true; }
197  { return has_tod_bonus_changed_; }
199  { return liminal_bonus_; }
200  void set_max_liminal_bonus(int bonus) {
201  liminal_bonus_ = bonus;
202  has_cfg_liminal_bonus_ = true;
203  }
206  has_cfg_liminal_bonus_ = false;
207  }
208 private:
209 
210  /**
211  * Returns time of day object in the turn "nturn".
212  *
213  * Correct time is calculated from current time.
214  */
215  const time_of_day& get_time_of_day_turn(const std::vector<time_of_day>& times, int nturn, const int current_time) const;
216 
217  /**
218  * Computes for the main time or a time area the index of its times where we're currently at.
219  * number_of_times: size of that main time or time area's times vector
220  * for_turn_number: for which current turn
221  * current_time: the main or time area's current time
222  */
223  static int fix_time_index(
224  int number_of_times,
225  int time);
226  /**
227  * Computes for the main time or a time area the index of its times where we're currently at.
228  * number_of_times: size of that main time or time area's times vector
229  * for_turn_number: for which current turn
230  * current_time: the main or time area's current time
231  */
233  int number_of_times,
234  int for_turn_number,
235  int current_time) const;
236  /**
237  * Computes the maximum absolute value of lawful_bonus in the schedule.
238  */
239  int calculate_best_liminal_bonus(const std::vector<time_of_day>& schedule) const;
240 
241  /**
242  * For a change of the current turn number, sets the current times of the main time
243  * and all time areas.
244  */
245  void set_new_current_times(const int new_current_turn_number);
246 
248  std::string xsrc, ysrc;
249  std::string id;
250  std::vector<time_of_day> times;
251  std::set<map_location> hexes;
252  int currentTime{0};
253  };
254 
255  void set_current_time(int time, area_time_of_day& area);
256 
257  //index of the times vector of the main time where we're currently at
259  std::vector<time_of_day> times_;
260  std::vector<area_time_of_day> areas_;
261 
262  //max liminal bonus
264 
265  // current turn
266  int turn_;
267  //turn limit
269  //Whether the "turn X" and the "new turn" events were already fired this turn.
273  //
275 };
map_location loc
Definition: move.cpp:172
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
Encapsulates the map of the game.
Definition: map.hpp:172
this class does not give synced random results derived classes might do.
Definition: random.hpp:28
bool has_time_area() const
static bool is_start_ToD(const std::string &)
void update_server_information() const
int get_max_liminal_bonus() const
void replace_schedule(const config &time_cfg)
Replace the time of day schedule.
int number_of_turns() const
const time_of_day & get_time_of_day_turn(const std::vector< time_of_day > &times, int nturn, const int current_time) const
Returns time of day object in the turn "nturn".
const std::string & get_area_id(int area_index) const
std::vector< std::string > get_area_ids() const
void move_all_areas(int x_offset, int y_offset)
Shift all hex locations for all time areas by the given x/y offsets.
void modify_turns_by_wml(const std::string &mod)
const std::set< map_location > & get_area_by_index(int index) const
void turn_event_fired()
void remove_time_area(const std::string &id)
Removes a time area from config, making it follow the scenario's normal time-of-day sequence.
void set_current_time(int time)
void set_area_id(int area_index, const std::string &id)
const std::vector< time_of_day > & times() const
config to_config(const std::string &textdomain="") const
Definition: tod_manager.cpp:95
void set_max_liminal_bonus(int bonus)
void set_turn_by_wml(const int num, game_data *vars=nullptr, const bool increase_limit_if_needed=true)
Dynamically change the current turn number.
void add_time_area(const gamemap &map, const config &cfg)
Adds a new local time area from config, making it follow its own time-of-day sequence.
int get_current_area_time(int index) const
const std::vector< time_of_day > & times(int index) const
bool has_tod_bonus_changed_
void replace_local_schedule(const std::vector< time_of_day > &schedule, int area_index, int initial_time=0)
void set_turn(const int num, game_data *vars=nullptr, const bool increase_limit_if_needed=true)
Dynamically change the current turn number.
bool has_turn_event_fired_
int turn() const
void set_number_of_turns(int num)
bool has_turn_event_fired() const
void set_number_of_turns_by_wml(int num)
const time_of_day & get_time_of_day(int for_turn=0) const
Returns global time of day for the passed turn.
Definition: tod_manager.hpp:57
static int fix_time_index(int number_of_times, int time)
Computes for the main time or a time area the index of its times where we're currently at.
bool next_turn(game_data *vars)
Function to move to the next turn.
std::vector< area_time_of_day > areas_
void replace_area_locations(int index, const std::set< map_location > &locs)
bool is_time_left() const
Function to check the end of turns.
void reset_max_liminal_bonus()
time_of_day get_illuminated_time_of_day(const unit_map &units, const gamemap &map, const map_location &loc, int for_turn=0) const
Returns time of day object for the passed turn at a location.
const time_of_day & get_area_time_of_day(int area_i, int for_turn=0) const
Returns time of day for the passed turn in the specified tod area.
const std::set< map_location > & get_area_by_id(const std::string &id) const
bool has_cfg_liminal_bonus_
std::vector< time_of_day > times_
tod_manager & operator=(const tod_manager &manager)=default
config::attribute_value random_tod_
int calculate_best_liminal_bonus(const std::vector< time_of_day > &schedule) const
Computes the maximum absolute value of lawful_bonus in the schedule.
tod_manager(const config &scenario_cfg=config())
Definition: tod_manager.cpp:37
std::pair< int, std::string > get_area_on_hex(const map_location &loc) const
const time_of_day & get_previous_time_of_day() const
void resolve_random(randomness::rng &r)
handles random_start_time, should be called before the game starts.
Definition: tod_manager.cpp:66
int get_current_time() const
Definition: tod_manager.hpp:44
void modify_turns(const std::string &mod)
void set_new_current_times(const int new_current_turn_number)
For a change of the current turn number, sets the current times of the main time and all time areas.
int calculate_time_index_at_turn(int number_of_times, int for_turn_number, int current_time) const
Computes for the main time or a time area the index of its times where we're currently at.
bool has_tod_bonus_changed() const
Container associating units to locations.
Definition: map.hpp:98
Definitions for the interface to Wesnoth Markup Language (WML).
const config * cfg
std::size_t index(std::string_view str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
Encapsulates the map of the game.
Definition: location.hpp:46
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:57
std::set< map_location > hexes
std::vector< time_of_day > times