/* Moria Version 4.8	COPYRIGHT (c) Robert Alan Koeneke		*/
/*                                                                       */
/*       I lovingly dedicate this game to hackers and adventurers        */
/*       everywhere...                                                   */
/*                                                                       */
/*                                                                       */
/*       Designer and Programmer : Robert Alan Koeneke                   */
/*                                 University of Oklahoma                */
/*                                                                       */
/*       Assistant Programmers   : Jimmey Wayne Todd                     */
/*                                 University of Oklahoma                */
/*                                                                       */
/*                                 Gary D. McAdoo                        */
/*                                 University of Oklahoma                */
/*                                                                       */
/*       UNIX Port               : James E. Wilson                       */
/*                                 UC Berkeley                           */
/*                                 wilson@ernie.Berkeley.EDU             */
/*                                 ucbvax!ucbernie!wilson                */
/*                                                                       */
/*       MSDOS Port              : Don Kneller                           */
/*                                 1349 - 10th ave                       */
/*                                 San Francisco, CA 94122               */
/*                                 kneller@cgl.ucsf.EDU                  */
/*                                 ...ucbvax!ucsfcgl!kneller             */
/*                                 kneller@ucsf-cgl.BITNET               */
/*                                                                       */
/*       Moria may be copied and modified freely as long as the above    */
/*       credits are retained.  No one who-so-ever may sell or market    */
/*       this software in any form without the expressed written consent */
/*       of the author Robert Alan Koeneke.                              */
/*                                                                       */

#include <curses.h>
#if defined(MSDOS) && defined(ANSI)
#include "ms_ansi.h"
#endif
#include <sys/types.h>

#include "constant.h"
#include "config.h"
#include "types.h"
#include "externs.h"

#ifdef USG
#include <string.h>
#else
#include <strings.h>
#endif

#if defined(ultrix) || defined(sun) || defined(USG)
int getuid();
int getgid();
#else
uid_t getuid();
uid_t getgid();
#endif

#if defined(ultrix) || defined(USG)
void perror();
void exit();
#endif

extern int key_bindings;

/* Initialize, restore, and get the ball rolling...	-RAK-	*/
main(argc, argv)
int argc;
char *argv[];
{
  int	new_game = FALSE;

  key_bindings = KEY_BINDINGS;
#ifdef MSDOS
  msdos_init();		/* find out where everything is */
#endif

  /* call this routine to grab a file pointer to the highscore file *
  /* and prepare things to relinquish setuid privileges */
  init_scorefile();

#ifndef MSDOS
  if (0 != setuid(getuid()))
    {
      perror("Gack!  Can't set permissions correctly!  Exiting!\n");
      exit(0);
    }
  if (0 != setgid(getgid()))
    {
      perror("Gack!  Can't set group id correctly!  Exiting!\n");
      exit(0);
    }
#endif

  /* use curses */
  init_curses();

  /* catch those nasty signals */
  /* must come after init_curses as some of the signal handlers use curses */
  init_signals();

  /* Build the secret wizard and god passwords	*/
  bpswd();

  /* check for user interface option */
  for (--argc, ++argv; argc > 0 && argv[0][0] == '-'; --argc, ++argv)
	switch (argv[0][1]) {
	    case 'n': new_game = TRUE; break;
	    case 'o': key_bindings = ORIGINAL; break;
	    case 'r': key_bindings = ROGUE_LIKE; break;
	    case 's': display_scores(); exit_game();
	    default: /* usage */;
	}

  /* Check operating hours 			*/
  /* If not wizard  No_Control_Y               */
  /* Check or create hours.dat, print message	*/
  /* if last arg is ^ then start as wizard, can not restore game also */
  intro(argv[0]);

  /* Some necessary initializations		*/
  /* all made into constants or initialized in variables.c */

  /* Grab a random seed from the clock		*/
  init_seeds();

  /* Sort the objects by level			*/
  sort_objects();

  /* Init monster and treasure levels for allocate */
  init_m_level();
  init_t_level();

  /* Init the store inventories			*/
  store_init();
  if (COST_ADJ != 1.00)  price_adjust();

  /* Auto-restart of saved file */
  if (argv[0] == NULL && access(MORIA_SAV, 0) == 0)
	argv[0] = MORIA_SAV;

  /* Generate a character, or retrieve old one...	*/
  if (new_game == FALSE && argv[0] != NULL && argv[0][0])
    {     /* Retrieve character    */
      generate = get_char(argv[0]);
      change_name();
      magic_init();
    }
  else
    {     /* Create character      */
      create_character();
      char_inven_init();
      if (class[py.misc.pclass].mspell)
	{         /* Magic realm   */
	  (void) learn_spell(&msg_flag);
	  gain_mana(int_adj());
	}
      else if (class[py.misc.pclass].pspell)
	{         /* Clerical realm*/
	  (void) learn_prayer();
	  gain_mana(wis_adj());
	}
      py.misc.cmana = (double)py.misc.mana;
      magic_init();
      generate = TRUE;
    }

  /* Begin the game				*/
  /*    This determines the maximum player level    */
  /* must be one less than real value so that prt_experience will work
     correctly, otherwise it is possible to reach level 41 */
  player_max_exp = player_exp[MAX_PLAYER_LEVEL-1] * py.misc.expfact - 1;
  clear_screen(0, 0);
  prt_stat_block();
  /* prevent ^c quit from entering score into scoreboard until this point */
  character_generated = 1;

  /* Loop till dead, or exit			*/
  while(!death) {
    if (generate)  generate_cave();         /* New level     */
    dungeon();                                  /* Dungeon logic */
    generate = TRUE;
  }
  upon_death();                         /* Character gets buried */
  /* should never reach here, but just in case */
  return (0);
}
