Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ShipCtrl.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: ShipCtrl.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Ship Controller class
13 */
14 
15 #include "MemDebug.h"
16 #include "ShipCtrl.h"
17 #include "Ship.h"
18 #include "ShipDesign.h"
19 #include "Shield.h"
20 #include "Sensor.h"
21 #include "NavSystem.h"
22 #include "FlightComp.h"
23 #include "LandingGear.h"
24 #include "Sim.h"
25 #include "StarSystem.h"
26 #include "Starshatter.h"
27 #include "HUDView.h"
28 #include "HUDSounds.h"
29 #include "KeyMap.h"
30 #include "RadioMessage.h"
31 #include "RadioTraffic.h"
32 
33 #include "MouseController.h"
34 #include "Keyboard.h"
35 #include "Joystick.h"
36 #include "Game.h"
37 #include "DataLoader.h"
38 
39 // +--------------------------------------------------------------------+
40 
42 : ship(s), controller(ctrl), throttle_active(false), launch_latch(false),
43 pickle_latch(false), target_latch(false)
44 {
45  for (int i = 0; i < 10; i++)
46  GetAsyncKeyState('0'+i);
47 }
48 
49 // +--------------------------------------------------------------------+
50 
51 int
52 ShipCtrl::KeyDown(int action)
53 {
54  int k = Joystick::KeyDownMap(action) ||
55  Keyboard::KeyDownMap(action);
56 
57  return k;
58 }
59 
60 int
61 ShipCtrl::Toggled(int action)
62 {
63  static double last_toggle_time = 0;
64 
65  if (KeyDown(action)) {
66  if ((Game::RealTime() - last_toggle_time) > 250) {
67  last_toggle_time = Game::RealTime();
68  return 1;
69  }
70  }
71 
72  return 0;
73 }
74 
75 // +--------------------------------------------------------------------+
76 
77 void
79 {
80  if (controller) {
81  ship->SetThrottle(100);
82  throttle_active = false;
83  launch_latch = true;
84  }
85 }
86 
87 // +--------------------------------------------------------------------+
88 
89 void
90 ShipCtrl::ExecFrame(double seconds)
91 {
93  if (stars && stars->GetChatMode()) return;
94  if (!ship) return;
95 
96  const int DELTA_THROTTLE = 5;
97 
99 
100  if (ship->IsStarship() && ship->GetFLCSMode() == Ship::FLCS_HELM) {
101  ship->ApplyHelmPitch(controller->Pitch() * seconds);
102  ship->ApplyHelmYaw(controller->Yaw() * seconds);
103  }
104  else {
108  }
109 
113 
114  bool augmenter = false;
115 
116  if (controller->Throttle() > 0.05) {
117  if (throttle_active) {
118  ship->SetThrottle(controller->Throttle() * 100);
119 
120  if (controller->Throttle() >= 0.99)
121  augmenter = true;
122  }
123  else if (!launch_latch || controller->Throttle() > 0.5) {
124  throttle_active = true;
125  launch_latch = false;
126  }
127  }
128  else if (throttle_active) {
129  ship->SetThrottle(0);
130  throttle_active = false;
131  }
132 
133  ship->ExecFLCSFrame();
134 
135  static double time_til_change = 0.0;
136 
137  if (time_til_change < 0.001) {
138  if (KeyDown(KEY_THROTTLE_UP)) {
139  ship->SetThrottle(ship->Throttle() + DELTA_THROTTLE);
140  time_til_change = 0.05;
141  }
142 
143  else if (KeyDown(KEY_THROTTLE_DOWN)) {
144  ship->SetThrottle(ship->Throttle() - DELTA_THROTTLE);
145  time_til_change = 0.05;
146  }
147 
148  else if (KeyDown(KEY_THROTTLE_ZERO)) {
149  ship->SetThrottle(0);
150  if (ship->GetFLCS())
151  ship->GetFLCS()->FullStop();
152  time_til_change = 0.05;
153  }
154 
155  else if (KeyDown(KEY_THROTTLE_FULL)) {
156  ship->SetThrottle(100);
157  time_til_change = 0.05;
158  }
159 
160  else if (KeyDown(KEY_FLCS_MODE_AUTO)) {
161  ship->CycleFLCSMode();
162  time_til_change = 0.5f;
163  }
164 
165  else if (KeyDown(KEY_CYCLE_PRIMARY)) {
167  ship->CyclePrimary();
168  time_til_change = 0.5f;
169  }
170 
171  else if (KeyDown(KEY_CYCLE_SECONDARY)) {
173  ship->CycleSecondary();
174  time_til_change = 0.5f;
175  }
176 
177  if (ship->GetShield()) {
178  Shield* shield = ship->GetShield();
179  double level = shield->GetPowerLevel();
180 
181  if (KeyDown(KEY_SHIELDS_FULL)) {
183  shield->SetPowerLevel(100);
184  time_til_change = 0.5f;
185  }
186 
187  else if (KeyDown(KEY_SHIELDS_ZERO)) {
189  shield->SetPowerLevel(0);
190  time_til_change = 0.5f;
191  }
192 
193  else if (KeyDown(KEY_SHIELDS_UP)) {
194  if (level < 25) level = 25;
195  else if (level < 50) level = 50;
196  else if (level < 75) level = 75;
197  else level = 100;
198 
200  shield->SetPowerLevel(level);
201  time_til_change = 0.5f;
202  }
203 
204  else if (KeyDown(KEY_SHIELDS_DOWN)) {
205  if (level > 75) level = 75;
206  else if (level > 50) level = 50;
207  else if (level > 25) level = 25;
208  else level = 0;
209 
211  shield->SetPowerLevel(level);
212  time_til_change = 0.5f;
213  }
214 
215  }
216 
217  if (ship->GetSensor()) {
218  Sensor* sensor = ship->GetSensor();
219 
220  if (sensor->GetMode() < Sensor::PST) {
221  if (KeyDown(KEY_SENSOR_MODE)) {
222  int sensor_mode = sensor->GetMode() + 1;
223  if (sensor_mode > Sensor::GM)
224  sensor_mode = Sensor::PAS;
225 
226  sensor->SetMode((Sensor::Mode) sensor_mode);
227  time_til_change = 0.5f;
228  }
229 
230  else if (KeyDown(KEY_SENSOR_GROUND_MODE)) {
231  if (ship->IsAirborne()) {
232  sensor->SetMode(Sensor::GM);
233  time_til_change = 0.5f;
234  }
235  }
236  }
237  else {
238  // manual "return to search" command for starships:
239  if (KeyDown(KEY_SENSOR_MODE)) {
240  ship->DropTarget();
241  }
242  }
243 
245  sensor->IncreaseRange();
246  time_til_change = 0.5f;
247  }
248 
249  else if (KeyDown(KEY_SENSOR_RANGE_MINUS)) {
250  sensor->DecreaseRange();
251  time_til_change = 0.5f;
252  }
253  }
254 
255  if (KeyDown(KEY_EMCON_PLUS)) {
256  ship->SetEMCON(ship->GetEMCON()+1);
257  time_til_change = 0.5f;
258  }
259 
260  else if (KeyDown(KEY_EMCON_MINUS)) {
261  ship->SetEMCON(ship->GetEMCON()-1);
262  time_til_change = 0.5f;
263  }
264  }
265  else
266  time_til_change -= seconds;
267 
269  ship->FirePrimary();
270 
272  if (!pickle_latch)
273  ship->FireSecondary();
274 
275  pickle_latch = true;
276  }
277  else {
278  pickle_latch = false;
279  }
280 
282  if (!target_latch)
284 
285  target_latch = true;
286  }
287  else {
288  target_latch = false;
289  }
290 
291  ship->SetAugmenter(augmenter || (KeyDown(KEY_AUGMENTER) ? true : false));
292 
293  if (Toggled(KEY_DECOY))
294  ship->FireDecoy();
295 
297  ship->LaunchProbe();
298 
300  ship->ToggleGear();
301 
304 
306  ship->LockTarget(SimObject::SIM_SHIP, false, true);
307 
308  else if (Toggled(KEY_LOCK_THREAT))
310 
311  else if (Toggled(KEY_LOCK_CLOSEST_SHIP))
312  ship->LockTarget(SimObject::SIM_SHIP, true, false);
313 
315  ship->LockTarget(SimObject::SIM_DRONE, true, false);
316 
317  else if (Toggled(KEY_LOCK_HOSTILE_SHIP))
318  ship->LockTarget(SimObject::SIM_SHIP, true, true);
319 
321  ship->LockTarget(SimObject::SIM_DRONE, true, true);
322 
323  else if (Toggled(KEY_CYCLE_SUBTARGET))
324  ship->CycleSubTarget(1);
325 
326  else if (Toggled(KEY_PREV_SUBTARGET))
327  ship->CycleSubTarget(-1);
328 
329  if (Toggled(KEY_AUTO_NAV)) {
330  ship->SetAutoNav(true);
331  // careful: this object has just been deleted!
332  return;
333  }
334 
335  if (Toggled(KEY_DROP_ORBIT)) {
336  ship->DropOrbit();
337  // careful: this object has just been deleted!
338  return;
339  }
340 }
341