1 /* $NetBSD: execute.c,v 1.9 2003/04/21 01:25:27 christos 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.9 2003/04/21 01:25:27 christos 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 #include <time.h> 53 54 #define SEGSIZE 8192 55 56 typedef struct stat STAT; 57 typedef struct tm TIME; 58 59 static char buf[257]; 60 61 static bool new_play; /* set if move on to new player */ 62 extern void *heapstart; 63 64 static void show_move __P((void)); 65 66 /* 67 * This routine executes the given command by index number 68 */ 69 void 70 execute(com_num) 71 int com_num; 72 { 73 new_play = FALSE; /* new_play is true if fixing */ 74 (*func[com_num])(); 75 notify(); 76 force_morg(); 77 if (new_play) 78 next_play(); 79 else if (num_doub) 80 printf("%s rolled doubles. Goes again\n", cur_p->name); 81 } 82 83 /* 84 * This routine moves a piece around. 85 */ 86 void 87 do_move() 88 { 89 int r1, r2; 90 bool was_jail; 91 92 new_play = was_jail = FALSE; 93 printf("roll is %d, %d\n", r1=roll(1, 6), r2=roll(1, 6)); 94 if (cur_p->loc == JAIL) { 95 was_jail++; 96 if (!move_jail(r1, r2)) { 97 new_play++; 98 goto ret; 99 } 100 } 101 else { 102 if (r1 == r2 && ++num_doub == 3) { 103 printf("That's 3 doubles. You go to jail\n"); 104 goto_jail(); 105 new_play++; 106 goto ret; 107 } 108 move(r1+r2); 109 } 110 if (r1 != r2 || was_jail) 111 new_play++; 112 ret: 113 return; 114 } 115 116 /* 117 * This routine moves a normal move 118 */ 119 void 120 move(rl) 121 int rl; 122 { 123 int old_loc; 124 125 old_loc = cur_p->loc; 126 cur_p->loc = (cur_p->loc + rl) % N_SQRS; 127 if (cur_p->loc < old_loc && rl > 0) { 128 cur_p->money += 200; 129 printf("You pass %s and get $200\n", board[0].name); 130 } 131 show_move(); 132 } 133 134 /* 135 * This routine shows the results of a move 136 */ 137 static void 138 show_move() 139 { 140 SQUARE *sqp; 141 142 sqp = &board[cur_p->loc]; 143 printf("That puts you on %s\n", sqp->name); 144 switch (sqp->type) { 145 case SAFE: 146 printf("That is a safe place\n"); 147 break; 148 case CC: 149 cc(); break; 150 case CHANCE: 151 chance(); break; 152 case INC_TAX: 153 inc_tax(); break; 154 case GOTO_J: 155 goto_jail(); break; 156 case LUX_TAX: 157 lux_tax(); break; 158 case PRPTY: 159 case RR: 160 case UTIL: 161 if (sqp->owner < 0) { 162 printf("That would cost $%d\n", sqp->cost); 163 if (getyn("Do you want to buy? ") == 0) { 164 buy(player, sqp); 165 cur_p->money -= sqp->cost; 166 } 167 else if (num_play > 2) 168 bid(); 169 } 170 else if (sqp->owner == player) 171 printf("You own it.\n"); 172 else 173 rent(sqp); 174 } 175 } 176 177 /* 178 * This routine saves the current game for use at a later date 179 */ 180 void 181 save() 182 { 183 char *sp; 184 int outf, num; 185 time_t t; 186 struct stat sb; 187 char *start, *end; 188 189 printf("Which file do you wish to save it in? "); 190 sp = buf; 191 while ((*sp++=getchar()) != '\n') 192 continue; 193 *--sp = '\0'; 194 195 /* 196 * check for existing files, and confirm overwrite if needed 197 */ 198 199 if (stat(buf, &sb) > -1 200 && getyn("File exists. Do you wish to overwrite? ") > 0) 201 return; 202 203 if ((outf=creat(buf, 0644)) < 0) { 204 perror(buf); 205 return; 206 } 207 printf("\"%s\" ", buf); 208 time(&t); /* get current time */ 209 strcpy(buf, ctime(&t)); 210 for (sp = buf; *sp != '\n'; sp++) 211 continue; 212 *sp = '\0'; 213 start = heapstart; 214 end = sbrk(0); 215 while (start < end) { /* write out entire data space */ 216 num = start + 16 * 1024 > end ? end - start : 16 * 1024; 217 write(outf, start, num); 218 start += num; 219 } 220 close(outf); 221 printf("[%s]\n", buf); 222 } 223 224 /* 225 * This routine restores an old game from a file 226 */ 227 void 228 restore() 229 { 230 char *sp; 231 232 printf("Which file do you wish to restore from? "); 233 for (sp = buf; (*sp=getchar()) != '\n'; sp++) 234 continue; 235 *sp = '\0'; 236 rest_f(buf); 237 } 238 239 /* 240 * This does the actual restoring. It returns TRUE if the 241 * backup was successful, else false. 242 */ 243 int 244 rest_f(file) 245 const char *file; 246 { 247 char *sp; 248 int inf, num; 249 char buf[80]; 250 char *start, *end; 251 STAT sbuf; 252 253 if ((inf=open(file, O_RDONLY)) < 0) { 254 perror(file); 255 return FALSE; 256 } 257 printf("\"%s\" ", file); 258 if (fstat(inf, &sbuf) < 0) { /* get file stats */ 259 perror(file); 260 exit(1); 261 } 262 start = heapstart; 263 brk(end = start + sbuf.st_size); 264 while (start < end) { /* write out entire data space */ 265 num = start + 16 * 1024 > end ? end - start : 16 * 1024; 266 read(inf, start, num); 267 start += num; 268 } 269 close(inf); 270 strcpy(buf, ctime(&sbuf.st_mtime)); 271 for (sp = buf; *sp != '\n'; sp++) 272 continue; 273 *sp = '\0'; 274 printf("[%s]\n", buf); 275 return TRUE; 276 } 277