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