xref: /netbsd-src/games/monop/execute.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: execute.c,v 1.21 2008/02/24 06:12:49 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.21 2008/02/24 06:12:49 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 	int ret = 0;
366 
367 	inf = fopen(file, "r");
368 	if (inf == NULL) {
369 		warn("%s", file);
370 		return -1;
371 	}
372 	printf("\"%s\" ", file);
373 	if (fstat(fileno(inf), &sbuf) < 0) {
374 		err(1, "%s: fstat", file);
375 	}
376 
377 	/* Clear the game state to prevent brokenness on misordered files. */
378 	reset_game();
379 
380 	/* Reset the parser */
381 	restore_reset();
382 
383 	/* Note: can't use buf[], file might point at it. (Lame...) */
384 	while (fgets(readbuf, sizeof(readbuf), inf)) {
385 		/*
386 		 * The input buffer is long enough to handle anything
387 		 * that's supposed to be in the output buffer, so if
388 		 * we get a partial line, complain.
389 		 */
390 		sp = strchr(readbuf, '\n');
391 		if (sp == NULL) {
392 			printf("file is corrupt: long lines.\n");
393 			ret = -1;
394 			break;
395 		}
396 		*sp = '\0';
397 
398 		if (restore_parseline(readbuf)) {
399 			ret = -1;
400 			break;
401 		}
402 	}
403 
404 	if (ferror(inf))
405 		warnx("%s: read error", file);
406 	fclose(inf);
407 
408 	if (ret < 0)
409 		return -1;
410 
411 	name_list[num_play] = "done";
412 
413 	if (play == NULL || cur_p == NULL || num_play < 2) {
414 		printf("save file is incomplete.\n");
415 		return -1;
416 	}
417 
418 	/*
419 	 * We could at this point crosscheck the following:
420 	 *    - there are only two GOJF cards floating around
421 	 *    - total number of houses and hotels does not exceed maximums
422 	 *    - no props are both built and mortgaged
423 	 * but for now we don't.
424 	 */
425 
426 	strcpy(xbuf, ctime(&sbuf.st_mtime));
427 	for (sp = xbuf; *sp != '\n'; sp++)
428 		continue;
429 	*sp = '\0';
430 	printf("[%s]\n", xbuf);
431 	return 0;
432 }
433 
434 /*
435  * State of the restore parser
436  */
437 static int restore_version;
438 static enum {
439 	RI_NONE,
440 	RI_PLAYER,
441 	RI_DECK,
442 	RI_SQUARE
443 } restore_item;
444 static int restore_itemnum;
445 
446 /*
447  * Reset the restore parser
448  */
449 static void
450 restore_reset(void)
451 {
452 	restore_version = -1;
453 	restore_item = RI_NONE;
454 	restore_itemnum = -1;
455 }
456 
457 /*
458  * Handle one line of the save file
459  */
460 static int
461 restore_parseline(char *txt)
462 {
463 	char *attribute;
464 	char *s;
465 
466 	if (restore_version < 0) {
467 		/* Haven't seen the header yet. Demand it right away. */
468 		if (!strncmp(txt, "NetBSD monop format v", 21)) {
469 			return getnum("format version", txt+21,
470 				      MIN_FORMAT_VERSION,
471 				      MAX_FORMAT_VERSION,
472 				      &restore_version);
473 		}
474 		printf("file is not a monop save file.\n");
475 		return -1;
476 	}
477 
478 	/* Check for lines that are right braces. */
479 	if (!strcmp(txt, "}")) {
480 		if (restore_item == RI_NONE) {
481 			printf("mismatched close brace.\n");
482 			return -1;
483 		}
484 		restore_item = RI_NONE;
485 		restore_itemnum = -1;
486 		return 0;
487 	}
488 
489 	/* Any other line must begin with a word, which is the attribute. */
490 	s = txt;
491 	while (*s==' ')
492 		s++;
493 	attribute = s;
494 	s = strchr(attribute, ' ');
495 	if (s == NULL) {
496 		printf("file is corrupt: attribute %s lacks value.\n",
497 		    attribute);
498 		return -1;
499 	}
500 	*(s++) = '\0';
501 	while (*s==' ')
502 		s++;
503 	/* keep the remaining text for further handling */
504 	txt = s;
505 
506 	switch (restore_item) {
507 	    case RI_NONE:
508 		/* toplevel attributes */
509 		return restore_toplevel_attr(attribute, txt);
510 
511 	    case RI_PLAYER:
512 		/* player attributes */
513 		return restore_player_attr(attribute, txt);
514 
515 	    case RI_DECK:
516 		/* deck attributes */
517 		return restore_deck_attr(attribute, txt);
518 
519 	    case RI_SQUARE:
520 		/* board square attributes */
521 		return restore_square_attr(attribute, txt);
522 	}
523 	/* NOTREACHED */
524 	printf("internal logic error\n");
525 	return -1;
526 }
527 
528 static int
529 restore_toplevel_attr(const char *attribute, char *txt)
530 {
531 	if (!strcmp(attribute, "time")) {
532 		/* nothing */
533 	} else if (!strcmp(attribute, "numplayers")) {
534 		if (getnum("numplayers", txt, 2, MAX_PL, &num_play) < 0) {
535 			return -1;
536 		}
537 		if (play != NULL) {
538 			printf("numplayers: multiple settings\n");
539 			return -1;
540 		}
541 		play = calloc((size_t)num_play, sizeof(play[0]));
542 		if (play == NULL) {
543 			err(1, "calloc");
544 		}
545 	} else if (!strcmp(attribute, "currentplayer")) {
546 		if (getnum("currentplayer", txt, 0, num_play-1, &player) < 0) {
547 			return -1;
548 		}
549 		if (play == NULL) {
550 			printf("currentplayer: before numplayers\n");
551 			return -1;
552 		}
553 		cur_p = &play[player];
554 	} else if (!strcmp(attribute, "doubles")) {
555 		if (getnum("doubles", txt, 0, 2, &num_doub) < 0) {
556 			return -1;
557 		}
558 	} else if (!strcmp(attribute, "player")) {
559 		if (getnum_withbrace("player", txt, 0, num_play-1,
560 		    &restore_itemnum) < 0) {
561 			return -1;
562 		}
563 		restore_item = RI_PLAYER;
564 	} else if (!strcmp(attribute, "deck")) {
565 		if (getnum_withbrace("deck", txt, 0, 1,
566 		    &restore_itemnum) < 0) {
567 			return -1;
568 		}
569 		restore_item = RI_DECK;
570 	} else if (!strcmp(attribute, "square")) {
571 		if (getnum_withbrace("square", txt, 0, N_SQRS-1,
572 		    &restore_itemnum) < 0) {
573 			return -1;
574 		}
575 		restore_item = RI_SQUARE;
576 	} else {
577 		printf("unknown attribute %s\n", attribute);
578 		return -1;
579 	}
580 	return 0;
581 }
582 
583 static int
584 restore_player_attr(const char *attribute, char *txt)
585 {
586 	PLAY *pp;
587 	int tmp;
588 
589 	if (play == NULL) {
590 		printf("player came before numplayers.\n");
591 		return -1;
592 	}
593 	pp = &play[restore_itemnum];
594 
595 	if (!strcmp(attribute, "name")) {
596 		if (pp->name != NULL) {
597 			printf("player has multiple names.\n");
598 			return -1;
599 		}
600 		/* XXX should really systematize the max name length */
601 		if (strlen(txt) > 256) {
602 			txt[256] = 0;
603 		}
604 		pp->name = strdup(txt);
605 		if (pp->name == NULL)
606 			err(1, "strdup");
607 		name_list[restore_itemnum] = pp->name;
608 	} else if (!strcmp(attribute, "money")) {
609 		if (getnum(attribute, txt, 0, INT_MAX, &pp->money) < 0) {
610 			return -1;
611 		}
612 	} else if (!strcmp(attribute, "loc")) {
613 		/* note: not N_SQRS-1 */
614 		if (getnum(attribute, txt, 0, N_SQRS, &tmp) < 0) {
615 			return -1;
616 		}
617 		pp->loc = tmp;
618 	} else if (!strcmp(attribute, "num_gojf")) {
619 		if (getnum(attribute, txt, 0, 2, &tmp) < 0) {
620 			return -1;
621 		}
622 		pp->num_gojf = tmp;
623 	} else if (!strcmp(attribute, "in_jail")) {
624 		if (getnum(attribute, txt, 0, 3, &tmp) < 0) {
625 			return -1;
626 		}
627 		pp->in_jail = tmp;
628 		if (pp->in_jail > 0 && pp->loc != JAIL) {
629 			printf("player escaped from jail?\n");
630 			return -1;
631 		}
632 	} else {
633 		printf("unknown attribute %s\n", attribute);
634 		return -1;
635 	}
636 	return 0;
637 }
638 
639 static int
640 restore_deck_attr(const char *attribute, char *txt)
641 {
642 	int tmp, j;
643 	char *s;
644 	DECK *dp;
645 
646 	dp = &deck[restore_itemnum];
647 
648 	if (!strcmp(attribute, "numcards")) {
649 		if (getnum(attribute, txt, dp->num_cards, dp->num_cards,
650 		    &tmp) < 0) {
651 			return -1;
652 		}
653 	} else if (!strcmp(attribute, "topcard")) {
654 		if (getnum(attribute, txt, 0, dp->num_cards,
655 		    &dp->top_card) < 0) {
656 			return -1;
657 		}
658 	} else if (!strcmp(attribute, "gojf_used")) {
659 		if (getnum(attribute, txt, 0, 1, &tmp) < 0) {
660 			return -1;
661 		}
662 		dp->gojf_used = tmp;
663 	} else if (!strcmp(attribute, "cards")) {
664 		errno = 0;
665 		s = txt;
666 		for (j = 0; j<dp->num_cards; j++) {
667 			tmp = strtol(s, &s, 10);
668 			if (tmp < 0 || tmp >= dp->num_cards) {
669 				printf("cards: out of range value\n");
670 				return -1;
671 			}
672 			dp->cards[j] = tmp;
673 		}
674 		if (errno) {
675 			printf("cards: invalid values\n");
676 			return -1;
677 		}
678 	} else {
679 		printf("unknown attribute %s\n", attribute);
680 		return -1;
681 	}
682 	return 0;
683 }
684 
685 static int
686 restore_square_attr(const char *attribute, char *txt)
687 {
688 	SQUARE *sp = &board[restore_itemnum];
689 	int tmp;
690 
691 	if (!strcmp(attribute, "owner")) {
692 		if (getnum(attribute, txt, -1, num_play-1, &tmp) < 0) {
693 			return -1;
694 		}
695 		sp->owner = tmp;
696 		if (tmp >= 0)
697 			add_list(tmp, &play[tmp].own_list, restore_itemnum);
698 	} else if (!strcmp(attribute, "morg")) {
699 		if (sp->type != PRPTY && sp->type != RR && sp->type != UTIL) {
700 			printf("unownable property is mortgaged.\n");
701 			return -1;
702 		}
703 		if (getnum(attribute, txt, 0, 1, &tmp) < 0) {
704 			return -1;
705 		}
706 		sp->desc->morg = tmp;
707 	} else if (!strcmp(attribute, "houses")) {
708 		if (sp->type != PRPTY) {
709 			printf("unbuildable property has houses.\n");
710 			return -1;
711 		}
712 		if (getnum(attribute, txt, 0, 5, &tmp) < 0) {
713 			return -1;
714 		}
715 		sp->desc->houses = tmp;
716 	} else {
717 		printf("unknown attribute %s\n", attribute);
718 		return -1;
719 	}
720 	return 0;
721 }
722 
723 static int
724 getnum(const char *what, char *txt, int min, int max, int *ret)
725 {
726 	char *s;
727 	long l;
728 
729 	errno = 0;
730 	l = strtol(txt, &s, 10);
731 	if (errno || strlen(s)>0) {
732 		printf("%s: not a number.\n", what);
733 		return -1;
734 	}
735 	if (l < min || l > max) {
736 		printf("%s: out of range.\n", what);
737 	}
738 	*ret = l;
739 	return 0;
740 }
741 
742 static int
743 getnum_withbrace(const char *what, char *txt, int min, int max, int *ret)
744 {
745 	char *s;
746 	s = strchr(txt, ' ');
747 	if (s == NULL) {
748 		printf("%s: expected open brace\n", what);
749 		return -1;
750 	}
751 	*(s++) = '\0';
752 	while (*s == ' ')
753 		s++;
754 	if (*s != '{') {
755 		printf("%s: expected open brace\n", what);
756 		return -1;
757 	}
758 	if (s[1] != 0) {
759 		printf("%s: garbage after open brace\n", what);
760 		return -1;
761 	}
762 	return getnum(what, txt, min, max, ret);
763 }
764