Home | History | Annotate | Line # | Download | only in monop
execute.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 static char sccsid[] = "@(#)execute.c	5.5 (Berkeley) 2/28/91";
     36 #endif /* not lint */
     37 
     38 # include	"monop.ext"
     39 # include	<sys/types.h>
     40 # include	<sys/stat.h>
     41 # include	<sys/time.h>
     42 
     43 # define	SEGSIZE	8192
     44 
     45 typedef	struct stat	STAT;
     46 typedef	struct tm	TIME;
     47 
     48 extern char	etext[],	/* end of text space			*/
     49 		rub();
     50 
     51 static char	buf[257],
     52 		*yn_only[]	= { "yes", "no"};
     53 
     54 static bool	new_play;	/* set if move on to new player		*/
     55 
     56 /*
     57  *	This routine executes the given command by index number
     58  */
     59 execute(com_num)
     60 reg int	com_num; {
     61 
     62 	new_play = FALSE;	/* new_play is true if fixing	*/
     63 	(*func[com_num])();
     64 	notify();
     65 	force_morg();
     66 	if (new_play)
     67 		next_play();
     68 	else if (num_doub)
     69 		printf("%s rolled doubles.  Goes again\n", cur_p->name);
     70 }
     71 /*
     72  *	This routine moves a piece around.
     73  */
     74 do_move() {
     75 
     76 	reg int		r1, r2;
     77 	reg bool	was_jail;
     78 
     79 	new_play = was_jail = FALSE;
     80 	printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
     81 	if (cur_p->loc == JAIL) {
     82 		was_jail++;
     83 		if (!move_jail(r1, r2)) {
     84 			new_play++;
     85 			goto ret;
     86 		}
     87 	}
     88 	else {
     89 		if (r1 == r2 && ++num_doub == 3) {
     90 			printf("That's 3 doubles.  You go to jail\n");
     91 			goto_jail();
     92 			new_play++;
     93 			goto ret;
     94 		}
     95 		move(r1+r2);
     96 	}
     97 	if (r1 != r2 || was_jail)
     98 		new_play++;
     99 ret:
    100 	return;
    101 }
    102 /*
    103  *	This routine moves a normal move
    104  */
    105 move(rl)
    106 reg int	rl; {
    107 
    108 	reg int	old_loc;
    109 
    110 	old_loc = cur_p->loc;
    111 	cur_p->loc = (cur_p->loc + rl) % N_SQRS;
    112 	if (cur_p->loc < old_loc && rl > 0) {
    113 		cur_p->money += 200;
    114 		printf("You pass %s and get $200\n", board[0].name);
    115 	}
    116 	show_move();
    117 }
    118 /*
    119  *	This routine shows the results of a move
    120  */
    121 show_move() {
    122 
    123 	reg SQUARE	*sqp;
    124 
    125 	sqp = &board[cur_p->loc];
    126 	printf("That puts you on %s\n", sqp->name);
    127 	switch (sqp->type) {
    128 	  case SAFE:
    129 		printf("That is a safe place\n");
    130 		break;
    131 	  case CC:
    132 		cc(); break;
    133 	  case CHANCE:
    134 		chance(); break;
    135 	  case INC_TAX:
    136 		inc_tax(); break;
    137 	  case GOTO_J:
    138 		goto_jail(); break;
    139 	  case LUX_TAX:
    140 		lux_tax(); break;
    141 	  case PRPTY:
    142 	  case RR:
    143 	  case UTIL:
    144 		if (sqp->owner < 0) {
    145 			printf("That would cost $%d\n", sqp->cost);
    146 			if (getyn("Do you want to buy? ") == 0) {
    147 				buy(player, sqp);
    148 				cur_p->money -= sqp->cost;
    149 			}
    150 			else if (num_play > 2)
    151 				bid(sqp);
    152 		}
    153 		else if (sqp->owner == player)
    154 			printf("You own it.\n");
    155 		else
    156 			rent(sqp);
    157 	}
    158 }
    159 /*
    160  *	This routine saves the current game for use at a later date
    161  */
    162 save() {
    163 
    164 	reg char	*sp;
    165 	reg int		outf, num;
    166 	time_t		t;
    167 	int		*dat_end;
    168 	struct stat	sb;
    169 	unsgn		start, end;
    170 
    171 	printf("Which file do you wish to save it in? ");
    172 	sp = buf;
    173 	while ((*sp++=getchar()) != '\n')
    174 		continue;
    175 	*--sp = '\0';
    176 
    177 	/*
    178 	 * check for existing files, and confirm overwrite if needed
    179 	 */
    180 
    181 	if (stat(buf, &sb) > -1
    182 	    && getyn("File exists.  Do you wish to overwrite? ", yn_only) > 0)
    183 		return;
    184 
    185 	if ((outf=creat(buf, 0644)) < 0) {
    186 		perror(buf);
    187 		return;
    188 	}
    189 	printf("\"%s\" ", buf);
    190 	time(&t);			/* get current time		*/
    191 	strcpy(buf, ctime(&t));
    192 	for (sp = buf; *sp != '\n'; sp++)
    193 		continue;
    194 	*sp = '\0';
    195 # if 0
    196 	start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
    197 # else
    198 	start = 0;
    199 # endif
    200 	end = sbrk(0);
    201 	while (start < end) {		/* write out entire data space */
    202 		num = start + 16 * 1024 > end ? end - start : 16 * 1024;
    203 		write(outf, start, num);
    204 		start += num;
    205 	}
    206 	close(outf);
    207 	printf("[%s]\n", buf);
    208 }
    209 /*
    210  *	This routine restores an old game from a file
    211  */
    212 restore() {
    213 
    214 	reg char	*sp;
    215 
    216 	printf("Which file do you wish to restore from? ");
    217 	for (sp = buf; (*sp=getchar()) != '\n'; sp++)
    218 		continue;
    219 	*sp = '\0';
    220 	rest_f(buf);
    221 }
    222 /*
    223  *	This does the actual restoring.  It returns TRUE if the
    224  * backup was successful, else false.
    225  */
    226 rest_f(file)
    227 reg char	*file; {
    228 
    229 	reg char	*sp;
    230 	reg int		inf, num;
    231 	char		buf[80];
    232 	unsgn		start, end;
    233 	STAT		sbuf;
    234 
    235 	if ((inf=open(file, 0)) < 0) {
    236 		perror(file);
    237 		return FALSE;
    238 	}
    239 	printf("\"%s\" ", file);
    240 	if (fstat(inf, &sbuf) < 0) {		/* get file stats	*/
    241 		perror(file);
    242 		exit(1);
    243 	}
    244 # if 0
    245 	start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
    246 # else
    247 	start = 0;
    248 # endif
    249 	brk(end = start + sbuf.st_size);
    250 	while (start < end) {		/* write out entire data space */
    251 		num = start + 16 * 1024 > end ? end - start : 16 * 1024;
    252 		read(inf, start, num);
    253 		start += num;
    254 	}
    255 	close(inf);
    256 	strcpy(buf, ctime(&sbuf.st_mtime));
    257 	for (sp = buf; *sp != '\n'; sp++)
    258 		continue;
    259 	*sp = '\0';
    260 	printf("[%s]\n", buf);
    261 	return TRUE;
    262 }
    263