]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/driver.c
88b497fb1eb3bb3dc3fc3107c3fb7edf8efa2756
[bsdgames-darwin.git] / hunt / huntd / driver.c
1 /* $NetBSD: driver.c,v 1.31 2014/03/29 22:11:19 dholland Exp $ */
2 /*
3 * Copyright (c) 1983-2003, Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * + Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * + 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 * + Neither the name of the University of California, San Francisco nor
16 * the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 #ifndef lint
35 __RCSID("$NetBSD: driver.c,v 1.31 2014/03/29 22:11:19 dholland Exp $");
36 #endif /* not lint */
37
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <signal.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46
47 #include "hunt.h"
48 #include "pathnames.h"
49
50
51 #ifdef INTERNET
52 static uint16_t Test_port = TEST_PORT;
53 #else
54 static const char Sock_name[] = PATH_HUNTSOCKET;
55 static const char Stat_name[] = PATH_STATSOCKET;
56 #endif
57
58 static SOCKET Daemon;
59
60 #ifdef INTERNET
61 static int Test_socket; /* test socket to answer datagrams */
62 static bool inetd_spawned; /* invoked via inetd */
63 static bool standard_port = true; /* true if listening on standard port */
64 static u_short sock_port; /* port # of tcp listen socket */
65 static u_short stat_port; /* port # of statistics tcp socket */
66 #define DAEMON_SIZE (sizeof Daemon)
67 #else
68 #define DAEMON_SIZE (sizeof Daemon - 1)
69 #endif
70
71 #ifdef VOLCANO
72 static int volcano = 0; /* Explosion size */
73 #endif
74
75 static int Status; /* stat socket */
76
77 static void clear_scores(void);
78 static bool havechar(PLAYER *, int);
79 static void init(void);
80 static void makeboots(void);
81 static void send_stats(void);
82 static void zap(PLAYER *, bool, int);
83
84
85 /*
86 * main:
87 * The main program.
88 */
89 int
90 main(int ac, char **av)
91 {
92 PLAYER *pp;
93 #ifdef INTERNET
94 u_short msg;
95 short reply;
96 socklen_t namelen;
97 SOCKET test;
98 #endif
99 static bool first = true;
100 static bool server = false;
101 int c, i;
102 const int linger = 90 * 1000;
103
104 while ((c = getopt(ac, av, "sp:")) != -1) {
105 switch (c) {
106 case 's':
107 server = true;
108 break;
109 #ifdef INTERNET
110 case 'p':
111 standard_port = false;
112 Test_port = atoi(optarg);
113 break;
114 #endif
115 default:
116 erred:
117 fprintf(stderr, "Usage: %s [-s] [-p port]\n", av[0]);
118 exit(1);
119 }
120 }
121 if (optind < ac)
122 goto erred;
123
124 init();
125
126
127 again:
128 do {
129 errno = 0;
130 while (poll(fdset, 3+MAXPL+MAXMON, INFTIM) < 0)
131 {
132 if (errno != EINTR)
133 #ifdef LOG
134 syslog(LOG_WARNING, "poll: %m");
135 #else
136 warn("poll");
137 #endif
138 errno = 0;
139 }
140 #ifdef INTERNET
141 if (fdset[2].revents & POLLIN) {
142 namelen = DAEMON_SIZE;
143 (void) recvfrom(Test_socket, &msg, sizeof msg,
144 0, (struct sockaddr *) &test, &namelen);
145 switch (ntohs(msg)) {
146 case C_MESSAGE:
147 if (Nplayer <= 0)
148 break;
149 reply = htons((u_short) Nplayer);
150 (void) sendto(Test_socket, &reply,
151 sizeof reply, 0,
152 (struct sockaddr *) &test, DAEMON_SIZE);
153 break;
154 case C_SCORES:
155 reply = htons(stat_port);
156 (void) sendto(Test_socket, &reply,
157 sizeof reply, 0,
158 (struct sockaddr *) &test, DAEMON_SIZE);
159 break;
160 case C_PLAYER:
161 case C_MONITOR:
162 if (msg == C_MONITOR && Nplayer <= 0)
163 break;
164 reply = htons(sock_port);
165 (void) sendto(Test_socket, &reply,
166 sizeof reply, 0,
167 (struct sockaddr *) &test, DAEMON_SIZE);
168 break;
169 }
170 }
171 #endif
172 {
173 for (pp = Player, i = 0; pp < End_player; pp++, i++)
174 if (havechar(pp, i + 3)) {
175 execute(pp);
176 pp->p_nexec++;
177 }
178 #ifdef MONITOR
179 for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++)
180 if (havechar(pp, i + MAXPL + 3)) {
181 mon_execute(pp);
182 pp->p_nexec++;
183 }
184 #endif
185 moveshots();
186 for (pp = Player, i = 0; pp < End_player; )
187 if (pp->p_death[0] != '\0')
188 zap(pp, true, i + 3);
189 else
190 pp++, i++;
191 #ifdef MONITOR
192 for (pp = Monitor, i = 0; pp < End_monitor; )
193 if (pp->p_death[0] != '\0')
194 zap(pp, false, i + MAXPL + 3);
195 else
196 pp++, i++;
197 #endif
198 }
199 if (fdset[0].revents & POLLIN)
200 if (answer()) {
201 if (first) {
202 /* announce start of game? */
203 }
204 first = false;
205 }
206 if (fdset[1].revents & POLLIN)
207 send_stats();
208 for (pp = Player, i = 0; pp < End_player; pp++, i++) {
209 if (fdset[i + 3].revents & POLLIN)
210 sendcom(pp, READY, pp->p_nexec);
211 pp->p_nexec = 0;
212 (void) fflush(pp->p_output);
213 }
214 #ifdef MONITOR
215 for (pp = Monitor, i = 0; pp < End_monitor; pp++, i++) {
216 if (fdset[i + MAXPL + 3].revents & POLLIN)
217 sendcom(pp, READY, pp->p_nexec);
218 pp->p_nexec = 0;
219 (void) fflush(pp->p_output);
220 }
221 #endif
222 } while (Nplayer > 0);
223
224 if (poll(fdset, 3+MAXPL+MAXMON, linger) > 0) {
225 goto again;
226 }
227 if (server) {
228 clear_scores();
229 makemaze();
230 clearwalls();
231 #ifdef BOOTS
232 makeboots();
233 #endif
234 first = true;
235 goto again;
236 }
237
238 #ifdef MONITOR
239 for (pp = Monitor, i = 0; pp < End_monitor; i++)
240 zap(pp, false, i + MAXPL + 3);
241 #endif
242 cleanup(0);
243 /* NOTREACHED */
244 return(0);
245 }
246
247 /*
248 * init:
249 * Initialize the global parameters.
250 */
251 static void
252 init(void)
253 {
254 int i;
255 #ifdef INTERNET
256 SOCKET test_port;
257 int msg;
258 socklen_t len;
259 #endif
260
261 #ifndef DEBUG
262 #ifdef TIOCNOTTY
263 (void) ioctl(fileno(stdout), TIOCNOTTY, NULL);
264 #endif
265 (void) setpgrp(getpid(), getpid());
266 (void) signal(SIGHUP, SIG_IGN);
267 (void) signal(SIGINT, SIG_IGN);
268 (void) signal(SIGQUIT, SIG_IGN);
269 (void) signal(SIGTERM, cleanup);
270 #endif
271
272 (void) chdir("/var/tmp"); /* just in case it core dumps */
273 (void) umask(0); /* No privacy at all! */
274 (void) signal(SIGPIPE, SIG_IGN);
275
276 #ifdef LOG
277 openlog("huntd", LOG_PID, LOG_DAEMON);
278 #endif
279
280 /*
281 * Initialize statistics socket
282 */
283 #ifdef INTERNET
284 Daemon.sin_family = SOCK_FAMILY;
285 Daemon.sin_addr.s_addr = INADDR_ANY;
286 Daemon.sin_port = 0;
287 #else
288 Daemon.sun_family = SOCK_FAMILY;
289 (void) strcpy(Daemon.sun_path, Stat_name);
290 #endif
291
292 Status = socket(SOCK_FAMILY, SOCK_STREAM, 0);
293 if (bind(Status, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
294 if (errno == EADDRINUSE)
295 exit(0);
296 else {
297 #ifdef LOG
298 syslog(LOG_ERR, "bind: %m");
299 #else
300 warn("bind");
301 #endif
302 cleanup(1);
303 }
304 }
305 (void) listen(Status, 5);
306
307 #ifdef INTERNET
308 len = sizeof (SOCKET);
309 if (getsockname(Status, (struct sockaddr *) &Daemon, &len) < 0) {
310 #ifdef LOG
311 syslog(LOG_ERR, "getsockname: %m");
312 #else
313 warn("getsockname");
314 #endif
315 exit(1);
316 }
317 stat_port = ntohs(Daemon.sin_port);
318 #endif
319
320 /*
321 * Initialize main socket
322 */
323 #ifdef INTERNET
324 Daemon.sin_family = SOCK_FAMILY;
325 Daemon.sin_addr.s_addr = INADDR_ANY;
326 Daemon.sin_port = 0;
327 #else
328 Daemon.sun_family = SOCK_FAMILY;
329 (void) strcpy(Daemon.sun_path, Sock_name);
330 #endif
331
332 Socket = socket(SOCK_FAMILY, SOCK_STREAM, 0);
333 #if defined(INTERNET)
334 msg = 1;
335 if (setsockopt(Socket, SOL_SOCKET, SO_USELOOPBACK, &msg, sizeof msg)<0)
336 #ifdef LOG
337 syslog(LOG_WARNING, "setsockopt loopback %m");
338 #else
339 warn("setsockopt loopback");
340 #endif
341 #endif
342 if (bind(Socket, (struct sockaddr *) &Daemon, DAEMON_SIZE) < 0) {
343 if (errno == EADDRINUSE)
344 exit(0);
345 else {
346 #ifdef LOG
347 syslog(LOG_ERR, "bind: %m");
348 #else
349 warn("bind");
350 #endif
351 cleanup(1);
352 }
353 }
354 (void) listen(Socket, 5);
355
356 #ifdef INTERNET
357 len = sizeof (SOCKET);
358 if (getsockname(Socket, (struct sockaddr *) &Daemon, &len) < 0) {
359 #ifdef LOG
360 syslog(LOG_ERR, "getsockname: %m");
361 #else
362 warn("getsockname");
363 #endif
364 exit(1);
365 }
366 sock_port = ntohs(Daemon.sin_port);
367 #endif
368
369 /*
370 * Initialize minimal poll mask
371 */
372 fdset[0].fd = Socket;
373 fdset[0].events = POLLIN;
374 fdset[1].fd = Status;
375 fdset[1].events = POLLIN;
376
377 #ifdef INTERNET
378 len = sizeof (SOCKET);
379 if (getsockname(0, (struct sockaddr *) &test_port, &len) >= 0
380 && test_port.sin_family == AF_INET) {
381 inetd_spawned = true;
382 Test_socket = 0;
383 if (test_port.sin_port != htons((u_short) Test_port)) {
384 standard_port = false;
385 Test_port = ntohs(test_port.sin_port);
386 }
387 } else {
388 test_port = Daemon;
389 test_port.sin_port = htons((u_short) Test_port);
390
391 Test_socket = socket(SOCK_FAMILY, SOCK_DGRAM, 0);
392 if (bind(Test_socket, (struct sockaddr *) &test_port,
393 DAEMON_SIZE) < 0) {
394 #ifdef LOG
395 syslog(LOG_ERR, "bind: %m");
396 #else
397 warn("bind");
398 #endif
399 exit(1);
400 }
401 (void) listen(Test_socket, 5);
402 }
403
404 fdset[2].fd = Test_socket;
405 fdset[2].events = POLLIN;
406 #else
407 fdset[2].fd = -1;
408 #endif
409
410 srandom(time(NULL));
411 makemaze();
412 #ifdef BOOTS
413 makeboots();
414 #endif
415
416 for (i = 0; i < NASCII; i++)
417 See_over[i] = true;
418 See_over[DOOR] = false;
419 See_over[WALL1] = false;
420 See_over[WALL2] = false;
421 See_over[WALL3] = false;
422 #ifdef REFLECT
423 See_over[WALL4] = false;
424 See_over[WALL5] = false;
425 #endif
426
427 }
428
429 #ifdef BOOTS
430 /*
431 * makeboots:
432 * Put the boots in the maze
433 */
434 static void
435 makeboots(void)
436 {
437 int x, y;
438 PLAYER *pp;
439
440 do {
441 x = rand_num(WIDTH - 1) + 1;
442 y = rand_num(HEIGHT - 1) + 1;
443 } while (Maze[y][x] != SPACE);
444 Maze[y][x] = BOOT_PAIR;
445 for (pp = Boot; pp < &Boot[NBOOTS]; pp++)
446 pp->p_flying = -1;
447 }
448 #endif
449
450
451 /*
452 * checkdam:
453 * Check the damage to the given player, and see if s/he is killed
454 */
455 void
456 checkdam(PLAYER *ouch, PLAYER *gotcha, IDENT *credit, int amt,
457 char this_shot_type)
458 {
459 const char *cp;
460
461 if (ouch->p_death[0] != '\0')
462 return;
463 #ifdef BOOTS
464 if (this_shot_type == SLIME)
465 switch (ouch->p_nboots) {
466 default:
467 break;
468 case 1:
469 amt = (amt + 1) / 2;
470 break;
471 case 2:
472 if (gotcha != NULL)
473 message(gotcha, "He has boots on!");
474 return;
475 }
476 #endif
477 ouch->p_damage += amt;
478 if (ouch->p_damage <= ouch->p_damcap) {
479 (void) snprintf(Buf, sizeof(Buf), "%2d", ouch->p_damage);
480 cgoto(ouch, STAT_DAM_ROW, STAT_VALUE_COL);
481 outstr(ouch, Buf, 2);
482 return;
483 }
484
485 /* Someone DIED */
486 switch (this_shot_type) {
487 default:
488 cp = "Killed";
489 break;
490 #ifdef FLY
491 case FALL:
492 cp = "Killed on impact";
493 break;
494 #endif
495 case KNIFE:
496 cp = "Stabbed to death";
497 ouch->p_ammo = 0; /* No exploding */
498 break;
499 case SHOT:
500 cp = "Shot to death";
501 break;
502 case GRENADE:
503 case SATCHEL:
504 case BOMB:
505 cp = "Bombed";
506 break;
507 case MINE:
508 case GMINE:
509 cp = "Blown apart";
510 break;
511 #ifdef OOZE
512 case SLIME:
513 cp = "Slimed";
514 if (credit != NULL)
515 credit->i_slime++;
516 break;
517 #endif
518 #ifdef VOLCANO
519 case LAVA:
520 cp = "Baked";
521 break;
522 #endif
523 #ifdef DRONE
524 case DSHOT:
525 cp = "Eliminated";
526 break;
527 #endif
528 }
529 if (credit == NULL) {
530 (void) snprintf(ouch->p_death, sizeof(ouch->p_death),
531 "| %s by %s |", cp,
532 (this_shot_type == MINE || this_shot_type == GMINE) ?
533 "a mine" : "act of God");
534 return;
535 }
536
537 (void) snprintf(ouch->p_death, sizeof(ouch->p_death),
538 "| %s by %s |", cp, credit->i_name);
539
540 if (ouch == gotcha) { /* No use killing yourself */
541 credit->i_kills--;
542 credit->i_bkills++;
543 }
544 else if (ouch->p_ident->i_team == ' '
545 || ouch->p_ident->i_team != credit->i_team) {
546 credit->i_kills++;
547 credit->i_gkills++;
548 }
549 else {
550 credit->i_kills--;
551 credit->i_bkills++;
552 }
553 credit->i_score = credit->i_kills / (double) credit->i_entries;
554 ouch->p_ident->i_deaths++;
555 if (ouch->p_nchar == 0)
556 ouch->p_ident->i_stillb++;
557 if (gotcha == NULL)
558 return;
559 gotcha->p_damcap += STABDAM;
560 gotcha->p_damage -= STABDAM;
561 if (gotcha->p_damage < 0)
562 gotcha->p_damage = 0;
563 (void) snprintf(Buf, sizeof(Buf), "%2d/%2d", gotcha->p_damage,
564 gotcha->p_damcap);
565 cgoto(gotcha, STAT_DAM_ROW, STAT_VALUE_COL);
566 outstr(gotcha, Buf, 5);
567 (void) snprintf(Buf, sizeof(Buf), "%3d",
568 (gotcha->p_damcap - MAXDAM) / 2);
569 cgoto(gotcha, STAT_KILL_ROW, STAT_VALUE_COL);
570 outstr(gotcha, Buf, 3);
571 (void) snprintf(Buf, sizeof(Buf), "%5.2f", gotcha->p_ident->i_score);
572 for (ouch = Player; ouch < End_player; ouch++) {
573 cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player),
574 STAT_NAME_COL);
575 outstr(ouch, Buf, 5);
576 }
577 #ifdef MONITOR
578 for (ouch = Monitor; ouch < End_monitor; ouch++) {
579 cgoto(ouch, STAT_PLAY_ROW + 1 + (gotcha - Player),
580 STAT_NAME_COL);
581 outstr(ouch, Buf, 5);
582 }
583 #endif
584 }
585
586 /*
587 * zap:
588 * Kill off a player and take him out of the game.
589 */
590 static void
591 zap(PLAYER *pp, bool was_player, int i)
592 {
593 int n, len;
594 BULLET *bp;
595 PLAYER *np;
596 int x, y;
597
598 if (was_player) {
599 if (pp->p_undershot)
600 fixshots(pp->p_y, pp->p_x, pp->p_over);
601 drawplayer(pp, false);
602 Nplayer--;
603 }
604
605 len = strlen(pp->p_death); /* Display the cause of death */
606 x = (WIDTH - len) / 2;
607 cgoto(pp, HEIGHT / 2, x);
608 outstr(pp, pp->p_death, len);
609 for (n = 1; n < len; n++)
610 pp->p_death[n] = '-';
611 pp->p_death[0] = '+';
612 pp->p_death[len - 1] = '+';
613 cgoto(pp, HEIGHT / 2 - 1, x);
614 outstr(pp, pp->p_death, len);
615 cgoto(pp, HEIGHT / 2 + 1, x);
616 outstr(pp, pp->p_death, len);
617 cgoto(pp, HEIGHT, 0);
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 n = rand_num(pp->p_ammo);
630 x = rand_num(pp->p_ammo);
631 if (x > n)
632 n = x;
633 if (pp->p_ammo == 0)
634 x = 0;
635 else if (n == 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 (n >= shot_req[x])
642 break;
643 for (y = MAXSLIME - 1; y > 0; y--)
644 if (n >= 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 NULL, true, SPACE);
658 (void) snprintf(Buf, sizeof(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 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 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 fdset[i] = fdset[End_player - Player + 3];
733 fdset[End_player - Player + 3].fd = -1;
734 (void) snprintf(Buf, sizeof(Buf), "%5.2f%c%-10.10s %c",
735 pp->p_ident->i_score, stat_char(pp),
736 pp->p_ident->i_name, pp->p_ident->i_team);
737 n = STAT_PLAY_ROW + 1 + (pp - Player);
738 for (np = Player; np < End_player; np++) {
739 cgoto(np, n, STAT_NAME_COL);
740 outstr(np, Buf, STAT_NAME_LEN);
741 }
742 #ifdef MONITOR
743 for (np = Monitor; np < End_monitor; np++) {
744 cgoto(np, n, STAT_NAME_COL);
745 outstr(np, Buf, STAT_NAME_LEN);
746 }
747 #endif
748 } else
749 fdset[i].fd = -1;
750
751 /* Erase the last player */
752 n = STAT_PLAY_ROW + 1 + Nplayer;
753 for (np = Player; np < End_player; np++) {
754 cgoto(np, n, STAT_NAME_COL);
755 ce(np);
756 }
757 #ifdef MONITOR
758 for (np = Monitor; np < End_monitor; np++) {
759 cgoto(np, n, STAT_NAME_COL);
760 ce(np);
761 }
762 }
763 else {
764 sendcom(pp, ENDWIN);
765 (void) putc(LAST_PLAYER, pp->p_output);
766 (void) fclose(pp->p_output);
767
768 End_monitor--;
769 if (pp != End_monitor) {
770 memcpy(pp, End_monitor, sizeof (PLAYER));
771 fdset[i] = fdset[End_monitor - Monitor + MAXPL + 3];
772 fdset[End_monitor - Monitor + MAXPL + 3].fd = -1;
773 (void) snprintf(Buf, sizeof(Buf), "%5.5s %-10.10s %c",
774 " ",
775 pp->p_ident->i_name, pp->p_ident->i_team);
776 n = STAT_MON_ROW + 1 + (pp - Player);
777 for (np = Player; np < End_player; np++) {
778 cgoto(np, n, STAT_NAME_COL);
779 outstr(np, Buf, STAT_NAME_LEN);
780 }
781 for (np = Monitor; np < End_monitor; np++) {
782 cgoto(np, n, STAT_NAME_COL);
783 outstr(np, Buf, STAT_NAME_LEN);
784 }
785 } else
786 fdset[i].fd = -1;
787
788 /* Erase the last monitor */
789 n = STAT_MON_ROW + 1 + (End_monitor - Monitor);
790 for (np = Player; np < End_player; np++) {
791 cgoto(np, n, STAT_NAME_COL);
792 ce(np);
793 }
794 for (np = Monitor; np < End_monitor; np++) {
795 cgoto(np, n, STAT_NAME_COL);
796 ce(np);
797 }
798 }
799 #endif
800 }
801
802 /*
803 * rand_num:
804 * Return a random number in a given range.
805 */
806 int
807 rand_num(int range)
808 {
809 return (range == 0 ? 0 : random() % range);
810 }
811
812 /*
813 * havechar:
814 * Check to see if we have any characters in the input queue; if
815 * we do, read them, stash them away, and return true; else return
816 * false.
817 */
818 static bool
819 havechar(PLAYER *pp, int i)
820 {
821
822 if (pp->p_ncount < pp->p_nchar)
823 return true;
824 if (!(fdset[i].revents & POLLIN))
825 return false;
826 check_again:
827 pp->p_nchar = read(pp->p_fd, pp->p_cbuf, sizeof pp->p_cbuf);
828 if (pp->p_nchar < 0 && errno == EINTR) {
829 goto check_again;
830 } else if (pp->p_nchar <= 0) {
831 if (errno == EINTR)
832 pp->p_cbuf[0] = 'q';
833 }
834 pp->p_ncount = 0;
835 return true;
836 }
837
838 /*
839 * cleanup:
840 * Exit with the given value, cleaning up any droppings lying around
841 */
842 void
843 cleanup(int exitval)
844 {
845 PLAYER *pp;
846
847 for (pp = Player; pp < End_player; pp++) {
848 cgoto(pp, HEIGHT, 0);
849 sendcom(pp, ENDWIN);
850 (void) putc(LAST_PLAYER, pp->p_output);
851 (void) fclose(pp->p_output);
852 }
853 #ifdef MONITOR
854 for (pp = Monitor; pp < End_monitor; pp++) {
855 cgoto(pp, HEIGHT, 0);
856 sendcom(pp, ENDWIN);
857 (void) putc(LAST_PLAYER, pp->p_output);
858 (void) fclose(pp->p_output);
859 }
860 #endif
861 (void) close(Socket);
862 #ifdef AF_UNIX_HACK
863 (void) unlink(Sock_name);
864 #endif
865
866 exit(exitval);
867 }
868
869 /*
870 * send_stats:
871 * Print stats to requestor
872 */
873 static void
874 send_stats(void)
875 {
876 IDENT *ip;
877 FILE *fp;
878 int s;
879 SOCKET sockstruct;
880 socklen_t socklen;
881
882 /*
883 * Get the output stream ready
884 */
885 #ifdef INTERNET
886 socklen = sizeof sockstruct;
887 #else
888 socklen = sizeof sockstruct - 1;
889 #endif
890 s = accept(Status, (struct sockaddr *) &sockstruct, &socklen);
891 if (s < 0) {
892 if (errno == EINTR)
893 return;
894 #ifdef LOG
895 syslog(LOG_WARNING, "accept: %m");
896 #else
897 warn("accept");
898 #endif
899 return;
900 }
901 fp = fdopen(s, "w");
902 if (fp == NULL) {
903 #ifdef LOG
904 syslog(LOG_WARNING, "fdopen: %m");
905 #else
906 warn("fdopen");
907 #endif
908 (void) close(s);
909 return;
910 }
911
912 /*
913 * Send output to requestor
914 */
915 fputs("Name\t\tScore\tDucked\tAbsorb\tFaced\tShot\tRobbed\tMissed\tSlimeK\n", fp);
916 for (ip = Scores; ip != NULL; ip = ip->i_next) {
917 fprintf(fp, "%s\t", ip->i_name);
918 if (strlen(ip->i_name) < 8)
919 putc('\t', fp);
920 fprintf(fp, "%.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
921 ip->i_score, ip->i_ducked, ip->i_absorbed,
922 ip->i_faced, ip->i_shot, ip->i_robbed,
923 ip->i_missed, ip->i_slime);
924 }
925 fputs("\n\nName\t\tEnemy\tFriend\tDeaths\tStill\tSaved\n", fp);
926 for (ip = Scores; ip != NULL; ip = ip->i_next) {
927 if (ip->i_team == ' ') {
928 fprintf(fp, "%s\t", ip->i_name);
929 if (strlen(ip->i_name) < 8)
930 putc('\t', fp);
931 }
932 else {
933 fprintf(fp, "%s[%c]\t", ip->i_name, ip->i_team);
934 if (strlen(ip->i_name) + 3 < 8)
935 putc('\t', fp);
936 }
937 fprintf(fp, "%d\t%d\t%d\t%d\t%d\n",
938 ip->i_gkills, ip->i_bkills, ip->i_deaths,
939 ip->i_stillb, ip->i_saved);
940 }
941
942 (void) fclose(fp);
943 }
944
945 /*
946 * clear_scores:
947 * Clear out the scores so the next session start clean
948 */
949 static void
950 clear_scores(void)
951 {
952 IDENT *ip, *nextip;
953
954 for (ip = Scores; ip != NULL; ip = nextip) {
955 nextip = ip->i_next;
956 (void) free(ip);
957 }
958 Scores = NULL;
959 }