summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/SimEvent.cpp
blob: 42ce2400863594d221fc1e4cadd93fd02496caaa (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
/*  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
    ========
    Simulation Events for mission summary
*/

#include "SimEvent.h"

#include "Clock.h"
#include "ContentBundle.h"
#include "Ship.h"
#include "Sim.h"

// +====================================================================+

List<ShipStats>   records;

// +====================================================================+

SimEvent::SimEvent(int e, const char* t, const char* i)
: event(e), count(0)
{
    Sim* sim = Sim::GetSim();
    if (sim) {
        time = (int) sim->MissionClock();
    }
    else {
        time = (int) (Clock::GetInstance()->GameTime()/1000);
    }

    SetTarget(t);
    SetInfo(i);
}

SimEvent::~SimEvent()
{
}

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

void
SimEvent::SetTime(int t)
{
    time = t;
}

void
SimEvent::SetTarget(const char* t)
{
    if (t && t[0])
    target = t;
}

void
SimEvent::SetInfo(const char* i)
{
    if (i && i[0])
    info = i;
}

void
SimEvent::SetCount(int c)
{
    count = c;
}

Text
SimEvent::GetEventDesc() const
{
    switch (event) {
    case LAUNCH:         return ContentBundle::GetInstance()->GetText("sim.event.Launch");
    case DOCK:           return ContentBundle::GetInstance()->GetText("sim.event.Dock");
    case LAND:           return ContentBundle::GetInstance()->GetText("sim.event.Land");
    case EJECT:          return ContentBundle::GetInstance()->GetText("sim.event.Eject");
    case CRASH:          return ContentBundle::GetInstance()->GetText("sim.event.Crash");
    case COLLIDE:        return ContentBundle::GetInstance()->GetText("sim.event.Collision With");
    case DESTROYED:      return ContentBundle::GetInstance()->GetText("sim.event.Destroyed By");
    case MAKE_ORBIT:     return ContentBundle::GetInstance()->GetText("sim.event.Make Orbit");
    case BREAK_ORBIT:    return ContentBundle::GetInstance()->GetText("sim.event.Break Orbit");
    case QUANTUM_JUMP:   return ContentBundle::GetInstance()->GetText("sim.event.Quantum Jump");
    case LAUNCH_SHIP:    return ContentBundle::GetInstance()->GetText("sim.event.Launch Ship");
    case RECOVER_SHIP:   return ContentBundle::GetInstance()->GetText("sim.event.Recover Ship");
    case FIRE_GUNS:      return ContentBundle::GetInstance()->GetText("sim.event.Fire Guns");
    case FIRE_MISSILE:   return ContentBundle::GetInstance()->GetText("sim.event.Fire Missile");
    case DROP_DECOY:     return ContentBundle::GetInstance()->GetText("sim.event.Drop Decoy");
    case GUNS_KILL:      return ContentBundle::GetInstance()->GetText("sim.event.Guns Kill");
    case MISSILE_KILL:   return ContentBundle::GetInstance()->GetText("sim.event.Missile Kill");
    case LAUNCH_PROBE:   return ContentBundle::GetInstance()->GetText("sim.event.Launch Probe");
    case SCAN_TARGET:    return ContentBundle::GetInstance()->GetText("sim.event.Scan Target");
    default:             return ContentBundle::GetInstance()->GetText("sim.event.no event");
    }
}

// +====================================================================+

ShipStats::ShipStats(const char* n, int i)
: name(n), iff(i), kill1(0), kill2(0), lost(0), coll(0), points(0),
cmd_points(0), gun_shots(0), gun_hits(0), missile_shots(0), missile_hits(0),
combat_group(0), combat_unit(0), player(false), ship_class(0), elem_index(-1)
{
    if (!n || !n[0])
    name = ContentBundle::GetInstance()->GetText("[unknown]");
}

ShipStats::~ShipStats()
{
    events.destroy();
}

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

void
ShipStats::SetType(const char* t)
{
    if (t && t[0])
    type = t;
}

void
ShipStats::SetRole(const char* r)
{
    if (r && r[0])
    role = r;
}

void
ShipStats::SetRegion(const char* r)
{
    if (r && r[0])
    region = r;
}

void
ShipStats::SetCombatGroup(CombatGroup* g)
{
    combat_group = g;
}

void
ShipStats::SetCombatUnit(CombatUnit* u)
{
    combat_unit = u;
}

void
ShipStats::SetElementIndex(int n)
{
    elem_index = n;
}

void
ShipStats::SetPlayer(bool p)
{
    player = p;
}

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

void
ShipStats::Summarize()
{
    kill1 = 0;
    kill2 = 0;
    lost  = 0;
    coll  = 0;

    ListIter<SimEvent> iter = events;
    while (++iter) {
        SimEvent* event = iter.value();
        int       code  = event->GetEvent();

        if (code == SimEvent::GUNS_KILL)
        kill1++;

        else if (code == SimEvent::MISSILE_KILL)
        kill2++;

        else if (code == SimEvent::DESTROYED)
        lost++;

        else if (code == SimEvent::CRASH)
        coll++;

        else if (code == SimEvent::COLLIDE)
        coll++;
    }
}


long double
ShipStats::FlightTime() const
{
    return flight_time;
}


void
ShipStats::SetFlightTime(long double seconds)
{
    flight_time = seconds;
}


static int
FlightTimeBonus(const ShipStats* stats)
{
    static constexpr long double TEN_MINUTES {600.0l};
    return static_cast<int>(stats->FlightTime() / TEN_MINUTES) * 10;
}


static int
DockBonus(const ShipStats* stats)
{
    return stats->HasEvent(SimEvent::DOCK) ? 100 : 0;
}


static int
LossPenalty(const ShipStats* stats)
{
    return 2.5 * Ship::Value(stats->GetShipClass());
}


int
ShipStats::EffectivePoints() const
{
    const auto whole {GetPoints() + GetCommandPoints()};
    if (GetDeaths() > 0 || GetColls() > 0)
        return std::max(0, whole - LossPenalty(this));
    return whole + FlightTimeBonus(this) + DockBonus(this);
}

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

SimEvent*
ShipStats::AddEvent(SimEvent* e)
{
    events.append(e);
    return e;
}

SimEvent*
ShipStats::AddEvent(int event, const char* tgt, const char* info)
{
    SimEvent* e = new SimEvent(event, tgt, info);
    events.append(e);
    return e;
}

bool
ShipStats::HasEvent(int event) const
{
    for (int i = 0; i < events.size(); i++)
    if (events[i]->GetEvent() == event)
    return true;

    return false;
}

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

void ShipStats::Initialize()  { records.destroy(); }
void ShipStats::Close()       { records.destroy(); }

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

int
ShipStats::NumStats()
{
    return records.size();
}

ShipStats*
ShipStats::GetStats(int i)
{
    if (i >= 0 && i < records.size())
    return records.at(i);

    return 0;
}

ShipStats*
ShipStats::Find(const char* name)
{
    if (name && name[0]) {
        ListIter<ShipStats> iter = records;
        while (++iter) {
            ShipStats* stats = iter.value();
            if (!strcmp(stats->GetName(), name))
            return stats;
        }

        ShipStats* stats = new ShipStats(name);
        records.append(stats);
        return stats;
    }

    return 0;
}