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