Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Farcaster.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: Farcaster.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12 */
13 
14 #include "MemDebug.h"
15 #include "Farcaster.h"
16 #include "QuantumDrive.h"
17 #include "Ship.h"
18 #include "ShipDesign.h"
19 #include "Explosion.h"
20 #include "Sim.h"
21 #include "Element.h"
22 #include "Instruction.h"
23 
24 #include "Game.h"
25 #include "Solid.h"
26 #include "Light.h"
27 #include "Sound.h"
28 #include "DataLoader.h"
29 
30 // +======================================================================+
31 
32 Farcaster::Farcaster(double cap, double rate)
33 : System(FARCASTER, 0, "Farcaster", 1, (float) cap, (float) cap, (float) rate),
34 ship(0), dest(0), jumpship(0), cycle_time(10),
35 active_state(QuantumDrive::ACTIVE_READY), warp_fov(1), no_dest(false)
36 {
37  name = Game::GetText("sys.farcaster");
38  abrv = Game::GetText("sys.farcaster.abrv");
39 }
40 
41 // +----------------------------------------------------------------------+
42 
44 : System(s),
45 ship(0), dest(0), start_rel(s.start_rel),
46 end_rel(s.end_rel), jumpship(0), cycle_time(s.cycle_time),
47 active_state(QuantumDrive::ACTIVE_READY), warp_fov(1), no_dest(false)
48 {
49  Mount(s);
51 
52  for (int i = 0; i < NUM_APPROACH_PTS; i++)
53  approach_rel[i] = s.approach_rel[i];
54 }
55 
56 // +--------------------------------------------------------------------+
57 
59 {
60 }
61 
62 // +--------------------------------------------------------------------+
63 
64 void
65 Farcaster::ExecFrame(double seconds)
66 {
67  System::ExecFrame(seconds);
68 
69  if (ship && !no_dest) {
70  if (!dest) {
71  Element* elem = ship->GetElement();
72 
73  if (elem->NumObjectives()) {
74  Sim* sim = Sim::GetSim();
75  Instruction* obj = elem->GetObjective(0);
76 
77  if (obj)
78  dest = sim->FindShip(obj->TargetName());
79  }
80 
81  if (!dest)
82  no_dest = true;
83  }
84  else {
85  if (dest->IsDying() || dest->IsDead()) {
86  dest = 0;
87  no_dest = true;
88  }
89  }
90  }
91 
92  // if no destination, show red nav lights:
93  if (no_dest)
94  energy = 0.0f;
95 
97  ship && ship->GetRegion() && dest && dest->GetRegion()) {
98  SimRegion* rgn = ship->GetRegion();
99  SimRegion* dst = dest->GetRegion();
100  ListIter<Ship> s_iter = rgn->Ships();
101 
102  jumpship = 0;
103 
104  while (++s_iter) {
105  Ship* s = s_iter.value();
106 
107  if (s == ship || s->IsStatic() || s->WarpFactor() > 1)
108  continue;
109 
110  Point delta = s->Location() - ship->Location();
111 
112  // activate:
113  if (delta.length() < 1000) {
115  jumpship = s;
116  Observe(jumpship);
117  break;
118  }
119  }
120  }
121 
123  return;
124 
125  if (ship) {
126  bool warping = false;
127 
129  if (warp_fov < 5000) {
130  warp_fov *= 1.5;
131  }
132  else {
133  Jump();
134  }
135 
136  warping = true;
137  }
138 
140  if (warp_fov > 1) {
141  warp_fov *= 0.75;
142  }
143  else {
144  warp_fov = 1;
146  }
147 
148  warping = true;
149  }
150 
151  if (jumpship) {
152  if (warping) {
154 
155  SimRegion* r = ship->GetRegion();
156  ListIter<Ship> neighbor = r->Ships();
157 
158  while (++neighbor) {
159  if (neighbor->IsDropship()) {
160  Ship* s = neighbor.value();
161  Point delta = s->Location() - ship->Location();
162 
163  if (delta.length() < 5e3)
164  s->SetWarp(warp_fov);
165  }
166  }
167  }
168  else {
169  warp_fov = 1;
171  }
172  }
173  }
174 }
175 
176 void
178 {
179  Sim* sim = Sim::GetSim();
180  SimRegion* rgn = ship->GetRegion();
181  SimRegion* dst = dest->GetRegion();
182 
183  sim->CreateExplosion(jumpship->Location(), Point(0,0,0),
184  Explosion::QUANTUM_FLASH, 1.0f, 0, rgn);
185  sim->RequestHyperJump(jumpship, dst, dest->Location().OtherHand(), 0, ship, dest);
186 
187  energy = 0.0f;
188 
189  Farcaster* f = dest->GetFarcaster();
190  if (f) f->Arrive(jumpship);
191 
193  warp_fov = 1;
194  jumpship = 0;
195 }
196 
197 void
199 {
200  energy = 0.0f;
201 
203  warp_fov = 5000;
204  jumpship = s;
205 
206  if (jumpship && jumpship->Velocity().length() < 500) {
208  }
209 }
210 
211 // +----------------------------------------------------------------------+
212 
213 void
215 {
216  if (i >= 0 && i < NUM_APPROACH_PTS)
217  approach_rel[i] = loc;
218 }
219 
220 void
222 {
223  start_rel = loc;
224 }
225 
226 void
228 {
229  end_rel = loc;
230 }
231 
232 // +----------------------------------------------------------------------+
233 
234 void
236 {
237  cycle_time = t;
238 }
239 
240 // +----------------------------------------------------------------------+
241 
242 void
244 {
245  System::Orient(rep);
246 
247  Matrix orientation = rep->Cam().Orientation();
248  Point loc = rep->Location();
249 
250  start_point = (start_rel * orientation) + loc;
251  end_point = (end_rel * orientation) + loc;
252 
253  for (int i = 0; i < NUM_APPROACH_PTS; i++)
254  approach_point[i] = (approach_rel[i] * orientation) + loc;
255 }
256 
257 // +----------------------------------------------------------------------+
258 
259 bool
261 {
262  if (obj == jumpship) {
263  jumpship->SetWarp(1);
264 
265  SimRegion* r = ship->GetRegion();
266  ListIter<Ship> neighbor = r->Ships();
267 
268  while (++neighbor) {
269  if (neighbor->IsDropship()) {
270  Ship* s = neighbor.value();
271  Point delta = s->Location() - ship->Location();
272 
273  if (delta.length() < 5e3)
274  s->SetWarp(1);
275  }
276  }
277 
278  jumpship = 0;
279  }
280 
281  return SimObserver::Update(obj);
282 }
283 
284 const char*
286 {
287  return Name();
288 }