Home | History | Annotate | Line # | Download | only in robots
main.c revision 1.1
      1 /*
      2  * Copyright (c) 1980 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 static char sccsid[] = "@(#)main.c	5.5 (Berkeley) 2/28/91";
     42 #endif /* not lint */
     43 
     44 # include	"robots.h"
     45 # include	<signal.h>
     46 # include	<ctype.h>
     47 
     48 main(ac, av)
     49 int	ac;
     50 char	**av;
     51 {
     52 	register char	*sp;
     53 	register bool	bad_arg;
     54 	register bool	show_only;
     55 	extern char	*Scorefile;
     56 	extern int	Max_per_uid;
     57 	void quit();
     58 
     59 	show_only = FALSE;
     60 	if (ac > 1) {
     61 		bad_arg = FALSE;
     62 		for (++av; ac > 1 && *av[0]; av++, ac--)
     63 			if (av[0][0] != '-')
     64 				if (isdigit(av[0][0]))
     65 					Max_per_uid = atoi(av[0]);
     66 				else {
     67 					setuid(getuid());
     68 					setgid(getgid());
     69 					Scorefile = av[0];
     70 # ifdef	FANCY
     71 					sp = rindex(Scorefile, '/');
     72 					if (sp == NULL)
     73 						sp = Scorefile;
     74 					if (strcmp(sp, "pattern_roll") == 0)
     75 						Pattern_roll = TRUE;
     76 					else if (strcmp(sp, "stand_still") == 0)
     77 						Stand_still = TRUE;
     78 					if (Pattern_roll || Stand_still)
     79 						Teleport = TRUE;
     80 # endif
     81 				}
     82 			else
     83 				for (sp = &av[0][1]; *sp; sp++)
     84 					switch (*sp) {
     85 					  case 's':
     86 						show_only = TRUE;
     87 						break;
     88 					  case 'r':
     89 						Real_time = TRUE;
     90 						break;
     91 					  case 'a':
     92 						Start_level = 4;
     93 						break;
     94 					  case 'j':
     95 						Jump = TRUE;
     96 						break;
     97 					  case 't':
     98 						Teleport = TRUE;
     99 						break;
    100 					  default:
    101 						fprintf(stderr, "robots: uknown option: %c\n", *sp);
    102 						bad_arg = TRUE;
    103 						break;
    104 					}
    105 		if (bad_arg) {
    106 			exit(1);
    107 			/* NOTREACHED */
    108 		}
    109 	}
    110 
    111 	if (show_only) {
    112 		show_score();
    113 		exit(0);
    114 		/* NOTREACHED */
    115 	}
    116 
    117 	initscr();
    118 	signal(SIGINT, quit);
    119 	crmode();
    120 	noecho();
    121 	nonl();
    122 	if (LINES != Y_SIZE || COLS != X_SIZE) {
    123 		if (LINES < Y_SIZE || COLS < X_SIZE) {
    124 			endwin();
    125 			printf("Need at least a %dx%d screen\n",
    126 			    Y_SIZE, X_SIZE);
    127 			exit(1);
    128 		}
    129 		delwin(stdscr);
    130 		stdscr = newwin(Y_SIZE, X_SIZE, 0, 0);
    131 	}
    132 
    133 	srand(getpid());
    134 	if (Real_time)
    135 		signal(SIGALRM, move_robots);
    136 	do {
    137 		init_field();
    138 		for (Level = Start_level; !Dead; Level++) {
    139 			make_level();
    140 			play_level();
    141 		}
    142 		move(My_pos.y, My_pos.x);
    143 		printw("AARRrrgghhhh....");
    144 		refresh();
    145 		score();
    146 	} while (another());
    147 	quit();
    148 }
    149 
    150 /*
    151  * quit:
    152  *	Leave the program elegantly.
    153  */
    154 void
    155 quit()
    156 {
    157 	extern int	_putchar();
    158 
    159 	mvcur(0, COLS - 1, LINES - 1, 0);
    160 	if (CE) {
    161 		tputs(CE, 1, _putchar);
    162 		endwin();
    163 	}
    164 	else {
    165 		endwin();
    166 		putchar('\n');
    167 	}
    168 	exit(0);
    169 	/* NOTREACHED */
    170 }
    171 
    172 /*
    173  * another:
    174  *	See if another game is desired
    175  */
    176 another()
    177 {
    178 	register int	y;
    179 
    180 #ifdef	FANCY
    181 	if ((Stand_still || Pattern_roll) && !Newscore)
    182 		return TRUE;
    183 #endif
    184 
    185 	if (query("Another game?")) {
    186 		if (Full_clear) {
    187 			for (y = 1; y <= Num_scores; y++) {
    188 				move(y, 1);
    189 				clrtoeol();
    190 			}
    191 			refresh();
    192 		}
    193 		return TRUE;
    194 	}
    195 	return FALSE;
    196 }
    197