Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FlightComp.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: FlightComp.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Flight Computer System class
13 */
14 
15 #include "MemDebug.h"
16 #include "FlightComp.h"
17 #include "Ship.h"
18 #include "ShipDesign.h"
19 #include "Thruster.h"
20 
21 // +----------------------------------------------------------------------+
22 
23 FlightComp::FlightComp(int comp_type, const char* comp_name)
24 : Computer(comp_type, comp_name), mode(0), vlimit(0.0f),
25 trans_x_limit(0.0f), trans_y_limit(0.0f), trans_z_limit(0.0f),
26 throttle(0.0f), halt(0)
27 { }
28 
29 // +----------------------------------------------------------------------+
30 
32 : Computer(c), mode(0), vlimit(0.0f),
33 trans_x_limit(0.0f), trans_y_limit(0.0f), trans_z_limit(0.0f),
34 throttle(0.0f), halt(0)
35 { }
36 
37 // +--------------------------------------------------------------------+
38 
40 { }
41 
42 // +--------------------------------------------------------------------+
43 
44 void
45 FlightComp::SetTransLimit(double x, double y, double z)
46 {
47  trans_x_limit = 0.0f;
48  trans_y_limit = 0.0f;
49  trans_z_limit = 0.0f;
50 
51  if (x >= 0) trans_x_limit = (float) x;
52  if (y >= 0) trans_y_limit = (float) y;
53  if (z >= 0) trans_z_limit = (float) z;
54 }
55 
56 // +--------------------------------------------------------------------+
57 
58 void
60 {
61  if (ship) {
62  ExecThrottle();
63  ExecTrans();
64  }
65 }
66 
67 // +--------------------------------------------------------------------+
68 
69 void
71 {
72  throttle = (float) ship->Throttle();
73 
74  if (throttle > 5)
75  halt = false;
76 }
77 
78 // +--------------------------------------------------------------------+
79 
80 void
82 {
83  double tx = ship->TransX();
84  double ty = ship->TransY();
85  double tz = ship->TransZ();
86 
87  double trans_x = tx;
88  double trans_y = ty;
89  double trans_z = tz;
90 
91  bool flcs_operative = false;
92 
93  if (IsPowerOn())
94  flcs_operative = Status() == System::NOMINAL ||
96 
97  // ----------------------------------------------------------
98  // FIGHTER FLCS AUTO MODE
99  // ----------------------------------------------------------
100 
101  if (mode == Ship::FLCS_AUTO) {
102  // auto thrust to align flight path with orientation:
103  if (tx == 0) {
104  if (flcs_operative)
105  trans_x = (ship->Velocity() * ship->BeamLine() * -200);
106 
107  else
108  trans_x = 0;
109  }
110 
111  // manual thrust up to vlimit/2:
112  else {
113  double vfwd = ship->BeamLine() * ship->Velocity();
114 
115  if (fabs(vfwd)>= vlimit) {
116  if (trans_x > 0 && vfwd > 0)
117  trans_x = 0;
118 
119  else if (trans_x < 0 && vfwd < 0)
120  trans_x = 0;
121  }
122  }
123 
124  if (halt && flcs_operative) {
125  if (ty == 0) {
126  double vfwd = ship->Heading() * ship->Velocity();
127  double vmag = fabs(vfwd);
128 
129  if (vmag > 0) {
130  if (vfwd > 0)
131  trans_y = -trans_y_limit;
132  else
133  trans_y = trans_y_limit;
134 
135  if (vfwd < vlimit/2)
136  trans_y *= (vmag/(vlimit/2));
137  }
138  }
139  }
140 
141 
142  // auto thrust to align flight path with orientation:
143  if (tz == 0) {
144  if (flcs_operative)
145  trans_z = (ship->Velocity() * ship->LiftLine() * -200);
146 
147  else
148  trans_z = 0;
149  }
150 
151  // manual thrust up to vlimit/2:
152  else {
153  double vfwd = ship->LiftLine() * ship->Velocity();
154 
155  if (fabs(vfwd) >= vlimit) {
156  if (trans_z > 0 && vfwd > 0)
157  trans_z = 0;
158 
159  else if (trans_z < 0 && vfwd < 0)
160  trans_z = 0;
161  }
162  }
163  }
164 
165  // ----------------------------------------------------------
166  // STARSHIP HELM MODE
167  // ----------------------------------------------------------
168 
169  else if (mode == Ship::FLCS_HELM) {
170  if (flcs_operative) {
171  double compass_heading = ship->CompassHeading();
172  double compass_pitch = ship->CompassPitch();
173  // rotate helm into compass orientation:
174  double helm = ship->GetHelmHeading() - compass_heading;
175 
176  if (helm > PI)
177  helm -= 2*PI;
178  else if (helm < -PI)
179  helm += 2*PI;
180 
181  // turn to align with helm heading:
182  if (helm != 0)
183  ship->ApplyYaw(helm);
184 
185  // pitch to align with helm pitch:
186  if (compass_pitch != ship->GetHelmPitch())
187  ship->ApplyPitch(compass_pitch - ship->GetHelmPitch());
188 
189  // roll to align with world coordinates:
190  if (ship->Design()->auto_roll > 0) {
191  Point vrt = ship->Cam().vrt();
192  double deflection = vrt.y;
193 
194  if (fabs(helm) < PI/16 || ship->Design()->turn_bank < 0.01) {
195  if (ship->Design()->auto_roll > 1) {
196  ship->ApplyRoll(0.5);
197  }
198  else if (deflection != 0) {
199  double theta = asin(deflection);
200  ship->ApplyRoll(-theta);
201  }
202  }
203 
204  // else roll through turn maneuvers:
205  else {
206  double desired_bank = ship->Design()->turn_bank;
207 
208  if (helm >= 0)
209  desired_bank = -desired_bank;
210 
211  double current_bank = asin(deflection);
212  double theta = desired_bank - current_bank;
213  ship->ApplyRoll(theta);
214 
215  // coordinate the turn:
216  if (current_bank < 0 && desired_bank < 0 ||
217  current_bank > 0 && desired_bank > 0) {
218 
219  double coord_pitch = compass_pitch
220  - ship->GetHelmPitch()
221  - fabs(helm) * fabs(current_bank);
222  ship->ApplyPitch(coord_pitch);
223  }
224  }
225  }
226  }
227 
228  // flcs inoperative, set helm heading based on actual compass heading:
229  else {
232  }
233 
234  // auto thrust to align flight path with helm order:
235  if (tx == 0) {
236  if (flcs_operative)
237  trans_x = (ship->Velocity() * ship->BeamLine() * ship->Mass() * -1);
238 
239  else
240  trans_x = 0;
241  }
242 
243  // manual thrust up to vlimit/2:
244  else {
245  double vfwd = ship->BeamLine() * ship->Velocity();
246 
247  if (fabs(vfwd) >= vlimit/2) {
248  if (trans_x > 0 && vfwd > 0)
249  trans_x = 0;
250 
251  else if (trans_x < 0 && vfwd < 0)
252  trans_x = 0;
253  }
254  }
255 
256  if (trans_y == 0 && halt) {
257  double vfwd = ship->Heading() * ship->Velocity();
258  double vdesired = 0;
259 
260  if (vfwd > vdesired) {
261  trans_y = -trans_y_limit;
262 
263  if (!flcs_operative)
264  trans_y = 0;
265 
266  double vdelta = vfwd-vdesired;
267  if (vdelta < vlimit/2)
268  trans_y *= (vdelta/(vlimit/2));
269  }
270  }
271 
272 
273 
274  // auto thrust to align flight path with helm order:
275  if (tz == 0) {
276  if (flcs_operative)
277  trans_z = (ship->Velocity() * ship->LiftLine() * ship->Mass() * -1);
278 
279  else
280  trans_z = 0;
281  }
282 
283  // manual thrust up to vlimit/2:
284  else {
285  double vfwd = ship->LiftLine() * ship->Velocity();
286 
287  if (fabs(vfwd) > vlimit/2) {
288  if (trans_z > 0 && vfwd > 0)
289  trans_z = 0;
290 
291  else if (trans_z < 0 && vfwd < 0)
292  trans_z = 0;
293  }
294  }
295  }
296 
297  if (ship->GetThruster()) {
298  ship->GetThruster()->ExecTrans(trans_x, trans_y, trans_z);
299  }
300  else {
301  ship->SetTransX(trans_x);
302  ship->SetTransY(trans_y);
303  ship->SetTransZ(trans_z);
304  }
305 }