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