cleanup, add manual page
[unix-history] / usr / src / games / monop / execute.c
CommitLineData
49e00f3c 1/*
d99e6414 2 * Copyright (c) 1980 Regents of the University of California.
49e00f3c
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
d99e6414
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
49e00f3c
KB
16 */
17
18#ifndef lint
d99e6414 19static char sccsid[] = "@(#)execute.c 5.3 (Berkeley) %G%";
49e00f3c
KB
20#endif /* not lint */
21
22# include "monop.ext"
23# include <sys/types.h>
24# include <sys/stat.h>
25# include <sys/time.h>
26
27# define SEGSIZE 8192
28
29typedef struct stat STAT;
30typedef struct tm TIME;
31
32extern char etext[], /* end of text space */
33 rub();
34
35static char buf[257],
36 *yn_only[] = { "yes", "no"};
37
38static bool new_play; /* set if move on to new player */
39
40/*
41 * This routine executes the given command by index number
42 */
43execute(com_num)
44reg int com_num; {
45
46 new_play = FALSE; /* new_play is true if fixing */
47 (*func[com_num])();
48 notify();
49 force_morg();
50 if (new_play)
51 next_play();
52 else if (num_doub)
53 printf("%s rolled doubles. Goes again\n", cur_p->name);
54}
55/*
56 * This routine moves a piece around.
57 */
58do_move() {
59
60 reg int r1, r2;
61 reg bool was_jail;
62
63 new_play = was_jail = FALSE;
64 printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
65 if (cur_p->loc == JAIL) {
66 was_jail++;
67 if (!move_jail(r1, r2)) {
68 new_play++;
69 goto ret;
70 }
71 }
72 else {
73 if (r1 == r2 && ++num_doub == 3) {
74 printf("That's 3 doubles. You go to jail\n");
75 goto_jail();
76 new_play++;
77 goto ret;
78 }
79 move(r1+r2);
80 }
81 if (r1 != r2 || was_jail)
82 new_play++;
83ret:
84 return;
85}
86/*
87 * This routine moves a normal move
88 */
89move(rl)
90reg int rl; {
91
92 reg int old_loc;
93
94 old_loc = cur_p->loc;
95 cur_p->loc = (cur_p->loc + rl) % N_SQRS;
96 if (cur_p->loc < old_loc && rl > 0) {
97 cur_p->money += 200;
98 printf("You pass %s and get $200\n", board[0].name);
99 }
100 show_move();
101}
102/*
103 * This routine shows the results of a move
104 */
105show_move() {
106
107 reg SQUARE *sqp;
108
109 sqp = &board[cur_p->loc];
110 printf("That puts you on %s\n", sqp->name);
111 switch (sqp->type) {
112 case SAFE:
113 printf("That is a safe place\n");
114 break;
115 case CC:
2497392a 116 cc(); break;
49e00f3c 117 case CHANCE:
2497392a
KB
118 chance(); break;
119 case INC_TAX:
120 inc_tax(); break;
121 case GOTO_J:
122 goto_jail(); break;
123 case LUX_TAX:
124 lux_tax(); break;
49e00f3c
KB
125 case PRPTY:
126 case RR:
127 case UTIL:
128 if (sqp->owner < 0) {
129 printf("That would cost $%d\n", sqp->cost);
130 if (getyn("Do you want to buy? ") == 0) {
131 buy(player, sqp);
132 cur_p->money -= sqp->cost;
133 }
134 else if (num_play > 2)
135 bid(sqp);
136 }
137 else if (sqp->owner == player)
138 printf("You own it.\n");
139 else
140 rent(sqp);
141 }
142}
143/*
144 * This routine saves the current game for use at a later date
145 */
146save() {
147
148 reg char *sp;
149 reg int outf, num;
150 TIME tme, *tp;
151 int *dat_end, junk[18];
152 unsgn start, end;
153
154 tp = &tme;
155 printf("Which file do you wish to save it in? ");
156 sp = buf;
157 while ((*sp++=getchar()) != '\n')
158 continue;
159 *--sp = '\0';
160
161 /*
162 * check for existing files, and confirm overwrite if needed
163 */
164
165 if (stat(buf, junk) > -1
166 && getyn("File exists. Do you wish to overwrite? ", yn_only) > 0)
167 return;
168
169 if ((outf=creat(buf, 0644)) < 0) {
170 perror(buf);
171 return;
172 }
173 printf("\"%s\" ", buf);
174 time(tp); /* get current time */
175 strcpy(buf, ctime(tp));
176 for (sp = buf; *sp != '\n'; sp++)
177 continue;
178 *sp = '\0';
179# if 0
180 start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
181# else
182 start = 0;
183# endif
184 end = sbrk(0);
185 while (start < end) { /* write out entire data space */
186 num = start + 16 * 1024 > end ? end - start : 16 * 1024;
187 write(outf, start, num);
188 start += num;
189 }
190 close(outf);
191 printf("[%s]\n", buf);
192}
193/*
194 * This routine restores an old game from a file
195 */
196restore() {
197
198 reg char *sp;
199
200 printf("Which file do you wish to restore from? ");
201 for (sp = buf; (*sp=getchar()) != '\n'; sp++)
202 continue;
203 *sp = '\0';
204 rest_f(buf);
205}
206/*
207 * This does the actual restoring. It returns TRUE if the
208 * backup was successful, else false.
209 */
210rest_f(file)
211reg char *file; {
212
213 reg char *sp;
214 reg int inf, num;
215 char buf[80];
216 unsgn start, end;
217 STAT sbuf;
218
219 if ((inf=open(file, 0)) < 0) {
220 perror(file);
221 return FALSE;
222 }
223 printf("\"%s\" ", file);
224 if (fstat(inf, &sbuf) < 0) { /* get file stats */
225 perror(file);
226 exit(1);
227 }
228# if 0
229 start = (((int) etext + (SEGSIZE-1)) / SEGSIZE ) * SEGSIZE;
230# else
231 start = 0;
232# endif
233 brk(end = start + sbuf.st_size);
234 while (start < end) { /* write out entire data space */
235 num = start + 16 * 1024 > end ? end - start : 16 * 1024;
236 read(inf, start, num);
237 start += num;
238 }
239 close(inf);
240 strcpy(buf, ctime(sbuf.st_mtime));
241 for (sp = buf; *sp != '\n'; sp++)
242 continue;
243 *sp = '\0';
244 printf("[%s]\n", buf);
245 return TRUE;
246}