summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Galaxy.cpp
blob: 1b5bb6f1be599843c4eccdc4822b72b8a80e62d2 (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
/*  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
    ========
    Galaxy (list of star systems) for a single campaign.
*/

#include "Galaxy.h"
#include "StarSystem.h"
#include "Starshatter.h"

#include "Game.h"
#include "Solid.h"
#include "Sprite.h"
#include "Light.h"
#include "Bitmap.h"
#include "DataLoader.h"
#include "ParseUtil.h"

static Galaxy* galaxy = 0;

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

Galaxy::Galaxy(const char* n)
    : name(n), radius(10)
{ }

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

Galaxy::~Galaxy()
{
    Print("  Destroying Galaxy %s\n", (const char*) name);
    systems.destroy();
    stars.destroy();
}

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

void
Galaxy::Initialize()
{
    if (galaxy) delete galaxy;
    galaxy = new Galaxy("Galaxy");
    galaxy->Load();
}

void
Galaxy::Close()
{
    delete galaxy;
    galaxy = 0;
}

Galaxy*
Galaxy::GetInstance()
{
    return galaxy;
}

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

void
Galaxy::Load()
{
    DataLoader* loader = DataLoader::GetLoader();
    loader->SetDataPath("Galaxy/");
    sprintf_s(filename, "%s.def", (const char*) name);
    Load(filename);

    // load mod galaxies:
    List<Text> mod_galaxies;
    loader->SetDataPath("Mods/Galaxy/");
    loader->ListFiles("*.def", mod_galaxies);

    ListIter<Text> iter = mod_galaxies;
    while (++iter) {
        Text* name = iter.value();

        if (!name->contains("/")) {
            loader->SetDataPath("Mods/Galaxy/");
            Load(name->data());
        }
    }
}

void
Galaxy::Load(const char* filename)
{
    Print("\nLoading Galaxy: %s\n", filename);

    BYTE*       block  = 0;
    DataLoader* loader = DataLoader::GetLoader();
    loader->LoadBuffer(filename, block, true);

    Parser parser(new BlockReader((const char*) block));

    Term*  term = parser.ParseTerm();

    if (!term) {
        Print("WARNING: could not parse '%s'\n", filename);
        return;
    }
    else {
        TermText* file_type = term->isText();
        if (!file_type || file_type->value() != "GALAXY") {
            Print("WARNING: invalid galaxy file '%s'\n", filename);
            return;
        }
    }

    // parse the galaxy:
    do {
        delete term;
        term = parser.ParseTerm();

        if (term) {
            TermDef* def = term->isDef();
            if (def) {
                if (def->name()->value() == "radius") {
                    GetDefNumber(radius, def, filename);
                }

                else if (def->name()->value() == "system") {
                    if (!def->term() || !def->term()->isStruct()) {
                        Print("WARNING: system struct missing in '%s'\n", filename);
                    }
                    else {
                        TermStruct* val = def->term()->isStruct();

                        char  sys_name[32];
                        char  classname[32];
                        Vec3  sys_loc;
                        int   sys_iff = 0;
                        int   star_class = Star::G;

                        sys_name[0] = 0;

                        for (int i = 0; i < val->elements()->size(); i++) {
                            TermDef* pdef = val->elements()->at(i)->isDef();
                            if (pdef) {
                                if (pdef->name()->value() == "name")
                                GetDefText(sys_name, pdef, filename);

                                else if (pdef->name()->value() == "loc")
                                GetDefVec(sys_loc, pdef, filename);

                                else if (pdef->name()->value() == "iff")
                                GetDefNumber(sys_iff, pdef, filename);

                                else if (pdef->name()->value() == "class") {
                                    GetDefText(classname, pdef, filename);

                                    switch (classname[0]) {
                                    case 'O':   star_class = Star::O;            break;
                                    case 'B':   star_class = Star::B;            break;
                                    case 'A':   star_class = Star::A;            break;
                                    case 'F':   star_class = Star::F;            break;
                                    case 'G':   star_class = Star::G;            break;
                                    case 'K':   star_class = Star::K;            break;
                                    case 'M':   star_class = Star::M;            break;
                                    case 'R':   star_class = Star::RED_GIANT;    break;
                                    case 'W':   star_class = Star::WHITE_DWARF;  break;
                                    case 'Z':   star_class = Star::BLACK_HOLE;   break;
                                    }
                                }
                            }
                        }

                        if (sys_name[0]) {
                            StarSystem* star_system = new StarSystem(sys_name, sys_loc, sys_iff, star_class);
                            star_system->Load();
                            systems.append(star_system);

                            Star* star = new Star(sys_name, sys_loc, star_class);
                            stars.append(star);
                        }
                    }
                }

                else if (def->name()->value() == "star") {
                    if (!def->term() || !def->term()->isStruct()) {
                        Print("WARNING: star struct missing in '%s'\n", filename);
                    }
                    else {
                        TermStruct* val = def->term()->isStruct();

                        char  star_name[32];
                        char  classname[32];
                        Vec3  star_loc;
                        int   star_class = Star::G;

                        star_name[0] = 0;

                        for (int i = 0; i < val->elements()->size(); i++) {
                            TermDef* pdef = val->elements()->at(i)->isDef();
                            if (pdef) {
                                if (pdef->name()->value() == "name")
                                GetDefText(star_name, pdef, filename);

                                else if (pdef->name()->value() == "loc")
                                GetDefVec(star_loc, pdef, filename);

                                else if (pdef->name()->value() == "class") {
                                    GetDefText(classname, pdef, filename);

                                    switch (classname[0]) {
                                    case 'O':   star_class = Star::O;            break;
                                    case 'B':   star_class = Star::B;            break;
                                    case 'A':   star_class = Star::A;            break;
                                    case 'F':   star_class = Star::F;            break;
                                    case 'G':   star_class = Star::G;            break;
                                    case 'K':   star_class = Star::K;            break;
                                    case 'M':   star_class = Star::M;            break;
                                    case 'R':   star_class = Star::RED_GIANT;    break;
                                    case 'W':   star_class = Star::WHITE_DWARF;  break;
                                    case 'Z':   star_class = Star::BLACK_HOLE;   break;
                                    }
                                }
                            }
                        }

                        if (star_name[0]) {
                            Star* star = new Star(star_name, star_loc, star_class);
                            stars.append(star);
                        }
                    }
                }
            }
        }
    }
    while (term);

    loader->ReleaseBuffer(block);
    loader->SetDataPath(0);
}

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

void
Galaxy::ExecFrame()
{
    ListIter<StarSystem> sys = systems;
    while (++sys) {
        sys->ExecFrame();
    }
}

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

StarSystem*
Galaxy::GetSystem(const char* name)
{
    ListIter<StarSystem> sys = systems;
    while (++sys) {
        if (!strcmp(sys->Name(), name))
        return sys.value();
    }

    return 0;
}

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

StarSystem*
Galaxy::FindSystemByRegion(const char* rgn_name)
{
    ListIter<StarSystem> iter = systems;
    while (++iter) {
        StarSystem* sys = iter.value();
        if (sys->FindRegion(rgn_name))
        return sys;
    }

    return 0;
}