Experimental X code
[super-star-trek.git] / sst.h
1 #include <stdio.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <curses.h>
6 #ifndef INCLUDED
7 #define EXTERN extern
8 #else
9 #define EXTERN
10 #endif
11
12 #define min(x, y)       ((x)<(y)?(x):(y))
13 #define max(x, y)       ((x)>(y)?(x):(y))
14
15 // #define DEBUG
16
17 #define PHASEFAC (2.0)
18 #define PLNETMAX (10)
19 #define GALSIZE (8)
20 #define QUADSIZE (10)
21 #define BASEMAX (5)
22
23 /*
24  * These macros hide the difference between 0-origin and 1-origin addressing.
25  * They're a step towards de-FORTRANizing the code.
26  */
27 #define VALID_QUADRANT(x, y)    ((x)>=1 && (x)<=GALSIZE && (y)>=1 && (y)<=GALSIZE)
28 #define VALID_SECTOR(x, y)      ((x)>=1 && (x)<=QUADSIZE && (y)>=1 && (y)<=QUADSIZE)
29 #define for_quadrants(i)        for (i = 1; i <= GALSIZE; i++)
30 #define for_sectors(i)          for (i = 1; i <= QUADSIZE; i++)
31 #define for_commanders(i)       for (i = 1; i <= game.state.remcom; i++)
32 #define for_local_enemies(i)    for (i = 1; i <= nenhere; i++)
33 #define for_starbases(i)        for (i = 1; i <= game.state.rembase; i++)
34
35 typedef struct {
36     int x;      /* Quadrant location of planet */
37     int y;
38     enum {M=0, N=1, O=2} pclass;
39     int crystals; /* has crystals */
40     enum {unknown, known, shuttle_down} known;
41 } planet;
42
43 #define DESTROY(pl)     memset(pl, '\0', sizeof(planet))
44
45 typedef struct {
46     int snap,           // snapshot taken
47         remkl,                  // remaining klingons
48         remcom,                 // remaining commanders
49         nscrem,                 // remaining super commanders
50         rembase,                // remaining bases
51         starkl,                 // destroyed stars
52         basekl,                 // destroyed bases
53         cx[QUADSIZE+1],cy[QUADSIZE+1],  // Commander quadrant coordinates
54         baseqx[BASEMAX+1],              // Base quadrant X
55         baseqy[BASEMAX+1],              // Base quadrant Y
56         isx, isy,               // Coordinate of Super Commander
57         nromrem,                // Romulans remaining
58         nplankl;                // destroyed planets
59         planet plnets[PLNETMAX];  // Planet information
60         double date,            // stardate
61             remres,             // remaining resources
62             remtime;            // remaining time
63     struct {
64         int stars;
65         int planets;
66         int starbase;
67         int klingons;
68         int romulans;
69         int supernova;
70         int charted;
71     } galaxy[GALSIZE+1][GALSIZE+1];     // The Galaxy (subscript 0 not used)
72     struct {
73         int stars;
74         int starbase;
75         int klingons;
76     } chart[GALSIZE+1][GALSIZE+1];      // the starchart (subscript 0 not used)
77 } snapshot;                             // Data that is snapshot
78
79 #define NKILLK (inkling - game.state.remkl)
80 #define NKILLC (incom - game.state.remcom)
81 #define NKILLSC (inscom - game.state.nscrem)
82 #define NKILLROM (inrom - game.state.nromrem)
83 #define KLINGREM (game.state.remkl + game.state.remcom + game.state.nscrem)
84 #define INKLINGTOT (inkling + incom + inscom)
85 #define KLINGKILLED (INKLINGTOT - KLINGREM)
86
87 #define SKILL_NONE      0
88 #define SKILL_NOVICE    1
89 #define SKILL_FAIR      2
90 #define SKILL_GOOD      3
91 #define SKILL_EXPERT    4
92 #define SKILL_EMERITUS  5
93
94 /* game options */
95 #define OPTION_ALL      0xffffffff
96 #define OPTION_TTY      0x00000001      /* old interface */
97 #define OPTION_CURSES   0x00000002      /* new interface */
98 #define OPTION_IOMODES  0x00000003      /* cover both interfaces */
99 #define OPTION_PLANETS  0x00000004      /* planets and mining */
100 #define OPTION_THOLIAN  0x00000008      /* Tholians and their webs */
101 #define OPTION_THINGY   0x00000010      /* Space Thingy can shoot back */
102 #define OPTION_PROBE    0x00000020      /* deep-space probes */
103 #define OPTION_SHOWME   0x00000040      /* bracket Enterprise in chart */
104 #define OPTION_RAMMING  0x00000080      /* enemies may ram Enterprise */
105 #define OPTION_MVBADDY  0x00000100      /* more enemies can move */
106 #define OPTION_BLKHOLE  0x00000200      /* black hole may timewarp you */
107 #define OPTION_BASE     0x00000400      /* bases have good shields */
108 #define OPTION_PLAIN    0x01000000      /* user chose plain game */
109 #define OPTION_ALMY     0x02000000      /* user chose Almy variant */
110
111 /* Define devices */
112 #define DSRSENS 0
113 #define DLRSENS 1
114 #define DPHASER 2
115 #define DPHOTON 3
116 #define DLIFSUP 4
117 #define DWARPEN 5
118 #define DIMPULS 6
119 #define DSHIELD 7
120 #define DRADIO  8
121 #define DSHUTTL 9
122 #define DCOMPTR 10
123 #define DTRANSP 11
124 #define DSHCTRL 12
125 #define DDRAY   13  // Added deathray
126 #define DDSP    14  // Added deep space probe
127 #define NDEVICES (15)   // Number of devices
128
129 #define FOREVER 1e30
130
131 /* Define future events */
132 #define FSPY    0       // Spy event happens always (no future[] entry)
133                                         // can cause SC to tractor beam Enterprise
134 #define FSNOVA  1   // Supernova
135 #define FTBEAM  2   // Commander tractor beams Enterprise
136 #define FSNAP   3   // Snapshot for time warp
137 #define FBATTAK 4   // Commander attacks base
138 #define FCDBAS  5   // Commander destroys base
139 #define FSCMOVE 6   // Supercommander moves (might attack base)
140 #define FSCDBAS 7   // Supercommander destroys base
141 #define FDSPROB 8   // Move deep space probe
142 #define NEVENTS (9)
143
144 // Scalar variables that are needed for freezing the game
145 // are placed in a structure. #defines are used to access by their
146 // original names. Gee, I could have done this with the d structure,
147 // but I just didn't think of it back when I started.
148
149 #define SSTMAGIC        "SST2.0\n"
150
151 EXTERN WINDOW *curwnd;
152
153 EXTERN struct {
154     char magic[sizeof(SSTMAGIC)];
155     unsigned long options;
156     snapshot state;
157     snapshot snapsht;
158     char quad[QUADSIZE+1][QUADSIZE+1];          // contents of our quadrant
159     double kpower[(QUADSIZE+1)*(QUADSIZE+1)];           // enemy energy levels
160     double kdist[(QUADSIZE+1)*(QUADSIZE+1)];            // enemy distances
161     double kavgd[(QUADSIZE+1)*(QUADSIZE+1)];            // average distances
162     double damage[NDEVICES];    // damage encountered
163     double future[NEVENTS];     // future events
164     char passwd[10];            // Self Destruct password
165     int kx[(QUADSIZE+1)*(QUADSIZE+1)];                  // enemy sector locations
166     int ky[(QUADSIZE+1)*(QUADSIZE+1)];
167     /* members with macro definitions start here */
168     int inkling,
169         inbase,
170         incom,
171         inscom,
172         inrom,
173         instar,
174         intorps,
175         condit,
176         torps,
177         ship,
178         quadx,
179         quady,
180         sectx,
181         secty,
182         length,
183         skill,
184         basex,
185         basey,
186         klhere,
187         comhere,
188         casual,
189         nhelp,
190         nkinks,
191         ididit,
192         gamewon,
193         alive,
194         justin,
195         alldone,
196         shldchg,
197         plnetx,
198         plnety,
199         inorbit,
200         landed,
201         iplnet,
202         imine,
203         inplan,
204         nenhere,
205         ishere,
206         neutz,
207         irhere,
208         icraft,
209         ientesc,
210         iscraft,
211         isatb,
212         iscate,
213 #ifdef DEBUG
214         idebug,
215 #endif
216         iattak,
217         icrystl,
218         tourn,
219         thawed,
220         batx,
221         baty,
222         ithere,
223         ithx,
224         ithy,
225         iseenit,
226         probecx,
227         probecy,
228         proben,
229         isarmed,
230         nprobes;
231     double inresor,
232         intime,
233         inenrg,
234         inshld,
235         inlsr,
236         indate,
237         energy,
238         shield,
239         shldup,
240         warpfac,
241         wfacsq,
242         lsupres,
243         dist,
244         direc,
245         Time,
246         docfac,
247         resting,
248         damfac,
249         lastchart,
250         cryprob,
251         probex,
252         probey,
253         probeinx,
254         probeiny,
255         height;
256 } game;
257
258 #define inkling game.inkling            // Initial number of klingons
259 #define inbase game.inbase              // Initial number of bases
260 #define incom game.incom                // Initian number of commanders
261 #define inscom game.inscom              // Initian number of commanders
262 #define inrom game.inrom                // Initian number of commanders
263 #define instar game.instar              // Initial stars
264 #define intorps game.intorps            // Initial/Max torpedoes
265 #define condit game.condit              // Condition (red/yellow/green/docked)
266 #define torps game.torps                // number of torpedoes
267 #define ship game.ship                  // Ship type -- 'E' is Enterprise
268 #define quadx game.quadx                // where we are
269 #define quady game.quady                //
270 #define sectx game.sectx                // where we are
271 #define secty game.secty                //
272 #define length game.length              // length of game
273 #define skill game.skill                // skill level
274 #define basex game.basex                // position of base in current quad
275 #define basey game.basey                //
276 #define klhere game.klhere              // klingons here
277 #define comhere game.comhere            // commanders here
278 #define casual game.casual              // causalties
279 #define nhelp game.nhelp                // calls for help
280 #define nkinks game.nkinks              //
281 #define ididit game.ididit              // Action taken -- allows enemy to attack
282 #define gamewon game.gamewon            // Finished!
283 #define alive game.alive                // We are alive (not killed)
284 #define justin game.justin              // just entered quadrant
285 #define alldone game.alldone            // game is now finished
286 #define shldchg game.shldchg            // shield is changing (affects efficiency)
287 #define plnetx game.plnetx              // location of planet in quadrant
288 #define plnety game.plnety              //
289 #define inorbit game.inorbit            // orbiting
290 #define landed game.landed              // party on planet (1), on ship (-1)
291 #define iplnet game.iplnet              // planet # in quadrant
292 #define imine game.imine                // mining
293 #define inplan game.inplan              // initial planets
294 #define nenhere game.nenhere            // Number of enemies in quadrant
295 #define ishere game.ishere              // Super-commander in quandrant
296 #define neutz game.neutz                // Romulan Neutral Zone
297 #define irhere game.irhere              // Romulans in quadrant
298 #define icraft game.icraft              // Kirk in Galileo
299 #define ientesc game.ientesc            // Attempted escape from supercommander
300 #define iscraft game.iscraft            // =1 if craft on ship, -1 if removed from game
301 #define isatb game.isatb                // =1 if SuperCommander is attacking base
302 #define iscate game.iscate              // Super Commander is here
303 #ifdef DEBUG
304 #define idebug game.idebug              // Debug mode
305 #endif
306 #define iattak game.iattak              // attack recursion elimination (was cracks[4])
307 #define icrystl game.icrystl            // dilithium crystals aboard
308 #define tourn game.tourn                // Tournament number
309 #define thawed game.thawed              // Thawed game
310 #define batx game.batx                  // Base coordinates being attacked
311 #define baty game.baty                  //
312 #define ithere game.ithere              // Tholean is here 
313 #define ithx game.ithx                  // coordinates of tholean
314 #define ithy game.ithy
315 #define iseenit game.iseenit            // Seen base attack report
316 #define inresor game.inresor            // initial resources
317 #define intime game.intime              // initial time
318 #define inenrg game.inenrg              // Initial/Max Energy
319 #define inshld game.inshld              // Initial/Max Shield
320 #define inlsr game.inlsr                // initial life support resources
321 #define indate game.indate              // Initial date
322 #define energy game.energy              // Energy level
323 #define shield game.shield              // Shield level
324 #define shldup game.shldup              // Shields are up
325 #define warpfac game.warpfac            // Warp speed
326 #define wfacsq game.wfacsq              // squared warp factor
327 #define lsupres game.lsupres            // life support reserves
328 #define dist game.dist                  // movement distance
329 #define direc game.direc                // movement direction
330 #define Time game.Time                  // time taken by current operation
331 #define docfac game.docfac              // repair factor when docking (constant?)
332 #define resting game.resting            // rest time
333 #define damfac game.damfac              // damage factor
334 #define lastchart game.lastchart        // time star chart was last updated
335 #define cryprob game.cryprob            // probability that crystal will work
336 #define probex game.probex              // location of probe
337 #define probey game.probey
338 #define probecx game.probecx            // current probe quadrant
339 #define probecy game.probecy    
340 #define probeinx game.probeinx          // Probe x,y increment
341 #define probeiny game.probeiny          
342 #define proben game.proben              // number of moves for probe
343 #define isarmed game.isarmed            // Probe is armed
344 #define nprobes game.nprobes            // number of probes available
345
346 /* the following global state doesn't need to be saved */
347 EXTERN char     *device[NDEVICES];
348 EXTERN int iscore, iskill; // Common PLAQ
349 EXTERN double perdate;
350 EXTERN double aaitem;
351 EXTERN char citem[10];
352
353 /* the Space Thingy's global state should *not* be saved! */
354 EXTERN int thingx, thingy, iqhere, iqengry;
355
356 typedef enum {FWON, FDEPLETE, FLIFESUP, FNRG, FBATTLE,
357               FNEG3, FNOVA, FSNOVAED, FABANDN, FDILITHIUM,
358                           FMATERIALIZE, FPHASER, FLOST, FMINING, FDPLANET,
359                           FPNOVA, FSSC, FSTRACTOR, FDRAY, FTRIBBLE,
360                           FHOLE} FINTYPE ;
361 enum loctype {neither, quadrant, sector};
362
363 #ifdef INCLUDED
364 char *device[NDEVICES] = {
365         "S. R. Sensors",
366         "L. R. Sensors",
367         "Phasers",
368         "Photon Tubes",
369         "Life Support",
370         "Warp Engines",
371         "Impulse Engines",
372         "Shields",
373         "Subspace Radio",
374         "Shuttle Craft",
375         "Computer",
376         "Transporter",
377         "Shield Control",
378         "Death Ray",
379         "D. S. Probe"};                                                                 
380 #endif
381
382 #ifndef TRUE
383 #define TRUE (1)
384 #define FALSE (0)
385 #endif
386
387 #define IHR 'R'
388 #define IHK 'K'
389 #define IHC 'C'
390 #define IHS 'S'
391 #define IHSTAR '*'
392 #define IHP 'P'
393 #define IHB 'B'
394 #define IHBLANK ' '
395 #define IHDOT '.'
396 #define IHQUEST '?'
397 #define IHE 'E'
398 #define IHF 'F'
399 #define IHT 'T'
400 #define IHWEB '#'
401 #define IHGREEN 'G'
402 #define IHYELLOW 'Y'
403 #define IHRED 'R'
404 #define IHDOCKED 'D'
405 #define IHDEAD 'Z'
406 #define IHMATER0 '-'
407 #define IHMATER1 'o'
408 #define IHMATER2 '0'
409
410
411 /* Function prototypes */
412 void prelim(void);
413 void attack(int);
414 int choose(int);
415 void setup(int);
416 void score(void);
417 void atover(int);
418 int srscan(int);
419 void lrscan(void);
420 void phasers(void);
421 void photon(void);
422 void warp(int);
423 void doshield(int);
424 void dock(int);
425 void dreprt(void);
426 void chart(int);
427 void rechart(void);
428 void impuls(void);
429 void wait(void);
430 void setwrp(void);
431 void events(void);
432 void report(void);
433 void eta(void);
434 void help(void);
435 void abandn(void);
436 void finish(FINTYPE);
437 void dstrct(void);
438 void kaboom(void);
439 void freeze(int);
440 int thaw(void);
441 void plaque(void);
442 int scan(void);
443 #define IHEOL (0)
444 #define IHALPHA (1)
445 #define IHREAL (2)
446 void chew(void);
447 void chew2(void);
448 void skip(int);
449 void prout(char *, ...);
450 void proutn(char *, ...);
451 void stars(void);
452 void newqad(int);
453 int ja(void);
454 void cramen(int);
455 void crmshp(void);
456 char *cramlc(enum loctype, int, int);
457 double expran(double);
458 double Rand(void);
459 void iran(int, int *, int *);
460 #define square(i) ((i)*(i))
461 void dropin(int, int*, int*);
462 void newcnd(void);
463 void sortkl(void);
464 void imove(void);
465 void ram(int, int, int, int);
466 void crmena(int, int, int, int, int);
467 void deadkl(int, int, int, int, int);
468 void timwrp(void);
469 void movcom(void);
470 void torpedo(double, double, int, int, double *, int, int);
471 void huh(void);
472 void pause_game(int);
473 void nova(int, int);
474 void snova(int, int);
475 void scom(int *);
476 void hittem(double *);
477 void prouts(char *, ...);
478 int isit(char *);
479 void preport(void);
480 void orbit(void);
481 void sensor(void);
482 void drawmaps(short);
483 void beam(void);
484 void mine(void);
485 void usecrystals(void);
486 void shuttle(void);
487 void deathray(void);
488 void debugme(void);
489 void attakreport(int);
490 void movetho(void);
491 void probe(void);
492 void iostart(void);
493 void setwnd(WINDOW *);
494 void warble(void);
495 void boom(int ii, int jj);
496 void tracktorpedo(int ix, int iy, int l, int i, int n, int iquad);
497 void cgetline(char *, int);
498 void waitfor(void);
499 void setpassword(void);
500 void commandhook(char *, int);
501 void makechart(void);
502 void enqueue(char *);
503
504 /* mode arguments for srscan() */
505 #define SCAN_FULL               1
506 #define SCAN_REQUEST            2
507 #define SCAN_STATUS             3
508 #define SCAN_NO_LEFTSIDE        4
509
510 WINDOW *fullscreen_window;
511 WINDOW *srscan_window;
512 WINDOW *report_window;
513 WINDOW *lrscan_window;
514 WINDOW *message_window;
515 WINDOW *prompt_window;
516
517 extern void clreol(void);
518 extern void clrscr(void);
519 extern void textcolor(int color);
520 extern void highvideo(void);
521
522 enum COLORS {
523    DEFAULT,
524    BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY,
525    DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE
526 };
527
528 #define DAMAGED 128     /* marker for damaged ship in starmap */