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