|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1985 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 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. ! 16: */ ! 17: ! 18: #ifndef lint ! 19: static char sccsid[] = "@(#)answer.c 5.2 (Berkeley) 6/27/88"; ! 20: #endif /* not lint */ ! 21: ! 22: /* ! 23: * Hunt ! 24: * Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold ! 25: * San Francisco, California ! 26: */ ! 27: ! 28: # include "hunt.h" ! 29: # include <errno.h> ! 30: ! 31: # define MAXPERMACH 3 /* Max player/monitor per machine */ ! 32: ! 33: static char Ttyname[NAMELEN]; ! 34: ! 35: answer() ! 36: { ! 37: register PLAYER *pp; ! 38: register int newsock; ! 39: register FILE *tmpfd; ! 40: # ifdef MONITOR ! 41: static FLAG monitor; ! 42: # endif MONITOR ! 43: static char name[NAMELEN]; ! 44: static int socklen; ! 45: static u_long machine; ! 46: static u_long uid; ! 47: static SOCKET sockstruct; ! 48: # ifdef OLDIPC ! 49: extern SOCKET Daemon; ! 50: # endif OLDIPC ! 51: ! 52: # ifdef INTERNET ! 53: socklen = sizeof sockstruct; ! 54: # else ! 55: socklen = sizeof sockstruct - 1; ! 56: # endif INTERNET ! 57: errno = 0; ! 58: # ifndef OLDIPC ! 59: if ((newsock = accept(Socket, &sockstruct, &socklen)) < 0) ! 60: # else OLDIPC ! 61: if (accept(Socket, &sockstruct) < 0) ! 62: # endif OLDIPC ! 63: { ! 64: if (errno == EINTR) ! 65: return; ! 66: perror("accept"); ! 67: cleanup(1); ! 68: } ! 69: # ifdef OLDIPC ! 70: newsock = Socket; ! 71: Socket = socket(SOCK_STREAM, 0, (struct sockaddr *) &Daemon, ! 72: SO_ACCEPTCONN); ! 73: if (Socket < 0) { ! 74: perror("new accept socket"); ! 75: cleanup(1); ! 76: } ! 77: Sock_mask = (1 << Socket); ! 78: Fds_mask |= Sock_mask; ! 79: if (Socket >= Num_fds) ! 80: Num_fds = Socket + 1; ! 81: # endif OLDIPC ! 82: ! 83: tmpfd = fdopen(newsock, "w"); ! 84: ! 85: # ifdef INTERNET ! 86: machine = ntohl(((struct sockaddr_in *) &sockstruct)->sin_addr.s_addr); ! 87: # else INTERNET ! 88: if (machine == 0) ! 89: machine = gethostid(); ! 90: # endif INTERNET ! 91: (void) putw(getpid(), tmpfd); ! 92: (void) read(newsock, (char *) &uid, sizeof uid); ! 93: uid = ntohl(uid); ! 94: (void) read(newsock, name, NAMELEN); ! 95: (void) read(newsock, Ttyname, NAMELEN); ! 96: # ifdef MONITOR ! 97: (void) read(newsock, (char *) &monitor, sizeof monitor); ! 98: # endif MONITOR ! 99: ! 100: if (reached_limit(machine)) { ! 101: socklen = 0; ! 102: (void) write(newsock, (char *) &socklen, sizeof socklen); ! 103: (void) close(newsock); ! 104: # ifdef OLDIPC ! 105: Fds_mask &= ~(1 << newsock); ! 106: # endif OLDIPC ! 107: return; ! 108: } ! 109: ! 110: # ifdef MONITOR ! 111: if (monitor) ! 112: if (End_monitor < &Monitor[MAXMON]) ! 113: pp = End_monitor++; ! 114: else { ! 115: socklen = 0; ! 116: (void) write(newsock, (char *) &socklen, ! 117: sizeof socklen); ! 118: (void) close(newsock); ! 119: return; ! 120: } ! 121: else ! 122: # endif MONITOR ! 123: if (End_player < &Player[MAXPL]) ! 124: pp = End_player++; ! 125: else { ! 126: socklen = 0; ! 127: (void) write(newsock, (char *) &socklen, ! 128: sizeof socklen); ! 129: (void) close(newsock); ! 130: return; ! 131: } ! 132: ! 133: pp->p_ident = get_ident(machine, uid, name); ! 134: pp->p_output = tmpfd; ! 135: pp->p_death[0] = '\0'; ! 136: pp->p_fd = newsock; ! 137: pp->p_mask = (1 << pp->p_fd); ! 138: # ifndef OLDIPC ! 139: Fds_mask |= pp->p_mask; ! 140: if (pp->p_fd >= Num_fds) ! 141: Num_fds = pp->p_fd + 1; ! 142: # endif OLDIPC ! 143: ! 144: pp->p_y = 0; ! 145: pp->p_x = 0; ! 146: ! 147: # ifdef MONITOR ! 148: if (monitor) ! 149: stmonitor(pp); ! 150: else ! 151: # endif MONITOR ! 152: stplayer(pp); ! 153: } ! 154: ! 155: # ifdef MONITOR ! 156: stmonitor(pp) ! 157: register PLAYER *pp; ! 158: { ! 159: register int line; ! 160: register PLAYER *npp; ! 161: ! 162: bcopy((char *) Maze, (char *) pp->p_maze, sizeof Maze); ! 163: ! 164: drawmaze(pp); ! 165: ! 166: (void) sprintf(Buf, "%5.5s%c%-10.10s", " ", stat_char(pp), ! 167: pp->p_ident->i_name); ! 168: line = STAT_MON_ROW + 1 + (pp - Monitor); ! 169: for (npp = Player; npp < End_player; npp++) { ! 170: cgoto(npp, line, STAT_NAME_COL); ! 171: outstr(npp, Buf, STAT_NAME_LEN); ! 172: } ! 173: for (npp = Monitor; npp < End_monitor; npp++) { ! 174: cgoto(npp, line, STAT_NAME_COL); ! 175: outstr(npp, Buf, STAT_NAME_LEN); ! 176: } ! 177: ! 178: sendcom(pp, REFRESH); ! 179: sendcom(pp, READY, 0); ! 180: (void) fflush(pp->p_output); ! 181: } ! 182: # endif MONITOR ! 183: ! 184: stplayer(newpp) ! 185: register PLAYER *newpp; ! 186: { ! 187: register int x, y; ! 188: register PLAYER *pp; ! 189: ! 190: Nplayer++; ! 191: ! 192: for (y = 0; y < UBOUND; y++) ! 193: for (x = 0; x < WIDTH; x++) ! 194: newpp->p_maze[y][x] = Maze[y][x]; ! 195: for ( ; y < DBOUND; y++) { ! 196: for (x = 0; x < LBOUND; x++) ! 197: newpp->p_maze[y][x] = Maze[y][x]; ! 198: for ( ; x < RBOUND; x++) ! 199: newpp->p_maze[y][x] = SPACE; ! 200: for ( ; x < WIDTH; x++) ! 201: newpp->p_maze[y][x] = Maze[y][x]; ! 202: } ! 203: for ( ; y < HEIGHT; y++) ! 204: for (x = 0; x < WIDTH; x++) ! 205: newpp->p_maze[y][x] = Maze[y][x]; ! 206: ! 207: do { ! 208: x = rand_num(WIDTH - 1) + 1; ! 209: y = rand_num(HEIGHT - 1) + 1; ! 210: } while (Maze[y][x] != SPACE); ! 211: newpp->p_over = SPACE; ! 212: newpp->p_x = x; ! 213: newpp->p_y = y; ! 214: newpp->p_undershot = FALSE; ! 215: ! 216: # ifdef START_FLYING ! 217: /* This is only for debugging */ ! 218: newpp->p_flying = rand_num(20); ! 219: newpp->p_flyx = 2 * rand_num(6) - 5; ! 220: newpp->p_flyy = 2 * rand_num(6) - 5; ! 221: newpp->p_face = FLYER; ! 222: # else START_FLYING ! 223: newpp->p_flying = -1; ! 224: rand_face(newpp); ! 225: # endif START_FLYING ! 226: newpp->p_damage = 0; ! 227: newpp->p_damcap = MAXDAM; ! 228: newpp->p_nchar = 0; ! 229: newpp->p_ncount = 0; ! 230: newpp->p_nexec = 0; ! 231: newpp->p_ammo = ISHOTS; ! 232: newpp->p_scan = -1; ! 233: newpp->p_cloak = CLOAKLEN; ! 234: newpp->p_ncshot = 0; ! 235: ! 236: do { ! 237: x = rand_num(WIDTH - 1) + 1; ! 238: y = rand_num(HEIGHT - 1) + 1; ! 239: } while (Maze[y][x] != SPACE); ! 240: Maze[y][x] = GMINE; ! 241: # ifdef MONITOR ! 242: for (pp = Monitor; pp < End_monitor; pp++) ! 243: check(pp, y, x); ! 244: # endif MONITOR ! 245: ! 246: do { ! 247: x = rand_num(WIDTH - 1) + 1; ! 248: y = rand_num(HEIGHT - 1) + 1; ! 249: } while (Maze[y][x] != SPACE); ! 250: Maze[y][x] = MINE; ! 251: # ifdef MONITOR ! 252: for (pp = Monitor; pp < End_monitor; pp++) ! 253: check(pp, y, x); ! 254: # endif MONITOR ! 255: ! 256: (void) sprintf(Buf, "%5.2f%c%-10.10s", newpp->p_ident->i_score, ! 257: stat_char(newpp), newpp->p_ident->i_name); ! 258: y = STAT_PLAY_ROW + 1 + (newpp - Player); ! 259: for (pp = Player; pp < End_player; pp++) { ! 260: if (pp != newpp) { ! 261: char smallbuf[10]; ! 262: ! 263: pp->p_ammo += NSHOTS; ! 264: newpp->p_ammo += NSHOTS; ! 265: cgoto(pp, y, STAT_NAME_COL); ! 266: outstr(pp, Buf, STAT_NAME_LEN); ! 267: (void) sprintf(smallbuf, "%3d", pp->p_ammo); ! 268: cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL); ! 269: outstr(pp, smallbuf, 3); ! 270: } ! 271: } ! 272: # ifdef MONITOR ! 273: for (pp = Monitor; pp < End_monitor; pp++) { ! 274: cgoto(pp, y, STAT_NAME_COL); ! 275: outstr(pp, Buf, STAT_NAME_LEN); ! 276: } ! 277: # endif MONITOR ! 278: ! 279: drawmaze(newpp); ! 280: drawplayer(newpp, TRUE); ! 281: look(newpp); ! 282: # ifdef START_FLYING ! 283: /* Make sure that the position you enter in will be erased */ ! 284: showexpl(newpp->p_y, newpp->p_x, FLYER); ! 285: # endif START_FLYING ! 286: sendcom(newpp, REFRESH); ! 287: sendcom(newpp, READY, 0); ! 288: (void) fflush(newpp->p_output); ! 289: } ! 290: ! 291: /* ! 292: * rand_face: ! 293: * Give the player a random facing direction ! 294: */ ! 295: rand_face(pp) ! 296: register PLAYER *pp; ! 297: { ! 298: switch (rand_num(4)) { ! 299: case 0: ! 300: pp->p_face = LEFTS; ! 301: break; ! 302: case 1: ! 303: pp->p_face = RIGHT; ! 304: break; ! 305: case 2: ! 306: pp->p_face = BELOW; ! 307: break; ! 308: case 3: ! 309: pp->p_face = ABOVE; ! 310: break; ! 311: } ! 312: } ! 313: ! 314: /* ! 315: * get_ident: ! 316: * Get the score structure of a player ! 317: */ ! 318: IDENT * ! 319: get_ident(machine, uid, name) ! 320: u_long machine; ! 321: u_long uid; ! 322: char *name; ! 323: { ! 324: register IDENT *ip; ! 325: static IDENT punt; ! 326: ! 327: for (ip = Scores; ip != NULL; ip = ip->i_next) ! 328: if (ip->i_machine == machine && ip->i_uid == uid && ! 329: strncmp(ip->i_name, name, NAMELEN) == 0) ! 330: break; ! 331: ! 332: if (ip != NULL) { ! 333: ip->i_entries++; ! 334: ip->i_score = ip->i_kills / (double) ip->i_entries; ! 335: } ! 336: else { ! 337: ip = (IDENT *) malloc(sizeof (IDENT)); ! 338: if (ip == NULL) { ! 339: /* Fourth down, time to punt */ ! 340: ip = &punt; ! 341: } ! 342: ip->i_machine = machine; ! 343: ip->i_uid = uid; ! 344: strncpy(ip->i_name, name, NAMELEN); ! 345: ip->i_kills = 0; ! 346: ip->i_entries = 1; ! 347: ip->i_score = 0; ! 348: ip->i_next = Scores; ! 349: Scores = ip; ! 350: } ! 351: ! 352: return ip; ! 353: } ! 354: ! 355: /* ! 356: * reached_limit: ! 357: * Returns whether the limit of x persons per machine has been reached ! 358: */ ! 359: reached_limit(machine) ! 360: u_long machine; ! 361: { ! 362: register PLAYER *pp; ! 363: register int count; ! 364: ! 365: count = 0; ! 366: for (pp = Player; pp < End_player; pp++) ! 367: if (pp->p_ident->i_machine == machine) ! 368: count++; ! 369: for (pp = Monitor; pp < End_monitor; pp++) ! 370: if (pp->p_ident->i_machine == machine) ! 371: count++; ! 372: return count >= MAXPERMACH; ! 373: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.