This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
/
side_filter.cpp
162 lines (141 loc) · 3.95 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
/* $Id: side_filter.cpp 49424 2011-05-08 11:44:35Z crab $ */
/*
Copyright (C) 2010 - 2011 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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "global.hpp"
#include "config.hpp"
#include "foreach.hpp"
#include "log.hpp"
#include "resources.hpp"
#include "side_filter.hpp"
#include "variable.hpp"
#include "team.hpp"
#include "serialization/string_utils.hpp"
static lg::log_domain log_engine_sf("engine/side_filter");
#define ERR_NG LOG_STREAM(err, log_engine_sf)
#ifdef _MSC_VER
// This is a workaround for a VC bug; this constructor is never called
// and so we don't care about the warnings this quick fix generates
#pragma warning(push)
#pragma warning(disable:4413)
side_filter::side_filter():
cfg_(vconfig::unconstructed_vconfig()),
flat_(),
side_string_()
{
assert(false);
}
#pragma warning(pop)
#endif
side_filter::side_filter(const vconfig& cfg, bool flat_tod) :
cfg_(cfg),
flat_(flat_tod),
side_string_()
{
}
side_filter::side_filter(const vconfig &cfg, const std::string &side_string, bool flat_tod)
: cfg_(cfg), flat_(flat_tod), side_string_(side_string)
{
}
side_filter::side_filter(const std::string &side_string, bool flat_tod)
: cfg_(vconfig::empty_vconfig()), flat_(flat_tod), side_string_(side_string)
{
}
std::set<int> side_filter::get_teams() const
{
//@todo: replace with better implementation
std::set<int> result;
foreach (const team &t, *resources::teams) {
if (match(t)) {
result.insert(t.side());
}
}
return result;
}
static bool check_side_number(const team &t, const std::string &str)
{
std::vector<std::string> list = utils::split(str);
std::string side_number = str_cast(t.side());
if (std::find(list.begin(),list.end(),side_number)==list.end())
{
return false;
}
return true;
}
bool side_filter::match_internal(const team &t) const
{
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;
}
}
//Allow filtering on units
if(cfg_.has_child("has_unit")) {
const vconfig& unit_filter = cfg_.child("has_unit");
bool found = false;
foreach (unit &u, *resources::units) {
if (u.side() != t.side()) {
continue;
}
if (u.matches_filter(unit_filter, u.get_location(), flat_)) {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
bool side_filter::match(int side) const
{
return this->match((*resources::teams)[side-1]);
}
bool side_filter::match(const team& t) const
{
bool matches = match_internal(t);
//handle [and], [or], and [not] with in-order precedence
vconfig::all_children_iterator cond = cfg_.ordered_begin();
vconfig::all_children_iterator cond_end = cfg_.ordered_end();
while (cond != cond_end) {
const std::string& cond_name = cond.get_key();
const vconfig& cond_cfg = cond.get_child();
//handle [and]
if(cond_name == "and")
{
matches = matches && side_filter(cond_cfg, flat_).match(t);
}
//handle [or]
else if(cond_name == "or")
{
matches = matches || side_filter(cond_cfg, flat_).match(t);
}
//handle [not]
else if(cond_name == "not")
{
matches = matches && !side_filter(cond_cfg, flat_).match(t);
}
++cond;
}
return matches;
}