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