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
collide_map.h
Go to the documentation of this file.
1 #ifndef _COLLIDE_MAP_H_
2 #define _COLLIDE_MAP_H_
3 #include "key_mutable_set.h"
4 #include "vegastrike.h"
5 #include "gfx/vec.h"
6 #if defined (_WIN32) || __GNUC__ != 2
7 #include <limits>
8 #endif
9 #include <vector>
10 /* Arbitrarily use Set for ALL PLATFORMS -hellcatv */
11 class Unit;
12 class Bolt;
14 {
15 public:
17  float radius; //radius == 0: to-be-deleted, radius <0 bolt (radius == speed in phys frame), radius >0 unit
18 
19  union CollideRef
20  {
22  unsigned int bolt_index;
23  }
24  ref;
26  {
27  return position;
28  }
29  void SetPosition( const QVector &bpos )
30  {
31  //in case we want to drop in an xtra radius parameter when we get performance testing
32  this->position = bpos;
33 #ifdef __APPLE__
34  if ( !FINITE( getKey() ) )
35  position = QVector( 0, 0, 0 ); //hack for now
36 
37 #else
38  if ( ISNAN( getKey() ) )
39  position = QVector( 0, 0, 0 ); //hack for now
40 #endif
41  }
43  {
44  return *this;
45  }
47  {
48  return this;
49  }
50 
51  double getKey() const
52  {
53  return position.i;
54  }
55  bool operator<( const Collidable &other ) const
56  {
57  return getKey() < other.getKey();
58  }
59  Collidable& get()
60  {
61  return *this;
62  }
64 #if defined (_WIN32) || __GNUC__ != 2
65  std::numeric_limits< float >::quiet_NaN()
66 #else
67  1.0f/1024.0f/1024.0f/1024.0f
68 #endif
69  ) {}
70  Collidable( Unit *un );
71  Collidable( unsigned int bolt_index, float speed, const QVector &p )
72  {
73  ref.bolt_index = bolt_index;
74  radius = -speed*SIMULATION_ATOM;
75  if (
76 #ifdef __APPLE__
77  !FINITE( radius )
78 #else
79  ISNAN( radius )
80 #endif
81  || radius >= -FLT_MIN) radius = -FLT_MIN*2;
82  this->SetPosition( p );
83  }
84 };
85 
87 {
88 public:
89  static float max_bolt_radius;
90 
91  std::vector< float >max_radius;
92  unsigned int location_index; //either UNIT_ONLY or UNIT_BOLT
94  {
95 public:
96 
99  CollidableBackref( Unit *un ) : Collidable( un ) {}
100  CollidableBackref( unsigned int bolt_index, float speed, const QVector &p ) : Collidable( bolt_index, speed, p ) {}
101  CollidableBackref( const Collidable &b, size_t offset ) : Collidable( b )
102  {
103  toflattenhints_offset = offset;
104  }
105  };
106  void SetLocationIndex( unsigned int li )
107  {
108  location_index = li;
109  }
111  bool Iterable( iterator );
112  typedef std::vector< Collidable >ResizableArray;
115  std::vector< std::list< CollidableBackref > >toflattenhints;
116  unsigned int count;
118  void flatten();
119  void flatten( CollideArray &example ); //maybe it has some xtra bolts
120  iterator insert( const Collidable &newKey, iterator hint );
121  iterator insert( const Collidable &newKey );
122  iterator changeKey( iterator iter, const Collidable &newKey );
123  iterator changeKey( iterator iter, const Collidable &newKey, iterator tless, iterator tmore );
125  {
126  return sorted.size() != 0 ? &*sorted.begin() : NULL;
127  }
129  {
130  return this->begin()+sorted.size();
131  }
132  iterator lower_bound( const Collidable& );
133  void erase( iterator iter );
134  void checkSet();
136  , count( 0 )
137  {
138  this->location_index = location_index;
139  }
140 };
141 
142 #ifdef VS_ENABLE_COLLIDE_KEY
143 class CollideMap : public KeyMutableSet< Collidable >
144 {
145 #else
146 #ifdef VS_ENABLE_COLLIDE_LIST
147 class CollideMap : public ListMutableSet< Collidable >
148 {
149 #else
150 class CollideMap : public CollideArray
151 {
152 #endif
153 #endif
154 public: CollideMap( unsigned int location_offset ) : CollideArray( location_offset ) {}
155 
156 //Check collisions takes an item to check collisions with, and returns whether that item collided with a Unit only
157  bool CheckCollisions( Bolt *bol, const Collidable &updated );
158  bool CheckUnitCollisions( Bolt *bol, const Collidable &updated ); //DANGER must be used on lists that are only populated with Units, not bolts
159  bool CheckCollisions( Unit *un, const Collidable &updated ); //will be handed off to a templated function
160  bool CheckUnitCollisions( Unit *un, const Collidable &updated ); //DANGER must be used on lists that are only populated with Units, not bolts
161 };
162 
163 #if defined (VS_ENABLE_COLLIDE_LIST) || defined (VS_ENABLE_COLLIDE_KEY)
164 extern CollideMap null_collide_map;
165 extern CollideMap::iterator null_collide_iter;
166 extern bool null_collide_iter_initialized;
167 
168 inline void init_null_collide_iter()
169 {
170  if (!null_collide_iter_initialized) {
171  null_collide_map.insert( Collidable() );
172  null_collide_iter = null_collide_map.begin();
173  null_collide_iter_initialized = true;
174  }
175 }
176 
177 inline bool is_null( const CollideMap::iterator &it )
178 {
179  return ISNAN( (**it).radius );
180 }
181 
182 inline void set_null( CollideMap::iterator &it )
183 {
185  it = null_collide_iter;
186 }
187 #else
188 inline void init_null_collide_iter() {}
189 inline bool is_null( const CollideMap::iterator &it )
190 {
191  return it == NULL;
192 }
193 inline void set_null( CollideMap::iterator &it )
194 {
195  it = NULL;
196 }
197 
198 #endif
199 #endif
200