home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / games / rogue / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-08  |  7.2 KB  |  339 lines

  1. /*
  2.  * Copyright (c) 1988 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Timothy C. Stoehr.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)init.c    5.4 (Berkeley) 2/28/91";
  39. #endif /* not lint */
  40.  
  41. /*
  42.  * init.c
  43.  *
  44.  * This source herein may be modified and/or distributed by anybody who
  45.  * so desires, with the following restrictions:
  46.  *    1.)  No portion of this notice shall be removed.
  47.  *    2.)  Credit shall not be taken for the creation of this source.
  48.  *    3.)  This code is not to be traded, sold, or used for personal
  49.  *         gain or profit.
  50.  *
  51.  */
  52.  
  53. #include <stdio.h>
  54. #include "rogue.h"
  55.  
  56. char login_name[MAX_OPT_LEN];
  57. char *nick_name = (char *) 0;
  58. char *rest_file = 0;
  59. boolean cant_int = 0;
  60. boolean did_int = 0;
  61. boolean score_only;
  62. boolean init_curses = 0;
  63. boolean save_is_interactive = 1;
  64. boolean ask_quit = 1;
  65. boolean no_skull = 0;
  66. boolean passgo = 0;
  67. char *error_file = "rogue.esave";
  68. char *byebye_string = "Okay, bye bye!";
  69.  
  70. extern char *fruit;
  71. extern char *save_file;
  72. extern short party_room;
  73. extern boolean jump;
  74.  
  75. init(argc, argv)
  76. int argc;
  77. char *argv[];
  78. {
  79.     char *pn;
  80.     int seed;
  81.  
  82.     pn = md_gln();
  83.     if ((!pn) || (strlen(pn) >= MAX_OPT_LEN)) {
  84.         clean_up("Hey!  Who are you?");
  85.     }
  86.     (void) strcpy(login_name, pn);
  87.  
  88.     do_args(argc, argv);
  89.     do_opts();
  90.  
  91.     if (!score_only && !rest_file) {
  92.         printf("Hello %s, just a moment while I dig the dungeon...",
  93.             nick_name);
  94.         fflush(stdout);
  95.     }
  96.  
  97.     initscr();
  98.     if ((LINES < DROWS) || (COLS < DCOLS)) {
  99.         clean_up("must be played on 24 x 80 screen");
  100.     }
  101.     start_window();
  102.     init_curses = 1;
  103.  
  104.     md_heed_signals();
  105.  
  106.     if (score_only) {
  107.         put_scores((object *) 0, 0);
  108.     }
  109.     seed = md_gseed();
  110.     (void) srrandom(seed);
  111.     if (rest_file) {
  112.         restore(rest_file);
  113.         return(1);
  114.     }
  115.     mix_colors();
  116.     get_wand_and_ring_materials();
  117.     make_scroll_titles();
  118.  
  119.     level_objects.next_object = (object *) 0;
  120.     level_monsters.next_monster = (object *) 0;
  121.     player_init();
  122.     ring_stats(0);
  123.     return(0);
  124. }
  125.  
  126. player_init()
  127. {
  128.     object *obj;
  129.  
  130.     rogue.pack.next_object = (object *) 0;
  131.  
  132.     obj = alloc_object();
  133.     get_food(obj, 1);
  134.     (void) add_to_pack(obj, &rogue.pack, 1);
  135.  
  136.     obj = alloc_object();        /* initial armor */
  137.     obj->what_is = ARMOR;
  138.     obj->which_kind = RINGMAIL;
  139.     obj->class = RINGMAIL+2;
  140.     obj->is_protected = 0;
  141.     obj->d_enchant = 1;
  142.     (void) add_to_pack(obj, &rogue.pack, 1);
  143.     do_wear(obj);
  144.  
  145.     obj = alloc_object();        /* initial weapons */
  146.     obj->what_is = WEAPON;
  147.     obj->which_kind = MACE;
  148.     obj->damage = "2d3";
  149.     obj->hit_enchant = obj->d_enchant = 1;
  150.     obj->identified = 1;
  151.     (void) add_to_pack(obj, &rogue.pack, 1);
  152.     do_wield(obj);
  153.  
  154.     obj = alloc_object();
  155.     obj->what_is = WEAPON;
  156.     obj->which_kind = BOW;
  157.     obj->damage = "1d2";
  158.     obj->hit_enchant = 1;
  159.     obj->d_enchant = 0;
  160.     obj->identified = 1;
  161.     (void) add_to_pack(obj, &rogue.pack, 1);
  162.  
  163.     obj = alloc_object();
  164.     obj->what_is = WEAPON;
  165.     obj->which_kind = ARROW;
  166.     obj->quantity = get_rand(25, 35);
  167.     obj->damage = "1d2";
  168.     obj->hit_enchant = 0;
  169.     obj->d_enchant = 0;
  170.     obj->identified = 1;
  171.     (void) add_to_pack(obj, &rogue.pack, 1);
  172. }
  173.  
  174. clean_up(estr)
  175. char *estr;
  176. {
  177.     if (save_is_interactive) {
  178.         if (init_curses) {
  179.             move(DROWS-1, 0);
  180.             refresh();
  181.             stop_window();
  182.         }
  183.         printf("\n%s\n", estr);
  184.     }
  185.     md_exit(0);
  186. }
  187.  
  188. start_window()
  189. {
  190.     crmode();
  191.     noecho();
  192. #ifndef BAD_NONL
  193.     nonl();
  194. #endif
  195.     md_control_keybord(0);
  196. }
  197.  
  198. stop_window()
  199. {
  200.     endwin();
  201.     md_control_keybord(1);
  202. }
  203.  
  204. void
  205. byebye()
  206. {
  207.     md_ignore_signals();
  208.     if (ask_quit) {
  209.         quit(1);
  210.     } else {
  211.         clean_up(byebye_string);
  212.     }
  213.     md_heed_signals();
  214. }
  215.  
  216. void
  217. onintr()
  218. {
  219.     md_ignore_signals();
  220.     if (cant_int) {
  221.         did_int = 1;
  222.     } else {
  223.         check_message();
  224.         message("interrupt", 1);
  225.     }
  226.     md_heed_signals();
  227. }
  228.  
  229. void
  230. error_save()
  231. {
  232.     save_is_interactive = 0;
  233.     save_into_file(error_file);
  234.     clean_up("");
  235. }
  236.  
  237. do_args(argc, argv)
  238. int argc;
  239. char *argv[];
  240. {
  241.     short i, j;
  242.  
  243.     for (i = 1; i < argc; i++) {
  244.         if (argv[i][0] == '-') {
  245.             for (j = 1; argv[i][j]; j++) {
  246.                 switch(argv[i][j]) {
  247.                 case 's':
  248.                     score_only = 1;
  249.                     break;
  250.                 }
  251.             }
  252.         } else {
  253.             rest_file = argv[i];
  254.         }
  255.     }
  256. }
  257.  
  258. do_opts()
  259. {
  260.     char *eptr;
  261.  
  262.     if (eptr = md_getenv("ROGUEOPTS")) {
  263.         for (;;) {
  264.             while ((*eptr) == ' ') {
  265.                 eptr++;
  266.             }
  267.             if (!(*eptr)) {
  268.                 break;
  269.             }
  270.             if (!strncmp(eptr, "fruit=", 6)) {
  271.                 eptr += 6;
  272.                 env_get_value(&fruit, eptr, 1);
  273.             } else if (!strncmp(eptr, "file=", 5)) {
  274.                 eptr += 5;
  275.                 env_get_value(&save_file, eptr, 0);
  276.             } else if (!strncmp(eptr, "jump", 4)) {
  277.                 jump = 1;
  278.             } else if (!strncmp(eptr, "name=", 5)) {
  279.                 eptr += 5;
  280.                 env_get_value(&nick_name, eptr, 0);
  281.             } else if (!strncmp(eptr, "noaskquit", 9)) {
  282.                 ask_quit = 0;
  283.             } else if (!strncmp(eptr, "noskull", 5) ||
  284.                     !strncmp(eptr,"notomb", 6)) {
  285.                 no_skull = 1;
  286.             } else if (!strncmp(eptr, "passgo", 5)) {
  287.                 passgo = 1;
  288.             }
  289.             while ((*eptr) && (*eptr != ',')) {
  290.                 eptr++;
  291.             }
  292.             if (!(*(eptr++))) {
  293.                 break;
  294.             }
  295.         }
  296.     }
  297.     /* If some strings have not been set through ROGUEOPTS, assign defaults
  298.      * to them so that the options editor has data to work with.
  299.      */
  300.     init_str(&nick_name, login_name);
  301.     init_str(&save_file, "rogue.save");
  302.     init_str(&fruit, "slime-mold");
  303. }
  304.  
  305. env_get_value(s, e, add_blank)
  306. char **s, *e;
  307. boolean add_blank;
  308. {
  309.     short i = 0;
  310.     char *t;
  311.  
  312.     t = e;
  313.  
  314.     while ((*e) && (*e != ',')) {
  315.         if (*e == ':') {
  316.             *e = ';';        /* ':' reserved for score file purposes */
  317.         }
  318.         e++;
  319.         if (++i >= MAX_OPT_LEN) {
  320.             break;
  321.         }
  322.     }
  323.     *s = md_malloc(MAX_OPT_LEN + 2);
  324.     (void) strncpy(*s, t, i);
  325.     if (add_blank) {
  326.         (*s)[i++] = ' ';
  327.     }
  328.     (*s)[i] = '\0';
  329. }
  330.  
  331. init_str(str, dflt)
  332. char **str, *dflt;
  333. {
  334.     if (!(*str)) {
  335.         *str = md_malloc(MAX_OPT_LEN + 2);
  336.         (void) strcpy(*str, dflt);
  337.     }
  338. }
  339.