]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/driver.c
1 /* $NetBSD: driver.c,v 1.32 2014/03/29 22:29:55 dholland Exp $ */
3 * Copyright (c) 1983-2003, Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
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
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.
33 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: driver.c,v 1.32 2014/03/29 22:29:55 dholland Exp $");
38 #include <sys/ioctl.h>
48 #include "pathnames.h"
52 static uint16_t Test_port
= TEST_PORT
;
54 static const char Sock_name
[] = PATH_HUNTSOCKET
;
55 static const char Stat_name
[] = PATH_STATSOCKET
;
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)
68 #define DAEMON_SIZE (sizeof Daemon - 1)
72 static int volcano
= 0; /* Explosion size */
75 static int Status
; /* stat socket */
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);
90 main(int ac
, char **av
)
99 static bool first
= true;
100 static bool server
= false;
102 const int linger
= 90 * 1000;
104 while ((c
= getopt(ac
, av
, "sp:")) != -1) {
111 standard_port
= false;
112 Test_port
= atoi(optarg
);
117 fprintf(stderr
, "Usage: %s [-s] [-p port]\n", av
[0]);
130 while (poll(fdset
, 3+MAXPL
+MAXMON
, INFTIM
) < 0)
133 complain(LOG_WARNING
, "poll");
137 if (fdset
[2].revents
& POLLIN
) {
138 namelen
= DAEMON_SIZE
;
139 (void) recvfrom(Test_socket
, &msg
, sizeof msg
,
140 0, (struct sockaddr
*) &test
, &namelen
);
141 switch (ntohs(msg
)) {
145 reply
= htons((u_short
) Nplayer
);
146 (void) sendto(Test_socket
, &reply
,
148 (struct sockaddr
*) &test
, DAEMON_SIZE
);
151 reply
= htons(stat_port
);
152 (void) sendto(Test_socket
, &reply
,
154 (struct sockaddr
*) &test
, DAEMON_SIZE
);
158 if (msg
== C_MONITOR
&& Nplayer
<= 0)
160 reply
= htons(sock_port
);
161 (void) sendto(Test_socket
, &reply
,
163 (struct sockaddr
*) &test
, DAEMON_SIZE
);
169 for (pp
= Player
, i
= 0; pp
< End_player
; pp
++, i
++)
170 if (havechar(pp
, i
+ 3)) {
175 for (pp
= Monitor
, i
= 0; pp
< End_monitor
; pp
++, i
++)
176 if (havechar(pp
, i
+ MAXPL
+ 3)) {
182 for (pp
= Player
, i
= 0; pp
< End_player
; )
183 if (pp
->p_death
[0] != '\0')
184 zap(pp
, true, i
+ 3);
188 for (pp
= Monitor
, i
= 0; pp
< End_monitor
; )
189 if (pp
->p_death
[0] != '\0')
190 zap(pp
, false, i
+ MAXPL
+ 3);
195 if (fdset
[0].revents
& POLLIN
)
198 /* announce start of game? */
202 if (fdset
[1].revents
& POLLIN
)
204 for (pp
= Player
, i
= 0; pp
< End_player
; pp
++, i
++) {
205 if (fdset
[i
+ 3].revents
& POLLIN
)
206 sendcom(pp
, READY
, pp
->p_nexec
);
208 (void) fflush(pp
->p_output
);
211 for (pp
= Monitor
, i
= 0; pp
< End_monitor
; pp
++, i
++) {
212 if (fdset
[i
+ MAXPL
+ 3].revents
& POLLIN
)
213 sendcom(pp
, READY
, pp
->p_nexec
);
215 (void) fflush(pp
->p_output
);
218 } while (Nplayer
> 0);
220 if (poll(fdset
, 3+MAXPL
+MAXMON
, linger
) > 0) {
235 for (pp
= Monitor
, i
= 0; pp
< End_monitor
; i
++)
236 zap(pp
, false, i
+ MAXPL
+ 3);
245 * Initialize the global parameters.
259 (void) ioctl(fileno(stdout
), TIOCNOTTY
, NULL
);
261 (void) setpgrp(getpid(), getpid());
262 (void) signal(SIGHUP
, SIG_IGN
);
263 (void) signal(SIGINT
, SIG_IGN
);
264 (void) signal(SIGQUIT
, SIG_IGN
);
265 (void) signal(SIGTERM
, cleanup
);
268 (void) chdir("/var/tmp"); /* just in case it core dumps */
269 (void) umask(0); /* No privacy at all! */
270 (void) signal(SIGPIPE
, SIG_IGN
);
273 openlog("huntd", LOG_PID
, LOG_DAEMON
);
277 * Initialize statistics socket
280 Daemon
.sin_family
= SOCK_FAMILY
;
281 Daemon
.sin_addr
.s_addr
= INADDR_ANY
;
284 Daemon
.sun_family
= SOCK_FAMILY
;
285 (void) strcpy(Daemon
.sun_path
, Stat_name
);
288 Status
= socket(SOCK_FAMILY
, SOCK_STREAM
, 0);
289 if (bind(Status
, (struct sockaddr
*) &Daemon
, DAEMON_SIZE
) < 0) {
290 if (errno
== EADDRINUSE
)
293 complain(LOG_ERR
, "bind");
297 (void) listen(Status
, 5);
300 len
= sizeof (SOCKET
);
301 if (getsockname(Status
, (struct sockaddr
*) &Daemon
, &len
) < 0) {
302 complain(LOG_ERR
, "getsockname");
305 stat_port
= ntohs(Daemon
.sin_port
);
309 * Initialize main socket
312 Daemon
.sin_family
= SOCK_FAMILY
;
313 Daemon
.sin_addr
.s_addr
= INADDR_ANY
;
316 Daemon
.sun_family
= SOCK_FAMILY
;
317 (void) strcpy(Daemon
.sun_path
, Sock_name
);
320 Socket
= socket(SOCK_FAMILY
, SOCK_STREAM
, 0);
321 #if defined(INTERNET)
323 if (setsockopt(Socket
, SOL_SOCKET
, SO_USELOOPBACK
, &msg
, sizeof msg
)<0)
324 complain(LOG_WARNING
, "setsockopt loopback");
326 if (bind(Socket
, (struct sockaddr
*) &Daemon
, DAEMON_SIZE
) < 0) {
327 if (errno
== EADDRINUSE
)
330 complain(LOG_ERR
, "bind");
334 (void) listen(Socket
, 5);
337 len
= sizeof (SOCKET
);
338 if (getsockname(Socket
, (struct sockaddr
*) &Daemon
, &len
) < 0) {
339 complain(LOG_ERR
, "getsockname");
342 sock_port
= ntohs(Daemon
.sin_port
);
346 * Initialize minimal poll mask
348 fdset
[0].fd
= Socket
;
349 fdset
[0].events
= POLLIN
;
350 fdset
[1].fd
= Status
;
351 fdset
[1].events
= POLLIN
;
354 len
= sizeof (SOCKET
);
355 if (getsockname(0, (struct sockaddr
*) &test_port
, &len
) >= 0
356 && test_port
.sin_family
== AF_INET
) {
357 inetd_spawned
= true;
359 if (test_port
.sin_port
!= htons((u_short
) Test_port
)) {
360 standard_port
= false;
361 Test_port
= ntohs(test_port
.sin_port
);
365 test_port
.sin_port
= htons((u_short
) Test_port
);
367 Test_socket
= socket(SOCK_FAMILY
, SOCK_DGRAM
, 0);
368 if (bind(Test_socket
, (struct sockaddr
*) &test_port
,
370 complain(LOG_ERR
, "bind");
373 (void) listen(Test_socket
, 5);
376 fdset
[2].fd
= Test_socket
;
377 fdset
[2].events
= POLLIN
;
388 for (i
= 0; i
< NASCII
; i
++)
390 See_over
[DOOR
] = false;
391 See_over
[WALL1
] = false;
392 See_over
[WALL2
] = false;
393 See_over
[WALL3
] = false;
395 See_over
[WALL4
] = false;
396 See_over
[WALL5
] = false;
404 * Put the boots in the maze
413 x
= rand_num(WIDTH
- 1) + 1;
414 y
= rand_num(HEIGHT
- 1) + 1;
415 } while (Maze
[y
][x
] != SPACE
);
416 Maze
[y
][x
] = BOOT_PAIR
;
417 for (pp
= Boot
; pp
< &Boot
[NBOOTS
]; pp
++)
425 * Check the damage to the given player, and see if s/he is killed
428 checkdam(PLAYER
*ouch
, PLAYER
*gotcha
, IDENT
*credit
, int amt
,
433 if (ouch
->p_death
[0] != '\0')
436 if (this_shot_type
== SLIME
)
437 switch (ouch
->p_nboots
) {
445 message(gotcha
, "He has boots on!");
449 ouch
->p_damage
+= amt
;
450 if (ouch
->p_damage
<= ouch
->p_damcap
) {
451 (void) snprintf(Buf
, sizeof(Buf
), "%2d", ouch
->p_damage
);
452 cgoto(ouch
, STAT_DAM_ROW
, STAT_VALUE_COL
);
453 outstr(ouch
, Buf
, 2);
458 switch (this_shot_type
) {
464 cp
= "Killed on impact";
468 cp
= "Stabbed to death";
469 ouch
->p_ammo
= 0; /* No exploding */
472 cp
= "Shot to death";
501 if (credit
== NULL
) {
502 (void) snprintf(ouch
->p_death
, sizeof(ouch
->p_death
),
504 (this_shot_type
== MINE
|| this_shot_type
== GMINE
) ?
505 "a mine" : "act of God");
509 (void) snprintf(ouch
->p_death
, sizeof(ouch
->p_death
),
510 "| %s by %s |", cp
, credit
->i_name
);
512 if (ouch
== gotcha
) { /* No use killing yourself */
516 else if (ouch
->p_ident
->i_team
== ' '
517 || ouch
->p_ident
->i_team
!= credit
->i_team
) {
525 credit
->i_score
= credit
->i_kills
/ (double) credit
->i_entries
;
526 ouch
->p_ident
->i_deaths
++;
527 if (ouch
->p_nchar
== 0)
528 ouch
->p_ident
->i_stillb
++;
531 gotcha
->p_damcap
+= STABDAM
;
532 gotcha
->p_damage
-= STABDAM
;
533 if (gotcha
->p_damage
< 0)
534 gotcha
->p_damage
= 0;
535 (void) snprintf(Buf
, sizeof(Buf
), "%2d/%2d", gotcha
->p_damage
,
537 cgoto(gotcha
, STAT_DAM_ROW
, STAT_VALUE_COL
);
538 outstr(gotcha
, Buf
, 5);
539 (void) snprintf(Buf
, sizeof(Buf
), "%3d",
540 (gotcha
->p_damcap
- MAXDAM
) / 2);
541 cgoto(gotcha
, STAT_KILL_ROW
, STAT_VALUE_COL
);
542 outstr(gotcha
, Buf
, 3);
543 (void) snprintf(Buf
, sizeof(Buf
), "%5.2f", gotcha
->p_ident
->i_score
);
544 for (ouch
= Player
; ouch
< End_player
; ouch
++) {
545 cgoto(ouch
, STAT_PLAY_ROW
+ 1 + (gotcha
- Player
),
547 outstr(ouch
, Buf
, 5);
550 for (ouch
= Monitor
; ouch
< End_monitor
; ouch
++) {
551 cgoto(ouch
, STAT_PLAY_ROW
+ 1 + (gotcha
- Player
),
553 outstr(ouch
, Buf
, 5);
560 * Kill off a player and take him out of the game.
563 zap(PLAYER
*pp
, bool was_player
, int i
)
572 fixshots(pp
->p_y
, pp
->p_x
, pp
->p_over
);
573 drawplayer(pp
, false);
577 len
= strlen(pp
->p_death
); /* Display the cause of death */
578 x
= (WIDTH
- len
) / 2;
579 cgoto(pp
, HEIGHT
/ 2, x
);
580 outstr(pp
, pp
->p_death
, len
);
581 for (n
= 1; n
< len
; n
++)
582 pp
->p_death
[n
] = '-';
583 pp
->p_death
[0] = '+';
584 pp
->p_death
[len
- 1] = '+';
585 cgoto(pp
, HEIGHT
/ 2 - 1, x
);
586 outstr(pp
, pp
->p_death
, len
);
587 cgoto(pp
, HEIGHT
/ 2 + 1, x
);
588 outstr(pp
, pp
->p_death
, len
);
589 cgoto(pp
, HEIGHT
, 0);
594 for (bp
= Bullets
; bp
!= NULL
; bp
= bp
->b_next
) {
595 if (bp
->b_owner
== pp
)
597 if (bp
->b_x
== pp
->p_x
&& bp
->b_y
== pp
->p_y
)
601 n
= rand_num(pp
->p_ammo
);
602 x
= rand_num(pp
->p_ammo
);
607 else if (n
== pp
->p_ammo
- 1) {
612 for (x
= MAXBOMB
- 1; x
> 0; x
--)
613 if (n
>= shot_req
[x
])
615 for (y
= MAXSLIME
- 1; y
> 0; y
--)
616 if (n
>= slime_req
[y
])
618 if (y
>= 0 && slime_req
[y
] > shot_req
[x
]) {
628 (void) add_shot(len
, pp
->p_y
, pp
->p_x
, pp
->p_face
, x
,
630 (void) snprintf(Buf
, sizeof(Buf
), "%s detonated.",
631 pp
->p_ident
->i_name
);
632 for (np
= Player
; np
< End_player
; np
++)
635 for (np
= Monitor
; np
< End_monitor
; np
++)
639 while (pp
->p_nboots
-- > 0) {
640 for (np
= Boot
; np
< &Boot
[NBOOTS
]; np
++)
641 if (np
->p_flying
< 0)
643 if (np
>= &Boot
[NBOOTS
])
644 err(1, "Too many boots");
645 np
->p_undershot
= false;
648 np
->p_flying
= rand_num(20);
649 np
->p_flyx
= 2 * rand_num(6) - 5;
650 np
->p_flyy
= 2 * rand_num(6) - 5;
653 showexpl(np
->p_y
, np
->p_x
, BOOT
);
658 else if (pp
->p_nboots
> 0) {
659 if (pp
->p_nboots
== 2)
660 Maze
[pp
->p_y
][pp
->p_x
] = BOOT_PAIR
;
662 Maze
[pp
->p_y
][pp
->p_x
] = BOOT
;
664 fixshots(pp
->p_y
, pp
->p_x
,
665 Maze
[pp
->p_y
][pp
->p_x
]);
670 volcano
+= pp
->p_ammo
- x
;
671 if (rand_num(100) < volcano
/ 50) {
673 x
= rand_num(WIDTH
/ 2) + WIDTH
/ 4;
674 y
= rand_num(HEIGHT
/ 2) + HEIGHT
/ 4;
675 } while (Maze
[y
][x
] != SPACE
);
676 (void) add_shot(LAVA
, y
, x
, LEFTS
, volcano
,
678 for (np
= Player
; np
< End_player
; np
++)
679 message(np
, "Volcano eruption.");
685 if (rand_num(100) < 2) {
687 x
= rand_num(WIDTH
/ 2) + WIDTH
/ 4;
688 y
= rand_num(HEIGHT
/ 2) + HEIGHT
/ 4;
689 } while (Maze
[y
][x
] != SPACE
);
690 add_shot(DSHOT
, y
, x
, rand_dir(),
692 rand_num(MAXBOMB
- MINDSHOT
)],
698 (void) putc(' ', pp
->p_output
);
699 (void) fclose(pp
->p_output
);
702 if (pp
!= End_player
) {
703 memcpy(pp
, End_player
, sizeof (PLAYER
));
704 fdset
[i
] = fdset
[End_player
- Player
+ 3];
705 fdset
[End_player
- Player
+ 3].fd
= -1;
706 (void) snprintf(Buf
, sizeof(Buf
), "%5.2f%c%-10.10s %c",
707 pp
->p_ident
->i_score
, stat_char(pp
),
708 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
709 n
= STAT_PLAY_ROW
+ 1 + (pp
- Player
);
710 for (np
= Player
; np
< End_player
; np
++) {
711 cgoto(np
, n
, STAT_NAME_COL
);
712 outstr(np
, Buf
, STAT_NAME_LEN
);
715 for (np
= Monitor
; np
< End_monitor
; np
++) {
716 cgoto(np
, n
, STAT_NAME_COL
);
717 outstr(np
, Buf
, STAT_NAME_LEN
);
723 /* Erase the last player */
724 n
= STAT_PLAY_ROW
+ 1 + Nplayer
;
725 for (np
= Player
; np
< End_player
; np
++) {
726 cgoto(np
, n
, STAT_NAME_COL
);
730 for (np
= Monitor
; np
< End_monitor
; np
++) {
731 cgoto(np
, n
, STAT_NAME_COL
);
737 (void) putc(LAST_PLAYER
, pp
->p_output
);
738 (void) fclose(pp
->p_output
);
741 if (pp
!= End_monitor
) {
742 memcpy(pp
, End_monitor
, sizeof (PLAYER
));
743 fdset
[i
] = fdset
[End_monitor
- Monitor
+ MAXPL
+ 3];
744 fdset
[End_monitor
- Monitor
+ MAXPL
+ 3].fd
= -1;
745 (void) snprintf(Buf
, sizeof(Buf
), "%5.5s %-10.10s %c",
747 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
748 n
= STAT_MON_ROW
+ 1 + (pp
- Player
);
749 for (np
= Player
; np
< End_player
; np
++) {
750 cgoto(np
, n
, STAT_NAME_COL
);
751 outstr(np
, Buf
, STAT_NAME_LEN
);
753 for (np
= Monitor
; np
< End_monitor
; np
++) {
754 cgoto(np
, n
, STAT_NAME_COL
);
755 outstr(np
, Buf
, STAT_NAME_LEN
);
760 /* Erase the last monitor */
761 n
= STAT_MON_ROW
+ 1 + (End_monitor
- Monitor
);
762 for (np
= Player
; np
< End_player
; np
++) {
763 cgoto(np
, n
, STAT_NAME_COL
);
766 for (np
= Monitor
; np
< End_monitor
; np
++) {
767 cgoto(np
, n
, STAT_NAME_COL
);
776 * Return a random number in a given range.
781 return (range
== 0 ? 0 : random() % range
);
786 * Check to see if we have any characters in the input queue; if
787 * we do, read them, stash them away, and return true; else return
791 havechar(PLAYER
*pp
, int i
)
794 if (pp
->p_ncount
< pp
->p_nchar
)
796 if (!(fdset
[i
].revents
& POLLIN
))
799 pp
->p_nchar
= read(pp
->p_fd
, pp
->p_cbuf
, sizeof pp
->p_cbuf
);
800 if (pp
->p_nchar
< 0 && errno
== EINTR
) {
802 } else if (pp
->p_nchar
<= 0) {
812 * Exit with the given value, cleaning up any droppings lying around
819 for (pp
= Player
; pp
< End_player
; pp
++) {
820 cgoto(pp
, HEIGHT
, 0);
822 (void) putc(LAST_PLAYER
, pp
->p_output
);
823 (void) fclose(pp
->p_output
);
826 for (pp
= Monitor
; pp
< End_monitor
; pp
++) {
827 cgoto(pp
, HEIGHT
, 0);
829 (void) putc(LAST_PLAYER
, pp
->p_output
);
830 (void) fclose(pp
->p_output
);
833 (void) close(Socket
);
835 (void) unlink(Sock_name
);
843 * Print stats to requestor
855 * Get the output stream ready
858 socklen
= sizeof sockstruct
;
860 socklen
= sizeof sockstruct
- 1;
862 s
= accept(Status
, (struct sockaddr
*) &sockstruct
, &socklen
);
866 complain(LOG_WARNING
, "accept");
871 complain(LOG_WARNING
, "fdopen");
877 * Send output to requestor
879 fputs("Name\t\tScore\tDucked\tAbsorb\tFaced\tShot\tRobbed\tMissed\tSlimeK\n", fp
);
880 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
) {
881 fprintf(fp
, "%s\t", ip
->i_name
);
882 if (strlen(ip
->i_name
) < 8)
884 fprintf(fp
, "%.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
885 ip
->i_score
, ip
->i_ducked
, ip
->i_absorbed
,
886 ip
->i_faced
, ip
->i_shot
, ip
->i_robbed
,
887 ip
->i_missed
, ip
->i_slime
);
889 fputs("\n\nName\t\tEnemy\tFriend\tDeaths\tStill\tSaved\n", fp
);
890 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
) {
891 if (ip
->i_team
== ' ') {
892 fprintf(fp
, "%s\t", ip
->i_name
);
893 if (strlen(ip
->i_name
) < 8)
897 fprintf(fp
, "%s[%c]\t", ip
->i_name
, ip
->i_team
);
898 if (strlen(ip
->i_name
) + 3 < 8)
901 fprintf(fp
, "%d\t%d\t%d\t%d\t%d\n",
902 ip
->i_gkills
, ip
->i_bkills
, ip
->i_deaths
,
903 ip
->i_stillb
, ip
->i_saved
);
911 * Clear out the scores so the next session start clean
918 for (ip
= Scores
; ip
!= NULL
; ip
= nextip
) {