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
order.cpp
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2001-2002 Daniel Horn
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 #include "vegastrike.h"
22 #include "cmd/unit_generic.h"
23 #include "order.h"
24 #include "cmd/collection.h"
25 #include "communication.h"
26 #include "config_xml.h"
27 #include "vs_globals.h"
28 using std::vector;
29 using std::list;
30 //#define ORDERDEBUG // FIXME ?
32 {
33  static float airesptime = XMLSupport::parse_float( vs_config->getVariable( "AI", "CommResponseTime", "3" ) );
34  ProcessCommunicationMessages( airesptime, true );
35  int completed = 0;
36  unsigned int i = 0;
37  for (i = 0; i < suborders.size(); i++)
38  if ( ( completed&( (suborders[i])->getType()&(ALLTYPES) ) ) == 0 ) {
39  (suborders[i])->Execute();
40  completed |= (suborders[i])->getType();
41  if ( (suborders[i])->Done() ) {
42  vector< Order* >::iterator ord = suborders.begin()+i;
43  (*ord)->Destroy();
44  suborders.erase( ord );
45  i--;
46  }
47  }
48  if (suborders.size() == 0)
49  done = true;
50  else
51  done = false;
52 }
53 
54 Order* Order::queryType( unsigned int type )
55 {
56  for (unsigned int i = 0; i < suborders.size(); i++)
57  if ( (suborders[i]->type&type) == type )
58  return suborders[i];
59  return NULL;
60 }
61 Order* Order::queryAny( unsigned int type )
62 {
63  for (unsigned int i = 0; i < suborders.size(); i++)
64  if ( (suborders[i]->type&type) != 0 )
65  return suborders[i];
66  return NULL;
67 }
68 
69 void Order::eraseType( unsigned int type )
70 {
71  for (unsigned int i = 0; i < suborders.size(); i++)
72  if ( (suborders[i]->type&type) == type ) {
73  suborders[i]->Destroy();
74  vector< Order* >::iterator j = suborders.begin()+i;
75  suborders.erase( j );
76  i--;
77  }
78 }
79 
81 {
82  if (ord == NULL) {
83  printf( "NOT ENQEUEING NULL ORDER\n" );
84  printf( "this order: %s\n", getOrderDescription().c_str() );
85  return NULL;
86  }
87  ord->SetParent( parent );
88  suborders.push_back( ord );
89  return this;
90 }
92 {
93  if (ord == NULL) {
94  printf( "NOT ENQEUEING NULL ORDER\n" );
95  printf( "this order: %s\n", getOrderDescription().c_str() );
96  return NULL;
97  }
98  ord->SetParent( parent );
99 
100  vector< Order* >::iterator first_elem = suborders.begin();
101  suborders.insert( first_elem, ord );
102  return this;
103 }
105 {
106  for (vector< Order* >::iterator ordd = suborders.begin(); ordd != suborders.end();) {
107  if ( ( ord->getType()&(*ordd)->getType()&(ALLTYPES) ) ) {
108  (*ordd)->Destroy();
109  ordd = suborders.erase( ordd );
110  } else {
111  ordd++;
112  }
113  }
114  suborders.push_back( ord );
115  return this;
116 }
117 
118 bool Order::AttachOrder( Unit *targets1 )
119 {
120  if ( !(subtype&STARGET) ) {
121  if (subtype&SSELF)
122  return AttachSelfOrder( targets1 ); //can use attach order to do shit
123 
124  return false;
125  }
126  parent->Target( targets1 );
127  return true;
128 }
129 
130 bool Order::AttachSelfOrder( Unit *targets1 )
131 {
132  if ( !(subtype&SSELF) )
133  return false;
134  group.SetUnit( targets1 );
135  return true;
136 }
137 
139 {
140  if ( !(subtype&SLOCATION) )
141  return false;
142  targetlocation = targetv;
143  return true;
144 }
145 
147 {
148  if (ord == NULL) {
149  printf( "FINDING EMPTY ORDER\n" );
150  printf( "this order: %s\n", getOrderDescription().c_str() );
151  return NULL;
152  }
153  for (unsigned int i = 0; i < suborders.size(); i++)
154  if (suborders[i] == ord)
155  return suborders[i];
156  return NULL;
157 }
159 {
161 }
163 {
164  delete this;
165 }
167 {
168  unsigned int i;
169  for (i = 0; i < suborders.size(); i++) {
170  if (suborders[i] == NULL) {
171  printf( "ORDER: a null order\n" );
172  printf( "this order: %s\n", getOrderDescription().c_str() );
173  } else {
174  suborders[i]->Destroy();
175  }
176  }
177  {
178  for (list< CommunicationMessage* >::iterator i = messagequeue.begin(); i != messagequeue.end(); i++)
179  delete (*i);
180  }
181  messagequeue.clear();
182  suborders.clear();
183  this->Destructor();
184 }
186 {
187  unsigned int i;
188  for (i = 0; i < suborders.size(); i++)
189  suborders[i]->ClearMessages();
190  {
191  for (list< CommunicationMessage* >::iterator i = messagequeue.begin(); i != messagequeue.end(); i++)
192  delete (*i);
193  }
194  messagequeue.clear();
195 }
197 {
198  bool found = false;
199  if (ord == NULL) {
200  printf( "NOT ERASING A NULL ORDER\n" );
201  printf( "this order: %s\n", getOrderDescription().c_str() );
202  return;
203  }
204  for (unsigned int i = 0; i < suborders.size() && found == false; i++)
205  if (suborders[i] == ord) {
206  suborders[i]->Destroy();
207  vector< Order* >::iterator j = suborders.begin()+i;
208  suborders.erase( j );
209  found = true;
210  }
211  if (!found) {
212  printf( "TOLD TO ERASE AN ORDER - NOT FOUND\n" );
213  printf( "this order: %s\n", getOrderDescription().c_str() );
214  }
215 }
216 
218 {
219  olist_t *orderlist = getOrderList();
220  if (orderlist)
221  return this;
222  Order *found_order = NULL;
223  for (unsigned int i = 0; i < suborders.size() && found_order == NULL; i++)
224  found_order = suborders[i]->findOrderList();
225  return found_order;
226 }
227 
229 {
230  string tabs;
231  for (int i = 0; i < level; i++)
232  tabs = tabs+" ";
233  string desc = tabs+"+"+getOrderDescription()+"\n";
234  for (unsigned int j = 0; j < suborders.size(); j++)
235  desc = desc+suborders[j]->createFullOrderDescription( level+1 );
236  return desc;
237 }
238 
239 namespace Orders
240 {
241 
243 {
244  if (child) {
245  child->SetParent( parent );
246  type = child->getType();
247  }
248  if (time > maxtime) {
249  done = true;
250  return;
251  }
252  time += SIMULATION_ATOM;
253  if (child)
254  child->Execute();
255 }
256 
258  : Order(first->getType() | second->getType(),
259  first->getSubType()),
260  first(first),
261  second(second)
262 {
263  assert((first->getType() & second->getType()) == 0);
264  assert(first->getSubType() == second->getSubType());
265 
266  SetParent(parent);
267  EnqueueOrder(first);
268  EnqueueOrder(second);
269 }
270 
272 {
273  // Execute both sub-orders
274  Order::Execute();
275  // Wait for both sub-orders to have finished
276  if (first->Done() && second->Done())
277  {
278  done = true;
279  }
280 }
281 
282 Sequence::Sequence(Unit *parent, Order *order, unsigned int excludeTypes)
283  : Order(order->getType() | excludeTypes,
284  order->getSubType()),
285  order(order)
286 {
287  SetParent(parent);
288  EnqueueOrder(order);
289 }
290 
292 {
293  Order::Execute();
294  if (order->Done())
295  {
296  done = true;
297  }
298 }
299 
300 } // namespace Orders