The Battle for Wesnoth  1.19.1+dev
display_context.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 "units/orb_status.hpp"
19 #include "units/ptr.hpp"
20 #include <string>
21 #include <vector>
22 
23 class team;
24 class gamemap;
25 class unit_map;
26 
27 class unit;
28 struct map_location;
29 
30 /**
31  * Abstract class for exposing game data that doesn't depend on the GUI, however which for historical
32  * reasons is generally accessed via the GUI method display::get_singleton().
33  */
35 {
36 public:
37  virtual const std::vector<team> & teams() const = 0;
38  virtual const gamemap & map() const = 0;
39  virtual const unit_map & units() const = 0;
40  virtual const std::vector<std::string> & hidden_label_categories() const = 0;
41  virtual std::vector<std::string> & hidden_label_categories() = 0;
42 
43  /** This getter takes a 1-based side number, not a 0-based team number. */
44  const team& get_team(int side) const;
45 
46  // this one is only a template function to prevent compilation erros when class team is an incomplete type.
47  template<typename T = void>
48  bool has_team(int side) const
49  {
50  return side > 0 && side <= static_cast<int>(teams().size());
51  }
52 
53  // Helper for is_visible_to_team
54 
55  /**
56  * Given a location and a side number, indicates whether an invisible unit of that side at that
57  * location would be revealed (perhaps ambushed), based on what team side_num can see.
58  * If see_all is true then the calculation ignores fog, and enemy ambushers.
59  */
60  bool would_be_discovered(const map_location & loc, int side_num, bool see_all = true);
61 
62  // Needed for reports
63 
64  const unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all = false) const;
65  unit_const_ptr get_visible_unit_shared_ptr(const map_location &loc, const team &current_team, bool see_all = false) const;
66 
68  {
69  /**
70  * The unit can move to another hex, taking account of enemies' locations, ZoC and
71  * terrain costs vs current movement points.
72  */
73  bool move = false;
74 
75  /**
76  * The unit can make an attack from the hex that it's currently on, this
77  * requires attack points and a non-petrified enemy in an adjacent hex.
78  */
79  bool attack_here = false;
80 
81  operator bool() const
82  {
83  return move || attack_here;
84  }
85  };
86 
87  /**
88  * Work out what @a u can do - this does not check which player's turn is currently active, the
89  * result is calculated assuming that the unit's owner is currently active.
90  */
91  can_move_result unit_can_move(const unit& u) const;
92 
93  /**
94  * Returns an enumurated summary of whether this unit can move and/or attack.
95  *
96  * This does not check which player's turn is currently active, the result is calculated
97  * assuming that the unit's owner is currently active. For this reason this never returns
98  * orb_status::enemy nor orb_status::allied.
99  */
100  orb_status unit_orb_status(const unit& u) const;
101 
102  /**
103  * Given the location of a village, will return the 1-based number
104  * of the team that currently owns it, and 0 if it is unowned.
105  */
106  int village_owner(const map_location & loc) const;
107 
108  // Accessors from unit.cpp
109 
110  /** Returns the number of units of the side @a side_num. */
111  int side_units(int side_num) const;
112 
113  /** Returns the total cost of units of side @a side_num. */
114  int side_units_cost(int side_num) const ;
115 
116  int side_upkeep(int side_num) const ;
117 
118  // Accessor from team.cpp
119 
120  /** Check if we are an observer in this game */
121  bool is_observer() const;
122 
123  // Dtor
124  virtual ~display_context() {}
125 };
126 
127 struct team_data
128 {
129  team_data(const display_context& dc, const team& tm);
130 
131  int side = 0, units = 0, upkeep = 0, expenses = 0, net_income = 0;
132 };
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.
orb_status unit_orb_status(const unit &u) const
Returns an enumurated summary of whether this unit can move and/or attack.
int village_owner(const map_location &loc) const
Given the location of a village, will return the 1-based number of the team that currently owns it,...
const unit * get_visible_unit(const map_location &loc, const team &current_team, bool see_all=false) const
bool is_observer() const
Check if we are an observer in this game.
can_move_result unit_can_move(const unit &u) const
Work out what u can do - this does not check which player's turn is currently active,...
virtual const gamemap & map() const =0
virtual const std::vector< std::string > & hidden_label_categories() const =0
int side_units(int side_num) const
Returns the number of units of the side side_num.
virtual ~display_context()
virtual std::vector< std::string > & hidden_label_categories()=0
bool has_team(int side) const
virtual const std::vector< team > & teams() const =0
unit_const_ptr get_visible_unit_shared_ptr(const map_location &loc, const team &current_team, bool see_all=false) const
int side_units_cost(int side_num) const
Returns the total cost of units of side side_num.
virtual const unit_map & units() const =0
bool would_be_discovered(const map_location &loc, int side_num, bool see_all=true)
Given a location and a side number, indicates whether an invisible unit of that side at that location...
int side_upkeep(int side_num) const
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
This class represents a single unit of a specific type.
Definition: unit.hpp:133
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
orb_status
Corresponds to the colored orbs displayed above units' hp-bar and xp-bar.
Definition: orb_status.hpp:23
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
bool attack_here
The unit can make an attack from the hex that it's currently on, this requires attack points and a no...
bool move
The unit can move to another hex, taking account of enemies' locations, ZoC and terrain costs vs curr...
Encapsulates the map of the game.
Definition: location.hpp:38
team_data(const display_context &dc, const team &tm)