Move I/O to a separate module -- first step towards curses interface.
[super-star-trek.git] / sst.c
diff --git a/sst.c b/sst.c
index e4fe2afe0fcf55a0eab1f3f5d3a4001e47c6bd79..d35b2556ae1407f297b0088838a2c5124b1140c0 100644 (file)
--- a/sst.c
+++ b/sst.c
@@ -1,17 +1,12 @@
 #define INCLUDED       // Define externs here\r
 #include "sst.h"\r
 #include <ctype.h>\r
 #define INCLUDED       // Define externs here\r
 #include "sst.h"\r
 #include <ctype.h>\r
-#ifdef MSDOS\r
-#include <dos.h>\r
+\r
+#ifndef SSTDOC\r
+#define SSTDOC "sst.doc"\r
 #endif\r
 #endif\r
-#include <time.h>\r
        \r
        \r
-int getch(void);\r
-\r
 static char line[128], *linep = line;\r
 static char line[128], *linep = line;\r
-static int linecount;  /* for paging */\r
-\r
-static void clearscreen(void);\r
 \r
 /* Compared to original version, I've changed the "help" command to\r
    "call" and the "terminate" command to "quit" to better match\r
 \r
 /* Compared to original version, I've changed the "help" command to\r
    "call" and the "terminate" command to "quit" to better match\r
@@ -38,7 +33,7 @@ static void clearscreen(void);
    2. deathray improvement (but keeping original failure alternatives)\r
 \r
    3. Tholian Web\r
    2. deathray improvement (but keeping original failure alternatives)\r
 \r
    3. Tholian Web\r
-\r
+s\r
    4. Enemies can ram the Enterprise. Regular Klingons and Romulans can\r
       move in Expert and Emeritus games. This code could use improvement.\r
 \r
    4. Enemies can ram the Enterprise. Regular Klingons and Romulans can\r
       move in Expert and Emeritus games. This code could use improvement.\r
 \r
@@ -102,7 +97,7 @@ static void listCommands(int x) {
 \r
 static void helpme(void) {\r
        int i, j;\r
 \r
 static void helpme(void) {\r
        int i, j;\r
-       char cmdbuf[32];\r
+       char cmdbuf[32], *cp;\r
        char linebuf[132];\r
        FILE *fp;\r
        /* Give help on commands */\r
        char linebuf[132];\r
        FILE *fp;\r
        /* Give help on commands */\r
@@ -129,37 +124,41 @@ static void helpme(void) {
                strcpy(cmdbuf, " ABBREV");\r
        }\r
        else {\r
                strcpy(cmdbuf, " ABBREV");\r
        }\r
        else {\r
-               strcpy(cmdbuf, "  Mnemonic:  ");\r
-               j = 0;\r
-               while ((cmdbuf[j+13] = toupper(commands[i][j])) != 0) j++;\r
+           for (j = 0; commands[i][j]; j++)\r
+               cmdbuf[j] = toupper(commands[i][j]);\r
+           cmdbuf[j] = '\0';\r
        }\r
        }\r
-       fp = fopen("sst.doc", "r");\r
+       fp = fopen(SSTDOC, "r");\r
        if (fp == NULL) {\r
                prout("Spock-  \"Captain, that information is missing from the");\r
                prout("   computer. You need to find SST.DOC and put it in the");\r
                prout("   current directory.\"");\r
                return;\r
        }\r
        if (fp == NULL) {\r
                prout("Spock-  \"Captain, that information is missing from the");\r
                prout("   computer. You need to find SST.DOC and put it in the");\r
                prout("   current directory.\"");\r
                return;\r
        }\r
