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