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