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