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