Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
FlightPlanner.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: FlightPlanner.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Flight Planning class for creating navpoint routes for fighter elements.
13  Used both by the CarrierAI class and the Flight Dialog.
14 */
15 
16 #include "MemDebug.h"
17 #include "FlightPlanner.h"
18 #include "Ship.h"
19 #include "ShipDesign.h"
20 #include "Element.h"
21 #include "Instruction.h"
22 #include "RadioMessage.h"
23 #include "RadioTraffic.h"
24 #include "Hangar.h"
25 #include "FlightDeck.h"
26 #include "Mission.h"
27 #include "Contact.h"
28 #include "Sim.h"
29 #include "StarSystem.h"
30 #include "Callsign.h"
31 
32 #include "Game.h"
33 #include "Random.h"
34 
35 // +----------------------------------------------------------------------+
36 
38 : sim(0), ship(s)
39 {
40  sim = Sim::GetSim();
41 
42  patrol_range = (float) (250e3 + 10e3 * (int) Random(0, 8.9));
43 }
44 
46 { }
47 
48 // +--------------------------------------------------------------------+
49 
50 void
52 {
53  RLoc rloc;
54  Vec3 dummy(0,0,0);
55  Point loc = ship->Location();
56  double zone = ship->CompassHeading();
57  Instruction* instr = 0;
58 
59  if (ship->IsAirborne())
60  loc.y += 8e3;
61  else
62  loc.y += 1e3;
63 
64  loc = loc.OtherHand();
65 
66  if (index > 2)
67  zone += 170*DEGREES;
68  else if (index > 1)
69  zone += -90*DEGREES;
70  else if (index > 0)
71  zone += 90*DEGREES;
72 
73  rloc.SetReferenceLoc(0);
74  rloc.SetBaseLocation(loc);
75  rloc.SetDistance(30e3);
76  rloc.SetDistanceVar(0);
77  rloc.SetAzimuth(-10*DEGREES + zone);
78  rloc.SetAzimuthVar(0);
79 
80  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::VECTOR);
81  instr->SetSpeed(750);
82  instr->GetRLoc() = rloc;
83 
84  elem->AddNavPoint(instr);
85 
86  rloc.SetReferenceLoc(0);
87  rloc.SetBaseLocation(loc);
88  if (ship->IsAirborne())
89  rloc.SetDistance(140e3);
90  else
91  rloc.SetDistance(220e3);
92  rloc.SetDistanceVar(50e3);
93  rloc.SetAzimuth(-20*DEGREES + zone);
94  rloc.SetAzimuthVar(15*DEGREES);
95 
96  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::PATROL);
97  instr->SetSpeed(500);
98  instr->GetRLoc() = rloc;
99 
100  elem->AddNavPoint(instr);
101 
102  rloc.SetReferenceLoc(&instr->GetRLoc());
103  rloc.SetDistance(120e3);
104  rloc.SetDistanceVar(30e3);
105  rloc.SetAzimuth(60*DEGREES + zone);
106  rloc.SetAzimuthVar(20*DEGREES);
107 
108  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::PATROL);
109  instr->SetSpeed(350);
110  instr->GetRLoc() = rloc;
111 
112  elem->AddNavPoint(instr);
113 
114  rloc.SetReferenceLoc(&instr->GetRLoc());
115  rloc.SetDistance(120e3);
116  rloc.SetDistanceVar(30e3);
117  rloc.SetAzimuth(120*DEGREES + zone);
118  rloc.SetAzimuthVar(20*DEGREES);
119 
120  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::PATROL);
121  instr->SetSpeed(350);
122  instr->GetRLoc() = rloc;
123 
124  elem->AddNavPoint(instr);
125 
126  rloc.SetReferenceLoc(0);
127  rloc.SetBaseLocation(loc);
128  rloc.SetDistance(40e3);
129  rloc.SetDistanceVar(0);
130  rloc.SetAzimuth(180*DEGREES + ship->CompassHeading());
131  rloc.SetAzimuthVar(0*DEGREES);
132 
133  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::RTB);
134  instr->SetSpeed(500);
135  instr->GetRLoc() = rloc;
136 
137  elem->AddNavPoint(instr);
138 }
139 
140 // +--------------------------------------------------------------------+
141 
142 void
144 {
145  if (!elem) return;
146 
147  RLoc rloc;
148  Vec3 dummy(0,0,0);
149  Point loc = ship->Location();
150  double head = ship->CompassHeading() + 15*DEGREES;
151  double dist = 30e3;
152  Instruction* instr = 0;
153  Ship* tgt_ship = 0;
154 
155  if (ship->IsAirborne())
156  loc += ship->Cam().vup() * 8e3;
157  else
158  loc += ship->Cam().vup() * 1e3;
159 
160  loc = loc.OtherHand();
161 
162  if (target)
163  tgt_ship = target->GetShip(1);
164 
165  if (tgt_ship) {
166  double range = Point(tgt_ship->Location() - ship->Location()).length();
167 
168  if (range < 100e3)
169  dist = 20e3;
170  }
171 
172  rloc.SetReferenceLoc(0);
173  rloc.SetBaseLocation(loc);
174  rloc.SetDistance(dist);
175  rloc.SetDistanceVar(0);
176  rloc.SetAzimuth(head);
177  rloc.SetAzimuthVar(2*DEGREES);
178 
179  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::VECTOR);
180  instr->SetSpeed(750);
181  instr->GetRLoc() = rloc;
182 
183  elem->AddNavPoint(instr);
184 
185  if (tgt_ship) {
186  Ship* tgt_ship = target->GetShip(1);
187  Point tgt = tgt_ship->Location() + tgt_ship->Velocity() * 10;
188  Point mid = ship->Location() + (tgt - ship->Location()) * 0.5;
189  double beam = tgt_ship->CompassHeading() + 90*DEGREES;
190 
191  if (tgt_ship->IsAirborne())
192  tgt += tgt_ship->Cam().vup() * 8e3;
193  else
194  tgt += tgt_ship->Cam().vup() * 1e3;
195 
196  tgt = tgt.OtherHand();
197  mid = mid.OtherHand();
198 
199  if (tgt_ship && tgt_ship->IsStarship()) {
200  rloc.SetReferenceLoc(0);
201  rloc.SetBaseLocation(tgt);
202  rloc.SetDistance(60e3);
203  rloc.SetDistanceVar(5e3);
204  rloc.SetAzimuth(beam);
205  rloc.SetAzimuthVar(5*DEGREES);
206 
207  instr = new(__FILE__,__LINE__) Instruction(tgt_ship->GetRegion(), dummy, Instruction::ASSAULT);
208  instr->SetSpeed(750);
209  instr->GetRLoc() = rloc;
210  instr->SetTarget(target->Name());
212 
213  elem->AddNavPoint(instr);
214  }
215 
216  if (tgt_ship && tgt_ship->IsStatic()) {
217  rloc.SetReferenceLoc(0);
218  rloc.SetBaseLocation(mid);
219  rloc.SetDistance(60e3);
220  rloc.SetDistanceVar(5e3);
221  rloc.SetAzimuth(beam);
222  rloc.SetAzimuthVar(15*DEGREES);
223 
224  instr = new(__FILE__,__LINE__) Instruction(tgt_ship->GetRegion(), dummy, Instruction::VECTOR);
225  instr->SetSpeed(750);
226  instr->GetRLoc() = rloc;
227 
228  elem->AddNavPoint(instr);
229 
230  rloc.SetReferenceLoc(0);
231  rloc.SetBaseLocation(tgt);
232  rloc.SetDistance(40e3);
233  rloc.SetDistanceVar(5e3);
234  rloc.SetAzimuth(beam);
235  rloc.SetAzimuthVar(5*DEGREES);
236 
237  int action = Instruction::ASSAULT;
238 
239  if (tgt_ship->IsGroundUnit())
240  action = Instruction::STRIKE;
241 
242  instr = new(__FILE__,__LINE__) Instruction(tgt_ship->GetRegion(), dummy, action);
243  instr->SetSpeed(750);
244  instr->GetRLoc() = rloc;
245  instr->SetTarget(target->Name());
247 
248  elem->AddNavPoint(instr);
249  }
250 
251  else if (tgt_ship && tgt_ship->IsDropship()) {
252  rloc.SetReferenceLoc(0);
253  rloc.SetBaseLocation(tgt);
254  rloc.SetDistance(60e3);
255  rloc.SetDistanceVar(5e3);
256  rloc.SetAzimuth(tgt_ship->CompassHeading());
257  rloc.SetAzimuthVar(20*DEGREES);
258 
259  instr = new(__FILE__,__LINE__) Instruction(tgt_ship->GetRegion(), dummy, Instruction::INTERCEPT);
260  instr->SetSpeed(750);
261  instr->GetRLoc() = rloc;
262  instr->SetTarget(target->Name());
264 
265  elem->AddNavPoint(instr);
266  }
267  }
268 
269  rloc.SetReferenceLoc(0);
270  rloc.SetBaseLocation(loc);
271  rloc.SetDistance(40e3);
272  rloc.SetDistanceVar(0);
273  rloc.SetAzimuth(180*DEGREES + ship->CompassHeading());
274  rloc.SetAzimuthVar(0*DEGREES);
275 
276  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::RTB);
277  instr->SetSpeed(500);
278  instr->GetRLoc() = rloc;
279 
280  elem->AddNavPoint(instr);
281 }
282 
283 // +--------------------------------------------------------------------+
284 
285 void
287 {
288  if (!elem) return;
289 
290  RLoc rloc;
291  Vec3 dummy(0,0,0);
292  Point loc = ship->Location();
293  double head = ship->CompassHeading();
294  Instruction* instr = 0;
295 
296  if (ship->IsAirborne())
297  loc += ship->Cam().vup() * 8e3;
298  else
299  loc += ship->Cam().vup() * 1e3;
300 
301  loc = loc.OtherHand();
302 
303  rloc.SetReferenceLoc(0);
304  rloc.SetBaseLocation(loc);
305  rloc.SetDistance(30e3);
306  rloc.SetDistanceVar(0);
307  rloc.SetAzimuth(head);
308  rloc.SetAzimuthVar(0);
309 
310  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::VECTOR);
311  instr->SetSpeed(750);
312  instr->GetRLoc() = rloc;
313 
314  elem->AddNavPoint(instr);
315 
316  if (ward && ward->GetShip(1)) {
317  // follow ward's flight plan:
318  if (ward->GetFlightPlan().size()) {
319  ListIter<Instruction> iter = ward->GetFlightPlan();
320 
321  while (++iter) {
322  Instruction* ward_instr = iter.value();
323 
324  if (ward_instr->Action() != Instruction::RTB) {
325  rloc.SetReferenceLoc(&ward_instr->GetRLoc());
326  rloc.SetDistance(25e3);
327  rloc.SetDistanceVar(5e3);
328  rloc.SetAzimuth(0);
329  rloc.SetAzimuthVar(90*DEGREES);
330 
331  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::ESCORT);
332  instr->SetSpeed(350);
333  instr->GetRLoc() = rloc;
334  instr->SetTarget(ward->Name());
335 
336  elem->AddNavPoint(instr);
337  }
338  }
339  }
340 
341  // if ward has no flight plan, just go to a point nearby:
342  else {
343  rloc.SetReferenceLoc(0);
344  rloc.SetBaseLocation(ward->GetShip(1)->Location());
345  rloc.SetDistance(25e3);
346  rloc.SetDistanceVar(5e3);
347  rloc.SetAzimuth(0);
348  rloc.SetAzimuthVar(90*DEGREES);
349 
350  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::DEFEND);
351  instr->SetSpeed(500);
352  instr->GetRLoc() = rloc;
353  instr->SetTarget(ward->Name());
354  instr->SetHoldTime(15 * 60); // fifteen minutes
355 
356  elem->AddNavPoint(instr);
357  }
358  }
359 
360  rloc.SetReferenceLoc(0);
361  rloc.SetBaseLocation(loc);
362  rloc.SetDistance(40e3);
363  rloc.SetDistanceVar(0);
364  rloc.SetAzimuth(180*DEGREES + ship->CompassHeading());
365  rloc.SetAzimuthVar(0*DEGREES);
366 
367  instr = new(__FILE__,__LINE__) Instruction(ship->GetRegion(), dummy, Instruction::RTB);
368  instr->SetSpeed(500);
369  instr->GetRLoc() = rloc;
370 
371  elem->AddNavPoint(instr);
372 }