Get rid of obnoxious iattak global.
[super-star-trek.git] / src / sst.h
1 #ifndef __SST_H__
2
3 #include <stdio.h>
4 #include <math.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <locale.h>
8 #include <libintl.h>
9 #include <curses.h>
10 #include <stdbool.h>
11
12 #ifdef DATA_DIR
13 #define SSTDOC DATA_DIR"/"DOC_NAME
14 #else
15 #define SSTDOC DOC_NAME
16 #endif
17
18 #define _(str) gettext(str)
19
20 #define min(x, y)       ((x)<(y)?(x):(y))
21 #define max(x, y)       ((x)>(y)?(x):(y))
22
23 #define PHASEFAC (2.0)
24 #define GALSIZE (8)
25 #define NINHAB (GALSIZE * GALSIZE / 2)
26 #define MAXUNINHAB (10)
27 #define PLNETMAX (NINHAB + MAXUNINHAB)
28 #define QUADSIZE (10)
29 #define BASEMAX (5)
30
31 /*
32  * These macros hide the difference between 0-origin and 1-origin addressing.
33  * They're a step towards de-FORTRANizing the code.
34  */
35 #define VALID_QUADRANT(x, y)    ((x)>=1 && (x)<=GALSIZE && (y)>=1 && (y)<=GALSIZE)
36 #define VALID_SECTOR(x, y)      ((x)>=1 && (x)<=QUADSIZE && (y)>=1 && (y)<=QUADSIZE)
37 #define for_quadrants(i)        for (i = 1; i <= GALSIZE; i++)
38 #define for_sectors(i)          for (i = 1; i <= QUADSIZE; i++)
39 #define for_commanders(i)       for (i = 1; i <= game.state.remcom; i++)
40 #define for_local_enemies(i)    for (i = 1; i <= game.nenhere; i++)
41 #define for_starbases(i)        for (i = 1; i <= game.state.rembase; i++)
42
43 typedef struct {int x; int y;} coord;
44
45 #define square(i)               ((i)*(i))
46 #define same(c1, c2)            ((c1.x == c2.x) && (c1.y == c2.y))
47 #define distance(c1, c2)        sqrt(square(c1.x - c2.x) + square(c1.y - c2.y))
48
49 typedef struct {
50     coord w;
51     enum {M=0, N=1, O=2} pclass;
52     int inhabited;      /* if NZ, an index into a name array */
53 #define UNINHABITED     -1
54     int crystals; /* has crystals */
55 #define MINED   -1      /* used to have crystals, but they were mined out */
56     enum {unknown, known, shuttle_down} known;
57 } planet;
58
59 #define DESTROY(pl)     memset(pl, '\0', sizeof(planet))
60
61 typedef enum {
62     IHR = 'R',
63     IHK = 'K',
64     IHC = 'C',
65     IHS = 'S',
66     IHSTAR = '*',
67     IHP = 'P',
68     IHW = '@',
69     IHB = 'B',
70     IHBLANK = ' ',
71     IHDOT = '.',
72     IHQUEST = '?',
73     IHE = 'E',
74     IHF = 'F',
75     IHT = 'T',
76     IHWEB = '#',
77     IHMATER0 = '-',
78     IHMATER1 = 'o',
79     IHMATER2 = '0',
80 } feature;
81
82 typedef struct {
83     bool snap;          // snapshot taken
84     int crew,           // crew complement
85 #define FULLCREW        428     /* BSD Trek was 387, that's wrong */
86         remkl,                  // remaining klingons
87         remcom,                 // remaining commanders
88         nscrem,                 // remaining super commanders
89         rembase,                // remaining bases
90         starkl,                 // destroyed stars
91         basekl,                 // destroyed bases
92         nromrem,                // Romulans remaining
93         nplankl,                // destroyed uninhabited planets
94         nworldkl;               // destroyed inhabited planets
95     planet plnets[PLNETMAX];  // Planet information
96     double date,                // stardate
97         remres,         // remaining resources
98         remtime;                // remaining time
99     coord baseq[BASEMAX+1];     // Base quadrant coordinates
100     coord kcmdr[QUADSIZE+1];    // Commander quadrant coordinates
101     coord kscmdr;               // Supercommander quadrant coordinates
102     struct quadrant {
103         int stars;
104         int planet;
105 #define NOPLANET        -1
106         bool starbase;
107         int klingons;
108         int romulans;
109         bool supernova;
110         bool charted;
111         enum {secure, distressed, enslaved} status;
112     } galaxy[GALSIZE+1][GALSIZE+1];     // The Galaxy (subscript 0 not used)
113     struct page {
114         int stars;
115         bool starbase;
116         int klingons;
117     } chart[GALSIZE+1][GALSIZE+1];      // the starchart (subscript 0 not used)
118 } snapshot;                             // Data that is snapshot
119
120 #define MAXKLGAME       127
121 #define MAXKLQUAD       9
122
123 #define NKILLK (game.inkling - game.state.remkl)
124 #define NKILLC (game.incom - game.state.remcom)
125 #define NKILLSC (game.inscom - game.state.nscrem)
126 #define NKILLROM (game.inrom - game.state.nromrem)
127 #define KLINGREM (game.state.remkl + game.state.remcom + game.state.nscrem)
128 #define INKLINGTOT (game.inkling + game.incom + game.inscom)
129 #define KLINGKILLED (INKLINGTOT - KLINGREM)
130
131 /* game options */
132 #define OPTION_ALL      0xffffffff
133 #define OPTION_TTY      0x00000001      /* old interface */
134 #define OPTION_CURSES   0x00000002      /* new interface */
135 #define OPTION_IOMODES  0x00000003      /* cover both interfaces */
136 #define OPTION_PLANETS  0x00000004      /* planets and mining */
137 #define OPTION_THOLIAN  0x00000008      /* Tholians and their webs */
138 #define OPTION_THINGY   0x00000010      /* Space Thingy can shoot back */
139 #define OPTION_PROBE    0x00000020      /* deep-space probes */
140 #define OPTION_SHOWME   0x00000040      /* bracket Enterprise in chart */
141 #define OPTION_RAMMING  0x00000080      /* enemies may ram Enterprise */
142 #define OPTION_MVBADDY  0x00000100      /* more enemies can move */
143 #define OPTION_BLKHOLE  0x00000200      /* black hole may timewarp you */
144 #define OPTION_BASE     0x00000400      /* bases have good shields */
145 #define OPTION_WORLDS   0x00000800      /* logic for inhabited worlds */
146 #define OPTION_PLAIN    0x01000000      /* user chose plain game */
147 #define OPTION_ALMY     0x02000000      /* user chose Almy variant */
148
149 /* Define devices */
150 #define DSRSENS 0
151 #define DLRSENS 1
152 #define DPHASER 2
153 #define DPHOTON 3
154 #define DLIFSUP 4
155 #define DWARPEN 5
156 #define DIMPULS 6
157 #define DSHIELD 7
158 #define DRADIO  8
159 #define DSHUTTL 9
160 #define DCOMPTR 10
161 #define DNAVSYS 11
162 #define DTRANSP 12
163 #define DSHCTRL 13
164 #define DDRAY   14
165 #define DDSP    15
166 #define NDEVICES (16)   // Number of devices
167
168 #define damaged(dev)    (game.damage[dev] != 0.0)
169
170 #define FOREVER 1e30
171
172 /* Define future events */
173 #define FSPY    0       // Spy event happens always (no future[] entry)
174                                         // can cause SC to tractor beam Enterprise
175 #define FSNOVA  1   // Supernova
176 #define FTBEAM  2   // Commander tractor beams Enterprise
177 #define FSNAP   3   // Snapshot for time warp
178 #define FBATTAK 4   // Commander attacks base
179 #define FCDBAS  5   // Commander destroys base
180 #define FSCMOVE 6   // Supercommander moves (might attack base)
181 #define FSCDBAS 7   // Supercommander destroys base
182 #define FDSPROB 8   // Move deep space probe
183 #define FDISTR  9   // Emit distress call from an inhabited world 
184 #define FENSLV  10  // Inhabited word is enslaved */
185 #define FREPRO  11  // Klingons build a ship in an enslaved system
186 #define NEVENTS (12)
187
188 typedef struct {
189     double date;
190     coord quadrant;
191 } event;
192
193 /*
194  * abstract out the event handling -- underlying data structures will change
195  * when we implement stateful events
196  */
197 extern event *unschedule(int);
198 extern int is_scheduled(int);
199 extern event *schedule(int, double);
200 extern void postpone(int, double);
201 extern double scheduled(int);
202 #define findevent(evtype)       &game.future[evtype]
203
204 #define SSTMAGIC        "SST2.0\n"
205
206 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
207
208 struct game {
209     char magic[sizeof(SSTMAGIC)];
210     unsigned long options;
211     snapshot state;
212     snapshot snapsht;
213     feature quad[QUADSIZE+1][QUADSIZE+1];               // contents of our quadrant
214     double kpower[(QUADSIZE+1)*(QUADSIZE+1)];           // enemy energy levels
215     double kdist[(QUADSIZE+1)*(QUADSIZE+1)];            // enemy distances
216     double kavgd[(QUADSIZE+1)*(QUADSIZE+1)];            // average distances
217     double damage[NDEVICES];    // damage encountered
218     event future[NEVENTS];      // future events
219     char passwd[10];            // Self Destruct password
220     coord ks[(QUADSIZE+1)*(QUADSIZE+1)];        // enemy sector locations
221     coord quadrant, sector;     // where we are
222     coord tholian;              // coordinates of Tholian
223     coord base;                 // position of base in current quadrant
224     coord battle;               // base coordinates being attacked
225     coord plnet;                // location of planet in quadrant
226     coord probec;       // current probe quadrant
227     bool gamewon,       // Finished!
228         ididit,         // action taken -- allows enemy to attack
229         alive,          // we are alive (not killed)
230         justin,         // just entered quadrant
231         shldup,         // shields are up
232         shldchg,        // shield is changing (affects efficiency)
233         comhere,        // commander here
234         ishere,         // super-commander in quadrant
235         iscate,         // super commander is here
236         ientesc,        // attempted escape from supercommander
237         ithere,         // Tholian is here 
238         resting,        // rest time
239         icraft,         // Kirk in Galileo
240         landed,         // party on planet (true), on ship (false)
241         alldone,        // game is now finished
242         neutz,          // Romulan Neutral Zone
243         isarmed,        // probe is armed
244         inorbit,        // orbiting a planet
245         imine,          // mining
246         icrystl,        // dilithium crystals aboard
247         iseenit,        // seen base attack report
248         thawed;         // thawed game
249     enum {
250         green,
251         yellow,
252         red,
253         docked,
254         dead,
255     } condition;                // condition (red/yellow/green/docked)
256     enum {
257         onship,
258         offship,
259         removed,
260     } iscraft;          // 'onship' if craft on ship, 'removed' if out of game
261     enum {
262         SKILL_NONE,
263         SKILL_NOVICE,
264         SKILL_FAIR,
265         SKILL_GOOD,
266         SKILL_EXPERT,
267         SKILL_EMERITUS,
268     } skill;            // skill level
269     int inkling,        // initial number of klingons
270         inbase,         // initial number of bases
271         incom,          // initial number of commanders
272         inscom,         // initial number of commanders
273         inrom,          // initial number of commanders
274         instar,         // initial stars
275         intorps,        // initial/max torpedoes
276         torps,          // number of torpedoes
277         ship,           // ship type -- 'E' is Enterprise
278         abandoned,      // count of crew abandoned in space
279         length,         // length of game
280         klhere,         // klingons here
281         casual,         // causalties
282         nhelp,          // calls for help
283         nkinks,         // count of energy-barrier crossings
284         iplnet,         // planet # in quadrant
285         inplan,         // initial planets
286         nenhere,        // number of enemies in quadrant
287         irhere,         // Romulans in quadrant
288         isatb,          // =1 if super commander is attacking base
289         tourn,          // tournament number
290         proben,         // number of moves for probe
291         nprobes;        // number of probes available
292     double inresor,     // initial resources
293         intime,         // initial time
294         inenrg,         // initial/max energy
295         inshld,         // initial/max shield
296         inlsr,          // initial life support resources
297         indate,         // initial date
298         energy,         // energy level
299         shield,         // shield level
300         warpfac,        // warp speed
301         wfacsq,         // squared warp factor
302         lsupres,        // life support reserves
303         dist,           // movement distance
304         direc,          // movement direction
305         optime,         // time taken by current operation
306         docfac,         // repair factor when docking (constant?)
307         damfac,         // damage factor
308         lastchart,      // time star chart was last updated
309         cryprob,        // probability that crystal will work
310         probex,         // location of probe
311         probey,         //
312         probeinx,       // probe x,y increment
313         probeiny,       //
314         height;         // height of orbit around planet
315 };
316 extern struct game game;
317
318 /* the following global state doesn't need to be saved */
319 extern char *device[NDEVICES];
320 extern char *systnames[NINHAB + 1];
321 extern int iscore, iskill; // Common PLAQ
322 extern double perdate;
323 extern double aaitem;
324 extern char citem[10];
325 extern int seed;
326 extern bool idebug;
327 extern FILE *logfp, *replayfp;
328
329 /* the Space Thingy's global state should *not* be saved! */
330 extern coord thing;
331 extern bool iqhere, iqengry;
332
333 typedef enum {
334     FWON, FDEPLETE, FLIFESUP, FNRG, FBATTLE,
335     FNEG3, FNOVA, FSNOVAED, FABANDN, FDILITHIUM,
336     FMATERIALIZE, FPHASER, FLOST, FMINING, FDPLANET,
337     FPNOVA, FSSC, FSTRACTOR, FDRAY, FTRIBBLE,
338     FHOLE, FCREW
339 } FINTYPE ;
340
341 enum loctype {neither, quadrant, sector};
342
343 typedef enum {
344     SCAN_FULL,
345     SCAN_REQUEST,
346     SCAN_STATUS,
347     SCAN_NO_LEFTSIDE,
348 } scantype;
349
350 /* Function prototypes */
351 void prelim(void);
352 void attack(bool);
353 bool choose(bool);
354 void setup(bool);
355 void score(void);
356 void atover(bool);
357 void srscan(scantype);
358 void lrscan(void);
359 void phasers(void);
360 void photon(void);
361 void warp(bool);
362 void doshield(bool);
363 void dock(bool);
364 void dreprt(void);
365 void chart(bool);
366 void rechart(void);
367 void impuls(void);
368 void wait(void);
369 void setwrp(void);
370 void events(void);
371 void report(void);
372 void eta(void);
373 void mayday(void);
374 void abandn(void);
375 void finish(FINTYPE);
376 void selfdestruct(void);
377 void kaboom(void);
378 void freeze(bool);
379 bool thaw(void);
380 void plaque(void);
381 int scan(void);
382 #define IHEOL (0)
383 #define IHALPHA (1)
384 #define IHREAL (2)
385 void chew(void);
386 void chew2(void);
387 void skip(int);
388 void prout(char *, ...);
389 void proutn(char *, ...);
390 void stars(void);
391 void newqad(bool);
392 bool ja(void);
393 void cramen(feature);
394 void crmshp(void);
395 char *cramlc(enum loctype, coord w);
396 double expran(double);
397 double Rand(void);
398 coord randplace(int);
399 coord dropin(feature);
400 void newcnd(void);
401 void sortkl(void);
402 void imove(bool);
403 void ram(bool, feature, coord);
404 void crmena(bool, feature, enum loctype, coord w);
405 void deadkl(coord, feature, coord);
406 void timwrp(void);
407 void movcom(void);
408 void torpedo(double, double, coord, double *, int, int);
409 void huh(void);
410 void pause_game(bool);
411 void nova(coord);
412 void snova(bool, coord *);
413 void scom(bool *);
414 void hittem(double *);
415 void prouts(char *, ...);
416 bool isit(char *);
417 void preport(void);
418 void orbit(void);
419 void sensor(void);
420 void drawmaps(int);
421 void beam(void);
422 void mine(void);
423 void usecrystals(void);
424 void shuttle(void);
425 void deathray(void);
426 void debugme(void);
427 void attakreport(bool);
428 void movetho(void);
429 void probe(void);
430 void iostart(void);
431 void setwnd(WINDOW *);
432 void warble(void);
433 void boom(coord);
434 void tracktorpedo(coord, int, int, int, int);
435 void cgetline(char *, int);
436 void waitfor(void);
437 void setpassword(void);
438 void commandhook(char *, bool);
439 void makechart(void);
440 void enqueue(char *);
441 coord newkling(int);
442 #if BSD_BUG_FOR_BUG
443 void visual(void);
444 #endif
445
446 extern WINDOW *curwnd;
447 extern WINDOW *fullscreen_window;
448 extern WINDOW *srscan_window;
449 extern WINDOW *report_window;
450 extern WINDOW *lrscan_window;
451 extern WINDOW *message_window;
452 extern WINDOW *prompt_window;
453
454 extern void clreol(void);
455 extern void clrscr(void);
456 extern void textcolor(int);
457 extern void highvideo(void);
458
459 enum COLORS {
460    DEFAULT,
461    BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY,
462    DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE
463 };
464
465 #endif