1 #ifndef RANGECONDITION_H_
2 #define RANGECONDITION_H_
8 class RangeCondition: public Condition
12 typedef typename Traits::Type Type;
20 RangeCondition(Type, Type);
22 virtual RangeCondition<T> *clone() const;
23 virtual bool can_merge(const Condition &, const CompoundCondition &) const;
24 virtual RangeCondition<T> *merge(const std::vector<const Condition *> &, const CompoundCondition &) const;
25 virtual bool is_viable() const { return min<=max; }
26 virtual void add_lines(std::list<FilterStatement> &) const;
33 static Rarity get_min() { return NORMAL; }
34 static Rarity get_max() { return UNIQUE; }
35 static const char *get_keyword() { return "Rarity"; }
38 typedef RangeCondition<RarityTraits> RarityCondition;
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"; }
49 typedef RangeCondition<QualityTraits> QualityCondition;
52 struct ItemLevelTraits
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"; }
60 typedef RangeCondition<ItemLevelTraits> ItemLevelCondition;
63 struct DropLevelTraits
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"; }
71 typedef RangeCondition<DropLevelTraits> DropLevelCondition;
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"; }
82 typedef RangeCondition<WidthTraits> WidthCondition;
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"; }
93 typedef RangeCondition<HeightTraits> HeightCondition;
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"; }
104 typedef RangeCondition<SocketsTraits> SocketsCondition;
107 struct LinkedSocketsTraits
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"; }
115 typedef RangeCondition<LinkedSocketsTraits> LinkedSocketsCondition;
119 RangeCondition<T>::RangeCondition(Type v):
125 RangeCondition<T>::RangeCondition(Type n, Type x):
131 RangeCondition<T> *RangeCondition<T>::clone() const
133 return new RangeCondition<T>(min, max);
137 bool RangeCondition<T>::can_merge(const Condition &other, const CompoundCondition &parent) const
139 const RangeCondition<T> *other_range = dynamic_cast<const RangeCondition<T> *>(&other);
143 if(dynamic_cast<const AndCondition *>(&parent))
145 else if(dynamic_cast<const OrCondition *>(&parent))
146 return min<=other_range->max+1 && max+1>=other_range->min;
152 RangeCondition<T> *RangeCondition<T>::merge(const std::vector<const Condition *> &conditions, const CompoundCondition &parent) const
154 if(conditions.empty())
157 bool intersect = dynamic_cast<const AndCondition *>(&parent);
158 RangeCondition<T> *result;
160 result = new RangeCondition<T>(Traits::get_min(), Traits::get_max());
162 result = new RangeCondition<T>(Traits::get_max(), Traits::get_min());
163 for(std::vector<const Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
165 const RangeCondition<T> *c = static_cast<const RangeCondition<T> *>(*i);
168 result->min = std::max(result->min, c->min);
169 result->max = std::min(result->max, c->max);
173 result->min = std::min(result->min, c->min);
174 result->max = std::max(result->max, c->max);
181 void RangeCondition<T>::add_lines(std::list<FilterStatement> &st) const
183 const char *keyword = Traits::get_keyword();
185 FilterStatement::add_line(st, Msp::format("%s %s", keyword, min));
188 if(min!=Traits::get_min())
189 FilterStatement::add_line(st, Msp::format("%s >= %s", keyword, min));
190 if(max!=Traits::get_max())
191 FilterStatement::add_line(st, Msp::format("%s <= %s", keyword, max));