]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/answer.c
1 /* $NetBSD: answer.c,v 1.5 2003/02/26 07:14:45 jdc Exp $ */
4 * Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
5 * San Francisco, California
10 __RCSID("$NetBSD: answer.c,v 1.5 2003/02/26 07:14:45 jdc Exp $");
20 # define SCOREDECAY 15
22 static char Ttyname
[NAMELEN
];
30 static char name
[NAMELEN
];
32 static int enter_status
;
34 static u_long machine
;
36 static SOCKET sockstruct
;
43 socklen
= sizeof sockstruct
;
45 socklen
= sizeof sockstruct
- 1;
48 newsock
= accept(Socket
, (struct sockaddr
*) &sockstruct
, &socklen
);
54 syslog(LOG_ERR
, "accept: %m");
62 machine
= ntohl(((struct sockaddr_in
*) &sockstruct
)->sin_addr
.s_addr
);
65 machine
= gethostid();
67 version
= htonl((u_int32_t
) HUNT_VERSION
);
68 (void) write(newsock
, (char *) &version
, LONGLEN
);
69 (void) read(newsock
, (char *) &uid
, LONGLEN
);
70 uid
= ntohl((unsigned long) uid
);
71 (void) read(newsock
, name
, NAMELEN
);
72 (void) read(newsock
, &team
, 1);
73 (void) read(newsock
, (char *) &enter_status
, LONGLEN
);
74 enter_status
= ntohl((unsigned long) enter_status
);
75 (void) read(newsock
, Ttyname
, NAMELEN
);
76 (void) read(newsock
, (char *) &mode
, sizeof mode
);
80 * Turn off blocking I/O, so a slow or dead terminal won't stop
81 * the game. All subsequent reads check how many bytes they read.
83 flags
= fcntl(newsock
, F_GETFL
, 0);
85 (void) fcntl(newsock
, F_SETFL
, flags
);
88 * Make sure the name contains only printable characters
89 * since we use control characters for cursor control
90 * between driver and player processes
92 for (cp1
= cp2
= name
; *cp1
!= '\0'; cp1
++)
93 if (isprint(*cp1
) || *cp1
== ' ')
98 if (mode
== C_MESSAGE
) {
103 (void) sprintf(buf
, "%s: ", name
);
105 (void) sprintf(buf
, "%s[%c]: ", name
, team
);
107 for (pp
= Player
; pp
< End_player
; pp
++) {
108 cgoto(pp
, HEIGHT
, 0);
111 while ((n
= read(newsock
, buf
, BUFSIZ
)) > 0)
112 for (pp
= Player
; pp
< End_player
; pp
++)
114 for (pp
= Player
; pp
< End_player
; pp
++) {
116 sendcom(pp
, REFRESH
);
117 sendcom(pp
, READY
, 0);
118 (void) fflush(pp
->p_output
);
120 (void) close(newsock
);
126 if (mode
== C_MONITOR
)
127 if (End_monitor
< &Monitor
[MAXMON
]) {
129 i
= pp
- Monitor
+ MAXPL
+ 3;
132 (void) write(newsock
, (char *) &socklen
,
134 (void) close(newsock
);
139 if (End_player
< &Player
[MAXPL
]) {
144 (void) write(newsock
, (char *) &socklen
,
146 (void) close(newsock
);
151 if (mode
== C_MONITOR
&& team
== ' ')
154 pp
->p_ident
= get_ident(machine
, uid
, name
, team
);
155 pp
->p_output
= fdopen(newsock
, "w");
156 pp
->p_death
[0] = '\0';
158 fdset
[i
].fd
= newsock
;
159 fdset
[i
].events
= POLLIN
;
165 if (mode
== C_MONITOR
)
169 stplayer(pp
, enter_status
);
181 memcpy(pp
->p_maze
, Maze
, sizeof Maze
);
185 (void) sprintf(Buf
, "%5.5s%c%-10.10s %c", " ", stat_char(pp
),
186 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
187 line
= STAT_MON_ROW
+ 1 + (pp
- Monitor
);
188 for (npp
= Player
; npp
< End_player
; npp
++) {
189 cgoto(npp
, line
, STAT_NAME_COL
);
190 outstr(npp
, Buf
, STAT_NAME_LEN
);
192 for (npp
= Monitor
; npp
< End_monitor
; npp
++) {
193 cgoto(npp
, line
, STAT_NAME_COL
);
194 outstr(npp
, Buf
, STAT_NAME_LEN
);
197 sendcom(pp
, REFRESH
);
198 sendcom(pp
, READY
, 0);
199 (void) fflush(pp
->p_output
);
204 stplayer(newpp
, enter_status
)
213 for (y
= 0; y
< UBOUND
; y
++)
214 for (x
= 0; x
< WIDTH
; x
++)
215 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
216 for ( ; y
< DBOUND
; y
++) {
217 for (x
= 0; x
< LBOUND
; x
++)
218 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
219 for ( ; x
< RBOUND
; x
++)
220 newpp
->p_maze
[y
][x
] = SPACE
;
221 for ( ; x
< WIDTH
; x
++)
222 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
224 for ( ; y
< HEIGHT
; y
++)
225 for (x
= 0; x
< WIDTH
; x
++)
226 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
229 x
= rand_num(WIDTH
- 1) + 1;
230 y
= rand_num(HEIGHT
- 1) + 1;
231 } while (Maze
[y
][x
] != SPACE
);
232 newpp
->p_over
= SPACE
;
235 newpp
->p_undershot
= FALSE
;
238 if (enter_status
== Q_FLY
) {
239 newpp
->p_flying
= rand_num(20);
240 newpp
->p_flyx
= 2 * rand_num(6) - 5;
241 newpp
->p_flyy
= 2 * rand_num(6) - 5;
242 newpp
->p_face
= FLYER
;
247 newpp
->p_flying
= -1;
248 newpp
->p_face
= rand_dir();
251 newpp
->p_damcap
= MAXDAM
;
255 newpp
->p_ammo
= ISHOTS
;
259 if (enter_status
== Q_SCAN
) {
260 newpp
->p_scan
= SCANLEN
;
265 newpp
->p_cloak
= CLOAKLEN
;
270 x
= rand_num(WIDTH
- 1) + 1;
271 y
= rand_num(HEIGHT
- 1) + 1;
272 } while (Maze
[y
][x
] != SPACE
);
275 for (pp
= Monitor
; pp
< End_monitor
; pp
++)
280 x
= rand_num(WIDTH
- 1) + 1;
281 y
= rand_num(HEIGHT
- 1) + 1;
282 } while (Maze
[y
][x
] != SPACE
);
285 for (pp
= Monitor
; pp
< End_monitor
; pp
++)
289 (void) sprintf(Buf
, "%5.2f%c%-10.10s %c", newpp
->p_ident
->i_score
,
290 stat_char(newpp
), newpp
->p_ident
->i_name
,
291 newpp
->p_ident
->i_team
);
292 y
= STAT_PLAY_ROW
+ 1 + (newpp
- Player
);
293 for (pp
= Player
; pp
< End_player
; pp
++) {
297 pp
->p_ammo
+= NSHOTS
;
298 newpp
->p_ammo
+= NSHOTS
;
299 cgoto(pp
, y
, STAT_NAME_COL
);
300 outstr(pp
, Buf
, STAT_NAME_LEN
);
301 (void) sprintf(smallbuf
, "%3d", pp
->p_ammo
);
302 cgoto(pp
, STAT_AMMO_ROW
, STAT_VALUE_COL
);
303 outstr(pp
, smallbuf
, 3);
307 for (pp
= Monitor
; pp
< End_monitor
; pp
++) {
308 cgoto(pp
, y
, STAT_NAME_COL
);
309 outstr(pp
, Buf
, STAT_NAME_LEN
);
314 drawplayer(newpp
, TRUE
);
317 if (enter_status
== Q_FLY
)
318 /* Make sure that the position you enter in will be erased */
319 showexpl(newpp
->p_y
, newpp
->p_x
, FLYER
);
321 sendcom(newpp
, REFRESH
);
322 sendcom(newpp
, READY
, 0);
323 (void) fflush(newpp
->p_output
);
328 * Return a random direction
333 switch (rand_num(4)) {
349 * Get the score structure of a player
352 get_ident(machine
, uid
, name
, team
)
361 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
)
362 if (ip
->i_machine
== machine
364 && ip
->i_team
== team
365 && strncmp(ip
->i_name
, name
, NAMELEN
) == 0)
369 if (ip
->i_entries
< SCOREDECAY
)
372 ip
->i_kills
= (ip
->i_kills
* (SCOREDECAY
- 1))
374 ip
->i_score
= ip
->i_kills
/ (double) ip
->i_entries
;
377 ip
= (IDENT
*) malloc(sizeof (IDENT
));
379 /* Fourth down, time to punt */
382 ip
->i_machine
= machine
;
385 strncpy(ip
->i_name
, name
, NAMELEN
);
396 ip
->i_gkills
= ip
->i_bkills
= ip
->i_deaths
= 0;
397 ip
->i_stillb
= ip
->i_saved
= 0;