-       i = strlen(cmdbuf);\r
-       do {\r
-               if (fgets(linebuf, 132, fp) == NULL) {\r
+       for (;;) {\r
+           if (fgets(linebuf, sizeof(linebuf), fp) == NULL) {\r
                        prout("Spock- \"Captain, there is no information on that command.\"");\r
                        fclose(fp);\r
                        return;\r
                }\r
                        prout("Spock- \"Captain, there is no information on that command.\"");\r
                        fclose(fp);\r
                        return;\r
                }\r
-       } while (strncmp(linebuf, cmdbuf, i) != 0);\r
+           if (linebuf[0] == '%' && linebuf[1] == '%'&& linebuf[2] == ' ') {\r
+               for (cp = linebuf+3; isspace(*cp); cp++)\r
+                       continue;\r
+               linebuf[strlen(linebuf)-1] = '\0';\r
+               if (strcmp(cp, cmdbuf) == 0)\r
+                   break;\r
+           }\r
+       }\r
 \r
        skip(1);\r
        prout("Spock- \"Captain, I've found the following information:\"");\r
        skip(1);\r
 \r
 \r
        skip(1);\r
        prout("Spock- \"Captain, I've found the following information:\"");\r
        skip(1);\r
 \r
-       do {\r
-               if (linebuf[0]!=12) { // ignore page break lines \r
-                       linebuf[strlen(linebuf)-1] = '\0'; // No \n at end\r
-                       prout(linebuf);\r
-               }\r
-               fgets(linebuf,132,fp);\r
-       } while (strstr(linebuf, "******")==NULL);\r
+       while (fgets(linebuf, sizeof(linebuf),fp)) {\r
+               if (strstr(linebuf, "******"))\r
+                       break;\r
+               proutc(linebuf);\r
+       }\r
        fclose(fp);\r
 }\r
 \r
        fclose(fp);\r
 }\r
 \r
@@ -323,7 +322,7 @@ static void makemoves(void) {
                                events();\r
                                if (alldone) break;             // Events did us in\r
                        }\r
                                events();\r
                                if (alldone) break;             // Events did us in\r
                        }\r
-                       if (d.galaxy[quadx][quady] == 1000) { // Galaxy went Nova!\r
+                       if (game.state.galaxy[quadx][quady] == 1000) { // Galaxy went Nova!\r
                                atover(0);\r
                                continue;\r
                        }\r
                                atover(0);\r
                                continue;\r
                        }\r
