2012-06-19 09:30:43 +04:00
|
|
|
/* $NetBSD: execute.c,v 1.22 2012/06/19 05:35:32 dholland Exp $ */
|
1995-03-23 11:28:00 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-03-23 11:28:00 +03:00
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:36:50 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-12 21:45:06 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1995-03-23 11:28:00 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)execute.c 8.1 (Berkeley) 5/31/93";
|
|
|
|
#else
|
2012-06-19 09:30:43 +04:00
|
|
|
__RCSID("$NetBSD: execute.c,v 1.22 2012/06/19 05:35:32 dholland Exp $");
|
1995-03-23 11:28:00 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1997-10-12 21:45:06 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2008-02-24 04:30:56 +03:00
|
|
|
#include <limits.h>
|
1997-10-12 21:45:06 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
2001-08-29 22:23:44 +04:00
|
|
|
#include <time.h>
|
2008-02-24 04:30:56 +03:00
|
|
|
#include <errno.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2008-02-24 04:57:34 +03:00
|
|
|
#include "deck.h"
|
|
|
|
#include "monop.h"
|
|
|
|
|
2008-02-24 04:30:56 +03:00
|
|
|
#define MIN_FORMAT_VERSION 1
|
|
|
|
#define CUR_FORMAT_VERSION 1
|
|
|
|
#define MAX_FORMAT_VERSION 1
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
typedef struct stat STAT;
|
|
|
|
typedef struct tm TIME;
|
|
|
|
|
1997-10-12 21:45:06 +04:00
|
|
|
static char buf[257];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
static bool new_play; /* set if move on to new player */
|
|
|
|
|
2004-01-27 23:30:28 +03:00
|
|
|
static void show_move(void);
|
1997-10-12 21:45:06 +04:00
|
|
|
|
2008-02-24 04:30:56 +03:00
|
|
|
static void restore_reset(void);
|
|
|
|
static int restore_parseline(char *txt);
|
|
|
|
static int restore_toplevel_attr(const char *attribute, char *txt);
|
|
|
|
static int restore_player_attr(const char *attribute, char *txt);
|
|
|
|
static int restore_deck_attr(const char *attribute, char *txt);
|
|
|
|
static int restore_square_attr(const char *attribute, char *txt);
|
|
|
|
static int getnum(const char *what, char *txt, int min, int max, int *ret);
|
|
|
|
static int getnum_withbrace(const char *what, char *txt, int min, int max,
|
|
|
|
int *ret);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* This routine executes the given command by index number
|
|
|
|
*/
|
1997-10-12 21:45:06 +04:00
|
|
|
void
|
2012-06-19 09:30:43 +04:00
|
|
|
execute(int com_num)
|
1997-10-12 21:45:06 +04:00
|
|
|
{
|
1993-03-21 12:45:37 +03:00
|
|
|
new_play = FALSE; /* new_play is true if fixing */
|
|
|
|
(*func[com_num])();
|
|
|
|
notify();
|
|
|
|
force_morg();
|
|
|
|
if (new_play)
|
|
|
|
next_play();
|
|
|
|
else if (num_doub)
|
|
|
|
printf("%s rolled doubles. Goes again\n", cur_p->name);
|
|
|
|
}
|
1999-08-21 14:40:03 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* This routine moves a piece around.
|
|
|
|
*/
|
1997-10-12 21:45:06 +04:00
|
|
|
void
|
2012-06-19 09:30:43 +04:00
|
|
|
do_move(void)
|
1997-10-12 21:45:06 +04:00
|
|
|
{
|
1999-08-21 14:40:03 +04:00
|
|
|
int r1, r2;
|
|
|
|
bool was_jail;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
new_play = was_jail = FALSE;
|
|
|
|
printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
|
|
|
|
if (cur_p->loc == JAIL) {
|
|
|
|
was_jail++;
|
|
|
|
if (!move_jail(r1, r2)) {
|
|
|
|
new_play++;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (r1 == r2 && ++num_doub == 3) {
|
|
|
|
printf("That's 3 doubles. You go to jail\n");
|
|
|
|
goto_jail();
|
|
|
|
new_play++;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
move(r1+r2);
|
|
|
|
}
|
|
|
|
if (r1 != r2 || was_jail)
|
|
|
|
new_play++;
|
|
|
|
ret:
|
|
|
|
return;
|
|
|
|
}
|
1999-08-21 14:40:03 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* This routine moves a normal move
|
|
|
|
*/
|
1997-10-12 21:45:06 +04:00
|
|
|
void
|
2012-06-19 09:30:43 +04:00
|
|
|
move(int rl)
|
1997-10-12 21:45:06 +04:00
|
|
|
{
|
1999-08-21 14:40:03 +04:00
|
|
|
int old_loc;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
old_loc = cur_p->loc;
|
|
|
|
cur_p->loc = (cur_p->loc + rl) % N_SQRS;
|
|
|
|
if (cur_p->loc < old_loc && rl > 0) {
|
|
|
|
cur_p->money += 200;
|
|
|
|
printf("You pass %s and get $200\n", board[0].name);
|
|
|
|
}
|
|
|
|
show_move();
|
|
|
|
}
|
1999-08-21 14:40:03 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* This routine shows the results of a move
|
|
|
|
*/
|
1997-10-12 21:45:06 +04:00
|
|
|
static void
|
2012-06-19 09:30:43 +04:00
|
|
|
show_move(void)
|
1997-10-12 21:45:06 +04:00
|
|
|
{
|
1999-08-21 14:40:03 +04:00
|
|
|
SQUARE *sqp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
sqp = &board[cur_p->loc];
|
|
|
|
printf("That puts you on %s\n", sqp->name);
|
|
|
|
switch (sqp->type) {
|
|
|
|
case SAFE:
|
|
|
|
printf("That is a safe place\n");
|
|
|
|
break;
|
|
|
|
case CC:
|
2008-02-19 12:45:02 +03:00
|
|
|
cc();
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case CHANCE:
|
2008-02-19 12:45:02 +03:00
|
|
|
chance();
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case INC_TAX:
|
2008-02-19 12:45:02 +03:00
|
|
|
inc_tax();
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case GOTO_J:
|
2008-02-19 12:45:02 +03:00
|
|
|
goto_jail();
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case LUX_TAX:
|
2008-02-19 12:45:02 +03:00
|
|
|
lux_tax();
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case PRPTY:
|
|
|
|
case RR:
|
|
|
|
case UTIL:
|
|
|
|
if (sqp->owner < 0) {
|
|
|
|
printf("That would cost $%d\n", sqp->cost);
|
|
|
|
if (getyn("Do you want to buy? ") == 0) {
|
|
|
|
buy(player, sqp);
|
|
|
|
cur_p->money -= sqp->cost;
|
|
|
|
}
|
|
|
|
else if (num_play > 2)
|
1997-10-12 21:45:06 +04:00
|
|
|
bid();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
else if (sqp->owner == player)
|
|
|
|
printf("You own it.\n");
|
|
|
|
else
|
|
|
|
rent(sqp);
|
|
|
|
}
|
|
|
|
}
|
1999-08-21 14:40:03 +04:00
|
|
|
|
2008-02-24 04:30:56 +03:00
|
|
|
/*
|
|
|
|
* Reset the game state.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
reset_game(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < N_SQRS; i++) {
|
|
|
|
board[i].owner = -1;
|
|
|
|
if (board[i].type == PRPTY) {
|
|
|
|
board[i].desc->morg = 0;
|
|
|
|
board[i].desc->houses = 0;
|
|
|
|
} else if (board[i].type == RR || board[i].type == UTIL) {
|
|
|
|
board[i].desc->morg = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
deck[i].top_card = 0;
|
|
|
|
deck[i].gojf_used = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (play) {
|
|
|
|
for (i = 0; i < num_play; i++) {
|
|
|
|
free(play[i].name);
|
|
|
|
play[i].name = NULL;
|
|
|
|
}
|
|
|
|
free(play);
|
|
|
|
play = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_PL+2; i++) {
|
|
|
|
name_list[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_p = NULL;
|
|
|
|
num_play = 0;
|
|
|
|
player = 0;
|
|
|
|
num_doub = 0;
|
|
|
|
fixing = FALSE;
|
|
|
|
trading = FALSE;
|
|
|
|
told_em = FALSE;
|
|
|
|
spec = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* This routine saves the current game for use at a later date
|
|
|
|
*/
|
1997-10-12 21:45:06 +04:00
|
|
|
void
|
2012-06-19 09:30:43 +04:00
|
|
|
save(void)
|
1997-10-12 21:45:06 +04:00
|
|
|
{
|
1999-08-21 14:40:03 +04:00
|
|
|
char *sp;
|
2008-02-24 04:30:56 +03:00
|
|
|
FILE *outf;
|
1999-08-21 14:40:03 +04:00
|
|
|
time_t t;
|
|
|
|
struct stat sb;
|
2008-02-24 04:30:56 +03:00
|
|
|
int i, j;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
printf("Which file do you wish to save it in? ");
|
2008-02-24 04:30:56 +03:00
|
|
|
fgets(buf, sizeof(buf), stdin);
|
|
|
|
if (feof(stdin))
|
|
|
|
return;
|
|
|
|
sp = strchr(buf, '\n');
|
|
|
|
if (sp)
|
|
|
|
*sp = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check for existing files, and confirm overwrite if needed
|
|
|
|
*/
|
|
|
|
|
2008-02-24 04:30:56 +03:00
|
|
|
if (stat(buf, &sb) == 0
|
1997-10-12 21:45:06 +04:00
|
|
|
&& getyn("File exists. Do you wish to overwrite? ") > 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
|
2008-02-24 04:30:56 +03:00
|
|
|
outf = fopen(buf, "w");
|
|
|
|
if (outf == NULL) {
|
2008-02-19 12:30:26 +03:00
|
|
|
warn("%s", buf);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("\"%s\" ", buf);
|
|
|
|
time(&t); /* get current time */
|
2008-02-24 04:30:56 +03:00
|
|
|
|
|
|
|
/* Header */
|
|
|
|
fprintf(outf, "NetBSD monop format v%d\n", CUR_FORMAT_VERSION);
|
|
|
|
fprintf(outf, "time %s", ctime(&t)); /* ctime includes a \n */
|
|
|
|
fprintf(outf, "numplayers %d\n", num_play);
|
|
|
|
fprintf(outf, "currentplayer %d\n", player);
|
|
|
|
fprintf(outf, "doubles %d\n", num_doub);
|
|
|
|
|
|
|
|
/* Players */
|
|
|
|
for (i = 0; i < num_play; i++) {
|
|
|
|
fprintf(outf, "player %d {\n", i);
|
|
|
|
fprintf(outf, " name %s\n", name_list[i]);
|
|
|
|
fprintf(outf, " money %d\n", play[i].money);
|
|
|
|
fprintf(outf, " loc %d\n", play[i].loc);
|
|
|
|
fprintf(outf, " num_gojf %d\n", play[i].num_gojf);
|
|
|
|
fprintf(outf, " in_jail %d\n", play[i].in_jail);
|
|
|
|
fprintf(outf, "}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decks */
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
fprintf(outf, "deck %d {\n", i);
|
|
|
|
fprintf(outf, " numcards %d\n", deck[i].num_cards);
|
|
|
|
fprintf(outf, " topcard %d\n", deck[i].top_card);
|
|
|
|
fprintf(outf, " gojf_used %d\n", deck[i].gojf_used);
|
2008-02-24 05:43:18 +03:00
|
|
|
fprintf(outf, " cards");
|
2008-02-24 04:30:56 +03:00
|
|
|
for (j = 0; j < deck[i].num_cards; j++)
|
2008-02-24 05:43:18 +03:00
|
|
|
fprintf(outf, " %d", deck[i].cards[j]);
|
2008-02-24 04:30:56 +03:00
|
|
|
fprintf(outf, "\n");
|
|
|
|
fprintf(outf, "}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Board */
|
|
|
|
for (i = 0; i < N_SQRS; i++) {
|
|
|
|
fprintf(outf, "square %d {\n", i);
|
|
|
|
fprintf(outf, "owner %d\n", board[i].owner);
|
|
|
|
if (board[i].owner < 0) {
|
|
|
|
/* nothing */
|
|
|
|
} else if (board[i].type == PRPTY) {
|
|
|
|
fprintf(outf, "morg %d\n", board[i].desc->morg);
|
|
|
|
fprintf(outf, "houses %d\n", board[i].desc->houses);
|
|
|
|
} else if (board[i].type == RR || board[i].type == UTIL) {
|
|
|
|
fprintf(outf, "morg %d\n", board[i].desc->morg);
|
|
|
|
}
|
|
|
|
fprintf(outf, "}\n");
|
|
|
|
}
|
|
|
|
if (ferror(outf) || fflush(outf))
|
|
|
|
warnx("write error");
|
|
|
|
fclose(outf);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
strcpy(buf, ctime(&t));
|
|
|
|
for (sp = buf; *sp != '\n'; sp++)
|
|
|
|
continue;
|
|
|
|
*sp = '\0';
|
|
|
|
printf("[%s]\n", buf);
|
|
|
|
}
|
1999-08-21 14:40:03 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* This routine restores an old game from a file
|
|
|
|
*/
|
1997-10-12 21:45:06 +04:00
|
|
|
void
|
2008-02-24 09:03:35 +03:00
|
|
|
restore(void)
|
1997-10-12 21:45:06 +04:00
|
|
|
{
|
1999-08-21 14:40:03 +04:00
|
|
|
char *sp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2008-02-24 09:03:35 +03:00
|
|
|
for (;;) {
|
|
|
|
printf("Which file do you wish to restore from? ");
|
|
|
|
fgets(buf, sizeof(buf), stdin);
|
|
|
|
if (feof(stdin))
|
|
|
|
return;
|
|
|
|
sp = strchr(buf, '\n');
|
|
|
|
if (sp)
|
|
|
|
*sp = '\0';
|
|
|
|
if (rest_f(buf) == 0)
|
|
|
|
break;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1999-08-21 14:40:03 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
2008-02-24 09:03:35 +03:00
|
|
|
* This does the actual restoring. It returns zero on success,
|
|
|
|
* and -1 on failure.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
1997-10-12 21:45:06 +04:00
|
|
|
int
|
2008-02-24 09:03:35 +03:00
|
|
|
rest_f(const char *file)
|
1997-10-12 21:45:06 +04:00
|
|
|
{
|
1999-08-21 14:40:03 +04:00
|
|
|
char *sp;
|
2008-02-24 04:30:56 +03:00
|
|
|
FILE *inf;
|
2008-01-28 09:16:13 +03:00
|
|
|
char xbuf[80];
|
1999-08-21 14:40:03 +04:00
|
|
|
STAT sbuf;
|
2008-02-24 04:30:56 +03:00
|
|
|
char readbuf[512];
|
2008-02-24 09:12:49 +03:00
|
|
|
int ret = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2008-02-24 04:30:56 +03:00
|
|
|
inf = fopen(file, "r");
|
|
|
|
if (inf == NULL) {
|
2008-02-19 12:30:26 +03:00
|
|
|
warn("%s", file);
|
2008-02-24 09:03:35 +03:00
|
|
|
return -1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
printf("\"%s\" ", file);
|
2008-02-24 04:30:56 +03:00
|
|
|
if (fstat(fileno(inf), &sbuf) < 0) {
|
2008-02-19 12:30:26 +03:00
|
|
|
err(1, "%s: fstat", file);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2008-02-24 04:30:56 +03:00
|
|
|
|
|
|
|
/* Clear the game state to prevent brokenness on misordered files. */
|
|
|
|
reset_game();
|
|
|
|
|
|
|
|
/* Reset the parser */
|
|
|
|
restore_reset();
|
|
|
|
|
|
|
|
/* Note: can't use buf[], file might point at it. (Lame...) */
|
|
|
|
while (fgets(readbuf, sizeof(readbuf), inf)) {
|
|
|
|
/*
|
|
|
|
* The input buffer is long enough to handle anything
|
|
|
|
* that's supposed to be in the output buffer, so if
|
|
|
|
* we get a partial line, complain.
|
|
|
|
*/
|
|
|
|
sp = strchr(readbuf, '\n');
|
|
|
|
if (sp == NULL) {
|
|
|
|
printf("file is corrupt: long lines.\n");
|
2008-02-24 09:12:49 +03:00
|
|
|
ret = -1;
|
2008-02-24 04:30:56 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*sp = '\0';
|
|
|
|
|
|
|
|
if (restore_parseline(readbuf)) {
|
2008-02-24 09:12:49 +03:00
|
|
|
ret = -1;
|
2008-02-24 04:30:56 +03:00
|
|
|
break;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2008-02-24 04:30:56 +03:00
|
|
|
|
|
|
|
if (ferror(inf))
|
|
|
|
warnx("%s: read error", file);
|
|
|
|
fclose(inf);
|
|
|
|
|
2008-02-24 09:12:49 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
|
2008-02-24 04:30:56 +03:00
|
|
|
name_list[num_play] = "done";
|
|
|
|
|
2008-02-24 09:12:49 +03:00
|
|
|
if (play == NULL || cur_p == NULL || num_play < 2) {
|
|
|
|
printf("save file is incomplete.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-24 04:30:56 +03:00
|
|
|
/*
|
|
|
|
* We could at this point crosscheck the following:
|
|
|
|
* - there are only two GOJF cards floating around
|
|
|
|
* - total number of houses and hotels does not exceed maximums
|
|
|
|
* - no props are both built and mortgaged
|
|
|
|
* but for now we don't.
|
|
|
|
*/
|
|
|
|
|
2008-01-28 09:16:13 +03:00
|
|
|
strcpy(xbuf, ctime(&sbuf.st_mtime));
|
|
|
|
for (sp = xbuf; *sp != '\n'; sp++)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
*sp = '\0';
|
2008-01-28 09:16:13 +03:00
|
|
|
printf("[%s]\n", xbuf);
|
2008-02-24 09:03:35 +03:00
|
|
|
return 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2008-02-24 04:30:56 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* State of the restore parser
|
|
|
|
*/
|
|
|
|
static int restore_version;
|
|
|
|
static enum {
|
|
|
|
RI_NONE,
|
|
|
|
RI_PLAYER,
|
|
|
|
RI_DECK,
|
2008-02-24 06:56:48 +03:00
|
|
|
RI_SQUARE
|
2008-02-24 04:30:56 +03:00
|
|
|
} restore_item;
|
|
|
|
static int restore_itemnum;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset the restore parser
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
restore_reset(void)
|
|
|
|
{
|
|
|
|
restore_version = -1;
|
|
|
|
restore_item = RI_NONE;
|
|
|
|
restore_itemnum = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle one line of the save file
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
restore_parseline(char *txt)
|
|
|
|
{
|
|
|
|
char *attribute;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (restore_version < 0) {
|
|
|
|
/* Haven't seen the header yet. Demand it right away. */
|
|
|
|
if (!strncmp(txt, "NetBSD monop format v", 21)) {
|
|
|
|
return getnum("format version", txt+21,
|
|
|
|
MIN_FORMAT_VERSION,
|
|
|
|
MAX_FORMAT_VERSION,
|
|
|
|
&restore_version);
|
|
|
|
}
|
|
|
|
printf("file is not a monop save file.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for lines that are right braces. */
|
|
|
|
if (!strcmp(txt, "}")) {
|
|
|
|
if (restore_item == RI_NONE) {
|
|
|
|
printf("mismatched close brace.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
restore_item = RI_NONE;
|
|
|
|
restore_itemnum = -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Any other line must begin with a word, which is the attribute. */
|
|
|
|
s = txt;
|
|
|
|
while (*s==' ')
|
|
|
|
s++;
|
|
|
|
attribute = s;
|
|
|
|
s = strchr(attribute, ' ');
|
|
|
|
if (s == NULL) {
|
|
|
|
printf("file is corrupt: attribute %s lacks value.\n",
|
|
|
|
attribute);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*(s++) = '\0';
|
|
|
|
while (*s==' ')
|
|
|
|
s++;
|
|
|
|
/* keep the remaining text for further handling */
|
|
|
|
txt = s;
|
|
|
|
|
|
|
|
switch (restore_item) {
|
|
|
|
case RI_NONE:
|
|
|
|
/* toplevel attributes */
|
|
|
|
return restore_toplevel_attr(attribute, txt);
|
|
|
|
|
|
|
|
case RI_PLAYER:
|
|
|
|
/* player attributes */
|
|
|
|
return restore_player_attr(attribute, txt);
|
|
|
|
|
|
|
|
case RI_DECK:
|
|
|
|
/* deck attributes */
|
|
|
|
return restore_deck_attr(attribute, txt);
|
|
|
|
|
|
|
|
case RI_SQUARE:
|
|
|
|
/* board square attributes */
|
|
|
|
return restore_square_attr(attribute, txt);
|
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
printf("internal logic error\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
restore_toplevel_attr(const char *attribute, char *txt)
|
|
|
|
{
|
|
|
|
if (!strcmp(attribute, "time")) {
|
|
|
|
/* nothing */
|
|
|
|
} else if (!strcmp(attribute, "numplayers")) {
|
|
|
|
if (getnum("numplayers", txt, 2, MAX_PL, &num_play) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (play != NULL) {
|
|
|
|
printf("numplayers: multiple settings\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2008-02-24 06:56:48 +03:00
|
|
|
play = calloc((size_t)num_play, sizeof(play[0]));
|
2008-02-24 04:30:56 +03:00
|
|
|
if (play == NULL) {
|
|
|
|
err(1, "calloc");
|
|
|
|
}
|
|
|
|
} else if (!strcmp(attribute, "currentplayer")) {
|
|
|
|
if (getnum("currentplayer", txt, 0, num_play-1, &player) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (play == NULL) {
|
|
|
|
printf("currentplayer: before numplayers\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cur_p = &play[player];
|
|
|
|
} else if (!strcmp(attribute, "doubles")) {
|
|
|
|
if (getnum("doubles", txt, 0, 2, &num_doub) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(attribute, "player")) {
|
|
|
|
if (getnum_withbrace("player", txt, 0, num_play-1,
|
|
|
|
&restore_itemnum) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
restore_item = RI_PLAYER;
|
|
|
|
} else if (!strcmp(attribute, "deck")) {
|
|
|
|
if (getnum_withbrace("deck", txt, 0, 1,
|
|
|
|
&restore_itemnum) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
restore_item = RI_DECK;
|
|
|
|
} else if (!strcmp(attribute, "square")) {
|
|
|
|
if (getnum_withbrace("square", txt, 0, N_SQRS-1,
|
|
|
|
&restore_itemnum) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
restore_item = RI_SQUARE;
|
|
|
|
} else {
|
|
|
|
printf("unknown attribute %s\n", attribute);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
restore_player_attr(const char *attribute, char *txt)
|
|
|
|
{
|
|
|
|
PLAY *pp;
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
if (play == NULL) {
|
|
|
|
printf("player came before numplayers.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pp = &play[restore_itemnum];
|
|
|
|
|
|
|
|
if (!strcmp(attribute, "name")) {
|
|
|
|
if (pp->name != NULL) {
|
|
|
|
printf("player has multiple names.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* XXX should really systematize the max name length */
|
|
|
|
if (strlen(txt) > 256) {
|
|
|
|
txt[256] = 0;
|
|
|
|
}
|
|
|
|
pp->name = strdup(txt);
|
|
|
|
if (pp->name == NULL)
|
|
|
|
err(1, "strdup");
|
|
|
|
name_list[restore_itemnum] = pp->name;
|
|
|
|
} else if (!strcmp(attribute, "money")) {
|
|
|
|
if (getnum(attribute, txt, 0, INT_MAX, &pp->money) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(attribute, "loc")) {
|
|
|
|
/* note: not N_SQRS-1 */
|
|
|
|
if (getnum(attribute, txt, 0, N_SQRS, &tmp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pp->loc = tmp;
|
|
|
|
} else if (!strcmp(attribute, "num_gojf")) {
|
|
|
|
if (getnum(attribute, txt, 0, 2, &tmp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pp->num_gojf = tmp;
|
|
|
|
} else if (!strcmp(attribute, "in_jail")) {
|
|
|
|
if (getnum(attribute, txt, 0, 3, &tmp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pp->in_jail = tmp;
|
|
|
|
if (pp->in_jail > 0 && pp->loc != JAIL) {
|
|
|
|
printf("player escaped from jail?\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("unknown attribute %s\n", attribute);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
restore_deck_attr(const char *attribute, char *txt)
|
|
|
|
{
|
|
|
|
int tmp, j;
|
|
|
|
char *s;
|
|
|
|
DECK *dp;
|
|
|
|
|
|
|
|
dp = &deck[restore_itemnum];
|
|
|
|
|
|
|
|
if (!strcmp(attribute, "numcards")) {
|
|
|
|
if (getnum(attribute, txt, dp->num_cards, dp->num_cards,
|
|
|
|
&tmp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(attribute, "topcard")) {
|
|
|
|
if (getnum(attribute, txt, 0, dp->num_cards,
|
|
|
|
&dp->top_card) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (!strcmp(attribute, "gojf_used")) {
|
|
|
|
if (getnum(attribute, txt, 0, 1, &tmp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dp->gojf_used = tmp;
|
2008-02-24 05:43:18 +03:00
|
|
|
} else if (!strcmp(attribute, "cards")) {
|
2008-02-24 04:30:56 +03:00
|
|
|
errno = 0;
|
|
|
|
s = txt;
|
|
|
|
for (j = 0; j<dp->num_cards; j++) {
|
2008-02-24 05:43:18 +03:00
|
|
|
tmp = strtol(s, &s, 10);
|
|
|
|
if (tmp < 0 || tmp >= dp->num_cards) {
|
|
|
|
printf("cards: out of range value\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
dp->cards[j] = tmp;
|
2008-02-24 04:30:56 +03:00
|
|
|
}
|
|
|
|
if (errno) {
|
2008-02-24 05:43:18 +03:00
|
|
|
printf("cards: invalid values\n");
|
2008-02-24 04:30:56 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("unknown attribute %s\n", attribute);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
restore_square_attr(const char *attribute, char *txt)
|
|
|
|
{
|
|
|
|
SQUARE *sp = &board[restore_itemnum];
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
if (!strcmp(attribute, "owner")) {
|
|
|
|
if (getnum(attribute, txt, -1, num_play-1, &tmp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sp->owner = tmp;
|
|
|
|
if (tmp >= 0)
|
|
|
|
add_list(tmp, &play[tmp].own_list, restore_itemnum);
|
|
|
|
} else if (!strcmp(attribute, "morg")) {
|
|
|
|
if (sp->type != PRPTY && sp->type != RR && sp->type != UTIL) {
|
|
|
|
printf("unownable property is mortgaged.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (getnum(attribute, txt, 0, 1, &tmp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sp->desc->morg = tmp;
|
|
|
|
} else if (!strcmp(attribute, "houses")) {
|
|
|
|
if (sp->type != PRPTY) {
|
|
|
|
printf("unbuildable property has houses.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (getnum(attribute, txt, 0, 5, &tmp) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sp->desc->houses = tmp;
|
|
|
|
} else {
|
|
|
|
printf("unknown attribute %s\n", attribute);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getnum(const char *what, char *txt, int min, int max, int *ret)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
long l;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
l = strtol(txt, &s, 10);
|
|
|
|
if (errno || strlen(s)>0) {
|
|
|
|
printf("%s: not a number.\n", what);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (l < min || l > max) {
|
|
|
|
printf("%s: out of range.\n", what);
|
|
|
|
}
|
|
|
|
*ret = l;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
getnum_withbrace(const char *what, char *txt, int min, int max, int *ret)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
s = strchr(txt, ' ');
|
|
|
|
if (s == NULL) {
|
|
|
|
printf("%s: expected open brace\n", what);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*(s++) = '\0';
|
|
|
|
while (*s == ' ')
|
|
|
|
s++;
|
|
|
|
if (*s != '{') {
|
|
|
|
printf("%s: expected open brace\n", what);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (s[1] != 0) {
|
|
|
|
printf("%s: garbage after open brace\n", what);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return getnum(what, txt, min, max, ret);
|
|
|
|
}
|