-
-
Notifications
You must be signed in to change notification settings - Fork 989
/
conditional_wml.cpp
250 lines (218 loc) · 7.81 KB
/
conditional_wml.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
/*
Copyright (C) 2003 - 2014 by David White <dave@whitevine.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.
*/
/**
* @file
* Implementations of conditional action WML tags.
*/
#include "../global.hpp"
#include "conditional_wml.hpp"
#include "../config.hpp"
#include "../game_board.hpp"
#include "../game_data.hpp"
#include "../log.hpp"
#include "../recall_list_manager.hpp"
#include "../resources.hpp"
#include "../serialization/string_utils.hpp"
#include "../team.hpp"
#include "../terrain_filter.hpp"
#include "../unit.hpp"
#include "../unit_filter.hpp"
#include "../unit_map.hpp"
#include "../unit_types.hpp"
#include "../util.hpp"
#include "../variable.hpp"
#include <boost/foreach.hpp>
static lg::log_domain log_engine("engine");
#define WRN_NG LOG_STREAM(warn, log_engine)
// This file is in the game_events namespace.
namespace game_events {
namespace { // Support functions
bool internal_conditional_passed(const vconfig& cond, bool& backwards_compat)
{
const vconfig::child_list& true_keyword = cond.get_children("true");
backwards_compat = backwards_compat && true_keyword.empty();
if(!true_keyword.empty()) {
return true;
}
const vconfig::child_list& false_keyword = cond.get_children("false");
backwards_compat = backwards_compat && false_keyword.empty();
if(!false_keyword.empty()) {
return false;
}
static std::vector<std::pair<int,int> > default_counts = utils::parse_ranges("1-99999");
// If the if statement requires we have a certain unit,
// then check for that.
const vconfig::child_list& have_unit = cond.get_children("have_unit");
backwards_compat = backwards_compat && have_unit.empty();
for(vconfig::child_list::const_iterator u = have_unit.begin(); u != have_unit.end(); ++u) {
if(resources::units == NULL)
return false;
std::vector<std::pair<int,int> > counts = (*u).has_attribute("count")
? utils::parse_ranges((*u)["count"]) : default_counts;
int match_count = 0;
BOOST_FOREACH(const unit &i, *resources::units)
{
if ( i.hitpoints() > 0 && unit_filter::matches_filter(*u,i, resources::filter_con) ) {
++match_count;
if(counts == default_counts) {
// by default a single match is enough, so avoid extra work
break;
}
}
}
if ((*u)["search_recall_list"].to_bool())
{
for(std::vector<team>::iterator team = resources::teams->begin();
team!=resources::teams->end(); ++team)
{
if(counts == default_counts && match_count) {
break;
}
for(size_t t = 0; t < team->recall_list().size(); ++t) {
if(counts == default_counts && match_count) {
break;
}
scoped_recall_unit auto_store("this_unit", team->save_id(), t);
if ( unit_filter::matches_filter(*u,*team->recall_list()[t], resources::filter_con) ) {
++match_count;
}
}
}
}
if(!in_ranges(match_count, counts)) {
return false;
}
}
// If the if statement requires we have a certain location,
// then check for that.
const vconfig::child_list& have_location = cond.get_children("have_location");
backwards_compat = backwards_compat && have_location.empty();
for(vconfig::child_list::const_iterator v = have_location.begin(); v != have_location.end(); ++v) {
std::set<map_location> res;
terrain_filter(*v, *resources::units).get_locations(res);
std::vector<std::pair<int,int> > counts = (*v).has_attribute("count")
? utils::parse_ranges((*v)["count"]) : default_counts;
if(!in_ranges<int>(res.size(), counts)) {
return false;
}
}
// Check against each variable statement,
// to see if the variable matches the conditions or not.
const vconfig::child_list& variables = cond.get_children("variable");
backwards_compat = backwards_compat && variables.empty();
BOOST_FOREACH(const vconfig &values, variables)
{
const std::string name = values["name"];
config::attribute_value value = resources::gamedata->get_variable_const(name);
#define TEST_STR_ATTR(name, test) do { \
if (values.has_attribute(name)) { \
std::string attr_str = values[name].str(); \
std::string str_value = value.str(); \
if (!(test)) return false; \
} \
} while (0)
#define TEST_NUM_ATTR(name, test) do { \
if (values.has_attribute(name)) { \
double attr_num = values[name].to_double(); \
double num_value = value.to_double(); \
if (!(test)) return false; \
} \
} while (0)
#define TEST_BOL_ATTR(name, test) do { \
if (values.has_attribute(name)) { \
bool attr_bool = values[name].to_bool(); \
bool bool_value = value.to_bool(); \
if (!(test)) return false; \
} \
} while (0)
TEST_STR_ATTR("equals", str_value == attr_str);
TEST_STR_ATTR("not_equals", str_value != attr_str);
TEST_NUM_ATTR("numerical_equals", num_value == attr_num);
TEST_NUM_ATTR("numerical_not_equals", num_value != attr_num);
TEST_NUM_ATTR("greater_than", num_value > attr_num);
TEST_NUM_ATTR("less_than", num_value < attr_num);
TEST_NUM_ATTR("greater_than_equal_to", num_value >= attr_num);
TEST_NUM_ATTR("less_than_equal_to", num_value <= attr_num);
TEST_BOL_ATTR("boolean_equals", bool_value == attr_bool);
TEST_BOL_ATTR("boolean_not_equals", bool_value != attr_bool);
TEST_STR_ATTR("contains", str_value.find(attr_str) != std::string::npos);
#undef TEST_STR_ATTR
#undef TEST_NUM_ATTR
#undef TEST_BOL_ATTR
}
return true;
}
} // end anonymous namespace (support functions)
bool conditional_passed(const vconfig& cond, bool backwards_compat)
{
bool allow_backwards_compat = backwards_compat = backwards_compat &&
cond["backwards_compat"].to_bool(true);
bool matches = internal_conditional_passed(cond, allow_backwards_compat);
// Handle [and], [or], and [not] with in-order precedence
int or_count = 0;
vconfig::all_children_iterator cond_i = cond.ordered_begin();
vconfig::all_children_iterator cond_end = cond.ordered_end();
while(cond_i != cond_end)
{
const std::string& cond_name = cond_i.get_key();
const vconfig& cond_filter = cond_i.get_child();
// Handle [and]
if(cond_name == "and")
{
matches = matches && conditional_passed(cond_filter, backwards_compat);
backwards_compat = false;
}
// Handle [or]
else if(cond_name == "or")
{
matches = matches || conditional_passed(cond_filter, backwards_compat);
++or_count;
}
// Handle [not]
else if(cond_name == "not")
{
matches = matches && !conditional_passed(cond_filter, backwards_compat);
backwards_compat = false;
}
++cond_i;
}
// Check for deprecated [or] syntax
if(matches && or_count > 1 && allow_backwards_compat)
{
///@deprecated r18803 [or] syntax
lg::wml_error << "possible deprecated [or] syntax: now forcing re-interpretation\n";
/**
* @todo For now we will re-interpret it according to the old
* rules, but this should be later to prevent re-interpretation
* errors.
*/
const vconfig::child_list& orcfgs = cond.get_children("or");
for(unsigned int i=0; i < orcfgs.size(); ++i) {
if(conditional_passed(orcfgs[i])) {
return true;
}
}
return false;
}
return matches;
}
bool matches_special_filter(const config &cfg, const vconfig& filter)
{
if (!cfg) {
WRN_NG << "attempt to filter attack for an event with no attack data." << std::endl;
// better to not execute the event (so the problem is more obvious)
return false;
}
const attack_type attack(cfg);
return attack.matches_filter(filter.get_parsed_config());
}
} // end namespace game_events