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