answer.c revision 1.16.6.1 1 1.16.6.1 yamt /* $NetBSD: answer.c,v 1.16.6.1 2014/05/22 11:36:23 yamt Exp $ */
2 1.1 mrg /*
3 1.6 wiz * Copyright (c) 1983-2003, Regents of the University of California.
4 1.6 wiz * All rights reserved.
5 1.6 wiz *
6 1.6 wiz * Redistribution and use in source and binary forms, with or without
7 1.6 wiz * modification, are permitted provided that the following conditions are
8 1.6 wiz * met:
9 1.6 wiz *
10 1.6 wiz * + Redistributions of source code must retain the above copyright
11 1.6 wiz * notice, this list of conditions and the following disclaimer.
12 1.6 wiz * + Redistributions in binary form must reproduce the above copyright
13 1.6 wiz * notice, this list of conditions and the following disclaimer in the
14 1.6 wiz * documentation and/or other materials provided with the distribution.
15 1.6 wiz * + Neither the name of the University of California, San Francisco nor
16 1.6 wiz * the names of its contributors may be used to endorse or promote
17 1.6 wiz * products derived from this software without specific prior written
18 1.6 wiz * permission.
19 1.6 wiz *
20 1.6 wiz * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 1.6 wiz * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.6 wiz * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 1.6 wiz * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 1.6 wiz * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 1.6 wiz * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 1.6 wiz * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.6 wiz * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.6 wiz * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.6 wiz * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 1.6 wiz * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 mrg */
32 1.1 mrg
33 1.3 lukem #include <sys/cdefs.h>
34 1.3 lukem #ifndef lint
35 1.16.6.1 yamt __RCSID("$NetBSD: answer.c,v 1.16.6.1 2014/05/22 11:36:23 yamt Exp $");
36 1.3 lukem #endif /* not lint */
37 1.3 lukem
38 1.16.6.1 yamt #include <sys/types.h>
39 1.16.6.1 yamt #include <sys/socket.h>
40 1.16.6.1 yamt #include <netinet/in.h>
41 1.14 dholland #include <ctype.h>
42 1.14 dholland #include <errno.h>
43 1.14 dholland #include <fcntl.h>
44 1.14 dholland #include <stdlib.h>
45 1.14 dholland #include <unistd.h>
46 1.16.6.1 yamt #include <assert.h>
47 1.14 dholland #include "hunt.h"
48 1.1 mrg
49 1.14 dholland #define SCOREDECAY 15
50 1.1 mrg
51 1.16.6.1 yamt static char Ttyname[WIRE_NAMELEN];
52 1.1 mrg
53 1.16.6.1 yamt static IDENT *get_ident(uint32_t, uint32_t, const char *, char);
54 1.15 dholland static void stmonitor(PLAYER *);
55 1.15 dholland static void stplayer(PLAYER *, int);
56 1.15 dholland
57 1.16.6.1 yamt bool
58 1.13 dholland answer(void)
59 1.1 mrg {
60 1.14 dholland PLAYER *pp;
61 1.14 dholland int newsock;
62 1.16.6.1 yamt static uint32_t mode;
63 1.16.6.1 yamt static char name[WIRE_NAMELEN];
64 1.14 dholland static char team;
65 1.16.6.1 yamt static int32_t enter_status;
66 1.14 dholland static socklen_t socklen;
67 1.14 dholland static uint32_t machine;
68 1.14 dholland static uint32_t uid;
69 1.16.6.1 yamt static struct sockaddr_storage newaddr;
70 1.14 dholland char *cp1, *cp2;
71 1.14 dholland int flags;
72 1.16 dholland uint32_t version;
73 1.14 dholland int i;
74 1.1 mrg
75 1.16.6.1 yamt socklen = sizeof(newaddr);
76 1.16.6.1 yamt newsock = accept(huntsock, (struct sockaddr *)&newaddr, &socklen);
77 1.16.6.1 yamt if (newsock < 0) {
78 1.1 mrg if (errno == EINTR)
79 1.16.6.1 yamt return false;
80 1.16.6.1 yamt complain(LOG_ERR, "accept");
81 1.1 mrg cleanup(1);
82 1.1 mrg }
83 1.1 mrg
84 1.16.6.1 yamt /*
85 1.16.6.1 yamt * XXX this is pretty bollocks
86 1.16.6.1 yamt */
87 1.16.6.1 yamt switch (newaddr.ss_family) {
88 1.16.6.1 yamt case AF_INET:
89 1.16.6.1 yamt machine = ((struct sockaddr_in *)&newaddr)->sin_addr.s_addr;
90 1.16.6.1 yamt machine = ntohl(machine);
91 1.16.6.1 yamt break;
92 1.16.6.1 yamt case AF_INET6:
93 1.16.6.1 yamt {
94 1.16.6.1 yamt struct sockaddr_in6 *sin6;
95 1.16.6.1 yamt
96 1.16.6.1 yamt sin6 = (struct sockaddr_in6 *)&newaddr;
97 1.16.6.1 yamt assert(sizeof(sin6->sin6_addr.s6_addr) >
98 1.16.6.1 yamt sizeof(machine));
99 1.16.6.1 yamt memcpy(&machine, sin6->sin6_addr.s6_addr,
100 1.16.6.1 yamt sizeof(machine));
101 1.16.6.1 yamt }
102 1.16.6.1 yamt break;
103 1.16.6.1 yamt case AF_UNIX:
104 1.1 mrg machine = gethostid();
105 1.16.6.1 yamt break;
106 1.16.6.1 yamt default:
107 1.16.6.1 yamt machine = 0; /* ? */
108 1.16.6.1 yamt break;
109 1.16.6.1 yamt }
110 1.16.6.1 yamt
111 1.16 dholland version = htonl((uint32_t) HUNT_VERSION);
112 1.16.6.1 yamt (void) write(newsock, &version, sizeof(version));
113 1.16.6.1 yamt (void) read(newsock, &uid, sizeof(uid));
114 1.9 dholland uid = ntohl(uid);
115 1.16.6.1 yamt (void) read(newsock, name, sizeof(name));
116 1.1 mrg (void) read(newsock, &team, 1);
117 1.16.6.1 yamt (void) read(newsock, &enter_status, sizeof(enter_status));
118 1.16.6.1 yamt enter_status = ntohl(enter_status);
119 1.16.6.1 yamt (void) read(newsock, Ttyname, sizeof(Ttyname));
120 1.16.6.1 yamt (void) read(newsock, &mode, sizeof(mode));
121 1.1 mrg mode = ntohl(mode);
122 1.1 mrg
123 1.1 mrg /*
124 1.10 dholland * Ensure null termination.
125 1.10 dholland */
126 1.10 dholland name[sizeof(name)-1] = '\0';
127 1.10 dholland Ttyname[sizeof(Ttyname)-1] = '\0';
128 1.10 dholland
129 1.10 dholland /*
130 1.1 mrg * Turn off blocking I/O, so a slow or dead terminal won't stop
131 1.1 mrg * the game. All subsequent reads check how many bytes they read.
132 1.1 mrg */
133 1.1 mrg flags = fcntl(newsock, F_GETFL, 0);
134 1.1 mrg flags |= O_NDELAY;
135 1.1 mrg (void) fcntl(newsock, F_SETFL, flags);
136 1.1 mrg
137 1.1 mrg /*
138 1.1 mrg * Make sure the name contains only printable characters
139 1.1 mrg * since we use control characters for cursor control
140 1.1 mrg * between driver and player processes
141 1.1 mrg */
142 1.1 mrg for (cp1 = cp2 = name; *cp1 != '\0'; cp1++)
143 1.7 dsl if (isprint((unsigned char)*cp1) || *cp1 == ' ')
144 1.1 mrg *cp2++ = *cp1;
145 1.1 mrg *cp2 = '\0';
146 1.1 mrg
147 1.1 mrg if (mode == C_MESSAGE) {
148 1.1 mrg char buf[BUFSIZ + 1];
149 1.1 mrg int n;
150 1.1 mrg
151 1.1 mrg if (team == ' ')
152 1.11 dholland (void) snprintf(buf, sizeof(buf), "%s: ", name);
153 1.1 mrg else
154 1.11 dholland (void) snprintf(buf, sizeof(buf), "%s[%c]: ", name,
155 1.11 dholland team);
156 1.1 mrg n = strlen(buf);
157 1.1 mrg for (pp = Player; pp < End_player; pp++) {
158 1.1 mrg cgoto(pp, HEIGHT, 0);
159 1.1 mrg outstr(pp, buf, n);
160 1.1 mrg }
161 1.1 mrg while ((n = read(newsock, buf, BUFSIZ)) > 0)
162 1.1 mrg for (pp = Player; pp < End_player; pp++)
163 1.1 mrg outstr(pp, buf, n);
164 1.1 mrg for (pp = Player; pp < End_player; pp++) {
165 1.1 mrg ce(pp);
166 1.1 mrg sendcom(pp, REFRESH);
167 1.1 mrg sendcom(pp, READY, 0);
168 1.1 mrg (void) fflush(pp->p_output);
169 1.1 mrg }
170 1.1 mrg (void) close(newsock);
171 1.16.6.1 yamt return false;
172 1.1 mrg }
173 1.1 mrg else
174 1.14 dholland #ifdef MONITOR
175 1.1 mrg if (mode == C_MONITOR)
176 1.4 mycroft if (End_monitor < &Monitor[MAXMON]) {
177 1.1 mrg pp = End_monitor++;
178 1.4 mycroft i = pp - Monitor + MAXPL + 3;
179 1.4 mycroft } else {
180 1.1 mrg socklen = 0;
181 1.12 dholland (void) write(newsock, &socklen,
182 1.1 mrg sizeof socklen);
183 1.1 mrg (void) close(newsock);
184 1.16.6.1 yamt return false;
185 1.1 mrg }
186 1.1 mrg else
187 1.14 dholland #endif
188 1.4 mycroft if (End_player < &Player[MAXPL]) {
189 1.1 mrg pp = End_player++;
190 1.4 mycroft i = pp - Player + 3;
191 1.4 mycroft } else {
192 1.1 mrg socklen = 0;
193 1.12 dholland (void) write(newsock, &socklen,
194 1.1 mrg sizeof socklen);
195 1.1 mrg (void) close(newsock);
196 1.16.6.1 yamt return false;
197 1.1 mrg }
198 1.1 mrg
199 1.1 mrg #ifdef MONITOR
200 1.1 mrg if (mode == C_MONITOR && team == ' ')
201 1.1 mrg team = '*';
202 1.1 mrg #endif
203 1.1 mrg pp->p_ident = get_ident(machine, uid, name, team);
204 1.1 mrg pp->p_output = fdopen(newsock, "w");
205 1.1 mrg pp->p_death[0] = '\0';
206 1.1 mrg pp->p_fd = newsock;
207 1.4 mycroft fdset[i].fd = newsock;
208 1.4 mycroft fdset[i].events = POLLIN;
209 1.1 mrg
210 1.1 mrg pp->p_y = 0;
211 1.1 mrg pp->p_x = 0;
212 1.1 mrg
213 1.14 dholland #ifdef MONITOR
214 1.1 mrg if (mode == C_MONITOR)
215 1.1 mrg stmonitor(pp);
216 1.1 mrg else
217 1.14 dholland #endif
218 1.1 mrg stplayer(pp, enter_status);
219 1.16.6.1 yamt return true;
220 1.1 mrg }
221 1.1 mrg
222 1.14 dholland #ifdef MONITOR
223 1.15 dholland static void
224 1.13 dholland stmonitor(PLAYER *pp)
225 1.1 mrg {
226 1.14 dholland int line;
227 1.14 dholland PLAYER *npp;
228 1.1 mrg
229 1.1 mrg memcpy(pp->p_maze, Maze, sizeof Maze);
230 1.1 mrg
231 1.1 mrg drawmaze(pp);
232 1.1 mrg
233 1.11 dholland (void) snprintf(Buf, sizeof(Buf), "%5.5s%c%-10.10s %c", " ",
234 1.11 dholland stat_char(pp),
235 1.1 mrg pp->p_ident->i_name, pp->p_ident->i_team);
236 1.1 mrg line = STAT_MON_ROW + 1 + (pp - Monitor);
237 1.1 mrg for (npp = Player; npp < End_player; npp++) {
238 1.1 mrg cgoto(npp, line, STAT_NAME_COL);
239 1.1 mrg outstr(npp, Buf, STAT_NAME_LEN);
240 1.1 mrg }
241 1.1 mrg for (npp = Monitor; npp < End_monitor; npp++) {
242 1.1 mrg cgoto(npp, line, STAT_NAME_COL);
243 1.1 mrg outstr(npp, Buf, STAT_NAME_LEN);
244 1.1 mrg }
245 1.1 mrg
246 1.1 mrg sendcom(pp, REFRESH);
247 1.1 mrg sendcom(pp, READY, 0);
248 1.1 mrg (void) fflush(pp->p_output);
249 1.1 mrg }
250 1.14 dholland #endif
251 1.1 mrg
252 1.15 dholland static void
253 1.13 dholland stplayer(PLAYER *newpp, int enter_status)
254 1.1 mrg {
255 1.14 dholland int x, y;
256 1.14 dholland PLAYER *pp;
257 1.1 mrg
258 1.1 mrg Nplayer++;
259 1.1 mrg
260 1.1 mrg for (y = 0; y < UBOUND; y++)
261 1.1 mrg for (x = 0; x < WIDTH; x++)
262 1.1 mrg newpp->p_maze[y][x] = Maze[y][x];
263 1.1 mrg for ( ; y < DBOUND; y++) {
264 1.1 mrg for (x = 0; x < LBOUND; x++)
265 1.1 mrg newpp->p_maze[y][x] = Maze[y][x];
266 1.1 mrg for ( ; x < RBOUND; x++)
267 1.1 mrg newpp->p_maze[y][x] = SPACE;
268 1.1 mrg for ( ; x < WIDTH; x++)
269 1.1 mrg newpp->p_maze[y][x] = Maze[y][x];
270 1.1 mrg }
271 1.1 mrg for ( ; y < HEIGHT; y++)
272 1.1 mrg for (x = 0; x < WIDTH; x++)
273 1.1 mrg newpp->p_maze[y][x] = Maze[y][x];
274 1.1 mrg
275 1.1 mrg do {
276 1.1 mrg x = rand_num(WIDTH - 1) + 1;
277 1.1 mrg y = rand_num(HEIGHT - 1) + 1;
278 1.1 mrg } while (Maze[y][x] != SPACE);
279 1.1 mrg newpp->p_over = SPACE;
280 1.1 mrg newpp->p_x = x;
281 1.1 mrg newpp->p_y = y;
282 1.16.6.1 yamt newpp->p_undershot = false;
283 1.1 mrg
284 1.14 dholland #ifdef FLY
285 1.1 mrg if (enter_status == Q_FLY) {
286 1.1 mrg newpp->p_flying = rand_num(20);
287 1.1 mrg newpp->p_flyx = 2 * rand_num(6) - 5;
288 1.1 mrg newpp->p_flyy = 2 * rand_num(6) - 5;
289 1.1 mrg newpp->p_face = FLYER;
290 1.1 mrg }
291 1.1 mrg else
292 1.14 dholland #endif
293 1.1 mrg {
294 1.1 mrg newpp->p_flying = -1;
295 1.1 mrg newpp->p_face = rand_dir();
296 1.1 mrg }
297 1.1 mrg newpp->p_damage = 0;
298 1.1 mrg newpp->p_damcap = MAXDAM;
299 1.1 mrg newpp->p_nchar = 0;
300 1.1 mrg newpp->p_ncount = 0;
301 1.1 mrg newpp->p_nexec = 0;
302 1.1 mrg newpp->p_ammo = ISHOTS;
303 1.14 dholland #ifdef BOOTS
304 1.1 mrg newpp->p_nboots = 0;
305 1.14 dholland #endif
306 1.1 mrg if (enter_status == Q_SCAN) {
307 1.1 mrg newpp->p_scan = SCANLEN;
308 1.1 mrg newpp->p_cloak = 0;
309 1.1 mrg }
310 1.1 mrg else {
311 1.1 mrg newpp->p_scan = 0;
312 1.1 mrg newpp->p_cloak = CLOAKLEN;
313 1.1 mrg }
314 1.1 mrg newpp->p_ncshot = 0;
315 1.1 mrg
316 1.1 mrg do {
317 1.1 mrg x = rand_num(WIDTH - 1) + 1;
318 1.1 mrg y = rand_num(HEIGHT - 1) + 1;
319 1.1 mrg } while (Maze[y][x] != SPACE);
320 1.1 mrg Maze[y][x] = GMINE;
321 1.14 dholland #ifdef MONITOR
322 1.1 mrg for (pp = Monitor; pp < End_monitor; pp++)
323 1.1 mrg check(pp, y, x);
324 1.14 dholland #endif
325 1.1 mrg
326 1.1 mrg do {
327 1.1 mrg x = rand_num(WIDTH - 1) + 1;
328 1.1 mrg y = rand_num(HEIGHT - 1) + 1;
329 1.1 mrg } while (Maze[y][x] != SPACE);
330 1.1 mrg Maze[y][x] = MINE;
331 1.14 dholland #ifdef MONITOR
332 1.1 mrg for (pp = Monitor; pp < End_monitor; pp++)
333 1.1 mrg check(pp, y, x);
334 1.14 dholland #endif
335 1.1 mrg
336 1.11 dholland (void) snprintf(Buf, sizeof(Buf), "%5.2f%c%-10.10s %c",
337 1.11 dholland newpp->p_ident->i_score,
338 1.1 mrg stat_char(newpp), newpp->p_ident->i_name,
339 1.1 mrg newpp->p_ident->i_team);
340 1.1 mrg y = STAT_PLAY_ROW + 1 + (newpp - Player);
341 1.1 mrg for (pp = Player; pp < End_player; pp++) {
342 1.1 mrg if (pp != newpp) {
343 1.10 dholland char smallbuf[16];
344 1.1 mrg
345 1.1 mrg pp->p_ammo += NSHOTS;
346 1.1 mrg newpp->p_ammo += NSHOTS;
347 1.1 mrg cgoto(pp, y, STAT_NAME_COL);
348 1.1 mrg outstr(pp, Buf, STAT_NAME_LEN);
349 1.11 dholland (void) snprintf(smallbuf, sizeof(smallbuf),
350 1.11 dholland "%3d", pp->p_ammo);
351 1.1 mrg cgoto(pp, STAT_AMMO_ROW, STAT_VALUE_COL);
352 1.1 mrg outstr(pp, smallbuf, 3);
353 1.1 mrg }
354 1.1 mrg }
355 1.14 dholland #ifdef MONITOR
356 1.1 mrg for (pp = Monitor; pp < End_monitor; pp++) {
357 1.1 mrg cgoto(pp, y, STAT_NAME_COL);
358 1.1 mrg outstr(pp, Buf, STAT_NAME_LEN);
359 1.1 mrg }
360 1.14 dholland #endif
361 1.1 mrg
362 1.1 mrg drawmaze(newpp);
363 1.16.6.1 yamt drawplayer(newpp, true);
364 1.1 mrg look(newpp);
365 1.14 dholland #ifdef FLY
366 1.1 mrg if (enter_status == Q_FLY)
367 1.1 mrg /* Make sure that the position you enter in will be erased */
368 1.1 mrg showexpl(newpp->p_y, newpp->p_x, FLYER);
369 1.14 dholland #endif
370 1.1 mrg sendcom(newpp, REFRESH);
371 1.1 mrg sendcom(newpp, READY, 0);
372 1.1 mrg (void) fflush(newpp->p_output);
373 1.1 mrg }
374 1.1 mrg
375 1.1 mrg /*
376 1.1 mrg * rand_dir:
377 1.1 mrg * Return a random direction
378 1.1 mrg */
379 1.3 lukem int
380 1.13 dholland rand_dir(void)
381 1.1 mrg {
382 1.1 mrg switch (rand_num(4)) {
383 1.1 mrg case 0:
384 1.1 mrg return LEFTS;
385 1.1 mrg case 1:
386 1.1 mrg return RIGHT;
387 1.1 mrg case 2:
388 1.1 mrg return BELOW;
389 1.1 mrg case 3:
390 1.1 mrg return ABOVE;
391 1.1 mrg }
392 1.1 mrg /* NOTREACHED */
393 1.3 lukem return(-1);
394 1.1 mrg }
395 1.1 mrg
396 1.1 mrg /*
397 1.1 mrg * get_ident:
398 1.1 mrg * Get the score structure of a player
399 1.1 mrg */
400 1.15 dholland static IDENT *
401 1.16.6.1 yamt get_ident(uint32_t machine, uint32_t uid, const char *name, char team)
402 1.1 mrg {
403 1.14 dholland IDENT *ip;
404 1.14 dholland static IDENT punt;
405 1.1 mrg
406 1.1 mrg for (ip = Scores; ip != NULL; ip = ip->i_next)
407 1.1 mrg if (ip->i_machine == machine
408 1.1 mrg && ip->i_uid == uid
409 1.1 mrg && ip->i_team == team
410 1.16.6.1 yamt && strncmp(ip->i_name, name, WIRE_NAMELEN) == 0)
411 1.1 mrg break;
412 1.1 mrg
413 1.1 mrg if (ip != NULL) {
414 1.1 mrg if (ip->i_entries < SCOREDECAY)
415 1.1 mrg ip->i_entries++;
416 1.1 mrg else
417 1.1 mrg ip->i_kills = (ip->i_kills * (SCOREDECAY - 1))
418 1.1 mrg / SCOREDECAY;
419 1.1 mrg ip->i_score = ip->i_kills / (double) ip->i_entries;
420 1.1 mrg }
421 1.1 mrg else {
422 1.12 dholland ip = malloc(sizeof(*ip));
423 1.1 mrg if (ip == NULL) {
424 1.1 mrg /* Fourth down, time to punt */
425 1.1 mrg ip = &punt;
426 1.1 mrg }
427 1.1 mrg ip->i_machine = machine;
428 1.1 mrg ip->i_team = team;
429 1.1 mrg ip->i_uid = uid;
430 1.16.6.1 yamt strncpy(ip->i_name, name, sizeof(ip->i_name));
431 1.1 mrg ip->i_kills = 0;
432 1.1 mrg ip->i_entries = 1;
433 1.1 mrg ip->i_score = 0;
434 1.1 mrg ip->i_absorbed = 0;
435 1.1 mrg ip->i_faced = 0;
436 1.1 mrg ip->i_shot = 0;
437 1.1 mrg ip->i_robbed = 0;
438 1.1 mrg ip->i_slime = 0;
439 1.1 mrg ip->i_missed = 0;
440 1.1 mrg ip->i_ducked = 0;
441 1.1 mrg ip->i_gkills = ip->i_bkills = ip->i_deaths = 0;
442 1.1 mrg ip->i_stillb = ip->i_saved = 0;
443 1.1 mrg ip->i_next = Scores;
444 1.1 mrg Scores = ip;
445 1.1 mrg }
446 1.1 mrg
447 1.1 mrg return ip;
448 1.1 mrg }
449