The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
game_board.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2014 - 2025
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"
25
27#include <set>
28#include <vector>
29
30class 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;
72
73public:
75 // Constructors, trivial dtor, and const accessors
76
77 game_board(const config& level);
78
79 virtual const std::vector<team>& teams() const override
80 {
81 return teams_;
82 }
83
84 std::vector<team>& teams()
85 {
86 return teams_;
87 }
88
89 using display_context::get_team; // so as not to hide the const version
90
92 {
93 return teams_.at(i - 1);
94 }
95
96 virtual const gamemap& map() const override
97 {
98 return *map_;
99 }
100
102 {
103 return *map_;
104 }
105
106 virtual const unit_map& units() const override
107 {
108 return units_;
109 }
110
112 {
113 return units_;
114 }
115
116 virtual const std::vector<std::string>& hidden_label_categories() const override
117 {
118 return labels_;
119 }
120
121 virtual std::vector<std::string>& hidden_label_categories() override
122 {
123 return labels_;
124 }
125
126 game_board(const game_board & other) = delete;
127 game_board& operator=(const game_board& other) = delete;
128
129 friend void swap(game_board & one, game_board & other);
130
131 // Saving
132
133 void write_config(config & cfg) const;
134
135 // Manipulators from play_controller
136
137 void new_turn(int pnum);
138 void end_turn(int pnum);
140
141 void heal_all_survivors();
142
143 void check_victory(bool &, bool &, bool &, bool &, std::set<unsigned> &, bool);
144
145 // Manipulator from playturn
146
147 void side_drop_to (int side_num, side_controller::type ctrl, side_proxy_controller::type proxy = side_proxy_controller::type::human);
148 void side_change_controller (int side_num, bool is_local, const std::string& pname, const std::string& controller_type);
149
150 // Manipulator from actionwml
151
153 utils::optional<std::string> replace_map(const gamemap & r);
154
155 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
156 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
157
158 // Global accessor from unit.hpp
159
160 unit_map::iterator find_visible_unit(const map_location &loc, const team& current_team, bool see_all = false);
161 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); }
162 bool has_visible_unit (const map_location & loc, const team & team, bool see_all = false) const;
163 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); }
164
165 // Wrapped functions from unit_map. These should ultimately provide notification to observers, pathfinding.
166
168 /** Calculates whether a team is defeated */
169 bool team_is_defeated(const team& t) const;
170};
171
172void swap(game_board & one, game_board & other);
173
174
175/**
176 * This object is used to temporary place a unit in the unit map, swapping out
177 * any unit that is already there. On destruction, it restores the unit map to
178 * its original.
179 */
191
192// Begin Temporary Unit Move Structs
193// TODO: Fix up the implementations which use game_board
194
195/**
196 * This object is used to temporary remove a unit from the unit map.
197 * On destruction, it restores the unit map to its original.
198 * unit_map iterators to this unit must not be accessed while the unit is temporarily
199 * removed, otherwise a collision will happen when trying to reinsert the unit.
200 */
212
213
214/**
215 * This object is used to temporary move a unit in the unit map, swapping out
216 * any unit that is already there. On destruction, it restores the unit map to
217 * its original.
218 */
220{
221 temporary_unit_mover(unit_map& m, const map_location& src, const map_location& dst, int new_moves, bool stand);
222 virtual ~temporary_unit_mover();
223
224private:
230 bool stand_;
231};
map_location loc
Definition move.cpp:172
double t
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
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.
virtual const gamemap & map() const override
virtual const std::vector< team > & teams() const override
void check_victory(bool &, bool &, bool &, bool &, std::set< unsigned > &, bool)
bool has_visible_unit(const map_location &loc, const team &team, bool see_all=false) const
gamemap & map()
virtual std::vector< std::string > & hidden_label_categories() override
friend void swap(game_board &one, game_board &other)
bool has_visible_unit(const map_location &loc, std::size_t team, bool see_all=false) const
std::vector< team > teams_
void side_drop_to(int side_num, side_controller::type ctrl, side_proxy_controller::type proxy=side_proxy_controller::type::human)
unit_map units_
unit_map & units()
n_unit::id_manager & unit_id_manager()
game_board & operator=(const game_board &other)=delete
std::vector< std::string > labels_
std::unique_ptr< gamemap > map_
bool change_terrain(const map_location &loc, const std::string &t, const std::string &mode, bool replace_if_failed)
utils::optional< std::string > replace_map(const gamemap &r)
void heal_all_survivors()
game_board(const game_board &other)=delete
n_unit::id_manager unit_id_manager_
unit_map::iterator find_visible_unit(const map_location &loc, const team &current_team, bool see_all=false)
bool team_is_defeated(const team &t) const
Calculates whether a team is defeated.
void end_turn(int pnum)
team & get_team(int i)
void side_change_controller(int side_num, bool is_local, const std::string &pname, const std::string &controller_type)
std::vector< team > & teams()
bool try_add_unit_to_recall_list(const map_location &loc, const unit_ptr &u)
void set_all_units_user_end_turn()
unit_map::iterator find_unit(const map_location &loc)
void write_config(config &cfg) const
unit_map::iterator find_visible_unit(const map_location &loc, std::size_t team, bool see_all=false)
virtual const unit_map & units() const override
void new_turn(int pnum)
virtual const std::vector< std::string > & hidden_label_categories() const override
Encapsulates the map of the game.
Definition map.hpp:176
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:39
const config * cfg
void swap(game_board &one, game_board &other)
std::shared_ptr< unit > unit_ptr
Definition ptr.hpp:26
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
Encapsulates the map of the game.
Definition location.hpp:46
A terrain string which is converted to a terrain is a string with 1 or 2 layers the layers are separa...
This object is used to temporary move a unit in the unit map, swapping out any unit that is already t...
const map_location src_
virtual ~temporary_unit_mover()
const map_location dst_
This object is used to temporary place a unit in the unit map, swapping out any unit that is already ...
virtual ~temporary_unit_placer()
const map_location loc_
This object is used to temporary remove a unit from the unit map.
const map_location loc_
virtual ~temporary_unit_remover()
#define i