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