The Battle for Wesnoth  1.15.11+dev
game_board.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
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 "display_context.hpp"
18 #include "team.hpp"
19 #include "terrain/translation.hpp"
20 #include "terrain/type_data.hpp"
21 #include "units/map.hpp"
22 #include "units/id.hpp"
23 #include <optional>
24 
25 #include <set>
26 #include <vector>
27 
28 class config;
29 class gamemap;
30 
31 namespace events {
32  class mouse_handler;
33  class menu_handler;
34 }
35 
36 /**
37  *
38  * Game board class.
39  *
40  * The purpose of this class is to encapsulate some of the core game logic, including the unit map,
41  * the list of teams, and the game map.
42  *
43  * This should eventually become part of the game state object IMO, which should be a child of play_controller.
44  *
45  * I also intend to move the pathfinding module to be housed within this class -- this way, we can implement a
46  * sound pathfinding data structure to speed up path computations for AI, without having to make "update event"
47  * code at all points in the engine which modify the relevant data.
48  *
49  **/
51 {
52  std::vector<team> teams_;
53  std::vector<std::string> labels_;
54 
55  std::unique_ptr<gamemap> map_;
58 
59  /**
60  * Temporary unit move structs:
61  *
62  * Probably don't remove these friends, this is actually fairly useful. These structs are used by:
63  * - AI
64  * - Whiteboard
65  * - I think certain wml actions
66  * For AI, the ai wants to move two units next to eachother so it can ask for attack calculations. This should not trigger
67  * pathfinding modifications, so the version that directly changes the unit map is probably preferable, although it should be
68  * refactored.
69  * For whiteboard and wml actions, we generally do want pathfinding to be updated, so use the game_board constructors which I
70  * have added to these structs instead.
71  *
72  **/
73  friend struct temporary_unit_placer;
74  friend struct temporary_unit_mover;
75  friend struct temporary_unit_remover;
76 
77 public:
78  n_unit::id_manager& unit_id_manager() { return unit_id_manager_; }
79  // Constructors, trivial dtor, and const accessors
80 
81  game_board(const config& level);
82  virtual ~game_board();
83 
84  virtual const std::vector<team>& teams() const override
85  {
86  return teams_;
87  }
88 
89  std::vector<team>& teams()
90  {
91  return teams_;
92  }
93 
94  using display_context::get_team; // so as not to hide the const version
95 
96  team& get_team(int i)
97  {
98  return teams_.at(i - 1);
99  }
100 
101  virtual const gamemap& map() const override
102  {
103  return *map_;
104  }
105 
107  {
108  return *map_;
109  }
110 
111  virtual const unit_map& units() const override
112  {
113  return units_;
114  }
115 
117  {
118  return units_;
119  }
120 
121  virtual const std::vector<std::string>& hidden_label_categories() const override
122  {
123  return labels_;
124  }
125 
126  virtual std::vector<std::string>& hidden_label_categories() override
127  {
128  return labels_;
129  }
130 
131  // Copy and swap idiom, because we have a scoped pointer.
132 
133  game_board(const game_board & other);
134  game_board& operator=(const game_board& other) = delete;
135 
136  friend void swap(game_board & one, game_board & other);
137 
138  // Saving
139 
140  void write_config(config & cfg) const;
141 
142  // Manipulators from play_controller
143 
144  void new_turn(int pnum);
145  void end_turn(int pnum);
146  void set_all_units_user_end_turn();
147 
148  void heal_all_survivors();
149 
150  void check_victory(bool &, bool &, bool &, bool &, std::set<unsigned> &, bool);
151 
152  // Manipulator from playturn
153 
154  void side_drop_to (int side_num, team::CONTROLLER ctrl, team::PROXY_CONTROLLER proxy = team::PROXY_CONTROLLER::PROXY_HUMAN);
155  void side_change_controller (int side_num, bool is_local, const std::string& pname, const std::string& controller_type);
156 
157  // Manipulator from actionwml
158 
159  bool try_add_unit_to_recall_list(const map_location& loc, const unit_ptr u);
160  std::optional<std::string> replace_map(const gamemap & r);
161 
162  bool change_terrain(const map_location &loc, const std::string &t, const std::string & mode, bool replace_if_failed); //used only by lua and debug commands
163  bool change_terrain(const map_location &loc, const t_translation::terrain_code &t, terrain_type_data::merge_mode& mode, bool replace_if_failed); //used only by lua and debug commands
164 
165  // Global accessor from unit.hpp
166 
167  unit_map::iterator find_visible_unit(const map_location &loc, const team& current_team, bool see_all = false);
168  unit_map::iterator find_visible_unit(const map_location & loc, std::size_t team, bool see_all = false) { return find_visible_unit(loc, teams_[team], see_all); }
169  bool has_visible_unit (const map_location & loc, const team & team, bool see_all = false) const;
170  bool has_visible_unit (const map_location & loc, std::size_t team, bool see_all = false) const { return has_visible_unit(loc, teams_[team], see_all); }
171 
172  unit* get_visible_unit(const map_location &loc, const team &current_team, bool see_all = false); //TODO: can this not return a pointer?
173 
174  // Wrapped functions from unit_map. These should ultimately provide notification to observers, pathfinding.
175 
176  unit_map::iterator find_unit(const map_location & loc) { return units_.find(loc); }
177  /** Calculates whether a team is defeated */
178  bool team_is_defeated(const team& t) const;
179 };
180 
181 void swap(game_board & one, game_board & other);
182 
183 
184 /**
185  * This object is used to temporary place a unit in the unit map, swapping out
186  * any unit that is already there. On destruction, it restores the unit map to
187  * its original.
188  */
190 {
191  temporary_unit_placer(unit_map& m, const map_location& loc, unit& u);
193  virtual ~temporary_unit_placer();
194 
195 private:
199 };
200 
201 // Begin Temporary Unit Move Structs
202 // TODO: Fix up the implementations which use game_board
203 
204 /**
205  * This object is used to temporary remove a unit from the unit map.
206  * On destruction, it restores the unit map to its original.
207  * unit_map iterators to this unit must not be accessed while the unit is temporarily
208  * removed, otherwise a collision will happen when trying to reinsert the unit.
209  */
211 {
214  virtual ~temporary_unit_remover();
215 
216 private:
220 };
221 
222 
223 /**
224  * This object is used to temporary move a unit in the unit map, swapping out
225  * any unit that is already there. On destruction, it restores the unit map to
226  * its original.
227  */
229 {
231  const map_location& dst, int new_moves);
233  const map_location& dst);
235  const map_location& dst, int new_moves);
237  const map_location& dst);
238  virtual ~temporary_unit_mover();
239 
240 private:
246 };
std::unique_ptr< gamemap > map_
Definition: game_board.hpp:55
unit_map::iterator find_visible_unit(const map_location &loc, std::size_t team, bool see_all=false)
Definition: game_board.hpp:168
Game board class.
Definition: game_board.hpp:50
virtual const std::vector< std::string > & hidden_label_categories() const override
Definition: game_board.hpp:121
const team & get_team(int side) const
virtual const std::vector< team > & teams() const override
Definition: game_board.hpp:84
virtual const unit_map & units() const override
Definition: game_board.hpp:111
This class represents a single unit of a specific type.
Definition: unit.hpp:120
n_unit::id_manager unit_id_manager_
Definition: game_board.hpp:56
const map_location src_
Definition: game_board.hpp:242
void swap(game_board &one, game_board &other)
Definition: game_board.cpp:59
virtual const gamemap & map() const override
Definition: game_board.hpp:101
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
Definition: translation.hpp:49
virtual std::vector< std::string > & hidden_label_categories() override
Definition: game_board.hpp:126
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:25
#define b
unit_map units_
Definition: game_board.hpp:57
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
team & get_team(int i)
Definition: game_board.hpp:96
std::vector< team > teams_
Definition: game_board.hpp:52
const map_location loc_
Definition: game_board.hpp:218
This object is used to temporary place a unit in the unit map, swapping out any unit that is already ...
Definition: game_board.hpp:189
Encapsulates the map of the game.
Definition: map.hpp:170
unit_map & units()
Definition: game_board.hpp:116
This object is used to temporary remove a unit from the unit map.
Definition: game_board.hpp:210
std::vector< std::string > labels_
Definition: game_board.hpp:53
Encapsulates the map of the game.
Definition: location.hpp:37
static const unit * get_visible_unit(reports::context &rc)
Definition: reports.cpp:130
unit_iterator find(std::size_t id)
Definition: map.cpp:309
gamemap & map()
Definition: game_board.hpp:106
std::size_t i
Definition: function.cpp:940
unit_map::iterator find_unit(const map_location &loc)
Definition: game_board.hpp:176
Handling of system events.
Definition: manager.hpp:42
n_unit::id_manager & unit_id_manager()
Definition: game_board.hpp:78
bool has_visible_unit(const map_location &loc, std::size_t team, bool see_all=false) const
Definition: game_board.hpp:170
double t
Definition: astarsearch.cpp:64
std::vector< team > & teams()
Definition: game_board.hpp:89
const map_location dst_
Definition: game_board.hpp:243
Container associating units to locations.
Definition: map.hpp:97
const map_location loc_
Definition: game_board.hpp:197
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
This object is used to temporary move a unit in the unit map, swapping out any unit that is already t...
Definition: game_board.hpp:228