summaryrefslogtreecommitdiffhomepage
path: root/Stars45/ShieldRep.cpp
blob: 38e19f4d321adc574a108f5d98a2f6207ac8ec76 (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
/*  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
    ========
    ShieldRep Solid class
*/

#include "MemDebug.h"
#include "ShieldRep.h"
#include "Random.h"

#include "Game.h"
#include "Light.h"
#include "Solid.h"
#include "Bitmap.h"
#include "Color.h"
#include "DataLoader.h"

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

const int MAX_SHIELD_HITS = 16;

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

struct ShieldHit
{
    Vec3     hitloc;
    double   damage;
    double   age;
    Shot*    shot;

    ShieldHit() : damage(0), age(0), shot(0) { }
};

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

ShieldRep::ShieldRep()
{
    bubble      = false;
    luminous    = true;
    trans       = true;
    nhits       = 0;

    hits = new(__FILE__,__LINE__) ShieldHit[MAX_SHIELD_HITS];
}

ShieldRep::~ShieldRep()
{
    delete [] hits;
}

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

void
ShieldRep::Hit(Vec3 impact, Shot* shot, double damage)
{
    if (!model || model->GetSurfaces().size() < 1)
    return;

    // transform impact into object space:
    Matrix xform(Orientation());

    Vec3 tmp = impact - loc;

    impact.x = tmp * Vec3(xform(0,0), xform(0,1), xform(0,2));
    impact.y = tmp * Vec3(xform(1,0), xform(1,1), xform(1,2));
    impact.z = tmp * Vec3(xform(2,0), xform(2,1), xform(2,2));

    // find slot to store the hit:
    int    i;
    int    slot = -1;
    double age  = -1;

    for (i = 0; i < MAX_SHIELD_HITS; i++) {
        if (hits[i].shot == shot) {
            slot = i;
            break;
        }
    }

    if (slot < 0) {
        for (i = 0; i < MAX_SHIELD_HITS; i++) {
            if (hits[i].damage <= 0) {
                slot = i;
                break;
            }

            if (hits[i].age > age) {
                slot = i;
                age  = hits[i].age;
            }
        }
    }

    if (slot >= 0 && slot < MAX_SHIELD_HITS) {
        // record the hit in the slot:
        hits[slot].hitloc  = impact;
        hits[slot].damage  = damage;
        hits[slot].age     = 1;
        hits[slot].shot    = shot;

        if (nhits < MAX_SHIELD_HITS)
        nhits++;
    }
}

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

void
ShieldRep::Energize(double seconds, bool b)
{
    bubble = b;

    if (nhits < 1) return;

    nhits = 0;

    for (int i = 0; i < MAX_SHIELD_HITS; i++) {
        if (hits[i].damage > 0) {
            // age the hit:
            hits[i].age += seconds;
            hits[i].damage -= (hits[i].damage * 4 * seconds);

            // collect garbage:
            if (hits[i].damage < 10) {
                hits[i].age     = 0;
                hits[i].damage  = 0;
                hits[i].shot    = 0;
            }
            else {
                nhits++;
            }
        }
    }
}

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

void
ShieldRep::TranslateBy(const Point& ref)
{
    true_eye_point = ref;
    Solid::TranslateBy(ref);
}

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

void
ShieldRep::Illuminate()
{
    if (!model) return;

    Surface*   surf   = model->GetSurfaces().first();
    VertexSet* vset   = surf->GetVertexSet();
    int        nverts = vset->nverts;

    for (int i = 0; i < nverts; i++) {
        vset->diffuse[i]  = 0;
        vset->specular[i] = 0;
    }

    double all_damage = 0;

    if (nhits < 1) return;

    for (int i = 0; i < MAX_SHIELD_HITS; i++) {
        if (hits[i].damage > 0) {
            // add the hit's contribution to the shield verts:
            Vec3   hitloc = hits[i].hitloc;
            double hitdam = hits[i].damage * 2000;

            all_damage += hits[i].damage;

            if (!bubble) {

                double limit = radius * radius;
                if (hitdam > limit)
                hitdam = limit;

                for (int v = 0; v < nverts; v++) {
                    double dist  = (vset->loc[v] - hitloc).length();

                    if (dist < 1)
                    dist  = 1;  // can't divide by zero!

                    else
                    dist = pow(dist, 2.7);

                    double pert      = Random(0.1, 1.5);
                    double intensity = pert*hitdam/dist;

                    if (intensity > 0.003)
                    vset->diffuse[v] = ((Color::White * intensity)  + vset->diffuse[v]).Value();
                }

            }
        }
    }

    if (bubble) {
        double shield_gain = 1;

        if (all_damage < 1000) {
            shield_gain = all_damage / 1000;
        }

        for (int i = 0; i < nverts; i++) {
            Vec3  vloc = (vset->loc[i] * orientation) + loc;
            Vec3  vnrm = (vset->nrm[i] * orientation);

            Vec3  V   = vloc * -1.0f;
            V.Normalize();

            double intensity = 1 - V*vnrm;

            if (intensity > 0) {
                intensity *= intensity;

                if (intensity > 1) intensity = 1;

                intensity *= (shield_gain * Random(0.75, 1.0));

                Color vs = Color::White * intensity;
                vset->diffuse[i] = vs.Value();
            }
        }
    }

    InvalidateSurfaceData();
}

void
ShieldRep::Render(Video* video, DWORD flags)
{
    if ((flags & RENDER_ADDITIVE) == 0)
    return;

    if (nhits > 0) {
        Illuminate();
        Solid::Render(video, RENDER_ALPHA); // have to lie about the render flag
        // or the engine will reject the solid
    }
}