]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hunt/huntd/answer.c
3 * Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
4 * San Francisco, California
12 # define SCOREDECAY 15
14 static char Ttyname
[NAMELEN
];
21 static char name
[NAMELEN
];
23 static int enter_status
;
25 static u_long machine
;
27 static SOCKET sockstruct
;
28 register char *cp1
, *cp2
;
33 socklen
= sizeof sockstruct
;
35 socklen
= sizeof sockstruct
- 1;
38 newsock
= accept(Socket
, (struct sockaddr
*) &sockstruct
, &socklen
);
44 syslog(LOG_ERR
, "accept: %m");
52 machine
= ntohl(((struct sockaddr_in
*) &sockstruct
)->sin_addr
.s_addr
);
55 machine
= gethostid();
57 version
= htonl((u_int32_t
) HUNT_VERSION
);
58 (void) write(newsock
, (char *) &version
, LONGLEN
);
59 (void) read(newsock
, (char *) &uid
, LONGLEN
);
60 uid
= ntohl((unsigned long) uid
);
61 (void) read(newsock
, name
, NAMELEN
);
62 (void) read(newsock
, &team
, 1);
63 (void) read(newsock
, (char *) &enter_status
, LONGLEN
);
64 enter_status
= ntohl((unsigned long) enter_status
);
65 (void) read(newsock
, Ttyname
, NAMELEN
);
66 (void) read(newsock
, (char *) &mode
, sizeof mode
);
70 * Turn off blocking I/O, so a slow or dead terminal won't stop
71 * the game. All subsequent reads check how many bytes they read.
73 flags
= fcntl(newsock
, F_GETFL
, 0);
75 (void) fcntl(newsock
, F_SETFL
, flags
);
78 * Make sure the name contains only printable characters
79 * since we use control characters for cursor control
80 * between driver and player processes
82 for (cp1
= cp2
= name
; *cp1
!= '\0'; cp1
++)
83 if (isprint(*cp1
) || *cp1
== ' ')
88 if (mode
== C_MESSAGE
) {
93 (void) sprintf(buf
, "%s: ", name
);
95 (void) sprintf(buf
, "%s[%c]: ", name
, team
);
97 for (pp
= Player
; pp
< End_player
; pp
++) {
101 while ((n
= read(newsock
, buf
, BUFSIZ
)) > 0)
102 for (pp
= Player
; pp
< End_player
; pp
++)
104 for (pp
= Player
; pp
< End_player
; pp
++) {
106 sendcom(pp
, REFRESH
);
107 sendcom(pp
, READY
, 0);
108 (void) fflush(pp
->p_output
);
110 (void) close(newsock
);
116 if (mode
== C_MONITOR
)
117 if (End_monitor
< &Monitor
[MAXMON
])
121 (void) write(newsock
, (char *) &socklen
,
123 (void) close(newsock
);
128 if (End_player
< &Player
[MAXPL
])
132 (void) write(newsock
, (char *) &socklen
,
134 (void) close(newsock
);
139 if (mode
== C_MONITOR
&& team
== ' ')
142 pp
->p_ident
= get_ident(machine
, uid
, name
, team
);
143 pp
->p_output
= fdopen(newsock
, "w");
144 pp
->p_death
[0] = '\0';
146 pp
->p_mask
= (1 << pp
->p_fd
);
147 Fds_mask
|= pp
->p_mask
;
148 if (pp
->p_fd
>= Num_fds
)
149 Num_fds
= pp
->p_fd
+ 1;
155 if (mode
== C_MONITOR
)
159 stplayer(pp
, enter_status
);
168 register PLAYER
*npp
;
170 memcpy(pp
->p_maze
, Maze
, sizeof Maze
);
174 (void) sprintf(Buf
, "%5.5s%c%-10.10s %c", " ", stat_char(pp
),
175 pp
->p_ident
->i_name
, pp
->p_ident
->i_team
);
176 line
= STAT_MON_ROW
+ 1 + (pp
- Monitor
);
177 for (npp
= Player
; npp
< End_player
; npp
++) {
178 cgoto(npp
, line
, STAT_NAME_COL
);
179 outstr(npp
, Buf
, STAT_NAME_LEN
);
181 for (npp
= Monitor
; npp
< End_monitor
; npp
++) {
182 cgoto(npp
, line
, STAT_NAME_COL
);
183 outstr(npp
, Buf
, STAT_NAME_LEN
);
186 sendcom(pp
, REFRESH
);
187 sendcom(pp
, READY
, 0);
188 (void) fflush(pp
->p_output
);
192 stplayer(newpp
, enter_status
)
193 register PLAYER
*newpp
;
201 for (y
= 0; y
< UBOUND
; y
++)
202 for (x
= 0; x
< WIDTH
; x
++)
203 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
204 for ( ; y
< DBOUND
; y
++) {
205 for (x
= 0; x
< LBOUND
; x
++)
206 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
207 for ( ; x
< RBOUND
; x
++)
208 newpp
->p_maze
[y
][x
] = SPACE
;
209 for ( ; x
< WIDTH
; x
++)
210 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
212 for ( ; y
< HEIGHT
; y
++)
213 for (x
= 0; x
< WIDTH
; x
++)
214 newpp
->p_maze
[y
][x
] = Maze
[y
][x
];
217 x
= rand_num(WIDTH
- 1) + 1;
218 y
= rand_num(HEIGHT
- 1) + 1;
219 } while (Maze
[y
][x
] != SPACE
);
220 newpp
->p_over
= SPACE
;
223 newpp
->p_undershot
= FALSE
;
226 if (enter_status
== Q_FLY
) {
227 newpp
->p_flying
= rand_num(20);
228 newpp
->p_flyx
= 2 * rand_num(6) - 5;
229 newpp
->p_flyy
= 2 * rand_num(6) - 5;
230 newpp
->p_face
= FLYER
;
235 newpp
->p_flying
= -1;
236 newpp
->p_face
= rand_dir();
239 newpp
->p_damcap
= MAXDAM
;
243 newpp
->p_ammo
= ISHOTS
;
247 if (enter_status
== Q_SCAN
) {
248 newpp
->p_scan
= SCANLEN
;
253 newpp
->p_cloak
= CLOAKLEN
;
258 x
= rand_num(WIDTH
- 1) + 1;
259 y
= rand_num(HEIGHT
- 1) + 1;
260 } while (Maze
[y
][x
] != SPACE
);
263 for (pp
= Monitor
; pp
< End_monitor
; pp
++)
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
++)
277 (void) sprintf(Buf
, "%5.2f%c%-10.10s %c", newpp
->p_ident
->i_score
,
278 stat_char(newpp
), newpp
->p_ident
->i_name
,
279 newpp
->p_ident
->i_team
);
280 y
= STAT_PLAY_ROW
+ 1 + (newpp
- Player
);
281 for (pp
= Player
; pp
< End_player
; pp
++) {
285 pp
->p_ammo
+= NSHOTS
;
286 newpp
->p_ammo
+= NSHOTS
;
287 cgoto(pp
, y
, STAT_NAME_COL
);
288 outstr(pp
, Buf
, STAT_NAME_LEN
);
289 (void) sprintf(smallbuf
, "%3d", pp
->p_ammo
);
290 cgoto(pp
, STAT_AMMO_ROW
, STAT_VALUE_COL
);
291 outstr(pp
, smallbuf
, 3);
295 for (pp
= Monitor
; pp
< End_monitor
; pp
++) {
296 cgoto(pp
, y
, STAT_NAME_COL
);
297 outstr(pp
, Buf
, STAT_NAME_LEN
);
302 drawplayer(newpp
, TRUE
);
305 if (enter_status
== Q_FLY
)
306 /* Make sure that the position you enter in will be erased */
307 showexpl(newpp
->p_y
, newpp
->p_x
, FLYER
);
309 sendcom(newpp
, REFRESH
);
310 sendcom(newpp
, READY
, 0);
311 (void) fflush(newpp
->p_output
);
316 * Return a random direction
320 switch (rand_num(4)) {
335 * Get the score structure of a player
338 get_ident(machine
, uid
, name
, team
)
347 for (ip
= Scores
; ip
!= NULL
; ip
= ip
->i_next
)
348 if (ip
->i_machine
== machine
350 && ip
->i_team
== team
351 && strncmp(ip
->i_name
, name
, NAMELEN
) == 0)
355 if (ip
->i_entries
< SCOREDECAY
)
358 ip
->i_kills
= (ip
->i_kills
* (SCOREDECAY
- 1))
360 ip
->i_score
= ip
->i_kills
/ (double) ip
->i_entries
;
363 ip
= (IDENT
*) malloc(sizeof (IDENT
));
365 /* Fourth down, time to punt */
368 ip
->i_machine
= machine
;
371 strncpy(ip
->i_name
, name
, NAMELEN
);
382 ip
->i_gkills
= ip
->i_bkills
= ip
->i_deaths
= 0;
383 ip
->i_stillb
= ip
->i_saved
= 0;