The Battle for Wesnoth  1.19.0-dev
flg_manager.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 2024
3  by Andrius Silinskas <silinskas.andrius@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 <string>
19 #include <vector>
20 
21 class config;
22 namespace randomness { class mt_rng; }
23 
24 namespace ng {
25 
26 const std::string random_enemy_picture("units/random-dice.png");
27 
28 /** FLG stands for faction, leader and gender. */
30 {
31 public:
32  flg_manager(const std::vector<const config*>& era_factions,
33  const config& side, bool lock_settings, bool use_map_settings, bool saved_game);
34 
35  void set_current_faction(const unsigned index);
36  void set_current_faction(const std::string& id);
37  void set_current_faction(const config& faction)
38  { set_current_faction(faction_index(faction)); }
39 
40  void set_current_leader(const unsigned index);
41  void set_current_leader(const std::string& leader);
42 
43  void set_current_gender(const unsigned index);
44  void set_current_gender(const std::string& gender);
45 
46  bool is_random_faction();
47 
48  // Second Argument is a list of faction ids we don't want to match, used to implement random faction modes.
49  // If it is not possible to resolve then we just proceed anyways rather than give an error.
50  void resolve_random(randomness::mt_rng & rng, const std::vector<std::string> & avoid);
51 
52  bool is_saved_game() const
53  {
54  return saved_game_;
55  }
56 
57  // Picks the first faction with the greater amount of data
58  // matching the criteria.
59  int find_suitable_faction() const;
60 
61  const std::vector<const config*>& choosable_factions() const
62  { return choosable_factions_; }
63  const std::vector<std::string>& choosable_leaders() const
64  { return choosable_leaders_; }
65  const std::vector<std::string>& choosable_genders() const
66  { return choosable_genders_; }
67  const config& current_faction() const
68  { return *current_faction_; }
69  const std::string& current_leader() const
70  { return current_leader_; }
71  const std::string& current_gender() const
72  { return current_gender_; }
73 
74  const config* default_leader_cfg() const
75  { return default_leader_cfg_; }
76 
77  int current_faction_index() const;
78 
80  { return leader_index(current_leader_); }
82  { return gender_index(current_gender_); }
83 
84 private:
85  flg_manager(const flg_manager&) = delete;
86  flg_manager& operator=(const flg_manager&) = delete;
87 
94 
95  // Append leaders from a given faction to a choosable factions.
96  void append_leaders_from_faction(const config* faction);
97 
99 
100  int faction_index(const config& faction) const;
101  /** returns -1 if no leader with that name was found */
102  int leader_index(const std::string& leader) const;
103  /** returns -1 if no gender with that name was found */
104  int gender_index(const std::string& gender) const;
105 
106  const std::vector<const config*>& era_factions_;
107 
108  // not sure how reilable the content of this field is, it's currently only used for debugging info.
109  const int side_num_;
111 
112  const std::string original_type_;
113  const std::string original_gender_;
114  std::string savegame_gender_;
115  const std::string original_faction_;
116  const std::vector<std::string> original_recruit_;
117  const std::string choose_faction_by_leader_;
118  const bool saved_game_;
119  const bool has_no_recruits_;
120 
123 
124  // All factions which could be played by a side (including Random).
125  std::vector<const config*> available_factions_;
126  std::vector<std::string> available_leaders_;
127  std::vector<std::string> available_genders_;
128 
129  std::vector<const config*> choosable_factions_;
130  std::vector<std::string> choosable_leaders_;
131  std::vector<std::string> choosable_genders_;
132 
134  std::string current_leader_;
135  std::string current_gender_;
136 
137  std::string default_leader_type_;
140 
141  static const config& get_default_faction(const config& cfg);
142 };
143 
144 } // end namespace ng
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
FLG stands for faction, leader and gender.
Definition: flg_manager.hpp:30
void update_available_factions()
const bool faction_from_recruit_
const std::vector< const config * > & choosable_factions() const
Definition: flg_manager.hpp:61
void update_choosable_leaders()
void update_choosable_factions()
std::string default_leader_type_
void update_available_leaders()
const std::vector< std::string > original_recruit_
std::string current_leader_
const std::string & current_gender() const
Definition: flg_manager.hpp:71
const bool has_no_recruits_
void update_available_genders()
void set_current_faction(const unsigned index)
void set_current_faction(const config &faction)
Definition: flg_manager.hpp:37
void select_default_faction()
void resolve_random(randomness::mt_rng &rng, const std::vector< std::string > &avoid)
flg_manager & operator=(const flg_manager &)=delete
const std::vector< std::string > & choosable_genders() const
Definition: flg_manager.hpp:65
std::vector< std::string > choosable_leaders_
std::vector< std::string > choosable_genders_
std::vector< std::string > available_genders_
bool is_saved_game() const
Definition: flg_manager.hpp:52
const std::string original_type_
flg_manager(const flg_manager &)=delete
int current_gender_index() const
Definition: flg_manager.hpp:81
int current_leader_index() const
Definition: flg_manager.hpp:79
const config * default_leader_cfg() const
Definition: flg_manager.hpp:74
void update_choosable_genders()
const config * current_faction_
const std::vector< std::string > & choosable_leaders() const
Definition: flg_manager.hpp:63
int faction_index(const config &faction) const
const std::string choose_faction_by_leader_
const std::string original_faction_
void set_current_leader(const unsigned index)
int leader_index(const std::string &leader) const
returns -1 if no leader with that name was found
const std::string original_gender_
int gender_index(const std::string &gender) const
returns -1 if no gender with that name was found
int current_faction_index() const
static const config & get_default_faction(const config &cfg)
const bool saved_game_
std::string default_leader_gender_
std::vector< const config * > available_factions_
std::vector< const config * > choosable_factions_
flg_manager(const std::vector< const config * > &era_factions, const config &side, bool lock_settings, bool use_map_settings, bool saved_game)
Definition: flg_manager.cpp:34
void set_current_gender(const unsigned index)
bool is_random_faction()
const config & current_faction() const
Definition: flg_manager.hpp:67
const std::string & current_leader() const
Definition: flg_manager.hpp:69
std::string savegame_gender_
void append_leaders_from_faction(const config *faction)
const config * default_leader_cfg_
const std::vector< const config * > & era_factions_
std::string current_gender_
int find_suitable_faction() const
const int side_num_
std::vector< std::string > available_leaders_
const std::string random_enemy_picture("units/random-dice.png")
bool use_map_settings()
Definition: game.cpp:478
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70