Add infos into target window
[ryzomcore.git] / ryzom / server / src / frontend_service / pair_selector.h
blobfa21c0683c2350b8c7743627757cde6cbffa9e8e
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef NL_PAIR_SELECTOR_H
20 #define NL_PAIR_SELECTOR_H
22 #include "nel/misc/types_nl.h"
23 #include "game_share/entity_types.h"
24 #include "fe_types.h"
25 #include "selection_generator.h"
26 #include <vector>
27 #include <functional>
31 * Forward declarations
33 class CVisionArray;
37 * Items of TPairVector
39 class TPairCE
41 public:
42 // Constructor
43 TPairCE( TClientId c, CLFECOMMON::TCLEntityId e ) : ClientId(c), CeId(e) {}
45 // Copy constructor
46 TPairCE( const TPairCE& src )
48 operator=( src );
51 // Assignment operator
52 TPairCE& operator=( const TPairCE& src )
54 // no need to check ( &src != this )
55 ClientId = src.ClientId;
56 CeId = src.CeId;
57 return *this;
60 // Comparison operator ==
61 friend bool operator==( const TPairCE& e1, const TPairCE& e2 )
63 return (e1.ClientId == e2.ClientId) && (e1.CeId == e2.CeId );
66 // Classical comparison operator < (not needed: we compare by distance instead)
67 /*friend bool operator<( const TPairCE& e1, const TPairCE& e2 )
69 if (e1.ClientId < e2.ClientId)
70 return true;
71 else if ( e1.ClientId == e2.ClientId )
72 return (e1.CeId < e2.CeId );
73 else
74 return false;
75 }*/
77 TClientId ClientId;
78 CLFECOMMON::TCLEntityId CeId;
83 * Container for pairs sorted by distance
85 typedef std::vector<TPairCE> TPairVector;
89 * Comparison function object for TPairCE
91 struct TComparePairCE : public std::binary_function < const TPairCE&, const TPairCE&, bool >
93 /// Constructor
94 TComparePairCE( CVisionArray *visionarray ) :
95 _VisionArray(visionarray)
98 /// Comparison operator
99 bool operator()( const TPairCE& e1, const TPairCE& e2 );
101 private:
103 /// Access to vision array to know distances
104 CVisionArray *_VisionArray;
109 * Type of index to pair
111 typedef sint32 TPairIndex;
115 * TPairRange: ranges in PairsByDistance, used by pseudosort algorithm
117 struct TPairRange
119 uint IndexInRanges;
120 TPairIndex NumberOfElements;
121 CLFECOMMON::TCoord DistThreshold;
123 // The "begin index" of the range depends on the previous ranges
124 // (prevents from updating following ranges every time an index boundary changes)
125 TPairIndex beginIndex( const std::vector<TPairRange>& vect ) const
127 uint i;
128 TPairIndex sum = 0;
129 for ( i=0; i!=IndexInRanges; ++i )
131 sum += vect[i].NumberOfElements;
133 return sum;
139 * Vector of ranges
141 typedef std::vector<TPairRange> TRanges;
146 * Pair Selector.
147 * It selects the pairs <Client,Entity> for which the priorities of the properties
148 * will be calculated. The pairs are stored in a vector and this one is sorted or
149 * pseudosorted (depending on its size) by distance.
151 * \author Olivier Cado
152 * \author Nevrax France
153 * \date 2002
155 class CPairSelector
157 public:
159 /// Type of selection strategy
160 enum TSelectionStrategy { Power2WithCeiling = 0, Scoring = 1 };
162 /// Constructor
163 CPairSelector();
165 /// Initialization
166 void init( CVisionArray *va );
168 // _______________________
169 // For the vision provider
171 /// Add to the container
172 void addPair( TClientId clientid, CLFECOMMON::TCLEntityId ceid, CLFECOMMON::TCoord distance );
174 /// Remove from the container
175 void removePair( TClientId clientid, CLFECOMMON::TCLEntityId ceid );
177 /// Remove from the container all pairs with the specified clientid
178 void removeAllPairs( TClientId clientid );
180 // _______________________________
181 // For the priority subsystem root
183 /// Return true if there is no pair
184 bool empty() { return _PairsByDistance.empty(); }
186 /// Sort or pseudosort the pairs, and initialize selection cycle. Must be called before a series of selectNextPair().
187 void sortPairs();
189 /// Set the selection strategy
190 void setSelectionStrategy( TSelectionStrategy strat );
192 // ___________________
193 // For the prioritizer
195 /// Select the next pair to prioritize, or return NULL if there is no more pairs for this cycle
196 const TPairCE *selectNextPair();
198 // ______________
199 // For statistics
201 /// Return the number of pairs in the container
202 TPairIndex nbPairs() const { return _PairsByDistance.size(); }
204 /// Print the scores if there is a scoring selection generator
205 void printScores() const;
207 /// Display the ranges (debugging)
208 void printRanges( bool checkIntegrity ) const;
210 /// Display the pairs (debugging)
211 void printPairs() const;
213 /// Pseudosort or sort (read)
214 bool PseudoSort;
216 /// Selection strategy (read)
217 TSelectionStrategy SelStrategy;
219 /// Average number of pairs per range (read/write)
220 TPairIndex NbPairsPerRange;
222 /// Number of pairs to select per cycle (read)
223 TPairIndex NbPairsToSelect;
225 /// Min number of pairs to select per cycle (read/write)
226 TPairIndex MinNbPairsToSelect;
228 protected:
230 /// Easy access to the last pair
231 TPairCE& lastPair() { nlassert( ! _PairsByDistance.empty() ); return _PairsByDistance.back(); }
233 /// Return the number of ranges for pseudosort
234 uint32 nbRanges() const { return _Ranges.size(); }
236 /// Easy access to the last range for pseudosort
237 TPairRange& lastRange() { nlassert( ! _Ranges.empty() ); return _Ranges.back(); }
239 /// Easy access to the last range for pseudosort (const version)
240 const TPairRange& lastRange() const { nlassert( ! _Ranges.empty() ); return _Ranges.back(); }
242 // Update ranges after deleting a pair
243 void adjustRangesAfterDeleting();
245 /// Add a range for pseudosort, and resample the distance thresholds.
246 void addRange();
248 /// Remove the last range (for pseudosort) when it's empty, and resample the distance thresholds.
249 void removeLastRange();
251 /// Setup selection generator, using SelStrategy (the argument is meaningful only in strategy P2C)
252 void setupSelectionGenerator( ISelectionGenerator::TSelectionLevel ceiling );
254 /// Select next level. Call selectNextPair() after selectNextLevel().
255 void selectNextLevel();
257 /// Calculate NbPairsToSelect taking into account the user loop time
258 void calcNbPairsToSelect();
260 /// Return the number of levels corresponding to the current size of _PairsByDistance
261 uint getNbLevels() { return (_PairsByDistance.size() / NbPairsToSelect) + 1; }
263 private:
265 /// Pairs sorted by distance
266 TPairVector _PairsByDistance;
268 /// Ranges used by pseudosort algorithm
269 TRanges _Ranges;
271 /// Index of the current range for pseudosorting
272 uint32 _CurrentRangeIndex;
274 /// Selection generator (either CP2CGenerator or CScoringGenerator)
275 ISelectionGenerator *_SelGenerator;
277 /// Beginning index of the current level given by the selection generator
278 TPairIndex _BeginIndex;
280 /// Index of currently selected pair
281 TPairIndex _NextPairIndex;
283 /// Difference between previous and current NbPairsToSelect;
284 sint32 _NPSDelta;
286 /// Vision Array
287 CVisionArray *_VisionArray;
291 #endif // NL_PAIR_SELECTOR_H
293 /* End of pair_selector.h */