]> git.tdb.fi Git - poefilter.git/blob - source/rangecondition.h
8355135fc2341c7f580e91f4b29ce3fbf422c4ea
[poefilter.git] / source / rangecondition.h
1 #ifndef RANGECONDITION_H_
2 #define RANGECONDITION_H_
3
4 #include "condition.h"
5 #include "filter.h"
6
7 template<typename T>
8 class RangeCondition: public Condition
9 {
10 public:
11         typedef T Traits;
12         typedef typename Traits::Type Type;
13
14 protected:
15         Type min;
16         Type max;
17
18 public:
19         RangeCondition(Type);
20         RangeCondition(Type, Type);
21
22         virtual RangeCondition<T> *clone() const;
23         virtual bool can_merge(const Condition &, const CompoundCondition &) const;
24         virtual RangeCondition<T> *merge(const std::vector<Condition *> &, const CompoundCondition &) const;
25         virtual void add_lines(std::list<FilterStatement> &) const;
26 };
27
28
29 struct RarityTraits
30 {
31         typedef Rarity Type;
32         static Rarity get_min() { return NORMAL; }
33         static Rarity get_max() { return UNIQUE; }
34         static const char *get_keyword() { return "Rarity"; }
35 };
36
37 typedef RangeCondition<RarityTraits> RarityCondition;
38
39
40 struct QualityTraits
41 {
42         typedef unsigned Type;
43         static unsigned get_min() { return 0; }
44         static unsigned get_max() { return 20; }
45         static const char *get_keyword() { return "Quality"; }
46 };
47
48 typedef RangeCondition<QualityTraits> QualityCondition;
49
50
51 struct ItemLevelTraits
52 {
53         typedef unsigned Type;
54         static unsigned get_min() { return 0; }
55         static unsigned get_max() { return 100; }
56         static const char *get_keyword() { return "ItemLevel"; }
57 };
58
59 typedef RangeCondition<ItemLevelTraits> ItemLevelCondition;
60
61
62 struct DropLevelTraits
63 {
64         typedef unsigned Type;
65         static unsigned get_min() { return 0; }
66         static unsigned get_max() { return 100; }
67         static const char *get_keyword() { return "DropLevel"; }
68 };
69
70 typedef RangeCondition<DropLevelTraits> DropLevelCondition;
71
72
73 struct WidthTraits
74 {
75         typedef unsigned Type;
76         static unsigned get_min() { return 1; }
77         static unsigned get_max() { return 2; }
78         static const char *get_keyword() { return "Width"; }
79 };
80
81 typedef RangeCondition<WidthTraits> WidthCondition;
82
83
84 struct HeightTraits
85 {
86         typedef unsigned Type;
87         static unsigned get_min() { return 1; }
88         static unsigned get_max() { return 4; }
89         static const char *get_keyword() { return "Height"; }
90 };
91
92 typedef RangeCondition<HeightTraits> HeightCondition;
93
94
95 struct SocketsTraits
96 {
97         typedef unsigned Type;
98         static unsigned get_min() { return 1; }
99         static unsigned get_max() { return 6; }
100         static const char *get_keyword() { return "Sockets"; }
101 };
102
103 typedef RangeCondition<SocketsTraits> SocketsCondition;
104
105
106 struct LinkedSocketsTraits
107 {
108         typedef unsigned Type;
109         static unsigned get_min() { return 1; }
110         static unsigned get_max() { return 6; }
111         static const char *get_keyword() { return "LinkedSockets"; }
112 };
113
114 typedef RangeCondition<LinkedSocketsTraits> LinkedSocketsCondition;
115
116
117 template<typename T>
118 RangeCondition<T>::RangeCondition(Type v):
119         min(v),
120         max(v)
121 { }
122
123 template<typename T>
124 RangeCondition<T>::RangeCondition(Type n, Type x):
125         min(n),
126         max(x)
127 { }
128
129 template<typename T>
130 RangeCondition<T> *RangeCondition<T>::clone() const
131 {
132         return new RangeCondition<T>(min, max);
133 }
134
135 template<typename T>
136 bool RangeCondition<T>::can_merge(const Condition &other, const CompoundCondition &parent) const
137 {
138         return dynamic_cast<const RangeCondition<T> *>(&other) && dynamic_cast<const AndCondition *>(&parent);
139 }
140
141 template<typename T>
142 RangeCondition<T> *RangeCondition<T>::merge(const std::vector<Condition *> &conditions, const CompoundCondition &parent) const
143 {
144         if(dynamic_cast<const AndCondition *>(&parent) && !conditions.empty())
145         {
146                 RangeCondition<T> *result = new RangeCondition<T>(Traits::get_min(), Traits::get_max());
147                 for(std::vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
148                 {
149                         const RangeCondition<T> *c = static_cast<RangeCondition<T> *>(*i);
150                         result->min = std::max(result->min, c->min);
151                         result->max = std::min(result->max, c->max);
152                 }
153                 return result;
154         }
155         else
156                 return 0;
157 }
158
159 template<typename T>
160 void RangeCondition<T>::add_lines(std::list<FilterStatement> &st) const
161 {
162         const char *keyword = Traits::get_keyword();
163         if(min==max)
164                 FilterStatement::add_line(st, Msp::format("%s %s", keyword, min));
165         else
166         {
167                 if(min!=Traits::get_min())
168                         FilterStatement::add_line(st, Msp::format("%s >= %s", keyword, min));
169                 if(max!=Traits::get_max())
170                         FilterStatement::add_line(st, Msp::format("%s <= %s", keyword, max));
171         }
172 }
173
174 #endif