File: earthquake.cpp

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (275 lines) | stat: -rw-r--r-- 7,255 bytes parent folder | download
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
264
265
266
267
268
269
270
271
272
273
274
275
/*
===========================================================================
Copyright (C) 2015 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

// earthquake.cpp: Earthquake trigger causes a localized earthquake when triggered.
// The earthquake effect is visible to the user as the shaking of his screen.
//
#include "earthquake.h"
#include "weapon.h"
#include "sentient.h"
#include "level.h"

/*****************************************************************************/
/*QUAKED func_viewjitter (0 0.25 0.5) (-8 -8 -8) (8 8 8)
Causes a radius view jitter
"radius" sets the max effective range if the view jitter. Default is 128
"edgeeffect" set the fraction of the total jitter to apply at the max range. Default is 0.2
"jitteramount" sets the amount to jitter the view. Set in angles as "pitch yaw roll". Default is "2 2 3"
"duration" set how long the view jitter should last. Default is 0 (instantanious)
"timedecay" sets how the jitter angles will fall off over the jitter's duraction in degrees per second. Default is "2 2 3"
"donedeath" makes the view jitter remove itself after being fired once. Any non-zero value will work.
******************************************************************************/

Event EV_ViewJitter_Think
(
    "_viewjitter_think",
    EV_DEFAULT,
    0,
    0,
    "think function for the viewjitter."
);

Event EV_ViewJitter_Radius
(
    "radius",
    EV_DEFAULT,
    "f",
    "radius",
    "Sets the max radius of the view jitter. 0 affects all"
);

Event EV_ViewJitter_EdgeEffect
(
    "edgeeffect",
    EV_DEFAULT,
    "f",
    "fraction",
    "Sets the fraction of the jitter to apply at the max radius"
);

Event EV_ViewJitter_Amount
(
    "jitteramount",
    EV_DEFAULT,
    "v",
    "jitterangles",
    "Sets the jitter angles to apply to the player"
);

Event EV_ViewJitter_Duration
(
    "duration",
    EV_DEFAULT,
    "f",
    "time",
    "Sets the length of time it should last. 0 will be instantanious"
);

Event EV_ViewJitter_TimeDecay
(
    "timedecay",
    -1,
    "v",
    "decayrate",
    "Sets jitter decay per second"
);

Event EV_ViewJitter_DoneDeath
(
    "donedeath",
    EV_DEFAULT,
    0,
    0,
    "Makes the view jitter only happen once"
);

CLASS_DECLARATION(Trigger, ViewJitter, "func_viewjitter") {
    {&EV_Touch,                 NULL                            },
    {&EV_Trigger_Effect,        &ViewJitter::EventActivateJitter},
    {&EV_ViewJitter_Radius,     &ViewJitter::EventSetRadius     },
    {&EV_ViewJitter_EdgeEffect, &ViewJitter::EventSetEdgeEffect },
    {&EV_ViewJitter_Amount,     &ViewJitter::EventSetAmount     },
    {&EV_ViewJitter_Duration,   &ViewJitter::EventSetDuration   },
    {&EV_ViewJitter_TimeDecay,  &ViewJitter::EventSetTimeDecay  },
    {&EV_ViewJitter_DoneDeath,  &ViewJitter::EventSetDoneDeath  },
    {&EV_ViewJitter_Think,      &ViewJitter::EventJitterThink   },
    {NULL,                      NULL                            }
};

ViewJitter::ViewJitter()
{
    if (LoadingSavegame) {
        return;
    }

    edict->r.svFlags |= SVF_NOCLIENT;

    m_fRadius     = 16384.0f;
    m_fEdgeEffect = 0.2f;

    m_vJitterStrength = Vector(2, 2, 3);
    m_vTimeDecay      = Vector(2, 2, 3);

    m_fDuration = 0;

    m_bDoneDeath = false;
}

