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 equals(const Condition &) const;
24 virtual bool can_merge(const Condition &, const CompoundCondition &) const;
25 virtual RangeCondition<T> *merge(const std::vector<const Condition *> &, const CompoundCondition &) const;
26 virtual bool is_viable() const { return min<=max; }
27 virtual void add_lines(std::list<FilterStatement> &) const;
34 static Rarity get_min() { return NORMAL; }
35 static Rarity get_max() { return UNIQUE; }
36 static const char *get_keyword() { return "Rarity"; }
39 typedef RangeCondition<RarityTraits> RarityCondition;
44 typedef unsigned Type;
45 static unsigned get_min() { return 0; }
46 static unsigned get_max() { return 20; }
47 static const char *get_keyword() { return "Quality"; }
50 typedef RangeCondition<QualityTraits> QualityCondition;
53 struct ItemLevelTraits
55 typedef unsigned Type;
56 static unsigned get_min() { return 1; }
57 static unsigned get_max() { return 100; }
58 static const char *get_keyword() { return "ItemLevel"; }
61 typedef RangeCondition<ItemLevelTraits> ItemLevelCondition;
64 struct DropLevelTraits
66 typedef unsigned Type;
67 static unsigned get_min() { return 1; }
68 static unsigned get_max() { return 100; }
69 static const char *get_keyword() { return "DropLevel"; }
72 typedef RangeCondition<DropLevelTraits> DropLevelCondition;
77 typedef unsigned Type;
78 static unsigned get_min() { return 1; }
79 static unsigned get_max() { return 17; }
80 static const char *get_keyword() { return "MapTier"; }
83 typedef RangeCondition<MapTierTraits> MapTierCondition;
88 typedef unsigned Type;
89 static unsigned get_min() { return 1; }
90 static unsigned get_max() { return 2; }
91 static const char *get_keyword() { return "Width"; }
94 typedef RangeCondition<WidthTraits> WidthCondition;
99 typedef unsigned Type;
100 static unsigned get_min() { return 1; }
101 static unsigned get_max() { return 4; }
102 static const char *get_keyword() { return "Height"; }
105 typedef RangeCondition<HeightTraits> HeightCondition;
110 typedef unsigned Type;
111 static unsigned get_min() { return 1; }
112 static unsigned get_max() { return 6; }
113 static const char *get_keyword() { return "Sockets"; }
116 typedef RangeCondition<SocketsTraits> SocketsCondition;
119 struct LinkedSocketsTraits
121 typedef unsigned Type;
122 static unsigned get_min() { return 1; }
123 static unsigned get_max() { return 6; }
124 static const char *get_keyword() { return "LinkedSockets"; }
127 typedef RangeCondition<LinkedSocketsTraits> LinkedSocketsCondition;
131 RangeCondition<T>::RangeCondition(Type v):
137 RangeCondition<T>::RangeCondition(Type n, Type x):
143 RangeCondition<T> *RangeCondition<T>::clone() const
145 return new RangeCondition<T>(min, max);
149 bool RangeCondition<T>::equals(const Condition &other) const
151 const RangeCondition<T> *other_range = dynamic_cast<const RangeCondition<T> *>(&other);
155 return (min==other_range->min && max==other_range->max);
159 bool RangeCondition<T>::can_merge(const Condition &other, const CompoundCondition &parent) const
161 const RangeCondition<T> *other_range = dynamic_cast<const RangeCondition<T> *>(&other);
165 if(dynamic_cast<const AndCondition *>(&parent))
167 else if(dynamic_cast<const OrCondition *>(&parent))
168 return min<=other_range->max+1 && max+1>=other_range->min;
174 RangeCondition<T> *RangeCondition<T>::merge(const std::vector<const Condition *> &conditions, const CompoundCondition &parent) const
176 if(conditions.empty())
179 bool intersect = dynamic_cast<const AndCondition *>(&parent);
180 RangeCondition<T> *result;
182 result = new RangeCondition<T>(Traits::get_min(), Traits::get_max());
184 result = new RangeCondition<T>(Traits::get_max(), Traits::get_min());
185 for(std::vector<const Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
187 const RangeCondition<T> *c = static_cast<const RangeCondition<T> *>(*i);
190 result->min = std::max(result->min, c->min);
191 result->max = std::min(result->max, c->max);
195 result->min = std::min(result->min, c->min);
196 result->max = std::max(result->max, c->max);
203 void RangeCondition<T>::add_lines(std::list<FilterStatement> &st) const
205 const char *keyword = Traits::get_keyword();
207 FilterStatement::add_line(st, Msp::format("%s %s", keyword, min));
210 if(min!=Traits::get_min())
211 FilterStatement::add_line(st, Msp::format("%s >= %s", keyword, min));
212 if(max!=Traits::get_max())
213 FilterStatement::add_line(st, Msp::format("%s <= %s", keyword, max));