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