More elimination of FORTAN-style if statements.
[super-star-trek.git] / src / sst.h
1 #ifndef __SST_H__
2
3 #include <stdio.h>
4 #include <math.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <locale.h>
8 #include <libintl.h>
9 #include <curses.h>
10 #include <stdbool.h>
11
12 #ifdef DATA_DIR
13 #define SSTDOC DATA_DIR"/"DOC_NAME
14 #else
15 #define SSTDOC DOC_NAME
16 #endif
17
18 #define _(str) gettext(str)
19
20 #define min(x, y)       ((x)<(y)?(x):(y))
21 #define max(x, y)       ((x)>(y)?(x):(y))
22
23 #define PHASEFAC (2.0)
24 #define GALSIZE (8)
25 #define NINHAB (GALSIZE * GALSIZE / 2)
26 #define MAXUNINHAB (10)
27 #define PLNETMAX (NINHAB + MAXUNINHAB)
28 #define QUADSIZE (10)
29 #define BASEMAX (5)
30
31 /*
32  * These macros hide the difference between 0-origin and 1-origin addressing.
33  * They're a step towards de-FORTRANizing the code.
34  */
35 #define VALID_QUADRANT(x, y)    ((x)>=1 && (x)<=GALSIZE && (y)>=1 && (y)<=GALSIZE)
36 #define VALID_SECTOR(x, y)      ((x)>=1 && (x)<=QUADSIZE && (y)>=1 && (y)<=QUADSIZE)
37 #define for_quadrants(i)        for (i = 1; i <= GALSIZE; i++)
38 #define for_sectors(i)          for (i = 1; i <= QUADSIZE; i++)
39 #define for_commanders(i)       for (i = 1; i <= game.state.remcom; i++)
40 #define for_local_enemies(i)    for (i = 1; i <= game.nenhere; i++)
41 #define for_starbases(i)        for (i = 1; i <= game.state.rembase; i++)
42
43 typedef struct {int x; int y;} coord;
44
45 #define square(i)               ((i)*(i))
46 #define same(c1, c2)            ((c1.x == c2.x) && (c1.y == c2.y))
47 #define distance(c1, c2)        sqrt(square(c1.x - c2.x) + square(c1.y - c2.y))
48 #define invalidate(w)           w.x = w.y = 0
49 #define is_valid(w)             (w.x != 0 && w.y != 0)
50
51 typedef struct {
52     coord w;
53     enum {M=0, N=1, O=2} pclass;
54     int inhabited;      /* if NZ, an index into a name array */
55 #define UNINHABITED     -1
56     enum {mined=-1, present=0, absent=1} crystals; /* has crystals */
57     enum {unknown, known, shuttle_down} known;
58 } planet;
59
60 #define DESTROY(pl)     memset(pl, '\0', sizeof(planet))
61
62 typedef enum {
63     IHR = 'R',
64     IHK = 'K',
65     IHC = 'C',
66     IHS = 'S',
67     IHSTAR = '*',
68     IHP = 'P',
69     IHW = '@',
70     IHB = 'B',
71     IHBLANK = ' ',
72     IHDOT = '.',
73     IHQUEST = '?',
74     IHE = 'E',
75     IHF = 'F',
76     IHT = 'T',
77     IHWEB = '#',
78     IHMATER0 = '-',
79     IHMATER1 = 'o',
80     IHMATER2 = '0',
81 } feature;
82
83 typedef struct {
84     bool snap;                  // snapshot taken
85     int crew,                   // crew complement
86 #define FULLCREW        428     /* BSD Trek was 387, that's wrong */
87         remkl,                  // remaining klingons
88         remcom,                 // remaining commanders
89         nscrem,                 // remaining super commanders
90         rembase,                // remaining bases
91         starkl,                 // destroyed stars
92         basekl,                 // destroyed bases
93         nromrem,                // Romulans remaining
94         nplankl,                // destroyed uninhabited planets
95         nworldkl;               // destroyed inhabited planets
96     planet planets[PLNETMAX];   // Planet information
97     double date,                // stardate
98         remres,                 // remaining resources
99         remtime;                // remaining time
100     coord baseq[BASEMAX+1];     // Base quadrant coordinates
101     coord kcmdr[QUADSIZE+1];    // Commander quadrant coordinates
102     coord kscmdr;               // Supercommander quadrant coordinates
103     struct quadrant {
104         int stars;
105         int planet;
106 #define NOPLANET        -1
107         bool starbase;
108         int klingons;
109         int romulans;
110         bool supernova;
111         bool charted;
112         enum {secure, distressed, enslaved} status;
113     } galaxy[GALSIZE+1][GALSIZE+1];     // The Galaxy (subscript 0 not used)
114     struct page {
115         int stars;
116         bool starbase;
117         int klingons;
118     } chart[GALSIZE+1][GALSIZE+1];      // the starchart (subscript 0 not used)
119 } snapshot;                             // Data that is snapshot
120
121 #define MAXKLGAME       127
122 #define MAXKLQUAD       9
123
124 #define NKILLK (game.inkling - game.state.remkl)
125 #define NKILLC (game.incom - game.state.remcom)
126 #define NKILLSC (game.inscom - game.state.nscrem)
127 #define NKILLROM (game.inrom - game.state.nromrem)
128 #define KLINGREM (game.state.remkl + game.state.remcom + game.state.nscrem)
129 #define INKLINGTOT (game.inkling + game.incom + game.inscom)
130 #define KLINGKILLED (INKLINGTOT - KLINGREM)
131
132 /* game options */
133 #define OPTION_ALL      0xffffffff
134 #define OPTION_TTY      0x00000001      /* old interface */
135 #define OPTION_CURSES   0x00000002      /* new interface */
136 #define OPTION_IOMODES  0x00000003      /* cover both interfaces */
137 #define OPTION_PLANETS  0x00000004      /* planets and mining */
138 #define OPTION_THOLIAN  0x00000008      /* Tholians and their webs */
139 #define OPTION_THINGY   0x00000010      /* Space Thingy can shoot back */
140 #define OPTION_PROBE    0x00000020      /* deep-space probes */
141 #define OPTION_SHOWME   0x00000040      /* bracket Enterprise in chart */
142 #define OPTION_RAMMING  0x00000080      /* enemies may ram Enterprise */
143 #define OPTION_MVBADDY  0x00000100      /* more enemies can move */
144 #define OPTION_BLKHOLE  0x00000200      /* black hole may timewarp you */
145 #define OPTION_BASE     0x00000400      /* bases have good shields */
146 #define OPTION_WORLDS   0x00000800      /* logic for inhabited worlds */
147 #define OPTION_PLAIN    0x01000000      /* user chose plain game */
148 #define OPTION_ALMY     0x02000000      /* user chose Almy variant */
149
150 /* Define devices */
151 #define DSRSENS 0
152 #define DLRSENS 1
153 #define DPHASER 2
154 #define DPHOTON 3
155 #define DLIFSUP 4
156 #define DWARPEN 5
157 #define DIMPULS 6
158 #define DSHIELD 7
159 #define DRADIO  8
160 #define DSHUTTL 9
161 #define DCOMPTR 10
162 #define DNAVSYS 11
163 #define DTRANSP 12
164 #define DSHCTRL 13
165 #define DDRAY   14
166 #define DDSP    15
167 #define NDEVICES (16)   // Number of devices
168
169 #define damaged(dev)    (game.damage[dev] != 0.0)
170
171 #define FOREVER 1e30
172
173 /* Define future events */
174 #define FSPY    0       // Spy event happens always (no future[] entry)
175                                         // can cause SC to tractor beam Enterprise
176 #define FSNOVA  1   // Supernova
177 #define FTBEAM  2   // Commander tractor beams Enterprise
178 #define FSNAP   3   // Snapshot for time warp
179 #define FBATTAK 4   // Commander attacks base
180 #define FCDBAS  5   // Commander destroys base
181 #define FSCMOVE 6   // Supercommander moves (might attack base)
182 #define FSCDBAS 7   // Supercommander destroys base
183 #define FDSPROB 8   // Move deep space probe
184 #define FDISTR  9   // Emit distress call from an inhabited world 
185 #define FENSLV  10  // Inhabited word is enslaved */
186 #define FREPRO  11  // Klingons build a ship in an enslaved system
187 #define NEVENTS (12)
188
189 typedef struct {
190     double date;
191     coord quadrant;
192 } event;
193
194 /*
195  * abstract out the event handling -- underlying data structures will change
196  * when we implement stateful events
197  */
198 extern event *unschedule(int);
199 extern int is_scheduled(int);
200 extern event *schedule(int, double);
201 extern void postpone(int, double);
202 extern double scheduled(int);
203 #define findevent(evtype)       &game.future[evtype]
204
205 #define SSTMAGIC        "SST2.0\n"
206
207 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
208
209 struct game {
210     char magic[sizeof(SSTMAGIC)];
211     unsigned long options;
212     snapshot state;
213     snapshot snapsht;
214     feature quad[QUADSIZE+1][QUADSIZE+1];               // contents of our quadrant
215     double kpower[(QUADSIZE+1)*(QUADSIZE+1)];           // enemy energy levels
216     double kdist[(QUADSIZE+1)*(QUADSIZE+1)];            // enemy distances
217     double kavgd[(QUADSIZE+1)*(QUADSIZE+1)];            // average distances
218     double damage[NDEVICES];    // damage encountered
219     event future[NEVENTS];      // future events
220     char passwd[10];            // Self Destruct password
221     coord ks[(QUADSIZE+1)*(QUADSIZE+1)];        // enemy sector locations
222     coord quadrant, sector;     // where we are
223     coord tholian;              // coordinates of Tholian
224     coord base;                 // position of base in current quadrant
225     coord battle;               // base coordinates being attacked
226     coord plnet;                // location of planet in quadrant
227     coord probec;       // current probe quadrant
228     bool gamewon,       // Finished!
229         ididit,         // action taken -- allows enemy to attack
230         alive,          // we are alive (not killed)
231         justin,         // just entered quadrant
232         shldup,         // shields are up
233         shldchg,        // shield is changing (affects efficiency)
234         comhere,        // commander here
235         ishere,         // super-commander in quadrant
236         iscate,         // super commander is here
237         ientesc,        // attempted escape from supercommander
238         ithere,         // Tholian is here 
239         resting,        // rest time
240         icraft,         // Kirk in Galileo
241         landed,         // party on planet (true), on ship (false)
242         alldone,        // game is now finished
243         neutz,          // Romulan Neutral Zone
244         isarmed,        // probe is armed
245         inorbit,        // orbiting a planet
246         imine,          // mining
247         icrystl,        // dilithium crystals aboard
248         iseenit,        // seen base attack report
249         thawed;         // thawed game
250     enum {
251         green,
252         yellow,
253         red,
254         docked,
255         dead,
256     } condition;                // condition (red/yellow/green/docked)
257     enum {
258         onship,
259         offship,
260         removed,
261     } iscraft;          // 'onship' if craft on ship, 'removed' if out of game
262     enum {
263         SKILL_NONE,
264         SKILL_NOVICE,
265         SKILL_FAIR,
266         SKILL_GOOD,
267         SKILL_EXPERT,
268         SKILL_EMERITUS,
269     } skill;            // skill level
270     int inkling,        // initial number of klingons
271         inbase,         // initial number of bases
272         incom,          // initial number of commanders
273         inscom,         // initial number of commanders
274         inrom,          // initial number of commanders
275         instar,         // initial stars
276         intorps,        // initial/max torpedoes
277         torps,          // number of torpedoes
278         ship,           // ship type -- 'E' is Enterprise
279         abandoned,      // count of crew abandoned in space
280         length,         // length of game
281         klhere,         // klingons here
282         casual,         // causalties
283         nhelp,          // calls for help
284         nkinks,         // count of energy-barrier crossings
285         iplnet,         // planet # in quadrant
286         inplan,         // initial planets
287         nenhere,        // number of enemies in quadrant
288         irhere,         // Romulans in quadrant
289         isatb,          // =1 if super commander is attacking base
290         tourn,          // tournament number
291         proben,         // number of moves for probe
292         nprobes;        // number of probes available
293     double inresor,     // initial resources
294         intime,         // initial time
295         inenrg,         // initial/max energy
296         inshld,         // initial/max shield
297         inlsr,          // initial life support resources
298         indate,         // initial date
299         energy,         // energy level
300         shield,         // shield level
301         warpfac,        // warp speed
302         wfacsq,         // squared warp factor
303         lsupres,        // life support reserves
304         dist,           // movement distance
305         direc,          // movement direction
306         optime,         // time taken by current operation
307         docfac,         // repair factor when docking (constant?)
308         damfac,         // damage factor
309         lastchart,      // time star chart was last updated
310         cryprob,        // probability that crystal will work
311         probex,         // location of probe
312         probey,         //
313         probeinx,       // probe x,y increment
314         probeiny,       //
315         height;         // height of orbit around planet
316 };
317 extern struct game game;
318
319 /* the following global state doesn't need to be saved */
320 extern char *device[NDEVICES];
321 extern char *systnames[NINHAB + 1];
322 extern int iscore, iskill; // Common PLAQ
323 extern double perdate;
324 extern double aaitem;
325 extern char citem[10];
326 extern int seed;
327 extern bool idebug;
328 extern FILE *logfp, *replayfp;
329
330 /* the Space Thingy's global state should *not* be saved! */
331 extern coord thing;
332 extern bool iqhere, iqengry;
333
334 typedef enum {
335     FWON, FDEPLETE, FLIFESUP, FNRG, FBATTLE,
336     FNEG3, FNOVA, FSNOVAED, FABANDN, FDILITHIUM,
337     FMATERIALIZE, FPHASER, FLOST, FMINING, FDPLANET,
338     FPNOVA, FSSC, FSTRACTOR, FDRAY, FTRIBBLE,
339     FHOLE, FCREW
340 } FINTYPE ;
341
342 enum loctype {neither, quadrant, sector};
343
344 /* Function prototypes */
345 extern void prelim(void);
346 extern void attack(bool);
347 extern bool choose(bool);
348 extern void setup(bool);
349 extern void score(void);
350 extern void atover(bool);
351 extern void srscan(void);
352 extern void lrscan(void);
353 extern void phasers(void);
354 extern void photon(void);
355 extern void warp(bool);
356 extern void doshield(bool);
357 extern void dock(bool);
358 extern void dreprt(void);
359 extern void chart(void);
360 extern void rechart(void);
361 extern void impuls(void);
362 extern void wait(void);
363 extern void setwrp(void);
364 extern void events(void);
365 extern void report(void);
366 extern void eta(void);
367 extern void mayday(void);
368 extern void abandn(void);
369 extern void finish(FINTYPE);
370 extern void selfdestruct(void);
371 extern void kaboom(void);
372 extern void freeze(bool);
373 extern bool thaw(void);
374 extern void plaque(void);
375 extern int scan(void);
376 extern void status(int req);
377 extern void request(void);
378 #define IHEOL (0)
379 #define IHALPHA (1)
380 #define IHREAL (2)
381 extern void chew(void);
382 extern void chew2(void);
383 extern void skip(int);
384 extern void prout(const char *, ...) __attribute__((format(printf, 1, 2)));
385 extern void proutn(const char *, ...) __attribute__((format(printf, 1, 2)));
386 extern void prouts(const char *, ...) __attribute__((format(printf, 1, 2)));
387 extern void prstat(const char *txt, const char *fmt, ...)
388         __attribute__((format(printf, 2, 3)));
389 extern void stars(void);
390 extern void newqad(bool);
391 extern bool ja(void);
392 extern void cramen(feature);
393 extern void crmshp(void);
394 extern char *cramlc(enum loctype, coord w);
395 extern double expran(double);
396 extern double Rand(void);
397 extern coord randplace(int);
398 extern coord dropin(feature);
399 extern void newcnd(void);
400 extern void sortkl(void);
401 extern void imove(bool);
402 extern void ram(bool, feature, coord);
403 extern void crmena(bool, feature, enum loctype, coord w);
404 extern void deadkl(coord, feature, coord);
405 extern void timwrp(void);
406 extern void moveklings(void);
407 extern void torpedo(double, double, coord, double *, int, int);
408 extern void huh(void);
409 extern void pause_reset(void);
410 extern void pause_game(bool);
411 extern void nova(coord);
412 extern void snova(bool, coord *);
413 extern void scom(void);
414 extern void hittem(double *);
415 extern bool isit(char *);
416 extern void preport(void);
417 extern void orbit(void);
418 extern void sensor(void);
419 extern void drawmaps(int);
420 extern void beam(void);
421 extern void mine(void);
422 extern void usecrystals(void);
423 extern void shuttle(void);
424 extern void deathray(void);
425 extern void debugme(void);
426 extern void attakreport(bool);
427 extern void movetho(void);
428 extern void probe(void);
429 extern void iostart(void);
430 extern void setwnd(WINDOW *);
431 extern void warble(void);
432 extern void boom(coord);
433 extern void tracktorpedo(coord, int, int, int, int);
434 extern void cgetline(char *, int);
435 extern void waitfor(void);
436 extern void setpassword(void);
437 extern void commandhook(char *, bool);
438 extern void makechart(void);
439 extern coord newkling(int);
440 #if BSD_BUG_FOR_BUG
441 extern void visual(void);
442 #endif
443
444 extern WINDOW *curwnd;
445 extern WINDOW *fullscreen_window;
446 extern WINDOW *srscan_window;
447 extern WINDOW *report_window;
448 extern WINDOW *lrscan_window;
449 extern WINDOW *message_window;
450 extern WINDOW *prompt_window;
451
452 extern void clreol(void);
453 extern void clrscr(void);
454 extern void textcolor(int);
455 extern void highvideo(void);
456
457 enum COLORS {
458    DEFAULT,
459    BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY,
460    DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE
461 };
462
463 #endif