Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
WeaponGroup.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2005. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: WeaponGroup.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Weapon Control Category (Group) class
13 */
14 
15 #include "MemDebug.h"
16 #include "WeaponGroup.h"
17 #include "Ship.h"
18 
19 // +----------------------------------------------------------------------+
20 
22 : selected(0), trigger(false), orders(Weapon::MANUAL),
23 control(Weapon::SINGLE_FIRE), sweep(Weapon::SWEEP_TIGHT),
24 mass(0.0f), resist(0.0f), name(n), ammo(0)
25 { }
26 
27 // +--------------------------------------------------------------------+
28 
30 {
31  weapons.destroy();
32 }
33 
34 // +--------------------------------------------------------------------+
35 
36 void
37 WeaponGroup::SetName(const char* n)
38 {
39  name = n;
40 }
41 
42 void
44 {
45  abrv = a;
46 }
47 
48 // +--------------------------------------------------------------------+
49 
50 bool
52 {
53  if (weapons.size() > 0)
54  return weapons[0]->IsPrimary();
55 
56  return false;
57 }
58 
59 bool
61 {
62  if (weapons.size() > 0)
63  return weapons[0]->IsDrone();
64 
65  return false;
66 }
67 
68 bool
70 {
71  if (weapons.size() > 0)
72  return weapons[0]->IsDecoy();
73 
74  return false;
75 }
76 
77 bool
79 {
80  if (weapons.size() > 0)
81  return weapons[0]->IsProbe();
82 
83  return false;
84 }
85 
86 bool
88 {
89  if (weapons.size() > 0)
90  return weapons[0]->IsMissile();
91 
92  return false;
93 }
94 
95 bool
97 {
98  if (weapons.size() > 0)
99  return weapons[0]->IsBeam();
100 
101  return false;
102 }
103 
104 // +--------------------------------------------------------------------+
105 
106 void
108 {
109  weapons.append(w);
110 }
111 
112 int
114 {
115  return weapons.size();
116 }
117 
120 {
121  return weapons;
122 }
123 
124 bool
126 {
127  return weapons.contains(w)?true:false;
128 }
129 
130 // +--------------------------------------------------------------------+
131 
132 void
134 {
135  if (n >= 0 && n < weapons.size())
136  selected = n;
137 }
138 
139 void
141 {
142  selected++;
143 
144  if (selected >= weapons.size())
145  selected = 0;
146 }
147 
148 Weapon*
150 {
151  if (n >= 0 && n < weapons.size())
152  return weapons[n];
153 
154  return 0;
155 }
156 
157 Weapon*
159 {
160  return weapons[selected];
161 }
162 
163 bool
164 WeaponGroup::CanTarget(DWORD tgt_class) const
165 {
166  if (selected >= 0 && selected < weapons.size())
167  return weapons[selected]->CanTarget(tgt_class);
168 
169  return false;
170 }
171 
172 // +--------------------------------------------------------------------+
173 
174 void
175 WeaponGroup::ExecFrame(double seconds)
176 {
177  ammo = 0;
178  mass = 0.0f;
179  resist = 0.0f;
180 
181  ListIter<Weapon> iter = weapons;
182  while (++iter) {
183  Weapon* w = iter.value();
184  w->ExecFrame(seconds);
185 
186  ammo += w->Ammo();
187  mass += w->Mass();
188  resist += w->Resistance();
189  }
190 }
191 
192 void
194 {
195  ammo = 0;
196  mass = 0.0f;
197  resist = 0.0f;
198 
199  ListIter<Weapon> iter = weapons;
200  while (++iter) {
201  Weapon* w = iter.value();
202 
203  ammo += w->Ammo();
204  mass += w->Mass();
205  resist += w->Resistance();
206  }
207 }
208 
209 // +--------------------------------------------------------------------+
210 
211 void
213 {
215  while (++w)
216  w->SetTarget(target, subtarget);
217 }
218 
219 SimObject*
221 {
222  SimObject* target = 0;
223 
224  if (weapons.size())
225  target = weapons[0]->GetTarget();
226 
227  return target;
228 }
229 
230 System*
232 {
233  System* subtarget = 0;
234 
235  if (weapons.size())
236  subtarget = weapons[0]->GetSubTarget();
237 
238  return subtarget;
239 }
240 
241 void
243 {
245  while (++w)
246  w->SetTarget(0, 0);
247 }
248 
249 // +--------------------------------------------------------------------+
250 
253 {
254  if (selected >= 0 && selected < weapons.size())
255  return (WeaponDesign*) weapons[selected]->Design();
256 
257  return 0;
258 }
259 
260 // +--------------------------------------------------------------------+
261 
262 int
264 {
265  int status = System::NOMINAL;
266  int critical = true;
267 
268  ListIter<Weapon> iter = (List<Weapon>&) weapons; // cast-away const
269  while (++iter) {
270  Weapon* w = iter.value();
271 
272  if (w->Status() < System::NOMINAL)
273  status = System::DEGRADED;
274 
275  if (w->Status() > System::CRITICAL)
276  critical = false;
277  }
278 
279  if (critical)
280  return System::CRITICAL;
281 
282  return status;
283 }
284 
285 // +--------------------------------------------------------------------+
286 
287 void
289 {
290  orders = o;
291 
293  while (++w)
295 }
296 
297 void
299 {
300  control = m;
301 
303  while (++w)
305 }
306 
307 void
309 {
310  sweep = s;
311 
313  while (++w)
314  w->SetSweep(sweep);
315 }
316 
317 // +--------------------------------------------------------------------+
318 
319 void
321 {
323  while (++w)
324  w->PowerOff();
325 }
326 
327 void
329 {
331  while (++w)
332  w->PowerOn();
333 }
334 
335 // +--------------------------------------------------------------------+
336 
337 int
339 {
340  int result = 0;
341 
342  for (int i = 0; i < weapons.size(); i++) {
343  const Weapon* w = weapons[i];
344  result += w->Value();
345  }
346 
347  return result;
348 }