The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
game_board.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef GAME_BOARD_HPP_INCLUDED
16 #define GAME_BOARD_HPP_INCLUDED
17 
18 #include "global.hpp"
19 
20 #include "display_context.hpp"
21 #include "team.hpp"
22 #include "terrain/type_data.hpp"
23 #include "units/map.hpp"
24 #include "units/id.hpp"
25 
26 #include <boost/optional.hpp>
27 #include <boost/scoped_ptr.hpp>
28 #include <boost/shared_ptr.hpp>
29 #include <set>
30 #include <vector>
31 
32 class config;
33 class gamemap;
34 
35 namespace events {
36  class mouse_handler;
37  class menu_handler;
38 }
39 
40 /**
41  *
42  * Game board class.
43  *
44  * The purpose of this class is to encapsulate some of the core game logic, including the unit map,
45  * the list of teams, and the game map.
46  *
47  * This should eventually become part of the game state object IMO, which should be a child of play_controller.
48  *
49  * I also intend to move the pathfinding module to be housed within this class -- this way, we can implement a
50  * sound pathfinding data structure to speed up path computations for AI, without having to make "update event"
51  * code at all points in the engine which modify the relevant data.
52  *
53  **/
54 
56 {
57 
58  std::vector<team> teams_;
59  std::vector<std::string> labels_;
60 
61  boost::scoped_ptr<gamemap> map_;
64 
65  //TODO: Remove these when we have refactored enough to make it possible.
66  friend class play_controller;
67  friend class events::mouse_handler;
68  friend class events::menu_handler;
69  friend class game_state;
70  friend class game_lua_kernel;
71 
72  /**
73  * Temporary unit move structs:
74  *
75  * Probably don't remove these friends, this is actually fairly useful. These structs are used by:
76  * - AI
77  * - Whiteboard
78  * - I think certain wml actions
79  * For AI, the ai wants to move two units next to eachother so it can ask for attack calculations. This should not trigger
80  * pathfinding modifications, so the version that directly changes the unit map is probably preferable, although it should be
81  * refactored.
82  * For whiteboard and wml actions, we generally do want pathfinding to be updated, so use the game_board constructors which I
83  * have added to these structs instead.
84  *
85  **/
86  friend struct temporary_unit_placer;
87  friend struct temporary_unit_mover;
88  friend struct temporary_unit_remover;
89 
90 public:
92  // Constructors, trivial dtor, and const accessors
93 
94  game_board(const tdata_cache & tdata, const config & level);
95  virtual ~game_board();
96 
97  virtual const std::vector<team> & teams() const { return teams_; }
98  virtual const gamemap & map() const { return *map_; }
99  virtual const unit_map & units() const { return units_; }
100  unit_map & units() { return units_; }
101  virtual const std::vector<std::string> & hidden_label_categories() const { return labels_; }
102 
103  // Copy and swap idiom, because we have a scoped pointer.
104 
105  game_board(const game_board & other);
107 
108  friend void swap(game_board & one, game_board & other);
109 
110  // Saving
111 
112  void write_config(config & cfg) const;
113 
114  // Manipulators from play_controller
115 
116  void new_turn(int pnum);
117  void end_turn(int pnum);
119 
120  void heal_all_survivors();
121 
122  void check_victory(bool &, bool &, bool &, bool &, std::set<unsigned> &, bool);
123 
124  // Manipulator from playturn
125 
126  void side_drop_to (int side_num, team::CONTROLLER ctrl, team::PROXY_CONTROLLER proxy = team::PROXY_CONTROLLER::PROXY_HUMAN);
127  void side_change_controller (int side_num, bool is_local, const std::string& pname = "");
128 
129  // Manipulator from actionwml
130 
131  bool try_add_unit_to_recall_list(const map_location& loc, const unit_ptr u);
132  boost::optional<std::string> replace_map (const gamemap & r);
133  void overlay_map (const gamemap & o, const config & cfg, map_location loc, bool border);
134 
135  bool change_terrain(const map_location &loc, const std::string &t,
136  const std::string & mode, bool replace_if_failed); //used only by lua
137 
138  // Global accessor from unit.hpp
139 
140  unit_map::iterator find_visible_unit(const map_location &loc, const team& current_team, bool see_all = false);
141  unit_map::iterator find_visible_unit(const map_location & loc, size_t team, bool see_all = false) { return find_visible_unit(loc, teams_[team], see_all); }
142  bool has_visible_unit (const map_location & loc, const team & team, bool see_all = false) const;
143  bool has_visible_unit (const map_location & loc, size_t team, bool see_all = false) const { return has_visible_unit(loc, teams_[team], see_all); }
144 
145  unit* get_visible_unit(const map_location &loc, const team &current_team, bool see_all = false); //TODO: can this not return a pointer?
146 
147  // Wrapped functions from unit_map. These should ultimately provide notification to observers, pathfinding.
148 
149  unit_map::iterator find_unit(const map_location & loc) { return units_.find(loc); }
150  /// Calculates whether a team is defeated
151  bool team_is_defeated(const team& t) const;
152 };
153 
154 void swap(game_board & one, game_board & other);
155 
156 
157 /**
158  * This object is used to temporary place a unit in the unit map, swapping out
159  * any unit that is already there. On destruction, it restores the unit map to
160  * its original.
161  */
163 {
166  virtual ~temporary_unit_placer();
167 
168 private:
172 };
173 
174 // Begin Temporary Unit Move Structs
175 // TODO: Fix up the implementations which use game_board
176 
177 /**
178  * This object is used to temporary remove a unit from the unit map.
179  * On destruction, it restores the unit map to its original.
180  * unit_map iterators to this unit must not be accessed while the unit is temporarily
181  * removed, otherwise a collision will happen when trying to reinsert the unit.
182  */
184 {
187  virtual ~temporary_unit_remover();
188 
189 private:
193 };
194 
195 
196 /**
197  * This object is used to temporary move a unit in the unit map, swapping out
198  * any unit that is already there. On destruction, it restores the unit map to
199  * its original.
200  */
202 {
204  const map_location& dst, int new_moves);
206  const map_location& dst);
208  const map_location& dst, int new_moves);
210  const map_location& dst);
211  virtual ~temporary_unit_mover();
212 
213 private:
219 };
220 
221 
222 #endif
void set_all_units_user_end_turn()
Definition: game_board.cpp:85
Game board class.
Definition: game_board.hpp:55
virtual const unit_map & units() const
Definition: game_board.hpp:99
void heal_all_survivors()
Definition: game_board.cpp:91
Definition: unit.hpp:95
n_unit::id_manager unit_id_manager_
Definition: game_board.hpp:62
GLint level
Definition: glew.h:1220
void write_config(config &cfg) const
Definition: game_board.cpp:340
const map_location src_
Definition: game_board.hpp:215
void swap(game_board &one, game_board &other)
Definition: game_board.cpp:56
boost::scoped_ptr< gamemap > map_
Definition: game_board.hpp:61
void overlay_map(const gamemap &o, const config &cfg, map_location loc, bool border)
Definition: game_board.cpp:294
virtual const std::vector< team > & teams() const
Definition: game_board.hpp:97
GLenum src
Definition: glew.h:2392
-file sdl_utils.hpp
GLenum mode
Definition: glew.h:2390
GLdouble GLdouble t
Definition: glew.h:1366
bool team_is_defeated(const team &t) const
Calculates whether a team is defeated.
Definition: game_board.cpp:230
virtual ~game_board()
Definition: game_board.cpp:50
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
unit_map units_
Definition: game_board.hpp:63
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
virtual ~temporary_unit_remover()
Definition: game_board.cpp:408
game_board & operator=(game_board other)
Definition: game_board.cpp:63
std::vector< team > teams_
Definition: game_board.hpp:58
temporary_unit_remover(unit_map &m, const map_location &loc)
Definition: game_board.cpp:398
const map_location loc_
Definition: game_board.hpp:191
unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
Definition: game_board.cpp:192
void side_drop_to(int side_num, team::CONTROLLER ctrl, team::PROXY_CONTROLLER proxy=team::PROXY_CONTROLLER::PROXY_HUMAN)
Definition: game_board.cpp:200
friend void swap(game_board &one, game_board &other)
Definition: game_board.cpp:56
GLenum GLenum dst
Definition: glew.h:2392
This object is used to temporary place a unit in the unit map, swapping out any unit that is already ...
Definition: game_board.hpp:162
Encapsulates the map of the game.
Definition: map.hpp:37
unit_map & units()
Definition: game_board.hpp:100
void check_victory(bool &, bool &, bool &, bool &, std::set< unsigned > &, bool)
Definition: game_board.cpp:101
virtual ~temporary_unit_mover()
Definition: game_board.cpp:473
This object is used to temporary remove a unit from the unit map.
Definition: game_board.hpp:183
temporary_unit_placer(unit_map &m, const map_location &loc, unit &u)
Definition: game_board.cpp:374
std::vector< std::string > labels_
Definition: game_board.hpp:59
Encapsulates the map of the game.
Definition: location.hpp:38
unit_map::iterator find_visible_unit(const map_location &loc, size_t team, bool see_all=false)
Definition: game_board.hpp:141
void end_turn(int pnum)
Definition: game_board.cpp:77
virtual ~temporary_unit_placer()
Definition: game_board.cpp:388
bool has_visible_unit(const map_location &loc, const team &team, bool see_all=false) const
Definition: game_board.cpp:183
unit_map::iterator find_unit(const map_location &loc)
Definition: game_board.hpp:149
bool change_terrain(const map_location &loc, const std::string &t, const std::string &mode, bool replace_if_failed)
Definition: game_board.cpp:298
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
game_board(const tdata_cache &tdata, const config &level)
Definition: game_board.cpp:36
Handling of system events.
Definition: manager.hpp:42
virtual const gamemap & map() const
Definition: game_board.hpp:98
GLint GLint GLsizei GLsizei GLsizei GLint border
Definition: glew.h:1222
temporary_unit_mover(unit_map &m, const map_location &src, const map_location &dst, int new_moves)
Constructor This version will change the unit's current movement to new_moves while the unit is moved...
Definition: game_board.cpp:423
boost::optional< std::string > replace_map(const gamemap &r)
Definition: game_board.cpp:258
n_unit::id_manager & unit_id_manager()
Definition: game_board.hpp:91
const GLdouble * m
Definition: glew.h:6968
bool has_visible_unit(const map_location &loc, size_t team, bool see_all=false) const
Definition: game_board.hpp:143
GLenum pname
Definition: glew.h:1656
const map_location dst_
Definition: game_board.hpp:216
Container associating units to locations.
Definition: map.hpp:90
void new_turn(int pnum)
Definition: game_board.cpp:69
const map_location loc_
Definition: game_board.hpp:170
unit_iterator find(size_t id)
Definition: map.cpp:285
bool try_add_unit_to_recall_list(const map_location &loc, const unit_ptr u)
Definition: game_board.cpp:251
virtual const std::vector< std::string > & hidden_label_categories() const
Definition: game_board.hpp:101
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
unit_map::iterator find_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
Definition: game_board.cpp:173
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:201
void side_change_controller(int side_num, bool is_local, const std::string &pname="")
Definition: game_board.cpp:213