answer.c revision 1.4 1 1.4 mycroft /* $NetBSD: answer.c,v 1.4 2002/09/20 20:54:16 mycroft Exp $ */
2 1.1 mrg /*
3 1.1 mrg * Hunt
4 1.1 mrg * Copyright (c) 1985 Conrad C. Huang, Gregory S. Couch, Kenneth C.R.C. Arnold
5 1.1 mrg * San Francisco, California
6 1.1 mrg */
7 1.1 mrg
8 1.3 lukem #include <sys/cdefs.h>
9 1.3 lukem #ifndef lint
10 1.4 mycroft __RCSID("$NetBSD: answer.c,v 1.4 2002/09/20 20:54:16 mycroft Exp $");
11 1.3 lukem #endif /* not lint */
12 1.3 lukem
13 1.1 mrg # include <ctype.h>
14 1.1 mrg # include <errno.h>
15 1.1 mrg # include <fcntl.h>
16 1.3 lukem # include <stdlib.h>
17 1.3 lukem # include <unistd.h>
18 1.3 lukem # include "hunt.h"
19 1.1 mrg
20 1.1 mrg # define SCOREDECAY 15
21 1.1 mrg
22 1.1 mrg static char Ttyname[NAMELEN];
23 1.1 mrg
24 1.3 lukem int
25 1.1 mrg answer()
26 1.1 mrg {
27 1.3 lukem PLAYER *pp;
28 1.3 lukem int newsock;
29 1.1 mrg static u_long mode;
30 1.1 mrg static char name[NAMELEN];
31 1.1 mrg static char team;
32 1.1 mrg static int enter_status;
33 1.1 mrg static int socklen;
34 1.1 mrg static u_long machine;
35 1.1 mrg static u_long uid;
36 1.1 mrg static SOCKET sockstruct;
37 1.3 lukem char *cp1, *cp2;
38 1.1 mrg int flags;
39 1.1 mrg long version;
40 1.4 mycroft int i;
41 1.1 mrg
42 1.1 mrg # ifdef INTERNET
43 1.1 mrg socklen = sizeof sockstruct;
44 1.1 mrg # else
45 1.1 mrg socklen = sizeof sockstruct - 1;
46 1.1 mrg # endif
47 1.1 mrg errno = 0;
48 1.1 mrg newsock = accept(Socket, (struct sockaddr *) &sockstruct, &socklen);
49 1.1 mrg if (newsock < 0)
50 1.1 mrg {
51 1.1 mrg if (errno == EINTR)
52 1.1 mrg return FALSE;
53 1.1 mrg # ifdef LOG
54 1.1 mrg syslog(LOG_ERR, "accept: %m");
55 1.1 mrg # else
56 1.1 mrg perror("accept");
57 1.1 mrg # endif
58 1.1 mrg cleanup(1);
59 1.1 mrg }
60 1.1 mrg
61 1.1 mrg # ifdef INTERNET
62 1.1 mrg machine = ntohl(((struct sockaddr_in *) &sockstruct)->sin_addr.s_addr);
63 1.1 mrg # else
64 1.1 mrg if (machine == 0)
65 1.1 mrg machine = gethostid();
66 1.1 mrg # endif
67 1.2 mrg version = htonl((u_int32_t) HUNT_VERSION);
68 1.1 mrg (void) write(newsock, (char *) &version, LONGLEN);
69 1.1 mrg (void) read(newsock, (char *) &uid, LONGLEN);
70 1.1 mrg uid = ntohl((unsigned long) uid);
71 1.1 mrg (void) read(newsock, name, NAMELEN);
72 1.1 mrg (void) read(newsock, &team, 1);
73 1.1 mrg (void) read(newsock, (char *) &enter_status, LONGLEN);
74 1.1 mrg enter_status = ntohl((unsigned long) enter_status);
75 1.1 mrg (void) read(newsock, Ttyname, NAMELEN);
76 1.1 mrg (void) read(newsock, (char *) &mode, sizeof mode);
77 1.1 mrg mode = ntohl(mode);
78 1.1 mrg
79 1.1 mrg /*
80 1.1 mrg * Turn off blocking I/O, so a slow or dead terminal won't stop
81 1.1 mrg * the game. All subsequent reads check how many bytes they read.
82 1.1 mrg */
83 1.1 mrg flags = fcntl(newsock, F_GETFL, 0);
84 1.1 mrg flags |= O_NDELAY;
85 1.1 mrg (void) fcntl(newsock, F_SETFL, flags);
86 1.1 mrg
87 1.1 mrg /*
88 1.1 mrg * Make sure the name contains only printable characters
89 1.1 mrg * since we use control characters for cursor control
90 1.1 mrg * between driver and player processes
91 1.1 mrg */
92 1.1 mrg for (cp1 = cp2 = name; *cp1 != '\0'; cp1++)
93 1.1 mrg if (isprint(*cp1) || *cp1 == ' ')
94 1.1 mrg *cp2++ = *cp1;
95 1.1 mrg *cp2 = '\0';
96 1.1 mrg
97 1.1 mrg # ifdef INTERNET
98 1.1 mrg if (mode == C_MESSAGE) {
99 1.1 mrg char buf[BUFSIZ + 1];
100 1.1 mrg int n;
101 1.1 mrg
102 1.1 mrg if (team == ' ')
103 1.1 mrg (void) sprintf(buf, "%s: ", name);
104 1.1 mrg else
105 1.1 mrg (void) sprintf(buf, "%s[%c]: ", name, team);
106 1.1 mrg n = strlen(buf);
107 1.1 mrg for (pp = Player; pp < End_player; pp++) {
108 1.1 mrg cgoto(pp, HEIGHT, 0);
109 1.1 mrg outstr(pp, buf, n);
110 1.1 mrg }
111 1.1 mrg while ((n = read(newsock, buf, BUFSIZ)) > 0)
112 1.1 mrg for (pp = Player; pp < End_player; pp++)
113 1.1 mrg outstr(pp, buf, n);
114 1.1 mrg for (pp = Player; pp < End_player; pp++) {
115 1.1 mrg ce(pp);
116 1.1 mrg sendcom(pp, REFRESH);
117 1.1 mrg sendcom(pp, READY, 0);
118 1.1 mrg (void) fflush(pp->p_output);
119 1.1 mrg }
120 1.1 mrg (void) close(newsock);
121 1.1 mrg return FALSE;
122 1.1 mrg }
123 1.1 mrg else
124 1.1 mrg # endif
125 1.1 mrg # ifdef MONITOR
126 1.1 mrg if (mode == C_MONITOR)
127 1.4 mycroft if (End_monitor < &Monitor[MAXMON]) {
128 1.1 mrg pp = End_monitor++;
129 1.4 mycroft i = pp - Monitor + MAXPL + 3;
130 1.4 mycroft } else {
131 1.1 mrg socklen = 0;
132 1.1 mrg (void) write(newsock, (char *) &socklen,
133 1.1 mrg sizeof socklen);
134 1.1 mrg (void) close(newsock);
135 1.1 mrg return FALSE;
136 1.1 mrg }
137 1.1 mrg else
138 1.1 mrg # endif
139 1.4 mycroft if (End_player < &Player[MAXPL]) {
140 1.1 mrg pp = End_player++;
141 1.4 mycroft i = pp - Player + 3;
142 1.4 mycroft } else {
143 1.1 mrg socklen = 0;
144 1.1 mrg (void) write(newsock, (char *) &socklen,
145 1.1 mrg sizeof socklen);
146 1.1 mrg (void) close(newsock);
147 1.1 mrg return FALSE;
148 1.1 mrg }
149 1.1 mrg
150 1.1 mrg #ifdef MONITOR
151 1.1 mrg if (mode == C_MONITOR && team == ' ')
152 1.1 mrg team = '*';
153 1.1 mrg #endif
154 1.1 mrg pp->p_ident = get_ident(machine, uid, name, team);
155 1.1 mrg pp->p_output = fdopen(newsock, "w");
156 1.1 mrg pp->p_death[0] = '\0';
157 1.1 mrg pp->p_fd = newsock;
158 1.4 mycroft fdset[i].fd = newsock;
159 1.4 mycroft fdset[i].events = POLLIN;
160 1.1 mrg
161 1.1 mrg pp->p_y = 0;
162 1.1 mrg pp->p_x = 0;
163 1.1 mrg
164 1.1 mrg # ifdef MONITOR
165 1.1 mrg if (mode == C_MONITOR)
166 1.1 mrg stmonitor(pp);
167 1.1 mrg else
168 1.1 mrg # endif
169 1.1 mrg stplayer(pp, enter_status);
170 1.1 mrg return TRUE;
171 1.1 mrg }
172 1.1 mrg
173 1.1 mrg # ifdef MONITOR
174 1.3 lukem void
175 1.1 mrg stmonitor(pp)
176 1.3 lukem PLAYER *pp;
177 1.1 mrg {
178 1.3 lukem int line;
179 1.3 lukem PLAYER *npp;
180 1.1 mrg
181 1.1 mrg memcpy(pp->p_maze, Maze, sizeof Maze);
182 1.1 mrg
183 1.1 mrg drawmaze(pp);
184 1.1 mrg
185 1.1 mrg (void) sprintf(Buf, "%5.5s%c%-10.10s %c", " ", stat_char(pp),
186 1.1 mrg pp->p_ident->i_name, pp->p_ident->i_team);
187 1.1 mrg line = STAT_MON_ROW + 1 + (pp - Monitor);
188 1.1 mrg for (npp = Player; npp < End_player; npp++) {
189 1.1 mrg cgoto(npp, line, STAT_NAME_COL);
190 1.1 mrg outstr(npp, Buf, STAT_NAME_LEN);
191 1.1 mrg }
192 1.1 mrg for (npp = Monitor; npp < End_monitor; npp++) {
193 1.1 mrg cgoto(npp, line, STAT_NAME_COL);
194 1.1 mrg outstr(npp, Buf, STAT_NAME_LEN);
195 1.1 mrg }
196 1.1 mrg
197 1.1 mrg sendcom(pp, REFRESH);
198 1.1 mrg sendcom(pp, READY, 0);
199 1.1 mrg (void) fflush(pp->p_output);
200 1.1 mrg }
201 1.1 mrg # endif
202 1.1 mrg
203 1.3 lukem void
204 1.1 mrg stplayer(newpp, enter_status)
205 1.3 lukem PLAYER *newpp;
206 1.3 lukem int enter_status;
207 1.1 mrg {
208 1.3 lukem int x, y;
209 1.3 lukem PLAYER *pp;
210 1.1 mrg
211 1.1 mrg Nplayer++;
212 1.1 mrg
213 1.1 mrg for (y = 0; y < UBOUND; y++)
214 1.1 mrg for (x = 0; x < WIDTH; x++)
215 1.1 mrg newpp->p_maze[y][x] = Maze[y][x];
216 1.1 mrg for ( ; y < DBOUND; y++) {
217 1.1 mrg for (x = 0; x < LBOUND; x++)
218 1.1 mrg newpp->p_maze[y][x] = Maze[y][x];
219 1.1 mrg for ( ; x < RBOUND; x++)
220 1.1 mrg newpp->p_maze[y][x] = SPACE;
221 1.1 mrg for ( ; x < WIDTH; x++)
222 1.1 mrg newpp->p_maze[y][x] = Maze[y][x];
223 1.1 mrg }
224 1.1 mrg for ( ; y < HEIGHT; y++)
225 1.1 mrg for (x = 0; x < WIDTH; x++)
226 1.1 mrg newpp->p_maze[y][x] = Maze[y][x];
227 1.1 mrg
228 1.1 mrg do {
229 1.1 mrg x = rand_num(WIDTH - 1) + 1;
230 1.1 mrg y = rand_num(HEIGHT - 1) + 1;
231 1.1 mrg } while (Maze[y][x] != SPACE);
232 1.1 mrg newpp->p_over = SPACE;
233 1.1 mrg newpp->p_x = x;
234 1.1 mrg newpp->p_y = y;
235 1.1 mrg newpp->p_undershot = FALSE;
236 1.1 mrg
237 1.1 mrg # ifdef FLY
238 1.1 mrg if (enter_status == Q_FLY) {
239 1.1 mrg newpp->p_flying = rand_num(20);
240 1.1 mrg newpp->p_flyx = 2 * rand_num(6) - 5;
241 1.1 mrg newpp->p_flyy = 2 * rand_num(6) - 5;
242 1.1 mrg newpp->p_face = FLYER;
243 1.1 mrg }
244 1.1 mrg else
245 1.1 mrg # endif
246 1.1 mrg {
247 1.1 mrg newpp->p_flying = -1;
248 1.1 mrg newpp->p_face = rand_dir();
249 1.1 mrg }
250 1.1 mrg newpp->p_damage = 0;
251 1.1 mrg newpp->p_damcap = MAXDAM;
252 1.1 mrg newpp->p_nchar = 0;
253 1.1 mrg newpp->p_ncount = 0;
254 1.1 mrg newpp->p_nexec = 0;
255 1.1 mrg newpp->p_ammo = ISHOTS;
256 1.1 mrg # ifdef BOOTS
257 1.1 mrg newpp->p_nboots = 0;
258 1.1 mrg # endif
259 1.1 mrg if (enter_status == Q_SCAN) {
260 1.1 mrg newpp->p_scan = SCANLEN;
261 1.1 mrg newpp->p_cloak = 0;
262 1.1 mrg }
263 1.1 mrg else {
264 1.1 mrg newpp->p_scan = 0;
265 1.1 mrg newpp->p_cloak = CLOAKLEN;
266 1.1 mrg }
267 1.1 mrg newpp->p_ncshot = 0;
268 1.1 mrg
269 1.1 mrg do {
270 1.1 mrg x = rand_num(WIDTH - 1) + 1;
271 1.1 mrg y = rand_num(HEIGHT - 1) + 1;
272 1.1 mrg } while (Maze[y][x] != SPACE);
273 1.1 mrg Maze[y][x] = GMINE;
274 1.1 mrg # ifdef MONITOR
275 1.1 mrg for (pp = Monitor; pp < End_monitor; pp++)
276 1.1 mrg check(pp, y, x);
277 1.1 mrg # endif
278 1.1 mrg
279 1.1 mrg do {
280 1.1 mrg x = rand_num(WIDTH - 1) + 1;
281 1.1 mrg y = rand_num(HEIGHT - 1) + 1;
282 1.1 mrg } while (Maze[y][x] != SPACE);
283 1.1 mrg Maze[y][x] = MINE;
284 1.1 mrg # ifdef MONITOR
285 1.1 mrg for (pp = Monitor; pp < End_monitor; pp++)
286 1.1 mrg check(pp, y, x);
287 1.1 mrg # endif
288 1.1 mrg
289 1.1 mrg (void) sprintf(Buf, "%5.2f%c%-10.10s %c", newpp->p_ident->i_score,
290 1.1 mrg stat_char(newpp), newpp->p_ident->i_name,
291 1.1 mrg newpp->p_ident->i_team);
292 1.1 mrg y = STAT_PLAY_ROW + 1 + (newpp - Player);
293 1.1 mrg for (pp = Player; pp < End_player; pp++) {
294 1.1 mrg if (pp != newpp) {
295 1.1 mrg char smallbuf[10];
296 1.1 mrg
297 1.1 mrg pp->p_ammo += NSHOTS;
298 1.1 mrg newpp->p_ammo += NSHOTS;
299 1.1 mrg cgoto(pp, y, STAT_NAME_COL);
300 1.1 mrg outstr(pp, Buf, STAT_NAME_LEN);
301 1.1 mrg (void) sprintf(smallbuf, "%3d", pp->p_ammo);
302 1.1 mrg cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
303 1.1 mrg outstr(pp, smallbuf, 3);
304 1.1 mrg }
305 1.1 mrg }
306 1.1 mrg # ifdef MONITOR
307 1.1 mrg for (pp = Monitor; pp < End_monitor; pp++) {
308 1.1 mrg cgoto(pp, y, STAT_NAME_COL);
309 1.1 mrg outstr(pp, Buf, STAT_NAME_LEN);
310 1.1 mrg }
311 1.1 mrg # endif
312 1.1 mrg
313 1.1 mrg drawmaze(newpp);
314 1.1 mrg drawplayer(newpp, TRUE);
315 1.1 mrg look(newpp);
316 1.1 mrg # ifdef FLY
317 1.1 mrg if (enter_status == Q_FLY)
318 1.1 mrg /* Make sure that the position you enter in will be erased */
319 1.1 mrg showexpl(newpp->p_y, newpp->p_x, FLYER);
320 1.1 mrg # endif
321 1.1 mrg sendcom(newpp, REFRESH);
322 1.1 mrg sendcom(newpp, READY, 0);
323 1.1 mrg (void) fflush(newpp->p_output);
324 1.1 mrg }
325 1.1 mrg
326 1.1 mrg /*
327 1.1 mrg * rand_dir:
328 1.1 mrg * Return a random direction
329 1.1 mrg */
330 1.3 lukem int
331 1.1 mrg rand_dir()
332 1.1 mrg {
333 1.1 mrg switch (rand_num(4)) {
334 1.1 mrg case 0:
335 1.1 mrg return LEFTS;
336 1.1 mrg case 1:
337 1.1 mrg return RIGHT;
338 1.1 mrg case 2:
339 1.1 mrg return BELOW;
340 1.1 mrg case 3:
341 1.1 mrg return ABOVE;
342 1.1 mrg }
343 1.1 mrg /* NOTREACHED */
344 1.3 lukem return(-1);
345 1.1 mrg }
346 1.1 mrg
347 1.1 mrg /*
348 1.1 mrg * get_ident:
349 1.1 mrg * Get the score structure of a player
350 1.1 mrg */
351 1.1 mrg IDENT *
352 1.1 mrg get_ident(machine, uid, name, team)
353 1.3 lukem u_long machine;
354 1.3 lukem u_long uid;
355 1.3 lukem char *name;
356 1.3 lukem char team;
357 1.1 mrg {
358 1.3 lukem IDENT *ip;
359 1.1 mrg static IDENT punt;
360 1.1 mrg
361 1.1 mrg for (ip = Scores; ip != NULL; ip = ip->i_next)
362 1.1 mrg if (ip->i_machine == machine
363 1.1 mrg && ip->i_uid == uid
364 1.1 mrg && ip->i_team == team
365 1.1 mrg && strncmp(ip->i_name, name, NAMELEN) == 0)
366 1.1 mrg break;
367 1.1 mrg
368 1.1 mrg if (ip != NULL) {
369 1.1 mrg if (ip->i_entries < SCOREDECAY)
370 1.1 mrg ip->i_entries++;
371 1.1 mrg else
372 1.1 mrg ip->i_kills = (ip->i_kills * (SCOREDECAY - 1))
373 1.1 mrg / SCOREDECAY;
374 1.1 mrg ip->i_score = ip->i_kills / (double) ip->i_entries;
375 1.1 mrg }
376 1.1 mrg else {
377 1.1 mrg ip = (IDENT *) malloc(sizeof (IDENT));
378 1.1 mrg if (ip == NULL) {
379 1.1 mrg /* Fourth down, time to punt */
380 1.1 mrg ip = &punt;
381 1.1 mrg }
382 1.1 mrg ip->i_machine = machine;
383 1.1 mrg ip->i_team = team;
384 1.1 mrg ip->i_uid = uid;
385 1.1 mrg strncpy(ip->i_name, name, NAMELEN);
386 1.1 mrg ip->i_kills = 0;
387 1.1 mrg ip->i_entries = 1;
388 1.1 mrg ip->i_score = 0;
389 1.1 mrg ip->i_absorbed = 0;
390 1.1 mrg ip->i_faced = 0;
391 1.1 mrg ip->i_shot = 0;
392 1.1 mrg ip->i_robbed = 0;
393 1.1 mrg ip->i_slime = 0;
394 1.1 mrg ip->i_missed = 0;
395 1.1 mrg ip->i_ducked = 0;
396 1.1 mrg ip->i_gkills = ip->i_bkills = ip->i_deaths = 0;
397 1.1 mrg ip->i_stillb = ip->i_saved = 0;
398 1.1 mrg ip->i_next = Scores;
399 1.1 mrg Scores = ip;
400 1.1 mrg }
401 1.1 mrg
402 1.1 mrg return ip;
403 1.1 mrg }
404