execute.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* $NetBSD: execute.c,v 1.11 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[] = "@(#)execute.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: execute.c,v 1.11 2004/01/27 20:30:30 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include "monop.ext"
  39. #include <fcntl.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <sys/time.h>
  45. #include <time.h>
  46. #define SEGSIZE 8192
  47. typedef struct stat STAT;
  48. typedef struct tm TIME;
  49. static char buf[257];
  50. static bool new_play; /* set if move on to new player */
  51. extern void *heapstart;
  52. static void show_move(void);
  53. /*
  54. * This routine executes the given command by index number
  55. */
  56. void
  57. execute(com_num)
  58. int com_num;
  59. {
  60. new_play = FALSE; /* new_play is true if fixing */
  61. (*func[com_num])();
  62. notify();
  63. force_morg();
  64. if (new_play)
  65. next_play();
  66. else if (num_doub)
  67. printf("%s rolled doubles. Goes again\n", cur_p->name);
  68. }
  69. /*
  70. * This routine moves a piece around.
  71. */
  72. void
  73. do_move()
  74. {
  75. int r1, r2;
  76. bool was_jail;
  77. new_play = was_jail = FALSE;
  78. printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
  79. if (cur_p->loc == JAIL) {
  80. was_jail++;
  81. if (!move_jail(r1, r2)) {
  82. new_play++;
  83. goto ret;
  84. }
  85. }
  86. else {
  87. if (r1 == r2 && ++num_doub == 3) {
  88. printf("That's 3 doubles. You go to jail\n");
  89. goto_jail();
  90. new_play++;
  91. goto ret;
  92. }
  93. move(r1+r2);
  94. }
  95. if (r1 != r2 || was_jail)
  96. new_play++;
  97. ret:
  98. return;
  99. }
  100. /*
  101. * This routine moves a normal move
  102. */
  103. void
  104. move(rl)
  105. int rl;
  106. {
  107. int old_loc;
  108. old_loc = cur_p->loc;
  109. cur_p->loc = (cur_p->loc + rl) % N_SQRS;
  110. if (cur_p->loc < old_loc && rl > 0) {
  111. cur_p->money += 200;
  112. printf("You pass %s and get $200\n", board[0].name);
  113. }
  114. show_move();
  115. }
  116. /*
  117. * This routine shows the results of a move
  118. */
  119. static void
  120. show_move()
  121. {
  122. SQUARE *sqp;
  123. sqp = &board[cur_p->loc];
  124. printf("That puts you on %s\n", sqp->name);
  125. switch (sqp->type) {
  126. case SAFE:
  127. printf("That is a safe place\n");
  128. break;
  129. case CC:
  130. cc(); break;
  131. case CHANCE:
  132. chance(); break;
  133. case INC_TAX:
  134. inc_tax(); break;
  135. case GOTO_J:
  136. goto_jail(); break;
  137. case LUX_TAX:
  138. lux_tax(); break;
  139. case PRPTY:
  140. case RR:
  141. case UTIL:
  142. if (sqp->owner < 0) {
  143. printf("That would cost $%d\n", sqp->cost);
  144. if (getyn("Do you want to buy? ") == 0) {
  145. buy(player, sqp);
  146. cur_p->money -= sqp->cost;
  147. }
  148. else if (num_play > 2)
  149. bid();
  150. }
  151. else if (sqp->owner == player)
  152. printf("You own it.\n");
  153. else
  154. rent(sqp);
  155. }
  156. }
  157. /*
  158. * This routine saves the current game for use at a later date
  159. */
  160. void
  161. save()
  162. {
  163. char *sp;
  164. int outf, num;
  165. time_t t;
  166. struct stat sb;
  167. char *start, *end;
  168. printf("Which file do you wish to save it in? ");
  169. sp = buf;
  170. while ((*sp++=getchar()) != '\n')
  171. continue;
  172. *--sp = '\0';
  173. /*
  174. * check for existing files, and confirm overwrite if needed
  175. */
  176. if (stat(buf, &sb) > -1
  177. && getyn("File exists. Do you wish to overwrite? ") > 0)
  178. return;
  179. if ((outf=creat(buf, 0644)) < 0) {
  180. perror(buf);
  181. return;
  182. }
  183. printf("\"%s\" ", buf);
  184. time(&t); /* get current time */
  185. strcpy(buf, ctime(&t));
  186. for (sp = buf; *sp != '\n'; sp++)
  187. continue;
  188. *sp = '\0';
  189. start = heapstart;
  190. end = sbrk(0);
  191. while (start < end) { /* write out entire data space */
  192. num = start + 16 * 1024 > end ? end - start : 16 * 1024;
  193. write(outf, start, num);
  194. start += num;
  195. }
  196. close(outf);
  197. printf("[%s]\n", buf);
  198. }
  199. /*
  200. * This routine restores an old game from a file
  201. */
  202. void
  203. restore()
  204. {
  205. char *sp;
  206. printf("Which file do you wish to restore from? ");
  207. for (sp = buf; (*sp=getchar()) != '\n'; sp++)
  208. continue;
  209. *sp = '\0';
  210. rest_f(buf);
  211. }
  212. /*
  213. * This does the actual restoring. It returns TRUE if the
  214. * backup was successful, else false.
  215. */
  216. int
  217. rest_f(file)
  218. const char *file;
  219. {
  220. char *sp;
  221. int inf, num;
  222. char buf[80];
  223. char *start, *end;
  224. STAT sbuf;
  225. if ((inf=open(file, O_RDONLY)) < 0) {
  226. perror(file);
  227. return FALSE;
  228. }
  229. printf("\"%s\" ", file);
  230. if (fstat(inf, &sbuf) < 0) { /* get file stats */
  231. perror(file);
  232. exit(1);
  233. }
  234. start = heapstart;
  235. brk(end = start + sbuf.st_size);
  236. while (start < end) { /* write out entire data space */
  237. num = start + 16 * 1024 > end ? end - start : 16 * 1024;
  238. read(inf, start, num);
  239. start += num;
  240. }
  241. close(inf);
  242. strcpy(buf, ctime(&sbuf.st_mtime));
  243. for (sp = buf; *sp != '\n'; sp++)
  244. continue;
  245. *sp = '\0';
  246. printf("[%s]\n", buf);
  247. return TRUE;
  248. }