]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/driver.c
1 /* $NetBSD: driver.c,v 1.5 1997/10/20 00:37:16 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.5 1997/10/20 00:37:16 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
;
78 static struct timeval linger
= { 90, 0 };
81 if (ep
== NULL
|| *ep
== NULL
)
85 Last_arg
= ep
[-1] + strlen(ep
[-1]);
87 while ((c
= getopt(ac
, av
, "sp:")) != -1) {
94 standard_port
= FALSE
;
95 Test_port
= atoi(optarg
);
100 fprintf(stderr
, "Usage: %s [-s] [-p port]\n", av
[0]);
114 while (select(Num_fds
, &read_fds
, NULL
, NULL
, NULL
) < 0)
118 syslog(LOG_WARNING
, "select: %m");
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
)) {
135 reply
= htons((u_short
) Nplayer
);
136 (void) sendto(Test_socket
, (char *) &reply
,
138 (struct sockaddr
*) &test
, DAEMON_SIZE
);
141 reply
= htons(stat_port
);
142 (void) sendto(Test_socket
, (char *) &reply
,
144 (struct sockaddr
*) &test
, DAEMON_SIZE
);
148 if (msg
== C_MONITOR
&& Nplayer
<= 0)
150 reply
= htons(sock_port
);
151 (void) sendto(Test_socket
, (char *) &reply
,
153 (struct sockaddr
*) &test
, DAEMON_SIZE
);
160 for (pp
= Player
; pp
< End_player
; pp
++)
167 for (pp
= Monitor
; pp
< End_monitor
; pp
++)
177 for (pp
= Player
; pp
< End_player
; )
178 if (pp
->p_death
[0] != '\0')
183 for (pp
= Monitor
; pp
< End_monitor
; )
184 if (pp
->p_death
[0] != '\0')
190 if (FD_ISSET(Socket
, &read_fds
))
193 if (first
&& standard_port
)
198 if (FD_ISSET(Status
, &read_fds
))
200 for (pp
= Player
; pp
< End_player
; pp
++) {
201 if (FD_ISSET(pp
->p_fd
, &read_fds
))
202 sendcom(pp
, READY
, pp
->p_nexec
);
204 (void) fflush(pp
->p_output
);
207 for (pp
= Monitor
; pp
< End_monitor
; pp
++) {
208 if (FD_ISSET(pp
->p_fd
, &read_fds
))
209 sendcom(pp
, READY
, pp
->p_nexec
);
211 (void) fflush(pp
->p_output
);
214 } while (Nplayer
> 0);
217 if (select(Num_fds
, &read_fds
, NULL
, NULL
, &linger
) > 0) {
232 for (pp
= Monitor
; pp
< End_monitor
; )
242 * Initialize the global parameters.
256 (void) ioctl(fileno(stdout
), TIOCNOTTY
, NULL
);
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
);
265 (void) chdir("/var/tmp"); /* just in case it core dumps */
266 (void) umask(0); /* No privacy at all! */
267 (void) signal(SIGPIPE
, SIG_IGN
);
271 openlog("HUNT", LOG_PID
, LOG_DAEMON
);
274 openlog("HUNT", LOG_PID
);
279 * Initialize statistics socket
282 Daemon
.sin_family
= SOCK_FAMILY
;
283 Daemon
.sin_addr
.s_addr
= INADDR_ANY
;
286 Daemon
.sun_family
= SOCK_FAMILY
;
287 (void) strcpy(Daemon
.sun_path
, Stat_name
);
290 Status
= socket(SOCK_FAMILY
, SOCK_STREAM
, 0);
291 if (bind(Status
, (struct sockaddr
*) &Daemon
, DAEMON_SIZE
) < 0) {
292 if (errno
== EADDRINUSE
)
296 syslog(LOG_ERR
, "bind: %m");
303 (void) listen(Status
, 5);
306 len
= sizeof (SOCKET
);
307 if (getsockname(Status
, (struct sockaddr
*) &Daemon
, &len
) < 0) {
309 syslog(LOG_ERR
, "getsockname: %m");
315 stat_port
= ntohs(Daemon
.sin_port
);
319 * Initialize main socket
322 Daemon
.sin_family
= SOCK_FAMILY
;
323 Daemon
.sin_addr
.s_addr
= INADDR_ANY
;
326 Daemon
.sun_family
= SOCK_FAMILY
;
327 (void) strcpy(Daemon
.sun_path
, Sock_name
);
330 Socket
= socket(SOCK_FAMILY
, SOCK_STREAM
, 0);
331 # if defined(INTERNET)
333 if (setsockopt(Socket
, SOL_SOCKET
, SO_USELOOPBACK
, &msg
, sizeof msg
)<0)
335 syslog(LOG_WARNING
, "setsockopt loopback %m");
337 warn("setsockopt loopback");
340 if (bind(Socket
, (struct sockaddr
*) &Daemon
, DAEMON_SIZE
) < 0) {
341 if (errno
== EADDRINUSE
)
345 syslog(LOG_ERR
, "bind: %m");
352 (void) listen(Socket
, 5);
355 len
= sizeof (SOCKET
);
356 if (getsockname(Socket
, (struct sockaddr
*) &Daemon
, &len
) < 0) {
358 syslog(LOG_ERR
, "getsockname: %m");
364 sock_port
= ntohs(Daemon
.sin_port
);
368 * Initialize minimal select mask
371 FD_SET(Socket
, &Fds_mask
);
372 FD_SET(Status
, &Fds_mask
);
373 Num_fds
= ((Socket
> Status
) ? Socket
: Status
) + 1;
376 len
= sizeof (SOCKET
);
377 if (getsockname(0, (struct sockaddr
*) &test_port
, &len
) >= 0
378 && test_port
.sin_family
== AF_INET
) {
379 inetd_spawned
= TRUE
;
381 if (test_port
.sin_port
!= htons((u_short
) Test_port
)) {
382 standard_port
= FALSE
;
383 Test_port
= ntohs(test_port
.sin_port
);
387 test_port
.sin_port
= htons((u_short
) Test_port
);
389 Test_socket
= socket(SOCK_FAMILY
, SOCK_DGRAM
, 0);
390 if (bind(Test_socket
, (struct sockaddr
*) &test_port
,
393 syslog(LOG_ERR
, "bind: %m");
399 (void) listen(Test_socket
, 5);
402 FD_SET(Test_socket
, &Fds_mask
);
403 if (Test_socket
+ 1 > Num_fds
)
404 Num_fds
= Test_socket
+ 1;
407 Seed
= getpid() + time((time_t *) NULL
);
413 for (i
= 0; i
< NASCII
; i
++)
415 See_over
[DOOR
] = FALSE
;
416 See_over
[WALL1
] = FALSE
;
417 See_over
[WALL2
] = FALSE
;
418 See_over
[WALL3
] = FALSE
;
420 See_over
[WALL4
] = FALSE
;
421 See_over
[WALL5
] = FALSE
;
429 * Put the boots in the maze
438 x
= rand_num(WIDTH
- 1) + 1;
439 y
= rand_num(HEIGHT
- 1) + 1;
440 } while (Maze
[y
][x
] != SPACE
);
441 Maze
[y
][x
] = BOOT_PAIR
;
442 for (pp
= Boot
; pp
< &Boot
[NBOOTS
]; pp
++)
450 * Check the damage to the given player, and see if s/he is killed
453 checkdam(ouch
, gotcha
, credit
, amt
, shot_type
)
454 PLAYER
*ouch
, *gotcha
;
461 if (ouch
->p_death
[0] != '\0')
464 if (shot_type
== SLIME
)
465 switch (ouch
->p_nboots
) {
473 message(gotcha
, "He has boots on!");
477 ouch
->p_damage
+= amt
;
478 if (ouch
->p_damage
<= ouch
->p_damcap
) {
479 (void) sprintf(Buf
, "%2d", ouch
->p_damage
);
480 cgoto(ouch
, STAT_DAM_ROW
, STAT_VALUE_COL
);
481 outstr(ouch
, Buf
, 2);
492 cp
= "Killed on impact";
496 cp
= "Stabbed to death";
497 ouch
->p_ammo
= 0; /* No exploding */
500 cp
= "Shot to death";
529 if (credit
== NULL
) {
530 (void) sprintf(ouch
->p_death
, "| %s by %s |", cp
,
531 (shot_type
== MINE
|| shot_type
== GMINE
) ?
532 "a mine" : "act of God");
536 (void) sprintf(ouch
->p_death
, "| %s by %s |", cp
, credit
->i_name
);
538 if (ouch
== gotcha
) { /* No use killing yourself */
542 else if (ouch
->p_ident
->i_team
== ' '
543 || ouch
->p_ident
->i_team
!= credit
->i_team
) {
551 credit
->i_score
= credit
->i_kills
/ (double) credit
->i_entries
;
552 ouch
->p_ident
->i_deaths
++;
553 if (ouch
->p_nchar
== 0)
554 ouch
->p_ident
->i_stillb
++;
557 gotcha
->p_damcap
+= STABDAM
;
558 gotcha
->p_damage
-= STABDAM
;
559 if (gotcha
->p_damage
< 0)
560 gotcha
->p_damage
= 0;
561 (void) sprintf(Buf
, "%2d/%2d", gotcha
->p_damage
, gotcha
->p_damcap
);
562 cgoto(gotcha
, STAT_DAM_ROW
, STAT_VALUE_COL
);
563 outstr(gotcha
, Buf
, 5);
564 (void) sprintf(Buf
, "%3d", (gotcha
->p_damcap
- MAXDAM
) / 2);
565 cgoto(gotcha
, STAT_KILL_ROW
, STAT_VALUE_COL
);
566 outstr(gotcha
, Buf
, 3);
567 (void) sprintf(Buf
, "%5.2f", gotcha
->p_ident
->i_score
);
568 for (ouch
= Player
; ouch
< End_player
; ouch
++) {
569 cgoto(ouch
, STAT_PLAY_ROW
+ 1 + (gotcha
- Player
),
571 outstr(ouch
, Buf
, 5);
574 for (ouch
= Monitor
; ouch
< End_monitor
; ouch
++) {
575 cgoto(ouch
, STAT_PLAY_ROW
+ 1 + (gotcha
- Player
),
577 outstr(ouch
, Buf
, 5);
584 * Kill off a player and take him out of the game.
599 fixshots(pp
->p_y
, pp
->p_x
, pp
->p_over
);
600 drawplayer(pp
, FALSE
);
604 len
= strlen(pp
->p_death
); /* Display the cause of death */
605 x
= (WIDTH
- len
) / 2;
606 cgoto(pp
, HEIGHT
/ 2, x
);
607 outstr(pp
, pp
->p_death
, len
);
608 for (i
= 1; i
< len
; i
++)
609 pp
->p_death
[i
] = '-';
610 pp
->p_death
[0] = '+';
611 pp
->p_death
[len
- 1] = '+';
612 cgoto(pp
, HEIGHT
/ 2 - 1, x
);
613 outstr(pp
, pp
->p_death
, len
);
614 cgoto(pp
, HEIGHT
/ 2 + 1, x
);
615 outstr(pp
, pp
->p_death
, len
);
616 cgoto(pp
, HEIGHT
, 0);
623 for (bp
= Bullets
; bp
!= NULL
; bp
= bp
->b_next
) {
624 if (bp
->b_owner
== pp
)
626 if (bp
->b_x
== pp
->p_x
&& bp
->b_y
== pp
->p_y
)
630 i
= rand_num(pp
->p_ammo
);
631 x
= rand_num(pp
->p_ammo
);
636 else if (i
== pp
->p_ammo
- 1) {
641 for (x
= MAXBOMB
- 1; x
> 0; x
--)
642 if (i
>= shot_req
[x
])
644 for (y
= MAXSLIME
- 1; y
> 0; y
--)
645 if (i
>= slime_req
[y
])
647 if (y
>= 0 && slime_req
[y
] > shot_req
[x
]) {
657 (void) add_shot(len
, pp
->p_y
, pp
->p_x
, pp
->p_face
, x
,
658 (PLAYER
*) NULL
, TRUE
, SPACE
);
659 (void) sprintf(Buf
, "%s detonated.",
660 pp
->p_ident
->i_name
);
661 for (np
= Player
; np
< End_player
; np
++)
664 for (np
= Monitor
; np
< End_monitor
; np
++)
668 while (pp
->p_nboots
-- > 0) {
669 for (np
= Boot
; np
< &Boot
[NBOOTS
]; np
++)
670 if (np
->p_flying
< 0)
672 if (np
>= &Boot
[NBOOTS
])
673 err(1, "Too many boots");
674 np
->p_undershot
= FALSE
;
677 np
->p_flying
= rand_num(20);
678 np
->p_flyx
= 2 * rand_num(6) - 5;
679 np
->p_flyy
= 2 * rand_num(6) - 5;
682 showexpl(np
->p_y
, np
->p_x
, BOOT
);
687 else if (pp
->p_nboots
> 0) {
688 if (pp
->p_nboots
== 2)
689 Maze
[pp
->p_y
][pp
->p_x
] = BOOT_PAIR
;
691 Maze
[pp
->p_y
][pp
->p_x
] = BOOT
;
693 fixshots(pp
->p_y
, pp
->p_x
,
694 Maze
[pp
->p_y
][pp
->p_x
]);
699 volcano
+= pp
->p_ammo
- x
;
700 if (rand_num(100) < volcano
/ 50) {
702 x
= rand_num(WIDTH
/ 2) + WIDTH
/ 4;
703 y
= rand_num(HEIGHT
/ 2) + HEIGHT
/ 4;
704 } while (Maze
[y
][x
] != SPACE
);
705 (void) add_shot(LAVA
, y
, x
, LEFTS
, volcano
,
706 (PLAYER
*) NULL
, TRUE
, SPACE
);
707 for (np
= Player
; np
< End_player
; np
++)
708 message(np
, "Volcano eruption.");
714 if (rand_num(100) < 2) {
716 x
= rand_num(WIDTH
/ 2) + WIDTH
/ 4;
717 y
= rand_num(HEIGHT
/ 2) + HEIGHT
/ 4;
718 } while (Maze
[y
][x
] != SPACE
);
719 add_shot(DSHOT
, y
, x
, rand_dir(),
721 rand_num(MAXBOMB
- MINDSHOT
)],
722 (PLAYER
*) NULL
, FALSE
, SPACE
);
727 (void) putc(' ', pp
->p_output
);
728 (void) fclose(pp
->p_output
);
731 if (pp
!= End_player
) {
732 memcpy(pp
, End_player
, sizeof (PLAYER
));
733 (void) sprintf(Buf
, "%5.2f%c%-10.10s %c",
734 pp
->p_ident
->i_score
, stat_char(pp
),
735 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
736 i
= STAT_PLAY_ROW
+ 1 + (pp
- Player
);
737 for (np
= Player
; np
< End_player
; np
++) {
738 cgoto(np
, i
, STAT_NAME_COL
);
739 outstr(np
, Buf
, STAT_NAME_LEN
);
742 for (np
= Monitor
; np
< End_monitor
; np
++) {
743 cgoto(np
, i
, STAT_NAME_COL
);
744 outstr(np
, Buf
, STAT_NAME_LEN
);
749 /* Erase the last player */
750 i
= STAT_PLAY_ROW
+ 1 + Nplayer
;
751 for (np
= Player
; np
< End_player
; np
++) {
752 cgoto(np
, i
, STAT_NAME_COL
);
756 for (np
= Monitor
; np
< End_monitor
; np
++) {
757 cgoto(np
, i
, STAT_NAME_COL
);
763 (void) putc(LAST_PLAYER
, pp
->p_output
);
764 (void) fclose(pp
->p_output
);
767 if (pp
!= End_monitor
) {
768 memcpy(pp
, End_monitor
, sizeof (PLAYER
));
769 (void) sprintf(Buf
, "%5.5s %-10.10s %c", " ",
770 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
771 i
= STAT_MON_ROW
+ 1 + (pp
- Player
);
772 for (np
= Player
; np
< End_player
; np
++) {
773 cgoto(np
, i
, STAT_NAME_COL
);
774 outstr(np
, Buf
, STAT_NAME_LEN
);
776 for (np
= Monitor
; np
< End_monitor
; np
++) {
777 cgoto(np
, i
, STAT_NAME_COL
);
778 outstr(np
, Buf
, STAT_NAME_LEN
);
782 /* Erase the last monitor */
783 i
= STAT_MON_ROW
+ 1 + (End_monitor
- Monitor
);
784 for (np
= Player
; np
< End_player
; np
++) {
785 cgoto(np
, i
, STAT_NAME_COL
);
788 for (np
= Monitor
; np
< End_monitor
; np
++) {
789 cgoto(np
, i
, STAT_NAME_COL
);
796 FD_CLR(savefd
, &Fds_mask
);
797 if (Num_fds
== savefd
+ 1) {
800 if (Test_socket
> Socket
)
801 Num_fds
= Test_socket
;
803 for (np
= Player
; np
< End_player
; np
++)
804 if (np
->p_fd
> Num_fds
)
807 for (np
= Monitor
; np
< End_monitor
; np
++)
808 if (np
->p_fd
> Num_fds
)
817 * Return a random number in a given range.
823 return (range
== 0 ? 0 : RN
% range
);
828 * Check to see if we have any characters in the input queue; if
829 * we do, read them, stash them away, and return TRUE; else return
837 if (pp
->p_ncount
< pp
->p_nchar
)
839 if (!FD_ISSET(pp
->p_fd
, &Have_inp
))
841 FD_CLR(pp
->p_fd
, &Have_inp
);
844 if ((pp
->p_nchar
= read(pp
->p_fd
, pp
->p_cbuf
, sizeof pp
->p_cbuf
)) <= 0)
856 * Exit with the given value, cleaning up any droppings lying around
864 for (pp
= Player
; pp
< End_player
; pp
++) {
865 cgoto(pp
, HEIGHT
, 0);
867 (void) putc(LAST_PLAYER
, pp
->p_output
);
868 (void) fclose(pp
->p_output
);
871 for (pp
= Monitor
; pp
< End_monitor
; pp
++) {
872 cgoto(pp
, HEIGHT
, 0);
874 (void) putc(LAST_PLAYER
, pp
->p_output
);
875 (void) fclose(pp
->p_output
);
878 (void) close(Socket
);
880 (void) unlink(Sock_name
);
888 * Print stats to requestor
900 * Get the output stream ready
903 socklen
= sizeof sockstruct
;
905 socklen
= sizeof sockstruct
- 1;
907 s
= accept(Status
, (struct sockaddr
*) &sockstruct
, &socklen
);
912 syslog(LOG_ERR
, "accept: %m");
921 syslog(LOG_ERR
, "fdopen: %m");
930 * Send output to requestor
932 fputs("Name\t\tScore\tDucked\tAbsorb\tFaced\tShot\tRobbed\tMissed\tSlimeK\n", fp
);
933 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
) {
934 fprintf(fp
, "%s\t", ip
->i_name
);
935 if (strlen(ip
->i_name
) < 8)
937 fprintf(fp
, "%.2f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
938 ip
->i_score
, ip
->i_ducked
, ip
->i_absorbed
,
939 ip
->i_faced
, ip
->i_shot
, ip
->i_robbed
,
940 ip
->i_missed
, ip
->i_slime
);
942 fputs("\n\nName\t\tEnemy\tFriend\tDeaths\tStill\tSaved\n", fp
);
943 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
) {
944 if (ip
->i_team
== ' ') {
945 fprintf(fp
, "%s\t", ip
->i_name
);
946 if (strlen(ip
->i_name
) < 8)
950 fprintf(fp
, "%s[%c]\t", ip
->i_name
, ip
->i_team
);
951 if (strlen(ip
->i_name
) + 3 < 8)
954 fprintf(fp
, "%d\t%d\t%d\t%d\t%d\n",
955 ip
->i_gkills
, ip
->i_bkills
, ip
->i_deaths
,
956 ip
->i_stillb
, ip
->i_saved
);
964 * Clear out the scores so the next session start clean
971 for (ip
= Scores
; ip
!= NULL
; ip
= nextip
) {
973 (void) free((char *) ip
);