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
UnitHash3d< CTSIZ, CTACCURACY, CTHUGE > Class Template Reference

#include <unit_collide.h>

Public Member Functions

 UnitHash3d (StarSystem *ss)
 
void SwapHugeAccum ()
 
void AddHugeToActive (Unit *un)
 
void Clear ()
 clears entire table More...
 
int Get (const QVector &Exact, UnitCollection *retval[], bool GetHuge)
 returns any objects residing in the sector occupied by Exact More...
 
UnitCollectionGetHuge ()
 Returns all objects too big to be conveniently fit in the array. More...
 
int Get (const LineCollide *target, UnitCollection *retval[], bool GetHuge)
 Returns all objects within sector(s) occupied by target. More...
 
void Put (LineCollide *target, Unit *objectToPut)
 Adds objectToPut into collide table with limits specified by target. More...
 
bool Eradicate (Unit *objectToKill)
 
bool Remove (const LineCollide *target, Unit *objectToKill)
 Removes objectToKill from collide table with span of Target. More...
 

Static Public Member Functions

static int hash_int (const double aye)
 Hashes a single value to a value on the collide table truncated to all 3d constraints. Consider using a swizzle. More...
 
static bool removeFromVector (UnitCollection &myvector, Unit *objectToKill)
 

Detailed Description

template<class CTSIZ, class CTACCURACY, class CTHUGE>
class UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >

