]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - monop/execute.c
Add use of `const' where appropriate to the games.
[bsdgames-darwin.git] / monop / execute.c
1 /* $NetBSD: execute.c,v 1.7 1999/09/08 21:17:51 jsm Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)execute.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: execute.c,v 1.7 1999/09/08 21:17:51 jsm Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "monop.ext"
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/time.h>
52
53 #define SEGSIZE 8192
54
55 typedef struct stat STAT;
56 typedef struct tm TIME;
57
58 static char buf[257];
59
60 static bool new_play; /* set if move on to new player */
61
62 static void show_move __P((void));
63
64 /*
65 * This routine executes the given command by index number
66 */
67 void
68 execute(com_num)
69 int com_num;
70 {
71 new_play = FALSE; /* new_play is true if fixing */
72 (*func[com_num])();
73 notify();
74 force_morg();
75 if (new_play)
76 next_play();
77 else if (num_doub)
78 printf("%s rolled doubles. Goes again\n", cur_p->name);
79 }
80
81 /*
82 * This routine moves a piece around.
83 */
84 void
85 do_move()
86 {
87 int r1, r2;
88 bool was_jail;
89
90 new_play = was_jail = FALSE;
91 printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6));
92 if (cur_p->loc == JAIL) {
93 was_jail++;
94 if (!move_jail(r1, r2)) {
95 new_play++;
96 goto ret;
97 }
98 }
99 else {
100 if (r1 == r2 && ++num_doub == 3) {
101 printf("That's 3 doubles. You go to jail\n");
102 goto_jail();
103 new_play++;
104 goto ret;
105 }
106 move(r1+r2);
107 }
108 if (r1 != r2 || was_jail)
109 new_play++;
110 ret:
111 return;
112 }
113
114 /*
115 * This routine moves a normal move
116 */
117 void
118 move(rl)
119 int rl;
120 {
121 int old_loc;
122
123 old_loc = cur_p->loc;
124 cur_p->loc = (cur_p->loc + rl) % N_SQRS;
125 if (cur_p->loc < old_loc && rl > 0) {
126 cur_p->money += 200;
127 printf("You pass %s and get $200\n", board[0].name);
128 }
129 show_move();
130 }
131
132 /*
133 * This routine shows the results of a move
134 */
135 static void
136 show_move()
137 {
138 SQUARE *sqp;
139
140 sqp = &board[cur_p->loc];
141 printf("That puts you on %s\n", sqp->name);
142 switch (sqp->type) {
143 case SAFE:
144 printf("That is a safe place\n");
145 break;
146 case CC:
147 cc(); break;
148 case CHANCE:
149 chance(); break;
150 case INC_TAX:
151 inc_tax(); break;
152 case GOTO_J:
153 goto_jail(); break;
154 case LUX_TAX:
155 lux_tax(); break;
156 case PRPTY:
157 case RR:
158 case UTIL:
159 if (sqp->owner < 0) {
160 printf("That would cost $%d\n", sqp->cost);
161 if (getyn("Do you want to buy? ") == 0) {
162 buy(player, sqp);
163 cur_p->money -= sqp->cost;
164 }
165 else if (num_play > 2)
166 bid();
167 }
168 else if (sqp->owner == player)
169 printf("You own it.\n");
170 else
171 rent(sqp);
172 }
173 }
174
175 /*
176 * This routine saves the current game for use at a later date
177 */
178 void
179 save()
180 {
181 char *sp;
182 int outf, num;
183 time_t t;
184 struct stat sb;
185 char *start, *end;
186
187 printf("Which file do you wish to save it in? ");
188 sp = buf;
189 while ((*sp++=getchar()) != '\n')
190 continue;
191 *--sp = '\0';
192
193 /*
194 * check for existing files, and confirm overwrite if needed
195 */
196
197 if (stat(buf, &sb) > -1
198 && getyn("File exists. Do you wish to overwrite? ") > 0)
199 return;
200
201 if ((outf=creat(buf, 0644)) < 0) {
202 perror(buf);
203 return;
204 }
205 printf("\"%s\" ", buf);
206 time(&t); /* get current time */
207 strcpy(buf, ctime(&t));
208 for (sp = buf; *sp != '\n'; sp++)
209 continue;
210 *sp = '\0';
211 start = 0;
212 end = sbrk(0);
213 while (start < end) { /* write out entire data space */
214 num = start + 16 * 1024 > end ? end - start : 16 * 1024;
215 write(outf, start, num);
216 start += num;
217 }
218 close(outf);
219 printf("[%s]\n", buf);
220 }
221
222 /*
223 * This routine restores an old game from a file
224 */
225 void
226 restore()
227 {
228 char *sp;
229
230 printf("Which file do you wish to restore from? ");
231 for (sp = buf; (*sp=getchar()) != '\n'; sp++)
232 continue;
233 *sp = '\0';
234 rest_f(buf);
235 }
236
237 /*
238 * This does the actual restoring. It returns TRUE if the
239 * backup was successful, else false.
240 */
241 int
242 rest_f(file)
243 const char *file;
244 {
245 char *sp;
246 int inf, num;
247 char buf[80];
248 char *start, *end;
249 STAT sbuf;
250
251 if ((inf=open(file, O_RDONLY)) < 0) {
252 perror(file);
253 return FALSE;
254 }
255 printf("\"%s\" ", file);
256 if (fstat(inf, &sbuf) < 0) { /* get file stats */
257 perror(file);
258 exit(1);
259 }
260 start = 0;
261 brk(end = start + sbuf.st_size);
262 while (start < end) { /* write out entire data space */
263 num = start + 16 * 1024 > end ? end - start : 16 * 1024;
264 read(inf, start, num);
265 start += num;
266 }
267 close(inf);
268 strcpy(buf, ctime(&sbuf.st_mtime));
269 for (sp = buf; *sp != '\n'; sp++)
270 continue;
271 *sp = '\0';
272 printf("[%s]\n", buf);
273 return TRUE;
274 }