1 /* $NetBSD: driver.c,v 1.19 2009/07/04 06:38:35 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.19 2009/07/04 06:38:35 dholland Exp $");
38 #include <sys/ioctl.h>
49 #define RN (((Seed = Seed * 11109 + 13849) >> 16) & 0xffff)
51 #define RN ((Seed = Seed * 11109 + 13849) & 0x7fff)
58 static char *First_arg
; /* pointer to argv[0] */
59 static char *Last_arg
; /* pointer to end of argv/environ */
62 static int Test_socket
; /* test socket to answer datagrams */
63 static FLAG inetd_spawned
; /* invoked via inetd */
64 static FLAG standard_port
= TRUE
; /* true if listening on standard port */
65 static u_short sock_port
; /* port # of tcp listen socket */
66 static u_short stat_port
; /* port # of statistics tcp socket */
67 #define DAEMON_SIZE (sizeof Daemon)
69 #define DAEMON_SIZE (sizeof Daemon - 1)
72 static void clear_scores(void);
73 static int havechar(PLAYER
*, int);
74 static void init(void);
75 int main(int, char *[], char *[]);
76 static void makeboots(void);
77 static void send_stats(void);
78 static void zap(PLAYER
*, FLAG
, int);
86 main(int ac
, char **av
, char **ep
)
91 short port_num
, reply
;
95 static FLAG first
= TRUE
;
96 static FLAG server
= FALSE
;
98 const int linger
= 90 * 1000;
101 if (ep
== NULL
|| *ep
== NULL
)
105 Last_arg
= ep
[-1] + strlen(ep
[-1]);
107 while ((c
= getopt(ac
, av
, "sp:")) != -1) {
114 standard_port
= FALSE
;
115 Test_port
= atoi(optarg
);
120 fprintf(stderr
, "Usage: %s [-s] [-p port]\n", av
[0]);
133 while (poll(fdset
, 3+MAXPL
+MAXMON
, INFTIM
) < 0)
137 syslog(LOG_WARNING
, "poll: %m");
144 if (fdset
[2].revents
& POLLIN
) {
145 namelen
= DAEMON_SIZE
;
146 port_num
= htons(sock_port
);
147 (void) recvfrom(Test_socket
, &msg
, sizeof msg
,
148 0, (struct sockaddr
*) &test
, &namelen
);
149 switch (ntohs(msg
)) {
153 reply
= htons((u_short
) Nplayer
);
154 (void) sendto(Test_socket
, &reply
,
156 (struct sockaddr
*) &test
, DAEMON_SIZE
);
159 reply
= htons(stat_port
);
160 (void) sendto(Test_socket
, &reply
,
162 (struct sockaddr
*) &test
, DAEMON_SIZE
);
166 if (msg
== C_MONITOR
&& Nplayer
<= 0)
168 reply
= htons(sock_port
);
169 (void) sendto(Test_socket
, &reply
,
171 (struct sockaddr
*) &test
, DAEMON_SIZE
);
177 for (pp
= Player
, i
= 0; pp
< End_player
; pp
++, i
++)
178 if (havechar(pp
, i
+ 3)) {
183 for (pp
= Monitor
, i
= 0; pp
< End_monitor
; pp
++, i
++)
184 if (havechar(pp
, i
+ MAXPL
+ 3)) {
190 for (pp
= Player
, i
= 0; pp
< End_player
; )
191 if (pp
->p_death
[0] != '\0')
192 zap(pp
, TRUE
, i
+ 3);
196 for (pp
= Monitor
, i
= 0; pp
< End_monitor
; )
197 if (pp
->p_death
[0] != '\0')
198 zap(pp
, FALSE
, i
+ MAXPL
+ 3);
203 if (fdset
[0].revents
& POLLIN
)
206 if (first
&& standard_port
)
211 if (fdset
[1].revents
& POLLIN
)
213 for (pp
= Player
, i
= 0; pp
< End_player
; pp
++, i
++) {
214 if (fdset
[i
+ 3].revents
& POLLIN
)
215 sendcom(pp
, READY
, pp
->p_nexec
);
217 (void) fflush(pp
->p_output
);
220 for (pp
= Monitor
, i
= 0; pp
< End_monitor
; pp
++, i
++) {
221 if (fdset
[i
+ MAXPL
+ 3].revents
& POLLIN
)
222 sendcom(pp
, READY
, pp
->p_nexec
);
224 (void) fflush(pp
->p_output
);
227 } while (Nplayer
> 0);
229 if (poll(fdset
, 3+MAXPL
+MAXMON
, linger
) > 0) {
244 for (pp
= Monitor
, i
= 0; pp
< End_monitor
; i
++)
245 zap(pp
, FALSE
, i
+ MAXPL
+ 3);
254 * Initialize the global parameters.
268 (void) ioctl(fileno(stdout
), TIOCNOTTY
, NULL
);
270 (void) setpgrp(getpid(), getpid());
271 (void) signal(SIGHUP
, SIG_IGN
);
272 (void) signal(SIGINT
, SIG_IGN
);
273 (void) signal(SIGQUIT
, SIG_IGN
);
274 (void) signal(SIGTERM
, cleanup
);
277 (void) chdir("/var/tmp"); /* just in case it core dumps */
278 (void) umask(0); /* No privacy at all! */
279 (void) signal(SIGPIPE
, SIG_IGN
);
282 openlog("huntd", LOG_PID
, LOG_DAEMON
);
286 * Initialize statistics socket
289 Daemon
.sin_family
= SOCK_FAMILY
;
290 Daemon
.sin_addr
.s_addr
= INADDR_ANY
;
293 Daemon
.sun_family
= SOCK_FAMILY
;
294 (void) strcpy(Daemon
.sun_path
, Stat_name
);
297 Status
= socket(SOCK_FAMILY
, SOCK_STREAM
, 0);
298 if (bind(Status
, (struct sockaddr
*) &Daemon
, DAEMON_SIZE
) < 0) {
299 if (errno
== EADDRINUSE
)
303 syslog(LOG_ERR
, "bind: %m");
310 (void) listen(Status
, 5);
313 len
= sizeof (SOCKET
);
314 if (getsockname(Status
, (struct sockaddr
*) &Daemon
, &len
) < 0) {
316 syslog(LOG_ERR
, "getsockname: %m");
322 stat_port
= ntohs(Daemon
.sin_port
);
326 * Initialize main socket
329 Daemon
.sin_family
= SOCK_FAMILY
;
330 Daemon
.sin_addr
.s_addr
= INADDR_ANY
;
333 Daemon
.sun_family
= SOCK_FAMILY
;
334 (void) strcpy(Daemon
.sun_path
, Sock_name
);
337 Socket
= socket(SOCK_FAMILY
, SOCK_STREAM
, 0);
338 #if defined(INTERNET)
340 if (setsockopt(Socket
, SOL_SOCKET
, SO_USELOOPBACK
, &msg
, sizeof msg
)<0)
342 syslog(LOG_WARNING
, "setsockopt loopback %m");
344 warn("setsockopt loopback");
347 if (bind(Socket
, (struct sockaddr
*) &Daemon
, DAEMON_SIZE
) < 0) {
348 if (errno
== EADDRINUSE
)
352 syslog(LOG_ERR
, "bind: %m");
359 (void) listen(Socket
, 5);
362 len
= sizeof (SOCKET
);
363 if (getsockname(Socket
, (struct sockaddr
*) &Daemon
, &len
) < 0) {
365 syslog(LOG_ERR
, "getsockname: %m");
371 sock_port
= ntohs(Daemon
.sin_port
);
375 * Initialize minimal poll mask
377 fdset
[0].fd
= Socket
;
378 fdset
[0].events
= POLLIN
;
379 fdset
[1].fd
= Status
;
380 fdset
[1].events
= POLLIN
;
383 len
= sizeof (SOCKET
);
384 if (getsockname(0, (struct sockaddr
*) &test_port
, &len
) >= 0
385 && test_port
.sin_family
== AF_INET
) {
386 inetd_spawned
= TRUE
;
388 if (test_port
.sin_port
!= htons((u_short
) Test_port
)) {
389 standard_port
= FALSE
;
390 Test_port
= ntohs(test_port
.sin_port
);
394 test_port
.sin_port
= htons((u_short
) Test_port
);
396 Test_socket
= socket(SOCK_FAMILY
, SOCK_DGRAM
, 0);
397 if (bind(Test_socket
, (struct sockaddr
*) &test_port
,
400 syslog(LOG_ERR
, "bind: %m");
406 (void) listen(Test_socket
, 5);
409 fdset
[2].fd
= Test_socket
;
410 fdset
[2].events
= POLLIN
;
415 Seed
= getpid() + time((time_t *) NULL
);
421 for (i
= 0; i
< NASCII
; i
++)
423 See_over
[DOOR
] = FALSE
;
424 See_over
[WALL1
] = FALSE
;
425 See_over
[WALL2
] = FALSE
;
426 See_over
[WALL3
] = FALSE
;
428 See_over
[WALL4
] = FALSE
;
429 See_over
[WALL5
] = FALSE
;
437 * Put the boots in the maze
446 x
= rand_num(WIDTH
- 1) + 1;
447 y
= rand_num(HEIGHT
- 1) + 1;
448 } while (Maze
[y
][x
] != SPACE
);
449 Maze
[y
][x
] = BOOT_PAIR
;
450 for (pp
= Boot
; pp
< &Boot
[NBOOTS
]; pp
++)
458 * Check the damage to the given player, and see if s/he is killed
461 checkdam(PLAYER
*ouch
, PLAYER
*gotcha
, IDENT
*credit
, int amt
,
466 if (ouch
->p_death
[0] != '\0')
469 if (this_shot_type
== SLIME
)
470 switch (ouch
->p_nboots
) {
478 message(gotcha
, "He has boots on!");
482 ouch
->p_damage
+= amt
;
483 if (ouch
->p_damage
<= ouch
->p_damcap
) {
484 (void) snprintf(Buf
, sizeof(Buf
), "%2d", ouch
->p_damage
);
485 cgoto(ouch
, STAT_DAM_ROW
, STAT_VALUE_COL
);
486 outstr(ouch
, Buf
, 2);
491 switch (this_shot_type
) {
497 cp
= "Killed on impact";
501 cp
= "Stabbed to death";
502 ouch
->p_ammo
= 0; /* No exploding */
505 cp
= "Shot to death";
534 if (credit
== NULL
) {
535 (void) snprintf(ouch
->p_death
, sizeof(ouch
->p_death
),
537 (this_shot_type
== MINE
|| this_shot_type
== GMINE
) ?
538 "a mine" : "act of God");
542 (void) snprintf(ouch
->p_death
, sizeof(ouch
->p_death
),
543 "| %s by %s |", cp
, credit
->i_name
);
545 if (ouch
== gotcha
) { /* No use killing yourself */
549 else if (ouch
->p_ident
->i_team
== ' '
550 || ouch
->p_ident
->i_team
!= credit
->i_team
) {
558 credit
->i_score
= credit
->i_kills
/ (double) credit
->i_entries
;
559 ouch
->p_ident
->i_deaths
++;
560 if (ouch
->p_nchar
== 0)
561 ouch
->p_ident
->i_stillb
++;
564 gotcha
->p_damcap
+= STABDAM
;
565 gotcha
->p_damage
-= STABDAM
;
566 if (gotcha
->p_damage
< 0)
567 gotcha
->p_damage
= 0;
568 (void) snprintf(Buf
, sizeof(Buf
), "%2d/%2d", gotcha
->p_damage
,
570 cgoto(gotcha
, STAT_DAM_ROW
, STAT_VALUE_COL
);
571 outstr(gotcha
, Buf
, 5);
572 (void) snprintf(Buf
, sizeof(Buf
), "%3d",
573 (gotcha
->p_damcap
- MAXDAM
) / 2);
574 cgoto(gotcha
, STAT_KILL_ROW
, STAT_VALUE_COL
);
575 outstr(gotcha
, Buf
, 3);
576 (void) snprintf(Buf
, sizeof(Buf
), "%5.2f", gotcha
->p_ident
->i_score
);
577 for (ouch
= Player
; ouch
< End_player
; ouch
++) {
578 cgoto(ouch
, STAT_PLAY_ROW
+ 1 + (gotcha
- Player
),
580 outstr(ouch
, Buf
, 5);
583 for (ouch
= Monitor
; ouch
< End_monitor
; ouch
++) {
584 cgoto(ouch
, STAT_PLAY_ROW
+ 1 + (gotcha
- Player
),
586 outstr(ouch
, Buf
, 5);
593 * Kill off a player and take him out of the game.
596 zap(PLAYER
*pp
, FLAG was_player
, int i
)
606 fixshots(pp
->p_y
, pp
->p_x
, pp
->p_over
);
607 drawplayer(pp
, FALSE
);
611 len
= strlen(pp
->p_death
); /* Display the cause of death */
612 x
= (WIDTH
- len
) / 2;
613 cgoto(pp
, HEIGHT
/ 2, x
);
614 outstr(pp
, pp
->p_death
, len
);
615 for (n
= 1; n
< len
; n
++)
616 pp
->p_death
[n
] = '-';
617 pp
->p_death
[0] = '+';
618 pp
->p_death
[len
- 1] = '+';
619 cgoto(pp
, HEIGHT
/ 2 - 1, x
);
620 outstr(pp
, pp
->p_death
, len
);
621 cgoto(pp
, HEIGHT
/ 2 + 1, x
);
622 outstr(pp
, pp
->p_death
, len
);
623 cgoto(pp
, HEIGHT
, 0);
630 for (bp
= Bullets
; bp
!= NULL
; bp
= bp
->b_next
) {
631 if (bp
->b_owner
== pp
)
633 if (bp
->b_x
== pp
->p_x
&& bp
->b_y
== pp
->p_y
)
637 n
= rand_num(pp
->p_ammo
);
638 x
= rand_num(pp
->p_ammo
);
643 else if (n
== pp
->p_ammo
- 1) {
648 for (x
= MAXBOMB
- 1; x
> 0; x
--)
649 if (n
>= shot_req
[x
])
651 for (y
= MAXSLIME
- 1; y
> 0; y
--)
652 if (n
>= slime_req
[y
])
654 if (y
>= 0 && slime_req
[y
] > shot_req
[x
]) {
664 (void) add_shot(len
, pp
->p_y
, pp
->p_x
, pp
->p_face
, x
,
665 (PLAYER
*) NULL
, TRUE
, SPACE
);
666 (void) snprintf(Buf
, sizeof(Buf
), "%s detonated.",
667 pp
->p_ident
->i_name
);
668 for (np
= Player
; np
< End_player
; np
++)
671 for (np
= Monitor
; np
< End_monitor
; np
++)
675 while (pp
->p_nboots
-- > 0) {
676 for (np
= Boot
; np
< &Boot
[NBOOTS
]; np
++)
677 if (np
->p_flying
< 0)
679 if (np
>= &Boot
[NBOOTS
])
680 err(1, "Too many boots");
681 np
->p_undershot
= FALSE
;
684 np
->p_flying
= rand_num(20);
685 np
->p_flyx
= 2 * rand_num(6) - 5;
686 np
->p_flyy
= 2 * rand_num(6) - 5;
689 showexpl(np
->p_y
, np
->p_x
, BOOT
);
694 else if (pp
->p_nboots
> 0) {
695 if (pp
->p_nboots
== 2)
696 Maze
[pp
->p_y
][pp
->p_x
] = BOOT_PAIR
;
698 Maze
[pp
->p_y
][pp
->p_x
] = BOOT
;
700 fixshots(pp
->p_y
, pp
->p_x
,
701 Maze
[pp
->p_y
][pp
->p_x
]);
706 volcano
+= pp
->p_ammo
- x
;
707 if (rand_num(100) < volcano
/ 50) {
709 x
= rand_num(WIDTH
/ 2) + WIDTH
/ 4;
710 y
= rand_num(HEIGHT
/ 2) + HEIGHT
/ 4;
711 } while (Maze
[y
][x
] != SPACE
);
712 (void) add_shot(LAVA
, y
, x
, LEFTS
, volcano
,
713 (PLAYER
*) NULL
, TRUE
, SPACE
);
714 for (np
= Player
; np
< End_player
; np
++)
715 message(np
, "Volcano eruption.");
721 if (rand_num(100) < 2) {
723 x
= rand_num(WIDTH
/ 2) + WIDTH
/ 4;
724 y
= rand_num(HEIGHT
/ 2) + HEIGHT
/ 4;
725 } while (Maze
[y
][x
] != SPACE
);
726 add_shot(DSHOT
, y
, x
, rand_dir(),
728 rand_num(MAXBOMB
- MINDSHOT
)],
729 (PLAYER
*) NULL
, FALSE
, SPACE
);
734 (void) putc(' ', pp
->p_output
);
735 (void) fclose(pp
->p_output
);
738 if (pp
!= End_player
) {
739 memcpy(pp
, End_player
, sizeof (PLAYER
));
740 fdset
[i
] = fdset
[End_player
- Player
+ 3];
741 fdset
[End_player
- Player
+ 3].fd
= -1;
742 (void) snprintf(Buf
, sizeof(Buf
), "%5.2f%c%-10.10s %c",
743 pp
->p_ident
->i_score
, stat_char(pp
),
744 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
745 n
= STAT_PLAY_ROW
+ 1 + (pp
- Player
);
746 for (np
= Player
; np
< End_player
; np
++) {
747 cgoto(np
, n
, STAT_NAME_COL
);
748 outstr(np
, Buf
, STAT_NAME_LEN
);
751 for (np
= Monitor
; np
< End_monitor
; np
++) {
752 cgoto(np
, n
, STAT_NAME_COL
);
753 outstr(np
, Buf
, STAT_NAME_LEN
);
759 /* Erase the last player */
760 n
= STAT_PLAY_ROW
+ 1 + Nplayer
;
761 for (np
= Player
; np
< End_player
; np
++) {
762 cgoto(np
, n
, STAT_NAME_COL
);
766 for (np
= Monitor
; np
< End_monitor
; np
++) {
767 cgoto(np
, n
, STAT_NAME_COL
);
773 (void) putc(LAST_PLAYER
, pp
->p_output
);
774 (void) fclose(pp
->p_output
);
777 if (pp
!= End_monitor
) {
778 memcpy(pp
, End_monitor
, sizeof (PLAYER
));
779 fdset
[i
] = fdset
[End_monitor
- Monitor
+ MAXPL
+ 3];
780 fdset
[End_monitor
- Monitor
+ MAXPL
+ 3].fd
= -1;
781 (void) snprintf(Buf
, sizeof(Buf
), "%5.5s %-10.10s %c",
783 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
784 n
= STAT_MON_ROW
+ 1 + (pp
- Player
);
785 for (np
= Player
; np
< End_player
; np
++) {
786 cgoto(np
, n
, STAT_NAME_COL
);
787 outstr(np
, Buf
, STAT_NAME_LEN
);
789 for (np
= Monitor
; np
< End_monitor
; np
++) {
790 cgoto(np
, n
, STAT_NAME_COL
);
791 outstr(np
, Buf
, STAT_NAME_LEN
);
796 /* Erase the last monitor */
797 n
= STAT_MON_ROW
+ 1 + (End_monitor
- Monitor
);
798 for (np
= Player
; np
< End_player
; np
++) {
799 cgoto(np
, n
, STAT_NAME_COL
);
802 for (np
= Monitor
; np
< End_monitor
; np
++) {
803 cgoto(np
, n
, STAT_NAME_COL
);
812 * Return a random number in a given range.
817 return (range
== 0 ? 0 : RN
% range
);
822 * Check to see if we have any characters in the input queue; if
823 * we do, read them, stash them away, and return TRUE; else return
827 havechar(PLAYER
*pp
, int i
)
830 if (pp
->p_ncount
< pp
->p_nchar
)
832 if (!(fdset
[i
].revents
& POLLIN
))
836 if ((pp
->p_nchar
= read(pp
->p_fd
, pp
->p_cbuf
, sizeof pp
->p_cbuf
)) <= 0)
848 * Exit with the given value, cleaning up any droppings lying around
855 for (pp
= Player
; pp
< End_player
; pp
++) {
856 cgoto(pp
, HEIGHT
, 0);
858 (void) putc(LAST_PLAYER
, pp
->p_output
);
859 (void) fclose(pp
->p_output
);
862 for (pp
= Monitor
; pp
< End_monitor
; pp
++) {
863 cgoto(pp
, HEIGHT
, 0);
865 (void) putc(LAST_PLAYER
, pp
->p_output
);
866 (void) fclose(pp
->p_output
);
869 (void) close(Socket
);
871 (void) unlink(Sock_name
);
879 * Print stats to requestor
891 * Get the output stream ready
894 socklen
= sizeof sockstruct
;
896 socklen
= sizeof sockstruct
- 1;
898 s
= accept(Status
, (struct sockaddr
*) &sockstruct
, &socklen
);
903 syslog(LOG_WARNING
, "accept: %m");
912 syslog(LOG_WARNING
, "fdopen: %m");
921 * Send output to requestor
923 fputs("Name\t\tScore\tDucked\tAbsorb\tFaced\tShot\tRobbed\tMissed\tSlimeK\n", fp
);
924 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
) {
925 fprintf(fp
, "%s\t", ip
->i_name
);
926 if (strlen(ip
->i_name
) < 8)
928 fprintf(fp
, "%.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
929 ip
->i_score
, ip
->i_ducked
, ip
->i_absorbed
,
930 ip
->i_faced
, ip
->i_shot
, ip
->i_robbed
,
931 ip
->i_missed
, ip
->i_slime
);
933 fputs("\n\nName\t\tEnemy\tFriend\tDeaths\tStill\tSaved\n", fp
);
934 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
) {
935 if (ip
->i_team
== ' ') {
936 fprintf(fp
, "%s\t", ip
->i_name
);
937 if (strlen(ip
->i_name
) < 8)
941 fprintf(fp
, "%s[%c]\t", ip
->i_name
, ip
->i_team
);
942 if (strlen(ip
->i_name
) + 3 < 8)
945 fprintf(fp
, "%d\t%d\t%d\t%d\t%d\n",
946 ip
->i_gkills
, ip
->i_bkills
, ip
->i_deaths
,
947 ip
->i_stillb
, ip
->i_saved
);
955 * Clear out the scores so the next session start clean
962 for (ip
= Scores
; ip
!= NULL
; ip
= nextip
) {