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