Home | History | Annotate | Line # | Download | only in monop
execute.c revision 1.3
      1 /*	$NetBSD: execute.c,v 1.3 1995/03/23 08:34:38 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)execute.c	8.1 (Berkeley) 5/31/93";
     39 #else
     40 static char rcsid[] = "$NetBSD: execute.c,v 1.3 1995/03/23 08:34:38 cgd Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 # include	"monop.ext"
     45 # include	<sys/types.h>
     46 # include	<sys/stat.h>
     47 # include	<sys/time.h>
     48 
     49 # define	SEGSIZE	8192
     50 
     51 typedef	struct stat	STAT;
     52 typedef	struct tm	TIME;
     53 
     54 extern char	etext[],	/* end of text space			*/
     55 		rub();
     56 
     57 static char	buf[257],
     58 		*yn_only[]	= { "yes", "no"};
     59 
     60 static bool	new_play;	/* set if move on to new player		*/
     61 
     62 /*
     63  *	This routine executes the given command by index number
     64  */
     65 execute(com_num)
     66 reg int	com_num; {
     67 
     68 	new_play = FALSE;	/* new_play is true if fixing	*/
     69 	(*func[com_num])();
     70 	notify();
     71 	force_morg();
     72 	if (new_play)
     73 		next_play();
     74 	else if (num_doub)
     75 		printf("%s rolled doubles.  Goes again\n", cur_p->name);
     76 }
     77 /*
     78  *	This routine moves a piece around.
     79  */
     80 do_move() {
     81 
     82 	reg int		r1, r2;
     83 	reg bool	was_jail;
     84 
     85 	new_play = was_jail = FALSE;
     86 	printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
     87 	if (cur_p->loc == JAIL) {
     88 		was_jail++;
     89 		if (!move_jail(r1, r2)) {
     90 			new_play++;
     91 			goto ret;
     92 		}
     93 	}
     94 	else {
     95 		if (r1 == r2 && ++num_doub == 3) {
     96 			printf("That's 3 doubles.  You go to jail\n");
     97 			goto_jail();
     98 			new_play++;
     99 			goto ret;
    100 		}
    101 		move(r1+r2);
    102 	}
    103 	if (r1 != r2 || was_jail)
    104 		new_play++;
    105 ret:
    106 	return;
    107 }
    108 /*
    109  *	This routine moves a normal move
    110  */
    111 move(rl)
    112 reg int	rl; {
    113 
    114 	reg int	old_loc;
    115 
    116 	old_loc = cur_p->loc;
    117 	cur_p->loc = (cur_p->loc + rl) % N_SQRS;
    118 	if (cur_p->loc < old_loc && rl > 0) {
    119 		cur_p->money += 200;
    120 		printf("You pass %s and get $200\n", board[0].name);
    121 	}
    122 	show_move();
    123 }
    124 /*
    125  *	This routine shows the results of a move
    126  */
    127 show_move() {
    128 
    129 	reg SQUARE	*sqp;
    130 
    131 	sqp = &board[cur_p->loc];
    132 	printf("That puts you on %s\n", sqp->name);
    133 	switch (sqp->type) {
    134 	  case SAFE:
    135 		printf("That is a safe place\n");
    136 		break;
    137 	  case CC:
    138 		cc(); break;
    139 	  case CHANCE:
    140 		chance(); break;
    141 	  case INC_TAX:
    142 		inc_tax(); break;
    143 	  case GOTO_J:
    144 		goto_jail(); break;
    145 	  case LUX_TAX:
    146 		lux_tax(); break;
    147 	  case PRPTY:
    148 	  case RR:
    149 	  case UTIL:
    150 		if (sqp->owner < 0) {
    151 			printf("That would cost $%d\n", sqp->cost);
    152 			if (getyn("Do you want to buy? ") == 0) {
    153 				buy(player, sqp);
    154 				cur_p->money -= sqp->cost;
    155 			}
    156 			else if (num_play > 2)
    157 				bid(sqp);
    158 		}
    159 		else if (sqp->owner == player)
    160 			printf("You own it.\n");
    161 		else
    162 			rent(sqp);
    163 	}
    164 }
    165 /*
    166  *	This routine saves the current game for use at a later date
    167  */
    168 save() {
    169 
    170 	reg char	*sp;
    171 	reg int		outf, num;
    172 	time_t		t;
    173 	int		*dat_end;
    174 	struct stat	sb;
    175 	unsgn		start, end;
    176 
    177 	printf("Which file do you wish to save it in? ");
    178 	sp = buf;
    179 	while ((*sp++=getchar()) != '\n')
    180 		continue;
    181 	*--sp = '\0';
    182 
    183 	/*
    184 	 * check for existing files, and confirm overwrite if needed
    185 	 */
    186 
    187 	if (stat(buf, &sb) > -1
    188 	    && getyn("File exists.  Do you wish to overwrite? ", yn_only) > 0)
    189 		return;
    190 
    191 	if ((outf=creat(buf, 0644)) < 0) {
    192 		perror(buf);
    193 		return;
    194 	}
    195 	printf("\"%s\" ", buf);
    196 	time(&t);			/* get current time		*/
    197 	strcpy(buf, ctime(&t));
    198 	for (sp = buf; *sp != '\n'; sp++)
    199 		continue;
    200 	*sp = '\0';
    201 # if 0
    202 	start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
    203 # else
    204 	start = 0;
    205 # endif
    206 	end = sbrk(0);
    207 	while (start < end) {		/* write out entire data space */
    208 		num = start + 16 * 1024 > end ? end - start : 16 * 1024;
    209 		write(outf, start, num);
    210 		start += num;
    211 	}
    212 	close(outf);
    213 	printf("[%s]\n", buf);
    214 }
    215 /*
    216  *	This routine restores an old game from a file
    217  */
    218 restore() {
    219 
    220 	reg char	*sp;
    221 
    222 	printf("Which file do you wish to restore from? ");
    223 	for (sp = buf; (*sp=getchar()) != '\n'; sp++)
    224 		continue;
    225 	*sp = '\0';
    226 	rest_f(buf);
    227 }
    228 /*
    229  *	This does the actual restoring.  It returns TRUE if the
    230  * backup was successful, else false.
    231  */
    232 rest_f(file)
    233 reg char	*file; {
    234 
    235 	reg char	*sp;
    236 	reg int		inf, num;
    237 	char		buf[80];
    238 	unsgn		start, end;
    239 	STAT		sbuf;
    240 
    241 	if ((inf=open(file, 0)) < 0) {
    242 		perror(file);
    243 		return FALSE;
    244 	}
    245 	printf("\"%s\" ", file);
    246 	if (fstat(inf, &sbuf) < 0) {		/* get file stats	*/
    247 		perror(file);
    248 		exit(1);
    249 	}
    250 # if 0
    251 	start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
    252 # else
    253 	start = 0;
    254 # endif
    255 	brk(end = start + sbuf.st_size);
    256 	while (start < end) {		/* write out entire data space */
    257 		num = start + 16 * 1024 > end ? end - start : 16 * 1024;
    258 		read(inf, start, num);
    259 		start += num;
    260 	}
    261 	close(inf);
    262 	strcpy(buf, ctime(&sbuf.st_mtime));
    263 	for (sp = buf; *sp != '\n'; sp++)
    264 		continue;
    265 	*sp = '\0';
    266 	printf("[%s]\n", buf);
    267 	return TRUE;
    268 }
    269