Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Sim.h
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: Sim.h
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Simulation Universe and Region classes
13 */
14 
15 #ifndef Sim_h
16 #define Sim_h
17 
18 #include "Types.h"
19 #include "Universe.h"
20 #include "Scene.h"
21 #include "Physical.h"
22 #include "Geometry.h"
23 #include "List.h"
24 #include "Text.h"
25 
26 // +--------------------------------------------------------------------+
27 
28 class Sim;
29 class SimRegion;
30 class SimObject;
31 class SimObserver;
32 class SimHyper;
33 class SimSplash;
34 
35 class StarSystem;
36 class Orbital;
37 class OrbitalRegion;
38 class Asteroid;
39 
40 class NetGame;
41 
42 class CameraDirector;
43 class Contact;
44 class Ship;
45 class ShipDesign;
46 class System;
47 class Element;
48 class Shot;
49 class Drone;
50 class Explosion;
51 class Debris;
52 class WeaponDesign;
53 class MotionController;
54 class Dust;
55 class Grid;
56 class Mission;
57 class MissionElement;
58 class MissionEvent;
59 class Hangar;
60 class FlightDeck;
61 
62 class Terrain;
63 class TerrainPatch;
64 
65 class Model;
66 
67 // +--------------------------------------------------------------------+
68 
69 class Sim : public Universe
70 {
71  friend class SimRegion;
72 
73 public:
74  enum { REAL_SPACE, AIR_SPACE };
75 
77  virtual ~Sim();
78 
79  static Sim* GetSim() { return sim; }
80 
81  virtual void ExecFrame(double seconds);
82 
83  void LoadMission(Mission* msn, bool preload_textures=false);
84  void ExecMission();
85  void CommitMission();
86  void UnloadMission();
87 
88  void NextView();
89  void ShowGrid(int show = true);
90  bool GridShown() const;
91 
92  const char* FindAvailCallsign(int IFF);
93  Element* CreateElement(const char* callsign, int IFF, int type=0/*PATROL*/);
94  void DestroyElement(Element* elem);
95  Ship* CreateShip(const char* name,
96  const char* reg_num,
97  ShipDesign* design,
98  const char* rgn_name,
99  const Point& loc,
100  int IFF=0,
101  int cmd_ai=0,
102  const int* loadout=0);
103  Ship* FindShip(const char* name, const char* rgn_name=0);
104  Shot* CreateShot(const Point& pos, const Camera& shot_cam, WeaponDesign* d, const Ship* ship=0, SimRegion* rgn=0);
105  Explosion* CreateExplosion(const Point& pos, const Point& vel, int type, float exp_scale, float part_scale, SimRegion* rgn=0, SimObject* source=0, System* sys=0);
106  Debris* CreateDebris(const Point& pos, const Point& vel, Model* model, double mass, SimRegion* rgn=0);
107  Asteroid* CreateAsteroid(const Point& pos, int type, double mass, SimRegion* rgn=0);
108  void CreateSplashDamage(Ship* ship);
109  void CreateSplashDamage(Shot* shot);
110  void DestroyShip(Ship* ship);
111  void NetDockShip(Ship* ship, Ship* carrier, FlightDeck* deck);
112 
113  virtual Ship* FindShipByObjID(DWORD objid);
114  virtual Shot* FindShotByObjID(DWORD objid);
115 
116  Mission* GetMission() { return mission; }
119  SimRegion* FindRegion(const char* name);
124  SimRegion* FindNearestRegion(SimObject* object, int type);
125  bool ActivateRegion(SimRegion* rgn);
126 
127  void RequestHyperJump(Ship* obj,
128  SimRegion* rgn,
129  const Point& loc,
130  int type=0,
131  Ship* fc_src=0,
132  Ship* fc_dst=0);
133 
137  Scene* GetScene() { return &scene; }
138  Ship* GetPlayerShip();
140  Orbital* FindOrbitalBody(const char* name);
141 
142  void SetSelection(Ship* s);
143  bool IsSelected(Ship* s);
145  void ClearSelection();
146  void AddSelection(Ship* s);
147 
148  void SetTestMode(bool t=true);
149 
150  bool IsTestMode() const { return test_mode; }
151  bool IsNetGame() const { return netgame != 0; }
152  bool IsActive() const;
153  bool IsComplete() const;
154 
155  MotionController* GetControls() const { return ctrl; }
156 
157  Element* FindElement(const char* name);
159 
160  int GetAssignedElements(Element* elem, List<Element>& assigned);
161 
162  void SkipCutscene();
163  void ResolveTimeSkip(double seconds);
164  void ResolveHyperList();
165  void ResolveSplashList();
166 
167  void ExecEvents(double seconds);
168  void ProcessEventTrigger(int type, int event_id=0, const char* ship=0, int param=0);
169  double MissionClock() const;
170  DWORD StartTime() const { return start_time; }
171 
172  // Create a list of mission elements based on the current
173  // state of the simulation. Used for multiplayer join in progress.
175 
176 protected:
177  void CreateRegions();
178  void CreateElements();
179  void CopyEvents();
180  void BuildLinks();
181 
182  // Convert a single live element into a mission element
183  // that can be serialized over the net.
185  Hangar* FindSquadron(const char* name, int& index);
186 
187  static Sim* sim;
193 
202 
204 
205  bool test_mode;
208 
210  DWORD start_time;
211 };
212 
213 // +--------------------------------------------------------------------+
214 
216 {
217  friend class Sim;
218 
219 public:
220  static const char* TYPENAME() { return "SimRegion"; }
221 
223 
224  SimRegion(Sim* sim, const char* name, int type);
225  SimRegion(Sim* sim, OrbitalRegion* rgn);
226  virtual ~SimRegion();
227 
228  int operator == (const SimRegion& r) const { return (sim==r.sim) && (name==r.name); }
229  int operator < (const SimRegion& r) const;
230  int operator <= (const SimRegion& r) const;
231 
232  virtual void Activate();
233  virtual void Deactivate();
234  virtual void ExecFrame(double seconds);
235  void ShowGrid(int show = true);
236  void NextView();
237  Ship* FindShip(const char* name);
239  void SetPlayerShip(Ship* ship);
241  Terrain* GetTerrain() { return terrain; }
242  bool IsActive() const { return active; }
243  bool IsAirSpace() const { return type == AIR_SPACE; }
244  bool IsOrbital() const { return type == REAL_SPACE; }
245  bool CanTimeSkip()const;
246 
247  virtual Ship* FindShipByObjID(DWORD objid);
248  virtual Shot* FindShotByObjID(DWORD objid);
249 
250  virtual void InsertObject(Ship* ship);
251  virtual void InsertObject(Shot* shot);
252  virtual void InsertObject(Explosion* explosion);
253  virtual void InsertObject(Debris* debris);
254  virtual void InsertObject(Asteroid* asteroid);
255 
256  const char* Name() const { return name; }
257  int Type() const { return type; }
258  int NumShips() { return ships.size(); }
259  List<Ship>& Ships() { return ships; }
260  List<Ship>& Carriers() { return carriers; }
261  List<Shot>& Shots() { return shots; }
262  List<Drone>& Drones() { return drones; }
263  List<Debris>& Rocks() { return debris; }
266  List<SimRegion>& Links() { return links; }
268 
269  Point Location() const { return location; }
270 
271  void SetSelection(Ship* s);
272  bool IsSelected(Ship* s);
274  void ClearSelection();
275  void AddSelection(Ship* s);
276 
277  List<Contact>& TrackList(int iff);
278 
279  void ResolveTimeSkip(double seconds);
280 
281 protected:
282  void CommitMission();
283  void TranslateObject(SimObject* object);
284 
285  void AttachPlayerShip(int index);
286  void DestroyShips();
287  void DestroyShip(Ship* ship);
288  void NetDockShip(Ship* ship, Ship* carrier, FlightDeck* deck);
289 
290  void UpdateSky(double seconds, const Point& ref);
291  void UpdateShips(double seconds);
292  void UpdateShots(double seconds);
293  void UpdateExplosions(double seconds);
294  void UpdateTracks(double seconds);
295 
296  void DamageShips();
297  void CollideShips();
298  void CrashShips();
299  void DockShips();
300 
303  int type;
309  bool active;
310 
324 
325  DWORD sim_time;
326  int ai_index;
327 };
328 
329 #endif Sim_h
330