]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/driver.c
1f3b47a376ad86351d20b8162fb067e5a3b935a1
[bsdgames-darwin.git] / hunt / huntd / driver.c
1 /* $NetBSD: driver.c,v 1.2 1997/10/10 16:33:08 lukem Exp $ */
2 /*
3 * Hunt
4 * Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
5 * San Francisco, California
6 */
7
8 #include <sys/cdefs.h>
9 #ifndef lint
10 __RCSID("$NetBSD: driver.c,v 1.2 1997/10/10 16:33:08 lukem Exp $");
11 #endif /* not lint */
12
13 # include <sys/ioctl.h>
14 # include <sys/stat.h>
15 # include <sys/time.h>
16 # include <err.h>
17 # include <errno.h>
18 # include <signal.h>
19 # include <stdlib.h>
20 # include <unistd.h>
21 # include "hunt.h"
22
23 # ifndef pdp11
24 # define RN (((Seed = Seed * 11109 + 13849) >> 16) & 0xffff)
25 # else
26 # define RN ((Seed = Seed * 11109 + 13849) & 0x7fff)
27 # endif
28
29 int Seed = 0;
30
31
32 SOCKET Daemon;
33 char *First_arg; /* pointer to argv[0] */
34 char *Last_arg; /* pointer to end of argv/environ */
35 # ifdef INTERNET
36 int Test_socket; /* test socket to answer datagrams */
37 FLAG inetd_spawned; /* invoked via inetd */
38 FLAG standard_port = TRUE; /* true if listening on standard port */
39 u_short sock_port; /* port # of tcp listen socket */
40 u_short stat_port; /* port # of statistics tcp socket */
41 # define DAEMON_SIZE (sizeof Daemon)
42 # else
43 # define DAEMON_SIZE (sizeof Daemon - 1)
44 # endif
45
46 static void clear_scores __P((void));
47 static int havechar __P((PLAYER *));
48 static void init __P((void));
49 int main __P((int, char *[], char *[]));
50 static void makeboots __P((void));
51 static void send_stats __P((void));
52 static void zap __P((PLAYER *, FLAG));
53
54
55 /*
56 * main:
57 * The main program.
58 */
59 int
60 main(ac, av, ep)
61 int ac;
62 char **av, **ep;
63 {
64 PLAYER *pp;
65 int had_char;
66 # ifdef INTERNET
67 u_short msg;
68 short port_num, reply;
69 int namelen;
70 SOCKET test;
71 # endif
72 static fd_set read_fds;
73 static FLAG first = TRUE;
74 static FLAG server = FALSE;
75 extern int optind;
76 extern char *optarg;
77 int c;
78 static struct timeval linger = { 90, 0 };
79
80 First_arg = av[0];
81 if (ep == NULL || *ep == NULL)
82 ep = av + ac;
83 while (*ep)
84 ep++;
85 Last_arg = ep[-1] + strlen(ep[-1]);
86
87 while ((c = getopt(ac, av, "sp:")) != -1) {
88 switch (c) {
89 case 's':
90 server = TRUE;
91 break;
92 # ifdef INTERNET
93 case 'p':
94 standard_port = FALSE;
95 Test_port = atoi(optarg);
96 break;
97 # endif
98 default:
99 erred:
100 fprintf(stderr, "Usage: %s [-s] [-p port]\n", av[0]);
101 exit(1);
102 }
103 }
104 if (optind < ac)
105 goto erred;
106
107 init();
108
109
110 again:
111 do {
112 read_fds = Fds_mask;
113 errno = 0;
114 while (select(Num_fds, &read_fds, NULL, NULL, NULL) < 0)
115 {
116 if (errno != EINTR)
117 # ifdef LOG
118 syslog(LOG_WARNING, "select: %m");
119 # else
120 perror("select");
121 # endif
122 errno = 0;
123 }
124 Have_inp = read_fds;
125 # ifdef INTERNET
126 if (FD_ISSET(Test_socket, &read_fds)) {
127 namelen = DAEMON_SIZE;
128 port_num = htons(sock_port);
129 (void) recvfrom(Test_socket, (char *) &msg, sizeof msg,
130 0, (struct sockaddr *) &test, &namelen);
131 switch (ntohs(msg)) {
132 case C_MESSAGE:
133 if (Nplayer <= 0)
134 break;
135 reply = htons((u_short) Nplayer);
136 (void) sendto(Test_socket, (char *) &reply,
137 sizeof reply, 0,
138 (struct sockaddr *) &test, DAEMON_SIZE);
139 break;
140 case C_SCORES:
141 reply = htons(stat_port);
142 (void) sendto(Test_socket, (char *) &reply,
143 sizeof reply, 0,
144 (struct sockaddr *) &test, DAEMON_SIZE);
145 break;
146 case C_PLAYER:
147 case C_MONITOR:
148 if (msg == C_MONITOR && Nplayer <= 0)
149 break;
150 reply = htons(sock_port);
151 (void) sendto(Test_socket, (char *) &reply,
152 sizeof reply, 0,
153 (struct sockaddr *) &test, DAEMON_SIZE);
154 break;
155 }
156 }
157 # endif
158 for (;;) {
159 had_char = FALSE;
160 for (pp = Player; pp < End_player; pp++)
161 if (havechar(pp)) {
162 execute(pp);
163 pp->p_nexec++;
164 had_char++;
165 }
166 # ifdef MONITOR
167 for (pp = Monitor; pp < End_monitor; pp++)
168 if (havechar(pp)) {
169 mon_execute(pp);
170 pp->p_nexec++;
171 had_char++;
172 }
173 # endif
174 if (!had_char)
175 break;
176 moveshots();
177 for (pp = Player; pp < End_player; )
178 if (pp->p_death[0] != '\0')
179 zap(pp, TRUE);
180 else
181 pp++;
182 # ifdef MONITOR
183 for (pp = Monitor; pp < End_monitor; )
184 if (pp->p_death[0] != '\0')
185 zap(pp, FALSE);
186 else
187 pp++;
188 # endif
189 }
190 if (FD_ISSET(Socket, &read_fds))
191 if (answer()) {
192 # ifdef INTERNET
193 if (first && standard_port)
194 faketalk();
195 # endif
196 first = FALSE;
197 }
198 if (FD_ISSET(Status, &read_fds))
199 send_stats();
200 for (pp = Player; pp < End_player; pp++) {
201 if (FD_ISSET(pp->p_fd, &read_fds))
202 sendcom(pp, READY, pp->p_nexec);
203 pp->p_nexec = 0;
204 (void) fflush(pp->p_output);
205 }
206 # ifdef MONITOR
207 for (pp = Monitor; pp < End_monitor; pp++) {
208 if (FD_ISSET(pp->p_fd, &read_fds))
209 sendcom(pp, READY, pp->p_nexec);
210 pp->p_nexec = 0;
211 (void) fflush(pp->p_output);
212 }
213 # endif
214 } while (Nplayer > 0);
215
216 read_fds = Fds_mask;
217 if (select(Num_fds, &read_fds, NULL, NULL, &linger) > 0) {
218 goto again;
219 }
220 if (server) {
221 clear_scores();
222 makemaze();
223 clearwalls();
224 # ifdef BOOTS
225 makeboots();
226 # endif
227 first = TRUE;
228 goto again;
229 }
230
231 # ifdef MONITOR
232 for (pp = Monitor; pp < End_monitor; )
233 zap(pp, FALSE);
234 # endif
235 cleanup(0);
236 /* NOTREACHED */
237 return(0);
238 }
239
240 /*
241 * init:
242 * Initialize the global parameters.
243 */
244 static void
245 init()
246 {
247 int i;
248 # ifdef INTERNET
249 SOCKET test_port;
250 int msg;
251 int len;
252 # endif
253
254 # ifndef DEBUG
255 # ifdef TIOCNOTTY
256 (void) ioctl(fileno(stdout), TIOCNOTTY, NULL);
257 # endif
258 (void) setpgrp(getpid(), getpid());
259 (void) signal(SIGHUP, SIG_IGN);
260 (void) signal(SIGINT, SIG_IGN);
261 (void) signal(SIGQUIT, SIG_IGN);
262 (void) signal(SIGTERM, cleanup);
263 # endif
264
265 (void) chdir("/usr/tmp"); /* just in case it core dumps */
266 (void) umask(0); /* No privacy at all! */
267 (void) signal(SIGPIPE, SIG_IGN);
268
269 # ifdef LOG
270 # ifdef SYSLOG_43
271 openlog("HUNT", LOG_PID, LOG_DAEMON);
272 # endif
273 # ifdef SYSLOG_42
274 openlog("HUNT", LOG_PID);
275 # endif
276 # endif
277
278 /*
279 * Initialize statistics socket
280 */
281 # ifdef INTERNET
282 Daemon.sin_family = SOCK_FAMILY;
283 Daemon.sin_addr.s_addr = INADDR_ANY;
284 Daemon.sin_port = 0;
285 # else
286 Daemon.sun_family = SOCK_FAMILY;
287 (void) strcpy(Daemon.sun_path, Stat_name);
288 # endif
289
290 Status = socket(SOCK_FAMILY, SOCK_STREAM, 0);
291 if (bind(Status, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
292 if (errno == EADDRINUSE)
293 exit(0);
294 else {
295 # ifdef LOG
296 syslog(LOG_ERR, "bind: %m");
297 # else
298 perror("bind");
299 # endif
300 cleanup(1);
301 }
302 }
303 (void) listen(Status, 5);
304
305 # ifdef INTERNET
306 len = sizeof (SOCKET);
307 if (getsockname(Status, (struct sockaddr *) &Daemon, &len) < 0) {
308 # ifdef LOG
309 syslog(LOG_ERR, "getsockname: %m");
310 # else
311 perror("getsockname");
312 # endif
313 exit(1);
314 }
315 stat_port = ntohs(Daemon.sin_port);
316 # endif
317
318 /*
319 * Initialize main socket
320 */
321 # ifdef INTERNET
322 Daemon.sin_family = SOCK_FAMILY;
323 Daemon.sin_addr.s_addr = INADDR_ANY;
324 Daemon.sin_port = 0;
325 # else
326 Daemon.sun_family = SOCK_FAMILY;
327 (void) strcpy(Daemon.sun_path, Sock_name);
328 # endif
329
330 Socket = socket(SOCK_FAMILY, SOCK_STREAM, 0);
331 # if defined(INTERNET)
332 msg = 1;
333 if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK, &msg, sizeof msg)<0)
334 # ifdef LOG
335 syslog(LOG_WARNING, "setsockopt loopback %m");
336 # else
337 perror("setsockopt loopback");
338 # endif
339 # endif
340 if (bind(Socket, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
341 if (errno == EADDRINUSE)
342 exit(0);
343 else {
344 # ifdef LOG
345 syslog(LOG_ERR, "bind: %m");
346 # else
347 perror("bind");
348 # endif
349 cleanup(1);
350 }
351 }
352 (void) listen(Socket, 5);
353
354 # ifdef INTERNET
355 len = sizeof (SOCKET);
356 if (getsockname(Socket, (struct sockaddr *) &Daemon, &len) < 0) {
357 # ifdef LOG
358 syslog(LOG_ERR, "getsockname: %m");
359 # else
360 perror("getsockname");
361 # endif
362 exit(1);
363 }
364 sock_port = ntohs(Daemon.sin_port);
365 # endif
366
367 /*
368 * Initialize minimal select mask
369 */
370 FD_SET(Socket, &Fds_mask);
371 FD_SET(Status, &Fds_mask);
372 Num_fds = ((Socket > Status) ? Socket : Status) + 1;
373
374 # ifdef INTERNET
375 len = sizeof (SOCKET);
376 if (getsockname(0, (struct sockaddr *) &test_port, &len) >= 0
377 && test_port.sin_family == AF_INET) {
378 inetd_spawned = TRUE;
379 Test_socket = 0;
380 if (test_port.sin_port != htons((u_short) Test_port)) {
381 standard_port = FALSE;
382 Test_port = ntohs(test_port.sin_port);
383 }
384 } else {
385 test_port = Daemon;
386 test_port.sin_port = htons((u_short) Test_port);
387
388 Test_socket = socket(SOCK_FAMILY, SOCK_DGRAM, 0);
389 if (bind(Test_socket, (struct sockaddr *) &test_port,
390 DAEMON_SIZE) < 0) {
391 # ifdef LOG
392 syslog(LOG_ERR, "bind: %m");
393 # else
394 perror("bind");
395 # endif
396 exit(1);
397 }
398 (void) listen(Test_socket, 5);
399 }
400
401 FD_SET(Test_socket, &Fds_mask);
402 if (Test_socket + 1 > Num_fds)
403 Num_fds = Test_socket + 1;
404 # endif
405
406 Seed = getpid() + time((time_t *) NULL);
407 makemaze();
408 # ifdef BOOTS
409 makeboots();
410 # endif
411
412 for (i = 0; i < NASCII; i++)
413 See_over[i] = TRUE;
414 See_over[DOOR] = FALSE;
415 See_over[WALL1] = FALSE;
416 See_over[WALL2] = FALSE;
417 See_over[WALL3] = FALSE;
418 # ifdef REFLECT
419 See_over[WALL4] = FALSE;
420 See_over[WALL5] = FALSE;
421 # endif
422
423 }
424
425 # ifdef BOOTS
426 /*
427 * makeboots:
428 * Put the boots in the maze
429 */
430 static void
431 makeboots()
432 {
433 int x, y;
434 PLAYER *pp;
435
436 do {
437 x = rand_num(WIDTH - 1) + 1;
438 y = rand_num(HEIGHT - 1) + 1;
439 } while (Maze[y][x] != SPACE);
440 Maze[y][x] = BOOT_PAIR;
441 for (pp = Boot; pp < &Boot[NBOOTS]; pp++)
442 pp->p_flying = -1;
443 }
444 # endif
445
446
447 /*
448 * checkdam:
449 * Check the damage to the given player, and see if s/he is killed
450 */
451 void
452 checkdam(ouch, gotcha, credit, amt, shot_type)
453 PLAYER *ouch, *gotcha;
454 IDENT *credit;
455 int amt;
456 char shot_type;
457 {
458 char *cp;
459
460 if (ouch->p_death[0] != '\0')
461 return;
462 # ifdef BOOTS
463 if (shot_type == SLIME)
464 switch (ouch->p_nboots) {
465 default:
466 break;
467 case 1:
468 amt = (amt + 1) / 2;
469 break;
470 case 2:
471 if (gotcha != NULL)
472 message(gotcha, "He has boots on!");
473 return;
474 }
475 # endif
476 ouch->p_damage += amt;
477 if (ouch->p_damage <= ouch->p_damcap) {
478 (void) sprintf(Buf, "%2d", ouch->p_damage);
479 cgoto(ouch, STAT_DAM_ROW, STAT_VALUE_COL);
480 outstr(ouch, Buf, 2);
481 return;
482 }
483
484 /* Someone DIED */
485 switch (shot_type) {
486 default:
487 cp = "Killed";
488 break;
489 # ifdef FLY
490 case FALL:
491 cp = "Killed on impact";
492 break;
493 # endif
494 case KNIFE:
495 cp = "Stabbed to death";
496 ouch->p_ammo = 0; /* No exploding */
497 break;
498 case SHOT:
499 cp = "Shot to death";
500 break;
501 case GRENADE:
502 case SATCHEL:
503 case BOMB:
504 cp = "Bombed";
505 break;
506 case MINE:
507 case GMINE:
508 cp = "Blown apart";
509 break;
510 # ifdef OOZE
511 case SLIME:
512 cp = "Slimed";
513 if (credit != NULL)
514 credit->i_slime++;
515 break;
516 # endif
517 # ifdef VOLCANO
518 case LAVA:
519 cp = "Baked";
520 break;
521 # endif
522 # ifdef DRONE
523 case DSHOT:
524 cp = "Eliminated";
525 break;
526 # endif
527 }
528 if (credit == NULL) {
529 (void) sprintf(ouch->p_death, "| %s by %s |", cp,
530 (shot_type == MINE || shot_type == GMINE) ?
531 "a mine" : "act of God");
532 return;
533 }
534
535 (void) sprintf(ouch->p_death, "| %s by %s |", cp, credit->i_name);
536
537 if (ouch == gotcha) { /* No use killing yourself */
538 credit->i_kills--;
539 credit->i_bkills++;
540 }
541 else if (ouch->p_ident->i_team == ' '
542 || ouch->p_ident->i_team != credit->i_team) {
543 credit->i_kills++;
544 credit->i_gkills++;
545 }
546 else {
547 credit->i_kills--;
548 credit->i_bkills++;
549 }
550 credit->i_score = credit->i_kills / (double) credit->i_entries;
551 ouch->p_ident->i_deaths++;
552 if (ouch->p_nchar == 0)
553 ouch->p_ident->i_stillb++;
554 if (gotcha == NULL)
555 return;
556 gotcha->p_damcap += STABDAM;
557 gotcha->p_damage -= STABDAM;
558 if (gotcha->p_damage < 0)
559 gotcha->p_damage = 0;
560 (void) sprintf(Buf, "%2d/%2d", gotcha->p_damage, gotcha->p_damcap);
561 cgoto(gotcha, STAT_DAM_ROW, STAT_VALUE_COL);
562 outstr(gotcha, Buf, 5);
563 (void) sprintf(Buf, "%3d", (gotcha->p_damcap - MAXDAM) / 2);
564 cgoto(gotcha, STAT_KILL_ROW, STAT_VALUE_COL);
565 outstr(gotcha, Buf, 3);
566 (void) sprintf(Buf, "%5.2f", gotcha->p_ident->i_score);
567 for (ouch = Player; ouch < End_player; ouch++) {
568 cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player),
569 STAT_NAME_COL);
570 outstr(ouch, Buf, 5);
571 }
572 # ifdef MONITOR
573 for (ouch = Monitor; ouch < End_monitor; ouch++) {
574 cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player),
575 STAT_NAME_COL);
576 outstr(ouch, Buf, 5);
577 }
578 # endif
579 }
580
581 /*
582 * zap:
583 * Kill off a player and take him out of the game.
584 */
585 static void
586 zap(pp, was_player)
587 PLAYER *pp;
588 FLAG was_player;
589 {
590 int i, len;
591 BULLET *bp;
592 PLAYER *np;
593 int x, y;
594 int savefd;
595
596 if (was_player) {
597 if (pp->p_undershot)
598 fixshots(pp->p_y, pp->p_x, pp->p_over);
599 drawplayer(pp, FALSE);
600 Nplayer--;
601 }
602
603 len = strlen(pp->p_death); /* Display the cause of death */
604 x = (WIDTH - len) / 2;
605 cgoto(pp, HEIGHT / 2, x);
606 outstr(pp, pp->p_death, len);
607 for (i = 1; i < len; i++)
608 pp->p_death[i] = '-';
609 pp->p_death[0] = '+';
610 pp->p_death[len - 1] = '+';
611 cgoto(pp, HEIGHT / 2 - 1, x);
612 outstr(pp, pp->p_death, len);
613 cgoto(pp, HEIGHT / 2 + 1, x);
614 outstr(pp, pp->p_death, len);
615 cgoto(pp, HEIGHT, 0);
616
617 savefd = pp->p_fd;
618
619 # ifdef MONITOR
620 if (was_player) {
621 # endif
622 for (bp = Bullets; bp != NULL; bp = bp->b_next) {
623 if (bp->b_owner == pp)
624 bp->b_owner = NULL;
625 if (bp->b_x == pp->p_x && bp->b_y == pp->p_y)
626 bp->b_over = SPACE;
627 }
628
629 i = rand_num(pp->p_ammo);
630 x = rand_num(pp->p_ammo);
631 if (x > i)
632 i = x;
633 if (pp->p_ammo == 0)
634 x = 0;
635 else if (i == pp->p_ammo - 1) {
636 x = pp->p_ammo;
637 len = SLIME;
638 }
639 else {
640 for (x = MAXBOMB - 1; x > 0; x--)
641 if (i >= shot_req[x])
642 break;
643 for (y = MAXSLIME - 1; y > 0; y--)
644 if (i >= slime_req[y])
645 break;
646 if (y >= 0 && slime_req[y] > shot_req[x]) {
647 x = slime_req[y];
648 len = SLIME;
649 }
650 else if (x != 0) {
651 len = shot_type[x];
652 x = shot_req[x];
653 }
654 }
655 if (x > 0) {
656 (void) add_shot(len, pp->p_y, pp->p_x, pp->p_face, x,
657 (PLAYER *) NULL, TRUE, SPACE);
658 (void) sprintf(Buf, "%s detonated.",
659 pp->p_ident->i_name);
660 for (np = Player; np < End_player; np++)
661 message(np, Buf);
662 # ifdef MONITOR
663 for (np = Monitor; np < End_monitor; np++)
664 message(np, Buf);
665 # endif
666 # ifdef BOOTS
667 while (pp->p_nboots-- > 0) {
668 for (np = Boot; np < &Boot[NBOOTS]; np++)
669 if (np->p_flying < 0)
670 break;
671 if (np >= &Boot[NBOOTS])
672 err(1, "Too many boots");
673 np->p_undershot = FALSE;
674 np->p_x = pp->p_x;
675 np->p_y = pp->p_y;
676 np->p_flying = rand_num(20);
677 np->p_flyx = 2 * rand_num(6) - 5;
678 np->p_flyy = 2 * rand_num(6) - 5;
679 np->p_over = SPACE;
680 np->p_face = BOOT;
681 showexpl(np->p_y, np->p_x, BOOT);
682 }
683 # endif
684 }
685 # ifdef BOOTS
686 else if (pp->p_nboots > 0) {
687 if (pp->p_nboots == 2)
688 Maze[pp->p_y][pp->p_x] = BOOT_PAIR;
689 else
690 Maze[pp->p_y][pp->p_x] = BOOT;
691 if (pp->p_undershot)
692 fixshots(pp->p_y, pp->p_x,
693 Maze[pp->p_y][pp->p_x]);
694 }
695 # endif
696
697 # ifdef VOLCANO
698 volcano += pp->p_ammo - x;
699 if (rand_num(100) < volcano / 50) {
700 do {
701 x = rand_num(WIDTH / 2) + WIDTH / 4;
702 y = rand_num(HEIGHT / 2) + HEIGHT / 4;
703 } while (Maze[y][x] != SPACE);
704 (void) add_shot(LAVA, y, x, LEFTS, volcano,
705 (PLAYER *) NULL, TRUE, SPACE);
706 for (np = Player; np < End_player; np++)
707 message(np, "Volcano eruption.");
708 volcano = 0;
709 }
710 # endif
711
712 # ifdef DRONE
713 if (rand_num(100) < 2) {
714 do {
715 x = rand_num(WIDTH / 2) + WIDTH / 4;
716 y = rand_num(HEIGHT / 2) + HEIGHT / 4;
717 } while (Maze[y][x] != SPACE);
718 add_shot(DSHOT, y, x, rand_dir(),
719 shot_req[MINDSHOT +
720 rand_num(MAXBOMB - MINDSHOT)],
721 (PLAYER *) NULL, FALSE, SPACE);
722 }
723 # endif
724
725 sendcom(pp, ENDWIN);
726 (void) putc(' ', pp->p_output);
727 (void) fclose(pp->p_output);
728
729 End_player--;
730 if (pp != End_player) {
731 memcpy(pp, End_player, sizeof (PLAYER));
732 (void) sprintf(Buf, "%5.2f%c%-10.10s %c",
733 pp->p_ident->i_score, stat_char(pp),
734 pp->p_ident->i_name, pp->p_ident->i_team);
735 i = STAT_PLAY_ROW + 1 + (pp - Player);
736 for (np = Player; np < End_player; np++) {
737 cgoto(np, i, STAT_NAME_COL);
738 outstr(np, Buf, STAT_NAME_LEN);
739 }
740 # ifdef MONITOR
741 for (np = Monitor; np < End_monitor; np++) {
742 cgoto(np, i, STAT_NAME_COL);
743 outstr(np, Buf, STAT_NAME_LEN);
744 }
745 # endif
746 }
747
748 /* Erase the last player */
749 i = STAT_PLAY_ROW + 1 + Nplayer;
750 for (np = Player; np < End_player; np++) {
751 cgoto(np, i, STAT_NAME_COL);
752 ce(np);
753 }
754 # ifdef MONITOR
755 for (np = Monitor; np < End_monitor; np++) {
756 cgoto(np, i, STAT_NAME_COL);
757 ce(np);
758 }
759 }
760 else {
761 sendcom(pp, ENDWIN);
762 (void) putc(LAST_PLAYER, pp->p_output);
763 (void) fclose(pp->p_output);
764
765 End_monitor--;
766 if (pp != End_monitor) {
767 memcpy(pp, End_monitor, sizeof (PLAYER));
768 (void) sprintf(Buf, "%5.5s %-10.10s %c", " ",
769 pp->p_ident->i_name, pp->p_ident->i_team);
770 i = STAT_MON_ROW + 1 + (pp - Player);
771 for (np = Player; np < End_player; np++) {
772 cgoto(np, i, STAT_NAME_COL);
773 outstr(np, Buf, STAT_NAME_LEN);
774 }
775 for (np = Monitor; np < End_monitor; np++) {
776 cgoto(np, i, STAT_NAME_COL);
777 outstr(np, Buf, STAT_NAME_LEN);
778 }
779 }
780
781 /* Erase the last monitor */
782 i = STAT_MON_ROW + 1 + (End_monitor - Monitor);
783 for (np = Player; np < End_player; np++) {
784 cgoto(np, i, STAT_NAME_COL);
785 ce(np);
786 }
787 for (np = Monitor; np < End_monitor; np++) {
788 cgoto(np, i, STAT_NAME_COL);
789 ce(np);
790 }
791
792 }
793 # endif
794
795 FD_CLR(savefd, &Fds_mask);
796 if (Num_fds == savefd + 1) {
797 Num_fds = Socket;
798 # ifdef INTERNET
799 if (Test_socket > Socket)
800 Num_fds = Test_socket;
801 # endif
802 for (np = Player; np < End_player; np++)
803 if (np->p_fd > Num_fds)
804 Num_fds = np->p_fd;
805 # ifdef MONITOR
806 for (np = Monitor; np < End_monitor; np++)
807 if (np->p_fd > Num_fds)
808 Num_fds = np->p_fd;
809 # endif
810 Num_fds++;
811 }
812 }
813
814 /*
815 * rand_num:
816 * Return a random number in a given range.
817 */
818 int
819 rand_num(range)
820 int range;
821 {
822 return (range == 0 ? 0 : RN % range);
823 }
824
825 /*
826 * havechar:
827 * Check to see if we have any characters in the input queue; if
828 * we do, read them, stash them away, and return TRUE; else return
829 * FALSE.
830 */
831 static int
832 havechar(pp)
833 PLAYER *pp;
834 {
835
836 if (pp->p_ncount < pp->p_nchar)
837 return TRUE;
838 if (!FD_ISSET(pp->p_fd, &Have_inp))
839 return FALSE;
840 FD_CLR(pp->p_fd, &Have_inp);
841 check_again:
842 errno = 0;
843 if ((pp->p_nchar = read(pp->p_fd, pp->p_cbuf, sizeof pp->p_cbuf)) <= 0)
844 {
845 if (errno == EINTR)
846 goto check_again;
847 pp->p_cbuf[0] = 'q';
848 }
849 pp->p_ncount = 0;
850 return TRUE;
851 }
852
853 /*
854 * cleanup:
855 * Exit with the given value, cleaning up any droppings lying around
856 */
857 SIGNAL_TYPE
858 cleanup(eval)
859 int eval;
860 {
861 PLAYER *pp;
862
863 for (pp = Player; pp < End_player; pp++) {
864 cgoto(pp, HEIGHT, 0);
865 sendcom(pp, ENDWIN);
866 (void) putc(LAST_PLAYER, pp->p_output);
867 (void) fclose(pp->p_output);
868 }
869 # ifdef MONITOR
870 for (pp = Monitor; pp < End_monitor; pp++) {
871 cgoto(pp, HEIGHT, 0);
872 sendcom(pp, ENDWIN);
873 (void) putc(LAST_PLAYER, pp->p_output);
874 (void) fclose(pp->p_output);
875 }
876 # endif
877 (void) close(Socket);
878 # ifdef AF_UNIX_HACK
879 (void) unlink(Sock_name);
880 # endif
881
882 exit(eval);
883 }
884
885 /*
886 * send_stats:
887 * Print stats to requestor
888 */
889 static void
890 send_stats()
891 {
892 IDENT *ip;
893 FILE *fp;
894 int s;
895 SOCKET sockstruct;
896 int socklen;
897
898 /*
899 * Get the output stream ready
900 */
901 # ifdef INTERNET
902 socklen = sizeof sockstruct;
903 # else
904 socklen = sizeof sockstruct - 1;
905 # endif
906 s = accept(Status, (struct sockaddr *) &sockstruct, &socklen);
907 if (s < 0) {
908 if (errno == EINTR)
909 return;
910 # ifdef LOG
911 syslog(LOG_ERR, "accept: %m");
912 # else
913 perror("accept");
914 # endif
915 return;
916 }
917 fp = fdopen(s, "w");
918 if (fp == NULL) {
919 # ifdef LOG
920 syslog(LOG_ERR, "fdopen: %m");
921 # else
922 perror("fdopen");
923 # endif
924 (void) close(s);
925 return;
926 }
927
928 /*
929 * Send output to requestor
930 */
931 fputs("Name\t\tScore\tDucked\tAbsorb\tFaced\tShot\tRobbed\tMissed\tSlimeK\n", fp);
932 for (ip = Scores; ip != NULL; ip = ip->i_next) {
933 fprintf(fp, "%s\t", ip->i_name);
934 if (strlen(ip->i_name) < 8)
935 putc('\t', fp);
936 fprintf(fp, "%.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
937 ip->i_score, ip->i_ducked, ip->i_absorbed,
938 ip->i_faced, ip->i_shot, ip->i_robbed,
939 ip->i_missed, ip->i_slime);
940 }
941 fputs("\n\nName\t\tEnemy\tFriend\tDeaths\tStill\tSaved\n", fp);
942 for (ip = Scores; ip != NULL; ip = ip->i_next) {
943 if (ip->i_team == ' ') {
944 fprintf(fp, "%s\t", ip->i_name);
945 if (strlen(ip->i_name) < 8)
946 putc('\t', fp);
947 }
948 else {
949 fprintf(fp, "%s[%c]\t", ip->i_name, ip->i_team);
950 if (strlen(ip->i_name) + 3 < 8)
951 putc('\t', fp);
952 }
953 fprintf(fp, "%d\t%d\t%d\t%d\t%d\n",
954 ip->i_gkills, ip->i_bkills, ip->i_deaths,
955 ip->i_stillb, ip->i_saved);
956 }
957
958 (void) fclose(fp);
959 }
960
961 /*
962 * clear_scores:
963 * Clear out the scores so the next session start clean
964 */
965 static void
966 clear_scores()
967 {
968 IDENT *ip, *nextip;
969
970 for (ip = Scores; ip != NULL; ip = nextip) {
971 nextip = ip->i_next;
972 (void) free((char *) ip);
973 }
974 Scores = NULL;
975 }