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