text: /*
   * Rogue: Exploring the Dungeons of Doom
   * Copyright (C) 1980-1983, 1985, 1999 Michael Toy, Ken Arnold and Glenn Wichman
   * All rights reserved.
   *
   * See the file LICENSE.TXT for full copyright and licensing information.
   *
   * @(#)main.c	4.22 (Berkeley) 02/05/99
   */
  
  #include <stdlib.h>
  #include <string.h>
  #include <signal.h>
  #include <time.h>
  #include <curses.h>
  #include "rogue.h"
  
  /*
   * main:
   *	The main program, of course
   */
  int
  main(int argc, char **argv, char **envp)
  {
      char *env;
      int lowtime;
  
      md_init();
  
      /*
       * FIXME: do better arg processing
       */
      while (argc >= 2) {
  #ifdef MASTER
  	if (strcmp(argv[1], "-w") == 0) {
  	    if (strcmp(PASSWD, md_crypt(md_getpass("wizard's password: "), "mT")) == 0)
  	    {
  	        wizard = TRUE;
  	        player.t_flags |= SEEMONST;
  	    }
              argc--;
              argv++;
  	    continue;
  	}
  #endif
  	if (strcmp(argv[1], "-r") == 0) {
              rookie_mode = TRUE;
              argc--;
              argv++;
  	    continue;
  	}
          if (strcmp(argv[1], "-5.3") == 0) {
              rogue_version = VERSION_5_3;
              argc--;
              argv++;
  	    continue;
          }
          if (strcmp(argv[1], "-5.4") == 0) {
              rogue_version = VERSION_5_4;
              argc--;
              argv++;
  	    continue;
  	}
  	if (strncmp(argv[1], "-", 1) == 0) {
              printf("rogue: WARNING: ignoring unknown option %s\n", argv[1]);
              argc--;
              argv++;
  	    continue;
  	}
  	break;
      }
  
      /*
       * get home and options from environment
       */
  
      strncpy(home, md_gethomedir(), MAXSTR);
  
      strcpy(file_name, home);
      strcat(file_name, "rogue.save");
  
      if ((env = getenv("ROGUEOPTS")) != NULL)
  	parse_opts(env);
      if (env == NULL || whoami[0] == '\0')
          strucpy(whoami, md_getusername(), (int) strlen(md_getusername()));
      lowtime = (int) time(NULL);
  #ifdef MASTER
      if (wizard && getenv("SEED") != NULL)
  	dnum = atoi(getenv("SEED"));
      else
  #endif
      dnum = lowtime + md_getpid();
      seed = dnum;
  
      open_score();
  
      /* 
       * Drop setuid/setgid after opening the scoreboard file. 
       */ 
      md_normaluser();
  
      /*
       * check for print-score option
       */
  
      if (argc == 2 && strcmp(argv[1], "-s") == 0) {
  	noscore = TRUE;
  	score(0, -1, 0);
  	exit(0);
      }
  
      init_check();			/* check for legal startup */
      if (argc == 2)
  	if (!restore(argv[1], envp))	/* Note: restore will never return */
  	    my_exit(1);
      printf("Hello %s %s, just a moment while I dig the dungeon...", (rookie_mode ? "rookie" : "warrior"), whoami);
  #ifdef MASTER
      if (wizard)
  	printf(" #%d", dnum);
  #endif
      fflush(stdout);
  
      md_sleep(1); /* allow enough time to see the message */
  
      initscr();				/* Start up cursor package */
      init_probs();			/* Set up prob tables for objects */
      init_names();			/* Set up names of scrolls */
      init_colors();			/* Set up colors of potions */
      init_stones();			/* Set up stone settings of rings */
      init_materials();			/* Set up materials of wands */
      /*
       * Must call this after all of the above.
       */
      init_player();			/* Set up initial player stats and objects */
      setup();
  
      /*
       * The screen must be at least NUMLINES x NUMCOLS
       */
      if (LINES < NUMLINES || COLS < NUMCOLS)
      {
  	endwin();
  	printf("\nsorry, the screen is %dx%d, must be at least %dx%d\n", LINES, COLS, NUMLINES, COLS, NUMCOLS);
          fflush(stdout);
  	my_exit(1);
      }
  
      /*
       * The screen must be no larger than NUMLINES+2 x NUMCOLS+2
       */
      if (LINES > (NUMLINES+2) || COLS > (NUMCOLS+2))
      {
  	endwin();
  	printf("\nsorry, the screen is %dx%d, must be no larger than %dx%d\n", LINES, COLS, NUMLINES+2, NUMCOLS+2);
          fflush(stdout);
  	my_exit(1);
      }
  
      /*
       * Set up windows
       */
      hw = newwin(LINES, COLS, 0, 0);
      idlok(stdscr, TRUE);
      idlok(hw, TRUE);
  #ifdef MASTER
      noscore = wizard;
  #endif
      new_level();			/* Draw current level */
      /*
       * Start up daemons and fuses
       */
      start_daemon(runners, 0, AFTER);
      start_daemon(doctor, 0, AFTER);
      fuse(swander, 0, WANDERTIME, AFTER);
      start_daemon(stomach, 0, AFTER);
      playit();
      return(0);
  }
  
  /*
   * endit:
   *	Exit the program abnormally.
   */
  
  void
  endit(int sig)
  {
      NOOP(sig);
      fatal("Okay, bye bye!\n");
  }
  
  /*
   * rnd:
   *	Pick a very random number.
   */
  int
  rnd(int range)
  {
      return range == 0 ? 0 : abs((int) RN) % range;
  }
  
  /*
   * roll:
   *	Roll a number of dice
   */
  int 
  roll(int number, int sides)
  {
      int dtotal = 0;
  
      while (number--)
  	dtotal += rnd(sides)+1;
      return dtotal;
  }
  
  /*
   * tstp:
   *	Handle stop and start signals
   */
  
  void
  tstp(int ignored)
  {
      int y, x;
      int oy, ox;
  
  	NOOP(ignored);
  
      /*
       * leave nicely
       */
      getyx(curscr, oy, ox);
      mvcur(0, COLS - 1, LINES - 1, 0);
      endwin();
      resetltchars();
      fflush(stdout);
  	md_tstpsignal();
  
      /*
       * start back up again
       */
  	md_tstpresume();
      raw();
      noecho();
      keypad(stdscr,1);
      playltchars();
      clearok(curscr, TRUE);
      wrefresh(curscr);
      getyx(curscr, y, x);
      mvcur(y, x, oy, ox);
      fflush(stdout);
      wmove(curscr, oy, ox);
  }
  
  /*
   * playit:
   *	The main loop of the program.  Loop until the game is over,
   *	refreshing things and looking at the proper times.
   */
  
  void
  playit()
  {
      char *opts;
  
      /*
       * set up defaults for slow terminals
       */
  
      if (baudrate() <= 1200)
      {
  	terse = TRUE;
  	jump = TRUE;
  	see_floor = FALSE;
      }
  
      if (md_hasclreol())
  	inv_type = INV_CLEAR;
  
      /*
       * parse environment declaration of options
       */
      if ((opts = getenv("ROGUEOPTS")) != NULL)
  	parse_opts(opts);
  
  
      oldpos = hero;
      oldrp = roomin(&hero);
      while (playing)
  	command();			/* Command execution */
      endit(0);
  }
  
  /*
   * quit:
   *	Have player make certain, then exit.
   */
  
  void
  quit(int sig)
  {
      int oy, ox;
  
      NOOP(sig);
  
      /*
       * Reset the signal in case we got here via an interrupt
       */
      if (!q_comm)
  	mpos = 0;
      getyx(curscr, oy, ox);
      msg("really quit?");
      if (readchar() == 'y')
      {
  	signal(SIGINT, leave);
  	clear();
  	mvprintw(LINES - 2, 0, "You quit with %d gold pieces", purse);
  	move(LINES - 1, 0);
  	refresh();
  	score(purse, 1, 0);
  	my_exit(0);
      }
      else
      {
  	move(0, 0);
  	clrtoeol();
  	status();
  	move(oy, ox);
  	refresh();
  	mpos = 0;
  	count = 0;
  	to_death = FALSE;
      }
  }
  
  /*
   * leave:
   *	Leave quickly, but curteously
   */
  
  void
  leave(int sig)
  {
      static char buf[BUFSIZ];
  
      NOOP(sig);
  
      setbuf(stdout, buf);	/* throw away pending output */
  
      if (!isendwin())
      {
  	mvcur(0, COLS - 1, LINES - 1, 0);
  	endwin();
      }
  
      putchar('\n');
      my_exit(0);
  }
  
  /*
   * shell:
   *	Let them escape for a while
   */
  
  void
  shell()
  {
      /*
       * Set the terminal back to original mode
       */
      move(LINES-1, 0);
      refresh();
      endwin();
      resetltchars();
      putchar('\n');
      in_shell = TRUE;
      after = FALSE;
      fflush(stdout);
      /*
       * Fork and do a shell
       */
      md_shellescape();
  
      printf("\n[Press return to continue]");
      fflush(stdout);
      noecho();
      raw();
      keypad(stdscr,1);
      playltchars();
      in_shell = FALSE;
      wait_for('\n');
      clearok(stdscr, TRUE);
  }
  
  /*
   * my_exit:
   *	Leave the process properly
   */
  
  void
  my_exit(int st)
  {
      resetltchars();
      exit(st);
  }
  
  