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