summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/Debris.cpp
blob: 952c1334b1cd5df63cbcc4c3f491c345aa8181fe (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
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Debris Sprite animation class
*/

#include "Debris.h"
#include "Shot.h"
#include "Explosion.h"
#include "Sim.h"
#include "StarSystem.h"
#include "Terrain.h"

#include "Solid.h"
#include "Bitmap.h"
#include "DataLoader.h"
#include "Clock.h"
#include "Random.h"

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

Debris::Debris(Model* model, const Vec3& pos, const Vec3& vel, double m)
: SimObject("Debris", SimObject::SIM_DEBRIS)
{
    MoveTo(pos);

    velocity  = vel;
    mass      = (float) m;
    integrity = mass * 10.0f;
    life      = 300;

    Solid* solid = new Solid;

    if (solid) {
        solid->UseModel(model);
        solid->MoveTo(pos);

        rep = solid;

        radius = solid->Radius();
    }

    Point torque = RandomVector(Mass()/2);

    if (Mass() < 10)
    torque *= (rand() / 3200);
    else if (Mass() > 10e3)
    torque *= 0.25;
    else if (Mass() > 10e6)
    torque *= 0.005;

    ApplyTorque(torque);
}

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

int
Debris::HitBy(Shot* shot, Point& impact)
{
    if (!shot->IsArmed()) return 0;

    const int HIT_NOTHING   = 0;
    const int HIT_HULL      = 1;

    Point    hull_impact;
    int      hit_type = HIT_NOTHING;
    bool     hit_hull = true;
    Point    shot_loc = shot->Location();
    Point    delta    = shot_loc - Location();
    double   dlen     = delta.length();
    double   dscale   = 1;
    float    scale    = 1.0f;
    Sim*     sim      = Sim::GetSim();

    // MISSILE PROCESSING ------------------------------------------------

    if (shot->IsMissile()) {
        if (dlen < Radius()) {
            hull_impact = impact = shot_loc;
            sim->CreateExplosion(impact, Velocity(), Explosion::HULL_FLASH,   0.3f * scale, scale, region, this);
            sim->CreateExplosion(impact, Point(),    Explosion::SHOT_BLAST,   2.0f,         scale, region);
            hit_type = HIT_HULL;
        }
    }

    // ENERGY WEP PROCESSING ---------------------------------------------

    else {

        Solid* solid = (Solid*) rep;

        Point  shot_loc = shot->Location();
        Point  shot_vpn = shot_loc - shot->Origin();
        double shot_len = shot_vpn.Normalize();
        if (shot_len == 0) shot_len = 1000;

        // impact:
        if (solid) {
            if (solid->CheckRayIntersection(shot->Origin(), shot_vpn, shot_len, impact)) {
                // trim beam shots to impact point:
                if (shot->IsBeam())
                shot->SetBeamPoints(shot->Origin(), impact);

                hull_impact = impact;

                if (shot->IsBeam())
                sim->CreateExplosion(impact, Velocity(), Explosion::BEAM_FLASH, 0.30f * scale, scale, region, this);
                else
                sim->CreateExplosion(impact, Velocity(), Explosion::HULL_FLASH, 0.30f * scale, scale, region, this);

                Point burst_vel = hull_impact - Location();
                burst_vel.Normalize();
                burst_vel *= Radius() * 0.5;
                burst_vel += Velocity();

                sim->CreateExplosion(hull_impact, burst_vel, Explosion::HULL_BURST, 0.50f * scale, scale, region, this);

                hit_type = HIT_HULL;
                hit_hull = true;
            }
        }

        else {
            if (dlen < Radius()) {
                hull_impact = impact = shot_loc;

                if (shot->IsBeam())
                sim->CreateExplosion(impact, Velocity(), Explosion::BEAM_FLASH, 0.30f * scale, scale, region, this);
                else
                sim->CreateExplosion(impact, Velocity(), Explosion::HULL_FLASH, 0.30f * scale, scale, region, this);

                hit_type = HIT_HULL;
            }
        }
    }

    // DAMAGE RESOLUTION -------------------------------------------------

    if (hit_type != HIT_NOTHING) {
        double effective_damage = shot->Damage() * dscale;

        if (shot->IsBeam()) {
            effective_damage *= Clock::GetInstance()->Delta();
        }
        else {
            ApplyTorque(shot->Velocity() * (float) effective_damage * 1e-6f);
        }

        if (effective_damage > 0)
        Physical::InflictDamage(effective_damage);
    }

    return hit_type;
}

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

void
Debris::ExecFrame(double seconds)
{
    if (GetRegion()->Type() == SimRegion::AIR_SPACE) {
        if (AltitudeAGL() < Radius()) {
            velocity          = Point();
            arcade_velocity   = Point();

            Terrain* terrain  = region->GetTerrain();

            if (terrain) {
                Point loc = Location();
                MoveTo(Point(loc.x, terrain->Height(loc.x, loc.z), loc.z));
            }
        }
        else {
            if (mass > 100) {
                Orbital* primary = GetRegion()->GetOrbitalRegion()->Primary();

                const double   GRAV = 6.673e-11;
                double         m0   = primary->Mass();
                double         r    = primary->Radius();

                SetDrag(0.001);
                SetGravity(6 * GRAV * m0 / (r*r));  // accentuate gravity
                SetBaseDensity(1.0f);
            }

            AeroFrame(seconds);
        }
    }
    else {
        Physical::ExecFrame(seconds);
    }
}

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

double
Debris::AltitudeAGL() const
{
    Point    loc          = Location();
    double   altitude_agl = loc.y;

    Terrain* terrain      = region->GetTerrain();

    if (terrain)
    altitude_agl -= terrain->Height(loc.x, loc.z);

    if (!_finite(altitude_agl))
    altitude_agl = 0;

    return altitude_agl;
}