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