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