The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tod_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Eugen Jiresch
3  Part of the Battle for Wesnoth Project http://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 #ifndef TOD_MANAGER_HPP_INCLUDED
15 #define TOD_MANAGER_HPP_INCLUDED
16 
17 #include "map/location.hpp"
18 #include "config.hpp"
19 #include "time_of_day.hpp"
20 
21 #include <boost/optional.hpp>
22 
23 class gamemap;
24 class unit_map;
25 class game_data;
26 
27 namespace random_new
28 {
29  class rng;
30 }
31 
32 //time of day and turn functionality
34 {
35  public:
36  explicit tod_manager(const config& scenario_cfg = config());
38  tod_manager& operator=(const tod_manager& manager);
39 
40  config to_config() const;
41  /**
42  handles random_start_time, should be called before the game starts.
43  */
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 
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,
66  int for_turn = 0) const;
67 
68  int get_current_area_time(int index) const;
69 
70  /**
71  * Returns time of day object for the passed turn at a location.
72  * tod areas matter, for_turn = 0 means current turn
73  * taking account of illumination caused by units
74  */
75  const time_of_day get_illuminated_time_of_day(const unit_map & units, const gamemap & map, const map_location& loc,
76  int for_turn = 0) const;
77 
79 
80  static bool is_start_ToD(const std::string&);
81 
82  /**
83  * Replace the time of day schedule
84  */
85  void replace_schedule(const config& time_cfg);
86  void replace_schedule(const std::vector<time_of_day>& schedule);
87  void replace_local_schedule(const std::vector<time_of_day>& schedule, int area_index);
88 
89  void replace_area_locations(int index, const std::set<map_location>& locs);
90 
91  /**
92  * @returns the [time_area]s' ids.
93  */
94  std::vector<std::string> get_area_ids() const;
95 
96  /**
97  * @returns the nth area.
98  */
99  const std::set<map_location>& get_area_by_index(int index) const;
100 
101  /**
102  * @param id The id of the area to return.
103  * @returns The area with id @p id.
104  */
105  const std::set<map_location>& get_area_by_id(const std::string& id) const;
106 
107  /**
108  * Adds a new local time area from config, making it follow its own
109  * time-of-day sequence.
110  *
111  * @param cfg Config object containing x,y range/list of
112  * locations and desired [time] information.
113  */
114  void add_time_area(const gamemap & map, const config& cfg);
115 
116  /**
117  * Adds a new local time area from a set of locations, making those
118  * follow a different time-of-day sequence.
119  *
120  * @param id Identifier string to associate this time area
121  * with.
122  * @param locs Set of locations to be affected.
123  * @param time_cfg Config object containing [time] information.
124  */
125  void add_time_area(const std::string& id, const std::set<map_location>& locs,
126  const config& time_cfg);
127 
128  /**
129  * Removes a time area from config, making it follow the scenario's
130  * normal time-of-day sequence.
131  *
132  * @param id Identifier of time_area to remove. Supply an
133  * empty one to remove all local time areas.
134  */
135  void remove_time_area(const std::string& id);
136 
137  void remove_time_area(int index);
138 
139 
140  bool has_time_area() const {return !areas_.empty();}
141 
142  const std::vector<time_of_day>& times(const map_location& loc = map_location::null_location()) const;
143 
144  const std::vector<time_of_day>& times(int index) const {
145  assert(index < static_cast<int>(areas_.size()));
146  return areas_[index].times;
147  }
148 
149  //turn functions
150  int turn() const { return turn_; }
151  int number_of_turns() const {return num_turns_;}
152  void modify_turns(const std::string& mod);
153  void set_number_of_turns(int num);
154 
155  void update_server_information() const;
156  void modify_turns_by_wml(const std::string& mod);
158 
159  /** Dynamically change the current turn number. */
160  void set_turn(const int num, game_data* vars = nullptr, const bool increase_limit_if_needed = true);
161  /** Dynamically change the current turn number. */
162  void set_turn_by_wml(const int num, game_data* vars = nullptr, const bool increase_limit_if_needed = true);
163 
164  /**
165  * Function to move to the next turn.
166  *
167  * @returns True if time has not expired.
168  */
169  bool next_turn(game_data* vars);
170 
171  /**
172  * Function to check the end of turns.
173  *
174  * @returns True if time has not expired.
175  */
176  bool is_time_left();
178  { return has_turn_event_fired_; }
180  { has_turn_event_fired_ = true; }
182  { return has_tod_bonus_changed_; }
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  */
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  /**
205  * For a change of the current turn number, sets the current times of the main time
206  * and all time areas.
207  */
208  void set_new_current_times(const int new_current_turn_number);
209 
210 
213  xsrc(),
214  ysrc(),
215  id(),
216  times(),
217  hexes(),
218  currentTime(0)
219  {}
220 
223  std::vector<time_of_day> times;
224  std::set<map_location> hexes;
226  };
227 
228  void set_current_time(int time, area_time_of_day& area);
229 
230  //index of the times vector of the main time where we're currently at
232  std::vector<time_of_day> times_;
233  std::vector<area_time_of_day> areas_;
234 
235  // current turn
236  int turn_;
237  //turn limit
239  //Whether the "turn X" and the "new turn" events were already fired this turn.
242  //
244 };
245 #endif
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...
void replace_local_schedule(const std::vector< time_of_day > &schedule, int area_index)
tod_manager & operator=(const tod_manager &manager)
Definition: tod_manager.cpp:58
bool has_turn_event_fired_
void replace_schedule(const config &time_cfg)
Replace the time of day schedule.
void set_turn(const int num, game_data *vars=nullptr, const bool increase_limit_if_needed=true)
Dynamically change the current turn number.
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
int get_current_area_time(int index) const
void set_area_id(int area_index, const std::string &id)
bool has_tod_bonus_changed_
void modify_turns_by_wml(const std::string &mod)
std::set< map_location > hexes
Definitions for the interface to Wesnoth Markup Language (WML).
Variant for storing WML attributes.
Definition: config.hpp:223
bool has_time_area() const
void modify_turns(const std::string &mod)
config to_config() const
const time_of_day & get_previous_time_of_day() const
Object which defines a time of day with associated bonuses, image, sounds etc.
Definition: time_of_day.hpp:48
void set_current_time(int time)
void set_number_of_turns_by_wml(int num)
GLuint id
Definition: glew.h:1647
std::vector< time_of_day > times
config::attribute_value random_tod_
void update_server_information() const
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...
std::vector< time_of_day > times_
Encapsulates the map of the game.
Definition: map.hpp:37
tod_manager(const config &scenario_cfg=config())
Definition: tod_manager.cpp:38
GLuint num
Definition: glew.h:2552
static const map_location & null_location()
Definition: location.hpp:195
bool is_time_left()
Function to check the end of turns.
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.
Encapsulates the map of the game.
Definition: location.hpp:38
const std::vector< time_of_day > & times(int index) const
bool has_tod_bonus_changed()
std::vector< std::string > get_area_ids() const
void set_number_of_turns(int num)
bool has_turn_event_fired()
GLuint index
Definition: glew.h:1782
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".
int turn() const
this class does not give synced random results derived classes might do.
Definition: random_new.hpp:27
const std::set< map_location > & get_area_by_index(int index) const
void turn_event_fired()
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
static bool is_start_ToD(const std::string &)
const std::set< map_location > & get_area_by_id(const std::string &id) const
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.
int calculate_current_time(const int number_of_times, const int for_turn_number, const int current_time, const bool only_to_allowed_range=false) const
Computes for the main time or a time area the index of its times where we're currently at...
int get_current_time(const map_location &loc=map_location::null_location()) const
void replace_area_locations(int index, const std::set< map_location > &locs)
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.
const std::vector< time_of_day > & times(const map_location &loc=map_location::null_location()) const
Container associating units to locations.
Definition: map.hpp:90
std::vector< area_time_of_day > areas_
void resolve_random(random_new::rng &r)
handles random_start_time, should be called before the game starts.
Definition: tod_manager.cpp:86
bool next_turn(game_data *vars)
Function to move to the next turn.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
unit_map * units
Definition: resources.cpp:35
int number_of_turns() const