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