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