summaryrefslogtreecommitdiffhomepage
path: root/Stars45/ShipKiller.cpp
blob: 68b443a4befd5bfcd1a5139ae3da211bbd79938a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*  Project Starshatter 4.5
    Destroyer Studios LLC
    Copyright © 1997-2004. All Rights Reserved.

    SUBSYSTEM:    Stars
    FILE:         ShipKiller.cpp
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    ShipKiller class implementation
*/

#include "MemDebug.h"
#include "ShipKiller.h"
#include "Sim.h"
#include "Ship.h"
#include "ShipDesign.h"
#include "System.h"
#include "Weapon.h"
#include "Shot.h"
#include "Explosion.h"
#include "Debris.h"
#include "HUDSounds.h"

#include "Solid.h"
#include "Random.h"

// +----------------------------------------------------------------------+

ShipKiller::ShipKiller(Ship* s)
   : ship(s), DEATH_CAM_LINGER(5.0f), time(0.0f), exp_time(0.0f), exp_index(0)
{
}

ShipKiller::~ShipKiller()
{
}

// +----------------------------------------------------------------------+

inline int random_index()
{
   return (int) (rand()/3277);
}

// +----------------------------------------------------------------------+

void
ShipKiller::BeginDeathSpiral()
{
   if (!ship) return;

   // shut down all ship systems:
   ListIter<System> iter = ship->Systems();
   while (++iter) {
      iter->PowerOff();
      iter->SetPowerLevel(0);

      if (iter->Type() == System::WEAPON) {
         Weapon* gun  = (Weapon*) iter.value();

         for (int i = 0; i < Weapon::MAX_BARRELS; i++) {
            Shot* beam = gun->GetBeam(i);
            if (beam)
               beam->Destroy();
         }
      }
   }

   if (ship->GetShieldRep())
      ship->GetShieldRep()->Hide();

   Sim*              sim    = Sim::GetSim();
   const ShipDesign* design = ship->Design();

   float  time_to_go = design->death_spiral_time;
   time = DEATH_CAM_LINGER + time_to_go;
   loc  = ship->Location() + ship->Velocity() * (time_to_go-1.0f);

   if (rand() < 16000)
      loc += ship->BeamLine() * -3 * ship->Radius();
   else
      loc += ship->BeamLine() *  3 * ship->Radius();

   if (rand() < 8000)
      loc += ship->LiftLine() * -1 * ship->Radius();
   else
      loc += ship->LiftLine() *  2 * ship->Radius();

   // stop on crash:
   if (ship->IsGroundUnit() || (ship->IsAirborne() && ship->AltitudeAGL() < ship->Radius()*2)) {
      time = DEATH_CAM_LINGER;
      loc  = ship->Location() + Point(6*ship->Radius(), 7*ship->Radius(), 8*ship->Radius());
      ship->SetVelocity(Point(0,0,0));
   }
   // else, slow tumble:
   else {
      Point torque = RandomVector(ship->Mass()/7);
      ship->ApplyTorque(torque);

      for (int i = 0; i < 5; i++) {
         exp_index = random_index() % ShipDesign::MAX_EXPLOSIONS;
         if (design->explosion[exp_index].type > 0 && !design->explosion[exp_index].final)
            break;
      }

      float exp_scale = design->explosion_scale;
      if (exp_scale <= 0)
         exp_scale = design->scale;

      exp_time  = design->explosion[exp_index].time;

      if (design->explosion[exp_index].type > 0) {
         Point exp_loc = ship->Location() + (design->explosion[exp_index].loc * ship->Cam().Orientation());
         sim->CreateExplosion(exp_loc,
                              ship->Velocity(), 
                              design->explosion[exp_index].type, 
                              (float) ship->Radius(), 
                              exp_scale, 
                              ship->GetRegion(), 
                              ship);
      }
   }

   ship->SetControls(0);
   ship->SetupAgility();
}

// +----------------------------------------------------------------------+

