<commit_before>/*
 * global variable initializaton
 *
 * @(#)init.c	4.31 (Berkeley) 02/05/99
 *
 * Rogue: Exploring the Dungeons of Doom
 * Copyright (C) 1980-1983, 1985, 1999 <NAME>, <NAME> and <NAME>
 * All rights reserved.
 *
 * See the file LICENSE.TXT for full copyright and licensing information.
 */

#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#include "rogue.h"
#include "potions.h"
#include "status_effects.h"
#include "scrolls.h"

/* init_new_game
 * Set up everything so we can start playing already */
bool
init_new_game()
{
  /* Parse environment opts */
  if (whoami[0] == '\0')
    strucpy(whoami, md_getusername(), (int) strlen(md_getusername()));

  if (wizard)
    printf("Hello %s, welcome to dungeon #%d", whoami, seed);
  else
    printf("Hello %s, just a moment while I dig the dungeon...", whoami);
  fflush(stdout);

  /* Init Graphics */
  if (init_graphics() != 0)
    return false;
  idlok(stdscr, true);
  idlok(hw, true);

  /* Init stuff */
  init_probs();                         /* Set up prob tables for objects */
  init_player();                        /* Set up initial player stats */
  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 */

  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);

  return true;
}

/** init_old_game:
 * Restore a saved game from a file with elaborate checks for file
 * integrity from cheaters */
bool
init_old_game()
{
    FILE *inf = fopen(file_name, "r");
    char buf[MAXSTR];

    if (inf == NULL)
    {
        perror(file_name);
        return false;
    }

    /* defeat multiple restarting from the same place
     * TODO: should these be removed? not hard to circumvent anyways */
    if (!wizard)
    {
      struct stat sbuf2;
      if (lstat(file_name, &sbuf2) == -1)
      {
        perror(file_name);
        return false;
      }
      if ((sbuf2.st_mode & S_IFMT) != S_IFREG)
      {
        printf("Only normal files allowed (no symlinks, FIFOs, etc)\n");
        return false;
      }
      if (sbuf2.st_nlink != 1)
      {
        printf("The savegame cannot be hardlinked, since that's cheating\n");
        return false;
      }
    }

    if (encread_and_check_version(buf, inf) == false)
    {
      printf("Sorry, saved game is out of date.\n");
      return false;
    }

    encread(buf, 80, inf);

    if (init_graphics() != 0)
      return false;

    rs_restore_file(inf);
    /*
     * we do not close the file so that we will have a hold of the
     * inode for as long as possible
     */

    if (!wizard && unlink(file_name) < 0)
    {
        endwin();
        printf("Cannot unlink file\n");
        return false;
    }
    mpos = 0;
    clearok(stdscr,true);

    if (pstats.s_hpt <= 0)
    {
        endwin();
        printf("\n\"He's dead, Jim\"\n");
        return false;
    }

    clearok(curscr, true);
    msg("file name: %s", file_name);
    return true;
}

/*
 * init_graphics:
 * 	get curses running
 */
int
init_graphics()
{
  initscr();  /* Start up cursor package */

  /* Ncurses colors */
  if (use_colors)
  {
    if (start_color() == ERR)
    {
      endwin();
      fprintf(stderr, "Error: Failed to start colors. "
                      "Try restarting without colors enabled\n");
      return 1;
    }

    assume_default_colors(0, -1); /* Default is white text and any background */
    init_pair(COLOR_RED, COLOR_RED, -1);
    init_pair(COLOR_GREEN, COLOR_GREEN, -1);
    init_pair(COLOR_YELLOW, COLOR_YELLOW, -1);
    init_pair(COLOR_BLUE, COLOR_BLUE, -1);
    init_pair(COLOR_MAGENTA, COLOR_MAGENTA, -1);
    init_pair(COLOR_CYAN, COLOR_CYAN, -1);
    init_pair(COLOR_WHITE, COLOR_WHITE, -1);
  }

  if (LINES < NUMLINES || COLS < NUMCOLS)
  {
    endwin();
    printf("\nSorry, the screen must be at least %dx%d\n", NUMLINES, NUMCOLS);
    return 1;
  }

  raw();     /* Raw mode */
  noecho();  /* Echo off */
  hw = newwin(LINES, COLS, 0, 0);

  return 0;
}

/*
 * init_player:
 *	Roll her up
 */
void<commit_msg>added black color<commit_after>/*
 * global variable initializaton
 *
 * @(#)init.c	4.31 (Berkeley) 02/05/99
 *
 * Rogue: Exploring the Dungeons of Doom
 * Copyright (C) 1980-1983, 1985, 1999 <NAME>, <NAME> and <NAME>
 * All rights reserved.
 *
 * See the file LICENSE.TXT for full copyright and licensing information.
 */

