]> git.tdb.fi Git - poefilter.git/blob - source/category.cpp
Check for and prune non-viable branches in the condition tree
[poefilter.git] / source / category.cpp
1 #include "category.h"
2 #include "choicecondition.h"
3 #include "filter.h"
4 #include "poefilter.h"
5 #include "rangecondition.h"
6 #include "theme.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 Category::Category():
12         condition(0),
13         order(0)
14 { }
15
16 Category::Category(const string &n, unsigned o):
17         name(n),
18         condition(0),
19         order(o)
20 { }
21
22 Category::Category(const Category &other):
23         name(other.name),
24         condition(other.condition ? other.condition->clone() : 0),
25         order(other.order),
26         appearance(other.appearance)
27 { }
28
29 Category &Category::operator=(const Category &other)
30 {
31         delete condition;
32         name = other.name;
33         condition = (other.condition ? other.condition->clone() : 0);
34         order = other.order;
35         appearance = other.appearance;
36         return *this;
37 }
38
39 Category::~Category()
40 {
41         delete condition;
42 }
43
44 void Category::create_statements(list<FilterStatement> &st) const
45 {
46         if(st.empty())
47                 st.push_back(FilterStatement());
48
49         if(condition)
50         {
51                 Condition *flat = condition->flatten();
52                 if(flat)
53                 {
54                         flat->add_lines(st);
55                         delete flat;
56                 }
57                 // TODO handle the case of the entire condition being non-viable
58         }
59 }
60
61
62 Category::Loader::Loader(Category &c, const PoeFilter &p, CompoundCondition *n):
63         DataFile::ObjectLoader<Category>(c),
64         poe(p),
65         compound(n),
66         app_loader(c.appearance)
67 {
68         add("and", &Loader::and_);
69         add("base_type", &Loader::condition<BaseTypeCondition>);
70         add("category", &Loader::category);
71         add("class", &Loader::condition<ClassCondition>);
72         add_range<DropLevelCondition>("drop_level");
73         add_range<HeightCondition>("height");
74         add_range<ItemLevelCondition>("item_level");
75         add("linked_colors", &Loader::linked_colors);
76         add_range<LinkedSocketsCondition>("linked_sockets");
77         add_range<SocketsCondition>("sockets");
78         add("or", &Loader::or_);
79         add_range<QualityCondition>("quality");
80         add_range<RarityCondition>("rarity");
81         add_range<WidthCondition>("width");
82
83         add_auxiliary_loader(app_loader);
84 }
85
86 template<typename T>
87 void Category::Loader::add_range(const string &keyword)
88 {
89         add(keyword, &Loader::condition<T>);
90         add(keyword, &Loader::condition_range<T>);
91         add("min_"+keyword, &Loader::condition_min<T>);
92         add("max_"+keyword, &Loader::condition_max<T>);
93 }
94
95 void Category::Loader::add_condition(Condition *cond)
96 {
97         if(compound)
98                 compound->add(cond);
99         else if(!obj.condition)
100                 obj.condition = cond;
101         else
102         {
103                 compound = new AndCondition;
104                 compound->add(obj.condition);
105                 compound->add(cond);
106                 obj.condition = compound;
107         }
108 }
109
110 void Category::Loader::and_()
111 {
112         RefPtr<AndCondition> cond = new AndCondition;
113         Loader sub_ldr(obj, poe, cond.get());
114         load_sub_with(sub_ldr);
115         add_condition(cond.release());
116 }
117
118 void Category::Loader::category(const string &name)
119 {
120         const Category &categ = poe.get_category(name);
121         add_condition(categ.condition->clone());
122 }
123
124 template<typename T>
125 void Category::Loader::condition(typename T::Type value)
126 {
127         add_condition(new T(value));
128 }
129
130 template<typename T>
131 void Category::Loader::condition_max(typename T::Type max)
132 {
133         add_condition(new T(T::Traits::get_min(), max));
134 }
135
136 template<typename T>
137 void Category::Loader::condition_min(typename T::Type min)
138 {
139         add_condition(new T(min, T::Traits::get_max()));
140 }
141
142 template<typename T>
143 void Category::Loader::condition_range(typename T::Type min, typename T::Type max)
144 {
145         add_condition(new T(min, max));
146 }
147
148 void Category::Loader::linked_colors(const LinkedColorsCondition::Colors &colors)
149 {
150         add_condition(new LinkedColorsCondition(colors));
151 }
152
153 void Category::Loader::or_()
154 {
155         RefPtr<OrCondition> cond = new OrCondition;
156         Loader sub_ldr(obj, poe, cond.get());
157         load_sub_with(sub_ldr);
158         add_condition(cond.release());
159 }