houses.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* $NetBSD: houses.c,v 1.8 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[] = "@(#)houses.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: houses.c,v 1.8 2004/01/27 20:30:30 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include "monop.ext"
  39. static const char *names[N_MON+2];
  40. static char cur_prop[80];
  41. static MON *monops[N_MON];
  42. static void buy_h(MON *);
  43. static void sell_h(MON *);
  44. static void list_cur(MON *);
  45. /*
  46. * These routines deal with buying and selling houses
  47. */
  48. void
  49. buy_houses()
  50. {
  51. int num_mon;
  52. MON *mp;
  53. OWN *op;
  54. bool good,got_morg;
  55. int i,p;
  56. over:
  57. num_mon = 0;
  58. good = TRUE;
  59. got_morg = FALSE;
  60. for (op = cur_p->own_list; op && op->sqr->type != PRPTY; op = op->next)
  61. continue;
  62. while (op)
  63. if (op->sqr->desc->monop) {
  64. mp = op->sqr->desc->mon_desc;
  65. names[num_mon] = (monops[num_mon]=mp)->name;
  66. num_mon++;
  67. got_morg = good = FALSE;
  68. for (i = 0; i < mp->num_in; i++) {
  69. if (op->sqr->desc->morg)
  70. got_morg++;
  71. if (op->sqr->desc->houses != 5)
  72. good++;
  73. op = op->next;
  74. }
  75. if (!good || got_morg)
  76. --num_mon;
  77. }
  78. else
  79. op = op->next;
  80. if (num_mon == 0) {
  81. if (got_morg)
  82. printf("You can't build on mortgaged monopolies.\n");
  83. else if (!good)
  84. printf("You can't build any more.\n");
  85. else
  86. printf("But you don't have any monopolies!!\n");
  87. return;
  88. }
  89. if (num_mon == 1)
  90. buy_h(monops[0]);
  91. else {
  92. names[num_mon++] = "done";
  93. names[num_mon--] = 0;
  94. if ((p = getinp(
  95. "Which property do you wish to buy houses for? ",
  96. names)) == num_mon)
  97. return;
  98. buy_h(monops[p]);
  99. goto over;
  100. }
  101. }
  102. static void
  103. buy_h(mnp)
  104. MON *mnp;
  105. {
  106. int i;
  107. MON *mp;
  108. int price;
  109. short input[3],temp[3];
  110. int tot;
  111. PROP *pp;
  112. mp = mnp;
  113. price = mp->h_cost * 50;
  114. blew_it:
  115. list_cur(mp);
  116. printf("Houses will cost $%d\n", price);
  117. printf("How many houses do you wish to buy for\n");
  118. for (i = 0; i < mp->num_in; i++) {
  119. pp = mp->sq[i]->desc;
  120. over:
  121. if (pp->houses == 5) {
  122. printf("%s (H):\n", mp->sq[i]->name);
  123. input[i] = 0;
  124. temp[i] = 5;
  125. continue;
  126. }
  127. (void)sprintf(cur_prop, "%s (%d): ",
  128. mp->sq[i]->name, pp->houses);
  129. input[i] = get_int(cur_prop);
  130. temp[i] = input[i] + pp->houses;
  131. if (temp[i] > 5) {
  132. printf("That's too many. The most you can buy is %d\n",
  133. 5 - pp->houses);
  134. goto over;
  135. }
  136. }
  137. if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
  138. abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
  139. err: printf("That makes the spread too wide. Try again\n");
  140. goto blew_it;
  141. }
  142. else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
  143. goto err;
  144. for (tot = i = 0; i < mp->num_in; i++)
  145. tot += input[i];
  146. if (tot) {
  147. printf("You asked for %d houses for $%d\n", tot, tot * price);
  148. if (getyn("Is that ok? ") == 0) {
  149. cur_p->money -= tot * price;
  150. for (tot = i = 0; i < mp->num_in; i++)
  151. mp->sq[i]->desc->houses = temp[i];
  152. }
  153. }
  154. }
  155. /*
  156. * This routine sells houses.
  157. */
  158. void
  159. sell_houses()
  160. {
  161. int num_mon;
  162. MON *mp;
  163. OWN *op;
  164. bool good;
  165. int p;
  166. over:
  167. num_mon = 0;
  168. good = TRUE;
  169. for (op = cur_p->own_list; op; op = op->next)
  170. if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
  171. mp = op->sqr->desc->mon_desc;
  172. names[num_mon] = (monops[num_mon]=mp)->name;
  173. num_mon++;
  174. good = 0;
  175. do
  176. if (!good && op->sqr->desc->houses != 0)
  177. good++;
  178. while (op->next && op->sqr->desc->mon_desc == mp
  179. && (op=op->next));
  180. if (!good)
  181. --num_mon;
  182. }
  183. if (num_mon == 0) {
  184. printf("You don't have any houses to sell!!\n");
  185. return;
  186. }
  187. if (num_mon == 1)
  188. sell_h(monops[0]);
  189. else {
  190. names[num_mon++] = "done";
  191. names[num_mon--] = 0;
  192. if ((p = getinp(
  193. "Which property do you wish to sell houses from? ",
  194. names)) == num_mon)
  195. return;
  196. sell_h(monops[p]);
  197. notify();
  198. goto over;
  199. }
  200. }
  201. static void
  202. sell_h(mnp)
  203. MON *mnp;
  204. {
  205. int i;
  206. MON *mp;
  207. int price;
  208. short input[3],temp[3];
  209. int tot;
  210. PROP *pp;
  211. mp = mnp;
  212. price = mp->h_cost * 25;
  213. blew_it:
  214. printf("Houses will get you $%d apiece\n", price);
  215. list_cur(mp);
  216. printf("How many houses do you wish to sell from\n");
  217. for (i = 0; i < mp->num_in; i++) {
  218. pp = mp->sq[i]->desc;
  219. over:
  220. if (pp->houses == 0) {
  221. printf("%s (0):\n", mp->sq[i]->name);
  222. input[i] = temp[i] = 0;
  223. continue;
  224. }
  225. if (pp->houses < 5)
  226. (void)sprintf(cur_prop,"%s (%d): ",
  227. mp->sq[i]->name,pp->houses);
  228. else
  229. (void)sprintf(cur_prop,"%s (H): ",mp->sq[i]->name);
  230. input[i] = get_int(cur_prop);
  231. temp[i] = pp->houses - input[i];
  232. if (temp[i] < 0) {
  233. printf(
  234. "That's too many. The most you can sell is %d\n",
  235. pp->houses);
  236. goto over;
  237. }
  238. }
  239. if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
  240. abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
  241. err: printf("That makes the spread too wide. Try again\n");
  242. goto blew_it;
  243. }
  244. else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
  245. goto err;
  246. for (tot = i = 0; i < mp->num_in; i++)
  247. tot += input[i];
  248. if (tot) {
  249. printf("You asked to sell %d houses for $%d\n",tot,tot * price);
  250. if (getyn("Is that ok? ") == 0) {
  251. cur_p->money += tot * price;
  252. for (tot = i = 0; i < mp->num_in; i++)
  253. mp->sq[i]->desc->houses = temp[i];
  254. }
  255. }
  256. }
  257. static void
  258. list_cur(mp)
  259. MON *mp;
  260. {
  261. int i;
  262. SQUARE *sqp;
  263. for (i = 0; i < mp->num_in; i++) {
  264. sqp = mp->sq[i];
  265. if (sqp->desc->houses == 5)
  266. printf("%s (H) ", sqp->name);
  267. else
  268. printf("%s (%d) ", sqp->name, sqp->desc->houses);
  269. }
  270. putchar('\n');
  271. }