
/*  program ADVENT.C    modified by LCC for V 1.43 by:
                        altering buffer sizes

           Changed call of exec() to static call for Eco-C88 (BW)
           Added function initw() for Eco-C88                (BW)
           Made function init() static for Eco-C88           (BW)

May 1990 - Bob Withers
         - Ported code to Microsoft C V 5.10 and 6.00
         - Placed all global variables in header ADVENT.H
         - Removed most runtime variable initialization and replaced
           by compile time initializers.
         - Added file ADVENTDB.C to completely replace all the adventure
           data base files.  This keeps all data in memory and eliminates
           all disk accesses during game play.  All functions in DATABASE.C
           were modified to access in memory data.
         - Added code to support the BRIEF and VERBOSE verbs.
         - Added code to ignore CTRL-C signal.
         - Modified vocab search to use a binary search.

Aug 1992 - Bob Withers
         - Ported to Sozobon V2.0 on the Atari ST.
*/

#define DRIVER

#include "advent.h"

#if ANSI_PROTO
static VOID     NEAR PASCAL init(VOID);
static VOID     NEAR PASCAL eadvent(SHORT rest);
static VOID     NEAR PASCAL AdvInit PROTO((VOID));
#else
extern VOID                 init();
extern VOID                 eadvent();
extern VOID                 AdvInit();
#endif


#if ANSI_PROTO
int main(int argc, char **argv)
#else
int main(argc, argv)
int         argc;
char        **argv;
#endif
{
    auto     SHORT      rflag;      /* user restore request option */
    auto     SHORT      dflag;      /* user restore request option */

    dbgflg = rflag = 0;
    AdvInit();

    while (--argc > 0)
    {
        if ((argv[argc])[0] == '-' || (argv[argc])[0] == '/')
        {
            switch (tolower((argv[argc])[1]))
            {
                case 'r':
                    ++rflag;
                    continue;

                case 'd':
                    ++dbgflg;
                    printf("Debug enabled.\n");
                    continue;

                case 'b':
                    ++G.brief_sw;
                    printf("Brief mode enabled.\n");
                    continue;

                default:
                    printf("unknown flag: %c\n", (argv[argc])[1]);
                    continue;
            }
        }
    }

    dflag = dbgflg;
    init();

    if (!rflag)
    {
        printf("\nDo you want to restore a saved game? (Y/N) ");
        do
        {
            rflag = getch();
            rflag = toupper(rflag);
        } while (!(rflag == 'Y' || rflag == 'N'));

        putchar(rflag);
        putchar('\n');
        if (rflag == 'N')
            rflag = 0;
    }

    if (rflag)
        restore();

    dbgflg = dflag;
    eadvent(rflag);
    return(0);
}


/*  Initialization  */

static VOID NEAR PASCAL init PROTO((VOID))
{
    G.dloc[DWARFMAX - 1] = G.chloc;
    return;
}


/*  restore saved game handler  */

VOID PASCAL restore PROTO((VOID))
{
    auto     char       username[14];
    auto     FILE      *restfd;
    auto     char      *sptr;

    printf("What is your saved game name? ");
    scanf("%8s", username);
    for (sptr = username; *sptr; sptr++)
    {
        *sptr = (char) toupper(*sptr);
        if ((!isprint(*sptr)) || *sptr == ' ' || *sptr == '.')
        {
            *sptr = '\0';
            break;
        }
    }

    if (strlen(username) == 0)
    {
        printf("\nNo name entered, restore request ignored.\n");
        return;
    }

    strcat(username, ".ADV");
    printf("\nOpening save file \"%s\".\n", username);
    if ((restfd = fopen(username, READ_BIN)) == 0)
    {
        printf("sorry, can't open save file...\n");
        return;
    }

    if (1 != fread(&G, sizeof(G), 1, restfd))
    {
        printf("Can't read save file...\n");
        fclose(restfd);
        exit(1);
    }

    if (fclose(restfd) == -1)
        printf("warning -- can't close save file...\n");

    printf("\n");
    saveflg = FALSE;

    strcpy(word1, "l");
    gettrav(G.loc);
    game_restored = TRUE;
    return;
}


#if ANSI_PROTO
static VOID NEAR PASCAL eadvent(SHORT rest)
#else
static VOID NEAR PASCAL eadvent(rest)
SHORT       rest;
#endif
{
    srand(511);

    if (!rest)
    {
        if (yes(65, 1, 0))
            G.limit = 1000;
        else
            G.limit = 330;
    }

    while (!saveflg)
        turn();

    if (saveflg)
        saveadv();

    return;
}


static VOID NEAR PASCAL AdvInit PROTO((VOID))
{
    register int        i;

    G.turns   = 0;
    G.loc     = 1;
    G.oldloc  = 1;
    G.oldloc2 = 1;
    G.newloc  = 3;

    G.brief_sw= 0;
    G.tally   = 15;
    G.tally2  = 0;
    G.limit   = 100;
    G.lmwarn  = 0;
    G.wzdark  = FALSE;
    G.closing = FALSE;
    G.closed  = FALSE;
    G.holding = 0;
    G.detail  = 0;
    G.knfloc  = 0;
    G.clock1  = 30;
    G.clock2  = 50;
    G.panic   = 0;
    G.dflag   = 0;
    G.daltloc = 18;
    G.dkill   = 0;
    G.chloc   = 114;
    G.chloc2  = 140;
    G.bonus   = 0;
    G.numdie  = 0;
    G.gaveup  = FALSE;
    G.foobar  = 0;

    for (i = 0; i < MAXOBJ; ++i)
    {
        G.place[i] = place[i];
        G.fixed[i] = fixed[i];
        G.prop[i]  = prop[i];
    }

    for (i = 0; i < DWARFMAX; ++i)
    {
        G.dloc[i] = dloc[i];
        G.dseen[i] = 0;
        G.odloc[i] = 0;
    }                

    for (i = 0; i < MAXLOC; ++i)
        G.visited[i] = 0;

    return;
}
