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