summaryrefslogtreecommitdiffhomepage
path: root/Stars45/CombatUnit.cpp
blob: 7d6d3dfb30adfe111d4aec0ef0ef10d40da2382f (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
/*  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
    ========
    A ship, station, or ground unit in the dynamic campaign.
*/

#include "MemDebug.h"
#include "CombatUnit.h"
#include "CombatGroup.h"
#include "Campaign.h"
#include "ShipDesign.h"
#include "Ship.h"

#include "Game.h"
#include "ContentBundle.h"

// +----------------------------------------------------------------------+
#undef random
inline double random() { return (double) rand() / (double) RAND_MAX; }

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

CombatUnit::CombatUnit(const char* n, const char* reg, int t, const char* d, int c, int i)
: name(n), regnum(reg), type(t), design_name(d), design(0),
count(c), iff(i), leader(false), dead_count(0), available(c),
carrier(0), plan_value(0), launch_time(-1e6), jump_time(0),
sustained_damage(0), target(0), group(0), heading(0)
{ }

CombatUnit::CombatUnit(const CombatUnit& u)
: name(u.name), regnum(u.regnum), type(u.type), design_name(u.design_name),
design(u.design), count(u.count), iff(u.iff),
dead_count(u.dead_count), available(u.available),
leader(u.leader), region(u.region), location(u.location),
carrier(u.carrier), plan_value(0), launch_time(u.launch_time),
jump_time(u.jump_time), sustained_damage(u.sustained_damage),
target(0), group(0), heading(u.heading)
{ }

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

const ShipDesign*
CombatUnit::GetDesign()
{
    if (!design)
    design = ShipDesign::Get(design_name);

    return design;
}

int
CombatUnit::GetShipClass() const
{
    if (design)
    return design->type;

    return type;
}

int
CombatUnit::GetValue() const
{
    return GetSingleValue() * LiveCount();
}

int
CombatUnit::GetSingleValue() const
{
    return Ship::Value(GetShipClass());
}

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

const char*
CombatUnit::GetDescription() const
{
    if (!design) {
        CombatUnit* pThis = (CombatUnit*) this; // cast-away const
        pThis->GetDesign();
    }

    static char desc[256];

    if (!design) {
        strcpy_s(desc, ContentBundle::GetInstance()->GetText("[unknown]").data());
    }

    else if (count > 1) {
        sprintf_s(desc, "%dx %s %s", LiveCount(), design->abrv, design->DisplayName());
    }

    else {
        if (regnum.length() > 0)
        sprintf_s(desc, "%s-%s %s", design->abrv, (const char*) regnum, (const char*) name);
        else
        sprintf_s(desc, "%s %s", design->abrv, (const char*) name);

        if (dead_count > 0) {
            strcat_s(desc, " ");
            strcat_s(desc, ContentBundle::GetInstance()->GetText("killed.in.action"));
        }
    }

    return desc;
}

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

bool
CombatUnit::CanLaunch() const
{
    bool result = false;

    switch (type) {
    case Ship::FIGHTER:
    case Ship::ATTACK:   result = (Campaign::Stardate() - launch_time) >= 300;
        break;

    case Ship::CORVETTE:
    case Ship::FRIGATE:
    case Ship::DESTROYER:
    case Ship::CRUISER:
    case Ship::CARRIER:  result = true;
        break;
    }

    return result;
}

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

Color
CombatUnit::MarkerColor() const
{
    return Ship::IFFColor(iff);
}

bool
CombatUnit::IsGroundUnit() const
{
    return (design && (design->type & Ship::GROUND_UNITS)) ? true : false;
}

bool
CombatUnit::IsStarship() const
{
    return (design && (design->type & Ship::STARSHIPS)) ? true : false;
}

bool
CombatUnit::IsDropship() const
{
    return (design && (design->type & Ship::DROPSHIPS)) ? true : false;
}

bool
CombatUnit::IsStatic() const
{
    return design && (design->type >= Ship::STATION);
}

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

double
CombatUnit::MaxRange() const
{
    return 100e3;
}

double CombatUnit::MaxEffectiveRange() const
{
    return 50e3;
}

double CombatUnit::OptimumRange() const
{
    if (type == Ship::FIGHTER || type == Ship::ATTACK)
    return 15e3;

    return 30e3;
}

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

bool CombatUnit::CanDefend(CombatUnit* unit) const
{
    if (unit == 0 || unit == this)
    return false;

    if (type > Ship::STATION)
    return false;

    double distance = (location - unit->location).length();

    if (type > unit->type)
    return false;

    if (distance > MaxRange())
    return false;

    return true;
}

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

double CombatUnit::PowerVersus(CombatUnit* tgt) const
{
    if (tgt == 0 || tgt == this || available < 1)
    return 0;

    if (type > Ship::STATION)
    return 0;

    double effectiveness = 1;
    double distance = (location - tgt->location).length();

    if (distance > MaxRange())
    return 0;

    if (distance > MaxEffectiveRange())
    effectiveness = 0.5;

    if (type == Ship::FIGHTER) {
        if (tgt->type == Ship::FIGHTER || tgt->type == Ship::ATTACK)
        return Ship::FIGHTER * 2 * available * effectiveness;
        else
        return 0;
    }
    else if (type == Ship::ATTACK) {
        if (tgt->type > Ship::ATTACK)
        return Ship::ATTACK * 3 * available * effectiveness;
        else
        return 0;
    }
    else if (type == Ship::CARRIER) {
        return 0;
    }
    else if (type == Ship::SWACS) {
        return 0;
    }
    else if (type == Ship::CRUISER) {
        if (tgt->type <= Ship::ATTACK)
        return type * effectiveness;

        else
        return 0;
    }
    else {
        if (tgt->type > Ship::ATTACK)
        return type * effectiveness;
        else
        return type * 0.1 * effectiveness;
    }

    return 0;
}

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

int
CombatUnit::AssignMission()
{
    int assign = count;

    if (count > 4)
    assign = 4;

    if (assign > 0) {
        available -= assign;
        launch_time = Campaign::Stardate();
        return assign;
    }

    return 0;
}

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

void
CombatUnit::CompleteMission()
{
    Disengage();

    if (count > 4)
    available += 4;

    else
    available += count;
}

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

void
CombatUnit::MoveTo(const Point& loc)
{
    if (!carrier)
    location = loc;
    else
    location = carrier->location;
}

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

void
CombatUnit::Engage(CombatUnit* tgt)
{
    if (!tgt)
    Disengage();

    else if (!tgt->attackers.contains(this))
    tgt->attackers.append(this);

    target = tgt;
}

void
CombatUnit::Disengage()
{
    if (target)
    target->attackers.remove(this);

    target = 0;
}

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

static int KillGroup(CombatGroup* group)
{
    int value_killed = 0;

    if (group) {
        ListIter<CombatUnit>  u_iter = group->GetUnits();
        while (++u_iter) {
            CombatUnit* u = u_iter.value();
            value_killed += u->Kill(u->LiveCount());
        }

        ListIter<CombatGroup> g_iter = group->GetComponents();
        while (++g_iter) {
            CombatGroup* g = g_iter.value();
            value_killed += KillGroup(g);
        }
    }

    return value_killed;
}

int
CombatUnit::Kill(int n)
{
    int killed = n;

    if (killed > LiveCount())
    killed = LiveCount();

    dead_count += killed;

    int value_killed = killed * GetSingleValue();

    if (killed) {
        // if unit could support children, kill them too:
        if (type == Ship::CARRIER ||
                type == Ship::STATION ||
                type == Ship::STARBASE) {

            if (group) {
                ListIter<CombatGroup> iter = group->GetComponents();
                while (++iter) {
                    CombatGroup* g = iter.value();
                    value_killed += KillGroup(g);
                }
            }
        }
    }

    return value_killed;
}