]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/answer.c
1 /* $NetBSD: answer.c,v 1.3 1997/10/10 16:32:50 lukem 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.3 1997/10/10 16:32:50 lukem 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
;
42 socklen
= sizeof sockstruct
;
44 socklen
= sizeof sockstruct
- 1;
47 newsock
= accept(Socket
, (struct sockaddr
*) &sockstruct
, &socklen
);
53 syslog(LOG_ERR
, "accept: %m");
61 machine
= ntohl(((struct sockaddr_in
*) &sockstruct
)->sin_addr
.s_addr
);
64 machine
= gethostid();
66 version
= htonl((u_int32_t
) HUNT_VERSION
);
67 (void) write(newsock
, (char *) &version
, LONGLEN
);
68 (void) read(newsock
, (char *) &uid
, LONGLEN
);
69 uid
= ntohl((unsigned long) uid
);
70 (void) read(newsock
, name
, NAMELEN
);
71 (void) read(newsock
, &team
, 1);
72 (void) read(newsock
, (char *) &enter_status
, LONGLEN
);
73 enter_status
= ntohl((unsigned long) enter_status
);
74 (void) read(newsock
, Ttyname
, NAMELEN
);
75 (void) read(newsock
, (char *) &mode
, sizeof mode
);
79 * Turn off blocking I/O, so a slow or dead terminal won't stop
80 * the game. All subsequent reads check how many bytes they read.
82 flags
= fcntl(newsock
, F_GETFL
, 0);
84 (void) fcntl(newsock
, F_SETFL
, flags
);
87 * Make sure the name contains only printable characters
88 * since we use control characters for cursor control
89 * between driver and player processes
91 for (cp1
= cp2
= name
; *cp1
!= '\0'; cp1
++)
92 if (isprint(*cp1
) || *cp1
== ' ')
97 if (mode
== C_MESSAGE
) {
102 (void) sprintf(buf
, "%s: ", name
);
104 (void) sprintf(buf
, "%s[%c]: ", name
, team
);
106 for (pp
= Player
; pp
< End_player
; pp
++) {
107 cgoto(pp
, HEIGHT
, 0);
110 while ((n
= read(newsock
, buf
, BUFSIZ
)) > 0)
111 for (pp
= Player
; pp
< End_player
; pp
++)
113 for (pp
= Player
; pp
< End_player
; pp
++) {
115 sendcom(pp
, REFRESH
);
116 sendcom(pp
, READY
, 0);
117 (void) fflush(pp
->p_output
);
119 (void) close(newsock
);
125 if (mode
== C_MONITOR
)
126 if (End_monitor
< &Monitor
[MAXMON
])
130 (void) write(newsock
, (char *) &socklen
,
132 (void) close(newsock
);
137 if (End_player
< &Player
[MAXPL
])
141 (void) write(newsock
, (char *) &socklen
,
143 (void) close(newsock
);
148 if (mode
== C_MONITOR
&& team
== ' ')
151 pp
->p_ident
= get_ident(machine
, uid
, name
, team
);
152 pp
->p_output
= fdopen(newsock
, "w");
153 pp
->p_death
[0] = '\0';
155 FD_SET(pp
->p_fd
, &Fds_mask
);
156 if (pp
->p_fd
>= Num_fds
)
157 Num_fds
= pp
->p_fd
+ 1;
163 if (mode
== C_MONITOR
)
167 stplayer(pp
, enter_status
);
179 memcpy(pp
->p_maze
, Maze
, sizeof Maze
);
183 (void) sprintf(Buf
, "%5.5s%c%-10.10s %c", " ", stat_char(pp
),
184 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
185 line
= STAT_MON_ROW
+ 1 + (pp
- Monitor
);
186 for (npp
= Player
; npp
< End_player
; npp
++) {
187 cgoto(npp
, line
, STAT_NAME_COL
);
188 outstr(npp
, Buf
, STAT_NAME_LEN
);
190 for (npp
= Monitor
; npp
< End_monitor
; npp
++) {
191 cgoto(npp
, line
, STAT_NAME_COL
);
192 outstr(npp
, Buf
, STAT_NAME_LEN
);
195 sendcom(pp
, REFRESH
);
196 sendcom(pp
, READY
, 0);
197 (void) fflush(pp
->p_output
);
202 stplayer(newpp
, enter_status
)
211 for (y
= 0; y
< UBOUND
; y
++)
212 for (x
= 0; x
< WIDTH
; x
++)
213 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
214 for ( ; y
< DBOUND
; y
++) {
215 for (x
= 0; x
< LBOUND
; x
++)
216 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
217 for ( ; x
< RBOUND
; x
++)
218 newpp
->p_maze
[y
][x
] = SPACE
;
219 for ( ; x
< WIDTH
; x
++)
220 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
222 for ( ; y
< HEIGHT
; y
++)
223 for (x
= 0; x
< WIDTH
; x
++)
224 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
227 x
= rand_num(WIDTH
- 1) + 1;
228 y
= rand_num(HEIGHT
- 1) + 1;
229 } while (Maze
[y
][x
] != SPACE
);
230 newpp
->p_over
= SPACE
;
233 newpp
->p_undershot
= FALSE
;
236 if (enter_status
== Q_FLY
) {
237 newpp
->p_flying
= rand_num(20);
238 newpp
->p_flyx
= 2 * rand_num(6) - 5;
239 newpp
->p_flyy
= 2 * rand_num(6) - 5;
240 newpp
->p_face
= FLYER
;
245 newpp
->p_flying
= -1;
246 newpp
->p_face
= rand_dir();
249 newpp
->p_damcap
= MAXDAM
;
253 newpp
->p_ammo
= ISHOTS
;
257 if (enter_status
== Q_SCAN
) {
258 newpp
->p_scan
= SCANLEN
;
263 newpp
->p_cloak
= CLOAKLEN
;
268 x
= rand_num(WIDTH
- 1) + 1;
269 y
= rand_num(HEIGHT
- 1) + 1;
270 } while (Maze
[y
][x
] != SPACE
);
273 for (pp
= Monitor
; pp
< End_monitor
; pp
++)
278 x
= rand_num(WIDTH
- 1) + 1;
279 y
= rand_num(HEIGHT
- 1) + 1;
280 } while (Maze
[y
][x
] != SPACE
);
283 for (pp
= Monitor
; pp
< End_monitor
; pp
++)
287 (void) sprintf(Buf
, "%5.2f%c%-10.10s %c", newpp
->p_ident
->i_score
,
288 stat_char(newpp
), newpp
->p_ident
->i_name
,
289 newpp
->p_ident
->i_team
);
290 y
= STAT_PLAY_ROW
+ 1 + (newpp
- Player
);
291 for (pp
= Player
; pp
< End_player
; pp
++) {
295 pp
->p_ammo
+= NSHOTS
;
296 newpp
->p_ammo
+= NSHOTS
;
297 cgoto(pp
, y
, STAT_NAME_COL
);
298 outstr(pp
, Buf
, STAT_NAME_LEN
);
299 (void) sprintf(smallbuf
, "%3d", pp
->p_ammo
);
300 cgoto(pp
, STAT_AMMO_ROW
, STAT_VALUE_COL
);
301 outstr(pp
, smallbuf
, 3);
305 for (pp
= Monitor
; pp
< End_monitor
; pp
++) {
306 cgoto(pp
, y
, STAT_NAME_COL
);
307 outstr(pp
, Buf
, STAT_NAME_LEN
);
312 drawplayer(newpp
, TRUE
);
315 if (enter_status
== Q_FLY
)
316 /* Make sure that the position you enter in will be erased */
317 showexpl(newpp
->p_y
, newpp
->p_x
, FLYER
);
319 sendcom(newpp
, REFRESH
);
320 sendcom(newpp
, READY
, 0);
321 (void) fflush(newpp
->p_output
);
326 * Return a random direction
331 switch (rand_num(4)) {
347 * Get the score structure of a player
350 get_ident(machine
, uid
, name
, team
)
359 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
)
360 if (ip
->i_machine
== machine
362 && ip
->i_team
== team
363 && strncmp(ip
->i_name
, name
, NAMELEN
) == 0)
367 if (ip
->i_entries
< SCOREDECAY
)
370 ip
->i_kills
= (ip
->i_kills
* (SCOREDECAY
- 1))
372 ip
->i_score
= ip
->i_kills
/ (double) ip
->i_entries
;
375 ip
= (IDENT
*) malloc(sizeof (IDENT
));
377 /* Fourth down, time to punt */
380 ip
->i_machine
= machine
;
383 strncpy(ip
->i_name
, name
, NAMELEN
);
394 ip
->i_gkills
= ip
->i_bkills
= ip
->i_deaths
= 0;
395 ip
->i_stillb
= ip
->i_saved
= 0;