summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/SteerAI.cpp
blob: 043cd8065fb44e8fbef83b333521017be4408cc7 (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2024, 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
    ========
    Steering (low-level) Artificial Intelligence class
*/

#include "SteerAI.h"
#include "SeekerAI.h"
#include "FighterAI.h"
#include "StarshipAI.h"
#include "GroundAI.h"
#include "System.h"

#include "Clock.h"
#include "Physical.h"

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

Steer  Steer::operator+(const Steer& s) const
{
    return Steer(yaw+s.yaw, pitch+s.pitch, roll+s.roll, (brake>s.brake)?brake:s.brake);
}

Steer  Steer::operator-(const Steer& s) const
{
    return Steer(yaw-s.yaw, pitch-s.pitch, roll-s.roll, (brake<s.brake)?brake:s.brake);
}

Steer  Steer::operator*(double f)       const
{
    return Steer(yaw*f, pitch*f, roll*f, brake);
}

Steer  Steer::operator/(double f)       const
{
    return Steer(yaw/f, pitch/f, roll/f, brake);
}


Steer& Steer::operator+=(const Steer& s)
{
    yaw   += s.yaw;
    pitch += s.pitch;
    roll  += s.roll;

    if (s.brake > brake)
    brake = s.brake;

    if (s.stop)
    stop = 1;

    return *this;
}

Steer& Steer::operator-=(const Steer& s)
{
    yaw   -= s.yaw;
    pitch -= s.pitch;
    roll  -= s.roll;

    if (s.brake < brake)
    brake = s.brake;

    if (s.stop)
    stop = 1;

    return *this;
}


double
Steer::Magnitude() const
{
    return sqrt(yaw*yaw + pitch*pitch);
}

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

Director*
SteerAI::Create(SimObject* self, int type)
{
    switch (type) {
    case SEEKER:   return new SeekerAI(self);
        break;

    case STARSHIP: return new StarshipAI(self);
        break;

    case GROUND:   return new GroundAI(self);
        break;

    default:
    case FIGHTER:  return new FighterAI(self);
        break;
    }
}

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

SteerAI::SteerAI(SimObject* ship)
: self(ship),
target(0), subtarget(0), other(0), distance(0.0), evade_time(0),
objective(0.0f, 0.0f, 0.0f)
{
    seek_gain = 20;
    seek_damp = 0.5;

    for (int i = 0; i < 3; i++)
    az[i] = el[i] = 0;
}


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

SteerAI::~SteerAI()
{ }

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

void
SteerAI::SetTarget(SimObject* targ, System* sub)
{
    if (target != targ) {
        target = targ;

        if (target)
        Observe(target);
    }

    subtarget = sub;
}

void
SteerAI::DropTarget(double dtime)
{
    SetTarget(0);
}

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

bool
SteerAI::Update(SimObject* obj)
{
    if (obj == target) {
        target = 0;
        subtarget = 0;
    }

    if (obj == other) {
        other = 0;
    }

    return SimObserver::Update(obj);
}

const char*
SteerAI::GetObserverName() const
{
    static char name[64];
    sprintf_s(name, "SteerAI(%s)", self->Name());
    return name;
}

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

Point
SteerAI::ClosingVelocity()
{
    if (self) {
        if (target)
        return self->Velocity() - target->Velocity();
        else
        return self->Velocity();
    }

    return Point(1, 0, 0);
}

void
SteerAI::FindObjective()
{
    if (!self || !target) return;

    Point  cv   = ClosingVelocity();
    double cvl  = cv.length();
    double time = 0;

    if (cvl > 5) {
        // distance from self to target:
        distance = Point(target->Location() - self->Location()).length();

        // time to reach target:
        time     = distance / cvl;

        // where the target will be when we reach it:
        Point run_vec = target->Velocity();
        obj_w   = target->Location() + (run_vec * time);
    }

    else {
        obj_w   = target->Location();
    }

    // subsystem offset:
    if (subtarget) {
        Point offset = target->Location() - subtarget->MountLocation();
        obj_w -= offset;
    }

    distance = Point(obj_w - self->Location()).length();

    if (cvl > 5)
    time     = distance / cvl;

    // where we will be when the target gets there:
    Point self_dest = self->Location() + cv * time;
    Point err = obj_w - self_dest;

    obj_w += err;

    // transform into camera coords:
    objective = Transform(obj_w);
    objective.Normalize();

    distance = Point(obj_w - self->Location()).length();
}

Point
SteerAI::Transform(const Point& pt)
{
    Point obj_t = pt - self->Location();
    Point result;

    if (self->FlightPathYawAngle() != 0 || self->FlightPathPitchAngle() != 0) {
        double az = self->FlightPathYawAngle();
        double el = self->FlightPathPitchAngle();

        const double MAX_ANGLE = 15*DEGREES;
        const double MIN_ANGLE =  3*DEGREES;

        if (az > MAX_ANGLE)
        az = MAX_ANGLE;
        else if (az < -MAX_ANGLE)
        az = -MAX_ANGLE;
        else if (az > MIN_ANGLE)
        az = MIN_ANGLE + (az-MIN_ANGLE)/2;
        else if (az < -MIN_ANGLE)
        az = -MIN_ANGLE + (az+MIN_ANGLE)/2;

        if (el > MAX_ANGLE)
        el = MAX_ANGLE;
        else if (el < -MAX_ANGLE)
        el = -MAX_ANGLE;
        else if (el > MIN_ANGLE)
        el = MIN_ANGLE + (el-MIN_ANGLE)/2;
        else if (el < -MIN_ANGLE)
        el = -MIN_ANGLE + (el+MIN_ANGLE)/2;

        Camera cam;
        cam.Clone(self->Cam());
        cam.Yaw(az);
        cam.Pitch(-el);

        result = Point(obj_t * cam.vrt(),
        obj_t * cam.vup(),
        obj_t * cam.vpn());
    }
    else {
        Camera& cam = (Camera&) self->Cam();   // cast away const

        result = Point(obj_t * cam.vrt(),
        obj_t * cam.vup(),
        obj_t * cam.vpn());
    }

    return result;
}

Point
SteerAI::AimTransform(const Point& pt)
{
    Camera& cam  = (Camera&) self->Cam();   // cast away const
    Point obj_t  = pt - self->Location();

    Point result = Point(obj_t * cam.vrt(),
    obj_t * cam.vup(),
    obj_t * cam.vpn());

    return result;
}

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

void
SteerAI::Navigator()
{
    accumulator.Clear();
    magnitude = 0;
}

int
SteerAI::Accumulate(const Steer& steer)
{
    int overflow = 0;

    double mag = steer.Magnitude();

    if (magnitude + mag > 1) {
        overflow = 1;
        double scale = (1 - magnitude) / mag;

        accumulator += steer * scale;
        magnitude   =  1;

        if (seeking) {
            az[0] *= scale;
            el[0] *= scale;
            seeking = 0;
        }
    }
    else {
        accumulator += steer;
        magnitude   += mag;
    }

    return overflow;
}

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

Steer
SteerAI::Seek(const Point& point)
{
    Steer s;

    // advance memory pipeline:
    az[2] = az[1]; az[1] = az[0];
    el[2] = el[1]; el[1] = el[0];

    // approach
    if (point.z > 0.0f) {
        az[0] = atan2(fabs(point.x), point.z) * seek_gain;
        el[0] = atan2(fabs(point.y), point.z) * seek_gain;

        if (point.x < 0) az[0] = -az[0];
        if (point.y > 0) el[0] = -el[0];

        s.yaw   = az[0] - seek_damp * (az[1] + az[2] * 0.5);
        s.pitch = el[0] - seek_damp * (el[1] + el[2] * 0.5);
    }

    // reverse
    else {
        if (point.x > 0) s.yaw = 1.0f;
        else             s.yaw = -1.0f;

        s.pitch = -point.y * 0.5f;
    }

    seeking = 1;

    return s;
}

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

Steer
SteerAI::Flee(const Point& pt)
{
    Steer s;

    Point point = pt;
    point.Normalize();

    // approach
    if (point.z > 0.0f) {
        if (point.x > 0) s.yaw = -1.0f;
        else             s.yaw =  1.0f;
    }

    // flee
    else {
        s.yaw   = -point.x;
        s.pitch =  point.y;
    }

    return s;
}

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

Steer
SteerAI::Avoid(const Point& point, float radius)
{
    Steer s;

    if (point.z > 0) {
        double ax = radius - fabs(point.x);
        double ay = radius - fabs(point.y);

        // go around?
        if (ax < ay) {
            s.yaw = atan2(ax, point.z) * seek_gain;
            if (point.x > 0) s.yaw = -s.yaw;
        }

        // go over/under:
        else {
            s.pitch = atan2(ay, point.z) * seek_gain;
            if (point.y < 0) s.pitch = -s.pitch;
        }
    }

    return s;
}

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

Steer
SteerAI::Evade(const Point& point, const Point& vel)
{
    Steer evade;

    if (Clock::GetInstance()->GameTime() - evade_time > 1250) {
        evade_time = Clock::GetInstance()->GameTime();

        int   direction = (rand()>>9) & 0x07;

        switch (direction) {
        default:
        case 0:  evade.yaw =  0; evade.pitch = -0.5; break;
        case 1:  evade.yaw =  0; evade.pitch = -1.0; break;
        case 2:  evade.yaw =  1; evade.pitch = -0.3; break;
        case 3:  evade.yaw =  1; evade.pitch = -0.6; break;
        case 4:  evade.yaw =  1; evade.pitch = -1.0; break;
        case 5:  evade.yaw = -1; evade.pitch = -0.3; break;
        case 6:  evade.yaw = -1; evade.pitch = -0.6; break;
        case 7:  evade.yaw = -1; evade.pitch = -1.0; break;
        }
    }

    return evade;
}