summaryrefslogtreecommitdiffhomepage
path: root/Stars45/CampaignPlanMovement.cpp
blob: a66f2021a19cbde05990045ae4e28bf749667b0a (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
/*  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
    ========
    CampaignPlanMovement simulates random patrol movements
    of starship groups between missions.
*/

#include "MemDebug.h"
#include "CampaignPlanMovement.h"
#include "Campaign.h"
#include "Combatant.h"
#include "CombatGroup.h"
#include "CombatUnit.h"
#include "CombatZone.h"
#include "Random.h"
#include "ShipDesign.h"

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

void
CampaignPlanMovement::ExecFrame()
{
    if (campaign && campaign->IsActive()) {
        if (Campaign::Stardate() - exec_time < 7200)
        return;

        campaign->GetAllCombatUnits(-1, all_units);

        ListIter<CombatUnit> iter = all_units;
        while (++iter) {
            CombatUnit* u = iter.value();

            if (u->IsStarship() && !u->IsStatic())
            MoveUnit(u);
        }

        all_units.clear();

        exec_time = Campaign::Stardate();
    }
}

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

void
CampaignPlanMovement::MoveUnit(CombatUnit* u)
{
    if (u) {
        // starship repair:
        double damage  = u->GetSustainedDamage();

        if (damage > 0 && u->GetDesign()) {
            int    percent = (int) (100 * damage / u->GetDesign()->integrity);

            if (percent > 50) {
                u->SetSustainedDamage(0.90 * damage);
            }
        }

        Point  loc  = u->Location();
        Point  dir  = loc;
        double dist = dir.Normalize();

        const double MAX_RAD  = 320e3;
        const double MIN_DIST = 150e3;

        if (dist < MAX_RAD) {
            double scale = 1 - dist/MAX_RAD;

            loc += dir * (Random(30e3, 90e3) * scale) + RandomDirection() * 10e3;

            if (fabs(loc.z) > 20e3)
            loc.z *= 0.1;

            u->MoveTo(loc);

            CombatGroup* g = u->GetCombatGroup();
            if (g && g->Type() > CombatGroup::FLEET && g->GetFirstUnit() == u) {
                g->MoveTo(loc);

                if (g->IntelLevel() > Intel::KNOWN)
                g->SetIntelLevel(Intel::KNOWN);
            }
        }

        else if (dist > 1.25 * MAX_RAD) {
            double scale = 1 - dist/MAX_RAD;

            loc += dir * (Random(80e3, 120e3) * scale) + RandomDirection() * 3e3;

            if (fabs(loc.z) > 20e3)
            loc.z *= 0.1;

            u->MoveTo(loc);

            CombatGroup* g = u->GetCombatGroup();
            if (g && g->Type() > CombatGroup::FLEET && g->GetFirstUnit() == u) {
                g->MoveTo(loc);

                if (g->IntelLevel() > Intel::KNOWN)
                g->SetIntelLevel(Intel::KNOWN);
            }
        }

        else {
            loc += RandomDirection() * 30e3;

            if (fabs(loc.z) > 20e3)
            loc.z *= 0.1;

            u->MoveTo(loc);

            CombatGroup* g = u->GetCombatGroup();
            if (g && g->Type() > CombatGroup::FLEET && g->GetFirstUnit() == u) {
                g->MoveTo(loc);

                if (g->IntelLevel() > Intel::KNOWN)
                g->SetIntelLevel(Intel::KNOWN);
            }
        }

        CombatUnit* closest_unit = 0;
        double      closest_dist = 1e6;

        ListIter<CombatUnit> iter = all_units;
        while (++iter) {
            CombatUnit* unit = iter.value();

            if (unit->GetCombatGroup() != u->GetCombatGroup() && unit->GetRegion() == u->GetRegion() && !unit->IsDropship()) {
                Point  delta = loc - unit->Location();
                dist  = delta.Normalize();

                if (dist < closest_dist) {
                    closest_unit = unit;
                    closest_dist = dist;
                }
            }
        }

        if (closest_unit && closest_dist < MIN_DIST) {
            Point  delta = loc - closest_unit->Location();
            dist  = delta.Normalize();

            loc += delta * 1.1 * (MIN_DIST - closest_dist);

            if (fabs(loc.z) > 20e3)
            loc.z *= 0.1;

            u->MoveTo(loc);

            CombatGroup* g = u->GetCombatGroup();
            if (g && g->Type() > CombatGroup::FLEET && g->GetFirstUnit() == u) {
                g->MoveTo(loc);

                if (g->IntelLevel() > Intel::KNOWN)
                g->SetIntelLevel(Intel::KNOWN);
            }
        }
    }
}