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