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