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