-
-
Notifications
You must be signed in to change notification settings - Fork 989
/
configuration.cpp
352 lines (306 loc) · 13.1 KB
/
configuration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
Copyright (C) 2009 - 2016 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* Managing the AI configuration
* @file
*/
#include "configuration.hpp"
#include "filesystem.hpp"
#include "log.hpp"
#include "serialization/parser.hpp"
#include "serialization/preprocessor.hpp"
#include "wml_exception.hpp"
#include "config_assign.hpp"
#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <vector>
#include <deque>
#include <set>
namespace ai {
static lg::log_domain log_ai_configuration("ai/config");
#define DBG_AI_CONFIGURATION LOG_STREAM(debug, log_ai_configuration)
#define LOG_AI_CONFIGURATION LOG_STREAM(info, log_ai_configuration)
#define WRN_AI_CONFIGURATION LOG_STREAM(warn, log_ai_configuration)
#define ERR_AI_CONFIGURATION LOG_STREAM(err, log_ai_configuration)
void configuration::init(const config &game_config)
{
ai_configurations_.clear();
era_ai_configurations_.clear();
mod_ai_configurations_.clear();
const config &ais = game_config.child("ais");
default_config_ = ais.child("default_config");
if (!default_config_) {
ERR_AI_CONFIGURATION << "Missing AI [default_config]. Therefore, default_config_ set to empty." << std::endl;
default_config_ = config();
}
BOOST_FOREACH(const config &ai_configuration, ais.child_range("ai")) {
const std::string &id = ai_configuration["id"];
if (id.empty()){
ERR_AI_CONFIGURATION << "skipped AI config due to missing id" << ". Config contains:"<< std::endl << ai_configuration << std::endl;
continue;
}
if (ai_configurations_.count(id)>0){
ERR_AI_CONFIGURATION << "skipped AI config due to duplicate id [" << id << "]. Config contains:"<< std::endl << ai_configuration << std::endl;
continue;
}
description desc;
desc.id=id;
desc.text = ai_configuration["description"].t_str();
desc.cfg=ai_configuration;
ai_configurations_.insert(std::make_pair(id,desc));
LOG_AI_CONFIGURATION << "loaded AI config: " << ai_configuration["description"] << std::endl;
}
}
namespace {
void extract_ai_configurations(std::map<std::string, description> &storage, const config &input)
{
BOOST_FOREACH(const config &ai_configuration, input.child_range("ai")) {
const std::string &id = ai_configuration["id"];
if (id.empty()){
ERR_AI_CONFIGURATION << "skipped AI config due to missing id" << ". Config contains:"<< std::endl << ai_configuration << std::endl;
continue;
}
if (storage.count(id)>0){
ERR_AI_CONFIGURATION << "skipped AI config due to duplicate id [" << id << "]. Config contains:"<< std::endl << ai_configuration << std::endl;
continue;
}
description desc;
desc.id=id;
desc.text = ai_configuration["description"].t_str();
desc.cfg=ai_configuration;
storage.insert(std::make_pair(id,desc));
LOG_AI_CONFIGURATION << "loaded AI config: " << ai_configuration["description"] << std::endl;
}
}
}
void configuration::add_era_ai_from_config(const config &era)
{
era_ai_configurations_.clear();
extract_ai_configurations(era_ai_configurations_, era);
}
void configuration::add_mod_ai_from_config(config::const_child_itors mods)
{
mod_ai_configurations_.clear();
BOOST_FOREACH(const config &mod, mods) {
extract_ai_configurations(mod_ai_configurations_, mod);
}
}
std::vector<description*> configuration::get_available_ais(){
std::vector<description*> ais_list;
for(description_map::iterator desc = ai_configurations_.begin(); desc!=ai_configurations_.end(); ++desc) {
ais_list.push_back(&desc->second);
DBG_AI_CONFIGURATION << "has ai with config: "<< std::endl << desc->second.cfg<< std::endl;
}
for(description_map::iterator desc = era_ai_configurations_.begin(); desc!=era_ai_configurations_.end(); ++desc) {
ais_list.push_back(&desc->second);
DBG_AI_CONFIGURATION << "has ai with config: "<< std::endl << desc->second.cfg<< std::endl;
}
for(description_map::iterator desc = mod_ai_configurations_.begin(); desc!=mod_ai_configurations_.end(); ++desc) {
ais_list.push_back(&desc->second);
DBG_AI_CONFIGURATION << "has ai with config: "<< std::endl << desc->second.cfg<< std::endl;
}
return ais_list;
}
const config& configuration::get_ai_config_for(const std::string &id)
{
description_map::iterator cfg_it = ai_configurations_.find(id);
if (cfg_it==ai_configurations_.end()){
description_map::iterator era_cfg_it = era_ai_configurations_.find(id);
if (era_cfg_it==era_ai_configurations_.end()){
description_map::iterator mod_cfg_it = mod_ai_configurations_.find(id);
if (mod_cfg_it==mod_ai_configurations_.end()) {
return default_config_;
} else {
return mod_cfg_it->second.cfg;
}
} else {
return era_cfg_it->second.cfg;
}
}
return cfg_it->second.cfg;
}
configuration::description_map configuration::ai_configurations_ = configuration::description_map();
configuration::description_map configuration::era_ai_configurations_ = configuration::description_map();
configuration::description_map configuration::mod_ai_configurations_ = configuration::description_map();
config configuration::default_config_ = config();
bool configuration::get_side_config_from_file(const std::string& file, config& cfg ){
try {
filesystem::scoped_istream stream = preprocess_file(filesystem::get_wml_location(file));
read(cfg, *stream);
LOG_AI_CONFIGURATION << "Reading AI configuration from file '" << file << "'" << std::endl;
} catch(config::error &) {
ERR_AI_CONFIGURATION << "Error while reading AI configuration from file '" << file << "'" << std::endl;
return false;
}
LOG_AI_CONFIGURATION << "Successfully read AI configuration from file '" << file << "'" << std::endl;
return true;
}
const config& configuration::get_default_ai_parameters()
{
return default_config_;
}
bool configuration::parse_side_config(side_number side, const config& original_cfg, config &cfg )
{
LOG_AI_CONFIGURATION << "side "<< side <<": parsing AI configuration from config" << std::endl;
//leave only the [ai] children
cfg = config();
BOOST_FOREACH(const config &aiparam, original_cfg.child_range("ai")) {
cfg.add_child("ai",aiparam);
}
//backward-compatibility hack: put ai_algorithm if it is present
if (const config::attribute_value *v = original_cfg.get("ai_algorithm")) {
config ai_a;
ai_a["ai_algorithm"] = *v;
cfg.add_child("ai",ai_a);
}
DBG_AI_CONFIGURATION << "side " << side << ": config contains:"<< std::endl << cfg << std::endl;
//insert default config at the beginning
if (default_config_) {
DBG_AI_CONFIGURATION << "side "<< side <<": applying default configuration" << std::endl;
cfg.add_child_at("ai",default_config_,0);
} else {
ERR_AI_CONFIGURATION << "side "<< side <<": default configuration is not available, do not applying it" << std::endl;
}
expand_simplified_aspects(side, cfg);
//construct new-style integrated config
LOG_AI_CONFIGURATION << "side "<< side << ": doing final operations on AI config"<< std::endl;
config parsed_cfg = config();
LOG_AI_CONFIGURATION << "side "<< side <<": merging AI configurations"<< std::endl;
BOOST_FOREACH(const config &aiparam, cfg.child_range("ai")) {
parsed_cfg.append(aiparam);
}
LOG_AI_CONFIGURATION << "side "<< side <<": merging AI aspect with the same id"<< std::endl;
parsed_cfg.merge_children_by_attribute("aspect","id");
LOG_AI_CONFIGURATION << "side "<< side <<": removing duplicate [default] tags from aspects"<< std::endl;
BOOST_FOREACH(config &aspect_cfg, parsed_cfg.child_range("aspect")) {
if (!aspect_cfg.child("default")) {
WRN_AI_CONFIGURATION << "side "<< side <<": aspect with id=["<<aspect_cfg["id"]<<"] lacks default config facet!" <<std::endl;
continue;
}
config c = aspect_cfg.child("default");
aspect_cfg.clear_children("default");
aspect_cfg.add_child("default",c);
}
DBG_AI_CONFIGURATION << "side "<< side <<": done parsing side config, it contains:"<< std::endl << parsed_cfg << std::endl;
LOG_AI_CONFIGURATION << "side "<< side <<": done parsing side config"<< std::endl;
cfg = parsed_cfg;
return true;
}
static const std::set<std::string> non_aspect_attributes = boost::assign::list_of("turns")("time_of_day")("engine")("ai_algorithm")("id")("description");
static const std::set<std::string> just_copy_tags = boost::assign::list_of("engine")("stage")("aspect")("goal")("modify_ai");
static const std::set<std::string> old_goal_tags = boost::assign::list_of("target")("target_location")("protect_unit")("protect_location");
void configuration::expand_simplified_aspects(side_number side, config &cfg) {
std::string algorithm;
config base_config, parsed_config;
BOOST_FOREACH(const config &aiparam, cfg.child_range("ai")) {
std::string turns, time_of_day, engine = "cpp";
if (aiparam.has_attribute("turns")) {
turns = aiparam["turns"].str();
}
if (aiparam.has_attribute("time_of_day")) {
time_of_day = aiparam["time_of_day"].str();
}
if (aiparam.has_attribute("engine")) {
engine = aiparam["engine"].str();
}
if (aiparam.has_attribute("ai_algorithm")) {
if (algorithm.empty()) {
algorithm = aiparam["ai_algorithm"].str();
if (algorithm == "idle_ai") {
lg::wml_error() << "side " << side << " uses deprecated ai_algorithm=idle_ai; use ai_idle instead.\n";
algorithm = "ai_idle";
}
base_config = get_ai_config_for(algorithm);
} else if(algorithm != aiparam["ai_algorithm"]) {
lg::wml_error() << "side " << side << " has two [ai] tags with contradictory ai_algorithm - the first one will take precedence.\n";
}
}
std::deque<std::pair<std::string, config> > facet_configs;
BOOST_FOREACH(const config::attribute &attr, aiparam.attribute_range()) {
if (non_aspect_attributes.count(attr.first)) {
continue;
}
config facet_config;
facet_config["engine"] = engine;
facet_config["name"] = "standard_aspect"; // I believe this can be omitted.
facet_config["turns"] = turns;
facet_config["time_of_day"] = time_of_day;
facet_config["value"] = attr.second;
facet_configs.push_back(std::make_pair(attr.first, facet_config));
}
BOOST_FOREACH(const config::any_child &child, aiparam.all_children_range()) {
if (just_copy_tags.count(child.key)) {
// These aren't simplified, so just copy over unchanged.
parsed_config.add_child(child.key, child.cfg);
continue;
} else if(old_goal_tags.count(child.key)) {
// A simplified goal, mainly kept around just for backwards compatibility.
config goal_config, criteria_config = child.cfg;
goal_config["name"] = child.key;
goal_config["turns"] = turns;
goal_config["time_of_day"] = time_of_day;
if(child.key.substr(0,7) == "protect" && criteria_config.has_attribute("protect_radius")) {
goal_config["protect_radius"] = criteria_config["protect_radius"];
criteria_config.remove_attribute("protect_radius");
}
if(criteria_config.has_attribute("value")) {
goal_config["value"] = criteria_config["value"];
criteria_config.remove_attribute("value");
}
parsed_config.add_child("goal", goal_config);
continue;
}
// Now there's two possibilities. If the tag is [attacks] or contains either value= or [value],
// then it can be copied verbatim as a [facet] tag.
// Otherwise, it needs to be placed as a [value] within a [facet] tag.
if (child.key == "attacks" || child.cfg.has_attribute("value") || child.cfg.has_child("value")) {
facet_configs.push_back(std::make_pair(child.key, child.cfg));
} else {
config facet_config;
facet_config["engine"] = engine;
facet_config["name"] = "standard_aspect"; // I believe this can be omitted.
facet_config["turns"] = turns;
facet_config["time_of_day"] = time_of_day;
facet_config.add_child("value", child.cfg);
if (child.key == "leader_goal" && !child.cfg["id"].empty()) {
// Use id= attribute (if present) as the facet ID
const std::string& id = child.cfg["id"];
if(id != "*" && id.find_first_not_of("0123456789") != std::string::npos) {
facet_config["id"] = child.cfg["id"];
}
}
facet_configs.push_back(std::make_pair(child.key, facet_config));
}
}
std::map<std::string, config> aspect_configs;
while (!facet_configs.empty()) {
const std::string &aspect = facet_configs.front().first;
const config &facet_config = facet_configs.front().second;
aspect_configs[aspect]["id"] = aspect; // Will sometimes be redundant assignment
aspect_configs[aspect].add_child("facet", facet_config);
facet_configs.pop_front();
}
typedef std::map<std::string, config>::value_type aspect_pair;
BOOST_FOREACH(const aspect_pair& p, aspect_configs) {
parsed_config.add_child("aspect", p.second);
}
}
if (algorithm.empty()) {
base_config = get_ai_config_for("ai_default_rca");
}
BOOST_FOREACH(const config::any_child &child, parsed_config.all_children_range()) {
base_config.add_child(child.key, child.cfg);
}
cfg.clear_children("ai");
cfg.add_child("ai", base_config);
}
} //end of namespace ai