Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Galaxy.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: Galaxy.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Galaxy (list of star systems) for a single campaign.
13 */
14 
15 #include "MemDebug.h"
16 #include "Galaxy.h"
17 #include "StarSystem.h"
18 #include "Starshatter.h"
19 
20 #include "Game.h"
21 #include "Solid.h"
22 #include "Sprite.h"
23 #include "Light.h"
24 #include "Bitmap.h"
25 #include "DataLoader.h"
26 #include "ParseUtil.h"
27 
28 static Galaxy* galaxy = 0;
29 
30 // +--------------------------------------------------------------------+
31 
32 Galaxy::Galaxy(const char* n)
33 : name(n), radius(10)
34 { }
35 
36 // +--------------------------------------------------------------------+
37 
39 {
40  Print(" Destroying Galaxy %s\n", (const char*) name);
41  systems.destroy();
42  stars.destroy();
43 }
44 
45 // +--------------------------------------------------------------------+
46 
47 void
49 {
50  if (galaxy) delete galaxy;
51  galaxy = new(__FILE__,__LINE__) Galaxy("Galaxy");
52  galaxy->Load();
53 }
54 
55 void
57 {
58  delete galaxy;
59  galaxy = 0;
60 }
61 
62 Galaxy*
64 {
65  return galaxy;
66 }
67 
68 // +--------------------------------------------------------------------+
69 
70 void
72 {
74  loader->SetDataPath("Galaxy/");
75  sprintf_s(filename, "%s.def", (const char*) name);
76  Load(filename);
77 
78  // load mod galaxies:
79  List<Text> mod_galaxies;
80  loader->SetDataPath("Mods/Galaxy/");
81  loader->ListFiles("*.def", mod_galaxies);
82 
83  ListIter<Text> iter = mod_galaxies;
84  while (++iter) {
85  Text* name = iter.value();
86 
87  if (!name->contains("/")) {
88  loader->SetDataPath("Mods/Galaxy/");
89  Load(name->data());
90  }
91  }
92 }
93 
94 void
95 Galaxy::Load(const char* filename)
96 {
97  Print("\nLoading Galaxy: %s\n", filename);
98 
99  BYTE* block = 0;
100  DataLoader* loader = DataLoader::GetLoader();
101  loader->LoadBuffer(filename, block, true);
102 
103  Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block));
104 
105  Term* term = parser.ParseTerm();
106 
107  if (!term) {
108  Print("WARNING: could not parse '%s'\n", filename);
109  return;
110  }
111  else {
112  TermText* file_type = term->isText();
113  if (!file_type || file_type->value() != "GALAXY") {
114  Print("WARNING: invalid galaxy file '%s'\n", filename);
115  return;
116  }
117  }
118 
119  // parse the galaxy:
120  do {
121  delete term;
122  term = parser.ParseTerm();
123 
124  if (term) {
125  TermDef* def = term->isDef();
126  if (def) {
127  if (def->name()->value() == "radius") {
128  GetDefNumber(radius, def, filename);
129  }
130 
131  else if (def->name()->value() == "system") {
132  if (!def->term() || !def->term()->isStruct()) {
133  Print("WARNING: system struct missing in '%s'\n", filename);
134  }
135  else {
136  TermStruct* val = def->term()->isStruct();
137 
138  char sys_name[32];
139  char classname[32];
140  Vec3 sys_loc;
141  int sys_iff = 0;
142  int star_class = Star::G;
143 
144  sys_name[0] = 0;
145 
146  for (int i = 0; i < val->elements()->size(); i++) {
147  TermDef* pdef = val->elements()->at(i)->isDef();
148  if (pdef) {
149  if (pdef->name()->value() == "name")
150  GetDefText(sys_name, pdef, filename);
151 
152  else if (pdef->name()->value() == "loc")
153  GetDefVec(sys_loc, pdef, filename);
154 
155  else if (pdef->name()->value() == "iff")
156  GetDefNumber(sys_iff, pdef, filename);
157 
158  else if (pdef->name()->value() == "class") {
159  GetDefText(classname, pdef, filename);
160 
161  switch (classname[0]) {
162  case 'O': star_class = Star::O; break;
163  case 'B': star_class = Star::B; break;
164  case 'A': star_class = Star::A; break;
165  case 'F': star_class = Star::F; break;
166  case 'G': star_class = Star::G; break;
167  case 'K': star_class = Star::K; break;
168  case 'M': star_class = Star::M; break;
169  case 'R': star_class = Star::RED_GIANT; break;
170  case 'W': star_class = Star::WHITE_DWARF; break;
171  case 'Z': star_class = Star::BLACK_HOLE; break;
172  }
173  }
174  }
175  }
176 
177  if (sys_name[0]) {
178  StarSystem* star_system = new(__FILE__,__LINE__) StarSystem(sys_name, sys_loc, sys_iff, star_class);
179  star_system->Load();
180  systems.append(star_system);
181 
182  Star* star = new(__FILE__,__LINE__) Star(sys_name, sys_loc, star_class);
183  stars.append(star);
184  }
185  }
186  }
187 
188  else if (def->name()->value() == "star") {
189  if (!def->term() || !def->term()->isStruct()) {
190  Print("WARNING: star struct missing in '%s'\n", filename);
191  }
192  else {
193  TermStruct* val = def->term()->isStruct();
194 
195  char star_name[32];
196  char classname[32];
197  Vec3 star_loc;
198  int star_class = Star::G;
199 
200  star_name[0] = 0;
201 
202  for (int i = 0; i < val->elements()->size(); i++) {
203  TermDef* pdef = val->elements()->at(i)->isDef();
204  if (pdef) {
205  if (pdef->name()->value() == "name")
206  GetDefText(star_name, pdef, filename);
207 
208  else if (pdef->name()->value() == "loc")
209  GetDefVec(star_loc, pdef, filename);
210 
211  else if (pdef->name()->value() == "class") {
212  GetDefText(classname, pdef, filename);
213 
214  switch (classname[0]) {
215  case 'O': star_class = Star::O; break;
216  case 'B': star_class = Star::B; break;
217  case 'A': star_class = Star::A; break;
218  case 'F': star_class = Star::F; break;
219  case 'G': star_class = Star::G; break;
220  case 'K': star_class = Star::K; break;
221  case 'M': star_class = Star::M; break;
222  case 'R': star_class = Star::RED_GIANT; break;
223  case 'W': star_class = Star::WHITE_DWARF; break;
224  case 'Z': star_class = Star::BLACK_HOLE; break;
225  }
226  }
227  }
228  }
229 
230  if (star_name[0]) {
231  Star* star = new(__FILE__,__LINE__) Star(star_name, star_loc, star_class);
232  stars.append(star);
233  }
234  }
235  }
236  }
237  }
238  }
239  while (term);
240 
241  loader->ReleaseBuffer(block);
242  loader->SetDataPath(0);
243 }
244 
245 // +--------------------------------------------------------------------+
246 
247 void
249 {
251  while (++sys) {
252  sys->ExecFrame();
253  }
254 }
255 
256 // +--------------------------------------------------------------------+
257 
258 StarSystem*
259 Galaxy::GetSystem(const char* name)
260 {
262  while (++sys) {
263  if (!strcmp(sys->Name(), name))
264  return sys.value();
265  }
266 
267  return 0;
268 }
269 
270 // +--------------------------------------------------------------------+
271 
272 StarSystem*
273 Galaxy::FindSystemByRegion(const char* rgn_name)
274 {
276  while (++iter) {
277  StarSystem* sys = iter.value();
278  if (sys->FindRegion(rgn_name))
279  return sys;
280  }
281 
282  return 0;
283 }