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