The Battle for Wesnoth  1.15.1+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);
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 cfg Config object containing x,y range/list of
110  * locations and desired [time] information.
111  */
112  void add_time_area(const gamemap & map, const config& cfg);
113 
114  /**
115  * Adds a new local time area from a set of locations, making those
116  * follow a different time-of-day sequence.
117  *
118  * @param id Identifier string to associate this time area
119  * with.
120  * @param locs Set of locations to be affected.
121  * @param time_cfg Config object containing [time] information.
122  */
123  void add_time_area(const std::string& id, const std::set<map_location>& locs,
124  const config& time_cfg);
125 
126  /**
127  * Removes a time area from config, making it follow the scenario's
128  * normal time-of-day sequence.
129  *
130  * @param id Identifier of time_area to remove. Supply an
131  * empty one to remove all local time areas.
132  */
133  void remove_time_area(const std::string& id);
134 
135  void remove_time_area(int index);
136 
137 
138  bool has_time_area() const {return !areas_.empty();}
139 
140  const std::vector<time_of_day>& times(const map_location& loc = map_location::null_location()) const;
141 
142  const std::vector<time_of_day>& times(int index) const {
143  assert(index < static_cast<int>(areas_.size()));
144  return areas_[index].times;
145  }
146 
147  //turn functions
148  int turn() const { return turn_; }
149  int number_of_turns() const {return num_turns_;}
150  void modify_turns(const std::string& mod);
151  void set_number_of_turns(int num);
152 
153  void update_server_information() const;
154  void modify_turns_by_wml(const std::string& mod);
155  void set_number_of_turns_by_wml(int num);
156 
157  /** Dynamically change the current turn number. */
158  void set_turn(const int num, game_data* vars = nullptr, const bool increase_limit_if_needed = true);
159  /** Dynamically change the current turn number. */
160  void set_turn_by_wml(const int num, game_data* vars = nullptr, const bool increase_limit_if_needed = true);
161 
162  /**
163  * Function to move to the next turn.
164  *
165  * @returns True if time has not expired.
166  */
167  bool next_turn(game_data* vars);
168 
169  /**
170  * Function to check the end of turns.
171  *
172  * @returns True if time has not expired.
173  */
174  bool is_time_left() const;
175  bool has_turn_event_fired() const
176  { return has_turn_event_fired_; }
178  { has_turn_event_fired_ = true; }
180  { return has_tod_bonus_changed_; }
182  { return liminal_bonus_; }
183  private:
184 
185  /**
186  * Returns time of day object in the turn "nturn".
187  *
188  * Correct time is calculated from current time.
189  */
190  const time_of_day& get_time_of_day_turn(const std::vector<time_of_day>& times, int nturn, const int current_time) const;
191 
192  /**
193  * Computes for the main time or a time area the index of its times where we're currently at.
194  * number_of_times: size of that main time or time area's times vector
195  * for_turn_number: for which current turn
196  * current_time: the main or time area's current time
197  */
198  int calculate_current_time(
199  const int number_of_times,
200  const int for_turn_number,
201  const int current_time,
202  const bool only_to_allowed_range = false) const;
203  /**
204  * Computes the maximum absolute value of lawful_bonus in the schedule.
205  */
206  int calculate_best_liminal_bonus(const std::vector<time_of_day>& schedule) const;
207 
208  /**
209  * For a change of the current turn number, sets the current times of the main time
210  * and all time areas.
211  */
212  void set_new_current_times(const int new_current_turn_number);
213 
214 
217  xsrc(),
218  ysrc(),
219  id(),
220  times(),
221  hexes(),
222  currentTime(0)
223  {}
224 
225  std::string xsrc, ysrc;
226  std::string id;
227  std::vector<time_of_day> times;
228  std::set<map_location> hexes;
230  };
231 
232  void set_current_time(int time, area_time_of_day& area);
233 
234  //index of the times vector of the main time where we're currently at
236  std::vector<time_of_day> times_;
237  std::vector<area_time_of_day> areas_;
238 
239  //max liminal bonus
241 
242  // current turn
243  int turn_;
244  //turn limit
246  //Whether the "turn X" and the "new turn" events were already fired this turn.
250  //
252 };
::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:57
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:36
int number_of_turns() const
Encapsulates the map of the game.
Definition: location.hpp:42
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:85
Container associating units to locations.
Definition: map.hpp:99
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:92
this class does not give synced random results derived classes might do.
Definition: random.hpp:27