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
terrain.cpp
Go to the documentation of this file.
1 #include "terrain.h"
2 #include "config_xml.h"
3 #include "vs_globals.h"
4 #include "xml_support.h"
5 #include "star_system.h"
6 #include "unit_generic.h"
7 #include "gfx/vec.h"
8 #include "vegastrike.h"
9 #include "universe.h"
10 #include <vector>
11 #include "collection.h"
12 #include "building.h"
13 static std::vector< Terrain* >allterrains;
14 
15 Terrain::Terrain( const char *filename,
16  const Vector &scales,
17  const float mass,
18  const float radius,
19  updateparity *updatetransform ) : QuadTree( filename, scales, radius )
20  , TotalSizeX( 0 )
21  , TotalSizeZ( 0 )
22  , mass( mass )
23  , whichstage( 0 )
24 {
25  this->updatetransform = updatetransform;
26  allterrains.push_back( this );
28 }
29 
31 {
32  for (unsigned int i = 0; i < allterrains.size(); i++)
33  if (allterrains[i] == this) {
34  allterrains.erase( allterrains.begin()+i );
35  break;
36  }
37 }
38 
40 {
42 }
43 
44 void Terrain::ApplyForce( Unit *un, const Vector &normal, float dist )
45 {
46  un->ApplyForce( normal*.4*un->GetMass()
47  *fabs( normal.Dot( (un->GetVelocity()/SIMULATION_ATOM) )+fabs( dist )/(SIMULATION_ATOM) ) );
48  un->ApplyDamage( un->Position().Cast()-normal*un->rSize(), -normal, .5*fabs( normal.Dot(
49  un->GetVelocity() ) )*mass*SIMULATION_ATOM,
50  un, GFXColor( 1, 1, 1, 1 ), NULL );
51 }
52 void Terrain::Collide( Unit *un, const Matrix &t )
53 {
54  Vector norm;
55  if (un->isUnit() == BUILDINGPTR)
56  return;
57  float dist = GetHeight( un->Position().Cast(), norm, t, TotalSizeX, TotalSizeZ )-un->rSize();
58  if (dist < 0)
59  ApplyForce( un, norm, -dist );
60 }
62 {
63  Collide( un, transformation );
64 }
66 {
67  draw &= (~TERRAINUPDATE);
68 }
69 
71 {
72  draw |= TERRAINUPDATE;
73 }
75 {
76  draw &= (~TERRAINRENDER);
77 }
79 {
80  draw |= (TERRAINRENDER);
81 }
83 {
84  Unit *unit;
85  for (un_iter iter = _Universe->activeStarSystem()->getUnitList().createIterator(); (unit=*iter)!=NULL; ++iter)
86  Collide( unit );
87 }
89 {
90  float col[4] = {.1f, .1f, .1f, 1.0f};
91  return GFXColor( col[0], col[1], col[2], col[3] );
92 }
94 {
95  for (unsigned int i = 0; i < allterrains.size(); i++)
96  if (allterrains[i]->draw&TERRAINRENDER)
97  allterrains[i]->Collide();
98 }
100 {
101  while ( !allterrains.empty() )
102  delete allterrains.front();
103 }
105 {
106  static GFXColor terraincolor( getTerrainColor() );
107  GFXColor tmpcol( 0, 0, 0, 1 );
108  GFXGetLightContextAmbient( tmpcol );
109  GFXLightContextAmbient( terraincolor );
111  GFXLightContextAmbient( tmpcol );
112 }
114 {
115  static GFXColor terraincolor( getTerrainColor() );
116  GFXColor tmpcol( 0, 0, 0, 1 );
117  GFXGetLightContextAmbient( tmpcol );
118  GFXLightContextAmbient( terraincolor );
119  for (unsigned int i = 0; i < allterrains.size(); i++)
120  if (allterrains[i]->draw&TERRAINRENDER)
121  allterrains[i]->Render();
122  GFXLightContextAmbient( tmpcol );
123 }
124 void Terrain::UpdateAll( int resolution )
125 {
126  int res = 4;
127  if (resolution == 0)
128  res = 0;
129  else
130  while (resolution > res)
131  res *= 4;
132  for (unsigned int i = 0; i < allterrains.size(); i++)
133  if (allterrains[i]->draw&TERRAINUPDATE) {
134  allterrains[i]->Update( res, allterrains[i]->whichstage%res, allterrains[i]->updatetransform );
135  allterrains[i]->whichstage++;
136  }
137 }
139 {
140  return GetNormal( pos, Vector( 0, 1, 0 ) );
141 }
142