1 // asterism.cpp
2 //
3 // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 
10 #include <algorithm>
11 
12 #ifndef _WIN32
13 #ifndef TARGET_OS_MAC
14 #include <config.h>
15 #endif /* TARGET_OS_MAC */
16 #endif /* _WIN32 */
17 
18 #include <celutil/util.h>
19 #include <celutil/debug.h>
20 #include "parser.h"
21 #include "asterism.h"
22 
23 using namespace std;
24 
25 
Asterism(string _name)26 Asterism::Asterism(string _name) :
27     name(_name),
28     active(true),
29     useOverrideColor(false)
30 {
31     i18nName = dgettext("celestia_constellations", _name.c_str());
32 }
33 
~Asterism()34 Asterism::~Asterism()
35 {
36 }
37 
38 
getName(bool i18n) const39 string Asterism::getName(bool i18n) const
40 {
41     return i18n?i18nName:name;
42 }
43 
getChainCount() const44 int Asterism::getChainCount() const
45 {
46     return chains.size();
47 }
48 
getChain(int index) const49 const Asterism::Chain& Asterism::getChain(int index) const
50 {
51     return *chains[index];
52 }
53 
addChain(Asterism::Chain & chain)54 void Asterism::addChain(Asterism::Chain& chain)
55 {
56     chains.insert(chains.end(), &chain);
57 }
58 
59 
60 /*! Return whether the constellation is visible.
61  */
getActive() const62 bool Asterism::getActive() const
63 {
64     return active;
65 }
66 
67 
68 /*! Set whether or not the constellation is visible.
69  */
setActive(bool _active)70 void Asterism::setActive(bool _active)
71 {
72 	active = _active;
73 }
74 
75 
76 /*! Get the override color for this constellation.
77  */
getOverrideColor() const78 Color Asterism::getOverrideColor() const
79 {
80     return color;
81 }
82 
83 
84 /*! Set an override color for the constellation. If this method isn't
85  *  called, the constellation is drawn in the render's default color
86  *  for contellations. Calling unsetOverrideColor will remove the
87  *  override color.
88  */
setOverrideColor(Color c)89 void Asterism::setOverrideColor(Color c)
90 {
91     color = c;
92     useOverrideColor = true;
93 }
94 
95 
96 /*! Make this constellation appear in the default color (undoing any
97  *  calls to setOverrideColor.
98  */
unsetOverrideColor()99 void Asterism::unsetOverrideColor()
100 {
101     useOverrideColor = false;
102 }
103 
104 
105 /*! Return true if this constellation has a custom color, or false
106  *  if it should be drawn in the default color.
107  */
isColorOverridden() const108 bool Asterism::isColorOverridden() const
109 {
110     return useOverrideColor;
111 }
112 
113 
ReadAsterismList(istream & in,const StarDatabase & stardb)114 AsterismList* ReadAsterismList(istream& in, const StarDatabase& stardb)
115 {
116     AsterismList* asterisms = new AsterismList();
117     Tokenizer tokenizer(&in);
118     Parser parser(&tokenizer);
119 
120     while (tokenizer.nextToken() != Tokenizer::TokenEnd)
121     {
122         if (tokenizer.getTokenType() != Tokenizer::TokenString)
123         {
124             DPRINTF(0, "Error parsing asterism file.\n");
125             for_each(asterisms->begin(), asterisms->end(), deleteFunc<Asterism*>());
126             delete asterisms;
127             return NULL;
128         }
129 
130         string name = tokenizer.getStringValue();
131         Asterism* ast = new Asterism(name);
132 
133         Value* chainsValue = parser.readValue();
134         if (chainsValue == NULL || chainsValue->getType() != Value::ArrayType)
135         {
136             DPRINTF(0, "Error parsing asterism %s\n", name.c_str());
137             for_each(asterisms->begin(), asterisms->end(), deleteFunc<Asterism*>());
138             delete asterisms;
139             delete chainsValue;
140             return NULL;
141         }
142 
143         Array* chains = chainsValue->getArray();
144 
145         for (int i = 0; i < (int) chains->size(); i++)
146         {
147             if ((*chains)[i]->getType() == Value::ArrayType)
148             {
149                 Array* a = (*chains)[i]->getArray();
150                 Asterism::Chain* chain = new Asterism::Chain();
151                 for (Array::const_iterator iter = a->begin(); iter != a->end(); iter++)
152                 {
153                     if ((*iter)->getType() == Value::StringType)
154                     {
155                         Star* star = stardb.find((*iter)->getString());
156                         if (star != NULL)
157                             chain->insert(chain->end(), star->getPosition());
158                     }
159                 }
160 
161                 ast->addChain(*chain);
162             }
163         }
164 
165         asterisms->insert(asterisms->end(), ast);
166 
167         delete chainsValue;
168     }
169 
170     return asterisms;
171 }
172