-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
side_filter.cpp
288 lines (258 loc) · 7.59 KB
/
side_filter.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
/*
Copyright (C) 2010 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project https://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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "config.hpp"
#include "display_context.hpp"
#include "filter_context.hpp"
#include "log.hpp"
#include "recall_list_manager.hpp"
#include "side_filter.hpp"
#include "variable.hpp"
#include "team.hpp"
#include "serialization/string_utils.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "play_controller.hpp"
#include "resources.hpp"
#include "synced_context.hpp"
#include "units/unit.hpp"
#include "units/filter.hpp"
#include "units/map.hpp"
#include "variable.hpp"
#include "filter_context.hpp"
#include "formula/callable_objects.hpp"
#include "formula/formula.hpp"
#include "formula/function_gamestate.hpp"
static lg::log_domain log_engine_sf("engine/side_filter");
#define ERR_NG LOG_STREAM(err, log_engine_sf)
side_filter::~side_filter() {}
side_filter::side_filter(const vconfig& cfg, const filter_context * fc, bool flat_tod)
: cfg_(cfg)
, flat_(flat_tod)
, side_string_()
, fc_(fc)
{
}
side_filter::side_filter(const std::string &side_string, const filter_context * fc, bool flat_tod)
: cfg_(vconfig::empty_vconfig()), flat_(flat_tod), side_string_(side_string), fc_(fc)
{
}
std::vector<int> side_filter::get_teams() const
{
assert(fc_);
//@todo: replace with better implementation
std::vector<int> result;
for(const team &t : fc_->get_disp_context().teams()) {
if (match(t)) {
result.push_back(t.side());
}
}
return result;
}
static bool check_side_number(const team &t, const std::string &str)
{
std::vector<std::pair<int,int>> ranges = utils::parse_ranges(str);
int side_number = t.side();
std::vector<std::pair<int,int>>::const_iterator range, range_end = ranges.end();
for (range = ranges.begin(); range != range_end; ++range) {
if(side_number >= range->first && side_number <= range->second) {
return true;
}
}
return false;
}
bool side_filter::match_internal(const team &t) const
{
assert(fc_);
if (cfg_.has_attribute("side_in")) {
if (!check_side_number(t,cfg_["side_in"])) {
return false;
}
}
if (cfg_.has_attribute("side")) {
if (!check_side_number(t,cfg_["side"])) {
return false;
}
}
if (!side_string_.empty()) {
if (!check_side_number(t,side_string_)) {
return false;
}
}
config::attribute_value cfg_team_name = cfg_["team_name"];
if (!cfg_team_name.blank()) {
const std::string& that_team_name = cfg_team_name;
const std::string& this_team_name = t.team_name();
if(std::find(this_team_name.begin(), this_team_name.end(), ',') == this_team_name.end()) {
if(this_team_name != that_team_name) return false;
}
else {
const std::vector<std::string>& these_team_names = utils::split(this_team_name);
bool search_futile = true;
for(const std::string& this_single_team_name : these_team_names) {
if(this_single_team_name == that_team_name) {
search_futile = false;
break;
}
}
if(search_futile) return false;
}
}
//Allow filtering on units
if(cfg_.has_child("has_unit")) {
const vconfig & ufilt_cfg = cfg_.child("has_unit");
if (!ufilter_) {
ufilter_.reset(new unit_filter(ufilt_cfg.make_safe()));
ufilter_->set_use_flat_tod(flat_);
}
bool found = false;
for(const unit &u : fc_->get_disp_context().units()) {
if (u.side() != t.side()) {
continue;
}
if (ufilter_->matches(u)) {
found = true;
break;
}
}
if(!found && ufilt_cfg["search_recall_list"].to_bool(false)) {
for(const unit_const_ptr u : t.recall_list()) {
scoped_recall_unit this_unit("this_unit", t.save_id_or_number(), t.recall_list().find_index(u->id()));
if(ufilter_->matches(*u)) {
found = true;
break;
}
}
}
if (!found) {
return false;
}
}
const vconfig& enemy_of = cfg_.child("enemy_of");
if(!enemy_of.null()) {
if (!enemy_filter_)
enemy_filter_.reset(new side_filter(enemy_of, fc_));
const std::vector<int>& teams = enemy_filter_->get_teams();
if(teams.empty()) return false;
for(const int side : teams) {
if(!fc_->get_disp_context().get_team(side).is_enemy(t.side()))
return false;
}
}
const vconfig& allied_with = cfg_.child("allied_with");
if(!allied_with.null()) {
if (!allied_filter_)
allied_filter_.reset(new side_filter(allied_with, fc_));
const std::vector<int>& teams = allied_filter_->get_teams();
if(teams.empty()) return false;
for(const int side : teams) {
if(fc_->get_disp_context().get_team(side).is_enemy(t.side()))
return false;
}
}
const vconfig& has_enemy = cfg_.child("has_enemy");
if(!has_enemy.null()) {
if (!has_enemy_filter_)
has_enemy_filter_.reset(new side_filter(has_enemy, fc_));
const std::vector<int>& teams = has_enemy_filter_->get_teams();
bool found = false;
for(const int side : teams) {
if(fc_->get_disp_context().get_team(side).is_enemy(t.side()))
{
found = true;
break;
}
}
if (!found) return false;
}
const vconfig& has_ally = cfg_.child("has_ally");
if(!has_ally.null()) {
if (!has_ally_filter_)
has_ally_filter_.reset(new side_filter(has_ally, fc_));
const std::vector<int>& teams = has_ally_filter_->get_teams();
bool found = false;
for(const int side : teams) {
if(!fc_->get_disp_context().get_team(side).is_enemy(t.side()))
{
found = true;
break;
}
}
if (!found) return false;
}
const config::attribute_value cfg_controller = cfg_["controller"];
if (!cfg_controller.blank())
{
if (resources::controller->is_networked_mp() && synced_context::is_synced()) {
ERR_NG << "ignoring controller= in SSF due to danger of OOS errors" << std::endl;
}
else {
bool found = false;
for(const std::string& controller : utils::split(cfg_controller))
{
if(t.controller().to_string() == controller) {
found = true;
}
}
if(!found) {
return false;
}
}
}
if (cfg_.has_attribute("formula")) {
try {
const wfl::team_callable callable(t);
const wfl::formula form(cfg_["formula"], new wfl::gamestate_function_symbol_table);
if(!form.evaluate(callable).as_bool()) {
return false;
}
return true;
} catch(const wfl::formula_error& e) {
lg::wml_error() << "Formula error in side filter: " << e.type << " at " << e.filename << ':' << e.line << ")\n";
// Formulae with syntax errors match nothing
return false;
}
}
if (cfg_.has_attribute("lua_function")) {
std::string lua_function = cfg_["lua_function"].str();
if (!lua_function.empty() && fc_->get_lua_kernel()) {
if (!fc_->get_lua_kernel()->run_filter(lua_function.c_str(), t)) {
return false;
}
}
}
return true;
}
bool side_filter::match(int side) const
{
assert(fc_);
return this->match((fc_->get_disp_context().get_team(side)));
}
bool side_filter::match(const team& t) const
{
bool matches = match_internal(t);
// Handle [and], [or], and [not] with in-order precedence
for(const auto& [key, filter] : cfg_.all_ordered()) {
// Handle [and]
if(key == "and") {
matches = matches && side_filter(filter, fc_, flat_).match(t);
}
// Handle [or]
else if(key == "or") {
matches = matches || side_filter(filter, fc_, flat_).match(t);
}
// Handle [not]
else if(key == "not") {
matches = matches && !side_filter(filter, fc_, flat_).match(t);
}
}
return matches;
}