The Battle for Wesnoth  1.19.15+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  /**
97  * @returns the [time_area]s' ids.
98  */
99  std::vector<std::string> get_area_ids() const;
100 
101  /**
102  * @returns the nth area.
103  */
104  const std::set<map_location>& get_area_by_index(int index) const;
105 
106  /**
107  * @param id The id of the area to return.
108  * @returns The area with id @p id.
109  */
110  const std::set<map_location>& get_area_by_id(const std::string& id) const;
111 
112  /**
113  * @returns the area ID and index active on the given location.
114  */
115  std::pair<int, std::string> get_area_on_hex(const map_location& loc) const;
116 
117  /**
118  * Adds a new local time area from config, making it follow its own
119  * time-of-day sequence.
120  *
121  * @param map The game's map.
122  * @param cfg Config object containing x,y range/list of
123  * locations and desired [time] information.
124  */
125  void add_time_area(const gamemap & map, const config& cfg);
126 
127  /**
128  * Adds a new local time area from a set of locations, making those
129  * follow a different time-of-day sequence.
130  *
131  * @param id Identifier string to associate this time area
132  * with.
133  * @param locs Set of locations to be affected.
134  * @param time_cfg Config object containing [time] information.
135  */
136  void add_time_area(const std::string& id, const std::set<map_location>& locs,
137  const config& time_cfg);
138 
139  /**
140  * Removes a time area from config, making it follow the scenario's
141  * normal time-of-day sequence.
142  *
143  * @param id Identifier of time_area to remove. Supply an
144  * empty one to remove all local time areas.
145  */
146  void remove_time_area(const std::string& id);
147 
148  void remove_time_area(int index);
149 
150 
151  bool has_time_area() const {return !areas_.empty();}
152 
153  const std::vector<time_of_day>& times() const { return times_; }
154  const std::vector<time_of_day>& times(const map_location& loc) const;
155 
156  const std::vector<time_of_day>& times(int index) const {
157  assert(index < static_cast<int>(areas_.size()));
158  return areas_[index].times;
159  }
160 
161  //turn functions
162  int turn() const { return turn_; }
163  int number_of_turns() const {return num_turns_;}
164  void modify_turns(const std::string& mod);
165  void set_number_of_turns(int num);
166 
167  void update_server_information() const;
168  void modify_turns_by_wml(const std::string& mod);
169  void set_number_of_turns_by_wml(int num);
170 
171  /** Dynamically change the current turn number. */
172  void set_turn(const int num, game_data* vars = nullptr, const bool increase_limit_if_needed = true);
173  /** Dynamically change the current turn number. */
174  void set_turn_by_wml(const int num, game_data* vars = nullptr, const bool increase_limit_if_needed = true);
175 
176  /**
177  * Function to move to the next turn.
178  *
179  * @returns True if time has not expired.
180  */
181  bool next_turn(game_data* vars);
182 
183  /**
184  * Function to check the end of turns.
185  *
186  * @returns True if time has not expired.
187  */
188  bool is_time_left() const;
189  bool has_turn_event_fired() const
190  { return has_turn_event_fired_; }
192  { has_turn_event_fired_ = true; }
194  { return has_tod_bonus_changed_; }
196  { return liminal_bonus_; }
197  void set_max_liminal_bonus(int bonus) {
198  liminal_bonus_ = bonus;
199  has_cfg_liminal_bonus_ = true;
200  }
203  has_cfg_liminal_bonus_ = false;
204  }
205 private:
206 
207  /**
208  * Returns time of day object in the turn "nturn".
209  *
210  * Correct time is calculated from current time.
211  */
212  const time_of_day& get_time_of_day_turn(const std::vector<time_of_day>& times, int nturn, const int current_time) const;
213 
214  /**
215  * Computes for the main time or a time area the index of its times where we're currently at.
216  * number_of_times: size of that main time or time area's times vector
217  * for_turn_number: for which current turn
218  * current_time: the main or time area's current time
219  */
220  static int fix_time_index(
221  int number_of_times,
222  int time);
223  /**
224  * Computes for the main time or a time area the index of its times where we're currently at.
225  * number_of_times: size of that main time or time area's times vector
226  * for_turn_number: for which current turn
227  * current_time: the main or time area's current time
228  */
230  int number_of_times,
231  int for_turn_number,
232  int current_time) const;
233  /**
234  * Computes the maximum absolute value of lawful_bonus in the schedule.
235  */
236  int calculate_best_liminal_bonus(const std::vector<time_of_day>& schedule) const;
237 
238  /**
239  * For a change of the current turn number, sets the current times of the main time
240  * and all time areas.
241  */
242  void set_new_current_times(const int new_current_turn_number);
243 
245  std::string xsrc, ysrc;
246  std::string id;
247  std::vector<time_of_day> times;
248  std::set<map_location> hexes;
249  int currentTime{0};
250  };
251 
252  void set_current_time(int time, area_time_of_day& area);
253 
254  //index of the times vector of the main time where we're currently at
256  std::vector<time_of_day> times_;
257  std::vector<area_time_of_day> areas_;
258 
259  //max liminal bonus
261 
262  // current turn
263  int turn_;
264  //turn limit
266  //Whether the "turn X" and the "new turn" events were already fired this turn.
270  //
272 };
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:158
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 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