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