summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Contact.cpp
blob: a2115dfd2e2234f5d49a064cc18aa7c2336740e0 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*  Starshatter OpenSource Distribution
    Copyright (c) 1997-2004, Destroyer Studios LLC.
    All Rights Reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name "Destroyer Studios" nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    SUBSYSTEM:    Stars.exe
    FILE:         Contact.cpp
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Integrated (Passive and Active) Sensor Package class implementation
*/

#include "MemDebug.h"
#include "Contact.h"
#include "Drone.h"
#include "Sensor.h"
#include "Ship.h"
#include "Sim.h"
#include "WeaponDesign.h"

#include "Game.h"

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

const int      DEFAULT_TRACK_UPDATE = 500; // milliseconds
const int      DEFAULT_TRACK_LENGTH =  20; // 10 seconds
const double   DEFAULT_TRACK_AGE    =  10; // 10 seconds
const double   SENSOR_THRESHOLD     = 0.25;

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

Contact::Contact()
: ship(0), shot(0), d_pas(0.0f), d_act(0.0f),
track(0), ntrack(0), time(0), track_time(0), probe(false)
{
    acquire_time = Game::GameTime();
}

Contact::Contact(Ship* s, float p, float a)
: ship(s), shot(0), d_pas(p), d_act(a),
track(0), ntrack(0), time(0), track_time(0), probe(false)
{
    acquire_time = Game::GameTime();
    Observe(ship);
}

Contact::Contact(Shot* s, float p, float a)
: ship(0), shot(s), d_pas(p), d_act(a),
track(0), ntrack(0), time(0), track_time(0), probe(false)
{
    acquire_time = Game::GameTime();
    Observe(shot);
}

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

Contact::~Contact()
{
    delete [] track;
}

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

int
Contact::operator == (const Contact& c) const
{
    if (ship)
    return ship == c.ship;

    else if (shot)
    return shot == c.shot;

    return 0;
}

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

bool
Contact::Update(SimObject* obj)
{
    if (obj == ship || obj == shot) {
        ship  = 0;
        shot  = 0;
        d_act = 0;
        d_pas = 0;

        ClearTrack();
    }

    return SimObserver::Update(obj);
}

const char*
Contact::GetObserverName() const
{
    static char name[128];

    if (ship)
        sprintf_s(name, "Contact Ship='%s'", ship->Name());
    else if (shot)
        sprintf_s(name, "Contact Shot='%s' %s", shot->Name(), shot->DesignName());
    else
        sprintf_s(name, "Contact (unknown)");

    return name;
}

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

double
Contact::Age() const
{
    double age = 0;

    if (!ship && !shot)
    return age;

    double seconds = (Game::GameTime() - time) / 1000.0;

    age = 1.0 - seconds/DEFAULT_TRACK_AGE;

    if (age < 0)
    age = 0;

    return age;
}

int
Contact::GetIFF(const Ship* observer) const
{
    int i = 0;

    if (ship) {
        i = ship->GetIFF();

        // if the contact is on our side or has locked us up,
        // we know whose side he's on.

        // Otherwise:
        if (i != observer->GetIFF() && !Threat(observer)) {
            if (d_pas < 2*SENSOR_THRESHOLD && d_act < SENSOR_THRESHOLD && !Visible(observer))
            i = -1000;   // indeterminate iff reading
        }
    }

    else if (shot && shot->Owner()) {
        i = shot->Owner()->GetIFF();
    }

    return i;
}

bool
Contact::ActLock() const
{
    return d_act >= SENSOR_THRESHOLD;
}

bool
Contact::PasLock() const
{
    return d_pas >= SENSOR_THRESHOLD;
}

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

