[BACK]Return to main.c CVS log [TXT][DIR] Up to [contributed] / early-roguelike / rogue3

Annotation of early-roguelike/rogue3/main.c, Revision 1.1.1.1

1.1       rubenllo    1: /*
                      2:  * @(#)main.c  3.27 (Berkeley) 6/15/81
                      3:  *
                      4:  * Rogue: Exploring the Dungeons of Doom
                      5:  * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
                      6:  * All rights reserved.
                      7:  *
                      8:  * See the file LICENSE.TXT for full copyright and licensing information.
                      9:  */
                     10:
                     11: #include "curses.h"
                     12: #include <time.h>
                     13: #include <signal.h>
                     14: #include <limits.h>
                     15: #include <stdlib.h>
                     16: #include <stdarg.h>
                     17: #include <string.h>
                     18: #include "machdep.h"
                     19: #include "rogue.h"
                     20:
                     21: int num_checks = 0;                    /* times we've gone over in checkout() */
                     22: WINDOW *cw;                              /* Window that the player sees */
                     23: WINDOW *hw;                              /* Used for the help command */
                     24: WINDOW *mw;                              /* Used to store mosnters */
                     25: FILE   *scoreboard = NULL;
                     26: FILE   *logfi = NULL;
                     27:
                     28: int
                     29: main(int argc, char *argv[], char *envp[])
                     30: {
                     31:     char *env;
                     32:     struct linked_list *item;
                     33:     struct object *obj;
                     34:
                     35:     md_init(MD_STRIP_CTRL_KEYPAD);
                     36:
                     37:     open_score();
                     38:     open_log();
                     39:
                     40:     /*
                     41:      * check for print-score option
                     42:      */
                     43:     if (argc == 2 && strcmp(argv[1], "-s") == 0)
                     44:     {
                     45:        waswizard = TRUE;
                     46:        score(0, -1, 0);
                     47:        exit(0);
                     48:     }
                     49:
                     50: #ifdef WIZARD
                     51:     /*
                     52:      * Check to see if he is a wizard
                     53:      */
                     54:     if (argc >= 2 && argv[1][0] == '\0')
                     55:        if (strcmp(PASSWD, crypt(md_getpass("Wizard's password: "), "mT")) == 0)
                     56:        {
                     57:            wizard = TRUE;
                     58:            argv++;
                     59:            argc--;
                     60:        }
                     61: #endif
                     62:
                     63: /* Are we using the system savefile directory? */
                     64: #ifdef SAVEDIR
                     65:     if (argc >= 3 && !strcmp(argv[1], "-n"))
                     66:     {
                     67:         strncpy(whoami, argv[2], 79);
                     68:         whoami[79] = '\0';
                     69:         use_savedir = TRUE;
                     70:         /* look for savefile at SAVEDIR/UID-playername.r3sav */
                     71:         if (snprintf(file_name, 256, "%s/%d-%s.r3sav", SAVEDIR, md_getuid(), whoami) >= 256)
                     72:         {
                     73:             /* this shouldn't happen */
                     74:             strcpy(file_name, "rogue3.save");
                     75:             use_savedir = FALSE;
                     76:         }
                     77:     }
                     78: #endif
                     79:
                     80:     if (use_savedir == FALSE)
                     81:     {
                     82:         md_normaluser();
                     83:         /* because we don't need to create a file in the common savedir,
                     84:          * and the scorefile is already open */
                     85:         strcpy(home, md_gethomedir());
                     86:
                     87:         if (strlen(home) > PATH_MAX - strlen("rogue3.save") - 1)
                     88:             *home = 0;
                     89:
                     90:         strcpy(file_name, home);
                     91:         strcat(file_name, "rogue3.save");
                     92:     }
                     93:
                     94:     if ((env = getenv("ROGUEOPTS")) != NULL)
                     95:        parse_opts(env);
                     96:     if (!use_savedir && (env == NULL || whoami[0] == '\0'))
                     97:        strucpy(whoami, md_getusername(), strlen(md_getusername()));
                     98:     if (env == NULL || fruit[0] == '\0')
                     99:        strcpy(fruit, "slime-mold");
                    100:
                    101:     if (too_much() && !wizard && !author())
                    102:     {
                    103:        printf("Sorry, %s, but the system is too loaded now.\n", whoami);
                    104:        printf("Try again later.  Meanwhile, why not enjoy a%s %s?\n",
                    105:            vowelstr(fruit), fruit);
                    106:        exit(1);
                    107:     }
                    108:
                    109:     /* now start the game */
                    110:     if (use_savedir)
                    111:     {
                    112:       /* Try to restore from file_name which we just set up. */
                    113:       if (!restore(file_name, envp))
                    114:         exit(1);
                    115:       /* If restore() returns true, the system savefile doesn't exist.
                    116:          So we'll start a new game. */
                    117:     }
                    118:     else if (argc == 2)
                    119:        if (!restore(argv[1], envp)) /* Note: restore will never return */
                    120:            exit(1);
                    121:
                    122:     /* If we reach this point, either
                    123:      * 1. A system savefile was specified and doesn't exist.
                    124:      * 2. No savefile was specified.
                    125:      * Either way, start a new game.
                    126:      */
                    127:
                    128:     if (!use_savedir)
                    129:       md_normaluser();
                    130:
                    131:     env = getenv("SEED");
                    132:
                    133:     if (env)
                    134:         seed = atoi(env);
                    135:     else
                    136:         seed = 0;
                    137:
                    138:     if (seed > 0)
                    139:     {
                    140:         waswizard = 1; /* don't save scores if SEED specified */
                    141:         dnum = seed;
                    142:     }
                    143:     else
                    144:         dnum = md_random_seed();
                    145:
                    146:     if (wizard || env)
                    147:        printf("Hello %s, welcome to dungeon #%d\n", whoami, dnum);
                    148:     else
                    149:        printf("Hello %s, just a moment while I dig the dungeon...\n", whoami);
                    150:
                    151:     fflush(stdout);
                    152:     seed = dnum;
                    153:     init_player();                     /* Roll up the rogue */
                    154:     init_things();                     /* Set up probabilities of things */
                    155:     init_names();                      /* Set up names of scrolls */
                    156:     init_colors();                     /* Set up colors of potions */
                    157:     init_stones();                     /* Set up stone settings of rings */
                    158:     init_materials();                  /* Set up materials of wands */
                    159:     initscr();                         /* Start up cursor package */
                    160:
                    161:     if (COLS < 70)
                    162:     {
                    163:        endwin();
                    164:        printf("\n\nSorry, %s, but your terminal window has too few columns.\n", whoami);
                    165:        printf("Your terminal has %d columns, needs 70.\n",COLS);
                    166:        exit(1);
                    167:     }
                    168:     if (LINES < 22)
                    169:     {
                    170:        endwin();
                    171:        printf("\n\nSorry, %s, but your terminal window has too few lines.\n", whoami);
                    172:        printf("Your terminal has %d lines, needs 22.\n",LINES);
                    173:        exit(1);
                    174:     }
                    175:
                    176:
                    177:     setup();
                    178:     /*
                    179:      * Set up windows
                    180:      */
                    181:     cw = newwin(LINES, COLS, 0, 0);
                    182:     mw = newwin(LINES, COLS, 0, 0);
                    183:     hw = newwin(LINES, COLS, 0, 0);
                    184:     keypad(cw,1);
                    185:     waswizard = wizard;
                    186:     new_level();                       /* Draw current level */
                    187:     /*
                    188:      * Start up daemons and fuses
                    189:      */
                    190:     start_daemon(doctor, 0, AFTER);
                    191:     fuse(swander, 0, WANDERTIME, AFTER);
                    192:     start_daemon(stomach, 0, AFTER);
                    193:     start_daemon(runners, 0, AFTER);
                    194:     /*
                    195:      * Give the rogue his weaponry.  First a mace.
                    196:      */
                    197:     item = new_item(sizeof *obj);
                    198:     obj = (struct object *) ldata(item);
                    199:     obj->o_type = WEAPON;
                    200:     obj->o_which = MACE;
                    201:     init_weapon(obj, MACE);
                    202:     obj->o_hplus = 1;
                    203:     obj->o_dplus = 1;
                    204:     obj->o_flags |= ISKNOW;
                    205:     add_pack(item, TRUE);
                    206:     cur_weapon = obj;
                    207:     /*
                    208:      * Now a +1 bow
                    209:      */
                    210:     item = new_item(sizeof *obj);
                    211:     obj = (struct object *) ldata(item);
                    212:     obj->o_type = WEAPON;
                    213:     obj->o_which = BOW;
                    214:     init_weapon(obj, BOW);
                    215:     obj->o_hplus = 1;
                    216:     obj->o_dplus = 0;
                    217:     obj->o_flags |= ISKNOW;
                    218:     add_pack(item, TRUE);
                    219:     /*
                    220:      * Now some arrows
                    221:      */
                    222:     item = new_item(sizeof *obj);
                    223:     obj = (struct object *) ldata(item);
                    224:     obj->o_type = WEAPON;
                    225:     obj->o_which = ARROW;
                    226:     init_weapon(obj, ARROW);
                    227:     obj->o_count = 25+rnd(15);
                    228:     obj->o_hplus = obj->o_dplus = 0;
                    229:     obj->o_flags |= ISKNOW;
                    230:     add_pack(item, TRUE);
                    231:     /*
                    232:      * And his suit of armor
                    233:      */
                    234:     item = new_item(sizeof *obj);
                    235:     obj = (struct object *) ldata(item);
                    236:     obj->o_type = ARMOR;
                    237:     obj->o_which = RING_MAIL;
                    238:     obj->o_ac = a_class[RING_MAIL] - 1;
                    239:     obj->o_flags |= ISKNOW;
                    240:     cur_armor = obj;
                    241:     add_pack(item, TRUE);
                    242:     /*
                    243:      * Give him some food too
                    244:      */
                    245:     item = new_item(sizeof *obj);
                    246:     obj = (struct object *) ldata(item);
                    247:     obj->o_type = FOOD;
                    248:     obj->o_count = 1;
                    249:     obj->o_which = 0;
                    250:     add_pack(item, TRUE);
                    251:     playit();
                    252: }
                    253:
                    254: /*
                    255:  * endit:
                    256:  *     Exit the program abnormally.
                    257:  */
                    258:
                    259: void
                    260: endit(int p)
                    261: {
                    262:     fatal("Ok, if you want to exit that badly, I'll have to allow it\n");
                    263: }
                    264:
                    265: /*
                    266:  * fatal:
                    267:  *     Exit the program, printing a message.
                    268:  */
                    269:
                    270: void
                    271: fatal(char *s)
                    272: {
                    273:     clear();
                    274:     move(LINES-2, 0);
                    275:     printw("%s", s);
                    276:     draw(stdscr);
                    277:     endwin();
                    278:     exit(0);
                    279: }
                    280:
                    281: /*
                    282:  * rnd:
                    283:  *     Pick a very random number.
                    284:  */
                    285:
                    286: int
                    287: rnd(int range)
                    288: {
                    289:     return range == 0 ? 0 : abs(RN) % range;
                    290: }
                    291:
                    292: /*
                    293:  * roll:
                    294:  *     roll a number of dice
                    295:  */
                    296:
                    297: int
                    298: roll(int number, int sides)
                    299: {
                    300:     int dtotal = 0;
                    301:
                    302:     while(number--)
                    303:        dtotal += rnd(sides)+1;
                    304:     return dtotal;
                    305: }
                    306: /*
                    307:  * handle stop and start signals
                    308:  */
                    309:
                    310: void
                    311: tstp(int p)
                    312: {
                    313: #ifdef SIGTSTP
                    314:     signal(SIGTSTP, SIG_IGN);
                    315: #endif
                    316:     mvcur(0, COLS - 1, LINES - 1, 0);
                    317:     endwin();
                    318:     fflush(stdout);
                    319: #ifdef SIGTSTP
                    320:     signal(SIGTSTP, SIG_DFL);
                    321:     kill(0, SIGTSTP);
                    322:     signal(SIGTSTP, tstp);
                    323: #endif
                    324:     nonl();
                    325:     crmode();
                    326:     noecho();
                    327:     clearok(curscr, TRUE);
                    328:     touchwin(cw);
                    329:     draw(cw);
                    330:     flush_type();      /* flush input */
                    331: }
                    332:
                    333: void
                    334: setup()
                    335: {
                    336: #ifdef SIGHUP
                    337:     signal(SIGHUP, auto_save);
                    338: #endif
                    339:     signal(SIGILL, auto_save);
                    340: #ifdef SIGTRAP
                    341:     signal(SIGTRAP, auto_save);
                    342: #endif
                    343: #ifdef SIGIOT
                    344:     signal(SIGIOT, auto_save);
                    345: #endif
                    346: #ifdef SIGEMT
                    347:     signal(SIGEMT, auto_save);
                    348: #endif
                    349:     signal(SIGFPE, auto_save);
                    350: #ifdef SIGBUS
                    351:     signal(SIGBUS, auto_save);
                    352: #endif
                    353:     signal(SIGSEGV, auto_save);
                    354: #ifdef SIGSYS
                    355:     signal(SIGSYS, auto_save);
                    356: #endif
                    357: #ifdef SIGPIPE
                    358:     signal(SIGPIPE, auto_save);
                    359: #endif
                    360:     signal(SIGTERM, auto_save);
                    361:     signal(SIGINT, quit);
                    362: #ifdef SIGQUIT
                    363:     signal(SIGQUIT, endit);
                    364: #endif
                    365: #ifdef SIGTSTP
                    366:     signal(SIGTSTP, tstp);
                    367: #endif
                    368:
                    369:     if (!author())
                    370:     {
                    371:        md_start_checkout_timer(CHECKTIME * 60);
                    372:        num_checks = 0;
                    373:     }
                    374:
                    375:     nonl();
                    376:     crmode();                          /* Cbreak mode */
                    377:     noecho();                          /* Echo off */
                    378: }
                    379:
                    380: /*
                    381:  * playit:
                    382:  *     The main loop of the program.  Loop until the game is over,
                    383:  * refreshing things and looking at the proper times.
                    384:  */
                    385:
                    386: void
                    387: playit()
                    388: {
                    389:     char *opts;
                    390:
                    391:     /*
                    392:      * set up defaults for slow terminals
                    393:      */
                    394:
                    395:
                    396:     if (baudrate() < 1200)
                    397:     {
                    398:        terse = TRUE;
                    399:        jump = TRUE;
                    400:     }
                    401:
                    402:     /*
                    403:      * parse environment declaration of options
                    404:      */
                    405:     if ((opts = getenv("ROGUEOPTS")) != NULL)
                    406:        parse_opts(opts);
                    407:
                    408:
                    409:     oldpos = hero;
                    410:     oldrp = roomin(&hero);
                    411:     while (playing)
                    412:        command();                      /* Command execution */
                    413:     endit(-1);
                    414: }
                    415:
                    416: /*
                    417:  * see if the system is being used too much for this game
                    418:  */
                    419: int
                    420: too_much()
                    421: {
                    422:     double avec[3];
                    423:
                    424: #ifdef MAXLOAD
                    425:     if (md_loadav(avec) == 0)
                    426:        return (avec[2] > (MAXLOAD / 10.0));
                    427: #endif
                    428: #ifdef MAXUSERS
                    429:     return (md_ucount() > MAXUSERS);
                    430: #endif
                    431:     return 0;
                    432: }
                    433:
                    434: /*
                    435:  * see if a user is an author of the program
                    436:  */
                    437: int
                    438: author()
                    439: {
                    440:     switch (md_getuid())
                    441:     {
                    442:        case AUTHORUID:
                    443:            return TRUE;
                    444:        default:
                    445:            return FALSE;
                    446:     }
                    447: }
                    448:
                    449: void
                    450: checkout(int p)
                    451: {
                    452:     static char *msgs[] = {
                    453:        "The load is too high to be playing.  Please leave in %d minutes",
                    454:        "Please save your game.  You have %d minutes",
                    455:        "Last warning.  You have %d minutes to leave",
                    456:     };
                    457:     int checktime;
                    458:     if (too_much())
                    459:     {
                    460:        if (num_checks >= 3)
                    461:            fatal("Sorry.  You took to long.  You are dead\n");
                    462:        checktime = CHECKTIME / (num_checks + 1);
                    463:        if (num_checks < 3)
                    464:                chmsg(msgs[num_checks++], checktime);
                    465:        md_start_checkout_timer(checktime * 60);
                    466:     }
                    467:     else
                    468:     {
                    469:        if (num_checks)
                    470:        {
                    471:            chmsg("The load has dropped back down.  You have a reprieve.");
                    472:            num_checks = 0;
                    473:        }
                    474:        md_start_checkout_timer(CHECKTIME * 60);
                    475:     }
                    476: }
                    477:
                    478: /*
                    479:  * checkout()'s version of msg.  If we are in the middle of a shell, do a
                    480:  * printf instead of a msg to avoid the refresh.
                    481:  */
                    482: void
                    483: chmsg(char *fmt, ...)
                    484: {
                    485:     va_list args;
                    486:
                    487:     if (in_shell)
                    488:     {
                    489:        va_start(args, fmt);
                    490:        vprintf(fmt, args);
                    491:        va_end(args);
                    492:        putchar('\n');
                    493:        fflush(stdout);
                    494:     }
                    495:     else
                    496:     {
                    497:         va_start(args, fmt);
                    498:         doadd(fmt, args);
                    499:         va_end(args);
                    500:        endmsg();
                    501:     }
                    502: }

CVSweb