summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/SeekerAI.cpp
blob: 148fb307d2391fa3d88a69aab7e54e9154f480e0 (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
/*  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
    ========
    Seeker Missile (low-level) Artificial Intelligence class
*/

#include "SeekerAI.h"
#include "Ship.h"
#include "Shot.h"
#include "System.h"
#include "WeaponDesign.h"

#include "Clock.h"

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

SeekerAI::SeekerAI(SimObject* s)
    : SteerAI(s), shot((Shot*) s), orig_target(0),
      pursuit(1), delay(0), overshot(false)
{
    ai_type = SEEKER;

    seek_gain = 25;
    seek_damp = 0.55;
}


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

SeekerAI::~SeekerAI()
{
    if (shot) {
        if (shot->Owner())
        ((Ship*) shot->Owner())->SetMissileEta(shot->Identity(), 0);
    }
}

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

void
SeekerAI::ExecFrame(double seconds)
{
    // setup:
    FindObjective();

    // adaptive behavior:
    Navigator();
}

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

void
SeekerAI::Navigator()
{
    if (delay > 0) {
        delay -= Clock::GetInstance()->Delta();
    }
    else {
        Steer s = SeekTarget();
        self->ApplyYaw((float) s.yaw);
        self->ApplyPitch((float) s.pitch);
    }
}

void
SeekerAI::SetTarget(SimObject* targ, System* sub)
{
    if (!orig_target && targ && targ->Type() == SimObject::SIM_SHIP) {
        orig_target = (Ship*) targ;
        Observe(orig_target);
    }

    SteerAI::SetTarget(targ, sub);

    if (!target) {
        shot->SetEta(0);

        if (shot->Owner())
        ((Ship*) shot->Owner())->SetMissileEta(shot->Identity(), 0);
    }
}

void
SeekerAI::FindObjective()
{
    if (!shot || !target) return;

    if (target->Life() == 0) {
        if (target != orig_target)
        SetTarget(orig_target,0);
        else
        SetTarget(0,0);

        return;
    }

    Point tloc = target->Location();
    tloc = Transform(tloc);

    // seeker head limit of 45 degrees:
    if (tloc.z < 0 || tloc.z < fabs(tloc.x) || tloc.z < fabs(tloc.y)) {
        overshot = true;
        SetTarget(0,0);
        return;
    }

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

    // are we being spoofed?
    CheckDecoys(distance);

    Point cv = ClosingVelocity();

    // time to reach target:
    double time    = distance / cv.length();
    double predict = time;
    if (predict > 15)
    predict = 15;

    // pure pursuit:
    if (pursuit == 1 || time < 0.1) {
        obj_w = target->Location();
    }

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

    // subsystem offset:
    if (subtarget) {
        Point offset = target->Location() - subtarget->MountLocation();
        obj_w -= offset;
    }
    else if (target->Type() == SimObject::SIM_SHIP) {
        Ship* tgt_ship = (Ship*) target;

        if (tgt_ship->IsGroundUnit())
        obj_w += Point(0,150,0);
    }


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

    // where we will be when the target gets there:
    if (predict > 0.1 && predict < 15) {
        Point self_dest = self->Location() + cv * predict;
        Point err = obj_w - self_dest;

        obj_w += err;
    }

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

    shot->SetEta((int) time);

    if (shot->Owner())
    ((Ship*) shot->Owner())->SetMissileEta(shot->Identity(), (int) time);
}

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

void
SeekerAI::CheckDecoys(double target_distance)
{
    // if the assigned target has the burner lit,
    // ignore the decoys:
    if (orig_target && orig_target->Augmenter()) {
        SetTarget(orig_target);
        return;
    }

    if (target &&
            target == orig_target &&
            orig_target->GetActiveDecoys().size()) {

        ListIter<Shot> decoy = orig_target->GetActiveDecoys();

        while (++decoy) {
            double decoy_distance = Point(decoy->Location() - self->Location()).length();

            if (decoy_distance < target_distance) {
                if (rand() < 1600) {
                    SetTarget(decoy.value(), 0);
                    return;
                }
            }
        }
    }
}

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

bool
SeekerAI::Overshot()
{
    return overshot;
}

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

Steer
SeekerAI::AvoidCollision()
{
    return Steer();
}

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

Steer
SeekerAI::SeekTarget()
{
    if (!self || !target || Overshot())
    return Steer();

    return Seek(objective);
}

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

bool
SeekerAI::Update(SimObject* obj)
{
    if (obj == target) {
        if (obj->Type() == SimObject::SIM_SHOT && orig_target != 0)
        target = orig_target;
    }

    if (obj == orig_target)
    orig_target = 0;

    return SteerAI::Update(obj);
}

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