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
CollideArray Class Reference

#include <collide_map.h>

Inheritance diagram for CollideArray:
CollideMap

Classes

class  CollidableBackref
 

Public Types

typedef Collidableiterator
 
typedef std::vector< CollidableResizableArray
 

Public Member Functions

void SetLocationIndex (unsigned int li)
 
bool Iterable (iterator)
 
void UpdateBoltInfo (iterator iter, Collidable::CollideRef ref)
 
void flatten ()
 
void flatten (CollideArray &example)
 
iterator insert (const Collidable &newKey, iterator hint)
 
iterator insert (const Collidable &newKey)
 
iterator changeKey (iterator iter, const Collidable &newKey)
 
iterator changeKey (iterator iter, const Collidable &newKey, iterator tless, iterator tmore)
 
iterator begin ()
 
iterator end ()
 
iterator lower_bound (const Collidable &)
 
void erase (iterator iter)
 
void checkSet ()
 
 CollideArray (unsigned int location_index)
 

Public Attributes

std::vector< floatmax_radius
 
unsigned int location_index
 
ResizableArray sorted
 
ResizableArray unsorted
 
std::vector< std::list
< CollidableBackref > > 
toflattenhints
 
unsigned int count
 

Static Public Attributes

static float max_bolt_radius = 0
 

Detailed Description

Definition at line 86 of file collide_map.h.

Member Typedef Documentation

Definition at line 110 of file collide_map.h.

typedef std::vector< Collidable > CollideArray::ResizableArray

Definition at line 112 of file collide_map.h.

Constructor & Destructor Documentation

CollideArray::CollideArray ( unsigned int  location_index)
inline

Definition at line 135 of file collide_map.h.

References location_index.

135  : toflattenhints( 1 )
136  , count( 0 )
137  {
139  }

Member Function Documentation

CollideArray::iterator CollideArray::changeKey ( CollideArray::iterator  iter,
const Collidable newKey 
)

Definition at line 44 of file collide_map.cpp.

References begin(), end(), and unsorted.

Referenced by changeKey(), and Bolt::Update().

45 {
46  if ( iter >= this->begin() && iter < this->end() ) {
47  iterator tmp = &*( this->unsorted.begin()+( iter-this->begin() ) );
48  *tmp = newKey;
49  } else {
50  *iter = newKey;
51  }
52  return iter;
53 }
CollideArray::iterator CollideArray::changeKey ( CollideArray::iterator  iter,
const Collidable newKey,
CollideArray::iterator  tless,
CollideArray::iterator  tmore 
)

Definition at line 55 of file collide_map.cpp.

References changeKey().

59 {
60  return this->changeKey( iter, newKey );
61 }
void CollideArray::checkSet ( )

Definition at line 240 of file collide_map.cpp.

References begin(), and end().

Referenced by Unit::RemoveFromSystem().

241 {
242  if ( this->begin() != this->end() )
243  for (iterator newiter = this->begin(), iter = newiter++; newiter != this->end(); iter = newiter++)
244  assert( *iter < *newiter );
245 }
void CollideArray::erase ( iterator  iter)

Definition at line 8 of file collide_map.cpp.

References begin(), count, end(), i, Collidable::radius, Collidable::ref, toflattenhints, CollideArray::CollidableBackref::toflattenhints_offset, Collidable::CollideRef::unit, and unsorted.

Referenced by BoltDestroyGeneric(), Unit::RemoveFromSystem(), and StarSystem::RemoveUnit().

9 {
10  count -= 1;
11  if ( target >= this->begin() && target < this->end() ) {
12  target->radius = 0;
13  target->ref.unit = NULL;
14  size_t diff = ( target-this->begin() );
15  if (this->unsorted.size() > diff) {
16  //for secondary collide arrays that have no unsorted array
17  iterator tmp = &*(this->unsorted.begin()+diff);
18  tmp->radius = 0;
19  tmp->ref.unit = NULL;
20  }
21  return;
22  } else if (target == NULL) {
23  return;
24  } else {
25  CollidableBackref *targ = static_cast< CollidableBackref* > (&*target);
26  std::list< CollidableBackref > *targlist = &toflattenhints[targ->toflattenhints_offset];
27  std::list< CollidableBackref >::iterator endlist = targlist->end();
28  for (std::list< CollidableBackref >::iterator i = targlist->begin(); i != endlist; ++i)
29  if (&*i == target) {
30  targlist->erase( i );
31  return;
32  }
33  }
34 }
void CollideArray::flatten ( )

Definition at line 121 of file collide_map.cpp.

References begin(), count, f, vsalg::for_each(), i, index, j, location_index, max_radius, size, sorted, toflattenhints, Unit::UNIT_BOLT, Unit::UNIT_ONLY, and unsorted.

Referenced by flatten(), and StarSystem::UpdateUnitPhysics().

