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