summaryrefslogtreecommitdiffhomepage
path: root/Stars45/DropShipAI.cpp
blob: 8411662d1c81ab6587b4c71ba4698bd8b2d0c270 (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
/*  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
    ========
    Drop Ship (orbit/surface and surface/orbit) AI class
*/

#include "MemDebug.h"
#include "DropShipAI.h"
#include "TacticalAI.h"
#include "Ship.h"
#include "ShipCtrl.h"
#include "Drive.h"
#include "Sim.h"
#include "StarSystem.h"
#include "KeyMap.h"

#include "Game.h"

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

DropShipAI::DropShipAI(Ship* s)
    : ShipAI(s)
{
    seek_gain = 20;
    seek_damp = 0.5;

    delete tactical;
    tactical = 0;
}

DropShipAI::~DropShipAI()
{
}

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

void
DropShipAI::FindObjective()
{
    distance = 0;

    if (!ship)  return;

    Sim*        sim = Sim::GetSim();
    SimRegion*  self_rgn = ship->GetRegion();

    // if making orbit, go up:
    if (self_rgn->Type() == Sim::AIR_SPACE) {
        obj_w = self->Location() + Point(0, 1e3, 0);
    }

    // if breaking orbit, head for terrain region:
    else {
        SimRegion* dst_rgn = sim->FindNearestTerrainRegion(ship);
        Point      dst     = dst_rgn->GetOrbitalRegion()->Location() -
        self_rgn->GetOrbitalRegion()->Location() +
        Point(0, 0, -1e6);

        obj_w =    dst.OtherHand();
    }

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

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

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

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

    if (other)
    ship->SetFLCSMode(Ship::FLCS_AUTO);
    else
    ship->SetFLCSMode(Ship::FLCS_MANUAL);

    Accumulate(AvoidCollision());
    Accumulate(Seek(objective));

    // are we being asked to flee?
    if (fabs(accumulator.yaw) == 1.0 && accumulator.pitch == 0.0) {
        accumulator.pitch  = -0.7f;
        accumulator.yaw   *= 0.25f;
    }

    self->ApplyRoll((float) (accumulator.yaw * -0.4));
    self->ApplyYaw((float) (accumulator.yaw * 0.2));

    if (fabs(accumulator.yaw) > 0.5 && fabs(accumulator.pitch) < 0.1)
    accumulator.pitch -= 0.1f;

    if (accumulator.pitch != 0)
    self->ApplyPitch((float) accumulator.pitch);

    // if not turning, roll to orient with world coords:
    if (fabs(accumulator.yaw) < 0.1) {
        Point vrt = ((Camera*) &(self->Cam()))->vrt();
        double deflection = vrt.y;
        if (deflection != 0) {
            double theta = asin(deflection/vrt.length());
            self->ApplyRoll(-theta);
        }
    }

    ship->SetThrottle(100);
    ship->ExecFLCSFrame();
}