Home | History | Annotate | Line # | Download | only in monop
cards.c revision 1.21
      1  1.21  dholland /*	$NetBSD: cards.c,v 1.21 2008/02/24 01:57:34 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.13       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[] = "@(#)cards.c	8.1 (Berkeley) 5/31/93";
     36   1.3       cgd #else
     37  1.21  dholland __RCSID("$NetBSD: cards.c,v 1.21 2008/02/24 01:57:34 dholland Exp $");
     38   1.3       cgd #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.7    simonb #include <sys/types.h>
     42   1.7    simonb #include <sys/endian.h>
     43  1.21  dholland #include "monop.h"
     44  1.21  dholland #include "deck.h"
     45   1.7    simonb #include "pathnames.h"
     46   1.1       cgd 
     47   1.1       cgd /*
     48   1.1       cgd  *	These routine deal with the card decks
     49   1.1       cgd  */
     50   1.1       cgd 
     51   1.7    simonb #define	GOJF	'F'	/* char for get-out-of-jail-free cards	*/
     52   1.1       cgd 
     53   1.7    simonb #ifndef DEV
     54   1.8       jsm static const char	*cardfile	= _PATH_CARDS;
     55   1.7    simonb #else
     56   1.8       jsm static const char	*cardfile	= "cards.pck";
     57   1.7    simonb #endif
     58   1.1       cgd 
     59   1.1       cgd static FILE	*deckf;
     60   1.1       cgd 
     61  1.14       jsm static void set_up(DECK *);
     62  1.14       jsm static void printmes(void);
     63   1.4  christos 
     64   1.1       cgd /*
     65   1.1       cgd  *	This routine initializes the decks from the data file,
     66   1.1       cgd  * which it opens.
     67   1.1       cgd  */
     68   1.4  christos void
     69   1.4  christos init_decks()
     70   1.4  christos {
     71   1.6    simonb 	int32_t nc;
     72   1.1       cgd 
     73  1.17  dholland 	if ((deckf = fopen(cardfile, "r")) == NULL) {
     74   1.1       cgd file_err:
     75  1.16  dholland 		err(1, "%s", cardfile);
     76   1.1       cgd 	}
     77   1.6    simonb 
     78   1.6    simonb 	/* read number of community chest cards... */
     79   1.6    simonb 	if (fread(&nc, sizeof(nc), 1, deckf) != 1)
     80   1.6    simonb 		goto file_err;
     81   1.6    simonb 	CC_D.num_cards = be32toh(nc);
     82   1.6    simonb 	/* ... and number of community chest cards. */
     83   1.6    simonb 	if (fread(&nc, sizeof(nc), 1, deckf) != 1)
     84   1.1       cgd 		goto file_err;
     85   1.6    simonb 	CH_D.num_cards = be32toh(nc);
     86   1.1       cgd 	set_up(&CC_D);
     87   1.1       cgd 	set_up(&CH_D);
     88   1.1       cgd }
     89   1.7    simonb 
     90   1.1       cgd /*
     91   1.1       cgd  *	This routine sets up the offset pointers for the given deck.
     92   1.1       cgd  */
     93   1.4  christos static void
     94   1.1       cgd set_up(dp)
     95   1.7    simonb 	DECK *dp;
     96   1.7    simonb {
     97   1.7    simonb 	int r1, r2;
     98   1.7    simonb 	int i;
     99   1.1       cgd 
    100  1.12    itojun 	dp->offsets = (off_t *) calloc(dp->num_cards, sizeof (off_t));
    101  1.10       jsm 	if (dp->offsets == NULL)
    102  1.10       jsm 		errx(1, "out of memory");
    103   1.6    simonb 	if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
    104  1.15  dholland 	    (unsigned) dp->num_cards) {
    105  1.16  dholland 		err(1, "%s", cardfile);
    106   1.1       cgd 	}
    107   1.6    simonb 	/* convert offsets from big-endian byte order */
    108   1.6    simonb 	for (i = 0; i < dp->num_cards; i++)
    109   1.6    simonb 		BE64TOH(dp->offsets[i]);
    110  1.19  dholland 	dp->top_card = 0;
    111   1.1       cgd 	dp->gojf_used = FALSE;
    112   1.1       cgd 	for (i = 0; i < dp->num_cards; i++) {
    113   1.6    simonb 		off_t	temp;
    114   1.1       cgd 
    115   1.1       cgd 		r1 = roll(1, dp->num_cards) - 1;
    116   1.1       cgd 		r2 = roll(1, dp->num_cards) - 1;
    117   1.1       cgd 		temp = dp->offsets[r2];
    118   1.1       cgd 		dp->offsets[r2] = dp->offsets[r1];
    119   1.1       cgd 		dp->offsets[r1] = temp;
    120   1.1       cgd 	}
    121   1.1       cgd }
    122   1.7    simonb 
    123   1.1       cgd /*
    124   1.1       cgd  *	This routine draws a card from the given deck
    125   1.1       cgd  */
    126   1.4  christos void
    127   1.1       cgd get_card(dp)
    128   1.7    simonb 	DECK *dp;
    129   1.4  christos {
    130   1.7    simonb 	char type_maj, type_min;
    131   1.7    simonb 	int num;
    132   1.7    simonb 	int i, per_h, per_H, num_h, num_H;
    133   1.7    simonb 	OWN *op;
    134   1.1       cgd 
    135   1.1       cgd 	do {
    136  1.19  dholland 		fseek(deckf, dp->offsets[dp->top_card], SEEK_SET);
    137  1.19  dholland 		dp->top_card = ++(dp->top_card) % dp->num_cards;
    138   1.1       cgd 		type_maj = getc(deckf);
    139   1.1       cgd 	} while (dp->gojf_used && type_maj == GOJF);
    140   1.1       cgd 	type_min = getc(deckf);
    141  1.11    simonb 	num = ntohl(getw(deckf));
    142   1.1       cgd 	printmes();
    143   1.1       cgd 	switch (type_maj) {
    144   1.1       cgd 	  case '+':		/* get money		*/
    145   1.1       cgd 		if (type_min == 'A') {
    146   1.1       cgd 			for (i = 0; i < num_play; i++)
    147   1.1       cgd 				if (i != player)
    148   1.1       cgd 					play[i].money -= num;
    149   1.1       cgd 			num = num * (num_play - 1);
    150   1.1       cgd 		}
    151   1.1       cgd 		cur_p->money += num;
    152   1.1       cgd 		break;
    153   1.1       cgd 	  case '-':		/* lose money		*/
    154   1.1       cgd 		if (type_min == 'A') {
    155   1.1       cgd 			for (i = 0; i < num_play; i++)
    156   1.1       cgd 				if (i != player)
    157   1.1       cgd 					play[i].money += num;
    158   1.1       cgd 			num = num * (num_play - 1);
    159   1.1       cgd 		}
    160   1.1       cgd 		cur_p->money -= num;
    161   1.1       cgd 		break;
    162   1.1       cgd 	  case 'M':		/* move somewhere	*/
    163   1.1       cgd 		switch (type_min) {
    164   1.1       cgd 		  case 'F':		/* move forward	*/
    165   1.1       cgd 			num -= cur_p->loc;
    166   1.1       cgd 			if (num < 0)
    167   1.1       cgd 				num += 40;
    168   1.1       cgd 			break;
    169   1.1       cgd 		  case 'J':		/* move to jail	*/
    170   1.1       cgd 			goto_jail();
    171   1.1       cgd 			return;
    172   1.1       cgd 		  case 'R':		/* move to railroad	*/
    173   1.1       cgd 			spec = TRUE;
    174   1.1       cgd 			num = (int)((cur_p->loc + 5)/10)*10 + 5 - cur_p->loc;
    175   1.1       cgd 			break;
    176   1.1       cgd 		  case 'U':		/* move to utility	*/
    177   1.1       cgd 			spec = TRUE;
    178   1.1       cgd 			if (cur_p->loc >= 12 && cur_p->loc < 28)
    179   1.1       cgd 				num = 28 - cur_p->loc;
    180   1.1       cgd 			else {
    181   1.1       cgd 				num = 12 - cur_p->loc;
    182   1.1       cgd 				if (num < 0)
    183   1.1       cgd 					num += 40;
    184   1.1       cgd 			}
    185   1.1       cgd 			break;
    186   1.1       cgd 		  case 'B':
    187   1.1       cgd 			num = -num;
    188   1.1       cgd 			break;
    189   1.1       cgd 		}
    190   1.1       cgd 		move(num);
    191   1.1       cgd 		break;
    192   1.1       cgd 	  case 'T':			/* tax			*/
    193   1.1       cgd 		if (dp == &CC_D) {
    194   1.1       cgd 			per_h = 40;
    195   1.1       cgd 			per_H = 115;
    196   1.1       cgd 		}
    197   1.1       cgd 		else {
    198   1.1       cgd 			per_h = 25;
    199   1.1       cgd 			per_H = 100;
    200   1.1       cgd 		}
    201   1.1       cgd 		num_h = num_H = 0;
    202   1.1       cgd 		for (op = cur_p->own_list; op; op = op->next)
    203   1.5     veego 			if (op->sqr->type == PRPTY) {
    204   1.1       cgd 				if (op->sqr->desc->houses == 5)
    205   1.1       cgd 					++num_H;
    206   1.1       cgd 				else
    207   1.1       cgd 					num_h += op->sqr->desc->houses;
    208   1.5     veego 			}
    209   1.1       cgd 		num = per_h * num_h + per_H * num_H;
    210   1.7    simonb 		printf(
    211   1.7    simonb 		    "You had %d Houses and %d Hotels, so that cost you $%d\n",
    212   1.7    simonb 		    num_h, num_H, num);
    213   1.1       cgd 		if (num == 0)
    214   1.1       cgd 			lucky("");
    215   1.1       cgd 		else
    216   1.1       cgd 			cur_p->money -= num;
    217   1.1       cgd 		break;
    218   1.1       cgd 	  case GOJF:		/* get-out-of-jail-free card	*/
    219   1.1       cgd 		cur_p->num_gojf++;
    220   1.1       cgd 		dp->gojf_used = TRUE;
    221   1.1       cgd 		break;
    222   1.1       cgd 	}
    223   1.1       cgd 	spec = FALSE;
    224   1.1       cgd }
    225   1.4  christos 
    226   1.1       cgd /*
    227   1.1       cgd  *	This routine prints out the message on the card
    228   1.1       cgd  */
    229   1.4  christos static void
    230   1.7    simonb printmes()
    231   1.7    simonb {
    232   1.7    simonb 	char c;
    233   1.1       cgd 
    234   1.1       cgd 	printline();
    235   1.1       cgd 	fflush(stdout);
    236   1.1       cgd 	while ((c = getc(deckf)) != '\0')
    237   1.1       cgd 		putchar(c);
    238   1.1       cgd 	printline();
    239   1.1       cgd 	fflush(stdout);
    240   1.1       cgd }
    241  1.18  dholland 
    242  1.18  dholland /*
    243  1.18  dholland  *	This routine returns the players get-out-of-jail-free card
    244  1.20  dholland  * to the bottom of a deck.  XXX currently does not return to the correct
    245  1.20  dholland  * deck.
    246  1.18  dholland  */
    247  1.18  dholland void
    248  1.18  dholland ret_card(plr)
    249  1.18  dholland 	PLAY *plr;
    250  1.18  dholland {
    251  1.20  dholland 	char type_maj;
    252  1.20  dholland 	int gojfpos, last_card;
    253  1.20  dholland 	int i;
    254  1.20  dholland 	DECK *dp;
    255  1.20  dholland 	off_t temp;
    256  1.20  dholland 
    257  1.18  dholland 	plr->num_gojf--;
    258  1.18  dholland 	if (CC_D.gojf_used)
    259  1.20  dholland 		dp = &CC_D;
    260  1.18  dholland 	else
    261  1.20  dholland 		dp = &CH_D;
    262  1.20  dholland 	dp->gojf_used = FALSE;
    263  1.20  dholland 
    264  1.20  dholland 	/* Put at bottom of deck (top_card - 1) and remove it from wherever else
    265  1.20  dholland 	 * it used to be.
    266  1.20  dholland 	 */
    267  1.20  dholland 	last_card = dp->top_card - 1;
    268  1.20  dholland 	if (last_card < 0)
    269  1.20  dholland 		last_card += dp->num_cards;
    270  1.20  dholland 	gojfpos = dp->top_card;
    271  1.20  dholland 	do {
    272  1.20  dholland 		gojfpos = (gojfpos + 1) % dp->num_cards;
    273  1.20  dholland 		fseek(deckf, dp->offsets[gojfpos], SEEK_SET);
    274  1.20  dholland 		type_maj = getc(deckf);
    275  1.20  dholland 	} while (type_maj != GOJF);
    276  1.20  dholland 	temp = dp->offsets[gojfpos];
    277  1.20  dholland 	/* Only one of the next two loops does anything */
    278  1.20  dholland 	for (i = gojfpos - 1; i > last_card; i--)
    279  1.20  dholland 		dp->offsets[i + 1] = dp->offsets[i];
    280  1.20  dholland 	for (i = gojfpos; i < last_card; i++)
    281  1.20  dholland 		dp->offsets[i] = dp->offsets[i + 1];
    282  1.20  dholland 	if (gojfpos > last_card) {
    283  1.20  dholland 		dp->offsets[dp->top_card] = temp;
    284  1.20  dholland 		dp->top_card++;
    285  1.20  dholland 		dp->top_card %= dp->num_cards;
    286  1.20  dholland 	} else
    287  1.20  dholland 		dp->offsets[last_card] = temp;
    288  1.18  dholland }
    289