122 {
123  sorted.resize( count );
124  max_radius.resize( count );
125  size_t len = unsorted.size();
126  size_t index = count;
127  RadiusUpdate< -1, true >collideUpdate( this );
128  for (ptrdiff_t i = len; i >= 0; i--) {
129  Collidable *tmp;
130  if (i < static_cast<ptrdiff_t>(len) && (tmp = &unsorted[i])->radius != 0.0f) {
131  sorted[--index] = *tmp;
132  collideUpdate( *tmp, index );
133  }
134 
135  std::list< CollidableBackref >::iterator listend = toflattenhints[i].end();
136  for (std::list< CollidableBackref >::iterator j = toflattenhints[i].begin();
137  j != listend;
138  ++j)
139  if (j->radius != 0) {
140  sorted[--index] = *j;
141  collideUpdate( *j, index );
142  }
143  toflattenhints[i].resize( 0 );
144  }
145 
146  std::sort( sorted.begin(), sorted.end() );
147  unsorted = sorted;
148 
149  toflattenhints.resize( count+1 );
151  size_t i = 0;
152  size_t size = sorted.size();
153  ResizableArray::iterator iter = sorted.begin();
155  RadiusUpdate< 1, false > radUpdate( this );
156  for (i = 0; i != size; ++i, ++iter) {
157  update( *iter );
158  radUpdate( *iter, i );
159  }
160  } else if (location_index == Unit::UNIT_ONLY) {
162  } else {
163  assert( 0 && "Only Support arrays of units_only and mixed units bolts" ); //right now only support 2 array types;
164  }
165 }
void CollideArray::flatten ( CollideArray example)

Definition at line 197 of file collide_map.cpp.

References count, flatten(), vsalg::for_each(), location_index, sorted, toflattenhints, and Unit::UNIT_ONLY.

198 {
200  sorted.resize( count );
201  for_each( toflattenhints.begin(), toflattenhints.end(), resizezero() );
202  toflattenhints.resize( count+1 );
203 
204  for_each( sorted.begin(), sorted.end(), CopyExample( hint.sorted.begin(), hint.sorted.end() ) );
205  } else {
206  printf( "Trying to use flatten hint on a array with both bolts and units\n" );
207  flatten();
208  }
209 }
CollideArray::iterator CollideArray::insert ( const Collidable newKey,
iterator  hint 
)

Definition at line 211 of file collide_map.cpp.

References begin(), count, end(), max_bolt_radius, Collidable::radius, SIMULATION_ATOM, sorted, toflattenhints, and unsorted.

Referenced by Bolt::Bolt(), Unit::CollideAll(), insert(), and Unit::UpdateCollideQueue().

212 {
213  if (newKey.radius < -max_bolt_radius*SIMULATION_ATOM)
215  if ( this->begin() == this->end() ) {
216  count += 1;
217  this->unsorted.push_back( newKey );
218  this->toflattenhints.resize( 2 );
219  this->sorted.push_back( newKey );
220  return &sorted.back();
221  } else if ( hint >= this->begin() && hint <= this->end() ) {
222  count += 1;
223  size_t len = hint-this->begin();
224  std::list< CollidableBackref > *hintlist = &toflattenhints[len];
225  return &*hintlist->insert( hintlist->end(), CollidableBackref( newKey, len ) );
226  } else {
227  return this->insert( newKey ); //don't use hint;
228  }
229 }
CollideArray::iterator CollideArray::insert ( const Collidable newKey)

Definition at line 236 of file collide_map.cpp.

References insert(), and lower_bound().

237 {
238  return this->insert( newKey, this->lower_bound( newKey ) );
239 }
bool CollideArray::Iterable ( CollideArray::iterator  a)

Definition at line 255 of file collide_map.cpp.

References begin(), and end().

Referenced by CollideChecker< T, canbebolt >::CheckCollisions(), and findObjectsFromPosition().

256 {
257  return a >= begin() && a < end();
258 }
CollideArray::iterator CollideArray::lower_bound ( const Collidable newKey)

Definition at line 231 of file collide_map.cpp.

References begin(), and end().

Referenced by GameStarSystem::Draw(), CommunicatingAI::GetRandomUnit(), and insert().

232 {
233  return ::std::lower_bound( this->begin(), this->end(), newKey );
234 }
void CollideArray::SetLocationIndex ( unsigned int  li)
inline

Definition at line 106 of file collide_map.h.

References location_index.

107  {
108  location_index = li;
109  }
void CollideArray::UpdateBoltInfo ( CollideArray::iterator  iter,
Collidable::CollideRef  ref 
)

Definition at line 35 of file collide_map.cpp.

References begin(), end(), is_null(), and unsorted.

Referenced by BoltDestroyGeneric().

36 {
37  if ( iter >= this->begin() && iter < this->end() ) {
38  ( *( unsorted.begin()+( iter-this->begin() ) ) ).ref = ref; //update both unsorted and sorted
39  (*iter).ref = ref;
40  } else if ( !is_null( iter ) ) {
41  (*iter).ref = ref;
42  }
43 }

Member Data Documentation

unsigned int CollideArray::count

Definition at line 116 of file collide_map.h.

Referenced by erase(), flatten(), and insert().

unsigned int CollideArray::location_index

Definition at line 92 of file collide_map.h.

Referenced by CollideArray(), flatten(), and SetLocationIndex().

float CollideArray::max_bolt_radius = 0
static

Definition at line 89 of file collide_map.h.

Referenced by insert(), and RadiusUpdate< direction, always_replace >::operator()().

std::vector< float > CollideArray::max_radius
ResizableArray CollideArray::sorted
std::vector< std::list< CollidableBackref > > CollideArray::toflattenhints

Definition at line 115 of file collide_map.h.

Referenced by erase(), flatten(), insert(), and Bolt::UpdatePhysics().

ResizableArray CollideArray::unsorted

Definition at line 114 of file collide_map.h.

Referenced by changeKey(), erase(), flatten(), insert(), and UpdateBoltInfo().


The documentation for this class was generated from the following files: