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