]> git.tdb.fi Git - poefilter.git/blob - source/category.cpp
Correctly merge icon and light beam appearances
[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<MapTierCondition>("map_tier");
78         add_range<SocketsCondition>("sockets");
79         add("or", &Loader::or_);
80         add_range<QualityCondition>("quality");
81         add_range<RarityCondition>("rarity");
82         add_range<WidthCondition>("width");
83
84         add_auxiliary_loader(app_loader);
85 }
86
87 template<typename T>
88 void Category::Loader::add_range(const string &keyword)
89 {
90         add(keyword, &Loader::condition<T>);
91         add(keyword, &Loader::condition_range<T>);
92         add("min_"+keyword, &Loader::condition_min<T>);
93         add("max_"+keyword, &Loader::condition_max<T>);
94 }
95
96 void Category::Loader::add_condition(Condition *cond)
97 {
98         if(compound)
99                 compound->add(cond);
100         else if(!obj.condition)
101                 obj.condition = cond;
102         else
103         {
104                 compound = new AndCondition;
105                 compound->add(obj.condition);
106                 compound->add(cond);
107                 obj.condition = compound;
108         }
109 }
110
111 void Category::Loader::and_()
112 {
113         RefPtr<AndCondition> cond = new AndCondition;
114         Loader sub_ldr(obj, poe, cond.get());
115         load_sub_with(sub_ldr);
116         add_condition(cond.release());
117 }
118
119 void Category::Loader::category(const string &name)
120 {
121         const Category &categ = poe.get_category(name);
122         add_condition(categ.condition->clone());
123 }
124
125 template<typename T>
126 void Category::Loader::condition(typename T::Type value)
127 {
128         add_condition(new T(value));
129 }
130
131 template<typename T>
132 void Category::Loader::condition_max(typename T::Type max)
133 {
134         add_condition(new T(T::Traits::get_min(), max));
135 }
136
137 template<typename T>
138 void Category::Loader::condition_min(typename T::Type min)
139 {
140         add_condition(new T(min, T::Traits::get_max()));
141 }
142
143 template<typename T>
144 void Category::Loader::condition_range(typename T::Type min, typename T::Type max)
145 {
146         add_condition(new T(min, max));
147 }
148
149 void Category::Loader::linked_colors(const LinkedColorsCondition::Colors &colors)
150 {
151         add_condition(new LinkedColorsCondition(colors));
152 }
153
154 void Category::Loader::or_()
155 {
156         RefPtr<OrCondition> cond = new OrCondition;
157         Loader sub_ldr(obj, poe, cond.get());
158         load_sub_with(sub_ldr);
159         add_condition(cond.release());
160 }