Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
criteria.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2003 Mike Byron
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #include "vegastrike.h"
23 #if defined (_WIN32) && !defined (__CYGWIN__) && !defined (__MINGW32__)
24 //For WIN32 debugging.
25 #include <crtdbg.h>
26 #endif
27 
28 #include <assert.h>
29 
30 #include "criteria.h"
31 #include "vs_globals.h"
32 #include "gfx/cockpit.h"
33 #include "galaxy_xml.h"
34 #include "savegame.h"
35 #include "universe_util.h"
36 
37 using std::string;
38 using std::set;
39 
41 
42 bool CriteriaRoot::isDestination( unsigned system ) const
43 {
44  if (m_child)
45  return m_child->isDestination( system );
46  else
47  return false;
48 }
49 
51 {
52  if (m_child)
53  return m_child->getDescription();
54  else
55  return "";
56 }
57 
58 string CriteriaRoot::getText() const
59 {
60  return "";
61 }
62 
64 {
65  assert( child == m_child );
66  m_child = replacement;
67 }
68 
70 {
71  return NULL;
72 }
73 
75 {
76  assert( child == m_child );
77  m_child = NULL;
78  return child;
79 }
80 
81 vector< CriteriaNode* >CriteriaRoot::getChildren() const
82 {
83  vector< CriteriaNode* >temp;
84  if (m_child)
85  temp.push_back( m_child );
86  return temp;
87 }
88 
90 {
91  CriteriaNode *cloned_child = NULL;
92  if (m_child)
93  cloned_child = m_child->clone();
94  CriteriaRoot *temp = new CriteriaRoot( cloned_child );
95  return temp;
96 }
97 
99 {
100  m_child = node;
101  if (m_child)
102  m_child->setParent( this );
103 }
104 
106  CriteriaParent( NULL )
107  , m_child( child )
108 {
109  if (m_child)
110  m_child->setParent( this );
111 }
112 
114 {
115  if (m_child)
116  delete m_child;
117 }
118 
120 
121 bool CriteriaNot::isDestination( unsigned system ) const
122 {
123  assert( m_child != NULL );
124 
125  return !( m_child->isDestination( system ) );
126 }
127 
129 {
130  assert( m_child != NULL );
131 
132  string temp = "NOT(";
133  temp += m_child->getDescription();
134  temp += ")";
135  return temp;
136 }
137 
138 string CriteriaNot::getText() const
139 {
140  return "NOT";
141 }
142 
144 {
145  assert( child == m_child );
146  m_child = replacement;
147 }
148 
150 {
152  getParent()->replaceChild( this, m_child );
153  m_child = NULL;
154  return this;
155 }
156 
158 {
159  return getParent()->unhook( this );
160 }
161 
162 vector< CriteriaNode* >CriteriaNot::getChildren() const
163 {
164  vector< CriteriaNode* >temp;
165  if (m_child)
166  temp.push_back( m_child );
167  return temp;
168 }
169 
171 {
172  assert( m_child );
173  CriteriaNode *cloned_child = m_child->clone();
174  CriteriaNot *temp = new CriteriaNot( cloned_child );
175  return temp;
176 }
177 
180 {
181  assert( child != NULL );
182  setParent( child->getParent() );
183  m_child = child;
184  if ( child->getParent() )
185  child->getParent()->replaceChild( child, this );
186  child->setParent( this );
187 }
188 
190 {
191  if (m_child)
192  delete m_child;
193 }
194 
196 
198  CriteriaParent( child->getParent() )
199 {
200  assert( child != NULL );
201  assert( newNode != NULL );
202  m_left = child;
203  m_right = newNode;
204  if ( child->getParent() )
205  child->getParent()->replaceChild( child, this );
206  child->setParent( this );
207 
208  newNode->setParent( this );
209 }
210 
212 {
213  return getParent()->unhook( this );
214 }
215 
217 {
218  assert( (m_left == child) || (m_right == child) );
219  if (child == m_left) {
221  getParent()->replaceChild( this, m_right );
222  m_right = NULL;
223  } else {
224  m_left->setParent( getParent() );
225  getParent()->replaceChild( this, m_left );
226  m_left = NULL;
227  }
228  return this;
229 }
230 
231 vector< CriteriaNode* >CriteriaBinaryOperator::getChildren() const
232 {
233  vector< CriteriaNode* >temp;
234  if (m_left)
235  temp.push_back( m_left );
236  if (m_right)
237  temp.push_back( m_right );
238  return temp;
239 }
240 
242 {
243  if (m_left)
244  delete m_left;
245  if (m_right)
246  delete m_right;
247 }
248 
250 {
251  assert( (m_left == child) || (m_right == child) );
252  if (child == m_left)
253  m_left = replacement;
254  else
255  m_right = replacement;
256 }
257 
259 
260 bool CriteriaAnd::isDestination( unsigned system ) const
261 {
262  assert( m_left != NULL );
263  assert( m_right != NULL );
264 
265  return m_left->isDestination( system ) && m_right->isDestination( system );
266 }
267 
269 {
270  assert( m_left != NULL );
271  assert( m_right != NULL );
272 
273  string temp = "(";
274  temp += m_left->getDescription();
275  temp += " AND ";
276  temp += m_right->getDescription();
277  temp += ")";
278  return temp;
279 }
280 
281 string CriteriaAnd::getText() const
282 {
283  return "AND";
284 }
285 
287 {
288  assert( m_left );
289  assert( m_right );
290 
291  CriteriaNode *cloned_left = m_left->clone();
292  CriteriaNode *cloned_right = m_right->clone();
293 
294  CriteriaAnd *temp = new CriteriaAnd( cloned_left, cloned_right );
295  return temp;
296 }
297 
299 
300 bool CriteriaOr::isDestination( unsigned system ) const
301 {
302  assert( m_left != NULL );
303  assert( m_right != NULL );
304 
305  return m_left->isDestination( system ) || m_right->isDestination( system );
306 }
307 
309 {
310  assert( m_left != NULL );
311  assert( m_right != NULL );
312 
313  string temp = "(";
314  temp += m_left->getDescription();
315  temp += " OR ";
316  temp += m_right->getDescription();
317  temp += ")";
318  return temp;
319 }
320 
321 string CriteriaOr::getText() const
322 {
323  return "OR";
324 }
325 
327 {
328  assert( m_left );
329  assert( m_right );
330 
331  CriteriaNode *cloned_left = m_left->clone();
332  CriteriaNode *cloned_right = m_right->clone();
333 
334  CriteriaOr *temp = new CriteriaOr( cloned_left, cloned_right );
335  return temp;
336 }
337 
339 
341 {
342  return getParent()->unhook( this );
343 }
344 
345 vector< CriteriaNode* >CriteriaLeaf::getChildren() const
346 {
347  vector< CriteriaNode* >temp;
348  return temp;
349 }
350 
352 
353 bool CriteriaContains::isDestination( unsigned system ) const
354 {
355  string name = _Universe->AccessCockpit()->AccessNavSystem()->systemIter[system].GetName();
356 
357  //Check to make sure we have been there in person
358  //Not only for realism
359  //but the systems may have not been created yet.
360  string key( string( "visited_" )+name );
361  vector< float > *v = &_Universe->AccessCockpit()->savegame->getMissionData( key );
362  if ( v->empty() )
363  return false;
364  if ( (*v)[0] != 1.0 )
365  return false;
366  string texture = _Universe->getGalaxy()->getPlanetVariable( m_value, "texture", "" );
367  if (texture == "")
368  return false;
369  set< string > types = getPlanetTypesFromXML( name.c_str() );
370  for (set< string >::iterator i = types.begin(); i != types.end(); ++i)
371  if ( (*i).find( texture, 0 ) != string::npos )
372  return true;
373  return false;
374 }
375 
377 {
378  string temp = "CONTAINS(";
379  temp += m_value;
380  temp += ")";
381  return temp;
382 }
383 
385 {
386  return getDescription();
387 }
388 
390 {
391  return new CriteriaContains( m_value, NULL );
392 }
393 
395 
396 bool CriteriaOwnedBy::isDestination( unsigned system ) const
397 {
398  string name = _Universe->AccessCockpit()->AccessNavSystem()->systemIter[system].GetName();
399  string faction = UniverseUtil::GetGalaxyFaction( name );
400  if (faction == m_value)
401  return true;
402  else
403  return false;
404 }
405 
407 {
408  string temp = "OWNEDBY(";
409  temp += m_value;
410  temp += ")";
411  return temp;
412 }
413 
415 {
416  return getDescription();
417 }
418 
420 {
421  return new CriteriaOwnedBy( m_value, NULL );
422 }
423 
425 
426 bool CriteriaSector::isDestination( unsigned system ) const
427 {
428  string name = _Universe->AccessCockpit()->AccessNavSystem()->systemIter[system].GetName();
429  string sector, systemname;
430  Beautify( name, sector, systemname );
431  if (sector == m_value)
432  return true;
433  else
434  return false;
435 }
436 
438 {
439  string temp = "SECTOR(";
440  temp += m_value;
441  temp += ")";
442  return temp;
443 }
444 
446 {
447  return getDescription();
448 }
449 
451 {
452  return new CriteriaSector( m_value, NULL );
453 }
454