NetBSD/games/monop/cards.c

242 lines
5.7 KiB
C
Raw Normal View History

/* $NetBSD: cards.c,v 1.13 2003/08/07 09:37:27 agc Exp $ */
1993-03-21 12:45:37 +03:00
/*
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-10-12 21:45:06 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93";
#else
__RCSID("$NetBSD: cards.c,v 1.13 2003/08/07 09:37:27 agc Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
1999-08-21 14:40:03 +04:00
#include <sys/types.h>
#include <sys/endian.h>
#include "monop.ext"
#include "pathnames.h"
1993-03-21 12:45:37 +03:00
/*
* These routine deal with the card decks
*/
1999-08-21 14:40:03 +04:00
#define GOJF 'F' /* char for get-out-of-jail-free cards */
1993-03-21 12:45:37 +03:00
1999-08-21 14:40:03 +04:00
#ifndef DEV
static const char *cardfile = _PATH_CARDS;
1999-08-21 14:40:03 +04:00
#else
static const char *cardfile = "cards.pck";
1999-08-21 14:40:03 +04:00
#endif
1993-03-21 12:45:37 +03:00
static FILE *deckf;
1997-10-12 21:45:06 +04:00
static void set_up __P((DECK *));
static void printmes __P((void));
1993-03-21 12:45:37 +03:00
/*
* This routine initializes the decks from the data file,
* which it opens.
*/
1997-10-12 21:45:06 +04:00
void
init_decks()
{
int32_t nc;
1993-03-21 12:45:37 +03:00
if ((deckf=fopen(cardfile, "r")) == NULL) {
file_err:
perror(cardfile);
exit(1);
}
/* read number of community chest cards... */
if (fread(&nc, sizeof(nc), 1, deckf) != 1)
goto file_err;
CC_D.num_cards = be32toh(nc);
/* ... and number of community chest cards. */
if (fread(&nc, sizeof(nc), 1, deckf) != 1)
1993-03-21 12:45:37 +03:00
goto file_err;
CH_D.num_cards = be32toh(nc);
1993-03-21 12:45:37 +03:00
set_up(&CC_D);
set_up(&CH_D);
}
1999-08-21 14:40:03 +04:00
1993-03-21 12:45:37 +03:00
/*
* This routine sets up the offset pointers for the given deck.
*/
1997-10-12 21:45:06 +04:00
static void
1993-03-21 12:45:37 +03:00
set_up(dp)
1999-08-21 14:40:03 +04:00
DECK *dp;
{
int r1, r2;
int i;
1993-03-21 12:45:37 +03:00
dp->offsets = (off_t *) calloc(dp->num_cards, sizeof (off_t));
if (dp->offsets == NULL)
errx(1, "out of memory");
if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
dp->num_cards) {
1993-03-21 12:45:37 +03:00
perror(cardfile);
exit(1);
}
/* convert offsets from big-endian byte order */
for (i = 0; i < dp->num_cards; i++)
BE64TOH(dp->offsets[i]);
1993-03-21 12:45:37 +03:00
dp->last_card = 0;
dp->gojf_used = FALSE;
for (i = 0; i < dp->num_cards; i++) {
off_t temp;
1993-03-21 12:45:37 +03:00
r1 = roll(1, dp->num_cards) - 1;
r2 = roll(1, dp->num_cards) - 1;
temp = dp->offsets[r2];
dp->offsets[r2] = dp->offsets[r1];
dp->offsets[r1] = temp;
}
}
1999-08-21 14:40:03 +04:00
1993-03-21 12:45:37 +03:00
/*
* This routine draws a card from the given deck
*/
1997-10-12 21:45:06 +04:00
void
1993-03-21 12:45:37 +03:00
get_card(dp)
1999-08-21 14:40:03 +04:00
DECK *dp;
1997-10-12 21:45:06 +04:00
{
1999-08-21 14:40:03 +04:00
char type_maj, type_min;
int num;
int i, per_h, per_H, num_h, num_H;
OWN *op;
1993-03-21 12:45:37 +03:00
do {
fseek(deckf, dp->offsets[dp->last_card], SEEK_SET);
1993-03-21 12:45:37 +03:00
dp->last_card = ++(dp->last_card) % dp->num_cards;
type_maj = getc(deckf);
} while (dp->gojf_used && type_maj == GOJF);
type_min = getc(deckf);
num = ntohl(getw(deckf));
1993-03-21 12:45:37 +03:00
printmes();
switch (type_maj) {
case '+': /* get money */
if (type_min == 'A') {
for (i = 0; i < num_play; i++)
if (i != player)
play[i].money -= num;
num = num * (num_play - 1);
}
cur_p->money += num;
break;
case '-': /* lose money */
if (type_min == 'A') {
for (i = 0; i < num_play; i++)
if (i != player)
play[i].money += num;
num = num * (num_play - 1);
}
cur_p->money -= num;
break;
case 'M': /* move somewhere */
switch (type_min) {
case 'F': /* move forward */
num -= cur_p->loc;
if (num < 0)
num += 40;
break;
case 'J': /* move to jail */
goto_jail();
return;
case 'R': /* move to railroad */
spec = TRUE;
num = (int)((cur_p->loc + 5)/10)*10 + 5 - cur_p->loc;
break;
case 'U': /* move to utility */
spec = TRUE;
if (cur_p->loc >= 12 && cur_p->loc < 28)
num = 28 - cur_p->loc;
else {
num = 12 - cur_p->loc;
if (num < 0)
num += 40;
}
break;
case 'B':
num = -num;
break;
}
move(num);
break;
case 'T': /* tax */
if (dp == &CC_D) {
per_h = 40;
per_H = 115;
}
else {
per_h = 25;
per_H = 100;
}
num_h = num_H = 0;
for (op = cur_p->own_list; op; op = op->next)
1998-08-30 13:19:36 +04:00
if (op->sqr->type == PRPTY) {
1993-03-21 12:45:37 +03:00
if (op->sqr->desc->houses == 5)
++num_H;
else
num_h += op->sqr->desc->houses;
1998-08-30 13:19:36 +04:00
}
1993-03-21 12:45:37 +03:00
num = per_h * num_h + per_H * num_H;
1999-08-21 14:40:03 +04:00
printf(
"You had %d Houses and %d Hotels, so that cost you $%d\n",
num_h, num_H, num);
1993-03-21 12:45:37 +03:00
if (num == 0)
lucky("");
else
cur_p->money -= num;
break;
case GOJF: /* get-out-of-jail-free card */
cur_p->num_gojf++;
dp->gojf_used = TRUE;
break;
}
spec = FALSE;
}
1997-10-12 21:45:06 +04:00
1993-03-21 12:45:37 +03:00
/*
* This routine prints out the message on the card
*/
1997-10-12 21:45:06 +04:00
static void
1999-08-21 14:40:03 +04:00
printmes()
{
char c;
1993-03-21 12:45:37 +03:00
printline();
fflush(stdout);
while ((c = getc(deckf)) != '\0')
putchar(c);
printline();
fflush(stdout);
}