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