cards.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* $NetBSD: cards.c,v 1.14 2004/01/27 20:30:30 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: cards.c,v 1.14 2004/01/27 20:30:30 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <sys/types.h>
  39. #include <sys/endian.h>
  40. #include "monop.ext"
  41. #include "pathnames.h"
  42. /*
  43. * These routine deal with the card decks
  44. */
  45. #define GOJF 'F' /* char for get-out-of-jail-free cards */
  46. #ifndef DEV
  47. static const char *cardfile = _PATH_CARDS;
  48. #else
  49. static const char *cardfile = "cards.pck";
  50. #endif
  51. static FILE *deckf;
  52. static void set_up(DECK *);
  53. static void printmes(void);
  54. /*
  55. * This routine initializes the decks from the data file,
  56. * which it opens.
  57. */
  58. void
  59. init_decks()
  60. {
  61. int32_t nc;
  62. if ((deckf=fopen(cardfile, "r")) == NULL) {
  63. file_err:
  64. perror(cardfile);
  65. exit(1);
  66. }
  67. /* read number of community chest cards... */
  68. if (fread(&nc, sizeof(nc), 1, deckf) != 1)
  69. goto file_err;
  70. CC_D.num_cards = be32toh(nc);
  71. /* ... and number of community chest cards. */
  72. if (fread(&nc, sizeof(nc), 1, deckf) != 1)
  73. goto file_err;
  74. CH_D.num_cards = be32toh(nc);
  75. set_up(&CC_D);
  76. set_up(&CH_D);
  77. }
  78. /*
  79. * This routine sets up the offset pointers for the given deck.
  80. */
  81. static void
  82. set_up(dp)
  83. DECK *dp;
  84. {
  85. int r1, r2;
  86. int i;
  87. dp->offsets = (u_int64_t *) calloc(dp->num_cards, sizeof (u_int64_t));
  88. if (dp->offsets == NULL)
  89. err(1, NULL);
  90. if (fread(dp->offsets, sizeof(u_int64_t), dp->num_cards, deckf) !=
  91. (size_t)dp->num_cards) {
  92. perror(cardfile);
  93. exit(1);
  94. }
  95. /* convert offsets from big-endian byte order */
  96. for (i = 0; i < dp->num_cards; i++)
  97. BE64TOH(dp->offsets[i]);
  98. dp->last_card = 0;
  99. dp->gojf_used = FALSE;
  100. for (i = 0; i < dp->num_cards; i++) {
  101. u_int64_t temp;
  102. r1 = roll(1, dp->num_cards) - 1;
  103. r2 = roll(1, dp->num_cards) - 1;
  104. temp = dp->offsets[r2];
  105. dp->offsets[r2] = dp->offsets[r1];
  106. dp->offsets[r1] = temp;
  107. }
  108. }
  109. /*
  110. * This routine draws a card from the given deck
  111. */
  112. void
  113. get_card(dp)
  114. DECK *dp;
  115. {
  116. char type_maj, type_min;
  117. int num;
  118. int i, per_h, per_H, num_h, num_H;
  119. OWN *op;
  120. do {
  121. fseek(deckf, dp->offsets[dp->last_card], SEEK_SET);
  122. dp->last_card = ++(dp->last_card) % dp->num_cards;
  123. type_maj = getc(deckf);
  124. } while (dp->gojf_used && type_maj == GOJF);
  125. type_min = getc(deckf);
  126. num = ntohl(getw(deckf));
  127. printmes();
  128. switch (type_maj) {
  129. case '+': /* get money */
  130. if (type_min == 'A') {
  131. for (i = 0; i < num_play; i++)
  132. if (i != player)
  133. play[i].money -= num;
  134. num = num * (num_play - 1);
  135. }
  136. cur_p->money += num;
  137. break;
  138. case '-': /* lose money */
  139. if (type_min == 'A') {
  140. for (i = 0; i < num_play; i++)
  141. if (i != player)
  142. play[i].money += num;
  143. num = num * (num_play - 1);
  144. }
  145. cur_p->money -= num;
  146. break;
  147. case 'M': /* move somewhere */
  148. switch (type_min) {
  149. case 'F': /* move forward */
  150. num -= cur_p->loc;
  151. if (num < 0)
  152. num += 40;
  153. break;
  154. case 'J': /* move to jail */
  155. goto_jail();
  156. return;
  157. case 'R': /* move to railroad */
  158. spec = TRUE;
  159. num = (int)((cur_p->loc + 5)/10)*10 + 5 - cur_p->loc;
  160. break;
  161. case 'U': /* move to utility */
  162. spec = TRUE;
  163. if (cur_p->loc >= 12 && cur_p->loc < 28)
  164. num = 28 - cur_p->loc;
  165. else {
  166. num = 12 - cur_p->loc;
  167. if (num < 0)
  168. num += 40;
  169. }
  170. break;
  171. case 'B':
  172. num = -num;
  173. break;
  174. }
  175. move(num);
  176. break;
  177. case 'T': /* tax */
  178. if (dp == &CC_D) {
  179. per_h = 40;
  180. per_H = 115;
  181. }
  182. else {
  183. per_h = 25;
  184. per_H = 100;
  185. }
  186. num_h = num_H = 0;
  187. for (op = cur_p->own_list; op; op = op->next)
  188. if (op->sqr->type == PRPTY) {
  189. if (op->sqr->desc->houses == 5)
  190. ++num_H;
  191. else
  192. num_h += op->sqr->desc->houses;
  193. }
  194. num = per_h * num_h + per_H * num_H;
  195. printf(
  196. "You had %d Houses and %d Hotels, so that cost you $%d\n",
  197. num_h, num_H, num);
  198. if (num == 0)
  199. lucky("");
  200. else
  201. cur_p->money -= num;
  202. break;
  203. case GOJF: /* get-out-of-jail-free card */
  204. cur_p->num_gojf++;
  205. dp->gojf_used = TRUE;
  206. break;
  207. }
  208. spec = FALSE;
  209. }
  210. /*
  211. * This routine prints out the message on the card
  212. */
  213. static void
  214. printmes()
  215. {
  216. char c;
  217. printline();
  218. fflush(stdout);
  219. while ((c = getc(deckf)) != '\0')
  220. putchar(c);
  221. printline();
  222. fflush(stdout);
  223. }