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