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