X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=sst.c;h=888b7ec4780b01333e7a04905a7dbef65e07d6c5;hb=cf91699c89b88f76dcf63c60624635184e8cb7eb;hp=2c888443c27eeaf4c6b943d71442fee786e68ecb;hpb=b0629033955fcfdcd93a4a2cf3ab29e8adc56200;p=super-star-trek.git diff --git a/sst.c b/sst.c index 2c88844..888b7ec 100644 --- a/sst.c +++ b/sst.c @@ -1,19 +1,13 @@ #define INCLUDED // Define externs here -#include "sst.h" #include -#ifdef MSDOS -#include +#include +#include "sst.h" + +#ifndef SSTDOC +#define SSTDOC "sst.doc" #endif -#include -int getch(void); - static char line[128], *linep = line; -static int linecount; /* for paging */ - -static void clearscreen(void); - -#define NUMCOMMANDS 34 /* Compared to original version, I've changed the "help" command to "call" and the "terminate" command to "quit" to better match @@ -40,7 +34,7 @@ static void clearscreen(void); 2. deathray improvement (but keeping original failure alternatives) 3. Tholian Web - +s 4. Enemies can ram the Enterprise. Regular Klingons and Romulans can move in Expert and Emeritus games. This code could use improvement. @@ -52,7 +46,7 @@ static void clearscreen(void); */ -static char *commands[NUMCOMMANDS] = { +static char *commands[] = { "srscan", "lrscan", "phasers", @@ -88,6 +82,7 @@ static char *commands[NUMCOMMANDS] = { "quit", "help" }; +#define NUMCOMMANDS sizeof(commands)/sizeof(commands[0]) static void listCommands(int x) { prout(" SRSCAN MOVE PHASERS CALL\n" @@ -103,7 +98,7 @@ static void listCommands(int x) { static void helpme(void) { int i, j; - char cmdbuf[32]; + char cmdbuf[32], *cp; char linebuf[132]; FILE *fp; /* Give help on commands */ @@ -130,37 +125,41 @@ static void helpme(void) { strcpy(cmdbuf, " ABBREV"); } else { - strcpy(cmdbuf, " Mnemonic: "); - j = 0; - while ((cmdbuf[j+13] = toupper(commands[i][j])) != 0) j++; + for (j = 0; commands[i][j]; j++) + cmdbuf[j] = toupper(commands[i][j]); + cmdbuf[j] = '\0'; } - fp = fopen("sst.doc", "r"); + fp = fopen(SSTDOC, "r"); if (fp == NULL) { prout("Spock- \"Captain, that information is missing from the"); prout(" computer. You need to find SST.DOC and put it in the"); prout(" current directory.\""); return; } - i = strlen(cmdbuf); - do { - if (fgets(linebuf, 132, fp) == NULL) { + for (;;) { + if (fgets(linebuf, sizeof(linebuf), fp) == NULL) { prout("Spock- \"Captain, there is no information on that command.\""); fclose(fp); return; } - } while (strncmp(linebuf, cmdbuf, i) != 0); + if (linebuf[0] == '%' && linebuf[1] == '%'&& linebuf[2] == ' ') { + for (cp = linebuf+3; isspace(*cp); cp++) + continue; + linebuf[strlen(linebuf)-1] = '\0'; + if (strcmp(cp, cmdbuf) == 0) + break; + } + } skip(1); prout("Spock- \"Captain, I've found the following information:\""); skip(1); - do { - if (linebuf[0]!=12) { // ignore page break lines - linebuf[strlen(linebuf)-1] = '\0'; // No \n at end - prout(linebuf); - } - fgets(linebuf,132,fp); - } while (strstr(linebuf, "******")==NULL); + while (fgets(linebuf, sizeof(linebuf),fp)) { + if (strstr(linebuf, "******")) + break; + proutc(linebuf); + } fclose(fp); } @@ -191,6 +190,7 @@ static void makemoves(void) { } else prout("UNRECOGNIZED COMMAND."); } + commandhook(commands[i], TRUE); switch (i) { /* command switch */ case 0: // srscan srscan(1); @@ -210,14 +210,15 @@ static void makemoves(void) { warp(1); break; case 5: // shields - sheild(1); + doshield(1); if (ididit) { attack(2); shldchg = 0; } break; case 6: // dock - dock(); + dock(1); + if (ididit) attack(0); break; case 7: // damages dreprt(); @@ -315,6 +316,7 @@ static void makemoves(void) { helpme(); // get help break; } + commandhook(commands[i], FALSE); for (;;) { if (alldone) break; // Game has ended #ifdef DEBUG @@ -324,7 +326,7 @@ static void makemoves(void) { events(); if (alldone) break; // Events did us in } - if (d.galaxy[quadx][quady] == 1000) { // Galaxy went Nova! + if (game.state.galaxy[quadx][quady] == 1000) { // Galaxy went Nova! atover(0); continue; } @@ -332,7 +334,7 @@ static void makemoves(void) { if (hitme && justin==0) { attack(2); if (alldone) break; - if (d.galaxy[quadx][quady] == 1000) { // went NOVA! + if (game.state.galaxy[quadx][quady] == 1000) { // went NOVA! atover(0); hitme = TRUE; continue; @@ -345,25 +347,31 @@ static void makemoves(void) { } -void main(int argc, char **argv) { - int i; +int main(int argc, char **argv) { + int i, option, usecurses = TRUE; int hitme; char ch; - prelim(); - - if (argc > 1) { - fromcommandline = 1; - line[0] = '\0'; - while (--argc > 0) { - strcat(line, *(++argv)); - strcat(line, " "); - } - } - else fromcommandline = 0; + while ((option = getopt(argc, argv, "t")) != -1) { + switch (option) { + case 't': + usecurses = FALSE; + break; + default: + fprintf(stderr, "usage: sst [-t] [startcommand...].\n"); + exit(0); + } + } + iostart(usecurses); + prelim(); + line[0] = '\0'; + for (i = optind; i < argc; i++) { + strcat(line, argv[i]); + strcat(line, " "); + } while (TRUE) { /* Play a game */ - setup(); + setup(line[0] == '\0'); if (alldone) { score(); alldone = 0; @@ -374,17 +382,18 @@ void main(int argc, char **argv) { skip(1); if (tourn && alldone) { - printf("Do you want your score recorded?"); + proutn("Do you want your score recorded?"); if (ja()) { chew2(); freeze(FALSE); } } - printf("Do you want to play again?"); + proutn("Do you want to play again?"); if (!ja()) break; } skip(1); - prout("May the Great Bird of the Galaxy roost upon your home planet."); + ioend(); + puts("May the Great Bird of the Galaxy roost upon your home planet."); } @@ -401,27 +410,28 @@ void cramen(int i) { case IHP: s = "Planet"; break; case IHB: s = "Starbase"; break; case IHBLANK: s = "Black hole"; break; - case IHT: s = "Tholean"; break; - case IHWEB: s = "Tholean web"; break; + case IHT: s = "Tholian"; break; + case IHWEB: s = "Tholian web"; break; + case IHQUEST: s = "Stranger"; break; default: s = "Unknown??"; break; } proutn(s); } -void cramlc(int key, int x, int y) { - if (key == 1) proutn(" Quadrant"); - else if (key == 2) proutn(" Sector"); - proutn(" "); - crami(x, 1); - proutn(" - "); - crami(y, 1); +char *cramlc(enum loctype key, int x, int y) { + static char buf[32]; + buf[0] = '\0'; + if (key == quadrant) strcpy(buf, "Quadrant "); + else if (key == sector) strcpy(buf, "Sector "); + sprintf(buf+strlen(buf), "%d-%d", x, y); + return buf; } void crmena(int i, int enemy, int key, int x, int y) { if (i == 1) proutn("***"); cramen(enemy); - proutn(" at"); - cramlc(key, x, y); + proutn(" at "); + proutn(cramlc(key, x, y)); } void crmshp(void) { @@ -458,14 +468,12 @@ void iran10(int *i, int *j) { } void chew(void) { - linecount = 0; linep = line; *linep = 0; } void chew2(void) { /* return IHEOL next time */ - linecount = 0; linep = line+1; *linep = 0; } @@ -474,8 +482,6 @@ int scan(void) { int i; char *cp; - linecount = 0; - // Init result aaitem = 0.0; *citem = 0; @@ -486,7 +492,7 @@ int scan(void) { chew(); return IHEOL; } - gets(line); + getline(line, sizeof(line)); linep = line; } // Skip leading white space @@ -498,6 +504,7 @@ int scan(void) { } if (isdigit(*linep) || *linep=='+' || *linep=='-' || *linep=='.') { // treat as a number + i = 0; if (sscanf(linep, "%lf%n", &aaitem, &i) < 1) { linep = line; // Invalid numbers are ignored *linep = 0; @@ -530,87 +537,6 @@ int ja(void) { } } -void cramf(double x, int w, int d) { - char buf[64]; - sprintf(buf, "%*.*f", w, d, x); - proutn(buf); -} - -void crami(int i, int w) { - char buf[16]; - sprintf(buf, "%*d", w, i); - proutn(buf); -} - -double square(double i) { return i*i; } - -static void clearscreen(void) { - /* Somehow we need to clear the screen */ -#ifdef __BORLANDC__ - extern void clrscr(void); - clrscr(); -#else - proutn("\033[2J"); /* Hope for an ANSI display */ -#endif -} - -/* We will pull these out in case we want to do something special later */ - -void pause(int i) { - putchar('\n'); - if (i==1) { - if (skill > 2) - prout("[ANOUNCEMENT ARRIVING...]"); - else - prout("[IMPORTANT ANNOUNCEMENT ARRIVING -- HIT SPACE BAR TO CONTINUE]"); - getch(); - } - else { - if (skill > 2) - proutn("[CONTINUE?]"); - else - proutn("[HIT SPACE BAR TO CONTINUE]"); - getch(); - proutn("\r \r"); - } - if (i != 0) { - clearscreen(); - } - linecount = 0; -} - - -void skip(int i) { - while (i-- > 0) { - linecount++; - if (linecount >= 23) - pause(0); - else - putchar('\n'); - } -} - - -void proutn(char *s) { - fputs(s, stdout); -} - -void prout(char *s) { - proutn(s); - skip(1); -} - -void prouts(char *s) { - clock_t endTime; - /* print slowly! */ - while (*s) { - endTime = clock() + CLOCKS_PER_SEC*0.05; - while (clock() < endTime) ; - putchar(*s++); - fflush(stdout); - } -} - void huh(void) { chew(); skip(1); @@ -637,7 +563,7 @@ void debugme(void) { proutn("Reset damage? "); if (ja() != 0) { int i; - for (i=0; i <= ndevice; i++) if (damage[i] > 0.0) damage[i] = 0.0; + for (i=0; i <= NDEVICES; i++) if (damage[i] > 0.0) damage[i] = 0.0; stdamtim = 1e30; } proutn("Toggle idebug? "); @@ -649,7 +575,7 @@ void debugme(void) { proutn("Cause selective damage? "); if (ja() != 0) { int i, key; - for (i=1; i <= ndevice; i++) { + for (i=1; i <= NDEVICES; i++) { proutn("Kill "); proutn(device[i]); proutn("? "); @@ -657,7 +583,7 @@ void debugme(void) { key = scan(); if (key == IHALPHA && isit("y")) { damage[i] = 10.0; - if (i == DRADIO) stdamtim = d.date; + if (i == DRADIO) stdamtim = game.state.date; } } } @@ -676,12 +602,12 @@ void debugme(void) { case FSCMOVE: proutn("SC Move "); break; case FSCDBAS: proutn("SC Base Destroy "); break; } - cramf(future[i]-d.date, 8, 2); + proutn("%.2f", future[i]-game.state.date); chew(); proutn(" ?"); key = scan(); if (key == IHREAL) { - future[i] = d.date + aaitem; + future[i] = game.state.date + aaitem; } } chew();