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