@@ -331,7 +330,7 @@ static void makemoves(void) {
                        if (hitme && justin==0) {\r
                                attack(2);\r
                                if (alldone) break;\r
                        if (hitme && justin==0) {\r
                                attack(2);\r
                                if (alldone) break;\r
-                               if (d.galaxy[quadx][quady] == 1000) {   // went NOVA! \r
+                               if (game.state.galaxy[quadx][quady] == 1000) {  // went NOVA! \r
                                        atover(0);\r
                                        hitme = TRUE;\r
                                        continue;\r
                                        atover(0);\r
                                        hitme = TRUE;\r
                                        continue;\r
@@ -348,21 +347,19 @@ int main(int argc, char **argv) {
        int i;\r
        int hitme;\r
        char ch;\r
        int i;\r
        int hitme;\r
        char ch;\r
-       prelim();\r
 \r
 \r
+       prelim(); \r
+       iostart();\r
+       line[0] = '\0';\r
        if (argc > 1) {\r
        if (argc > 1) {\r
-               fromcommandline = 1;\r
-               line[0] = '\0';\r
                while (--argc > 0) {\r
                        strcat(line, *(++argv));\r
                        strcat(line, " ");\r
                }\r
        }\r
                while (--argc > 0) {\r
                        strcat(line, *(++argv));\r
                        strcat(line, " ");\r
                }\r
        }\r
-       else fromcommandline = 0;\r
-\r
 \r
        while (TRUE) { /* Play a game */\r
 \r
        while (TRUE) { /* Play a game */\r
-               setup();\r
+               setup(line[0] == '\0');\r
                if (alldone) {\r
                        score();\r
                        alldone = 0;\r
                if (alldone) {\r
                        score();\r
                        alldone = 0;\r
@@ -373,13 +370,13 @@ int main(int argc, char **argv) {
                skip(1);\r
 \r
                if (tourn && alldone) {\r
                skip(1);\r
 \r
                if (tourn && alldone) {\r
-                       printf("Do you want your score recorded?");\r
+                       proutn("Do you want your score recorded?");\r
                        if (ja()) {\r
                                chew2();\r
                                freeze(FALSE);\r
                        }\r
                }\r
                        if (ja()) {\r
                                chew2();\r
                                freeze(FALSE);\r
                        }\r
                }\r
-               printf("Do you want to play again?");\r
+               proutn("Do you want to play again?");\r
                if (!ja()) break;\r
        }\r
        skip(1);\r
                if (!ja()) break;\r
        }\r
        skip(1);\r
@@ -407,20 +404,20 @@ void cramen(int i) {
        proutn(s);\r
 }\r
 \r
        proutn(s);\r
 }\r
 \r
-void cramlc(int key, int x, int y) {\r
-       if (key == 1) proutn(" Quadrant");\r
-       else if (key == 2) proutn(" Sector");\r
-       proutn(" ");\r
-       crami(x, 1);\r
-       proutn(" - ");\r
-       crami(y, 1);\r
+char *cramlc(enum loctype key, int x, int y) {\r
+       static char buf[32];\r
+       buf[0] = '\0';\r
+       if (key == quadrant) strcpy(buf, "Quadrant ");\r
+       else if (key == sector) strcpy(buf, "Sector ");\r
+       sprintf(buf+strlen(buf), "%d-%d", x, y);\r
+       return buf;\r
 }\r
 \r
 void crmena(int i, int enemy, int key, int x, int y) {\r
        if (i == 1) proutn("***");\r
        cramen(enemy);\r
 }\r
 \r
 void crmena(int i, int enemy, int key, int x, int y) {\r
        if (i == 1) proutn("***");\r
        cramen(enemy);\r
-       proutn(" at");\r
-       cramlc(key, x, y);\r
+       proutn(" at ");\r
+       proutn(cramlc(key, x, y));\r
 }\r
 \r
 void crmshp(void) {\r
 }\r
 \r
 void crmshp(void) {\r
@@ -457,14 +454,12 @@ void iran10(int *i, int *j) {
 }\r
 \r
 void chew(void) {\r
 }\r
 \r
 void chew(void) {\r
-       linecount = 0;\r
        linep = line;\r
        *linep = 0;\r
 }\r
 \r
 void chew2(void) {\r
        /* return IHEOL next time */\r
        linep = line;\r
        *linep = 0;\r
 }\r
 \r
 void chew2(void) {\r
        /* return IHEOL next time */\r
-       linecount = 0;\r
        linep = line+1;\r
        *linep = 0;\r
 }\r
        linep = line+1;\r
        *linep = 0;\r
 }\r
@@ -473,8 +468,6 @@ int scan(void) {
        int i;\r
        char *cp;\r
 \r
        int i;\r
        char *cp;\r
 \r
-       linecount = 0;\r
-\r
        // Init result\r
        aaitem = 0.0;\r
        *citem = 0;\r
        // Init result\r
        aaitem = 0.0;\r
        *citem = 0;\r
@@ -530,87 +523,8 @@ int ja(void) {
        }\r
 }\r
 \r
        }\r
 }\r
 \r
-void cramf(double x, int w, int d) {\r
-       char buf[64];\r
-       sprintf(buf, "%*.*f", w, d, x);\r
-       proutn(buf);\r
-}\r
-\r
-void crami(int i, int w) {\r
-       char buf[16];\r
-       sprintf(buf, "%*d", w, i);\r
-       proutn(buf);\r
-}\r
-\r
 double square(double i) { return i*i; }\r
                                                                        \r
 double square(double i) { return i*i; }\r
                                                                        \r
-static void clearscreen(void) {\r
-       /* Somehow we need to clear the screen */\r
-#ifdef __BORLANDC__\r
-       extern void clrscr(void);\r
-       clrscr();\r
-#else\r
-       proutn("\033[2J");      /* Hope for an ANSI display */\r
-#endif\r
-}\r
-\r
-/* We will pull these out in case we want to do something special later */\r
-\r
-void pause(int i) {\r
-       putchar('\n');\r
-       if (i==1) {\r
-               if (skill > 2)\r
-                       prout("[ANOUNCEMENT ARRIVING...]");\r
-               else\r
-                       prout("[IMPORTANT ANNOUNCEMENT ARRIVING -- HIT SPACE BAR TO CONTINUE]");\r
-               getch();\r
-       }\r
-       else {\r
-               if (skill > 2)\r
-                       proutn("[CONTINUE?]");\r
-               else\r
-                       proutn("[HIT SPACE BAR TO CONTINUE]");\r
-               getch();\r
-               proutn("\r                           \r");\r
-       }\r
-       if (i != 0) {\r
-               clearscreen();\r
-       }\r
-       linecount = 0;\r
-}\r
-\r
-\r
-void skip(int i) {\r
-       while (i-- > 0) {\r
-               linecount++;\r
-               if (linecount >= 23)\r
-                       pause(0);\r
-               else\r
-                       putchar('\n');\r
-       }\r
-}\r
-\r
-\r
-void proutn(char *s) {\r
-       fputs(s, stdout);\r
-}\r
-\r
-void prout(char *s) {\r
-       proutn(s);\r
-       skip(1);\r
-}\r
-\r
-void prouts(char *s) {\r
-       clock_t endTime;\r
-       /* print slowly! */\r
-       while (*s) {\r
-               endTime = clock() + CLOCKS_PER_SEC*0.05;\r
-               while (clock() < endTime) ;\r
-               putchar(*s++);\r
-               fflush(stdout);\r
-       }\r
-}\r
-\r
 void huh(void) {\r
        chew();\r
        skip(1);\r
 void huh(void) {\r
        chew();\r
        skip(1);\r
@@ -637,7 +551,7 @@ void debugme(void) {
        proutn("Reset damage? ");\r
        if (ja() != 0) {\r
                int i;\r
        proutn("Reset damage? ");\r
        if (ja() != 0) {\r
                int i;\r
-               for (i=0; i <= ndevice; i++) if (damage[i] > 0.0) damage[i] = 0.0;\r
+               for (i=0; i <= NDEVICES; i++) if (damage[i] > 0.0) damage[i] = 0.0;\r
                stdamtim = 1e30;\r
        }\r
        proutn("Toggle idebug? ");\r
                stdamtim = 1e30;\r
        }\r
        proutn("Toggle idebug? ");\r
@@ -649,7 +563,7 @@ void debugme(void) {
        proutn("Cause selective damage? ");\r
        if (ja() != 0) {\r
                int i, key;\r
        proutn("Cause selective damage? ");\r
        if (ja() != 0) {\r
                int i, key;\r
-               for (i=1; i <= ndevice; i++) {\r
+               for (i=1; i <= NDEVICES; i++) {\r
                        proutn("Kill ");\r
                        proutn(device[i]);\r
                        proutn("? ");\r
                        proutn("Kill ");\r
                        proutn(device[i]);\r
                        proutn("? ");\r
@@ -657,7 +571,7 @@ void debugme(void) {
                        key = scan();\r
                        if (key == IHALPHA &&  isit("y")) {\r
                                damage[i] = 10.0;\r
                        key = scan();\r
                        if (key == IHALPHA &&  isit("y")) {\r
                                damage[i] = 10.0;\r
-                               if (i == DRADIO) stdamtim = d.date;\r
+                               if (i == DRADIO) stdamtim = game.state.date;\r
                        }\r
                }\r
        }\r
                        }\r
                }\r
        }\r
@@ -676,12 +590,12 @@ void debugme(void) {
                                case FSCMOVE: proutn("SC Move         "); break;\r
                                case FSCDBAS: proutn("SC Base Destroy "); break;\r
                        }\r
                                case FSCMOVE: proutn("SC Move         "); break;\r
                                case FSCDBAS: proutn("SC Base Destroy "); break;\r
                        }\r
-                       cramf(future[i]-d.date, 8, 2);\r
+                       proutn("%.2f", future[i]-game.state.date);\r
                        chew();\r
                        proutn("  ?");\r
                        key = scan();\r
                        if (key == IHREAL) {\r
                        chew();\r
                        proutn("  ?");\r
                        key = scan();\r
                        if (key == IHREAL) {\r
-                               future[i] = d.date + aaitem;\r
+                               future[i] = game.state.date + aaitem;\r
                        }\r
                }\r
                chew();\r
                        }\r
                }\r
                chew();\r