void
ShipKiller::ExecFrame(double seconds)
{
   Sim*              sim    = Sim::GetSim();
   const ShipDesign* design = ship->Design();

   time     -= (float) seconds;
   exp_time -= (float) seconds;

   float exp_scale = design->explosion_scale;
   if (exp_scale <= 0)
      exp_scale = design->scale;

   if (exp_time < 0) {
      exp_index++;
      if (exp_index >= ShipDesign::MAX_EXPLOSIONS || design->explosion[exp_index].final)
         exp_index = 0;

      exp_time  = design->explosion[exp_index].time;

      if (design->explosion[exp_index].type > 0) {
         Point exp_loc = ship->Location() + (design->explosion[exp_index].loc * ship->Cam().Orientation());
         sim->CreateExplosion(exp_loc,
                              ship->Velocity(), 
                              design->explosion[exp_index].type, 
                              (float) ship->Radius(), 
                              exp_scale, 
                              ship->GetRegion(), 
                              ship);
      }
   }

   if (time < DEATH_CAM_LINGER) {
      for (int i = 0; i < ShipDesign::MAX_EXPLOSIONS; i++) {
         if (design->explosion[i].final) {
            Point exp_loc = ship->Location() + (design->explosion[i].loc * ship->Cam().Orientation());
            sim->CreateExplosion(exp_loc,
                                 ship->Velocity(), 
                                 design->explosion[i].type, 
                                 (float) ship->Radius(), 
                                 exp_scale, 
                                 ship->GetRegion());
         }
      }

      for (int i = 0; i < ShipDesign::MAX_DEBRIS; i++) {
         if (design->debris[i].model) {
            Point debris_loc = ship->Location() + (design->debris[i].loc * ship->Cam().Orientation());
            Point debris_vel = debris_loc - ship->Location();
            debris_vel.Normalize();

            if (design->debris[i].speed > 0)
               debris_vel *= design->debris[i].speed;
            else
               debris_vel *= 200;

            if (ship->IsGroundUnit()) {
               debris_vel *= 2;
               
               if (debris_vel.y < 0)
                  debris_vel.y *= -1;
            }

            for (int n = 0; n < design->debris[i].count; n++) {
               Debris* debris = sim->CreateDebris(debris_loc,
                                       debris_vel + ship->Velocity(),
                                       design->debris[i].model,
                                       design->debris[i].mass,
                                       ship->GetRegion());

               debris->SetLife(design->debris[i].life);
               debris->SetDrag(design->debris[i].drag);

               if (n == 0) {
                  debris->CloneCam(ship->Cam());
                  debris->MoveTo(debris_loc);
               }

               for (int fire = 0; fire < 5; fire++) {
                  if (design->debris[i].fire_loc[fire] == Vec3(0,0,0))
                     continue;

                  Point fire_loc = debris->Location() + (design->debris[i].fire_loc[fire] * debris->Cam().Orientation());

                  if (design->debris[i].fire_type > 0) {
                     sim->CreateExplosion(fire_loc,
                                       ship->Velocity(), 
                                       design->debris[i].fire_type, 
                                       exp_scale,
                                       exp_scale, 
                                       ship->GetRegion(),
                                       debris);
                  }
                  else {
                     sim->CreateExplosion(fire_loc,
                                       ship->Velocity(), 
                                       Explosion::SMALL_FIRE, 
                                       exp_scale,
                                       exp_scale, 
                                       ship->GetRegion(),
                                       debris);

                     sim->CreateExplosion(fire_loc,
                                       ship->Velocity(), 
                                       Explosion::SMOKE_TRAIL, 
                                       exp_scale * 0.25f,
                                       exp_scale * 0.25f, 
                                       ship->GetRegion(),
                                       debris);
                  }
               }

               if (n+1 < design->debris[i].count) {
                  debris_vel = RandomVector(1);

                  if (design->debris[i].speed > 0)
                     debris_vel *= design->debris[i].speed * Random(0.8, 1.2);
                  else
                     debris_vel *= 300 + rand()/50;
               }
            }
         }
      }

      if (ship == sim->GetPlayerShip())
         HUDSounds::StopSound(HUDSounds::SND_RED_ALERT);

      sim->CreateSplashDamage(ship);
      ship->Destroy(); // CAREFUL!!!  This will also delete this object!
   }
}