summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Shield.cpp
blob: e7c8fb817ff6768e2cd81aa3f84b01aaa65fc881 (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
/*  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
    ========
    Weapon class
*/

#include "Shield.h"
#include "Shot.h"
#include "WeaponDesign.h"

#include "Game.h"
#include "ContentBundle.h"

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

static char*   shield_name[] = {
    "sys.shield.none",
    "sys.shield.deflector",
    "sys.shield.grav",
    "sys.shield.hyper"
};

static int     shield_value[] = {
    0, 2, 2, 3
};

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

Shield::Shield(SUBTYPE shield_type)
: System(SHIELD, shield_type, "shield", shield_value[shield_type], 100, 0),
shield_cutoff(0.0f), shield_capacitor(false), shield_bubble(false),
deflection_cost(1.0f), shield_curve(0.05f)
{
    name = ContentBundle::GetInstance()->GetText(shield_name[shield_type]);
    abrv = ContentBundle::GetInstance()->GetText("sys.shield.abrv");

    power_flags  = POWER_WATTS | POWER_CRITICAL;
    energy       = 0.0f;
    power_level  = 0.0f;
    shield_level = 0.0f;

    switch (shield_type) {
    default:
    case DEFLECTOR:
        capacity = sink_rate = 2.0e3f;
        shield_factor        = 0.05f;
        break;

    case GRAV_SHIELD:
        capacity = sink_rate = 7.0e3f;
        shield_factor        = 0.01f;
        break;

    case HYPER_SHIELD:
        capacity = sink_rate = 10.0e3f;
        shield_factor        = 0.003f;
        break;
    }

    emcon_power[0] = 0;
    emcon_power[1] = 0;
    emcon_power[2] = 100;
}

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

Shield::Shield(const Shield& s)
: System(s), shield_factor(s.shield_factor), requested_power_level(0.0f),
shield_cutoff(s.shield_cutoff), shield_capacitor(s.shield_capacitor),
shield_bubble(s.shield_bubble), deflection_cost(s.deflection_cost),
shield_curve(s.shield_curve)
{
    power_flags  = s.power_flags;
    energy       = 0.0f;
    power_level  = 0.0f;
    shield_level = 0.0f;

    Mount(s);
}

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

Shield::~Shield()
{ }

void
Shield::SetShieldCapacitor(bool c)
{
    shield_capacitor = c;

    if (shield_capacitor) {
        power_flags = POWER_CRITICAL;
        shield_curve = 0.05f;
    }
    else {
        power_flags = POWER_WATTS | POWER_CRITICAL;
        shield_curve = 0.25f;
    }
}

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

void
Shield::ExecFrame(double seconds)
{
    System::ExecFrame(seconds);

    if (power_level < requested_power_level) {
        power_level += (float) (seconds * 0.10);     // ten seconds to charge up

        if (power_level > requested_power_level)
        power_level = (float) requested_power_level;
    }
    else if (power_level > requested_power_level) {
        power_level -= (float) (seconds * 0.20);     // five seconds to power down

        if (power_level < requested_power_level)
        power_level = (float) requested_power_level;
    }

    if (power_level < 0.01 && !shield_capacitor) {
        shield_level = 0.0f;
        energy       = 0.0f;
    }
}

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

void
Shield::Distribute(double delivered_energy, double seconds)
{
    System::Distribute(delivered_energy, seconds);

    if (shield_capacitor) {
        if (shield_cutoff > 0 && shield_cutoff < 0.999) {
            float cutoff = shield_cutoff * capacity;

            if (energy > cutoff)
            shield_level = (energy-cutoff)/(capacity-cutoff);
            else
            shield_level = 0.0f;
        }

        else {
            shield_level = energy/capacity;
        }
    }
    else {
        shield_level = energy/sink_rate;
        energy       = 0.0f;
    }

    if (shield_level < 0)
    shield_level = 0;
}

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

double
Shield::DeflectDamage(Shot* shot, double damage)
{
    double filter      = 1;
    double penetration = 5;
    double leak        = 0;

    if (shot)
    penetration = shot->Design()->penetration;

    filter = 1 - shield_factor * penetration;

    if (filter < 0)
    filter = 0;

    else if (filter > 1)
    filter = 1;

    if (shield_capacitor) {
        if (shield_cutoff > 0 && shield_level < 1e-6) {
            leak = damage;
            energy -= (float) (damage * deflection_cost);
        }

        else {
            leak = damage * (1 - pow(shield_level, shield_curve) * filter * availability);

            double deflected = damage - leak;
            energy -= (float) deflected * deflection_cost;
        }

    }
    else {
        leak = damage * (1 - pow(shield_level, shield_curve) * filter * availability);
    }

    return leak;
}

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

void
Shield::SetPowerLevel(double level)
{
    if (level > 100)
    level = 100;
    else if (level < 0)
    level = 0;

    level /= 100;

    if (requested_power_level != level) {
        // if the system is on emergency override power,
        // do not let the EMCON system use this method
        // to drop it back to normal power:
        if (power_level > 1 && level == 1) {
            requested_power_level = (float) power_level;
            return;
        }

        requested_power_level = (float) level;
    }
}

void
Shield::SetNetShieldLevel(int level)
{
    if (level > 100)     level = 100;
    else if (level < 0)  level =   0;

    requested_power_level = (float) (level/100.0);
    power_level           = requested_power_level;
}

void
Shield::DoEMCON(int index)
{
    int e = GetEMCONPower(index);

    if (power_level * 100 > e || emcon != index) {
        if (e == 0) {
            PowerOff();
        }
        else if (emcon != index) {
            PowerOn();

            if (power_level * 100 > e)
            SetPowerLevel(e);
        }
    }

    emcon = index;
}