Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
QuantumDrive.cpp
Go to the documentation of this file.
1 /* Project Starshatter 5.0
2  Destroyer Studios LLC
3  Copyright © 1997-2007. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: QuantumDrive.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Quantum Drive class
13 */
14 
15 #include "MemDebug.h"
16 #include "QuantumDrive.h"
17 #include "Ship.h"
18 #include "Explosion.h"
19 #include "Drive.h"
20 #include "Sim.h"
21 #include "SimEvent.h"
22 #include "StarSystem.h"
23 
24 #include "Game.h"
25 #include "Random.h"
26 
27 // +----------------------------------------------------------------------+
28 
29 static int drive_value = 3;
30 
31 // +----------------------------------------------------------------------+
32 
33 QuantumDrive::QuantumDrive(SUBTYPE s, double cap, double rate)
34 : System(DRIVE, s, "Quantum", drive_value, (float) cap, (float) cap, (float) rate),
35 dst_rgn(0), active_state(ACTIVE_READY), warp_fov(1), jump_time(0), countdown(5)
36 {
37  name = Game::GetText("sys.quantum");
38  abrv = Game::GetText("sys.quantum.abrv");
39 
40  emcon_power[0] = 0;
41  emcon_power[1] = 0;
42  emcon_power[2] = 100;
43 }
44 
45 // +----------------------------------------------------------------------+
46 
48 : System(d),
49 dst_rgn(0), active_state(ACTIVE_READY), warp_fov(1), jump_time(0),
50 countdown(d.countdown)
51 {
52  Mount(d);
54 
55  energy = capacity;
56 }
57 
58 // +--------------------------------------------------------------------+
59 
61 { }
62 
63 // +--------------------------------------------------------------------+
64 
65 void
67 {
68  dst_rgn = rgn;
69  dst_loc = loc;
70 }
71 
72 // +--------------------------------------------------------------------+
73 
74 bool
75 QuantumDrive::Engage(bool immediate)
76 {
77  if (active_state == ACTIVE_READY && ship != 0 &&
78  IsPowerOn() && Status() == NOMINAL && energy == capacity) {
79 
81  if (immediate) {
82  jump_time = 1;
83  return true;
84  }
85 
87 
88  SimRegion* rgn = ship->GetRegion();
89 
90  ListIter<Ship> s_iter = rgn->Ships();
91  while (++s_iter) {
92  Ship* s = s_iter.value();
93 
94  if (s != ship) {
95  double dist = Point(s->Location() - ship->Location()).length();
96 
97  if (dist < 25e3)
98  jump_time += 5;
99 
100  else if (dist < 50e3)
101  jump_time += 2;
102 
103  else if (dist < 100e3)
104  jump_time += 1;
105 
106  else if (dist < 200e3)
107  jump_time += 0.5;
108  }
109  }
110 
111  return true;
112  }
113 
114  return false;
115 }
116 
117 void
119 {
121  AbortJump();
122 }
123 
124 void
126 {
128  jump_time = 0;
129  energy = 0;
130  warp_fov = 1;
131 
133 
134  SimRegion* r = ship->GetRegion();
135  ListIter<Ship> neighbor = r->Ships();
136 
137  while (++neighbor) {
138  if (neighbor->IsDropship()) {
139  Ship* s = neighbor.value();
140  Point delta = s->Location() - ship->Location();
141 
142  if (delta.length() < 5e3)
143  s->SetWarp(warp_fov);
144  }
145  }
146 }
147 
148 // +--------------------------------------------------------------------+
149 
150 void
151 QuantumDrive::ExecFrame(double seconds)
152 {
153  System::ExecFrame(seconds);
154 
155  if (active_state == ACTIVE_READY)
156  return;
157 
158  if (ship) {
159  bool warping = false;
160 
162  if (jump_time > 0) {
163  jump_time -= seconds;
164  }
165 
166  else {
167  jump_time = 0;
169  }
170  }
171 
172  else if (active_state == ACTIVE_PREWARP) {
173  if (warp_fov < 5000) {
174  warp_fov *= 1.5;
175  }
176  else {
177  Jump();
178  energy = 0.0f;
179  }
180 
181  warping = true;
182  }
183 
184  else if (active_state == ACTIVE_POSTWARP) {
185  if (warp_fov > 1) {
186  warp_fov *= 0.75;
187  }
188  else {
189  warp_fov = 1;
191  }
192 
193  warping = true;
194  }
195 
196  if (warping) {
198 
199  SimRegion* r = ship->GetRegion();
200  ListIter<Ship> neighbor = r->Ships();
201 
202  while (++neighbor) {
203  if (neighbor->IsDropship()) {
204  Ship* s = neighbor.value();
205  Point delta = s->Location() - ship->Location();
206 
207  if (delta.length() < 5e3)
208  s->SetWarp(warp_fov);
209  }
210  }
211  }
212  }
213 }
214 
215 // +--------------------------------------------------------------------+
216 
217 void
219 {
220  Sim* sim = Sim::GetSim();
221 
222  if (ship && sim) {
223  double dist = 150e3 + Random(0, 60e3);
224  Point esc_vec = dst_rgn->GetOrbitalRegion()->Location() -
226 
227  esc_vec.Normalize();
228  esc_vec *= dist;
229  esc_vec += RandomDirection() * Random(15e3, 22e3);
230 
231  if (subtype == HYPER)
233  else
235 
236  sim->RequestHyperJump(ship, dst_rgn, esc_vec);
237 
238  ShipStats* stats = ShipStats::Find(ship->Name());
240  }
241 
242  dst_rgn = 0;
243  dst_loc = Point();
244 
246 }