]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/driver.c
1 /* $NetBSD: driver.c,v 1.7 2001/01/09 23:49:54 lukem Exp $ */
4 * Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
5 * San Francisco, California
10 __RCSID("$NetBSD: driver.c,v 1.7 2001/01/09 23:49:54 lukem Exp $");
13 # include <sys/ioctl.h>
14 # include <sys/stat.h>
15 # include <sys/time.h>
24 # define RN (((Seed = Seed * 11109 + 13849) >> 16) & 0xffff)
26 # define RN ((Seed = Seed * 11109 + 13849) & 0x7fff)
33 char *First_arg
; /* pointer to argv[0] */
34 char *Last_arg
; /* pointer to end of argv/environ */
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)
43 # define DAEMON_SIZE (sizeof Daemon - 1)
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
));
68 short port_num
, reply
;
72 static fd_set read_fds
;
73 static FLAG first
= TRUE
;
74 static FLAG server
= FALSE
;
76 static struct timeval linger
= { 90, 0 };
79 if (ep
== NULL
|| *ep
== NULL
)
83 Last_arg
= ep
[-1] + strlen(ep
[-1]);
85 while ((c
= getopt(ac
, av
, "sp:")) != -1) {
92 standard_port
= FALSE
;
93 Test_port
= atoi(optarg
);
98 fprintf(stderr
, "Usage: %s [-s] [-p port]\n", av
[0]);
112 while (select(Num_fds
, &read_fds
, NULL
, NULL
, NULL
) < 0)
116 syslog(LOG_WARNING
, "select: %m");
124 if (FD_ISSET(Test_socket
, &read_fds
)) {
125 namelen
= DAEMON_SIZE
;
126 port_num
= htons(sock_port
);
127 (void) recvfrom(Test_socket
, (char *) &msg
, sizeof msg
,
128 0, (struct sockaddr
*) &test
, &namelen
);
129 switch (ntohs(msg
)) {
133 reply
= htons((u_short
) Nplayer
);
134 (void) sendto(Test_socket
, (char *) &reply
,
136 (struct sockaddr
*) &test
, DAEMON_SIZE
);
139 reply
= htons(stat_port
);
140 (void) sendto(Test_socket
, (char *) &reply
,
142 (struct sockaddr
*) &test
, DAEMON_SIZE
);
146 if (msg
== C_MONITOR
&& Nplayer
<= 0)
148 reply
= htons(sock_port
);
149 (void) sendto(Test_socket
, (char *) &reply
,
151 (struct sockaddr
*) &test
, DAEMON_SIZE
);
158 for (pp
= Player
; pp
< End_player
; pp
++)
165 for (pp
= Monitor
; pp
< End_monitor
; pp
++)
175 for (pp
= Player
; pp
< End_player
; )
176 if (pp
->p_death
[0] != '\0')
181 for (pp
= Monitor
; pp
< End_monitor
; )
182 if (pp
->p_death
[0] != '\0')
188 if (FD_ISSET(Socket
, &read_fds
))
191 if (first
&& standard_port
)
196 if (FD_ISSET(Status
, &read_fds
))
198 for (pp
= Player
; pp
< End_player
; pp
++) {
199 if (FD_ISSET(pp
->p_fd
, &read_fds
))
200 sendcom(pp
, READY
, pp
->p_nexec
);
202 (void) fflush(pp
->p_output
);
205 for (pp
= Monitor
; pp
< End_monitor
; pp
++) {
206 if (FD_ISSET(pp
->p_fd
, &read_fds
))
207 sendcom(pp
, READY
, pp
->p_nexec
);
209 (void) fflush(pp
->p_output
);
212 } while (Nplayer
> 0);
215 if (select(Num_fds
, &read_fds
, NULL
, NULL
, &linger
) > 0) {
230 for (pp
= Monitor
; pp
< End_monitor
; )
240 * Initialize the global parameters.
254 (void) ioctl(fileno(stdout
), TIOCNOTTY
, NULL
);
256 (void) setpgrp(getpid(), getpid());
257 (void) signal(SIGHUP
, SIG_IGN
);
258 (void) signal(SIGINT
, SIG_IGN
);
259 (void) signal(SIGQUIT
, SIG_IGN
);
260 (void) signal(SIGTERM
, cleanup
);
263 (void) chdir("/var/tmp"); /* just in case it core dumps */
264 (void) umask(0); /* No privacy at all! */
265 (void) signal(SIGPIPE
, SIG_IGN
);
269 openlog("huntd", LOG_PID
, LOG_DAEMON
);
272 openlog("huntd", LOG_PID
);
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
)
294 syslog(LOG_ERR
, "bind: %m");
301 (void) listen(Status
, 5);
304 len
= sizeof (SOCKET
);
305 if (getsockname(Status
, (struct sockaddr
*) &Daemon
, &len
) < 0) {
307 syslog(LOG_ERR
, "getsockname: %m");
313 stat_port
= ntohs(Daemon
.sin_port
);
317 * Initialize main socket
320 Daemon
.sin_family
= SOCK_FAMILY
;
321 Daemon
.sin_addr
.s_addr
= INADDR_ANY
;
324 Daemon
.sun_family
= SOCK_FAMILY
;
325 (void) strcpy(Daemon
.sun_path
, Sock_name
);
328 Socket
= socket(SOCK_FAMILY
, SOCK_STREAM
, 0);
329 # if defined(INTERNET)
331 if (setsockopt(Socket
, SOL_SOCKET
, SO_USELOOPBACK
, &msg
, sizeof msg
)<0)
333 syslog(LOG_WARNING
, "setsockopt loopback %m");
335 warn("setsockopt loopback");
338 if (bind(Socket
, (struct sockaddr
*) &Daemon
, DAEMON_SIZE
) < 0) {
339 if (errno
== EADDRINUSE
)
343 syslog(LOG_ERR
, "bind: %m");
350 (void) listen(Socket
, 5);
353 len
= sizeof (SOCKET
);
354 if (getsockname(Socket
, (struct sockaddr
*) &Daemon
, &len
) < 0) {
356 syslog(LOG_ERR
, "getsockname: %m");
362 sock_port
= ntohs(Daemon
.sin_port
);
366 * Initialize minimal select mask
369 FD_SET(Socket
, &Fds_mask
);
370 FD_SET(Status
, &Fds_mask
);
371 Num_fds
= ((Socket
> Status
) ? Socket
: Status
) + 1;
374 len
= sizeof (SOCKET
);
375 if (getsockname(0, (struct sockaddr
*) &test_port
, &len
) >= 0
376 && test_port
.sin_family
== AF_INET
) {
377 inetd_spawned
= TRUE
;
379 if (test_port
.sin_port
!= htons((u_short
) Test_port
)) {
380 standard_port
= FALSE
;
381 Test_port
= ntohs(test_port
.sin_port
);
385 test_port
.sin_port
= htons((u_short
) Test_port
);
387 Test_socket
= socket(SOCK_FAMILY
, SOCK_DGRAM
, 0);
388 if (bind(Test_socket
, (struct sockaddr
*) &test_port
,
391 syslog(LOG_ERR
, "bind: %m");
397 (void) listen(Test_socket
, 5);
400 FD_SET(Test_socket
, &Fds_mask
);
401 if (Test_socket
+ 1 > Num_fds
)
402 Num_fds
= Test_socket
+ 1;
405 Seed
= getpid() + time((time_t *) NULL
);
411 for (i
= 0; i
< NASCII
; i
++)
413 See_over
[DOOR
] = FALSE
;
414 See_over
[WALL1
] = FALSE
;
415 See_over
[WALL2
] = FALSE
;
416 See_over
[WALL3
] = FALSE
;
418 See_over
[WALL4
] = FALSE
;
419 See_over
[WALL5
] = FALSE
;
427 * Put the boots in the maze
436 x
= rand_num(WIDTH
- 1) + 1;
437 y
= rand_num(HEIGHT
- 1) + 1;
438 } while (Maze
[y
][x
] != SPACE
);
439 Maze
[y
][x
] = BOOT_PAIR
;
440 for (pp
= Boot
; pp
< &Boot
[NBOOTS
]; pp
++)
448 * Check the damage to the given player, and see if s/he is killed
451 checkdam(ouch
, gotcha
, credit
, amt
, shot_type
)
452 PLAYER
*ouch
, *gotcha
;
459 if (ouch
->p_death
[0] != '\0')
462 if (shot_type
== SLIME
)
463 switch (ouch
->p_nboots
) {
471 message(gotcha
, "He has boots on!");
475 ouch
->p_damage
+= amt
;
476 if (ouch
->p_damage
<= ouch
->p_damcap
) {
477 (void) sprintf(Buf
, "%2d", ouch
->p_damage
);
478 cgoto(ouch
, STAT_DAM_ROW
, STAT_VALUE_COL
);
479 outstr(ouch
, Buf
, 2);
490 cp
= "Killed on impact";
494 cp
= "Stabbed to death";
495 ouch
->p_ammo
= 0; /* No exploding */
498 cp
= "Shot to death";
527 if (credit
== NULL
) {
528 (void) sprintf(ouch
->p_death
, "| %s by %s |", cp
,
529 (shot_type
== MINE
|| shot_type
== GMINE
) ?
530 "a mine" : "act of God");
534 (void) sprintf(ouch
->p_death
, "| %s by %s |", cp
, credit
->i_name
);
536 if (ouch
== gotcha
) { /* No use killing yourself */
540 else if (ouch
->p_ident
->i_team
== ' '
541 || ouch
->p_ident
->i_team
!= credit
->i_team
) {
549 credit
->i_score
= credit
->i_kills
/ (double) credit
->i_entries
;
550 ouch
->p_ident
->i_deaths
++;
551 if (ouch
->p_nchar
== 0)
552 ouch
->p_ident
->i_stillb
++;
555 gotcha
->p_damcap
+= STABDAM
;
556 gotcha
->p_damage
-= STABDAM
;
557 if (gotcha
->p_damage
< 0)
558 gotcha
->p_damage
= 0;
559 (void) sprintf(Buf
, "%2d/%2d", gotcha
->p_damage
, gotcha
->p_damcap
);
560 cgoto(gotcha
, STAT_DAM_ROW
, STAT_VALUE_COL
);
561 outstr(gotcha
, Buf
, 5);
562 (void) sprintf(Buf
, "%3d", (gotcha
->p_damcap
- MAXDAM
) / 2);
563 cgoto(gotcha
, STAT_KILL_ROW
, STAT_VALUE_COL
);
564 outstr(gotcha
, Buf
, 3);
565 (void) sprintf(Buf
, "%5.2f", gotcha
->p_ident
->i_score
);
566 for (ouch
= Player
; ouch
< End_player
; ouch
++) {
567 cgoto(ouch
, STAT_PLAY_ROW
+ 1 + (gotcha
- Player
),
569 outstr(ouch
, Buf
, 5);
572 for (ouch
= Monitor
; ouch
< End_monitor
; ouch
++) {
573 cgoto(ouch
, STAT_PLAY_ROW
+ 1 + (gotcha
- Player
),
575 outstr(ouch
, Buf
, 5);
582 * Kill off a player and take him out of the game.
597 fixshots(pp
->p_y
, pp
->p_x
, pp
->p_over
);
598 drawplayer(pp
, FALSE
);
602 len
= strlen(pp
->p_death
); /* Display the cause of death */
603 x
= (WIDTH
- len
) / 2;
604 cgoto(pp
, HEIGHT
/ 2, x
);
605 outstr(pp
, pp
->p_death
, len
);
606 for (i
= 1; i
< len
; i
++)
607 pp
->p_death
[i
] = '-';
608 pp
->p_death
[0] = '+';
609 pp
->p_death
[len
- 1] = '+';
610 cgoto(pp
, HEIGHT
/ 2 - 1, x
);
611 outstr(pp
, pp
->p_death
, len
);
612 cgoto(pp
, HEIGHT
/ 2 + 1, x
);
613 outstr(pp
, pp
->p_death
, len
);
614 cgoto(pp
, HEIGHT
, 0);
621 for (bp
= Bullets
; bp
!= NULL
; bp
= bp
->b_next
) {
622 if (bp
->b_owner
== pp
)
624 if (bp
->b_x
== pp
->p_x
&& bp
->b_y
== pp
->p_y
)
628 i
= rand_num(pp
->p_ammo
);
629 x
= rand_num(pp
->p_ammo
);
634 else if (i
== pp
->p_ammo
- 1) {
639 for (x
= MAXBOMB
- 1; x
> 0; x
--)
640 if (i
>= shot_req
[x
])
642 for (y
= MAXSLIME
- 1; y
> 0; y
--)
643 if (i
>= slime_req
[y
])
645 if (y
>= 0 && slime_req
[y
] > shot_req
[x
]) {
655 (void) add_shot(len
, pp
->p_y
, pp
->p_x
, pp
->p_face
, x
,
656 (PLAYER
*) NULL
, TRUE
, SPACE
);
657 (void) sprintf(Buf
, "%s detonated.",
658 pp
->p_ident
->i_name
);
659 for (np
= Player
; np
< End_player
; np
++)
662 for (np
= Monitor
; np
< End_monitor
; np
++)
666 while (pp
->p_nboots
-- > 0) {
667 for (np
= Boot
; np
< &Boot
[NBOOTS
]; np
++)
668 if (np
->p_flying
< 0)
670 if (np
>= &Boot
[NBOOTS
])
671 err(1, "Too many boots");
672 np
->p_undershot
= FALSE
;
675 np
->p_flying
= rand_num(20);
676 np
->p_flyx
= 2 * rand_num(6) - 5;
677 np
->p_flyy
= 2 * rand_num(6) - 5;
680 showexpl(np
->p_y
, np
->p_x
, BOOT
);
685 else if (pp
->p_nboots
> 0) {
686 if (pp
->p_nboots
== 2)
687 Maze
[pp
->p_y
][pp
->p_x
] = BOOT_PAIR
;
689 Maze
[pp
->p_y
][pp
->p_x
] = BOOT
;
691 fixshots(pp
->p_y
, pp
->p_x
,
692 Maze
[pp
->p_y
][pp
->p_x
]);
697 volcano
+= pp
->p_ammo
- x
;
698 if (rand_num(100) < volcano
/ 50) {
700 x
= rand_num(WIDTH
/ 2) + WIDTH
/ 4;
701 y
= rand_num(HEIGHT
/ 2) + HEIGHT
/ 4;
702 } while (Maze
[y
][x
] != SPACE
);
703 (void) add_shot(LAVA
, y
, x
, LEFTS
, volcano
,
704 (PLAYER
*) NULL
, TRUE
, SPACE
);
705 for (np
= Player
; np
< End_player
; np
++)
706 message(np
, "Volcano eruption.");
712 if (rand_num(100) < 2) {
714 x
= rand_num(WIDTH
/ 2) + WIDTH
/ 4;
715 y
= rand_num(HEIGHT
/ 2) + HEIGHT
/ 4;
716 } while (Maze
[y
][x
] != SPACE
);
717 add_shot(DSHOT
, y
, x
, rand_dir(),
719 rand_num(MAXBOMB
- MINDSHOT
)],
720 (PLAYER
*) NULL
, FALSE
, SPACE
);
725 (void) putc(' ', pp
->p_output
);
726 (void) fclose(pp
->p_output
);
729 if (pp
!= End_player
) {
730 memcpy(pp
, End_player
, sizeof (PLAYER
));
731 (void) sprintf(Buf
, "%5.2f%c%-10.10s %c",
732 pp
->p_ident
->i_score
, stat_char(pp
),
733 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
734 i
= STAT_PLAY_ROW
+ 1 + (pp
- Player
);
735 for (np
= Player
; np
< End_player
; np
++) {
736 cgoto(np
, i
, STAT_NAME_COL
);
737 outstr(np
, Buf
, STAT_NAME_LEN
);
740 for (np
= Monitor
; np
< End_monitor
; np
++) {
741 cgoto(np
, i
, STAT_NAME_COL
);
742 outstr(np
, Buf
, STAT_NAME_LEN
);
747 /* Erase the last player */
748 i
= STAT_PLAY_ROW
+ 1 + Nplayer
;
749 for (np
= Player
; np
< End_player
; np
++) {
750 cgoto(np
, i
, STAT_NAME_COL
);
754 for (np
= Monitor
; np
< End_monitor
; np
++) {
755 cgoto(np
, i
, STAT_NAME_COL
);
761 (void) putc(LAST_PLAYER
, pp
->p_output
);
762 (void) fclose(pp
->p_output
);
765 if (pp
!= End_monitor
) {
766 memcpy(pp
, End_monitor
, sizeof (PLAYER
));
767 (void) sprintf(Buf
, "%5.5s %-10.10s %c", " ",
768 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
769 i
= STAT_MON_ROW
+ 1 + (pp
- Player
);
770 for (np
= Player
; np
< End_player
; np
++) {
771 cgoto(np
, i
, STAT_NAME_COL
);
772 outstr(np
, Buf
, STAT_NAME_LEN
);
774 for (np
= Monitor
; np
< End_monitor
; np
++) {
775 cgoto(np
, i
, STAT_NAME_COL
);
776 outstr(np
, Buf
, STAT_NAME_LEN
);
780 /* Erase the last monitor */
781 i
= STAT_MON_ROW
+ 1 + (End_monitor
- Monitor
);
782 for (np
= Player
; np
< End_player
; np
++) {
783 cgoto(np
, i
, STAT_NAME_COL
);
786 for (np
= Monitor
; np
< End_monitor
; np
++) {
787 cgoto(np
, i
, STAT_NAME_COL
);
794 FD_CLR(savefd
, &Fds_mask
);
795 if (Num_fds
== savefd
+ 1) {
798 if (Test_socket
> Socket
)
799 Num_fds
= Test_socket
;
801 for (np
= Player
; np
< End_player
; np
++)
802 if (np
->p_fd
> Num_fds
)
805 for (np
= Monitor
; np
< End_monitor
; np
++)
806 if (np
->p_fd
> Num_fds
)
815 * Return a random number in a given range.
821 return (range
== 0 ? 0 : RN
% range
);
826 * Check to see if we have any characters in the input queue; if
827 * we do, read them, stash them away, and return TRUE; else return
835 if (pp
->p_ncount
< pp
->p_nchar
)
837 if (!FD_ISSET(pp
->p_fd
, &Have_inp
))
839 FD_CLR(pp
->p_fd
, &Have_inp
);
842 if ((pp
->p_nchar
= read(pp
->p_fd
, pp
->p_cbuf
, sizeof pp
->p_cbuf
)) <= 0)
854 * Exit with the given value, cleaning up any droppings lying around
862 for (pp
= Player
; pp
< End_player
; pp
++) {
863 cgoto(pp
, HEIGHT
, 0);
865 (void) putc(LAST_PLAYER
, pp
->p_output
);
866 (void) fclose(pp
->p_output
);
869 for (pp
= Monitor
; pp
< End_monitor
; pp
++) {
870 cgoto(pp
, HEIGHT
, 0);
872 (void) putc(LAST_PLAYER
, pp
->p_output
);
873 (void) fclose(pp
->p_output
);
876 (void) close(Socket
);
878 (void) unlink(Sock_name
);
886 * Print stats to requestor
898 * Get the output stream ready
901 socklen
= sizeof sockstruct
;
903 socklen
= sizeof sockstruct
- 1;
905 s
= accept(Status
, (struct sockaddr
*) &sockstruct
, &socklen
);
910 syslog(LOG_WARNING
, "accept: %m");
919 syslog(LOG_WARNING
, "fdopen: %m");
928 * Send output to requestor
930 fputs("Name\t\tScore\tDucked\tAbsorb\tFaced\tShot\tRobbed\tMissed\tSlimeK\n", fp
);
931 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
) {
932 fprintf(fp
, "%s\t", ip
->i_name
);
933 if (strlen(ip
->i_name
) < 8)
935 fprintf(fp
, "%.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
936 ip
->i_score
, ip
->i_ducked
, ip
->i_absorbed
,
937 ip
->i_faced
, ip
->i_shot
, ip
->i_robbed
,
938 ip
->i_missed
, ip
->i_slime
);
940 fputs("\n\nName\t\tEnemy\tFriend\tDeaths\tStill\tSaved\n", fp
);
941 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
) {
942 if (ip
->i_team
== ' ') {
943 fprintf(fp
, "%s\t", ip
->i_name
);
944 if (strlen(ip
->i_name
) < 8)
948 fprintf(fp
, "%s[%c]\t", ip
->i_name
, ip
->i_team
);
949 if (strlen(ip
->i_name
) + 3 < 8)
952 fprintf(fp
, "%d\t%d\t%d\t%d\t%d\n",
953 ip
->i_gkills
, ip
->i_bkills
, ip
->i_deaths
,
954 ip
->i_stillb
, ip
->i_saved
);
962 * Clear out the scores so the next session start clean
969 for (ip
= Scores
; ip
!= NULL
; ip
= nextip
) {
971 (void) free((char *) ip
);