summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/CombatAction.cpp
blob: c4ed9d0fb05cf5fc3b069dde504deb203a9f7dd3 (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
/*  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
    ========
    A significant (newsworthy) event in the dynamic campaign.
*/

#include "CombatAction.h"
#include "CombatGroup.h"
#include "Campaign.h"
#include "Combatant.h"
#include "Player.h"
#include "Random.h"

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

CombatAction::CombatAction(int n, int typ, int sub, int iff)
    : id(n), type(typ), subtype(sub), opp_type(-1), team(iff),
      status(PENDING), count(0), rval(-1), source(0), time(0),
      start_before((int) 1e9), start_after(0),
      min_rank(0), max_rank(100),
      delay(0), probability(100), asset_type(0), target_type(0)
{ }

CombatAction::~CombatAction()
{
    requirements.destroy();
    asset_kills.destroy();
    target_kills.destroy();
}

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

bool
CombatAction::IsAvailable() const
{
    CombatAction* pThis = (CombatAction*) this;

    if (rval < 0) {
        pThis->rval = (int) Random(0, 100);

        if (rval > probability)
        pThis->status = SKIPPED;
    }

    if (status != PENDING)
    return false;

    if (min_rank > 0 || max_rank < 100) {
        Player* player = Player::GetCurrentPlayer();

        if (player->Rank() < min_rank || player->Rank() > max_rank)
        return false;
    }

    Campaign* campaign = Campaign::GetCampaign();
    if (campaign) {
        if (campaign->GetTime() < start_after) {
            return false;
        }

        if (campaign->GetTime() > start_before) {
            pThis->status = FAILED; // too late!
            return false;
        }

        // check requirements against actions in current campaign:
        ListIter<CombatActionReq> iter = pThis->requirements;
        while (++iter) {
            CombatActionReq* r  = iter.value();
            bool             ok = false;

            if (r->action > 0) {
                ListIter<CombatAction> action = campaign->GetActions();
                while (++action) {
                    CombatAction* a = action.value();

                    if (a->Identity() == r->action) {
                        if (r->_not) {
                            if (a->Status() == r->stat)
                            return false;
                        }
                        else {
                            if (a->Status() != r->stat)
                            return false;
                        }
                    }
                }
            }

            // group-based requirement
            else if (r->group_type > 0) {
                if (r->c1) {
                    CombatGroup* group = r->c1->FindGroup(r->group_type, r->group_id);

                    if (group) {
                        int test = 0;
                        int comp = 0;

                        if (r->intel) {
                            test = group->IntelLevel();
                            comp = r->intel;
                        }

                        else {
                            test = group->CalcValue();
                            comp = r->score;
                        }

                        switch (r->comp) {
                        case CombatActionReq::LT:  ok = (test <  comp); break;
                        case CombatActionReq::LE:  ok = (test <= comp); break;
                        case CombatActionReq::GT:  ok = (test >  comp); break;
                        case CombatActionReq::GE:  ok = (test >= comp); break;
                        case CombatActionReq::EQ:  ok = (test == comp); break;
                        }
                    }

                    if (!ok)
                    return false;
                }
            }

            // score-based requirement
            else {
                int test = 0;

                if (r->comp <= CombatActionReq::EQ) {  // absolute
                    if (r->c1) {
                        int test = r->c1->Score();

                        switch (r->comp) {
                        case CombatActionReq::LT:  ok = (test <  r->score); break;
                        case CombatActionReq::LE:  ok = (test <= r->score); break;
                        case CombatActionReq::GT:  ok = (test >  r->score); break;
                        case CombatActionReq::GE:  ok = (test >= r->score); break;
                        case CombatActionReq::EQ:  ok = (test == r->score); break;
                        }
                    }
                }

                else {                                 // relative
                    if (r->c1 && r->c2) {
                        int test = r->c1->Score() - r->c2->Score();

                        switch (r->comp) {
                        case CombatActionReq::RLT: ok = (test <  r->score); break;
                        case CombatActionReq::RLE: ok = (test <= r->score); break;
                        case CombatActionReq::RGT: ok = (test >  r->score); break;
                        case CombatActionReq::RGE: ok = (test >= r->score); break;
                        case CombatActionReq::REQ: ok = (test == r->score); break;
                        }
                    }
                }

                if (!ok)
                return false;
            }

            if (delay > 0) {
                pThis->start_after = (int) campaign->GetTime() + delay;
                pThis->delay       = 0;
                return IsAvailable();
            }
        }
    }

    return true;
}

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

void
CombatAction::FireAction()
{
    Campaign* campaign = Campaign::GetCampaign();
    if (campaign)
    time = (int) campaign->GetTime();

    if (count >= 1)
    count--;

    if (count < 1)
    status = COMPLETE;
}

void
CombatAction::FailAction()
{
    Campaign* campaign = Campaign::GetCampaign();
    if (campaign)
    time = (int) campaign->GetTime();

    count  = 0;
    status = FAILED;
}

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

void
CombatAction::AddRequirement(int action, int stat, bool _not)
{
    requirements.append(new CombatActionReq(action, stat, _not));
}

void
CombatAction::AddRequirement(Combatant* c1, Combatant* c2, int comp, int score)
{
    requirements.append(new CombatActionReq(c1, c2, comp, score));
}

void
CombatAction::AddRequirement(Combatant* c1, int group_type, int group_id, int comp, int score, int intel)
{
    requirements.append(new CombatActionReq(c1, group_type, group_id, comp, score, intel));
}

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

int
CombatAction::TypeFromName(const char* n)
{
    int type = 0;

    if (!_stricmp(n, "NO_ACTION"))
        type = NO_ACTION;

    else if (!_stricmp(n, "MARKER"))
        type = NO_ACTION;

    else if (!_stricmp(n, "STRATEGIC_DIRECTIVE"))
        type = STRATEGIC_DIRECTIVE;

    else if (!_stricmp(n, "STRATEGIC"))
        type = STRATEGIC_DIRECTIVE;

    else if (!_stricmp(n, "ZONE_ASSIGNMENT"))
        type = ZONE_ASSIGNMENT;

    else if (!_stricmp(n, "ZONE"))
        type = ZONE_ASSIGNMENT;

    else if (!_stricmp(n, "SYSTEM_ASSIGNMENT"))
        type = SYSTEM_ASSIGNMENT;

    else if (!_stricmp(n, "SYSTEM"))
        type = SYSTEM_ASSIGNMENT;

    else if (!_stricmp(n, "MISSION_TEMPLATE"))
        type = MISSION_TEMPLATE;

    else if (!_stricmp(n, "MISSION"))
        type = MISSION_TEMPLATE;

    else if (!_stricmp(n, "COMBAT_EVENT"))
        type = COMBAT_EVENT;

    else if (!_stricmp(n, "EVENT"))
        type = COMBAT_EVENT;

    else if (!_stricmp(n, "INTEL_EVENT"))
        type = INTEL_EVENT;

    else if (!_stricmp(n, "INTEL"))
        type = INTEL_EVENT;

    else if (!_stricmp(n, "CAMPAIGN_SITUATION"))
        type = CAMPAIGN_SITUATION;

    else if (!_stricmp(n, "SITREP"))
        type = CAMPAIGN_SITUATION;

    else if (!_stricmp(n, "CAMPAIGN_ORDERS"))
        type = CAMPAIGN_ORDERS;

    else if (!_stricmp(n, "ORDERS"))
        type = CAMPAIGN_ORDERS;

    return type;
}

int
CombatAction::StatusFromName(const char* n)
{
    int stat = 0;

    if (!_stricmp(n, "PENDING"))
    stat = PENDING;

    else if (!_stricmp(n, "ACTIVE"))
    stat = ACTIVE;

    else if (!_stricmp(n, "SKIPPED"))
    stat = SKIPPED;

    else if (!_stricmp(n, "FAILED"))
    stat = FAILED;

    else if (!_stricmp(n, "COMPLETE"))
    stat = COMPLETE;

    return stat;
}


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

int
CombatActionReq::CompFromName(const char* n)
{
    int comp = 0;

    if (!_stricmp(n, "LT"))
    comp = LT;

    else if (!_stricmp(n, "LE"))
    comp = LE;

    else if (!_stricmp(n, "GT"))
    comp = GT;

    else if (!_stricmp(n, "GE"))
    comp = GE;

    else if (!_stricmp(n, "EQ"))
    comp = EQ;

    else if (!_stricmp(n, "RLT"))
    comp = RLT;

    else if (!_stricmp(n, "RLE"))
    comp = RLE;

    else if (!_stricmp(n, "RGT"))
    comp = RGT;

    else if (!_stricmp(n, "RGE"))
    comp = RGE;

    else if (!_stricmp(n, "REQ"))
    comp = REQ;

    return comp;
}