void
Contact::GetBearing(const Ship* observer, double& az, double& el, double& rng) const
{
    // translate:
    Point targ_pt = loc - observer->Location();

    // rotate:
    const Camera* cam = &observer->Cam();
    double        tx  = targ_pt * cam->vrt();
    double        ty  = targ_pt * cam->vup();
    double        tz  = targ_pt * cam->vpn();

    // convert to spherical coords:
    rng = targ_pt.length();
    az  = asin(fabs(tx) / rng);
    el  = asin(fabs(ty) / rng);

    if (tx < 0) az = -az;
    if (ty < 0) el = -el;

    // correct az/el for back hemisphere:
    if (tz < 0) {
        if (az < 0) az = -PI - az;
        else        az =  PI - az;
    }
}

double
Contact::Range(const Ship* observer, double limit) const
{
    double r = Point(loc - observer->Location()).length();

    // if passive only, return approximate range:
    if (!ActLock()) {
        const int chunk = 25000;

        if (!PasLock()) {
            r = (int) limit;
        }

        else if (r <= chunk) {
            r = chunk;
        }

        else {
            int r1 = (int) (r + chunk/2) / chunk;
            r = r1 * chunk;
        }
    }

    return r;
}

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

bool
Contact::InFront(const Ship* observer) const
{
    // translate:
    Point targ_pt = loc - observer->Location();

    // rotate:
    const Camera* cam = &observer->Cam();
    double tz = targ_pt * cam->vpn();

    if (tz > 1.0)
    return true;

    return false;
}

bool
Contact::Threat(const Ship* observer) const
{
    bool threat = false;

    if (observer && observer->Life() != 0) {
        if (ship && ship->Life() != 0) {
            threat = (ship->GetIFF() &&
            ship->GetIFF() != observer->GetIFF() &&
            ship->GetEMCON() > 2 &&
            ship->IsTracking((Ship*) observer) &&
            ship->Weapons().size() > 0);

            if (threat && observer->GetIFF() == 0)
            threat = ship->GetIFF() > 1;
        }

        else if (shot) {
            threat = shot->IsTracking((Ship*) observer);

            if (!threat && shot->Design()->probe && shot->GetIFF() != observer->GetIFF()) {
                Point  probe_pt = shot->Location() - observer->Location();
                double prng     = probe_pt.length();

                threat = (prng < shot->Design()->lethal_radius);
            }
        }
    }

    return threat;
}

bool
Contact::Visible(const Ship* observer) const
{
    // translate:
    Point targ_pt = loc - observer->Location();
    double radius = 0;

    if (ship)
    radius = ship->Radius();

    else if (shot)
    radius = shot->Radius();

    // rotate:
    const Camera* cam = &observer->Cam();
    double        rng = targ_pt.length();

    return radius/rng > 0.002;
}

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

void
Contact::Reset()
{
    if (Game::Paused()) return;

    float step_down = (float) (Game::FrameTime() / 10);

    if (d_pas > 0)
    d_pas -= step_down;

    if (d_act > 0)
    d_act -= step_down;
}

void
Contact::Merge(Contact* c)
{
    if (c->GetShip() == ship && c->GetShot() == shot) {
        if (c->d_pas > d_pas)
        d_pas = c->d_pas;

        if (c->d_act > d_act)
        d_act = c->d_act;
    }
}

void
Contact::ClearTrack()
{
    delete [] track;
    track = 0;
    ntrack = 0;
}

void
Contact::UpdateTrack()
{
    time = Game::GameTime();

    if (shot || (ship && ship->IsGroundUnit()))
    return;

    if (!track) {
        track = new(__FILE__,__LINE__) Point[DEFAULT_TRACK_LENGTH];
        track[0] = loc;
        ntrack   = 1;
        track_time = time;
    }

    else if (time - track_time > DEFAULT_TRACK_UPDATE) {
        if (loc != track[0]) {
            for (int i = DEFAULT_TRACK_LENGTH-2; i >= 0; i--)
            track[i+1] = track[i];

            track[0] = loc;
            if (ntrack < DEFAULT_TRACK_LENGTH) ntrack++;
        }

        track_time = time;
    }
}

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

Point
Contact::TrackPoint(int i) const
{
    if (track && i < ntrack)
    return track[i];

    return Point();
}