1 /*
2  * See Licensing and Copyright notice in naev.h
3  */
4 
5 
6 
7 #ifndef SPACE_H
8 #  define SPACE_H
9 
10 
11 #include "faction.h"
12 #include "opengl.h"
13 #include "pilot.h"
14 #include "economy.h"
15 #include "fleet.h"
16 #include "mission.h"
17 #include "tech.h"
18 
19 
20 #define SYSTEM_SIMULATE_TIME  15. /**< Time to simulate system before player is added. */
21 
22 #define MAX_HYPERSPACE_VEL    25 /**< Speed to brake to before jumping. */
23 
24 #define ASSET_VIRTUAL         0 /**< The asset is virtual. */
25 #define ASSET_REAL            1 /**< The asset is real. */
26 
27 
28 /*
29  * planet services
30  */
31 #define PLANET_SERVICE_LAND         (1<<0) /**< Can land. */
32 #define PLANET_SERVICE_INHABITED    (1<<1) /**< Planet is inhabited. */
33 #define PLANET_SERVICE_REFUEL       (1<<2) /**< Has refueling. */
34 #define PLANET_SERVICE_BAR          (1<<3) /**< Has bar and thus news. */
35 #define PLANET_SERVICE_MISSIONS     (1<<4) /**< Has mission computer. */
36 #define PLANET_SERVICE_COMMODITY    (1<<5) /**< Can trade commodities. */
37 #define PLANET_SERVICE_OUTFITS      (1<<6) /**< Can trade outfits. */
38 #define PLANET_SERVICE_SHIPYARD     (1<<7) /**< Can trade ships. */
39 #define PLANET_SERVICE_BLACKMARKET  (1<<8) /**< Disables license restrictions on goods. */
40 #define PLANET_SERVICES_MAX         (PLANET_SERVICE_BLACKMARKET<<1)
41 #define planet_hasService(p,s)      ((p)->services & s) /**< Checks if planet has service. */
42 #define planet_addService(p,s)      ((p)->services |= (s)) /**< Adds a planet service. */
43 #define planet_rmService(p,s)       ((p)->services &= ~(s)) /**< Removes a planet service. */
44 
45 
46 /*
47  * Planet flags.
48  */
49 #define PLANET_KNOWN       (1<<0) /**< Planet is known. */
50 #define PLANET_BLACKMARKET (1<<1) /**< Planet is a black market. */
51 #define planet_isFlag(p,f)    ((p)->flags & (f)) /**< Checks planet flag. */
52 #define planet_setFlag(p,f)   ((p)->flags |= (f)) /**< Sets a planet flag. */
53 #define planet_rmFlag(p,f)    ((p)->flags &= ~(f)) /**< Removes a planet flag. */
54 #define planet_isKnown(p)     planet_isFlag(p,PLANET_KNOWN) /**< Checks if planet is known. */
55 
56 
57 /**
58  * @struct Planet
59  *
60  * @brief Represents a planet.
61  */
62 typedef struct Planet_ {
63    int id; /**< Planet ID. */
64    char* name; /**< planet name */
65    Vector2d pos; /**< position in star system */
66    double radius; /**< Radius of the planet. */
67 
68    /* Planet details. */
69    char *class; /**< planet type */
70    int faction; /**< planet faction */
71    uint64_t population; /**< Population of the planet. */
72 
73    /* Asset details. */
74    double presenceAmount; /**< The amount of presence this asset exerts. */
75    int presenceRange; /**< The range of presence exertion of this asset. */
76    int real; /**< If the asset is tangible or not. */
77    double hide; /**< The ewarfare hide value for an asset. */
78 
79    /* Landing details. */
80    int land_override; /**< Forcibly allows the player to either be able to land or not (+1 is land, -1 is not, 0 otherwise). */
81    char *land_func; /**< Landing function to execute. */
82    int can_land; /**< Whether or not the player can land. */
83    char *land_msg; /**< Message on landing. */
84    credits_t bribe_price; /**< Cost of bribing. */
85    char *bribe_msg; /**< Bribe message. */
86    char *bribe_ack_msg; /**< Bribe ACK message. */
87    int bribed; /**< If planet has been bribed. */
88 
89    /* Landed details. */
90    char* description; /**< planet description */
91    char* bar_description; /**< spaceport bar description */
92    unsigned int services; /**< what services they offer */
93    Commodity **commodities; /**< what commodities they sell */
94    int ncommodities; /**< the amount they have */
95    tech_group_t *tech; /**< Planet tech. */
96 
97    /* Graphics. */
98    glTexture* gfx_space; /**< graphic in space */
99    char *gfx_spaceName; /**< Name to load texture quickly with. */
100    char *gfx_spacePath; /**< Name of the gfx_space for saving purposes. */
101    char *gfx_exterior; /**< Don't actually load the texture */
102    char *gfx_exteriorPath; /**< Name of the gfx_exterior for saving purposes. */
103 
104    /* Misc. */
105    unsigned int flags; /**< flags for planet properties */
106 } Planet;
107 
108 
109 /*
110  * star system flags
111  */
112 #define SYSTEM_KNOWN       (1<<0) /**< System is known. */
113 #define SYSTEM_MARKED      (1<<1) /**< System is marked by a regular mission. */
114 #define SYSTEM_CMARKED     (1<<2) /**< System is marked by a computer mission. */
115 #define SYSTEM_CLAIMED     (1<<3) /**< System is claimed by a mission. */
116 #define sys_isFlag(s,f)    ((s)->flags & (f)) /**< Checks system flag. */
117 #define sys_setFlag(s,f)   ((s)->flags |= (f)) /**< Sets a system flag. */
118 #define sys_rmFlag(s,f)    ((s)->flags &= ~(f)) /**< Removes a system flag. */
119 #define sys_isKnown(s)     sys_isFlag(s,SYSTEM_KNOWN) /**< Checks if system is known. */
120 #define sys_isMarked(s)    sys_isFlag(s,SYSTEM_MARKED) /**< Checks if system is marked. */
121 
122 
123 /*
124  * Forward declaration.
125  */
126 typedef struct StarSystem_ StarSystem;
127 
128 
129 /**
130  * @brief Represents presence in a system
131  */
132 typedef struct SystemPresence_ {
133    int faction; /**< Faction of this presence. */
134    double value; /**< Amount of presence. */
135    double curUsed; /**< Presence currently used. */
136    double timer; /**< Current faction timer. */
137    int disabled; /**< Whether or not spawning is disabled for this presence. */
138 } SystemPresence;
139 
140 
141 /*
142  * Jump point flags.
143  */
144 #define JP_AUTOPOS      (1<<0) /**< Automatically position jump point based on system radius. */
145 #define JP_KNOWN        (1<<1) /**< Jump point is known. */
146 #define JP_HIDDEN       (1<<2) /**< Jump point is hidden. */
147 #define JP_EXITONLY     (1<<3) /**< Jump point is exit only */
148 #define jp_isFlag(j,f)    ((j)->flags & (f)) /**< Checks jump flag. */
149 #define jp_setFlag(j,f)   ((j)->flags |= (f)) /**< Sets a jump flag. */
150 #define jp_rmFlag(j,f)    ((j)->flags &= ~(f)) /**< Removes a jump flag. */
151 #define jp_isKnown(j)     jp_isFlag(j,JP_KNOWN) /**< Checks if jump is known. */
152 #define jp_isUsable(j)    (jp_isKnown(j) && !jp_isFlag(j,JP_EXITONLY))
153 
154 
155 
156 /**
157  * @brief Represents a jump lane.
158  */
159 typedef struct JumpPoint_ {
160    StarSystem *target; /**< Target star system to jump to. */
161    int targetid; /**< ID of the target star system. */
162    Vector2d pos; /**< Position in the system. */
163    double radius; /**< Radius of jump range. */
164    unsigned int flags; /**< Flags related to the jump point's status. */
165    double hide; /**< ewarfare hide value for the jump point */
166    double angle; /**< Direction the jump is facing. */
167    double cosa; /**< Cosinus of the angle. */
168    double sina; /**< Sinus of the angle. */
169    int sx; /**< X sprite to use. */
170    int sy; /**< Y sprite to use. */
171 } JumpPoint;
172 extern glTexture *jumppoint_gfx; /**< Jump point graphics. */
173 
174 
175 /**
176  * @brief Represents a type of asteroid.
177  */
178 typedef struct AsteroidType_ {
179    char *ID; /**< ID ot the asteroid type. */
180    glTexture **gfxs; /**< asteroid possible gfxs. */
181    int ngfx; /**< nb of gfx. */
182 } AsteroidType;
183 
184 
185 /**
186  * @brief Represents a small player-rendered debris.
187  */
188 typedef struct Debris_ {
189    Vector2d pos; /**< Position. */
190    Vector2d vel; /**< Velocity. */
191    int gfxID; /**< ID of the asteroid gfx. */
192 } Debris;
193 
194 
195 /**
196  * @brief Represents a single asteroid.
197  */
198 typedef struct Asteroid_ {
199    Vector2d pos; /**< Position. */
200    Vector2d vel; /**< Velocity. */
201    int gfxID; /**< ID of the asteroid gfx. */
202    double timer; /**< Internal timer for animations. */
203    int appearing; /**< 1: appearing, 2: disappaering, 0 otherwise. */
204    int type; /**< The ID of the asteroid type */
205 } Asteroid;
206 extern glTexture **asteroid_gfx; /**< Asteroid graphics list. */
207 
208 
209 /**
210  * @brief Represents a convex subset of an asteroid field.
211  */
212 typedef struct AsteroidSubset_ {
213    Vector2d *corners; /**< Set of corners of the polygon. */
214    int ncorners; /**< Number of corners. */
215    Vector2d pos; /**< Center. */
216    double aera; /**< Subset's aera. */
217 } AsteroidSubset;
218 
219 
220 /**
221  * @brief Represents an asteroid field anchor.
222  */
223 typedef struct AsteroidAnchor_ {
224    Vector2d pos; /**< Position in the system. */
225    double density; /**< Density of the field. */
226    Asteroid *asteroids; /**< Asteroids belonging to the field. */
227    int nb; /**< Number of asteroids. */
228    Debris *debris; /**< Debris belonging to the field. */
229    int ndebris; /**< Number of debris. */
230    Vector2d *corners; /**< Set of corners of the polygon. */
231    int ncorners; /**< Number of corners. */
232    double aera; /**< Field's aera. */
233    AsteroidSubset *subsets; /**< Convex subsets. */
234    int nsubsets; /**< Number of convex subsets. */
235 } AsteroidAnchor;
236 
237 
238 /**
239  * @brief Represents a star system.
240  *
241  * The star system is the basic setting in Naev.
242  */
243 struct StarSystem_ {
244    int id; /**< Star system index. */
245 
246    /* General. */
247    char* name; /**< star system name */
248    Vector2d pos; /**< position */
249    int stars; /**< Amount of "stars" it has. */
250    double interference; /**< in % @todo implement interference. */
251    double nebu_density; /**< Nebula density (0. - 1000.) */
252    double nebu_volatility; /**< Nebula volatility (0. - 1000.) */
253    double radius; /**< Default system radius for standard jump points. */
254    char *background; /**< Background script. */
255 
256    /* Planets. */
257    Planet **planets; /**< planets */
258    int *planetsid; /**< IDs of the planets. */
259    int nplanets; /**< total number of planets */
260    int faction; /**< overall faction */
261 
262    /* Jumps. */
263    JumpPoint *jumps; /**< Jump points in the system */
264    int njumps; /**< number of adjacent jumps */
265 
266    /* Asteroids. */
267    AsteroidAnchor *asteroids; /**< Asteroids fields in the system */
268    int nasteroids; /**< number of asteroids fields */
269 
270    /* Fleets. */
271    Fleet** fleets; /**< fleets that can appear in the current system */
272    int nfleets; /**< total number of fleets */
273    double avg_pilot; /**< Target amount of pilots in the system. */
274 
275    /* Calculated. */
276    double *prices; /**< Handles the prices in the system. */
277 
278    /* Presence. */
279    SystemPresence *presence; /**< Pointer to an array of presences in this system. */
280    int npresence; /**< Number of elements in the presence array. */
281    int spilled; /**< If the system has been spilled to yet. */
282    double ownerpresence; /**< Amount of presence the owning faction has in a system. */
283 
284    /* Markers. */
285    int markers_computer; /**< Number of mission computer markers. */
286    int markers_low; /**< Number of low mission markers. */
287    int markers_high; /**< Number of high mission markers. */
288    int markers_plot; /**< Number of plot level mission markers. */
289 
290    /* Misc. */
291    unsigned int flags; /**< flags for system properties */
292 };
293 
294 
295 extern StarSystem *cur_system; /**< current star system */
296 extern int space_spawn; /**< 1 if spawning is enabled. */
297 
298 
299 /*
300  * loading/exiting
301  */
302 void space_init( const char* sysname );
303 int space_load (void);
304 void space_exit (void);
305 
306 /*
307  * planet stuff
308  */
309 Planet *planet_new (void);
310 char* planet_getSystem( const char* planetname );
311 Planet* planet_getAll( int *n );
312 Planet* planet_get( const char* planetname );
313 Planet* planet_getIndex( int ind );
314 void planet_setKnown( Planet *p );
315 int planet_index( const Planet *p );
316 int planet_exists( const char* planetname );
317 const char *planet_existsCase( const char* planetname );
318 char **planet_searchFuzzyCase( const char* planetname, int *n );
319 char* planet_getServiceName( int service );
320 int planet_getService( char *name );
321 credits_t planet_commodityPrice( const Planet *p, const Commodity *c );
322 /* Misc modification. */
323 int planet_setFaction( Planet *p, int faction );
324 /* Land related stuff. */
325 char planet_getColourChar( Planet *p );
326 const glColour* planet_getColour( Planet *p );
327 void planet_updateLand( Planet *p );
328 int planet_setRadiusFromGFX(Planet* planet);
329 
330 
331 /*
332  * jump stuff
333  */
334 JumpPoint* jump_get( const char* jumpname, const StarSystem* sys );
335 JumpPoint* jump_getTarget( StarSystem* target, const StarSystem* sys );
336 
337 /*
338  * system adding/removing stuff.
339  */
340 void system_reconstructJumps (StarSystem *sys);
341 void systems_reconstructJumps (void);
342 void systems_reconstructPlanets (void);
343 StarSystem *system_new (void);
344 int system_addPlanet( StarSystem *sys, const char *planetname );
345 int system_rmPlanet( StarSystem *sys, const char *planetname );
346 int system_addJump( StarSystem *sys, xmlNodePtr node );
347 int system_addJumpDiff( StarSystem *sys, xmlNodePtr node );
348 int system_rmJump( StarSystem *sys, const char *jumpname );
349 int system_addFleet( StarSystem *sys, Fleet *fleet );
350 int system_rmFleet( StarSystem *sys, Fleet *fleet );
351 
352 /*
353  * render
354  */
355 void space_render( const double dt );
356 void space_renderOverlay( const double dt );
357 void planets_render (void);
358 
359 /*
360  * Presence stuff.
361  */
362 void system_presenceCleanupAll( void );
363 void system_addPresence( StarSystem *sys, int faction, double amount, int range );
364 double system_getPresence( StarSystem *sys, int faction );
365 void system_addAllPlanetsPresence( StarSystem *sys );
366 void space_reconstructPresences( void );
367 void system_rmCurrentPresence( StarSystem *sys, int faction, double amount );
368 
369 /*
370  * update.
371  */
372 void space_update( const double dt );
373 
374 /*
375  * Graphics.
376  */
377 void space_gfxLoad( StarSystem *sys );
378 void space_gfxUnload( StarSystem *sys );
379 
380 /*
381  * Getting stuff.
382  */
383 StarSystem* system_getAll( int *nsys );
384 int system_exists( const char* sysname );
385 const char *system_existsCase( const char* sysname );
386 char **system_searchFuzzyCase( const char* sysname, int *n );
387 StarSystem* system_get( const char* sysname );
388 StarSystem* system_getIndex( int id );
389 int system_index( StarSystem *sys );
390 int space_sysReachable( StarSystem *sys );
391 int space_sysReallyReachable( char* sysname );
392 int space_sysReachableFromSys( StarSystem *target, StarSystem *sys );
393 char** space_getFactionPlanet( int *nplanets, int *factions, int nfactions, int landable );
394 char* space_getRndPlanet( int landable, unsigned int services,
395       int (*filter)(Planet *p));
396 double system_getClosest( const StarSystem *sys, int *pnt, int *jp, double x, double y );
397 double system_getClosestAng( const StarSystem *sys, int *pnt, int *jp, double x, double y, double ang );
398 
399 
400 /*
401  * Markers.
402  */
403 int space_addMarker( int sys, SysMarker type );
404 int space_rmMarker( int sys, SysMarker type );
405 void space_clearKnown (void);
406 void space_clearMarkers (void);
407 void space_clearComputerMarkers (void);
408 int system_hasPlanet( const StarSystem *sys );
409 
410 
411 /*
412  * Hyperspace.
413  */
414 int space_canHyperspace( Pilot* p);
415 int space_hyperspace( Pilot* p );
416 int space_calcJumpInPos( StarSystem *in, StarSystem *out, Vector2d *pos, Vector2d *vel, double *dir );
417 
418 
419 /*
420  * Misc.
421  */
422 int space_isInField ( Vector2d *p );
423 void system_setFaction( StarSystem *sys );
424 void space_factionChange (void);
425 
426 
427 #endif /* SPACE_H */
428