Hashtable3d is a 3d datastructure that holds various starships that are near enough to crash into each other (or also lights that are big enough to shine on nearby units.

Definition at line 26 of file unit_collide.h.

Constructor & Destructor Documentation

template<class CTSIZ, class CTACCURACY, class CTHUGE>
UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::UnitHash3d ( StarSystem ss)
inline

Definition at line 54 of file unit_collide.h.

54  : UnitHash3d( StarSystem *ss )
55  {
56  activeStarSystem = ss;
57  active_huge = &ha;
58  accum_huge = &hb;
59  }

Member Function Documentation

template<class CTSIZ, class CTACCURACY, class CTHUGE>
void UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::AddHugeToActive ( Unit un)
inline

Definition at line 73 of file unit_collide.h.

74  {
75  if ( acc_huge.find( un ) == acc_huge.end() ) {
76  acc_huge.insert( un );
77  accum_huge->prepend( un );
78  if ( act_huge.find( un ) == act_huge.end() ) {
79  act_huge.insert( un );
80  active_huge->prepend( un );
81  }
82  }
83  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
void UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::Clear ( )
inline

clears entire table

Definition at line 93 of file unit_collide.h.

94  {
95  if ( !hugeobjects.empty() )
96  hugeobjects.clear();
97  if ( this->active_huge.size() )
98  ha.clear();
99  if ( this->accum_huge.size() )
100  hb.clear();
101  acc_huge.clear();
102  act_huge.clear();
103  for (int i = 0; i <= COLLIDETABLESIZE-1; i++)
104  for (int j = 0; j <= COLLIDETABLESIZE-1; j++)
105  for (int k = 0; k <= COLLIDETABLESIZE-1; k++)
106  if ( !table[i][j][k].empty() )
107  table[i][j][k].clear();
108  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
bool UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::Eradicate ( Unit objectToKill)
inline

Definition at line 212 of file unit_collide.h.

Referenced by EradicateCollideTable().

213  {
214  bool ret = removeFromVector( hugeobjects, objectToKill );
215  for (unsigned int i = 0; i <= COLLIDETABLESIZE-1; i++)
216  for (unsigned int j = 0; j <= COLLIDETABLESIZE-1; j++)
217  for (unsigned int k = 0; k <= COLLIDETABLESIZE-1; k++)
218  ret |= removeFromVector( table[i][j][k], objectToKill );
219  return ret;
220  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
int UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::Get ( const QVector Exact,
UnitCollection retval[],
bool  GetHuge 
)
inline

returns any objects residing in the sector occupied by Exact

Definition at line 110 of file unit_collide.h.

111  {
112  retval[1] = &table[hash_int( Exact.i )][hash_int( Exact.j )][hash_int( Exact.k )];
113  retval[0] = active_huge;
114  if (GetHuge)
115  retval[0] = &hugeobjects;
116  return 2;
117  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
int UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::Get ( const LineCollide target,
UnitCollection retval[],
bool  GetHuge 
)
inline

Returns all objects within sector(s) occupied by target.

Definition at line 124 of file unit_collide.h.

125  {
126  unsigned int sizer = 1;
127  double maxx = ( ceil( target->Maxi.i/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
128  double maxy = ( ceil( target->Maxi.j/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
129  double maxz = ( ceil( target->Maxi.k/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
130  int x, y, z;
131  if (target->Mini.i == maxx) maxx += COLLIDETABLEACCURACY/2;
132  if (target->Mini.j == maxy) maxy += COLLIDETABLEACCURACY/2;
133  if (target->Mini.k == maxz) maxz += COLLIDETABLEACCURACY/2;
134  retval[0] = &hugeobjects;
135  if (!GetHuge)
136  retval[0] = active_huge;
137  if (target->hhuge)
138  return sizer; //we can't get _everything
139  for (double i = target->Mini.i; i <= maxx; i += COLLIDETABLEACCURACY) {
140  x = hash_int( i );
141  for (double j = target->Mini.j; j <= maxy; j += COLLIDETABLEACCURACY) {
142  y = hash_int( j );
143  for (double k = target->Mini.k; k <= maxz; k += COLLIDETABLEACCURACY) {
144  z = hash_int( k );
145  if ( !table[x][y][z].empty() ) {
146  retval[sizer] = &table[x][y][z];
147  sizer++;
148  if (sizer >= HUGEOBJECT+1)
149  return sizer;
150  }
151  }
152  }
153  }
154  assert( sizer <= HUGEOBJECT+1 ); //make sure we didn't overrun our array
155  return sizer;
156  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
UnitCollection& UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::GetHuge ( )
inline

Returns all objects too big to be conveniently fit in the array.

Definition at line 119 of file unit_collide.h.

120  {
121  return hugeobjects;
122  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
static int UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::hash_int ( const double  aye)
inlinestatic

Hashes a single value to a value on the collide table truncated to all 3d constraints. Consider using a swizzle.

Definition at line 86 of file unit_collide.h.

Referenced by UnitHash3d< char[coltablesize], char[coltableacc], char[tablehuge] >::Get(), UnitHash3d< char[coltablesize], char[coltableacc], char[tablehuge] >::Put(), UnitHash3d< char[coltablesize], char[coltableacc], char[tablehuge] >::Remove(), and TableLocationChanged().

87  {
88  return ( (int) ( ( (aye < 0) ? (aye
89  -COLLIDETABLEACCURACY) : aye )
91  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
void UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::Put ( LineCollide target,
Unit objectToPut 
)
inline

Adds objectToPut into collide table with limits specified by target.

Definition at line 158 of file unit_collide.h.

Referenced by AddCollideQueue().

159  {
160  int x, y, z;
161  double maxx = ( ceil( target->Maxi.i/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
162  double maxy = ( ceil( target->Maxi.j/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
163  double maxz = ( ceil( target->Maxi.k/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
164  //for huge calculation...not sure it's necessary
165  double minx = ( floor( target->Mini.i/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
166  double miny = ( floor( target->Mini.j/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
167  double minz = ( floor( target->Mini.k/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
168  if (target->Mini.i == maxx)
169  maxx += COLLIDETABLEACCURACY/2;
170  if (target->Mini.j == maxy) maxy += COLLIDETABLEACCURACY/2;
171  if (target->Mini.k == maxz) maxz += COLLIDETABLEACCURACY/2;
172  if ( fabs( (maxx
173  -minx)
174  *(maxy
175  -miny)
176  *(maxz
177  -minz) ) > ( (double) COLLIDETABLEACCURACY )*( (double) COLLIDETABLEACCURACY )
178  *( (double) COLLIDETABLEACCURACY )
179  *( (double) HUGEOBJECT ) ) {
180  target->hhuge = true;
181  hugeobjects.prepend( objectToPut );
182  return;
183  } else {
184  target->hhuge = false;
185  }
186  for (double i = target->Mini.i; i < maxx; i += COLLIDETABLEACCURACY) {
187  x = hash_int( i );
188  for (double j = target->Mini.j; j < maxy; j += COLLIDETABLEACCURACY) {
189  y = hash_int( j );
190  for (double k = target->Mini.k; k < maxz; k += COLLIDETABLEACCURACY) {
191  z = hash_int( k );
192  table[x][y][z].prepend( objectToPut );
193  }
194  }
195  }
196  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
bool UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::Remove ( const LineCollide target,
Unit objectToKill 
)
inline

Removes objectToKill from collide table with span of Target.

Definition at line 222 of file unit_collide.h.

Referenced by KillCollideTable().

223  {
224  bool ret = false;
225  int x, y, z;
226  double maxx = ( ceil( target->Maxi.i/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
227  double maxy = ( ceil( target->Maxi.j/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
228  double maxz = ( ceil( target->Maxi.k/COLLIDETABLEACCURACY ) )*COLLIDETABLEACCURACY;
229  if (target->Mini.i == maxx) maxx += COLLIDETABLEACCURACY/2;
230  if (target->Mini.j == maxy) maxy += COLLIDETABLEACCURACY/2;
231  if (target->Mini.k == maxz) maxz += COLLIDETABLEACCURACY/2;
232  if (!target->hhuge)
233  for (double i = target->Mini.i; i < maxx; i += COLLIDETABLEACCURACY) {
234  x = hash_int( i );
235  for (double j = target->Mini.j; j < maxy; j += COLLIDETABLEACCURACY) {
236  y = hash_int( j );
237  for (double k = target->Mini.k; k < maxz; k += COLLIDETABLEACCURACY) {
238  z = hash_int( k );
239  ret |= removeFromVector( table[x][y][z], objectToKill );
240  }
241  }
242  }
243  if (!ret && !target->hhuge)
244  fprintf( stderr, "Nonfatal Collide Error\n" );
245  if (!ret || target->hhuge)
246  ret |= removeFromVector( hugeobjects, objectToKill );
247  return ret;
248  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
static bool UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::removeFromVector ( UnitCollection myvector,
Unit objectToKill 
)
inlinestatic

Definition at line 197 of file unit_collide.h.

Referenced by UnitHash3d< char[coltablesize], char[coltableacc], char[tablehuge] >::Eradicate(), and UnitHash3d< char[coltablesize], char[coltableacc], char[tablehuge] >::Remove().

198  {
199  bool ret = false;
200  un_iter removal = myvector.createIterator();
201  Unit *un;
202  while ( (un = *removal) ) {
203  if (un == objectToKill) {
204  ret = true;
205  removal.remove();
206  } else {
207  ++removal;
208  }
209  }
210  return ret;
211  }
template<class CTSIZ, class CTACCURACY, class CTHUGE>
void UnitHash3d< CTSIZ, CTACCURACY, CTHUGE >::SwapHugeAccum ( )
inline

Definition at line 60 of file unit_collide.h.

61  {
62  if (active_huge == &ha) {
63  active_huge = &hb;
64  accum_huge = &ha;
65  } else {
66  active_huge = &ha;
67  accum_huge = &hb;
68  }
69  accum_huge->clear();
70  act_huge.swap( acc_huge );
71  acc_huge.clear();
72  }

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