summaryrefslogtreecommitdiffhomepage
path: root/Stars45/CombatZone.cpp
blob: 3e2cac4d53a2d038738dafdce1f0c927df963afa (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
/*  Starshatter OpenSource Distribution
    Copyright (c) 1997-2004, Destroyer Studios LLC.
    All Rights Reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name "Destroyer Studios" nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    SUBSYSTEM:    Stars.exe
    FILE:         CombatZone.cpp
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    CombatZone is used by the dynamic campaign strategy
    and logistics algorithms to assign forces to locations
    within the campaign.  A CombatZone is a collection of
    closely related sectors, and the assets contained
    within them.
*/

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

#include "Game.h"
#include "DataLoader.h"
#include "ParseUtil.h"

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

CombatZone::CombatZone()
{
}

CombatZone::~CombatZone()
{
    regions.destroy();
    forces.destroy();
}

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

void
CombatZone::Clear()
{
    forces.destroy();
}

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

void
CombatZone::AddGroup(CombatGroup* group)
{
    if (group) {
        int iff = group->GetIFF();
        ZoneForce* f = FindForce(iff);
        f->AddGroup(group);
        group->SetCurrentZone(this);
    }
}

void
CombatZone::RemoveGroup(CombatGroup* group)
{
    if (group) {
        int iff = group->GetIFF();
        ZoneForce* f = FindForce(iff);
        f->RemoveGroup(group);
        group->SetCurrentZone(0);
    }
}

bool
CombatZone::HasGroup(CombatGroup* group)
{
    if (group) {
        int iff = group->GetIFF();
        ZoneForce* f = FindForce(iff);
        return f->HasGroup(group);
    }

    return false;
}

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

void
CombatZone::AddRegion(const char* rgn)
{
    if (rgn && *rgn) {
        regions.append(new (__FILE__,__LINE__) Text(rgn));

        if (name.length() < 1)
        name = rgn;
    }
}

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

bool
CombatZone::HasRegion(const char* rgn)
{
    if (rgn && *rgn && regions.size()) {
        Text test(rgn);
        return regions.contains(&test);
    }

    return false;
}

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

ZoneForce*
CombatZone::FindForce(int iff)
{
    ListIter<ZoneForce> f = forces;
    while (++f) {
        if (f->GetIFF() == iff)
        return f.value();
    }

    return MakeForce(iff);
}

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

ZoneForce*
CombatZone::MakeForce(int iff)
{
    ZoneForce* f = new(__FILE__,__LINE__) ZoneForce(iff);
    forces.append(f);
    return f;
}

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

static List<CombatZone> zonelist;

List<CombatZone>&
CombatZone::Load(const char* filename)
{
    zonelist.clear();

    DataLoader* loader   = DataLoader::GetLoader();
    BYTE*       block    = 0;

    loader->LoadBuffer(filename, block, true);
    Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block));

    Term*  term = parser.ParseTerm();

    if (!term) {
        return zonelist;
    }
    else {
        TermText* file_type = term->isText();
        if (!file_type || file_type->value() != "ZONES") {
            return zonelist;
        }
    }

    do {
        delete term; term = 0;
        term = parser.ParseTerm();
        
        if (term) {
            TermDef* def = term->isDef();
            if (def) {
                if (def->name()->value() == "zone") {
                    if (!def->term() || !def->term()->isStruct()) {
                        ::Print("WARNING: zone struct missing in '%s%s'\n", loader->GetDataPath(), filename);
                    }
                    else {
                        TermStruct* val = def->term()->isStruct();

                        CombatZone* zone = new(__FILE__,__LINE__) CombatZone();
                        char        rgn[64];
                        ZeroMemory(rgn,  sizeof(rgn));

                        for (int i = 0; i < val->elements()->size(); i++) {
                            TermDef* pdef = val->elements()->at(i)->isDef();
                            if (pdef) {
                                if (pdef->name()->value() == "region") {
                                    GetDefText(rgn, pdef, filename);
                                    zone->AddRegion(rgn);
                                }
                                else if (pdef->name()->value() == "system") {
                                    GetDefText(rgn, pdef, filename);
                                    zone->system = rgn;
                                }
                            }
                        }

                        zonelist.append(zone);
                    }
                }
            }
        }
    }
    while (term);

    loader->ReleaseBuffer(block);

    return zonelist;
}

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

ZoneForce::ZoneForce(int i)
{
    iff = i;

    for (int n = 0; n < 8; n++)
    need[n] = 0;
}

void
ZoneForce::AddGroup(CombatGroup* group)
{
    if (group)
    groups.append(group);
}

void
ZoneForce::RemoveGroup(CombatGroup* group)
{
    if (group)
    groups.remove(group);
}

bool
ZoneForce::HasGroup(CombatGroup* group)
{
    if (group)
    return groups.contains(group);

    return false;
}

int
ZoneForce::GetNeed(int group_type) const
{
    switch (group_type) {
    case CombatGroup::CARRIER_GROUP:       return need[0];
    case CombatGroup::BATTLE_GROUP:        return need[1];
    case CombatGroup::DESTROYER_SQUADRON:  return need[2];
    case CombatGroup::ATTACK_SQUADRON:     return need[3];
    case CombatGroup::FIGHTER_SQUADRON:    return need[4];
    case CombatGroup::INTERCEPT_SQUADRON:  return need[5];
    }

    return 0;
}

void
ZoneForce::SetNeed(int group_type, int needed)
{
    switch (group_type) {
    case CombatGroup::CARRIER_GROUP:       need[0] = needed; break;
    case CombatGroup::BATTLE_GROUP:        need[1] = needed; break;
    case CombatGroup::DESTROYER_SQUADRON:  need[2] = needed; break;
    case CombatGroup::ATTACK_SQUADRON:     need[3] = needed; break;
    case CombatGroup::FIGHTER_SQUADRON:    need[4] = needed; break;
    case CombatGroup::INTERCEPT_SQUADRON:  need[5] = needed; break;
    }
}

void
ZoneForce::AddNeed(int group_type, int needed)
{
    switch (group_type) {
    case CombatGroup::CARRIER_GROUP:       need[0] += needed; break;
    case CombatGroup::BATTLE_GROUP:        need[1] += needed; break;
    case CombatGroup::DESTROYER_SQUADRON:  need[2] += needed; break;
    case CombatGroup::ATTACK_SQUADRON:     need[3] += needed; break;
    case CombatGroup::FIGHTER_SQUADRON:    need[4] += needed; break;
    case CombatGroup::INTERCEPT_SQUADRON:  need[5] += needed; break;
    }
}