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 b3a0202663bd5d6760366c82f8a1bf73d4f24545..d35b2556ae1407f297b0088838a2c5124b1140c0 100644 (file)
--- a/sst.c
+++ b/sst.c
@@ -1,19 +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
-#include <stdarg.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
-static int screenheight = 24;\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
@@ -40,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
@@ -135,7 +128,7 @@ static void helpme(void) {
                cmdbuf[j] = toupper(commands[i][j]);\r
            cmdbuf[j] = '\0';\r
        }\r
                cmdbuf[j] = toupper(commands[i][j]);\r
            cmdbuf[j] = '\0';\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
        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
@@ -164,8 +157,7 @@ static void helpme(void) {
        while (fgets(linebuf, sizeof(linebuf),fp)) {\r
                if (strstr(linebuf, "******"))\r
                        break;\r
        while (fgets(linebuf, sizeof(linebuf),fp)) {\r
                if (strstr(linebuf, "******"))\r
                        break;\r
-               /* use fputs here to avoid % expansion */\r
-               fputs(linebuf, stdout);\r
+               proutc(linebuf);\r
        }\r
        fclose(fp);\r
 }\r
        }\r
        fclose(fp);\r
 }\r
@@ -355,25 +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
-       char *LINES = getenv("LINES");\r
\r
-       if (LINES)\r
-           screenheight = atoi(LINES);\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
@@ -430,7 +416,7 @@ char *cramlc(enum loctype key, int x, int y) {
 void crmena(int i, int enemy, int key, int x, int y) {\r
        if (i == 1) proutn("***");\r
        cramen(enemy);\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
+       proutn(" at ");\r
        proutn(cramlc(key, x, y));\r
 }\r
 \r
        proutn(cramlc(key, x, y));\r
 }\r
 \r
@@ -468,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
@@ -484,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
@@ -543,85 +525,6 @@ int ja(void) {
 \r
 double square(double i) { return i*i; }\r
                                                                        \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 >= screenheight)\r
-                       pause(0);\r
-               else\r
-                       putchar('\n');\r
-       }\r
-}\r
-\r
-\r
-void proutn(char *fmt, ...) {\r
-    va_list ap;\r
-    va_start(ap, fmt);\r
-    vprintf(fmt, ap);\r
-    va_end(ap);\r
-}\r
-\r
-void prout(char *fmt, ...) {\r
-    va_list ap;\r
-    va_start(ap, fmt);\r
-    vprintf(fmt, ap);\r
-    va_end(ap);\r
-    skip(1);\r
-}\r
-\r
-void prouts(char *fmt, ...) {\r
-       clock_t endTime;\r
-       char *s, buf[BUFSIZ];\r
-       /* print slowly! */\r
-       va_list ap;\r
-       va_start(ap, fmt);\r
-       vsprintf(buf, fmt, ap);\r
-       va_end(ap);\r
-       skip(1);\r
-       for (s = buf; *s; 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