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