1 /* $NetBSD: execute.c,v 1.22 2012/06/19 05:35:32 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)execute.c 8.1 (Berkeley) 5/31/93
32 */
33
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <time.h>
42 #include <errno.h>
43
44 #include "deck.h"
45 #include "monop.h"
46
47 #define MIN_FORMAT_VERSION 1
48 #define CUR_FORMAT_VERSION 1
49 #define MAX_FORMAT_VERSION 1
50
51 typedef struct stat STAT;
52 typedef struct tm TIME;
53
54 static char buf[257];
55
56 static bool new_play; /* set if move on to new player */
57
58 static void show_move(void);
59
60 static void restore_reset(void);
61 static int restore_parseline(char *txt);
62 static int restore_toplevel_attr(const char *attribute, char *txt);
63 static int restore_player_attr(const char *attribute, char *txt);
64 static int restore_deck_attr(const char *attribute, char *txt);
65 static int restore_square_attr(const char *attribute, char *txt);
66 static int getnum(const char *what, char *txt, int min, int max, int *ret);
67 static int getnum_withbrace(const char *what, char *txt, int min, int max,
68 int *ret);
69
70 /*
71 * This routine executes the given command by index number
72 */
73 void
execute(int com_num)74 execute(int com_num)
75 {
76 new_play = FALSE; /* new_play is true if fixing */
77 (*func[com_num])();
78 notify();
79 force_morg();
80 if (new_play)
81 next_play();
82 else if (num_doub)
83 printf("%s rolled doubles. Goes again\n", cur_p->name);
84 }
85
86 /*
87 * This routine moves a piece around.
88 */
89 void
do_move(void)90 do_move(void)
91 {
92 int r1, r2;
93 bool was_jail;
94
95 new_play = was_jail = FALSE;
96 printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
97 if (cur_p->loc == JAIL) {
98 was_jail++;
99 if (!move_jail(r1, r2)) {
100 new_play++;
101 goto ret;
102 }
103 }
104 else {
105 if (r1 == r2 && ++num_doub == 3) {
106 printf("That's 3 doubles. You go to jail\n");
107 goto_jail();
108 new_play++;
109 goto ret;
110 }
111 move(r1+r2);
112 }
113 if (r1 != r2 || was_jail)
114 new_play++;
115 ret:
116 return;
117 }
118
119 /*
120 * This routine moves a normal move
121 */
122 void
move(int rl)123 move(int rl)
124 {
125 int old_loc;
126
127 old_loc = cur_p->loc;
128 cur_p->loc = (cur_p->loc + rl) % N_SQRS;
129 if (cur_p->loc < old_loc && rl > 0) {
130 cur_p->money += 200;
131 printf("You pass %s and get $200\n", board[0].name);
132 }
133 show_move();
134 }
135
136 /*
137 * This routine shows the results of a move
138 */
139 static void
show_move(void)140 show_move(void)
141 {
142 SQUARE *sqp;
143
144 sqp = &board[cur_p->loc];
145 printf("That puts you on %s\n", sqp->name);
146 switch (sqp->type) {
147 case SAFE:
148 printf("That is a safe place\n");
149 break;
150 case CC:
151 cc();
152 break;
153 case CHANCE:
154 chance();
155 break;
156 case INC_TAX:
157 inc_tax();
158 break;
159 case GOTO_J:
160 goto_jail();
161 break;
162 case LUX_TAX:
163 lux_tax();
164 break;
165 case PRPTY:
166 case RR:
167 case UTIL:
168 if (sqp->owner < 0) {
169 printf("That would cost $%d\n", sqp->cost);
170 if (getyn("Do you want to buy? ") == 0) {
171 buy(player, sqp);
172 cur_p->money -= sqp->cost;
173 }
174 else if (num_play > 2)
175 bid();
176 }
177 else if (sqp->owner == player)
178 printf("You own it.\n");
179 else
180 rent(sqp);
181 }
182 }
183
184 /*
185 * Reset the game state.
186 */
187 static void
reset_game(void)188 reset_game(void)
189 {
190 int i;
191
192 for (i = 0; i < N_SQRS; i++) {
193 board[i].owner = -1;
194 if (board[i].type == PRPTY) {
195 board[i].desc->morg = 0;
196 board[i].desc->houses = 0;
197 } else if (board[i].type == RR || board[i].type == UTIL) {
198 board[i].desc->morg = 0;
199 }
200 }
201
202 for (i = 0; i < 2; i++) {
203 deck[i].top_card = 0;
204 deck[i].gojf_used = FALSE;
205 }
206
207 if (play) {
208 for (i = 0; i < num_play; i++) {
209 free(play[i].name);
210 play[i].name = NULL;
211 }
212 free(play);
213 play = NULL;
214 }
215
216 for (i = 0; i < MAX_PL+2; i++) {
217 name_list[i] = NULL;
218 }
219
220 cur_p = NULL;
221 num_play = 0;
222 player = 0;
223 num_doub = 0;
224 fixing = FALSE;
225 trading = FALSE;
226 told_em = FALSE;
227 spec = FALSE;
228 }
229
230
231 /*
232 * This routine saves the current game for use at a later date
233 */
234 void
save(void)235 save(void)
236 {
237 char *sp;
238 FILE *outf;
239 time_t t;
240 struct stat sb;
241 int i, j;
242
243 printf("Which file do you wish to save it in? ");
244 fgets(buf, sizeof(buf), stdin);
245 if (feof(stdin))
246 return;
247 sp = strchr(buf, '\n');
248 if (sp)
249 *sp = '\0';
250
251 /*
252 * check for existing files, and confirm overwrite if needed
253 */
254
255 if (stat(buf, &sb) == 0
256 && getyn("File exists. Do you wish to overwrite? ") > 0)
257 return;
258
259 outf = fopen(buf, "w");
260 if (outf == NULL) {
261 warn("%s", buf);
262 return;
263 }
264 printf("\"%s\" ", buf);
265 time(&t); /* get current time */
266
267 /* Header */
268 fprintf(outf, "NetBSD monop format v%d\n", CUR_FORMAT_VERSION);
269 fprintf(outf, "time %s", ctime(&t)); /* ctime includes a \n */
270 fprintf(outf, "numplayers %d\n", num_play);
271 fprintf(outf, "currentplayer %d\n", player);
272 fprintf(outf, "doubles %d\n", num_doub);
273
274 /* Players */
275 for (i = 0; i < num_play; i++) {
276 fprintf(outf, "player %d {\n", i);
277 fprintf(outf, " name %s\n", name_list[i]);
278 fprintf(outf, " money %d\n", play[i].money);
279 fprintf(outf, " loc %d\n", play[i].loc);
280 fprintf(outf, " num_gojf %d\n", play[i].num_gojf);
281 fprintf(outf, " in_jail %d\n", play[i].in_jail);
282 fprintf(outf, "}\n");
283 }
284
285 /* Decks */
286 for (i = 0; i < 2; i++) {
287 fprintf(outf, "deck %d {\n", i);
288 fprintf(outf, " numcards %d\n", deck[i].num_cards);
289 fprintf(outf, " topcard %d\n", deck[i].top_card);
290 fprintf(outf, " gojf_used %d\n", deck[i].gojf_used);
291 fprintf(outf, " cards");
292 for (j = 0; j < deck[i].num_cards; j++)
293 fprintf(outf, " %d", deck[i].cards[j]);
294 fprintf(outf, "\n");
295 fprintf(outf, "}\n");
296 }
297
298 /* Board */
299 for (i = 0; i < N_SQRS; i++) {
300 fprintf(outf, "square %d {\n", i);
301 fprintf(outf, "owner %d\n", board[i].owner);
302 if (board[i].owner < 0) {
303 /* nothing */
304 } else if (board[i].type == PRPTY) {
305 fprintf(outf, "morg %d\n", board[i].desc->morg);
306 fprintf(outf, "houses %d\n", board[i].desc->houses);
307 } else if (board[i].type == RR || board[i].type == UTIL) {
308 fprintf(outf, "morg %d\n", board[i].desc->morg);
309 }
310 fprintf(outf, "}\n");
311 }
312 if (ferror(outf) || fflush(outf))
313 warnx("write error");
314 fclose(outf);
315
316 strcpy(buf, ctime(&t));
317 for (sp = buf; *sp != '\n'; sp++)
318 continue;
319 *sp = '\0';
320 printf("[%s]\n", buf);
321 }
322
323 /*
324 * This routine restores an old game from a file
325 */
326 void
restore(void)327 restore(void)
328 {
329 char *sp;
330
331 for (;;) {
332 printf("Which file do you wish to restore from? ");
333 fgets(buf, sizeof(buf), stdin);
334 if (feof(stdin))
335 return;
336 sp = strchr(buf, '\n');
337 if (sp)
338 *sp = '\0';
339 if (rest_f(buf) == 0)
340 break;
341 }
342 }
343
344 /*
345 * This does the actual restoring. It returns zero on success,
346 * and -1 on failure.
347 */
348 int
rest_f(const char * file)349 rest_f(const char *file)
350 {
351 char *sp;
352 FILE *inf;
353 char xbuf[80];
354 STAT sbuf;
355 char readbuf[512];
356 int ret = 0;
357
358 inf = fopen(file, "r");
359 if (inf == NULL) {
360 warn("%s", file);
361 return -1;
362 }
363 printf("\"%s\" ", file);
364 if (fstat(fileno(inf), &sbuf) < 0) {
365 err(1, "%s: fstat", file);
366 }
367
368 /* Clear the game state to prevent brokenness on misordered files. */
369 reset_game();
370
371 /* Reset the parser */
372 restore_reset();
373
374 /* Note: can't use buf[], file might point at it. (Lame...) */
375 while (fgets(readbuf, sizeof(readbuf), inf)) {
376 /*
377 * The input buffer is long enough to handle anything
378 * that's supposed to be in the output buffer, so if
379 * we get a partial line, complain.
380 */
381 sp = strchr(readbuf, '\n');
382 if (sp == NULL) {
383 printf("file is corrupt: long lines.\n");
384 ret = -1;
385 break;
386 }
387 *sp = '\0';
388
389 if (restore_parseline(readbuf)) {
390 ret = -1;
391 break;
392 }
393 }
394
395 if (ferror(inf))
396 warnx("%s: read error", file);
397 fclose(inf);
398
399 if (ret < 0)
400 return -1;
401
402 name_list[num_play] = "done";
403
404 if (play == NULL || cur_p == NULL || num_play < 2) {
405 printf("save file is incomplete.\n");
406 return -1;
407 }
408
409 /*
410 * We could at this point crosscheck the following:
411 * - there are only two GOJF cards floating around
412 * - total number of houses and hotels does not exceed maximums
413 * - no props are both built and mortgaged
414 * but for now we don't.
415 */
416
417 strcpy(xbuf, ctime(&sbuf.st_mtime));
418 for (sp = xbuf; *sp != '\n'; sp++)
419 continue;
420 *sp = '\0';
421 printf("[%s]\n", xbuf);
422 return 0;
423 }
424
425 /*
426 * State of the restore parser
427 */
428 static int restore_version;
429 static enum {
430 RI_NONE,
431 RI_PLAYER,
432 RI_DECK,
433 RI_SQUARE
434 } restore_item;
435 static int restore_itemnum;
436
437 /*
438 * Reset the restore parser
439 */
440 static void
restore_reset(void)441 restore_reset(void)
442 {
443 restore_version = -1;
444 restore_item = RI_NONE;
445 restore_itemnum = -1;
446 }
447
448 /*
449 * Handle one line of the save file
450 */
451 static int
restore_parseline(char * txt)452 restore_parseline(char *txt)
453 {
454 char *attribute;
455 char *s;
456
457 if (restore_version < 0) {
458 /* Haven't seen the header yet. Demand it right away. */
459 if (!strncmp(txt, "NetBSD monop format v", 21)) {
460 return getnum("format version", txt+21,
461 MIN_FORMAT_VERSION,
462 MAX_FORMAT_VERSION,
463 &restore_version);
464 }
465 printf("file is not a monop save file.\n");
466 return -1;
467 }
468
469 /* Check for lines that are right braces. */
470 if (!strcmp(txt, "}")) {
471 if (restore_item == RI_NONE) {
472 printf("mismatched close brace.\n");
473 return -1;
474 }
475 restore_item = RI_NONE;
476 restore_itemnum = -1;
477 return 0;
478 }
479
480 /* Any other line must begin with a word, which is the attribute. */
481 s = txt;
482 while (*s==' ')
483 s++;
484 attribute = s;
485 s = strchr(attribute, ' ');
486 if (s == NULL) {
487 printf("file is corrupt: attribute %s lacks value.\n",
488 attribute);
489 return -1;
490 }
491 *(s++) = '\0';
492 while (*s==' ')
493 s++;
494 /* keep the remaining text for further handling */
495 txt = s;
496
497 switch (restore_item) {
498 case RI_NONE:
499 /* toplevel attributes */
500 return restore_toplevel_attr(attribute, txt);
501
502 case RI_PLAYER:
503 /* player attributes */
504 return restore_player_attr(attribute, txt);
505
506 case RI_DECK:
507 /* deck attributes */
508 return restore_deck_attr(attribute, txt);
509
510 case RI_SQUARE:
511 /* board square attributes */
512 return restore_square_attr(attribute, txt);
513 }
514 /* NOTREACHED */
515 printf("internal logic error\n");
516 return -1;
517 }
518
519 static int
restore_toplevel_attr(const char * attribute,char * txt)520 restore_toplevel_attr(const char *attribute, char *txt)
521 {
522 if (!strcmp(attribute, "time")) {
523 /* nothing */
524 } else if (!strcmp(attribute, "numplayers")) {
525 if (getnum("numplayers", txt, 2, MAX_PL, &num_play) < 0) {
526 return -1;
527 }
528 if (play != NULL) {
529 printf("numplayers: multiple settings\n");
530 return -1;
531 }
532 play = calloc((size_t)num_play, sizeof(play[0]));
533 if (play == NULL) {
534 err(1, "calloc");
535 }
536 } else if (!strcmp(attribute, "currentplayer")) {
537 if (getnum("currentplayer", txt, 0, num_play-1, &player) < 0) {
538 return -1;
539 }
540 if (play == NULL) {
541 printf("currentplayer: before numplayers\n");
542 return -1;
543 }
544 cur_p = &play[player];
545 } else if (!strcmp(attribute, "doubles")) {
546 if (getnum("doubles", txt, 0, 2, &num_doub) < 0) {
547 return -1;
548 }
549 } else if (!strcmp(attribute, "player")) {
550 if (getnum_withbrace("player", txt, 0, num_play-1,
551 &restore_itemnum) < 0) {
552 return -1;
553 }
554 restore_item = RI_PLAYER;
555 } else if (!strcmp(attribute, "deck")) {
556 if (getnum_withbrace("deck", txt, 0, 1,
557 &restore_itemnum) < 0) {
558 return -1;
559 }
560 restore_item = RI_DECK;
561 } else if (!strcmp(attribute, "square")) {
562 if (getnum_withbrace("square", txt, 0, N_SQRS-1,
563 &restore_itemnum) < 0) {
564 return -1;
565 }
566 restore_item = RI_SQUARE;
567 } else {
568 printf("unknown attribute %s\n", attribute);
569 return -1;
570 }
571 return 0;
572 }
573
574 static int
restore_player_attr(const char * attribute,char * txt)575 restore_player_attr(const char *attribute, char *txt)
576 {
577 PLAY *pp;
578 int tmp;
579
580 if (play == NULL) {
581 printf("player came before numplayers.\n");
582 return -1;
583 }
584 pp = &play[restore_itemnum];
585
586 if (!strcmp(attribute, "name")) {
587 if (pp->name != NULL) {
588 printf("player has multiple names.\n");
589 return -1;
590 }
591 /* XXX should really systematize the max name length */
592 if (strlen(txt) > 256) {
593 txt[256] = 0;
594 }
595 pp->name = strdup(txt);
596 if (pp->name == NULL)
597 err(1, "strdup");
598 name_list[restore_itemnum] = pp->name;
599 } else if (!strcmp(attribute, "money")) {
600 if (getnum(attribute, txt, 0, INT_MAX, &pp->money) < 0) {
601 return -1;
602 }
603 } else if (!strcmp(attribute, "loc")) {
604 /* note: not N_SQRS-1 */
605 if (getnum(attribute, txt, 0, N_SQRS, &tmp) < 0) {
606 return -1;
607 }
608 pp->loc = tmp;
609 } else if (!strcmp(attribute, "num_gojf")) {
610 if (getnum(attribute, txt, 0, 2, &tmp) < 0) {
611 return -1;
612 }
613 pp->num_gojf = tmp;
614 } else if (!strcmp(attribute, "in_jail")) {
615 if (getnum(attribute, txt, 0, 3, &tmp) < 0) {
616 return -1;
617 }
618 pp->in_jail = tmp;
619 if (pp->in_jail > 0 && pp->loc != JAIL) {
620 printf("player escaped from jail?\n");
621 return -1;
622 }
623 } else {
624 printf("unknown attribute %s\n", attribute);
625 return -1;
626 }
627 return 0;
628 }
629
630 static int
restore_deck_attr(const char * attribute,char * txt)631 restore_deck_attr(const char *attribute, char *txt)
632 {
633 int tmp, j;
634 char *s;
635 DECK *dp;
636
637 dp = &deck[restore_itemnum];
638
639 if (!strcmp(attribute, "numcards")) {
640 if (getnum(attribute, txt, dp->num_cards, dp->num_cards,
641 &tmp) < 0) {
642 return -1;
643 }
644 } else if (!strcmp(attribute, "topcard")) {
645 if (getnum(attribute, txt, 0, dp->num_cards,
646 &dp->top_card) < 0) {
647 return -1;
648 }
649 } else if (!strcmp(attribute, "gojf_used")) {
650 if (getnum(attribute, txt, 0, 1, &tmp) < 0) {
651 return -1;
652 }
653 dp->gojf_used = tmp;
654 } else if (!strcmp(attribute, "cards")) {
655 errno = 0;
656 s = txt;
657 for (j = 0; j<dp->num_cards; j++) {
658 tmp = strtol(s, &s, 10);
659 if (tmp < 0 || tmp >= dp->num_cards) {
660 printf("cards: out of range value\n");
661 return -1;
662 }
663 dp->cards[j] = tmp;
664 }
665 if (errno) {
666 printf("cards: invalid values\n");
667 return -1;
668 }
669 } else {
670 printf("unknown attribute %s\n", attribute);
671 return -1;
672 }
673 return 0;
674 }
675
676 static int
restore_square_attr(const char * attribute,char * txt)677 restore_square_attr(const char *attribute, char *txt)
678 {
679 SQUARE *sp = &board[restore_itemnum];
680 int tmp;
681
682 if (!strcmp(attribute, "owner")) {
683 if (getnum(attribute, txt, -1, num_play-1, &tmp) < 0) {
684 return -1;
685 }
686 sp->owner = tmp;
687 if (tmp >= 0)
688 add_list(tmp, &play[tmp].own_list, restore_itemnum);
689 } else if (!strcmp(attribute, "morg")) {
690 if (sp->type != PRPTY && sp->type != RR && sp->type != UTIL) {
691 printf("unownable property is mortgaged.\n");
692 return -1;
693 }
694 if (getnum(attribute, txt, 0, 1, &tmp) < 0) {
695 return -1;
696 }
697 sp->desc->morg = tmp;
698 } else if (!strcmp(attribute, "houses")) {
699 if (sp->type != PRPTY) {
700 printf("unbuildable property has houses.\n");
701 return -1;
702 }
703 if (getnum(attribute, txt, 0, 5, &tmp) < 0) {
704 return -1;
705 }
706 sp->desc->houses = tmp;
707 } else {
708 printf("unknown attribute %s\n", attribute);
709 return -1;
710 }
711 return 0;
712 }
713
714 static int
getnum(const char * what,char * txt,int min,int max,int * ret)715 getnum(const char *what, char *txt, int min, int max, int *ret)
716 {
717 char *s;
718 long l;
719
720 errno = 0;
721 l = strtol(txt, &s, 10);
722 if (errno || strlen(s)>0) {
723 printf("%s: not a number.\n", what);
724 return -1;
725 }
726 if (l < min || l > max) {
727 printf("%s: out of range.\n", what);
728 }
729 *ret = l;
730 return 0;
731 }
732
733 static int
getnum_withbrace(const char * what,char * txt,int min,int max,int * ret)734 getnum_withbrace(const char *what, char *txt, int min, int max, int *ret)
735 {
736 char *s;
737 s = strchr(txt, ' ');
738 if (s == NULL) {
739 printf("%s: expected open brace\n", what);
740 return -1;
741 }
742 *(s++) = '\0';
743 while (*s == ' ')
744 s++;
745 if (*s != '{') {
746 printf("%s: expected open brace\n", what);
747 return -1;
748 }
749 if (s[1] != 0) {
750 printf("%s: garbage after open brace\n", what);
751 return -1;
752 }
753 return getnum(what, txt, min, max, ret);
754 }
755