5ea29e9a07f96f96062326f7f3ed0b69370b0631
[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         (GALSIZE * GALSIZE / 12)
30 #define MAXKLGAME       127
31 #define MAXKLQUAD       9
32
33 /*
34  * These macros hide the difference between 0-origin and 1-origin addressing.
35  * They're a step towards de-FORTRANizing the code.
36  */
37 #define VALID_QUADRANT(x, y)    ((x)>=1 && (x)<=GALSIZE && (y)>=1 && (y)<=GALSIZE)
38 #define VALID_SECTOR(x, y)      ((x)>=1 && (x)<=QUADSIZE && (y)>=1 && (y)<=QUADSIZE)
39 #define for_quadrants(i)        for (i = 1; i <= GALSIZE; i++)
40 #define for_sectors(i)          for (i = 1; i <= QUADSIZE; i++)
41 #define for_commanders(i)       for (i = 1; i <= game.state.remcom; i++)
42 #define for_local_enemies(i)    for (i = 1; i <= game.nenhere; i++)
43 #define for_starbases(i)        for (i = 1; i <= game.state.rembase; i++)
44
45 typedef struct {int x; int y;} coord;
46
47 #define square(i)               ((i)*(i))
48 #define same(c1, c2)            ((c1.x == c2.x) && (c1.y == c2.y))
49 #define distance(c1, c2)        sqrt(square(c1.x - c2.x) + square(c1.y - c2.y))
50 #define invalidate(w)           w.x = w.y = 0
51 #define is_valid(w)             (w.x != 0 && w.y != 0)
52
53 typedef struct {
54     coord w;
55     enum {M=0, N=1, O=2} pclass;
56     int inhabited;      /* if NZ, an index into a name array */
57 #define UNINHABITED     -1
58     enum {mined=-1, present=0, absent=1} crystals; /* has crystals */
59     enum {unknown, known, shuttle_down} known;
60 } planet;
61
62 #define DESTROY(pl)     memset(pl, '\0', sizeof(planet))
63
64 typedef enum {
65     IHR = 'R',
66     IHK = 'K',
67     IHC = 'C',
68     IHS = 'S',
69     IHSTAR = '*',
70     IHP = 'P',
71     IHW = '@',
72     IHB = 'B',
73     IHBLANK = ' ',
74     IHDOT = '.',
75     IHQUEST = '?',
76     IHE = 'E',
77     IHF = 'F',
78     IHT = 'T',
79     IHWEB = '#',
80     IHMATER0 = '-',
81     IHMATER1 = 'o',
82     IHMATER2 = '0',
83 } feature;
84
85 typedef struct {
86     bool snap;                  // snapshot taken
87     int crew,                   // crew complement
88 #define FULLCREW        428     /* BSD Trek was 387, that's wrong */
89         remkl,                  // remaining klingons
90         remcom,                 // remaining commanders
91         nscrem,                 // remaining super commanders
92         rembase,                // remaining bases
93         starkl,                 // destroyed stars
94         basekl,                 // destroyed bases
95         nromrem,                // Romulans remaining
96         nplankl,                // destroyed uninhabited planets
97         nworldkl;               // destroyed inhabited planets
98     planet planets[PLNETMAX];   // Planet information
99     double date,                // stardate
100         remres,                 // remaining resources
101         remtime;                // remaining time
102     coord baseq[BASEMAX+1];     // Base quadrant coordinates
103     coord kcmdr[QUADSIZE+1];    // Commander quadrant coordinates
104     coord kscmdr;               // Supercommander quadrant coordinates
105     struct quadrant {
106         int stars;
107         int planet;
108 #define NOPLANET        -1
109         bool starbase;
110         int klingons;
111         int romulans;
112         bool supernova;
113         bool charted;
114         enum {secure, distressed, enslaved} status;
115     } galaxy[GALSIZE+1][GALSIZE+1];     // The Galaxy (subscript 0 not used)
116     struct page {
117         int stars;
118         bool starbase;
119         int klingons;
120     } chart[GALSIZE+1][GALSIZE+1];      // the starchart (subscript 0 not used)
121 } snapshot;                             // Data that is snapshot
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 /* Function prototypes */
344 extern void prelim(void);
345 extern void attack(bool);
346 extern bool choose(bool);
347 extern void setup(bool);
348 extern void score(void);
349 extern void atover(bool);
350 extern void srscan(void);
351 extern void lrscan(void);
352 extern void phasers(void);
353 extern void photon(void);
354 extern void warp(bool);
355 extern void doshield(bool);
356 extern void dock(bool);
357 extern void damagereport(void);
358 extern void chart(void);
359 extern void rechart(void);
360 extern void impulse(void);
361 extern void wait(void);
362 extern void setwarp(void);
363 extern void events(void);
364 extern void report(void);
365 extern void eta(void);
366 extern void mayday(void);
367 extern void abandon(void);
368 extern void finish(FINTYPE);
369 extern void selfdestruct(void);
370 extern void kaboom(void);
371 extern void freeze(bool);
372 extern bool thaw(void);
373 extern void plaque(void);
374 extern int scan(void);
375 extern void status(int req);
376 extern void request(void);
377 #define IHEOL (0)
378 #define IHALPHA (1)
379 #define IHREAL (2)
380 extern void chew(void);
381 extern void chew2(void);
382 extern void skip(int);
383 extern void prout(const char *, ...) __attribute__((format(printf, 1, 2)));
384 extern void proutn(const char *, ...) __attribute__((format(printf, 1, 2)));
385 extern void prouts(const char *, ...) __attribute__((format(printf, 1, 2)));
386 extern void prstat(const char *txt, const char *fmt, ...)
387         __attribute__((format(printf, 2, 3)));
388 extern void stars(void);
389 extern void newqad(bool);
390 extern bool ja(void);
391 extern void cramen(feature);
392 extern void crmshp(void);
393 extern char *cramlc(enum loctype, coord w);
394 extern double expran(double);
395 extern double Rand(void);
396 extern coord randplace(int);
397 extern coord dropin(feature);
398 extern void newcnd(void);
399 extern void sortklings(void);
400 extern void imove(bool);
401 extern void ram(bool, feature, coord);
402 extern void crmena(bool, feature, enum loctype, coord w);
403 extern void deadkl(coord, feature, coord);
404 extern void timwrp(void);
405 extern void moveklings(void);
406 extern void torpedo(double, double, coord, double *, int, int);
407 extern void huh(void);
408 extern void pause_reset(void);
409 extern void pause_game(bool);
410 extern void nova(coord);
411 extern void supernova(bool, coord *);
412 extern void supercommander(void);
413 extern void hittem(double *);
414 extern bool isit(char *);
415 extern void survey(void);
416 extern void orbit(void);
417 extern void sensor(void);
418 extern void drawmaps(int);
419 extern void beam(void);
420 extern void mine(void);
421 extern void usecrystals(void);
422 extern void shuttle(void);
423 extern void deathray(void);
424 extern void debugme(void);
425 extern void attackreport(bool);
426 extern void movetholian(void);
427 extern void probe(void);
428 extern void iostart(void);
429 extern void setwnd(WINDOW *);
430 extern void warble(void);
431 extern void boom(coord);
432 extern void tracktorpedo(coord, int, int, int, int);
433 extern void cgetline(char *, int);
434 extern void waitfor(void);
435 extern void setpassword(void);
436 extern void commandhook(char *, bool);
437 extern void makechart(void);
438 extern coord newkling(int);
439 #if BSD_BUG_FOR_BUG
440 extern void visual(void);
441 #endif
442
443 extern WINDOW *curwnd;
444 extern WINDOW *fullscreen_window;
445 extern WINDOW *srscan_window;
446 extern WINDOW *report_window;
447 extern WINDOW *lrscan_window;
448 extern WINDOW *message_window;
449 extern WINDOW *prompt_window;
450
451 extern void clreol(void);
452 extern void clrscr(void);
453 extern void textcolor(int);
454 extern void highvideo(void);
455
456 enum COLORS {
457    DEFAULT,
458    BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY,
459    DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA, YELLOW, WHITE
460 };
461
462 #endif