Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unit_find.h
Go to the documentation of this file.
1 #ifndef _UNIT_FIND_H_
2 #define _UNIT_FIND_H_
3 #include "unit_util.h"
4 template < class Locator >
6  CollideMap::iterator location,
7  Locator *check,
8  QVector thispos,
9  float thisrad,
10  bool acquire_on_location )
11 {
12  CollideMap::iterator cmend = cm->end();
13  CollideMap::iterator cmbegin = cm->begin();
14  if ( cmend != cmbegin && !is_null( location ) ) {
15  CollideMap::iterator tless = location;
16  CollideMap::iterator tmore = location;
17  bool workA = true;
18  bool workB = true;
19  if ( location != cmend && !cm->Iterable( location ) ) {
21  location = cmbegin+br->toflattenhints_offset;
22  if (location == cmend)
23  location--;
24  tmore = location;
25  tless = location;
26  } else if (location != cmend) {
27  ++tmore;
28  } else {
29  //location == cmend
30  --location;
31  if (acquire_on_location)
32  --tless; //allowed because cmend!=cmbegin
33  workB = false;
34  }
35  check->init( cm, location );
36  if (!acquire_on_location) {
37  if (tless != cmbegin)
38  --tless;
39  else
40  workA = false;
41  }
42  while (workA || workB) {
43  if ( workA
44  && !check->cullless( tless ) ) {
45  float rad = (*tless)->radius;
46  if ( rad != 0.0f && ( check->BoltsOrUnits() || ( check->UnitsOnly() == (rad > 0) ) ) ) {
47  float trad =
48  check->NeedDistance() ? ( (*tless)->GetPosition()-thispos ).Magnitude()-fabs( rad )-thisrad : 0;
49  if ( !check->acquire( trad, tless ) )
50  workA = false;
51  }
52  if (tless != cmbegin)
53  tless--;
54  else
55  workA = false;
56  } else {workA = false; } if ( workB
57  && tmore != cmend
58  && !check->cullmore( tmore ) ) {
59  float rad = (*tmore)->radius;
60  if (rad!=0.0f&&(check->BoltsOrUnits()||(check->UnitsOnly()==(rad>0)))) {
61  float trad =
62  check->NeedDistance() ? ( (*tmore)->GetPosition()-thispos ).Magnitude()-fabs( rad )-thisrad : 0;
63  if ( !check->acquire( trad, tmore ) )
64  workB = false;
65  }
66  tmore++;
67  } else {workB = false; }}
68  }
69 }
70 
71 template < class Locator >
72 void findObjects( CollideMap *cm, CollideMap::iterator location, Locator *check )
73 {
74  if ( is_null( location ) )
75  return;
76  QVector thispos = (**location).GetPosition();
77  float thisrad = fabs( (*location)->radius );
78  findObjectsFromPosition( cm, location, check, thispos, thisrad, false );
79 }
80 
82 {
83  CollideMap::iterator location;
84  double startkey;
85  float rad;
86 public:
88  bool BoltsOrUnits()
89  {
90  return false;
91  }
92  bool UnitsOnly()
93  {
94  return true;
95  }
96  bool NeedDistance()
97  {
98  return true;
99  }
101  {
102  retval.unit = NULL;
103  }
105  {
106  this->location = parent;
107  startkey = (*location)->getKey();
108  rad = FLT_MAX;
109  retval.unit = NULL;
110  }
112  {
113  return rad != FLT_MAX && (startkey-rad) > (*tless)->getKey();
114  }
116  {
117  return rad != FLT_MAX && (startkey+rad) < (*tmore)->getKey();
118  }
119  bool acquire( float distance, CollideMap::iterator i )
120  {
121  if (distance < rad) {
122  rad = distance;
123  retval = (*i)->ref;
124  }
125  return true;
126  }
127 };
129 {
130 public:
131  bool UnitsOnly()
132  {
133  return false;
134  }
135 };
137 {
138 public:
139  bool isUnit;
140  bool UnitsOnly()
141  {
142  return false;
143  }
145  {
146  return true;
147  }
148  bool acquire( float distance, CollideMap::iterator i )
149  {
150  Collidable::CollideRef lastret = retval;
151  bool retval = NearestUnitLocator::acquire( distance, i );
152  if ( memcmp( (void*) &retval, (void*) &lastret, sizeof (Collidable::CollideRef) ) )
153  isUnit = (*i)->radius > 0;
154  return retval;
155  }
156 };
158 {
159 public:
161  {
162  return false;
163  }
164  bool UnitsOnly()
165  {
166  return true;
167  }
168  bool acquire( float distance, CollideMap::iterator i )
169  {
170  if ( UnitUtil::isSignificant( (*i)->ref.unit ) )
171  return NearestUnitLocator::acquire( distance, i );
172  return true;
173  }
174 };
176 {
177 public:
179  {
180  return false;
181  }
182  bool UnitsOnly()
183  {
184  return true;
185  }
186  bool acquire( float distance, CollideMap::iterator i )
187  {
188  if ( UnitUtil::isSignificant( (*i)->ref.unit ) || UnitUtil::isCapitalShip( (*i)->ref.unit ) )
189  return NearestUnitLocator::acquire( distance, i );
190  return true;
191  }
192 };
193 template < class T >
195 {
196 public:
198  double startkey;
199  float radius;
202  startkey( 0 )
203  , radius( radius )
204  , maxUnitRadius( maxUnitRadius ) {}
205 
206  bool UnitsOnly()
207  {
208  return true;
209  }
211  {
212  return false;
213  }
215  {
216  return true;
217  }
218 
220  {
221  startkey = (*parent)->getKey();
222  }
224  {
225  double tmp = startkey-radius-maxUnitRadius;
226  return tmp > (*tless)->getKey();
227  }
229  {
230  return startkey+radius+maxUnitRadius < (*tmore)->getKey();
231  }
232 
233  bool acquire( float dist, CollideMap::iterator i )
234  {
235  if (dist < radius)
236  //Inside radius...
237  return action.acquire( (*i)->ref.unit, dist );
238  return true;
239  }
240 };
241 template < class T >
243 {
244 public: UnitWithinRangeOfPosition( float radius, float maxUnitRadius, const Collidable &key_iterator ) :
245  UnitWithinRangeLocator< T > ( radius, maxUnitRadius )
246  {
247  this->startkey = key_iterator.getKey();
248  }
249  void init( CollideMap *cm, CollideMap::iterator parent ) {}
250 };
252 {
253  const void *unit;
254 public:
255  bool retval;
257  {
258  return false;
259  }
260  bool UnitsOnly()
261  {
262  return true;
263  }
265  {
266  return false;
267  }
268  UnitPtrLocator( const void *unit )
269  {
270  retval = false;
271  this->unit = unit;
272  }
274  {
275  return retval;
276  }
278  {
279  return retval;
280  }
281  bool acquire( float distance, CollideMap::iterator i )
282  {
283  return retval = ( ( (const void*) ( (*i)->ref.unit ) ) == unit );
284  }
285  void init( CollideMap *cm, CollideMap::iterator parent ) {}
286 };
287 
288 #endif
289