#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#include "rogue.h"
#include "potions.h"
#include "status_effects.h"
#include "scrolls.h"

/* init_new_game
 * Set up everything so we can start playing already */
bool
init_new_game()
{
  /* Parse environment opts */
  if (whoami[0] == '\0')
    strucpy(whoami, md_getusername(), (int) strlen(md_getusername()));

  if (wizard)
    printf("Hello %s, welcome to dungeon #%d", whoami, seed);
  else
    printf("Hello %s, just a moment while I dig the dungeon...", whoami);
  fflush(stdout);

  /* Init Graphics */
  if (init_graphics() != 0)
    return false;
  idlok(stdscr, true);
  idlok(hw, true);

  /* Init stuff */
  init_probs();                         /* Set up prob tables for objects */
  init_player();                        /* Set up initial player stats */
  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 */

  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);

  return true;
}

/** init_old_game:
 * Restore a saved game from a file with elaborate checks for file
 * integrity from cheaters */
bool
init_old_game()
{
    FILE *inf = fopen(file_name, "r");
    char buf[MAXSTR];

    if (inf == NULL)
    {
        perror(file_name);
        return false;
    }

    /* defeat multiple restarting from the same place
     * TODO: should these be removed? not hard to circumvent anyways */
    if (!wizard)
    {
      struct stat sbuf2;
      if (lstat(file_name, &sbuf2) == -1)
      {
        perror(file_name);
        return false;
      }
      if ((sbuf2.st_mode & S_IFMT) != S_IFREG)
      {
        printf("Only normal files allowed (no symlinks, FIFOs, etc)\n");
        return false;
      }
      if (sbuf2.st_nlink != 1)
      {
        printf("The savegame cannot be hardlinked, since that's cheating\n");
        return false;
      }
    }

    if (encread_and_check_version(buf, inf) == false)
    {
      printf("Sorry, saved game is out of date.\n");
      return false;
    }

    encread(buf, 80, inf);

    if (init_graphics() != 0)
      return false;

    rs_restore_file(inf);
    /*
     * we do not close the file so that we will have a hold of the
     * inode for as long as possible
     */

    if (!wizard && unlink(file_name) < 0)
    {
        endwin();
        printf("Cannot unlink file\n");
        return false;
    }
    mpos = 0;
    clearok(stdscr,true);

    if (pstats.s_hpt <= 0)
    {
        endwin();
        printf("\n\"He's dead, Jim\"\n");
        return false;
    }

    clearok(curscr, true);
    msg("file name: %s", file_name);
    return true;
}

/*
 * init_graphics:
 * 	get curses running
 */
int
init_graphics()
{
  initscr();  /* Start up cursor package */

  /* Ncurses colors */
  if (use_colors)
  {
    if (start_color() == ERR)
    {
      endwin();
      fprintf(stderr, "Error: Failed to start colors. "
                      "Try restarting without colors enabled\n");
      return 1;
    }

    /* Because ncurses has defined COLOR_BLACK to 0 and COLOR_WHITE to 7,
     * and then decided that init_pair cannot change number 0 (COLOR_BLACK)
     * I use COLOR_WHITE for black text and COLOR_BLACK for white text */

    assume_default_colors(0, -1); /* Default is white text and any background */
    init_pair(COLOR_RED, COLOR_RED, -1);
    init_pair(COLOR_GREEN, COLOR_GREEN, -1);
    init_pair(COLOR_YELLOW, COLOR_YELLOW, -1);
    init_pair(COLOR_BLUE, COLOR_BLUE, -1);
    init_pair(COLOR_MAGENTA, COLOR_MAGENTA, -1);
    init_pair(COLOR_CYAN, COLOR_CYAN, -1);
    init_pair(COLOR_WHITE, COLOR_BLACK, -1);
  }

  if (LINES < NUMLINES || COLS < NUMCOLS)
  {
    endwin();
    printf("\nSorry, the screen must be at least %dx%d\n", NUMLINES, NUMCOLS);
    return 1;
  }

  raw();     /* Raw mode */
  noecho();  /* Echo off */
  hw = newwin(LINES, COLS, 0, 0);

  return 0;
}

/*
 * init_player:
 *	Roll her up
 */
void
{'fraction_non_alphanumeric': 0.13086692435118719, 'fraction_numerical': 0.013141910546659304, 'mean_word_length': 2.2954876273653566, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6130853', 'n_tokens_mistral': 3217, 'n_tokens_neox': 3050, 'n_words': 894}