ViewJitter::ViewJitter(
    Vector vOrigin,
    float  fRadius,
    float  fEdgeEffect,
    Vector vStrength,
    float  fDuration,
    Vector vTimeDecay,
    float  fStartDecay
)
{
    if (LoadingSavegame) {
        return;
    }

    setOrigin(vOrigin);
    m_fEdgeEffect = fEdgeEffect;

    // square the raidus
    m_fRadius = fRadius * fRadius;

    m_vJitterStrength = vStrength;
    m_vTimeDecay      = vTimeDecay;

    m_fDuration = fDuration;

    m_bDoneDeath   = true;
    m_fTimeRunning = 0;

    PostEvent(EV_ViewJitter_Think, fStartDecay);
}

void ViewJitter::EventActivateJitter(Event *ev)
{
    m_fTimeRunning = 0;
    PostEvent(EV_ViewJitter_Think, 0);
}

void ViewJitter::EventJitterThink(Event *ev)
{
    int       i;
    int       iNumSents;
    float     fDist;
    float     fRadiusDecay;
    Vector    vCurrJitter;
    Vector    vApplyJitter;
    Vector    vDelta;
    Sentient *pSent;

    m_fTimeRunning += level.frametime;

    vDelta = m_vJitterStrength - m_vTimeDecay * m_fTimeRunning;

    iNumSents = SentientList.NumObjects();

    for (i = 1; i <= iNumSents; i++) {
        pSent = SentientList.ObjectAt(i);

        if (pSent->deadflag) {
            continue;
        }

        fRadiusDecay = (pSent->origin - origin).lengthSquared();
        if (fRadiusDecay <= m_fRadius) {
            Vector vVariation;

            if (m_fEdgeEffect == 1.0f) {
                vApplyJitter = vDelta;
            } else {
                fDist        = sqrt(fRadiusDecay);
                vApplyJitter = Vector((1.0f - fDist * (m_fEdgeEffect / m_fRadius)) * vDelta[0], 0, 0);
            }

            if (pSent->m_vViewVariation[0] <= vApplyJitter[0]) {
                vVariation[0] = vApplyJitter[0];
            } else {
                vVariation[0] = pSent->m_vViewVariation[0];
            }

            if (pSent->m_vViewVariation[1] <= vApplyJitter[1]) {
                vVariation[1] = vApplyJitter[1];
            } else {
                vVariation[1] = pSent->m_vViewVariation[1];
            }

            if (pSent->m_vViewVariation[2] <= vApplyJitter[2]) {
                vVariation[2] = vApplyJitter[2];
            } else {
                vVariation[2] = pSent->m_vViewVariation[2];
            }

            pSent->m_vViewVariation = vVariation;
        }
    }

    if (m_fDuration > 0.0f) {
        if (m_fTimeRunning <= m_fDuration || !m_bDoneDeath) {
            PostEvent(EV_ViewJitter_Think, level.frametime);
        } else {
            ProcessEvent(EV_Remove);
        }
    } else if (m_bDoneDeath) {
        ProcessEvent(EV_Remove);
    }
}

void ViewJitter::EventSetRadius(Event *ev)
{
    float rad = ev->GetFloat(1);

    m_fRadius = rad * rad;
    if (m_fRadius < 1.0f) {
        m_fRadius = 16384.0f;
    }
}

void ViewJitter::EventSetEdgeEffect(Event *ev)
{
    m_fEdgeEffect = ev->GetFloat(1);
}

void ViewJitter::EventSetAmount(Event *ev)
{
    m_vJitterStrength = ev->GetVector(1);
}

void ViewJitter::EventSetDuration(Event *ev)
{
    m_fDuration = ev->GetFloat(1);
}

void ViewJitter::EventSetTimeDecay(Event *ev)
{
    m_vTimeDecay = ev->GetVector(1);
}

void ViewJitter::EventSetDoneDeath(Event *ev)
{
    m_bDoneDeath = qtrue;
}