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