1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name deity_domain.cpp - The deity domain source file. */
12 //
13 // (c) Copyright 2018-2019 by Andrettin
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29
30 /*----------------------------------------------------------------------------
31 -- Includes
32 ----------------------------------------------------------------------------*/
33
34 #include "stratagus.h"
35
36 #include "religion/deity_domain.h"
37
38 #include "config.h"
39 #include "upgrade/upgrade_structs.h"
40
41 /*----------------------------------------------------------------------------
42 -- Variables
43 ----------------------------------------------------------------------------*/
44
45 std::vector<CDeityDomain *> CDeityDomain::DeityDomains;
46 std::map<std::string, CDeityDomain *> CDeityDomain::DeityDomainsByIdent;
47 std::map<const CUpgrade *, CDeityDomain *> CDeityDomain::DeityDomainsByUpgrade;
48
49 /*----------------------------------------------------------------------------
50 -- Functions
51 ----------------------------------------------------------------------------*/
52
53 /**
54 ** @brief Get a deity domain
55 **
56 ** @param ident The deity domain's string identifier
57 ** @param should_find Whether it is an error if the deity domain could not be found; this is true by default
58 **
59 ** @return The deity domain if found, or null otherwise
60 */
GetDeityDomain(const std::string & ident,const bool should_find)61 CDeityDomain *CDeityDomain::GetDeityDomain(const std::string &ident, const bool should_find)
62 {
63 std::map<std::string, CDeityDomain *>::const_iterator find_iterator = DeityDomainsByIdent.find(ident);
64
65 if (find_iterator != DeityDomainsByIdent.end()) {
66 return find_iterator->second;
67 }
68
69 if (should_find) {
70 fprintf(stderr, "Invalid deity domain: \"%s\".\n", ident.c_str());
71 }
72
73 return nullptr;
74 }
75
76 /**
77 ** @brief Get or add a deity domain
78 **
79 ** @param ident The deity domain's string identifier
80 **
81 ** @return The deity domain if found, or a newly-created one otherwise
82 */
GetOrAddDeityDomain(const std::string & ident)83 CDeityDomain *CDeityDomain::GetOrAddDeityDomain(const std::string &ident)
84 {
85 CDeityDomain *deity_domain = GetDeityDomain(ident, false);
86
87 if (!deity_domain) {
88 deity_domain = new CDeityDomain;
89 deity_domain->Ident = ident;
90 DeityDomains.push_back(deity_domain);
91 DeityDomainsByIdent[ident] = deity_domain;
92 }
93
94 return deity_domain;
95 }
96
97 /**
98 ** @brief Get a deity domain by its respective upgrade
99 **
100 ** @param upgrade The deity domain's upgrade
101 ** @param should_find Whether it is an error if the deity domain could not be found; this is true by default
102 **
103 ** @return The upgrade's deity domain, if any
104 */
GetDeityDomainByUpgrade(const CUpgrade * upgrade,const bool should_find)105 CDeityDomain *CDeityDomain::GetDeityDomainByUpgrade(const CUpgrade *upgrade, const bool should_find)
106 {
107 std::map<const CUpgrade *, CDeityDomain *>::const_iterator find_iterator = DeityDomainsByUpgrade.find(upgrade);
108
109 if (find_iterator != DeityDomainsByUpgrade.end()) {
110 return find_iterator->second;
111 }
112
113 if (should_find) {
114 fprintf(stderr, "No deity domain found for upgrade: \"%s\".\n", upgrade->Ident.c_str());
115 }
116
117 return nullptr;
118 }
119
120 /**
121 ** @brief Remove the existing deity domains
122 */
ClearDeityDomains()123 void CDeityDomain::ClearDeityDomains()
124 {
125 for (size_t i = 0; i < DeityDomains.size(); ++i) {
126 delete DeityDomains[i];
127 }
128 DeityDomains.clear();
129 }
130
131 /**
132 ** @brief Process data provided by a configuration file
133 **
134 ** @param config_data The configuration data
135 */
ProcessConfigData(const CConfigData * config_data)136 void CDeityDomain::ProcessConfigData(const CConfigData *config_data)
137 {
138 for (size_t i = 0; i < config_data->Properties.size(); ++i) {
139 std::string key = config_data->Properties[i].first;
140 std::string value = config_data->Properties[i].second;
141
142 if (key == "name") {
143 this->Name = value;
144 } else if (key == "upgrade") {
145 value = FindAndReplaceString(value, "_", "-");
146 CUpgrade *upgrade = CUpgrade::Get(value);
147 if (upgrade) {
148 this->Upgrade = upgrade;
149 CDeityDomain::DeityDomainsByUpgrade[upgrade] = this;
150 } else {
151 fprintf(stderr, "Invalid upgrade: \"%s\".\n", value.c_str());
152 }
153 } else if (key == "ability") {
154 value = FindAndReplaceString(value, "_", "-");
155 CUpgrade *ability = CUpgrade::Get(value);
156 if (ability) {
157 this->Abilities.push_back(ability);
158 ability->DeityDomains.push_back(this);
159 } else {
160 fprintf(stderr, "Invalid upgrade: \"%s\".\n", value.c_str());
161 }
162 } else if (key == "description") {
163 this->Description = value;
164 } else if (key == "background") {
165 this->Background = value;
166 } else if (key == "quote") {
167 this->Quote = value;
168 } else {
169 fprintf(stderr, "Invalid school of magic property: \"%s\".\n", key.c_str());
170 }
171 }
172 }
173